This is the mail archive of the gdb-patches@sourceware.org mailing list for the GDB project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[commit] Eliminate read_pc_pid and write_pc_pid


Hello,

this patch replaces the functions read_pc_pid and write_pc_pid by
regcache_read_pc and regcache_write_pc, which take a regcache 
instead of a ptid argument.  This allows to use a regcache the
caller might already have available, instead of always using
get_thread_regcache (ptid).

Moving the regcache to the caller also makes the thread architecture
available there, which allows to get rid of current_gdbarch in a
number of places (in particular, nearly every call to 
gdbarch_decr_pc_after_break now uses the regcache arch).

This patch should not change the behaviour of GDB on any platform.

Tested on powerpc-linux and powerpc64-linux, committed to mainline.

Bye,
Ulrich


ChangeLog:

	* inferior.h (read_pc_pid, write_pc_pid): Remove.
	* regcache.h (regcache_read_pc, regcache_write_pc): Add prototypes.

	* regcache.c (read_pc_pid): Remove, replace by ...
	(regcache_read_pc): ... this function.
	(write_pc_pid): Remove, replace by ...
	(regcache_write_pc): ... this function.
	(read_pc, write_pc): Update.

	* infrun.c (displaced_step_prepare): Replace read_pc_pid and
	write_pc_pid by regcache_read_pc and regcache_write_pc.
	(displaced_step_fixup): Likewise.
	(resume): Likewise.  Use regcache arch instead of current_gdbarch.
	(prepare_to_proceed): Likewise.
	(proceed): Likewise.
	(adjust_pc_after_break): Likewise.
	(handle_inferior_event): Likewise.

	* linux-nat.c (cancel_breakpoint): Likewise.
	* linux-thread-db.c (check_event): Likewise.
	* aix-thread.c (aix_thread_wait): Likewise.
	* tracepoint.c (trace_dump_command): Likewise.


diff -urNp gdb-orig/gdb/aix-thread.c gdb-head/gdb/aix-thread.c
--- gdb-orig/gdb/aix-thread.c	2008-03-21 21:06:58.000000000 +0100
+++ gdb-head/gdb/aix-thread.c	2008-05-04 14:18:07.319802253 +0200
@@ -1010,11 +1010,16 @@ aix_thread_wait (ptid_t ptid, struct tar
     return pid_to_ptid (-1);
 
   /* Check whether libpthdebug might be ready to be initialized.  */
-  if (!pd_active && status->kind == TARGET_WAITKIND_STOPPED &&
-      status->value.sig == TARGET_SIGNAL_TRAP
-      && read_pc_pid (ptid)
-	 - gdbarch_decr_pc_after_break (current_gdbarch) == pd_brk_addr)
-    return pd_activate (0);
+  if (!pd_active && status->kind == TARGET_WAITKIND_STOPPED
+      && status->value.sig == TARGET_SIGNAL_TRAP)
+    {
+      struct regcache *regcache = get_thread_regcache (ptid);
+      struct gdbarch *gdbarch = get_regcache_arch (regcache);
+
+      if (regcache_read_pc (regcache)
+	  - gdbarch_decr_pc_after_break (gdbarch) == pd_brk_addr)
+	return pd_activate (0);
+    }
 
   return pd_update (0);
 }
diff -urNp gdb-orig/gdb/inferior.h gdb-head/gdb/inferior.h
--- gdb-orig/gdb/inferior.h	2008-05-03 01:25:57.000000000 +0200
+++ gdb-head/gdb/inferior.h	2008-05-04 14:18:07.324801533 +0200
@@ -151,12 +151,8 @@ extern void terminal_ours (void);
 
 extern CORE_ADDR read_pc (void);
 
-extern CORE_ADDR read_pc_pid (ptid_t);
-
 extern void write_pc (CORE_ADDR);
 
