]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commitdiff
gdb: protect some 'regcache_read_pc' calls
authorTankut Baris Aktemur <tankut.baris.aktemur@intel.com>
Thu, 14 May 2020 11:59:53 +0000 (13:59 +0200)
committerTankut Baris Aktemur <tankut.baris.aktemur@intel.com>
Thu, 14 May 2020 11:59:53 +0000 (13:59 +0200)
It possible that a thread whose PC we attempt to read is already dead.
In this case, 'regcache_read_pc' errors out.  This impacts the
"proceed" execution flow, where GDB quits early before having a chance
to check if there exists a pending event.  To remedy, keep going with
a 0 value for the PC if 'regcache_read_pc' fails.  Because the value
of PC before resuming a thread is mostly used for storing and checking
the next time the thread stops, this tolerance is expected to be
harmless for a dead thread/process.

gdb/ChangeLog:
2020-05-14  Tankut Baris Aktemur  <tankut.baris.aktemur@intel.com>

* regcache.c (regcache_read_pc_protected): New function
implementation that returns 0 if the PC cannot read via
'regcache_read_pc'.
* infrun.c (proceed): Call 'regcache_read_pc_protected'
instead of 'regcache_read_pc'.
(keep_going_pass_signal): Ditto.

gdbsupport/ChangeLog:
2020-05-14  Tankut Baris Aktemur  <tankut.baris.aktemur@intel.com>

* common-regcache.h (regcache_read_pc_protected): New function
declaration.

gdb/ChangeLog
gdb/infrun.c
gdb/regcache.c
gdbsupport/ChangeLog
gdbsupport/common-regcache.h

index 4f948d57a040e442648aee72792a362bc1dcca00..462884ce41ead6c7ecfe71a4976f083b2dda1a59 100644 (file)
@@ -1,3 +1,12 @@
+2020-05-14  Tankut Baris Aktemur  <tankut.baris.aktemur@intel.com>
+
+       * regcache.c (regcache_read_pc_protected): New function
+       implementation that returns 0 if the PC cannot read via
+       'regcache_read_pc'.
+       * infrun.c (proceed): Call 'regcache_read_pc_protected'
+       instead of 'regcache_read_pc'.
+       (keep_going_pass_signal): Ditto.
+
 2020-05-13  Tom Tromey  <tromey@adacore.com>
 
        * ada-lang.c (align_value): Remove.
index 3c6b201a9fc8cd006b084f5bc6160fa7a6abbb20..5e01336ab091b72eba524748cd6c7fab22efe4aa 100644 (file)
@@ -2995,7 +2995,8 @@ proceed (CORE_ADDR addr, enum gdb_signal siggnal)
   gdbarch = regcache->arch ();
   const address_space *aspace = regcache->aspace ();
 
-  pc = regcache_read_pc (regcache);
+  pc = regcache_read_pc_protected (regcache);
+
   thread_info *cur_thr = inferior_thread ();
 
   /* Fill in with reasonable starting values.  */
@@ -3122,7 +3123,7 @@ proceed (CORE_ADDR addr, enum gdb_signal siggnal)
      advanced.  Must do this before resuming any thread, as in
      all-stop/remote, once we resume we can't send any other packet
      until the target stops again.  */
-  cur_thr->prev_pc = regcache_read_pc (regcache);
+  cur_thr->prev_pc = regcache_read_pc_protected (regcache);
 
   {
     scoped_restore save_defer_tc = make_scoped_defer_target_commit_resume ();
@@ -7929,7 +7930,7 @@ keep_going_pass_signal (struct execution_control_state *ecs)
 
   /* Save the pc before execution, to compare with pc after stop.  */
   ecs->event_thread->prev_pc
-    = regcache_read_pc (get_thread_regcache (ecs->event_thread));
+    = regcache_read_pc_protected (get_thread_regcache (ecs->event_thread));
 
   if (ecs->event_thread->control.trap_expected)
     {
index 4f079c91a7f23a2ef8f15c0537fa03b7864ea9dc..1be794520ec34022bd2b931d20a4a8edd0a0d49a 100644 (file)
@@ -1220,6 +1220,24 @@ regcache_read_pc (struct regcache *regcache)
   return pc_val;
 }
 
+/* See gdbsupport/common-regcache.h.  */
+
+CORE_ADDR
+regcache_read_pc_protected (regcache *regcache)
+{
+  CORE_ADDR pc;
+  try
+    {
+      pc = regcache_read_pc (regcache);
+    }
+  catch (const gdb_exception_error &ex)
+    {
+      pc = 0;
+    }
+
+  return pc;
+}
+
 void
 regcache_write_pc (struct regcache *regcache, CORE_ADDR pc)
 {
index 194811e65cb7e76cf767071f3b25608a6af5b41d..636a3d34e145f683eea8db5f8884a53d19860e3c 100644 (file)
@@ -1,3 +1,8 @@
+2020-05-14  Tankut Baris Aktemur  <tankut.baris.aktemur@intel.com>
+
+       * common-regcache.h (regcache_read_pc_protected): New function
+       declaration.
+
 2020-04-28  Tankut Baris Aktemur  <tankut.baris.aktemur@intel.com>
 
        * gdb-sigmask.h: Fix typo (pthead_sigmask -> pthread_sigmask).
index 18446ff8416d052c244decb298a7faa8d0ba688a..650536e8a88cb59681a90cd67c351855a16f61df 100644 (file)
@@ -56,6 +56,11 @@ extern int regcache_register_size (const struct regcache *regcache, int n);
 
 extern CORE_ADDR regcache_read_pc (struct regcache *regcache);
 
+/* Read the PC register.  If PC cannot be read, return 0.
+   This is a wrapper around 'regcache_read_pc'.  */
+
+extern CORE_ADDR regcache_read_pc_protected (regcache *regcache);
+
 /* Read a raw register into a unsigned integer.  */
 extern enum register_status regcache_raw_read_unsigned
   (struct regcache *regcache, int regnum, ULONGEST *val);