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] gdb,gdbserver: Introduce "inform"


On 11/23/2016 10:19 AM, Pedro Alves wrote:
We have 'error', 'warning' and 'debug_printf', all usable throughout
gdb, gdbserver and the shared directories, but we don't have a common
function to print normal output to the user.  Instead, gdbserver code
uses "fprintf (stderr, ...)", while gdb uses "printf_filtered (....)"
(to stdout).

This adds a new "inform" function for the "normal output" case, and
makes gdbserver use it.  I added it to common/errors.c even though
it's not an error just because it's convenient to find it described
alongside error/warning, I think.  And I added it to common/ even
though nothing uses it outside gdbserver, in antecipation that
gdb/common/, gdb/nat/ or gdb/arch/ code will want to make use of it.

This is my last (I think) patch of a series getting rid of naked
"fprintf" calls , to help making a switch to using gnulib's C++
namespace support more palatable, though as the others, I think it
stands on its own.

gdb/gdbserver/ChangeLog:
yyyy-mm-dd  Pedro Alves <palves@redhat.com>

	* remote-utils.c (handle_accept_event, remote_open)
	(input_interrupt, getpkt): Use inform.
	* server.c (start_inferior, attach_inferior, print_started_pid)
	(detach_or_kill_for_exit, detach_or_kill_for_exit_cleanup)
	(captured_main, main, process_point_options)
	(process_serial_event): Use inform.
	* target.c (mywait): Use inform.
	* utils.c (vinform): New function.

gdb/ChangeLog:
yyyy-mm-dd  Pedro Alves <palves@redhat.com>

	* common/errors.c (inform): New function.
	* common/errors.h (inform, vinform): New declarations.
	* utils.c (vinform): New function.
---
 gdb/common/errors.c          | 12 +++++++++
 gdb/common/errors.h          | 11 ++++++++
 gdb/gdbserver/remote-utils.c | 29 +++++++++-----------
 gdb/gdbserver/server.c       | 63 +++++++++++++++++++-------------------------
 gdb/gdbserver/target.c       |  9 +++----
 gdb/gdbserver/utils.c        |  8 ++++++
 gdb/utils.c                  |  7 +++++
 7 files changed, 82 insertions(+), 57 deletions(-)

diff --git a/gdb/common/errors.c b/gdb/common/errors.c
index 1fc9602..57dd50d 100644
--- a/gdb/common/errors.c
+++ b/gdb/common/errors.c
@@ -47,6 +47,18 @@ error (const char *fmt, ...)
 /* See common/errors.h.  */

 void
