[05/15] Introduce and use debug_printf and debug_vprintf

Message ID 1404902255-11101-6-git-send-email-gbenson@redhat.com
State Changes Requested, archived
Headers

Commit Message

Gary Benson July 9, 2014, 10:37 a.m. UTC
  This introduces debug_printf and debug_vprintf, a function that
clients of "common" are expected to implement.  gdbserver's
existing debug_printf is repurposed as debug_vprintf, and a new
wrapper is written.

common/agent.c is changed to use debug_vprintf rather than
defining the macro DEBUG_AGENT depending on GDBSERVER.

nat/i386-dregs.c is changed to use the externally-implemented
debug_printf, rather than defining it itself.

gdb/
2014-07-09  Tom Tromey  <tromey@redhat.com>
	    Gary Benson  <gbenson@redhat.com>

	* common/common-debug.h: New file.
	* utils.h: Include common-debug.h.
	* utils.c (debug_vprintf): New function.
	(debug_printf): Likewise.
	* common/agent.c (debug_agent_print): New function.
	(DEBUG_AGENT): Redefine.
	* nat/i386-dregs.c (debug_printf): Undefine.

gdb/gdbserver/
2014-07-09  Tom Tromey  <tromey@redhat.com>
	    Gary Benson  <gbenson@redhat.com>

	* utils.h: Include common-debug.h.
	* debug.h (debug_printf): Don't declare.
	* debug.c (debug_vprintf): New function.
	(debug_printf): Use the above.
---
 gdb/ChangeLog             |   11 +++++++++++
 gdb/common/agent.c        |   24 +++++++++++++++---------
 gdb/common/common-debug.h |   35 +++++++++++++++++++++++++++++++++++
 gdb/gdbserver/ChangeLog   |    8 ++++++++
 gdb/gdbserver/debug.c     |   23 ++++++++++++++++-------
 gdb/gdbserver/debug.h     |    1 -
 gdb/gdbserver/utils.h     |    1 +
 gdb/nat/i386-dregs.c      |    4 ----
 gdb/utils.c               |   20 ++++++++++++++++++++
 gdb/utils.h               |    1 +
 10 files changed, 107 insertions(+), 21 deletions(-)
 create mode 100644 gdb/common/common-debug.h
  

Comments

Doug Evans July 11, 2014, 7:44 p.m. UTC | #1
Gary Benson writes:
 > This introduces debug_printf and debug_vprintf, a function that
 > clients of "common" are expected to implement.  gdbserver's
 > existing debug_printf is repurposed as debug_vprintf, and a new
 > wrapper is written.
 > 
 > common/agent.c is changed to use debug_vprintf rather than
 > defining the macro DEBUG_AGENT depending on GDBSERVER.
 > 
 > nat/i386-dregs.c is changed to use the externally-implemented
 > debug_printf, rather than defining it itself.
 > 
 > gdb/
 > 2014-07-09  Tom Tromey  <tromey@redhat.com>
 > 	    Gary Benson  <gbenson@redhat.com>
 > 
 > 	* common/common-debug.h: New file.
 > 	* utils.h: Include common-debug.h.
 > 	* utils.c (debug_vprintf): New function.
 > 	(debug_printf): Likewise.
 > 	* common/agent.c (debug_agent_print): New function.
 > 	(DEBUG_AGENT): Redefine.
 > 	* nat/i386-dregs.c (debug_printf): Undefine.
 > 
 > gdb/gdbserver/
 > 2014-07-09  Tom Tromey  <tromey@redhat.com>
 > 	    Gary Benson  <gbenson@redhat.com>
 > 
 > 	* utils.h: Include common-debug.h.
 > 	* debug.h (debug_printf): Don't declare.
 > 	* debug.c (debug_vprintf): New function.
 > 	(debug_printf): Use the above.

IWBN if there was more file naming consistency.
As a general rule, how objectionable is it to have
gdb/foo.c and gdbserver/foo.c for every shared foo.h header?
[Or common-foo.h header in the case of, e.g., common/common-debug.h.]
An alternative would be to move common-debug.h to a new directory,
e.g., shared, and call it shared/debug.h.  I'm only mentioning
this for discussion sake, it's not a requisite for this patch.

In this case, any objection to putting the gdb implementation
in gdb/debug.c instead of utils.c?
  
