binutils-gdb/gdb/target/waitstatus.h
Pedro Alves 0d36baa9af Step over clone syscall w/ breakpoint, TARGET_WAITKIND_THREAD_CLONED
(A good chunk of the problem statement in the commit log below is
Andrew's, adjusted for a different solution, and for covering
displaced stepping too.  The testcase is mostly Andrew's too.)

This commit addresses bugs gdb/19675 and gdb/27830, which are about
stepping over a breakpoint set at a clone syscall instruction, one is
about displaced stepping, and the other about in-line stepping.

Currently, when a new thread is created through a clone syscall, GDB
sets the new thread running.  With 'continue' this makes sense
(assuming no schedlock):

 - all-stop mode, user issues 'continue', all threads are set running,
   a newly created thread should also be set running.

 - non-stop mode, user issues 'continue', other pre-existing threads
   are not affected, but as the new thread is (sort-of) a child of the
   thread the user asked to run, it makes sense that the new threads
   should be created in the running state.

Similarly, if we are stopped at the clone syscall, and there's no
software breakpoint at this address, then the current behaviour is
fine:

 - all-stop mode, user issues 'stepi', stepping will be done in place
   (as there's no breakpoint to step over).  While stepping the thread
   of interest all the other threads will be allowed to continue.  A
   newly created thread will be set running, and then stopped once the
   thread of interest has completed its step.

 - non-stop mode, user issues 'stepi', stepping will be done in place
   (as there's no breakpoint to step over).  Other threads might be
   running or stopped, but as with the continue case above, the new
   thread will be created running.  The only possible issue here is
   that the new thread will be left running after the initial thread
   has completed its stepi.  The user would need to manually select
   the thread and interrupt it, this might not be what the user
   expects.  However, this is not something this commit tries to
   change.

The problem then is what happens when we try to step over a clone
syscall if there is a breakpoint at the syscall address.

- For both all-stop and non-stop modes, with in-line stepping:

   + user issues 'stepi',
   + [non-stop mode only] GDB stops all threads.  In all-stop mode all
     threads are already stopped.
   + GDB removes s/w breakpoint at syscall address,
   + GDB single steps just the thread of interest, all other threads
     are left stopped,
   + New thread is created running,
   + Initial thread completes its step,
   + [non-stop mode only] GDB resumes all threads that it previously
     stopped.

There are two problems in the in-line stepping scenario above:

  1. The new thread might pass through the same code that the initial
     thread is in (i.e. the clone syscall code), in which case it will
     fail to hit the breakpoint in clone as this was removed so the
     first thread can single step,

  2. The new thread might trigger some other stop event before the
     initial thread reports its step completion.  If this happens we
     end up triggering an assertion as GDB assumes that only the
     thread being stepped should stop.  The assert looks like this:

     infrun.c:5899: internal-error: int finish_step_over(execution_control_state*): Assertion `ecs->event_thread->control.trap_expected' failed.

- For both all-stop and non-stop modes, with displaced stepping:

   + user issues 'stepi',
   + GDB starts the displaced step, moves thread's PC to the
     out-of-line scratch pad, maybe adjusts registers,
   + GDB single steps the thread of interest, [non-stop mode only] all
     other threads are left as they were, either running or stopped.
     In all-stop, all other threads are left stopped.
   + New thread is created running,
   + Initial thread completes its step, GDB re-adjusts its PC,
     restores/releases scratchpad,
   + [non-stop mode only] GDB resumes the thread, now past its
     breakpoint.
   + [all-stop mode only] GDB resumes all threads.

There is one problem with the displaced stepping scenario above:

  3. When the parent thread completed its step, GDB adjusted its PC,
     but did not adjust the child's PC, thus that new child thread
     will continue execution in the scratch pad, invoking undefined
     behavior.  If you're lucky, you see a crash.  If unlucky, the
     inferior gets silently corrupted.

What is needed is for GDB to have more control over whether the new
thread is created running or not.  Issue #1 above requires that the
new thread not be allowed to run until the breakpoint has been
reinserted.  The only way to guarantee this is if the new thread is
held in a stopped state until the single step has completed.  Issue #3
above requires that GDB is informed of when a thread clones itself,
and of what is the child's ptid, so that GDB can fixup both the parent
and the child.

When looking for solutions to this problem I considered how GDB
handles fork/vfork as these have some of the same issues.  The main
difference between fork/vfork and clone is that the clone events are
not reported back to core GDB.  Instead, the clone event is handled
automatically in the target code and the child thread is immediately
set running.

Note we have support for requesting thread creation events out of the
target (TARGET_WAITKIND_THREAD_CREATED).  However, those are reported
for the new/child thread.  That would be sufficient to address in-line
stepping (issue #1), but not for displaced-stepping (issue #3).  To
handle displaced-stepping, we need an event that is reported to the
_parent_ of the clone, as the information about the displaced step is
associated with the clone parent.  TARGET_WAITKIND_THREAD_CREATED
includes no indication of which thread is the parent that spawned the
new child.  In fact, for some targets, like e.g., Windows, it would be
impossible to know which thread that was, as thread creation there
doesn't work by "cloning".

The solution implemented here is to model clone on fork/vfork, and
introduce a new TARGET_WAITKIND_THREAD_CLONED event.  This event is
similar to TARGET_WAITKIND_FORKED and TARGET_WAITKIND_VFORKED, except
that we end up with a new thread in the same process, instead of a new
thread of a new process.  Like FORKED and VFORKED, THREAD_CLONED
waitstatuses have a child_ptid property, and the child is held stopped
until GDB explicitly resumes it.  This addresses the in-line stepping
case (issues #1 and #2).

The infrun code that handles displaced stepping fixup for the child
after a fork/vfork event is thus reused for THREAD_CLONE, with some
minimal conditions added, addressing the displaced stepping case
(issue #3).

The native Linux backend is adjusted to unconditionally report
TARGET_WAITKIND_THREAD_CLONED events to the core.

Following the follow_fork model in core GDB, we introduce a
target_follow_clone target method, which is responsible for making the
new clone child visible to the rest of GDB.

Subsequent patches will add clone events support to the remote
protocol and gdbserver.

displaced_step_in_progress_thread becomes unused with this patch, but
a new use will reappear later in the series.  To avoid deleting it and
readding it back, this patch marks it with attribute unused, and the
latter patch removes the attribute again.  We need to do this because
the function is static, and with no callers, the compiler would warn,
(error with -Werror), breaking the build.

This adds a new gdb.threads/stepi-over-clone.exp testcase, which
exercises stepping over a clone syscall, with displaced stepping vs
inline stepping, and all-stop vs non-stop.  We already test stepping
over clone syscalls with gdb.base/step-over-syscall.exp, but this test
uses pthreads, while the other test uses raw clone, and this one is
more thorough.  The testcase passes on native GNU/Linux, but fails
against GDBserver.  GDBserver will be fixed by a later patch in the
series.

Co-authored-by: Andrew Burgess <aburgess@redhat.com>
Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=19675
Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=27830
Change-Id: I95c06024736384ae8542a67ed9fdf6534c325c8e
Reviewed-By: Andrew Burgess <aburgess@redhat.com>
2023-11-13 14:16:09 +00:00

474 lines
12 KiB
C++

/* Target waitstatus definitions and prototypes.
Copyright (C) 1990-2023 Free Software Foundation, Inc.
This file is part of GDB.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. */
#ifndef TARGET_WAITSTATUS_H
#define TARGET_WAITSTATUS_H
#include "diagnostics.h"
#include "gdbsupport/gdb_signals.h"
/* Stuff for target_wait. */
/* Generally, what has the program done? */
enum target_waitkind
{
/* The program has exited. The exit status is in value.integer. */
TARGET_WAITKIND_EXITED,
/* The program has stopped with a signal. Which signal is in
value.sig. */
TARGET_WAITKIND_STOPPED,
/* The program has terminated with a signal. Which signal is in
value.sig. */
TARGET_WAITKIND_SIGNALLED,
/* The program is letting us know that it dynamically loaded
something (e.g. it called load(2) on AIX). */
TARGET_WAITKIND_LOADED,
/* The program has forked. A "related" process' PTID is in
value.related_pid. I.e., if the child forks, value.related_pid
is the parent's ID. */
TARGET_WAITKIND_FORKED,
/* The program has vforked. A "related" process's PTID is in
value.related_pid. */
TARGET_WAITKIND_VFORKED,
/* The program has exec'ed a new executable file. The new file's
pathname is pointed to by value.execd_pathname. */
TARGET_WAITKIND_EXECD,
/* The program had previously vforked, and now the child is done
with the shared memory region, because it exec'ed or exited.
Note that the event is reported to the vfork parent. This is
only used if GDB did not stay attached to the vfork child,
otherwise, a TARGET_WAITKIND_EXECD or
TARGET_WAITKIND_EXIT|SIGNALLED event associated with the child
has the same effect. */
TARGET_WAITKIND_VFORK_DONE,
/* The program has entered or returned from a system call. On
HP-UX, this is used in the hardware watchpoint implementation.
The syscall's unique integer ID number is in
value.syscall_id. */
TARGET_WAITKIND_SYSCALL_ENTRY,
TARGET_WAITKIND_SYSCALL_RETURN,
/* Nothing happened, but we stopped anyway. This perhaps should
be handled within target_wait, but I'm not sure target_wait
should be resuming the inferior. */
TARGET_WAITKIND_SPURIOUS,
/* An event has occurred, but we should wait again.
Remote_async_wait() returns this when there is an event
on the inferior, but the rest of the world is not interested in
it. The inferior has not stopped, but has just sent some output
to the console, for instance. In this case, we want to go back
to the event loop and wait there for another event from the
inferior, rather than being stuck in the remote_async_wait()
function. This way the event loop is responsive to other events,
like for instance the user typing. */
TARGET_WAITKIND_IGNORE,
/* The target has run out of history information,
and cannot run backward any further. */
TARGET_WAITKIND_NO_HISTORY,
/* There are no resumed children left in the program. */
TARGET_WAITKIND_NO_RESUMED,
/* The thread was cloned. The event's ptid corresponds to the
cloned parent. The cloned child is held stopped at its entry
point, and its ptid is in the event's m_child_ptid. The target
must not add the cloned child to GDB's thread list until
target_ops::follow_clone() is called. */
TARGET_WAITKIND_THREAD_CLONED,
/* The thread was created. */
TARGET_WAITKIND_THREAD_CREATED,
/* The thread has exited. The exit status is in value.integer. */
TARGET_WAITKIND_THREAD_EXITED,
};
/* Determine if KIND represents an event with a new child - a fork,
vfork, or clone. */
static inline bool
is_new_child_status (target_waitkind kind)
{
return (kind == TARGET_WAITKIND_FORKED
|| kind == TARGET_WAITKIND_VFORKED
|| kind == TARGET_WAITKIND_THREAD_CLONED);
}
/* Return KIND as a string. */
static inline const char *
target_waitkind_str (target_waitkind kind)
{
/* Make sure the compiler warns if a new TARGET_WAITKIND enumerator is added
but not handled here. */
DIAGNOSTIC_PUSH
DIAGNOSTIC_ERROR_SWITCH
switch (kind)
{
case TARGET_WAITKIND_EXITED:
return "EXITED";
case TARGET_WAITKIND_STOPPED:
return "STOPPED";
case TARGET_WAITKIND_SIGNALLED:
return "SIGNALLED";
case TARGET_WAITKIND_LOADED:
return "LOADED";
case TARGET_WAITKIND_FORKED:
return "FORKED";
case TARGET_WAITKIND_VFORKED:
return "VFORKED";
case TARGET_WAITKIND_THREAD_CLONED:
return "THREAD_CLONED";
case TARGET_WAITKIND_EXECD:
return "EXECD";
case TARGET_WAITKIND_VFORK_DONE:
return "VFORK_DONE";
case TARGET_WAITKIND_SYSCALL_ENTRY:
return "SYSCALL_ENTRY";
case TARGET_WAITKIND_SYSCALL_RETURN:
return "SYSCALL_RETURN";
case TARGET_WAITKIND_SPURIOUS:
return "SPURIOUS";
case TARGET_WAITKIND_IGNORE:
return "IGNORE";
case TARGET_WAITKIND_NO_HISTORY:
return "NO_HISTORY";
case TARGET_WAITKIND_NO_RESUMED:
return "NO_RESUMED";
case TARGET_WAITKIND_THREAD_CREATED:
return "THREAD_CREATED";
case TARGET_WAITKIND_THREAD_EXITED:
return "THREAD_EXITED";
};
DIAGNOSTIC_POP
gdb_assert_not_reached ("invalid target_waitkind value: %d\n", (int) kind);
}
struct target_waitstatus
{
/* Default constructor. */
target_waitstatus () = default;
/* Copy constructor. */
target_waitstatus (const target_waitstatus &other)
{
m_kind = other.m_kind;
m_value = other.m_value;
if (m_kind == TARGET_WAITKIND_EXECD)
m_value.execd_pathname = xstrdup (m_value.execd_pathname);
}
/* Move constructor. */
target_waitstatus (target_waitstatus &&other)
{
m_kind = other.m_kind;
m_value = other.m_value;
if (m_kind == TARGET_WAITKIND_EXECD)
other.m_value.execd_pathname = nullptr;
other.reset ();
}
/* Copy assignment operator. */
target_waitstatus &operator= (const target_waitstatus &rhs)
{
this->reset ();
m_kind = rhs.m_kind;
m_value = rhs.m_value;
if (m_kind == TARGET_WAITKIND_EXECD)
m_value.execd_pathname = xstrdup (m_value.execd_pathname);
return *this;
}
/* Move assignment operator. */
target_waitstatus &operator= (target_waitstatus &&rhs)
{
this->reset ();
m_kind = rhs.m_kind;
m_value = rhs.m_value;
if (m_kind == TARGET_WAITKIND_EXECD)
rhs.m_value.execd_pathname = nullptr;
rhs.reset ();
return *this;
}
/* Destructor. */
~target_waitstatus ()
{
this->reset ();
}
/* Setters: set the wait status kind plus any associated data. */
target_waitstatus &set_exited (int exit_status)
{
this->reset ();
m_kind = TARGET_WAITKIND_EXITED;
m_value.exit_status = exit_status;
return *this;
}
target_waitstatus &set_stopped (gdb_signal sig)
{
this->reset ();
m_kind = TARGET_WAITKIND_STOPPED;
m_value.sig = sig;
return *this;
}
target_waitstatus &set_signalled (gdb_signal sig)
{
this->reset ();
m_kind = TARGET_WAITKIND_SIGNALLED;
m_value.sig = sig;
return *this;
}
target_waitstatus &set_loaded ()
{
this->reset ();
m_kind = TARGET_WAITKIND_LOADED;
return *this;
}
target_waitstatus &set_forked (ptid_t child_ptid)
{
this->reset ();
m_kind = TARGET_WAITKIND_FORKED;
m_value.child_ptid = child_ptid;
return *this;
}
target_waitstatus &set_vforked (ptid_t child_ptid)
{
this->reset ();
m_kind = TARGET_WAITKIND_VFORKED;
m_value.child_ptid = child_ptid;
return *this;
}
target_waitstatus &set_execd (gdb::unique_xmalloc_ptr<char> execd_pathname)
{
this->reset ();
m_kind = TARGET_WAITKIND_EXECD;
m_value.execd_pathname = execd_pathname.release ();
return *this;
}
target_waitstatus &set_vfork_done ()
{
this->reset ();
m_kind = TARGET_WAITKIND_VFORK_DONE;
return *this;
}
target_waitstatus &set_syscall_entry (int syscall_number)
{
this->reset ();
m_kind = TARGET_WAITKIND_SYSCALL_ENTRY;
m_value.syscall_number = syscall_number;
return *this;
}
target_waitstatus &set_syscall_return (int syscall_number)
{
this->reset ();
m_kind = TARGET_WAITKIND_SYSCALL_RETURN;
m_value.syscall_number = syscall_number;
return *this;
}
target_waitstatus &set_spurious ()
{
this->reset ();
m_kind = TARGET_WAITKIND_SPURIOUS;
return *this;
}
target_waitstatus &set_ignore ()
{
this->reset ();
m_kind = TARGET_WAITKIND_IGNORE;
return *this;
}
target_waitstatus &set_no_history ()
{
this->reset ();
m_kind = TARGET_WAITKIND_NO_HISTORY;
return *this;
}
target_waitstatus &set_no_resumed ()
{
this->reset ();
m_kind = TARGET_WAITKIND_NO_RESUMED;
return *this;
}
target_waitstatus &set_thread_cloned (ptid_t child_ptid)
{
this->reset ();
m_kind = TARGET_WAITKIND_THREAD_CLONED;
m_value.child_ptid = child_ptid;
return *this;
}
target_waitstatus &set_thread_created ()
{
this->reset ();
m_kind = TARGET_WAITKIND_THREAD_CREATED;
return *this;
}
target_waitstatus &set_thread_exited (int exit_status)
{
this->reset ();
m_kind = TARGET_WAITKIND_THREAD_EXITED;
m_value.exit_status = exit_status;
return *this;
}
/* Get the kind of this wait status. */
target_waitkind kind () const
{
return m_kind;
}
/* Getters for the associated data.
Getters can only be used if the wait status is of the appropriate kind.
See the setters above or the assertions below to know which data is
associated to which kind. */
int exit_status () const
{
gdb_assert (m_kind == TARGET_WAITKIND_EXITED
|| m_kind == TARGET_WAITKIND_THREAD_EXITED);
return m_value.exit_status;
}
gdb_signal sig () const
{
gdb_assert (m_kind == TARGET_WAITKIND_STOPPED
|| m_kind == TARGET_WAITKIND_SIGNALLED);
return m_value.sig;
}
ptid_t child_ptid () const
{
gdb_assert (is_new_child_status (m_kind));
return m_value.child_ptid;
}
const char *execd_pathname () const
{
gdb_assert (m_kind == TARGET_WAITKIND_EXECD);
return m_value.execd_pathname;
}
int syscall_number () const
{
gdb_assert (m_kind == TARGET_WAITKIND_SYSCALL_ENTRY
|| m_kind == TARGET_WAITKIND_SYSCALL_RETURN);
return m_value.syscall_number;
}
/* Return a pretty printed form of target_waitstatus.
This is only meant to be used in debug messages, not for user-visible
messages. */
std::string to_string () const;
private:
/* Reset the wait status to its original state. */
void reset ()
{
if (m_kind == TARGET_WAITKIND_EXECD)
xfree (m_value.execd_pathname);
m_kind = TARGET_WAITKIND_IGNORE;
}
target_waitkind m_kind = TARGET_WAITKIND_IGNORE;
/* Additional information about the event. */
union
{
/* Exit status */
int exit_status;
/* Signal number */
enum gdb_signal sig;
/* Forked child pid */
ptid_t child_ptid;
/* execd pathname */
char *execd_pathname;
/* Syscall number */
int syscall_number;
} m_value {};
};
/* Extended reasons that can explain why a target/thread stopped for a
trap signal. */
enum target_stop_reason
{
/* Either not stopped, or stopped for a reason that doesn't require
special tracking. */
TARGET_STOPPED_BY_NO_REASON,
/* Stopped by a software breakpoint. */
TARGET_STOPPED_BY_SW_BREAKPOINT,
/* Stopped by a hardware breakpoint. */
TARGET_STOPPED_BY_HW_BREAKPOINT,
/* Stopped by a watchpoint. */
TARGET_STOPPED_BY_WATCHPOINT,
/* Stopped by a single step finishing. */
TARGET_STOPPED_BY_SINGLE_STEP
};
#endif /* TARGET_WAITSTATUS_H */