+inform (const char *fmt, ...)
+{
+  va_list ap;
+
+  va_start (ap, fmt);
+  vinform (fmt, ap);
+  va_end (ap);
+}
+
+/* See common/errors.h.  */
+
+void
 internal_error (const char *file, int line, const char *fmt, ...)
 {
   va_list ap;
diff --git a/gdb/common/errors.h b/gdb/common/errors.h
index 56979ad..e6752f4 100644
--- a/gdb/common/errors.h
+++ b/gdb/common/errors.h
@@ -42,6 +42,17 @@ extern void error (const char *fmt, ...)
 extern void verror (const char *fmt, va_list args)
      ATTRIBUTE_NORETURN ATTRIBUTE_PRINTF (1, 0);

+/* Inform the user about something.  Informations are not related to
+   any problem.  They're just regular program output.  An information
+   message is constructed using a printf- or vprintf-style argument
+   list.  The function "vinform" must be provided by the client.  */
+
+extern void inform (const char *fmt, ...)
+     ATTRIBUTE_PRINTF (1, 2);
+
+extern void vinform (const char *fmt, va_list args)
+     ATTRIBUTE_PRINTF (1, 0);
+
 /* An internal error was detected.  Internal errors indicate
    programming errors such as assertion failures, as opposed to
    more general errors beyond the application's control.  These
diff --git a/gdb/gdbserver/remote-utils.c b/gdb/gdbserver/remote-utils.c
index b6c521b..d8df5b5 100644
--- a/gdb/gdbserver/remote-utils.c
+++ b/gdb/gdbserver/remote-utils.c
@@ -194,8 +194,7 @@ handle_accept_event (int err, gdb_client_data client_data)
   delete_file_handler (listen_desc);

   /* Convert IP address to string.  */
-  fprintf (stderr, "Remote debugging from host %s\n",
-	   inet_ntoa (sockaddr.sin_addr));
+  inform ("Remote debugging from host %s\n", inet_ntoa (sockaddr.sin_addr));

   enable_async_notification (remote_desc);

@@ -296,7 +295,7 @@ remote_open (char *name)

   if (strcmp (name, STDIO_CONNECTION_NAME) == 0)
     {
-      fprintf (stderr, "Remote debugging using stdio\n");
+      inform ("Remote debugging using stdio\n");

       /* Use stdin as the handle of the connection.
 	 We only select on reads, for example.  */
@@ -368,7 +367,7 @@ remote_open (char *name)
       }
 #endif

-      fprintf (stderr, "Remote debugging using %s\n", name);
+      inform ("Remote debugging using %s\n", name);

       enable_async_notification (remote_desc);

@@ -389,8 +388,7 @@ remote_open (char *name)
 	perror_with_name ("Can't determine port");
       port = ntohs (sockaddr.sin_port);

-      fprintf (stderr, "Listening on port %d\n", port);
-      fflush (stderr);
+      inform ("Listening on port %d\n", port);

       /* Register the event loop handler.  */
       add_file_handler (listen_desc, handle_accept_event, NULL);
@@ -750,16 +748,16 @@ input_interrupt (int unused)

       if (cc == 0)
 	{
-	  fprintf (stderr, "client connection closed\n");
+	  inform ("client connection closed\n");
 	  return;
 	}
       else if (cc != 1 || c != '\003')
 	{
-	  fprintf (stderr, "input_interrupt, count = %d c = %d ", cc, c);
+	  inform ("input_interrupt, count = %d c = %d ", cc, c);
 	  if (isprint (c))
-	    fprintf (stderr, "('%c')\n", c);
+	    inform ("('%c')\n", c);
 	  else
-	    fprintf (stderr, "('\\x%02x')\n", c & 0xff);
+	    inform ("('\\x%02x')\n", c & 0xff);
 	  return;
 	}

@@ -1006,16 +1004,15 @@ getpkt (char *buf)

       if (noack_mode)
 	{
-	  fprintf (stderr,
-		   "Bad checksum, sentsum=0x%x, csum=0x%x, "
-		   "buf=%s [no-ack-mode, Bad medium?]\n",
-		   (c1 << 4) + c2, csum, buf);
+	  inform ("Bad checksum, sentsum=0x%x, csum=0x%x, "
+		  "buf=%s [no-ack-mode, Bad medium?]\n",
+		  (c1 << 4) + c2, csum, buf);
 	  /* Not much we can do, GDB wasn't expecting an ack/nac.  */
 	  break;
 	}

-      fprintf (stderr, "Bad checksum, sentsum=0x%x, csum=0x%x, buf=%s\n",
-	       (c1 << 4) + c2, csum, buf);
+      inform ("Bad checksum, sentsum=0x%x, csum=0x%x, buf=%s\n",
+	      (c1 << 4) + c2, csum, buf);
       if (write_prim ("-", 1) != 1)
 	return -1;
     }
diff --git a/gdb/gdbserver/server.c b/gdb/gdbserver/server.c
index 52c5b73..464e7a4 100644
--- a/gdb/gdbserver/server.c
+++ b/gdb/gdbserver/server.c
@@ -275,9 +275,7 @@ start_inferior (char **argv)

   /* FIXME: we don't actually know at this point that the create
      actually succeeded.  We won't know that until we wait.  */
-  fprintf (stderr, "Process %s created; pid = %ld\n", argv[0],
-	   signal_pid);
-  fflush (stderr);
+  inform ("Process %s created; pid = %ld\n", argv[0], signal_pid);

 #ifdef SIGTTOU
   signal (SIGTTOU, SIG_IGN);
@@ -343,8 +341,7 @@ attach_inferior (int pid)
   if (myattach (pid) != 0)
     return -1;

