[RFA,2/2] Remove some clanups from solib-svr4.c

Message ID 20180328042424.15276-3-tom@tromey.com
State New
Headers show
Series
  • more cleanup removal
Related show

Commit Message

Tom Tromey March 28, 2018, 4:24 a.m.
This removes a few cleanups from solib-svr4.c in a straightforward
way.

gdb/ChangeLog
2018-03-26  Tom Tromey  <tom@tromey.com>

	* solib-svr4.c (lm_info_read): Use gdb::byte_vector.
	(svr4_keep_data_in_core): Use gdb::unique_xmalloc_ptr.
---
 gdb/ChangeLog    |  5 +++++
 gdb/solib-svr4.c | 47 ++++++++++-------------------------------------
 2 files changed, 15 insertions(+), 37 deletions(-)

-- 
2.13.6

Comments

Simon Marchi March 30, 2018, 5:47 a.m. | #1
"clanups" in the title.

On 2018-03-28 00:24, Tom Tromey wrote:
> This removes a few cleanups from solib-svr4.c in a straightforward

> way.

> 

> gdb/ChangeLog

> 2018-03-26  Tom Tromey  <tom@tromey.com>

> 

> 	* solib-svr4.c (lm_info_read): Use gdb::byte_vector.

> 	(svr4_keep_data_in_core): Use gdb::unique_xmalloc_ptr.

> ---

>  gdb/ChangeLog    |  5 +++++

>  gdb/solib-svr4.c | 47 ++++++++++-------------------------------------

>  2 files changed, 15 insertions(+), 37 deletions(-)

> 

> diff --git a/gdb/solib-svr4.c b/gdb/solib-svr4.c

> index d8d047d394..c2170891e5 100644

> --- a/gdb/solib-svr4.c

> +++ b/gdb/solib-svr4.c

> @@ -172,14 +172,11 @@ static lm_info_svr4 *

>  lm_info_read (CORE_ADDR lm_addr)

>  {

>    struct link_map_offsets *lmo = svr4_fetch_link_map_offsets ();

> -  gdb_byte *lm;

>    lm_info_svr4 *lm_info;

> -  struct cleanup *back_to;

> 

> -  lm = (gdb_byte *) xmalloc (lmo->link_map_size);

> -  back_to = make_cleanup (xfree, lm);

> +  gdb::byte_vector lm (lmo->link_map_size);

> 

> -  if (target_read_memory (lm_addr, lm, lmo->link_map_size) != 0)

> +  if (target_read_memory (lm_addr, lm.data (), lmo->link_map_size) != 

> 0)

>      {

>        warning (_("Error reading shared library list entry at %s"),

>  	       paddress (target_gdbarch (), lm_addr)),

> @@ -203,8 +200,6 @@ lm_info_read (CORE_ADDR lm_addr)

>  					       ptr_type);

>      }

> 

> -  do_cleanups (back_to);

> -

>    return lm_info;

>  }

> 

> @@ -958,8 +953,6 @@ svr4_keep_data_in_core (CORE_ADDR vaddr, unsigned 

> long size)