Gary Benson July 15, 2014, 3:21 p.m. UTC | #2
Doug Evans wrote:
> Gary Benson writes:
> > 2014-07-09  Tom Tromey  <tromey@redhat.com>
> >             Gary Benson  <gbenson@redhat.com>
> > 
> >         * common/common-debug.h: New file.
> >         * utils.h: Include common-debug.h.
> >         * utils.c (debug_vprintf): New function.
> >         (debug_printf): Likewise.
> >         * common/agent.c (debug_agent_print): New function.
> >         (DEBUG_AGENT): Redefine.
> >         * nat/i386-dregs.c (debug_printf): Undefine.
> > 
> > gdb/gdbserver/
> > 2014-07-09  Tom Tromey  <tromey@redhat.com>
> >             Gary Benson  <gbenson@redhat.com>
> > 
> >         * utils.h: Include common-debug.h.
> >         * debug.h (debug_printf): Don't declare.
> >         * debug.c (debug_vprintf): New function.
> >         (debug_printf): Use the above.
> 
> IWBN if there was more file naming consistency.
> As a general rule, how objectionable is it to have gdb/foo.c
> and gdbserver/foo.c for every shared foo.h header?

I like it.

> [Or common-foo.h header in the case of, e.g., common/common-debug.h.]
> An alternative would be to move common-debug.h to a new directory,
> e.g., shared, and call it shared/debug.h.  I'm only mentioning this
> for discussion sake, it's not a requisite for this patch.

I have a script to automate the changes required so we can remove
"-I/path/to/gdb/common" from $CFLAGS.  This would allow GDB and/or
gdbserver to have their own "foo.h" that could include "common/foo.h".
The script also removes all the "common-" and "gdb_" prefixes from
header files in common.  I plan to submit a patch after this series
is in.  This means this series is creating files that will almost
immediately be moved, but the alternative is I end up having to
extensively fix this series (and I already did that once already!)

> In this case, any objection to putting the gdb implementation
> in gdb/debug.c instead of utils.c?

No objection from me, I'll do it.

Thanks,
Gary
  

Patch

diff --git a/gdb/common/agent.c b/gdb/common/agent.c
index 54f861a..549b784 100644
--- a/gdb/common/agent.c
+++ b/gdb/common/agent.c
@@ -33,15 +33,21 @@ 
 
 int debug_agent = 0;
 