-  fprintf (stderr, "Attached; pid = %d\n", pid);
-  fflush (stderr);
+  inform ("Attached; pid = %d\n", pid);

   /* FIXME - It may be that we should get the SIGNAL_PID from the
      attach function, so that it can be the main thread instead of
@@ -3429,7 +3426,7 @@ print_started_pid (struct inferior_list_entry *entry)
   if (! process->attached)
     {
       int pid = ptid_get_pid (process->entry.id);
-      fprintf (stderr, " %d", pid);
+      inform (" %d", pid);
     }
 }

@@ -3444,7 +3441,7 @@ print_attached_pid (struct inferior_list_entry *entry)
   if (process->attached)
     {
       int pid = ptid_get_pid (process->entry.id);
-      fprintf (stderr, " %d", pid);
+      inform (" %d", pid);
     }
 }

@@ -3461,15 +3458,15 @@ detach_or_kill_for_exit (void)

   if (have_started_inferiors_p ())
     {
-      fprintf (stderr, "Killing process(es):");
+      inform ("Killing process(es):");
       for_each_inferior (&all_processes, print_started_pid);
-      fprintf (stderr, "\n");
+      inform ("\n");
     }
   if (have_attached_inferiors_p ())
     {
-      fprintf (stderr, "Detaching process(es):");
+      inform ("Detaching process(es):");
       for_each_inferior (&all_processes, print_attached_pid);
-      fprintf (stderr, "\n");
+      inform ("\n");
     }

   /* Now we can kill or detach the inferiors.  */
@@ -3494,7 +3491,7 @@ detach_or_kill_for_exit_cleanup (void *ignore)
   CATCH (exception, RETURN_MASK_ALL)
     {
       fflush (stdout);
-      fprintf (stderr, "Detach or kill failed: %s\n", exception.message);
+      inform ("Detach or kill failed: %s\n", exception.message);
       exit_code = 1;
     }
   END_CATCH
@@ -3557,7 +3554,7 @@ captured_main (int argc, char *argv[])

 	  if (error_msg != NULL)
 	    {
-	      fprintf (stderr, "%s", error_msg);
+	      inform ("%s", error_msg);
 	      exit (1);
 	    }
 	}
@@ -3594,8 +3591,7 @@ captured_main (int argc, char *argv[])
 		}
 	      else
 		{
-		  fprintf (stderr, "Don't know how to disable \"%s\".\n\n",
-			   tok);
+		  inform ("Don't know how to disable \"%s\".\n\n", tok);
 		  gdbserver_show_disableable (stderr);
 		  exit (1);
 		}
@@ -3616,7 +3612,7 @@ captured_main (int argc, char *argv[])
 	run_once = 1;
       else
 	{
-	  fprintf (stderr, "Unknown argument: %s\n", *next_arg);
+	  inform ("Unknown argument: %s\n", *next_arg);
 	  exit (1);
 	}

@@ -3766,9 +3762,8 @@ captured_main (int argc, char *argv[])
 	  if (run_once || (!extended_protocol && !target_running ()))
 	    throw_quit ("Quit");

-	  fprintf (stderr,
-		   "Remote side has terminated connection.  "
-		   "GDBserver will reopen the connection.\n");
+	  inform ("Remote side has terminated connection.  "
+		  "GDBserver will reopen the connection.\n");

 	  /* Get rid of any pending statuses.  An eventual reconnection
 	     (by the same GDB instance or another) will refresh all its
@@ -3798,9 +3793,8 @@ captured_main (int argc, char *argv[])
 		}
 	      else
 		{
-		  fprintf (stderr,
-			   "Disconnected tracing disabled; "
-			   "stopping trace run.\n");
+		  inform ("Disconnected tracing disabled; "
+			  "stopping trace run.\n");
 		  stop_tracing ();
 		}
 	    }
@@ -3808,7 +3802,7 @@ captured_main (int argc, char *argv[])
       CATCH (exception, RETURN_MASK_ERROR)
 	{
 	  fflush (stdout);
-	  fprintf (stderr, "gdbserver: %s\n", exception.message);
+	  inform ("gdbserver: %s\n", exception.message);

 	  if (response_needed)
 	    {
@@ -3838,8 +3832,8 @@ main (int argc, char *argv[])
       if (exception.reason == RETURN_ERROR)
 	{
 	  fflush (stdout);
-	  fprintf (stderr, "%s\n", exception.message);
-	  fprintf (stderr, "Exiting\n");
+	  inform ("%s\n", exception.message);
+	  inform ("Exiting\n");
 	  exit_code = 1;
 	}

@@ -3891,8 +3885,7 @@ process_point_options (struct gdb_breakpoint *bp, char **packet)
 	}
       else
 	{
-	  fprintf (stderr, "Unknown token %c, ignoring.\n",
-		   *dataptr);
+	  inform ("Unknown token %c, ignoring.\n", *dataptr);
 	  /* Skip tokens until we find one that we recognize.  */
 	  dataptr = strchrnul (dataptr, ';');
 	}