>  {

>    struct svr4_info *info;

>    CORE_ADDR ldsomap;

> -  struct so_list *newobj;

> -  struct cleanup *old_chain;

>    CORE_ADDR name_lm;

> 

>    info = get_svr4_info ();

> @@ -973,13 +966,8 @@ svr4_keep_data_in_core (CORE_ADDR vaddr, unsigned

> long size)

>    if (!ldsomap)

>      return 0;

> 

> -  newobj = XCNEW (struct so_list);

> -  old_chain = make_cleanup (xfree, newobj);

> -  lm_info_svr4 *li = lm_info_read (ldsomap);

> -  newobj->lm_info = li;

> -  make_cleanup (xfree, newobj->lm_info);

> +  gdb::unique_xmalloc_ptr<lm_info_svr4> li (lm_info_read (ldsomap));


This is not an error because lm_info_svr4 is trivially destructible, but 
since we allocate it with "new", we might as well use a unique_ptr, it's 
more future-proof.  At least, with the xfree poisoning, adding something 
that does not destruct trivially to lm_info_svr4 would warn us not to 
use unique_xmalloc_ptr here.

How about making lm_info_read return an std::unique_ptr<lm_info_svr4>?

The patch LGTM with or without those changes.

Simon
Tom Tromey March 30, 2018, 7:04 p.m. | #2
>> -  newobj = XCNEW (struct so_list);

>> -  old_chain = make_cleanup (xfree, newobj);

>> -  lm_info_svr4 *li = lm_info_read (ldsomap);

>> -  newobj->lm_info = li;

>> -  make_cleanup (xfree, newobj->lm_info);

>> +  gdb::unique_xmalloc_ptr<lm_info_svr4> li (lm_info_read (ldsomap));


Simon> This is not an error because lm_info_svr4 is trivially destructible,
Simon> but since we allocate it with "new", we might as well use a
Simon> unique_ptr, it's more future-proof.

Good catch, thanks.

Simon> How about making lm_info_read return an std::unique_ptr<lm_info_svr4>?

Simon> The patch LGTM with or without those changes.

How's this?

I re-ran it through the buildbot.

Tom

commit 0662ad7b81c11c4b23e74c1d2fcf847ca63e3a8d
Author: Tom Tromey <tom@tromey.com>
Date:   Tue Mar 27 14:42:55 2018 -0600

    Remove some cleanups from solib-svr4.c
    
    This removes a few cleanups from solib-svr4.c in a straightforward
    way.
    
    gdb/ChangeLog
    2018-03-30  Tom Tromey  <tom@tromey.com>
    
            * solib-svr4.c (lm_info_read): Use gdb::byte_vector.  Return
            std::unique_ptr.
            (svr4_keep_data_in_core): Update.
            (svr4_read_so_list): Update.

diff --git a/gdb/ChangeLog b/gdb/ChangeLog
index d2fe40d0a7..7add93ed63 100644
--- a/gdb/ChangeLog
+++ b/gdb/ChangeLog
@@ -1,3 +1,10 @@
+2018-03-30  Tom Tromey  <tom@tromey.com>
+
+	* solib-svr4.c (lm_info_read): Use gdb::byte_vector.  Return
+	std::unique_ptr.
+	(svr4_keep_data_in_core): Update.
+	(svr4_read_so_list): Update.
+
 2018-03-27  Tom Tromey  <tom@tromey.com>
 
 	* windows-nat.c (handle_output_debug_string, handle_exception):
diff --git a/gdb/solib-svr4.c b/gdb/solib-svr4.c
index d8d047d394..7644c9f4a9 100644
--- a/gdb/solib-svr4.c
+++ b/gdb/solib-svr4.c
@@ -168,28 +168,24 @@ svr4_same (struct so_list *gdb, struct so_list *inferior)
   return (svr4_same_1 (gdb->so_original_name, inferior->so_original_name));
 }
 
-static lm_info_svr4 *
+static std::unique_ptr<lm_info_svr4>
 lm_info_read (CORE_ADDR lm_addr)
 {
   struct link_map_offsets *lmo = svr4_fetch_link_map_offsets ();
-  gdb_byte *lm;
-  lm_info_svr4 *lm_info;
-  struct cleanup *back_to;
+  std::unique_ptr<lm_info_svr4> lm_info;
 
-  lm = (gdb_byte *) xmalloc (lmo->link_map_size);
-  back_to = make_cleanup (xfree, lm);
+  gdb::byte_vector lm (lmo->link_map_size);
 
-  if (target_read_memory (lm_addr, lm, lmo->link_map_size) != 0)
+  if (target_read_memory (lm_addr, lm.data (), lmo->link_map_size) != 0)
     {
       warning (_("Error reading shared library list entry at %s"),
-	       paddress (target_gdbarch (), lm_addr)),
-      lm_info = NULL;
+	       paddress (target_gdbarch (), lm_addr));
     }
   else
     {
       struct type *ptr_type = builtin_type (target_gdbarch ())->builtin_data_ptr;
 
-      lm_info = new lm_info_svr4;
+      lm_info.reset (new lm_info_svr4);
       lm_info->lm_addr = lm_addr;
 
       lm_info->l_addr_inferior = extract_typed_address (&lm[lmo->l_addr_offset],
@@ -203,8 +199,6 @@ lm_info_read (CORE_ADDR lm_addr)
 					       ptr_type);
     }
 
