[RFA,2/2] C++-ify break-catch-throw

Message ID 20170604225353.18008-3-tom@tromey.com
State New, archived
Headers

Commit Message

Tom Tromey June 4, 2017, 10:53 p.m. UTC
  This changes exception_catchpoint to be more of a C++ class, using
std::string and std::vector, and updating the users.

gdb/ChangeLog
2017-06-04  Tom Tromey  <tom@tromey.com>

	* break-catch-throw.c (struct exception_catchpoint)
	<exception_rx>: Now a std::string.
	<pattern>: Now a unique_ptr.
	(~exception_catchpoint, check_status_exception_catchpoint)
	(print_one_detail_exception_catchpoint): Update.
	(handle_gnu_v3_exceptions): Change type of except_rx.
	(extract_exception_regexp): Return a std::string.
	(catch_exception_command_1): Update.
---
 gdb/ChangeLog           | 11 +++++++++++
 gdb/break-catch-throw.c | 49 +++++++++++++++++++++----------------------------
 2 files changed, 32 insertions(+), 28 deletions(-)
  

Comments

Simon Marchi June 5, 2017, 8:54 a.m. UTC | #1
> --- a/gdb/break-catch-throw.c
> +++ b/gdb/break-catch-throw.c
> @@ -82,15 +82,15 @@ struct exception_catchpoint : public breakpoint
> 
>    enum exception_event_kind kind;
> 
> -  /* If non-NULL, an xmalloc'd string holding the source form of the
> -     regular expression to match against.  */
> +  /* If not empty, a string holding the source form of the regular
> +     expression to match against.  */
> 
> -  char *exception_rx;
> +  std::string exception_rx;
> 
> -  /* If non-NULL, an xmalloc'd, compiled regular expression which is
> +  /* If non-NULL, a compiled regular expression which is
>       used to determine which exceptions to stop on.  */
> 
> -  regex_t *pattern;
> +  std::unique_ptr<regex_t> pattern;

 From what I understand, we were freeing the regex with regfree, but we 
weren't freeing the regex_t object, which is allocated separately, is 
that right?  Or what is freed in handle_gnu_v3_exceptions by the 
cleanup, and then referenced later?  Either way your code LGTM.

Simon
  
