public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
From: Markus Metzger <markus.t.metzger@intel.com>
To: gdb-patches@sourceware.org
Subject: [PATCH v2 4/7] btrace, gdbserver: use exceptions to convey btrace enable/disable errors
Date: Fri, 26 Jan 2018 14:14:00 -0000	[thread overview]
Message-ID: <1516976072-19282-5-git-send-email-markus.t.metzger@intel.com> (raw)
In-Reply-To: <1516976072-19282-1-git-send-email-markus.t.metzger@intel.com>

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.

2018-01-26  Markus Metzger  <markus.t.metzger@intel.com>

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/gdbserver/server.c | 55 ++++++++++++++++++++++----------------------------
 gdb/nat/linux-btrace.c |  3 +++
 gdb/x86-linux-nat.c    | 19 +++++++++--------
 3 files changed, 38 insertions(+), 39 deletions(-)

diff --git a/gdb/gdbserver/server.c b/gdb/gdbserver/server.c
index 9d12ce6..5ce6281 100644
--- a/gdb/gdbserver/server.c
+++ b/gdb/gdbserver/server.c
@@ -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;
 }
diff --git a/gdb/nat/linux-btrace.c b/gdb/nat/linux-btrace.c
index 354094f..2b37e41 100644
--- a/gdb/nat/linux-btrace.c
+++ b/gdb/nat/linux-btrace.c
@@ -909,6 +909,9 @@ linux_enable_btrace (ptid_t ptid, const struct btrace_config *conf)
       break;
     }
 
+  if (tinfo == NULL)
+    error (_("Unknown error."));
+
   return tinfo;
 }
 
diff --git a/gdb/x86-linux-nat.c b/gdb/x86-linux-nat.c
index 46bb6a4..75f68de 100644
--- a/gdb/x86-linux-nat.c
+++ b/gdb/x86-linux-nat.c
@@ -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;
 }
-- 
1.8.3.1

  parent reply	other threads:[~2018-01-26 14:14 UTC|newest]

Thread overview: 47+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-01-26 14:14 [PATCH v2 0/7] improve btrace enable error reporting Markus Metzger
2018-01-26 14:14 ` [PATCH v2 2/7] common: add scoped_mmap Markus Metzger
2018-01-26 14:14 ` [PATCH v2 6/7] btrace: improve enable error messages Markus Metzger
2018-01-26 14:14 ` [PATCH v2 1/7] common: add scoped_fd Markus Metzger
2018-02-13 16:48   ` Yao Qi
2018-02-13 17:28     ` Metzger, Markus T
2018-02-14 15:22       ` Yao Qi
2018-02-14 17:26         ` Metzger, Markus T
2018-02-19 15:28           ` Metzger, Markus T
2018-02-19 15:46             ` Eli Zaretskii
2018-02-20 10:12             ` Yao Qi
2018-02-20 10:46               ` Metzger, Markus T
2018-02-20 11:09                 ` Yao Qi
2018-02-20 13:16                   ` Metzger, Markus T
2018-01-26 14:14 ` [PATCH v2 5/7] btrace, gdbserver: remove the to_supports_btrace target method Markus Metzger
2018-02-24 16:51   ` Maciej W. Rozycki
2018-02-26 13:08     ` Metzger, Markus T
2018-02-26 19:30       ` Andreas Arnez
2018-02-26 21:58         ` Maciej W. Rozycki
2018-02-27 10:57           ` Metzger, Markus T
2018-02-27 15:32             ` Maciej W. Rozycki
2018-02-27 18:14               ` Metzger, Markus T
2018-02-28 10:29                 ` Maciej W. Rozycki
2018-02-28 11:09                   ` Maciej W. Rozycki
2018-02-28 11:24                     ` Metzger, Markus T
2018-02-28 12:53                       ` Metzger, Markus T
2018-02-28 15:55                         ` Maciej W. Rozycki
2018-02-28 16:08                         ` Eli Zaretskii
2018-02-28 12:00     ` Yao Qi
2018-02-28 16:27       ` Sergio Durigan Junior
2018-03-01 11:33         ` Metzger, Markus T
2018-03-01 19:27           ` Sergio Durigan Junior
2018-03-05 12:14             ` Yao Qi
2018-03-05 16:13               ` Sergio Durigan Junior
2018-03-06  9:02                 ` Yao Qi
2018-03-06 16:32                   ` Sergio Durigan Junior
2018-03-01 19:34           ` Maciej W. Rozycki
2018-01-26 14:14 ` [PATCH v2 7/7] btrace: check perf_event_paranoid Markus Metzger
2018-01-26 14:14 ` [PATCH v2 3/7] btrace: prepare for throwing exceptions when enabling btrace Markus Metzger
2018-01-26 14:14 ` Markus Metzger [this message]
2018-02-06 16:28 ` [PATCH v2 0/7] improve btrace enable error reporting Pedro Alves
2018-02-07 10:41   ` Metzger, Markus T
2018-02-07 12:11     ` Pedro Alves
2018-02-08 10:40       ` Metzger, Markus T
2018-02-08 13:01         ` Pedro Alves
2018-02-08 13:49           ` Metzger, Markus T
2018-02-08 14:24             ` Pedro Alves

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1516976072-19282-5-git-send-email-markus.t.metzger@intel.com \
    --to=markus.t.metzger@intel.com \
    --cc=gdb-patches@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).