-  do_cleanups (back_to);
-
   return lm_info;
 }
 
@@ -958,8 +952,6 @@ svr4_keep_data_in_core (CORE_ADDR vaddr, unsigned long size)
 {
   struct svr4_info *info;
   CORE_ADDR ldsomap;
-  struct so_list *newobj;
-  struct cleanup *old_chain;
   CORE_ADDR name_lm;
 
   info = get_svr4_info ();
@@ -973,13 +965,8 @@ svr4_keep_data_in_core (CORE_ADDR vaddr, unsigned long size)
   if (!ldsomap)
     return 0;
 
-  newobj = XCNEW (struct so_list);
-  old_chain = make_cleanup (xfree, newobj);
-  lm_info_svr4 *li = lm_info_read (ldsomap);
-  newobj->lm_info = li;
-  make_cleanup (xfree, newobj->lm_info);
+  std::unique_ptr<lm_info_svr4> li (lm_info_read (ldsomap));
   name_lm = li != NULL ? li->l_name : 0;
-  do_cleanups (old_chain);
 
   return (name_lm >= vaddr && name_lm < vaddr + size);
 }
@@ -995,8 +982,7 @@ open_symbol_file_object (int from_tty)
   struct link_map_offsets *lmo = svr4_fetch_link_map_offsets ();
   struct type *ptr_type = builtin_type (target_gdbarch ())->builtin_data_ptr;
   int l_name_size = TYPE_LENGTH (ptr_type);
-  gdb_byte *l_name_buf = (gdb_byte *) xmalloc (l_name_size);
-  struct cleanup *cleanups = make_cleanup (xfree, l_name_buf);
+  gdb::byte_vector l_name_buf (l_name_size);
   struct svr4_info *info = get_svr4_info ();
   symfile_add_flags add_flags = 0;
 
@@ -1005,38 +991,26 @@ open_symbol_file_object (int from_tty)
 
   if (symfile_objfile)
     if (!query (_("Attempt to reload symbols from process? ")))
-      {
-	do_cleanups (cleanups);
-	return 0;
-      }
+      return 0;
 
   /* Always locate the debug struct, in case it has moved.  */
   info->debug_base = 0;
   if (locate_base (info) == 0)
-    {
-      do_cleanups (cleanups);
-      return 0;	/* failed somehow...  */
-    }
+    return 0;	/* failed somehow...  */
 
   /* First link map member should be the executable.  */
   lm = solib_svr4_r_map (info);
   if (lm == 0)
-    {
-      do_cleanups (cleanups);
-      return 0;	/* failed somehow...  */
-    }
+    return 0;	/* failed somehow...  */
 
   /* Read address of name from target memory to GDB.  */
-  read_memory (lm + lmo->l_name_offset, l_name_buf, l_name_size);
+  read_memory (lm + lmo->l_name_offset, l_name_buf.data (), l_name_size);
 
   /* Convert the address to host format.  */
-  l_name = extract_typed_address (l_name_buf, ptr_type);
+  l_name = extract_typed_address (l_name_buf.data (), ptr_type);
 
   if (l_name == 0)
-    {
-      do_cleanups (cleanups);
-      return 0;		/* No filename.  */
-    }
+    return 0;		/* No filename.  */
 
   /* Now fetch the filename from target memory.  */
   target_read_string (l_name, &filename, SO_NAME_MAX_PATH_SIZE - 1, &errcode);
@@ -1045,14 +1019,12 @@ open_symbol_file_object (int from_tty)
     {
       warning (_("failed to read exec filename from attached file: %s"),
 	       safe_strerror (errcode));
-      do_cleanups (cleanups);
       return 0;
     }
 
   /* Have a pathname: read the symbol file.  */
   symbol_file_add_main (filename.get (), add_flags);
 
-  do_cleanups (cleanups);
   return 1;
 }
 