Pedro Alves June 5, 2017, 10:21 a.m. UTC | #2
On 06/04/2017 11:53 PM, Tom Tromey wrote:
> @@ -452,17 +450,14 @@ catch_exception_command_1 (enum exception_event_kind ex_event,
>  			   char *arg_entry,
>  			   int tempflag, int from_tty)
>  {
> -  char *except_rx;
>    const char *cond_string = NULL;
> -  struct cleanup *cleanup;
>    const char *arg = arg_entry;
>  
>    if (!arg)
>      arg = "";
>    arg = skip_spaces_const (arg);
>  
> -  except_rx = extract_exception_regexp (&arg);
> -  cleanup = make_cleanup (xfree, except_rx);
> +  std::string except_rx = extract_exception_regexp (&arg);
>  
>    cond_string = ep_parse_optional_if_clause (&arg);
>  
> @@ -474,10 +469,8 @@ catch_exception_command_1 (enum exception_event_kind ex_event,
>        && ex_event != EX_EVENT_RETHROW)
>      error (_("Unsupported or unknown exception event; cannot catch it"));
>  
> -  handle_gnu_v3_exceptions (tempflag, except_rx, cond_string,
> +  handle_gnu_v3_exceptions (tempflag, std::move (except_rx), cond_string,
>  			    ex_event, from_tty);
> -
> -  discard_cleanups (cleanup);
>  }

Something looks suspicious to me -- compile_rx_or_error returns with
an installed cleanup that calls regfree, and handle_gnu_v3_exceptions
leaves it installed too.

Thanks,
Pedro Alves
  
Pedro Alves June 5, 2017, 10:33 a.m. UTC | #3
On 06/05/2017 11:21 AM, Pedro Alves wrote:
> On 06/04/2017 11:53 PM, Tom Tromey wrote:
>> @@ -452,17 +450,14 @@ catch_exception_command_1 (enum exception_event_kind ex_event,
>>  			   char *arg_entry,
>>  			   int tempflag, int from_tty)
>>  {
>> -  char *except_rx;
>>    const char *cond_string = NULL;
>> -  struct cleanup *cleanup;
>>    const char *arg = arg_entry;
>>  
>>    if (!arg)
>>      arg = "";
>>    arg = skip_spaces_const (arg);
>>  
>> -  except_rx = extract_exception_regexp (&arg);
>> -  cleanup = make_cleanup (xfree, except_rx);
>> +  std::string except_rx = extract_exception_regexp (&arg);
>>  
>>    cond_string = ep_parse_optional_if_clause (&arg);
>>  
>> @@ -474,10 +469,8 @@ catch_exception_command_1 (enum exception_event_kind ex_event,
>>        && ex_event != EX_EVENT_RETHROW)
>>      error (_("Unsupported or unknown exception event; cannot catch it"));
>>  
>> -  handle_gnu_v3_exceptions (tempflag, except_rx, cond_string,
>> +  handle_gnu_v3_exceptions (tempflag, std::move (except_rx), cond_string,
>>  			    ex_event, from_tty);
>> -
>> -  discard_cleanups (cleanup);
>>  }
> 
> Something looks suspicious to me -- compile_rx_or_error returns with
> an installed cleanup that calls regfree, and handle_gnu_v3_exceptions
> leaves it installed too.

Maybe the cleanest would be to add a wrapper around regex_t,
and replace compile_rx_or_error with a ctor that throws,
like:

class gdb_regex
{
public:
  // replaces old compile_rx_or_error
  gdb_regex (const char *rx, const char *message)
  {
    gdb_assert (rx != NULL);

    int code = regcomp (&m_pattern, rx, REG_NOSUB);
    if (code != 0)
      {
        gdb::unique_xmalloc_ptr<char> err
          = get_regcomp_error (code, &m_pattern);

        error (("%s: %s"), message, err.get ());
      }
  }
  ~gdb_regex ()
  {
    regfree (&m_pattern);
  }

  // add convenience methods as needed.

private:
  regex_t m_pattern;
};

and use it throughout.

Thanks,
Pedro Alves
  
Pedro Alves June 5, 2017, 10:36 a.m. UTC | #4
On 06/05/2017 11:33 AM, Pedro Alves wrote:
> class gdb_regex
> {
> public:
>   // replaces old compile_rx_or_error
>   gdb_regex (const char *rx, const char *message)
>   {

Maybe call it "compiled_regex" instead, since you wouldn't
be able to end up with a not-compiled-yet regex with this.

Thanks,
Pedro Alves
  
Tom Tromey June 5, 2017, 7:10 p.m. UTC | #5
>>>>> "Simon" == Simon Marchi <simon.marchi@polymtl.ca> writes:

Simon> From what I understand, we were freeing the regex with regfree, but we
Simon> weren't freeing the regex_t object, which is allocated separately, is
Simon> that right?

Yes, I think that's a leak in the current code.

Tom
  

Patch

diff --git a/gdb/ChangeLog b/gdb/ChangeLog
index b89b47d..7be1267 100644
--- a/gdb/ChangeLog
+++ b/gdb/ChangeLog
@@ -1,5 +1,16 @@ 
 2017-06-04  Tom Tromey  <tom@tromey.com>
 
+	* break-catch-throw.c (struct exception_catchpoint)
+	<exception_rx>: Now a std::string.
+	<pattern>: Now a unique_ptr.
+	(~exception_catchpoint, check_status_exception_catchpoint)
+	(print_one_detail_exception_catchpoint): Update.
+	(handle_gnu_v3_exceptions): Change type of except_rx.
+	(extract_exception_regexp): Return a std::string.
+	(catch_exception_command_1): Update.
+
+2017-06-04  Tom Tromey  <tom@tromey.com>
+
 	* break-catch-sig.c (gdb_signal_type): Remove typedef.
 	(struct signal_catchpoint) <signals_to_be_caught>: Now a
 	std::vector.
diff --git a/gdb/break-catch-throw.c b/gdb/break-catch-throw.c
index 7731c5e..5e88ad5 100644
--- a/gdb/break-catch-throw.c
+++ b/gdb/break-catch-throw.c
@@ -82,15 +82,15 @@  struct exception_catchpoint : public breakpoint
 
   enum exception_event_kind kind;
 
-  /* If non-NULL, an xmalloc'd string holding the source form of the
-     regular expression to match against.  */
+  /* If not empty, a string holding the source form of the regular
+     expression to match against.  */
 
-  char *exception_rx;
+  std::string exception_rx;
 
-  /* If non-NULL, an xmalloc'd, compiled regular expression which is
+  /* If non-NULL, a compiled regular expression which is
      used to determine which exceptions to stop on.  */
 
-  regex_t *pattern;
+  std::unique_ptr<regex_t> pattern;
 };
 
 
@@ -144,9 +144,8 @@  classify_exception_breakpoint (struct breakpoint *b)
 
 exception_catchpoint::~exception_catchpoint ()
 {
-  xfree (this->exception_rx);
   if (this->pattern != NULL)
-    regfree (this->pattern);
+    regfree (this->pattern.get ());
 }
 
 /* Implement the 'check_status' method.  */
@@ -185,7 +184,7 @@  check_status_exception_catchpoint (struct bpstats *bs)
 
   if (!type_name.empty ())
     {
-      if (regexec (self->pattern, type_name.c_str (), 0, NULL, 0) != 0)
+      if (regexec (self->pattern.get (), type_name.c_str (), 0, NULL, 0) != 0)
 	bs->stop = 0;
     }
 }
@@ -321,10 +320,10 @@  print_one_detail_exception_catchpoint (const struct breakpoint *b,
   const struct exception_catchpoint *cp
     = (const struct exception_catchpoint *) b;
 
-  if (cp->exception_rx != NULL)
+  if (!cp->exception_rx.empty ())
     {
       uiout->text (_("\tmatching: "));
-      uiout->field_string ("regexp", cp->exception_rx);
+      uiout->field_string ("regexp", cp->exception_rx.c_str ());
       uiout->text ("\n");
     }
 }
@@ -373,18 +372,17 @@  print_recreate_exception_catchpoint (struct breakpoint *b,
 }
 
 static void
-handle_gnu_v3_exceptions (int tempflag, char *except_rx,
+handle_gnu_v3_exceptions (int tempflag, std::string &&except_rx,
 			  const char *cond_string,
 			  enum exception_event_kind ex_event, int from_tty)
 {
-  regex_t *pattern = NULL;
+  std::unique_ptr<regex_t> pattern;
 
-  if (except_rx != NULL)
+  if (!except_rx.empty ())
     {
-      pattern = XNEW (regex_t);
-      make_cleanup (xfree, pattern);
+      pattern.reset (new regex_t);
 
-      compile_rx_or_error (pattern, except_rx,
+      compile_rx_or_error (pattern.get (), except_rx.c_str (),
 			   _("invalid type-matching regexp"));
     }
 
@@ -396,8 +394,8 @@  handle_gnu_v3_exceptions (int tempflag, char *except_rx,
      the right thing.  */
   cp->type = bp_breakpoint;
   cp->kind = ex_event;
-  cp->exception_rx = except_rx;
-  cp->pattern = pattern;
+  cp->exception_rx = std::move (except_rx);
+  cp->pattern = std::move (pattern);
 
   re_set_exception_catchpoint (cp.get ());
 
@@ -415,7 +413,7 @@  handle_gnu_v3_exceptions (int tempflag, char *except_rx,
    STRING is updated to point to the "if" token, if it exists, or to
    the end of the string.  */
 
-static char *
+static std::string
 extract_exception_regexp (const char **string)
 {
   const char *start;
@@ -440,8 +438,8 @@  extract_exception_regexp (const char **string)
 
   *string = last;
   if (last_space > start)
-    return savestring (start, last_space - start);
-  return NULL;
+    return std::string (start, last_space - start);
+  return std::string ();
 }
 
 /* Deal with "catch catch", "catch throw", and "catch rethrow"
@@ -452,17 +450,14 @@  catch_exception_command_1 (enum exception_event_kind ex_event,
 			   char *arg_entry,
 			   int tempflag, int from_tty)
 {
-  char *except_rx;
   const char *cond_string = NULL;
-  struct cleanup *cleanup;
   const char *arg = arg_entry;
 
   if (!arg)
     arg = "";
   arg = skip_spaces_const (arg);
 
-  except_rx = extract_exception_regexp (&arg);
-  cleanup = make_cleanup (xfree, except_rx);
+  std::string except_rx = extract_exception_regexp (&arg);
 
   cond_string = ep_parse_optional_if_clause (&arg);
 
@@ -474,10 +469,8 @@  catch_exception_command_1 (enum exception_event_kind ex_event,
       && ex_event != EX_EVENT_RETHROW)
     error (_("Unsupported or unknown exception event; cannot catch it"));
 
-  handle_gnu_v3_exceptions (tempflag, except_rx, cond_string,
+  handle_gnu_v3_exceptions (tempflag, std::move (except_rx), cond_string,
 			    ex_event, from_tty);
-
-  discard_cleanups (cleanup);
 }
 
 /* Implementation of "catch catch" command.  */