diff mbox

[2/3] debugging with yama: add suberror code to gdb_exception

Message ID 047d7b15aa736cfe890520d3ae29@google.com
State New
Headers show

Commit Message

Doug Evans Sept. 28, 2015, 7:27 p.m. UTC
Hi.

This patch basically just adds a suberror code to struct gdb_exception
so that we can pass errno in the exception, and then makes passing of
errno explicit in a couple of places.

2015-09-28  Doug Evans  <dje@google.com>

	* common/common-exceptions.c (exceptions_state_mc_init): Initialize
	suberror.
	(throw_it): New arg suberror.  All callers updated.
	(throw_error_with_suberror): New function.
	* common/common-exceptions.h (enum errors): New enum value
	SYSCALL_FAILED_ERROR.
	(struct gdb_exception): New member suberror.
	(throw_error_with_suberror): Declare.
	* utils.c (perror_string): New arg errno_value.  All callers updated.
	(throw_perror_with_name): Ditto.  Call throw_error_with_suberror.


  extern void perror_warning_with_name (const char *string);
diff mbox

Patch

diff --git a/gdb/common/common-exceptions.c b/gdb/common/common-exceptions.c
index 8ee96ab..b88b232 100644
--- a/gdb/common/common-exceptions.c
+++ b/gdb/common/common-exceptions.c
@@ -20,7 +20,7 @@ 
  #include "common-defs.h"
  #include "common-exceptions.h"

-const struct gdb_exception exception_none = { 0, GDB_NO_ERROR, NULL };
+const struct gdb_exception exception_none = { 0, GDB_NO_ERROR, 0, NULL };

  #ifndef __cplusplus

@@ -314,9 +314,9 @@  static char **exception_messages;
  /* The number of currently allocated entries in exception_messages.  */
  static int exception_messages_size;

