public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
* [PATCH v2 00/14] Concurrent displaced stepping
@ 2020-12-02 15:47 Simon Marchi
  2020-12-02 15:47 ` [PATCH v2 01/14] gdb: add inferior_execd observable Simon Marchi
                   ` (14 more replies)
  0 siblings, 15 replies; 40+ messages in thread
From: Simon Marchi @ 2020-12-02 15:47 UTC (permalink / raw)
  To: gdb-patches

This is v2 of

    https://sourceware.org/pipermail/gdb-patches/2020-November/173150.html

I tried to put in each commit log what changed in v2, I hope I didn't
forget anything.  Patches 3 and 4 are new.

Simon Marchi (14):
  gdb: add inferior_execd observable
  gdb: clear inferior displaced stepping state and in-line step-over
    info on exec
  gdb: restore displaced step buffer bytes when another thread forks
  gdb: get rid of get_displaced_stepping_state
  gdb: rename things related to step over chains
  gdb: rename displaced_step_closure to displaced_step_copy_insn_closure
  gdb: rename displaced_step_fixup to displaced_step_finish
  gdb: introduce status enum for displaced step prepare/finish
  gdb: pass inferior to get_linux_inferior_data
  gdb: move displaced stepping types to displaced-stepping.{h,c}
  gdb: move displaced stepping logic to gdbarch, allow starting
    concurrent displaced steps
  gdb: change linux gdbarch data from post to pre-init
  gdb: make displaced stepping implementation capable of managing
    multiple buffers
  gdb: use two displaced step buffers on amd64/Linux

 gdb/Makefile.in                               |   1 +
 gdb/aarch64-linux-tdep.c                      |   3 +-
 gdb/aarch64-tdep.c                            |  18 +-
 gdb/aarch64-tdep.h                            |   5 +-
 gdb/alpha-linux-tdep.c                        |   2 +-
 gdb/amd64-linux-tdep.c                        |  11 +-
 gdb/amd64-tdep.c                              |  23 +-
 gdb/amd64-tdep.h                              |   9 +-
 gdb/arc-linux-tdep.c                          |   2 +-
 gdb/arm-linux-tdep.c                          |  19 +-
 gdb/arm-tdep.c                                | 183 +++----
 gdb/arm-tdep.h                                |  18 +-
 gdb/bfin-linux-tdep.c                         |   2 +-
 gdb/cris-linux-tdep.c                         |   2 +-
 gdb/csky-linux-tdep.c                         |   2 +-
 gdb/displaced-stepping.c                      | 305 +++++++++++
 gdb/displaced-stepping.h                      | 215 ++++++++
 gdb/frv-linux-tdep.c                          |   2 +-
 gdb/gdbarch.c                                 | 117 ++++-
 gdb/gdbarch.h                                 |  47 +-
 gdb/gdbarch.sh                                |  26 +-
 gdb/gdbthread.h                               |  45 +-
 gdb/hppa-linux-tdep.c                         |   2 +-
 gdb/i386-linux-tdep.c                         |  31 +-
 gdb/i386-tdep.c                               |  14 +-
 gdb/i386-tdep.h                               |  12 +-
 gdb/ia64-linux-tdep.c                         |   2 +-
 gdb/inferior.h                                |   1 +
 gdb/infrun.c                                  | 487 ++++++++----------
 gdb/infrun.h                                  |  84 +--
 gdb/jit.c                                     |   9 +-
 gdb/jit.h                                     |   7 -
 gdb/linux-tdep.c                              | 118 ++++-
 gdb/linux-tdep.h                              |  27 +-
 gdb/m32r-linux-tdep.c                         |   2 +-
 gdb/m68k-linux-tdep.c                         |   2 +-
 gdb/microblaze-linux-tdep.c                   |   2 +-
 gdb/mips-linux-tdep.c                         |   2 +-
 gdb/mn10300-linux-tdep.c                      |   2 +-
 gdb/nios2-linux-tdep.c                        |   2 +-
 gdb/observable.c                              |   1 +
 gdb/observable.h                              |   3 +
 gdb/or1k-linux-tdep.c                         |   2 +-
 gdb/ppc-linux-tdep.c                          |   5 +-
 gdb/riscv-linux-tdep.c                        |   2 +-
 gdb/rs6000-aix-tdep.c                         |   6 +-
 gdb/rs6000-tdep.c                             |  94 +++-
 gdb/s390-linux-tdep.c                         |   2 +-
 gdb/s390-tdep.c                               |  22 +-
 gdb/sh-linux-tdep.c                           |   2 +-
 gdb/solib.c                                   |   4 +
 gdb/sparc-linux-tdep.c                        |   2 +-
 gdb/sparc64-linux-tdep.c                      |   2 +-
 .../gdb.arch/amd64-disp-step-avx.exp          |   2 +-
 .../forking-threads-plus-breakpoint.exp       |   2 +-
 .../gdb.threads/non-stop-fair-events.exp      |   2 +-
 .../gdb.threads/step-over-exec-execd.c        |  13 +
 gdb/testsuite/gdb.threads/step-over-exec.c    |  90 ++++
 gdb/testsuite/gdb.threads/step-over-exec.exp  | 106 ++++
 gdb/testsuite/lib/my-syscalls.S               |  46 ++
 gdb/testsuite/lib/my-syscalls.h               |  25 +
 gdb/thread.c                                  |  84 ++-
 gdb/tic6x-linux-tdep.c                        |   2 +-
 gdb/tilegx-linux-tdep.c                       |   2 +-
 gdb/xtensa-linux-tdep.c                       |   2 +-
 65 files changed, 1728 insertions(+), 658 deletions(-)
 create mode 100644 gdb/displaced-stepping.c
 create mode 100644 gdb/displaced-stepping.h
 create mode 100644 gdb/testsuite/gdb.threads/step-over-exec-execd.c
 create mode 100644 gdb/testsuite/gdb.threads/step-over-exec.c
 create mode 100644 gdb/testsuite/gdb.threads/step-over-exec.exp
 create mode 100644 gdb/testsuite/lib/my-syscalls.S
 create mode 100644 gdb/testsuite/lib/my-syscalls.h

-- 
2.29.2


^ permalink raw reply	[flat|nested] 40+ messages in thread

* [PATCH v2 01/14] gdb: add inferior_execd observable
  2020-12-02 15:47 [PATCH v2 00/14] Concurrent displaced stepping Simon Marchi
@ 2020-12-02 15:47 ` Simon Marchi
  2020-12-04  1:45   ` Pedro Alves
  2020-12-02 15:47 ` [PATCH v2 02/14] gdb: clear inferior displaced stepping state and in-line step-over info on exec Simon Marchi
                   ` (13 subsequent siblings)
  14 siblings, 1 reply; 40+ messages in thread
From: Simon Marchi @ 2020-12-02 15:47 UTC (permalink / raw)
  To: gdb-patches; +Cc: Simon Marchi

From: Simon Marchi <simon.marchi@efficios.com>

I want to add another action (clearing displaced stepping state) that
happens when an inferior execs.  I think it would be cleaner to have an
observer for this event, rather than have infrun know about each other
sub-component.

Replace the calls to solib_create_inferior_hook and
jit_inferior_created_hook in follow_exec by observers.

gdb/ChangeLog:

	* observable.h (inferior_execd): Declare new observable.
	* observable.c (inferior_execd): Declare new observable.
	* infrun.c (follow_exec): Notify inferior_execd observer.
	* jit.c (jit_inferior_created_hook): Make static.
	(_initialize_jit): Register inferior_execd observer.
	* jit.h (jit_inferior_created_hook): Remove declaration.
	* solib.c (_initialize_solib): Register inferior_execd observer.

Change-Id: I000cce00094e23baa67df693d912646b6ae38e44
---
 gdb/infrun.c     | 4 +---
 gdb/jit.c        | 9 +++++++--
 gdb/jit.h        | 7 -------
 gdb/observable.c | 1 +
 gdb/observable.h | 3 +++
 gdb/solib.c      | 4 ++++
 6 files changed, 16 insertions(+), 12 deletions(-)

diff --git a/gdb/infrun.c b/gdb/infrun.c
index 2e5e837452da..473a0fb85420 100644
--- a/gdb/infrun.c
+++ b/gdb/infrun.c
@@ -1222,9 +1222,7 @@ follow_exec (ptid_t ptid, const char *exec_file_target)
      registers.  */
   target_find_description ();
 
-  solib_create_inferior_hook (0);
-
-  jit_inferior_created_hook (inf);
+  gdb::observers::inferior_execd.notify (inf);
 
   breakpoint_re_set ();
 
diff --git a/gdb/jit.c b/gdb/jit.c
index fd24d5391596..9deeed7ab597 100644
--- a/gdb/jit.c
+++ b/gdb/jit.c
@@ -49,6 +49,7 @@ static const char jit_break_name[] = "__jit_debug_register_code";
 
 static const char jit_descriptor_name[] = "__jit_debug_descriptor";
 
+static void jit_inferior_created_hook (inferior *inf);
 static void jit_inferior_exit_hook (struct inferior *inf);
 
 /* An unwinder is registered for every gdbarch.  This key is used to
@@ -1230,9 +1231,12 @@ jit_inferior_init (inferior *inf)
     }
 }
 
-/* See jit.h.  */
+/* Looks for the descriptor and registration symbols and breakpoints
+   the registration function.  If it finds both, it registers all the
+   already JITed code.  If it has already found the symbols, then it
+   doesn't try again.  */
 
-void
+static void
 jit_inferior_created_hook (inferior *inf)
 {
   jit_inferior_init (inf);
@@ -1337,6 +1341,7 @@ _initialize_jit ()
 			     &setdebuglist, &showdebuglist);
 
   gdb::observers::inferior_created.attach (jit_inferior_created_hook);
+  gdb::observers::inferior_execd.attach (jit_inferior_created_hook);
   gdb::observers::inferior_exit.attach (jit_inferior_exit_hook);
   gdb::observers::breakpoint_deleted.attach (jit_breakpoint_deleted);
 
diff --git a/gdb/jit.h b/gdb/jit.h
index 6f972a6e077a..969380607337 100644
--- a/gdb/jit.h
+++ b/gdb/jit.h
@@ -103,13 +103,6 @@ struct jited_objfile_data
   CORE_ADDR addr;
 };
 
-/* Looks for the descriptor and registration symbols and breakpoints
-   the registration function.  If it finds both, it registers all the
-   already JITed code.  If it has already found the symbols, then it
-   doesn't try again.  */
-
-extern void jit_inferior_created_hook (inferior *inf);
-
 /* Re-establish the jit breakpoint(s).  */
 
 extern void jit_breakpoint_re_set (void);
diff --git a/gdb/observable.c b/gdb/observable.c
index 81aa392cc21f..231f955fa260 100644
--- a/gdb/observable.c
+++ b/gdb/observable.c
@@ -43,6 +43,7 @@ DEFINE_OBSERVABLE (command_error);
 DEFINE_OBSERVABLE (target_changed);
 DEFINE_OBSERVABLE (executable_changed);
 DEFINE_OBSERVABLE (inferior_created);
+DEFINE_OBSERVABLE (inferior_execd);
 DEFINE_OBSERVABLE (record_changed);
 DEFINE_OBSERVABLE (solib_loaded);
 DEFINE_OBSERVABLE (solib_unloaded);
diff --git a/gdb/observable.h b/gdb/observable.h
index 9114b28d04f0..1dce6746ff39 100644
--- a/gdb/observable.h
+++ b/gdb/observable.h
@@ -89,6 +89,9 @@ extern observable<> executable_changed;
    information on the inferior has been printed.  */
 extern observable<inferior */* inferior */> inferior_created;
 
+/* The inferior INF has exec'ed a new executable file.  */
+extern observable<struct inferior */* inf */> inferior_execd;
+
 /* The status of process record for inferior inferior in gdb has
    changed.  The process record is started if STARTED is true, and
    the process record is stopped if STARTED is false.
diff --git a/gdb/solib.c b/gdb/solib.c
index b11c6f389009..1f6e91599a56 100644
--- a/gdb/solib.c
+++ b/gdb/solib.c
@@ -1557,6 +1557,10 @@ _initialize_solib ()
   solib_data = gdbarch_data_register_pre_init (solib_init);
 
   gdb::observers::free_objfile.attach (remove_user_added_objfile);
+  gdb::observers::inferior_execd.attach ([] (inferior *inf)
+    {
+      solib_create_inferior_hook (0);
+    });
 
   add_com ("sharedlibrary", class_files, sharedlibrary_command,
 	   _("Load shared object library symbols for files matching REGEXP."));
-- 
2.29.2


^ permalink raw reply	[flat|nested] 40+ messages in thread

* [PATCH v2 02/14] gdb: clear inferior displaced stepping state and in-line step-over info on exec
  2020-12-02 15:47 [PATCH v2 00/14] Concurrent displaced stepping Simon Marchi
  2020-12-02 15:47 ` [PATCH v2 01/14] gdb: add inferior_execd observable Simon Marchi
@ 2020-12-02 15:47 ` Simon Marchi
  2020-12-02 17:16   ` Simon Marchi
  2020-12-04  1:48   ` Pedro Alves
  2020-12-02 15:47 ` [PATCH v2 03/14] gdb: restore displaced step buffer bytes when another thread forks Simon Marchi
                   ` (12 subsequent siblings)
  14 siblings, 2 replies; 40+ messages in thread
From: Simon Marchi @ 2020-12-02 15:47 UTC (permalink / raw)
  To: gdb-patches; +Cc: Simon Marchi

From: Simon Marchi <simon.marchi@efficios.com>

New in v2:

- A test!
- I found (from the te st) that in-line step over exec hangs too,
  because the step over info is never cleared.  So, clear in-line step
  over info as well in infrun_inferior_execd.  Not sure how right it is,
  but it makes the test pass.

When a process does an exec, all its program space is replaced with the
newly loaded executable.  All non-main threads disappear and the main
thread starts executing at the entry point of the new executable.

Things can go wrong if a displaced step operation is in progress while
we process the exec event.

If the main thread is the one executing the displaced step: when that
thread (now executing in the new executable) stops somewhere (say, at a
breakpoint), displaced_step_fixup will run and clear up the state.  We
will execute the "fixup" phase for the instruction we single-stepped in
the old program space.  We are now in a completely different context,
so doing the fixup may corrupt the state.

If it is a non-main thread that is doing the displaced step: while
handling the exec event, GDB deletes the thread_info representing that
thread (since the thread doesn't exist in the inferior after the exec).
But inferior::displaced_step_state::step_thread will still point to it.
When handling events later, this condition, in displaced_step_fixup,
will likely never be true:

    /* Was this event for the thread we displaced?  */
    if (displaced->step_thread != event_thread)
      return 0;

... since displaced->step_thread points to a deleted thread (unless that
storage gets re-used for a new thread_info, but that wouldn't be good
either).  This effectively makes the displaced stepping buffer occupied
for ever.  When a thread in the new program space will want to do a
displaced step, it will wait for ever.

I think we simply need to reset the displaced stepping state of the
inferior on exec.  Everything execution-related that existed before the
exec is now gone.

Similarly, if a thread does an in-line step over an exec syscall
instruction, nothing clears the in-line step over info when the event is
handled.  So it the in-line step over info stays there indefinitely, and
things hang because we can never start another step over.  To fix this,
I added a call to clear_step_over_info in infrun_inferior_execd.

Add a test with a program with two threads that does an exec.  The test
includes the following axis:

- whether it's the leader thread or the other thread that does the exec.

- whether the exec'r and exec'd program have different text segment
  addresses.  This is to hopefully catch cases where the displaced
  stepping info doesn't get reset, and GDB later tries to restore bytes
  of the old address space in the new address space.  If the mapped
  addresses are different, we should get some memory error.   This
  happens without the patch applied:

  $ ./gdb -q -nx --data-directory=data-directory testsuite/outputs/gdb.threads/step-over-exec/step-over-exec-execr-thread-leader-diff-text-segs-true -ex "b main" -ex r -ex "b my_execve_syscall if 0"  -ex "set displaced-stepping on"
  ...
  Breakpoint 1, main (argc=1, argv=0x7fffffffde38) at /home/simark/src/binutils-gdb/gdb/testsuite/gdb.threads/step-over-exec.c:69
  69        argv0 = argv[0];
  Breakpoint 2 at 0x60133a: file /home/simark/src/binutils-gdb/gdb/testsuite/lib/my-syscalls.S, line 34.
  (gdb) c
  Continuing.
  [New Thread 0x7ffff7c62640 (LWP 1455423)]
  Leader going in exec.
  Exec-ing /home/simark/build/binutils-gdb/gdb/testsuite/outputs/gdb.threads/step-over-exec/step-over-exec-execr-thread-leader-diff-text-segs-true-execd
  [Thread 0x7ffff7c62640 (LWP 1455423) exited]
  process 1455418 is executing new program: /home/simark/build/binutils-gdb/gdb/testsuite/outputs/gdb.threads/step-over-exec/step-over-exec-execr-thread-leader-diff-text-segs-true-execd
  Error in re-setting breakpoint 2: Function "my_execve_syscall" not defined.
  No unwaited-for children left.
  (gdb) n
  Single stepping until exit from function _start,
  which has no line number information.
  Cannot access memory at address 0x6010d2
  (gdb)

- Whether displaced stepping is allowed or not, so that we end up
  testing both displaced stepping and in-line stepping on arches that do
  support displaced stepping (otherwise, it just tests in-line stepping
  twice I suppose)

To be able to precisely put a breakpoint on the syscall instruction, I
added a small assembly file (lib/my-syscalls.S) that contains minimal
Linux syscall wrappers.  I prefer that to the strategy used in
gdb.base/step-over-syscall.exp, which is to stepi into the glibc wrapper
until we find something that looks like a syscall instruction, I find
that more predictable.

gdb/ChangeLog:

	* infrun.c (infrun_inferior_execd): New function.
	(_initialize_infrun): Attach inferior_execd observer.

gdb/testsuite/ChangeLog:

	* gdb.threads/step-over-exec.exp: New.
	* gdb.threads/step-over-exec.c: New.
	* gdb.threads/step-over-exec-execd.c: New.
	* lib/my-syscalls.S: New.
	* lib/my-syscalls.h: New.

Change-Id: I1bbc8538e683f53af5b980091849086f4fec5ff9
---
 gdb/infrun.c                                  |  16 +++
 .../gdb.threads/step-over-exec-execd.c        |  13 +++
 gdb/testsuite/gdb.threads/step-over-exec.c    |  90 +++++++++++++++
 gdb/testsuite/gdb.threads/step-over-exec.exp  | 106 ++++++++++++++++++
 gdb/testsuite/lib/my-syscalls.S               |  46 ++++++++
 gdb/testsuite/lib/my-syscalls.h               |  25 +++++
 6 files changed, 296 insertions(+)
 create mode 100644 gdb/testsuite/gdb.threads/step-over-exec-execd.c
 create mode 100644 gdb/testsuite/gdb.threads/step-over-exec.c
 create mode 100644 gdb/testsuite/gdb.threads/step-over-exec.exp
 create mode 100644 gdb/testsuite/lib/my-syscalls.S
 create mode 100644 gdb/testsuite/lib/my-syscalls.h

diff --git a/gdb/infrun.c b/gdb/infrun.c
index 473a0fb85420..0e0a7a691c43 100644
--- a/gdb/infrun.c
+++ b/gdb/infrun.c
@@ -1528,6 +1528,21 @@ infrun_inferior_exit (struct inferior *inf)
   inf->displaced_step_state.reset ();
 }
 
+static void
+infrun_inferior_execd (inferior *inf)
+{
+  /* If a thread was doing a displaced step in this inferior at the moment of
+     the exec, it no longer exists.  Even if the exec'ing thread was the one
+     doing a displaced step, we don't want to to any fixup nor restore displaced
+     stepping buffer bytes.  */
+  inf->displaced_step_state.reset ();
+
+  /* Since an in-line step is done with everything else stopped, if there was
+     one in progress at the time of the exec, it must have been the exec'ing
+     thread.  */
+  clear_step_over_info ();
+}
+
 /* If ON, and the architecture supports it, GDB will use displaced
    stepping to step over breakpoints.  If OFF, or if the architecture
    doesn't support it, GDB will instead use the traditional
@@ -9509,6 +9524,7 @@ enabled by default on some platforms."),
   gdb::observers::thread_stop_requested.attach (infrun_thread_stop_requested);
   gdb::observers::thread_exit.attach (infrun_thread_thread_exit);
   gdb::observers::inferior_exit.attach (infrun_inferior_exit);
+  gdb::observers::inferior_execd.attach (infrun_inferior_execd);
 
   /* Explicitly create without lookup, since that tries to create a
      value with a void typed value, and when we get here, gdbarch
diff --git a/gdb/testsuite/gdb.threads/step-over-exec-execd.c b/gdb/testsuite/gdb.threads/step-over-exec-execd.c
new file mode 100644
index 000000000000..f47fd79b80bd
--- /dev/null
+++ b/gdb/testsuite/gdb.threads/step-over-exec-execd.c
@@ -0,0 +1,13 @@
+int a_variable_in_execd = 0;
+
+static void
+foo (void)
+{
+}
+
+int
+main (void)
+{
+  foo ();
+  return a_variable_in_execd;
+}
diff --git a/gdb/testsuite/gdb.threads/step-over-exec.c b/gdb/testsuite/gdb.threads/step-over-exec.c
new file mode 100644
index 000000000000..a043e8d2535f
--- /dev/null
+++ b/gdb/testsuite/gdb.threads/step-over-exec.c
@@ -0,0 +1,90 @@
+/* This testcase is part of GDB, the GNU debugger.
+
+   Copyright 2020 Free Software Foundation, Inc.
+
+   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/>.  */
+
+#include <unistd.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <pthread.h>
+
+#include "../lib/my-syscalls.h"
+
+#if (!defined(LEADER_DOES_EXEC) && !defined(OTHER_DOES_EXEC) \
+     || defined(LEADER_DOES_EXEC) && defined(OTHER_DOES_EXEC))
+# error "Exactly one of LEADER_DOES_EXEC and OTHER_DOES_EXEC must be defined."
+#endif
+
+
+static char *argv0;
+static pthread_barrier_t barrier;
+
+static void
+do_the_exec (void)
+{
+  char *execd_path = (char *) malloc (strlen (argv0) + sizeof ("-execd"));
+  sprintf (execd_path, "%s-execd", argv0);
+  char *argv[] = { execd_path, NULL };
+
+  printf ("Exec-ing %s\n", execd_path);
+
+  extern char **environ;
+  my_execve (execd_path, argv, environ);
+
+  printf ("Exec failed :(\n");
+  abort ();
+}
+
+static void *
+thread_func (void *arg)
+{
+  pthread_barrier_wait (&barrier);
+#ifdef OTHER_DOES_EXEC
+  printf ("Other going in exec.\n");
+  do_the_exec ();
+#endif
+
+  /* Just make sure the thread does not exit when the leader does the exec.  */
+  pthread_barrier_wait (&barrier);
+
+  return NULL;
+}
+
+int
+main (int argc, char *argv[])
+{
+  argv0 = argv[0];
+
+  int ret = pthread_barrier_init (&barrier, NULL, 2);
+  if (ret != 0)
+    abort ();
+
+  pthread_t thread;
+  ret = pthread_create (&thread, NULL, thread_func, argv[0]);
+  if (ret != 0)
+    abort ();
+
+  pthread_barrier_wait (&barrier);
+
+#ifdef LEADER_DOES_EXEC
+  printf ("Leader going in exec.\n");
+  do_the_exec ();
+#endif
+
+  pthread_join (thread, NULL);
+
+  return 0;
+}
diff --git a/gdb/testsuite/gdb.threads/step-over-exec.exp b/gdb/testsuite/gdb.threads/step-over-exec.exp
new file mode 100644
index 000000000000..72cb024a2b1c
--- /dev/null
+++ b/gdb/testsuite/gdb.threads/step-over-exec.exp
@@ -0,0 +1,106 @@
+# Copyright 2020 Free Software Foundation, Inc.
+
+# 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/>.
+
+# Test stepping over an exec syscall instruction in a multi-threaded program.
+
+standard_testfile .c -execd.c
+
+set syscalls_src $srcdir/lib/my-syscalls.S
+
+# EXECR_THREAD is "leader" or "other", and decides which thread does the exec.
+#
+# If DIFFERENT_TEXT_SEGMENTS is true, the exec'er and exec'd program are
+# compiled with different, explicit text segment addresses.  This makes it so
+# the address of the displaced stepping buffer in the old executable is likely
+# not accessible in the new executable.  This might catch cases where GDB tries
+# (wrongfully) to restore the bytes saved from the old executable in the new
+# executable.
+#
+# DISPLACED_STEPPING is "auto" or "off" and controls the value of "set
+# displaced-stepping".
+
+proc do_test { execr_thread different_text_segments displaced_stepping } {
+    global srcdir subdir srcfile srcfile2 binfile
+    global syscalls_src
+    global decimal hex
+
+    set execr_srcs [list $srcdir/$subdir/$srcfile $syscalls_src]
+    set execd_srcs [list $srcdir/$subdir/$srcfile2]
+
+    # Generate unique filenames for each case.
+    set execr_binfile $binfile-execr-thread-$execr_thread-diff-text-segs-$different_text_segments
+    set execd_binfile $execr_binfile-execd
+
+    set execr_opts [list debug]
+    set execd_opts [list debug]
+
+    if { $different_text_segments } {
+	lappend execr_opts "ldflags=-Wl,-Ttext-segment=0x600000"
+	lappend execd_opts "ldflags=-Wl,-Ttext-segment=0x800000"
+    }
+
+    if { $execr_thread == "leader" } {
+	lappend execr_opts "additional_flags=-DLEADER_DOES_EXEC"
+    } elseif { $execr_thread == "other" } {
+	lappend execr_opts "additional_flags=-DOTHER_DOES_EXEC"
+    } else {
+	error "Invalid execr_thread value: $execr_thread."
+    }
+
+    # Compile execr binary (the one that does the exec).
+    if {[gdb_compile_pthreads $execr_srcs $execr_binfile executable $execr_opts] != "" } {
+	return -1
+    }
+
+    # Compile the second binary (the one that gets exec'd).
+    if {[gdb_compile $execd_srcs $execd_binfile executable $execd_opts] != "" } {
+	return -1
+    }
+
+    clean_restart ${execr_binfile}
+
+    gdb_test_no_output "set displaced-stepping $displaced_stepping"
+
+    if ![runto_main] {
+	return
+    }
+
+    # Leave breakpoint main inserted, we expect to hit it after exec.
+
+    # This breakpoint will be stepped by whatever thread does the exec.
+    gdb_test "break my_execve_syscall if 0" "Breakpoint $decimal at $hex.*"
+
+    # Continue across exec to main.
+    gdb_test "continue" "process $decimal is executing new program: .* hit Breakpoint $decimal, main .*" \
+	"continue across exec"
+
+    # Just to confirm we are indeed in the execd program.
+    gdb_test "print a_variable_in_execd" " = 0"
+
+    # Continue execution to make sure we can step over the breakpoint on main.
+    # It would be nice to use gdb_continue_to_end to ensure the program can
+    # exit properly, but it hangs due to PR gdb/26995.
+    gdb_breakpoint foo
+    gdb_test "continue" "Breakpoint $decimal, foo .*" \
+	"continue to foo"
+}
+
+foreach_with_prefix displaced_stepping {auto off} {
+    foreach_with_prefix different_text_segments {true false} {
+	foreach_with_prefix execr_thread {leader other} {
+	    do_test $execr_thread $different_text_segments $displaced_stepping
+	}
+    }
+}
diff --git a/gdb/testsuite/lib/my-syscalls.S b/gdb/testsuite/lib/my-syscalls.S
new file mode 100644
index 000000000000..44ed00cb04b3
--- /dev/null
+++ b/gdb/testsuite/lib/my-syscalls.S
@@ -0,0 +1,46 @@
+/* This testcase is part of GDB, the GNU debugger.
+
+   Copyright 2020 Free Software Foundation, Inc.
+
+   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/>.  */
+
+
+/* This file implements simple Linux syscall wrappers, to be used by tests that
+   need to know exactly where the syscall instructions are.  */
+
+#include <asm/unistd.h>
+
+/* int my_execve (const char *file, char *argv[], char *envp[]);  */
+
+.global my_execve
+my_execve:
+
+#if defined(__x86_64__)
+
+	mov $__NR_execve, %rax
+	/* rdi, rsi and rdx already contain the right arguments.  */
+my_execve_syscall:
+	syscall
+	ret
+
+#elif defined(__aarch64__)
+
+	mov x8, #__NR_execve
+	/* x0, x1 and x2 already contain the right arguments.  */
+my_execve_syscall:
+	svc #0
+
+#else
+# error "Unsupported architecture"
+#endif
diff --git a/gdb/testsuite/lib/my-syscalls.h b/gdb/testsuite/lib/my-syscalls.h
new file mode 100644
index 000000000000..8a82780e35bb
--- /dev/null
+++ b/gdb/testsuite/lib/my-syscalls.h
@@ -0,0 +1,25 @@
+/* This testcase is part of GDB, the GNU debugger.
+
+   Copyright 2020 Free Software Foundation, Inc.
+
+   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 MY_SYSCALLS_H
+#define MY_SYSCALLS_H
+
+/* Declarations for syscall wrappers implemented in my-syscalls.S.  */
+
+int my_execve (const char *file, char *argv[], char *envp[]);
+
+#endif /* MY_SYSCALLS_H */
-- 
2.29.2


^ permalink raw reply	[flat|nested] 40+ messages in thread

* [PATCH v2 03/14] gdb: restore displaced step buffer bytes when another thread forks
  2020-12-02 15:47 [PATCH v2 00/14] Concurrent displaced stepping Simon Marchi
  2020-12-02 15:47 ` [PATCH v2 01/14] gdb: add inferior_execd observable Simon Marchi
  2020-12-02 15:47 ` [PATCH v2 02/14] gdb: clear inferior displaced stepping state and in-line step-over info on exec Simon Marchi
@ 2020-12-02 15:47 ` Simon Marchi
  2020-12-04  1:48   ` Pedro Alves
  2020-12-02 15:47 ` [PATCH v2 04/14] gdb: get rid of get_displaced_stepping_state Simon Marchi
                   ` (11 subsequent siblings)
  14 siblings, 1 reply; 40+ messages in thread
From: Simon Marchi @ 2020-12-02 15:47 UTC (permalink / raw)
  To: gdb-patches

In handle_inferior_event, where we handle forks, we make sure to restore
the bytes of the displaced stepping buffer in the child's address
space.  However, we only do it when the forking thread was the one
doing a displaced step.  It could happen that a thread forks while
another one is doing a displaced step.  In this case, we also need to
restore the bytes in the child.

Move the byte-restoring code outside of the condition that checks
whether the event thread was displaced stepping.

gdb/ChangeLog:

	* infrun.c (handle_inferior_event): Restore displaced step
	buffer bytes in child process when handling fork, even if fork
	happened in another thread than the displaced-stepping one.

Change-Id: Ibb0daaeb123aba03f4fb4b4d820754eb2436bc69
---
 gdb/infrun.c | 28 +++++++++++++++-------------
 1 file changed, 15 insertions(+), 13 deletions(-)

diff --git a/gdb/infrun.c b/gdb/infrun.c
index 0e0a7a691c43..5c383b4d8a12 100644
--- a/gdb/infrun.c
+++ b/gdb/infrun.c
@@ -5302,25 +5302,27 @@ handle_inferior_event (struct execution_control_state *ecs)
       {
 	struct regcache *regcache = get_thread_regcache (ecs->event_thread);
 	struct gdbarch *gdbarch = regcache->arch ();
+	inferior *parent_inf = find_inferior_ptid (ecs->target, ecs->ptid);
 
-	/* If checking displaced stepping is supported, and thread
-	   ecs->ptid is displaced stepping.  */
+	/* If this is a fork (child gets its own address space copy) and the
+	   displaced step buffer was in use at the time of the fork, restore
+	   displaced step buffer bytes in the child process.  */
+	if (ecs->ws.kind == TARGET_WAITKIND_FORKED)
+	  {
+	    displaced_step_inferior_state *displaced
+	      = get_displaced_stepping_state (parent_inf);
+
+	    if (displaced->step_thread != nullptr)
+	      displaced_step_restore (displaced, ecs->ws.value.related_pid);
+	  }
+
+	/* If displaced stepping is supported, and thread ecs->ptid is
+	   displaced stepping.  */
 	if (displaced_step_in_progress_thread (ecs->event_thread))
 	  {
-	    struct inferior *parent_inf
-	      = find_inferior_ptid (ecs->target, ecs->ptid);
 	    struct regcache *child_regcache;
 	    CORE_ADDR parent_pc;
 
-	    if (ecs->ws.kind == TARGET_WAITKIND_FORKED)
-	      {
-		struct displaced_step_inferior_state *displaced
-		  = get_displaced_stepping_state (parent_inf);
-
-		/* Restore scratch pad for child process.  */
-		displaced_step_restore (displaced, ecs->ws.value.related_pid);
-	      }
-
 	    /* GDB has got TARGET_WAITKIND_FORKED or TARGET_WAITKIND_VFORKED,
 	       indicating that the displaced stepping of syscall instruction
 	       has been done.  Perform cleanup for parent process here.  Note
-- 
2.29.2


^ permalink raw reply	[flat|nested] 40+ messages in thread

* [PATCH v2 04/14] gdb: get rid of get_displaced_stepping_state
  2020-12-02 15:47 [PATCH v2 00/14] Concurrent displaced stepping Simon Marchi
                   ` (2 preceding siblings ...)
  2020-12-02 15:47 ` [PATCH v2 03/14] gdb: restore displaced step buffer bytes when another thread forks Simon Marchi
@ 2020-12-02 15:47 ` Simon Marchi
  2020-12-04  1:48   ` Pedro Alves
  2020-12-02 15:47 ` [PATCH v2 05/14] gdb: rename things related to step over chains Simon Marchi
                   ` (10 subsequent siblings)
  14 siblings, 1 reply; 40+ messages in thread
From: Simon Marchi @ 2020-12-02 15:47 UTC (permalink / raw)
  To: gdb-patches

Remove function get_displaced_stepping_state.  When it was introduced,
inferiors' dispalced stepping state were kept in a linked list in
infrun.c, so it was handy.  Nowadays, the state is kept inside struct
inferior directly, so we can just access it directly instead.

gdb/ChangeLog:

	* infrun.c (get_displaced_stepping_state): Remove, change
	callers to access the field directly.

Change-Id: I9a733e32e29c7ebf856ab0befe1076bbb8c7af69
---
 gdb/infrun.c | 44 ++++++++++++++------------------------------
 1 file changed, 14 insertions(+), 30 deletions(-)

diff --git a/gdb/infrun.c b/gdb/infrun.c
index 5c383b4d8a12..7bde61597d69 100644
--- a/gdb/infrun.c
+++ b/gdb/infrun.c
@@ -1463,14 +1463,6 @@ step_over_info_valid_p (void)
 
 displaced_step_closure::~displaced_step_closure () = default;
 
-/* Get the displaced stepping state of inferior INF.  */
-
-static displaced_step_inferior_state *
-get_displaced_stepping_state (inferior *inf)
-{
-  return &inf->displaced_step_state;
-}
-
 /* Returns true if any inferior has a thread doing a displaced
    step.  */
 
@@ -1493,7 +1485,7 @@ displaced_step_in_progress_thread (thread_info *thread)
 {
   gdb_assert (thread != NULL);
 
-  return get_displaced_stepping_state (thread->inf)->step_thread == thread;
+  return thread->inf->displaced_step_state.step_thread == thread;
 }
 
 /* Return true if INF has a thread doing a displaced step.  */
@@ -1501,7 +1493,7 @@ displaced_step_in_progress_thread (thread_info *thread)
 static bool
 displaced_step_in_progress (inferior *inf)
 {
-  return get_displaced_stepping_state (inf)->step_thread != nullptr;
+  return inf->displaced_step_state.step_thread != nullptr;
 }
 
 /* If inferior is in displaced stepping, and ADDR equals to starting address
@@ -1511,13 +1503,13 @@ displaced_step_in_progress (inferior *inf)
 struct displaced_step_closure*
 get_displaced_step_closure_by_addr (CORE_ADDR addr)
 {
-  displaced_step_inferior_state *displaced
-    = get_displaced_stepping_state (current_inferior ());
+  displaced_step_inferior_state &displaced
+    = current_inferior ()->displaced_step_state;
 
   /* If checking the mode of displaced instruction in copy area.  */
-  if (displaced->step_thread != nullptr
-      && displaced->step_copy == addr)
-    return displaced->step_closure.get ();
+  if (displaced.step_thread != nullptr
+      && displaced.step_copy == addr)
+    return displaced.step_closure.get ();
 
   return NULL;
 }
@@ -1606,12 +1598,9 @@ use_displaced_stepping (thread_info *tp)
   if (find_record_target () != nullptr)
     return false;
 
-  displaced_step_inferior_state *displaced_state
-    = get_displaced_stepping_state (tp->inf);
-
   /* If displaced stepping failed before for this inferior, don't bother trying
      again.  */
-  if (displaced_state->failed_before)
+  if (tp->inf->displaced_step_state.failed_before)
     return false;
 
   return true;
@@ -1690,8 +1679,7 @@ displaced_step_prepare_throw (thread_info *tp)
   /* We have to displaced step one thread at a time, as we only have
      access to a single scratch space per inferior.  */
 
-  displaced_step_inferior_state *displaced
-    = get_displaced_stepping_state (tp->inf);
+  displaced_step_inferior_state *displaced = &tp->inf->displaced_step_state;
 
   if (displaced->step_thread != nullptr)
     {
@@ -1795,8 +1783,6 @@ displaced_step_prepare (thread_info *thread)
     }
   catch (const gdb_exception_error &ex)
     {
-      struct displaced_step_inferior_state *displaced_state;
-
       if (ex.error != MEMORY_ERROR
 	  && ex.error != NOT_SUPPORTED_ERROR)
 	throw;
@@ -1813,9 +1799,7 @@ displaced_step_prepare (thread_info *thread)
 	}
 
       /* Disable further displaced stepping attempts.  */
-      displaced_state
-	= get_displaced_stepping_state (thread->inf);
-      displaced_state->failed_before = 1;
+      thread->inf->displaced_step_state.failed_before = 1;
     }
 
   return prepared;
@@ -1857,8 +1841,8 @@ displaced_step_restore (struct displaced_step_inferior_state *displaced,
 static int
 displaced_step_fixup (thread_info *event_thread, enum gdb_signal signal)
 {
-  struct displaced_step_inferior_state *displaced
-    = get_displaced_stepping_state (event_thread->inf);
+  displaced_step_inferior_state *displaced
+    = &event_thread->inf->displaced_step_state;
   int ret;
 
   /* Was this event for the thread we displaced?  */
@@ -3629,7 +3613,7 @@ prepare_for_detach (void)
   struct inferior *inf = current_inferior ();
   ptid_t pid_ptid = ptid_t (inf->pid);
 
-  displaced_step_inferior_state *displaced = get_displaced_stepping_state (inf);
+  displaced_step_inferior_state *displaced = &inf->displaced_step_state;
 
   /* Is any thread of this process displaced stepping?  If not,
      there's nothing else to do.  */
@@ -5310,7 +5294,7 @@ handle_inferior_event (struct execution_control_state *ecs)
 	if (ecs->ws.kind == TARGET_WAITKIND_FORKED)
 	  {
 	    displaced_step_inferior_state *displaced
-	      = get_displaced_stepping_state (parent_inf);
+	      = &parent_inf->displaced_step_state;
 
 	    if (displaced->step_thread != nullptr)
 	      displaced_step_restore (displaced, ecs->ws.value.related_pid);
-- 
2.29.2


^ permalink raw reply	[flat|nested] 40+ messages in thread

* [PATCH v2 05/14] gdb: rename things related to step over chains
  2020-12-02 15:47 [PATCH v2 00/14] Concurrent displaced stepping Simon Marchi
                   ` (3 preceding siblings ...)
  2020-12-02 15:47 ` [PATCH v2 04/14] gdb: get rid of get_displaced_stepping_state Simon Marchi
@ 2020-12-02 15:47 ` Simon Marchi
  2020-12-04  1:49   ` Pedro Alves
  2020-12-02 15:47 ` [PATCH v2 06/14] gdb: rename displaced_step_closure to displaced_step_copy_insn_closure Simon Marchi
                   ` (9 subsequent siblings)
  14 siblings, 1 reply; 40+ messages in thread
From: Simon Marchi @ 2020-12-02 15:47 UTC (permalink / raw)
  To: gdb-patches; +Cc: Simon Marchi

From: Simon Marchi <simon.marchi@efficios.com>

New in v2: comment and formatting fixes.

Rename step_over_queue_head to global_thread_step_over_chain_head, to
make it more obvious when reading code that we are touching the global
queue.  Rename all functions that operate on it to have "global" in
their name, to make it clear on which chain they operate on.  Also, in a
subsequent patch, we'll need both global and non-global versions of
these functions, so it will be easier to do the distinction if they are
named properly.

Normalize the naming to use "chain" everywhere instead of sometimes
"queue", sometimes "chain".

I also reworded a few comments in gdbthread.h.  They implied that the
step over chain is per-inferior, when in reality there is only one
global chain, not one per inferior, as far as I understand.

gdb/ChangeLog:

        * gdbthread.h (thread_step_over_chain_enqueue): Rename to...
        (global_thread_step_over_chain_enqueue): ... this.  Update all
	users.
        (thread_step_over_chain_remove): Rename to...
        (global_thread_step_over_chain_remove): ... this.  Update all
	users.
        (thread_step_over_chain_next): Rename to...
        (global_thread_step_over_chain_next): ... this.  Update all
	users.
        * infrun.h (step_over_queue_head): Rename to...
	(global_thread_step_over_chain_head): ... this.  Update all
	users.
        * infrun.c (step_over_queue_head): Rename to...
	(global_thread_step_over_chain_head): ... this.  Update all
	users.
        * thread.c (step_over_chain_remove): Rename to...
	(thread_step_over_chain_remove): ... this.  Update all users.
	(thread_step_over_chain_next): Rename to...
        (global_thread_step_over_chain_next): ... this.  Update all
	users.
        (thread_step_over_chain_enqueue): Rename to...
        (global_thread_step_over_chain_enqueue): ... this.  Update all
	users.
        (thread_step_over_chain_remove): Rename to...
        (global_thread_step_over_chain_remove): ... this.  Update all
	users.

Change-Id: Iabbf57d83c01321ca199d83fadb57f5b04e4d6d9
---
 gdb/gdbthread.h | 16 ++++++++--------
 gdb/infrun.c    | 26 +++++++++++++-------------
 gdb/infrun.h    |  4 ++--
 gdb/thread.c    | 20 ++++++++++----------
 4 files changed, 33 insertions(+), 33 deletions(-)

diff --git a/gdb/gdbthread.h b/gdb/gdbthread.h
index 630727e2fb50..1eecb989a79e 100644
--- a/gdb/gdbthread.h
+++ b/gdb/gdbthread.h
@@ -741,20 +741,20 @@ extern value *get_last_thread_stack_temporary (struct thread_info *tp);
 extern bool value_in_thread_stack_temporaries (struct value *,
 					       struct thread_info *thr);
 
-/* Add TP to the end of its inferior's pending step-over chain.  */
+/* Add TP to the end of the global pending step-over chain.  */
 
-extern void thread_step_over_chain_enqueue (struct thread_info *tp);
+extern void global_thread_step_over_chain_enqueue (thread_info *tp);
 
-/* Remove TP from its inferior's pending step-over chain.  */
+/* Remove TP from the global pending step-over chain.  */
 
-extern void thread_step_over_chain_remove (struct thread_info *tp);
+extern void global_thread_step_over_chain_remove (thread_info *tp);
 
-/* Return the next thread in the step-over chain starting at TP.  NULL
-   if TP is the last entry in the chain.  */
+/* Return the thread following TP in the global step-over chain, or NULL if TP
+   is the last entry in the chain.  */
 
-extern struct thread_info *thread_step_over_chain_next (struct thread_info *tp);
+extern thread_info *global_thread_step_over_chain_next (thread_info *tp);
 
-/* Return true if TP is in the step-over chain.  */
+/* Return true if TP is in any step-over chain.  */
 
 extern int thread_is_in_step_over_chain (struct thread_info *tp);
 
diff --git a/gdb/infrun.c b/gdb/infrun.c
index 7bde61597d69..b965e702538c 100644
--- a/gdb/infrun.c
+++ b/gdb/infrun.c
@@ -1237,14 +1237,14 @@ follow_exec (ptid_t ptid, const char *exec_file_target)
      matically get reset there in the new process.).  */
 }
 
-/* The queue of threads that need to do a step-over operation to get
+/* The chain of threads that need to do a step-over operation to get
    past e.g., a breakpoint.  What technique is used to step over the
    breakpoint/watchpoint does not matter -- all threads end up in the
    same queue, to maintain rough temporal order of execution, in order
    to avoid starvation, otherwise, we could e.g., find ourselves
    constantly stepping the same couple threads past their breakpoints
    over and over, if the single-step finish fast enough.  */
-struct thread_info *step_over_queue_head;
+struct thread_info *global_thread_step_over_chain_head;
 
 /* Bit flags indicating what the thread needs to step over.  */
 
@@ -1689,7 +1689,7 @@ displaced_step_prepare_throw (thread_info *tp)
       displaced_debug_printf ("deferring step of %s",
 			      target_pid_to_str (tp->ptid).c_str ());
 
-      thread_step_over_chain_enqueue (tp);
+      global_thread_step_over_chain_enqueue (tp);
       return 0;
     }
   else
@@ -1940,7 +1940,7 @@ start_step_over (void)
   if (step_over_info_valid_p ())
     return false;
 
-  for (tp = step_over_queue_head; tp != NULL; tp = next)
+  for (tp = global_thread_step_over_chain_head; tp != NULL; tp = next)
     {
       struct execution_control_state ecss;
       struct execution_control_state *ecs = &ecss;
@@ -1949,7 +1949,7 @@ start_step_over (void)
 
       gdb_assert (!tp->stop_requested);
 
-      next = thread_step_over_chain_next (tp);
+      next = global_thread_step_over_chain_next (tp);
 
       /* If this inferior already has a displaced step in process,
 	 don't start a new one.  */
@@ -1967,9 +1967,9 @@ start_step_over (void)
       if (must_be_in_line && displaced_step_in_progress_any_inferior ())
 	return false;
 
-      thread_step_over_chain_remove (tp);
+      global_thread_step_over_chain_remove (tp);
 
-      if (step_over_queue_head == NULL)
+      if (global_thread_step_over_chain_head == NULL)
 	infrun_debug_printf ("step-over queue now empty");
 
       if (tp->control.trap_expected
@@ -3026,7 +3026,7 @@ proceed (CORE_ADDR addr, enum gdb_signal siggnal)
 	  infrun_debug_printf ("need to step-over [%s] first",
 			       target_pid_to_str (tp->ptid).c_str ());
 
-	  thread_step_over_chain_enqueue (tp);
+	  global_thread_step_over_chain_enqueue (tp);
 	}
 
       switch_to_thread (cur_thr);
@@ -3035,7 +3035,7 @@ proceed (CORE_ADDR addr, enum gdb_signal siggnal)
   /* Enqueue the current thread last, so that we move all other
      threads over their breakpoints first.  */
   if (cur_thr->stepping_over_breakpoint)
-    thread_step_over_chain_enqueue (cur_thr);
+    global_thread_step_over_chain_enqueue (cur_thr);
 
   /* If the thread isn't started, we'll still need to set its prev_pc,
      so that switch_back_to_stepped_thread knows the thread hasn't
@@ -3219,7 +3219,7 @@ infrun_thread_stop_requested (ptid_t ptid)
 	 start_step_over doesn't try to resume them
 	 automatically.  */
       if (thread_is_in_step_over_chain (tp))
-	thread_step_over_chain_remove (tp);
+	global_thread_step_over_chain_remove (tp);
 
       /* If the thread is stopped, but the user/frontend doesn't
 	 know about that yet, queue a pending event, as if the
@@ -4823,7 +4823,7 @@ stop_all_threads (void)
 			      target_pid_to_str (t->ptid).c_str ());
 
 			  t->control.trap_expected = 0;
-			  thread_step_over_chain_enqueue (t);
+			  global_thread_step_over_chain_enqueue (t);
 			}
 		    }
 		  else
@@ -4846,7 +4846,7 @@ stop_all_threads (void)
 			{
 			  /* Add it back to the step-over queue.  */
 			  t->control.trap_expected = 0;
-			  thread_step_over_chain_enqueue (t);
+			  global_thread_step_over_chain_enqueue (t);
 			}
 
 		      regcache = get_thread_regcache (t);
@@ -7776,7 +7776,7 @@ keep_going_pass_signal (struct execution_control_state *ecs)
 	  infrun_debug_printf ("step-over already in progress: "
 			       "step-over for %s deferred",
 			       target_pid_to_str (tp->ptid).c_str ());
-	  thread_step_over_chain_enqueue (tp);
+	  global_thread_step_over_chain_enqueue (tp);
 	}
       else
 	{
diff --git a/gdb/infrun.h b/gdb/infrun.h
index 2084d3d16b56..ca0774e8e6b9 100644
--- a/gdb/infrun.h
+++ b/gdb/infrun.h
@@ -262,9 +262,9 @@ extern void infrun_async (int enable);
    loop.  */
 extern void mark_infrun_async_event_handler (void);
 
-/* The global queue of threads that need to do a step-over operation
+/* The global chain of threads that need to do a step-over operation
    to get past e.g., a breakpoint.  */
-extern struct thread_info *step_over_queue_head;
+extern struct thread_info *global_thread_step_over_chain_head;
 
 /* Remove breakpoints if possible (usually that means, if everything
    is stopped).  On failure, print a message.  */
diff --git a/gdb/thread.c b/gdb/thread.c
index 32d14e8662c5..2bbb016d4d98 100644
--- a/gdb/thread.c
+++ b/gdb/thread.c
@@ -205,9 +205,9 @@ clear_thread_inferior_resources (struct thread_info *tp)
 static void
 set_thread_exited (thread_info *tp, bool silent)
 {
-  /* Dead threads don't need to step-over.  Remove from queue.  */
+  /* Dead threads don't need to step-over.  Remove from chain.  */
   if (tp->step_over_next != NULL)
-    thread_step_over_chain_remove (tp);
+    global_thread_step_over_chain_remove (tp);
 
   if (tp->state != THREAD_EXITED)
     {
@@ -365,7 +365,7 @@ step_over_chain_enqueue (struct thread_info **list_p, struct thread_info *tp)
 /* Remove TP from step-over chain LIST_P.  */
 
 static void
-step_over_chain_remove (struct thread_info **list_p, struct thread_info *tp)
+thread_step_over_chain_remove (struct thread_info **list_p, struct thread_info *tp)
 {
   gdb_assert (tp->step_over_next != NULL);
   gdb_assert (tp->step_over_prev != NULL);
@@ -386,11 +386,11 @@ step_over_chain_remove (struct thread_info **list_p, struct thread_info *tp)
 /* See gdbthread.h.  */
 
 struct thread_info *
-thread_step_over_chain_next (struct thread_info *tp)
+global_thread_step_over_chain_next (struct thread_info *tp)
 {
   struct thread_info *next = tp->step_over_next;
 
-  return (next == step_over_queue_head ? NULL : next);
+  return (next == global_thread_step_over_chain_head ? NULL : next);
 }
 
 /* See gdbthread.h.  */
@@ -404,17 +404,17 @@ thread_is_in_step_over_chain (struct thread_info *tp)
 /* See gdbthread.h.  */
 
 void
-thread_step_over_chain_enqueue (struct thread_info *tp)
+global_thread_step_over_chain_enqueue (struct thread_info *tp)
 {
-  step_over_chain_enqueue (&step_over_queue_head, tp);
+  step_over_chain_enqueue (&global_thread_step_over_chain_head, tp);
 }
 
 /* See gdbthread.h.  */
 
 void
-thread_step_over_chain_remove (struct thread_info *tp)
+global_thread_step_over_chain_remove (struct thread_info *tp)
 {
-  step_over_chain_remove (&step_over_queue_head, tp);
+  thread_step_over_chain_remove (&global_thread_step_over_chain_head, tp);
 }
 
 /* Delete the thread referenced by THR.  If SILENT, don't notify
@@ -805,7 +805,7 @@ set_running_thread (struct thread_info *tp, bool running)
 	 the step-over queue, so that we don't try to resume
 	 it until the user wants it to.  */
       if (tp->step_over_next != NULL)
-	thread_step_over_chain_remove (tp);
+	global_thread_step_over_chain_remove (tp);
     }
 
   return started;
-- 
2.29.2


^ permalink raw reply	[flat|nested] 40+ messages in thread

* [PATCH v2 06/14] gdb: rename displaced_step_closure to displaced_step_copy_insn_closure
  2020-12-02 15:47 [PATCH v2 00/14] Concurrent displaced stepping Simon Marchi
                   ` (4 preceding siblings ...)
  2020-12-02 15:47 ` [PATCH v2 05/14] gdb: rename things related to step over chains Simon Marchi
@ 2020-12-02 15:47 ` Simon Marchi
  2020-12-04  1:49   ` Pedro Alves
  2020-12-02 15:47 ` [PATCH v2 07/14] gdb: rename displaced_step_fixup to displaced_step_finish Simon Marchi
                   ` (8 subsequent siblings)
  14 siblings, 1 reply; 40+ messages in thread
From: Simon Marchi @ 2020-12-02 15:47 UTC (permalink / raw)
  To: gdb-patches; +Cc: Simon Marchi

From: Simon Marchi <simon.marchi@efficios.com>

Since we're going to introduce other "displaced step" functions and
another kind of displaced step closure, make it clear that this is the
return type of the gdbarch_displaced_step_copy_insn function.

gdb/ChangeLog:

	* infrun.h (get_displaced_step_closure_by_addr): Rename to...
	(get_displaced_step_copy_insn_closure_by_addr): ... this.
	Update all users.
	(displaced_step_closure): Rename to...
	(displaced_step_copy_insn_closure): ... this.  Update all users.
	(displaced_step_closure_up): Rename to...
	(displaced_step_copy_insn_closure_up). ... this.  Update all
	users.
	(buf_displaced_step_closure): Rename to...
	(buf_displaced_step_copy_insn_closure): ... this.  Update all
	users.
	* infrun.c (get_displaced_step_closure_by_addr): Rename to...
	(get_displaced_step_copy_insn_closure_by_addr): ... this.
	Update all users.
	* aarch64-tdep.c (aarch64_displaced_step_closure): Rename to...
	(aarch64_displaced_step_copy_insn_closure): ... this.  Update
	all users.
	* amd64-tdep.c (amd64_displaced_step_closure): Rename to...
	(amd64_displaced_step_copy_insn_closure): ... this.  Update all
	users.
	* arm-tdep.h (arm_displaced_step_closure): Rename to...
	(arm_displaced_step_copy_insn_closure): ... this.  Update all
	users.
	* i386-tdep.h (i386_displaced_step_closure): Rename to...
	(i386_displaced_step_copy_insn_closure): ... this.  Update all
	users.
	* rs6000-tdep.c (ppc_displaced_step_closure): Rename to...
	(ppc_displaced_step_copy_insn_closure): ... this.  Update all
	users.
	* s390-tdep.c (s390_displaced_step_closure): Rename to...
	(s390_displaced_step_copy_insn_closure): ... this.  Update all
	users.
	* gdbarch.h: Re-generate.
	* gdbarch.c: Re-generate.

Change-Id: I11f56dbcd4c3532fb195a08ba93bccf1d12a03c8
---
 gdb/aarch64-tdep.c    |  18 +++--
 gdb/aarch64-tdep.h    |   4 +-
 gdb/amd64-tdep.c      |  23 +++---
 gdb/amd64-tdep.h      |   9 +--
 gdb/arm-linux-tdep.c  |  16 ++--
 gdb/arm-tdep.c        | 180 +++++++++++++++++++++---------------------
 gdb/arm-tdep.h        |  18 +++--
 gdb/gdbarch.c         |   4 +-
 gdb/gdbarch.h         |   8 +-
 gdb/gdbarch.sh        |   4 +-
 gdb/i386-linux-tdep.c |  27 ++++---
 gdb/i386-tdep.c       |  14 ++--
 gdb/i386-tdep.h       |  12 +--
 gdb/infrun.c          |  13 +--
 gdb/infrun.h          |  17 ++--
 gdb/rs6000-tdep.c     |  16 ++--
 gdb/s390-tdep.c       |  17 ++--
 17 files changed, 207 insertions(+), 193 deletions(-)

diff --git a/gdb/aarch64-tdep.c b/gdb/aarch64-tdep.c
index 2c1d888904a9..a0b7d2e72bb4 100644
--- a/gdb/aarch64-tdep.c
+++ b/gdb/aarch64-tdep.c
@@ -2903,7 +2903,8 @@ aarch64_software_single_step (struct regcache *regcache)
   return next_pcs;
 }
 
-struct aarch64_displaced_step_closure : public displaced_step_closure
+struct aarch64_displaced_step_copy_insn_closure
+  : public displaced_step_copy_insn_closure
 {
   /* It is true when condition instruction, such as B.CON, TBZ, etc,
      is being displaced stepping.  */
@@ -2929,7 +2930,7 @@ struct aarch64_displaced_step_data
   /* Registers when doing displaced stepping.  */
   struct regcache *regs;
 
-  aarch64_displaced_step_closure *dsc;
+  aarch64_displaced_step_copy_insn_closure *dsc;
 };
 
 /* Implementation of aarch64_insn_visitor method "b".  */
@@ -3138,7 +3139,7 @@ static const struct aarch64_insn_visitor visitor =
 
 /* Implement the "displaced_step_copy_insn" gdbarch method.  */
 
-displaced_step_closure_up
+displaced_step_copy_insn_closure_up
 aarch64_displaced_step_copy_insn (struct gdbarch *gdbarch,
 				  CORE_ADDR from, CORE_ADDR to,
 				  struct regcache *regs)
@@ -3158,8 +3159,8 @@ aarch64_displaced_step_copy_insn (struct gdbarch *gdbarch,
       return NULL;
     }
 
-  std::unique_ptr<aarch64_displaced_step_closure> dsc
-    (new aarch64_displaced_step_closure);
+  std::unique_ptr<aarch64_displaced_step_copy_insn_closure> dsc
+    (new aarch64_displaced_step_copy_insn_closure);
   dsd.base.insn_addr = from;
   dsd.new_addr = to;
   dsd.regs = regs;
@@ -3191,18 +3192,19 @@ aarch64_displaced_step_copy_insn (struct gdbarch *gdbarch,
     }
 
   /* This is a work around for a problem with g++ 4.8.  */
-  return displaced_step_closure_up (dsc.release ());
+  return displaced_step_copy_insn_closure_up (dsc.release ());
 }
 
 /* Implement the "displaced_step_fixup" gdbarch method.  */
 
 void
 aarch64_displaced_step_fixup (struct gdbarch *gdbarch,
-			      struct displaced_step_closure *dsc_,
+			      struct displaced_step_copy_insn_closure *dsc_,
 			      CORE_ADDR from, CORE_ADDR to,
 			      struct regcache *regs)
 {
-  aarch64_displaced_step_closure *dsc = (aarch64_displaced_step_closure *) dsc_;
+  aarch64_displaced_step_copy_insn_closure *dsc
+    = (aarch64_displaced_step_copy_insn_closure *) dsc_;
 
   ULONGEST pc;
 
diff --git a/gdb/aarch64-tdep.h b/gdb/aarch64-tdep.h
index 81ce4d84b41b..895aa5977f05 100644
--- a/gdb/aarch64-tdep.h
+++ b/gdb/aarch64-tdep.h
@@ -107,13 +107,13 @@ const target_desc *aarch64_read_description (uint64_t vq, bool pauth_p);
 extern int aarch64_process_record (struct gdbarch *gdbarch,
 			       struct regcache *regcache, CORE_ADDR addr);
 
-displaced_step_closure_up
+displaced_step_copy_insn_closure_up
   aarch64_displaced_step_copy_insn (struct gdbarch *gdbarch,
 				    CORE_ADDR from, CORE_ADDR to,
 				    struct regcache *regs);
 
 void aarch64_displaced_step_fixup (struct gdbarch *gdbarch,
-				   struct displaced_step_closure *dsc,
+				   displaced_step_copy_insn_closure *dsc,
 				   CORE_ADDR from, CORE_ADDR to,
 				   struct regcache *regs);
 
diff --git a/gdb/amd64-tdep.c b/gdb/amd64-tdep.c
index 31d4d3460ef2..d2bcbe199bcd 100644
--- a/gdb/amd64-tdep.c
+++ b/gdb/amd64-tdep.c
@@ -1103,9 +1103,10 @@ struct amd64_insn
   gdb_byte *raw_insn;
 };
 
-struct amd64_displaced_step_closure : public displaced_step_closure
+struct amd64_displaced_step_copy_insn_closure
+  : public displaced_step_copy_insn_closure
 {
-  amd64_displaced_step_closure (int insn_buf_len)
+  amd64_displaced_step_copy_insn_closure (int insn_buf_len)
   : insn_buf (insn_buf_len, 0)
   {}
 
@@ -1386,7 +1387,8 @@ amd64_get_insn_details (gdb_byte *insn, struct amd64_insn *details)
    We set base = pc + insn_length so we can leave disp unchanged.  */
 
 static void
-fixup_riprel (struct gdbarch *gdbarch, amd64_displaced_step_closure *dsc,
+fixup_riprel (struct gdbarch *gdbarch,
+	      amd64_displaced_step_copy_insn_closure *dsc,
 	      CORE_ADDR from, CORE_ADDR to, struct regcache *regs)
 {
   const struct amd64_insn *insn_details = &dsc->insn_details;
@@ -1447,7 +1449,7 @@ fixup_riprel (struct gdbarch *gdbarch, amd64_displaced_step_closure *dsc,
 
 static void
 fixup_displaced_copy (struct gdbarch *gdbarch,
-		      amd64_displaced_step_closure *dsc,
+		      amd64_displaced_step_copy_insn_closure *dsc,
 		      CORE_ADDR from, CORE_ADDR to, struct regcache *regs)
 {
   const struct amd64_insn *details = &dsc->insn_details;
@@ -1465,7 +1467,7 @@ fixup_displaced_copy (struct gdbarch *gdbarch,
     }
 }
 
-displaced_step_closure_up
+displaced_step_copy_insn_closure_up
 amd64_displaced_step_copy_insn (struct gdbarch *gdbarch,
 				CORE_ADDR from, CORE_ADDR to,
 				struct regcache *regs)
@@ -1474,8 +1476,8 @@ amd64_displaced_step_copy_insn (struct gdbarch *gdbarch,
   /* Extra space for sentinels so fixup_{riprel,displaced_copy} don't have to
      continually watch for running off the end of the buffer.  */
   int fixup_sentinel_space = len;
-  std::unique_ptr<amd64_displaced_step_closure> dsc
-    (new amd64_displaced_step_closure (len + fixup_sentinel_space));
+  std::unique_ptr<amd64_displaced_step_copy_insn_closure> dsc
+    (new amd64_displaced_step_copy_insn_closure (len + fixup_sentinel_space));
   gdb_byte *buf = &dsc->insn_buf[0];
   struct amd64_insn *details = &dsc->insn_details;
 
@@ -1509,7 +1511,7 @@ amd64_displaced_step_copy_insn (struct gdbarch *gdbarch,
 			  displaced_step_dump_bytes (buf, len).c_str ());
 
   /* This is a work around for a problem with g++ 4.8.  */
-  return displaced_step_closure_up (dsc.release ());
+  return displaced_step_copy_insn_closure_up (dsc.release ());
 }
 
 static int
@@ -1671,11 +1673,12 @@ amd64_insn_is_jump (struct gdbarch *gdbarch, CORE_ADDR addr)
 
 void
 amd64_displaced_step_fixup (struct gdbarch *gdbarch,
-			    struct displaced_step_closure *dsc_,
+			    struct displaced_step_copy_insn_closure *dsc_,
 			    CORE_ADDR from, CORE_ADDR to,
 			    struct regcache *regs)
 {
-  amd64_displaced_step_closure *dsc = (amd64_displaced_step_closure *) dsc_;
+  amd64_displaced_step_copy_insn_closure *dsc
+    = (amd64_displaced_step_copy_insn_closure *) dsc_;
   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
   /* The offset we applied to the instruction's address.  */
   ULONGEST insn_offset = to - from;
diff --git a/gdb/amd64-tdep.h b/gdb/amd64-tdep.h
index a4e695655934..56aef6c5d9ab 100644
--- a/gdb/amd64-tdep.h
+++ b/gdb/amd64-tdep.h
@@ -88,13 +88,12 @@ enum amd64_regnum
 
 #define AMD64_NUM_REGS		(AMD64_GSBASE_REGNUM + 1)
 
-extern displaced_step_closure_up amd64_displaced_step_copy_insn
+extern displaced_step_copy_insn_closure_up amd64_displaced_step_copy_insn
   (struct gdbarch *gdbarch, CORE_ADDR from, CORE_ADDR to,
    struct regcache *regs);
-extern void amd64_displaced_step_fixup (struct gdbarch *gdbarch,
-					struct displaced_step_closure *closure,
-					CORE_ADDR from, CORE_ADDR to,
-					struct regcache *regs);
+extern void amd64_displaced_step_fixup
+  (struct gdbarch *gdbarch, displaced_step_copy_insn_closure *closure,
+   CORE_ADDR from, CORE_ADDR to, struct regcache *regs);
 
 /* Initialize the ABI for amd64.  Uses DEFAULT_TDESC as fallback
    tdesc, if INFO does not specify one.  */
diff --git a/gdb/arm-linux-tdep.c b/gdb/arm-linux-tdep.c
index 2d563c2b024f..9caae06adfee 100644
--- a/gdb/arm-linux-tdep.c
+++ b/gdb/arm-linux-tdep.c
@@ -946,7 +946,7 @@ arm_linux_software_single_step (struct regcache *regcache)
 static void
 arm_linux_cleanup_svc (struct gdbarch *gdbarch,
 		       struct regcache *regs,
-		       arm_displaced_step_closure *dsc)
+		       arm_displaced_step_copy_insn_closure *dsc)
 {
   ULONGEST apparent_pc;
   int within_scratch;
@@ -970,7 +970,7 @@ arm_linux_cleanup_svc (struct gdbarch *gdbarch,
 
 static int
 arm_linux_copy_svc (struct gdbarch *gdbarch, struct regcache *regs,
-		    arm_displaced_step_closure *dsc)
+		    arm_displaced_step_copy_insn_closure *dsc)
 {
   CORE_ADDR return_to = 0;
 
@@ -1056,7 +1056,7 @@ arm_linux_copy_svc (struct gdbarch *gdbarch, struct regcache *regs,
 static void
 cleanup_kernel_helper_return (struct gdbarch *gdbarch,
 			      struct regcache *regs,
-			      arm_displaced_step_closure *dsc)
+			      arm_displaced_step_copy_insn_closure *dsc)
 {
   displaced_write_reg (regs, dsc, ARM_LR_REGNUM, dsc->tmp[0], CANNOT_WRITE_PC);
   displaced_write_reg (regs, dsc, ARM_PC_REGNUM, dsc->tmp[0], BRANCH_WRITE_PC);
@@ -1065,7 +1065,7 @@ cleanup_kernel_helper_return (struct gdbarch *gdbarch,
 static void
 arm_catch_kernel_helper_return (struct gdbarch *gdbarch, CORE_ADDR from,
 				CORE_ADDR to, struct regcache *regs,
-				arm_displaced_step_closure *dsc)
+				arm_displaced_step_copy_insn_closure *dsc)
 {
   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
 
@@ -1094,13 +1094,13 @@ arm_catch_kernel_helper_return (struct gdbarch *gdbarch, CORE_ADDR from,
    the program has stepped into a Linux kernel helper routine (which must be
    handled as a special case).  */
 
-static displaced_step_closure_up
+static displaced_step_copy_insn_closure_up
 arm_linux_displaced_step_copy_insn (struct gdbarch *gdbarch,
 				    CORE_ADDR from, CORE_ADDR to,
 				    struct regcache *regs)
 {
-  std::unique_ptr<arm_displaced_step_closure> dsc
-    (new arm_displaced_step_closure);
+  std::unique_ptr<arm_displaced_step_copy_insn_closure> dsc
+    (new arm_displaced_step_copy_insn_closure);
 
   /* Detect when we enter an (inaccessible by GDB) Linux kernel helper, and
      stop at the return location.  */
@@ -1122,7 +1122,7 @@ arm_linux_displaced_step_copy_insn (struct gdbarch *gdbarch,
   arm_displaced_init_closure (gdbarch, from, to, dsc.get ());
 
   /* This is a work around for a problem with g++ 4.8.  */
-  return displaced_step_closure_up (dsc.release ());
+  return displaced_step_copy_insn_closure_up (dsc.release ());
 }
 
 /* Implementation of `gdbarch_stap_is_single_operand', as defined in
diff --git a/gdb/arm-tdep.c b/gdb/arm-tdep.c
index d1e3a98c0b5d..bc086e18a571 100644
--- a/gdb/arm-tdep.c
+++ b/gdb/arm-tdep.c
@@ -433,9 +433,9 @@ arm_pc_is_thumb (struct gdbarch *gdbarch, CORE_ADDR memaddr)
 {
   struct bound_minimal_symbol sym;
   char type;
-  arm_displaced_step_closure *dsc
-    = ((arm_displaced_step_closure * )
-	get_displaced_step_closure_by_addr (memaddr));
+  arm_displaced_step_copy_insn_closure *dsc
+    = ((arm_displaced_step_copy_insn_closure * )
+	get_displaced_step_copy_insn_closure_by_addr (memaddr));
 
   /* If checking the mode of displaced instruction in copy area, the mode
      should be determined by instruction on the original address.  */
@@ -4474,7 +4474,7 @@ arm_adjust_breakpoint_address (struct gdbarch *gdbarch, CORE_ADDR bpaddr)
    location.  */
 
 ULONGEST
-displaced_read_reg (struct regcache *regs, arm_displaced_step_closure *dsc,
+displaced_read_reg (regcache *regs, arm_displaced_step_copy_insn_closure *dsc,
 		    int regno)
 {
   ULONGEST ret;
@@ -4522,7 +4522,7 @@ displaced_in_arm_mode (struct regcache *regs)
 /* Write to the PC as from a branch instruction.  */
 
 static void
-branch_write_pc (struct regcache *regs, arm_displaced_step_closure *dsc,
+branch_write_pc (regcache *regs, arm_displaced_step_copy_insn_closure *dsc,
 		 ULONGEST val)
 {
   if (!dsc->is_thumb)
@@ -4568,7 +4568,7 @@ bx_write_pc (struct regcache *regs, ULONGEST val)
 /* Write to the PC as if from a load instruction.  */
 
 static void
-load_write_pc (struct regcache *regs, arm_displaced_step_closure *dsc,
+load_write_pc (regcache *regs, arm_displaced_step_copy_insn_closure *dsc,
 	       ULONGEST val)
 {
   if (DISPLACED_STEPPING_ARCH_VERSION >= 5)
@@ -4580,7 +4580,7 @@ load_write_pc (struct regcache *regs, arm_displaced_step_closure *dsc,
 /* Write to the PC as if from an ALU instruction.  */
 
 static void
-alu_write_pc (struct regcache *regs, arm_displaced_step_closure *dsc,
+alu_write_pc (regcache *regs, arm_displaced_step_copy_insn_closure *dsc,
 	      ULONGEST val)
 {
   if (DISPLACED_STEPPING_ARCH_VERSION >= 7 && !dsc->is_thumb)
@@ -4594,7 +4594,7 @@ alu_write_pc (struct regcache *regs, arm_displaced_step_closure *dsc,
    this is controlled by the WRITE_PC argument.  */
 
 void
-displaced_write_reg (struct regcache *regs, arm_displaced_step_closure *dsc,
+displaced_write_reg (regcache *regs, arm_displaced_step_copy_insn_closure *dsc,
 		     int regno, ULONGEST val, enum pc_write_style write_pc)
 {
   if (regno == ARM_PC_REGNUM)
@@ -4675,8 +4675,8 @@ insn_references_pc (uint32_t insn, uint32_t bitmask)
    matter what address they are executed at: in those cases, use this.  */
 
 static int
-arm_copy_unmodified (struct gdbarch *gdbarch, uint32_t insn,
-		     const char *iname, arm_displaced_step_closure *dsc)
+arm_copy_unmodified (struct gdbarch *gdbarch, uint32_t insn, const char *iname,
+		     arm_displaced_step_copy_insn_closure *dsc)
 {
   displaced_debug_printf ("copying insn %.8lx, opcode/class '%s' unmodified",
 			  (unsigned long) insn, iname);
@@ -4689,7 +4689,7 @@ arm_copy_unmodified (struct gdbarch *gdbarch, uint32_t insn,
 static int
 thumb_copy_unmodified_32bit (struct gdbarch *gdbarch, uint16_t insn1,
 			     uint16_t insn2, const char *iname,
-			     arm_displaced_step_closure *dsc)
+			     arm_displaced_step_copy_insn_closure *dsc)
 {
   displaced_debug_printf ("copying insn %.4x %.4x, opcode/class '%s' "
 			  "unmodified", insn1, insn2, iname);
@@ -4706,7 +4706,7 @@ thumb_copy_unmodified_32bit (struct gdbarch *gdbarch, uint16_t insn1,
 static int
 thumb_copy_unmodified_16bit (struct gdbarch *gdbarch, uint16_t insn,
 			     const char *iname,
-			     arm_displaced_step_closure *dsc)
+			     arm_displaced_step_copy_insn_closure *dsc)
 {
   displaced_debug_printf ("copying insn %.4x, opcode/class '%s' unmodified",
 			  insn, iname);
@@ -4719,8 +4719,8 @@ thumb_copy_unmodified_16bit (struct gdbarch *gdbarch, uint16_t insn,
 /* Preload instructions with immediate offset.  */
 
 static void
-cleanup_preload (struct gdbarch *gdbarch,
-		 struct regcache *regs, arm_displaced_step_closure *dsc)
+cleanup_preload (struct gdbarch *gdbarch, regcache *regs,
+		 arm_displaced_step_copy_insn_closure *dsc)
 {
   displaced_write_reg (regs, dsc, 0, dsc->tmp[0], CANNOT_WRITE_PC);
   if (!dsc->u.preload.immed)
@@ -4729,7 +4729,7 @@ cleanup_preload (struct gdbarch *gdbarch,
 
 static void
 install_preload (struct gdbarch *gdbarch, struct regcache *regs,
-		 arm_displaced_step_closure *dsc, unsigned int rn)
+		 arm_displaced_step_copy_insn_closure *dsc, unsigned int rn)
 {
   ULONGEST rn_val;
   /* Preload instructions:
@@ -4748,7 +4748,7 @@ install_preload (struct gdbarch *gdbarch, struct regcache *regs,
 
 static int
 arm_copy_preload (struct gdbarch *gdbarch, uint32_t insn, struct regcache *regs,
-		  arm_displaced_step_closure *dsc)
+		  arm_displaced_step_copy_insn_closure *dsc)
 {
   unsigned int rn = bits (insn, 16, 19);
 
@@ -4766,7 +4766,7 @@ arm_copy_preload (struct gdbarch *gdbarch, uint32_t insn, struct regcache *regs,
 
 static int
 thumb2_copy_preload (struct gdbarch *gdbarch, uint16_t insn1, uint16_t insn2,
-		     struct regcache *regs, arm_displaced_step_closure *dsc)
+		     regcache *regs, arm_displaced_step_copy_insn_closure *dsc)
 {
   unsigned int rn = bits (insn1, 0, 3);
   unsigned int u_bit = bit (insn1, 7);
@@ -4814,7 +4814,7 @@ thumb2_copy_preload (struct gdbarch *gdbarch, uint16_t insn1, uint16_t insn2,
 
 static void
 install_preload_reg(struct gdbarch *gdbarch, struct regcache *regs,
-		    arm_displaced_step_closure *dsc, unsigned int rn,
+		    arm_displaced_step_copy_insn_closure *dsc, unsigned int rn,
 		    unsigned int rm)
 {
   ULONGEST rn_val, rm_val;
@@ -4839,7 +4839,7 @@ install_preload_reg(struct gdbarch *gdbarch, struct regcache *regs,
 static int
 arm_copy_preload_reg (struct gdbarch *gdbarch, uint32_t insn,
 		      struct regcache *regs,
-		      arm_displaced_step_closure *dsc)
+		      arm_displaced_step_copy_insn_closure *dsc)
 {
   unsigned int rn = bits (insn, 16, 19);
   unsigned int rm = bits (insn, 0, 3);
@@ -4862,7 +4862,7 @@ arm_copy_preload_reg (struct gdbarch *gdbarch, uint32_t insn,
 static void
 cleanup_copro_load_store (struct gdbarch *gdbarch,
 			  struct regcache *regs,
-			  arm_displaced_step_closure *dsc)
+			  arm_displaced_step_copy_insn_closure *dsc)
 {
   ULONGEST rn_val = displaced_read_reg (regs, dsc, 0);
 
@@ -4874,7 +4874,7 @@ cleanup_copro_load_store (struct gdbarch *gdbarch,
 
 static void
 install_copro_load_store (struct gdbarch *gdbarch, struct regcache *regs,
-			  arm_displaced_step_closure *dsc,
+			  arm_displaced_step_copy_insn_closure *dsc,
 			  int writeback, unsigned int rn)
 {
   ULONGEST rn_val;
@@ -4902,7 +4902,7 @@ install_copro_load_store (struct gdbarch *gdbarch, struct regcache *regs,
 static int
 arm_copy_copro_load_store (struct gdbarch *gdbarch, uint32_t insn,
 			   struct regcache *regs,
-			   arm_displaced_step_closure *dsc)
+			   arm_displaced_step_copy_insn_closure *dsc)
 {
   unsigned int rn = bits (insn, 16, 19);
 
@@ -4922,7 +4922,7 @@ arm_copy_copro_load_store (struct gdbarch *gdbarch, uint32_t insn,
 static int
 thumb2_copy_copro_load_store (struct gdbarch *gdbarch, uint16_t insn1,
 			      uint16_t insn2, struct regcache *regs,
-			      arm_displaced_step_closure *dsc)
+			      arm_displaced_step_copy_insn_closure *dsc)
 {
   unsigned int rn = bits (insn1, 0, 3);
 
@@ -4949,7 +4949,7 @@ thumb2_copy_copro_load_store (struct gdbarch *gdbarch, uint16_t insn1,
 
 static void
 cleanup_branch (struct gdbarch *gdbarch, struct regcache *regs,
-		arm_displaced_step_closure *dsc)
+		arm_displaced_step_copy_insn_closure *dsc)
 {
   uint32_t status = displaced_read_reg (regs, dsc, ARM_PS_REGNUM);
   int branch_taken = condition_true (dsc->u.branch.cond, status);
@@ -4980,7 +4980,7 @@ cleanup_branch (struct gdbarch *gdbarch, struct regcache *regs,
 
 static void
 install_b_bl_blx (struct gdbarch *gdbarch, struct regcache *regs,
-		  arm_displaced_step_closure *dsc,
+		  arm_displaced_step_copy_insn_closure *dsc,
 		  unsigned int cond, int exchange, int link, long offset)
 {
   /* Implement "BL<cond> <label>" as:
@@ -5009,7 +5009,7 @@ install_b_bl_blx (struct gdbarch *gdbarch, struct regcache *regs,
 }
 static int
 arm_copy_b_bl_blx (struct gdbarch *gdbarch, uint32_t insn,
-		   struct regcache *regs, arm_displaced_step_closure *dsc)
+		   regcache *regs, arm_displaced_step_copy_insn_closure *dsc)
 {
   unsigned int cond = bits (insn, 28, 31);
   int exchange = (cond == 0xf);
@@ -5038,7 +5038,7 @@ arm_copy_b_bl_blx (struct gdbarch *gdbarch, uint32_t insn,
 static int
 thumb2_copy_b_bl_blx (struct gdbarch *gdbarch, uint16_t insn1,
 		      uint16_t insn2, struct regcache *regs,
-		      arm_displaced_step_closure *dsc)
+		      arm_displaced_step_copy_insn_closure *dsc)
 {
   int link = bit (insn2, 14);
   int exchange = link && !bit (insn2, 12);
@@ -5091,7 +5091,7 @@ thumb2_copy_b_bl_blx (struct gdbarch *gdbarch, uint16_t insn1,
 /* Copy B Thumb instructions.  */
 static int
 thumb_copy_b (struct gdbarch *gdbarch, uint16_t insn,
-	      arm_displaced_step_closure *dsc)
+	      arm_displaced_step_copy_insn_closure *dsc)
 {
   unsigned int cond = 0;
   int offset = 0;
@@ -5129,7 +5129,7 @@ thumb_copy_b (struct gdbarch *gdbarch, uint16_t insn,
 
 static void
 install_bx_blx_reg (struct gdbarch *gdbarch, struct regcache *regs,
-		    arm_displaced_step_closure *dsc, int link,
+		    arm_displaced_step_copy_insn_closure *dsc, int link,
 		    unsigned int cond, unsigned int rm)
 {
   /* Implement {BX,BLX}<cond> <reg>" as:
@@ -5152,7 +5152,7 @@ install_bx_blx_reg (struct gdbarch *gdbarch, struct regcache *regs,
 
 static int
 arm_copy_bx_blx_reg (struct gdbarch *gdbarch, uint32_t insn,
-		     struct regcache *regs, arm_displaced_step_closure *dsc)
+		     regcache *regs, arm_displaced_step_copy_insn_closure *dsc)
 {
   unsigned int cond = bits (insn, 28, 31);
   /* BX:  x12xxx1x
@@ -5171,7 +5171,7 @@ arm_copy_bx_blx_reg (struct gdbarch *gdbarch, uint32_t insn,
 static int
 thumb_copy_bx_blx_reg (struct gdbarch *gdbarch, uint16_t insn,
 		       struct regcache *regs,
-		       arm_displaced_step_closure *dsc)
+		       arm_displaced_step_copy_insn_closure *dsc)
 {
   int link = bit (insn, 7);
   unsigned int rm = bits (insn, 3, 6);
@@ -5190,7 +5190,7 @@ thumb_copy_bx_blx_reg (struct gdbarch *gdbarch, uint16_t insn,
 
 static void
 cleanup_alu_imm (struct gdbarch *gdbarch,
-		 struct regcache *regs, arm_displaced_step_closure *dsc)
+		 regcache *regs, arm_displaced_step_copy_insn_closure *dsc)
 {
   ULONGEST rd_val = displaced_read_reg (regs, dsc, 0);
   displaced_write_reg (regs, dsc, 0, dsc->tmp[0], CANNOT_WRITE_PC);
@@ -5200,7 +5200,7 @@ cleanup_alu_imm (struct gdbarch *gdbarch,
 
 static int
 arm_copy_alu_imm (struct gdbarch *gdbarch, uint32_t insn, struct regcache *regs,
-		  arm_displaced_step_closure *dsc)
+		  arm_displaced_step_copy_insn_closure *dsc)
 {
   unsigned int rn = bits (insn, 16, 19);
   unsigned int rd = bits (insn, 12, 15);
@@ -5248,7 +5248,7 @@ arm_copy_alu_imm (struct gdbarch *gdbarch, uint32_t insn, struct regcache *regs,
 static int
 thumb2_copy_alu_imm (struct gdbarch *gdbarch, uint16_t insn1,
 		     uint16_t insn2, struct regcache *regs,
-		     arm_displaced_step_closure *dsc)
+		     arm_displaced_step_copy_insn_closure *dsc)
 {
   unsigned int op = bits (insn1, 5, 8);
   unsigned int rn, rm, rd;
@@ -5299,7 +5299,7 @@ thumb2_copy_alu_imm (struct gdbarch *gdbarch, uint16_t insn1,
 
 static void
 cleanup_alu_reg (struct gdbarch *gdbarch,
-		 struct regcache *regs, arm_displaced_step_closure *dsc)
+		 regcache *regs, arm_displaced_step_copy_insn_closure *dsc)
 {
   ULONGEST rd_val;
   int i;
@@ -5314,7 +5314,7 @@ cleanup_alu_reg (struct gdbarch *gdbarch,
 
 static void
 install_alu_reg (struct gdbarch *gdbarch, struct regcache *regs,
-		 arm_displaced_step_closure *dsc,
+		 arm_displaced_step_copy_insn_closure *dsc,
 		 unsigned int rd, unsigned int rn, unsigned int rm)
 {
   ULONGEST rd_val, rn_val, rm_val;
@@ -5347,7 +5347,7 @@ install_alu_reg (struct gdbarch *gdbarch, struct regcache *regs,
 
 static int
 arm_copy_alu_reg (struct gdbarch *gdbarch, uint32_t insn, struct regcache *regs,
-		  arm_displaced_step_closure *dsc)
+		  arm_displaced_step_copy_insn_closure *dsc)
 {
   unsigned int op = bits (insn, 21, 24);
   int is_mov = (op == 0xd);
@@ -5371,7 +5371,7 @@ arm_copy_alu_reg (struct gdbarch *gdbarch, uint32_t insn, struct regcache *regs,
 static int
 thumb_copy_alu_reg (struct gdbarch *gdbarch, uint16_t insn,
 		    struct regcache *regs,
-		    arm_displaced_step_closure *dsc)
+		    arm_displaced_step_copy_insn_closure *dsc)
 {
   unsigned rm, rd;
 
@@ -5395,7 +5395,7 @@ thumb_copy_alu_reg (struct gdbarch *gdbarch, uint16_t insn,
 static void
 cleanup_alu_shifted_reg (struct gdbarch *gdbarch,
 			 struct regcache *regs,
-			 arm_displaced_step_closure *dsc)
+			 arm_displaced_step_copy_insn_closure *dsc)
 {
   ULONGEST rd_val = displaced_read_reg (regs, dsc, 0);
   int i;
@@ -5408,7 +5408,7 @@ cleanup_alu_shifted_reg (struct gdbarch *gdbarch,
 
 static void
 install_alu_shifted_reg (struct gdbarch *gdbarch, struct regcache *regs,
-			 arm_displaced_step_closure *dsc,
+			 arm_displaced_step_copy_insn_closure *dsc,
 			 unsigned int rd, unsigned int rn, unsigned int rm,
 			 unsigned rs)
 {
@@ -5447,7 +5447,7 @@ install_alu_shifted_reg (struct gdbarch *gdbarch, struct regcache *regs,
 static int
 arm_copy_alu_shifted_reg (struct gdbarch *gdbarch, uint32_t insn,
 			  struct regcache *regs,
-			  arm_displaced_step_closure *dsc)
+			  arm_displaced_step_copy_insn_closure *dsc)
 {
   unsigned int op = bits (insn, 21, 24);
   int is_mov = (op == 0xd);
@@ -5479,7 +5479,7 @@ arm_copy_alu_shifted_reg (struct gdbarch *gdbarch, uint32_t insn,
 
 static void
 cleanup_load (struct gdbarch *gdbarch, struct regcache *regs,
-	      arm_displaced_step_closure *dsc)
+	      arm_displaced_step_copy_insn_closure *dsc)
 {
   ULONGEST rt_val, rt_val2 = 0, rn_val;
 
@@ -5508,7 +5508,7 @@ cleanup_load (struct gdbarch *gdbarch, struct regcache *regs,
 
 static void
 cleanup_store (struct gdbarch *gdbarch, struct regcache *regs,
-	       arm_displaced_step_closure *dsc)
+	       arm_displaced_step_copy_insn_closure *dsc)
 {
   ULONGEST rn_val = displaced_read_reg (regs, dsc, 2);
 
@@ -5531,7 +5531,7 @@ cleanup_store (struct gdbarch *gdbarch, struct regcache *regs,
 
 static int
 arm_copy_extra_ld_st (struct gdbarch *gdbarch, uint32_t insn, int unprivileged,
-		      struct regcache *regs, arm_displaced_step_closure *dsc)
+		      regcache *regs, arm_displaced_step_copy_insn_closure *dsc)
 {
   unsigned int op1 = bits (insn, 20, 24);
   unsigned int op2 = bits (insn, 5, 6);
@@ -5604,7 +5604,7 @@ arm_copy_extra_ld_st (struct gdbarch *gdbarch, uint32_t insn, int unprivileged,
 
 static void
 install_load_store (struct gdbarch *gdbarch, struct regcache *regs,
-		    arm_displaced_step_closure *dsc, int load,
+		    arm_displaced_step_copy_insn_closure *dsc, int load,
 		    int immed, int writeback, int size, int usermode,
 		    int rt, int rm, int rn)
 {
@@ -5660,7 +5660,7 @@ install_load_store (struct gdbarch *gdbarch, struct regcache *regs,
 static int
 thumb2_copy_load_literal (struct gdbarch *gdbarch, uint16_t insn1,
 			  uint16_t insn2, struct regcache *regs,
-			  arm_displaced_step_closure *dsc, int size)
+			  arm_displaced_step_copy_insn_closure *dsc, int size)
 {
   unsigned int u_bit = bit (insn1, 7);
   unsigned int rt = bits (insn2, 12, 15);
@@ -5714,7 +5714,7 @@ thumb2_copy_load_literal (struct gdbarch *gdbarch, uint16_t insn1,
 static int
 thumb2_copy_load_reg_imm (struct gdbarch *gdbarch, uint16_t insn1,
 			  uint16_t insn2, struct regcache *regs,
-			  arm_displaced_step_closure *dsc,
+			  arm_displaced_step_copy_insn_closure *dsc,
 			  int writeback, int immed)
 {
   unsigned int rt = bits (insn2, 12, 15);
@@ -5761,7 +5761,7 @@ thumb2_copy_load_reg_imm (struct gdbarch *gdbarch, uint16_t insn1,
 static int
 arm_copy_ldr_str_ldrb_strb (struct gdbarch *gdbarch, uint32_t insn,
 			    struct regcache *regs,
-			    arm_displaced_step_closure *dsc,
+			    arm_displaced_step_copy_insn_closure *dsc,
 			    int load, int size, int usermode)
 {
   int immed = !bit (insn, 25);
@@ -5844,7 +5844,7 @@ arm_copy_ldr_str_ldrb_strb (struct gdbarch *gdbarch, uint32_t insn,
 
 static void
 cleanup_block_load_all (struct gdbarch *gdbarch, struct regcache *regs,
-			arm_displaced_step_closure *dsc)
+			arm_displaced_step_copy_insn_closure *dsc)
 {
   int inc = dsc->u.block.increment;
   int bump_before = dsc->u.block.before ? (inc ? 4 : -4) : 0;
@@ -5904,7 +5904,7 @@ cleanup_block_load_all (struct gdbarch *gdbarch, struct regcache *regs,
 
 static void
 cleanup_block_store_pc (struct gdbarch *gdbarch, struct regcache *regs,
-			arm_displaced_step_closure *dsc)
+			arm_displaced_step_copy_insn_closure *dsc)
 {
   uint32_t status = displaced_read_reg (regs, dsc, ARM_PS_REGNUM);
   int store_executed = condition_true (dsc->u.block.cond, status);
@@ -5955,7 +5955,7 @@ cleanup_block_store_pc (struct gdbarch *gdbarch, struct regcache *regs,
 static void
 cleanup_block_load_pc (struct gdbarch *gdbarch,
 		       struct regcache *regs,
-		       arm_displaced_step_closure *dsc)
+		       arm_displaced_step_copy_insn_closure *dsc)
 {
   uint32_t status = displaced_read_reg (regs, dsc, ARM_PS_REGNUM);
   int load_executed = condition_true (dsc->u.block.cond, status);
@@ -6031,7 +6031,7 @@ cleanup_block_load_pc (struct gdbarch *gdbarch,
 static int
 arm_copy_block_xfer (struct gdbarch *gdbarch, uint32_t insn,
 		     struct regcache *regs,
-		     arm_displaced_step_closure *dsc)
+		     arm_displaced_step_copy_insn_closure *dsc)
 {
   int load = bit (insn, 20);
   int user = bit (insn, 22);
@@ -6143,7 +6143,7 @@ arm_copy_block_xfer (struct gdbarch *gdbarch, uint32_t insn,
 static int
 thumb2_copy_block_xfer (struct gdbarch *gdbarch, uint16_t insn1, uint16_t insn2,
 			struct regcache *regs,
-			arm_displaced_step_closure *dsc)
+			arm_displaced_step_copy_insn_closure *dsc)
 {
   int rn = bits (insn1, 0, 3);
   int load = bit (insn1, 4);
@@ -6287,7 +6287,7 @@ arm_software_single_step (struct regcache *regcache)
 
 static void
 cleanup_svc (struct gdbarch *gdbarch, struct regcache *regs,
-	     arm_displaced_step_closure *dsc)
+	     arm_displaced_step_copy_insn_closure *dsc)
 {
   CORE_ADDR resume_addr = dsc->insn_addr + dsc->insn_size;
 
@@ -6302,7 +6302,7 @@ cleanup_svc (struct gdbarch *gdbarch, struct regcache *regs,
 
 static int
 install_svc (struct gdbarch *gdbarch, struct regcache *regs,
-	     arm_displaced_step_closure *dsc)
+	     arm_displaced_step_copy_insn_closure *dsc)
 {
   /* Preparation: none.
      Insn: unmodified svc.
@@ -6324,7 +6324,7 @@ install_svc (struct gdbarch *gdbarch, struct regcache *regs,
 
 static int
 arm_copy_svc (struct gdbarch *gdbarch, uint32_t insn,
-	      struct regcache *regs, arm_displaced_step_closure *dsc)
+	      regcache *regs, arm_displaced_step_copy_insn_closure *dsc)
 {
 
   displaced_debug_printf ("copying svc insn %.8lx",
@@ -6337,7 +6337,7 @@ arm_copy_svc (struct gdbarch *gdbarch, uint32_t insn,
 
 static int
 thumb_copy_svc (struct gdbarch *gdbarch, uint16_t insn,
-		struct regcache *regs, arm_displaced_step_closure *dsc)
+		regcache *regs, arm_displaced_step_copy_insn_closure *dsc)
 {
 
   displaced_debug_printf ("copying svc insn %.4x", insn);
@@ -6351,7 +6351,7 @@ thumb_copy_svc (struct gdbarch *gdbarch, uint16_t insn,
 
 static int
 arm_copy_undef (struct gdbarch *gdbarch, uint32_t insn,
-		arm_displaced_step_closure *dsc)
+		arm_displaced_step_copy_insn_closure *dsc)
 {
   displaced_debug_printf ("copying undefined insn %.8lx",
 			  (unsigned long) insn);
@@ -6363,7 +6363,7 @@ arm_copy_undef (struct gdbarch *gdbarch, uint32_t insn,
 
 static int
 thumb_32bit_copy_undef (struct gdbarch *gdbarch, uint16_t insn1, uint16_t insn2,
-		       arm_displaced_step_closure *dsc)
+			arm_displaced_step_copy_insn_closure *dsc)
 {
 
   displaced_debug_printf ("copying undefined insn %.4x %.4x",
@@ -6380,7 +6380,7 @@ thumb_32bit_copy_undef (struct gdbarch *gdbarch, uint16_t insn1, uint16_t insn2,
 
 static int
 arm_copy_unpred (struct gdbarch *gdbarch, uint32_t insn,
-		 arm_displaced_step_closure *dsc)
+		 arm_displaced_step_copy_insn_closure *dsc)
 {
   displaced_debug_printf ("copying unpredictable insn %.8lx",
 			  (unsigned long) insn);
@@ -6396,7 +6396,7 @@ arm_copy_unpred (struct gdbarch *gdbarch, uint32_t insn,
 static int
 arm_decode_misc_memhint_neon (struct gdbarch *gdbarch, uint32_t insn,
 			      struct regcache *regs,
-			      arm_displaced_step_closure *dsc)
+			      arm_displaced_step_copy_insn_closure *dsc)
 {
   unsigned int op1 = bits (insn, 20, 26), op2 = bits (insn, 4, 7);
   unsigned int rn = bits (insn, 16, 19);
@@ -6456,7 +6456,7 @@ arm_decode_misc_memhint_neon (struct gdbarch *gdbarch, uint32_t insn,
 static int
 arm_decode_unconditional (struct gdbarch *gdbarch, uint32_t insn,
 			  struct regcache *regs,
-			  arm_displaced_step_closure *dsc)
+			  arm_displaced_step_copy_insn_closure *dsc)
 {
   if (bit (insn, 27) == 0)
     return arm_decode_misc_memhint_neon (gdbarch, insn, regs, dsc);
@@ -6541,7 +6541,7 @@ arm_decode_unconditional (struct gdbarch *gdbarch, uint32_t insn,
 static int
 arm_decode_miscellaneous (struct gdbarch *gdbarch, uint32_t insn,
 			  struct regcache *regs,
-			  arm_displaced_step_closure *dsc)
+			  arm_displaced_step_copy_insn_closure *dsc)
 {
   unsigned int op2 = bits (insn, 4, 6);
   unsigned int op = bits (insn, 21, 22);
@@ -6592,7 +6592,7 @@ arm_decode_miscellaneous (struct gdbarch *gdbarch, uint32_t insn,
 static int
 arm_decode_dp_misc (struct gdbarch *gdbarch, uint32_t insn,
 		    struct regcache *regs,
-		    arm_displaced_step_closure *dsc)
+		    arm_displaced_step_copy_insn_closure *dsc)
 {
   if (bit (insn, 25))
     switch (bits (insn, 20, 24))
@@ -6638,7 +6638,7 @@ arm_decode_dp_misc (struct gdbarch *gdbarch, uint32_t insn,
 static int
 arm_decode_ld_st_word_ubyte (struct gdbarch *gdbarch, uint32_t insn,
 			     struct regcache *regs,
-			     arm_displaced_step_closure *dsc)
+			     arm_displaced_step_copy_insn_closure *dsc)
 {
   int a = bit (insn, 25), b = bit (insn, 4);
   uint32_t op1 = bits (insn, 20, 24);
@@ -6674,7 +6674,7 @@ arm_decode_ld_st_word_ubyte (struct gdbarch *gdbarch, uint32_t insn,
 
 static int
 arm_decode_media (struct gdbarch *gdbarch, uint32_t insn,
-		  arm_displaced_step_closure *dsc)
+		  arm_displaced_step_copy_insn_closure *dsc)
 {
   switch (bits (insn, 20, 24))
     {
@@ -6731,7 +6731,7 @@ arm_decode_media (struct gdbarch *gdbarch, uint32_t insn,
 static int
 arm_decode_b_bl_ldmstm (struct gdbarch *gdbarch, uint32_t insn,
 			struct regcache *regs,
-			arm_displaced_step_closure *dsc)
+			arm_displaced_step_copy_insn_closure *dsc)
 {
   if (bit (insn, 25))
     return arm_copy_b_bl_blx (gdbarch, insn, regs, dsc);
@@ -6742,7 +6742,7 @@ arm_decode_b_bl_ldmstm (struct gdbarch *gdbarch, uint32_t insn,
 static int
 arm_decode_ext_reg_ld_st (struct gdbarch *gdbarch, uint32_t insn,
 			  struct regcache *regs,
-			  arm_displaced_step_closure *dsc)
+			  arm_displaced_step_copy_insn_closure *dsc)
 {
   unsigned int opcode = bits (insn, 20, 24);
 
@@ -6775,7 +6775,7 @@ arm_decode_ext_reg_ld_st (struct gdbarch *gdbarch, uint32_t insn,
 static int
 thumb2_decode_dp_shift_reg (struct gdbarch *gdbarch, uint16_t insn1,
 			    uint16_t insn2,  struct regcache *regs,
-			    arm_displaced_step_closure *dsc)
+			    arm_displaced_step_copy_insn_closure *dsc)
 {
   /* PC is only allowed to be used in instruction MOV.  */
 
@@ -6796,7 +6796,7 @@ thumb2_decode_dp_shift_reg (struct gdbarch *gdbarch, uint16_t insn1,
 static int
 thumb2_decode_ext_reg_ld_st (struct gdbarch *gdbarch, uint16_t insn1,
 			     uint16_t insn2,  struct regcache *regs,
-			     arm_displaced_step_closure *dsc)
+			     arm_displaced_step_copy_insn_closure *dsc)
 {
   unsigned int opcode = bits (insn1, 4, 8);
 
@@ -6831,7 +6831,7 @@ thumb2_decode_ext_reg_ld_st (struct gdbarch *gdbarch, uint16_t insn1,
 
 static int
 arm_decode_svc_copro (struct gdbarch *gdbarch, uint32_t insn,
-		      struct regcache *regs, arm_displaced_step_closure *dsc)
+		      regcache *regs, arm_displaced_step_copy_insn_closure *dsc)
 {
   unsigned int op1 = bits (insn, 20, 25);
   int op = bit (insn, 4);
@@ -6877,7 +6877,7 @@ arm_decode_svc_copro (struct gdbarch *gdbarch, uint32_t insn,
 static int
 thumb2_decode_svc_copro (struct gdbarch *gdbarch, uint16_t insn1,
 			 uint16_t insn2, struct regcache *regs,
-			 arm_displaced_step_closure *dsc)
+			 arm_displaced_step_copy_insn_closure *dsc)
 {
   unsigned int coproc = bits (insn2, 8, 11);
   unsigned int bit_5_8 = bits (insn1, 5, 8);
@@ -6917,7 +6917,7 @@ thumb2_decode_svc_copro (struct gdbarch *gdbarch, uint16_t insn1,
 
 static void
 install_pc_relative (struct gdbarch *gdbarch, struct regcache *regs,
-		     arm_displaced_step_closure *dsc, int rd)
+		     arm_displaced_step_copy_insn_closure *dsc, int rd)
 {
   /* ADR Rd, #imm
 
@@ -6935,7 +6935,7 @@ install_pc_relative (struct gdbarch *gdbarch, struct regcache *regs,
 
 static int
 thumb_copy_pc_relative_16bit (struct gdbarch *gdbarch, struct regcache *regs,
-			      arm_displaced_step_closure *dsc,
+			      arm_displaced_step_copy_insn_closure *dsc,
 			      int rd, unsigned int imm)
 {
 
@@ -6950,7 +6950,7 @@ thumb_copy_pc_relative_16bit (struct gdbarch *gdbarch, struct regcache *regs,
 static int
 thumb_decode_pc_relative_16bit (struct gdbarch *gdbarch, uint16_t insn,
 				struct regcache *regs,
-				arm_displaced_step_closure *dsc)
+				arm_displaced_step_copy_insn_closure *dsc)
 {
   unsigned int rd = bits (insn, 8, 10);
   unsigned int imm8 = bits (insn, 0, 7);
@@ -6964,7 +6964,7 @@ thumb_decode_pc_relative_16bit (struct gdbarch *gdbarch, uint16_t insn,
 static int
 thumb_copy_pc_relative_32bit (struct gdbarch *gdbarch, uint16_t insn1,
 			      uint16_t insn2, struct regcache *regs,
-			      arm_displaced_step_closure *dsc)
+			      arm_displaced_step_copy_insn_closure *dsc)
 {
   unsigned int rd = bits (insn2, 8, 11);
   /* Since immediate has the same encoding in ADR ADD and SUB, so we simply
@@ -6999,7 +6999,7 @@ thumb_copy_pc_relative_32bit (struct gdbarch *gdbarch, uint16_t insn1,
 static int
 thumb_copy_16bit_ldr_literal (struct gdbarch *gdbarch, uint16_t insn1,
 			      struct regcache *regs,
-			      arm_displaced_step_closure *dsc)
+			      arm_displaced_step_copy_insn_closure *dsc)
 {
   unsigned int rt = bits (insn1, 8, 10);
   unsigned int pc;
@@ -7046,7 +7046,7 @@ thumb_copy_16bit_ldr_literal (struct gdbarch *gdbarch, uint16_t insn1,
 static int
 thumb_copy_cbnz_cbz (struct gdbarch *gdbarch, uint16_t insn1,
 		     struct regcache *regs,
-		     arm_displaced_step_closure *dsc)
+		     arm_displaced_step_copy_insn_closure *dsc)
 {
   int non_zero = bit (insn1, 11);
   unsigned int imm5 = (bit (insn1, 9) << 6) | (bits (insn1, 3, 7) << 1);
@@ -7083,7 +7083,7 @@ thumb_copy_cbnz_cbz (struct gdbarch *gdbarch, uint16_t insn1,
 static int
 thumb2_copy_table_branch (struct gdbarch *gdbarch, uint16_t insn1,
 			  uint16_t insn2, struct regcache *regs,
-			  arm_displaced_step_closure *dsc)
+			  arm_displaced_step_copy_insn_closure *dsc)
 {
   ULONGEST rn_val, rm_val;
   int is_tbh = bit (insn2, 4);
@@ -7125,7 +7125,7 @@ thumb2_copy_table_branch (struct gdbarch *gdbarch, uint16_t insn1,
 
 static void
 cleanup_pop_pc_16bit_all (struct gdbarch *gdbarch, struct regcache *regs,
-			  arm_displaced_step_closure *dsc)
+			  arm_displaced_step_copy_insn_closure *dsc)
 {
   /* PC <- r7 */
   int val = displaced_read_reg (regs, dsc, 7);
@@ -7143,7 +7143,7 @@ cleanup_pop_pc_16bit_all (struct gdbarch *gdbarch, struct regcache *regs,
 static int
 thumb_copy_pop_pc_16bit (struct gdbarch *gdbarch, uint16_t insn1,
 			 struct regcache *regs,
-			 arm_displaced_step_closure *dsc)
+			 arm_displaced_step_copy_insn_closure *dsc)
 {
   dsc->u.block.regmask = insn1 & 0x00ff;
 
@@ -7212,7 +7212,7 @@ thumb_copy_pop_pc_16bit (struct gdbarch *gdbarch, uint16_t insn1,
 static void
 thumb_process_displaced_16bit_insn (struct gdbarch *gdbarch, uint16_t insn1,
 				    struct regcache *regs,
-				    arm_displaced_step_closure *dsc)
+				    arm_displaced_step_copy_insn_closure *dsc)
 {
   unsigned short op_bit_12_15 = bits (insn1, 12, 15);
   unsigned short op_bit_10_11 = bits (insn1, 10, 11);
@@ -7316,7 +7316,7 @@ static int
 decode_thumb_32bit_ld_mem_hints (struct gdbarch *gdbarch,
 				 uint16_t insn1, uint16_t insn2,
 				 struct regcache *regs,
-				 arm_displaced_step_closure *dsc)
+				 arm_displaced_step_copy_insn_closure *dsc)
 {
   int rt = bits (insn2, 12, 15);
   int rn = bits (insn1, 0, 3);
@@ -7395,7 +7395,7 @@ decode_thumb_32bit_ld_mem_hints (struct gdbarch *gdbarch,
 static void
 thumb_process_displaced_32bit_insn (struct gdbarch *gdbarch, uint16_t insn1,
 				    uint16_t insn2, struct regcache *regs,
-				    arm_displaced_step_closure *dsc)
+				    arm_displaced_step_copy_insn_closure *dsc)
 {
   int err = 0;
   unsigned short op = bit (insn2, 15);
@@ -7523,7 +7523,7 @@ thumb_process_displaced_32bit_insn (struct gdbarch *gdbarch, uint16_t insn1,
 static void
 thumb_process_displaced_insn (struct gdbarch *gdbarch, CORE_ADDR from,
 			      struct regcache *regs,
-			      arm_displaced_step_closure *dsc)
+			      arm_displaced_step_copy_insn_closure *dsc)
 {
   enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
   uint16_t insn1
@@ -7547,7 +7547,7 @@ thumb_process_displaced_insn (struct gdbarch *gdbarch, CORE_ADDR from,
 void
 arm_process_displaced_insn (struct gdbarch *gdbarch, CORE_ADDR from,
 			    CORE_ADDR to, struct regcache *regs,
-			    arm_displaced_step_closure *dsc)
+			    arm_displaced_step_copy_insn_closure *dsc)
 {
   int err = 0;
   enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
@@ -7604,7 +7604,8 @@ arm_process_displaced_insn (struct gdbarch *gdbarch, CORE_ADDR from,
 
 void
 arm_displaced_init_closure (struct gdbarch *gdbarch, CORE_ADDR from,
-			    CORE_ADDR to, arm_displaced_step_closure *dsc)
+			    CORE_ADDR to,
+			    arm_displaced_step_copy_insn_closure *dsc)
 {
   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
   unsigned int i, len, offset;
@@ -7654,11 +7655,12 @@ arm_displaced_init_closure (struct gdbarch *gdbarch, CORE_ADDR from,
 
 void
 arm_displaced_step_fixup (struct gdbarch *gdbarch,
-			  struct displaced_step_closure *dsc_,
+			  struct displaced_step_copy_insn_closure *dsc_,
 			  CORE_ADDR from, CORE_ADDR to,
 			  struct regcache *regs)
 {
-  arm_displaced_step_closure *dsc = (arm_displaced_step_closure *) dsc_;
+  arm_displaced_step_copy_insn_closure *dsc
+    = (arm_displaced_step_copy_insn_closure *) dsc_;
 
   if (dsc->cleanup)
     dsc->cleanup (gdbarch, regs, dsc);
diff --git a/gdb/arm-tdep.h b/gdb/arm-tdep.h
index fd7e2ea7e766..b1f52ff229f2 100644
--- a/gdb/arm-tdep.h
+++ b/gdb/arm-tdep.h
@@ -149,7 +149,8 @@ struct gdbarch_tdep
    sequence) and any scratch words, etc.  */
 #define ARM_DISPLACED_MODIFIED_INSNS	8
 
-struct arm_displaced_step_closure : public displaced_step_closure
+struct arm_displaced_step_copy_insn_closure
+  : public displaced_step_copy_insn_closure
 {
   ULONGEST tmp[DISPLACED_TEMPS];
   int rd;
@@ -196,7 +197,7 @@ struct arm_displaced_step_closure : public displaced_step_closure
       /* If non-NULL, override generic SVC handling (e.g. for a particular
 	 OS).  */
       int (*copy_svc_os) (struct gdbarch *gdbarch, struct regcache *regs,
-			  arm_displaced_step_closure *dsc);
+			  arm_displaced_step_copy_insn_closure *dsc);
     } svc;
   } u;
 
@@ -215,7 +216,7 @@ struct arm_displaced_step_closure : public displaced_step_closure
   CORE_ADDR insn_addr;
   CORE_ADDR scratch_base;
   void (*cleanup) (struct gdbarch *, struct regcache *,
-		   arm_displaced_step_closure *);
+		   arm_displaced_step_copy_insn_closure *);
 };
 
 /* Values for the WRITE_PC argument to displaced_write_reg.  If the register
@@ -234,16 +235,17 @@ enum pc_write_style
 extern void
   arm_process_displaced_insn (struct gdbarch *gdbarch, CORE_ADDR from,
 			      CORE_ADDR to, struct regcache *regs,
-			      arm_displaced_step_closure *dsc);
+			      arm_displaced_step_copy_insn_closure *dsc);
 extern void
   arm_displaced_init_closure (struct gdbarch *gdbarch, CORE_ADDR from,
-			      CORE_ADDR to, arm_displaced_step_closure *dsc);
+			      CORE_ADDR to,
+			      arm_displaced_step_copy_insn_closure *dsc);
 extern ULONGEST
-  displaced_read_reg (struct regcache *regs, arm_displaced_step_closure *dsc,
+  displaced_read_reg (regcache *regs, arm_displaced_step_copy_insn_closure *dsc,
 		      int regno);
 extern void
   displaced_write_reg (struct regcache *regs,
-		       arm_displaced_step_closure *dsc, int regno,
+		       arm_displaced_step_copy_insn_closure *dsc, int regno,
 		       ULONGEST val, enum pc_write_style write_pc);
 
 CORE_ADDR arm_skip_stub (struct frame_info *, CORE_ADDR);
@@ -262,7 +264,7 @@ int arm_is_thumb (struct regcache *regcache);
 int arm_frame_is_thumb (struct frame_info *frame);
 
 extern void arm_displaced_step_fixup (struct gdbarch *,
-				      struct displaced_step_closure *,
+				      displaced_step_copy_insn_closure *,
 				      CORE_ADDR, CORE_ADDR, struct regcache *);
 
 /* Return the bit mask in ARM_PS_REGNUM that indicates Thumb mode.  */
diff --git a/gdb/gdbarch.c b/gdb/gdbarch.c
index cd9341132ef1..0ec626c6f8c4 100644
--- a/gdb/gdbarch.c
+++ b/gdb/gdbarch.c
@@ -3969,7 +3969,7 @@ gdbarch_displaced_step_copy_insn_p (struct gdbarch *gdbarch)
   return gdbarch->displaced_step_copy_insn != NULL;
 }
 
-displaced_step_closure_up
+displaced_step_copy_insn_closure_up
 gdbarch_displaced_step_copy_insn (struct gdbarch *gdbarch, CORE_ADDR from, CORE_ADDR to, struct regcache *regs)
 {
   gdb_assert (gdbarch != NULL);
@@ -4011,7 +4011,7 @@ gdbarch_displaced_step_fixup_p (struct gdbarch *gdbarch)
 }
 
 void
-gdbarch_displaced_step_fixup (struct gdbarch *gdbarch, struct displaced_step_closure *closure, CORE_ADDR from, CORE_ADDR to, struct regcache *regs)
+gdbarch_displaced_step_fixup (struct gdbarch *gdbarch, struct displaced_step_copy_insn_closure *closure, CORE_ADDR from, CORE_ADDR to, struct regcache *regs)
 {
   gdb_assert (gdbarch != NULL);
   gdb_assert (gdbarch->displaced_step_fixup != NULL);
diff --git a/gdb/gdbarch.h b/gdb/gdbarch.h
index 70bd8a6b4428..4f52e8cd874f 100644
--- a/gdb/gdbarch.h
+++ b/gdb/gdbarch.h
@@ -1030,8 +1030,8 @@ extern void set_gdbarch_max_insn_length (struct gdbarch *gdbarch, ULONGEST max_i
 
 extern bool gdbarch_displaced_step_copy_insn_p (struct gdbarch *gdbarch);
 
-typedef displaced_step_closure_up (gdbarch_displaced_step_copy_insn_ftype) (struct gdbarch *gdbarch, CORE_ADDR from, CORE_ADDR to, struct regcache *regs);
-extern displaced_step_closure_up gdbarch_displaced_step_copy_insn (struct gdbarch *gdbarch, CORE_ADDR from, CORE_ADDR to, struct regcache *regs);
+typedef displaced_step_copy_insn_closure_up (gdbarch_displaced_step_copy_insn_ftype) (struct gdbarch *gdbarch, CORE_ADDR from, CORE_ADDR to, struct regcache *regs);
+extern displaced_step_copy_insn_closure_up gdbarch_displaced_step_copy_insn (struct gdbarch *gdbarch, CORE_ADDR from, CORE_ADDR to, struct regcache *regs);
 extern void set_gdbarch_displaced_step_copy_insn (struct gdbarch *gdbarch, gdbarch_displaced_step_copy_insn_ftype *displaced_step_copy_insn);
 
 /* Return true if GDB should use hardware single-stepping to execute a displaced
@@ -1066,8 +1066,8 @@ extern void set_gdbarch_displaced_step_hw_singlestep (struct gdbarch *gdbarch, g
 
 extern bool gdbarch_displaced_step_fixup_p (struct gdbarch *gdbarch);
 
-typedef void (gdbarch_displaced_step_fixup_ftype) (struct gdbarch *gdbarch, struct displaced_step_closure *closure, CORE_ADDR from, CORE_ADDR to, struct regcache *regs);
-extern void gdbarch_displaced_step_fixup (struct gdbarch *gdbarch, struct displaced_step_closure *closure, CORE_ADDR from, CORE_ADDR to, struct regcache *regs);
+typedef void (gdbarch_displaced_step_fixup_ftype) (struct gdbarch *gdbarch, struct displaced_step_copy_insn_closure *closure, CORE_ADDR from, CORE_ADDR to, struct regcache *regs);
+extern void gdbarch_displaced_step_fixup (struct gdbarch *gdbarch, struct displaced_step_copy_insn_closure *closure, CORE_ADDR from, CORE_ADDR to, struct regcache *regs);
 extern void set_gdbarch_displaced_step_fixup (struct gdbarch *gdbarch, gdbarch_displaced_step_fixup_ftype *displaced_step_fixup);
 
 /* Return the address of an appropriate place to put displaced
diff --git a/gdb/gdbarch.sh b/gdb/gdbarch.sh
index 86f2c798e32b..f67ec9dcd2d6 100755
--- a/gdb/gdbarch.sh
+++ b/gdb/gdbarch.sh
@@ -783,7 +783,7 @@ V;ULONGEST;max_insn_length;;;0;0
 # If the instruction cannot execute out of line, return NULL.  The
 # core falls back to stepping past the instruction in-line instead in
 # that case.
-M;displaced_step_closure_up;displaced_step_copy_insn;CORE_ADDR from, CORE_ADDR to, struct regcache *regs;from, to, regs
+M;displaced_step_copy_insn_closure_up;displaced_step_copy_insn;CORE_ADDR from, CORE_ADDR to, struct regcache *regs;from, to, regs
 
 # Return true if GDB should use hardware single-stepping to execute a displaced
 # step instruction.  If false, GDB will simply restart execution at the
@@ -811,7 +811,7 @@ m;bool;displaced_step_hw_singlestep;void;;;default_displaced_step_hw_singlestep;
 #
 # For a general explanation of displaced stepping and how GDB uses it,
 # see the comments in infrun.c.
-M;void;displaced_step_fixup;struct displaced_step_closure *closure, CORE_ADDR from, CORE_ADDR to, struct regcache *regs;closure, from, to, regs;;NULL
+M;void;displaced_step_fixup;struct displaced_step_copy_insn_closure *closure, CORE_ADDR from, CORE_ADDR to, struct regcache *regs;closure, from, to, regs;;NULL
 
 # Return the address of an appropriate place to put displaced
 # instructions while we step over them.  There need only be one such
diff --git a/gdb/i386-linux-tdep.c b/gdb/i386-linux-tdep.c
index b9ec95806349..ab7d23611f89 100644
--- a/gdb/i386-linux-tdep.c
+++ b/gdb/i386-linux-tdep.c
@@ -789,29 +789,30 @@ i386_linux_iterate_over_regset_sections (struct gdbarch *gdbarch,
    PC should get relocated back to its vDSO address.  Hide the 'ret'
    instruction by 'nop' so that i386_displaced_step_fixup is not confused.
    
-   It is not fully correct as the bytes in struct displaced_step_closure will
-   not match the inferior code.  But we would need some new flag in
-   displaced_step_closure otherwise to keep the state that syscall is finishing
-   for the later i386_displaced_step_fixup execution as the syscall execution
-   is already no longer detectable there.  The new flag field would mean
-   i386-linux-tdep.c needs to wrap all the displacement methods of i386-tdep.c
-   which does not seem worth it.  The same effect is achieved by patching that
-   'nop' instruction there instead.  */
-
-static displaced_step_closure_up
+   It is not fully correct as the bytes in struct
+   displaced_step_copy_insn_closure will not match the inferior code.  But we
+   would need some new flag in displaced_step_copy_insn_closure otherwise to
+   keep the state that syscall is finishing for the later
+   i386_displaced_step_fixup execution as the syscall execution is already no
+   longer detectable there.  The new flag field would mean i386-linux-tdep.c
+   needs to wrap all the displacement methods of i386-tdep.c which does not seem
+   worth it.  The same effect is achieved by patching that 'nop' instruction
+   there instead.  */
+
+static displaced_step_copy_insn_closure_up
 i386_linux_displaced_step_copy_insn (struct gdbarch *gdbarch,
 				     CORE_ADDR from, CORE_ADDR to,
 				     struct regcache *regs)
 {
-  displaced_step_closure_up closure_
+  displaced_step_copy_insn_closure_up closure_
     =  i386_displaced_step_copy_insn (gdbarch, from, to, regs);
 
   if (i386_linux_get_syscall_number_from_regcache (regs) != -1)
     {
       /* The closure returned by i386_displaced_step_copy_insn is simply a
 	 buffer with a copy of the instruction. */
-      i386_displaced_step_closure *closure
-	= (i386_displaced_step_closure *) closure_.get ();
+      i386_displaced_step_copy_insn_closure *closure
+	= (i386_displaced_step_copy_insn_closure *) closure_.get ();
 
       /* Fake nop.  */
       closure->buf[0] = 0x90;
diff --git a/gdb/i386-tdep.c b/gdb/i386-tdep.c
index 00de4e1ccb9e..42918b0b6979 100644
--- a/gdb/i386-tdep.c
+++ b/gdb/i386-tdep.c
@@ -799,14 +799,14 @@ i386_insn_is_jump (struct gdbarch *gdbarch, CORE_ADDR addr)
 
 /* Some kernels may run one past a syscall insn, so we have to cope.  */
 
-displaced_step_closure_up
+displaced_step_copy_insn_closure_up
 i386_displaced_step_copy_insn (struct gdbarch *gdbarch,
 			       CORE_ADDR from, CORE_ADDR to,
 			       struct regcache *regs)
 {
   size_t len = gdbarch_max_insn_length (gdbarch);
-  std::unique_ptr<i386_displaced_step_closure> closure
-    (new i386_displaced_step_closure (len));
+  std::unique_ptr<i386_displaced_step_copy_insn_closure> closure
+    (new i386_displaced_step_copy_insn_closure (len));
   gdb_byte *buf = closure->buf.data ();
 
   read_memory (from, buf, len);
@@ -830,7 +830,7 @@ i386_displaced_step_copy_insn (struct gdbarch *gdbarch,
 			  displaced_step_dump_bytes (buf, len).c_str ());
 
   /* This is a work around for a problem with g++ 4.8.  */
-  return displaced_step_closure_up (closure.release ());
+  return displaced_step_copy_insn_closure_up (closure.release ());
 }
 
 /* Fix up the state of registers and memory after having single-stepped
@@ -838,7 +838,7 @@ i386_displaced_step_copy_insn (struct gdbarch *gdbarch,
 
 void
 i386_displaced_step_fixup (struct gdbarch *gdbarch,
-			   struct displaced_step_closure *closure_,
+			   struct displaced_step_copy_insn_closure *closure_,
 			   CORE_ADDR from, CORE_ADDR to,
 			   struct regcache *regs)
 {
@@ -850,8 +850,8 @@ i386_displaced_step_fixup (struct gdbarch *gdbarch,
      applying it.  */
   ULONGEST insn_offset = to - from;
 
-  i386_displaced_step_closure *closure
-    = (i386_displaced_step_closure *) closure_;
+  i386_displaced_step_copy_insn_closure *closure
+    = (i386_displaced_step_copy_insn_closure *) closure_;
   gdb_byte *insn = closure->buf.data ();
   /* The start of the insn, needed in case we see some prefixes.  */
   gdb_byte *insn_start = insn;
diff --git a/gdb/i386-tdep.h b/gdb/i386-tdep.h
index 45d64eb009e9..f8a93b121c9b 100644
--- a/gdb/i386-tdep.h
+++ b/gdb/i386-tdep.h
@@ -439,15 +439,15 @@ extern void
 				     void *cb_data,
 				     const struct regcache *regcache);
 
-typedef buf_displaced_step_closure i386_displaced_step_closure;
+typedef buf_displaced_step_copy_insn_closure
+  i386_displaced_step_copy_insn_closure;
 
-extern displaced_step_closure_up i386_displaced_step_copy_insn
+extern displaced_step_copy_insn_closure_up i386_displaced_step_copy_insn
   (struct gdbarch *gdbarch, CORE_ADDR from, CORE_ADDR to,
    struct regcache *regs);
-extern void i386_displaced_step_fixup (struct gdbarch *gdbarch,
-				       struct displaced_step_closure *closure,
-				       CORE_ADDR from, CORE_ADDR to,
-				       struct regcache *regs);
+extern void i386_displaced_step_fixup
+  (struct gdbarch *gdbarch, displaced_step_copy_insn_closure *closure,
+   CORE_ADDR from, CORE_ADDR to, regcache *regs);
 
 /* Initialize a basic ELF architecture variant.  */
 extern void i386_elf_init_abi (struct gdbarch_info, struct gdbarch *);
diff --git a/gdb/infrun.c b/gdb/infrun.c
index b965e702538c..175d095154cc 100644
--- a/gdb/infrun.c
+++ b/gdb/infrun.c
@@ -1459,9 +1459,10 @@ step_over_info_valid_p (void)
    displaced step operation on it.  See displaced_step_prepare and
    displaced_step_fixup for details.  */
 
-/* Default destructor for displaced_step_closure.  */
+/* Default destructor for displaced_step_copy_insn_closure.  */
 
-displaced_step_closure::~displaced_step_closure () = default;
+displaced_step_copy_insn_closure::~displaced_step_copy_insn_closure ()
+  = default;
 
 /* Returns true if any inferior has a thread doing a displaced
    step.  */
@@ -1497,11 +1498,11 @@ displaced_step_in_progress (inferior *inf)
 }
 
 /* If inferior is in displaced stepping, and ADDR equals to starting address
-   of copy area, return corresponding displaced_step_closure.  Otherwise,
-   return NULL.  */
+   of copy area, return corresponding displaced_step_copy_insn_closure.
+   Otherwise, return NULL.  */
 
-struct displaced_step_closure*
-get_displaced_step_closure_by_addr (CORE_ADDR addr)
+displaced_step_copy_insn_closure *
+get_displaced_step_copy_insn_closure_by_addr (CORE_ADDR addr)
 {
   displaced_step_inferior_state &displaced
     = current_inferior ()->displaced_step_state;
diff --git a/gdb/infrun.h b/gdb/infrun.h
index ca0774e8e6b9..a276ddf02502 100644
--- a/gdb/infrun.h
+++ b/gdb/infrun.h
@@ -239,8 +239,8 @@ extern void clear_exit_convenience_vars (void);
 /* Dump LEN bytes at BUF in hex to a string and return it.  */
 extern std::string displaced_step_dump_bytes (const gdb_byte *buf, size_t len);
 
-extern struct displaced_step_closure *get_displaced_step_closure_by_addr
-    (CORE_ADDR addr);
+extern struct displaced_step_copy_insn_closure *
+  get_displaced_step_copy_insn_closure_by_addr (CORE_ADDR addr);
 
 extern void update_observer_mode (void);
 
@@ -282,18 +282,19 @@ extern void all_uis_on_sync_execution_starting (void);
 
 /* Base class for displaced stepping closures (the arch-specific data).  */
 
-struct displaced_step_closure
+struct displaced_step_copy_insn_closure
 {
-  virtual ~displaced_step_closure () = 0;
+  virtual ~displaced_step_copy_insn_closure () = 0;
 };
 
-using displaced_step_closure_up = std::unique_ptr<displaced_step_closure>;
+using displaced_step_copy_insn_closure_up
+  = std::unique_ptr<displaced_step_copy_insn_closure>;
 
 /* A simple displaced step closure that contains only a byte buffer.  */
 
-struct buf_displaced_step_closure : displaced_step_closure
+struct buf_displaced_step_copy_insn_closure : displaced_step_copy_insn_closure
 {
-  buf_displaced_step_closure (int buf_size)
+  buf_displaced_step_copy_insn_closure (int buf_size)
   : buf (buf_size)
   {}
 
@@ -334,7 +335,7 @@ struct displaced_step_inferior_state
 
   /* The closure provided gdbarch_displaced_step_copy_insn, to be used
      for post-step cleanup.  */
-  displaced_step_closure_up step_closure;
+  displaced_step_copy_insn_closure_up step_closure;
 
   /* The address of the original instruction, and the copy we
      made.  */
diff --git a/gdb/rs6000-tdep.c b/gdb/rs6000-tdep.c
index bde3ee7b3575..626b47e244f1 100644
--- a/gdb/rs6000-tdep.c
+++ b/gdb/rs6000-tdep.c
@@ -851,18 +851,19 @@ typedef BP_MANIPULATION_ENDIAN (little_breakpoint, big_breakpoint)
 					 || (insn & STORE_CONDITIONAL_MASK) == STHCX_INSTRUCTION \
 					 || (insn & STORE_CONDITIONAL_MASK) == STQCX_INSTRUCTION)
 
-typedef buf_displaced_step_closure ppc_displaced_step_closure;
+typedef buf_displaced_step_copy_insn_closure
+  ppc_displaced_step_copy_insn_closure;
 
 /* We can't displaced step atomic sequences.  */
 
-static displaced_step_closure_up
+static displaced_step_copy_insn_closure_up
 ppc_displaced_step_copy_insn (struct gdbarch *gdbarch,
 			      CORE_ADDR from, CORE_ADDR to,
 			      struct regcache *regs)
 {
   size_t len = gdbarch_max_insn_length (gdbarch);
-  std::unique_ptr<ppc_displaced_step_closure> closure
-    (new ppc_displaced_step_closure (len));
+  std::unique_ptr<ppc_displaced_step_copy_insn_closure> closure
+    (new ppc_displaced_step_copy_insn_closure (len));
   gdb_byte *buf = closure->buf.data ();
   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
   int insn;
@@ -887,20 +888,21 @@ ppc_displaced_step_copy_insn (struct gdbarch *gdbarch,
 			  displaced_step_dump_bytes (buf, len).c_str ());;
 
   /* This is a work around for a problem with g++ 4.8.  */
-  return displaced_step_closure_up (closure.release ());
+  return displaced_step_copy_insn_closure_up (closure.release ());
 }
 
 /* Fix up the state of registers and memory after having single-stepped
    a displaced instruction.  */
 static void
 ppc_displaced_step_fixup (struct gdbarch *gdbarch,
-			  struct displaced_step_closure *closure_,
+			  struct displaced_step_copy_insn_closure *closure_,
 			  CORE_ADDR from, CORE_ADDR to,
 			  struct regcache *regs)
 {
   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
   /* Our closure is a copy of the instruction.  */
-  ppc_displaced_step_closure *closure = (ppc_displaced_step_closure *) closure_;
+  ppc_displaced_step_copy_insn_closure *closure
+    = (ppc_displaced_step_copy_insn_closure *) closure_;
   ULONGEST insn  = extract_unsigned_integer (closure->buf.data (),
 					     PPC_INSN_SIZE, byte_order);
   ULONGEST opcode = 0;
diff --git a/gdb/s390-tdep.c b/gdb/s390-tdep.c
index 247adcd6e274..5429d718c3be 100644
--- a/gdb/s390-tdep.c
+++ b/gdb/s390-tdep.c
@@ -421,18 +421,19 @@ is_non_branch_ril (gdb_byte *insn)
   return 0;
 }
 
-typedef buf_displaced_step_closure s390_displaced_step_closure;
+typedef buf_displaced_step_copy_insn_closure
+  s390_displaced_step_copy_insn_closure;
 
 /* Implementation of gdbarch_displaced_step_copy_insn.  */
 
-static displaced_step_closure_up
+static displaced_step_copy_insn_closure_up
 s390_displaced_step_copy_insn (struct gdbarch *gdbarch,
 			       CORE_ADDR from, CORE_ADDR to,
 			       struct regcache *regs)
 {
   size_t len = gdbarch_max_insn_length (gdbarch);
-  std::unique_ptr<s390_displaced_step_closure> closure
-    (new s390_displaced_step_closure (len));
+  std::unique_ptr<s390_displaced_step_copy_insn_closure> closure
+    (new s390_displaced_step_copy_insn_closure (len));
   gdb_byte *buf = closure->buf.data ();
 
   read_memory (from, buf, len);
@@ -470,7 +471,7 @@ s390_displaced_step_copy_insn (struct gdbarch *gdbarch,
 			  displaced_step_dump_bytes (buf, len).c_str ());
 
   /* This is a work around for a problem with g++ 4.8.  */
-  return displaced_step_closure_up (closure.release ());
+  return displaced_step_copy_insn_closure_up (closure.release ());
 }
 
 /* Fix up the state of registers and memory after having single-stepped
@@ -478,13 +479,13 @@ s390_displaced_step_copy_insn (struct gdbarch *gdbarch,
 
 static void
 s390_displaced_step_fixup (struct gdbarch *gdbarch,
-			   struct displaced_step_closure *closure_,
+			   displaced_step_copy_insn_closure *closure_,
 			   CORE_ADDR from, CORE_ADDR to,
 			   struct regcache *regs)
 {
   /* Our closure is a copy of the instruction.  */
-  s390_displaced_step_closure *closure
-    = (s390_displaced_step_closure *) closure_;
+  s390_displaced_step_copy_insn_closure *closure
+    = (s390_displaced_step_copy_insn_closure *) closure_;
   gdb_byte *insn = closure->buf.data ();
   static int s390_instrlen[] = { 2, 4, 4, 6 };
   int insnlen = s390_instrlen[insn[0] >> 6];
-- 
2.29.2


^ permalink raw reply	[flat|nested] 40+ messages in thread

* [PATCH v2 07/14] gdb: rename displaced_step_fixup to displaced_step_finish
  2020-12-02 15:47 [PATCH v2 00/14] Concurrent displaced stepping Simon Marchi
                   ` (5 preceding siblings ...)
  2020-12-02 15:47 ` [PATCH v2 06/14] gdb: rename displaced_step_closure to displaced_step_copy_insn_closure Simon Marchi
@ 2020-12-02 15:47 ` Simon Marchi
  2020-12-04  1:49   ` Pedro Alves
  2020-12-02 15:47 ` [PATCH v2 08/14] gdb: introduce status enum for displaced step prepare/finish Simon Marchi
                   ` (7 subsequent siblings)
  14 siblings, 1 reply; 40+ messages in thread
From: Simon Marchi @ 2020-12-02 15:47 UTC (permalink / raw)
  To: gdb-patches; +Cc: Simon Marchi

From: Simon Marchi <simon.marchi@efficios.com>

This is a preparatory patch to reduce a little bit the diff size of the
main patch later in this series.  It renames the displaced_step_fixup
function in infrun.c to displaced_step_finish.

The rationale is to better differentiate the low and high level
operations.

We first have the low level operation of writing an instruction to a
displaced buffer, called "copy_insn".  The mirror low level operation to
fix up the state after having executed the instruction is "fixup".  The
high level operation of preparing a thread for a displaced step (which
includes doing the "copy_insn" and some more bookkeeping) is called
"prepare" (as in displaced_step_prepare).  The mirror high level
operation to cleaning up after a displaced step (which includes doing
the "fixup" and some more bookkeeping) is currently also called "fixup"
(as in displaced_step_fixup), just like the low level operation.

I think that choosing a different name for the low and high level
cleanup operation makes it clearer, hence "finish".

gdb/ChangeLog:

	* infrun.c (displaced_step_fixup): Rename to...
	(displaced_step_finish): ... this, update all callers.

Change-Id: Id32f48c1e2091d09854c77fcedcc14d2519957a2
---
 gdb/infrun.c | 16 ++++++++--------
 1 file changed, 8 insertions(+), 8 deletions(-)

diff --git a/gdb/infrun.c b/gdb/infrun.c
index 175d095154cc..1b747fedbbc5 100644
--- a/gdb/infrun.c
+++ b/gdb/infrun.c
@@ -1457,7 +1457,7 @@ step_over_info_valid_p (void)
    place it in the displaced_step_request_queue.  Whenever a displaced
    step finishes, we pick the next thread in the queue and start a new
    displaced step operation on it.  See displaced_step_prepare and
-   displaced_step_fixup for details.  */
+   displaced_step_finish for details.  */
 
 /* Default destructor for displaced_step_copy_insn_closure.  */
 
@@ -1840,7 +1840,7 @@ displaced_step_restore (struct displaced_step_inferior_state *displaced,
    -1.  If the thread wasn't displaced stepping, return 0.  */
 
 static int
-displaced_step_fixup (thread_info *event_thread, enum gdb_signal signal)
+displaced_step_finish (thread_info *event_thread, enum gdb_signal signal)
 {
   displaced_step_inferior_state *displaced
     = &event_thread->inf->displaced_step_state;
@@ -2201,7 +2201,7 @@ do_target_resume (ptid_t resume_ptid, bool step, enum gdb_signal sig)
 
      Likewise if we're displaced stepping, otherwise a trap for a
      breakpoint in a signal handler might be confused with the
-     displaced step finishing.  We don't make the displaced_step_fixup
+     displaced step finishing.  We don't make the displaced_step_finish
      step distinguish the cases instead, because:
 
      - a backtrace while stopped in the signal handler would show the
@@ -4815,7 +4815,7 @@ stop_all_threads (void)
 		      t->suspend.waitstatus.kind = TARGET_WAITKIND_IGNORE;
 		      t->suspend.waitstatus_pending_p = 0;
 
-		      if (displaced_step_fixup (t, GDB_SIGNAL_0) < 0)
+		      if (displaced_step_finish (t, GDB_SIGNAL_0) < 0)
 			{
 			  /* Add it back to the step-over queue.  */
 			  infrun_debug_printf
@@ -4843,7 +4843,7 @@ stop_all_threads (void)
 		      sig = (event.ws.kind == TARGET_WAITKIND_STOPPED
 			     ? event.ws.value.sig : GDB_SIGNAL_0);
 
-		      if (displaced_step_fixup (t, sig) < 0)
+		      if (displaced_step_finish (t, sig) < 0)
 			{
 			  /* Add it back to the step-over queue.  */
 			  t->control.trap_expected = 0;
@@ -5313,7 +5313,7 @@ handle_inferior_event (struct execution_control_state *ecs)
 	       has been done.  Perform cleanup for parent process here.  Note
 	       that this operation also cleans up the child process for vfork,
 	       because their pages are shared.  */
-	    displaced_step_fixup (ecs->event_thread, GDB_SIGNAL_TRAP);
+	    displaced_step_finish (ecs->event_thread, GDB_SIGNAL_TRAP);
 	    /* Start a new step-over in another thread if there's one
 	       that needs it.  */
 	    start_step_over ();
@@ -5656,8 +5656,8 @@ resumed_thread_with_pending_status (struct thread_info *tp,
 static int
 finish_step_over (struct execution_control_state *ecs)
 {
-  displaced_step_fixup (ecs->event_thread,
-			ecs->event_thread->suspend.stop_signal);
+  displaced_step_finish (ecs->event_thread,
+			 ecs->event_thread->suspend.stop_signal);
 
   bool had_step_over_info = step_over_info_valid_p ();
 
-- 
2.29.2


^ permalink raw reply	[flat|nested] 40+ messages in thread

* [PATCH v2 08/14] gdb: introduce status enum for displaced step prepare/finish
  2020-12-02 15:47 [PATCH v2 00/14] Concurrent displaced stepping Simon Marchi
                   ` (6 preceding siblings ...)
  2020-12-02 15:47 ` [PATCH v2 07/14] gdb: rename displaced_step_fixup to displaced_step_finish Simon Marchi
@ 2020-12-02 15:47 ` Simon Marchi
  2020-12-04  1:49   ` Pedro Alves
  2020-12-02 15:48 ` [PATCH v2 09/14] gdb: pass inferior to get_linux_inferior_data Simon Marchi
                   ` (6 subsequent siblings)
  14 siblings, 1 reply; 40+ messages in thread
From: Simon Marchi @ 2020-12-02 15:47 UTC (permalink / raw)
  To: gdb-patches; +Cc: Simon Marchi

From: Simon Marchi <simon.marchi@efficios.com>

New in v2:

 - Rename DISPLACED_STEP_PREPARE_STATUS_ERROR to
   DISPLACED_STEP_PREPARE_STATUS_CANT.

This is a preparatory patch to reduce the size of the diff of the
upcoming main patch.  It introduces enum types for the return values of
displaced step "prepare" and "finish" operations.  I find that this
expresses better the intention of the code, rather than returning
arbitrary integer values (-1, 0 and 1) which are difficult to remember.
That makes the code easier to read.

I put the new enum types in a new displaced-stepping.h file, because I
introduce that file in a later patch anyway.  Putting it there avoids
having to move it later.

There is one change in behavior for displaced_step_finish: it currently
returns 0 if the thread wasn't doing a displaced step and 1 if the
thread was doing a displaced step which was executed successfully.  It
turns out that this distinction is not needed by any caller, so I've
merged these two cases into "_OK", rather than adding an extra
enumerator.

gdb/ChangeLog:

	* infrun.c (displaced_step_prepare_throw): Change return type to
	displaced_step_prepare_status.
	(displaced_step_prepare): Likewise.
	(displaced_step_finish): Change return type to
	displaced_step_finish_status.
	(resume_1): Adjust.
	(stop_all_threads): Adjust.
	* displaced-stepping.h: New file.

Change-Id: I5c8fe07212cd398d5b486b5936d9d0807acd3788
---
 gdb/displaced-stepping.h | 47 ++++++++++++++++++++++++++
 gdb/infrun.c             | 72 +++++++++++++++++++++++-----------------
 2 files changed, 89 insertions(+), 30 deletions(-)
 create mode 100644 gdb/displaced-stepping.h

diff --git a/gdb/displaced-stepping.h b/gdb/displaced-stepping.h
new file mode 100644
index 000000000000..6068b3a33dcf
--- /dev/null
+++ b/gdb/displaced-stepping.h
@@ -0,0 +1,47 @@
+/* Displaced stepping related things.
+
+   Copyright (C) 2020 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 DISPLACED_STEPPING_H
+#define DISPLACED_STEPPING_H
+
+enum displaced_step_prepare_status
+{
+  /* A displaced stepping buffer was successfully allocated and prepared.  */
+  DISPLACED_STEP_PREPARE_STATUS_OK,
+
+  /* This particular instruction can't be displaced step, GDB should fall back
+     on in-line stepping.  */
+  DISPLACED_STEP_PREPARE_STATUS_CANT,
+
+  /* Not enough resources are available at this time, try again later.  */
+  DISPLACED_STEP_PREPARE_STATUS_UNAVAILABLE,
+};
+
+enum displaced_step_finish_status
+{
+  /* Either the instruction was stepped and fixed up, or the specified thread
+     wasn't executing a displaced step (in which case there's nothing to
+     finish). */
+  DISPLACED_STEP_FINISH_STATUS_OK,
+
+  /* The thread started a displaced step, but didn't complete it.  */
+  DISPLACED_STEP_FINISH_STATUS_NOT_EXECUTED,
+};
+
+#endif /* DISPLACED_STEPPING_H */
diff --git a/gdb/infrun.c b/gdb/infrun.c
index 1b747fedbbc5..29bbc209d188 100644
--- a/gdb/infrun.c
+++ b/gdb/infrun.c
@@ -19,6 +19,7 @@
    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
 
 #include "defs.h"
+#include "displaced-stepping.h"
 #include "infrun.h"
 #include <ctype.h>
 #include "symtab.h"
@@ -1650,11 +1651,13 @@ displaced_step_dump_bytes (const gdb_byte *buf, size_t len)
    Comments in the code for 'random signals' in handle_inferior_event
    explain how we handle this case instead.
 
-   Returns 1 if preparing was successful -- this thread is going to be
-   stepped now; 0 if displaced stepping this thread got queued; or -1
-   if this instruction can't be displaced stepped.  */
+   Returns DISPLACED_STEP_PREPARE_STATUS_OK if preparing was successful -- this
+   thread is going to be stepped now; DISPLACED_STEP_PREPARE_STATUS_UNAVAILABLE
+   if displaced stepping this thread got queued; or
+   DISPLACED_STEP_PREPARE_STATUS_CANT if this instruction can't be displaced
+   stepped.  */
 
-static int
+static displaced_step_prepare_status
 displaced_step_prepare_throw (thread_info *tp)
 {
   regcache *regcache = get_thread_regcache (tp);
@@ -1691,7 +1694,7 @@ displaced_step_prepare_throw (thread_info *tp)
 			      target_pid_to_str (tp->ptid).c_str ());
 
       global_thread_step_over_chain_enqueue (tp);
-      return 0;
+      return DISPLACED_STEP_PREPARE_STATUS_UNAVAILABLE;
     }
   else
     displaced_debug_printf ("stepping %s now",
@@ -1722,7 +1725,7 @@ displaced_step_prepare_throw (thread_info *tp)
       displaced_debug_printf ("breakpoint set in scratch pad.  "
 			      "Stepping over breakpoint in-line instead.");
 
-      return -1;
+      return DISPLACED_STEP_PREPARE_STATUS_CANT;
     }
 
   /* Save the original contents of the copy area.  */
@@ -1746,7 +1749,7 @@ displaced_step_prepare_throw (thread_info *tp)
       /* The architecture doesn't know how or want to displaced step
 	 this instruction or instruction sequence.  Fallback to
 	 stepping over the breakpoint in-line.  */
-      return -1;
+      return DISPLACED_STEP_PREPARE_STATUS_CANT;
     }
 
   /* Save the information we need to fix things up if the step
@@ -1767,20 +1770,21 @@ displaced_step_prepare_throw (thread_info *tp)
 
   displaced_debug_printf ("displaced pc to %s", paddress (gdbarch, copy));
 
-  return 1;
+  return DISPLACED_STEP_PREPARE_STATUS_OK;
 }
 
 /* Wrapper for displaced_step_prepare_throw that disabled further
    attempts at displaced stepping if we get a memory error.  */
 
-static int
+static displaced_step_prepare_status
 displaced_step_prepare (thread_info *thread)
 {
-  int prepared = -1;
+  displaced_step_prepare_status status
+    = DISPLACED_STEP_PREPARE_STATUS_CANT;
 
   try
     {
-      prepared = displaced_step_prepare_throw (thread);
+      status = displaced_step_prepare_throw (thread);
     }
   catch (const gdb_exception_error &ex)
     {
@@ -1803,7 +1807,7 @@ displaced_step_prepare (thread_info *thread)
       thread->inf->displaced_step_state.failed_before = 1;
     }
 
-  return prepared;
+  return status;
 }
 
 static void
@@ -1833,22 +1837,24 @@ displaced_step_restore (struct displaced_step_inferior_state *displaced,
 				    displaced->step_copy));
 }
 
-/* If we displaced stepped an instruction successfully, adjust
-   registers and memory to yield the same effect the instruction would
-   have had if we had executed it at its original address, and return
-   1.  If the instruction didn't complete, relocate the PC and return
-   -1.  If the thread wasn't displaced stepping, return 0.  */
+/* If we displaced stepped an instruction successfully, adjust registers and
+   memory to yield the same effect the instruction would have had if we had
+   executed it at its original address, and return
+   DISPLACED_STEP_FINISH_STATUS_OK.  If the instruction didn't complete,
+   relocate the PC and return DISPLACED_STEP_FINISH_STATUS_NOT_EXECUTED.
 
-static int
+   If the thread wasn't displaced stepping, return
+   DISPLACED_STEP_FINISH_STATUS_OK as well.  */
+
+static displaced_step_finish_status
 displaced_step_finish (thread_info *event_thread, enum gdb_signal signal)
 {
   displaced_step_inferior_state *displaced
     = &event_thread->inf->displaced_step_state;
-  int ret;
 
   /* Was this event for the thread we displaced?  */
   if (displaced->step_thread != event_thread)
-    return 0;
+    return DISPLACED_STEP_FINISH_STATUS_OK;
 
   /* Fixup may need to read memory/registers.  Switch to the thread
      that we're fixing up.  Also, target_stopped_by_watchpoint checks
@@ -1872,7 +1878,8 @@ displaced_step_finish (thread_info *event_thread, enum gdb_signal signal)
 				    displaced->step_original,
 				    displaced->step_copy,
 				    get_thread_regcache (displaced->step_thread));
-      ret = 1;
+
+      return DISPLACED_STEP_FINISH_STATUS_OK;
     }
   else
     {
@@ -1883,10 +1890,9 @@ displaced_step_finish (thread_info *event_thread, enum gdb_signal signal)
 
       pc = displaced->step_original + (pc - displaced->step_copy);
       regcache_write_pc (regcache, pc);
-      ret = -1;
-    }
 
-  return ret;
+      return DISPLACED_STEP_FINISH_STATUS_NOT_EXECUTED;
+    }
 }
 
 /* Data to be passed around while handling an event.  This data is
@@ -2410,16 +2416,17 @@ resume_1 (enum gdb_signal sig)
       && sig == GDB_SIGNAL_0
       && !current_inferior ()->waiting_for_vfork_done)
     {
-      int prepared = displaced_step_prepare (tp);
+      displaced_step_prepare_status prepare_status
+	= displaced_step_prepare (tp);
 
-      if (prepared == 0)
+      if (prepare_status == DISPLACED_STEP_PREPARE_STATUS_UNAVAILABLE)
 	{
 	  infrun_debug_printf ("Got placed in step-over queue");
 
 	  tp->control.trap_expected = 0;
 	  return;
 	}
-      else if (prepared < 0)
+      else if (prepare_status == DISPLACED_STEP_PREPARE_STATUS_CANT)
 	{
 	  /* Fallback to stepping over the breakpoint in-line.  */
 
@@ -2433,7 +2440,7 @@ resume_1 (enum gdb_signal sig)
 
 	  insert_breakpoints ();
 	}
-      else if (prepared > 0)
+      else if (prepare_status == DISPLACED_STEP_PREPARE_STATUS_OK)
 	{
 	  /* Update pc to reflect the new address from which we will
 	     execute instructions due to displaced stepping.  */
@@ -2441,6 +2448,9 @@ resume_1 (enum gdb_signal sig)
 
 	  step = gdbarch_displaced_step_hw_singlestep (gdbarch);
 	}
+      else
+	gdb_assert_not_reached (_("Invalid displaced_step_prepare_status "
+				  "value."));
     }
 
   /* Do we need to do it the hard way, w/temp breakpoints?  */
@@ -4815,7 +4825,8 @@ stop_all_threads (void)
 		      t->suspend.waitstatus.kind = TARGET_WAITKIND_IGNORE;
 		      t->suspend.waitstatus_pending_p = 0;
 
-		      if (displaced_step_finish (t, GDB_SIGNAL_0) < 0)
+		      if (displaced_step_finish (t, GDB_SIGNAL_0)
+			  == DISPLACED_STEP_FINISH_STATUS_NOT_EXECUTED)
 			{
 			  /* Add it back to the step-over queue.  */
 			  infrun_debug_printf
@@ -4843,7 +4854,8 @@ stop_all_threads (void)
 		      sig = (event.ws.kind == TARGET_WAITKIND_STOPPED
 			     ? event.ws.value.sig : GDB_SIGNAL_0);
 
-		      if (displaced_step_finish (t, sig) < 0)
+		      if (displaced_step_finish (t, sig)
+			  == DISPLACED_STEP_FINISH_STATUS_NOT_EXECUTED)
 			{
 			  /* Add it back to the step-over queue.  */
 			  t->control.trap_expected = 0;
-- 
2.29.2


^ permalink raw reply	[flat|nested] 40+ messages in thread

* [PATCH v2 09/14] gdb: pass inferior to get_linux_inferior_data
  2020-12-02 15:47 [PATCH v2 00/14] Concurrent displaced stepping Simon Marchi
                   ` (7 preceding siblings ...)
  2020-12-02 15:47 ` [PATCH v2 08/14] gdb: introduce status enum for displaced step prepare/finish Simon Marchi
@ 2020-12-02 15:48 ` Simon Marchi
  2020-12-04  1:50   ` Pedro Alves
  2020-12-02 15:48 ` [PATCH v2 10/14] gdb: move displaced stepping types to displaced-stepping.{h, c} Simon Marchi
                   ` (5 subsequent siblings)
  14 siblings, 1 reply; 40+ messages in thread
From: Simon Marchi @ 2020-12-02 15:48 UTC (permalink / raw)
  To: gdb-patches; +Cc: Simon Marchi

From: Simon Marchi <simon.marchi@efficios.com>

Pass to get_linux_inferior_data the inferior for which we want to obtain
the linux-specific data, rather than assuming the current inferior.
This helps slightly reduce the diff in the upcoming main patch.

Update the sole caller to pass the current inferior.

gdb/ChangeLog:

	* linux-tdep.c (get_linux_inferior_data): Add inferior
	parameter.
	(linux_vsyscall_range): Pass current inferior.

Change-Id: Ie4b61190e4a2e89b5b55a140cfecd4de66d92393
---
 gdb/linux-tdep.c | 10 ++++------
 1 file changed, 4 insertions(+), 6 deletions(-)

diff --git a/gdb/linux-tdep.c b/gdb/linux-tdep.c
index a651d1b26517..655a2c33737d 100644
--- a/gdb/linux-tdep.c
+++ b/gdb/linux-tdep.c
@@ -217,13 +217,11 @@ invalidate_linux_cache_inf (struct inferior *inf)
    valid INFO pointer.  */
 
 static struct linux_info *
-get_linux_inferior_data (void)
+get_linux_inferior_data (inferior *inf)
 {
-  struct linux_info *info;
-  struct inferior *inf = current_inferior ();
+  linux_info *info = linux_inferior_data.get (inf);
 
-  info = linux_inferior_data.get (inf);
-  if (info == NULL)
+  if (info == nullptr)
     info = linux_inferior_data.emplace (inf);
 
   return info;
@@ -2407,7 +2405,7 @@ linux_vsyscall_range_raw (struct gdbarch *gdbarch, struct mem_range *range)
 static int
 linux_vsyscall_range (struct gdbarch *gdbarch, struct mem_range *range)
 {
-  struct linux_info *info = get_linux_inferior_data ();
+  struct linux_info *info = get_linux_inferior_data (current_inferior ());
 
   if (info->vsyscall_range_p == 0)
     {
-- 
2.29.2


^ permalink raw reply	[flat|nested] 40+ messages in thread

* [PATCH v2 10/14] gdb: move displaced stepping types to displaced-stepping.{h, c}
  2020-12-02 15:47 [PATCH v2 00/14] Concurrent displaced stepping Simon Marchi
                   ` (8 preceding siblings ...)
  2020-12-02 15:48 ` [PATCH v2 09/14] gdb: pass inferior to get_linux_inferior_data Simon Marchi
@ 2020-12-02 15:48 ` Simon Marchi
  2020-12-04  1:50   ` Pedro Alves
  2020-12-02 15:48 ` [PATCH v2 11/14] gdb: move displaced stepping logic to gdbarch, allow starting concurrent displaced steps Simon Marchi
                   ` (4 subsequent siblings)
  14 siblings, 1 reply; 40+ messages in thread
From: Simon Marchi @ 2020-12-02 15:48 UTC (permalink / raw)
  To: gdb-patches; +Cc: Simon Marchi

From: Simon Marchi <simon.marchi@efficios.com>

Move displaced-stepping related stuff unchanged to displaced-stepping.h
and displaced-stepping.c.  This helps make the following patch a bit
smaller and easier to read.

gdb/ChangeLog:

	* Makefile.in (COMMON_SFILES): Add displaced-stepping.c.
	* aarch64-tdep.h: Include displaced-stepping.h.
	* displaced-stepping.h (struct displaced_step_copy_insn_closure):
	Move here.
	(displaced_step_copy_insn_closure_up): Move here.
	(struct buf_displaced_step_copy_insn_closure): Move here.
	(struct displaced_step_inferior_state): Move here.
	(debug_displaced): Move here.
	(displaced_debug_printf_1): Move here.
	(displaced_debug_printf): Move here.
	* displaced-stepping.c: New file.
	* gdbarch.sh: Include displaced-stepping.h in gdbarch.h.
	* gdbarch.h: Re-generate.
	* inferior.h: Include displaced-stepping.h.
	* infrun.h (debug_displaced): Move to displaced-stepping.h.
	(displaced_debug_printf_1): Likewise.
	(displaced_debug_printf): Likewise.
	(struct displaced_step_copy_insn_closure): Likewise.
	(displaced_step_copy_insn_closure_up): Likewise.
	(struct buf_displaced_step_copy_insn_closure): Likewise.
	(struct displaced_step_inferior_state): Likewise.
	* infrun.c (show_debug_displaced): Move to displaced-stepping.c.
	(displaced_debug_printf_1): Likewise.
	(displaced_step_copy_insn_closure::~displaced_step_copy_insn_closure):
	Likewise.
	(_initialize_infrun): Don't register "set/show debug displaced".

Change-Id: I29935f5959b80425370630a45148fc06cd4227ca
---
 gdb/Makefile.in          |  1 +
 gdb/aarch64-tdep.h       |  1 +
 gdb/displaced-stepping.c | 52 +++++++++++++++++++++++++
 gdb/displaced-stepping.h | 84 ++++++++++++++++++++++++++++++++++++++++
 gdb/gdbarch.h            |  1 +
 gdb/gdbarch.sh           |  1 +
 gdb/inferior.h           |  1 +
 gdb/infrun.c             | 22 -----------
 gdb/infrun.h             | 78 -------------------------------------
 9 files changed, 141 insertions(+), 100 deletions(-)
 create mode 100644 gdb/displaced-stepping.c

diff --git a/gdb/Makefile.in b/gdb/Makefile.in
index a86e8d648e65..997c99c30f27 100644
--- a/gdb/Makefile.in
+++ b/gdb/Makefile.in
@@ -1019,6 +1019,7 @@ COMMON_SFILES = \
 	debuginfod-support.c \
 	dictionary.c \
 	disasm.c \
+	displaced-stepping.c \
 	dummy-frame.c \
 	dwarf2/abbrev.c \
 	dwarf2/attribute.c \
diff --git a/gdb/aarch64-tdep.h b/gdb/aarch64-tdep.h
index 895aa5977f05..76ff812abc64 100644
--- a/gdb/aarch64-tdep.h
+++ b/gdb/aarch64-tdep.h
@@ -23,6 +23,7 @@
 #define AARCH64_TDEP_H
 
 #include "arch/aarch64.h"
+#include "displaced-stepping.h"
 #include "infrun.h"
 
 /* Forward declarations.  */
diff --git a/gdb/displaced-stepping.c b/gdb/displaced-stepping.c
new file mode 100644
index 000000000000..5ae280fac398
--- /dev/null
+++ b/gdb/displaced-stepping.c
@@ -0,0 +1,52 @@
+/* Displaced stepping related things.
+
+   Copyright (C) 2020 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/>.  */
+
+#include "defs.h"
+#include "displaced-stepping.h"
+#include "cli/cli-cmds.h"
+
+#include "command.h"
+
+/* Default destructor for displaced_step_copy_insn_closure.  */
+
+displaced_step_copy_insn_closure::~displaced_step_copy_insn_closure ()
+  = default;
+
+bool debug_displaced = false;
+
+static void
+show_debug_displaced (struct ui_file *file, int from_tty,
+		      struct cmd_list_element *c, const char *value)
+{
+  fprintf_filtered (file, _("Displace stepping debugging is %s.\n"), value);
+}
+
+void _initialize_displaced_stepping ();
+void
+_initialize_displaced_stepping ()
+{
+  add_setshow_boolean_cmd ("displaced", class_maintenance,
+			   &debug_displaced, _("\
+Set displaced stepping debugging."), _("\
+Show displaced stepping debugging."), _("\
+When non-zero, displaced stepping specific debugging is enabled."),
+			    NULL,
+			    show_debug_displaced,
+			    &setdebuglist, &showdebuglist);
+}
diff --git a/gdb/displaced-stepping.h b/gdb/displaced-stepping.h
index 6068b3a33dcf..82ea3389cf30 100644
--- a/gdb/displaced-stepping.h
+++ b/gdb/displaced-stepping.h
@@ -20,6 +20,24 @@
 #ifndef DISPLACED_STEPPING_H
 #define DISPLACED_STEPPING_H
 
+#include "gdbsupport/byte-vector.h"
+
+struct thread_info;
+
+/* True if we are debugging displaced stepping.  */
+
+extern bool debug_displaced;
+
+/* Print a "displaced" debug statement.  */
+
+#define displaced_debug_printf(fmt, ...) \
+  do \
+    { \
+      if (debug_displaced) \
+	debug_prefixed_printf ("displaced", __func__, fmt, ##__VA_ARGS__); \
+    } \
+  while (0)
+
 enum displaced_step_prepare_status
 {
   /* A displaced stepping buffer was successfully allocated and prepared.  */
@@ -44,4 +62,70 @@ enum displaced_step_finish_status
   DISPLACED_STEP_FINISH_STATUS_NOT_EXECUTED,
 };
 
+/* Base class for displaced stepping closures (the arch-specific data).  */
+
+struct displaced_step_copy_insn_closure
+{
+  virtual ~displaced_step_copy_insn_closure () = 0;
+};
+
+using displaced_step_copy_insn_closure_up
+  = std::unique_ptr<displaced_step_copy_insn_closure>;
+
+/* A simple displaced step closure that contains only a byte buffer.  */
+
+struct buf_displaced_step_copy_insn_closure : displaced_step_copy_insn_closure
+{
+  buf_displaced_step_copy_insn_closure (int buf_size)
+  : buf (buf_size)
+  {}
+
+  gdb::byte_vector buf;
+};
+
+/* Per-inferior displaced stepping state.  */
+
+struct displaced_step_inferior_state
+{
+  displaced_step_inferior_state ()
+  {
+    reset ();
+  }
+
+  /* Put this object back in its original state.  */
+  void reset ()
+  {
+    failed_before = 0;
+    step_thread = nullptr;
+    step_gdbarch = nullptr;
+    step_closure.reset ();
+    step_original = 0;
+    step_copy = 0;
+    step_saved_copy.clear ();
+  }
+
+  /* True if preparing a displaced step ever failed.  If so, we won't
+     try displaced stepping for this inferior again.  */
+  int failed_before;
+
+  /* If this is not nullptr, this is the thread carrying out a
+     displaced single-step in process PID.  This thread's state will
+     require fixing up once it has completed its step.  */
+  thread_info *step_thread;
+
+  /* The architecture the thread had when we stepped it.  */
+  gdbarch *step_gdbarch;
+
+  /* The closure provided gdbarch_displaced_step_copy_insn, to be used
+     for post-step cleanup.  */
+  displaced_step_copy_insn_closure_up step_closure;
+
+  /* The address of the original instruction, and the copy we
+     made.  */
+  CORE_ADDR step_original, step_copy;
+
+  /* Saved contents of copy area.  */
+  gdb::byte_vector step_saved_copy;
+};
+
 #endif /* DISPLACED_STEPPING_H */
diff --git a/gdb/gdbarch.h b/gdb/gdbarch.h
index 4f52e8cd874f..2b6876a1916c 100644
--- a/gdb/gdbarch.h
+++ b/gdb/gdbarch.h
@@ -31,6 +31,7 @@
 #include "gdb_obstack.h"
 #include "infrun.h"
 #include "osabi.h"
+#include "displaced-stepping.h"
 
 struct floatformat;
 struct ui_file;
diff --git a/gdb/gdbarch.sh b/gdb/gdbarch.sh
index f67ec9dcd2d6..76ce2ff5efb4 100755
--- a/gdb/gdbarch.sh
+++ b/gdb/gdbarch.sh
@@ -1270,6 +1270,7 @@ cat <<EOF
 #include "gdb_obstack.h"
 #include "infrun.h"
 #include "osabi.h"
+#include "displaced-stepping.h"
 
 struct floatformat;
 struct ui_file;
diff --git a/gdb/inferior.h b/gdb/inferior.h
index d016161fb051..c5257ac1e640 100644
--- a/gdb/inferior.h
+++ b/gdb/inferior.h
@@ -60,6 +60,7 @@ struct thread_info;
 #include "gdbthread.h"
 
 #include "process-stratum-target.h"
+#include "displaced-stepping.h"
 
 struct infcall_suspend_state;
 struct infcall_control_state;
diff --git a/gdb/infrun.c b/gdb/infrun.c
index 29bbc209d188..9ac75eff4d30 100644
--- a/gdb/infrun.c
+++ b/gdb/infrun.c
@@ -153,14 +153,6 @@ static ptid_t previous_inferior_ptid;
 
 static bool detach_fork = true;
 
-bool debug_displaced = false;
-static void
-show_debug_displaced (struct ui_file *file, int from_tty,
-		      struct cmd_list_element *c, const char *value)
-{
-  fprintf_filtered (file, _("Displace stepping debugging is %s.\n"), value);
-}
-
 unsigned int debug_infrun = 0;
 static void
 show_debug_infrun (struct ui_file *file, int from_tty,
@@ -1460,11 +1452,6 @@ step_over_info_valid_p (void)
    displaced step operation on it.  See displaced_step_prepare and
    displaced_step_finish for details.  */
 
-/* Default destructor for displaced_step_copy_insn_closure.  */
-
-displaced_step_copy_insn_closure::~displaced_step_copy_insn_closure ()
-  = default;
-
 /* Returns true if any inferior has a thread doing a displaced
    step.  */
 
@@ -9297,15 +9284,6 @@ When non-zero, inferior specific debugging is enabled."),
 			     show_debug_infrun,
 			     &setdebuglist, &showdebuglist);
 
-  add_setshow_boolean_cmd ("displaced", class_maintenance,
-			   &debug_displaced, _("\
-Set displaced stepping debugging."), _("\
-Show displaced stepping debugging."), _("\
-When non-zero, displaced stepping specific debugging is enabled."),
-			    NULL,
-			    show_debug_displaced,
-			    &setdebuglist, &showdebuglist);
-
   add_setshow_boolean_cmd ("non-stop", no_class,
 			   &non_stop_1, _("\
 Set whether gdb controls the inferior in non-stop mode."), _("\
diff --git a/gdb/infrun.h b/gdb/infrun.h
index a276ddf02502..c83cb3330833 100644
--- a/gdb/infrun.h
+++ b/gdb/infrun.h
@@ -41,19 +41,6 @@ extern unsigned int debug_infrun;
     } \
   while (0)
 
-/* True if we are debugging displaced stepping.  */
-extern bool debug_displaced;
-
-/* Print a "displaced" debug statement.  */
-
-#define displaced_debug_printf(fmt, ...) \
-  do \
-    { \
-      if (debug_displaced) \
-	debug_prefixed_printf ("displaced", __func__, fmt, ##__VA_ARGS__); \
-    } \
-  while (0)
-
 /* Nonzero if we want to give control to the user when we're notified
    of shared library events by the dynamic linker.  */
 extern int stop_on_solib_events;
@@ -280,69 +267,4 @@ extern void all_uis_check_sync_execution_done (void);
    started or re-started).  */
 extern void all_uis_on_sync_execution_starting (void);
 
-/* Base class for displaced stepping closures (the arch-specific data).  */
-
-struct displaced_step_copy_insn_closure
-{
-  virtual ~displaced_step_copy_insn_closure () = 0;
-};
-
-using displaced_step_copy_insn_closure_up
-  = std::unique_ptr<displaced_step_copy_insn_closure>;
-
-/* A simple displaced step closure that contains only a byte buffer.  */
-
-struct buf_displaced_step_copy_insn_closure : displaced_step_copy_insn_closure
-{
-  buf_displaced_step_copy_insn_closure (int buf_size)
-  : buf (buf_size)
-  {}
-
-  gdb::byte_vector buf;
-};
-
-/* Per-inferior displaced stepping state.  */
-struct displaced_step_inferior_state
-{
-  displaced_step_inferior_state ()
-  {
-    reset ();
-  }
-
-  /* Put this object back in its original state.  */
-  void reset ()
-  {
-    failed_before = 0;
-    step_thread = nullptr;
-    step_gdbarch = nullptr;
-    step_closure.reset ();
-    step_original = 0;
-    step_copy = 0;
-    step_saved_copy.clear ();
-  }
-
-  /* True if preparing a displaced step ever failed.  If so, we won't
-     try displaced stepping for this inferior again.  */
-  int failed_before;
-
-  /* If this is not nullptr, this is the thread carrying out a
-     displaced single-step in process PID.  This thread's state will
-     require fixing up once it has completed its step.  */
-  thread_info *step_thread;
-
-  /* The architecture the thread had when we stepped it.  */
-  gdbarch *step_gdbarch;
-
-  /* The closure provided gdbarch_displaced_step_copy_insn, to be used
-     for post-step cleanup.  */
-  displaced_step_copy_insn_closure_up step_closure;
-
-  /* The address of the original instruction, and the copy we
-     made.  */
-  CORE_ADDR step_original, step_copy;
-
-  /* Saved contents of copy area.  */
-  gdb::byte_vector step_saved_copy;
-};
-
 #endif /* INFRUN_H */
-- 
2.29.2


^ permalink raw reply	[flat|nested] 40+ messages in thread

* [PATCH v2 11/14] gdb: move displaced stepping logic to gdbarch, allow starting concurrent displaced steps
  2020-12-02 15:47 [PATCH v2 00/14] Concurrent displaced stepping Simon Marchi
                   ` (9 preceding siblings ...)
  2020-12-02 15:48 ` [PATCH v2 10/14] gdb: move displaced stepping types to displaced-stepping.{h, c} Simon Marchi
@ 2020-12-02 15:48 ` Simon Marchi
  2020-12-04  1:50   ` Pedro Alves
  2020-12-02 15:48 ` [PATCH v2 12/14] gdb: change linux gdbarch data from post to pre-init Simon Marchi
                   ` (3 subsequent siblings)
  14 siblings, 1 reply; 40+ messages in thread
From: Simon Marchi @ 2020-12-02 15:48 UTC (permalink / raw)
  To: gdb-patches; +Cc: Simon Marchi

From: Simon Marchi <simon.marchi@efficios.com>

New in v2:

 - error handling in displaced_step_buffer::prepare, don't swallow
   exceptions
 - use local displaced_step_copy_insn_closure_up variables in
   displaced_step_buffer::prepare and displaced_step_buffer::finish to
   ensure they are released in case of error
 - use SCOPE_EXIT in start_step_over to ensure leftover threads are
   enqueued back in the global step over chain.
 - explicit on displaced_step_buffer's constructor
 - add / fix function comments
 - add assert, use for loop in thread_step_over_chain_length
 - keep number of active displaced steps per inferior, implement
   displaced_step_in_progress* functions more efficiently
 - start_step_over: append leftover threads by fiddling with pointers
   instead of appending threads one by one (new function
   global_thread_step_over_chain_enqueue_chain).
 - Allow gdbarch's displaced stepping implementation to set an
   unavailable flag to indicate that it's not worth asking to prepare
   another displaced step
 - reset thread displaced stepping state in infrun_inferior_execd
   (bug caught by the new gdb.threads/step-over-exec.exp test)

Today, GDB only allows a single displaced stepping operation to happen
per inferior at a time.  There is a single displaced stepping buffer per
inferior, whose address is fixed (obtained with
gdbarch_displaced_step_location), managed by infrun.c.

In the case of the AMD ROCm target [1] (in the context of which this
work has been done), it is typical to have thousands of threads (or
waves, in SMT terminology) executing the same code, hitting the same
breakpoint (possibly conditional) and needing to to displaced step it at
the same time.  The limitation of only one displaced step executing at a
any given time becomes a real bottleneck.

To fix this bottleneck, we want to make it possible for threads of a
same inferior to execute multiple displaced steps in parallel.  This
patch builds the foundation for that.

In essence, this patch moves the task of preparing a displaced step and
cleaning up after to gdbarch functions.  This allows using different
schemes for allocating and managing displaced stepping buffers for
different platforms.  The gdbarch decides how to assign a buffer to a
thread that needs to execute a displaced step.

On the ROCm target, we are able to allocate one displaced stepping
buffer per thread, so a thread will never have to wait to execute a
displaced step.

On Linux, the entry point of the executable if used as the displaced
stepping buffer, since we assume that this code won't get used after
startup.  From what I saw (I checked with a binary generated against
glibc and musl), on AMD64 we have enough space there to fit two
displaced stepping buffers.  A subsequent patch makes AMD64/Linux use
two buffers.

In addition to having multiple displaced stepping buffers, there is also
the idea of sharing displaced stepping buffers between threads.  Two
threads doing displaced steps for the same PC could use the same buffer
at the same time.  Two threads stepping over the same instruction (same
opcode) at two different PCs may also be able to share a displaced
stepping buffer.  This is an idea for future patches, but the
architecture built by this patch is made to allow this.

Now, the implementation details.  The main part of this patch is moving
the responsibility of preparing and finishing a displaced step to the
gdbarch.  Before this patch, preparing a displaced step is driven by the
displaced_step_prepare_throw function.  It does some calls to the
gdbarch to do some low-level operations, but the high-level logic is
there.  The steps are roughly:

- Ask the gdbarch for the displaced step buffer location
- Save the existing bytes in the displaced step buffer
- Ask the gdbarch to copy the instruction into the displaced step buffer
- Set the pc of the thread to the beginning of the displaced step buffer

Similarly, the "fixup" phase, executed after the instruction was
successfully single-stepped, is driven by the infrun code (function
displaced_step_finish).  The steps are roughly:

- Restore the original bytes in the displaced stepping buffer
- Ask the gdbarch to fixup the instruction result (adjust the target's
  registers or memory to do as if the instruction had been executed in
  its original location)

The displaced_step_inferior_state::step_thread field indicates which
thread (if any) is currently using the displaced stepping buffer, so it
is used by displaced_step_prepare_throw to check if the displaced
stepping buffer is free to use or not.

This patch defers the whole task of preparing and cleaning up after a
displaced step to the gdbarch.  Two new main gdbarch methods are added,
with the following semantics:

  - gdbarch_displaced_step_prepare: Prepare for the given thread to
    execute a displaced step of the instruction located at its current PC.
    Upon return, everything should be ready for GDB to resume the thread
    (with either a single step or continue, as indicated by
    gdbarch_displaced_step_hw_singlestep) to make it displaced step the
    instruction.

  - gdbarch_displaced_step_finish: Called when the thread stopped after
    having started a displaced step.  Verify if the instruction was
    executed, if so apply any fixup required to compensate for the fact
    that the instruction was executed at a different place than its
    original pc.  Release any resources that were allocated for this
    displaced step.  Upon return, everything should be ready for GDB to
    resume the thread in its "normal" code path.

The displaced_step_prepare_throw function now pretty much just offloads
to gdbarch_displaced_step_prepare and the displaced_step_finish function
offloads to gdbarch_displaced_step_finish.

The gdbarch_displaced_step_location method is now unnecessary, so is
removed.  Indeed, the core of GDB doesn't know how many displaced step
buffers there are nor where they are.

To keep the existing behavior for existing architectures, the logic that
was previously implemented in infrun.c for preparing and finishing a
displaced step is moved to displaced-stepping.c, to the
displaced_step_buffer class.  Architectures are modified to implement
the new gdbarch methods using this class.  The behavior is not expected
to change.

The other important change (which arises from the above) is that the
core of GDB no longer prevents concurrent displaced steps.  Before this
patch, start_step_over walks the global step over chain and tries to
initiate a step over (whether it is in-line or displaced).  It follows
these rules:

  - if an in-line step is in progress (in any inferior), don't start any
    other step over
  - if a displaced step is in progress for an inferior, don't start
    another displaced step for that inferior

After starting a displaced step for a given inferior, it won't start
another displaced step for that inferior.

In the new code, start_step_over simply tries to initiate step overs for
all the threads in the list.  But because threads may be added back to
the global list as it iterates the global list, trying to initiate step
overs, start_step_over now starts by stealing the global queue into a
local queue and iterates on the local queue.  In the typical case, each
thread will either:

  - have initiated a displaced step and be resumed
  - have been added back by the global step over queue by
    displaced_step_prepare_throw, because the gdbarch will have returned
    that there aren't enough resources (i.e. buffers) to initiate a
    displaced step for that thread

Lastly, if start_step_over initiates an in-line step, it stops
iterating, and moves back whatever remaining threads it had in its local
step over queue to the global step over queue.

Two other gdbarch methods are added, to handle some slightly annoying
corner cases.  They feel awkwardly specific to these cases, but I don't
see any way around them:

  - gdbarch_displaced_step_copy_insn_closure_by_addr: in
    arm_pc_is_thumb, arm-tdep.c wants to get the closure for a given
    buffer address.

  - gdbarch_displaced_step_restore_all_in_ptid: when a process forks
    (at least on Linux), the address space is copied.  If some displaced
    step buffers were in use at the time of the fork, we need to restore
    the original bytes in the child's address space.

These two adjustments are also made in infrun.c:

  - prepare_for_detach: there may be multiple threads doing displaced
    steps when we detach, so wait until all of them are done

  - handle_inferior_event: when we handle a fork event for a given
    thread, it's possible that other threads are doing a displaced step at
    the same time.  Make sure to restore the displaced step buffer
    contents in the child for them.

[1] https://github.com/ROCm-Developer-Tools/ROCgdb

gdb/ChangeLog:

	* displaced-stepping.h (struct
	displaced_step_copy_insn_closure): Adjust comments.
	(struct displaced_step_inferior_state) <step_thread,
	step_gdbarch, step_closure, step_original, step_copy,
	step_saved_copy>: Remove fields.
	(struct displaced_step_thread_state): New.
	(struct displaced_step_buffer): New.
	* displaced-stepping.c (displaced_step_buffer::prepare): New.
	(write_memory_ptid): Move from infrun.c.
	(displaced_step_instruction_executed_successfully): New,
	factored out of displaced_step_finish.
	(displaced_step_buffer::finish): New.
	(displaced_step_buffer::copy_insn_closure_by_addr): New.
	(displaced_step_buffer::restore_in_ptid): New.
	* gdbarch.sh (displaced_step_location): Remove.
	(displaced_step_prepare, displaced_step_finish,
	displaced_step_copy_insn_closure_by_addr,
	displaced_step_restore_all_in_ptid): New.
	* gdbarch.c: Re-generate.
	* gdbarch.h: Re-generate.
	* gdbthread.h (class thread_info) <displaced_step_state>: New
	field.
	(thread_step_over_chain_remove): New declaration.
	(thread_step_over_chain_next): New declaration.
	(thread_step_over_chain_length): New declaration.
	* thread.c (thread_step_over_chain_remove): Make non-static.
	(thread_step_over_chain_next): New.
	(global_thread_step_over_chain_next): Use
	thread_step_over_chain_next.
	(thread_step_over_chain_length): New.
	(global_thread_step_over_chain_enqueue): Add debug print.
	(global_thread_step_over_chain_remove): Add debug print.
	* infrun.h (get_displaced_step_copy_insn_closure_by_addr):
	Remove.
	* infrun.c (get_displaced_stepping_state): New.
	(displaced_step_in_progress_any_inferior): Remove.
	(displaced_step_in_progress_thread): Adjust.
	(displaced_step_in_progress): Adjust.
	(displaced_step_in_progress_any_thread): New.
	(get_displaced_step_copy_insn_closure_by_addr): Remove.
	(gdbarch_supports_displaced_stepping): Use
	gdbarch_displaced_step_prepare_p.
	(displaced_step_reset): Change parameter from inferior to
	thread.
	(displaced_step_prepare_throw): Implement using
	gdbarch_displaced_step_prepare.
	(write_memory_ptid): Move to displaced-step.c.
	(displaced_step_restore): Remove.
	(displaced_step_finish): Implement using
	gdbarch_displaced_step_finish.
	(start_step_over): Allow starting more than one displaced step.
	(prepare_for_detach): Handle possibly multiple threads doing
	displaced steps.
	(handle_inferior_event): Handle possibility that fork event
	happens while another thread displaced steps.
	* linux-tdep.h (linux_displaced_step_prepare): New.
	(linux_displaced_step_finish): New.
	(linux_displaced_step_copy_insn_closure_by_addr): New.
	(linux_displaced_step_restore_all_in_ptid): New.
	(linux_init_abi): Add supports_displaced_step parameter.
	* linux-tdep.c (struct linux_info) <disp_step_buf>: New field.
	(linux_displaced_step_prepare): New.
	(linux_displaced_step_finish): New.
	(linux_displaced_step_copy_insn_closure_by_addr): New.
	(linux_displaced_step_restore_all_in_ptid): New.
	(linux_init_abi): Add supports_displaced_step parameter,
	register displaced step methods if true.
	(_initialize_linux_tdep): Register inferior_execd observer.
	* amd64-linux-tdep.c (amd64_linux_init_abi_common): Add
	supports_displaced_step parameter, adjust call to
	linux_init_abi.  Remove call to
	set_gdbarch_displaced_step_location.
	(amd64_linux_init_abi): Adjust call to
	amd64_linux_init_abi_common.
	(amd64_x32_linux_init_abi): Likewise.
	* aarch64-linux-tdep.c (aarch64_linux_init_abi): Adjust call to
	linux_init_abi.  Remove call to
	set_gdbarch_displaced_step_location.
	* arm-linux-tdep.c (arm_linux_init_abi): Likewise.
	* i386-linux-tdep.c (i386_linux_init_abi): Likewise.
	* alpha-linux-tdep.c (alpha_linux_init_abi): Adjust call to
	linux_init_abi.
	* arc-linux-tdep.c (arc_linux_init_osabi): Likewise.
	* bfin-linux-tdep.c (bfin_linux_init_abi): Likewise.
	* cris-linux-tdep.c (cris_linux_init_abi): Likewise.
	* csky-linux-tdep.c (csky_linux_init_abi): Likewise.
	* frv-linux-tdep.c (frv_linux_init_abi): Likewise.
	* hppa-linux-tdep.c (hppa_linux_init_abi): Likewise.
	* ia64-linux-tdep.c (ia64_linux_init_abi): Likewise.
	* m32r-linux-tdep.c (m32r_linux_init_abi): Likewise.
	* m68k-linux-tdep.c (m68k_linux_init_abi): Likewise.
	* microblaze-linux-tdep.c (microblaze_linux_init_abi): Likewise.
	* mips-linux-tdep.c (mips_linux_init_abi): Likewise.
	* mn10300-linux-tdep.c (am33_linux_init_osabi): Likewise.
	* nios2-linux-tdep.c (nios2_linux_init_abi): Likewise.
	* or1k-linux-tdep.c (or1k_linux_init_abi): Likewise.
	* riscv-linux-tdep.c (riscv_linux_init_abi): Likewise.
	* s390-linux-tdep.c (s390_linux_init_abi_any): Likewise.
	* sh-linux-tdep.c (sh_linux_init_abi): Likewise.
	* sparc-linux-tdep.c (sparc32_linux_init_abi): Likewise.
	* sparc64-linux-tdep.c (sparc64_linux_init_abi): Likewise.
	* tic6x-linux-tdep.c (tic6x_uclinux_init_abi): Likewise.
	* tilegx-linux-tdep.c (tilegx_linux_init_abi): Likewise.
	* xtensa-linux-tdep.c (xtensa_linux_init_abi): Likewise.
	* ppc-linux-tdep.c (ppc_linux_init_abi): Adjust call to
	linux_init_abi.  Remove call to
	set_gdbarch_displaced_step_location.
	* arm-tdep.c (arm_pc_is_thumb): Call
	gdbarch_displaced_step_copy_insn_closure_by_addr instead of
	get_displaced_step_copy_insn_closure_by_addr.
	* rs6000-aix-tdep.c (rs6000_aix_init_osabi): Adjust calls to
	clear gdbarch methods.
	* rs6000-tdep.c (struct ppc_inferior_data): New structure.
	(get_ppc_per_inferior): New function.
	(ppc_displaced_step_prepare): New function.
	(ppc_displaced_step_finish): New function.
	(ppc_displaced_step_restore_all_in_ptid): New function.
	(rs6000_gdbarch_init): Register new gdbarch methods.
	* s390-tdep.c (s390_gdbarch_init): Don't call
	set_gdbarch_displaced_step_location, set new gdbarch methods.

gdb/testsuite/ChangeLog:

	* gdb.arch/amd64-disp-step-avx.exp: Adjust pattern.
	* gdb.threads/forking-threads-plus-breakpoint.exp: Likewise.
	* gdb.threads/non-stop-fair-events.exp: Likewise.

Change-Id: I387cd235a442d0620ec43608fd3dc0097fcbf8c8
---
 gdb/aarch64-linux-tdep.c                      |   3 +-
 gdb/alpha-linux-tdep.c                        |   2 +-
 gdb/amd64-linux-tdep.c                        |  11 +-
 gdb/arc-linux-tdep.c                          |   2 +-
 gdb/arm-linux-tdep.c                          |   3 +-
 gdb/arm-tdep.c                                |   9 +-
 gdb/bfin-linux-tdep.c                         |   2 +-
 gdb/cris-linux-tdep.c                         |   2 +-
 gdb/csky-linux-tdep.c                         |   2 +-
 gdb/displaced-stepping.c                      | 195 +++++++++-
 gdb/displaced-stepping.h                      | 107 ++++--
 gdb/frv-linux-tdep.c                          |   2 +-
 gdb/gdbarch.c                                 | 113 +++++-
 gdb/gdbarch.h                                 |  38 +-
 gdb/gdbarch.sh                                |  21 +-
 gdb/gdbthread.h                               |  29 ++
 gdb/hppa-linux-tdep.c                         |   2 +-
 gdb/i386-linux-tdep.c                         |   4 +-
 gdb/ia64-linux-tdep.c                         |   2 +-
 gdb/infrun.c                                  | 334 ++++++++----------
 gdb/infrun.h                                  |   3 -
 gdb/linux-tdep.c                              |  76 +++-
 gdb/linux-tdep.h                              |  27 +-
 gdb/m32r-linux-tdep.c                         |   2 +-
 gdb/m68k-linux-tdep.c                         |   2 +-
 gdb/microblaze-linux-tdep.c                   |   2 +-
 gdb/mips-linux-tdep.c                         |   2 +-
 gdb/mn10300-linux-tdep.c                      |   2 +-
 gdb/nios2-linux-tdep.c                        |   2 +-
 gdb/or1k-linux-tdep.c                         |   2 +-
 gdb/ppc-linux-tdep.c                          |   5 +-
 gdb/riscv-linux-tdep.c                        |   2 +-
 gdb/rs6000-aix-tdep.c                         |   6 +-
 gdb/rs6000-tdep.c                             |  78 +++-
 gdb/s390-linux-tdep.c                         |   2 +-
 gdb/s390-tdep.c                               |   5 +-
 gdb/sh-linux-tdep.c                           |   2 +-
 gdb/sparc-linux-tdep.c                        |   2 +-
 gdb/sparc64-linux-tdep.c                      |   2 +-
 .../gdb.arch/amd64-disp-step-avx.exp          |   2 +-
 .../forking-threads-plus-breakpoint.exp       |   2 +-
 .../gdb.threads/non-stop-fair-events.exp      |   2 +-
 gdb/thread.c                                  |  68 +++-
 gdb/tic6x-linux-tdep.c                        |   2 +-
 gdb/tilegx-linux-tdep.c                       |   2 +-
 gdb/xtensa-linux-tdep.c                       |   2 +-
 46 files changed, 880 insertions(+), 307 deletions(-)

diff --git a/gdb/aarch64-linux-tdep.c b/gdb/aarch64-linux-tdep.c
index c9898bdafda6..4fe7babe59cf 100644
--- a/gdb/aarch64-linux-tdep.c
+++ b/gdb/aarch64-linux-tdep.c
@@ -1445,7 +1445,7 @@ aarch64_linux_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
 
   tdep->lowest_pc = 0x8000;
 
-  linux_init_abi (info, gdbarch);
+  linux_init_abi (info, gdbarch, true);
 
   set_solib_svr4_fetch_link_map_offsets (gdbarch,
 					 svr4_lp64_fetch_link_map_offsets);
@@ -1658,7 +1658,6 @@ aarch64_linux_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
   set_gdbarch_displaced_step_copy_insn (gdbarch,
 					aarch64_displaced_step_copy_insn);
   set_gdbarch_displaced_step_fixup (gdbarch, aarch64_displaced_step_fixup);
-  set_gdbarch_displaced_step_location (gdbarch, linux_displaced_step_location);
   set_gdbarch_displaced_step_hw_singlestep (gdbarch,
 					    aarch64_displaced_step_hw_singlestep);
 
diff --git a/gdb/alpha-linux-tdep.c b/gdb/alpha-linux-tdep.c
index 70ac5a87766d..a6d6b15e9fd6 100644
--- a/gdb/alpha-linux-tdep.c
+++ b/gdb/alpha-linux-tdep.c
@@ -356,7 +356,7 @@ alpha_linux_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
 {
   struct gdbarch_tdep *tdep;
 
-  linux_init_abi (info, gdbarch);
+  linux_init_abi (info, gdbarch, false);
 
   /* Hook into the DWARF CFI frame unwinder.  */
   alpha_dwarf2_init_abi (info, gdbarch);
diff --git a/gdb/amd64-linux-tdep.c b/gdb/amd64-linux-tdep.c
index d484b1a1c598..a81bb9039df5 100644
--- a/gdb/amd64-linux-tdep.c
+++ b/gdb/amd64-linux-tdep.c
@@ -1795,11 +1795,12 @@ amd64_dtrace_parse_probe_argument (struct gdbarch *gdbarch,
 }
 
 static void
-amd64_linux_init_abi_common(struct gdbarch_info info, struct gdbarch *gdbarch)
+amd64_linux_init_abi_common(struct gdbarch_info info, struct gdbarch *gdbarch,
+			    bool supports_displaced_step)
 {
   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
 
-  linux_init_abi (info, gdbarch);
+  linux_init_abi (info, gdbarch, supports_displaced_step);
 
   tdep->sigtramp_p = amd64_linux_sigtramp_p;
   tdep->sigcontext_addr = amd64_linux_sigcontext_addr;
@@ -1839,8 +1840,6 @@ amd64_linux_init_abi_common(struct gdbarch_info info, struct gdbarch *gdbarch)
   set_gdbarch_displaced_step_copy_insn (gdbarch,
 					amd64_displaced_step_copy_insn);
   set_gdbarch_displaced_step_fixup (gdbarch, amd64_displaced_step_fixup);
-  set_gdbarch_displaced_step_location (gdbarch,
-				       linux_displaced_step_location);
 
   set_gdbarch_process_record (gdbarch, i386_process_record);
   set_gdbarch_process_record_signal (gdbarch, amd64_linux_record_signal);
@@ -1881,7 +1880,7 @@ amd64_linux_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
   if (!valid_p)
     return;
 
-  amd64_linux_init_abi_common (info, gdbarch);
+  amd64_linux_init_abi_common (info, gdbarch, true);
 
   /* Initialize the amd64_linux_record_tdep.  */
   /* These values are the size of the type that will be used in a system
@@ -2096,7 +2095,7 @@ amd64_x32_linux_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
   if (!valid_p)
     return;
 
-  amd64_linux_init_abi_common (info, gdbarch);
+  amd64_linux_init_abi_common (info, gdbarch, false);
 
   /* Initialize the amd64_x32_linux_record_tdep.  */
   /* These values are the size of the type that will be used in a system
diff --git a/gdb/arc-linux-tdep.c b/gdb/arc-linux-tdep.c
index ef459bf596f0..b919882177e6 100644
--- a/gdb/arc-linux-tdep.c
+++ b/gdb/arc-linux-tdep.c
@@ -439,7 +439,7 @@ arc_linux_init_osabi (struct gdbarch_info info, struct gdbarch *gdbarch)
    */
   tdep->jb_pc = 15;
 
-  linux_init_abi (info, gdbarch);
+  linux_init_abi (info, gdbarch, false);
 
   /* Set up target dependent GDB architecture entries.  */
   set_gdbarch_cannot_fetch_register (gdbarch, arc_linux_cannot_fetch_register);
diff --git a/gdb/arm-linux-tdep.c b/gdb/arm-linux-tdep.c
index 9caae06adfee..d164cff3dff7 100644
--- a/gdb/arm-linux-tdep.c
+++ b/gdb/arm-linux-tdep.c
@@ -1721,7 +1721,7 @@ arm_linux_init_abi (struct gdbarch_info info,
 								    NULL };
   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
 
-  linux_init_abi (info, gdbarch);
+  linux_init_abi (info, gdbarch, true);
 
   tdep->lowest_pc = 0x8000;
   if (info.byte_order_for_code == BFD_ENDIAN_BIG)
@@ -1807,7 +1807,6 @@ arm_linux_init_abi (struct gdbarch_info info,
   set_gdbarch_displaced_step_copy_insn (gdbarch,
 					arm_linux_displaced_step_copy_insn);
   set_gdbarch_displaced_step_fixup (gdbarch, arm_displaced_step_fixup);
-  set_gdbarch_displaced_step_location (gdbarch, linux_displaced_step_location);
 
   /* Reversible debugging, process record.  */
   set_gdbarch_process_record (gdbarch, arm_process_record);
diff --git a/gdb/arm-tdep.c b/gdb/arm-tdep.c
index bc086e18a571..7d96884695c4 100644
--- a/gdb/arm-tdep.c
+++ b/gdb/arm-tdep.c
@@ -433,9 +433,12 @@ arm_pc_is_thumb (struct gdbarch *gdbarch, CORE_ADDR memaddr)
 {
   struct bound_minimal_symbol sym;
   char type;
-  arm_displaced_step_copy_insn_closure *dsc
-    = ((arm_displaced_step_copy_insn_closure * )
-	get_displaced_step_copy_insn_closure_by_addr (memaddr));
+  arm_displaced_step_copy_insn_closure *dsc = nullptr;
+
+  if (gdbarch_displaced_step_copy_insn_closure_by_addr_p (gdbarch))
+    dsc = ((arm_displaced_step_copy_insn_closure * )
+	   gdbarch_displaced_step_copy_insn_closure_by_addr
+	     (gdbarch, current_inferior (), memaddr));
 
   /* If checking the mode of displaced instruction in copy area, the mode
      should be determined by instruction on the original address.  */
diff --git a/gdb/bfin-linux-tdep.c b/gdb/bfin-linux-tdep.c
index 16a0a97eaf40..fc2f1d9ac656 100644
--- a/gdb/bfin-linux-tdep.c
+++ b/gdb/bfin-linux-tdep.c
@@ -150,7 +150,7 @@ bfin_linux_get_syscall_number (struct gdbarch *gdbarch,
 static void
 bfin_linux_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
 {
-  linux_init_abi (info, gdbarch);
+  linux_init_abi (info, gdbarch, false);
 
   /* Set the sigtramp frame sniffer.  */
   tramp_frame_prepend_unwinder (gdbarch, &bfin_linux_sigframe);
diff --git a/gdb/cris-linux-tdep.c b/gdb/cris-linux-tdep.c
index 535dc4a1fceb..85cbf4cc0931 100644
--- a/gdb/cris-linux-tdep.c
+++ b/gdb/cris-linux-tdep.c
@@ -35,7 +35,7 @@ cris_linux_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
 {
   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
 
-  linux_init_abi (info, gdbarch);
+  linux_init_abi (info, gdbarch, false);
 
   if (tdep->cris_version == 32)
     /* Threaded debugging is only supported on CRISv32 for now.  */
diff --git a/gdb/csky-linux-tdep.c b/gdb/csky-linux-tdep.c
index fb4310204463..184fa5ffb239 100644
--- a/gdb/csky-linux-tdep.c
+++ b/gdb/csky-linux-tdep.c
@@ -233,7 +233,7 @@ csky_linux_rt_sigreturn_tramp_frame = {
 static void
 csky_linux_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
 {
-  linux_init_abi (info, gdbarch);
+  linux_init_abi (info, gdbarch, false);
 
   /* Shared library handling.  */
   set_gdbarch_skip_trampoline_code (gdbarch, find_solib_trampoline_target);
diff --git a/gdb/displaced-stepping.c b/gdb/displaced-stepping.c
index 5ae280fac398..984d21eedb93 100644
--- a/gdb/displaced-stepping.c
+++ b/gdb/displaced-stepping.c
@@ -19,9 +19,15 @@
 
 #include "defs.h"
 #include "displaced-stepping.h"
-#include "cli/cli-cmds.h"
 
+#include "cli/cli-cmds.h"
 #include "command.h"
+#include "gdbarch.h"
+#include "gdbcore.h"
+#include "gdbthread.h"
+#include "inferior.h"
+#include "regcache.h"
+#include "target/target.h"
 
 /* Default destructor for displaced_step_copy_insn_closure.  */
 
@@ -37,6 +43,193 @@ show_debug_displaced (struct ui_file *file, int from_tty,
   fprintf_filtered (file, _("Displace stepping debugging is %s.\n"), value);
 }
 
+displaced_step_prepare_status
+displaced_step_buffer::prepare (thread_info *thread, CORE_ADDR &displaced_pc)
+{
+  gdb_assert (!thread->displaced_step_state.in_progress ());
+
+  /* Is a thread currently using the buffer?  */
+  if (m_current_thread != nullptr)
+    {
+      /* If so, it better not be this thread.  */
+      gdb_assert (thread != m_current_thread);
+      return DISPLACED_STEP_PREPARE_STATUS_UNAVAILABLE;
+    }
+
+  regcache *regcache = get_thread_regcache (thread);
+  const address_space *aspace = regcache->aspace ();
+  gdbarch *arch = regcache->arch ();
+  ULONGEST len = gdbarch_max_insn_length (arch);
+
+  if (breakpoint_in_range_p (aspace, m_addr, len))
+    {
+      /* There's a breakpoint set in the scratch pad location range
+	 (which is usually around the entry point).  We'd either
+	 install it before resuming, which would overwrite/corrupt the
+	 scratch pad, or if it was already inserted, this displaced
+	 step would overwrite it.  The latter is OK in the sense that
+	 we already assume that no thread is going to execute the code
+	 in the scratch pad range (after initial startup) anyway, but
+	 the former is unacceptable.  Simply punt and fallback to
+	 stepping over this breakpoint in-line.  */
+      displaced_debug_printf ("breakpoint set in scratch pad.  "
+			      "Stepping over breakpoint in-line instead.");
+
+      return DISPLACED_STEP_PREPARE_STATUS_CANT;
+    }
+
+  m_original_pc = regcache_read_pc (regcache);
+  displaced_pc = m_addr;
+
+  /* Save the original contents of the displaced stepping buffer.  */
+  m_saved_copy.resize (len);
+
+  int status = target_read_memory (m_addr, m_saved_copy.data (), len);
+  if (status != 0)
+    throw_error (MEMORY_ERROR,
+		 _("Error accessing memory address %s (%s) for "
+		   "displaced-stepping scratch space."),
+		 paddress (arch, m_addr), safe_strerror (status));
+
+  displaced_debug_printf ("saved %s: %s",
+			  paddress (arch, m_addr),
+			  displaced_step_dump_bytes
+			    (m_saved_copy.data (), len).c_str ());
+
+  /* Save this in a local variable first, so it's released if code below
+     throws.  */
+  displaced_step_copy_insn_closure_up copy_insn_closure
+    = gdbarch_displaced_step_copy_insn (arch, m_original_pc, m_addr, regcache);
+
+  if (copy_insn_closure == nullptr)
+    {
+      /* The architecture doesn't know how or want to displaced step
+        this instruction or instruction sequence.  Fallback to
+        stepping over the breakpoint in-line.  */
+      return DISPLACED_STEP_PREPARE_STATUS_CANT;
+    }
+
+  /* Resume execution at the copy.  */
+  regcache_write_pc (regcache, m_addr);
+
+  /* This marks the buffer as being in use.  */
+  m_current_thread = thread;
+
+  /* Save this, now that we know everything went fine.  */
+  m_copy_insn_closure = std::move (copy_insn_closure);
+
+  /* Tell GDB not to try preparing a displaced step again for this inferior.  */
+  thread->inf->displaced_step_state.unavailable = true;
+
+  return DISPLACED_STEP_PREPARE_STATUS_OK;
+}
+
+static void
+write_memory_ptid (ptid_t ptid, CORE_ADDR memaddr,
+		   const gdb_byte *myaddr, int len)
+{
+  scoped_restore save_inferior_ptid = make_scoped_restore (&inferior_ptid);
+
+  inferior_ptid = ptid;
+  write_memory (memaddr, myaddr, len);
+}
+
+static bool
+displaced_step_instruction_executed_successfully (gdbarch *arch,
+						  gdb_signal signal)
+{
+  if (signal != GDB_SIGNAL_TRAP)
+    return false;
+
+  if (target_stopped_by_watchpoint ())
+    {
+      if (gdbarch_have_nonsteppable_watchpoint (arch)
+	  || target_have_steppable_watchpoint ())
+	return false;
+    }
+
+  return true;
+}
+
+displaced_step_finish_status
+displaced_step_buffer::finish (gdbarch *arch, thread_info *thread,
+			       gdb_signal sig)
+{
+  gdb_assert (thread->displaced_step_state.in_progress ());
+  gdb_assert (thread == m_current_thread);
+
+  /* Move this to a local variable so it's released in case something goes
+     wrong.  */
+  displaced_step_copy_insn_closure_up copy_insn_closure
+    = std::move (m_copy_insn_closure);
+  gdb_assert (copy_insn_closure != nullptr);
+
+  /* Reset m_current_thread immediately to mark the buffer as available, in case
+     something goes wrong below.  */
+  m_current_thread = nullptr;
+
+  /* Now that a buffer gets freed, tell GDB it can ask us to prepare a displaced
+     step again for this inferior.  Do that here in case something goes wrong
+     below.  */
+  thread->inf->displaced_step_state.unavailable = false;
+
+  ULONGEST len = gdbarch_max_insn_length (arch);
+
+  write_memory_ptid (thread->ptid, m_addr,
+		     m_saved_copy.data (), len);
+
+  displaced_debug_printf ("restored %s %s",
+			  target_pid_to_str (thread->ptid).c_str (),
+			  paddress (arch, m_addr));
+
+  regcache *rc = get_thread_regcache (thread);
+
+  bool instruction_executed_successfully
+    = displaced_step_instruction_executed_successfully (arch, sig);
+
+  if (instruction_executed_successfully)
+    {
+      gdbarch_displaced_step_fixup (arch, copy_insn_closure.get (), m_original_pc,
+				    m_addr, rc);
+      return DISPLACED_STEP_FINISH_STATUS_OK;
+    }
+  else
+    {
+      /* Since the instruction didn't complete, all we can do is relocate the
+	 PC.  */
+      CORE_ADDR pc = regcache_read_pc (rc);
+      pc = m_original_pc + (pc - m_addr);
+      regcache_write_pc (rc, pc);
+      return DISPLACED_STEP_FINISH_STATUS_NOT_EXECUTED;
+    }
+}
+
+const displaced_step_copy_insn_closure *
+displaced_step_buffer::copy_insn_closure_by_addr (CORE_ADDR addr)
+{
+  if (addr == m_addr)
+    return m_copy_insn_closure.get ();
+  else
+    return nullptr;
+}
+
+void
+displaced_step_buffer::restore_in_ptid (ptid_t ptid)
+{
+  if (m_current_thread != nullptr)
+    {
+      regcache *regcache = get_thread_regcache (m_current_thread);
+      gdbarch *arch = regcache->arch ();
+      ULONGEST len = gdbarch_max_insn_length (arch);
+
+      write_memory_ptid (ptid, m_addr, m_saved_copy.data (), len);
+
+      displaced_debug_printf ("restored in ptid %s %s",
+			      target_pid_to_str (ptid).c_str (),
+			      paddress (arch, m_addr));
+    }
+}
+
 void _initialize_displaced_stepping ();
 void
 _initialize_displaced_stepping ()
diff --git a/gdb/displaced-stepping.h b/gdb/displaced-stepping.h
index 82ea3389cf30..8ed32852fe5a 100644
--- a/gdb/displaced-stepping.h
+++ b/gdb/displaced-stepping.h
@@ -22,6 +22,7 @@
 
 #include "gdbsupport/byte-vector.h"
 
+struct gdbarch;
 struct thread_info;
 
 /* True if we are debugging displaced stepping.  */
@@ -62,7 +63,8 @@ enum displaced_step_finish_status
   DISPLACED_STEP_FINISH_STATUS_NOT_EXECUTED,
 };
 
-/* Base class for displaced stepping closures (the arch-specific data).  */
+/* Data returned by a gdbarch displaced_step_copy_insn method, to be passed to
+   the matching displaced_step_fixup method.  */
 
 struct displaced_step_copy_insn_closure
 {
@@ -80,6 +82,9 @@ struct buf_displaced_step_copy_insn_closure : displaced_step_copy_insn_closure
   : buf (buf_size)
   {}
 
+  /* The content of this buffer is up to the user of the class, but typically
+     original instruction bytes, used during fixup to determine what needs to
+     be fixed up.  */
   gdb::byte_vector buf;
 };
 
@@ -95,37 +100,95 @@ struct displaced_step_inferior_state
   /* Put this object back in its original state.  */
   void reset ()
   {
-    failed_before = 0;
-    step_thread = nullptr;
-    step_gdbarch = nullptr;
-    step_closure.reset ();
-    step_original = 0;
-    step_copy = 0;
-    step_saved_copy.clear ();
+    failed_before = false;
+    in_progress_count = 0;
+    unavailable = false;
   }
 
   /* True if preparing a displaced step ever failed.  If so, we won't
      try displaced stepping for this inferior again.  */
-  int failed_before;
+  bool failed_before;
 
-  /* If this is not nullptr, this is the thread carrying out a
-     displaced single-step in process PID.  This thread's state will
-     require fixing up once it has completed its step.  */
-  thread_info *step_thread;
+  /* Number of displaced steps in progress for this inferior.  */
+  unsigned int in_progress_count;
 
-  /* The architecture the thread had when we stepped it.  */
-  gdbarch *step_gdbarch;
+  /* If true, this tells GDB that it's not worth asking the gdbarch displaced
+     stepping implementation to prepare a displaced step, because it would
+     return UNAVAILABLE.  This is set and reset by the gdbarch in the
+     displaced_step_prepare and displaced_step_finish methods.  */
+  bool unavailable;
+};
 
-  /* The closure provided gdbarch_displaced_step_copy_insn, to be used
-     for post-step cleanup.  */
-  displaced_step_copy_insn_closure_up step_closure;
+/* Per-thread displaced stepping state.  */
 
-  /* The address of the original instruction, and the copy we
-     made.  */
-  CORE_ADDR step_original, step_copy;
+struct displaced_step_thread_state
+{
+  /* Return true if this thread is currently executing a displaced step.  */
+  bool in_progress () const
+  {
+    return m_original_gdbarch != nullptr;
+  }
+
+  /* Return the gdbarch of the thread prior to the step.  */
+  gdbarch *get_original_gdbarch () const
+  {
+    return m_original_gdbarch;
+  }
+
+  /* Mark this thread as currently executing a displaced step.
+
+     ORIGINAL_GDBARCH is the current gdbarch of the thread (before the step
+     is executed).  */
+  void set (gdbarch *original_gdbarch)
+  {
+    m_original_gdbarch = original_gdbarch;
+  }
+
+  /* Mark this thread as no longer executing a displaced step.  */
+  void reset ()
+  {
+    m_original_gdbarch = nullptr;
+  }
+
+private:
+  gdbarch *m_original_gdbarch = nullptr;
+};
+
+/* Manage access to a single displaced stepping buffer.  */
+
+struct displaced_step_buffer
+{
+  explicit displaced_step_buffer (CORE_ADDR buffer_addr)
+    : m_addr (buffer_addr)
+  {}
+
+  displaced_step_prepare_status prepare (thread_info *thread,
+					 CORE_ADDR &displaced_pc);
+
+  displaced_step_finish_status finish (gdbarch *arch, thread_info *thread,
+				       gdb_signal sig);
+
+  const displaced_step_copy_insn_closure *
+    copy_insn_closure_by_addr (CORE_ADDR addr);
+
+  void restore_in_ptid (ptid_t ptid);
+
+private:
+  /* Original PC of the instruction being displaced-stepped in this buffer.  */
+  CORE_ADDR m_original_pc = 0;
+
+  /* Address of the buffer.  */
+  const CORE_ADDR m_addr;
+
+  /* If set, the thread currently using the buffer.  */
+  thread_info *m_current_thread = nullptr;
 
   /* Saved contents of copy area.  */
-  gdb::byte_vector step_saved_copy;
+  gdb::byte_vector m_saved_copy;
+
+  /* The closure provided gdbarch_displaced_step_copy_insn, to be used
+     for post-step cleanup.  */
+  displaced_step_copy_insn_closure_up m_copy_insn_closure;
 };
 
 #endif /* DISPLACED_STEPPING_H */
diff --git a/gdb/frv-linux-tdep.c b/gdb/frv-linux-tdep.c
index 2dc18206975a..c5ae4212ab44 100644
--- a/gdb/frv-linux-tdep.c
+++ b/gdb/frv-linux-tdep.c
@@ -456,7 +456,7 @@ frv_linux_iterate_over_regset_sections (struct gdbarch *gdbarch,
 static void
 frv_linux_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
 {
-  linux_init_abi (info, gdbarch);
+  linux_init_abi (info, gdbarch, false);
 
   /* Set the sigtramp frame sniffer.  */
   frame_unwind_append_unwinder (gdbarch, &frv_linux_sigtramp_frame_unwind); 
diff --git a/gdb/gdbarch.c b/gdb/gdbarch.c
index 0ec626c6f8c4..bf8d8bc211f3 100644
--- a/gdb/gdbarch.c
+++ b/gdb/gdbarch.c
@@ -290,7 +290,10 @@ struct gdbarch
   gdbarch_displaced_step_copy_insn_ftype *displaced_step_copy_insn;
   gdbarch_displaced_step_hw_singlestep_ftype *displaced_step_hw_singlestep;
   gdbarch_displaced_step_fixup_ftype *displaced_step_fixup;
-  gdbarch_displaced_step_location_ftype *displaced_step_location;
+  gdbarch_displaced_step_prepare_ftype *displaced_step_prepare;
+  gdbarch_displaced_step_finish_ftype *displaced_step_finish;
+  gdbarch_displaced_step_copy_insn_closure_by_addr_ftype *displaced_step_copy_insn_closure_by_addr;
+  gdbarch_displaced_step_restore_all_in_ptid_ftype *displaced_step_restore_all_in_ptid;
   gdbarch_relocate_instruction_ftype *relocate_instruction;
   gdbarch_overlay_update_ftype *overlay_update;
   gdbarch_core_read_description_ftype *core_read_description;
@@ -445,7 +448,7 @@ gdbarch_alloc (const struct gdbarch_info *info,
   gdbarch->skip_permanent_breakpoint = default_skip_permanent_breakpoint;
   gdbarch->displaced_step_hw_singlestep = default_displaced_step_hw_singlestep;
   gdbarch->displaced_step_fixup = NULL;
-  gdbarch->displaced_step_location = NULL;
+  gdbarch->displaced_step_finish = NULL;
   gdbarch->relocate_instruction = NULL;
   gdbarch->has_shared_address_space = default_has_shared_address_space;
   gdbarch->fast_tracepoint_valid_at = default_fast_tracepoint_valid_at;
@@ -660,8 +663,10 @@ verify_gdbarch (struct gdbarch *gdbarch)
   /* Skip verify of displaced_step_copy_insn, has predicate.  */
   /* Skip verify of displaced_step_hw_singlestep, invalid_p == 0 */
   /* Skip verify of displaced_step_fixup, has predicate.  */
-  if ((! gdbarch->displaced_step_location) != (! gdbarch->displaced_step_copy_insn))
-    log.puts ("\n\tdisplaced_step_location");
+  /* Skip verify of displaced_step_prepare, has predicate.  */
+  if ((! gdbarch->displaced_step_finish) != (! gdbarch->displaced_step_prepare))
+    log.puts ("\n\tdisplaced_step_finish");
+  /* Skip verify of displaced_step_copy_insn_closure_by_addr, has predicate.  */
   /* Skip verify of relocate_instruction, has predicate.  */
   /* Skip verify of overlay_update, has predicate.  */
   /* Skip verify of core_read_description, has predicate.  */
@@ -924,6 +929,15 @@ gdbarch_dump (struct gdbarch *gdbarch, struct ui_file *file)
   fprintf_unfiltered (file,
                       "gdbarch_dump: displaced_step_copy_insn = <%s>\n",
                       host_address_to_string (gdbarch->displaced_step_copy_insn));
+  fprintf_unfiltered (file,
+                      "gdbarch_dump: gdbarch_displaced_step_copy_insn_closure_by_addr_p() = %d\n",
+                      gdbarch_displaced_step_copy_insn_closure_by_addr_p (gdbarch));
+  fprintf_unfiltered (file,
+                      "gdbarch_dump: displaced_step_copy_insn_closure_by_addr = <%s>\n",
+                      host_address_to_string (gdbarch->displaced_step_copy_insn_closure_by_addr));
+  fprintf_unfiltered (file,
+                      "gdbarch_dump: displaced_step_finish = <%s>\n",
+                      host_address_to_string (gdbarch->displaced_step_finish));
   fprintf_unfiltered (file,
                       "gdbarch_dump: gdbarch_displaced_step_fixup_p() = %d\n",
                       gdbarch_displaced_step_fixup_p (gdbarch));
@@ -934,8 +948,14 @@ gdbarch_dump (struct gdbarch *gdbarch, struct ui_file *file)
                       "gdbarch_dump: displaced_step_hw_singlestep = <%s>\n",
                       host_address_to_string (gdbarch->displaced_step_hw_singlestep));
   fprintf_unfiltered (file,
-                      "gdbarch_dump: displaced_step_location = <%s>\n",
-                      host_address_to_string (gdbarch->displaced_step_location));
+                      "gdbarch_dump: gdbarch_displaced_step_prepare_p() = %d\n",
+                      gdbarch_displaced_step_prepare_p (gdbarch));
+  fprintf_unfiltered (file,
+                      "gdbarch_dump: displaced_step_prepare = <%s>\n",
+                      host_address_to_string (gdbarch->displaced_step_prepare));
+  fprintf_unfiltered (file,
+                      "gdbarch_dump: displaced_step_restore_all_in_ptid = <%s>\n",
+                      host_address_to_string (gdbarch->displaced_step_restore_all_in_ptid));
   fprintf_unfiltered (file,
                       "gdbarch_dump: double_bit = %s\n",
                       plongest (gdbarch->double_bit));
@@ -4028,21 +4048,86 @@ set_gdbarch_displaced_step_fixup (struct gdbarch *gdbarch,
   gdbarch->displaced_step_fixup = displaced_step_fixup;
 }
 
-CORE_ADDR
-gdbarch_displaced_step_location (struct gdbarch *gdbarch)
+bool
+gdbarch_displaced_step_prepare_p (struct gdbarch *gdbarch)
+{
+  gdb_assert (gdbarch != NULL);
+  return gdbarch->displaced_step_prepare != NULL;
+}
+
+displaced_step_prepare_status
+gdbarch_displaced_step_prepare (struct gdbarch *gdbarch, thread_info *thread, CORE_ADDR &displaced_pc)
+{
+  gdb_assert (gdbarch != NULL);
+  gdb_assert (gdbarch->displaced_step_prepare != NULL);
+  if (gdbarch_debug >= 2)
+    fprintf_unfiltered (gdb_stdlog, "gdbarch_displaced_step_prepare called\n");
+  return gdbarch->displaced_step_prepare (gdbarch, thread, displaced_pc);
+}
+
+void
+set_gdbarch_displaced_step_prepare (struct gdbarch *gdbarch,
+                                    gdbarch_displaced_step_prepare_ftype displaced_step_prepare)
+{
+  gdbarch->displaced_step_prepare = displaced_step_prepare;
+}
+
+displaced_step_finish_status
+gdbarch_displaced_step_finish (struct gdbarch *gdbarch, thread_info *thread, gdb_signal sig)
+{
+  gdb_assert (gdbarch != NULL);
+  gdb_assert (gdbarch->displaced_step_finish != NULL);
+  if (gdbarch_debug >= 2)
+    fprintf_unfiltered (gdb_stdlog, "gdbarch_displaced_step_finish called\n");
+  return gdbarch->displaced_step_finish (gdbarch, thread, sig);
+}
+
+void
+set_gdbarch_displaced_step_finish (struct gdbarch *gdbarch,
+                                   gdbarch_displaced_step_finish_ftype displaced_step_finish)
+{
+  gdbarch->displaced_step_finish = displaced_step_finish;
+}
+
+bool
+gdbarch_displaced_step_copy_insn_closure_by_addr_p (struct gdbarch *gdbarch)
+{
+  gdb_assert (gdbarch != NULL);
+  return gdbarch->displaced_step_copy_insn_closure_by_addr != NULL;
+}
+
+const displaced_step_copy_insn_closure *
+gdbarch_displaced_step_copy_insn_closure_by_addr (struct gdbarch *gdbarch, inferior *inf, CORE_ADDR addr)
+{
+  gdb_assert (gdbarch != NULL);
+  gdb_assert (gdbarch->displaced_step_copy_insn_closure_by_addr != NULL);
+  if (gdbarch_debug >= 2)
+    fprintf_unfiltered (gdb_stdlog, "gdbarch_displaced_step_copy_insn_closure_by_addr called\n");
+  return gdbarch->displaced_step_copy_insn_closure_by_addr (inf, addr);
+}
+
+void
+set_gdbarch_displaced_step_copy_insn_closure_by_addr (struct gdbarch *gdbarch,
+                                                      gdbarch_displaced_step_copy_insn_closure_by_addr_ftype displaced_step_copy_insn_closure_by_addr)
+{
+  gdbarch->displaced_step_copy_insn_closure_by_addr = displaced_step_copy_insn_closure_by_addr;
+}
+
+void
+gdbarch_displaced_step_restore_all_in_ptid (struct gdbarch *gdbarch, inferior *parent_inf, ptid_t child_ptid)
 {
   gdb_assert (gdbarch != NULL);
-  gdb_assert (gdbarch->displaced_step_location != NULL);
+  gdb_assert (gdbarch->displaced_step_restore_all_in_ptid != NULL);
   if (gdbarch_debug >= 2)
-    fprintf_unfiltered (gdb_stdlog, "gdbarch_displaced_step_location called\n");
-  return gdbarch->displaced_step_location (gdbarch);
+    fprintf_unfiltered (gdb_stdlog, "gdbarch_displaced_step_restore_all_in_ptid called\n");
+  gdbarch->displaced_step_restore_all_in_ptid (parent_inf, child_ptid);
 }
 
 void
-set_gdbarch_displaced_step_location (struct gdbarch *gdbarch,
-                                     gdbarch_displaced_step_location_ftype displaced_step_location)
+set_gdbarch_displaced_step_restore_all_in_ptid (struct gdbarch *gdbarch,
+                                                gdbarch_displaced_step_restore_all_in_ptid_ftype displaced_step_restore_all_in_ptid)
 {
-  gdbarch->displaced_step_location = displaced_step_location;
+  gdbarch->displaced_step_restore_all_in_ptid = displaced_step_restore_all_in_ptid;
 }
 
 bool
diff --git a/gdb/gdbarch.h b/gdb/gdbarch.h
index 2b6876a1916c..002628d8988b 100644
--- a/gdb/gdbarch.h
+++ b/gdb/gdbarch.h
@@ -58,6 +58,7 @@ struct mem_range;
 struct syscalls_info;
 struct thread_info;
 struct ui_out;
+struct inferior;
 
 #include "regcache.h"
 
@@ -1071,17 +1072,36 @@ typedef void (gdbarch_displaced_step_fixup_ftype) (struct gdbarch *gdbarch, stru
 extern void gdbarch_displaced_step_fixup (struct gdbarch *gdbarch, struct displaced_step_copy_insn_closure *closure, CORE_ADDR from, CORE_ADDR to, struct regcache *regs);
 extern void set_gdbarch_displaced_step_fixup (struct gdbarch *gdbarch, gdbarch_displaced_step_fixup_ftype *displaced_step_fixup);
 
-/* Return the address of an appropriate place to put displaced
-   instructions while we step over them.  There need only be one such
-   place, since we're only stepping one thread over a breakpoint at a
-   time.
+/* Prepare THREAD for it to displaced step the instruction at its current PC.
   
-   For a general explanation of displaced stepping and how GDB uses it,
-   see the comments in infrun.c. */
+   Throw an exception if any unexpected error happens. */
+
+extern bool gdbarch_displaced_step_prepare_p (struct gdbarch *gdbarch);
+
+typedef displaced_step_prepare_status (gdbarch_displaced_step_prepare_ftype) (struct gdbarch *gdbarch, thread_info *thread, CORE_ADDR &displaced_pc);
+extern displaced_step_prepare_status gdbarch_displaced_step_prepare (struct gdbarch *gdbarch, thread_info *thread, CORE_ADDR &displaced_pc);
+extern void set_gdbarch_displaced_step_prepare (struct gdbarch *gdbarch, gdbarch_displaced_step_prepare_ftype *displaced_step_prepare);
+
+/* Clean up after a displaced step of THREAD. */
+
+typedef displaced_step_finish_status (gdbarch_displaced_step_finish_ftype) (struct gdbarch *gdbarch, thread_info *thread, gdb_signal sig);
+extern displaced_step_finish_status gdbarch_displaced_step_finish (struct gdbarch *gdbarch, thread_info *thread, gdb_signal sig);
+extern void set_gdbarch_displaced_step_finish (struct gdbarch *gdbarch, gdbarch_displaced_step_finish_ftype *displaced_step_finish);
+
+/* Return the closure associated to the displaced step buffer that is at ADDR. */
+
+extern bool gdbarch_displaced_step_copy_insn_closure_by_addr_p (struct gdbarch *gdbarch);
+
+typedef const displaced_step_copy_insn_closure * (gdbarch_displaced_step_copy_insn_closure_by_addr_ftype) (inferior *inf, CORE_ADDR addr);
+extern const displaced_step_copy_insn_closure * gdbarch_displaced_step_copy_insn_closure_by_addr (struct gdbarch *gdbarch, inferior *inf, CORE_ADDR addr);
+extern void set_gdbarch_displaced_step_copy_insn_closure_by_addr (struct gdbarch *gdbarch, gdbarch_displaced_step_copy_insn_closure_by_addr_ftype *displaced_step_copy_insn_closure_by_addr);
+
+/* PARENT_INF has forked and CHILD_PTID is the ptid of the child.  Restore the
+   contents of all displaced step buffers in the child's address space. */
 
-typedef CORE_ADDR (gdbarch_displaced_step_location_ftype) (struct gdbarch *gdbarch);
-extern CORE_ADDR gdbarch_displaced_step_location (struct gdbarch *gdbarch);
-extern void set_gdbarch_displaced_step_location (struct gdbarch *gdbarch, gdbarch_displaced_step_location_ftype *displaced_step_location);
+typedef void (gdbarch_displaced_step_restore_all_in_ptid_ftype) (inferior *parent_inf, ptid_t child_ptid);
+extern void gdbarch_displaced_step_restore_all_in_ptid (struct gdbarch *gdbarch, inferior *parent_inf, ptid_t child_ptid);
+extern void set_gdbarch_displaced_step_restore_all_in_ptid (struct gdbarch *gdbarch, gdbarch_displaced_step_restore_all_in_ptid_ftype *displaced_step_restore_all_in_ptid);
 
 /* Relocate an instruction to execute at a different address.  OLDLOC
    is the address in the inferior memory where the instruction to
diff --git a/gdb/gdbarch.sh b/gdb/gdbarch.sh
index 76ce2ff5efb4..e90654dc5c1c 100755
--- a/gdb/gdbarch.sh
+++ b/gdb/gdbarch.sh
@@ -813,14 +813,20 @@ m;bool;displaced_step_hw_singlestep;void;;;default_displaced_step_hw_singlestep;
 # see the comments in infrun.c.
 M;void;displaced_step_fixup;struct displaced_step_copy_insn_closure *closure, CORE_ADDR from, CORE_ADDR to, struct regcache *regs;closure, from, to, regs;;NULL
 
-# Return the address of an appropriate place to put displaced
-# instructions while we step over them.  There need only be one such
-# place, since we're only stepping one thread over a breakpoint at a
-# time.
+# Prepare THREAD for it to displaced step the instruction at its current PC.
 #
-# For a general explanation of displaced stepping and how GDB uses it,
-# see the comments in infrun.c.
-m;CORE_ADDR;displaced_step_location;void;;;NULL;;(! gdbarch->displaced_step_location) != (! gdbarch->displaced_step_copy_insn)
+# Throw an exception if any unexpected error happens.
+M;displaced_step_prepare_status;displaced_step_prepare;thread_info *thread, CORE_ADDR &displaced_pc;thread, displaced_pc
+
+# Clean up after a displaced step of THREAD.
+m;displaced_step_finish_status;displaced_step_finish;thread_info *thread, gdb_signal sig;thread, sig;;NULL;;(! gdbarch->displaced_step_finish) != (! gdbarch->displaced_step_prepare)
+
+# Return the closure associated to the displaced step buffer that is at ADDR.
+F;const displaced_step_copy_insn_closure *;displaced_step_copy_insn_closure_by_addr;inferior *inf, CORE_ADDR addr;inf, addr
+
+# PARENT_INF has forked and CHILD_PTID is the ptid of the child.  Restore the
+# contents of all displaced step buffers in the child's address space.
+f;void;displaced_step_restore_all_in_ptid;inferior *parent_inf, ptid_t child_ptid;parent_inf, child_ptid
 
 # Relocate an instruction to execute at a different address.  OLDLOC
 # is the address in the inferior memory where the instruction to
@@ -1297,6 +1303,7 @@ struct mem_range;
 struct syscalls_info;
 struct thread_info;
 struct ui_out;
+struct inferior;
 
 #include "regcache.h"
 
diff --git a/gdb/gdbthread.h b/gdb/gdbthread.h
index 1eecb989a79e..e5484ac54ca4 100644
--- a/gdb/gdbthread.h
+++ b/gdb/gdbthread.h
@@ -32,6 +32,7 @@ struct symtab;
 #include "gdbsupport/refcounted-object.h"
 #include "gdbsupport/common-gdbthread.h"
 #include "gdbsupport/forward-scope-exit.h"
+#include "displaced-stepping.h"
 
 struct inferior;
 struct process_stratum_target;
@@ -388,6 +389,9 @@ class thread_info : public refcounted_object
      fields point to self.  */
   struct thread_info *step_over_prev = NULL;
   struct thread_info *step_over_next = NULL;
+
+  /* Displaced-step state for this thread.  */
+  displaced_step_thread_state displaced_step_state;
 };
 
 /* A gdb::ref_ptr pointer to a thread_info.  */
@@ -745,10 +749,27 @@ extern bool value_in_thread_stack_temporaries (struct value *,
 
 extern void global_thread_step_over_chain_enqueue (thread_info *tp);
 
+/* Append the thread step over chain CHAIN_HEAD to the global thread step over
+   chain. */
+
+extern void global_thread_step_over_chain_enqueue_chain
+  (thread_info *chain_head);
+
+/* Remove TP from step-over chain LIST_P.  */
+
+extern void thread_step_over_chain_remove (thread_info **list_p,
+					   thread_info *tp);
+
 /* Remove TP from the global pending step-over chain.  */
 
 extern void global_thread_step_over_chain_remove (thread_info *tp);
 
+/* Return the thread following TP in the step-over chain whose head is
+   CHAIN_HEAD.  Return NULL if TP is the last entry in the chain.  */
+
+extern thread_info *thread_step_over_chain_next (thread_info *chain_head,
+						 thread_info *tp);
+
 /* Return the thread following TP in the global step-over chain, or NULL if TP
    is the last entry in the chain.  */
 
@@ -758,6 +779,14 @@ extern thread_info *global_thread_step_over_chain_next (thread_info *tp);
 
 extern int thread_is_in_step_over_chain (struct thread_info *tp);
 
+/* Return the length of the the step over chain TP is in.
+
+   If TP is non-nullptr, the thread must be in a step over chain.
+   TP may be nullptr, in which case it denotes an empty list, so a length of
+   0.  */
+
+extern int thread_step_over_chain_length (thread_info *tp);
+
 /* Cancel any ongoing execution command.  */
 
 extern void thread_cancel_execution_command (struct thread_info *thr);
diff --git a/gdb/hppa-linux-tdep.c b/gdb/hppa-linux-tdep.c
index 080379992aeb..a171e582e595 100644
--- a/gdb/hppa-linux-tdep.c
+++ b/gdb/hppa-linux-tdep.c
@@ -489,7 +489,7 @@ hppa_linux_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
 {
   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
 
-  linux_init_abi (info, gdbarch);
+  linux_init_abi (info, gdbarch, false);
 
   /* GNU/Linux is always ELF.  */
   tdep->is_elf = 1;
diff --git a/gdb/i386-linux-tdep.c b/gdb/i386-linux-tdep.c
index ab7d23611f89..1b209fd3efff 100644
--- a/gdb/i386-linux-tdep.c
+++ b/gdb/i386-linux-tdep.c
@@ -832,7 +832,7 @@ i386_linux_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
 
   gdb_assert (tdesc_data);
 
-  linux_init_abi (info, gdbarch);
+  linux_init_abi (info, gdbarch, true);
 
   /* GNU/Linux uses ELF.  */
   i386_elf_init_abi (info, gdbarch);
@@ -1065,8 +1065,6 @@ i386_linux_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
   set_gdbarch_displaced_step_copy_insn (gdbarch,
 					i386_linux_displaced_step_copy_insn);
   set_gdbarch_displaced_step_fixup (gdbarch, i386_displaced_step_fixup);
-  set_gdbarch_displaced_step_location (gdbarch,
-				       linux_displaced_step_location);
 
   /* Functions for 'catch syscall'.  */
   set_xml_syscall_file_name (gdbarch, XML_SYSCALL_FILENAME_I386);
diff --git a/gdb/ia64-linux-tdep.c b/gdb/ia64-linux-tdep.c
index 587a455467da..d6d581ab3dda 100644
--- a/gdb/ia64-linux-tdep.c
+++ b/gdb/ia64-linux-tdep.c
@@ -223,7 +223,7 @@ ia64_linux_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
   static const char *const stap_register_indirection_suffixes[] = { "]",
 								    NULL };
 
-  linux_init_abi (info, gdbarch);
+  linux_init_abi (info, gdbarch, false);
 
   /* Set the method of obtaining the sigcontext addresses at which
      registers are saved.  */
diff --git a/gdb/infrun.c b/gdb/infrun.c
index 9ac75eff4d30..9d04086be253 100644
--- a/gdb/infrun.c
+++ b/gdb/infrun.c
@@ -1452,21 +1452,6 @@ step_over_info_valid_p (void)
    displaced step operation on it.  See displaced_step_prepare and
    displaced_step_finish for details.  */
 
-/* Returns true if any inferior has a thread doing a displaced
-   step.  */
-
-static bool
-displaced_step_in_progress_any_inferior ()
-{
-  for (inferior *i : all_inferiors ())
-    {
-      if (i->displaced_step_state.step_thread != nullptr)
-	return true;
-    }
-
-  return false;
-}
-
 /* Return true if THREAD is doing a displaced step.  */
 
 static bool
@@ -1474,7 +1459,7 @@ displaced_step_in_progress_thread (thread_info *thread)
 {
   gdb_assert (thread != NULL);
 
-  return thread->inf->displaced_step_state.step_thread == thread;
+  return thread->displaced_step_state.in_progress ();
 }
 
 /* Return true if INF has a thread doing a displaced step.  */
@@ -1482,25 +1467,21 @@ displaced_step_in_progress_thread (thread_info *thread)
 static bool
 displaced_step_in_progress (inferior *inf)
 {
-  return inf->displaced_step_state.step_thread != nullptr;
+  return inf->displaced_step_state.in_progress_count > 0;
 }
 
-/* If inferior is in displaced stepping, and ADDR equals to starting address
-   of copy area, return corresponding displaced_step_copy_insn_closure.
-   Otherwise, return NULL.  */
+/* Return true if any thread is doing a displaced step.  */
 
-displaced_step_copy_insn_closure *
-get_displaced_step_copy_insn_closure_by_addr (CORE_ADDR addr)
+static bool
+displaced_step_in_progress_any_thread ()
 {
-  displaced_step_inferior_state &displaced
-    = current_inferior ()->displaced_step_state;
-
-  /* If checking the mode of displaced instruction in copy area.  */
-  if (displaced.step_thread != nullptr
-      && displaced.step_copy == addr)
-    return displaced.step_closure.get ();
+  for (inferior *inf : all_non_exited_inferiors ())
+    {
+      if (displaced_step_in_progress (inf))
+	return true;
+    }
 
-  return NULL;
+  return false;
 }
 
 static void
@@ -1512,12 +1493,15 @@ infrun_inferior_exit (struct inferior *inf)
 static void
 infrun_inferior_execd (inferior *inf)
 {
-  /* If a thread was doing a displaced step in this inferior at the moment of
-     the exec, it no longer exists.  Even if the exec'ing thread was the one
+  /* If some threads where was doing a displaced step in this inferior at the
+     moment of the exec, they no longer exist.  Even if the exec'ing thread
      doing a displaced step, we don't want to to any fixup nor restore displaced
      stepping buffer bytes.  */
   inf->displaced_step_state.reset ();
 
+  for (thread_info *thread : inf->threads ())
+    thread->displaced_step_state.reset ();
+
   /* Since an in-line step is done with everything else stopped, if there was
      one in progress at the time of the exec, it must have been the exec'ing
      thread.  */
@@ -1555,9 +1539,9 @@ show_can_use_displaced_stepping (struct ui_file *file, int from_tty,
 static bool
 gdbarch_supports_displaced_stepping (gdbarch *arch)
 {
-  /* Only check for the presence of step_copy_insn.  Other required methods
-     are checked by the gdbarch validation.  */
-  return gdbarch_displaced_step_copy_insn_p (arch);
+  /* Only check for the presence of `prepare`.  The gdbarch verification ensures
+     that if `prepare` is provided, so is `finish`.  */
+  return gdbarch_displaced_step_prepare_p (arch);
 }
 
 /* Return non-zero if displaced stepping can/should be used to step
@@ -1595,10 +1579,10 @@ use_displaced_stepping (thread_info *tp)
   return true;
 }
 
-/* Simple function wrapper around displaced_step_inferior_state::reset.  */
+/* Simple function wrapper around displaced_step_thread_state::reset.  */
 
 static void
-displaced_step_reset (displaced_step_inferior_state *displaced)
+displaced_step_reset (displaced_step_thread_state *displaced)
 {
   displaced->reset ();
 }
@@ -1649,10 +1633,8 @@ displaced_step_prepare_throw (thread_info *tp)
 {
   regcache *regcache = get_thread_regcache (tp);
   struct gdbarch *gdbarch = regcache->arch ();
-  const address_space *aspace = regcache->aspace ();
-  CORE_ADDR original, copy;
-  ULONGEST len;
-  int status;
+  displaced_step_thread_state &disp_step_thread_state
+    = tp->displaced_step_state;
 
   /* We should never reach this function if the architecture does not
      support displaced stepping.  */
@@ -1667,15 +1649,14 @@ displaced_step_prepare_throw (thread_info *tp)
      jump/branch).  */
   tp->control.may_range_step = 0;
 
-  /* We have to displaced step one thread at a time, as we only have
-     access to a single scratch space per inferior.  */
-
-  displaced_step_inferior_state *displaced = &tp->inf->displaced_step_state;
+  /* We are about to start a displaced step for this thread.  If one is already
+     in progress, something's wrong.  */
+  gdb_assert (!disp_step_thread_state.in_progress ());
 
-  if (displaced->step_thread != nullptr)
+  if (tp->inf->displaced_step_state.unavailable)
     {
-      /* Already waiting for a displaced step to finish.  Defer this
-	 request and place in queue.  */
+      /* The gdbarch tells us it's not worth asking to try a prepare because
+	 it is likely that it will return unavailable, so don't bother asking.  */
 
       displaced_debug_printf ("deferring step of %s",
 			      target_pid_to_str (tp->ptid).c_str ());
@@ -1683,79 +1664,54 @@ displaced_step_prepare_throw (thread_info *tp)
       global_thread_step_over_chain_enqueue (tp);
       return DISPLACED_STEP_PREPARE_STATUS_UNAVAILABLE;
     }
-  else
-    displaced_debug_printf ("stepping %s now",
-			    target_pid_to_str (tp->ptid).c_str ());
 
-  displaced_step_reset (displaced);
+  displaced_debug_printf ("displaced-stepping %s now",
+			  target_pid_to_str (tp->ptid).c_str ());
 
   scoped_restore_current_thread restore_thread;
 
   switch_to_thread (tp);
 
-  original = regcache_read_pc (regcache);
+  CORE_ADDR original_pc = regcache_read_pc (regcache);
+  CORE_ADDR displaced_pc;
 
-  copy = gdbarch_displaced_step_location (gdbarch);
-  len = gdbarch_max_insn_length (gdbarch);
+  displaced_step_prepare_status status
+    = gdbarch_displaced_step_prepare (gdbarch, tp, displaced_pc);
 
-  if (breakpoint_in_range_p (aspace, copy, len))
+  if (status == DISPLACED_STEP_PREPARE_STATUS_CANT)
     {
-      /* There's a breakpoint set in the scratch pad location range
-	 (which is usually around the entry point).  We'd either
-	 install it before resuming, which would overwrite/corrupt the
-	 scratch pad, or if it was already inserted, this displaced
-	 step would overwrite it.  The latter is OK in the sense that
-	 we already assume that no thread is going to execute the code
-	 in the scratch pad range (after initial startup) anyway, but
-	 the former is unacceptable.  Simply punt and fallback to
-	 stepping over this breakpoint in-line.  */
-      displaced_debug_printf ("breakpoint set in scratch pad.  "
-			      "Stepping over breakpoint in-line instead.");
+      displaced_debug_printf ("failed to prepare (%s)",
+			      target_pid_to_str (tp->ptid).c_str ());
 
       return DISPLACED_STEP_PREPARE_STATUS_CANT;
     }
+  else if (status == DISPLACED_STEP_PREPARE_STATUS_UNAVAILABLE)
+    {
+      /* Not enough displaced stepping resources available, defer this
+	 request by placing it the queue.  */
 
-  /* Save the original contents of the copy area.  */
-  displaced->step_saved_copy.resize (len);
-  status = target_read_memory (copy, displaced->step_saved_copy.data (), len);
-  if (status != 0)
-    throw_error (MEMORY_ERROR,
-		 _("Error accessing memory address %s (%s) for "
-		   "displaced-stepping scratch space."),
-		 paddress (gdbarch, copy), safe_strerror (status));
+      displaced_debug_printf ("not enough resources available, "
+			      "deferring step of %s",
+			      target_pid_to_str (tp->ptid).c_str ());
 
-  displaced_debug_printf ("saved %s: %s",
-			  paddress (gdbarch, copy),
-			  displaced_step_dump_bytes
-			    (displaced->step_saved_copy.data (), len).c_str ());
+      global_thread_step_over_chain_enqueue (tp);
 
-  displaced->step_closure
-    = gdbarch_displaced_step_copy_insn (gdbarch, original, copy, regcache);
-  if (displaced->step_closure == NULL)
-    {
-      /* The architecture doesn't know how or want to displaced step
-	 this instruction or instruction sequence.  Fallback to
-	 stepping over the breakpoint in-line.  */
-      return DISPLACED_STEP_PREPARE_STATUS_CANT;
+      return DISPLACED_STEP_PREPARE_STATUS_UNAVAILABLE;
     }
 
+  gdb_assert (status == DISPLACED_STEP_PREPARE_STATUS_OK);
+
   /* Save the information we need to fix things up if the step
      succeeds.  */
-  displaced->step_thread = tp;
-  displaced->step_gdbarch = gdbarch;
-  displaced->step_original = original;
-  displaced->step_copy = copy;
-
-  {
-    displaced_step_reset_cleanup cleanup (displaced);
+  disp_step_thread_state.set (gdbarch);
 
-    /* Resume execution at the copy.  */
-    regcache_write_pc (regcache, copy);
+  tp->inf->displaced_step_state.in_progress_count++;
 
-    cleanup.release ();
-  }
-
-  displaced_debug_printf ("displaced pc to %s", paddress (gdbarch, copy));
+  displaced_debug_printf ("prepared successfully thread=%s, "
+			  "original_pc=%s, displaced_pc=%s",
+			  target_pid_to_str (tp->ptid).c_str (),
+			  paddress (gdbarch, original_pc),
+			  paddress (gdbarch, displaced_pc));
 
   return DISPLACED_STEP_PREPARE_STATUS_OK;
 }
@@ -1797,33 +1753,6 @@ displaced_step_prepare (thread_info *thread)
   return status;
 }
 
-static void
-write_memory_ptid (ptid_t ptid, CORE_ADDR memaddr,
-		   const gdb_byte *myaddr, int len)
-{
-  scoped_restore save_inferior_ptid = make_scoped_restore (&inferior_ptid);
-
-  inferior_ptid = ptid;
-  write_memory (memaddr, myaddr, len);
-}
-
-/* Restore the contents of the copy area for thread PTID.  */
-
-static void
-displaced_step_restore (struct displaced_step_inferior_state *displaced,
-			ptid_t ptid)
-{
-  ULONGEST len = gdbarch_max_insn_length (displaced->step_gdbarch);
-
-  write_memory_ptid (ptid, displaced->step_copy,
-		     displaced->step_saved_copy.data (), len);
-
-  displaced_debug_printf ("restored %s %s",
-			  target_pid_to_str (ptid).c_str (),
-			  paddress (displaced->step_gdbarch,
-				    displaced->step_copy));
-}
-
 /* If we displaced stepped an instruction successfully, adjust registers and
    memory to yield the same effect the instruction would have had if we had
    executed it at its original address, and return
@@ -1836,13 +1765,15 @@ displaced_step_restore (struct displaced_step_inferior_state *displaced,
 static displaced_step_finish_status
 displaced_step_finish (thread_info *event_thread, enum gdb_signal signal)
 {
-  displaced_step_inferior_state *displaced
-    = &event_thread->inf->displaced_step_state;
+  displaced_step_thread_state *displaced = &event_thread->displaced_step_state;
 
-  /* Was this event for the thread we displaced?  */
-  if (displaced->step_thread != event_thread)
+  /* Was this thread performing a displaced step?  */
+  if (!displaced->in_progress ())
     return DISPLACED_STEP_FINISH_STATUS_OK;
 
+  gdb_assert (event_thread->inf->displaced_step_state.in_progress_count > 0);
+  event_thread->inf->displaced_step_state.in_progress_count--;
+
   /* Fixup may need to read memory/registers.  Switch to the thread
      that we're fixing up.  Also, target_stopped_by_watchpoint checks
      the current thread, and displaced_step_restore performs ptid-dependent
@@ -1851,35 +1782,10 @@ displaced_step_finish (thread_info *event_thread, enum gdb_signal signal)
 
   displaced_step_reset_cleanup cleanup (displaced);
 
-  displaced_step_restore (displaced, displaced->step_thread->ptid);
-
-  /* Did the instruction complete successfully?  */
-  if (signal == GDB_SIGNAL_TRAP
-      && !(target_stopped_by_watchpoint ()
-	   && (gdbarch_have_nonsteppable_watchpoint (displaced->step_gdbarch)
-	       || target_have_steppable_watchpoint ())))
-    {
-      /* Fix up the resulting state.  */
-      gdbarch_displaced_step_fixup (displaced->step_gdbarch,
-				    displaced->step_closure.get (),
-				    displaced->step_original,
-				    displaced->step_copy,
-				    get_thread_regcache (displaced->step_thread));
-
-      return DISPLACED_STEP_FINISH_STATUS_OK;
-    }
-  else
-    {
-      /* Since the instruction didn't complete, all we can do is
-	 relocate the PC.  */
-      struct regcache *regcache = get_thread_regcache (event_thread);
-      CORE_ADDR pc = regcache_read_pc (regcache);
-
-      pc = displaced->step_original + (pc - displaced->step_copy);
-      regcache_write_pc (regcache, pc);
-
-      return DISPLACED_STEP_FINISH_STATUS_NOT_EXECUTED;
-    }
+  /* Do the fixup, and release the resources acquired to do the displaced
+     step. */
+  return gdbarch_displaced_step_finish (displaced->get_original_gdbarch (),
+					event_thread, signal);
 }
 
 /* Data to be passed around while handling an event.  This data is
@@ -1927,14 +1833,41 @@ static step_over_what thread_still_needs_step_over (struct thread_info *tp);
 static bool
 start_step_over (void)
 {
-  struct thread_info *tp, *next;
+  thread_info *next;
 
   /* Don't start a new step-over if we already have an in-line
      step-over operation ongoing.  */
   if (step_over_info_valid_p ())
     return false;
 
-  for (tp = global_thread_step_over_chain_head; tp != NULL; tp = next)
+  /* Steal the global thread step over chain.  As we try to initiate displaced
+     steps, threads will be enqueued in the global chain if no buffers are
+     available.  If we iterated on the global chain directly, we might iterate
+     indefinitely.  */
+  thread_info *threads_to_step = global_thread_step_over_chain_head;
+  global_thread_step_over_chain_head = NULL;
+
+  infrun_debug_printf ("stealing global queue of threads to step, length = %d",
+		       thread_step_over_chain_length (threads_to_step));
+
+  bool started = false;
+
+  /* On scope exit (whatever the reason, return or exception), if there are
+     threads left in the THREADS_TO_STEP chain, put back these threads in the
+     global list.  */
+  SCOPE_EXIT {
+    if (threads_to_step == nullptr)
+      infrun_debug_printf ("step-over queue now empty");
+    else
+      {
+	infrun_debug_printf ("putting back %d threads to step in global queue",
+			     thread_step_over_chain_length (threads_to_step));
+
+	global_thread_step_over_chain_enqueue_chain (threads_to_step);
+      }
+  };
+
+  for (thread_info *tp = threads_to_step; tp != NULL; tp = next)
     {
       struct execution_control_state ecss;
       struct execution_control_state *ecs = &ecss;
@@ -1943,12 +1876,23 @@ start_step_over (void)
 
       gdb_assert (!tp->stop_requested);
 
-      next = global_thread_step_over_chain_next (tp);
+      next = thread_step_over_chain_next (threads_to_step, tp);
 
-      /* If this inferior already has a displaced step in process,
-	 don't start a new one.  */
-      if (displaced_step_in_progress (tp->inf))
-	continue;
+      if (tp->inf->displaced_step_state.unavailable)
+	{
+	  /* The arch told us to not even try preparing another displaced step
+	     for this inferior.  Just leave the thread in THREADS_TO_STEP, it
+	     will get moved to the global chain on scope exit.  */
+	  continue;
+	}
+
+      /* Remove thread from the THREADS_TO_STEP chain.  If anything goes wrong
+	 while we try to prepare the displaced step, we don't add it back to
+	 the global step over chain.  This is to avoid a thread staying in the
+	 step over chain indefinitely if something go wrong when resuming it.
+	 If the error is intermittent and it still needs a step over, it will
+	 get enqueued again when we try to resume it normally.  */
+      thread_step_over_chain_remove (&threads_to_step, tp);
 
       step_what = thread_still_needs_step_over (tp);
       must_be_in_line = ((step_what & STEP_OVER_WATCHPOINT)
@@ -1958,13 +1902,11 @@ start_step_over (void)
       /* We currently stop all threads of all processes to step-over
 	 in-line.  If we need to start a new in-line step-over, let
 	 any pending displaced steps finish first.  */
-      if (must_be_in_line && displaced_step_in_progress_any_inferior ())
-	return false;
-
-      global_thread_step_over_chain_remove (tp);
-
-      if (global_thread_step_over_chain_head == NULL)
-	infrun_debug_printf ("step-over queue now empty");
+      if (must_be_in_line && displaced_step_in_progress_any_thread ())
+	{
+	  global_thread_step_over_chain_enqueue (tp);
+	  continue;
+	}
 
       if (tp->control.trap_expected
 	  || tp->resumed
@@ -1998,13 +1940,27 @@ start_step_over (void)
       if (!ecs->wait_some_more)
 	error (_("Command aborted."));
 
-      gdb_assert (tp->resumed);
+      /* If the thread's step over could not be initiated because no buffers
+	 were available, it was re-added to the global step over chain.  */
+      if (tp->resumed)
+	{
+	  infrun_debug_printf ("[%s] was resumed.",
+			       target_pid_to_str (tp->ptid).c_str ());
+	  gdb_assert (!thread_is_in_step_over_chain (tp));
+	}
+      else
+	{
+	  infrun_debug_printf ("[%s] was NOT resumed.",
+			       target_pid_to_str (tp->ptid).c_str ());
+	  gdb_assert (thread_is_in_step_over_chain (tp));
+	}
 
       /* If we started a new in-line step-over, we're done.  */
       if (step_over_info_valid_p ())
 	{
 	  gdb_assert (tp->control.trap_expected);
-	  return true;
+	  started = true;
+	  break;
 	}
 
       if (!target_is_non_stop_p ())
@@ -2017,7 +1973,8 @@ start_step_over (void)
 	  /* With remote targets (at least), in all-stop, we can't
 	     issue any further remote commands until the program stops
 	     again.  */
-	  return true;
+	  started = true;
+	  break;
 	}
 
       /* Either the thread no longer needed a step-over, or a new
@@ -2026,7 +1983,7 @@ start_step_over (void)
 	 displaced step on a thread of other process. */
     }
 
-  return false;
+  return started;
 }
 
 /* Update global variables holding ptids to hold NEW_PTID if they were
@@ -3611,18 +3568,16 @@ prepare_for_detach (void)
   struct inferior *inf = current_inferior ();
   ptid_t pid_ptid = ptid_t (inf->pid);
 
-  displaced_step_inferior_state *displaced = &inf->displaced_step_state;
-
   /* Is any thread of this process displaced stepping?  If not,
      there's nothing else to do.  */
-  if (displaced->step_thread == nullptr)
+  if (displaced_step_in_progress (inf))
     return;
 
   infrun_debug_printf ("displaced-stepping in-process while detaching");
 
   scoped_restore restore_detaching = make_scoped_restore (&inf->detaching, true);
 
-  while (displaced->step_thread != nullptr)
+  while (displaced_step_in_progress (inf))
     {
       struct execution_control_state ecss;
       struct execution_control_state *ecs;
@@ -5288,17 +5243,12 @@ handle_inferior_event (struct execution_control_state *ecs)
 	struct gdbarch *gdbarch = regcache->arch ();
 	inferior *parent_inf = find_inferior_ptid (ecs->target, ecs->ptid);
 
-	/* If this is a fork (child gets its own address space copy) and the
-	   displaced step buffer was in use at the time of the fork, restore
-	   displaced step buffer bytes in the child process.  */
+	/* If this is a fork (child gets its own address space copy) and some
+	   displaced step buffers were in use at the time of the fork, restore
+	   the displaced step buffer bytes in the child process.  */
 	if (ecs->ws.kind == TARGET_WAITKIND_FORKED)
-	  {
-	    displaced_step_inferior_state *displaced
-	      = &parent_inf->displaced_step_state;
-
-	    if (displaced->step_thread != nullptr)
-	      displaced_step_restore (displaced, ecs->ws.value.related_pid);
-	  }
+	  gdbarch_displaced_step_restore_all_in_ptid
+	    (gdbarch, parent_inf, ecs->ws.value.related_pid);
 
 	/* If displaced stepping is supported, and thread ecs->ptid is
 	   displaced stepping.  */
diff --git a/gdb/infrun.h b/gdb/infrun.h
index c83cb3330833..d5e6d279f1a0 100644
--- a/gdb/infrun.h
+++ b/gdb/infrun.h
@@ -226,9 +226,6 @@ extern void clear_exit_convenience_vars (void);
 /* Dump LEN bytes at BUF in hex to a string and return it.  */
 extern std::string displaced_step_dump_bytes (const gdb_byte *buf, size_t len);
 
-extern struct displaced_step_copy_insn_closure *
-  get_displaced_step_copy_insn_closure_by_addr (CORE_ADDR addr);
-
 extern void update_observer_mode (void);
 
 extern void signal_catch_update (const unsigned int *);
diff --git a/gdb/linux-tdep.c b/gdb/linux-tdep.c
index 655a2c33737d..70e74d510971 100644
--- a/gdb/linux-tdep.c
+++ b/gdb/linux-tdep.c
@@ -199,6 +199,9 @@ struct linux_info
      yet.  Positive if we tried looking it up, and found it.  Negative
      if we tried looking it up but failed.  */
   int vsyscall_range_p = 0;
+
+  /* Inferior's displaced step buffer.  */
+  gdb::optional<displaced_step_buffer> disp_step_buf;
 };
 
 /* Per-inferior data key.  */
@@ -2531,6 +2534,65 @@ linux_displaced_step_location (struct gdbarch *gdbarch)
 
 /* See linux-tdep.h.  */
 
+displaced_step_prepare_status
+linux_displaced_step_prepare (gdbarch *arch, thread_info *thread,
+			      CORE_ADDR &displaced_pc)
+{
+  linux_info *per_inferior = get_linux_inferior_data (thread->inf);
+
+  if (!per_inferior->disp_step_buf.has_value ())
+    {
+      CORE_ADDR disp_step_buf_addr
+	= linux_displaced_step_location (thread->inf->gdbarch);
+
+      per_inferior->disp_step_buf.emplace (disp_step_buf_addr);
+    }
+
+  return per_inferior->disp_step_buf->prepare (thread, displaced_pc);
+}
+
+/* See linux-tdep.h.  */
+
+displaced_step_finish_status
+linux_displaced_step_finish (gdbarch *arch, thread_info *thread, gdb_signal sig)
+{
+  linux_info *per_inferior = get_linux_inferior_data (thread->inf);
+
+  gdb_assert (per_inferior->disp_step_buf.has_value ());
+
+  return per_inferior->disp_step_buf->finish (arch, thread, sig);
+}
+
+/* See linux-tdep.h.  */
+
+const displaced_step_copy_insn_closure *
+linux_displaced_step_copy_insn_closure_by_addr (inferior *inf, CORE_ADDR addr)
+{
+  linux_info *per_inferior = linux_inferior_data.get (inf);
+
+  if (per_inferior == nullptr
+      || !per_inferior->disp_step_buf.has_value ())
+    return nullptr;
+
+  return per_inferior->disp_step_buf->copy_insn_closure_by_addr (addr);
+}
+
+/* See linux-tdep.h.  */
+
+void
+linux_displaced_step_restore_all_in_ptid (inferior *parent_inf, ptid_t ptid)
+{
+  linux_info *per_inferior = linux_inferior_data.get (parent_inf);
+
+  if (per_inferior == nullptr
+      || !per_inferior->disp_step_buf.has_value ())
+    return;
+
+  per_inferior->disp_step_buf->restore_in_ptid (ptid);
+}
+
+/* See linux-tdep.h.  */
+
 CORE_ADDR
 linux_get_hwcap (struct target_ops *target)
 {
@@ -2577,8 +2639,19 @@ show_dump_excluded_mappings (struct ui_file *file, int from_tty,
    various GNU/Linux architectures and machine types.  */
 
 void
-linux_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
+linux_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch,
+		bool supports_displaced_step)
 {
+  if (supports_displaced_step)
+    {
+      set_gdbarch_displaced_step_prepare (gdbarch, linux_displaced_step_prepare);
+      set_gdbarch_displaced_step_finish (gdbarch, linux_displaced_step_finish);
+      set_gdbarch_displaced_step_copy_insn_closure_by_addr
+	(gdbarch, linux_displaced_step_copy_insn_closure_by_addr);
+      set_gdbarch_displaced_step_restore_all_in_ptid
+	(gdbarch, linux_displaced_step_restore_all_in_ptid);
+    }
+
   set_gdbarch_core_pid_to_str (gdbarch, linux_core_pid_to_str);
   set_gdbarch_info_proc (gdbarch, linux_info_proc);
   set_gdbarch_core_info_proc (gdbarch, linux_core_info_proc);
@@ -2608,6 +2681,7 @@ _initialize_linux_tdep ()
   /* Observers used to invalidate the cache when needed.  */
   gdb::observers::inferior_exit.attach (invalidate_linux_cache_inf);
   gdb::observers::inferior_appeared.attach (invalidate_linux_cache_inf);
+  gdb::observers::inferior_execd.attach (invalidate_linux_cache_inf);
 
   add_setshow_boolean_cmd ("use-coredump-filter", class_files,
 			   &use_coredump_filter, _("\
diff --git a/gdb/linux-tdep.h b/gdb/linux-tdep.h
index 91c28738f52d..0f83dc3c7811 100644
--- a/gdb/linux-tdep.h
+++ b/gdb/linux-tdep.h
@@ -21,7 +21,9 @@
 #define LINUX_TDEP_H
 
 #include "bfd.h"
+#include "displaced-stepping.h"
 
+struct inferior;
 struct regcache;
 
 /* Enum used to define the extra fields of the siginfo type used by an
@@ -57,7 +59,30 @@ extern int linux_gdb_signal_to_target (struct gdbarch *gdbarch,
    the target auxiliary vector.  */
 extern CORE_ADDR linux_displaced_step_location (struct gdbarch *gdbarch);
 
-extern void linux_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch);
+
+/* Implementation of gdbarch_displaced_step_prepare.  */
+
+extern displaced_step_prepare_status linux_displaced_step_prepare
+  (gdbarch *arch, thread_info *thread, CORE_ADDR &displaced_pc);
+
+/* Implementation of gdbarch_displaced_step_finish.  */
+
+extern displaced_step_finish_status linux_displaced_step_finish
+  (gdbarch *arch, thread_info *thread, gdb_signal sig);
+
+/* Implementation of gdbarch_displaced_step_copy_insn_closure_by_addr.  */
+
+extern const displaced_step_copy_insn_closure *
+  linux_displaced_step_copy_insn_closure_by_addr
+    (inferior *inf, CORE_ADDR addr);
+
+/* Implementation of gdbarch_displaced_step_restore_all_in_ptid.  */
+
+extern void linux_displaced_step_restore_all_in_ptid (inferior *parent_inf,
+						      ptid_t ptid);
+
+extern void linux_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch,
+			    bool supports_displaced_step);
 
 extern int linux_is_uclinux (void);
 
diff --git a/gdb/m32r-linux-tdep.c b/gdb/m32r-linux-tdep.c
index 60a52e60b6ed..961d54a2ca26 100644
--- a/gdb/m32r-linux-tdep.c
+++ b/gdb/m32r-linux-tdep.c
@@ -449,7 +449,7 @@ static void
 m32r_linux_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
 {
 
-  linux_init_abi (info, gdbarch);
+  linux_init_abi (info, gdbarch, false);
 
   /* Since EVB register is not available for native debug, we reduce
      the number of registers.  */
diff --git a/gdb/m68k-linux-tdep.c b/gdb/m68k-linux-tdep.c
index 63dd53e71d6d..509333558ec9 100644
--- a/gdb/m68k-linux-tdep.c
+++ b/gdb/m68k-linux-tdep.c
@@ -385,7 +385,7 @@ m68k_linux_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
 {
   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
 
-  linux_init_abi (info, gdbarch);
+  linux_init_abi (info, gdbarch, false);
 
   tdep->jb_pc = M68K_LINUX_JB_PC;
   tdep->jb_elt_size = M68K_LINUX_JB_ELEMENT_SIZE;
diff --git a/gdb/microblaze-linux-tdep.c b/gdb/microblaze-linux-tdep.c
index be710bedb649..2a91e1bb39a8 100644
--- a/gdb/microblaze-linux-tdep.c
+++ b/gdb/microblaze-linux-tdep.c
@@ -117,7 +117,7 @@ static void
 microblaze_linux_init_abi (struct gdbarch_info info,
 			   struct gdbarch *gdbarch)
 {
-  linux_init_abi (info, gdbarch);
+  linux_init_abi (info, gdbarch, false);
 
   set_gdbarch_memory_remove_breakpoint (gdbarch,
 					microblaze_linux_memory_remove_breakpoint);
diff --git a/gdb/mips-linux-tdep.c b/gdb/mips-linux-tdep.c
index aa2bd53348d2..9ca59e5b296b 100644
--- a/gdb/mips-linux-tdep.c
+++ b/gdb/mips-linux-tdep.c
@@ -1531,7 +1531,7 @@ mips_linux_init_abi (struct gdbarch_info info,
   enum mips_abi abi = mips_abi (gdbarch);
   struct tdesc_arch_data *tdesc_data = info.tdesc_data;
 
-  linux_init_abi (info, gdbarch);
+  linux_init_abi (info, gdbarch, false);
 
   /* Get the syscall number from the arch's register.  */
   set_gdbarch_get_syscall_number (gdbarch, mips_linux_get_syscall_number);
diff --git a/gdb/mn10300-linux-tdep.c b/gdb/mn10300-linux-tdep.c
index b5c391c0ccce..27645b1260c8 100644
--- a/gdb/mn10300-linux-tdep.c
+++ b/gdb/mn10300-linux-tdep.c
@@ -704,7 +704,7 @@ am33_linux_sigframe_cache_init (const struct tramp_frame *self,
 static void
 am33_linux_init_osabi (struct gdbarch_info info, struct gdbarch *gdbarch)
 {
-  linux_init_abi (info, gdbarch);
+  linux_init_abi (info, gdbarch, false);
 
   set_gdbarch_iterate_over_regset_sections
     (gdbarch, am33_iterate_over_regset_sections);
diff --git a/gdb/nios2-linux-tdep.c b/gdb/nios2-linux-tdep.c
index e6703ae9bccb..b5c12852c705 100644
--- a/gdb/nios2-linux-tdep.c
+++ b/gdb/nios2-linux-tdep.c
@@ -219,7 +219,7 @@ nios2_linux_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
 {
   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
 
-  linux_init_abi (info, gdbarch);
+  linux_init_abi (info, gdbarch, false);
 
   /* Shared library handling.  */
   set_gdbarch_skip_trampoline_code (gdbarch, find_solib_trampoline_target);
diff --git a/gdb/or1k-linux-tdep.c b/gdb/or1k-linux-tdep.c
index 15677f9cfaf6..33ddd10e85f3 100644
--- a/gdb/or1k-linux-tdep.c
+++ b/gdb/or1k-linux-tdep.c
@@ -140,7 +140,7 @@ or1k_linux_sigframe_init (const struct tramp_frame *self,
 static void
 or1k_linux_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
 {
-  linux_init_abi (info, gdbarch);
+  linux_init_abi (info, gdbarch, false);
 
   set_solib_svr4_fetch_link_map_offsets (gdbarch,
 					 svr4_ilp32_fetch_link_map_offsets);
diff --git a/gdb/ppc-linux-tdep.c b/gdb/ppc-linux-tdep.c
index 8cd8ef893ad4..e6c10dd83db5 100644
--- a/gdb/ppc-linux-tdep.c
+++ b/gdb/ppc-linux-tdep.c
@@ -1993,7 +1993,7 @@ ppc_linux_init_abi (struct gdbarch_info info,
   static const char *const stap_register_indirection_suffixes[] = { ")",
 								    NULL };
 
-  linux_init_abi (info, gdbarch);
+  linux_init_abi (info, gdbarch, false);
 
   /* PPC GNU/Linux uses either 64-bit or 128-bit long doubles; where
      128-bit, they can be either IBM long double or IEEE quad long double.
@@ -2143,9 +2143,6 @@ ppc_linux_init_abi (struct gdbarch_info info,
 	}
     }
 
-  set_gdbarch_displaced_step_location (gdbarch,
-				       linux_displaced_step_location);
-
   /* Support reverse debugging.  */
   set_gdbarch_process_record (gdbarch, ppc_process_record);
   set_gdbarch_process_record_signal (gdbarch, ppc_linux_record_signal);
diff --git a/gdb/riscv-linux-tdep.c b/gdb/riscv-linux-tdep.c
index fce838097e26..a2238ad786eb 100644
--- a/gdb/riscv-linux-tdep.c
+++ b/gdb/riscv-linux-tdep.c
@@ -159,7 +159,7 @@ riscv_linux_sigframe_init (const struct tramp_frame *self,
 static void
 riscv_linux_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
 {
-  linux_init_abi (info, gdbarch);
+  linux_init_abi (info, gdbarch, false);
 
   set_gdbarch_software_single_step (gdbarch, riscv_software_single_step);
 
diff --git a/gdb/rs6000-aix-tdep.c b/gdb/rs6000-aix-tdep.c
index 03e573b436a0..b5b398ed4d7f 100644
--- a/gdb/rs6000-aix-tdep.c
+++ b/gdb/rs6000-aix-tdep.c
@@ -1132,10 +1132,12 @@ rs6000_aix_init_osabi (struct gdbarch_info info, struct gdbarch *gdbarch)
   set_gdbarch_software_single_step (gdbarch, rs6000_software_single_step);
 
   /* Displaced stepping is currently not supported in combination with
-     software single-stepping.  */
+     software single-stepping.  These override the values set by
+     rs6000_gdbarch_init.  */
   set_gdbarch_displaced_step_copy_insn (gdbarch, NULL);
   set_gdbarch_displaced_step_fixup (gdbarch, NULL);
-  set_gdbarch_displaced_step_location (gdbarch, NULL);
+  set_gdbarch_displaced_step_prepare (gdbarch, NULL);
+  set_gdbarch_displaced_step_finish (gdbarch, NULL);
 
   set_gdbarch_push_dummy_call (gdbarch, rs6000_push_dummy_call);
   set_gdbarch_return_value (gdbarch, rs6000_return_value);
diff --git a/gdb/rs6000-tdep.c b/gdb/rs6000-tdep.c
index 626b47e244f1..1a7c7fcb9f16 100644
--- a/gdb/rs6000-tdep.c
+++ b/gdb/rs6000-tdep.c
@@ -153,6 +153,31 @@ static const char *const powerpc_vector_strings[] =
 static enum powerpc_vector_abi powerpc_vector_abi_global = POWERPC_VEC_AUTO;
 static const char *powerpc_vector_abi_string = "auto";
 
+/* PowerPC-related per-inferior data.  */
+
+struct ppc_inferior_data
+{
+  /* This is an optional in case we add more fields to ppc_inferior_data, we
+     don't want it instantiated as soon as we get the ppc_inferior_data for an
+     inferior.  */
+  gdb::optional<displaced_step_buffer> disp_step_buf;
+};
+
+static inferior_key<ppc_inferior_data> ppc_inferior_data_key;
+
+/* Get the per-inferior PowerPC data for INF.  */
+
+static ppc_inferior_data *
+get_ppc_per_inferior (inferior *inf)
+{
+  ppc_inferior_data *per_inf = ppc_inferior_data_key.get (inf);
+
+  if (per_inf == nullptr)
+    per_inf = ppc_inferior_data_key.emplace (inf);
+
+  return per_inf;
+}
+
 /* To be used by skip_prologue.  */
 
 struct rs6000_framedata
@@ -979,6 +1004,53 @@ ppc_displaced_step_fixup (struct gdbarch *gdbarch,
 				    from + offset);
 }
 
+/* Implementation of gdbarch_displaced_step_prepare.  */
+
+static displaced_step_prepare_status
+ppc_displaced_step_prepare  (gdbarch *arch, thread_info *thread,
+			     CORE_ADDR &displaced_pc)
+{
+  ppc_inferior_data *per_inferior = get_ppc_per_inferior (thread->inf);
+
+  if (!per_inferior->disp_step_buf.has_value ())
+    {
+      /* Figure out where the displaced step buffer is.  */
+      CORE_ADDR disp_step_buf_addr
+	= displaced_step_at_entry_point (thread->inf->gdbarch);
+
+      per_inferior->disp_step_buf.emplace (disp_step_buf_addr);
+    }
+
+  return per_inferior->disp_step_buf->prepare (thread, displaced_pc);
+}
+
+/* Implementation of gdbarch_displaced_step_finish.  */
+
+static displaced_step_finish_status
+ppc_displaced_step_finish (gdbarch *arch, thread_info *thread,
+			   gdb_signal sig)
+{
+  ppc_inferior_data *per_inferior = get_ppc_per_inferior (thread->inf);
+
+  gdb_assert (per_inferior->disp_step_buf.has_value ());
+
+  return per_inferior->disp_step_buf->finish (arch, thread, sig);
+}
+
+/* Implementation of gdbarch_displaced_step_restore_all_in_ptid.  */
+
+static void
+ppc_displaced_step_restore_all_in_ptid (inferior *parent_inf, ptid_t ptid)
+{
+  ppc_inferior_data *per_inferior = ppc_inferior_data_key.get (parent_inf);
+
+  if (per_inferior == nullptr
+      || !per_inferior->disp_step_buf.has_value ())
+    return;
+
+  per_inferior->disp_step_buf->restore_in_ptid (ptid);
+}
+
 /* Always use hardware single-stepping to execute the
    displaced instruction.  */
 static bool
@@ -6990,8 +7062,10 @@ rs6000_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
   set_gdbarch_displaced_step_hw_singlestep (gdbarch,
 					    ppc_displaced_step_hw_singlestep);
   set_gdbarch_displaced_step_fixup (gdbarch, ppc_displaced_step_fixup);
-  set_gdbarch_displaced_step_location (gdbarch,
-				       displaced_step_at_entry_point);
+  set_gdbarch_displaced_step_prepare (gdbarch, ppc_displaced_step_prepare);
+  set_gdbarch_displaced_step_finish (gdbarch, ppc_displaced_step_finish);
+  set_gdbarch_displaced_step_restore_all_in_ptid
+    (gdbarch, ppc_displaced_step_restore_all_in_ptid);
 
   set_gdbarch_max_insn_length (gdbarch, PPC_INSN_SIZE);
 
diff --git a/gdb/s390-linux-tdep.c b/gdb/s390-linux-tdep.c
index 8020832d9782..14e92d2c6f33 100644
--- a/gdb/s390-linux-tdep.c
+++ b/gdb/s390-linux-tdep.c
@@ -1119,7 +1119,7 @@ s390_linux_init_abi_any (struct gdbarch_info info, struct gdbarch *gdbarch)
 
   tdep->s390_syscall_record = s390_linux_syscall_record;
 
-  linux_init_abi (info, gdbarch);
+  linux_init_abi (info, gdbarch, false);
 
   /* Register handling.  */
   set_gdbarch_core_read_description (gdbarch, s390_core_read_description);
diff --git a/gdb/s390-tdep.c b/gdb/s390-tdep.c
index 5429d718c3be..76eeda64c7ca 100644
--- a/gdb/s390-tdep.c
+++ b/gdb/s390-tdep.c
@@ -7047,7 +7047,10 @@ s390_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
   set_gdbarch_displaced_step_copy_insn (gdbarch,
 					s390_displaced_step_copy_insn);
   set_gdbarch_displaced_step_fixup (gdbarch, s390_displaced_step_fixup);
-  set_gdbarch_displaced_step_location (gdbarch, linux_displaced_step_location);
+  set_gdbarch_displaced_step_prepare (gdbarch, linux_displaced_step_prepare);
+  set_gdbarch_displaced_step_finish (gdbarch, linux_displaced_step_finish);
+  set_gdbarch_displaced_step_restore_all_in_ptid
+    (gdbarch, linux_displaced_step_restore_all_in_ptid);
   set_gdbarch_displaced_step_hw_singlestep (gdbarch, s390_displaced_step_hw_singlestep);
   set_gdbarch_software_single_step (gdbarch, s390_software_single_step);
   set_gdbarch_max_insn_length (gdbarch, S390_MAX_INSTR_SIZE);
diff --git a/gdb/sh-linux-tdep.c b/gdb/sh-linux-tdep.c
index 91272001561e..b7c66b70294e 100644
--- a/gdb/sh-linux-tdep.c
+++ b/gdb/sh-linux-tdep.c
@@ -184,7 +184,7 @@ static struct tramp_frame sh_linux_rt_sigreturn_tramp_frame = {
 static void
 sh_linux_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
 {
-  linux_init_abi (info, gdbarch);
+  linux_init_abi (info, gdbarch, false);
 
   /* GNU/Linux uses SVR4-style shared libraries.  */
   set_gdbarch_skip_trampoline_code (gdbarch, find_solib_trampoline_target);
diff --git a/gdb/sparc-linux-tdep.c b/gdb/sparc-linux-tdep.c
index 693ca6d8e343..3dbc65ca24df 100644
--- a/gdb/sparc-linux-tdep.c
+++ b/gdb/sparc-linux-tdep.c
@@ -422,7 +422,7 @@ sparc32_linux_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
 {
   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
 
-  linux_init_abi (info, gdbarch);
+  linux_init_abi (info, gdbarch, false);
 
   tdep->gregset = &sparc32_linux_gregset;
   tdep->sizeof_gregset = 152;
diff --git a/gdb/sparc64-linux-tdep.c b/gdb/sparc64-linux-tdep.c
index a7bb64a16737..10a6eb527785 100644
--- a/gdb/sparc64-linux-tdep.c
+++ b/gdb/sparc64-linux-tdep.c
@@ -365,7 +365,7 @@ sparc64_linux_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
 {
   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
 
-  linux_init_abi (info, gdbarch);
+  linux_init_abi (info, gdbarch, false);
 
   tdep->gregset = &sparc64_linux_gregset;
   tdep->sizeof_gregset = 288;
diff --git a/gdb/testsuite/gdb.arch/amd64-disp-step-avx.exp b/gdb/testsuite/gdb.arch/amd64-disp-step-avx.exp
index 5dd827a40ae5..a2cd682d5435 100644
--- a/gdb/testsuite/gdb.arch/amd64-disp-step-avx.exp
+++ b/gdb/testsuite/gdb.arch/amd64-disp-step-avx.exp
@@ -97,7 +97,7 @@ proc disp_step_func { func } {
     gdb_test_no_output "set debug displaced on"
 
     gdb_test "continue" \
-	"Continuing.*displaced pc to.*Breakpoint.*, ${test_end_label} ().*" \
+	"Continuing.*prepared successfully .*Breakpoint.*, ${test_end_label} ().*" \
 	"continue to ${test_end_label}"
 
     gdb_test_no_output "set debug displaced off"
diff --git a/gdb/testsuite/gdb.threads/forking-threads-plus-breakpoint.exp b/gdb/testsuite/gdb.threads/forking-threads-plus-breakpoint.exp
index c4c9596be312..4ce30154bdf8 100644
--- a/gdb/testsuite/gdb.threads/forking-threads-plus-breakpoint.exp
+++ b/gdb/testsuite/gdb.threads/forking-threads-plus-breakpoint.exp
@@ -48,7 +48,7 @@ proc probe_displaced_stepping_support {} {
 	# that breakpoint.
 	gdb_test_no_output "set debug displaced 1"
 	gdb_test_multiple "next" "probe" {
-	    -re "displaced pc to.*$gdb_prompt $" {
+	    -re "prepared successfully .*$gdb_prompt $" {
 		pass "supported"
 	    }
 	    -re ".*$gdb_prompt $" {
diff --git a/gdb/testsuite/gdb.threads/non-stop-fair-events.exp b/gdb/testsuite/gdb.threads/non-stop-fair-events.exp
index bc1351f688e0..117766161500 100644
--- a/gdb/testsuite/gdb.threads/non-stop-fair-events.exp
+++ b/gdb/testsuite/gdb.threads/non-stop-fair-events.exp
@@ -69,7 +69,7 @@ set displaced_stepping_enabled 0
 set msg "check displaced-stepping"
 gdb_test_no_output "set debug displaced 1"
 gdb_test_multiple "next" $msg {
-    -re "displaced pc to.*$gdb_prompt $" {
+    -re "prepared successfully .*$gdb_prompt $" {
 	set displaced_stepping_enabled 1
     }
     -re ".*$gdb_prompt $" {
diff --git a/gdb/thread.c b/gdb/thread.c
index 2bbb016d4d98..856bdee97b38 100644
--- a/gdb/thread.c
+++ b/gdb/thread.c
@@ -362,10 +362,10 @@ step_over_chain_enqueue (struct thread_info **list_p, struct thread_info *tp)
     }
 }
 
-/* Remove TP from step-over chain LIST_P.  */
+/* See gdbthread.h.  */
 
-static void
-thread_step_over_chain_remove (struct thread_info **list_p, struct thread_info *tp)
+void
+thread_step_over_chain_remove (thread_info **list_p, thread_info *tp)
 {
   gdb_assert (tp->step_over_next != NULL);
   gdb_assert (tp->step_over_prev != NULL);
@@ -385,12 +385,20 @@ thread_step_over_chain_remove (struct thread_info **list_p, struct thread_info *
 
 /* See gdbthread.h.  */
 
+thread_info *
+thread_step_over_chain_next (thread_info *chain_head, thread_info *tp)
+{
+  thread_info *next = tp->step_over_next;
+
+  return next == chain_head ? NULL : next;
+}
+
+/* See gdbthread.h.  */
+
 struct thread_info *
 global_thread_step_over_chain_next (struct thread_info *tp)
 {
-  struct thread_info *next = tp->step_over_next;
-
-  return (next == global_thread_step_over_chain_head ? NULL : next);
+  return thread_step_over_chain_next (global_thread_step_over_chain_head, tp);
 }
 
 /* See gdbthread.h.  */
@@ -403,17 +411,65 @@ thread_is_in_step_over_chain (struct thread_info *tp)
 
 /* See gdbthread.h.  */
 
+int
+thread_step_over_chain_length (thread_info *tp)
+{
+  if (tp == nullptr)
+    return 0;
+
+  gdb_assert (thread_is_in_step_over_chain (tp));
+
+  int num = 1;
+
+  for (thread_info *iter = tp->step_over_next;
+       iter != tp;
+       iter = iter->step_over_next)
+    ++num;
+
+  return num;
+}
+
+/* See gdbthread.h.  */
+
 void
 global_thread_step_over_chain_enqueue (struct thread_info *tp)
 {
+  infrun_debug_printf ("enqueueing thread %s in global step over chain",
+		       target_pid_to_str (tp->ptid).c_str ());
+
   step_over_chain_enqueue (&global_thread_step_over_chain_head, tp);
 }
 
 /* See gdbthread.h.  */
 
+void
+global_thread_step_over_chain_enqueue_chain (thread_info *chain_head)
+{
+  gdb_assert (chain_head->step_over_next != nullptr);
+  gdb_assert (chain_head->step_over_prev != nullptr);
+
+  if (global_thread_step_over_chain_head == nullptr)
+    global_thread_step_over_chain_head = chain_head;
+  else
+    {
+      thread_info *global_last = global_thread_step_over_chain_head->step_over_prev;
+      thread_info *chain_last = chain_head->step_over_prev;
+
+      chain_last->step_over_next = global_thread_step_over_chain_head;
+      global_last->step_over_next = chain_head;
+      global_thread_step_over_chain_head->step_over_prev = chain_last;
+      chain_head->step_over_prev = global_last;
+    }
+}
+
+/* See gdbthread.h.  */
+
 void
 global_thread_step_over_chain_remove (struct thread_info *tp)
 {
+  infrun_debug_printf ("removing thread %s from global step over chain",
+		       target_pid_to_str (tp->ptid).c_str ());
+
   thread_step_over_chain_remove (&global_thread_step_over_chain_head, tp);
 }
 
diff --git a/gdb/tic6x-linux-tdep.c b/gdb/tic6x-linux-tdep.c
index 1b626b5a174d..5a8d7c7f8258 100644
--- a/gdb/tic6x-linux-tdep.c
+++ b/gdb/tic6x-linux-tdep.c
@@ -167,7 +167,7 @@ tic6x_uclinux_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
 {
   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
 
-  linux_init_abi (info, gdbarch);
+  linux_init_abi (info, gdbarch, false);
 
   /* Shared library handling.  */
   set_solib_ops (gdbarch, &dsbt_so_ops);
diff --git a/gdb/tilegx-linux-tdep.c b/gdb/tilegx-linux-tdep.c
index 18dfe8892d8a..14cfafe33dec 100644
--- a/gdb/tilegx-linux-tdep.c
+++ b/gdb/tilegx-linux-tdep.c
@@ -111,7 +111,7 @@ tilegx_linux_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
 {
   int arch_size = gdbarch_addr_bit (gdbarch);
 
-  linux_init_abi (info, gdbarch);
+  linux_init_abi (info, gdbarch, false);
 
   tramp_frame_prepend_unwinder (gdbarch, &tilegx_linux_rt_sigframe);
 
diff --git a/gdb/xtensa-linux-tdep.c b/gdb/xtensa-linux-tdep.c
index b419970eceab..c2aeb8e9397b 100644
--- a/gdb/xtensa-linux-tdep.c
+++ b/gdb/xtensa-linux-tdep.c
@@ -110,7 +110,7 @@ xtensa_linux_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
       set_gdbarch_num_pseudo_regs (gdbarch, tdep->num_pseudo_regs);
     }
 
-  linux_init_abi (info, gdbarch);
+  linux_init_abi (info, gdbarch, false);
 
   set_solib_svr4_fetch_link_map_offsets
     (gdbarch, svr4_ilp32_fetch_link_map_offsets);
-- 
2.29.2


^ permalink raw reply	[flat|nested] 40+ messages in thread

* [PATCH v2 12/14] gdb: change linux gdbarch data from post to pre-init
  2020-12-02 15:47 [PATCH v2 00/14] Concurrent displaced stepping Simon Marchi
                   ` (10 preceding siblings ...)
  2020-12-02 15:48 ` [PATCH v2 11/14] gdb: move displaced stepping logic to gdbarch, allow starting concurrent displaced steps Simon Marchi
@ 2020-12-02 15:48 ` Simon Marchi
  2020-12-04  1:50   ` Pedro Alves
  2020-12-02 15:48 ` [PATCH v2 13/14] gdb: make displaced stepping implementation capable of managing multiple buffers Simon Marchi
                   ` (2 subsequent siblings)
  14 siblings, 1 reply; 40+ messages in thread
From: Simon Marchi @ 2020-12-02 15:48 UTC (permalink / raw)
  To: gdb-patches; +Cc: Simon Marchi

From: Simon Marchi <simon.marchi@efficios.com>

The following patch will need to fill a field in linux_gdbarch_data
while the gdbarch is being built.  linux_gdbarch_data is currently
allocated as a post-init gdbarch data, meaning it's not possible to fill
it before the gdbarch is completely initialized.  Change it to a
pre-init gdbarch data to allow this.

The init_linux_gdbarch_data function doesn't use the created gdbarch,
it only allocates the linux_gdbarch_data structure on the gdbarch's
obstack, so the change is trivial.

gdb/ChangeLog:

	* linux-tdep.c (init_linux_gdbarch_data): Change parameter to
	obkstack.
	(_initialize_linux_tdep): Register pre-init gdb data instead of
	post-init.

Change-Id: If35ce91b6bb5435680d43b9268d811d95661644f
---
 gdb/linux-tdep.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/gdb/linux-tdep.c b/gdb/linux-tdep.c
index 70e74d510971..57ea45cf6b6b 100644
--- a/gdb/linux-tdep.c
+++ b/gdb/linux-tdep.c
@@ -171,9 +171,9 @@ struct linux_gdbarch_data
   };
 
 static void *
-init_linux_gdbarch_data (struct gdbarch *gdbarch)
+init_linux_gdbarch_data (struct obstack *obstack)
 {
-  return GDBARCH_OBSTACK_ZALLOC (gdbarch, struct linux_gdbarch_data);
+  return obstack_zalloc<linux_gdbarch_data> (obstack);
 }
 
 static struct linux_gdbarch_data *
@@ -2676,7 +2676,7 @@ void
 _initialize_linux_tdep ()
 {
   linux_gdbarch_data_handle =
-    gdbarch_data_register_post_init (init_linux_gdbarch_data);
+    gdbarch_data_register_pre_init (init_linux_gdbarch_data);
 
   /* Observers used to invalidate the cache when needed.  */
   gdb::observers::inferior_exit.attach (invalidate_linux_cache_inf);
-- 
2.29.2


^ permalink raw reply	[flat|nested] 40+ messages in thread

* [PATCH v2 13/14] gdb: make displaced stepping implementation capable of managing multiple buffers
  2020-12-02 15:47 [PATCH v2 00/14] Concurrent displaced stepping Simon Marchi
                   ` (11 preceding siblings ...)
  2020-12-02 15:48 ` [PATCH v2 12/14] gdb: change linux gdbarch data from post to pre-init Simon Marchi
@ 2020-12-02 15:48 ` Simon Marchi
  2020-12-04  1:51   ` Pedro Alves
  2020-12-02 15:48 ` [PATCH v2 14/14] gdb: use two displaced step buffers on amd64/Linux Simon Marchi
  2020-12-04  1:56 ` [PATCH v2 00/14] Concurrent displaced stepping Pedro Alves
  14 siblings, 1 reply; 40+ messages in thread
From: Simon Marchi @ 2020-12-02 15:48 UTC (permalink / raw)
  To: gdb-patches; +Cc: Simon Marchi

From: Simon Marchi <simon.marchi@efficios.com>

New in v2:

 - call reserve in displaced_step_buffers constructor
 - formatting/comment fixes
 - use explicit on displaced_step_buffer's constructor

The displaced_step_buffer class, introduced in the previous patch,
manages access to a single displaced step buffer.  Change it into
displaced_step_buffers (note the plural), which manages access to
multiple displaced step buffers.

When preparing a displaced step for a thread, it looks for an unused
buffer.

For now, all users still pass a single displaced step buffer, so no real
behavior change is expected here.  The following patch makes a user pass
more than one buffer, so the functionality introduced by this patch is
going to be useful in the next one.

gdb/ChangeLog:

	* displaced-stepping.h (struct displaced_step_buffer): Rename
	to...
	(struct displaced_step_buffers): ... this.
	<m_addr, m_current_thread, m_copy_insn_closure>: Remove.
	<struct displaced_step_buffer>: New inner class.
	<m_buffers>: New.
	* displaced-stepping.c (displaced_step_buffer::prepare): Rename
	to...
	(displaced_step_buffers::prepare): ... this, adjust for multiple
	buffers.
	(displaced_step_buffer::finish):  Rename to...
	(displaced_step_buffers::finish): ... this, adjust for multiple
	buffers.
	(displaced_step_buffer::copy_insn_closure_by_addr): Rename to...
	(displaced_step_buffers::copy_insn_closure_by_addr): ... this,
	adjust for multiple buffers.
	(displaced_step_buffer::restore_in_ptid): Rename to...
	(displaced_step_buffers::restore_in_ptid): ... this, adjust for
	multiple buffers.
	* linux-tdep.h (linux_init_abi): Change supports_displaced_step
	for num_disp_step_buffers.
	* linux-tdep.c (struct linux_gdbarch_data)
	<num_disp_step_buffers>: New field.
	(struct linux_info) <disp_step_buf>: Rename to...
	<disp_step_bufs>: ... this, change type to
	displaced_step_buffers.
	(linux_displaced_step_prepare): Use
	linux_gdbarch_data::num_disp_step_buffers to create that number
	of buffers.
	(linux_displaced_step_finish): Adjust.
	(linux_displaced_step_copy_insn_closure_by_addr): Adjust.
	(linux_displaced_step_restore_all_in_ptid): Adjust.
	(linux_init_abi): Change supports_displaced_step parameter for
	num_disp_step_buffers, save it in linux_gdbarch_data.
	* aarch64-linux-tdep.c (aarch64_linux_init_abi): Adjust.
	* alpha-linux-tdep.c (alpha_linux_init_abi): Adjust.
	* amd64-linux-tdep.c (amd64_linux_init_abi_common): Change
	supports_displaced_step parameter for num_disp_step_buffers.
	(amd64_linux_init_abi): Adjust.
	(amd64_x32_linux_init_abi): Adjust.
	* arc-linux-tdep.c (arc_linux_init_osabi): Adjust.
	* arm-linux-tdep.c (arm_linux_init_abi): Adjust.
	* bfin-linux-tdep.c (bfin_linux_init_abi): Adjust.
	* cris-linux-tdep.c (cris_linux_init_abi): Adjust.
	* csky-linux-tdep.c (csky_linux_init_abi): Adjust.
	* frv-linux-tdep.c (frv_linux_init_abi): Adjust.
	* hppa-linux-tdep.c (hppa_linux_init_abi): Adjust.
	* i386-linux-tdep.c (i386_linux_init_abi): Adjust.
	* ia64-linux-tdep.c (ia64_linux_init_abi): Adjust.
	* m32r-linux-tdep.c (m32r_linux_init_abi): Adjust.
	* m68k-linux-tdep.c (m68k_linux_init_abi):
	* microblaze-linux-tdep.c (microblaze_linux_init_abi):
	* mips-linux-tdep.c (mips_linux_init_abi): Adjust.
	* mn10300-linux-tdep.c (am33_linux_init_osabi): Adjust.
	* nios2-linux-tdep.c (nios2_linux_init_abi): Adjust.
	* or1k-linux-tdep.c (or1k_linux_init_abi): Adjust.
	* ppc-linux-tdep.c (ppc_linux_init_abi): Adjust.
	* riscv-linux-tdep.c (riscv_linux_init_abi): Adjust.
	* rs6000-tdep.c (struct ppc_inferior_data) <disp_step_buf>:
	Change type to displaced_step_buffers.
	* s390-linux-tdep.c (s390_linux_init_abi_any): Adjust.
	* sh-linux-tdep.c (sh_linux_init_abi): Adjust.
	* sparc-linux-tdep.c (sparc32_linux_init_abi): Adjust.
	* sparc64-linux-tdep.c (sparc64_linux_init_abi): Adjust.
	* tic6x-linux-tdep.c (tic6x_uclinux_init_abi): Adjust.
	* tilegx-linux-tdep.c (tilegx_linux_init_abi): Adjust.
	* xtensa-linux-tdep.c (xtensa_linux_init_abi): Adjust.

Change-Id: Ia9c02f207da2c9e1d9188020139619122392bb70
---
 gdb/aarch64-linux-tdep.c    |   2 +-
 gdb/alpha-linux-tdep.c      |   2 +-
 gdb/amd64-linux-tdep.c      |   8 +-
 gdb/arc-linux-tdep.c        |   2 +-
 gdb/arm-linux-tdep.c        |   2 +-
 gdb/bfin-linux-tdep.c       |   2 +-
 gdb/cris-linux-tdep.c       |   2 +-
 gdb/csky-linux-tdep.c       |   2 +-
 gdb/displaced-stepping.c    | 178 ++++++++++++++++++++++++------------
 gdb/displaced-stepping.h    |  53 +++++++----
 gdb/frv-linux-tdep.c        |   2 +-
 gdb/hppa-linux-tdep.c       |   2 +-
 gdb/i386-linux-tdep.c       |   2 +-
 gdb/ia64-linux-tdep.c       |   2 +-
 gdb/linux-tdep.c            |  54 +++++++----
 gdb/linux-tdep.h            |   2 +-
 gdb/m32r-linux-tdep.c       |   2 +-
 gdb/m68k-linux-tdep.c       |   2 +-
 gdb/microblaze-linux-tdep.c |   2 +-
 gdb/mips-linux-tdep.c       |   2 +-
 gdb/mn10300-linux-tdep.c    |   2 +-
 gdb/nios2-linux-tdep.c      |   2 +-
 gdb/or1k-linux-tdep.c       |   2 +-
 gdb/ppc-linux-tdep.c        |   2 +-
 gdb/riscv-linux-tdep.c      |   2 +-
 gdb/rs6000-tdep.c           |   2 +-
 gdb/s390-linux-tdep.c       |   2 +-
 gdb/sh-linux-tdep.c         |   2 +-
 gdb/sparc-linux-tdep.c      |   2 +-
 gdb/sparc64-linux-tdep.c    |   2 +-
 gdb/tic6x-linux-tdep.c      |   2 +-
 gdb/tilegx-linux-tdep.c     |   2 +-
 gdb/xtensa-linux-tdep.c     |   2 +-
 33 files changed, 225 insertions(+), 126 deletions(-)

diff --git a/gdb/aarch64-linux-tdep.c b/gdb/aarch64-linux-tdep.c
index 4fe7babe59cf..ce697ff246e1 100644
--- a/gdb/aarch64-linux-tdep.c
+++ b/gdb/aarch64-linux-tdep.c
@@ -1445,7 +1445,7 @@ aarch64_linux_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
 
   tdep->lowest_pc = 0x8000;
 
-  linux_init_abi (info, gdbarch, true);
+  linux_init_abi (info, gdbarch, 1);
 
   set_solib_svr4_fetch_link_map_offsets (gdbarch,
 					 svr4_lp64_fetch_link_map_offsets);
diff --git a/gdb/alpha-linux-tdep.c b/gdb/alpha-linux-tdep.c
index a6d6b15e9fd6..520dd980d88d 100644
--- a/gdb/alpha-linux-tdep.c
+++ b/gdb/alpha-linux-tdep.c
@@ -356,7 +356,7 @@ alpha_linux_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
 {
   struct gdbarch_tdep *tdep;
 
-  linux_init_abi (info, gdbarch, false);
+  linux_init_abi (info, gdbarch, 0);
 
   /* Hook into the DWARF CFI frame unwinder.  */
   alpha_dwarf2_init_abi (info, gdbarch);
diff --git a/gdb/amd64-linux-tdep.c b/gdb/amd64-linux-tdep.c
index a81bb9039df5..60707ed7aaff 100644
--- a/gdb/amd64-linux-tdep.c
+++ b/gdb/amd64-linux-tdep.c
@@ -1796,11 +1796,11 @@ amd64_dtrace_parse_probe_argument (struct gdbarch *gdbarch,
 
 static void
 amd64_linux_init_abi_common(struct gdbarch_info info, struct gdbarch *gdbarch,
-			    bool supports_displaced_step)
+			    int num_disp_step_buffers)
 {
   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
 
-  linux_init_abi (info, gdbarch, supports_displaced_step);
+  linux_init_abi (info, gdbarch, num_disp_step_buffers);
 
   tdep->sigtramp_p = amd64_linux_sigtramp_p;
   tdep->sigcontext_addr = amd64_linux_sigcontext_addr;
@@ -1880,7 +1880,7 @@ amd64_linux_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
   if (!valid_p)
     return;
 
-  amd64_linux_init_abi_common (info, gdbarch, true);
+  amd64_linux_init_abi_common (info, gdbarch, 1);
 
   /* Initialize the amd64_linux_record_tdep.  */
   /* These values are the size of the type that will be used in a system
@@ -2095,7 +2095,7 @@ amd64_x32_linux_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
   if (!valid_p)
     return;
 
-  amd64_linux_init_abi_common (info, gdbarch, false);
+  amd64_linux_init_abi_common (info, gdbarch, 0);
 
   /* Initialize the amd64_x32_linux_record_tdep.  */
   /* These values are the size of the type that will be used in a system
diff --git a/gdb/arc-linux-tdep.c b/gdb/arc-linux-tdep.c
index b919882177e6..2bdeaaf0614c 100644
--- a/gdb/arc-linux-tdep.c
+++ b/gdb/arc-linux-tdep.c
@@ -439,7 +439,7 @@ arc_linux_init_osabi (struct gdbarch_info info, struct gdbarch *gdbarch)
    */
   tdep->jb_pc = 15;
 
-  linux_init_abi (info, gdbarch, false);
+  linux_init_abi (info, gdbarch, 0);
 
   /* Set up target dependent GDB architecture entries.  */
   set_gdbarch_cannot_fetch_register (gdbarch, arc_linux_cannot_fetch_register);
diff --git a/gdb/arm-linux-tdep.c b/gdb/arm-linux-tdep.c
index d164cff3dff7..11e71e56b46c 100644
--- a/gdb/arm-linux-tdep.c
+++ b/gdb/arm-linux-tdep.c
@@ -1721,7 +1721,7 @@ arm_linux_init_abi (struct gdbarch_info info,
 								    NULL };
   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
 
-  linux_init_abi (info, gdbarch, true);
+  linux_init_abi (info, gdbarch, 1);
 
   tdep->lowest_pc = 0x8000;
   if (info.byte_order_for_code == BFD_ENDIAN_BIG)
diff --git a/gdb/bfin-linux-tdep.c b/gdb/bfin-linux-tdep.c
index fc2f1d9ac656..bc2bb1aad453 100644
--- a/gdb/bfin-linux-tdep.c
+++ b/gdb/bfin-linux-tdep.c
@@ -150,7 +150,7 @@ bfin_linux_get_syscall_number (struct gdbarch *gdbarch,
 static void
 bfin_linux_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
 {
-  linux_init_abi (info, gdbarch, false);
+  linux_init_abi (info, gdbarch, 0);
 
   /* Set the sigtramp frame sniffer.  */
   tramp_frame_prepend_unwinder (gdbarch, &bfin_linux_sigframe);
diff --git a/gdb/cris-linux-tdep.c b/gdb/cris-linux-tdep.c
index 85cbf4cc0931..4ae1cdd1390a 100644
--- a/gdb/cris-linux-tdep.c
+++ b/gdb/cris-linux-tdep.c
@@ -35,7 +35,7 @@ cris_linux_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
 {
   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
 
-  linux_init_abi (info, gdbarch, false);
+  linux_init_abi (info, gdbarch, 0);
 
   if (tdep->cris_version == 32)
     /* Threaded debugging is only supported on CRISv32 for now.  */
diff --git a/gdb/csky-linux-tdep.c b/gdb/csky-linux-tdep.c
index 184fa5ffb239..a0d32b5f2d0c 100644
--- a/gdb/csky-linux-tdep.c
+++ b/gdb/csky-linux-tdep.c
@@ -233,7 +233,7 @@ csky_linux_rt_sigreturn_tramp_frame = {
 static void
 csky_linux_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
 {
-  linux_init_abi (info, gdbarch, false);
+  linux_init_abi (info, gdbarch, 0);
 
   /* Shared library handling.  */
   set_gdbarch_skip_trampoline_code (gdbarch, find_solib_trampoline_target);
diff --git a/gdb/displaced-stepping.c b/gdb/displaced-stepping.c
index 984d21eedb93..5564a1b33ca2 100644
--- a/gdb/displaced-stepping.c
+++ b/gdb/displaced-stepping.c
@@ -44,82 +44,121 @@ show_debug_displaced (struct ui_file *file, int from_tty,
 }
 
 displaced_step_prepare_status
-displaced_step_buffer::prepare (thread_info *thread, CORE_ADDR &displaced_pc)
+displaced_step_buffers::prepare (thread_info *thread, CORE_ADDR &displaced_pc)
 {
   gdb_assert (!thread->displaced_step_state.in_progress ());
 
-  /* Is a thread currently using the buffer?  */
-  if (m_current_thread != nullptr)
-    {
-      /* If so, it better not be this thread.  */
-      gdb_assert (thread != m_current_thread);
-      return DISPLACED_STEP_PREPARE_STATUS_UNAVAILABLE;
-    }
+  /* Sanity check: the thread should not be using a buffer at this point.  */
+  for (displaced_step_buffer &buf : m_buffers)
+    gdb_assert (buf.current_thread != thread);
 
   regcache *regcache = get_thread_regcache (thread);
   const address_space *aspace = regcache->aspace ();
   gdbarch *arch = regcache->arch ();
   ULONGEST len = gdbarch_max_insn_length (arch);
 
-  if (breakpoint_in_range_p (aspace, m_addr, len))
-    {
-      /* There's a breakpoint set in the scratch pad location range
-	 (which is usually around the entry point).  We'd either
-	 install it before resuming, which would overwrite/corrupt the
-	 scratch pad, or if it was already inserted, this displaced
-	 step would overwrite it.  The latter is OK in the sense that
-	 we already assume that no thread is going to execute the code
-	 in the scratch pad range (after initial startup) anyway, but
-	 the former is unacceptable.  Simply punt and fallback to
-	 stepping over this breakpoint in-line.  */
-      displaced_debug_printf ("breakpoint set in scratch pad.  "
-			      "Stepping over breakpoint in-line instead.");
+  /* Search for an unused buffer.  */
+  displaced_step_buffer *buffer = nullptr;
+  displaced_step_prepare_status fail_status
+    = DISPLACED_STEP_PREPARE_STATUS_CANT;
 
-      return DISPLACED_STEP_PREPARE_STATUS_CANT;
+  for (displaced_step_buffer &candidate : m_buffers)
+    {
+      bool bp_in_range = breakpoint_in_range_p (aspace, candidate.addr, len);
+      bool is_free = candidate.current_thread == nullptr;
+
+      if (!bp_in_range)
+	{
+	  if (is_free)
+	    {
+	      buffer = &candidate;
+	      break;
+	    }
+	  else
+	    {
+	      /* This buffer would be suitable, but it's used right now.  */
+	      fail_status = DISPLACED_STEP_PREPARE_STATUS_UNAVAILABLE;
+	    }
+	}
+      else
+	{
+	  /* There's a breakpoint set in the scratch pad location range
+	     (which is usually around the entry point).  We'd either
+	     install it before resuming, which would overwrite/corrupt the
+	     scratch pad, or if it was already inserted, this displaced
+	     step would overwrite it.  The latter is OK in the sense that
+	     we already assume that no thread is going to execute the code
+	     in the scratch pad range (after initial startup) anyway, but
+	     the former is unacceptable.  Simply punt and fallback to
+	     stepping over this breakpoint in-line.  */
+	  displaced_debug_printf ("breakpoint set in displaced stepping "
+				  "buffer at %s, can't use.",
+				  paddress (arch, candidate.addr));
+	}
     }
 
-  m_original_pc = regcache_read_pc (regcache);
-  displaced_pc = m_addr;
+  if (buffer == nullptr)
+    return fail_status;
+
+  displaced_debug_printf ("selected buffer at %s",
+			  paddress (arch, buffer->addr));
+
+  /* Save the original PC of the thread.  */
+  buffer->original_pc = regcache_read_pc (regcache);
+
+  /* Return displaced step buffer address to caller.  */
+  displaced_pc = buffer->addr;
 
   /* Save the original contents of the displaced stepping buffer.  */
-  m_saved_copy.resize (len);
+  buffer->saved_copy.resize (len);
 
-  int status = target_read_memory (m_addr, m_saved_copy.data (), len);
+  int status = target_read_memory (buffer->addr,
+				    buffer->saved_copy.data (), len);
   if (status != 0)
     throw_error (MEMORY_ERROR,
 		 _("Error accessing memory address %s (%s) for "
 		   "displaced-stepping scratch space."),
-		 paddress (arch, m_addr), safe_strerror (status));
+		 paddress (arch, buffer->addr), safe_strerror (status));
 
   displaced_debug_printf ("saved %s: %s",
-			  paddress (arch, m_addr),
+			  paddress (arch, buffer->addr),
 			  displaced_step_dump_bytes
-			    (m_saved_copy.data (), len).c_str ());
+			  (buffer->saved_copy.data (), len).c_str ());
 
   /* Save this in a local variable first, so it's released if code below
      throws.  */
   displaced_step_copy_insn_closure_up copy_insn_closure
-    = gdbarch_displaced_step_copy_insn (arch, m_original_pc, m_addr, regcache);
+    = gdbarch_displaced_step_copy_insn (arch, buffer->original_pc,
+					buffer->addr, regcache);
 
   if (copy_insn_closure == nullptr)
     {
       /* The architecture doesn't know how or want to displaced step
-        this instruction or instruction sequence.  Fallback to
-        stepping over the breakpoint in-line.  */
+	 this instruction or instruction sequence.  Fallback to
+	 stepping over the breakpoint in-line.  */
       return DISPLACED_STEP_PREPARE_STATUS_CANT;
     }
 
   /* Resume execution at the copy.  */
-  regcache_write_pc (regcache, m_addr);
+  regcache_write_pc (regcache, buffer->addr);
 
   /* This marks the buffer as being in use.  */
-  m_current_thread = thread;
+  buffer->current_thread = thread;
 
   /* Save this, now that we know everything went fine.  */
-  m_copy_insn_closure = std::move (copy_insn_closure);
+  buffer->copy_insn_closure = std::move (copy_insn_closure);
 
-  /* Tell GDB not to try preparing a displaced step again for this inferior.  */
+  /* Tell GDB not to try preparing a displaced step again for this inferior if
+     all buffers are taken.  */
   thread->inf->displaced_step_state.unavailable = true;
+  for (const displaced_step_buffer &buf : m_buffers)
+    {
+      if (buf.current_thread == nullptr)
+	{
+	  thread->inf->displaced_step_state.unavailable = false;
+	  break;
+	}
+    }
 
   return DISPLACED_STEP_PREPARE_STATUS_OK;
 }
@@ -152,21 +191,34 @@ displaced_step_instruction_executed_successfully (gdbarch *arch,
 }
 
 displaced_step_finish_status
-displaced_step_buffer::finish (gdbarch *arch, thread_info *thread,
-			       gdb_signal sig)
+displaced_step_buffers::finish (gdbarch *arch, thread_info *thread,
+				gdb_signal sig)
 {
   gdb_assert (thread->displaced_step_state.in_progress ());
-  gdb_assert (thread == m_current_thread);
+
+  /* Find the buffer this thread was using.  */
+  displaced_step_buffer *buffer = nullptr;
+
+  for (displaced_step_buffer &candidate : m_buffers)
+    {
+      if (candidate.current_thread == thread)
+	{
+	  buffer = &candidate;
+	  break;
+	}
+    }
+
+  gdb_assert (buffer != nullptr);
 
   /* Move this to a local variable so it's released in case something goes
      wrong.  */
   displaced_step_copy_insn_closure_up copy_insn_closure
-    = std::move (m_copy_insn_closure);
+    = std::move (buffer->copy_insn_closure);
   gdb_assert (copy_insn_closure != nullptr);
 
-  /* Reset m_current_thread immediately to mark the buffer as available, in case
-     something goes wrong below.  */
-  m_current_thread = nullptr;
+  /* Reset buffer->current_thread immediately to mark the buffer as available,
+     in case something goes wrong below.  */
+  buffer->current_thread = nullptr;
 
   /* Now that a buffer gets freed, tell GDB it can ask us to prepare a displaced
      step again for this inferior.  Do that here in case something goes wrong
@@ -175,12 +227,13 @@ displaced_step_buffer::finish (gdbarch *arch, thread_info *thread,
 
   ULONGEST len = gdbarch_max_insn_length (arch);
 
-  write_memory_ptid (thread->ptid, m_addr,
-		     m_saved_copy.data (), len);
+  /* Restore memory of the buffer.  */
+  write_memory_ptid (thread->ptid, buffer->addr,
+		     buffer->saved_copy.data (), len);
 
   displaced_debug_printf ("restored %s %s",
 			  target_pid_to_str (thread->ptid).c_str (),
-			  paddress (arch, m_addr));
+			  paddress (arch, buffer->addr));
 
   regcache *rc = get_thread_regcache (thread);
 
@@ -189,8 +242,9 @@ displaced_step_buffer::finish (gdbarch *arch, thread_info *thread,
 
   if (instruction_executed_successfully)
     {
-      gdbarch_displaced_step_fixup (arch, copy_insn_closure.get (), m_original_pc,
-				    m_addr, rc);
+      gdbarch_displaced_step_fixup (arch, copy_insn_closure.get (),
+				    buffer->original_pc,
+				    buffer->addr, rc);
       return DISPLACED_STEP_FINISH_STATUS_OK;
     }
   else
@@ -198,35 +252,41 @@ displaced_step_buffer::finish (gdbarch *arch, thread_info *thread,
       /* Since the instruction didn't complete, all we can do is relocate the
 	 PC.  */
       CORE_ADDR pc = regcache_read_pc (rc);
-      pc = m_original_pc + (pc - m_addr);
+      pc = buffer->original_pc + (pc - buffer->addr);
       regcache_write_pc (rc, pc);
       return DISPLACED_STEP_FINISH_STATUS_NOT_EXECUTED;
     }
 }
 
 const displaced_step_copy_insn_closure *
-displaced_step_buffer::copy_insn_closure_by_addr (CORE_ADDR addr)
+displaced_step_buffers::copy_insn_closure_by_addr (CORE_ADDR addr)
 {
-  if (addr == m_addr)
-    return m_copy_insn_closure.get ();
-  else
-    return nullptr;
+  for (const displaced_step_buffer &buffer : m_buffers)
+    {
+      if (addr == buffer.addr)
+	return buffer.copy_insn_closure.get ();
+    }
+
+  return nullptr;
 }
 
 void
-displaced_step_buffer::restore_in_ptid (ptid_t ptid)
+displaced_step_buffers::restore_in_ptid (ptid_t ptid)
 {
-  if (m_current_thread != nullptr)
+  for (const displaced_step_buffer &buffer : m_buffers)
     {
-      regcache *regcache = get_thread_regcache (m_current_thread);
+      if (buffer.current_thread == nullptr)
+	continue;
+
+      regcache *regcache = get_thread_regcache (buffer.current_thread);
       gdbarch *arch = regcache->arch ();
       ULONGEST len = gdbarch_max_insn_length (arch);
 
-      write_memory_ptid (ptid, m_addr, m_saved_copy.data (), len);
+      write_memory_ptid (ptid, buffer.addr, buffer.saved_copy.data (), len);
 
       displaced_debug_printf ("restored in ptid %s %s",
 			      target_pid_to_str (ptid).c_str (),
-			      paddress (arch, m_addr));
+			      paddress (arch, buffer.addr));
     }
 }
 
diff --git a/gdb/displaced-stepping.h b/gdb/displaced-stepping.h
index 8ed32852fe5a..c0d727d6b1c7 100644
--- a/gdb/displaced-stepping.h
+++ b/gdb/displaced-stepping.h
@@ -20,6 +20,7 @@
 #ifndef DISPLACED_STEPPING_H
 #define DISPLACED_STEPPING_H
 
+#include "gdbsupport/array-view.h"
 #include "gdbsupport/byte-vector.h"
 
 struct gdbarch;
@@ -154,13 +155,19 @@ struct displaced_step_thread_state
   gdbarch *m_original_gdbarch = nullptr;
 };
 
-/* Manage access to a single displaced stepping buffer.  */
+/* Control access to multiple displaced stepping buffers at fixed addresses.  */
 
-struct displaced_step_buffer
+struct displaced_step_buffers
 {
-  explicit displaced_step_buffer (CORE_ADDR buffer_addr)
-    : m_addr (buffer_addr)
-  {}
+  explicit displaced_step_buffers (gdb::array_view<CORE_ADDR> buffer_addrs)
+  {
+    gdb_assert (buffer_addrs.size () > 0);
+
+    m_buffers.reserve (buffer_addrs.size ());
+
+    for (CORE_ADDR buffer_addr : buffer_addrs)
+      m_buffers.emplace_back (buffer_addr);
+  }
 
   displaced_step_prepare_status prepare (thread_info *thread,
 					 CORE_ADDR &displaced_pc);
@@ -174,21 +181,35 @@ struct displaced_step_buffer
   void restore_in_ptid (ptid_t ptid);
 
 private:
-  /* Original PC of the instruction being displaced-stepped in this buffer.  */
-  CORE_ADDR m_original_pc = 0;
 
-  /* Address of the buffer.  */
-  const CORE_ADDR m_addr;
+  /* State of a single buffer.  */
+
+  struct displaced_step_buffer
+  {
+    explicit displaced_step_buffer (CORE_ADDR addr)
+      : addr (addr)
+    {}
+
+    /* Address of the buffer.  */
+    const CORE_ADDR addr;
+
+    /* The original PC of the instruction currently being stepped.  */
+    CORE_ADDR original_pc = 0;
+
+    /* If set, the thread currently using the buffer.  If unset, the buffer is not
+       used.  */
+    thread_info *current_thread = nullptr;
 
-  /* If set, the thread currently using the buffer.  */
-  thread_info *m_current_thread = nullptr;
+    /* Saved copy of the bytes in the displaced buffer, to be restored once the
+       buffer is no longer used.  */
+    gdb::byte_vector saved_copy;
 
-  /* Saved contents of copy area.  */
-  gdb::byte_vector m_saved_copy;
+    /* Closure obtained from gdbarch_displaced_step_copy_insn, to be passed to
+       gdbarch_displaced_step_fixup_insn.  */
+    displaced_step_copy_insn_closure_up copy_insn_closure;
+  };
 
-  /* The closure provided gdbarch_displaced_step_copy_insn, to be used
-     for post-step cleanup.  */
-  displaced_step_copy_insn_closure_up m_copy_insn_closure;
+  std::vector<displaced_step_buffer> m_buffers;
 };
 
 #endif /* DISPLACED_STEPPING_H */
diff --git a/gdb/frv-linux-tdep.c b/gdb/frv-linux-tdep.c
index c5ae4212ab44..c5b20737654f 100644
--- a/gdb/frv-linux-tdep.c
+++ b/gdb/frv-linux-tdep.c
@@ -456,7 +456,7 @@ frv_linux_iterate_over_regset_sections (struct gdbarch *gdbarch,
 static void
 frv_linux_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
 {
-  linux_init_abi (info, gdbarch, false);
+  linux_init_abi (info, gdbarch, 0);
 
   /* Set the sigtramp frame sniffer.  */
   frame_unwind_append_unwinder (gdbarch, &frv_linux_sigtramp_frame_unwind); 
diff --git a/gdb/hppa-linux-tdep.c b/gdb/hppa-linux-tdep.c
index a171e582e595..ce85f327d1e4 100644
--- a/gdb/hppa-linux-tdep.c
+++ b/gdb/hppa-linux-tdep.c
@@ -489,7 +489,7 @@ hppa_linux_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
 {
   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
 
-  linux_init_abi (info, gdbarch, false);
+  linux_init_abi (info, gdbarch, 0);
 
   /* GNU/Linux is always ELF.  */
   tdep->is_elf = 1;
diff --git a/gdb/i386-linux-tdep.c b/gdb/i386-linux-tdep.c
index 1b209fd3efff..90ee30969aa1 100644
--- a/gdb/i386-linux-tdep.c
+++ b/gdb/i386-linux-tdep.c
@@ -832,7 +832,7 @@ i386_linux_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
 
   gdb_assert (tdesc_data);
 
-  linux_init_abi (info, gdbarch, true);
+  linux_init_abi (info, gdbarch, 1);
 
   /* GNU/Linux uses ELF.  */
   i386_elf_init_abi (info, gdbarch);
diff --git a/gdb/ia64-linux-tdep.c b/gdb/ia64-linux-tdep.c
index d6d581ab3dda..3a69f758c11f 100644
--- a/gdb/ia64-linux-tdep.c
+++ b/gdb/ia64-linux-tdep.c
@@ -223,7 +223,7 @@ ia64_linux_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
   static const char *const stap_register_indirection_suffixes[] = { "]",
 								    NULL };
 
-  linux_init_abi (info, gdbarch, false);
+  linux_init_abi (info, gdbarch, 0);
 
   /* Set the method of obtaining the sigcontext addresses at which
      registers are saved.  */
diff --git a/gdb/linux-tdep.c b/gdb/linux-tdep.c
index 57ea45cf6b6b..5ae28d826c1a 100644
--- a/gdb/linux-tdep.c
+++ b/gdb/linux-tdep.c
@@ -166,9 +166,10 @@ enum
 static struct gdbarch_data *linux_gdbarch_data_handle;
 
 struct linux_gdbarch_data
-  {
-    struct type *siginfo_type;
-  };
+{
+  struct type *siginfo_type;
+  int num_disp_step_buffers;
+};
 
 static void *
 init_linux_gdbarch_data (struct obstack *obstack)
@@ -200,8 +201,8 @@ struct linux_info
      if we tried looking it up but failed.  */
   int vsyscall_range_p = 0;
 
-  /* Inferior's displaced step buffer.  */
-  gdb::optional<displaced_step_buffer> disp_step_buf;
+  /* Inferior's displaced step buffers.  */
+  gdb::optional<displaced_step_buffers> disp_step_bufs;
 };
 
 /* Per-inferior data key.  */
@@ -2540,15 +2541,25 @@ linux_displaced_step_prepare (gdbarch *arch, thread_info *thread,
 {
   linux_info *per_inferior = get_linux_inferior_data (thread->inf);
 
-  if (!per_inferior->disp_step_buf.has_value ())
+  if (!per_inferior->disp_step_bufs.has_value ())
     {
+      /* Figure out the location of the buffers.  They are contiguous, starting
+	 at DISP_STEP_BUF_ADDR.  They are all of size BUF_LEN.  */
       CORE_ADDR disp_step_buf_addr
 	= linux_displaced_step_location (thread->inf->gdbarch);
+      int buf_len = gdbarch_max_insn_length (arch);
 
-      per_inferior->disp_step_buf.emplace (disp_step_buf_addr);
+      linux_gdbarch_data *gdbarch_data = get_linux_gdbarch_data (arch);
+      gdb_assert (gdbarch_data->num_disp_step_buffers > 0);
+
+      std::vector<CORE_ADDR> buffers;
+      for (int i = 0; i < gdbarch_data->num_disp_step_buffers; i++)
+	buffers.push_back (disp_step_buf_addr + i * buf_len);
+
+      per_inferior->disp_step_bufs.emplace (buffers);
     }
 
-  return per_inferior->disp_step_buf->prepare (thread, displaced_pc);
+  return per_inferior->disp_step_bufs->prepare (thread, displaced_pc);
 }
 
 /* See linux-tdep.h.  */
@@ -2558,9 +2569,9 @@ linux_displaced_step_finish (gdbarch *arch, thread_info *thread, gdb_signal sig)
 {
   linux_info *per_inferior = get_linux_inferior_data (thread->inf);
 
-  gdb_assert (per_inferior->disp_step_buf.has_value ());
+  gdb_assert (per_inferior->disp_step_bufs.has_value ());
 
-  return per_inferior->disp_step_buf->finish (arch, thread, sig);
+  return per_inferior->disp_step_bufs->finish (arch, thread, sig);
 }
 
 /* See linux-tdep.h.  */
@@ -2571,10 +2582,10 @@ linux_displaced_step_copy_insn_closure_by_addr (inferior *inf, CORE_ADDR addr)
   linux_info *per_inferior = linux_inferior_data.get (inf);
 
   if (per_inferior == nullptr
-      || !per_inferior->disp_step_buf.has_value ())
+      || !per_inferior->disp_step_bufs.has_value ())
     return nullptr;
 
-  return per_inferior->disp_step_buf->copy_insn_closure_by_addr (addr);
+  return per_inferior->disp_step_bufs->copy_insn_closure_by_addr (addr);
 }
 
 /* See linux-tdep.h.  */
@@ -2585,10 +2596,10 @@ linux_displaced_step_restore_all_in_ptid (inferior *parent_inf, ptid_t ptid)
   linux_info *per_inferior = linux_inferior_data.get (parent_inf);
 
   if (per_inferior == nullptr
-      || !per_inferior->disp_step_buf.has_value ())
+      || !per_inferior->disp_step_bufs.has_value ())
     return;
 
-  per_inferior->disp_step_buf->restore_in_ptid (ptid);
+  per_inferior->disp_step_bufs->restore_in_ptid (ptid);
 }
 
 /* See linux-tdep.h.  */
@@ -2636,15 +2647,22 @@ show_dump_excluded_mappings (struct ui_file *file, int from_tty,
 }
 
 /* To be called from the various GDB_OSABI_LINUX handlers for the
-   various GNU/Linux architectures and machine types.  */
+   various GNU/Linux architectures and machine types.
+
+   NUM_DISP_STEP_BUFFERS is the number of displaced step buffers to use.  If 0,
+   displaced stepping is not supported. */
 
 void
 linux_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch,
-		bool supports_displaced_step)
+		int num_disp_step_buffers)
 {
-  if (supports_displaced_step)
+  if (num_disp_step_buffers > 0)
     {
-      set_gdbarch_displaced_step_prepare (gdbarch, linux_displaced_step_prepare);
+      linux_gdbarch_data *gdbarch_data = get_linux_gdbarch_data (gdbarch);
+      gdbarch_data->num_disp_step_buffers = num_disp_step_buffers;
+
+      set_gdbarch_displaced_step_prepare (gdbarch,
+					  linux_displaced_step_prepare);
       set_gdbarch_displaced_step_finish (gdbarch, linux_displaced_step_finish);
       set_gdbarch_displaced_step_copy_insn_closure_by_addr
 	(gdbarch, linux_displaced_step_copy_insn_closure_by_addr);
diff --git a/gdb/linux-tdep.h b/gdb/linux-tdep.h
index 0f83dc3c7811..723eec3dc10b 100644
--- a/gdb/linux-tdep.h
+++ b/gdb/linux-tdep.h
@@ -82,7 +82,7 @@ extern void linux_displaced_step_restore_all_in_ptid (inferior *parent_inf,
 						      ptid_t ptid);
 
 extern void linux_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch,
-			    bool supports_displaced_step);
+			    int num_disp_step_buffers);
 
 extern int linux_is_uclinux (void);
 
diff --git a/gdb/m32r-linux-tdep.c b/gdb/m32r-linux-tdep.c
index 961d54a2ca26..0a1ff780d8c5 100644
--- a/gdb/m32r-linux-tdep.c
+++ b/gdb/m32r-linux-tdep.c
@@ -449,7 +449,7 @@ static void
 m32r_linux_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
 {
 
-  linux_init_abi (info, gdbarch, false);
+  linux_init_abi (info, gdbarch, 0);
 
   /* Since EVB register is not available for native debug, we reduce
      the number of registers.  */
diff --git a/gdb/m68k-linux-tdep.c b/gdb/m68k-linux-tdep.c
index 509333558ec9..f057915965f9 100644
--- a/gdb/m68k-linux-tdep.c
+++ b/gdb/m68k-linux-tdep.c
@@ -385,7 +385,7 @@ m68k_linux_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
 {
   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
 
-  linux_init_abi (info, gdbarch, false);
+  linux_init_abi (info, gdbarch, 0);
 
   tdep->jb_pc = M68K_LINUX_JB_PC;
   tdep->jb_elt_size = M68K_LINUX_JB_ELEMENT_SIZE;
diff --git a/gdb/microblaze-linux-tdep.c b/gdb/microblaze-linux-tdep.c
index 2a91e1bb39a8..54f89ec03777 100644
--- a/gdb/microblaze-linux-tdep.c
+++ b/gdb/microblaze-linux-tdep.c
@@ -117,7 +117,7 @@ static void
 microblaze_linux_init_abi (struct gdbarch_info info,
 			   struct gdbarch *gdbarch)
 {
-  linux_init_abi (info, gdbarch, false);
+  linux_init_abi (info, gdbarch, 0);
 
   set_gdbarch_memory_remove_breakpoint (gdbarch,
 					microblaze_linux_memory_remove_breakpoint);
diff --git a/gdb/mips-linux-tdep.c b/gdb/mips-linux-tdep.c
index 9ca59e5b296b..ed95c2200841 100644
--- a/gdb/mips-linux-tdep.c
+++ b/gdb/mips-linux-tdep.c
@@ -1531,7 +1531,7 @@ mips_linux_init_abi (struct gdbarch_info info,
   enum mips_abi abi = mips_abi (gdbarch);
   struct tdesc_arch_data *tdesc_data = info.tdesc_data;
 
-  linux_init_abi (info, gdbarch, false);
+  linux_init_abi (info, gdbarch, 0);
 
   /* Get the syscall number from the arch's register.  */
   set_gdbarch_get_syscall_number (gdbarch, mips_linux_get_syscall_number);
diff --git a/gdb/mn10300-linux-tdep.c b/gdb/mn10300-linux-tdep.c
index 27645b1260c8..f7586bf3eae9 100644
--- a/gdb/mn10300-linux-tdep.c
+++ b/gdb/mn10300-linux-tdep.c
@@ -704,7 +704,7 @@ am33_linux_sigframe_cache_init (const struct tramp_frame *self,
 static void
 am33_linux_init_osabi (struct gdbarch_info info, struct gdbarch *gdbarch)
 {
-  linux_init_abi (info, gdbarch, false);
+  linux_init_abi (info, gdbarch, 0);
 
   set_gdbarch_iterate_over_regset_sections
     (gdbarch, am33_iterate_over_regset_sections);
diff --git a/gdb/nios2-linux-tdep.c b/gdb/nios2-linux-tdep.c
index b5c12852c705..0a28c9cca6d6 100644
--- a/gdb/nios2-linux-tdep.c
+++ b/gdb/nios2-linux-tdep.c
@@ -219,7 +219,7 @@ nios2_linux_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
 {
   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
 
-  linux_init_abi (info, gdbarch, false);
+  linux_init_abi (info, gdbarch, 0);
 
   /* Shared library handling.  */
   set_gdbarch_skip_trampoline_code (gdbarch, find_solib_trampoline_target);
diff --git a/gdb/or1k-linux-tdep.c b/gdb/or1k-linux-tdep.c
index 33ddd10e85f3..2779fd0d1e61 100644
--- a/gdb/or1k-linux-tdep.c
+++ b/gdb/or1k-linux-tdep.c
@@ -140,7 +140,7 @@ or1k_linux_sigframe_init (const struct tramp_frame *self,
 static void
 or1k_linux_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
 {
-  linux_init_abi (info, gdbarch, false);
+  linux_init_abi (info, gdbarch, 0);
 
   set_solib_svr4_fetch_link_map_offsets (gdbarch,
 					 svr4_ilp32_fetch_link_map_offsets);
diff --git a/gdb/ppc-linux-tdep.c b/gdb/ppc-linux-tdep.c
index e6c10dd83db5..57bdd2d7a723 100644
--- a/gdb/ppc-linux-tdep.c
+++ b/gdb/ppc-linux-tdep.c
@@ -1993,7 +1993,7 @@ ppc_linux_init_abi (struct gdbarch_info info,
   static const char *const stap_register_indirection_suffixes[] = { ")",
 								    NULL };
 
-  linux_init_abi (info, gdbarch, false);
+  linux_init_abi (info, gdbarch, 0);
 
   /* PPC GNU/Linux uses either 64-bit or 128-bit long doubles; where
      128-bit, they can be either IBM long double or IEEE quad long double.
diff --git a/gdb/riscv-linux-tdep.c b/gdb/riscv-linux-tdep.c
index a2238ad786eb..623c7d9382cc 100644
--- a/gdb/riscv-linux-tdep.c
+++ b/gdb/riscv-linux-tdep.c
@@ -159,7 +159,7 @@ riscv_linux_sigframe_init (const struct tramp_frame *self,
 static void
 riscv_linux_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
 {
-  linux_init_abi (info, gdbarch, false);
+  linux_init_abi (info, gdbarch, 0);
 
   set_gdbarch_software_single_step (gdbarch, riscv_software_single_step);
 
diff --git a/gdb/rs6000-tdep.c b/gdb/rs6000-tdep.c
index 1a7c7fcb9f16..235abd873f72 100644
--- a/gdb/rs6000-tdep.c
+++ b/gdb/rs6000-tdep.c
@@ -160,7 +160,7 @@ struct ppc_inferior_data
   /* This is an optional in case we add more fields to ppc_inferior_data, we
      don't want it instantiated as soon as we get the ppc_inferior_data for an
      inferior.  */
-  gdb::optional<displaced_step_buffer> disp_step_buf;
+  gdb::optional<displaced_step_buffers> disp_step_buf;
 };
 
 static inferior_key<ppc_inferior_data> ppc_inferior_data_key;
diff --git a/gdb/s390-linux-tdep.c b/gdb/s390-linux-tdep.c
index 14e92d2c6f33..8588d046bd6b 100644
--- a/gdb/s390-linux-tdep.c
+++ b/gdb/s390-linux-tdep.c
@@ -1119,7 +1119,7 @@ s390_linux_init_abi_any (struct gdbarch_info info, struct gdbarch *gdbarch)
 
   tdep->s390_syscall_record = s390_linux_syscall_record;
 
-  linux_init_abi (info, gdbarch, false);
+  linux_init_abi (info, gdbarch, 0);
 
   /* Register handling.  */
   set_gdbarch_core_read_description (gdbarch, s390_core_read_description);
diff --git a/gdb/sh-linux-tdep.c b/gdb/sh-linux-tdep.c
index b7c66b70294e..84bcd3000694 100644
--- a/gdb/sh-linux-tdep.c
+++ b/gdb/sh-linux-tdep.c
@@ -184,7 +184,7 @@ static struct tramp_frame sh_linux_rt_sigreturn_tramp_frame = {
 static void
 sh_linux_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
 {
-  linux_init_abi (info, gdbarch, false);
+  linux_init_abi (info, gdbarch, 0);
 
   /* GNU/Linux uses SVR4-style shared libraries.  */
   set_gdbarch_skip_trampoline_code (gdbarch, find_solib_trampoline_target);
diff --git a/gdb/sparc-linux-tdep.c b/gdb/sparc-linux-tdep.c
index 3dbc65ca24df..71759e19fa82 100644
--- a/gdb/sparc-linux-tdep.c
+++ b/gdb/sparc-linux-tdep.c
@@ -422,7 +422,7 @@ sparc32_linux_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
 {
   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
 
-  linux_init_abi (info, gdbarch, false);
+  linux_init_abi (info, gdbarch, 0);
 
   tdep->gregset = &sparc32_linux_gregset;
   tdep->sizeof_gregset = 152;
diff --git a/gdb/sparc64-linux-tdep.c b/gdb/sparc64-linux-tdep.c
index 10a6eb527785..fe1b276bc4e8 100644
--- a/gdb/sparc64-linux-tdep.c
+++ b/gdb/sparc64-linux-tdep.c
@@ -365,7 +365,7 @@ sparc64_linux_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
 {
   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
 
-  linux_init_abi (info, gdbarch, false);
+  linux_init_abi (info, gdbarch, 0);
 
   tdep->gregset = &sparc64_linux_gregset;
   tdep->sizeof_gregset = 288;
diff --git a/gdb/tic6x-linux-tdep.c b/gdb/tic6x-linux-tdep.c
index 5a8d7c7f8258..7820e2da5a00 100644
--- a/gdb/tic6x-linux-tdep.c
+++ b/gdb/tic6x-linux-tdep.c
@@ -167,7 +167,7 @@ tic6x_uclinux_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
 {
   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
 
-  linux_init_abi (info, gdbarch, false);
+  linux_init_abi (info, gdbarch, 0);
 
   /* Shared library handling.  */
   set_solib_ops (gdbarch, &dsbt_so_ops);
diff --git a/gdb/tilegx-linux-tdep.c b/gdb/tilegx-linux-tdep.c
index 14cfafe33dec..37c0790a1909 100644
--- a/gdb/tilegx-linux-tdep.c
+++ b/gdb/tilegx-linux-tdep.c
@@ -111,7 +111,7 @@ tilegx_linux_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
 {
   int arch_size = gdbarch_addr_bit (gdbarch);
 
-  linux_init_abi (info, gdbarch, false);
+  linux_init_abi (info, gdbarch, 0);
 
   tramp_frame_prepend_unwinder (gdbarch, &tilegx_linux_rt_sigframe);
 
diff --git a/gdb/xtensa-linux-tdep.c b/gdb/xtensa-linux-tdep.c
index c2aeb8e9397b..fccac7d49fc3 100644
--- a/gdb/xtensa-linux-tdep.c
+++ b/gdb/xtensa-linux-tdep.c
@@ -110,7 +110,7 @@ xtensa_linux_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
       set_gdbarch_num_pseudo_regs (gdbarch, tdep->num_pseudo_regs);
     }
 
-  linux_init_abi (info, gdbarch, false);
+  linux_init_abi (info, gdbarch, 0);
 
   set_solib_svr4_fetch_link_map_offsets
     (gdbarch, svr4_ilp32_fetch_link_map_offsets);
-- 
2.29.2


^ permalink raw reply	[flat|nested] 40+ messages in thread

* [PATCH v2 14/14] gdb: use two displaced step buffers on amd64/Linux
  2020-12-02 15:47 [PATCH v2 00/14] Concurrent displaced stepping Simon Marchi
                   ` (12 preceding siblings ...)
  2020-12-02 15:48 ` [PATCH v2 13/14] gdb: make displaced stepping implementation capable of managing multiple buffers Simon Marchi
@ 2020-12-02 15:48 ` Simon Marchi
  2020-12-04  1:51   ` Pedro Alves
  2020-12-04  1:56 ` [PATCH v2 00/14] Concurrent displaced stepping Pedro Alves
  14 siblings, 1 reply; 40+ messages in thread
From: Simon Marchi @ 2020-12-02 15:48 UTC (permalink / raw)
  To: gdb-patches; +Cc: Simon Marchi

From: Simon Marchi <simon.marchi@efficios.com>

As observed on a binary compiled on AMD64 Ubuntu 20.04, against glibc
2.31 (I think it's the libc that provides this startup code, right?),
there are enough bytes at the executable's entry point to hold more than
one displaced step buffer.  gdbarch_max_insn_length is 16, and the
code at _start looks like:

0000000000001040 <_start>:
    1040:       f3 0f 1e fa             endbr64
    1044:       31 ed                   xor    %ebp,%ebp
    1046:       49 89 d1                mov    %rdx,%r9
    1049:       5e                      pop    %rsi
    104a:       48 89 e2                mov    %rsp,%rdx
    104d:       48 83 e4 f0             and    $0xfffffffffffffff0,%rsp
    1051:       50                      push   %rax
    1052:       54                      push   %rsp
    1053:       4c 8d 05 56 01 00 00    lea    0x156(%rip),%r8        # 11b0 <__libc_csu_fini>
    105a:       48 8d 0d df 00 00 00    lea    0xdf(%rip),%rcx        # 1140 <__libc_csu_init>
    1061:       48 8d 3d c1 00 00 00    lea    0xc1(%rip),%rdi        # 1129 <main>
    1068:       ff 15 72 2f 00 00       callq  *0x2f72(%rip)        # 3fe0 <__libc_start_main@GLIBC_2.2.5>
    106e:       f4                      hlt
    106f:       90                      nop

The two buffers would occupy [0x1040, 0x1060).

I checked on Alpine, which uses the musl C library, the startup code
looks like:

0000000000001048 <_start>:
    1048:       48 31 ed                xor    %rbp,%rbp
    104b:       48 89 e7                mov    %rsp,%rdi
    104e:       48 8d 35 e3 2d 00 00    lea    0x2de3(%rip),%rsi        # 3e38 <_DYNAMIC>
    1055:       48 83 e4 f0             and    $0xfffffffffffffff0,%rsp
    1059:       e8 00 00 00 00          callq  105e <_start_c>

000000000000105e <_start_c>:
    105e:       48 8b 37                mov    (%rdi),%rsi
    1061:       48 8d 57 08             lea    0x8(%rdi),%rdx
    1065:       45 31 c9                xor    %r9d,%r9d
    1068:       4c 8d 05 47 01 00 00    lea    0x147(%rip),%r8        # 11b6 <_fini>
    106f:       48 8d 0d 8a ff ff ff    lea    -0x76(%rip),%rcx        # 1000 <_init>
    1076:       48 8d 3d 0c 01 00 00    lea    0x10c(%rip),%rdi        # 1189 <main>
    107d:       e9 9e ff ff ff          jmpq   1020 <__libc_start_main@plt>

Even though there's a _start_c symbol, it all appears to be code that
runs once at the very beginning of the program, so it looks fine if the
two buffers occupy [0x1048, 0x1068).

One important thing I discovered while doing this is that when debugging
a dynamically-linked executable, breakpoints in the shared library
loader are hit before executing the _start code, and these breakpoints
may be displaced-stepped.  So it's very important that the buffer bytes
are restored properly after doing the displaced steps, otherwise the
_start code will be corrupted once we try to execute it.

Another thing that made me think about is that library constructors (as
in `__attribute__((constructor))`) run before _start.  And they are free
to spawn threads.  What if one of these threads executes a displaced
step, therefore changing the bytes at _start, while the main thread
executes _start?  That doesn't sound good and I don't know how we could
prevent it.  But this is a problem that predates the current patch.

Even when stress-testing the implementation, by making many threads do
displaced steps over and over, I didn't see a significant performance (I
confirmed that the two buffers were used by checking the "set debug
displaced" logs though).  However, this patch mostly helps make the
feature testable by anybody with an AMD64/Linux machine, so I think it's
useful.

gdb/ChangeLog:

        * amd64-linux-tdep.c (amd64_linux_init_abi): Pass 2 as the
	number of displaced step buffers.

Change-Id: Ia0c96ea0fcda893f4726df6fdac7be5214620112
---
 gdb/amd64-linux-tdep.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/gdb/amd64-linux-tdep.c b/gdb/amd64-linux-tdep.c
index 60707ed7aaff..4f2d83f0286f 100644
--- a/gdb/amd64-linux-tdep.c
+++ b/gdb/amd64-linux-tdep.c
@@ -1880,7 +1880,7 @@ amd64_linux_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
   if (!valid_p)
     return;
 
-  amd64_linux_init_abi_common (info, gdbarch, 1);
+  amd64_linux_init_abi_common (info, gdbarch, 2);
 
   /* Initialize the amd64_linux_record_tdep.  */
   /* These values are the size of the type that will be used in a system
-- 
2.29.2


^ permalink raw reply	[flat|nested] 40+ messages in thread

* Re: [PATCH v2 02/14] gdb: clear inferior displaced stepping state and in-line step-over info on exec
  2020-12-02 15:47 ` [PATCH v2 02/14] gdb: clear inferior displaced stepping state and in-line step-over info on exec Simon Marchi
@ 2020-12-02 17:16   ` Simon Marchi
  2020-12-04  1:54     ` Pedro Alves
  2020-12-04  1:48   ` Pedro Alves
  1 sibling, 1 reply; 40+ messages in thread
From: Simon Marchi @ 2020-12-02 17:16 UTC (permalink / raw)
  To: Simon Marchi, gdb-patches; +Cc: Simon Marchi

On 2020-12-02 10:47 a.m., Simon Marchi via Gdb-patches wrote:
> From: Simon Marchi <simon.marchi@efficios.com>
> Add a test with a program with two threads that does an exec.  The test
> includes the following axis:
>
> - whether it's the leader thread or the other thread that does the exec.
>
> - whether the exec'r and exec'd program have different text segment
>   addresses.  This is to hopefully catch cases where the displaced
>   stepping info doesn't get reset, and GDB later tries to restore bytes
>   of the old address space in the new address space.  If the mapped
>   addresses are different, we should get some memory error.   This
>   happens without the patch applied:
>
>   $ ./gdb -q -nx --data-directory=data-directory testsuite/outputs/gdb.threads/step-over-exec/step-over-exec-execr-thread-leader-diff-text-segs-true -ex "b main" -ex r -ex "b my_execve_syscall if 0"  -ex "set displaced-stepping on"
>   ...
>   Breakpoint 1, main (argc=1, argv=0x7fffffffde38) at /home/simark/src/binutils-gdb/gdb/testsuite/gdb.threads/step-over-exec.c:69
>   69        argv0 = argv[0];
>   Breakpoint 2 at 0x60133a: file /home/simark/src/binutils-gdb/gdb/testsuite/lib/my-syscalls.S, line 34.
>   (gdb) c
>   Continuing.
>   [New Thread 0x7ffff7c62640 (LWP 1455423)]
>   Leader going in exec.
>   Exec-ing /home/simark/build/binutils-gdb/gdb/testsuite/outputs/gdb.threads/step-over-exec/step-over-exec-execr-thread-leader-diff-text-segs-true-execd
>   [Thread 0x7ffff7c62640 (LWP 1455423) exited]
>   process 1455418 is executing new program: /home/simark/build/binutils-gdb/gdb/testsuite/outputs/gdb.threads/step-over-exec/step-over-exec-execr-thread-leader-diff-text-segs-true-execd
>   Error in re-setting breakpoint 2: Function "my_execve_syscall" not defined.
>   No unwaited-for children left.
>   (gdb) n
>   Single stepping until exit from function _start,
>   which has no line number information.
>   Cannot access memory at address 0x6010d2
>   (gdb)
>
> - Whether displaced stepping is allowed or not, so that we end up
>   testing both displaced stepping and in-line stepping on arches that do
>   support displaced stepping (otherwise, it just tests in-line stepping
>   twice I suppose)

Just to let you know, I forgot to run the test on
native-gdbserver/native-extended-gdbserver.  I get some failures due to
a GDBserver internal error:

/home/smarchi/src/binutils-gdb/gdbserver/linux-low.cc:3665: A problem internal to GDBserver has been detected.
ptid_t linux_process_target::wait_1(ptid_t, target_waitstatus*, target_wait_flags): Assertion `step_over_bkpt == null_ptid' failed.

Simon

^ permalink raw reply	[flat|nested] 40+ messages in thread

* Re: [PATCH v2 01/14] gdb: add inferior_execd observable
  2020-12-02 15:47 ` [PATCH v2 01/14] gdb: add inferior_execd observable Simon Marchi
@ 2020-12-04  1:45   ` Pedro Alves
  0 siblings, 0 replies; 40+ messages in thread
From: Pedro Alves @ 2020-12-04  1:45 UTC (permalink / raw)
  To: Simon Marchi, gdb-patches; +Cc: Simon Marchi

On 12/2/20 3:47 PM, Simon Marchi via Gdb-patches wrote:
> From: Simon Marchi <simon.marchi@efficios.com>
> 
> I want to add another action (clearing displaced stepping state) that
> happens when an inferior execs.  I think it would be cleaner to have an
> observer for this event, rather than have infrun know about each other
> sub-component.
> 
> Replace the calls to solib_create_inferior_hook and
> jit_inferior_created_hook in follow_exec by observers.
> 
> gdb/ChangeLog:
> 
> 	* observable.h (inferior_execd): Declare new observable.
> 	* observable.c (inferior_execd): Declare new observable.
> 	* infrun.c (follow_exec): Notify inferior_execd observer.
> 	* jit.c (jit_inferior_created_hook): Make static.
> 	(_initialize_jit): Register inferior_execd observer.
> 	* jit.h (jit_inferior_created_hook): Remove declaration.
> 	* solib.c (_initialize_solib): Register inferior_execd observer.

OK.

^ permalink raw reply	[flat|nested] 40+ messages in thread

* Re: [PATCH v2 02/14] gdb: clear inferior displaced stepping state and in-line step-over info on exec
  2020-12-02 15:47 ` [PATCH v2 02/14] gdb: clear inferior displaced stepping state and in-line step-over info on exec Simon Marchi
  2020-12-02 17:16   ` Simon Marchi
@ 2020-12-04  1:48   ` Pedro Alves
  2020-12-04 21:03     ` Simon Marchi
  1 sibling, 1 reply; 40+ messages in thread
From: Pedro Alves @ 2020-12-04  1:48 UTC (permalink / raw)
  To: Simon Marchi, gdb-patches; +Cc: Simon Marchi

On 12/2/20 3:47 PM, Simon Marchi via Gdb-patches wrote:
> From: Simon Marchi <simon.marchi@efficios.com>
> 
> New in v2:
> 
> - A test!

Hurray!

> - I found (from the te st) that in-line step over exec hangs too,

"te st"

> Add a test with a program with two threads that does an exec.  The test
> includes the following axis:

plural -> axes


> To be able to precisely put a breakpoint on the syscall instruction, I
> added a small assembly file (lib/my-syscalls.S) that contains minimal
> Linux syscall wrappers.  I prefer that to the strategy used in
> gdb.base/step-over-syscall.exp, which is to stepi into the glibc wrapper
> until we find something that looks like a syscall instruction, I find
> that more predictable.

The downside of course is that this way you have to write the wrappers
for all archs and OSs.  I suppose an alternative would be to use
"catch syscall" and then "x /-i" to find the syscall instruction
address, like:

 (gdb) catch syscall execve
 Catchpoint 1 (syscall 'execve' [59])
 (gdb) r
 ...
 Thread 1 "execl" hit Catchpoint 1 (call to syscall execve), 0x00007ffff7e7a16b in execve () at ../sysdeps/unix/syscall-template.S:78
 (gdb) x /-i $pc
   0x7ffff7e7a169 <execve+9>:   syscall 
 (gdb) 

I don't object your approach though.

> gdb/ChangeLog:
> 
> 	* infrun.c (infrun_inferior_execd): New function.
> 	(_initialize_infrun): Attach inferior_execd observer.
> 
> gdb/testsuite/ChangeLog:
> 
> 	* gdb.threads/step-over-exec-execd.c: New.

Missing copyright header on this file.

> 	* lib/my-syscalls.S: New.

I suppose the plan for other OSs, e.g., FreeBSD, is to use 
#ifdef in this file, like #ifdef __linux__ etc.?

> 	* lib/my-syscalls.h: New.

> --- /dev/null
> +++ b/gdb/testsuite/gdb.threads/step-over-exec-execd.c
> @@ -0,0 +1,13 @@
> +int a_variable_in_execd = 0;

I would suggest giving this variable a value != 0, since "0" is
also a typical "garbage" value.

> --- /dev/null
> +++ b/gdb/testsuite/lib/my-syscalls.S
> @@ -0,0 +1,46 @@
> +/* This testcase is part of GDB, the GNU debugger.
> +
> +   Copyright 2020 Free Software Foundation, Inc.
> +
> +   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/>.  */
> +
> +
> +/* This file implements simple Linux syscall wrappers, to be used by tests that
> +   need to know exactly where the syscall instructions are.  */
> +
> +#include <asm/unistd.h>
> +
> +/* int my_execve (const char *file, char *argv[], char *envp[]);  */
> +
> +.global my_execve
> +my_execve:
> +
> +#if defined(__x86_64__)
> +
> +	mov $__NR_execve, %rax
> +	/* rdi, rsi and rdx already contain the right arguments.  */
> +my_execve_syscall:
> +	syscall
> +	ret
> +
> +#elif defined(__aarch64__)
> +
> +	mov x8, #__NR_execve
> +	/* x0, x1 and x2 already contain the right arguments.  */
> +my_execve_syscall:
> +	svc #0
> +
> +#else
> +# error "Unsupported architecture"
> +#endif

OK with the nits above fixed.

OOC, did you try porting this to 32-bit x86?

^ permalink raw reply	[flat|nested] 40+ messages in thread

* Re: [PATCH v2 03/14] gdb: restore displaced step buffer bytes when another thread forks
  2020-12-02 15:47 ` [PATCH v2 03/14] gdb: restore displaced step buffer bytes when another thread forks Simon Marchi
@ 2020-12-04  1:48   ` Pedro Alves
  0 siblings, 0 replies; 40+ messages in thread
From: Pedro Alves @ 2020-12-04  1:48 UTC (permalink / raw)
  To: Simon Marchi, gdb-patches

On 12/2/20 3:47 PM, Simon Marchi via Gdb-patches wrote:
> In handle_inferior_event, where we handle forks, we make sure to restore
> the bytes of the displaced stepping buffer in the child's address
> space.  However, we only do it when the forking thread was the one
> doing a displaced step.  It could happen that a thread forks while
> another one is doing a displaced step.  In this case, we also need to
> restore the bytes in the child.
> 
> Move the byte-restoring code outside of the condition that checks
> whether the event thread was displaced stepping.
> 
> gdb/ChangeLog:
> 
> 	* infrun.c (handle_inferior_event): Restore displaced step
> 	buffer bytes in child process when handling fork, even if fork
> 	happened in another thread than the displaced-stepping one.

OK.


^ permalink raw reply	[flat|nested] 40+ messages in thread

* Re: [PATCH v2 04/14] gdb: get rid of get_displaced_stepping_state
  2020-12-02 15:47 ` [PATCH v2 04/14] gdb: get rid of get_displaced_stepping_state Simon Marchi
@ 2020-12-04  1:48   ` Pedro Alves
  2020-12-04 21:06     ` Simon Marchi
  0 siblings, 1 reply; 40+ messages in thread
From: Pedro Alves @ 2020-12-04  1:48 UTC (permalink / raw)
  To: Simon Marchi, gdb-patches

On 12/2/20 3:47 PM, Simon Marchi via Gdb-patches wrote:
> Remove function get_displaced_stepping_state.  When it was introduced,
> inferiors' dispalced stepping state were kept in a linked list in

dispalced -> displaced
were -> was

> infrun.c, so it was handy.  Nowadays, the state is kept inside struct
> inferior directly, so we can just access it directly instead.
> 
> gdb/ChangeLog:
> 
> 	* infrun.c (get_displaced_stepping_state): Remove, change
> 	callers to access the field directly.

OK.

^ permalink raw reply	[flat|nested] 40+ messages in thread

* Re: [PATCH v2 05/14] gdb: rename things related to step over chains
  2020-12-02 15:47 ` [PATCH v2 05/14] gdb: rename things related to step over chains Simon Marchi
@ 2020-12-04  1:49   ` Pedro Alves
  2020-12-04 21:04     ` Simon Marchi
  0 siblings, 1 reply; 40+ messages in thread
From: Pedro Alves @ 2020-12-04  1:49 UTC (permalink / raw)
  To: Simon Marchi, gdb-patches; +Cc: Simon Marchi

On 12/2/20 3:47 PM, Simon Marchi via Gdb-patches wrote:
> From: Simon Marchi <simon.marchi@efficios.com>
> 
> New in v2: comment and formatting fixes.
> 
> Rename step_over_queue_head to global_thread_step_over_chain_head, to
> make it more obvious when reading code that we are touching the global
> queue.  Rename all functions that operate on it to have "global" in
> their name, to make it clear on which chain they operate on.  Also, in a
> subsequent patch, we'll need both global and non-global versions of
> these functions, so it will be easier to do the distinction if they are
> named properly.
> 
> Normalize the naming to use "chain" everywhere instead of sometimes
> "queue", sometimes "chain".
> 
> I also reworded a few comments in gdbthread.h.  They implied that the
> step over chain is per-inferior, when in reality there is only one
> global chain, not one per inferior, as far as I understand.
> 
> gdb/ChangeLog:
> 
>         * gdbthread.h (thread_step_over_chain_enqueue): Rename to...
>         (global_thread_step_over_chain_enqueue): ... this.  Update all
> 	users.
>         (thread_step_over_chain_remove): Rename to...
>         (global_thread_step_over_chain_remove): ... this.  Update all
> 	users.
>         (thread_step_over_chain_next): Rename to...
>         (global_thread_step_over_chain_next): ... this.  Update all
> 	users.
>         * infrun.h (step_over_queue_head): Rename to...
> 	(global_thread_step_over_chain_head): ... this.  Update all
> 	users.
>         * infrun.c (step_over_queue_head): Rename to...
> 	(global_thread_step_over_chain_head): ... this.  Update all
> 	users.
>         * thread.c (step_over_chain_remove): Rename to...
> 	(thread_step_over_chain_remove): ... this.  Update all users.
> 	(thread_step_over_chain_next): Rename to...
>         (global_thread_step_over_chain_next): ... this.  Update all
> 	users.
>         (thread_step_over_chain_enqueue): Rename to...
>         (global_thread_step_over_chain_enqueue): ... this.  Update all
> 	users.
>         (thread_step_over_chain_remove): Rename to...
>         (global_thread_step_over_chain_remove): ... this.  Update all
> 	users.

Tabs vs spaces in ChangeLog.  Otherwise OK.

^ permalink raw reply	[flat|nested] 40+ messages in thread

* Re: [PATCH v2 06/14] gdb: rename displaced_step_closure to displaced_step_copy_insn_closure
  2020-12-02 15:47 ` [PATCH v2 06/14] gdb: rename displaced_step_closure to displaced_step_copy_insn_closure Simon Marchi
@ 2020-12-04  1:49   ` Pedro Alves
  0 siblings, 0 replies; 40+ messages in thread
From: Pedro Alves @ 2020-12-04  1:49 UTC (permalink / raw)
  To: Simon Marchi, gdb-patches; +Cc: Simon Marchi

On 12/2/20 3:47 PM, Simon Marchi via Gdb-patches wrote:
> From: Simon Marchi <simon.marchi@efficios.com>
> 
> Since we're going to introduce other "displaced step" functions and
> another kind of displaced step closure, make it clear that this is the
> return type of the gdbarch_displaced_step_copy_insn function.
> 
> gdb/ChangeLog:
> 
> 	* infrun.h (get_displaced_step_closure_by_addr): Rename to...
> 	(get_displaced_step_copy_insn_closure_by_addr): ... this.
> 	Update all users.
> 	(displaced_step_closure): Rename to...
> 	(displaced_step_copy_insn_closure): ... this.  Update all users.
> 	(displaced_step_closure_up): Rename to...
> 	(displaced_step_copy_insn_closure_up). ... this.  Update all
> 	users.
> 	(buf_displaced_step_closure): Rename to...
> 	(buf_displaced_step_copy_insn_closure): ... this.  Update all
> 	users.
> 	* infrun.c (get_displaced_step_closure_by_addr): Rename to...
> 	(get_displaced_step_copy_insn_closure_by_addr): ... this.
> 	Update all users.
> 	* aarch64-tdep.c (aarch64_displaced_step_closure): Rename to...
> 	(aarch64_displaced_step_copy_insn_closure): ... this.  Update
> 	all users.
> 	* amd64-tdep.c (amd64_displaced_step_closure): Rename to...
> 	(amd64_displaced_step_copy_insn_closure): ... this.  Update all
> 	users.
> 	* arm-tdep.h (arm_displaced_step_closure): Rename to...
> 	(arm_displaced_step_copy_insn_closure): ... this.  Update all
> 	users.
> 	* i386-tdep.h (i386_displaced_step_closure): Rename to...
> 	(i386_displaced_step_copy_insn_closure): ... this.  Update all
> 	users.
> 	* rs6000-tdep.c (ppc_displaced_step_closure): Rename to...
> 	(ppc_displaced_step_copy_insn_closure): ... this.  Update all
> 	users.
> 	* s390-tdep.c (s390_displaced_step_closure): Rename to...
> 	(s390_displaced_step_copy_insn_closure): ... this.  Update all
> 	users.
> 	* gdbarch.h: Re-generate.
> 	* gdbarch.c: Re-generate.

OK.

^ permalink raw reply	[flat|nested] 40+ messages in thread

* Re: [PATCH v2 07/14] gdb: rename displaced_step_fixup to displaced_step_finish
  2020-12-02 15:47 ` [PATCH v2 07/14] gdb: rename displaced_step_fixup to displaced_step_finish Simon Marchi
@ 2020-12-04  1:49   ` Pedro Alves
  0 siblings, 0 replies; 40+ messages in thread
From: Pedro Alves @ 2020-12-04  1:49 UTC (permalink / raw)
  To: Simon Marchi, gdb-patches; +Cc: Simon Marchi

On 12/2/20 3:47 PM, Simon Marchi via Gdb-patches wrote:
> From: Simon Marchi <simon.marchi@efficios.com>
> 
> This is a preparatory patch to reduce a little bit the diff size of the
> main patch later in this series.  It renames the displaced_step_fixup
> function in infrun.c to displaced_step_finish.
> 
> The rationale is to better differentiate the low and high level
> operations.
> 
> We first have the low level operation of writing an instruction to a
> displaced buffer, called "copy_insn".  The mirror low level operation to
> fix up the state after having executed the instruction is "fixup".  The
> high level operation of preparing a thread for a displaced step (which
> includes doing the "copy_insn" and some more bookkeeping) is called
> "prepare" (as in displaced_step_prepare).  The mirror high level
> operation to cleaning up after a displaced step (which includes doing
> the "fixup" and some more bookkeeping) is currently also called "fixup"
> (as in displaced_step_fixup), just like the low level operation.
> 
> I think that choosing a different name for the low and high level
> cleanup operation makes it clearer, hence "finish".
> 
> gdb/ChangeLog:
> 
> 	* infrun.c (displaced_step_fixup): Rename to...
> 	(displaced_step_finish): ... this, update all callers.

OK.

^ permalink raw reply	[flat|nested] 40+ messages in thread

* Re: [PATCH v2 08/14] gdb: introduce status enum for displaced step prepare/finish
  2020-12-02 15:47 ` [PATCH v2 08/14] gdb: introduce status enum for displaced step prepare/finish Simon Marchi
@ 2020-12-04  1:49   ` Pedro Alves
  2020-12-04 21:08     ` Simon Marchi
  0 siblings, 1 reply; 40+ messages in thread
From: Pedro Alves @ 2020-12-04  1:49 UTC (permalink / raw)
  To: Simon Marchi, gdb-patches; +Cc: Simon Marchi

On 12/2/20 3:47 PM, Simon Marchi via Gdb-patches wrote:
> +enum displaced_step_prepare_status
> +{
> +  /* A displaced stepping buffer was successfully allocated and prepared.  */
> +  DISPLACED_STEP_PREPARE_STATUS_OK,
> +
> +  /* This particular instruction can't be displaced step, GDB should fall back

"be ... stepped"

> +     on in-line stepping.  */
> +  DISPLACED_STEP_PREPARE_STATUS_CANT,

Otherwise OK.

^ permalink raw reply	[flat|nested] 40+ messages in thread

* Re: [PATCH v2 09/14] gdb: pass inferior to get_linux_inferior_data
  2020-12-02 15:48 ` [PATCH v2 09/14] gdb: pass inferior to get_linux_inferior_data Simon Marchi
@ 2020-12-04  1:50   ` Pedro Alves
  0 siblings, 0 replies; 40+ messages in thread
From: Pedro Alves @ 2020-12-04  1:50 UTC (permalink / raw)
  To: Simon Marchi, gdb-patches; +Cc: Simon Marchi

On 12/2/20 3:48 PM, Simon Marchi via Gdb-patches wrote:
> From: Simon Marchi <simon.marchi@efficios.com>
> 
> Pass to get_linux_inferior_data the inferior for which we want to obtain
> the linux-specific data, rather than assuming the current inferior.
> This helps slightly reduce the diff in the upcoming main patch.
> 
> Update the sole caller to pass the current inferior.
> 
> gdb/ChangeLog:
> 
> 	* linux-tdep.c (get_linux_inferior_data): Add inferior
> 	parameter.
> 	(linux_vsyscall_range): Pass current inferior.

OK.

^ permalink raw reply	[flat|nested] 40+ messages in thread

* Re: [PATCH v2 10/14] gdb: move displaced stepping types to displaced-stepping.{h, c}
  2020-12-02 15:48 ` [PATCH v2 10/14] gdb: move displaced stepping types to displaced-stepping.{h, c} Simon Marchi
@ 2020-12-04  1:50   ` Pedro Alves
  0 siblings, 0 replies; 40+ messages in thread
From: Pedro Alves @ 2020-12-04  1:50 UTC (permalink / raw)
  To: Simon Marchi, gdb-patches; +Cc: Simon Marchi

On 12/2/20 3:48 PM, Simon Marchi via Gdb-patches wrote:
> From: Simon Marchi <simon.marchi@efficios.com>
> 
> Move displaced-stepping related stuff unchanged to displaced-stepping.h
> and displaced-stepping.c.  This helps make the following patch a bit
> smaller and easier to read.
> 
> gdb/ChangeLog:
> 
> 	* Makefile.in (COMMON_SFILES): Add displaced-stepping.c.
> 	* aarch64-tdep.h: Include displaced-stepping.h.
> 	* displaced-stepping.h (struct displaced_step_copy_insn_closure):
> 	Move here.
> 	(displaced_step_copy_insn_closure_up): Move here.
> 	(struct buf_displaced_step_copy_insn_closure): Move here.
> 	(struct displaced_step_inferior_state): Move here.
> 	(debug_displaced): Move here.
> 	(displaced_debug_printf_1): Move here.
> 	(displaced_debug_printf): Move here.
> 	* displaced-stepping.c: New file.
> 	* gdbarch.sh: Include displaced-stepping.h in gdbarch.h.
> 	* gdbarch.h: Re-generate.
> 	* inferior.h: Include displaced-stepping.h.
> 	* infrun.h (debug_displaced): Move to displaced-stepping.h.
> 	(displaced_debug_printf_1): Likewise.
> 	(displaced_debug_printf): Likewise.
> 	(struct displaced_step_copy_insn_closure): Likewise.
> 	(displaced_step_copy_insn_closure_up): Likewise.
> 	(struct buf_displaced_step_copy_insn_closure): Likewise.
> 	(struct displaced_step_inferior_state): Likewise.
> 	* infrun.c (show_debug_displaced): Move to displaced-stepping.c.
> 	(displaced_debug_printf_1): Likewise.
> 	(displaced_step_copy_insn_closure::~displaced_step_copy_insn_closure):
> 	Likewise.
> 	(_initialize_infrun): Don't register "set/show debug displaced".

OK.

^ permalink raw reply	[flat|nested] 40+ messages in thread

* Re: [PATCH v2 11/14] gdb: move displaced stepping logic to gdbarch, allow starting concurrent displaced steps
  2020-12-02 15:48 ` [PATCH v2 11/14] gdb: move displaced stepping logic to gdbarch, allow starting concurrent displaced steps Simon Marchi
@ 2020-12-04  1:50   ` Pedro Alves
  2020-12-04 21:11     ` Simon Marchi
  0 siblings, 1 reply; 40+ messages in thread
From: Pedro Alves @ 2020-12-04  1:50 UTC (permalink / raw)
  To: Simon Marchi, gdb-patches; +Cc: Simon Marchi

Very good!  Only a few minor comments below.  Otherwise OK.

On 12/2/20 3:48 PM, Simon Marchi via Gdb-patches wrote:

> +displaced_step_finish_status
> +displaced_step_buffer::finish (gdbarch *arch, thread_info *thread,
> +			       gdb_signal sig)
> +{
> +  gdb_assert (thread->displaced_step_state.in_progress ());
> +  gdb_assert (thread == m_current_thread);
> +
> +  /* Move this to a local variable so it's released in case something goes
> +     wrong.  */
> +  displaced_step_copy_insn_closure_up copy_insn_closure
> +    = std::move (m_copy_insn_closure);
> +  gdb_assert (copy_insn_closure != nullptr);
> +
> +  /* Reset m_current_thread immediately to mark the buffer as available, in case

Uppercase m_current_thread.

> +     something goes wrong below.  */
> +  m_current_thread = nullptr;
> +
> +  /* Now that a buffer gets freed, tell GDB it can ask us to prepare a displaced

How about "tell infrun" instead of "tell GDB"?  This is all in GDB.

> +     step again for this inferior.  Do that here in case something goes wrong
> +     below.  */
> +  thread->inf->displaced_step_state.unavailable = false;
> +
> +  ULONGEST len = gdbarch_max_insn_length (arch);
> +


> @@ -1927,14 +1833,41 @@ static step_over_what thread_still_needs_step_over (struct thread_info *tp);
>  static bool
>  start_step_over (void)
>  {
> -  struct thread_info *tp, *next;
> +  thread_info *next;
>  
>    /* Don't start a new step-over if we already have an in-line
>       step-over operation ongoing.  */
>    if (step_over_info_valid_p ())
>      return false;
>  
> -  for (tp = global_thread_step_over_chain_head; tp != NULL; tp = next)
> +  /* Steal the global thread step over chain.  As we try to initiate displaced
> +     steps, threads will be enqueued in the global chain if no buffers are
> +     available.  If we iterated on the global chain directly, we might iterate
> +     indefinitely.  */
> +  thread_info *threads_to_step = global_thread_step_over_chain_head;
> +  global_thread_step_over_chain_head = NULL;
> +
> +  infrun_debug_printf ("stealing global queue of threads to step, length = %d",
> +		       thread_step_over_chain_length (threads_to_step));
> +
> +  bool started = false;
> +
> +  /* On scope exit (whatever the reason, return or exception), if there are
> +     threads left in the THREADS_TO_STEP chain, put back these threads in the
> +     global list.  */
> +  SCOPE_EXIT {

This format looks un-GNU-ish to me.  I've been putting the {
on the following line, indented by two spaces, like if it was
a if/for/while block, like:

  SCOPE_EXIT
    {

... unless the whole thing fits in one line:

  SCOPE_EXIT { foo (); };

> +    if (threads_to_step == nullptr)
> +      infrun_debug_printf ("step-over queue now empty");
> +    else
> +      {
> +	infrun_debug_printf ("putting back %d threads to step in global queue",
> +			     thread_step_over_chain_length (threads_to_step));
> +
> +	global_thread_step_over_chain_enqueue_chain (threads_to_step);
> +      }
> +  };
> +
> +  for (thread_info *tp = threads_to_step; tp != NULL; tp = next)
>      {
>        struct execution_control_state ecss;
>        struct execution_control_state *ecs = &ecss;
> @@ -1943,12 +1876,23 @@ start_step_over (void)
>  
>        gdb_assert (!tp->stop_requested);
>  
> -      next = global_thread_step_over_chain_next (tp);
> +      next = thread_step_over_chain_next (threads_to_step, tp);
>  
> -      /* If this inferior already has a displaced step in process,
> -	 don't start a new one.  */
> -      if (displaced_step_in_progress (tp->inf))
> -	continue;
> +      if (tp->inf->displaced_step_state.unavailable)
> +	{
> +	  /* The arch told us to not even try preparing another displaced step
> +	     for this inferior.  Just leave the thread in THREADS_TO_STEP, it
> +	     will get moved to the global chain on scope exit.  */
> +	  continue;
> +	}
> +
> +      /* Remove thread from the THREADS_TO_STEP chain.  If anything goes wrong
> +	 while we try to prepare the displaced step, we don't add it back to
> +	 the global step over chain.  This is to avoid a thread staying in the
> +	 step over chain indefinitely if something go wrong when resuming it.

something goes wrong

> +	 If the error is intermittent and it still needs a step over, it will
> +	 get enqueued again when we try to resume it normally.  */
> +      thread_step_over_chain_remove (&threads_to_step, tp);


^ permalink raw reply	[flat|nested] 40+ messages in thread

* Re: [PATCH v2 12/14] gdb: change linux gdbarch data from post to pre-init
  2020-12-02 15:48 ` [PATCH v2 12/14] gdb: change linux gdbarch data from post to pre-init Simon Marchi
@ 2020-12-04  1:50   ` Pedro Alves
  0 siblings, 0 replies; 40+ messages in thread
From: Pedro Alves @ 2020-12-04  1:50 UTC (permalink / raw)
  To: Simon Marchi, gdb-patches; +Cc: Simon Marchi

On 12/2/20 3:48 PM, Simon Marchi via Gdb-patches wrote:
> From: Simon Marchi <simon.marchi@efficios.com>
> 
> The following patch will need to fill a field in linux_gdbarch_data
> while the gdbarch is being built.  linux_gdbarch_data is currently
> allocated as a post-init gdbarch data, meaning it's not possible to fill
> it before the gdbarch is completely initialized.  Change it to a
> pre-init gdbarch data to allow this.
> 
> The init_linux_gdbarch_data function doesn't use the created gdbarch,
> it only allocates the linux_gdbarch_data structure on the gdbarch's
> obstack, so the change is trivial.
> 
> gdb/ChangeLog:
> 
> 	* linux-tdep.c (init_linux_gdbarch_data): Change parameter to
> 	obkstack.
> 	(_initialize_linux_tdep): Register pre-init gdb data instead of
> 	post-init.

OK.

^ permalink raw reply	[flat|nested] 40+ messages in thread

* Re: [PATCH v2 13/14] gdb: make displaced stepping implementation capable of managing multiple buffers
  2020-12-02 15:48 ` [PATCH v2 13/14] gdb: make displaced stepping implementation capable of managing multiple buffers Simon Marchi
@ 2020-12-04  1:51   ` Pedro Alves
  2020-12-04 21:14     ` Simon Marchi
  0 siblings, 1 reply; 40+ messages in thread
From: Pedro Alves @ 2020-12-04  1:51 UTC (permalink / raw)
  To: Simon Marchi, gdb-patches; +Cc: Simon Marchi

On 12/2/20 3:48 PM, Simon Marchi via Gdb-patches wrote:

>    /* Save this, now that we know everything went fine.  */
> -  m_copy_insn_closure = std::move (copy_insn_closure);
> +  buffer->copy_insn_closure = std::move (copy_insn_closure);
>  
> -  /* Tell GDB not to try preparing a displaced step again for this inferior.  */
> +  /* Tell GDB not to try preparing a displaced step again for this inferior if
> +     all buffers are taken.  */

Another spot where "Tell GDB" -> "Tell infrun" would make sense, IMO.

Anyhow, OK.

^ permalink raw reply	[flat|nested] 40+ messages in thread

* Re: [PATCH v2 14/14] gdb: use two displaced step buffers on amd64/Linux
  2020-12-02 15:48 ` [PATCH v2 14/14] gdb: use two displaced step buffers on amd64/Linux Simon Marchi
@ 2020-12-04  1:51   ` Pedro Alves
  0 siblings, 0 replies; 40+ messages in thread
From: Pedro Alves @ 2020-12-04  1:51 UTC (permalink / raw)
  To: Simon Marchi, gdb-patches; +Cc: Simon Marchi

OK

^ permalink raw reply	[flat|nested] 40+ messages in thread

* Re: [PATCH v2 02/14] gdb: clear inferior displaced stepping state and in-line step-over info on exec
  2020-12-02 17:16   ` Simon Marchi
@ 2020-12-04  1:54     ` Pedro Alves
  2020-12-04 20:49       ` Simon Marchi
  0 siblings, 1 reply; 40+ messages in thread
From: Pedro Alves @ 2020-12-04  1:54 UTC (permalink / raw)
  To: Simon Marchi, Simon Marchi, gdb-patches; +Cc: Simon Marchi

On 12/2/20 5:16 PM, Simon Marchi wrote:
> On 2020-12-02 10:47 a.m., Simon Marchi via Gdb-patches wrote:
>> From: Simon Marchi <simon.marchi@efficios.com>
>> Add a test with a program with two threads that does an exec.  The test
>> includes the following axis:
>>
>> - whether it's the leader thread or the other thread that does the exec.
>>
>> - whether the exec'r and exec'd program have different text segment
>>   addresses.  This is to hopefully catch cases where the displaced
>>   stepping info doesn't get reset, and GDB later tries to restore bytes
>>   of the old address space in the new address space.  If the mapped
>>   addresses are different, we should get some memory error.   This
>>   happens without the patch applied:
>>
>>   $ ./gdb -q -nx --data-directory=data-directory testsuite/outputs/gdb.threads/step-over-exec/step-over-exec-execr-thread-leader-diff-text-segs-true -ex "b main" -ex r -ex "b my_execve_syscall if 0"  -ex "set displaced-stepping on"
>>   ...
>>   Breakpoint 1, main (argc=1, argv=0x7fffffffde38) at /home/simark/src/binutils-gdb/gdb/testsuite/gdb.threads/step-over-exec.c:69
>>   69        argv0 = argv[0];
>>   Breakpoint 2 at 0x60133a: file /home/simark/src/binutils-gdb/gdb/testsuite/lib/my-syscalls.S, line 34.
>>   (gdb) c
>>   Continuing.
>>   [New Thread 0x7ffff7c62640 (LWP 1455423)]
>>   Leader going in exec.
>>   Exec-ing /home/simark/build/binutils-gdb/gdb/testsuite/outputs/gdb.threads/step-over-exec/step-over-exec-execr-thread-leader-diff-text-segs-true-execd
>>   [Thread 0x7ffff7c62640 (LWP 1455423) exited]
>>   process 1455418 is executing new program: /home/simark/build/binutils-gdb/gdb/testsuite/outputs/gdb.threads/step-over-exec/step-over-exec-execr-thread-leader-diff-text-segs-true-execd
>>   Error in re-setting breakpoint 2: Function "my_execve_syscall" not defined.
>>   No unwaited-for children left.
>>   (gdb) n
>>   Single stepping until exit from function _start,
>>   which has no line number information.
>>   Cannot access memory at address 0x6010d2
>>   (gdb)
>>
>> - Whether displaced stepping is allowed or not, so that we end up
>>   testing both displaced stepping and in-line stepping on arches that do
>>   support displaced stepping (otherwise, it just tests in-line stepping
>>   twice I suppose)
> 
> Just to let you know, I forgot to run the test on
> native-gdbserver/native-extended-gdbserver.  I get some failures due to
> a GDBserver internal error:
> 
> /home/smarchi/src/binutils-gdb/gdbserver/linux-low.cc:3665: A problem internal to GDBserver has been detected.
> ptid_t linux_process_target::wait_1(ptid_t, target_waitstatus*, target_wait_flags): Assertion `step_over_bkpt == null_ptid' failed.

Can we kfail it?

It sounds like the kind of thing that would happen if gdbserver forgets to
clear step_over_bkpt when the process execs.

^ permalink raw reply	[flat|nested] 40+ messages in thread

* Re: [PATCH v2 00/14] Concurrent displaced stepping
  2020-12-02 15:47 [PATCH v2 00/14] Concurrent displaced stepping Simon Marchi
                   ` (13 preceding siblings ...)
  2020-12-02 15:48 ` [PATCH v2 14/14] gdb: use two displaced step buffers on amd64/Linux Simon Marchi
@ 2020-12-04  1:56 ` Pedro Alves
  2020-12-04 21:52   ` Simon Marchi
  14 siblings, 1 reply; 40+ messages in thread
From: Pedro Alves @ 2020-12-04  1:56 UTC (permalink / raw)
  To: Simon Marchi, gdb-patches

On 12/2/20 3:47 PM, Simon Marchi via Gdb-patches wrote:
> This is v2 of
> 
>     https://sourceware.org/pipermail/gdb-patches/2020-November/173150.html
> 
> I tried to put in each commit log what changed in v2, I hope I didn't
> forget anything.  Patches 3 and 4 are new.

Alright, I think I re-reviewed everything and other than some minor
comments stuff and the like, this is all basically OK.

Thanks,
Pedro Alves

^ permalink raw reply	[flat|nested] 40+ messages in thread

* Re: [PATCH v2 02/14] gdb: clear inferior displaced stepping state and in-line step-over info on exec
  2020-12-04  1:54     ` Pedro Alves
@ 2020-12-04 20:49       ` Simon Marchi
  0 siblings, 0 replies; 40+ messages in thread
From: Simon Marchi @ 2020-12-04 20:49 UTC (permalink / raw)
  To: Pedro Alves, Simon Marchi, Simon Marchi, gdb-patches

On 2020-12-03 8:54 p.m., Pedro Alves wrote:
> Can we kfail it?

Yes, I filed a PR for that.

https://sourceware.org/bugzilla/show_bug.cgi?id=27020

I'll make the test return early if the "continue across exec" fails,
because the rest will fail anyway.  So:


 85     # Continue across exec to main.
 86     if { [target_is_gdbserver] } {
 87         setup_kfail gdb/27020 "*-*-*"
 88     }
 89     set failed [gdb_test "continue" \
 90                     "process $decimal is executing new program: .* hit Breakpoint $decimal, main .*" \
 91                     "continue across exec"]
 92     if { $failed } {
 93         return
 94     }

>
> It sounds like the kind of thing that would happen if gdbserver forgets to
> clear step_over_bkpt when the process execs.

Probably, but I don't want to start investigating this right now, I have
enough things on my plate for now :).

Simon

^ permalink raw reply	[flat|nested] 40+ messages in thread

* Re: [PATCH v2 02/14] gdb: clear inferior displaced stepping state and in-line step-over info on exec
  2020-12-04  1:48   ` Pedro Alves
@ 2020-12-04 21:03     ` Simon Marchi
  0 siblings, 0 replies; 40+ messages in thread
From: Simon Marchi @ 2020-12-04 21:03 UTC (permalink / raw)
  To: Pedro Alves, Simon Marchi, gdb-patches

On 2020-12-03 8:48 p.m., Pedro Alves wrote:
> On 12/2/20 3:47 PM, Simon Marchi via Gdb-patches wrote:
>> From: Simon Marchi <simon.marchi@efficios.com>
>> - I found (from the te st) that in-line step over exec hangs too,
>
> "te st"

I'll remove this part before pushing.

>> Add a test with a program with two threads that does an exec.  The test
>> includes the following axis:
>
> plural -> axes

Done.

>> To be able to precisely put a breakpoint on the syscall instruction, I
>> added a small assembly file (lib/my-syscalls.S) that contains minimal
>> Linux syscall wrappers.  I prefer that to the strategy used in
>> gdb.base/step-over-syscall.exp, which is to stepi into the glibc wrapper
>> until we find something that looks like a syscall instruction, I find
>> that more predictable.
>
> The downside of course is that this way you have to write the wrappers
> for all archs and OSs.  I suppose an alternative would be to use
> "catch syscall" and then "x /-i" to find the syscall instruction
> address, like:
>
>  (gdb) catch syscall execve
>  Catchpoint 1 (syscall 'execve' [59])
>  (gdb) r
>  ...
>  Thread 1 "execl" hit Catchpoint 1 (call to syscall execve), 0x00007ffff7e7a16b in execve () at ../sysdeps/unix/syscall-template.S:78
>  (gdb) x /-i $pc
>    0x7ffff7e7a169 <execve+9>:   syscall
>  (gdb)
>
> I don't object your approach though.

Indeed, writing the wrappers is arch/os-specific.  My baseline for this
was the gdb.base/step-over-syscall.exp, where we also need the test to
know about some architecture-specific syscall details.  So I figured if
we need to know arch/os-specific details anyway, we might as well just
write the wrappers, it's simpler.

What you propose sounds like a very good solution.  I'll keep the test
for now and keep it for future work, to avoid delaying this series
further.

>> gdb/ChangeLog:
>>
>> 	* infrun.c (infrun_inferior_execd): New function.
>> 	(_initialize_infrun): Attach inferior_execd observer.
>>
>> gdb/testsuite/ChangeLog:
>>
>> 	* gdb.threads/step-over-exec-execd.c: New.
>
> Missing copyright header on this file.

Fixed.

>
>> 	* lib/my-syscalls.S: New.
>
> I suppose the plan for other OSs, e.g., FreeBSD, is to use
> #ifdef in this file, like #ifdef __linux__ etc.?

If they want to support it, yes.  Although if they support "catch
syscall", we might as well do the catchpoint thing.

>
>> 	* lib/my-syscalls.h: New.
>
>> --- /dev/null
>> +++ b/gdb/testsuite/gdb.threads/step-over-exec-execd.c
>> @@ -0,0 +1,13 @@
>> +int a_variable_in_execd = 0;
>
> I would suggest giving this variable a value != 0, since "0" is
> also a typical "garbage" value.

Ok, my initial idea was more to just verify that the new executable's
symbols was correctly loaded.  But it doesn't hurt to check for a more
specific value, we'll just be more sure that everything is fine, so I'll
change it.

>
>> --- /dev/null
>> +++ b/gdb/testsuite/lib/my-syscalls.S
>> @@ -0,0 +1,46 @@
>> +/* This testcase is part of GDB, the GNU debugger.
>> +
>> +   Copyright 2020 Free Software Foundation, Inc.
>> +
>> +   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/>.  */
>> +
>> +
>> +/* This file implements simple Linux syscall wrappers, to be used by tests that
>> +   need to know exactly where the syscall instructions are.  */
>> +
>> +#include <asm/unistd.h>
>> +
>> +/* int my_execve (const char *file, char *argv[], char *envp[]);  */
>> +
>> +.global my_execve
>> +my_execve:
>> +
>> +#if defined(__x86_64__)
>> +
>> +	mov $__NR_execve, %rax
>> +	/* rdi, rsi and rdx already contain the right arguments.  */
>> +my_execve_syscall:
>> +	syscall
>> +	ret
>> +
>> +#elif defined(__aarch64__)
>> +
>> +	mov x8, #__NR_execve
>> +	/* x0, x1 and x2 already contain the right arguments.  */
>> +my_execve_syscall:
>> +	svc #0
>> +
>> +#else
>> +# error "Unsupported architecture"
>> +#endif
>
> OK with the nits above fixed.
>
> OOC, did you try porting this to 32-bit x86?

I hadn't, but I just did and it works fine.

Simon

^ permalink raw reply	[flat|nested] 40+ messages in thread

* Re: [PATCH v2 05/14] gdb: rename things related to step over chains
  2020-12-04  1:49   ` Pedro Alves
@ 2020-12-04 21:04     ` Simon Marchi
  0 siblings, 0 replies; 40+ messages in thread
From: Simon Marchi @ 2020-12-04 21:04 UTC (permalink / raw)
  To: Pedro Alves, Simon Marchi, gdb-patches

On 2020-12-03 8:49 p.m., Pedro Alves wrote:
> On 12/2/20 3:47 PM, Simon Marchi via Gdb-patches wrote:
>> From: Simon Marchi <simon.marchi@efficios.com>
>>
>> New in v2: comment and formatting fixes.
>>
>> Rename step_over_queue_head to global_thread_step_over_chain_head, to
>> make it more obvious when reading code that we are touching the global
>> queue.  Rename all functions that operate on it to have "global" in
>> their name, to make it clear on which chain they operate on.  Also, in a
>> subsequent patch, we'll need both global and non-global versions of
>> these functions, so it will be easier to do the distinction if they are
>> named properly.
>>
>> Normalize the naming to use "chain" everywhere instead of sometimes
>> "queue", sometimes "chain".
>>
>> I also reworded a few comments in gdbthread.h.  They implied that the
>> step over chain is per-inferior, when in reality there is only one
>> global chain, not one per inferior, as far as I understand.
>>
>> gdb/ChangeLog:
>>
>>         * gdbthread.h (thread_step_over_chain_enqueue): Rename to...
>>         (global_thread_step_over_chain_enqueue): ... this.  Update all
>> 	users.
>>         (thread_step_over_chain_remove): Rename to...
>>         (global_thread_step_over_chain_remove): ... this.  Update all
>> 	users.
>>         (thread_step_over_chain_next): Rename to...
>>         (global_thread_step_over_chain_next): ... this.  Update all
>> 	users.
>>         * infrun.h (step_over_queue_head): Rename to...
>> 	(global_thread_step_over_chain_head): ... this.  Update all
>> 	users.
>>         * infrun.c (step_over_queue_head): Rename to...
>> 	(global_thread_step_over_chain_head): ... this.  Update all
>> 	users.
>>         * thread.c (step_over_chain_remove): Rename to...
>> 	(thread_step_over_chain_remove): ... this.  Update all users.
>> 	(thread_step_over_chain_next): Rename to...
>>         (global_thread_step_over_chain_next): ... this.  Update all
>> 	users.
>>         (thread_step_over_chain_enqueue): Rename to...
>>         (global_thread_step_over_chain_enqueue): ... this.  Update all
>> 	users.
>>         (thread_step_over_chain_remove): Rename to...
>>         (global_thread_step_over_chain_remove): ... this.  Update all
>> 	users.
> 
> Tabs vs spaces in ChangeLog.  Otherwise OK.
> 

Fixed, thanks.

Simon

^ permalink raw reply	[flat|nested] 40+ messages in thread

* Re: [PATCH v2 04/14] gdb: get rid of get_displaced_stepping_state
  2020-12-04  1:48   ` Pedro Alves
@ 2020-12-04 21:06     ` Simon Marchi
  0 siblings, 0 replies; 40+ messages in thread
From: Simon Marchi @ 2020-12-04 21:06 UTC (permalink / raw)
  To: Pedro Alves, gdb-patches

On 2020-12-03 8:48 p.m., Pedro Alves wrote:
> On 12/2/20 3:47 PM, Simon Marchi via Gdb-patches wrote:
>> Remove function get_displaced_stepping_state.  When it was introduced,
>> inferiors' dispalced stepping state were kept in a linked list in
> 
> dispalced -> displaced
> were -> was

Fixed, thanks.

Simon

^ permalink raw reply	[flat|nested] 40+ messages in thread

* Re: [PATCH v2 08/14] gdb: introduce status enum for displaced step prepare/finish
  2020-12-04  1:49   ` Pedro Alves
@ 2020-12-04 21:08     ` Simon Marchi
  0 siblings, 0 replies; 40+ messages in thread
From: Simon Marchi @ 2020-12-04 21:08 UTC (permalink / raw)
  To: Pedro Alves, gdb-patches; +Cc: Simon Marchi

On 2020-12-03 8:49 p.m., Pedro Alves wrote:
> On 12/2/20 3:47 PM, Simon Marchi via Gdb-patches wrote:
>> +enum displaced_step_prepare_status
>> +{
>> +  /* A displaced stepping buffer was successfully allocated and prepared.  */
>> +  DISPLACED_STEP_PREPARE_STATUS_OK,
>> +
>> +  /* This particular instruction can't be displaced step, GDB should fall back
> 
> "be ... stepped"

Fixed, thanks.

Simon

^ permalink raw reply	[flat|nested] 40+ messages in thread

* Re: [PATCH v2 11/14] gdb: move displaced stepping logic to gdbarch, allow starting concurrent displaced steps
  2020-12-04  1:50   ` Pedro Alves
@ 2020-12-04 21:11     ` Simon Marchi
  0 siblings, 0 replies; 40+ messages in thread
From: Simon Marchi @ 2020-12-04 21:11 UTC (permalink / raw)
  To: Pedro Alves, gdb-patches; +Cc: Simon Marchi

On 2020-12-03 8:50 p.m., Pedro Alves wrote:
> Very good!  Only a few minor comments below.  Otherwise OK.
>
> On 12/2/20 3:48 PM, Simon Marchi via Gdb-patches wrote:
>
>> +displaced_step_finish_status
>> +displaced_step_buffer::finish (gdbarch *arch, thread_info *thread,
>> +			       gdb_signal sig)
>> +{
>> +  gdb_assert (thread->displaced_step_state.in_progress ());
>> +  gdb_assert (thread == m_current_thread);
>> +
>> +  /* Move this to a local variable so it's released in case something goes
>> +     wrong.  */
>> +  displaced_step_copy_insn_closure_up copy_insn_closure
>> +    = std::move (m_copy_insn_closure);
>> +  gdb_assert (copy_insn_closure != nullptr);
>> +
>> +  /* Reset m_current_thread immediately to mark the buffer as available, in case
>
> Uppercase m_current_thread.

Fixed.

>> +     something goes wrong below.  */
>> +  m_current_thread = nullptr;
>> +
>> +  /* Now that a buffer gets freed, tell GDB it can ask us to prepare a displaced
>
> How about "tell infrun" instead of "tell GDB"?  This is all in GDB.

Makes sense, fixed.

>> @@ -1927,14 +1833,41 @@ static step_over_what thread_still_needs_step_over (struct thread_info *tp);
>>  static bool
>>  start_step_over (void)
>>  {
>> -  struct thread_info *tp, *next;
>> +  thread_info *next;
>>
>>    /* Don't start a new step-over if we already have an in-line
>>       step-over operation ongoing.  */
>>    if (step_over_info_valid_p ())
>>      return false;
>>
>> -  for (tp = global_thread_step_over_chain_head; tp != NULL; tp = next)
>> +  /* Steal the global thread step over chain.  As we try to initiate displaced
>> +     steps, threads will be enqueued in the global chain if no buffers are
>> +     available.  If we iterated on the global chain directly, we might iterate
>> +     indefinitely.  */
>> +  thread_info *threads_to_step = global_thread_step_over_chain_head;
>> +  global_thread_step_over_chain_head = NULL;
>> +
>> +  infrun_debug_printf ("stealing global queue of threads to step, length = %d",
>> +		       thread_step_over_chain_length (threads_to_step));
>> +
>> +  bool started = false;
>> +
>> +  /* On scope exit (whatever the reason, return or exception), if there are
>> +     threads left in the THREADS_TO_STEP chain, put back these threads in the
>> +     global list.  */
>> +  SCOPE_EXIT {
>
> This format looks un-GNU-ish to me.  I've been putting the {
> on the following line, indented by two spaces, like if it was
> a if/for/while block, like:
>
>   SCOPE_EXIT
>     {
>
> ... unless the whole thing fits in one line:
>
>   SCOPE_EXIT { foo (); };

Makes sense, fixed.

>> +    if (threads_to_step == nullptr)
>> +      infrun_debug_printf ("step-over queue now empty");
>> +    else
>> +      {
>> +	infrun_debug_printf ("putting back %d threads to step in global queue",
>> +			     thread_step_over_chain_length (threads_to_step));
>> +
>> +	global_thread_step_over_chain_enqueue_chain (threads_to_step);
>> +      }
>> +  };
>> +
>> +  for (thread_info *tp = threads_to_step; tp != NULL; tp = next)
>>      {
>>        struct execution_control_state ecss;
>>        struct execution_control_state *ecs = &ecss;
>> @@ -1943,12 +1876,23 @@ start_step_over (void)
>>
>>        gdb_assert (!tp->stop_requested);
>>
>> -      next = global_thread_step_over_chain_next (tp);
>> +      next = thread_step_over_chain_next (threads_to_step, tp);
>>
>> -      /* If this inferior already has a displaced step in process,
>> -	 don't start a new one.  */
>> -      if (displaced_step_in_progress (tp->inf))
>> -	continue;
>> +      if (tp->inf->displaced_step_state.unavailable)
>> +	{
>> +	  /* The arch told us to not even try preparing another displaced step
>> +	     for this inferior.  Just leave the thread in THREADS_TO_STEP, it
>> +	     will get moved to the global chain on scope exit.  */
>> +	  continue;
>> +	}
>> +
>> +      /* Remove thread from the THREADS_TO_STEP chain.  If anything goes wrong
>> +	 while we try to prepare the displaced step, we don't add it back to
>> +	 the global step over chain.  This is to avoid a thread staying in the
>> +	 step over chain indefinitely if something go wrong when resuming it.
>
> something goes wrong

Fixed.

Thanks for the review!

Simon

^ permalink raw reply	[flat|nested] 40+ messages in thread

* Re: [PATCH v2 13/14] gdb: make displaced stepping implementation capable of managing multiple buffers
  2020-12-04  1:51   ` Pedro Alves
@ 2020-12-04 21:14     ` Simon Marchi
  0 siblings, 0 replies; 40+ messages in thread
From: Simon Marchi @ 2020-12-04 21:14 UTC (permalink / raw)
  To: Pedro Alves, gdb-patches; +Cc: Simon Marchi

On 2020-12-03 8:51 p.m., Pedro Alves wrote:
> On 12/2/20 3:48 PM, Simon Marchi via Gdb-patches wrote:
> 
>>    /* Save this, now that we know everything went fine.  */
>> -  m_copy_insn_closure = std::move (copy_insn_closure);
>> +  buffer->copy_insn_closure = std::move (copy_insn_closure);
>>  
>> -  /* Tell GDB not to try preparing a displaced step again for this inferior.  */
>> +  /* Tell GDB not to try preparing a displaced step again for this inferior if
>> +     all buffers are taken.  */
> 
> Another spot where "Tell GDB" -> "Tell infrun" would make sense, IMO.

Fixed it in patch 11 where it was first added.

Simon


^ permalink raw reply	[flat|nested] 40+ messages in thread

* Re: [PATCH v2 00/14] Concurrent displaced stepping
  2020-12-04  1:56 ` [PATCH v2 00/14] Concurrent displaced stepping Pedro Alves
@ 2020-12-04 21:52   ` Simon Marchi
  0 siblings, 0 replies; 40+ messages in thread
From: Simon Marchi @ 2020-12-04 21:52 UTC (permalink / raw)
  To: Pedro Alves, gdb-patches


On 2020-12-03 8:56 p.m., Pedro Alves wrote:
> Alright, I think I re-reviewed everything and other than some minor
> comments stuff and the like, this is all basically OK.

Thanks a lot for the reviews!  I think I fixed everything you pointed
out, so I'll push it after a last smoke test.

Simon

^ permalink raw reply	[flat|nested] 40+ messages in thread

end of thread, other threads:[~2020-12-04 21:53 UTC | newest]

Thread overview: 40+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-12-02 15:47 [PATCH v2 00/14] Concurrent displaced stepping Simon Marchi
2020-12-02 15:47 ` [PATCH v2 01/14] gdb: add inferior_execd observable Simon Marchi
2020-12-04  1:45   ` Pedro Alves
2020-12-02 15:47 ` [PATCH v2 02/14] gdb: clear inferior displaced stepping state and in-line step-over info on exec Simon Marchi
2020-12-02 17:16   ` Simon Marchi
2020-12-04  1:54     ` Pedro Alves
2020-12-04 20:49       ` Simon Marchi
2020-12-04  1:48   ` Pedro Alves
2020-12-04 21:03     ` Simon Marchi
2020-12-02 15:47 ` [PATCH v2 03/14] gdb: restore displaced step buffer bytes when another thread forks Simon Marchi
2020-12-04  1:48   ` Pedro Alves
2020-12-02 15:47 ` [PATCH v2 04/14] gdb: get rid of get_displaced_stepping_state Simon Marchi
2020-12-04  1:48   ` Pedro Alves
2020-12-04 21:06     ` Simon Marchi
2020-12-02 15:47 ` [PATCH v2 05/14] gdb: rename things related to step over chains Simon Marchi
2020-12-04  1:49   ` Pedro Alves
2020-12-04 21:04     ` Simon Marchi
2020-12-02 15:47 ` [PATCH v2 06/14] gdb: rename displaced_step_closure to displaced_step_copy_insn_closure Simon Marchi
2020-12-04  1:49   ` Pedro Alves
2020-12-02 15:47 ` [PATCH v2 07/14] gdb: rename displaced_step_fixup to displaced_step_finish Simon Marchi
2020-12-04  1:49   ` Pedro Alves
2020-12-02 15:47 ` [PATCH v2 08/14] gdb: introduce status enum for displaced step prepare/finish Simon Marchi
2020-12-04  1:49   ` Pedro Alves
2020-12-04 21:08     ` Simon Marchi
2020-12-02 15:48 ` [PATCH v2 09/14] gdb: pass inferior to get_linux_inferior_data Simon Marchi
2020-12-04  1:50   ` Pedro Alves
2020-12-02 15:48 ` [PATCH v2 10/14] gdb: move displaced stepping types to displaced-stepping.{h, c} Simon Marchi
2020-12-04  1:50   ` Pedro Alves
2020-12-02 15:48 ` [PATCH v2 11/14] gdb: move displaced stepping logic to gdbarch, allow starting concurrent displaced steps Simon Marchi
2020-12-04  1:50   ` Pedro Alves
2020-12-04 21:11     ` Simon Marchi
2020-12-02 15:48 ` [PATCH v2 12/14] gdb: change linux gdbarch data from post to pre-init Simon Marchi
2020-12-04  1:50   ` Pedro Alves
2020-12-02 15:48 ` [PATCH v2 13/14] gdb: make displaced stepping implementation capable of managing multiple buffers Simon Marchi
2020-12-04  1:51   ` Pedro Alves
2020-12-04 21:14     ` Simon Marchi
2020-12-02 15:48 ` [PATCH v2 14/14] gdb: use two displaced step buffers on amd64/Linux Simon Marchi
2020-12-04  1:51   ` Pedro Alves
2020-12-04  1:56 ` [PATCH v2 00/14] Concurrent displaced stepping Pedro Alves
2020-12-04 21:52   ` Simon Marchi

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).