[PATCH 25/28] Don't write to inferior_ptid in windows-nat.c, part II

Pedro Alves
2020-04-14T17:54:59Z

Writing to inferior_ptid in
windows_nat_target::get_windows_debug_event is just incorrect and not
necessary. We'll report the event to GDB's core, which then takes
care of switching inferior_ptid / current thread.

Related (see windows_nat_target::get_windows_debug_event), there's
also a "current_windows_thread" global that is just begging to get out
of sync with core GDB's current thread. This patch removes it.
gdbserver already does not have an equivalent global in win32-low.cc.

gdb/ChangeLog:
yyyy-mm-dd Pedro Alves <palves@redhat.com>

* nat/windows-nat.c (current_windows_thread): Remove.
* nat/windows-nat.h (current_windows_thread): Remove.
* windows-nat.c (windows_nat_target::stopped_by_sw_breakpoint):
Adjust.
(display_selectors): Adjust to fetch the current
windows_thread_info based on inferior_ptid.
(fake_create_process): No longer write to current_windows_thread.
(windows_nat_target::get_windows_debug_event):
Don't set inferior_ptid or current_windows_thread.
(windows_nat_target::wait): Adjust to not rely on
current_windows_thread.
(windows_nat_target::detach): Use switch_to_no_thread instead of
writing to inferior_ptid directly.
---
gdb/nat/windows-nat.c | 1 -
gdb/nat/windows-nat.h | 3 ---
gdb/windows-nat.c | 62 ++++++++++++++++++++++++++-------------------------
3 files changed, 32 insertions(+), 34 deletions(-)

diff --git a/gdb/nat/windows-nat.c b/gdb/nat/windows-nat.c
index cd7c1d177c..a382b9edb9 100644
--- a/gdb/nat/windows-nat.c
+++ b/gdb/nat/windows-nat.c
@@ -36,7 +36,6 @@ DEBUG_EVENT current_event;
ContinueDebugEvent. */
static DEBUG_EVENT last_wait_event;

-windows_thread_info *current_windows_thread;
DWORD desired_stop_thread_id = -1;
std::vector<pending_stop> pending_stops;
EXCEPTION_RECORD siginfo_er;
diff --git a/gdb/nat/windows-nat.h b/gdb/nat/windows-nat.h
index aea1519672..dc27827d9c 100644
--- a/gdb/nat/windows-nat.h
+++ b/gdb/nat/windows-nat.h
@@ -168,9 +168,6 @@ extern enum gdb_signal last_sig;
stop. */
extern DEBUG_EVENT current_event;

-/* Info on currently selected thread */
-extern windows_thread_info *current_windows_thread;
-
/* The ID of the thread for which we anticipate a stop event.
Normally this is -1, meaning we'll accept an event in any
thread. */
diff --git a/gdb/windows-nat.c b/gdb/windows-nat.c
index 1bbebf00b9..78e7e31c67 100644
--- a/gdb/windows-nat.c
+++ b/gdb/windows-nat.c
@@ -317,7 +317,9 @@ struct windows_nat_target final : public x86_nat_target<inf_child_target>

bool stopped_by_sw_breakpoint () override
{
- return current_windows_thread->stopped_at_software_breakpoint;
+ windows_thread_info *th
+ = thread_rec (inferior_ptid, DONT_INVALIDATE_CONTEXT);
+ return th->stopped_at_software_breakpoint;
}

bool supports_stopped_by_sw_breakpoint () override
@@ -1125,11 +1127,15 @@ display_selector (HANDLE thread, DWORD sel)
static void
display_selectors (const char * args, int from_tty)
{
- if (!current_windows_thread)
+ if (inferior_ptid == null_ptid)
{
puts_filtered ("Impossible to display selectors now.\n");
return;
}
+
+ windows_thread_info *current_windows_thread
+ = thread_rec (inferior_ptid, DONT_INVALIDATE_CONTEXT);
+
if (!args)
{
#ifdef __x86_64__
@@ -1338,12 +1344,11 @@ fake_create_process (void)
(unsigned) GetLastError ());
/* We can not debug anything in that case. */
}
- current_windows_thread
- = windows_add_thread (ptid_t (current_event.dwProcessId,
- current_event.dwThreadId, 0),
- current_event.u.CreateThread.hThread,
- current_event.u.CreateThread.lpThreadLocalBase,
- true /* main_thread_p */);
+ windows_add_thread (ptid_t (current_event.dwProcessId, 0,
+ current_event.dwThreadId),
+ current_event.u.CreateThread.hThread,
+ current_event.u.CreateThread.lpThreadLocalBase,
+ true /* main_thread_p */);
return current_event.dwThreadId;
}

@@ -1516,10 +1521,9 @@ windows_nat_target::get_windows_debug_event (int pid,
thread_id = stop->thread_id;
*ourstatus = stop->status;

- inferior_ptid = ptid_t (current_event.dwProcessId, thread_id, 0);
- current_windows_thread = thread_rec (inferior_ptid,
- INVALIDATE_CONTEXT);
- current_windows_thread->reload_context = 1;
+ ptid_t ptid (current_event.dwProcessId, thread_id);
+ th = thread_rec (ptid, INVALIDATE_CONTEXT);
+ th->reload_context = 1;

return thread_id;
}
@@ -1734,14 +1738,6 @@ windows_nat_target::get_windows_debug_event (int pid,
thread_id = 0;
CHECK (windows_continue (continue_status, desired_stop_thread_id, 0));
}
- else
- {
- inferior_ptid = ptid_t (current_event.dwProcessId, thread_id, 0);
- current_windows_thread = th;
- if (!current_windows_thread)
- current_windows_thread = thread_rec (inferior_ptid,
- INVALIDATE_CONTEXT);
- }

out:
return thread_id;
@@ -1798,16 +1794,22 @@ windows_nat_target::wait (ptid_t ptid, struct target_waitstatus *ourstatus,
{
ptid_t result = ptid_t (current_event.dwProcessId, retval, 0);

- if (current_windows_thread != nullptr)
+ if (ourstatus->kind != TARGET_WAITKIND_EXITED
+ && ourstatus->kind != TARGET_WAITKIND_SIGNALLED)
{
- current_windows_thread->stopped_at_software_breakpoint = false;
- if (current_event.dwDebugEventCode == EXCEPTION_DEBUG_EVENT
- && ((current_event.u.Exception.ExceptionRecord.ExceptionCode
- == EXCEPTION_BREAKPOINT)
- || (current_event.u.Exception.ExceptionRecord.ExceptionCode
- == STATUS_WX86_BREAKPOINT))
- && windows_initialization_done)
- current_windows_thread->stopped_at_software_breakpoint = true;
+ windows_thread_info *th = thread_rec (result, INVALIDATE_CONTEXT);
+
+ if (th != nullptr)
+ {
+ th->stopped_at_software_breakpoint = false;
+ if (current_event.dwDebugEventCode == EXCEPTION_DEBUG_EVENT
+ && ((current_event.u.Exception.ExceptionRecord.ExceptionCode
+ == EXCEPTION_BREAKPOINT)
+ || (current_event.u.Exception.ExceptionRecord.ExceptionCode
+ == STATUS_WX86_BREAKPOINT))
+ && windows_initialization_done)
+ th->stopped_at_software_breakpoint = true;
+ }
}

return result;
@@ -2166,7 +2168,7 @@ windows_nat_target::detach (inferior *inf, int from_tty)
}

x86_cleanup_dregs ();
- inferior_ptid = null_ptid;
+ switch_to_no_thread ();
detach_inferior (inf);

maybe_unpush_target ();
--
2.14.5