at which the DLL was loaded is equal to LOAD_ADDR. */
if (!(load_addr != nullptr && mi.lpBaseOfDll != load_addr))
{
+ maybe_note_cygwin1_dll (name);
handle_load_dll (name, mi.lpBaseOfDll);
if (load_addr != nullptr)
return;
by enumerating all the DLLs loaded into the inferior, looking for
one that is loaded at base address = lpBaseOfDll. */
if (dll_name != nullptr)
- handle_load_dll (dll_name, event->lpBaseOfDll);
+ {
+ maybe_note_cygwin1_dll (dll_name);
+ handle_load_dll (dll_name, event->lpBaseOfDll);
+ }
else if (event->lpBaseOfDll != nullptr)
add_dll (event->lpBaseOfDll);
}
add_dll (nullptr);
}
+#ifdef __CYGWIN__
+
+/* See nat/windows-nat.h. */
+
+void
+windows_process_info::maybe_note_cygwin1_dll (const char *dll_path)
+{
+ const char *base = dll_path + strlen (dll_path);
+ while (base > dll_path && base[-1] != '/' && base[-1] != '\\')
+ base--;
+ if (strcasecmp (base, "cygwin1.dll") == 0)
+ cygwin1_dll_loaded = true;
+}
+
+/* See nat/windows-nat.h. */
+
+bool
+inferior_started_by_cygwin (DWORD winpid, bool attaching)
+{
+ /* In the run (non-attach) case this is called early when the
+ inferior has only just reached its first instruction and
+ cygwin1.dll hasn't initialized itself yet -- GDB launched the
+ inferior with raw CreateProcess, not through Cygwin's fork/spawn
+ path, so PID_CYGPARENT is necessarily false, so we can shortcut
+ without calling Cygwin. */
+ if (!attaching)
+ return false;
+
+ /* Note CW_WINPID_TO_CYGWIN_PID never fails. It returns a synthetic
+ pid for non-Cygwin or unknown winpids, in which case CW_GETPINFO
+ returns either a pinfo with PID_CYGPARENT unset, or NULL. */
+ auto cygpid = (pid_t) cygwin_internal (CW_WINPID_TO_CYGWIN_PID, winpid);
+
+ auto *pinfo = (external_pinfo *) cygwin_internal (CW_GETPINFO, cygpid);
+ if (pinfo == nullptr)
+ return false;
+
+ return (pinfo->process_state & PID_CYGPARENT) != 0;
+}
+
+#endif /* __CYGWIN__. */
+
/* See nat/windows-nat.h. */
target_waitstatus
DWORD exit_code = info.dwExitCode;
target_waitstatus tstatus;
+#ifdef __CYGWIN__
+ /* A Cygwin parent waiting on a Cygwin child via waitpid doesn't go
+ through GetExitCodeProcess / the Win32 exit code at all. It
+ reads the child's wait status directly out of the child's Cygwin
+ pinfo (shared memory), set by pinfo::exit in
+ winsup/cygwin/pinfo.cc. So sys/wait.h macros apply to that value
+ verbatim.
+
+ GDB, however, even though it is itself a Cygwin program, drives
+ its inferiors via the native Win32 debugger API: it spawns them
+ with CreateProcess (DEBUG_PROCESS), not via Cygwin's
+ fork/spawn/posix_spawn, and consumes
+ EXIT_PROCESS_DEBUG_EVENT.dwExitCode from WaitForDebugEvent rather
+ than calling waitpid. That dwExitCode value comes from the
+ inferior's ExitProcess call.
+
+ What that value means depends on two orthogonal things:
+
+ 1. Is the inferior a Cygwin process at all? If not, dwExitCode
+ is a raw Win32 exit value.
+
+ 2. For a Cygwin inferior, was it created through Cygwin's spawn
+ path?
+
+ - If not, cygwin1.dll's pinfo::exit byte-swaps the wait status
+ on the way out, so that the meaningful exit value lands in
+ the low byte where native Win32 consumers (cmd.exe's "echo
+ %errorlevel%", and bare GetExitCodeProcess readers) expect
+ it. This is the case for Cygwin inferiors that we run, via
+ CreateProcess.
+
+ - If yes, cygwin1.dll does not swap. We see this case if we
+ attach to an already-running process with a Cygwin parent.
+
+ See winsup/cygwin/pinfo.cc:
+
+ int exitcode = self->exitcode & 0xffff;
+ if (!self->cygstarted)
+ exitcode = ((exitcode & 0xff) << 8) | ((exitcode >> 8) & 0xff);
+ ...
+ ExitProcess (exitcode);
+ */
+
+ /* The inferior may also exit with a raw NTSTATUS error code, e.g.,
+ STATUS_ACCESS_VIOLATION (0xc0000005), without going through the
+ pinfo::exit at all -- for example, if the unhandled-exception
+ filter didn't run (e.g., the inferior was killed before
+ installing one), or for inferiors that don't link cygwin1.dll.
+ Detect those and map them the same way Cygwin's set_exit_code
+ does in winsup/cygwin/pinfo.cc, so Cygwin GDB sees the same
+ status a Cygwin parent's waitpid would. */
+ if (exit_code >= 0xc0000000)
+ {
+ gdb_signal sig;
+ switch (exit_code)
+ {
+ case EXCEPTION_ACCESS_VIOLATION:
+ sig = GDB_SIGNAL_SEGV;
+ break;
+ case EXCEPTION_ILLEGAL_INSTRUCTION:
+ sig = GDB_SIGNAL_ILL;
+ break;
+ case STATUS_NO_MEMORY:
+ sig = GDB_SIGNAL_BUS;
+ break;
+ case STATUS_CONTROL_C_EXIT:
+ sig = GDB_SIGNAL_INT;
+ break;
+ default:
+ /* Cygwin maps any other NTSTATUS to exit 127. */
+ tstatus.set_exited (127);
+ return tstatus;
+ }
+ tstatus.set_signalled (sig);
+ return tstatus;
+ }
+
+ if (!this->cygwin1_dll_loaded)
+ {
+ /* Non-Cygwin inferior: dwExitCode is a raw Win32 exit value.
+ Limit to 8 bits, like Cygwin does, matching what happens with
+ Cygwin inferiors. */
+ tstatus.set_exited (exit_code & 0xff);
+ return tstatus;
+ }
+
+ /* Note: when GDB attaches to a Cygwin inferior and the inferior is
+ then killed externally (e.g., taskkill /F with exit code 1), GDB
+ and Cygwin disagree. Cygwin's parent waitpid reports WIFEXITED,
+ code=1; GDB reports SIGHUP (signal 1, no swap below because
+ started_by_cygwin). Cygwin's parent distinguishes "pinfo::exit
+ ran" from "didn't run" via the child's wait pipe and only applies
+ the swap-undo for the former. GDB has only dwExitCode and can't
+ tell. This can't be solved without Cygwin's help. However, such
+ an external termination steps out of Cygwin and falls outside
+ Cygwin's contract, so it matters less than the cases where the
+ inferior exits through Cygwin's own mechanisms. */
+
+ int wstatus = exit_code & 0xffff;
+ if (!this->started_by_cygwin)
+ wstatus = ((wstatus & 0xff) << 8) | ((wstatus >> 8) & 0xff);
+ if (!WIFSIGNALED (wstatus))
+ tstatus.set_exited (WEXITSTATUS (wstatus));
+ else
+ tstatus.set_signalled (gdb_signal_from_host (WTERMSIG (wstatus)));
+#else
/* If the exit status looks like a fatal exception, but we don't
recognize the exception's code, make the original exit status
value available, to avoid losing information. */
tstatus.set_exited (exit_code);
else
tstatus.set_signalled (gdb_signal_from_host (exit_signal));
+#endif
return tstatus;
}
DWORD process_id = 0;
DWORD main_thread_id = 0;
+#ifdef __CYGWIN__
+ /* True if the inferior was created through Cygwin's spawn path
+ (i.e., its Cygwin pinfo has PID_CYGPARENT set). We need this at
+ exit time, but we cache it early when we start debugging the
+ inferior, because by exit time the inferior's Cygwin pinfo may
+ have been torn down (CW_GETPINFO returns NULL). */
+ bool started_by_cygwin = false;
+
+ /* True if cygwin1.dll is loaded into the inferior. */
+ bool cygwin1_dll_loaded = false;
+
+ /* If DLL_PATH is cygwin1.dll, set cygwin1_dll_loaded to true. */
+ void maybe_note_cygwin1_dll (const char *dll_path);
+#else
+ void maybe_note_cygwin1_dll (const char *) {}
+#endif
+
#ifdef __x86_64__
/* The target is a WOW64 process */
bool wow64_process = false;
int get_exec_module_filename (char *exe_name_ret, size_t exe_name_max_len);
};
+#ifdef __CYGWIN__
+/* Return true if the process with native Windows pid WINPID was
+ started by a Cygwin parent -- that is, its Cygwin pinfo exists and
+ has PID_CYGPARENT set. Returns false if the process is not a
+ Cygwin process at all, or if its parent is not a Cygwin process.
+
+ ATTACHING indicates whether GDB is attaching to an already-running
+ inferior (true) or has just launched it via CreateProcess
+ (false). */
+extern bool inferior_started_by_cygwin (DWORD winpid, bool attaching);
+#endif
+
/* Return a string version of EVENT_CODE. */
extern std::string event_code_to_string (DWORD event_code);
set srcfile2 ${exec2}.c
set binfile2 [standard_output_file ${exec2}]
+set exec3 "segv-win32"
+set srcfile3 ${exec1}.c
+set binfile3 [standard_output_file ${exec3}]
+
if { [build_executable "failed to build $exec1" ${exec1} "${srcfile1}" \
{debug}] == -1 } {
return
return
}
+# On Cygwin, also build a pure-Win32 segv binary, used to test that
+# GDB extracts the terminating SIGSEGV out of the 0xc0000005
+# (STATUS_ACCESS_VIOLATION) Windows exit code.
+if {[istarget "*-*-cygwin*"]} {
+ if { [build_executable "failed to build $exec3" ${exec3} "${srcfile3}" \
+ {debug win32}] == -1} {
+ return
+ }
+}
+
# Get the inferior under GDB's control in mode HOW ("run" or
# "attach"), using BINFILE. In "attach" mode, spawn the binary and
# attach to it; in "run" mode, run to main. In both modes, clear the
}
}
-proc test_signal {how} {
- clean_restart $::exec1
+proc test_signal {how exec binfile} {
+ clean_restart $exec
# Get the inferior under GDB's control. But, before, change cwd
# so the core file ends up in the output directory.
set_inferior_cwd_to_output_dir
- setup $how $::binfile1
+ setup $how $binfile
# Get the inferior's PID for later.
set pid [get_inferior_pid]
gdb_test "continue" "(Thread .*|Program) received signal SIGSEGV.*" \
"trigger SIGSEGV"
- if {[istarget "*-*-mingw*"]} {
+ if {[istarget "*-*-mingw*"]
+ || ([istarget "*-*-cygwin*"] && $binfile == $::binfile3)} {
# We're debugging a pure Win32 program with no SEH handler. The
# previous continue caught the first-chance exception. Now we
# catch the second-chance one.
} else {
with_test_prefix "reattach" {
kill_wait_spawned_process $::test_spawn_id
- setup $how $::binfile1
+ setup $how $binfile
}
}
}
with_test_prefix "signal" {
- test_signal $how
+ test_signal $how $exec1 $binfile1
}
with_test_prefix "normal" {
test_normal $how
}
+ if {[istarget "*-*-cygwin*"]} {
+ with_test_prefix "signal, win32" {
+ test_signal $how $exec3 $binfile3
+ }
+ }
}