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]

Re: [PATCH 4/6] gdbserver: Delimit debugging output for readability


Pedro Alves writes:
 > On 12/17/2013 09:47 PM, Doug Evans wrote:
 > > +   We could also use indentation, but that's adds more complexity.
 > 
 > "but that adds"
 > 
 > Looks good to me, though I think it'd be better to write it as:
 > 
 > linux_wait_1 ()
 > ...
 > /* Debugging output is delimited to make it easier to read.  */
 > #define DELIMITER "====\n"
 >   if (debug_threads)
 >     fprintf (stderr, DELIMITER "linux_wait_1: [%s]\n", target_pid_to_str (ptid));
 >   ...
 >   ...
 >   if (debug_threads)
 >     fprintf (stderr, "linux_wait_1 ret = null_ptid\n" DELIMITER);
 > 
 > #undef DELIMITER
 > }

Hi.
While going through the reviews, I found myself wanting something more,
namely timestamps (like "set debug timestamp on" in gdb).

This patch adds a check for gettimeofday and doesn't fall back to using one
in libiberty or gnulib, leaving that for another day.

It also adds macro FUNCTION_NAME, based on the implementation in gdb_assert.h.
A subsequent patch will use this when replacing abbreviations with
function names.

Unlike gdb, I didn't add an option to enable timestamps, you get them
automagically with --debug.  I can do that if people want.

I had a version of this patch that used indentation to represent
the heirarchy of debug_level_{enter,exit}.  I left the basic support
in thinking it might be useful.  I can either remove it or add the indentation,
but I found myself not really wanting the indentation.

2014-01-13  Doug Evans  <dje@google.com>

	* configure.ac (AC_CHECK_FUNCS): Add test for gettimeofday.
	* configure: Regenerate.
	* config.in: Regenerate.
	* linux-aarch64-low.c (*): Update all debugging printfs to use
	debug_printf instead of fprintf.
	* linux-arm-low.c (*): Ditto.
	* linux-cris-low.c (*): Ditto.
	* linux-crisv32-low.c (*): Ditto.
	* linux-m32r-low.c (*): Ditto.
	* linux-low.c (*): Ditto.
	* linux-sparc-low.c (*): Ditto.
	* linux-x86-low.c (*): Ditto.
	(linux_wait_1): Add calls to debug_level_enter, debug_level_exit.
	(linux_wait): Remove redundant debugging printf.
	(stop_all_lwps): Add calls to debug_level_enter, debug_level_exit.
	(linux_resume, unstop_all_lwps): Ditto.
	* mem-break.c (*): Update all debugging printfs to use
	debug_printf instead of fprintf.
	* remote-utils.c (*): Ditto.
	* thread-db.c (*): Ditto.
	* server.c (*): Ditto.
	(start_inferior): Replace call to fflush with call to debug_flush.
	(main): Add calls to debug_level_reset.
	* tracepoint.c (trace_debug_1 [!IN_PROCESS_AGENT]): Add version of
	trace_debug_1 that uses debug_printf.
	(tracepoint_look_up_symbols): Update all debugging printfs to use
	debug_printf instead of fprintf.
	* utils.c: #include <sys/time.h>.
	(debug_nesting_level): New static global.
	(debug_printf, debug_flush): New functions.
	(debug_level_reset, debug_level_incr): New functions.
	* utils.h (FUNCTION_NAME): New macro.
	(debug_printf, debug_flush): Declare.
	(debug_level_reset, debug_level_incr): Declare.
	(debug_level_enter, debug_level_exit): New macros.

diff --git a/gdb/gdbserver/config.in b/gdb/gdbserver/config.in
index e33cdea..1b9751f 100644
--- a/gdb/gdbserver/config.in
+++ b/gdb/gdbserver/config.in
@@ -66,6 +66,9 @@
 /* Define to 1 if you have the `getrlimit' function. */
 #undef HAVE_GETRLIMIT
 
+/* Define to 1 if you have the `gettimeofday' function. */
+#undef HAVE_GETTIMEOFDAY
+
 /* Define to 1 if you have the <inttypes.h> header file. */
 #undef HAVE_INTTYPES_H
 
diff --git a/gdb/gdbserver/configure b/gdb/gdbserver/configure
index fac7fb3..6d8a6a7 100755
--- a/gdb/gdbserver/configure
+++ b/gdb/gdbserver/configure
@@ -4728,7 +4728,7 @@ fi
 
 done
 
-for ac_func in pread pwrite pread64 readlink
+for ac_func in gettimeofday pread pwrite pread64 readlink
 do :
   as_ac_var=`$as_echo "ac_cv_func_$ac_func" | $as_tr_sh`
 ac_fn_c_check_func "$LINENO" "$ac_func" "$as_ac_var"
diff --git a/gdb/gdbserver/configure.ac b/gdb/gdbserver/configure.ac
index dc8131c..0dfa4a9 100644
--- a/gdb/gdbserver/configure.ac
+++ b/gdb/gdbserver/configure.ac
@@ -82,7 +82,7 @@ AC_CHECK_HEADERS(sgtty.h termio.h termios.h sys/reg.h string.h dnl
 		 errno.h fcntl.h signal.h sys/file.h malloc.h dnl
 		 sys/ioctl.h netinet/in.h sys/socket.h netdb.h dnl
 		 netinet/tcp.h arpa/inet.h)
-AC_CHECK_FUNCS(pread pwrite pread64 readlink)
+AC_CHECK_FUNCS(gettimeofday pread pwrite pread64 readlink)
 AC_REPLACE_FUNCS(vasprintf vsnprintf)
 
 GDB_AC_COMMON
diff --git a/gdb/gdbserver/linux-aarch64-low.c b/gdb/gdbserver/linux-aarch64-low.c
index 1b0da6c..e7d3e4f 100644
--- a/gdb/gdbserver/linux-aarch64-low.c
+++ b/gdb/gdbserver/linux-aarch64-low.c
@@ -323,7 +323,7 @@ aarch64_get_pc (struct regcache *regcache)
 
   collect_register_by_name (regcache, "pc", &pc);
   if (debug_threads)
-    fprintf (stderr, "stop pc is %08lx\n", pc);
+    debug_printf ("stop pc is %08lx\n", pc);
   return pc;
 }
 
diff --git a/gdb/gdbserver/linux-arm-low.c b/gdb/gdbserver/linux-arm-low.c
index da5085c..fb6ff68 100644
--- a/gdb/gdbserver/linux-arm-low.c
+++ b/gdb/gdbserver/linux-arm-low.c
@@ -256,7 +256,7 @@ arm_get_pc (struct regcache *regcache)
   unsigned long pc;
   collect_register_by_name (regcache, "pc", &pc);
   if (debug_threads)
-    fprintf (stderr, "stop pc is %08lx\n", pc);
+    debug_printf ("stop pc is %08lx\n", pc);
   return pc;
 }
 
diff --git a/gdb/gdbserver/linux-cris-low.c b/gdb/gdbserver/linux-cris-low.c
index d229d43..2abd987 100644
--- a/gdb/gdbserver/linux-cris-low.c
+++ b/gdb/gdbserver/linux-cris-low.c
@@ -67,7 +67,7 @@ cris_get_pc (struct regcache *regcache, void)
   unsigned long pc;
   collect_register_by_name (regcache, "pc", &pc);
   if (debug_threads)
-    fprintf (stderr, "stop pc is %08lx\n", pc);
+    debug_printf ("stop pc is %08lx\n", pc);
   return pc;
 }
 
diff --git a/gdb/gdbserver/linux-crisv32-low.c b/gdb/gdbserver/linux-crisv32-low.c
index e4ebb62..a16a656 100644
--- a/gdb/gdbserver/linux-crisv32-low.c
+++ b/gdb/gdbserver/linux-crisv32-low.c
@@ -63,7 +63,7 @@ cris_get_pc (struct regcache *regcache)
   unsigned long pc;
   collect_register_by_name (regcache, "pc", &pc);
   if (debug_threads)
-    fprintf (stderr, "stop pc is %08lx\n", pc);
+    debug_printf ("stop pc is %08lx\n", pc);
   return pc;
 }
 
diff --git a/gdb/gdbserver/linux-low.c b/gdb/gdbserver/linux-low.c
index 2bc619a..6ab0966 100644
--- a/gdb/gdbserver/linux-low.c
+++ b/gdb/gdbserver/linux-low.c
@@ -475,7 +475,7 @@ get_pc (struct lwp_info *lwp)
   pc = (*the_low_target.get_pc) (regcache);
 
   if (debug_threads)
-    fprintf (stderr, "pc is 0x%lx\n", (long) pc);
+    debug_printf ("pc is 0x%lx\n", (long) pc);
 
   current_inferior = saved_inferior;
   return pc;
@@ -520,7 +520,7 @@ get_stop_pc (struct lwp_info *lwp)
     stop_pc -= the_low_target.decr_pc_after_break;
 
   if (debug_threads)
-    fprintf (stderr, "stop pc is 0x%lx\n", (long) stop_pc);
+    debug_printf ("stop pc is 0x%lx\n", (long) stop_pc);
 
   return stop_pc;
 }
