Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
* [RFA 5/8] Remove cleanups from find_frame_funname
  2017-09-10 21:50 [RFA 0/8] various cleanup removals Tom Tromey
                   ` (6 preceding siblings ...)
  2017-09-10 21:50 ` [RFA 2/8] Replace interp_set_temp with scoped_restore_interp Tom Tromey
@ 2017-09-10 21:50 ` Tom Tromey
  2017-09-11 20:25   ` Simon Marchi
  2017-09-11 20:35 ` [RFA 0/8] various cleanup removals Simon Marchi
  8 siblings, 1 reply; 13+ messages in thread
From: Tom Tromey @ 2017-09-10 21:50 UTC (permalink / raw)
  To: gdb-patches; +Cc: Tom Tromey

This changes find_frame_funname to return a unique_xmalloc_ptr and
then fixes up the callers.  This removes several cleanups.

ChangeLog
2017-09-10  Tom Tromey  <tom@tromey.com>

	* ada-lang.c (is_known_support_routine): Update.
	(ada_unhandled_exception_name_addr_from_raise): Update.
	* guile/scm-frame.c (gdbscm_frame_name): Update.
	* python/py-frame.c (frapy_name): Update.
	(frapy_function): Update.
	* stack.h (find_frame_funname): Update.
	* stack.c (find_frame_funname): Return unique_xmalloc_ptr.
	(print_frame): Update.
---
 gdb/ChangeLog         | 11 +++++++++++
 gdb/ada-lang.c        | 23 +++++++----------------
 gdb/guile/scm-frame.c |  8 +++-----
 gdb/python/py-frame.c | 14 ++++++--------
 gdb/stack.c           | 41 +++++++++++++++++++----------------------
 gdb/stack.h           |  5 +++--
 6 files changed, 49 insertions(+), 53 deletions(-)

diff --git a/gdb/ChangeLog b/gdb/ChangeLog
index af874df..fe99b47 100644
--- a/gdb/ChangeLog
+++ b/gdb/ChangeLog
@@ -1,5 +1,16 @@
 2017-09-10  Tom Tromey  <tom@tromey.com>
 
+	* ada-lang.c (is_known_support_routine): Update.
+	(ada_unhandled_exception_name_addr_from_raise): Update.
+	* guile/scm-frame.c (gdbscm_frame_name): Update.
+	* python/py-frame.c (frapy_name): Update.
+	(frapy_function): Update.
+	* stack.h (find_frame_funname): Update.
+	* stack.c (find_frame_funname): Return unique_xmalloc_ptr.
+	(print_frame): Update.
+
+2017-09-10  Tom Tromey  <tom@tromey.com>
+
 	* findcmd.c (put_bits): Take a gdb::byte_vector.
 	(parse_find_args): Return gdb::byte_vector.  "args" now const.
 	Remove "pattern_bufp" and "pattern_lenp" parameters.  Remove
diff --git a/gdb/ada-lang.c b/gdb/ada-lang.c
index fdc2a90..3a46831 100644
--- a/gdb/ada-lang.c
+++ b/gdb/ada-lang.c
@@ -11979,7 +11979,6 @@ ada_exception_support_info_sniffer (void)
 static int
 is_known_support_routine (struct frame_info *frame)
 {
-  char *func_name;
   enum language func_lang;
   int i;
   const char *fullname;
@@ -12018,21 +12017,18 @@ is_known_support_routine (struct frame_info *frame)
 
   /* Check whether the function is a GNAT-generated entity.  */
 
-  find_frame_funname (frame, &func_name, &func_lang, NULL);
+  gdb::unique_xmalloc_ptr<char> func_name
+    = find_frame_funname (frame, &func_lang, NULL);
   if (func_name == NULL)
     return 1;
 
   for (i = 0; known_auxiliary_function_name_patterns[i] != NULL; i += 1)
     {
       re_comp (known_auxiliary_function_name_patterns[i]);
-      if (re_exec (func_name))
-	{
-	  xfree (func_name);
-	  return 1;
-	}
+      if (re_exec (func_name.get ()))
+	return 1;
     }
 
-  xfree (func_name);
   return 0;
 }
 
@@ -12076,7 +12072,6 @@ ada_unhandled_exception_name_addr_from_raise (void)
   int frame_level;
   struct frame_info *fi;
   struct ada_inferior_data *data = get_ada_inferior_data (current_inferior ());
-  struct cleanup *old_chain;
 
   /* To determine the name of this exception, we need to select
      the frame corresponding to RAISE_SYM_NAME.  This frame is
@@ -12087,24 +12082,20 @@ ada_unhandled_exception_name_addr_from_raise (void)
     if (fi != NULL)
       fi = get_prev_frame (fi); 
 
-  old_chain = make_cleanup (null_cleanup, NULL);
   while (fi != NULL)
     {
-      char *func_name;
       enum language func_lang;
 
-      find_frame_funname (fi, &func_name, &func_lang, NULL);
+      gdb::unique_xmalloc_ptr<char> func_name
+	= find_frame_funname (fi, &func_lang, NULL);
       if (func_name != NULL)
 	{
-	  make_cleanup (xfree, func_name);
-
-          if (strcmp (func_name,
+          if (strcmp (func_name.get (),
 		      data->exception_info->catch_exception_sym) == 0)
 	    break; /* We found the frame we were looking for...  */
 	  fi = get_prev_frame (fi);
 	}
     }
-  do_cleanups (old_chain);
 
   if (fi == NULL)
     return 0;
diff --git a/gdb/guile/scm-frame.c b/gdb/guile/scm-frame.c
index b2af743..f504a88 100644
--- a/gdb/guile/scm-frame.c
+++ b/gdb/guile/scm-frame.c
@@ -418,7 +418,7 @@ static SCM
 gdbscm_frame_name (SCM self)
 {
   frame_smob *f_smob;
-  char *name = NULL;
+  gdb::unique_xmalloc_ptr<char> name;
   enum language lang = language_minimal;
   struct frame_info *frame = NULL;
   SCM result;
@@ -429,11 +429,10 @@ gdbscm_frame_name (SCM self)
     {
       frame = frscm_frame_smob_to_frame (f_smob);
       if (frame != NULL)
-	find_frame_funname (frame, &name, &lang, NULL);
+	name = find_frame_funname (frame, &lang, NULL);
     }
   CATCH (except, RETURN_MASK_ALL)
     {
-      xfree (name);
       GDBSCM_HANDLE_GDB_EXCEPTION (except);
     }
   END_CATCH
@@ -446,8 +445,7 @@ gdbscm_frame_name (SCM self)
 
   if (name != NULL)
     {
-      result = gdbscm_scm_from_c_string (name);
-      xfree (name);
+      result = gdbscm_scm_from_c_string (name.get ());
     }
   else
     result = SCM_BOOL_F;
diff --git a/gdb/python/py-frame.c b/gdb/python/py-frame.c
index c5ae391..a927b3c 100644
--- a/gdb/python/py-frame.c
+++ b/gdb/python/py-frame.c
@@ -119,7 +119,7 @@ static PyObject *
 frapy_name (PyObject *self, PyObject *args)
 {
   struct frame_info *frame;
-  char *name = NULL;
+  gdb::unique_xmalloc_ptr<char> name;
   enum language lang;
   PyObject *result;
 
@@ -127,19 +127,18 @@ frapy_name (PyObject *self, PyObject *args)
     {
       FRAPY_REQUIRE_VALID (self, frame);
 
-      find_frame_funname (frame, &name, &lang, NULL);
+      name = find_frame_funname (frame, &lang, NULL);
     }
   CATCH (except, RETURN_MASK_ALL)
     {
-      xfree (name);
       GDB_PY_HANDLE_EXCEPTION (except);
     }
   END_CATCH
 
   if (name)
     {
-      result = PyUnicode_Decode (name, strlen (name), host_charset (), NULL);
-      xfree (name);
+      result = PyUnicode_Decode (name.get (), strlen (name.get ()),
+				 host_charset (), NULL);
     }
   else
     {
@@ -334,13 +333,12 @@ frapy_function (PyObject *self, PyObject *args)
 
   TRY
     {
-      char *funname;
       enum language funlang;
 
       FRAPY_REQUIRE_VALID (self, frame);
 
-      find_frame_funname (frame, &funname, &funlang, &sym);
-      xfree (funname);
+      gdb::unique_xmalloc_ptr<char> funname
+	= find_frame_funname (frame, &funlang, &sym);
     }
   CATCH (except, RETURN_MASK_ALL)
     {
diff --git a/gdb/stack.c b/gdb/stack.c
index 2433009..2dae380 100644
--- a/gdb/stack.c
+++ b/gdb/stack.c
@@ -1032,16 +1032,16 @@ get_last_displayed_sal ()
 }
 
 
-/* Attempt to obtain the FUNNAME, FUNLANG and optionally FUNCP of the function
-   corresponding to FRAME.  FUNNAME needs to be freed by the caller.  */
+/* Attempt to obtain the name, FUNLANG and optionally FUNCP of the function
+   corresponding to FRAME.  */
 
-void
-find_frame_funname (struct frame_info *frame, char **funname,
-		    enum language *funlang, struct symbol **funcp)
+gdb::unique_xmalloc_ptr<char>
+find_frame_funname (struct frame_info *frame, enum language *funlang,
+		    struct symbol **funcp)
 {
   struct symbol *func;
+  gdb::unique_xmalloc_ptr<char> funname;
 
-  *funname = NULL;
   *funlang = language_unknown;
   if (funcp)
     *funcp = NULL;
@@ -1084,7 +1084,7 @@ find_frame_funname (struct frame_info *frame, char **funname,
 	  /* We also don't know anything about the function besides
 	     its address and name.  */
 	  func = 0;
-	  *funname = xstrdup (MSYMBOL_PRINT_NAME (msymbol.minsym));
+	  funname.reset (xstrdup (MSYMBOL_PRINT_NAME (msymbol.minsym)));
 	  *funlang = MSYMBOL_LANGUAGE (msymbol.minsym);
 	}
       else
@@ -1104,14 +1104,13 @@ find_frame_funname (struct frame_info *frame, char **funname,
 	      char *func_only = cp_remove_params (print_name);
 
 	      if (func_only)
-		*funname = func_only;
+		funname.reset (func_only);
 	    }
 
-	  /* If we didn't hit the C++ case above, set *funname here.
-	     This approach is taken to avoid having to install a
-	     cleanup in case cp_remove_params can throw.  */
-	  if (*funname == NULL)
-	    *funname = xstrdup (print_name);
+	  /* If we didn't hit the C++ case above, set *funname
+	     here.  */
+	  if (funname == NULL)
+	    funname.reset (xstrdup (print_name));
 	}
     }
   else
@@ -1120,15 +1119,17 @@ find_frame_funname (struct frame_info *frame, char **funname,
       CORE_ADDR pc;
 
       if (!get_frame_address_in_block_if_available (frame, &pc))
-	return;
+	return funname;
 
       msymbol = lookup_minimal_symbol_by_pc (pc);
       if (msymbol.minsym != NULL)
 	{
-	  *funname = xstrdup (MSYMBOL_PRINT_NAME (msymbol.minsym));
+	  funname.reset (xstrdup (MSYMBOL_PRINT_NAME (msymbol.minsym)));
 	  *funlang = MSYMBOL_LANGUAGE (msymbol.minsym);
 	}
     }
+
+  return funname;
 }
 
 static void
@@ -1138,9 +1139,7 @@ print_frame (struct frame_info *frame, int print_level,
 {
   struct gdbarch *gdbarch = get_frame_arch (frame);
   struct ui_out *uiout = current_uiout;
-  char *funname = NULL;
   enum language funlang = language_unknown;
-  struct cleanup *old_chain;
   struct value_print_options opts;
   struct symbol *func;
   CORE_ADDR pc = 0;
@@ -1148,9 +1147,8 @@ print_frame (struct frame_info *frame, int print_level,
 
   pc_p = get_frame_pc_if_available (frame, &pc);
 
-
-  find_frame_funname (frame, &funname, &funlang, &func);
-  old_chain = make_cleanup (xfree, funname);
+  gdb::unique_xmalloc_ptr<char> funname
+    = find_frame_funname (frame, &funlang, &func);
 
   annotate_frame_begin (print_level ? frame_relative_level (frame) : 0,
 			gdbarch, pc);
@@ -1181,7 +1179,7 @@ print_frame (struct frame_info *frame, int print_level,
     annotate_frame_function_name ();
 
     string_file stb;
-    fprintf_symbol_filtered (&stb, funname ? funname : "??",
+    fprintf_symbol_filtered (&stb, funname ? funname.get () : "??",
 			     funlang, DMGL_ANSI);
     uiout->field_stream ("func", stb);
     uiout->wrap_hint ("   ");
@@ -1257,7 +1255,6 @@ print_frame (struct frame_info *frame, int print_level,
   }
 
   uiout->text ("\n");
-  do_cleanups (old_chain);
 }
 \f
 
diff --git a/gdb/stack.h b/gdb/stack.h
index f41d21e..3379318 100644
--- a/gdb/stack.h
+++ b/gdb/stack.h
@@ -22,8 +22,9 @@
 
 void select_frame_command (char *level_exp, int from_tty);
 
-void find_frame_funname (struct frame_info *frame, char **funname,
-			 enum language *funlang, struct symbol **funcp);
+gdb::unique_xmalloc_ptr<char> find_frame_funname (struct frame_info *frame,
+						  enum language *funlang,
+						  struct symbol **funcp);
 
 typedef void (*iterate_over_block_arg_local_vars_cb) (const char *print_name,
 						      struct symbol *sym,
-- 
2.9.4


^ permalink raw reply	[flat|nested] 13+ messages in thread

* [RFA 0/8] various cleanup removals
@ 2017-09-10 21:50 Tom Tromey
  2017-09-10 21:50 ` [RFA 6/8] Use std::string in ctf_start Tom Tromey
                   ` (8 more replies)
  0 siblings, 9 replies; 13+ messages in thread
From: Tom Tromey @ 2017-09-10 21:50 UTC (permalink / raw)
  To: gdb-patches

Here's a series that removes a few more cleanups.  It's a mixed bag of
string-based cleanups and scoped_restore-like cleanups.

Regression tested on the buildbot.

Tom


^ permalink raw reply	[flat|nested] 13+ messages in thread

* [RFA 6/8] Use std::string in ctf_start
  2017-09-10 21:50 [RFA 0/8] various cleanup removals Tom Tromey
@ 2017-09-10 21:50 ` Tom Tromey
  2017-09-10 21:50 ` [RFA 1/8] Change setup_breakpoint_reporting to return a scoped_restore Tom Tromey
                   ` (7 subsequent siblings)
  8 siblings, 0 replies; 13+ messages in thread