@@ -3963,14 +3956,12 @@ process_serial_event (void)
 	    }

 	  if (tracing && disconnected_tracing)
-	    fprintf (stderr,
-		     "Disconnected tracing in effect, "
-		     "leaving gdbserver attached to the process\n");
+	    inform ("Disconnected tracing in effect, "
+		    "leaving gdbserver attached to the process\n");

 	  if (any_persistent_commands ())
-	    fprintf (stderr,
-		     "Persistent commands are present, "
-		     "leaving gdbserver attached to the process\n");
+	    inform ("Persistent commands are present, "
+		    "leaving gdbserver attached to the process\n");

 	  /* Make sure we're in non-stop/async mode, so we we can both
 	     wait for an async socket accept, and handle async target
@@ -3995,7 +3986,7 @@ process_serial_event (void)
 	  break; /* from switch/case */
 	}

-      fprintf (stderr, "Detaching from process %d\n", pid);
+      inform ("Detaching from process %d\n", pid);
       stop_tracing ();
       if (detach_inferior (pid) != 0)
 	write_enn (own_buf);
@@ -4258,7 +4249,7 @@ process_serial_event (void)
 	   reply to it, either.  */
 	return 0;

-      fprintf (stderr, "Killing all inferiors\n");
+      inform ("Killing all inferiors\n");
       for_each_inferior (&all_processes, kill_inferior_callback);

       /* When using the extended protocol, we wait with no program
@@ -4302,7 +4293,7 @@ process_serial_event (void)
 	  if (target_running ())
 	    for_each_inferior (&all_processes,
 			       kill_inferior_callback);
-	  fprintf (stderr, "GDBserver restarting\n");
+	  inform ("GDBserver restarting\n");

 	  /* Wait till we are at 1st instruction in prog.  */
 	  if (program_argv != NULL)
diff --git a/gdb/gdbserver/target.c b/gdb/gdbserver/target.c
index 249a063..97d9fe8 100644
--- a/gdb/gdbserver/target.c
+++ b/gdb/gdbserver/target.c
@@ -227,12 +227,11 @@ mywait (ptid_t ptid, struct target_waitstatus *ourstatus, int options,
   if (!remote_connection_is_stdio ())
     {
       if (ourstatus->kind == TARGET_WAITKIND_EXITED)
-	fprintf (stderr,
-		 "\nChild exited with status %d\n", ourstatus->value.integer);
+	inform ("\nChild exited with status %d\n", ourstatus->value.integer);
       else if (ourstatus->kind == TARGET_WAITKIND_SIGNALLED)
-	fprintf (stderr, "\nChild terminated with signal = 0x%x (%s)\n",
-		 gdb_signal_to_host (ourstatus->value.sig),
-		 gdb_signal_to_name (ourstatus->value.sig));
+	inform ("\nChild terminated with signal = 0x%x (%s)\n",
+		gdb_signal_to_host (ourstatus->value.sig),
+		gdb_signal_to_name (ourstatus->value.sig));
     }

   if (connected_wait)
diff --git a/gdb/gdbserver/utils.c b/gdb/gdbserver/utils.c
index 37b9c89..2dca14d 100644
--- a/gdb/gdbserver/utils.c
+++ b/gdb/gdbserver/utils.c
@@ -94,6 +94,14 @@ vwarning (const char *string, va_list args)
   fprintf (stderr, "\n");
 }

+/* See common/errors.h.  */
+
+void
+vinform (const char *string, va_list args)
+{
+  vfprintf (stderr, string, args);
+}
+
 /* Report a problem internal to GDBserver, and exit.  */

 void
diff --git a/gdb/utils.c b/gdb/utils.c
index 8ca0a2e..40bc07c3 100644
--- a/gdb/utils.c
+++ b/gdb/utils.c
@@ -432,6 +432,13 @@ free_current_contents (void *ptr)
 }
 

+/* See common/errors.h  */
+
+void
+vinform (const char *string, va_list args)
+{
+  vprintf_filtered (string, args);
+}

 /* Print a warning message.  The first argument STRING is the warning
    message, used as an fprintf format string, the second is the
-- 2.5.5


Looks good to me. I noticed the gdbserver/GDBserver inconsistency, but that can be addressed in a different patch.


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