Multi-arch exec, more register reading avoidance
authorPedro Alves <palves@redhat.com>
Mon, 9 Oct 2017 17:11:01 +0000 (18:11 +0100)
committerPedro Alves <palves@redhat.com>
Mon, 9 Oct 2017 17:11:01 +0000 (18:11 +0100)
As mentioned in commit bf93d7ba9931 ("Add thread after updating
gdbarch when exec'ing"), we should avoid doing register reads after a
process does an exec and before we've updated that inferior's gdbarch.
Otherwise, we may interpret the registers using the wrong
architecture.

There's still (at least) one case where we still read registers
post-exec with the pre-exec architecture.  That's when infrun decides
it needs to switch context to the exec'ing thread.  I.e., if the exec
event is processed at a time when the current thread is not already
the exec'ing thread, then we get (with the test added by this commit):

  continue
  Continuing.
  Truncated register 50 in remote 'g' packet
  Truncated register 50 in remote 'g' packet
  (gdb) FAIL: gdb.multi/multi-arch-exec.exp: selected_thread=2: follow_exec_mode=same: continue across exec that changes architecture

The fix is to avoid reading registers when switching context in this
case.

(I'd be nice to get rid of the constant stop_pc reading when switching
threads, but that'd be a deeper change.)

gdb/ChangeLog:
2017-10-09  Pedro Alves  <palves@redhat.com>

* infrun.c (handle_inferior_event_1) <TARGET_WAITKIND_EXECD>: Skip
reading registers when switching context.

gdb/testsuite/ChangeLog:
2017-10-09  Pedro Alves  <palves@redhat.com>

* gdb.multi/multi-arch-exec.c: Include <pthread.h> and <assert.h>.
(barrier): New.
(thread_start, all_started): New functions.
(main): Spawn new thread and wait until it is scheduled.
* gdb.multi/multi-arch-exec.exp: Build $srcfile1 with the pthreads
option.
(do_test): Add 'selected_thread' parameter.  Run to all_started
instead of main.  Explicitly set the breakpoint at main.  Switch
to the SELECTED_THREAD thread.
(top level): Test handling the exec event with either the main
thread or the second thread selected.

gdb/ChangeLog
gdb/infrun.c
gdb/testsuite/ChangeLog
gdb/testsuite/gdb.multi/multi-arch-exec.c
gdb/testsuite/gdb.multi/multi-arch-exec.exp

index b04da8bd443d490036dbbd77d4ccdf231a5872fb..f1fd73ce201491f8d8c96854287e985318afdcc4 100644 (file)
@@ -1,3 +1,8 @@
+2017-10-09  Pedro Alves  <palves@redhat.com>
+
+       * infrun.c (handle_inferior_event_1) <TARGET_WAITKIND_EXECD>: Skip
+       reading registers when switching context.
+
 2017-10-09  John Baldwin  <jhb@FreeBSD.org>
 
        * fbsd-nat.c (fbsd_siginfo_size): Use gdbarch_long_bit.
index e82f61f079f47339797308efb9214e9c564d6649..113add6fed48df86a9581ce5b61eababdae898bb 100644 (file)
@@ -5307,8 +5307,11 @@ Cannot fill $_exitsignal with the correct signal number.\n"));
       if (debug_infrun)
         fprintf_unfiltered (gdb_stdlog, "infrun: TARGET_WAITKIND_EXECD\n");
 
+      /* Note we can't read registers yet (the stop_pc), because we
+        don't yet know the inferior's post-exec architecture.
+        'stop_pc' is explicitly read below instead.  */
       if (!ptid_equal (ecs->ptid, inferior_ptid))
-       context_switch (ecs->ptid);
+       switch_to_thread_no_regs (ecs->event_thread);
 
       /* Do whatever is necessary to the parent branch of the vfork.  */
       handle_vfork_child_exec_or_exit (1);
index bb62d222ba57d107968704fc74ea0eec529d327e..e7b63ff2abce02a6a618880784154599f34546ef 100644 (file)
@@ -1,3 +1,17 @@
+2017-10-09  Pedro Alves  <palves@redhat.com>
+
+       * gdb.multi/multi-arch-exec.c: Include <pthread.h> and <assert.h>.
+       (barrier): New.
+       (thread_start, all_started): New functions.
+       (main): Spawn new thread and wait until it is scheduled.
+       * gdb.multi/multi-arch-exec.exp: Build $srcfile1 with the pthreads
+       option.
+       (do_test): Add 'selected_thread' parameter.  Run to all_started
+       instead of main.  Explicitly set the breakpoint at main.  Switch
+       to the SELECTED_THREAD thread.
+       (top level): Test handling the exec event with either the main
+       thread or the second thread selected.
+
 2017-10-09  Pedro Alves  <palves@redhat.com>
 
        * gdb.base/print-file-var-main.c: Fix get_version_2 value check
index 1a443dee7bf4f6c57967f7ef40853247d2744f26..f43a5a8b6a747281d1d1738a7221eea8a618f365 100644 (file)
 #include <unistd.h>
 #include <limits.h>
 #include <string.h>
+#include <pthread.h>
+
+#define NUM_THREADS 1
+
+static pthread_barrier_t barrier;
+
+static void *
+thread_start (void *arg)
+{
+  pthread_barrier_wait (&barrier);
+
+  while (1)
+    sleep (1);
+  return NULL;
+}
+
+static void
+all_started (void)
+{
+}
 
 int
 main (int argc, char ** argv)
 {
   char prog[PATH_MAX];
+  pthread_t thread;
   int len;
 
   strcpy (prog, argv[0]);
@@ -33,6 +54,12 @@ main (int argc, char ** argv)
   memcpy (prog + len - 15, "multi-arch-exec-hello", 21);
   prog[len + 6] = 0;
 
+  pthread_barrier_init (&barrier, NULL, NUM_THREADS + 1);
+  pthread_create (&thread, NULL, thread_start, NULL);
+
+  pthread_barrier_wait (&barrier);
+  all_started ();
+
   execl (prog,
          prog,
          (char *) NULL);
index 27a0d602a124d50c145c34b33e135dbe32cad736..3196cf3f3adc1d5c89f3c86105036c0d9c85c23a 100644 (file)
@@ -53,7 +53,7 @@ if [istarget "s390*-*-*"] {
 }
 
 if { [prepare_for_testing "failed to prepare" ${exec1} "${srcfile1}" \
-         [list debug \
+         [list debug pthreads \
               additional_flags=${march1}]] } {
     return -1
 }
@@ -76,22 +76,38 @@ if { [prepare_for_testing "failed to prepare" ${exec2} "${srcfile2}" \
     return -1
 }
 
-proc do_test { mode } {
+proc do_test { mode selected_thread } {
        global exec1
 
        clean_restart ${exec1}
-       if ![runto_main] then {
-           fail "couldn't run to main"
+       if ![runto all_started] then {
+           fail "couldn't run to all_started"
            return -1
        }
 
+       # Delete the breakpoint at 'all_started' otherwise GDB may
+       # switch context back to thread 1 to step over the breakpoint.
+       delete_breakpoints
+
+       # A location for this breakpoint should be found in the new
+       # post-exec image too.
+       gdb_breakpoint main
+
+       gdb_test "thread $selected_thread" "Switching to thread $selected_thread .*"
+
        gdb_test_no_output "set follow-exec-mode $mode"
 
        # Test that GDB updates the target description / arch successfuly
        # after the exec.
-       gdb_test "continue" "Breakpoint 1, main.*" "continue across exec that changes architecture"
+       gdb_test "continue" "Breakpoint 2, main.*" "continue across exec that changes architecture"
 }
 
-foreach follow_exec_mode {"same" "new"} {
-    do_test $follow_exec_mode
+# Test handling the exec event with either the main thread or the
+# second thread selected.  This tries to ensure that GDB doesn't read
+# registers off of the execing thread before figuring out its
+# architecture.
+foreach_with_prefix selected_thread {1 2} {
+    foreach_with_prefix follow_exec_mode {"same" "new"} {
+       do_test $follow_exec_mode $selected_thread
+    }
 }
This page took 0.041183 seconds and 4 git commands to generate.