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

Pedro Alves
2020-04-15T15:08:25Z

On 4/14/20 11:41 PM, Hannes Domani via Gdb-patches wrote:

> The removal of these lines breaks debugging on Windows, right on startup I get:
>
> (gdb) r
> Starting program: C:\qiewer\heob\heob64.exe
> C:/src/repos/binutils-gdb.git/gdb/thread.c:86: internal-error: thread_info* inferior_thread(): Assertion `tp' failed.
>
> Full stacktrace of the assertion failure:
>
> (gdb) bt
> #0  internal_error (file=0xa713a8 <selftests::test_target_info+168> "C:/src/repos/binutils-gdb.git/gdb/thread.c", line=86,
>     fmt=0xa71388 <selftests::test_target_info+136> "%s: Assertion `%s' failed.") at C:/src/repos/binutils-gdb.git/gdbsupport/errors.cc:51
> #1  0x000000000066cfd9 in inferior_thread () at C:/src/repos/binutils-gdb.git/gdb/thread.c:86
> #2  inferior_thread () at C:/src/repos/binutils-gdb.git/gdb/thread.c:83
> #3  0x00000000005603a0 in post_create_inferior (target=0x999ba0 <the_windows_nat_target>, from_tty=from_tty@entry=0)
>     at C:/src/repos/binutils-gdb.git/gdb/infcmd.c:444
> #4  0x0000000000563727 in run_command_1 (args=<optimized out>, from_tty=1, run_how=RUN_NORMAL) at C:/src/repos/binutils-gdb.git/gdb/infcmd.c:670

> Btw., I think this variable in get_windows_debug_event is no longer useful:
> static windows_thread_info dummy_thread_info (0, 0, 0);
>

Thanks for pointing that out.

>
> This replacement works fine for me so far:

Does this instead work for you? This adds a single
switch_to_thread at the end of the initialization.

From 00648f5a0bd7b8d85a2bdf49a2d8f71087826062 Mon Sep 17 00:00:00 2001
From: Pedro Alves <palves@redhat.com>
Date: Wed, 15 Apr 2020 15:52:32 +0100
Subject: [PATCH] Fix Windows

---
gdb/windows-nat.c | 34 ++++++++++++++++++----------------
1 file changed, 18 insertions(+), 16 deletions(-)

diff --git a/gdb/windows-nat.c b/gdb/windows-nat.c
index 78e7e31c67..dd15fa1fab 100644
--- a/gdb/windows-nat.c
+++ b/gdb/windows-nat.c
@@ -358,6 +358,8 @@ struct windows_nat_target final : public x86_nat_target<inf_child_target>
const char *thread_name (struct thread_info *) override;

int get_windows_debug_event (int pid, struct target_waitstatus *ourstatus);
+
+ void do_initial_windows_stuff (DWORD pid, bool attaching);
};