-extern void write_pc_pid (CORE_ADDR, ptid_t);
-
 extern CORE_ADDR unsigned_pointer_to_address (struct type *type,
 					      const gdb_byte *buf);
 extern void unsigned_address_to_pointer (struct type *type, gdb_byte *buf,
diff -urNp gdb-orig/gdb/infrun.c gdb-head/gdb/infrun.c
--- gdb-orig/gdb/infrun.c	2008-05-04 01:18:03.000000000 +0200
+++ gdb-head/gdb/infrun.c	2008-05-04 14:31:11.218941014 +0200
@@ -710,7 +710,7 @@ displaced_step_prepare (ptid_t ptid)
 
   displaced_step_clear ();
 
-  original = read_pc_pid (ptid);
+  original = regcache_read_pc (regcache);
 
   copy = gdbarch_displaced_step_location (gdbarch);
   len = gdbarch_max_insn_length (gdbarch);
@@ -736,7 +736,7 @@ displaced_step_prepare (ptid_t ptid)
   make_cleanup (cleanup_displaced_step_closure, closure);
 
   /* Resume execution at the copy.  */
-  write_pc_pid (copy, ptid);
+  regcache_write_pc (regcache, copy);
 
   discard_cleanups (old_cleanups);
 
@@ -805,9 +805,10 @@ displaced_step_fixup (ptid_t event_ptid,
     {
       /* Since the instruction didn't complete, all we can do is
          relocate the PC.  */
-      CORE_ADDR pc = read_pc_pid (event_ptid);
+      struct regcache *regcache = get_thread_regcache (event_ptid);
+      CORE_ADDR pc = regcache_read_pc (regcache);
       pc = displaced_step_original + (pc - displaced_step_copy);
-      write_pc_pid (pc, event_ptid);
+      regcache_write_pc (regcache, pc);
     }
 
   do_cleanups (old_cleanups);
@@ -889,7 +890,9 @@ resume (int step, enum target_signal sig
 {
   int should_resume = 1;
   struct cleanup *old_cleanups = make_cleanup (resume_cleanups, 0);
-  CORE_ADDR pc = read_pc ();
+  struct regcache *regcache = get_current_regcache ();
+  struct gdbarch *gdbarch = get_regcache_arch (regcache);
+  CORE_ADDR pc = regcache_read_pc (regcache);
   QUIT;
 
   if (debug_infrun)
@@ -916,9 +919,8 @@ resume (int step, enum target_signal sig
      breakpoints can't be removed.  So we have to test for it here.  */
   if (breakpoint_here_p (pc) == permanent_breakpoint_here)
     {
-      if (gdbarch_skip_permanent_breakpoint_p (current_gdbarch))
-	gdbarch_skip_permanent_breakpoint (current_gdbarch,
-					   get_current_regcache ());
+      if (gdbarch_skip_permanent_breakpoint_p (gdbarch))
+	gdbarch_skip_permanent_breakpoint (gdbarch, regcache);
       else
 	error (_("\
 The program is stopped at a permanent breakpoint, but GDB does not know\n\
@@ -933,7 +935,7 @@ a command like `return' or `jump' to con
      the comments for displaced_step_prepare explain why.  The
      comments in the handle_inferior event for dealing with 'random
      signals' explain what we do instead.  */
-  if (use_displaced_stepping (current_gdbarch)
+  if (use_displaced_stepping (gdbarch)
       && stepping_over_breakpoint
       && sig == TARGET_SIGNAL_0)
     {
@@ -944,10 +946,10 @@ a command like `return' or `jump' to con
 	return;
     }
 
-  if (step && gdbarch_software_single_step_p (current_gdbarch))
+  if (step && gdbarch_software_single_step_p (gdbarch))
     {
       /* Do it the hard way, w/temp breakpoints */
-      if (gdbarch_software_single_step (current_gdbarch, get_current_frame ()))
+      if (gdbarch_software_single_step (gdbarch, get_current_frame ()))
         {
           /* ...and don't ask hardware to do it.  */
           step = 0;
@@ -1034,7 +1036,7 @@ a command like `return' or `jump' to con
 	  resume_ptid = inferior_ptid;
 	}
 
-      if (gdbarch_cannot_step_breakpoint (current_gdbarch))
+      if (gdbarch_cannot_step_breakpoint (gdbarch))
 	{
 	  /* Most targets can step a breakpoint instruction, thus
 	     executing it normally.  But if this one cannot, just
@@ -1044,10 +1046,11 @@ a command like `return' or `jump' to con
 	}
 
       if (debug_displaced
-          && use_displaced_stepping (current_gdbarch)
+          && use_displaced_stepping (gdbarch)
           && stepping_over_breakpoint)
         {
-          CORE_ADDR actual_pc = read_pc_pid (resume_ptid);
+	  struct regcache *resume_regcache = get_thread_regcache (resume_ptid);
+          CORE_ADDR actual_pc = regcache_read_pc (resume_regcache);
           gdb_byte buf[4];
 
           fprintf_unfiltered (gdb_stdlog, "displaced: run 0x%s: ",
@@ -1110,22 +1113,24 @@ prepare_to_proceed (int step)
 
   /* Switched over from WAIT_PID.  */
   if (!ptid_equal (wait_ptid, minus_one_ptid)
-      && !ptid_equal (inferior_ptid, wait_ptid)
-      && breakpoint_here_p (read_pc_pid (wait_ptid)))
+      && !ptid_equal (inferior_ptid, wait_ptid))
     {
-      /* If stepping, remember current thread to switch back to.  */
-      if (step)
+      struct regcache *regcache = get_thread_regcache (wait_ptid);
+
+      if (breakpoint_here_p (regcache_read_pc (regcache)))
 	{
-	  deferred_step_ptid = inferior_ptid;
-	}
+	  /* If stepping, remember current thread to switch back to.  */
+	  if (step)
+	    deferred_step_ptid = inferior_ptid;
 
-      /* Switch back to WAIT_PID thread.  */
-      switch_to_thread (wait_ptid);
+	  /* Switch back to WAIT_PID thread.  */
+	  switch_to_thread (wait_ptid);
 
-      /* We return 1 to indicate that there is a breakpoint here,
-	 so we need to step over it before continuing to avoid
-	 hitting it straight away. */
-      return 1;
+	  /* We return 1 to indicate that there is a breakpoint here,
+	     so we need to step over it before continuing to avoid
+	     hitting it straight away. */
+	  return 1;
+	}
     }
 
   return 0;
@@ -1151,31 +1156,34 @@ static CORE_ADDR prev_pc;
 void
 proceed (CORE_ADDR addr, enum target_signal siggnal, int step)
 {
+  struct regcache *regcache = get_current_regcache ();
+  struct gdbarch *gdbarch = get_regcache_arch (regcache);
+  CORE_ADDR pc = regcache_read_pc (regcache);
   int oneproc = 0;
 
   if (step > 0)
-    step_start_function = find_pc_function (read_pc ());
+    step_start_function = find_pc_function (pc);
   if (step < 0)
     stop_after_trap = 1;
 
   if (addr == (CORE_ADDR) -1)
     {
-      if (read_pc () == stop_pc && breakpoint_here_p (read_pc ()))
+      if (pc == stop_pc && breakpoint_here_p (pc))
 	/* There is a breakpoint at the address we will resume at,
 	   step one instruction before inserting breakpoints so that
 	   we do not stop right away (and report a second hit at this
 	   breakpoint).  */
 	oneproc = 1;
-      else if (gdbarch_single_step_through_delay_p (current_gdbarch)
-              && gdbarch_single_step_through_delay (current_gdbarch,
-                                                    get_current_frame ()))
+      else if (gdbarch_single_step_through_delay_p (gdbarch)
+	       && gdbarch_single_step_through_delay (gdbarch,
+						     get_current_frame ()))
 	/* We stepped onto an instruction that needs to be stepped
 	   again before re-inserting the breakpoint, do so.  */
 	oneproc = 1;
     }
   else
     {
-      write_pc (addr);
+      regcache_write_pc (regcache, addr);
     }
 
   if (debug_infrun)
@@ -1205,14 +1213,14 @@ proceed (CORE_ADDR addr, enum target_sig
 	 inserted.  Otherwise we need to disable all breakpoints, step
 	 one instruction, and then re-add them when that step is
 	 finished.  */
-      if (!use_displaced_stepping (current_gdbarch))
+      if (!use_displaced_stepping (gdbarch))
 	remove_breakpoints ();
     }
 
   /* We can insert breakpoints if we're not trying to step over one,
      or if we are stepping over one but we're using displaced stepping
      to do so.  */
-  if (! stepping_over_breakpoint || use_displaced_stepping (current_gdbarch))
+  if (! stepping_over_breakpoint || use_displaced_stepping (gdbarch))
     insert_breakpoints ();
 
   if (siggnal != TARGET_SIGNAL_DEFAULT)
@@ -1247,10 +1255,10 @@ proceed (CORE_ADDR addr, enum target_sig
      the prev_pc value before calculating the line number.  This approach
      did not work because on platforms that use ptrace, the pc register
      cannot be read unless the inferior is stopped.  At that point, we
-     are not guaranteed the inferior is stopped and so the read_pc ()
+     are not guaranteed the inferior is stopped and so the regcache_read_pc ()
      call can fail.  Setting the prev_pc value here ensures the value is 
      updated correctly when the inferior is stopped.  */
-  prev_pc = read_pc ();
+  prev_pc = regcache_read_pc (get_current_regcache ());
 
   /* Resume inferior.  */
   resume (oneproc || step || bpstat_should_step (), stop_signal);
@@ -1615,11 +1623,13 @@ context_switch (struct execution_control
 static void
 adjust_pc_after_break (struct execution_control_state *ecs)
 {
+  struct regcache *regcache = get_thread_regcache (ecs->ptid);
+  struct gdbarch *gdbarch = get_regcache_arch (regcache);
   CORE_ADDR breakpoint_pc;
 
   /* If this target does not decrement the PC after breakpoints, then
      we have nothing to do.  */
-  if (gdbarch_decr_pc_after_break (current_gdbarch) == 0)
+  if (gdbarch_decr_pc_after_break (gdbarch) == 0)
     return;
 
   /* If we've hit a breakpoint, we'll normally be stopped with SIGTRAP.  If
@@ -1651,8 +1661,8 @@ adjust_pc_after_break (struct execution_
 
   /* Find the location where (if we've hit a breakpoint) the
      breakpoint would be.  */
-  breakpoint_pc = read_pc_pid (ecs->ptid) - gdbarch_decr_pc_after_break
-					    (current_gdbarch);
+  breakpoint_pc = regcache_read_pc (regcache)
+		  - gdbarch_decr_pc_after_break (gdbarch);
 
   /* Check whether there actually is a software breakpoint inserted
      at that location.  */
@@ -1680,7 +1690,7 @@ adjust_pc_after_break (struct execution_
 	  || !ptid_equal (ecs->ptid, inferior_ptid)
 	  || !currently_stepping (ecs)
 	  || prev_pc == breakpoint_pc)
-	write_pc_pid (breakpoint_pc, ecs->ptid);
+	regcache_write_pc (regcache, breakpoint_pc);
     }
 }
 
@@ -1918,7 +1928,7 @@ handle_inferior_event (struct execution_
       follow_exec (PIDGET (inferior_ptid), pending_follow.execd_pathname);
       xfree (pending_follow.execd_pathname);
 
-      stop_pc = read_pc_pid (ecs->ptid);
+      stop_pc = regcache_read_pc (get_thread_regcache (ecs->ptid));
       ecs->saved_inferior_ptid = inferior_ptid;
       inferior_ptid = ecs->ptid;
 
@@ -2004,7 +2014,7 @@ handle_inferior_event (struct execution_
      it here, before we set stop_pc.)  */
   displaced_step_fixup (ecs->ptid, stop_signal);
 
-  stop_pc = read_pc_pid (ecs->ptid);
+  stop_pc = regcache_read_pc (get_thread_regcache (ecs->ptid));
 
   if (debug_infrun)
     {
@@ -2143,7 +2153,11 @@ handle_inferior_event (struct execution_
 		 when they stop, or to re-poll the remote looking for
 		 this particular thread (i.e. temporarily enable
 		 schedlock).  */
-             if (read_pc_pid (singlestep_ptid) != singlestep_pc)
+
+	     CORE_ADDR new_singlestep_pc
+	       = regcache_read_pc (get_thread_regcache (singlestep_ptid));
+
+	     if (new_singlestep_pc != singlestep_pc)
 	       {
 		 if (debug_infrun)
 		   fprintf_unfiltered (gdb_stdlog, "infrun: unexpected thread,"
@@ -2154,7 +2168,7 @@ handle_inferior_event (struct execution_
 		    the context we want to use.  Just fudge our
 		    state and continue.  */
                  ecs->ptid = singlestep_ptid;
-                 stop_pc = read_pc_pid (ecs->ptid);
+                 stop_pc = new_singlestep_pc;
                }
              else
 	       {
diff -urNp gdb-orig/gdb/linux-nat.c gdb-head/gdb/linux-nat.c
--- gdb-orig/gdb/linux-nat.c	2008-05-03 19:10:25.000000000 +0200
+++ gdb-head/gdb/linux-nat.c	2008-05-04 14:18:07.416788292 +0200
@@ -2157,9 +2157,12 @@ cancel_breakpoint (struct lwp_info *lp)
      delete or disable the breakpoint, but the LWP will have already
      tripped on it.  */
 
-  if (breakpoint_inserted_here_p (read_pc_pid (lp->ptid) -
-				  gdbarch_decr_pc_after_break
-				  (current_gdbarch)))
+  struct regcache *regcache = get_thread_regcache (lp->ptid);
+  struct gdbarch *gdbarch = get_regcache_arch (regcache);
+  CORE_ADDR pc;
+
+  pc = regcache_read_pc (regcache) - gdbarch_decr_pc_after_break (gdbarch);
+  if (breakpoint_inserted_here_p (pc))
     {
       if (debug_linux_nat)
 	fprintf_unfiltered (gdb_stdlog,
@@ -2167,10 +2170,9 @@ cancel_breakpoint (struct lwp_info *lp)
 			    target_pid_to_str (lp->ptid));
 
       /* Back up the PC if necessary.  */
-      if (gdbarch_decr_pc_after_break (current_gdbarch))
-	write_pc_pid (read_pc_pid (lp->ptid) - gdbarch_decr_pc_after_break
-		      (current_gdbarch),
-		      lp->ptid);
+      if (gdbarch_decr_pc_after_break (gdbarch))
+	regcache_write_pc (regcache, pc);
+
       return 1;
     }
   return 0;
diff -urNp gdb-orig/gdb/linux-thread-db.c gdb-head/gdb/linux-thread-db.c
--- gdb-orig/gdb/linux-thread-db.c	2008-04-21 15:19:40.000000000 +0200
+++ gdb-head/gdb/linux-thread-db.c	2008-05-04 14:18:07.458782248 +0200
@@ -747,6 +747,8 @@ thread_db_detach (char *args, int from_t
 static void
 check_event (ptid_t ptid)
 {
+  struct regcache *regcache = get_thread_regcache (ptid);
+  struct gdbarch *gdbarch = get_regcache_arch (regcache);
   td_event_msg_t msg;
   td_thrinfo_t ti;
   td_err_e err;
@@ -754,7 +756,8 @@ check_event (ptid_t ptid)
   int loop = 0;
 
   /* Bail out early if we're not at a thread event breakpoint.  */
-  stop_pc = read_pc_pid (ptid) - gdbarch_decr_pc_after_break (current_gdbarch);
+  stop_pc = regcache_read_pc (regcache)
+	    - gdbarch_decr_pc_after_break (gdbarch);
   if (stop_pc != td_create_bp_addr && stop_pc != td_death_bp_addr)
     return;
 
diff -urNp gdb-orig/gdb/regcache.c gdb-head/gdb/regcache.c
--- gdb-orig/gdb/regcache.c	2008-02-27 02:07:42.000000000 +0100
+++ gdb-head/gdb/regcache.c	2008-05-04 14:18:07.464781384 +0200
@@ -805,22 +805,11 @@ regcache_raw_collect (const struct regca
 }
 
 
-/* read_pc, write_pc, etc.  Special handling for register PC.  */
-
-/* NOTE: cagney/2001-02-18: The functions read_pc_pid(), read_pc() and
-   read_sp(), will eventually be replaced by per-frame methods.
-   Instead of relying on the global INFERIOR_PTID, they will use the
-   contextual information provided by the FRAME.  These functions do
-   not belong in the register cache.  */
-
-/* NOTE: cagney/2003-06-07: The functions generic_target_write_pc(),
-   write_pc_pid() and write_pc(), all need to be replaced by something
-   that does not rely on global state.  But what?  */
+/* Special handling for register PC.  */
 
 CORE_ADDR
-read_pc_pid (ptid_t ptid)
+regcache_read_pc (struct regcache *regcache)
 {
-  struct regcache *regcache = get_thread_regcache (ptid);
   struct gdbarch *gdbarch = get_regcache_arch (regcache);
 
   CORE_ADDR pc_val;
@@ -837,21 +826,20 @@ read_pc_pid (ptid_t ptid)
       pc_val = gdbarch_addr_bits_remove (gdbarch, raw_val);
     }
   else
-    internal_error (__FILE__, __LINE__, _("read_pc_pid: Unable to find PC"));
-
+    internal_error (__FILE__, __LINE__,
+		    _("regcache_read_pc: Unable to find PC"));
   return pc_val;
 }
 
 CORE_ADDR
 read_pc (void)
 {
-  return read_pc_pid (inferior_ptid);
+  return regcache_read_pc (get_current_regcache ());
 }
 
 void
-write_pc_pid (CORE_ADDR pc, ptid_t ptid)
+regcache_write_pc (struct regcache *regcache, CORE_ADDR pc)
 {
-  struct regcache *regcache = get_thread_regcache (ptid);
   struct gdbarch *gdbarch = get_regcache_arch (regcache);
 
   if (gdbarch_write_pc_p (gdbarch))
@@ -861,13 +849,13 @@ write_pc_pid (CORE_ADDR pc, ptid_t ptid)
 				    gdbarch_pc_regnum (gdbarch), pc);
   else
     internal_error (__FILE__, __LINE__,
-		    _("write_pc_pid: Unable to update PC"));
+		    _("regcache_write_pc: Unable to update PC"));
 }
 
 void
 write_pc (CORE_ADDR pc)
 {
-  write_pc_pid (pc, inferior_ptid);
+  regcache_write_pc (get_current_regcache (), pc);
 }
 
 
diff -urNp gdb-orig/gdb/regcache.h gdb-head/gdb/regcache.h
--- gdb-orig/gdb/regcache.h	2008-02-18 17:37:17.000000000 +0100
+++ gdb-head/gdb/regcache.h	2008-05-04 14:18:07.469780664 +0200
@@ -94,6 +94,11 @@ void regcache_cooked_read_part (struct r
 void regcache_cooked_write_part (struct regcache *regcache, int regnum,
 				 int offset, int len, const gdb_byte *buf);
 
+/* Special routines to read/write the PC.  */
+
+extern CORE_ADDR regcache_read_pc (struct regcache *regcache);
+extern void regcache_write_pc (struct regcache *regcache, CORE_ADDR pc);
+
 /* Transfer a raw register [0..NUM_REGS) between the regcache and the
    target.  These functions are called by the target in response to a
    target_fetch_registers() or target_store_registers().  */
diff -urNp gdb-orig/gdb/tracepoint.c gdb-head/gdb/tracepoint.c
--- gdb-orig/gdb/tracepoint.c	2008-02-05 17:05:56.000000000 +0100
+++ gdb-head/gdb/tracepoint.c	2008-05-04 14:18:07.477779513 +0200
@@ -2558,6 +2558,8 @@ replace_comma (void *data)
 static void
 trace_dump_command (char *args, int from_tty)
 {
+  struct regcache *regcache;
+  struct gdbarch *gdbarch;
   struct tracepoint *t;
   struct action_line *action;
   char *action_exp, *next_comma;
@@ -2594,8 +2596,11 @@ trace_dump_command (char *args, int from
      to the tracepoint PC.  If not, then the current frame was
      collected during single-stepping.  */
 
-  stepping_frame = (t->address != (read_pc () - gdbarch_decr_pc_after_break
-						  (current_gdbarch)));
+  regcache = get_current_regcache ();
+  gdbarch = get_regcache_arch (regcache);
+
+  stepping_frame = (t->address != (regcache_read_pc (regcache)
+				   - gdbarch_decr_pc_after_break (gdbarch)));
 
   for (action = t->actions; action; action = action->next)
     {
-- 
  Dr. Ulrich Weigand
  GNU Toolchain for Linux on System z and Cell BE
  Ulrich.Weigand@de.ibm.com


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]