From: Tom Tromey @ 2017-09-10 21:50 UTC (permalink / raw)
  To: gdb-patches; +Cc: Tom Tromey

This changes ctf_start to use std::string, allowing for some cleanup
removal.

ChangeLog
2017-09-10  Tom Tromey  <tom@tromey.com>

	* ctf.c (ctf_start): Use std::string.
---
 gdb/ChangeLog |  4 ++++
 gdb/ctf.c     | 18 ++++++------------
 2 files changed, 10 insertions(+), 12 deletions(-)

diff --git a/gdb/ChangeLog b/gdb/ChangeLog
index fe99b47..735de68 100644
--- a/gdb/ChangeLog
+++ b/gdb/ChangeLog
@@ -1,5 +1,9 @@
 2017-09-10  Tom Tromey  <tom@tromey.com>
 
+	* ctf.c (ctf_start): Use std::string.
+
+2017-09-10  Tom Tromey  <tom@tromey.com>
+
 	* ada-lang.c (is_known_support_routine): Update.
 	(ada_unhandled_exception_name_addr_from_raise): Update.
 	* guile/scm-frame.c (gdbscm_frame_name): Update.
diff --git a/gdb/ctf.c b/gdb/ctf.c
index 46a1bb5..d719163 100644
--- a/gdb/ctf.c
+++ b/gdb/ctf.c
@@ -311,8 +311,6 @@ ctf_target_save (struct trace_file_writer *self,
 static void
 ctf_start (struct trace_file_writer *self, const char *dirname)
 {
-  char *file_name;
-  struct cleanup *old_chain;
   struct ctf_trace_file_writer *writer
     = (struct ctf_trace_file_writer *) self;
   int i;
@@ -325,24 +323,20 @@ ctf_start (struct trace_file_writer *self, const char *dirname)
 
   memset (&writer->tcs, '\0', sizeof (writer->tcs));
 
-  file_name = xstrprintf ("%s/%s", dirname, CTF_METADATA_NAME);
-  old_chain = make_cleanup (xfree, file_name);
+  std::string file_name = string_printf ("%s/%s", dirname, CTF_METADATA_NAME);
 
-  writer->tcs.metadata_fd = fopen (file_name, "w");
+  writer->tcs.metadata_fd = fopen (file_name.c_str (), "w");
   if (writer->tcs.metadata_fd == NULL)
     error (_("Unable to open file '%s' for saving trace data (%s)"),
-	   file_name, safe_strerror (errno));
-  do_cleanups (old_chain);
+	   file_name.c_str (), safe_strerror (errno));
 
   ctf_save_metadata_header (&writer->tcs);
 
-  file_name = xstrprintf ("%s/%s", dirname, CTF_DATASTREAM_NAME);
-  old_chain = make_cleanup (xfree, file_name);
-  writer->tcs.datastream_fd = fopen (file_name, "w");
+  file_name = string_printf ("%s/%s", dirname, CTF_DATASTREAM_NAME);
+  writer->tcs.datastream_fd = fopen (file_name.c_str (), "w");
   if (writer->tcs.datastream_fd == NULL)
     error (_("Unable to open file '%s' for saving trace data (%s)"),
-	   file_name, safe_strerror (errno));
-  do_cleanups (old_chain);
+	   file_name.c_str (), safe_strerror (errno));
 }
 
 /* This is the implementation of trace_file_write_ops method
-- 
2.9.4


^ permalink raw reply	[flat|nested] 13+ messages in thread

* [RFA 3/8] Replace clear_hook_in_cleanup with scoped_restore_hook_in
  2017-09-10 21:50 [RFA 0/8] various cleanup removals Tom Tromey
  2017-09-10 21:50 ` [RFA 6/8] Use std::string in ctf_start Tom Tromey
  2017-09-10 21:50 ` [RFA 1/8] Change setup_breakpoint_reporting to return a scoped_restore Tom Tromey
@ 2017-09-10 21:50 ` Tom Tromey
  2017-09-10 21:50 ` [RFA 8/8] Remove make_show_memory_breakpoints_cleanup Tom Tromey
                   ` (5 subsequent siblings)
  8 siblings, 0 replies; 13+ messages in thread
From: Tom Tromey @ 2017-09-10 21:50 UTC (permalink / raw)
  To: gdb-patches; +Cc: Tom Tromey

This removes clear_hook_in_cleanup in favor of a scoped_restore-like
class.  scoped_restore itself can't be used because hook_in is a
bitfield.

ChangeLog
2017-09-10  Tom Tromey  <tom@tromey.com>

	* cli/cli-script.c (class scoped_restore_hook_in): New.
	(clear_hook_in_cleanup): Remove.
	(execute_cmd_pre_hook, execute_cmd_post_hook): Use
	scoped_restore_hook_in.
---
 gdb/ChangeLog        |  7 +++++++
 gdb/cli/cli-script.c | 31 +++++++++++++++++++++----------
 2 files changed, 28 insertions(+), 10 deletions(-)

diff --git a/gdb/ChangeLog b/gdb/ChangeLog
index de88894..62d3d3c 100644
--- a/gdb/ChangeLog
+++ b/gdb/ChangeLog
@@ -1,5 +1,12 @@
 2017-09-10  Tom Tromey  <tom@tromey.com>
 
+	* cli/cli-script.c (class scoped_restore_hook_in): New.
+	(clear_hook_in_cleanup): Remove.
+	(execute_cmd_pre_hook, execute_cmd_post_hook): Use
+	scoped_restore_hook_in.
+
+2017-09-10  Tom Tromey  <tom@tromey.com>
+
 	* cli/cli-script.c (restore_interp): Remove.
 	(read_command_lines): Use scoped_restore_interp.
 	* interps.c (scoped_restore_interp::set_temp): Rename from
diff --git a/gdb/cli/cli-script.c b/gdb/cli/cli-script.c
index 805064f..9b7ca12 100644
--- a/gdb/cli/cli-script.c
+++ b/gdb/cli/cli-script.c
@@ -340,23 +340,36 @@ print_command_lines (struct ui_out *uiout, struct command_line *cmd,
 
 /* Handle pre-post hooks.  */
 