@@ -692,8 +692,7 @@ linux_attach_lwp_1 (unsigned long lwpid, int initial)
   if (linux_proc_pid_is_stopped (lwpid))
     {
       if (debug_threads)
-	fprintf (stderr,
-		 "Attached to a stopped process\n");
+	debug_printf ("Attached to a stopped process\n");
 
       /* The process is definitely stopped.  It is in a job control
 	 stop, unless the kernel predates the TASK_STOPPED /
@@ -824,8 +823,8 @@ linux_attach (unsigned long pid)
 		      new_threads_found++;
 
 		      if (debug_threads)
-			fprintf (stderr, "\
-Found and attached to new lwp %ld\n", lwp);
+			debug_printf ("Found and attached to new lwp %ld\n",
+				      lwp);
 		    }
 		}
 
@@ -898,18 +897,16 @@ linux_kill_one_lwp (struct lwp_info *lwp)
   errno = 0;
   kill (pid, SIGKILL);
   if (debug_threads)
-    fprintf (stderr,
-	     "LKL:  kill (SIGKILL) %s, 0, 0 (%s)\n",
-	     target_pid_to_str (ptid_of (lwp)),
-	     errno ? strerror (errno) : "OK");
+    debug_printf ("LKL:  kill (SIGKILL) %s, 0, 0 (%s)\n",
+		  target_pid_to_str (ptid_of (lwp)),
+		  errno ? strerror (errno) : "OK");
 
   errno = 0;
   ptrace (PTRACE_KILL, pid, (PTRACE_TYPE_ARG3) 0, (PTRACE_TYPE_ARG4) 0);
   if (debug_threads)
-    fprintf (stderr,
-	     "LKL:  PTRACE_KILL %s, 0, 0 (%s)\n",
-	     target_pid_to_str (ptid_of (lwp)),
-	     errno ? strerror (errno) : "OK");
+    debug_printf ("LKL:  PTRACE_KILL %s, 0, 0 (%s)\n",
+		  target_pid_to_str (ptid_of (lwp)),
+		  errno ? strerror (errno) : "OK");
 }
 
 /* Callback for `find_inferior'.  Kills an lwp of a given process,
@@ -934,8 +931,8 @@ kill_one_lwp_callback (struct inferior_list_entry *entry, void *args)
   if (lwpid_of (lwp) == pid)
     {
       if (debug_threads)
-	fprintf (stderr, "lkop: is last of process %s\n",
-		 target_pid_to_str (entry->id));
+	debug_printf ("lkop: is last of process %s\n",
+		      target_pid_to_str (entry->id));
       return 0;
     }
 
@@ -975,14 +972,14 @@ linux_kill (int pid)
   if (lwp == NULL)
     {
       if (debug_threads)
-	fprintf (stderr, "lk_1: cannot find lwp %ld, for pid: %d\n",
-		 lwpid_of (lwp), pid);
+	debug_printf ("lk_1: cannot find lwp %ld, for pid: %d\n",
+		      lwpid_of (lwp), pid);
     }
   else
     {
       if (debug_threads)
-	fprintf (stderr, "lk_1: killing lwp %ld, for pid: %d\n",
-		 lwpid_of (lwp), pid);
+	debug_printf ("lk_1: killing lwp %ld, for pid: %d\n",
+		      lwpid_of (lwp), pid);
 
       do
 	{
@@ -1031,9 +1028,8 @@ get_detach_signal (struct thread_info *thread)
   if (!WIFSTOPPED (status))
     {
       if (debug_threads)
-	fprintf (stderr,
-		 "GPS: lwp %s hasn't stopped: no pending signal\n",
-		 target_pid_to_str (ptid_of (lp)));
+	debug_printf ("GPS: lwp %s hasn't stopped: no pending signal\n",
+		      target_pid_to_str (ptid_of (lp)));
       return 0;
     }
 
@@ -1041,10 +1037,9 @@ get_detach_signal (struct thread_info *thread)
   if (WSTOPSIG (status) == SIGTRAP && status >> 16 != 0)
     {
       if (debug_threads)
-	fprintf (stderr,
-		 "GPS: lwp %s had stopped with extended "
-		 "status: no pending signal\n",
-		 target_pid_to_str (ptid_of (lp)));
+	debug_printf ("GPS: lwp %s had stopped with extended "
+		      "status: no pending signal\n",
+		      target_pid_to_str (ptid_of (lp)));
       return 0;
     }
 
@@ -1053,10 +1048,9 @@ get_detach_signal (struct thread_info *thread)
   if (program_signals_p && !program_signals[signo])
     {
       if (debug_threads)
-	fprintf (stderr,
-		 "GPS: lwp %s had signal %s, but it is in nopass state\n",
-		 target_pid_to_str (ptid_of (lp)),
-		 gdb_signal_to_string (signo));
+	debug_printf ("GPS: lwp %s had signal %s, but it is in nopass state\n",
+		      target_pid_to_str (ptid_of (lp)),
+		      gdb_signal_to_string (signo));
       return 0;
     }
   else if (!program_signals_p
@@ -1066,20 +1060,19 @@ get_detach_signal (struct thread_info *thread)
 	   && (signo == GDB_SIGNAL_TRAP || signo == GDB_SIGNAL_INT))
     {
       if (debug_threads)
-	fprintf (stderr,
-		 "GPS: lwp %s had signal %s, "
-		 "but we don't know if we should pass it.  Default to not.\n",
-		 target_pid_to_str (ptid_of (lp)),
-		 gdb_signal_to_string (signo));
+	debug_printf ("GPS: lwp %s had signal %s, "
+		      "but we don't know if we should pass it. "
+		      "Default to not.\n",
+		      target_pid_to_str (ptid_of (lp)),
+		      gdb_signal_to_string (signo));
       return 0;
     }
   else
     {
       if (debug_threads)
-	fprintf (stderr,
-		 "GPS: lwp %s has pending signal %s: delivering it.\n",
-		 target_pid_to_str (ptid_of (lp)),
-		 gdb_signal_to_string (signo));
+	debug_printf ("GPS: lwp %s has pending signal %s: delivering it.\n",
+		      target_pid_to_str (ptid_of (lp)),
+		      gdb_signal_to_string (signo));
 
       return WSTOPSIG (status);
     }
@@ -1100,9 +1093,8 @@ linux_detach_one_lwp (struct inferior_list_entry *entry, void *args)
   if (lwp->stop_expected)
     {
       if (debug_threads)
-	fprintf (stderr,
-		 "Sending SIGCONT to %s\n",
-		 target_pid_to_str (ptid_of (lwp)));
+	debug_printf ("Sending SIGCONT to %s\n",
+		      target_pid_to_str (ptid_of (lwp)));
 
       kill_lwp (lwpid_of (lwp), SIGCONT);
       lwp->stop_expected = 0;
@@ -1276,7 +1268,7 @@ linux_wait_for_lwp (ptid_t ptid, int *wstatp, int options)
   struct lwp_info *child = NULL;
 
   if (debug_threads)
-    fprintf (stderr, "linux_wait_for_lwp: %s\n", target_pid_to_str (ptid));
+    debug_printf ("linux_wait_for_lwp: %s\n", target_pid_to_str (ptid));
 
   if (ptid_equal (ptid, minus_one_ptid))
     to_wait_for = -1;			/* any child */
@@ -1297,7 +1289,7 @@ retry:
       && (!WIFSTOPPED (*wstatp)
 	  || (WSTOPSIG (*wstatp) != 32
 	      && WSTOPSIG (*wstatp) != 33)))
-    fprintf (stderr, "Got an event from %d (%x)\n", ret, *wstatp);
+    debug_printf ("Got an event from %d (%x)\n", ret, *wstatp);
 
   child = find_lwp_pid (pid_to_ptid (ret));
 
@@ -1404,7 +1396,7 @@ retry:
       current_inferior = get_lwp_thread (child);
       regcache = get_thread_regcache (current_inferior, 1);
       pc = (*the_low_target.get_pc) (regcache);
-      fprintf (stderr, "linux_wait_for_lwp: pc is 0x%lx\n", (long) pc);
+      debug_printf ("linux_wait_for_lwp: pc is 0x%lx\n", (long) pc);
       current_inferior = saved_inferior;
     }
 
@@ -1450,7 +1442,7 @@ handle_tracepoints (struct lwp_info *lwp)
   if (tpoint_related_event)
     {
       if (debug_threads)
-	fprintf (stderr, "got a tracepoint event\n");
+	debug_printf ("got a tracepoint event\n");
       return 1;
     }
 
@@ -1501,9 +1493,9 @@ maybe_move_out_of_jump_pad (struct lwp_info *lwp, int *wstat)
       int r;
 
       if (debug_threads)
-	fprintf (stderr, "\
-Checking whether LWP %ld needs to move out of the jump pad.\n",
-		 lwpid_of (lwp));
+	debug_printf ("Checking whether LWP %ld needs to move out of the "
+		      "jump pad.\n",
+		      lwpid_of (lwp));
 
       r = linux_fast_tracepoint_collecting (lwp, &status);
 
@@ -1527,9 +1519,9 @@ Checking whether LWP %ld needs to move out of the jump pad.\n",
 		}
 
 	      if (debug_threads)
-		fprintf (stderr, "\
-Checking whether LWP %ld needs to move out of the jump pad...it does\n",
-		 lwpid_of (lwp));
+		debug_printf ("Checking whether LWP %ld needs to move out of "
+			      "the jump pad...it does\n",
+			      lwpid_of (lwp));
 	      current_inferior = saved_inferior;
 
 	      return 1;
@@ -1582,9 +1574,8 @@ Checking whether LWP %ld needs to move out of the jump pad...it does\n",
 	  if (lwp->exit_jump_pad_bkpt != NULL)
 	    {
 	      if (debug_threads)
-		fprintf (stderr,
-			 "Cancelling fast exit-jump-pad: removing bkpt. "
-			 "stopping all threads momentarily.\n");
+		debug_printf ("Cancelling fast exit-jump-pad: removing bkpt. "
+			      "stopping all threads momentarily.\n");
 
 	      stop_all_lwps (1, lwp);
 	      cancel_breakpoints ();
@@ -1600,9 +1591,9 @@ Checking whether LWP %ld needs to move out of the jump pad...it does\n",
     }
 
   if (debug_threads)
-    fprintf (stderr, "\
-Checking whether LWP %ld needs to move out of the jump pad...no\n",
-	     lwpid_of (lwp));
+    debug_printf ("Checking whether LWP %ld needs to move out of the "
+		  "jump pad...no\n",
+		  lwpid_of (lwp));
 
   current_inferior = saved_inferior;
   return 0;
@@ -1617,8 +1608,8 @@ enqueue_one_deferred_signal (struct lwp_info *lwp, int *wstat)
   struct pending_signals *p_sig;
 
   if (debug_threads)
-    fprintf (stderr, "\
-Deferring signal %d for LWP %ld.\n", WSTOPSIG (*wstat), lwpid_of (lwp));
+    debug_printf ("Deferring signal %d for LWP %ld.\n",
+		  WSTOPSIG (*wstat), lwpid_of (lwp));
 
   if (debug_threads)
     {
@@ -1627,11 +1618,10 @@ Deferring signal %d for LWP %ld.\n", WSTOPSIG (*wstat), lwpid_of (lwp));
       for (sig = lwp->pending_signals_to_report;
 	   sig != NULL;
 	   sig = sig->prev)
-	fprintf (stderr,
-		 "   Already queued %d\n",
-		 sig->signal);
+	debug_printf ("   Already queued %d\n",
+		      sig->signal);
 
-      fprintf (stderr, "   (no more currently queued signals)\n");
+      debug_printf ("   (no more currently queued signals)\n");
     }
 
   /* Don't enqueue non-RT signals if they are already in the deferred
@@ -1648,11 +1638,10 @@ Deferring signal %d for LWP %ld.\n", WSTOPSIG (*wstat), lwpid_of (lwp));
 	  if (sig->signal == WSTOPSIG (*wstat))
 	    {
 	      if (debug_threads)
-		fprintf (stderr,
-			 "Not requeuing already queued non-RT signal %d"
-			 " for LWP %ld\n",
-			 sig->signal,
-			 lwpid_of (lwp));
+		debug_printf ("Not requeuing already queued non-RT signal %d"
+			      " for LWP %ld\n",
+			      sig->signal,
+			      lwpid_of (lwp));
 	      return;
 	    }
 	}
@@ -1690,8 +1679,8 @@ dequeue_one_deferred_signal (struct lwp_info *lwp, int *wstat)
       *p_sig = NULL;
 
       if (debug_threads)
-	fprintf (stderr, "Reporting deferred signal %d for LWP %ld.\n",
-		 WSTOPSIG (*wstat), lwpid_of (lwp));
+	debug_printf ("Reporting deferred signal %d for LWP %ld.\n",
+		      WSTOPSIG (*wstat), lwpid_of (lwp));
 
       if (debug_threads)
 	{
@@ -1700,11 +1689,10 @@ dequeue_one_deferred_signal (struct lwp_info *lwp, int *wstat)
 	  for (sig = lwp->pending_signals_to_report;
 	       sig != NULL;
 	       sig = sig->prev)
-	    fprintf (stderr,
-		     "   Still queued %d\n",
-		     sig->signal);
+	    debug_printf ("   Still queued %d\n",
+			  sig->signal);
 
-	  fprintf (stderr, "   (no more queued signals)\n");
+	  debug_printf ("   (no more queued signals)\n");
 	}
 
       return 1;
@@ -1734,9 +1722,8 @@ cancel_breakpoint (struct lwp_info *lwp)
   if ((*the_low_target.breakpoint_at) (lwp->stop_pc))
     {
       if (debug_threads)
-	fprintf (stderr,
-		 "CB: Push back breakpoint for %s\n",
-		 target_pid_to_str (ptid_of (lwp)));
+	debug_printf ("CB: Push back breakpoint for %s\n",
+		      target_pid_to_str (ptid_of (lwp)));
 
       /* Back up the PC if necessary.  */
       if (the_low_target.decr_pc_after_break)
@@ -1752,10 +1739,9 @@ cancel_breakpoint (struct lwp_info *lwp)
   else
     {
       if (debug_threads)
-	fprintf (stderr,
-		 "CB: No breakpoint found at %s for [%s]\n",
-		 paddress (lwp->stop_pc),
-		 target_pid_to_str (ptid_of (lwp)));
+	debug_printf ("CB: No breakpoint found at %s for [%s]\n",
+		      paddress (lwp->stop_pc),
+		      target_pid_to_str (ptid_of (lwp)));
     }
 
   current_inferior = saved_inferior;
