From patchwork Wed Dec 18 17:07:49 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eli Zaretskii X-Patchwork-Id: 36945 Received: (qmail 15454 invoked by alias); 18 Dec 2019 17:08:09 -0000 Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sourceware.org Delivered-To: mailing list gdb-patches@sourceware.org Received: (qmail 15443 invoked by uid 89); 18 Dec 2019 17:08:09 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-8.5 required=5.0 tests=AWL, BAYES_00, GIT_PATCH_2, GIT_PATCH_3, SPF_PASS autolearn=ham version=3.3.1 spammy=sigh, 4018, 4084, 4776 X-HELO: eggs.gnu.org Received: from eggs.gnu.org (HELO eggs.gnu.org) (209.51.188.92) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Wed, 18 Dec 2019 17:08:07 +0000 Received: from fencepost.gnu.org ([2001:470:142:3::e]:46361) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ihcnp-0002R5-Pr; Wed, 18 Dec 2019 12:08:03 -0500 Received: from [176.228.60.248] (port=3337 helo=home-c4e4a596f7) by fencepost.gnu.org with esmtpsa (TLS1.2:RSA_AES_256_CBC_SHA1:256) (Exim 4.82) (envelope-from ) id 1ihcno-0004I2-I9; Wed, 18 Dec 2019 12:08:01 -0500 Date: Wed, 18 Dec 2019 19:07:49 +0200 Message-Id: <83o8w536l6.fsf@gnu.org> From: Eli Zaretskii To: Pedro Alves CC: philippe.waroquiers@skynet.be, gdb-patches@sourceware.org In-reply-to: <52c4ca33-ffc4-8e1e-fe08-a92123ef02aa@redhat.com> (message from Pedro Alves on Tue, 17 Dec 2019 17:51:26 +0000) Subject: Re: [RFAv3 2/6] Improve process exit status macros on MinGW References: <20190504161753.15530-1-philippe.waroquiers@skynet.be> <20190504161753.15530-3-philippe.waroquiers@skynet.be> <835zie51mf.fsf@gnu.org> <52c4ca33-ffc4-8e1e-fe08-a92123ef02aa@redhat.com> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-IsSubscribed: yes > Cc: philippe.waroquiers@skynet.be, gdb-patches@sourceware.org > From: Pedro Alves > Date: Tue, 17 Dec 2019 17:51:26 +0000 > > The issue pointed out was that by putting the windows_status_to_termsig > function in windows-nat.c, and then calling it from gdb's common code > (cli/cli-cmds.c, via WTERMSIG) would result in a build/link failure when > you try to build a cross debugger hosted on mingw, because such a gdb > build does not include the native Windows target support, i.e., does not > build/link the windows-nat.o object. Putting said function in mingw-hdep.c > instead fixes that issue because that file is included as part of the build > in all kinds of mingw-hosted GDBs, either native or cross-debugger. > > >> I admit to being a bit confused about why we want to do this > >> translation for this feature while we don't do it for the exit code > >> of inferiors running under gdb, for example. I mean, exit status > >> with 0xc0000000 set don't cause $_exitsignal to be set instead of > >> $_exitcode. > > > > Yes, we should do this for exit code of inferiors as well. > > > > Native MS-Windows debugging produces the TARGET_WAITKIND_* values in > > windows-nat.c, so I think the conversion we are talking about will > > have to be done there, perhaps _in_addition_to_ other places? IOW, > > the function that performs the conversion can be defined in > > mingw-hdep.c, but it will have to be called from windows-nat.c at > > least, right? And I'm uncertain what other places will have to call > > that conversion function for the use case of running a cross-debugger, > > can someone please help me understand that? > > You'll also want to call it in gdbserver's win32-low.c file, so that > you get the translation too when debugging against gdbserver. > This actually suggests putting the new function in some new > shared file in gdb/gdbsupport/, since gdb/mingw-hdep.c is gdb-only. A new file for just one function sounds too much to me. Is it OK to define an inline function in gdb_wait.h, as in the prototype change below? If this way is accepted, I will post a fully formatted patch. Thanks. --- gdb/gdbsupport/gdb_wait.h~0 2019-09-21 00:58:17.000000000 +0300 +++ gdb/gdbsupport/gdb_wait.h 2019-12-18 17:59:28.434097000 +0200 @@ -40,18 +40,84 @@ NOTE exception for GNU/Linux below). We also fail to declare wait() and waitpid(). */ +/* For MINGW, the underlying idea is that when a Windows program is + terminated by a fatal exception, its exit code is the value of that + exception, as defined by the various EXCEPTION_* symbols in the + Windows API headers. + + The translation below is not perfect, because a program could + legitimately exit normally with a status whose value happens to + have the high bits set, but that's extremely rare, to say the + least, and it is deemed such a negligibly small probability of + false positives is justified by the utility of reporting the + terminating signal in the "normal" cases. */ + +#ifdef __MINGW32__ + +# define WIN32_LEAN_AND_MEAN +# include /* for EXCEPTION_* constants */ +# include "gdb/signals.h" /* for enum gdb_signal */ + +struct xlate_status +{ + DWORD status; /* exit status (actually, fatal exception code) */ + enum gdb_signal sig; /* corresponding GDB signal value */ +}; + +static inline enum gdb_signal +windows_status_to_termsig (DWORD status) +{ + static const xlate_status status_xlate_tbl[] = + { + {EXCEPTION_ACCESS_VIOLATION, GDB_SIGNAL_SEGV}, + {EXCEPTION_IN_PAGE_ERROR, GDB_SIGNAL_SEGV}, + {EXCEPTION_INVALID_HANDLE, GDB_SIGNAL_SEGV}, + {EXCEPTION_ILLEGAL_INSTRUCTION, GDB_SIGNAL_ILL}, + {EXCEPTION_NONCONTINUABLE_EXCEPTION, GDB_SIGNAL_ILL}, + {EXCEPTION_ARRAY_BOUNDS_EXCEEDED, GDB_SIGNAL_SEGV}, + {EXCEPTION_FLT_DENORMAL_OPERAND, GDB_SIGNAL_FPE}, + {EXCEPTION_FLT_DIVIDE_BY_ZERO, GDB_SIGNAL_FPE}, + {EXCEPTION_FLT_INEXACT_RESULT, GDB_SIGNAL_FPE}, + {EXCEPTION_FLT_INVALID_OPERATION, GDB_SIGNAL_FPE}, + {EXCEPTION_FLT_OVERFLOW, GDB_SIGNAL_FPE}, + {EXCEPTION_FLT_STACK_CHECK, GDB_SIGNAL_FPE}, + {EXCEPTION_FLT_UNDERFLOW, GDB_SIGNAL_FPE}, + {EXCEPTION_INT_DIVIDE_BY_ZERO, GDB_SIGNAL_FPE}, + {EXCEPTION_INT_OVERFLOW, GDB_SIGNAL_FPE}, + {EXCEPTION_PRIV_INSTRUCTION, GDB_SIGNAL_ILL}, + {EXCEPTION_STACK_OVERFLOW, GDB_SIGNAL_SEGV}, + {CONTROL_C_EXIT, GDB_SIGNAL_TERM} + }; + + for (const xlate_status &x : status_xlate_tbl) + if (x.status == status) + return x.sig; + + return GDB_SIGNAL_UNKNOWN; +} + +#endif /* __MINGW32__ */ + #ifndef WIFEXITED -#define WIFEXITED(w) (((w)&0377) == 0) +# ifdef __MINGW32__ +# define WIFEXITED(w) (((w) & 0xC0000000) == 0) +# else +# define WIFEXITED(w) (((w)&0377) == 0) +# endif #endif #ifndef WIFSIGNALED -#define WIFSIGNALED(w) (((w)&0377) != 0177 && ((w)&~0377) == 0) +# ifdef __MINGW32__ +# define WIFSIGNALED(w) (((w) & 0xC0000000) == 0xC0000000) +# else +# define WIFSIGNALED(w) (((w)&0377) != 0177 && ((w)&~0377) == 0) +# endif #endif #ifndef WIFSTOPPED #ifdef IBM6000 -/* Unfortunately, the above comment (about being compatible in all Unix +/* Unfortunately, the above comment (about being compatible in all Unix systems) is not quite correct for AIX, sigh. And AIX 3.2 can generate status words like 0x57c (sigtrap received after load), and gdb would choke on it. */ @@ -64,11 +130,19 @@ #endif #ifndef WEXITSTATUS -#define WEXITSTATUS(w) (((w) >> 8) & 0377) /* same as WRETCODE */ +# ifdef __MINGW32__ +# define WEXITSTATUS(w) ((w) & ~0xC0000000) +# else +# define WEXITSTATUS(w) (((w) >> 8) & 0377) /* same as WRETCODE */ +# endif #endif #ifndef WTERMSIG -#define WTERMSIG(w) ((w) & 0177) +# ifdef __MINGW32__ +# define WTERMSIG(w) windows_status_to_termsig (w) +# else +# define WTERMSIG(w) ((w) & 0177) +# endif #endif #ifndef WSTOPSIG --- gdb/windows-nat.c~0 2019-12-11 22:24:51.000000000 +0200 +++ gdb/windows-nat.c 2019-12-18 18:21:00.264558400 +0200 @@ -68,6 +68,7 @@ #include "inf-child.h" #include "gdbsupport/gdb_tilde_expand.h" #include "gdbsupport/pathstuff.h" +#include "gdbsupport/gdb_wait.h" #define AdjustTokenPrivileges dyn_AdjustTokenPrivileges #define DebugActiveProcessStop dyn_DebugActiveProcessStop @@ -1620,8 +1621,17 @@ get_windows_debug_event (struct target_o windows_delete_thread (ptid_t (current_event.dwProcessId, 0, current_event.dwThreadId), 0, true /* main_thread_p */); - ourstatus->kind = TARGET_WAITKIND_EXITED; - ourstatus->value.integer = current_event.u.ExitProcess.dwExitCode; + DWORD exit_status = current_event.u.ExitProcess.dwExitCode; + if (WIFEXITED (exit_status)) + { + ourstatus->kind = TARGET_WAITKIND_EXITED; + ourstatus->value.integer = WEXITSTATUS (exit_status); + } + else + { + ourstatus->kind = TARGET_WAITKIND_SIGNALLED; + ourstatus->value.sig = WTERMSIG (exit_status); + } thread_id = current_event.dwThreadId; } break; --- gdb/windows-tdep.c~0 2019-09-21 00:58:17.000000000 +0300 +++ gdb/windows-tdep.c 2019-12-18 17:49:52.360580700 +0200 @@ -35,6 +35,8 @@ #include "solib-target.h" #include "gdbcore.h" +#include + struct cmd_list_element *info_w32_cmdlist; typedef struct thread_information_block_32 @@ -461,6 +463,29 @@ init_w32_command_list (void) } } +static int +windows_gdb_signal_to_target (struct gdbarch *gdbarch, enum gdb_signal signal) +{ + switch (signal) + { + case GDB_SIGNAL_0: + return 0; + case GDB_SIGNAL_INT: + return SIGINT; + case GDB_SIGNAL_ILL: + return SIGILL; + case GDB_SIGNAL_ABRT: + return SIGABRT; + case GDB_SIGNAL_FPE: + return SIGFPE; + case GDB_SIGNAL_SEGV: + return SIGSEGV; + case GDB_SIGNAL_TERM: + return SIGTERM; + } + return -1; +} + /* To be called from the various GDB_OSABI_CYGWIN handlers for the various Windows architectures and machine types. */ @@ -477,6 +502,8 @@ windows_init_abi (struct gdbarch_info in set_gdbarch_iterate_over_objfiles_in_search_order (gdbarch, windows_iterate_over_objfiles_in_search_order); + set_gdbarch_gdb_signal_to_target (gdbarch, windows_gdb_signal_to_target); + set_solib_ops (gdbarch, &solib_target_so_ops); } --- gdb/gdbserver/win32-low.c~0 2019-11-19 03:10:41.000000000 +0200 +++ gdb/gdbserver/win32-low.c 2019-12-18 17:51:32.098324200 +0200 @@ -34,6 +34,7 @@ #include #include "gdbsupport/gdb_tilde_expand.h" #include "gdbsupport/common-inferior.h" +#include "gdbsupport/gdb_wait.h" #ifndef USE_WIN32API #include @@ -1511,8 +1512,19 @@ get_child_debug_event (struct target_wai "for pid=%u tid=%x\n", (unsigned) current_event.dwProcessId, (unsigned) current_event.dwThreadId)); - ourstatus->kind = TARGET_WAITKIND_EXITED; - ourstatus->value.integer = current_event.u.ExitProcess.dwExitCode; + { + DWORD exit_status = current_event.u.ExitProcess.dwExitCode; + if (WIFEXITED (exit_status)) + { + ourstatus->kind = TARGET_WAITKIND_EXITED; + ourstatus->value.integer = WEXITSTATUS (exit_status); + } + else + { + ourstatus->kind = TARGET_WAITKIND_SIGNALLED; + ourstatus->value.sig = WTERMSIG (exit_status); + } + } child_continue (DBG_CONTINUE, -1); CloseHandle (current_process_handle); current_process_handle = NULL; @@ -1607,6 +1619,7 @@ win32_wait (ptid_t ptid, struct target_w win32_clear_inferiors (); return ptid_t (current_event.dwProcessId); case TARGET_WAITKIND_STOPPED: + case TARGET_WAITKIND_SIGNALLED: case TARGET_WAITKIND_LOADED: OUTMSG2 (("Child Stopped with signal = %d \n", ourstatus->value.sig));