@@ -1342,7 +1314,7 @@ svr4_read_so_list (CORE_ADDR lm, CORE_ADDR prev_lm,
 
       so_list_up newobj (XCNEW (struct so_list));
 
-      lm_info_svr4 *li = lm_info_read (lm);
+      lm_info_svr4 *li = lm_info_read (lm).release ();
       newobj->lm_info = li;
       if (li == NULL)
 	return 0;
Simon Marchi March 30, 2018, 7:11 p.m. | #3
On 2018-03-30 15:04, Tom Tromey wrote:
>>> -  newobj = XCNEW (struct so_list);

>>> -  old_chain = make_cleanup (xfree, newobj);

>>> -  lm_info_svr4 *li = lm_info_read (ldsomap);

>>> -  newobj->lm_info = li;

>>> -  make_cleanup (xfree, newobj->lm_info);

>>> +  gdb::unique_xmalloc_ptr<lm_info_svr4> li (lm_info_read (ldsomap));

> 

> Simon> This is not an error because lm_info_svr4 is trivially 

> destructible,

> Simon> but since we allocate it with "new", we might as well use a

> Simon> unique_ptr, it's more future-proof.

> 

> Good catch, thanks.

> 

> Simon> How about making lm_info_read return an 

> std::unique_ptr<lm_info_svr4>?

> 

> Simon> The patch LGTM with or without those changes.

> 

> How's this?


LGTM, I just noted two nits:

> @@ -168,28 +168,24 @@ svr4_same (struct so_list *gdb, struct so_list 

> *inferior)

>    return (svr4_same_1 (gdb->so_original_name, 

> inferior->so_original_name));

>  }

> 

> -static lm_info_svr4 *

> +static std::unique_ptr<lm_info_svr4>

>  lm_info_read (CORE_ADDR lm_addr)

>  {

>    struct link_map_offsets *lmo = svr4_fetch_link_map_offsets ();

> -  gdb_byte *lm;

> -  lm_info_svr4 *lm_info;

> -  struct cleanup *back_to;

> +  std::unique_ptr<lm_info_svr4> lm_info;

> 

> -  lm = (gdb_byte *) xmalloc (lmo->link_map_size);

> -  back_to = make_cleanup (xfree, lm);

> +  gdb::byte_vector lm (lmo->link_map_size);

> 

> -  if (target_read_memory (lm_addr, lm, lmo->link_map_size) != 0)

> +  if (target_read_memory (lm_addr, lm.data (), lmo->link_map_size) != 

> 0)

>      {

>        warning (_("Error reading shared library list entry at %s"),

> -	       paddress (target_gdbarch (), lm_addr)),

> -      lm_info = NULL;

> +	       paddress (target_gdbarch (), lm_addr));

>      }


Curly braces can be removed here.

> @@ -973,13 +965,8 @@ svr4_keep_data_in_core (CORE_ADDR vaddr, unsigned

> long size)

>    if (!ldsomap)

>      return 0;

> 

> -  newobj = XCNEW (struct so_list);

> -  old_chain = make_cleanup (xfree, newobj);

> -  lm_info_svr4 *li = lm_info_read (ldsomap);

> -  newobj->lm_info = li;

> -  make_cleanup (xfree, newobj->lm_info);

> +  std::unique_ptr<lm_info_svr4> li (lm_info_read (ldsomap));


I think it's clearer to use the assignment operator in that case:

std::unique_ptr<lm_info_svr4> li = lm_info_read (ldsomap);

Simon
Tom Tromey March 30, 2018, 7:18 p.m. | #4
>>>>> "Simon" == Simon Marchi <simon.marchi@polymtl.ca> writes:


Simon> LGTM, I just noted two nits:
[...]

Thanks, I've made these changes and I'm checking it in.

Tom

Patch

diff --git a/gdb/solib-svr4.c b/gdb/solib-svr4.c
index d8d047d394..c2170891e5 100644
--- a/gdb/solib-svr4.c
+++ b/gdb/solib-svr4.c
@@ -172,14 +172,11 @@  static lm_info_svr4 *
 lm_info_read (CORE_ADDR lm_addr)
 {
   struct link_map_offsets *lmo = svr4_fetch_link_map_offsets ();
-  gdb_byte *lm;
   lm_info_svr4 *lm_info;
-  struct cleanup *back_to;
 
-  lm = (gdb_byte *) xmalloc (lmo->link_map_size);
-  back_to = make_cleanup (xfree, lm);
+  gdb::byte_vector lm (lmo->link_map_size);
 
-  if (target_read_memory (lm_addr, lm, lmo->link_map_size) != 0)
+  if (target_read_memory (lm_addr, lm.data (), lmo->link_map_size) != 0)
     {
       warning (_("Error reading shared library list entry at %s"),
 	       paddress (target_gdbarch (), lm_addr)),
@@ -203,8 +200,6 @@  lm_info_read (CORE_ADDR lm_addr)
 					       ptr_type);
     }
 