-#ifdef GDBSERVER
-#define DEBUG_AGENT(fmt, args...)	\
-  if (debug_agent)			\
-    fprintf (stderr, fmt, ##args);
-#else
-#define DEBUG_AGENT(fmt, args...)	\
-  if (debug_agent)			\
-    fprintf_unfiltered (gdb_stdlog, fmt, ##args);
-#endif
+/* A stdarg wrapper for debug_vprintf.  */
+
+static void ATTRIBUTE_PRINTF (1, 2)
+debug_agent_print (const char *fmt, ...)
+{
+  va_list ap;
+
+  if (!debug_agent)
+    return;
+  va_start (ap, fmt);
+  debug_vprintf (fmt, ap);
+  va_end (ap);
+}
+
+#define DEBUG_AGENT debug_agent_print
 
 /* Global flag to determine using agent or not.  */
 int use_agent = 0;
diff --git a/gdb/common/common-debug.h b/gdb/common/common-debug.h
new file mode 100644
index 0000000..0467610
--- /dev/null
+++ b/gdb/common/common-debug.h
@@ -0,0 +1,35 @@ 
+/* Declarations for debug printing functions.
+
+   Copyright (C) 2014 Free Software Foundation, Inc.
+
+   This file is part of GDB.
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
+
+#ifndef COMMON_DEBUG_H
+#define COMMON_DEBUG_H
+
+/* Print a formatted message to the appropriate channel for debugging
+   output for the client.  */
+
+extern void debug_printf (const char *format, ...)
+     ATTRIBUTE_PRINTF (1, 2);
+
+/* Print a formatted message to the appropriate channel for debugging
+   output for the client.  */
+
+extern void debug_vprintf (const char *format, va_list ap)
+     ATTRIBUTE_PRINTF (1, 0);
+
+#endif /* COMMON_DEBUG_H */
diff --git a/gdb/gdbserver/debug.c b/gdb/gdbserver/debug.c
index c50af76..c008b17 100644
--- a/gdb/gdbserver/debug.c
+++ b/gdb/gdbserver/debug.c
@@ -33,9 +33,8 @@  int debug_timestamp;
    previous call ended with "\n".  */
 
 void
-debug_printf (const char *msg, ...)
+debug_vprintf (const char *format, va_list ap)
 {
-  va_list args;
 #if !defined (IN_PROCESS_AGENT)
   /* N.B. Not thread safe, and can't be used, as is, with IPA.  */
   static int new_line = 1;
@@ -53,16 +52,26 @@  debug_printf (const char *msg, ...)
     }
 #endif
 
-  va_start (args, msg);
-  vfprintf (stderr, msg, args);
-  va_end (args);
+  vfprintf (stderr, format, ap);
 
 #if !defined (IN_PROCESS_AGENT)
-  if (*msg)
-    new_line = msg[strlen (msg) - 1] == '\n';
+  if (*format)
+    new_line = format[strlen (format) - 1] == '\n';
 #endif
 }
 
+/* Print a debugging message using debug_vprintf.  */
+
+void
+debug_printf (const char *format, ...)
+{
+  va_list ap;
+
+  va_start (ap, format);
+  debug_vprintf (format, ap);
+  va_end (ap);
+}
+
 /* Flush debugging output.
    This is called, for example, when starting an inferior to ensure all debug
    output thus far appears before any inferior output.  */
diff --git a/gdb/gdbserver/debug.h b/gdb/gdbserver/debug.h
index 0f056ca..42a3f21 100644
--- a/gdb/gdbserver/debug.h
+++ b/gdb/gdbserver/debug.h
@@ -29,7 +29,6 @@ 
 extern int debug_threads;
 extern int debug_timestamp;
 
-void debug_printf (const char *msg, ...) ATTRIBUTE_PRINTF (1, 2);
 void debug_flush (void);
 void do_debug_enter (const char *function_name);
 void do_debug_exit (const char *function_name);
diff --git a/gdb/gdbserver/utils.h b/gdb/gdbserver/utils.h
index 819fa35..d48179d 100644
--- a/gdb/gdbserver/utils.h
+++ b/gdb/gdbserver/utils.h
@@ -21,6 +21,7 @@ 
 
 #include "print-utils.h"
 #include "errors.h"
+#include "common-debug.h"
 
 char *paddress (CORE_ADDR addr);
 char *pfildes (gdb_fildes_t fd);
diff --git a/gdb/nat/i386-dregs.c b/gdb/nat/i386-dregs.c
index 1fa5c19..e3272cd 100644
--- a/gdb/nat/i386-dregs.c
+++ b/gdb/nat/i386-dregs.c
@@ -178,10 +178,6 @@  typedef enum { WP_INSERT, WP_REMOVE, WP_COUNT } i386_wp_op_t;
 #ifndef GDBSERVER
 /* Whether or not to print the mirrored debug registers.  */
 extern int debug_hw_points;
-
-/* Print debugging messages.  */
-#define debug_printf(fmt, args...) \
-  fprintf_unfiltered (gdb_stdlog, fmt, ##args);
 #endif
 
 /* Print the values of the mirrored debug registers.  */
diff --git a/gdb/utils.c b/gdb/utils.c
index 6f47cb0..064e2ee 100644
--- a/gdb/utils.c
+++ b/gdb/utils.c
@@ -886,6 +886,26 @@  demangler_warning (const char *file, int line, const char *string, ...)
   va_end (ap);
 }
 
+/* See common/common-debug.h.  */
+
+void
+debug_vprintf (const char *fmt, va_list ap)
+{
+  vfprintf_unfiltered (gdb_stdlog, fmt, ap);
+}
+
+/* See common/common-debug.h.  */
+
+void
+debug_printf (const char *fmt, ...)
+{
+  va_list ap;
+
+  va_start (ap, fmt);
+  debug_vprintf (fmt, ap);
+  va_end (ap);
+}
+
 /* Dummy functions to keep add_prefix_cmd happy.  */
 
 static void
diff --git a/gdb/utils.h b/gdb/utils.h
index d9fe7e3..914d0b3 100644
--- a/gdb/utils.h
+++ b/gdb/utils.h
@@ -25,6 +25,7 @@ 
 #include "exceptions.h"
 #include "print-utils.h"
 #include "errors.h"
+#include "common-debug.h"
 
 extern void initialize_utils (void);