btrace, gdbserver: use exceptions to convey btrace enable/disable errors
authorMarkus Metzger <markus.t.metzger@intel.com>
Fri, 19 Jan 2018 08:41:42 +0000 (09:41 +0100)
committerMarkus Metzger <markus.t.metzger@intel.com>
Fri, 9 Feb 2018 13:03:19 +0000 (14:03 +0100)
Change error reporting to use exceptions and be prepared to catch them in
gdbserver.  We use the exception message in our error reply to GDB.

This may remove some detail from the error message in the native case since
errno is no longer printed.  Later patches will improve that.

We're still using error strings on the RSP level.  This patch does not affect
the interoperability of older/newer GDB/gdbserver.

gdbserver/
* server.c (handle_btrace_enable_bts, handle_btrace_enable_pt)
(handle_btrace_disable): Change return type to void.  Use exceptions
to report errors.
(handle_btrace_general_set): Catch exception and copy message to
return message.

gdb/
* nat/linux-btrace.c (linux_enable_btrace): Throw exception if enabling
btrace failed.
* x86-linux-nat.c (x86_linux_enable_btrace): Catch btrace enabling
exception and use message in own exception.

gdb/ChangeLog
gdb/gdbserver/ChangeLog
gdb/gdbserver/server.c
gdb/nat/linux-btrace.c
gdb/x86-linux-nat.c

index 31d533433f7bc445975254075dc1a9079856a1c2..3ff2ccf7c5c3c27fa2df8541bfc8d9dd110fb167 100644 (file)
@@ -1,3 +1,10 @@
+2018-02-09  Markus Metzger  <markus.t.metzger@intel.com>
+
+       * nat/linux-btrace.c (linux_enable_btrace): Throw exception if enabling
+       btrace failed.
+       * x86-linux-nat.c (x86_linux_enable_btrace): Catch btrace enabling
+       exception and use message in own exception.
+
 2018-02-09  Markus Metzger  <markus.t.metzger@intel.com>
 
        * nat/linux-btrace.c: Include scoped_fd.h and scoped_mmap.h.
index b80d82058eaf032b4479ab89d376154bdd9c89df..1b8d6f6276c66f5b139f07337415d99fd4e4ad4f 100644 (file)
@@ -1,3 +1,11 @@
+2018-02-09  Markus Metzger  <markus.t.metzger@intel.com>
+
+       * server.c (handle_btrace_enable_bts, handle_btrace_enable_pt)
+       (handle_btrace_disable): Change return type to void.  Use exceptions
+       to report errors.
+       (handle_btrace_general_set): Catch exception and copy message to
+       return message.
+
 2018-02-08  Tom Tromey  <tom@tromey.com>
 
        * linux-low.c (install_software_single_step_breakpoints): Use
index 9d12ce6f7069ad23cc95ed91a10a37536410deb0..5ce62810bf6d80a7bee43ab055013c859a583b27 100644 (file)
@@ -380,50 +380,41 @@ write_qxfer_response (char *buf, const gdb_byte *data, int len, int is_more)
 
 /* Handle btrace enabling in BTS format.  */
 
-static const char *
+static void
 handle_btrace_enable_bts (struct thread_info *thread)
 {
   if (thread->btrace != NULL)
-    return "E.Btrace already enabled.";
+    error (_("Btrace already enabled."));
 
   current_btrace_conf.format = BTRACE_FORMAT_BTS;
   thread->btrace = target_enable_btrace (thread->id, &current_btrace_conf);
-  if (thread->btrace == NULL)
-    return "E.Could not enable btrace.";
-
-  return NULL;
 }
 
 /* Handle btrace enabling in Intel Processor Trace format.  */
 