-  do_cleanups (back_to);
-
   return lm_info;
 }
 
@@ -958,8 +953,6 @@  svr4_keep_data_in_core (CORE_ADDR vaddr, unsigned long size)
 {
   struct svr4_info *info;
   CORE_ADDR ldsomap;
-  struct so_list *newobj;
-  struct cleanup *old_chain;
   CORE_ADDR name_lm;
 
   info = get_svr4_info ();
@@ -973,13 +966,8 @@  svr4_keep_data_in_core (CORE_ADDR vaddr, unsigned long size)
   if (!ldsomap)
     return 0;
 
-  newobj = XCNEW (struct so_list);
-  old_chain = make_cleanup (xfree, newobj);
-  lm_info_svr4 *li = lm_info_read (ldsomap);
-  newobj->lm_info = li;
-  make_cleanup (xfree, newobj->lm_info);
+  gdb::unique_xmalloc_ptr<lm_info_svr4> li (lm_info_read (ldsomap));
   name_lm = li != NULL ? li->l_name : 0;
-  do_cleanups (old_chain);
 
   return (name_lm >= vaddr && name_lm < vaddr + size);
 }
@@ -995,8 +983,7 @@  open_symbol_file_object (int from_tty)
   struct link_map_offsets *lmo = svr4_fetch_link_map_offsets ();
   struct type *ptr_type = builtin_type (target_gdbarch ())->builtin_data_ptr;
   int l_name_size = TYPE_LENGTH (ptr_type);
-  gdb_byte *l_name_buf = (gdb_byte *) xmalloc (l_name_size);
-  struct cleanup *cleanups = make_cleanup (xfree, l_name_buf);
+  gdb::byte_vector l_name_buf (l_name_size);
   struct svr4_info *info = get_svr4_info ();
   symfile_add_flags add_flags = 0;
 
@@ -1005,38 +992,26 @@  open_symbol_file_object (int from_tty)
 
   if (symfile_objfile)
     if (!query (_("Attempt to reload symbols from process? ")))
-      {
-	do_cleanups (cleanups);
-	return 0;
-      }
+      return 0;
 
   /* Always locate the debug struct, in case it has moved.  */
   info->debug_base = 0;
   if (locate_base (info) == 0)
-    {
-      do_cleanups (cleanups);
-      return 0;	/* failed somehow...  */
-    }
+    return 0;	/* failed somehow...  */
 
   /* First link map member should be the executable.  */
   lm = solib_svr4_r_map (info);
   if (lm == 0)
-    {
-      do_cleanups (cleanups);
-      return 0;	/* failed somehow...  */
-    }
+    return 0;	/* failed somehow...  */
 
   /* Read address of name from target memory to GDB.  */
-  read_memory (lm + lmo->l_name_offset, l_name_buf, l_name_size);
+  read_memory (lm + lmo->l_name_offset, l_name_buf.data (), l_name_size);
 
   /* Convert the address to host format.  */
-  l_name = extract_typed_address (l_name_buf, ptr_type);
+  l_name = extract_typed_address (l_name_buf.data (), ptr_type);
 
   if (l_name == 0)
-    {
-      do_cleanups (cleanups);
-      return 0;		/* No filename.  */
-    }
+    return 0;		/* No filename.  */
 
   /* Now fetch the filename from target memory.  */
   target_read_string (l_name, &filename, SO_NAME_MAX_PATH_SIZE - 1, &errcode);
@@ -1045,14 +1020,12 @@  open_symbol_file_object (int from_tty)
     {
       warning (_("failed to read exec filename from attached file: %s"),
 	       safe_strerror (errcode));
-      do_cleanups (cleanups);
       return 0;
     }
 
   /* Have a pathname: read the symbol file.  */
   symbol_file_add_main (filename.get (), add_flags);
 
-  do_cleanups (cleanups);
   return 1;
 }