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]

add gdb_assert/internal error to gdbserver


I've been finding it a real nuisance that we don't have gdb_assert
or internal_error in gdbserver.  `fatal' isn't so practical.  So
I added them.  Follow up patches will add uses.

Applied.

-- 
Pedro Alves

2010-03-16  Pedro Alves  <pedro@codesourcery.com>

	gdb/gdbserver/
	* server.h (internal_error): Declare.
	(gdb_assert, ASSERT_FUNCTION, gdb_assert_fail): Define.
	* utils.c (internal_error): New function.

---
 gdb/gdbserver/server.h |   30 ++++++++++++++++++++++++++++++
 gdb/gdbserver/utils.c  |   16 ++++++++++++++++
 2 files changed, 46 insertions(+)

Index: src/gdb/gdbserver/server.h
===================================================================
--- src.orig/gdb/gdbserver/server.h	2010-03-16 17:36:30.000000000 +0000
+++ src/gdb/gdbserver/server.h	2010-03-16 17:36:56.000000000 +0000
@@ -407,9 +407,39 @@ void freeargv (char **argv);
 void perror_with_name (const char *string);
 void error (const char *string,...) ATTR_NORETURN ATTR_FORMAT (printf, 1, 2);
 void fatal (const char *string,...) ATTR_NORETURN ATTR_FORMAT (printf, 1, 2);
+void internal_error (const char *file, int line, const char *, ...)
+     ATTR_NORETURN ATTR_FORMAT (printf, 3, 4);
 void warning (const char *string,...) ATTR_FORMAT (printf, 1, 2);
 char *paddress (CORE_ADDR addr);
 
+#define gdb_assert(expr)                                                      \
+  ((void) ((expr) ? 0 :                                                       \
+	   (gdb_assert_fail (#expr, __FILE__, __LINE__, ASSERT_FUNCTION), 0)))
+
+/* 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 ASSERT_FUNCTION		__PRETTY_FUNCTION__
+#else
+#if defined __STDC_VERSION__ && __STDC_VERSION__ >= 199901L
+#define ASSERT_FUNCTION		__func__
+#endif
+#endif
+
+/* This prints an "Assertion failed" message, and exits.  */
+#if defined (ASSERT_FUNCTION)
+#define gdb_assert_fail(assertion, file, line, function)		\
+  internal_error (file, line, _("%s: Assertion `%s' failed."),		\
+		  function, assertion)
+#else
+#define gdb_assert_fail(assertion, file, line, function)		\
+  internal_error (file, line, _("Assertion `%s' failed."),		\
+		  assertion)
+#endif
+
 /* Maximum number of bytes to read/write at once.  The value here
    is chosen to fill up a packet (the headers account for the 32).  */
 #define MAXBUFBYTES(N) (((N)-32)/2)
Index: src/gdb/gdbserver/utils.c
===================================================================
--- src.orig/gdb/gdbserver/utils.c	2010-03-16 17:36:29.000000000 +0000
+++ src/gdb/gdbserver/utils.c	2010-03-16 17:36:56.000000000 +0000
@@ -171,6 +171,22 @@ warning (const char *string,...)
   va_end (args);
 }
 
+/* Report a problem internal to GDBserver, and exit.  */
+
+void
+internal_error (const char *file, int line, const char *fmt, ...)
+{
+  va_list args;
+  va_start (args, fmt);
+
+  fprintf (stderr,  "\
+%s:%d: A problem internal to GDBserver has been detected.\n", file, line);
+  vfprintf (stderr, fmt, args);
+  fprintf (stderr, "\n");
+  va_end (args);
+  exit (1);
+}
+
 /* Temporary storage using circular buffer.  */
 #define NUMCELLS 4
 #define CELLSIZE 50


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