-static void ATTRIBUTE_NORETURN ATTRIBUTE_PRINTF (3, 0)
-throw_it (enum return_reason reason, enum errors error, const char *fmt,
-	  va_list ap)
+static void ATTRIBUTE_NORETURN ATTRIBUTE_PRINTF (4, 0)
+throw_it (enum return_reason reason, enum errors error, int suberror,
+	  const char *fmt, va_list ap)
  {
    struct gdb_exception e;
    char *new_message;
@@ -348,6 +348,7 @@  throw_it (enum return_reason reason, enum errors error,  
const char *fmt,
    /* Create the exception.  */
    e.reason = reason;
    e.error = error;
+  e.suberror = suberror;
    e.message = new_message;

    /* Throw the exception.  */
@@ -357,13 +358,13 @@  throw_it (enum return_reason reason, enum errors  
error, const char *fmt,
  void
  throw_verror (enum errors error, const char *fmt, va_list ap)
  {
-  throw_it (RETURN_ERROR, error, fmt, ap);
+  throw_it (RETURN_ERROR, error, 0, fmt, ap);
  }

  void
  throw_vquit (const char *fmt, va_list ap)
  {
-  throw_it (RETURN_QUIT, GDB_NO_ERROR, fmt, ap);
+  throw_it (RETURN_QUIT, GDB_NO_ERROR, 0, fmt, ap);
  }

  void
@@ -376,6 +377,19 @@  throw_error (enum errors error, const char *fmt, ...)
    va_end (args);
  }

+/* Throw ERROR with suberror value SUBERROR.  */
+
+void
+throw_error_with_suberror (enum errors error, int suberror,
+			   const char *fmt, ...)
+{
+  va_list args;
+
+  va_start (args, fmt);
+  throw_it (RETURN_ERROR, error, suberror, fmt, args);
+  va_end (args);
+}
+
  void
  throw_quit (const char *fmt, ...)
  {
diff --git a/gdb/common/common-exceptions.h b/gdb/common/common-exceptions.h
index 2e6a6d9..45054c1 100644
--- a/gdb/common/common-exceptions.h
+++ b/gdb/common/common-exceptions.h
@@ -105,6 +105,10 @@  enum errors {
       "_ERROR" is appended to the name.  */
    MAX_COMPLETIONS_REACHED_ERROR,

+  /* A system call failed.
+     The "errno" value is in gdb_exception.suberror.  */
+  SYSCALL_FAILED_ERROR,
+
    /* Add more errors here.  */
    NR_ERRORS
  };
@@ -113,6 +117,9 @@  struct gdb_exception
  {
    enum return_reason reason;
    enum errors error;
+  /* Extra information to describe ERROR.  Unless the error is documented  
to
+     provide a suberror code, this value is zero.  */
+  int suberror;
    const char *message;
  };

@@ -262,6 +269,9 @@  extern void throw_vquit (const char *fmt, va_list ap)
       ATTRIBUTE_NORETURN ATTRIBUTE_PRINTF (1, 0);
  extern void throw_error (enum errors error, const char *fmt, ...)
       ATTRIBUTE_NORETURN ATTRIBUTE_PRINTF (2, 3);
+extern void throw_error_with_suberror (enum errors error, int suberror,
+				       const char *fmt, ...)
+     ATTRIBUTE_NORETURN ATTRIBUTE_PRINTF (3, 4);
  extern void throw_quit (const char *fmt, ...)
       ATTRIBUTE_NORETURN ATTRIBUTE_PRINTF (1, 2);

diff --git a/gdb/utils.c b/gdb/utils.c
index c7f00d9..a6b5744 100644
--- a/gdb/utils.c
+++ b/gdb/utils.c
@@ -934,17 +916,17 @@  add_internal_problem_command (struct internal_problem  
*problem)
  }

  /* Return a newly allocated string, containing the PREFIX followed
-   by the system error message for errno (separated by a colon).
+   by the system error message for ERRNO_VALUE (separated by a colon).

     The result must be deallocated after use.  */

  static char *
-perror_string (const char *prefix)
+perror_string (const char *prefix, int errno_value)
  {
    char *err;
    char *combined;

-  err = safe_strerror (errno);
+  err = safe_strerror (errno_value);
    combined = (char *) xmalloc (strlen (err) + strlen (prefix) + 3);
    strcpy (combined, prefix);
    strcat (combined, ": ");
@@ -953,16 +935,16 @@  perror_string (const char *prefix)
    return combined;
  }

-/* Print the system error message for errno, and also mention STRING
-   as the file name for which the error was encountered.  Use ERRCODE
-   for the thrown exception.  Then return to command level.  */
+/* Throw an error with the system error message for ERRNO_VAL, and also  
mention
+   STRING as the file name or system call for which the error was  
encountered.
+   Use ERRCODE for the thrown exception.  */

  void
-throw_perror_with_name (enum errors errcode, const char *string)
+throw_perror_with_name (enum errors errcode, int errno_val, const char  
*string)
  {
    char *combined;

-  combined = perror_string (string);
+  combined = perror_string (string, errno_val);
    make_cleanup (xfree, combined);

    /* I understand setting these is a matter of taste.  Still, some people
@@ -971,7 +953,9 @@  throw_perror_with_name (enum errors errcode, const char  
*string)
    bfd_set_error (bfd_error_no_error);
    errno = 0;

-  throw_error (errcode, _("%s."), combined);
+  throw_error_with_suberror (errcode,
+			     errcode == SYSCALL_FAILED_ERROR ? errno_val : 0,
+			     _("%s."), combined);
  }

  /* See throw_perror_with_name, ERRCODE defaults here to GENERIC_ERROR.  */
@@ -979,7 +963,7 @@  throw_perror_with_name (enum errors errcode, const char  
*string)
  void
  perror_with_name (const char *string)
  {
-  throw_perror_with_name (GENERIC_ERROR, string);
+  throw_perror_with_name (GENERIC_ERROR, errno, string);
  }

  /* Same as perror_with_name except that it prints a warning instead
@@ -990,7 +974,7 @@  perror_warning_with_name (const char *string)
  {
    char *combined;

-  combined = perror_string (string);
+  combined = perror_string (string, errno);
    warning (_("%s"), combined);
    xfree (combined);
  }
diff --git a/gdb/utils.h b/gdb/utils.h
index 995a1cf..d1111fa 100644
--- a/gdb/utils.h
+++ b/gdb/utils.h
@@ -267,7 +263,8 @@  extern CORE_ADDR string_to_core_addr (const char  
*my_string);
  extern void fprintf_symbol_filtered (struct ui_file *, const char *,
  				     enum language, int);

-extern void throw_perror_with_name (enum errors errcode, const char  
*string)
+extern void throw_perror_with_name (enum errors errcode, int errno_val,
+				    const char *string)
    ATTRIBUTE_NORETURN;