-static const char *
+static void
 handle_btrace_enable_pt (struct thread_info *thread)
 {
   if (thread->btrace != NULL)
-    return "E.Btrace already enabled.";
+    error (_("Btrace already enabled."));
 
   current_btrace_conf.format = BTRACE_FORMAT_PT;
   thread->btrace = target_enable_btrace (thread->id, &current_btrace_conf);
-  if (thread->btrace == NULL)
-    return "E.Could not enable btrace.";
-
-  return NULL;
 }
 
 /* Handle btrace disabling.  */
 
-static const char *
+static void
 handle_btrace_disable (struct thread_info *thread)
 {
 
   if (thread->btrace == NULL)
-    return "E.Branch tracing not enabled.";
+    error (_("Branch tracing not enabled."));
 
   if (target_disable_btrace (thread->btrace) != 0)
-    return "E.Could not disable branch tracing.";
+    error (_("Could not disable branch tracing."));
 
   thread->btrace = NULL;
-  return NULL;
 }
 
 /* Handle the "Qbtrace" packet.  */
@@ -432,7 +423,6 @@ static int
 handle_btrace_general_set (char *own_buf)
 {
   struct thread_info *thread;
-  const char *err;
   char *op;
 
   if (!startswith (own_buf, "Qbtrace:"))
@@ -454,21 +444,24 @@ handle_btrace_general_set (char *own_buf)
       return -1;
     }
 
-  err = NULL;
-
-  if (strcmp (op, "bts") == 0)
-    err = handle_btrace_enable_bts (thread);
-  else if (strcmp (op, "pt") == 0)
-    err = handle_btrace_enable_pt (thread);
-  else if (strcmp (op, "off") == 0)
-    err = handle_btrace_disable (thread);
-  else
-    err = "E.Bad Qbtrace operation. Use bts, pt, or off.";
+  TRY
+    {
+      if (strcmp (op, "bts") == 0)
+       handle_btrace_enable_bts (thread);
+      else if (strcmp (op, "pt") == 0)
+       handle_btrace_enable_pt (thread);
+      else if (strcmp (op, "off") == 0)
+       handle_btrace_disable (thread);
+      else
+       error (_("Bad Qbtrace operation.  Use bts, pt, or off."));
 
-  if (err != 0)
-    strcpy (own_buf, err);
-  else
-    write_ok (own_buf);
+      write_ok (own_buf);
+    }
+  CATCH (exception, RETURN_MASK_ERROR)
+    {
+      sprintf (own_buf, "E.%s", exception.message);
+    }
+  END_CATCH
 
   return 1;
 }
index 354094f6cfd9b986e359b8cf318727ca2461d58a..2b37e419ce769e429a3fdae36403fc9f2d66bdf9 100644 (file)
@@ -909,6 +909,9 @@ linux_enable_btrace (ptid_t ptid, const struct btrace_config *conf)
       break;
     }
 
+  if (tinfo == NULL)
+    error (_("Unknown error."));
+
   return tinfo;
 }
 
index 46bb6a4dca70df54ab1f5e63850af4e0d5a4376f..75f68de9c2da88b1ac92c04b2a0672cae8c3d038 100644 (file)
@@ -216,14 +216,17 @@ static struct btrace_target_info *
 x86_linux_enable_btrace (struct target_ops *self, ptid_t ptid,
                         const struct btrace_config *conf)
 {
-  struct btrace_target_info *tinfo;
-
-  errno = 0;
-  tinfo = linux_enable_btrace (ptid, conf);
-
-  if (tinfo == NULL)
-    error (_("Could not enable branch tracing for %s: %s."),
-          target_pid_to_str (ptid), safe_strerror (errno));
+  struct btrace_target_info *tinfo = nullptr;
+  TRY
+    {
+      tinfo = linux_enable_btrace (ptid, conf);
+    }
+  CATCH (exception, RETURN_MASK_ERROR)
+    {
+      error (_("Could not enable branch tracing for %s: %s"),
+            target_pid_to_str (ptid), exception.message);
+    }
+  END_CATCH
 
   return tinfo;
 }
This page took 0.03948 seconds and 4 git commands to generate.