@@ -1788,7 +1774,7 @@ linux_wait_for_event (ptid_t ptid, int *wstat, int options)
       event_child = (struct lwp_info *)
 	find_inferior (&all_lwps, status_pending_p_callback, &ptid);
       if (debug_threads && event_child)
-	fprintf (stderr, "Got a pending child %ld\n", lwpid_of (event_child));
+	debug_printf ("Got a pending child %ld\n", lwpid_of (event_child));
     }
   else
     {
@@ -1816,8 +1802,8 @@ linux_wait_for_event (ptid_t ptid, int *wstat, int options)
   if (event_child != NULL)
     {
       if (debug_threads)
-	fprintf (stderr, "Got an event from pending child %ld (%04x)\n",
-		 lwpid_of (event_child), event_child->status_pending);
+	debug_printf ("Got an event from pending child %ld (%04x)\n",
+		      lwpid_of (event_child), event_child->status_pending);
       *wstat = event_child->status_pending;
       event_child->status_pending_p = 0;
       event_child->status_pending = 0;
@@ -1847,7 +1833,7 @@ linux_wait_for_event (ptid_t ptid, int *wstat, int options)
       if ((options & WNOHANG) && event_child == NULL)
 	{
 	  if (debug_threads)
-	    fprintf (stderr, "WNOHANG set, no event found\n");
+	    debug_printf ("WNOHANG set, no event found\n");
 	  return 0;
 	}
 
@@ -1873,14 +1859,14 @@ linux_wait_for_event (ptid_t ptid, int *wstat, int options)
       if (! WIFSTOPPED (*wstat))
 	{
 	  if (debug_threads)
-	    fprintf (stderr, "LWP %ld exiting\n", lwpid_of (event_child));
+	    debug_printf ("LWP %ld exiting\n", lwpid_of (event_child));
 
 	  /* If the last thread is exiting, just return.  */
 	  if (last_thread_of_process_p (current_inferior))
 	    {
 	      if (debug_threads)
-		fprintf (stderr, "LWP %ld is last lwp of process\n",
-			 lwpid_of (event_child));
+		debug_printf ("LWP %ld is last lwp of process\n",
+			      lwpid_of (event_child));
 	      return lwpid_of (event_child);
 	    }
 
@@ -1888,14 +1874,14 @@ linux_wait_for_event (ptid_t ptid, int *wstat, int options)
 	    {
 	      current_inferior = (struct thread_info *) all_threads.head;
 	      if (debug_threads)
-		fprintf (stderr, "Current inferior is now %ld\n",
-			 lwpid_of (get_thread_lwp (current_inferior)));
+		debug_printf ("Current inferior is now %ld\n",
+			      lwpid_of (get_thread_lwp (current_inferior)));
 	    }
 	  else
 	    {
 	      current_inferior = NULL;
 	      if (debug_threads)
-		fprintf (stderr, "Current inferior is now <NULL>\n");
+		debug_printf ("Current inferior is now <NULL>\n");
 	    }
 
 	  /* If we were waiting for this particular child to do something...
@@ -1938,7 +1924,7 @@ linux_wait_for_event (ptid_t ptid, int *wstat, int options)
 	  int should_stop;
 
 	  if (debug_threads)
-	    fprintf (stderr, "Expected stop.\n");
+	    debug_printf ("Expected stop.\n");
 	  event_child->stop_expected = 0;
 
 	  should_stop = (current_inferior->last_resume_kind == resume_stop
@@ -2082,9 +2068,8 @@ select_event_lwp (struct lwp_info **orig_lp)
   if (event_lp != NULL)
     {
       if (debug_threads)
-	fprintf (stderr,
-		 "SEL: Select single-step %s\n",
-		 target_pid_to_str (ptid_of (event_lp)));
+	debug_printf ("SEL: Select single-step %s\n",
+		      target_pid_to_str (ptid_of (event_lp)));
     }
   else
     {
@@ -2099,9 +2084,8 @@ select_event_lwp (struct lwp_info **orig_lp)
 	((num_events * (double) rand ()) / (RAND_MAX + 1.0));
 
       if (debug_threads && num_events > 1)
-	fprintf (stderr,
-		 "SEL: Found %d SIGTRAP events, selecting #%d\n",
-		 num_events, random_selector);
+	debug_printf ("SEL: Found %d SIGTRAP events, selecting #%d\n",
+		      num_events, random_selector);
 
       event_lp = (struct lwp_info *) find_inferior (&all_lwps,
 						    select_event_lwp_callback,
@@ -2191,8 +2175,8 @@ linux_stabilize_threads (void)
   if (lwp_stuck != NULL)
     {
       if (debug_threads)
-	fprintf (stderr, "can't stabilize, LWP %ld is stuck in jump pad\n",
-		 lwpid_of (lwp_stuck));
+	debug_printf ("can't stabilize, LWP %ld is stuck in jump pad\n",
+		      lwpid_of (lwp_stuck));
       return;
     }
 
@@ -2243,8 +2227,8 @@ linux_stabilize_threads (void)
 	= (struct lwp_info *) find_inferior (&all_lwps,
 					 stuck_in_jump_pad_callback, NULL);
       if (lwp_stuck != NULL)
-	fprintf (stderr, "couldn't stabilize, LWP %ld got stuck in jump pad\n",
-		 lwpid_of (lwp_stuck));
+	debug_printf ("couldn't stabilize, LWP %ld got stuck in jump pad\n",
+		      lwpid_of (lwp_stuck));
     }
 }
 
@@ -2265,6 +2249,12 @@ linux_wait_1 (ptid_t ptid,
   int trace_event;
   int in_step_range;
 
+  if (debug_threads)
+    {
+      debug_level_enter ();
+      debug_printf ("linux_wait_1: [%s]\n", target_pid_to_str (ptid));
+    }
+
   /* Translate generic target options into linux options.  */
   options = __WALL;
   if (target_options & TARGET_WNOHANG)
@@ -2309,13 +2299,20 @@ retry:
   else
     {
       if (debug_threads)
-	fprintf (stderr, "step_over_bkpt set [%s], doing a blocking wait\n",
-		 target_pid_to_str (step_over_bkpt));
+	debug_printf ("step_over_bkpt set [%s], doing a blocking wait\n",
+		      target_pid_to_str (step_over_bkpt));
       pid = linux_wait_for_event (step_over_bkpt, &w, options & ~WNOHANG);
     }
 
   if (pid == 0) /* only if TARGET_WNOHANG */
-    return null_ptid;
+    {
+      if (debug_threads)
+	{
+	  debug_printf ("linux_wait_1 ret = null_ptid\n");
+	  debug_level_exit ();
+	}
+      return null_ptid;
+    }
 
   event_child = get_thread_lwp (current_inferior);
 
@@ -2341,9 +2338,13 @@ retry:
 	      ourstatus->value.integer = WEXITSTATUS (w);
 
 	      if (debug_threads)
-		fprintf (stderr,
-			 "\nChild exited with retcode = %x \n",
-			 WEXITSTATUS (w));
+		{
+		  debug_printf ("linux_wait_1 ret = %s, exited with "
+				"retcode %d\n",
+				target_pid_to_str (ptid_of (event_child)),
+				WEXITSTATUS (w));
+		  debug_level_exit ();
+		}
 	    }
 	  else
 	    {
@@ -2351,10 +2352,13 @@ retry:
 	      ourstatus->value.sig = gdb_signal_from_host (WTERMSIG (w));
 
 	      if (debug_threads)
-		fprintf (stderr,
-			 "\nChild terminated with signal = %x \n",
-			 WTERMSIG (w));
-
+		{
+		  debug_printf ("linux_wait_1 ret = %s, terminated with "
+				"signal %d\n",
+				target_pid_to_str (ptid_of (event_child)),
+				WTERMSIG (w));
+		  debug_level_exit ();
+		}
 	    }
 
 	  return ptid_of (event_child);
@@ -2409,7 +2413,7 @@ retry:
 	     already handled it.  So next time we resume (from this
 	     PC), we should step over it.  */
 	  if (debug_threads)
-	    fprintf (stderr, "Hit a gdbserver breakpoint.\n");
+	    debug_printf ("Hit a gdbserver breakpoint.\n");
 
 	  if (breakpoint_here (event_child->stop_pc))
 	    event_child->need_step_over = 1;
@@ -2435,10 +2439,9 @@ retry:
       && agent_loaded_p ())
     {
       if (debug_threads)
-	fprintf (stderr,
-		 "Got signal %d for LWP %ld.  Check if we need "
-		 "to defer or adjust it.\n",
-		 WSTOPSIG (w), lwpid_of (event_child));
+	debug_printf ("Got signal %d for LWP %ld.  Check if we need "
+		      "to defer or adjust it.\n",
+		      WSTOPSIG (w), lwpid_of (event_child));
 
       /* Allow debugging the jump pad itself.  */
       if (current_inferior->last_resume_kind != resume_step
@@ -2447,9 +2450,8 @@ retry:
 	  enqueue_one_deferred_signal (event_child, &w);
 
 	  if (debug_threads)
-	    fprintf (stderr,
-		     "Signal %d for LWP %ld deferred (in jump pad)\n",
-		     WSTOPSIG (w), lwpid_of (event_child));
+	    debug_printf ("Signal %d for LWP %ld deferred (in jump pad)\n",
+			  WSTOPSIG (w), lwpid_of (event_child));
 
 	  linux_resume_one_lwp (event_child, 0, 0, NULL);
 	  goto retry;
@@ -2459,11 +2461,10 @@ retry:
   if (event_child->collecting_fast_tracepoint)
     {
       if (debug_threads)
-	fprintf (stderr, "\
-LWP %ld was trying to move out of the jump pad (%d).  \
-Check if we're already there.\n",
-		 lwpid_of (event_child),
-		 event_child->collecting_fast_tracepoint);
+	debug_printf ("LWP %ld was trying to move out of the jump pad (%d). "
+		      "Check if we're already there.\n",
+		      lwpid_of (event_child),
+		      event_child->collecting_fast_tracepoint);
 
       trace_event = 1;
 
@@ -2476,9 +2477,8 @@ Check if we're already there.\n",
 	  if (event_child->exit_jump_pad_bkpt != NULL)
 	    {
 	      if (debug_threads)
-		fprintf (stderr,
-			 "No longer need exit-jump-pad bkpt; removing it."
-			 "stopping all threads momentarily.\n");
+		debug_printf ("No longer need exit-jump-pad bkpt; removing it."
+			      "stopping all threads momentarily.\n");
 
 	      /* Other running threads could hit this breakpoint.
 		 We don't handle moribund locations like GDB does,
@@ -2502,25 +2502,33 @@ Check if we're already there.\n",
       if (event_child->collecting_fast_tracepoint == 0)
 	{
 	  if (debug_threads)
-	    fprintf (stderr,
-		     "fast tracepoint finished "
-		     "collecting successfully.\n");
+	    debug_printf ("fast tracepoint finished "
+			  "collecting successfully.\n");
 
 	  /* We may have a deferred signal to report.  */
 	  if (dequeue_one_deferred_signal (event_child, &w))
 	    {
 	      if (debug_threads)
-		fprintf (stderr, "dequeued one signal.\n");
+		debug_printf ("dequeued one signal.\n");
 	    }
 	  else
 	    {
 	      if (debug_threads)
-		fprintf (stderr, "no deferred signals.\n");
+		debug_printf ("no deferred signals.\n");
 
 	      if (stabilizing_threads)
 		{
 		  ourstatus->kind = TARGET_WAITKIND_STOPPED;
 		  ourstatus->value.sig = GDB_SIGNAL_0;
+
+		  if (debug_threads)
+		    {
+		      debug_printf ("linux_wait_1 ret = %s, stopped "
+				    "while stabilizing threads\n",
+				    target_pid_to_str (ptid_of (event_child)));
+		      debug_level_exit ();
+		    }
+
 		  return ptid_of (event_child);
 		}
 	    }
@@ -2554,8 +2562,8 @@ Check if we're already there.\n",
       siginfo_t info, *info_p;
 
       if (debug_threads)
-	fprintf (stderr, "Ignored signal %d for LWP %ld.\n",
-		 WSTOPSIG (w), lwpid_of (event_child));
+	debug_printf ("Ignored signal %d for LWP %ld.\n",
+		      WSTOPSIG (w), lwpid_of (event_child));
 
       if (ptrace (PTRACE_GETSIGINFO, lwpid_of (event_child),
 		  (PTRACE_TYPE_ARG3) 0, &info) == 0)
@@ -2600,16 +2608,16 @@ Check if we're already there.\n",
       if (debug_threads)
 	{
 	  if (bp_explains_trap)
-	    fprintf (stderr, "Hit a gdbserver breakpoint.\n");
+	    debug_printf ("Hit a gdbserver breakpoint.\n");
 	  if (step_over_finished)
-	    fprintf (stderr, "Step-over finished.\n");
+	    debug_printf ("Step-over finished.\n");
 	  if (trace_event)
-	    fprintf (stderr, "Tracepoint event.\n");
+	    debug_printf ("Tracepoint event.\n");
 	  if (lwp_in_step_range (event_child))
-	    fprintf (stderr, "Range stepping pc 0x%s [0x%s, 0x%s).\n",
-		     paddress (event_child->stop_pc),
-		     paddress (event_child->step_range_start),
-		     paddress (event_child->step_range_end));
+	    debug_printf ("Range stepping pc 0x%s [0x%s, 0x%s).\n",
+			  paddress (event_child->stop_pc),
+			  paddress (event_child->step_range_start),
+			  paddress (event_child->step_range_end));
 	}
 
       /* We're not reporting this breakpoint to GDB, so apply the
@@ -2629,7 +2637,7 @@ Check if we're already there.\n",
 	 going to keep waiting, so use proceed, which handles stepping
 	 over the next breakpoint.  */
       if (debug_threads)
-	fprintf (stderr, "proceeding all threads.\n");
+	debug_printf ("proceeding all threads.\n");
 
       if (step_over_finished)
 	unsuspend_all_lwps (event_child);
@@ -2643,16 +2651,16 @@ Check if we're already there.\n",
       if (current_inferior->last_resume_kind == resume_step)
 	{
 	  if (event_child->step_range_start == event_child->step_range_end)
-	    fprintf (stderr, "GDB wanted to single-step, reporting event.\n");
+	    debug_printf ("GDB wanted to single-step, reporting event.\n");
 	  else if (!lwp_in_step_range (event_child))
-	    fprintf (stderr, "Out of step range, reporting event.\n");
+	    debug_printf ("Out of step range, reporting event.\n");
 	}
       if (event_child->stopped_by_watchpoint)
-	fprintf (stderr, "Stopped by watchpoint.\n");
+	debug_printf ("Stopped by watchpoint.\n");
       if (gdb_breakpoint_here (event_child->stop_pc))
-	fprintf (stderr, "Stopped by GDB breakpoint.\n");
+	debug_printf ("Stopped by GDB breakpoint.\n");
       if (debug_threads)
-	fprintf (stderr, "Hit a non-gdbserver trap event.\n");
+	debug_printf ("Hit a non-gdbserver trap event.\n");
     }
 
   /* Alright, we're going to report a stop.  */
@@ -2730,10 +2738,12 @@ Check if we're already there.\n",
   gdb_assert (ptid_equal (step_over_bkpt, null_ptid));
 
   if (debug_threads)
-    fprintf (stderr, "linux_wait ret = %s, %d, %d\n",
-	     target_pid_to_str (ptid_of (event_child)),
-	     ourstatus->kind,
-	     ourstatus->value.sig);
+    {
+      debug_printf ("linux_wait_1 ret = %s, %d, %d\n",
+		    target_pid_to_str (ptid_of (event_child)),
+		    ourstatus->kind, ourstatus->value.sig);
+      debug_level_exit ();
+    }
 
   return ptid_of (event_child);
 }
@@ -2772,9 +2782,6 @@ linux_wait (ptid_t ptid,
 {
   ptid_t event_ptid;
 
-  if (debug_threads)
-    fprintf (stderr, "linux_wait: [%s]\n", target_pid_to_str (ptid));
-
   /* Flush the async file first.  */
   if (target_is_async_p ())
     async_file_flush ();
@@ -2837,13 +2844,13 @@ send_sigstop (struct lwp_info *lwp)
   if (lwp->stop_expected)
     {
       if (debug_threads)
-	fprintf (stderr, "Have pending sigstop for lwp %d\n", pid);
+	debug_printf ("Have pending sigstop for lwp %d\n", pid);
 
       return;
     }
 
   if (debug_threads)
-    fprintf (stderr, "Sending sigstop to lwp %d\n", pid);
+    debug_printf ("Sending sigstop to lwp %d\n", pid);
 
   lwp->stop_expected = 1;
   kill_lwp (pid, SIGSTOP);
@@ -2912,8 +2919,8 @@ wait_for_sigstop (struct inferior_list_entry *entry)
   if (lwp->stopped)
     {
       if (debug_threads)
-	fprintf (stderr, "wait_for_sigstop: LWP %ld already stopped\n",
-		 lwpid_of (lwp));
+	debug_printf ("wait_for_sigstop: LWP %ld already stopped\n",
+		      lwpid_of (lwp));
       return;
     }
 
@@ -2926,7 +2933,7 @@ wait_for_sigstop (struct inferior_list_entry *entry)
   ptid = lwp->head.id;
 
   if (debug_threads)
-    fprintf (stderr, "wait_for_sigstop: pulling one event\n");
+    debug_printf ("wait_for_sigstop: pulling one event\n");
 
   pid = linux_wait_for_event (ptid, &wstat, __WALL);
 
@@ -2936,14 +2943,14 @@ wait_for_sigstop (struct inferior_list_entry *entry)
   if (WIFSTOPPED (wstat))
     {
       if (debug_threads)
-	fprintf (stderr, "LWP %ld stopped with signal %d\n",
-		 lwpid_of (lwp), WSTOPSIG (wstat));
+	debug_printf ("LWP %ld stopped with signal %d\n",
+		      lwpid_of (lwp), WSTOPSIG (wstat));
 
       if (WSTOPSIG (wstat) != SIGSTOP)
 	{
 	  if (debug_threads)
-	    fprintf (stderr, "LWP %ld stopped with non-sigstop status %06x\n",
-		     lwpid_of (lwp), wstat);
+	    debug_printf ("LWP %ld stopped with non-sigstop status %06x\n",
+			  lwpid_of (lwp), wstat);
 
 	  lwp->status_pending_p = 1;
 	  lwp->status_pending = wstat;
@@ -2952,7 +2959,7 @@ wait_for_sigstop (struct inferior_list_entry *entry)
   else
     {
       if (debug_threads)
-	fprintf (stderr, "Process %d exited while stopping LWPs\n", pid);
+	debug_printf ("Process %d exited while stopping LWPs\n", pid);
 
       lwp = find_lwp_pid (pid_to_ptid (pid));
       if (lwp)
@@ -2973,7 +2980,7 @@ wait_for_sigstop (struct inferior_list_entry *entry)
   else
     {
       if (debug_threads)
-	fprintf (stderr, "Previously current thread died.\n");
+	debug_printf ("Previously current thread died.\n");
 
       if (non_stop)
 	{
@@ -3032,9 +3039,8 @@ move_out_of_jump_pad_callback (struct inferior_list_entry *entry)
       && maybe_move_out_of_jump_pad (lwp, wstat))
     {
       if (debug_threads)
-	fprintf (stderr,
-		 "LWP %ld needs stabilizing (in jump pad)\n",
-		 lwpid_of (lwp));
+	debug_printf ("LWP %ld needs stabilizing (in jump pad)\n",
+		      lwpid_of (lwp));
 
       if (wstat)
 	{
@@ -3042,10 +3048,9 @@ move_out_of_jump_pad_callback (struct inferior_list_entry *entry)
 	  enqueue_one_deferred_signal (lwp, wstat);
 
 	  if (debug_threads)
-	    fprintf (stderr,
-		     "Signal %d for LWP %ld deferred "
-		     "(in jump pad)\n",
-		     WSTOPSIG (*wstat), lwpid_of (lwp));
+	    debug_printf ("Signal %d for LWP %ld deferred "
+			  "(in jump pad)\n",
+			  WSTOPSIG (*wstat), lwpid_of (lwp));
 	}
 
       linux_resume_one_lwp (lwp, 0, 0, NULL);
@@ -3076,6 +3081,16 @@ stop_all_lwps (int suspend, struct lwp_info *except)
   /* Should not be called recursively.  */
   gdb_assert (stopping_threads == NOT_STOPPING_THREADS);
 
+  if (debug_threads)
+    {
+      debug_level_enter ();
+      debug_printf ("stop_all_lwps (%s, except=%s)\n",
+		    suspend ? "stop-and-suspend" : "stop",
+		    except != NULL
+		    ? target_pid_to_str (ptid_of (except))
+		    : "none");
+    }
+
   stopping_threads = (suspend
 		      ? STOPPING_AND_SUSPENDING_THREADS
 		      : STOPPING_THREADS);
@@ -3086,6 +3101,13 @@ stop_all_lwps (int suspend, struct lwp_info *except)
     find_inferior (&all_lwps, send_sigstop_callback, except);
   for_each_inferior (&all_lwps, wait_for_sigstop);
   stopping_threads = NOT_STOPPING_THREADS;
+
+  if (debug_threads)
+    {
+      debug_printf ("stop_all_lwps done, setting stopping_threads "
+		    "back to !stopping\n");
+      debug_level_exit ();
+    }
 }
 
 /* Resume execution of the inferior process.
@@ -3138,10 +3160,10 @@ linux_resume_one_lwp (struct lwp_info *lwp,
   if (lwp->status_pending_p)
     {
       if (debug_threads)
-	fprintf (stderr, "Not resuming lwp %ld (%s, signal %d, stop %s);"
-		 " has pending status\n",
-		 lwpid_of (lwp), step ? "step" : "continue", signal,
-		 lwp->stop_expected ? "expected" : "not expected");
+	debug_printf ("Not resuming lwp %ld (%s, signal %d, stop %s);"
+		      " has pending status\n",
+		      lwpid_of (lwp), step ? "step" : "continue", signal,
+		      lwp->stop_expected ? "expected" : "not expected");
       return;
     }
 
@@ -3149,9 +3171,9 @@ linux_resume_one_lwp (struct lwp_info *lwp,
   current_inferior = get_lwp_thread (lwp);
 
   if (debug_threads)
-    fprintf (stderr, "Resuming lwp %ld (%s, signal %d, stop %s)\n",
-	     lwpid_of (lwp), step ? "step" : "continue", signal,
-	     lwp->stop_expected ? "expected" : "not expected");
+    debug_printf ("Resuming lwp %ld (%s, signal %d, stop %s)\n",
+		  lwpid_of (lwp), step ? "step" : "continue", signal,
+		  lwp->stop_expected ? "expected" : "not expected");
 
   /* This bit needs some thinking about.  If we get a signal that
      we must report while a single-step reinsert is still pending,
@@ -3166,8 +3188,8 @@ linux_resume_one_lwp (struct lwp_info *lwp,
   if (lwp->bp_reinsert != 0)
     {
       if (debug_threads)
-	fprintf (stderr, "  pending reinsert at 0x%s\n",
-		 paddress (lwp->bp_reinsert));
+	debug_printf ("  pending reinsert at 0x%s\n",
+		      paddress (lwp->bp_reinsert));
 
       if (can_hardware_single_step ())
 	{
@@ -3190,9 +3212,9 @@ linux_resume_one_lwp (struct lwp_info *lwp,
   if (fast_tp_collecting == 1)
     {
       if (debug_threads)
-	fprintf (stderr, "\
-lwp %ld wants to get out of fast tracepoint jump pad (exit-jump-pad-bkpt)\n",
-		 lwpid_of (lwp));
+	debug_printf ("lwp %ld wants to get out of fast tracepoint jump pad"
+		      " (exit-jump-pad-bkpt)\n",
+		      lwpid_of (lwp));
 
       /* Postpone any pending signal.  It was enqueued above.  */
       signal = 0;
@@ -3200,9 +3222,9 @@ lwp %ld wants to get out of fast tracepoint jump pad (exit-jump-pad-bkpt)\n",
   else if (fast_tp_collecting == 2)
     {
       if (debug_threads)
-	fprintf (stderr, "\
-lwp %ld wants to get out of fast tracepoint jump pad single-stepping\n",
-		 lwpid_of (lwp));
+	debug_printf ("lwp %ld wants to get out of fast tracepoint jump pad"
+		      " single-stepping\n",
+		      lwpid_of (lwp));
 
       if (can_hardware_single_step ())
 	step = 1;
@@ -3226,9 +3248,8 @@ lwp %ld wants to get out of fast tracepoint jump pad single-stepping\n",
       && can_hardware_single_step ())
     {
       if (debug_threads)
-	fprintf (stderr,
-		 "lwp %ld has a while-stepping action -> forcing step.\n",
-		 lwpid_of (lwp));
+	debug_printf ("lwp %ld has a while-stepping action -> forcing step.\n",
+		      lwpid_of (lwp));
       step = 1;
     }
 
@@ -3236,7 +3257,7 @@ lwp %ld wants to get out of fast tracepoint jump pad single-stepping\n",
     {
       struct regcache *regcache = get_thread_regcache (current_inferior, 1);
       CORE_ADDR pc = (*the_low_target.get_pc) (regcache);
-      fprintf (stderr, "  resuming from pc 0x%lx\n", (long) pc);
+      debug_printf ("  resuming from pc 0x%lx\n", (long) pc);
     }
 
   /* If we have pending signals, consume one unless we are trying to
@@ -3334,11 +3355,12 @@ linux_set_resume_request (struct inferior_list_entry *entry, void *arg)
 	      && thread->last_resume_kind == resume_stop)
 	    {
 	      if (debug_threads)
-		fprintf (stderr, "already %s LWP %ld at GDB's request\n",
-			 thread->last_status.kind == TARGET_WAITKIND_STOPPED
-			 ? "stopped"
-			 : "stopping",
-			 lwpid_of (lwp));
+		debug_printf ("already %s LWP %ld at GDB's request\n",
+			      (thread->last_status.kind
+			       == TARGET_WAITKIND_STOPPED)
+			      ? "stopped"
+			      : "stopping",
+			      lwpid_of (lwp));
 
 	      continue;
 	    }
@@ -3359,10 +3381,9 @@ linux_set_resume_request (struct inferior_list_entry *entry, void *arg)
 	      lwp->status_pending_p = 1;
 
 	      if (debug_threads)
-		fprintf (stderr,
-			 "Dequeueing deferred signal %d for LWP %ld, "
-			 "leaving status pending.\n",
-			 WSTOPSIG (lwp->status_pending), lwpid_of (lwp));
+		debug_printf ("Dequeueing deferred signal %d for LWP %ld, "
+			      "leaving status pending.\n",
+			      WSTOPSIG (lwp->status_pending), lwpid_of (lwp));
 	    }
 
 	  return 0;
@@ -3413,9 +3434,8 @@ need_step_over_p (struct inferior_list_entry *entry, void *dummy)
   if (!lwp->stopped)
     {
       if (debug_threads)
-	fprintf (stderr,
-		 "Need step over [LWP %ld]? Ignoring, not stopped\n",
-		 lwpid_of (lwp));
+	debug_printf ("Need step over [LWP %ld]? Ignoring, not stopped\n",
+		      lwpid_of (lwp));
       return 0;
     }
 
@@ -3424,9 +3444,9 @@ need_step_over_p (struct inferior_list_entry *entry, void *dummy)
   if (thread->last_resume_kind == resume_stop)
     {
       if (debug_threads)
-	fprintf (stderr,
-		 "Need step over [LWP %ld]? Ignoring, should remain stopped\n",
-		 lwpid_of (lwp));
+	debug_printf ("Need step over [LWP %ld]? Ignoring, should remain"
+		      " stopped\n",
+		      lwpid_of (lwp));
       return 0;
     }
 
@@ -3435,25 +3455,23 @@ need_step_over_p (struct inferior_list_entry *entry, void *dummy)
   if (lwp->suspended)
     {
       if (debug_threads)
-	fprintf (stderr,
-		 "Need step over [LWP %ld]? Ignoring, suspended\n",
-		 lwpid_of (lwp));
+	debug_printf ("Need step over [LWP %ld]? Ignoring, suspended\n",
+		      lwpid_of (lwp));
       return 0;
     }
 
   if (!lwp->need_step_over)
     {
       if (debug_threads)
-	fprintf (stderr,
-		 "Need step over [LWP %ld]? No\n", lwpid_of (lwp));
+	debug_printf ("Need step over [LWP %ld]? No\n", lwpid_of (lwp));
     }
 
   if (lwp->status_pending_p)
     {
       if (debug_threads)
-	fprintf (stderr,
-		 "Need step over [LWP %ld]? Ignoring, has pending status.\n",
-		 lwpid_of (lwp));
+	debug_printf ("Need step over [LWP %ld]? Ignoring, has pending"
+		      " status.\n",
+		      lwpid_of (lwp));
       return 0;
     }
 
@@ -3469,10 +3487,9 @@ need_step_over_p (struct inferior_list_entry *entry, void *dummy)
   if (pc != lwp->stop_pc)
     {
       if (debug_threads)
-	fprintf (stderr,
-		 "Need step over [LWP %ld]? Cancelling, PC was changed.  "
-		 "Old stop_pc was 0x%s, PC is now 0x%s\n",
-		 lwpid_of (lwp), paddress (lwp->stop_pc), paddress (pc));
+	debug_printf ("Need step over [LWP %ld]? Cancelling, PC was changed. "
+		      "Old stop_pc was 0x%s, PC is now 0x%s\n",
+		      lwpid_of (lwp), paddress (lwp->stop_pc), paddress (pc));
 
       lwp->need_step_over = 0;
       return 0;
@@ -3492,10 +3509,9 @@ need_step_over_p (struct inferior_list_entry *entry, void *dummy)
 	  && gdb_no_commands_at_breakpoint (pc))
 	{
 	  if (debug_threads)
-	    fprintf (stderr,
-		     "Need step over [LWP %ld]? yes, but found"
-		     " GDB breakpoint at 0x%s; skipping step over\n",
-		     lwpid_of (lwp), paddress (pc));
+	    debug_printf ("Need step over [LWP %ld]? yes, but found"
+			  " GDB breakpoint at 0x%s; skipping step over\n",
+			  lwpid_of (lwp), paddress (pc));
 
 	  current_inferior = saved_inferior;
 	  return 0;
@@ -3503,10 +3519,9 @@ need_step_over_p (struct inferior_list_entry *entry, void *dummy)
       else
 	{
 	  if (debug_threads)
-	    fprintf (stderr,
-		     "Need step over [LWP %ld]? yes, "
-		     "found breakpoint at 0x%s\n",
-		     lwpid_of (lwp), paddress (pc));
+	    debug_printf ("Need step over [LWP %ld]? yes, "
+			  "found breakpoint at 0x%s\n",
+			  lwpid_of (lwp), paddress (pc));
 
 	  /* We've found an lwp that needs stepping over --- return 1 so
 	     that find_inferior stops looking.  */
@@ -3521,9 +3536,9 @@ need_step_over_p (struct inferior_list_entry *entry, void *dummy)
   current_inferior = saved_inferior;
 
   if (debug_threads)
-    fprintf (stderr,
-	     "Need step over [LWP %ld]? No, no breakpoint found at 0x%s\n",
-	     lwpid_of (lwp), paddress (pc));
+    debug_printf ("Need step over [LWP %ld]? No, no breakpoint found"
+		  " at 0x%s\n",
+		  lwpid_of (lwp), paddress (pc));
 
   return 0;
 }
@@ -3554,15 +3569,14 @@ start_step_over (struct lwp_info *lwp)
   int step;
 
   if (debug_threads)
-    fprintf (stderr,
-	     "Starting step-over on LWP %ld.  Stopping all threads\n",
-	     lwpid_of (lwp));
+    debug_printf ("Starting step-over on LWP %ld.  Stopping all threads\n",
+		  lwpid_of (lwp));
 
   stop_all_lwps (1, lwp);
   gdb_assert (lwp->suspended == 0);
 
   if (debug_threads)
-    fprintf (stderr, "Done stopping all threads for step-over.\n");
+    debug_printf ("Done stopping all threads for step-over.\n");
 
   /* Note, we should always reach here with an already adjusted PC,
      either by GDB (if we're resuming due to GDB's request), or by our
@@ -3607,7 +3621,7 @@ finish_step_over (struct lwp_info *lwp)
   if (lwp->bp_reinsert != 0)
     {
       if (debug_threads)
-	fprintf (stderr, "Finished step over.\n");
+	debug_printf ("Finished step over.\n");
 
       /* Reinsert any breakpoint at LWP->BP_REINSERT.  Note that there
 	 may be no breakpoint to reinsert there by now.  */
@@ -3662,12 +3676,12 @@ linux_resume_one_thread (struct inferior_list_entry *entry, void *arg)
   if (lwp->resume->kind == resume_stop)
     {
       if (debug_threads)
-	fprintf (stderr, "resume_stop request for LWP %ld\n", lwpid_of (lwp));
+	debug_printf ("resume_stop request for LWP %ld\n", lwpid_of (lwp));
 
       if (!lwp->stopped)
 	{
 	  if (debug_threads)
-	    fprintf (stderr, "stopping LWP %ld\n", lwpid_of (lwp));
+	    debug_printf ("stopping LWP %ld\n", lwpid_of (lwp));
 
 	  /* Stop the thread, and wait for the event asynchronously,
 	     through the event loop.  */
@@ -3676,8 +3690,8 @@ linux_resume_one_thread (struct inferior_list_entry *entry, void *arg)
       else
 	{
 	  if (debug_threads)
-	    fprintf (stderr, "already stopped LWP %ld\n",
-		     lwpid_of (lwp));
+	    debug_printf ("already stopped LWP %ld\n",
+			  lwpid_of (lwp));
 
 	  /* The LWP may have been stopped in an internal event that
 	     was not meant to be notified back to GDB (e.g., gdbserver
@@ -3719,7 +3733,7 @@ linux_resume_one_thread (struct inferior_list_entry *entry, void *arg)
   if (!leave_pending)
     {
       if (debug_threads)
-	fprintf (stderr, "resuming LWP %ld\n", lwpid_of (lwp));
+	debug_printf ("resuming LWP %ld\n", lwpid_of (lwp));
 
       step = (lwp->resume->kind == resume_step);
       linux_resume_one_lwp (lwp, step, lwp->resume->sig, NULL);
@@ -3727,7 +3741,7 @@ linux_resume_one_thread (struct inferior_list_entry *entry, void *arg)
   else
     {
       if (debug_threads)
-	fprintf (stderr, "leaving LWP %ld stopped\n", lwpid_of (lwp));
+	debug_printf ("leaving LWP %ld stopped\n", lwpid_of (lwp));
 
       /* If we have a new signal, enqueue the signal.  */
       if (lwp->resume->sig != 0)
@@ -3764,6 +3778,12 @@ linux_resume (struct thread_resume *resume_info, size_t n)
   int any_pending;
   int leave_all_stopped;
 
+  if (debug_threads)
+    {
+      debug_level_enter ();
+      debug_printf ("linux_resume:\n");
+    }
+
   find_inferior (&all_threads, linux_set_resume_request, &array);
 
   /* If there is a thread which would otherwise be resumed, which has
@@ -3792,13 +3812,12 @@ linux_resume (struct thread_resume *resume_info, size_t n)
   if (debug_threads)
     {
       if (need_step_over != NULL)
-	fprintf (stderr, "Not resuming all, need step over\n");
+	debug_printf ("Not resuming all, need step over\n");
       else if (any_pending)
-	fprintf (stderr,
-		 "Not resuming, all-stop and found "
-		 "an LWP with pending status\n");
+	debug_printf ("Not resuming, all-stop and found "
+		      "an LWP with pending status\n");
       else
-	fprintf (stderr, "Resuming, no pending status or step over needed\n");
+	debug_printf ("Resuming, no pending status or step over needed\n");
     }
 
   /* Even if we're leaving threads stopped, queue all signals we'd
@@ -3807,6 +3826,12 @@ linux_resume (struct thread_resume *resume_info, size_t n)
 
   if (need_step_over)
     start_step_over (need_step_over);
+
+  if (debug_threads)
+    {
+      debug_printf ("linux_resume done\n");
+      debug_level_exit ();
+    }
 }
 
 /* This function is called once per thread.  We check the thread's
@@ -3829,13 +3854,12 @@ proceed_one_lwp (struct inferior_list_entry *entry, void *except)
     return 0;
 
   if (debug_threads)
-    fprintf (stderr,
-	     "proceed_one_lwp: lwp %ld\n", lwpid_of (lwp));
+    debug_printf ("proceed_one_lwp: lwp %ld\n", lwpid_of (lwp));
 
   if (!lwp->stopped)
     {
       if (debug_threads)
-	fprintf (stderr, "   LWP %ld already running\n", lwpid_of (lwp));
+	debug_printf ("   LWP %ld already running\n", lwpid_of (lwp));
       return 0;
     }
 
@@ -3845,16 +3869,16 @@ proceed_one_lwp (struct inferior_list_entry *entry, void *except)
       && thread->last_status.kind != TARGET_WAITKIND_IGNORE)
     {
       if (debug_threads)
-	fprintf (stderr, "   client wants LWP to remain %ld stopped\n",
-		 lwpid_of (lwp));
+	debug_printf ("   client wants LWP to remain %ld stopped\n",
+		      lwpid_of (lwp));
       return 0;
     }
 
   if (lwp->status_pending_p)
     {
       if (debug_threads)
-	fprintf (stderr, "   LWP %ld has pending status, leaving stopped\n",
-		 lwpid_of (lwp));
+	debug_printf ("   LWP %ld has pending status, leaving stopped\n",
+		      lwpid_of (lwp));
       return 0;
     }
 
@@ -3863,7 +3887,7 @@ proceed_one_lwp (struct inferior_list_entry *entry, void *except)
   if (lwp->suspended)
     {
       if (debug_threads)
-	fprintf (stderr, "   LWP %ld is suspended\n", lwpid_of (lwp));
+	debug_printf ("   LWP %ld is suspended\n", lwpid_of (lwp));
       return 0;
     }
 
@@ -3882,10 +3906,9 @@ proceed_one_lwp (struct inferior_list_entry *entry, void *except)
 	 pending, this is a no-op.  */
 
       if (debug_threads)
-	fprintf (stderr,
-		 "Client wants LWP %ld to stop. "
-		 "Making sure it has a SIGSTOP pending\n",
-		 lwpid_of (lwp));
+	debug_printf ("Client wants LWP %ld to stop. "
+		      "Making sure it has a SIGSTOP pending\n",
+		      lwpid_of (lwp));
 
       send_sigstop (lwp);
     }
@@ -3932,9 +3955,9 @@ proceed_all_lwps (void)
       if (need_step_over != NULL)
 	{
 	  if (debug_threads)
-	    fprintf (stderr, "proceed_all_lwps: found "
-		     "thread %ld needing a step-over\n",
-		     lwpid_of (need_step_over));
+	    debug_printf ("proceed_all_lwps: found "
+			  "thread %ld needing a step-over\n",
+			  lwpid_of (need_step_over));
 
 	  start_step_over (need_step_over);
 	  return;
@@ -3942,7 +3965,7 @@ proceed_all_lwps (void)
     }
 
   if (debug_threads)
-    fprintf (stderr, "Proceeding, no step-over needed\n");
+    debug_printf ("Proceeding, no step-over needed\n");
 
   find_inferior (&all_lwps, proceed_one_lwp, NULL);
 }
@@ -3956,18 +3979,24 @@ unstop_all_lwps (int unsuspend, struct lwp_info *except)
 {
   if (debug_threads)
     {
+      debug_level_enter ();
       if (except)
-	fprintf (stderr,
-		 "unstopping all lwps, except=(LWP %ld)\n", lwpid_of (except));
+	debug_printf ("unstopping all lwps, except=(LWP %ld)\n",
+		      lwpid_of (except));
       else
-	fprintf (stderr,
-		 "unstopping all lwps\n");
+	debug_printf ("unstopping all lwps\n");
     }
 
   if (unsuspend)
     find_inferior (&all_lwps, unsuspend_and_proceed_one_lwp, except);
   else
     find_inferior (&all_lwps, proceed_one_lwp, except);
+
+  if (debug_threads)
+    {
+      debug_printf ("unstop_all_lwps done\n");
+      debug_level_exit ();
+    }
 }
 
 
@@ -4533,8 +4562,8 @@ linux_write_memory (CORE_ADDR memaddr, const unsigned char *myaddr, int len)
 	val = val & 0xffff;
       else if (len == 3)
 	val = val & 0xffffff;
-      fprintf (stderr, "Writing %0*x to 0x%08lx\n", 2 * ((len < 4) ? len : 4),
-	       val, (long)memaddr);
+      debug_printf ("Writing %0*x to 0x%08lx\n", 2 * ((len < 4) ? len : 4),
+		    val, (long)memaddr);
     }
 
   /* Fill start and end extra bytes of buffer with existing memory data.  */
@@ -4777,9 +4806,9 @@ linux_xfer_siginfo (const char *annex, unsigned char *readbuf,
   pid = lwpid_of (get_thread_lwp (current_inferior));
 
   if (debug_threads)
-    fprintf (stderr, "%s siginfo for lwp %d.\n",
-	     readbuf != NULL ? "Reading" : "Writing",
-	     pid);
+    debug_printf ("%s siginfo for lwp %d.\n",
+		  readbuf != NULL ? "Reading" : "Writing",
+		  pid);
 
   if (offset >= sizeof (siginfo))
     return -1;
@@ -4851,8 +4880,8 @@ linux_async (int enable)
   int previous = (linux_event_pipe[0] != -1);
 
   if (debug_threads)
-    fprintf (stderr, "linux_async (%d), previous=%d\n",
-	     enable, previous);
+    debug_printf ("linux_async (%d), previous=%d\n",
+		  enable, previous);
 
   if (previous != enable)
     {
diff --git a/gdb/gdbserver/linux-m32r-low.c b/gdb/gdbserver/linux-m32r-low.c
index e208523..477e185 100644
--- a/gdb/gdbserver/linux-m32r-low.c
+++ b/gdb/gdbserver/linux-m32r-low.c
@@ -59,7 +59,7 @@ m32r_get_pc (struct regcache *regcache)
   unsigned long pc;
   collect_register_by_name (regcache, "pc", &pc);
   if (debug_threads)
-    fprintf (stderr, "stop pc is %08lx\n", pc);
+    debug_printf ("stop pc is %08lx\n", pc);
   return pc;
 }
 
diff --git a/gdb/gdbserver/linux-sparc-low.c b/gdb/gdbserver/linux-sparc-low.c
index fcffe05..75c97dd 100644
--- a/gdb/gdbserver/linux-sparc-low.c
+++ b/gdb/gdbserver/linux-sparc-low.c
@@ -231,7 +231,7 @@ sparc_get_pc (struct regcache *regcache)
   unsigned long pc;
   collect_register_by_name (regcache, "pc", &pc);
   if (debug_threads)
-    fprintf (stderr, "stop pc is %08lx\n", pc);
+    debug_printf ("stop pc is %08lx\n", pc);
   return pc;
 }
 
diff --git a/gdb/gdbserver/linux-x86-low.c b/gdb/gdbserver/linux-x86-low.c
index 04fa3c9..b141c2b 100644
--- a/gdb/gdbserver/linux-x86-low.c
+++ b/gdb/gdbserver/linux-x86-low.c
@@ -2022,8 +2022,8 @@ add_insns (unsigned char *start, int len)
   CORE_ADDR buildaddr = current_insn_ptr;
 
   if (debug_threads)
-    fprintf (stderr, "Adding %d bytes of insn at %s\n",
-	     len, paddress (buildaddr));
+    debug_printf ("Adding %d bytes of insn at %s\n",
+		  len, paddress (buildaddr));
 
   append_insns (&buildaddr, len, start);
   current_insn_ptr = buildaddr;
diff --git a/gdb/gdbserver/mem-break.c b/gdb/gdbserver/mem-break.c
index fd706e9..b77ce10 100644
--- a/gdb/gdbserver/mem-break.c
+++ b/gdb/gdbserver/mem-break.c
@@ -197,10 +197,9 @@ set_raw_breakpoint_at (CORE_ADDR where)
   if (err != 0)
     {
       if (debug_threads)
-	fprintf (stderr,
-		 "Failed to read shadow memory of"
-		 " breakpoint at 0x%s (%s).\n",
-		 paddress (where), strerror (err));
+	debug_printf ("Failed to read shadow memory of"
+		      " breakpoint at 0x%s (%s).\n",
+		      paddress (where), strerror (err));
       free (bp);
       return NULL;
     }
@@ -211,9 +210,8 @@ set_raw_breakpoint_at (CORE_ADDR where)
   if (err != 0)
     {
       if (debug_threads)
-	fprintf (stderr,
-		 "Failed to insert breakpoint at 0x%s (%s).\n",
-		 paddress (where), strerror (err));
+	debug_printf ("Failed to insert breakpoint at 0x%s (%s).\n",
+		      paddress (where), strerror (err));
       free (bp);
       return NULL;
     }
@@ -334,10 +332,9 @@ delete_fast_tracepoint_jump (struct fast_tracepoint_jump *todel)
 		  *bp_link = prev_bp_link;
 
 		  if (debug_threads)
-		    fprintf (stderr,
-			     "Failed to uninsert fast tracepoint jump "
-			     "at 0x%s (%s) while deleting it.\n",
-			     paddress (bp->pc), strerror (ret));
+		    debug_printf ("Failed to uninsert fast tracepoint jump "
+				  "at 0x%s (%s) while deleting it.\n",
+				  paddress (bp->pc), strerror (ret));
 		  return ret;
 		}
 
@@ -398,10 +395,9 @@ set_fast_tracepoint_jump (CORE_ADDR where,
   if (err != 0)
     {
       if (debug_threads)
-	fprintf (stderr,
-		 "Failed to read shadow memory of"
-		 " fast tracepoint at 0x%s (%s).\n",
-		 paddress (where), strerror (err));
+	debug_printf ("Failed to read shadow memory of"
+		      " fast tracepoint at 0x%s (%s).\n",
+		      paddress (where), strerror (err));
       free (jp);
       return NULL;
     }
@@ -424,9 +420,8 @@ set_fast_tracepoint_jump (CORE_ADDR where,
   if (err != 0)
     {
       if (debug_threads)
-	fprintf (stderr,
-		 "Failed to insert fast tracepoint jump at 0x%s (%s).\n",
-		 paddress (where), strerror (err));
+	debug_printf ("Failed to insert fast tracepoint jump at 0x%s (%s).\n",
+		      paddress (where), strerror (err));
 
       /* Unlink it.  */
       proc->fast_tracepoint_jumps = jp->next;
@@ -450,10 +445,9 @@ uninsert_fast_tracepoint_jumps_at (CORE_ADDR pc)
       /* This can happen when we remove all breakpoints while handling
 	 a step-over.  */
       if (debug_threads)
-	fprintf (stderr,
-		 "Could not find fast tracepoint jump at 0x%s "
-		 "in list (uninserting).\n",
-		 paddress (pc));
+	debug_printf ("Could not find fast tracepoint jump at 0x%s "
+		      "in list (uninserting).\n",
+		      paddress (pc));
       return;
     }
 
@@ -480,9 +474,9 @@ uninsert_fast_tracepoint_jumps_at (CORE_ADDR pc)
 	  jp->inserted = 1;
 
 	  if (debug_threads)
-	    fprintf (stderr,
-		     "Failed to uninsert fast tracepoint jump at 0x%s (%s).\n",
-		     paddress (pc), strerror (err));
+	    debug_printf ("Failed to uninsert fast tracepoint jump at"
+			  " 0x%s (%s).\n",
+			  paddress (pc), strerror (err));
 	}
     }
 }
@@ -500,10 +494,9 @@ reinsert_fast_tracepoint_jumps_at (CORE_ADDR where)
       /* This can happen when we remove breakpoints when a tracepoint
 	 hit causes a tracing stop, while handling a step-over.  */
       if (debug_threads)
-	fprintf (stderr,
-		 "Could not find fast tracepoint jump at 0x%s "
-		 "in list (reinserting).\n",
-		 paddress (where));
+	debug_printf ("Could not find fast tracepoint jump at 0x%s "
+		      "in list (reinserting).\n",
+		      paddress (where));
       return;
     }
 
@@ -528,9 +521,9 @@ reinsert_fast_tracepoint_jumps_at (CORE_ADDR where)
       jp->inserted = 0;
 
       if (debug_threads)
-	fprintf (stderr,
-		 "Failed to reinsert fast tracepoint jump at 0x%s (%s).\n",
-		 paddress (where), strerror (err));
+	debug_printf ("Failed to reinsert fast tracepoint jump at"
+		      " 0x%s (%s).\n",
+		      paddress (where), strerror (err));
     }
 }
 
@@ -598,10 +591,9 @@ delete_raw_breakpoint (struct process_info *proc, struct raw_breakpoint *todel)
 		  *bp_link = prev_bp_link;
 
 		  if (debug_threads)
-		    fprintf (stderr,
-			     "Failed to uninsert raw breakpoint "
-			     "at 0x%s (%s) while deleting it.\n",
-			     paddress (bp->pc), strerror (ret));
+		    debug_printf ("Failed to uninsert raw breakpoint "
+				  "at 0x%s (%s) while deleting it.\n",
+				  paddress (bp->pc), strerror (ret));
 		  return ret;
 		}
 
@@ -932,9 +924,9 @@ gdb_no_commands_at_breakpoint (CORE_ADDR where)
     return 0;
 
   if (debug_threads)
-    fprintf (stderr, "at 0x%s, bp command_list is 0x%s\n",
-	     paddress (where),
-	     phex_nz ((uintptr_t) bp->command_list, 0));
+    debug_printf ("at 0x%s, bp command_list is 0x%s\n",
+		  paddress (where),
+		  phex_nz ((uintptr_t) bp->command_list, 0));
   return (bp->command_list == NULL);
 }
 
@@ -1034,9 +1026,8 @@ uninsert_raw_breakpoint (struct raw_breakpoint *bp)
 	  bp->inserted = 1;
 
 	  if (debug_threads)
-	    fprintf (stderr,
-		     "Failed to uninsert raw breakpoint at 0x%s (%s).\n",
-		     paddress (bp->pc), strerror (err));
+	    debug_printf ("Failed to uninsert raw breakpoint at 0x%s (%s).\n",
+			  paddress (bp->pc), strerror (err));
 	}
     }
 }
