From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED!not-for-mail From: Alain Schneble Newsgroups: gmane.emacs.devel Subject: Re: SIGTRAP in kill emulation on Windows Date: Fri, 26 Aug 2016 12:19:18 +0200 Message-ID: <86r39bhly1.fsf@realize.ch> References: <86d1kwj37g.fsf@realize.ch> <83bn0gesab.fsf@gnu.org> <868tvkiplu.fsf@realize.ch> <83wpj4c4yz.fsf@gnu.org> <86zinzhpnx.fsf@realize.ch> <83shtrdhb0.fsf@gnu.org> <86vaynhnev.fsf@realize.ch> NNTP-Posting-Host: blaine.gmane.org Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" X-Trace: blaine.gmane.org 1472206848 13350 195.159.176.226 (26 Aug 2016 10:20:48 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Fri, 26 Aug 2016 10:20:48 +0000 (UTC) User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.4 (windows-nt) Cc: emacs-devel@gnu.org To: Eli Zaretskii Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Fri Aug 26 12:20:43 2016 Return-path: Envelope-to: ged-emacs-devel@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by blaine.gmane.org with esmtp (Exim 4.84_2) (envelope-from ) id 1bdEFf-000330-0c for ged-emacs-devel@m.gmane.org; Fri, 26 Aug 2016 12:20:43 +0200 Original-Received: from localhost ([::1]:59825 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bdEFc-0007pO-Ej for ged-emacs-devel@m.gmane.org; Fri, 26 Aug 2016 06:20:40 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:35728) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bdEEz-0007pI-MI for emacs-devel@gnu.org; Fri, 26 Aug 2016 06:20:03 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1bdEEy-0005H9-6m for emacs-devel@gnu.org; Fri, 26 Aug 2016 06:20:01 -0400 Original-Received: from clientmail.realize.ch ([46.140.89.53]:2691) by eggs.gnu.org with smtp (Exim 4.71) (envelope-from ) id 1bdEEu-0005G7-4K; Fri, 26 Aug 2016 06:19:56 -0400 Original-Received: from rintintin.hq.realize.ch.lan.rit ([192.168.0.105]) by clientmail.realize.ch ; Fri, 26 Aug 2016 12:19:53 +0200 Original-Received: from MYNGB (192.168.66.65) by rintintin.hq.realize.ch.lan.rit (192.168.0.105) with Microsoft SMTP Server (TLS) id 15.0.516.32; Fri, 26 Aug 2016 12:19:20 +0200 In-Reply-To: <86vaynhnev.fsf@realize.ch> (Alain Schneble's message of "Fri, 26 Aug 2016 11:47:36 +0200") X-ClientProxiedBy: rintintin.hq.realize.ch.lan.rit (192.168.0.105) To rintintin.hq.realize.ch.lan.rit (192.168.0.105) X-detected-operating-system: by eggs.gnu.org: Windows NT kernel [generic] X-Received-From: 46.140.89.53 X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Original-Sender: "Emacs-devel" Xref: news.gmane.org gmane.emacs.devel:206817 Archived-At: --=-=-= Content-Type: text/plain Alain Schneble writes: >>> But that's kind of irrelevant here I guess. Where would be the best >>> place to document this quirk (c, signal-process, info manual)? >> >> Actually, perhaps not even there. In NEWS might be enough, I think. > > Ok, I'll arrange a new patch with that added to the NEWS entry. Here is a new version of the patch with the additional comment in the NEWS entry. Thanks. Alain --=-=-= Content-Type: text/x-patch Content-Disposition: inline; filename="0001-Support-SIGTRAP-in-kill-emulation-on-Windows.patch" Content-Description: SIGTRAP in kill emulation on Windows >From e9943ab1846c1b021bbafd1cb1e4dbc2e5fff528 Mon Sep 17 00:00:00 2001 From: Alain Schneble Date: Fri, 26 Aug 2016 12:13:51 +0200 Subject: [PATCH] Support SIGTRAP in kill emulation on Windows * src/w32proc.c (sys_kill): Translate SIGTRAP signal into a call to DebugBreakProcess to cause a breakpoint exception to occur in the specified process. Windows versions prior to Windows XP that do not support DebugBreakProcess return -1 and errno set to ENOTSUP (as opposed to EINVAL before this change). * src/w32proc.c: Add typedef for DebugBreakProcess function pointer and global variable to track state of run-time dynamic linking of this function. * etc/NEWS: Add entry to document that 'signal-process' now supports SIGTRAP. --- etc/NEWS | 10 ++++++++++ src/w32.c | 2 ++ src/w32proc.c | 52 +++++++++++++++++++++++++++++++++++++++++++++++++--- 3 files changed, 61 insertions(+), 3 deletions(-) diff --git a/etc/NEWS b/etc/NEWS index 494a091..4b7647c 100644 --- a/etc/NEWS +++ b/etc/NEWS @@ -599,6 +599,16 @@ still apply.) Previously, on MS-Windows this function converted slash characters in file names into backslashes. It no longer does that. +--- +** 'signal-process' supports SIGTRAP on Windows XP and later. +The 'kill' emulation on Windows now maps SIGTRAP to a call to Win32 +'DebugBreakProcess'. This causes the receiving process to break +execution and return control to the debugger. If no debugger is +attached to the receiving process, the call is typically ignored. +This is in contrast to the default action on POSIX Systems, where it +causes the receiving process to terminate with a core dump if no +debugger has been attached to it. + * Installation Changes in Emacs 25.1 diff --git a/src/w32.c b/src/w32.c index 1db3426..0f22387 100644 --- a/src/w32.c +++ b/src/w32.c @@ -330,6 +330,7 @@ static BOOL g_b_init_set_named_security_info_a; static BOOL g_b_init_get_adapters_info; BOOL g_b_init_compare_string_w; +BOOL g_b_init_debug_break_process; /* BEGIN: Wrapper functions around OpenProcessToken @@ -9653,6 +9654,7 @@ globals_of_w32 (void) g_b_init_set_named_security_info_a = 0; g_b_init_get_adapters_info = 0; g_b_init_compare_string_w = 0; + g_b_init_debug_break_process = 0; num_of_processors = 0; /* The following sets a handler for shutdown notifications for console apps. This actually applies to Emacs in both console and diff --git a/src/w32proc.c b/src/w32proc.c index 11a121f..bb637bc 100644 --- a/src/w32proc.c +++ b/src/w32proc.c @@ -66,6 +66,8 @@ along with GNU Emacs. If not, see . */ + (filedata).file_base)) extern BOOL g_b_init_compare_string_w; +extern BOOL g_b_init_debug_break_process; + int sys_select (int, SELECT_TYPE *, SELECT_TYPE *, SELECT_TYPE *, struct timespec *, void *); @@ -2494,6 +2496,9 @@ find_child_console (HWND hwnd, LPARAM arg) return TRUE; } +typedef BOOL (WINAPI * DebugBreakProcess_Proc) ( + HANDLE hProcess); + /* Emulate 'kill', but only for other processes. */ int sys_kill (pid_t pid, int sig) @@ -2507,9 +2512,9 @@ sys_kill (pid_t pid, int sig) if (pid < 0) pid = -pid; - /* Only handle signals that will result in the process dying */ + /* Only handle signals that can be mapped to a similar behavior on Windows */ if (sig != 0 - && sig != SIGINT && sig != SIGKILL && sig != SIGQUIT && sig != SIGHUP) + && sig != SIGINT && sig != SIGKILL && sig != SIGQUIT && sig != SIGHUP && sig != SIGTRAP) { errno = EINVAL; return -1; @@ -2552,7 +2557,11 @@ sys_kill (pid_t pid, int sig) close the selected frame, which does not necessarily terminates Emacs. But then we are not supposed to call sys_kill with our own PID. */ - proc_hand = OpenProcess (PROCESS_TERMINATE, 0, pid); + + DWORD desiredAccess = + (sig == SIGTRAP) ? PROCESS_ALL_ACCESS : PROCESS_TERMINATE; + + proc_hand = OpenProcess (desiredAccess, 0, pid); if (proc_hand == NULL) { errno = EPERM; @@ -2648,6 +2657,43 @@ sys_kill (pid_t pid, int sig) rc = -1; } } + else if (sig == SIGTRAP) + { + static DebugBreakProcess_Proc s_pfn_Debug_Break_Process = NULL; + + if (g_b_init_debug_break_process == 0) + { + g_b_init_debug_break_process = 1; + s_pfn_Debug_Break_Process = (DebugBreakProcess_Proc) + GetProcAddress (GetModuleHandle ("kernel32.dll"), + "DebugBreakProcess"); + } + + if (s_pfn_Debug_Break_Process == NULL) + { + errno = ENOTSUP; + rc = -1; + } + else if (!s_pfn_Debug_Break_Process (proc_hand)) + { + DWORD err = GetLastError (); + + DebPrint (("sys_kill.DebugBreakProcess return %d " + "for pid %lu\n", err, pid)); + + switch (err) + { + case ERROR_ACCESS_DENIED: + errno = EPERM; + break; + default: + errno = EINVAL; + break; + } + + rc = -1; + } + } else { if (NILP (Vw32_start_process_share_console) && cp && cp->hwnd) -- 2.8.1.windows.1 --=-=-=--