static windows_nat_target the_windows_nat_target;
@@ -1508,8 +1510,6 @@ windows_nat_target::get_windows_debug_event (int pid,
{
BOOL debug_event;
DWORD continue_status, event_code;
- windows_thread_info *th;
- static windows_thread_info dummy_thread_info (0, 0, 0);
DWORD thread_id = 0;

/* If there is a relevant pending stop, report it now. See the
@@ -1522,7 +1522,7 @@ windows_nat_target::get_windows_debug_event (int pid,
*ourstatus = stop->status;

ptid_t ptid (current_event.dwProcessId, thread_id);
- th = thread_rec (ptid, INVALIDATE_CONTEXT);
+ windows_thread_info *th = thread_rec (ptid, INVALIDATE_CONTEXT);
th->reload_context = 1;

return thread_id;
@@ -1537,7 +1537,6 @@ windows_nat_target::get_windows_debug_event (int pid,

event_code = current_event.dwDebugEventCode;
ourstatus->kind = TARGET_WAITKIND_SPURIOUS;
- th = NULL;
have_saved_context = 0;

switch (event_code)
@@ -1563,7 +1562,7 @@ windows_nat_target::get_windows_debug_event (int pid,
}
/* Record the existence of this thread. */
thread_id = current_event.dwThreadId;
- th = windows_add_thread
+ windows_add_thread
(ptid_t (current_event.dwProcessId, current_event.dwThreadId, 0),
current_event.u.CreateThread.hThread,
current_event.u.CreateThread.lpThreadLocalBase,
@@ -1580,7 +1579,6 @@ windows_nat_target::get_windows_debug_event (int pid,
current_event.dwThreadId, 0),
current_event.u.ExitThread.dwExitCode,
false /* main_thread_p */);
- th = &dummy_thread_info;
break;

case CREATE_PROCESS_DEBUG_EVENT:
@@ -1594,7 +1592,7 @@ windows_nat_target::get_windows_debug_event (int pid,

current_process_handle = current_event.u.CreateProcessInfo.hProcess;
/* Add the main thread. */
- th = windows_add_thread
+ windows_add_thread
(ptid_t (current_event.dwProcessId,
current_event.dwThreadId, 0),
current_event.u.CreateProcessInfo.hThread,
@@ -1731,7 +1729,7 @@ windows_nat_target::get_windows_debug_event (int pid,
&& windows_initialization_done)
{
ptid_t ptid = ptid_t (current_event.dwProcessId, thread_id, 0);
- th = thread_rec (ptid, INVALIDATE_CONTEXT);
+ windows_thread_info *th = thread_rec (ptid, INVALIDATE_CONTEXT);
th->stopped_at_software_breakpoint = true;
}
pending_stops.push_back ({thread_id, *ourstatus, current_event});
@@ -1944,8 +1942,8 @@ windows_add_all_dlls (void)
}
}

-static void
-do_initial_windows_stuff (struct target_ops *ops, DWORD pid, int attaching)
+void
+windows_nat_target::do_initial_windows_stuff (DWORD pid, bool attaching)
{
int i;
struct inferior *inf;
@@ -1961,8 +1959,8 @@ do_initial_windows_stuff (struct target_ops *ops, DWORD pid, int attaching)
#endif
current_event.dwProcessId = pid;
memset (&current_event, 0, sizeof (current_event));
- if (!target_is_pushed (ops))
- push_target (ops);
+ if (!target_is_pushed (this))
+ push_target (this);
disable_breakpoints_in_shlibs ();
windows_clear_solib ();
clear_proceed_status (0);
@@ -1992,11 +1990,13 @@ do_initial_windows_stuff (struct target_ops *ops, DWORD pid, int attaching)

windows_initialization_done = 0;

+ ptid_t last_ptid;
+
while (1)
{
struct target_waitstatus status;

- ops->wait (minus_one_ptid, &status, 0);
+ last_ptid = this->wait (minus_one_ptid, &status, 0);

/* Note windows_wait returns TARGET_WAITKIND_SPURIOUS for thread
events. */
@@ -2004,9 +2004,11 @@ do_initial_windows_stuff (struct target_ops *ops, DWORD pid, int attaching)
&& status.kind != TARGET_WAITKIND_SPURIOUS)
break;

- ops->resume (minus_one_ptid, 0, GDB_SIGNAL_0);
+ this->resume (minus_one_ptid, 0, GDB_SIGNAL_0);
}

+ switch_to_thread (find_thread_ptid (this, last_ptid));
+
/* Now that the inferior has been started and all DLLs have been mapped,
we can iterate over all DLLs and load them in.

@@ -2138,7 +2140,7 @@ windows_nat_target::attach (const char *args, int from_tty)
}
#endif

- do_initial_windows_stuff (this, pid, 1);
+ do_initial_windows_stuff (pid, 1);
target_terminal::ours ();
}

@@ -2978,9 +2980,8 @@ windows_nat_target::create_inferior (const char *exec_file,
else
saw_create = 0;

- do_initial_windows_stuff (this, pi.dwProcessId, 0);
+ do_initial_windows_stuff (pi.dwProcessId, 0);

/* windows_continue (DBG_CONTINUE, -1, 0); */
}

--
2.14.5