@@ -1052,10 +1043,9 @@ uninsert_breakpoints_at (CORE_ADDR pc)
       /* This can happen when we remove all breakpoints while handling
 	 a step-over.  */
       if (debug_threads)
-	fprintf (stderr,
-		 "Could not find breakpoint at 0x%s "
-		 "in list (uninserting).\n",
-		 paddress (pc));
+	debug_printf ("Could not find breakpoint at 0x%s "
+		      "in list (uninserting).\n",
+		      paddress (pc));
       return;
     }
 
@@ -1087,9 +1077,8 @@ reinsert_raw_breakpoint (struct raw_breakpoint *bp)
   if (err == 0)
     bp->inserted = 1;
   else if (debug_threads)
-    fprintf (stderr,
-	     "Failed to reinsert breakpoint at 0x%s (%s).\n",
-	     paddress (bp->pc), strerror (err));
+    debug_printf ("Failed to reinsert breakpoint at 0x%s (%s).\n",
+		  paddress (bp->pc), strerror (err));
 }
 
 void
@@ -1103,10 +1092,9 @@ reinsert_breakpoints_at (CORE_ADDR pc)
       /* This can happen when we remove all breakpoints while handling
 	 a step-over.  */
       if (debug_threads)
-	fprintf (stderr,
-		 "Could not find raw breakpoint at 0x%s "
-		 "in list (reinserting).\n",
-		 paddress (pc));
+	debug_printf ("Could not find raw breakpoint at 0x%s "
+		      "in list (reinserting).\n",
+		      paddress (pc));
       return;
     }
 
