gdb, btrace: switch threads in remote_btrace_maybe_reopen()
authorMarkus Metzger <markus.t.metzger@intel.com>
Thu, 25 Nov 2021 06:33:20 +0000 (07:33 +0100)
committerMarkus Metzger <markus.t.metzger@intel.com>
Thu, 27 Jan 2022 12:31:20 +0000 (13:31 +0100)
commitb674665b51112de22260709a214d4d6ba01c7e57
treef9bb4fa3b2b177d28770eeb875d1e74537e7e6e7
parentb02b09623dfd4e75a9f172e9d0a917e3b856b471
gdb, btrace: switch threads in remote_btrace_maybe_reopen()

In remote_btrace_maybe_reopen() we iterate over threads and use
set_general_thread() to set the thread from which to transfer the btrace
configuration.

This sets the remote general thread but does not affect inferior_ptid.  On
the xfer request later on, remote_target::xfer_partial() again sets the
remote general thread to inferior_ptid, overwriting what
remote_btrace_maybe_reopen() had done.

In one case, this led to inferior_ptid being null_ptid when we tried to
enable tracing on a newly created thread inside a newly created process
during attach.

This, in turn, led to find_inferior_pid() asserting when we iterated over
threads in record_btrace_is_replaying(), which was called from
record_btrace_target::xfer_partial() when reading the btrace configuration
of the new thread to check whether it was already being recorded.

The bug was exposed by

    0618ae41497 gdb: optimize all_matching_threads_iterator

and found by

    FAIL: gdb.btrace/enable-new-thread.exp: ... (GDB internal error)

Use switch_to_thread() in remote_btrace_maybe_reopen().
gdb/remote.c