-static void
-clear_hook_in_cleanup (void *data)
+class scoped_restore_hook_in
 {
-  struct cmd_list_element *c = (struct cmd_list_element *) data;
+public:
 
-  c->hook_in = 0; /* Allow hook to work again once it is complete.  */
-}
+  scoped_restore_hook_in (struct cmd_list_element *c)
+    : m_cmd (c)
+  {
+  }
+
+  ~scoped_restore_hook_in ()
+  {
+    m_cmd->hook_in = 0;
+  }
+
+  scoped_restore_hook_in (const scoped_restore_hook_in &) = delete;
+  scoped_restore_hook_in &operator= (const scoped_restore_hook_in &) = delete;
+
+private:
+
+  struct cmd_list_element *m_cmd;
+};
 
 void
 execute_cmd_pre_hook (struct cmd_list_element *c)
 {
   if ((c->hook_pre) && (!c->hook_in))
     {
-      struct cleanup *cleanups = make_cleanup (clear_hook_in_cleanup, c);
+      scoped_restore_hook_in restore_hook (c);
       c->hook_in = 1; /* Prevent recursive hooking.  */
       execute_user_command (c->hook_pre, (char *) 0);
-      do_cleanups (cleanups);
     }
 }
 
@@ -365,11 +378,9 @@ execute_cmd_post_hook (struct cmd_list_element *c)
 {
   if ((c->hook_post) && (!c->hook_in))
     {
-      struct cleanup *cleanups = make_cleanup (clear_hook_in_cleanup, c);
-
+      scoped_restore_hook_in restore_hook (c);
       c->hook_in = 1; /* Prevent recursive hooking.  */
       execute_user_command (c->hook_post, (char *) 0);
-      do_cleanups (cleanups);
     }
 }
 
-- 
2.9.4


^ permalink raw reply	[flat|nested] 13+ messages in thread

* [RFA 8/8] Remove make_show_memory_breakpoints_cleanup
  2017-09-10 21:50 [RFA 0/8] various cleanup removals Tom Tromey
                   ` (2 preceding siblings ...)
  2017-09-10 21:50 ` [RFA 3/8] Replace clear_hook_in_cleanup with scoped_restore_hook_in Tom Tromey
@ 2017-09-10 21:50 ` Tom Tromey
  2017-09-11 20:34   ` Simon Marchi
  2017-09-10 21:50 ` [RFA 7/8] Use std::string in d-namespace.c Tom Tromey
                   ` (4 subsequent siblings)
  8 siblings, 1 reply; 13+ messages in thread
From: Tom Tromey @ 2017-09-10 21:50 UTC (permalink / raw)
  To: gdb-patches; +Cc: Tom Tromey

This removes make_show_memory_breakpoints_cleanup, replacing it with
make_scoped_restore_show_memory_breakpoints and updating all callers.

ChangeLog
2017-09-10  Tom Tromey  <tom@tromey.com>

	* breakpoint.c (program_breakpoint_here_p): Update.
	* target.c (make_scoped_restore_show_memory_breakpoints): Rename
	from make_show_memory_breakpoints_cleanup.  Return a
	scoped_restore_tmpl<int>.
	(restore_show_memory_breakpoints): Remove.
	* ppc-linux-tdep.c (ppc_linux_memory_remove_breakpoint): Update.
	* mem-break.c (memory_validate_breakpoint): Update.
	* ia64-tdep.c (ia64_memory_insert_breakpoint): Update.
	(ia64_memory_remove_breakpoint): Update.
	(ia64_breakpoint_from_pc): Update.
	* target.h (make_scoped_restore_show_memory_breakpoints): Rename
	from make_show_memory_breakpoints_cleanup.
---
 gdb/ChangeLog        | 15 +++++++++++++++
 gdb/breakpoint.c     |  6 ++----
 gdb/ia64-tdep.c      | 39 ++++++++++++++-------------------------
 gdb/mem-break.c      | 10 +++-------
 gdb/ppc-linux-tdep.c |  5 ++---
 gdb/target.c         | 16 +++-------------
 gdb/target.h         |  8 +++++---
 7 files changed, 44 insertions(+), 55 deletions(-)

diff --git a/gdb/ChangeLog b/gdb/ChangeLog
index f757ab5..faec9a1 100644
--- a/gdb/ChangeLog
+++ b/gdb/ChangeLog
@@ -1,5 +1,20 @@
 2017-09-10  Tom Tromey  <tom@tromey.com>
 
+	* breakpoint.c (program_breakpoint_here_p): Update.
+	* target.c (make_scoped_restore_show_memory_breakpoints): Rename
+	from make_show_memory_breakpoints_cleanup.  Return a
+	scoped_restore_tmpl<int>.
+	(restore_show_memory_breakpoints): Remove.
+	* ppc-linux-tdep.c (ppc_linux_memory_remove_breakpoint): Update.
+	* mem-break.c (memory_validate_breakpoint): Update.
+	* ia64-tdep.c (ia64_memory_insert_breakpoint): Update.
+	(ia64_memory_remove_breakpoint): Update.
+	(ia64_breakpoint_from_pc): Update.
+	* target.h (make_scoped_restore_show_memory_breakpoints): Rename
+	from make_show_memory_breakpoints_cleanup.
+
+2017-09-10  Tom Tromey  <tom@tromey.com>
+
 	* d-namespace.c (d_lookup_symbol): Use std::string.
 	(find_symbol_in_baseclass): Likewise.
 
diff --git a/gdb/breakpoint.c b/gdb/breakpoint.c
index 123420c..cf6e6e4 100644
--- a/gdb/breakpoint.c
+++ b/gdb/breakpoint.c
@@ -8994,7 +8994,6 @@ program_breakpoint_here_p (struct gdbarch *gdbarch, CORE_ADDR address)
   CORE_ADDR addr;
   const gdb_byte *bpoint;
   gdb_byte *target_mem;
-  struct cleanup *cleanup;
   int retval = 0;
 
   addr = address;
@@ -9009,14 +9008,13 @@ program_breakpoint_here_p (struct gdbarch *gdbarch, CORE_ADDR address)
   /* Enable the automatic memory restoration from breakpoints while
      we read the memory.  Otherwise we could say about our temporary
      breakpoints they are permanent.  */
-  cleanup = make_show_memory_breakpoints_cleanup (0);
+  scoped_restore restore_memory
+    = make_scoped_restore_show_memory_breakpoints (0);
 
   if (target_read_memory (address, target_mem, len) == 0
       && memcmp (target_mem, bpoint, len) == 0)
     retval = 1;
 
-  do_cleanups (cleanup);
-
   return retval;
 }
 