diff --git a/gdb/gdbserver/remote-utils.c b/gdb/gdbserver/remote-utils.c
index 75ace6e..3b88995 100644
--- a/gdb/gdbserver/remote-utils.c
+++ b/gdb/gdbserver/remote-utils.c
@@ -161,7 +161,7 @@ handle_accept_event (int err, gdb_client_data client_data)
   socklen_t tmp;
 
   if (debug_threads)
-    fprintf (stderr, "handling possible accept event\n");
+    debug_printf ("handling possible accept event\n");
 
   tmp = sizeof (sockaddr);
   remote_desc = accept (listen_desc, (struct sockaddr *) &sockaddr, &tmp);
@@ -1315,8 +1315,8 @@ prepare_resume_reply (char *buf, ptid_t ptid,
 		      struct target_waitstatus *status)
 {
   if (debug_threads)
-    fprintf (stderr, "Writing resume reply for %s:%d\n",
-	     target_pid_to_str (ptid), status->kind);
+    debug_printf ("Writing resume reply for %s:%d\n",
+		  target_pid_to_str (ptid), status->kind);
 
   switch (status->kind)
     {
diff --git a/gdb/gdbserver/server.c b/gdb/gdbserver/server.c
index c9d9eec..da8161c 100644
--- a/gdb/gdbserver/server.c
+++ b/gdb/gdbserver/server.c
@@ -222,8 +222,8 @@ start_inferior (char **argv)
     {
       int i;
       for (i = 0; new_argv[i]; ++i)
-	fprintf (stderr, "new_argv[%d] = \"%s\"\n", i, new_argv[i]);
-      fflush (stderr);
+	debug_printf ("new_argv[%d] = \"%s\"\n", i, new_argv[i]);
+      debug_flush ();
     }
 
 #ifdef SIGTTOU
@@ -2528,10 +2528,9 @@ queue_stop_reply_callback (struct inferior_list_entry *entry, void *arg)
 	      char *status_string
 		= target_waitstatus_to_string (&thread->last_status);
 
-	      fprintf (stderr,
-		       "Reporting thread %s as already stopped with %s\n",
-		       target_pid_to_str (entry->id),
-		       status_string);
+	      debug_printf ("Reporting thread %s as already stopped with %s\n",
+			    target_pid_to_str (entry->id),
+			    status_string);
 
 	      xfree (status_string);
 	    }
@@ -3064,6 +3063,8 @@ main (int argc, char *argv[])
 
   if (setjmp (toplevel))
     {
+      debug_level_reset ();
+
       /* If something fails and longjmps while detaching or killing
 	 inferiors, we'd end up here again, stuck in an infinite loop
 	 trap.  Be sure that if that happens, we exit immediately
@@ -3106,6 +3107,8 @@ main (int argc, char *argv[])
 	    }
 	}
 
+      debug_level_reset ();
+
       /* Wait for events.  This will return when all event sources are
 	 removed from the event loop.  */
       start_event_loop ();
@@ -3197,14 +3200,14 @@ process_point_options (CORE_ADDR point_addr, char **packet)
 	{
 	  /* Conditional expression.  */
 	  if (debug_threads)
-	    fprintf (stderr, "Found breakpoint condition.\n");
+	    debug_printf ("Found breakpoint condition.\n");
 	  add_breakpoint_condition (point_addr, &dataptr);
 	}
       else if (strncmp (dataptr, "cmds:", strlen ("cmds:")) == 0)
 	{
 	  dataptr += strlen ("cmds:");
 	  if (debug_threads)
-	    fprintf (stderr, "Found breakpoint commands %s.\n", dataptr);
+	    debug_printf ("Found breakpoint commands %s.\n", dataptr);
 	  persist = (*dataptr == '1');
 	  dataptr += 2;
 	  add_breakpoint_commands (point_addr, &dataptr, persist);
@@ -3309,7 +3312,7 @@ process_serial_event (void)
 	  if (!non_stop)
 	    {
 	      if (debug_threads)
-		fprintf (stderr, "Forcing non-stop mode\n");
+		debug_printf ("Forcing non-stop mode\n");
 
 	      non_stop = 1;
 	      start_non_stop (1);
@@ -3702,7 +3705,7 @@ int
 handle_serial_event (int err, gdb_client_data client_data)
 {
   if (debug_threads)
-    fprintf (stderr, "handling possible serial event\n");
+    debug_printf ("handling possible serial event\n");
 
   /* Really handle it.  */
   if (process_serial_event () < 0)
@@ -3721,7 +3724,7 @@ int
 handle_target_event (int err, gdb_client_data client_data)
 {
   if (debug_threads)
-    fprintf (stderr, "handling possible target event\n");
+    debug_printf ("handling possible target event\n");
 
   last_ptid = mywait (minus_one_ptid, &last_status,
 		      TARGET_WNOHANG, 1);
@@ -3763,10 +3766,10 @@ handle_target_event (int err, gdb_client_data client_data)
 	      struct thread_resume resume_info;
 
 	      if (debug_threads)
-		fprintf (stderr,
-			 "GDB not connected; forwarding event %d for [%s]\n",
-			 (int) last_status.kind,
-			 target_pid_to_str (last_ptid));
+		debug_printf ("GDB not connected; forwarding event %d for"
+			      " [%s]\n",
+			      (int) last_status.kind,
+			      target_pid_to_str (last_ptid));
 
 	      resume_info.thread = last_ptid;
 	      resume_info.kind = resume_continue;
@@ -3774,9 +3777,9 @@ handle_target_event (int err, gdb_client_data client_data)
 	      (*the_target->resume) (&resume_info, 1);
 	    }
 	  else if (debug_threads)
-	    fprintf (stderr, "GDB not connected; ignoring event %d for [%s]\n",
-		     (int) last_status.kind,
-		     target_pid_to_str (last_ptid));
+	    debug_printf ("GDB not connected; ignoring event %d for [%s]\n",
+			  (int) last_status.kind,
+			  target_pid_to_str (last_ptid));
 	}
       else
 	{
diff --git a/gdb/gdbserver/thread-db.c b/gdb/gdbserver/thread-db.c
index 8b8f6ce..df06b9d 100644
--- a/gdb/gdbserver/thread-db.c
+++ b/gdb/gdbserver/thread-db.c
@@ -194,7 +194,7 @@ thread_db_create_event (CORE_ADDR where)
     fatal ("unexpected thread_db->td_ta_event_getmsg_p == NULL");
 
   if (debug_threads)
-    fprintf (stderr, "Thread creation event.\n");
+    debug_printf ("Thread creation event.\n");
 
   /* FIXME: This assumes we don't get another event.
      In the LinuxThreads implementation, this is safe,
@@ -289,8 +289,8 @@ find_one_thread (ptid_t ptid)
 	   lwpid, thread_db_err_str (err));
 
   if (debug_threads)
-    fprintf (stderr, "Found thread %ld (LWP %d)\n",
-	     ti.ti_tid, ti.ti_lid);
+    debug_printf ("Found thread %ld (LWP %d)\n",
+		  ti.ti_tid, ti.ti_lid);
 
   if (lwpid != ti.ti_lid)
     {
@@ -326,8 +326,8 @@ attach_thread (const td_thrhandle_t *th_p, td_thrinfo_t *ti_p)
   struct lwp_info *lwp;
 
   if (debug_threads)
-    fprintf (stderr, "Attaching to thread %ld (LWP %d)\n",
-	     ti_p->ti_tid, ti_p->ti_lid);
+    debug_printf ("Attaching to thread %ld (LWP %d)\n",
+		  ti_p->ti_tid, ti_p->ti_lid);
   linux_attach_lwp (ti_p->ti_lid);
   lwp = find_lwp_pid (pid_to_ptid (ti_p->ti_lid));
   if (lwp == NULL)
@@ -432,8 +432,8 @@ thread_db_find_new_threads (void)
 					 TD_THR_LOWEST_PRIORITY,
 					 TD_SIGNO_MASK, TD_THR_ANY_USER_FLAGS);
       if (debug_threads)
-	fprintf (stderr, "Found %d threads in iteration %d.\n",
-		 new_thread_count, iteration);
+	debug_printf ("Found %d threads in iteration %d.\n",
+		      new_thread_count, iteration);
 
       if (new_thread_count != 0)
 	{
@@ -546,7 +546,7 @@ thread_db_load_search (void)
   if (err != TD_OK)
     {
       if (debug_threads)
-	fprintf (stderr, "td_ta_new(): %s\n", thread_db_err_str (err));
+	debug_printf ("td_ta_new(): %s\n", thread_db_err_str (err));
       free (tdb);
       proc->private->thread_db = NULL;
       return 0;
@@ -595,7 +595,7 @@ try_thread_db_load_1 (void *handle)
       if ((a) == NULL)						\
 	{							\
 	  if (debug_threads)					\
-	    fprintf (stderr, "dlsym: %s\n", dlerror ());	\
+	    debug_printf ("dlsym: %s\n", dlerror ());		\
 	  if (required)						\
 	    {							\
 	      free (tdb);					\
@@ -613,7 +613,7 @@ try_thread_db_load_1 (void *handle)
   if (err != TD_OK)
     {
       if (debug_threads)
-	fprintf (stderr, "td_ta_new(): %s\n", thread_db_err_str (err));
+	debug_printf ("td_ta_new(): %s\n", thread_db_err_str (err));
       free (tdb);
       proc->private->thread_db = NULL;
       return 0;
@@ -663,13 +663,13 @@ try_thread_db_load (const char *library)
   void *handle;
 
   if (debug_threads)
-    fprintf (stderr, "Trying host libthread_db library: %s.\n",
-	     library);
+    debug_printf ("Trying host libthread_db library: %s.\n",
+		  library);
   handle = dlopen (library, RTLD_NOW);
   if (handle == NULL)
     {
       if (debug_threads)
-	fprintf (stderr, "dlopen failed: %s.\n", dlerror ());
+	debug_printf ("dlopen failed: %s.\n", dlerror ());
       return 0;
     }
 
@@ -786,7 +786,7 @@ thread_db_load_search (void)
 
   free_char_ptr_vec (dir_vec);
   if (debug_threads)
-    fprintf (stderr, "thread_db_load_search returning %d\n", rc);
+    debug_printf ("thread_db_load_search returning %d\n", rc);
   return rc;
 }
 
diff --git a/gdb/gdbserver/tracepoint.c b/gdb/gdbserver/tracepoint.c
index 13b3ff6..3706577 100644
--- a/gdb/gdbserver/tracepoint.c
+++ b/gdb/gdbserver/tracepoint.c
@@ -61,6 +61,8 @@
 
 */
 
+#ifdef IN_PROCESS_AGENT
+
 static void trace_vdebug (const char *, ...) ATTRIBUTE_PRINTF (1, 2);
 
 static void
@@ -81,6 +83,19 @@ trace_vdebug (const char *fmt, ...)
       trace_vdebug ((fmt), ##args);		\
   } while (0)
 
+#else
+
+#define trace_debug_1(level, fmt, args...)	\
+  do {						\
+    if (level <= debug_threads)			\
+      {						\
+	debug_printf ((fmt), ##args);		\
+	debug_printf ("\n");			\
+      }						\
+  } while (0)
+
+#endif
+
 #define trace_debug(FMT, args...)		\
   trace_debug_1 (1, FMT, ##args)
 
@@ -338,7 +353,7 @@ tracepoint_look_up_symbols (void)
       if (look_up_one_symbol (symbol_list[i].name, addrp, 1) == 0)
 	{
 	  if (debug_threads)
-	    fprintf (stderr, "symbol `%s' not found\n", symbol_list[i].name);
+	    debug_printf ("symbol `%s' not found\n", symbol_list[i].name);
 	  return;
 	}
     }
diff --git a/gdb/gdbserver/utils.c b/gdb/gdbserver/utils.c
index eff4499..1ce5512 100644
--- a/gdb/gdbserver/utils.c
+++ b/gdb/gdbserver/utils.c
@@ -17,6 +17,7 @@
    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
 
 #include "server.h"
+#include <sys/time.h>
 #include <stdio.h>
 #include <string.h>
 #include <stdlib.h>
@@ -32,6 +33,11 @@
 #  define TOOLNAME "GDBserver"
 #endif
 
+/* The current debug printf call nesting level.
+   This is used to add some heirarchy to the debug logging messages
+   and make them easier to read.  */
+static int debug_nesting_level = 0;
+
 /* Generally useful subroutines used throughout the program.  */
 
 void
@@ -165,6 +171,89 @@ internal_error (const char *file, int line, const char *fmt, ...)
   exit (1);
 }
 
+/* Print a debugging message.
+   If the text begins a new line it is preceded by a timestamp, if the
+   system has gettimeofday.
+   We don't get fancy with newline checking, we just check whether the
+   previous call ended with "\n".  */
+
+void
+debug_printf (const char *msg, ...)
+{
+  va_list args;
+#ifdef HAVE_GETTIMEOFDAY
+  static int new_line = 1;
+
+  if (new_line)
+    {
+      struct timeval tm;
+
+      gettimeofday (&tm, NULL);
+      fprintf (stderr, "%ld:%06ld ", (long) tm.tv_sec, (long) tm.tv_usec);
+    }
+#endif
+
+  va_start (args, msg);
+  vfprintf (stderr, msg, args);
+  va_end (args);
+
+#ifdef HAVE_GETTIMEOFDAY
+  if (*msg)
+    new_line = msg[strlen (msg) - 1] == '\n';
+#endif
+}
+
+/* Flush debugging output.
+   This is called, for example, when starting an inferior to ensure all debug
+   output thus far appears before any inferior output.  */
+
+void
+debug_flush (void)
+{
+  fflush (stderr);
+}
+
+/* Reset the debug printf call nesting level.
+   This is called from the top level error (setjmp) handler.  */
+
+void
+debug_level_reset (void)
+{
+  debug_nesting_level = 0;
+}
+
+/* Increase or decrease the debug printf call nesting level.
+   FUNCTION_NAME is the name of the calling function, or NULL if unknown.
+   Call this when entering major routines that can trigger a lot of debug
+   output before it exits.  It allows the reader to associate subsequent
+   debug output to the call that ultimately triggered it.  */
+
+void
+debug_level_incr (int incr, const char *function_name)
+{
+  gdb_assert (incr == 1 || incr == -1);
+
+  /* Increment(/decrement) the level by one before printing the function name,
+     to distinguish this as an entry(/exit) point.
+     Then increment(/decrement) it again so that debugging printfs within
+     the function are recognized as such.  */
+  if (function_name != NULL)
+    {
+      debug_nesting_level += incr;
+      debug_printf ("%s %s\n",
+		    incr > 0 ? ">>>>entering" : "<<<<exiting",
+		    function_name);
+    }
+  debug_nesting_level += incr;
+
+  /* Don't crash on mismatched enter/exit, but still inform the user.  */
+  if (debug_nesting_level < 0)
+    {
+      debug_printf ("ERROR: mismatch in debug_level_enter/exit, level < 0\n");
+      debug_nesting_level = 0;
+    }
+}
+
 /* Temporary storage using circular buffer.  */
 #define NUMCELLS 10
 #define CELLSIZE 50
diff --git a/gdb/gdbserver/utils.h b/gdb/gdbserver/utils.h
index 6d3df71..6c781c0 100644
--- a/gdb/gdbserver/utils.h
+++ b/gdb/gdbserver/utils.h
@@ -19,10 +19,44 @@
 #ifndef UTILS_H
 #define UTILS_H
 
+/* Version 2.4 and later of GCC define a magical variable `__PRETTY_FUNCTION__'
+   which contains the name of the function currently being defined.
+   This is broken in G++ before version 2.6.
+   C9x has a similar variable called __func__, but prefer the GCC one since
+   it demangles C++ function names.  */
+#if (GCC_VERSION >= 2004)
+#define FUNCTION_NAME __PRETTY_FUNCTION__
+#else
+#if defined __STDC_VERSION__ && __STDC_VERSION__ >= 199901L
+#define FUNCTION_NAME __func__
+#endif
+#endif
+
 void perror_with_name (const char *string);
 void error (const char *string,...) ATTRIBUTE_NORETURN ATTRIBUTE_PRINTF (1, 2);
 void fatal (const char *string,...) ATTRIBUTE_NORETURN ATTRIBUTE_PRINTF (1, 2);
 void warning (const char *string,...) ATTRIBUTE_PRINTF (1, 2);
+
+void debug_printf (const char *msg, ...) ATTRIBUTE_PRINTF (1, 2);
+void debug_flush (void);
+void debug_level_reset (void);
+void debug_level_incr (int incr, const char *function_name);
+
+/* These macros are for use in major functions that produce a lot of
+   debugging output.  They help identify in the mass of debugging output
+   when these functions enter and exit.  */
+#ifdef FUNCTION_NAME
+#define debug_level_enter() \
+  do { debug_level_incr (1, FUNCTION_NAME); } while (0)
+#define debug_level_exit() \
+  do { debug_level_incr (-1, FUNCTION_NAME); } while (0)
+#else
+#define debug_level_enter() \
+  do { debug_level_incr (1, NULL); } while (0)
+#define debug_level_exit() \
+  do { debug_level_incr (-1, NULL); } while (0)
+#endif
+
 char *paddress (CORE_ADDR addr);
 char *pulongest (ULONGEST u);
 char *plongest (LONGEST l);


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