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

Hannes Domani
2020-04-14T22:41:17Z

I did my tests now, thanks for your git branch.

Am Dienstag, 14. April 2020, 19:55:30 MESZ hat Pedro Alves via Gdb-patches <gdb-patches@sourceware.org> Folgendes geschrieben:

> 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;

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
#5  0x00000000004810c2 in cmd_func (cmd=0xa713a8 <selftests::test_target_info+168>, args=0x56 <error: Cannot access memory at address 0x56>,
from_tty=10949512) at C:/src/repos/binutils-gdb.git/gdb/cli/cli-decode.c:1952
#6  0x00000000006735a8 in execute_command (p=<optimized out>, p@entry=0x13bae0 "", from_tty=1) at C:/src/repos/binutils-gdb.git/gdb/top.c:655
#7  0x00000000005153f4 in command_handler (command=0x13bae0 "") at C:/src/repos/binutils-gdb.git/gdb/event-top.c:588
#8  0x00000000005162a2 in command_line_handler (rl=...) at C:/src/repos/binutils-gdb.git/gdb/event-top.c:773
#9  0x0000000000515bd3 in gdb_rl_callback_handler (rl=0x13b4c0 "r") at c:/msys64/mingw64/x86_64-w64-mingw32/include/c++/9.3.0/bits/unique_ptr.h:153
#10 0x00000000006e4eec in rl_callback_read_char () at C:/src/repos/binutils-gdb.git/readline/readline/callback.c:281
#11 0x0000000000514f4e in gdb_rl_callback_read_char_wrapper_noexcept () at C:/src/repos/binutils-gdb.git/gdb/event-top.c:177
#12 0x0000000000515a84 in gdb_rl_callback_read_char_wrapper (client_data=<optimized out>) at C:/src/repos/binutils-gdb.git/gdb/event-top.c:193
#13 0x0000000000514d92 in stdin_event_handler (error=<optimized out>, client_data=0x13b720) at C:/src/repos/binutils-gdb.git/gdb/event-top.c:516
#14 0x00000000007a91e0 in handle_file_event (ready_mask=2, file_ptr=0x1e93c0) at C:/src/repos/binutils-gdb.git/gdbsupport/event-loop.cc:548
#15 gdb_wait_for_event (block=<optimized out>) at C:/src/repos/binutils-gdb.git/gdbsupport/event-loop.cc:698
#16 gdb_wait_for_event (block=<optimized out>) at C:/src/repos/binutils-gdb.git/gdbsupport/event-loop.cc:561
#17 0x00000000007a9318 in gdb_do_one_event () at C:/src/repos/binutils-gdb.git/gdbsupport/event-loop.cc:215
#18 0x0000000000592c4d in start_event_loop () at C:/src/repos/binutils-gdb.git/gdb/main.c:356
#19 captured_command_loop () at C:/src/repos/binutils-gdb.git/gdb/main.c:416
#20 0x0000000000594b55 in captured_main (data=0xfd4fdc0) at C:/src/repos/binutils-gdb.git/gdb/main.c:1254
#21 gdb_main (args=args@entry=0xfd4fe20) at C:/src/repos/binutils-gdb.git/gdb/main.c:1269
#22 0x000000000098a757 in main (argc=3, argv=0x1048a0) at C:/src/repos/binutils-gdb.git/gdb/gdb.c:32
(gdb) up
#1  0x000000000066cfd9 in inferior_thread () at C:/src/repos/binutils-gdb.git/gdb/thread.c:86
86        gdb_assert (tp);
(gdb) l
81
82      struct thread_info*
83      inferior_thread (void)
84      {
85        struct thread_info *tp = find_thread_ptid (current_inferior (), inferior_ptid);
86        gdb_assert (tp);
87        return tp;
88      }
89
90      /* Delete the breakpoint pointed at by BP_P, if there's one.  */

This replacement works fine for me so far:

diff --git a/gdb/windows-nat.c b/gdb/windows-nat.c
index 78e7e31c67..b22df182a1 100644
--- a/gdb/windows-nat.c
+++ b/gdb/windows-nat.c
@@ -1738,6 +1738,12 @@ windows_nat_target::get_windows_debug_event (int pid,
thread_id = 0;
CHECK (windows_continue (continue_status, desired_stop_thread_id, 0));
}
+  else
+    {
+      if (th != nullptr)
+       switch_to_thread (&the_windows_nat_target,
+                         ptid_t (current_event.dwProcessId, thread_id, 0));
+    }

out:
return thread_id;

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

Hannes