diff --git a/gdb/ia64-tdep.c b/gdb/ia64-tdep.c
index 5fc32a8..222bb33 100644
--- a/gdb/ia64-tdep.c
+++ b/gdb/ia64-tdep.c
@@ -644,7 +644,6 @@ ia64_memory_insert_breakpoint (struct gdbarch *gdbarch,
   long long instr_breakpoint;
   int val;
   int templ;
-  struct cleanup *cleanup;
 
   if (slotnum > 2)
     error (_("Can't insert breakpoint for slot numbers greater than 2."));
@@ -656,13 +655,11 @@ ia64_memory_insert_breakpoint (struct gdbarch *gdbarch,
      Otherwise, we could possibly store into the shadow parts of the adjacent
      placed breakpoints.  It is due to our SHADOW_CONTENTS overlapping the real
      breakpoint instruction bits region.  */
-  cleanup = make_show_memory_breakpoints_cleanup (0);
+  scoped_restore restore_memory_0
+    = make_scoped_restore_show_memory_breakpoints (0);
   val = target_read_memory (addr, bundle, BUNDLE_LEN);
   if (val != 0)
-    {
-      do_cleanups (cleanup);
-      return val;
-    }
+    return val;
 
   /* SHADOW_SLOTNUM saves the original slot number as expected by the caller
      for addressing the SHADOW_CONTENTS placement.  */
@@ -703,13 +700,11 @@ ia64_memory_insert_breakpoint (struct gdbarch *gdbarch,
      restoration mechanism kicks in and we would possibly remove parts of the
      adjacent placed breakpoints.  It is due to our SHADOW_CONTENTS overlapping
      the real breakpoint instruction bits region.  */
-  make_show_memory_breakpoints_cleanup (1);
+  scoped_restore restore_memory_1
+    = make_scoped_restore_show_memory_breakpoints (1);
   val = target_read_memory (addr, bundle, BUNDLE_LEN);
   if (val != 0)
-    {
-      do_cleanups (cleanup);
-      return val;
-    }
+    return val;
 
   /* Breakpoints already present in the code will get deteacted and not get
      reinserted by bp_loc_is_permanent.  Multiple breakpoints at the same
@@ -725,7 +720,6 @@ ia64_memory_insert_breakpoint (struct gdbarch *gdbarch,
   val = target_write_memory (addr + shadow_slotnum, bundle + shadow_slotnum,
 			     bp_tgt->shadow_len);
 
-  do_cleanups (cleanup);
   return val;
 }
 
@@ -739,7 +733,6 @@ ia64_memory_remove_breakpoint (struct gdbarch *gdbarch,
   long long instr_breakpoint, instr_saved;
   int val;
   int templ;
-  struct cleanup *cleanup;
 
   addr &= ~0x0f;
 
@@ -748,13 +741,11 @@ ia64_memory_remove_breakpoint (struct gdbarch *gdbarch,
      mechanism kicks in and we would possibly remove parts of the adjacent
      placed breakpoints.  It is due to our SHADOW_CONTENTS overlapping the real
      breakpoint instruction bits region.  */
-  cleanup = make_show_memory_breakpoints_cleanup (1);
+  scoped_restore restore_memory_1
+    = make_scoped_restore_show_memory_breakpoints (1);
   val = target_read_memory (addr, bundle_mem, BUNDLE_LEN);
   if (val != 0)
-    {
-      do_cleanups (cleanup);
-      return val;
-    }
+    return val;
 
   /* SHADOW_SLOTNUM saves the original slot number as expected by the caller
      for addressing the SHADOW_CONTENTS placement.  */
@@ -772,7 +763,6 @@ ia64_memory_remove_breakpoint (struct gdbarch *gdbarch,
       warning (_("Cannot remove breakpoint at address %s from non-existing "
 		 "X-type slot, memory has changed underneath"),
 	       paddress (gdbarch, bp_tgt->placed_address));
-      do_cleanups (cleanup);
       return -1;
     }
   if (template_encoding_table[templ][slotnum] == L)
@@ -792,7 +782,6 @@ ia64_memory_remove_breakpoint (struct gdbarch *gdbarch,
       warning (_("Cannot remove breakpoint at address %s, "
 		 "no break instruction at such address."),
 	       paddress (gdbarch, bp_tgt->placed_address));
-      do_cleanups (cleanup);
       return -1;
     }
 
@@ -808,7 +797,6 @@ ia64_memory_remove_breakpoint (struct gdbarch *gdbarch,
   replace_slotN_contents (bundle_mem, instr_saved, slotnum);
   val = target_write_raw_memory (addr, bundle_mem, BUNDLE_LEN);
 
-  do_cleanups (cleanup);
   return val;
 }
 
@@ -837,7 +825,6 @@ ia64_breakpoint_from_pc (struct gdbarch *gdbarch,
   long long instr_fetched;
   int val;
   int templ;
-  struct cleanup *cleanup;
 
   if (slotnum > 2)
     error (_("Can't insert breakpoint for slot numbers greater than 2."));
@@ -846,9 +833,11 @@ ia64_breakpoint_from_pc (struct gdbarch *gdbarch,
 
   /* Enable the automatic memory restoration from breakpoints while
      we read our instruction bundle to match bp_loc_is_permanent.  */
-  cleanup = make_show_memory_breakpoints_cleanup (0);
-  val = target_read_memory (addr, bundle, BUNDLE_LEN);
-  do_cleanups (cleanup);
+  {
+    scoped_restore restore_memory_0
+      = make_scoped_restore_show_memory_breakpoints (0);
+    val = target_read_memory (addr, bundle, BUNDLE_LEN);
+  }
 
   /* The memory might be unreachable.  This can happen, for instance,
      when the user inserts a breakpoint at an invalid address.  */
diff --git a/gdb/mem-break.c b/gdb/mem-break.c
index e35d0a5..8e2debf 100644
--- a/gdb/mem-break.c
+++ b/gdb/mem-break.c
@@ -107,8 +107,6 @@ memory_validate_breakpoint (struct gdbarch *gdbarch,
   int val;
   int bplen;
   gdb_byte cur_contents[BREAKPOINT_MAX];
-  struct cleanup *cleanup;
-  int ret;
 
   /* Determine appropriate breakpoint contents and size for this
      address.  */
@@ -118,14 +116,12 @@ memory_validate_breakpoint (struct gdbarch *gdbarch,
     return 0;
 
   /* Make sure we see the memory breakpoints.  */
-  cleanup = make_show_memory_breakpoints_cleanup (1);
+  scoped_restore restore_memory
+    = make_scoped_restore_show_memory_breakpoints (1);
   val = target_read_memory (addr, cur_contents, bplen);
 
   /* If our breakpoint is no longer at the address, this means that
      the program modified the code on us, so it is wrong to put back
      the old value.  */
-  ret = (val == 0 && memcmp (bp, cur_contents, bplen) == 0);
-
-  do_cleanups (cleanup);
-  return ret;
+  return (val == 0 && memcmp (bp, cur_contents, bplen) == 0);
 }
diff --git a/gdb/ppc-linux-tdep.c b/gdb/ppc-linux-tdep.c
index b6e5362..df664ea 100644
--- a/gdb/ppc-linux-tdep.c
+++ b/gdb/ppc-linux-tdep.c
@@ -218,13 +218,13 @@ ppc_linux_memory_remove_breakpoint (struct gdbarch *gdbarch,
   int val;
   int bplen;
   gdb_byte old_contents[BREAKPOINT_MAX];
-  struct cleanup *cleanup;
 
   /* Determine appropriate breakpoint contents and size for this address.  */
   bp = gdbarch_breakpoint_from_pc (gdbarch, &addr, &bplen);
 
   /* Make sure we see the memory breakpoints.  */
-  cleanup = make_show_memory_breakpoints_cleanup (1);
+  scoped_restore restore_memory
+    = make_scoped_restore_show_memory_breakpoints (1);
   val = target_read_memory (addr, old_contents, bplen);
 
   /* If our breakpoint is no longer at the address, this means that the
@@ -233,7 +233,6 @@ ppc_linux_memory_remove_breakpoint (struct gdbarch *gdbarch,
   if (val == 0 && memcmp (bp, old_contents, bplen) == 0)
     val = target_write_raw_memory (addr, bp_tgt->shadow_contents, bplen);
 
-  do_cleanups (cleanup);
   return val;
 }
 
diff --git a/gdb/target.c b/gdb/target.c
index 2f7f317..3e2b4d0 100644
--- a/gdb/target.c
+++ b/gdb/target.c
@@ -1302,20 +1302,10 @@ memory_xfer_partial (struct target_ops *ops, enum target_object object,
   return res;
 }
 
-static void
-restore_show_memory_breakpoints (void *arg)
-{
-  show_memory_breakpoints = (uintptr_t) arg;
-}
-
-struct cleanup *
-make_show_memory_breakpoints_cleanup (int show)
+scoped_restore_tmpl<int>
+make_scoped_restore_show_memory_breakpoints (int show)
 {
-  int current = show_memory_breakpoints;
-
-  show_memory_breakpoints = show;
-  return make_cleanup (restore_show_memory_breakpoints,
-		       (void *) (uintptr_t) current);
+  return make_scoped_restore (&show_memory_breakpoints, show);
 }
 
 /* For docs see target.h, to_xfer_partial.  */
diff --git a/gdb/target.h b/gdb/target.h
index 796717f..a3f00ab 100644
--- a/gdb/target.h
+++ b/gdb/target.h
@@ -42,6 +42,7 @@ struct inferior;
 
 #include "infrun.h" /* For enum exec_direction_kind.  */
 #include "breakpoint.h" /* For enum bptype.  */
+#include "common/scoped_restore.h"
 
 /* This include file defines the interface between the main part
    of the debugger, and the part which is target-specific, or
@@ -2441,9 +2442,10 @@ extern int remote_timeout;
 
 \f
 
-/* Set the show memory breakpoints mode to show, and installs a cleanup
-   to restore it back to the current value.  */
-extern struct cleanup *make_show_memory_breakpoints_cleanup (int show);
+/* Set the show memory breakpoints mode to show, and return a
+   scoped_restore to restore it back to the current value.  */
+extern scoped_restore_tmpl<int>
+    make_scoped_restore_show_memory_breakpoints (int show);
 
 extern int may_write_registers;
 extern int may_write_memory;
-- 
2.9.4


^ permalink raw reply	[flat|nested] 13+ messages in thread

* [RFA 2/8] Replace interp_set_temp with scoped_restore_interp
  2017-09-10 21:50 [RFA 0/8] various cleanup removals Tom Tromey
                   ` (5 preceding siblings ...)
  2017-09-10 21:50 ` [RFA 4/8] Remove cleanups from findcmd.c Tom Tromey
@ 2017-09-10 21:50 ` Tom Tromey
  2017-09-10 21:50 ` [RFA 5/8] Remove cleanups from find_frame_funname Tom Tromey
  2017-09-11 20:35 ` [RFA 0/8] various cleanup removals Simon Marchi
  8 siblings, 0 replies; 13+ messages in thread
From: Tom Tromey @ 2017-09-10 21:50 UTC (permalink / raw)
  To: gdb-patches; +Cc: Tom Tromey

This removes interp_set_temp and an associated cleanup, in favor of a
new RAII class, scoped_restore_interp.

ChangeLog
2017-09-10  Tom Tromey  <tom@tromey.com>

	* cli/cli-script.c (restore_interp): Remove.
	(read_command_lines): Use scoped_restore_interp.
	* interps.c (scoped_restore_interp::set_temp): Rename from
	interp_set_temp.
	* interps.h (class scoped_restore_interp): New.
	(interp_set_temp): Remove.
---
 gdb/ChangeLog        |  9 +++++++++
 gdb/cli/cli-script.c | 10 +---------
 gdb/interps.c        |  2 +-
 gdb/interps.h        | 26 ++++++++++++++++++++++++++
 4 files changed, 37 insertions(+), 10 deletions(-)

diff --git a/gdb/ChangeLog b/gdb/ChangeLog
index 80d615b..de88894 100644
--- a/gdb/ChangeLog
+++ b/gdb/ChangeLog
@@ -1,5 +1,14 @@
 2017-09-10  Tom Tromey  <tom@tromey.com>
 
+	* cli/cli-script.c (restore_interp): Remove.
+	(read_command_lines): Use scoped_restore_interp.
+	* interps.c (scoped_restore_interp::set_temp): Rename from
+	interp_set_temp.
+	* interps.h (class scoped_restore_interp): New.
+	(interp_set_temp): Remove.
+
+2017-09-10  Tom Tromey  <tom@tromey.com>
+
 	* mi/mi-cmd-catch.c (mi_cmd_catch_assert)
 	(mi_cmd_catch_exception, mi_catch_load_unload): Update.
 	* mi/mi-cmd-break.c (setup_breakpoint_reporting): Return a
diff --git a/gdb/cli/cli-script.c b/gdb/cli/cli-script.c
index 9b2ffd0..805064f 100644
--- a/gdb/cli/cli-script.c
+++ b/gdb/cli/cli-script.c
@@ -1158,12 +1158,6 @@ recurse_read_control_structure (char * (*read_next_line_func) (void),
   return ret;
 }
 
-static void
-restore_interp (void *arg)
-{
-  interp_set_temp (interp_name ((struct interp *)arg));
-}
-
 /* Read lines from the input stream and accumulate them in a chain of
    struct command_line's, which is then returned.  For input from a
    terminal, the special command "end" is used to mark the end of the
@@ -1203,12 +1197,10 @@ read_command_lines (char *prompt_arg, int from_tty, int parse_commands,
 				 validator, closure);
   else
     {
-      struct interp *old_interp = interp_set_temp (INTERP_CONSOLE);
-      struct cleanup *old_chain = make_cleanup (restore_interp, old_interp);
+      scoped_restore_interp interp_restorer (INTERP_CONSOLE);
 
       head = read_command_lines_1 (read_next_line, parse_commands,
 				   validator, closure);
-      do_cleanups (old_chain);
     }
 
   if (from_tty && input_interactive_p (current_ui)
diff --git a/gdb/interps.c b/gdb/interps.c
index 19694ff..63a1230 100644
--- a/gdb/interps.c
+++ b/gdb/interps.c
@@ -288,7 +288,7 @@ current_interp_set_logging (ui_file_up logfile,
 
 /* Temporarily overrides the current interpreter.  */
 struct interp *
-interp_set_temp (const char *name)
+scoped_restore_interp::set_interp (const char *name)
 {
   struct ui_interp_info *ui_interp = get_current_interp_info ();
   struct interp *interp = interp_lookup (current_ui, name);
diff --git a/gdb/interps.h b/gdb/interps.h
index b20cf5c..09d1314 100644
--- a/gdb/interps.h
+++ b/gdb/interps.h
@@ -104,6 +104,32 @@ extern struct ui_out *interp_ui_out (struct interp *interp);
 extern const char *interp_name (struct interp *interp);
 extern struct interp *interp_set_temp (const char *name);
 
+/* Temporarily set the current interpreter, and reset it on
+   destruction.  */
+class scoped_restore_interp
+{
+public:
+
+  scoped_restore_interp (const char *name)
+    : m_interp (set_interp (name))
+  {
+  }
+
+  ~scoped_restore_interp ()
+  {
+    set_interp (interp_name (m_interp));
+  }
+
+  scoped_restore_interp (const scoped_restore_interp &) = delete;
+  scoped_restore_interp &operator= (const scoped_restore_interp &) = delete;
+
+private:
+
+  struct interp *set_interp (const char *name);
+
+  struct interp *m_interp;
+};
+
 extern int current_interp_named_p (const char *name);
 
 /* Call this function to give the current interpreter an opportunity
-- 
2.9.4


^ permalink raw reply	[flat|nested] 13+ messages in thread

* [RFA 4/8] Remove cleanups from findcmd.c
  2017-09-10 21:50 [RFA 0/8] various cleanup removals Tom Tromey
                   ` (4 preceding siblings ...)
  2017-09-10 21:50 ` [RFA 7/8] Use std::string in d-namespace.c Tom Tromey
@ 2017-09-10 21:50 ` Tom Tromey
  2017-09-10 21:50 ` [RFA 2/8] Replace interp_set_temp with scoped_restore_interp Tom Tromey
                   ` (2 subsequent siblings)
  8 siblings, 0 replies; 13+ messages in thread
From: Tom Tromey @ 2017-09-10 21:50 UTC (permalink / raw)
  To: gdb-patches; +Cc: Tom Tromey

This removes cleanups from findcmd.c, replacing manual buffer
management with a gdb::byte_vector.

ChangeLog
2017-09-10  Tom Tromey  <tom@tromey.com>

	* findcmd.c (put_bits): Take a gdb::byte_vector.
	(parse_find_args): Return gdb::byte_vector.  "args" now const.
	Remove "pattern_bufp" and "pattern_lenp" parameters.  Remove
	cleanups.
	(find_command): Update.
---
 gdb/ChangeLog |  8 ++++++
 gdb/findcmd.c | 91 ++++++++++++++++++-----------------------------------------
 2 files changed, 35 insertions(+), 64 deletions(-)

diff --git a/gdb/ChangeLog b/gdb/ChangeLog
index 62d3d3c..af874df 100644
--- a/gdb/ChangeLog
+++ b/gdb/ChangeLog
@@ -1,5 +1,13 @@
 2017-09-10  Tom Tromey  <tom@tromey.com>
 
+	* findcmd.c (put_bits): Take a gdb::byte_vector.
+	(parse_find_args): Return gdb::byte_vector.  "args" now const.
+	Remove "pattern_bufp" and "pattern_lenp" parameters.  Remove
+	cleanups.
+	(find_command): Update.
+
+2017-09-10  Tom Tromey  <tom@tromey.com>
+
 	* cli/cli-script.c (class scoped_restore_hook_in): New.
 	(clear_hook_in_cleanup): Remove.
 	(execute_cmd_pre_hook, execute_cmd_post_hook): Use
diff --git a/gdb/findcmd.c b/gdb/findcmd.c
index e35c224..c3baac7 100644
--- a/gdb/findcmd.c
+++ b/gdb/findcmd.c
@@ -25,11 +25,12 @@
 #include "target.h"
 #include "cli/cli-utils.h"
 #include <algorithm>
+#include "common/byte-vector.h"
 
 /* Copied from bfd_put_bits.  */
 
 static void
-put_bits (bfd_uint64_t data, gdb_byte *buf, int bits, bfd_boolean big_p)
+put_bits (bfd_uint64_t data, gdb::byte_vector &buf, int bits, bfd_boolean big_p)
 {
   int i;
   int bytes;
@@ -37,11 +38,13 @@ put_bits (bfd_uint64_t data, gdb_byte *buf, int bits, bfd_boolean big_p)
   gdb_assert (bits % 8 == 0);
 
   bytes = bits / 8;
+  size_t last = buf.size ();
+  buf.resize (last + bytes);
   for (i = 0; i < bytes; i++)
     {
       int index = big_p ? bytes - i - 1 : i;
 
-      buf[index] = data & 0xff;
+      buf[last + index] = data & 0xff;
       data >>= 8;
     }
 }
@@ -49,9 +52,8 @@ put_bits (bfd_uint64_t data, gdb_byte *buf, int bits, bfd_boolean big_p)
 /* Subroutine of find_command to simplify it.
    Parse the arguments of the "find" command.  */
 
-static void
-parse_find_args (char *args, ULONGEST *max_countp,
-		 gdb_byte **pattern_bufp, ULONGEST *pattern_lenp,
+static gdb::byte_vector
+parse_find_args (const char *args, ULONGEST *max_countp,
 		 CORE_ADDR *start_addrp, ULONGEST *search_space_lenp,
 		 bfd_boolean big_p)
 {
@@ -59,27 +61,15 @@ parse_find_args (char *args, ULONGEST *max_countp,
   char size = '\0';
   ULONGEST max_count = ~(ULONGEST) 0;
   /* Buffer to hold the search pattern.  */
-  gdb_byte *pattern_buf;
-  /* Current size of search pattern buffer.
-     We realloc space as needed.  */
-#define INITIAL_PATTERN_BUF_SIZE 100
-  ULONGEST pattern_buf_size = INITIAL_PATTERN_BUF_SIZE;
-  /* Pointer to one past the last in-use part of pattern_buf.  */
-  gdb_byte *pattern_buf_end;
-  ULONGEST pattern_len;
+  gdb::byte_vector pattern_buf;
   CORE_ADDR start_addr;
   ULONGEST search_space_len;
   const char *s = args;
-  struct cleanup *old_cleanups;
   struct value *v;
 
   if (args == NULL)
     error (_("Missing search parameters."));
 
-  pattern_buf = (gdb_byte *) xmalloc (pattern_buf_size);
-  pattern_buf_end = pattern_buf;
-  old_cleanups = make_cleanup (free_current_contents, &pattern_buf);
-
   /* Get search granularity and/or max count if specified.
      They may be specified in either order, together or separately.  */
 
@@ -131,9 +121,8 @@ parse_find_args (char *args, ULONGEST *max_countp,
       len = value_as_long (v);
       if (len == 0)
 	{
-	  do_cleanups (old_cleanups);
 	  printf_filtered (_("Empty search range.\n"));
-	  return;
+	  return pattern_buf;
 	}
       if (len < 0)
 	error (_("Invalid length."));
@@ -169,53 +158,36 @@ parse_find_args (char *args, ULONGEST *max_countp,
     {
       LONGEST x;
       struct type *t;
-      ULONGEST pattern_buf_size_need;
 
       s = skip_spaces_const (s);
 
       v = parse_to_comma_and_eval (&s);
       t = value_type (v);
 
-      /* Keep it simple and assume size == 'g' when watching for when we
-	 need to grow the pattern buf.  */
-      pattern_buf_size_need = (pattern_buf_end - pattern_buf
-			       + std::max (TYPE_LENGTH (t),
-					   (unsigned) sizeof (int64_t)));
-      if (pattern_buf_size_need > pattern_buf_size)
-	{
-	  size_t current_offset = pattern_buf_end - pattern_buf;
-
-	  pattern_buf_size = pattern_buf_size_need * 2;
-	  pattern_buf = (gdb_byte *) xrealloc (pattern_buf, pattern_buf_size);
-	  pattern_buf_end = pattern_buf + current_offset;
-	}
-
       if (size != '\0')
 	{
 	  x = value_as_long (v);
 	  switch (size)
 	    {
 	    case 'b':
-	      *pattern_buf_end++ = x;
+	      pattern_buf.push_back (x);
 	      break;
 	    case 'h':
-	      put_bits (x, pattern_buf_end, 16, big_p);
-	      pattern_buf_end += sizeof (int16_t);
+	      put_bits (x, pattern_buf, 16, big_p);
 	      break;
 	    case 'w':
-	      put_bits (x, pattern_buf_end, 32, big_p);
-	      pattern_buf_end += sizeof (int32_t);
+	      put_bits (x, pattern_buf, 32, big_p);
 	      break;
 	    case 'g':
-	      put_bits (x, pattern_buf_end, 64, big_p);
-	      pattern_buf_end += sizeof (int64_t);
+	      put_bits (x, pattern_buf, 64, big_p);
 	      break;
 	    }
 	}
       else
 	{
-	  memcpy (pattern_buf_end, value_contents (v), TYPE_LENGTH (t));
-	  pattern_buf_end += TYPE_LENGTH (t);
+	  const gdb_byte *contents = value_contents (v);
+	  pattern_buf.insert (pattern_buf.end (), contents,
+			      contents + TYPE_LENGTH (t));
 	}
 
       if (*s == ',')
@@ -223,23 +195,17 @@ parse_find_args (char *args, ULONGEST *max_countp,
       s = skip_spaces_const (s);
     }
 
-  if (pattern_buf_end == pattern_buf)
+  if (pattern_buf.empty ())
     error (_("Missing search pattern."));
 
-  pattern_len = pattern_buf_end - pattern_buf;
-
-  if (search_space_len < pattern_len)
+  if (search_space_len < pattern_buf.size ())
     error (_("Search space too small to contain pattern."));
 
   *max_countp = max_count;
-  *pattern_bufp = pattern_buf;
-  *pattern_lenp = pattern_len;
   *start_addrp = start_addr;
   *search_space_lenp = search_space_len;
 
-  /* We successfully parsed the arguments, leave the freeing of PATTERN_BUF
-     to the caller now.  */
-  discard_cleanups (old_cleanups);
+  return pattern_buf;
 }
 
 static void
@@ -250,33 +216,32 @@ find_command (char *args, int from_tty)
   /* Command line parameters.
      These are initialized to avoid uninitialized warnings from -Wall.  */
   ULONGEST max_count = 0;
-  gdb_byte *pattern_buf = 0;
-  ULONGEST pattern_len = 0;
   CORE_ADDR start_addr = 0;
   ULONGEST search_space_len = 0;
   /* End of command line parameters.  */
   unsigned int found_count;
   CORE_ADDR last_found_addr;
-  struct cleanup *old_cleanups;
 
-  parse_find_args (args, &max_count, &pattern_buf, &pattern_len, 
-		   &start_addr, &search_space_len, big_p);
-
-  old_cleanups = make_cleanup (free_current_contents, &pattern_buf);
+  gdb::byte_vector pattern_buf = parse_find_args (args, &max_count,
+						  &start_addr,
+						  &search_space_len,
+						  big_p);
 
   /* Perform the search.  */
 
   found_count = 0;
   last_found_addr = 0;
 
-  while (search_space_len >= pattern_len
+  while (search_space_len >= pattern_buf.size ()
 	 && found_count < max_count)
     {
       /* Offset from start of this iteration to the next iteration.  */
       ULONGEST next_iter_incr;
       CORE_ADDR found_addr;
       int found = target_search_memory (start_addr, search_space_len,
-					pattern_buf, pattern_len, &found_addr);
+					pattern_buf.data (),
+					pattern_buf.size (),
+					&found_addr);
 
       if (found <= 0)
 	break;
@@ -313,8 +278,6 @@ find_command (char *args, int from_tty)
   else
     printf_filtered ("%d pattern%s found.\n", found_count,
 		     found_count > 1 ? "s" : "");
-
-  do_cleanups (old_cleanups);
 }
 
 void
-- 
2.9.4


^ permalink raw reply	[flat|nested] 13+ messages in thread

* [RFA 7/8] Use std::string in d-namespace.c
  2017-09-10 21:50 [RFA 0/8] various cleanup removals Tom Tromey
                   ` (3 preceding siblings ...)
  2017-09-10 21:50 ` [RFA 8/8] Remove make_show_memory_breakpoints_cleanup Tom Tromey
@ 2017-09-10 21:50 ` Tom Tromey
  2017-09-10 21:50 ` [RFA 4/8] Remove cleanups from findcmd.c Tom Tromey
                   ` (3 subsequent siblings)
  8 siblings, 0 replies; 13+ messages in thread
From: Tom Tromey @ 2017-09-10 21:50 UTC (permalink / raw)
  To: gdb-patches; +Cc: Tom Tromey

This removes some cleanups from d-namespace.c by replacing manual
string management with std::string.

ChangeLog
2017-09-10  Tom Tromey  <tom@tromey.com>

	* d-namespace.c (d_lookup_symbol): Use std::string.
	(find_symbol_in_baseclass): Likewise.
---
 gdb/ChangeLog     |  5 +++++
 gdb/d-namespace.c | 45 ++++++++++++---------------------------------
 2 files changed, 17 insertions(+), 33 deletions(-)

diff --git a/gdb/ChangeLog b/gdb/ChangeLog
index 735de68..f757ab5 100644
--- a/gdb/ChangeLog
+++ b/gdb/ChangeLog
@@ -1,5 +1,10 @@
 2017-09-10  Tom Tromey  <tom@tromey.com>
 
+	* d-namespace.c (d_lookup_symbol): Use std::string.
+	(find_symbol_in_baseclass): Likewise.
+
+2017-09-10  Tom Tromey  <tom@tromey.com>
+
 	* ctf.c (ctf_start): Use std::string.
 
 2017-09-10  Tom Tromey  <tom@tromey.com>
diff --git a/gdb/d-namespace.c b/gdb/d-namespace.c
index ae2e7e3..bc791f7 100644
--- a/gdb/d-namespace.c
+++ b/gdb/d-namespace.c
@@ -108,16 +108,13 @@ d_lookup_symbol (const struct language_defn *langdef,
 
   if (search)
     {
-      char *classname, *nested;
+      std::string classname, nested;
       unsigned int prefix_len;
-      struct cleanup *cleanup;
       struct block_symbol class_sym;
 
       /* A simple lookup failed.  Check if the symbol was defined in
 	 a base class.  */
 
-      cleanup = make_cleanup (null_cleanup, NULL);
-
       /* Find the name of the class and the name of the method,
 	 variable, etc.  */
       prefix_len = d_entire_prefix_len (name);
@@ -130,42 +127,31 @@ d_lookup_symbol (const struct language_defn *langdef,
 
 	  lang_this = lookup_language_this (language_def (language_d), block);
 	  if (lang_this.symbol == NULL)
-	    {
-	      do_cleanups (cleanup);
-	      return null_block_symbol;
-	    }
+	    return null_block_symbol;
 
 	  type = check_typedef (TYPE_TARGET_TYPE (SYMBOL_TYPE (lang_this.symbol)));
-	  classname = xstrdup (TYPE_NAME (type));
-	  nested = xstrdup (name);
+	  classname = TYPE_NAME (type);
+	  nested = name;
 	}
       else
 	{
 	  /* The class name is everything up to and including PREFIX_LEN.  */
-	  classname = savestring (name, prefix_len);
+	  classname = std::string (name, prefix_len);
 
 	  /* The rest of the name is everything else past the initial scope
 	     operator.  */
-	  nested = xstrdup (name + prefix_len + 1);
+	  nested = std::string (name + prefix_len + 1);
 	}
 
-      /* Add cleanups to free memory for these strings.  */
-      make_cleanup (xfree, classname);
-      make_cleanup (xfree, nested);
-
       /* Lookup a class named CLASSNAME.  If none is found, there is nothing
 	 more that can be done.  */
-      class_sym = lookup_global_symbol (classname, block, domain);
+      class_sym = lookup_global_symbol (classname.c_str (), block, domain);
       if (class_sym.symbol == NULL)
-	{
-	  do_cleanups (cleanup);
-	  return null_block_symbol;
-	}
+	return null_block_symbol;
 
       /* Look for a symbol named NESTED in this class.  */
       sym = d_lookup_nested_symbol (SYMBOL_TYPE (class_sym.symbol),
-				    nested, block);
-      do_cleanups (cleanup);
+				    nested.c_str (), block);
     }
 
   return sym;
@@ -260,18 +246,14 @@ static struct block_symbol
 find_symbol_in_baseclass (struct type *parent_type, const char *name,
 			  const struct block *block)
 {
-  char *concatenated_name = NULL;
   struct block_symbol sym;
-  struct cleanup *cleanup;
   int i;
 
   sym.symbol = NULL;
   sym.block = NULL;
-  cleanup = make_cleanup (free_current_contents, &concatenated_name);
 
   for (i = 0; i < TYPE_N_BASECLASSES (parent_type); ++i)
     {
-      size_t len;
       struct type *base_type = TYPE_BASECLASS (parent_type, i);
       const char *base_name = TYPE_BASECLASS_NAME (parent_type, i);
 
@@ -287,10 +269,8 @@ find_symbol_in_baseclass (struct type *parent_type, const char *name,
       /* Now search all static file-level symbols.  We have to do this for
 	 things like typedefs in the class.  First search in this symtab,
 	 what we want is possibly there.  */
-      len = strlen (base_name) + strlen (name) + 2;
-      concatenated_name = (char *) xrealloc (concatenated_name, len);
-      xsnprintf (concatenated_name, len, "%s.%s", base_name, name);
-      sym = lookup_symbol_in_static_block (concatenated_name, block,
+      std::string concatenated_name = std::string (base_name) + "." + name;
+      sym = lookup_symbol_in_static_block (concatenated_name.c_str (), block,
 					   VAR_DOMAIN);
       if (sym.symbol != NULL)
 	break;
@@ -298,7 +278,7 @@ find_symbol_in_baseclass (struct type *parent_type, const char *name,
       /* Nope.  We now have to search all static blocks in all objfiles,
 	 even if block != NULL, because there's no guarantees as to which
 	 symtab the symbol we want is in.  */
-      sym = lookup_static_symbol (concatenated_name, VAR_DOMAIN);
+      sym = lookup_static_symbol (concatenated_name.c_str (), VAR_DOMAIN);
       if (sym.symbol != NULL)
 	break;
 
@@ -312,7 +292,6 @@ find_symbol_in_baseclass (struct type *parent_type, const char *name,
 	}
     }
 
-  do_cleanups (cleanup);
   return sym;
 }
 
-- 
2.9.4


^ permalink raw reply	[flat|nested] 13+ messages in thread

* [RFA 1/8] Change setup_breakpoint_reporting to return a scoped_restore
  2017-09-10 21:50 [RFA 0/8] various cleanup removals Tom Tromey
  2017-09-10 21:50 ` [RFA 6/8] Use std::string in ctf_start Tom Tromey
@ 2017-09-10 21:50 ` Tom Tromey
  2017-09-10 21:50 ` [RFA 3/8] Replace clear_hook_in_cleanup with scoped_restore_hook_in Tom Tromey
                   ` (6 subsequent siblings)
  8 siblings, 0 replies; 13+ messages in thread
From: Tom Tromey @ 2017-09-10 21:50 UTC (permalink / raw)
  To: gdb-patches; +Cc: Tom Tromey

This changes setup_breakpoint_reporting to return a scoped_restore,
allowing for some cleanup removal.

ChangeLog
2017-09-10  Tom Tromey  <tom@tromey.com>

	* mi/mi-cmd-catch.c (mi_cmd_catch_assert)
	(mi_cmd_catch_exception, mi_catch_load_unload): Update.
	* mi/mi-cmd-break.c (setup_breakpoint_reporting): Return a
	scoped_restore.
	(mi_cmd_break_insert_1): Update.
	* mi/mi-cmd-break.h (setup_breakpoint_reporting): Return a
	scoped_restore.
---
 gdb/ChangeLog         | 10 ++++++++++
 gdb/mi/mi-cmd-break.c | 14 +++++---------
 gdb/mi/mi-cmd-break.h |  3 ++-
 gdb/mi/mi-cmd-catch.c | 10 +++-------
 4 files changed, 20 insertions(+), 17 deletions(-)

diff --git a/gdb/ChangeLog b/gdb/ChangeLog
index 1781ddd..80d615b 100644
--- a/gdb/ChangeLog
+++ b/gdb/ChangeLog
@@ -1,3 +1,13 @@
+2017-09-10  Tom Tromey  <tom@tromey.com>
+
+	* mi/mi-cmd-catch.c (mi_cmd_catch_assert)
+	(mi_cmd_catch_exception, mi_catch_load_unload): Update.
+	* mi/mi-cmd-break.c (setup_breakpoint_reporting): Return a
+	scoped_restore.
+	(mi_cmd_break_insert_1): Update.
+	* mi/mi-cmd-break.h (setup_breakpoint_reporting): Return a
+	scoped_restore.
+
 2017-09-10  Andrew Burgess  <andrew.burgess@embecosm.com>
 
 	* utils.c (abort_with_message): Don't compare gdb_stderr to NULL,
diff --git a/gdb/mi/mi-cmd-break.c b/gdb/mi/mi-cmd-break.c
index 560174e..bae8711 100644
--- a/gdb/mi/mi-cmd-break.c
+++ b/gdb/mi/mi-cmd-break.c
@@ -64,26 +64,22 @@ enum bp_type
   };
 
 /* Arrange for all new breakpoints and catchpoints to be reported to
-   CURRENT_UIOUT until the cleanup returned by this function is run.
+   CURRENT_UIOUT until the destructor of the returned scoped_restore
+   is run.
 
    Note that MI output will be probably invalid if more than one
    breakpoint is created inside one MI command.  */
 
-struct cleanup *
+scoped_restore_tmpl<int>
 setup_breakpoint_reporting (void)
 {
-  struct cleanup *rev_flag;
-
   if (! mi_breakpoint_observers_installed)
     {
       observer_attach_breakpoint_created (breakpoint_notify);
       mi_breakpoint_observers_installed = 1;
     }
 
-  rev_flag = make_cleanup_restore_integer (&mi_can_breakpoint_notify);
-  mi_can_breakpoint_notify = 1;
-
-  return rev_flag;
+  return make_scoped_restore (&mi_can_breakpoint_notify, 1);
 }
 
 
@@ -301,7 +297,7 @@ mi_cmd_break_insert_1 (int dprintf, const char *command, char **argv, int argc)
     }
 
   /* Now we have what we need, let's insert the breakpoint!  */
-  setup_breakpoint_reporting ();
+  scoped_restore restore_breakpoint_reporting = setup_breakpoint_reporting ();
 
   if (tracepoint)
     {
diff --git a/gdb/mi/mi-cmd-break.h b/gdb/mi/mi-cmd-break.h
index 6fb30c8..2394074 100644
--- a/gdb/mi/mi-cmd-break.h
+++ b/gdb/mi/mi-cmd-break.h
@@ -21,10 +21,11 @@
 #ifndef MI_CMD_BREAK_H
 #define MI_CMD_BREAK_H
 
+#include "common/scoped_restore.h"
 
 /* Setup the reporting of the insertion of a new breakpoint or
    catchpoint.  */
-struct cleanup *setup_breakpoint_reporting (void);
+scoped_restore_tmpl<int> setup_breakpoint_reporting (void);
 
 #endif
 
diff --git a/gdb/mi/mi-cmd-catch.c b/gdb/mi/mi-cmd-catch.c
index a767ee7..0e1fb7e 100644
--- a/gdb/mi/mi-cmd-catch.c
+++ b/gdb/mi/mi-cmd-catch.c
@@ -79,7 +79,7 @@ mi_cmd_catch_assert (const char *cmd, char *argv[], int argc)
   if (oind != argc)
     error (_("Invalid argument: %s"), argv[oind]);
 
-  setup_breakpoint_reporting ();
+  scoped_restore restore_breakpoint_reporting = setup_breakpoint_reporting ();
   /* create_ada_exception_catchpoint needs CONDITION to be xstrdup'ed,
      and will assume control of its lifetime.  */
   if (condition != NULL)
@@ -156,7 +156,7 @@ mi_cmd_catch_exception (const char *cmd, char *argv[], int argc)
   if (ex_kind == ada_catch_exception_unhandled && exception_name != NULL)
     error (_("\"-e\" and \"-u\" are mutually exclusive"));
 
-  setup_breakpoint_reporting ();
+  scoped_restore restore_breakpoint_reporting = setup_breakpoint_reporting ();
   /* create_ada_exception_catchpoint needs EXCEPTION_NAME and CONDITION
      to be xstrdup'ed, and will assume control of their lifetime.  */
   if (exception_name != NULL)
@@ -173,7 +173,6 @@ mi_cmd_catch_exception (const char *cmd, char *argv[], int argc)
 static void
 mi_catch_load_unload (int load, char *argv[], int argc)
 {
-  struct cleanup *back_to;
   const char *actual_cmd = load ? "-catch-load" : "-catch-unload";
   int temp = 0;
   int enabled = 1;
@@ -215,11 +214,8 @@ mi_catch_load_unload (int load, char *argv[], int argc)
   if (oind < argc -1)
     error (_("-catch-load/unload: Garbage following the <library name>"));
 
-  back_to = setup_breakpoint_reporting ();
-
+  scoped_restore restore_breakpoint_reporting = setup_breakpoint_reporting ();
   add_solib_catchpoint (argv[oind], load, temp, enabled);
-
-  do_cleanups (back_to);
 }
 
 /* Handler for the -catch-load.  */
-- 
2.9.4


^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [RFA 5/8] Remove cleanups from find_frame_funname
  2017-09-10 21:50 ` [RFA 5/8] Remove cleanups from find_frame_funname Tom Tromey
@ 2017-09-11 20:25   ` Simon Marchi
  0 siblings, 0 replies; 13+ messages in thread
From: Simon Marchi @ 2017-09-11 20:25 UTC (permalink / raw)
  To: Tom Tromey; +Cc: gdb-patches

> diff --git a/gdb/guile/scm-frame.c b/gdb/guile/scm-frame.c
> index b2af743..f504a88 100644
> --- a/gdb/guile/scm-frame.c
> +++ b/gdb/guile/scm-frame.c
> @@ -418,7 +418,7 @@ static SCM
>  gdbscm_frame_name (SCM self)
>  {
>    frame_smob *f_smob;
> -  char *name = NULL;
> +  gdb::unique_xmalloc_ptr<char> name;
>    enum language lang = language_minimal;
>    struct frame_info *frame = NULL;
>    SCM result;
> @@ -429,11 +429,10 @@ gdbscm_frame_name (SCM self)
>      {
>        frame = frscm_frame_smob_to_frame (f_smob);
>        if (frame != NULL)
> -	find_frame_funname (frame, &name, &lang, NULL);
> +	name = find_frame_funname (frame, &lang, NULL);
>      }
>    CATCH (except, RETURN_MASK_ALL)
>      {
> -      xfree (name);
>        GDBSCM_HANDLE_GDB_EXCEPTION (except);
>      }
>    END_CATCH
> @@ -446,8 +445,7 @@ gdbscm_frame_name (SCM self)
> 
>    if (name != NULL)
>      {
> -      result = gdbscm_scm_from_c_string (name);
> -      xfree (name);
> +      result = gdbscm_scm_from_c_string (name.get ());
>      }

Nit: curly braces not needed.

Simon


^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [RFA 8/8] Remove make_show_memory_breakpoints_cleanup
  2017-09-10 21:50 ` [RFA 8/8] Remove make_show_memory_breakpoints_cleanup Tom Tromey
@ 2017-09-11 20:34   ` Simon Marchi
  0 siblings, 0 replies; 13+ messages in thread
From: Simon Marchi @ 2017-09-11 20:34 UTC (permalink / raw)
  To: Tom Tromey; +Cc: gdb-patches

> @@ -9009,14 +9008,13 @@ program_breakpoint_here_p (struct gdbarch
> *gdbarch, CORE_ADDR address)
>    /* Enable the automatic memory restoration from breakpoints while
>       we read the memory.  Otherwise we could say about our temporary
>       breakpoints they are permanent.  */
> -  cleanup = make_show_memory_breakpoints_cleanup (0);
> +  scoped_restore restore_memory
> +    = make_scoped_restore_show_memory_breakpoints (0);
> 
>    if (target_read_memory (address, target_mem, len) == 0
>        && memcmp (target_mem, bpoint, len) == 0)
>      retval = 1;
> 
> -  do_cleanups (cleanup);
> -
>    return retval;
>  }

I think the retval variable can be removed in favor of returning 
directly.

Simon


^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [RFA 0/8] various cleanup removals
  2017-09-10 21:50 [RFA 0/8] various cleanup removals Tom Tromey
                   ` (7 preceding siblings ...)
  2017-09-10 21:50 ` [RFA 5/8] Remove cleanups from find_frame_funname Tom Tromey
@ 2017-09-11 20:35 ` Simon Marchi
  2017-09-11 22:04   ` Tom Tromey
  8 siblings, 1 reply; 13+ messages in thread
From: Simon Marchi @ 2017-09-11 20:35 UTC (permalink / raw)
  To: Tom Tromey; +Cc: gdb-patches

On 2017-09-10 23:50, Tom Tromey wrote:
> Here's a series that removes a few more cleanups.  It's a mixed bag of
> string-based cleanups and scoped_restore-like cleanups.
> 
> Regression tested on the buildbot.
> 
> Tom

The series LGTM, I sent two small nit comments separately.

Thanks,

Simon


^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [RFA 0/8] various cleanup removals
  2017-09-11 20:35 ` [RFA 0/8] various cleanup removals Simon Marchi
@ 2017-09-11 22:04   ` Tom Tromey
  0 siblings, 0 replies; 13+ messages in thread
From: Tom Tromey @ 2017-09-11 22:04 UTC (permalink / raw)
  To: Simon Marchi; +Cc: Tom Tromey, gdb-patches

>>>>> "Simon" == Simon Marchi <simon.marchi@polymtl.ca> writes:

Simon> The series LGTM, I sent two small nit comments separately.

Thanks.  I've fixed those and I will commit it all shortly.

thanks,
Tom


^ permalink raw reply	[flat|nested] 13+ messages in thread

end of thread, other threads:[~2017-09-11 22:04 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-09-10 21:50 [RFA 0/8] various cleanup removals Tom Tromey
2017-09-10 21:50 ` [RFA 6/8] Use std::string in ctf_start Tom Tromey
2017-09-10 21:50 ` [RFA 1/8] Change setup_breakpoint_reporting to return a scoped_restore Tom Tromey
2017-09-10 21:50 ` [RFA 3/8] Replace clear_hook_in_cleanup with scoped_restore_hook_in Tom Tromey
2017-09-10 21:50 ` [RFA 8/8] Remove make_show_memory_breakpoints_cleanup Tom Tromey
2017-09-11 20:34   ` Simon Marchi
2017-09-10 21:50 ` [RFA 7/8] Use std::string in d-namespace.c Tom Tromey
2017-09-10 21:50 ` [RFA 4/8] Remove cleanups from findcmd.c Tom Tromey
2017-09-10 21:50 ` [RFA 2/8] Replace interp_set_temp with scoped_restore_interp Tom Tromey
2017-09-10 21:50 ` [RFA 5/8] Remove cleanups from find_frame_funname Tom Tromey
2017-09-11 20:25   ` Simon Marchi
2017-09-11 20:35 ` [RFA 0/8] various cleanup removals Simon Marchi
2017-09-11 22:04   ` Tom Tromey

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox