[13/24] gdb: make solib-rocm not use so_list internally

Message ID 20231010204213.111285-14-simon.marchi@efficios.com
State New
Headers
Series C++ification of struct so_list |

Checks

Context Check Description
linaro-tcwg-bot/tcwg_gdb_build--master-arm success Testing passed
linaro-tcwg-bot/tcwg_gdb_check--master-aarch64 success Testing passed
linaro-tcwg-bot/tcwg_gdb_check--master-arm success Testing passed
linaro-tcwg-bot/tcwg_gdb_build--master-aarch64 success Testing passed

Commit Message

Simon Marchi Oct. 10, 2023, 8:40 p.m. UTC
  Same rationale as the previous patch, but for solib-rocm.

 - Introduce rocm_so, which is a name a unique_name (see comment in
   rocm_update_solib_list for that) and a unique_ptr to the
   lm_info_svr4.
 - Change the internal lists from so_list lists to vectors of rocm_so.
 - Remove rocm_free_solib_list, as everything is automatic now.
 - Replace rocm_solib_copy_list with so_list_from_rocm_sos.

Change-Id: I71e06e3ea22d6420c9e4e500501c06e9a13398a8
---
 gdb/solib-rocm.c | 92 ++++++++++++++++++++++--------------------------
 1 file changed, 42 insertions(+), 50 deletions(-)
  

Comments

Lancelot SIX Oct. 13, 2023, 6:35 p.m. UTC | #1
Hi Simon,

When applying this patch, I have:

    Applying: gdb: make solib-rocm not use so_list internally
    .git/rebase-apply/patch:171: indent with spaces.
            = string_printf ("code_object_%ld", code_object_list[i].handle);
    warning: 1 line adds whitespace errors.


On Tue, Oct 10, 2023 at 04:40:08PM -0400, Simon Marchi wrote:
> Same rationale as the previous patch, but for solib-rocm.
> 
>  - Introduce rocm_so, which is a name a unique_name (see comment in
>    rocm_update_solib_list for that) and a unique_ptr to the
>    lm_info_svr4.
>  - Change the internal lists from so_list lists to vectors of rocm_so.
>  - Remove rocm_free_solib_list, as everything is automatic now.
>  - Replace rocm_solib_copy_list with so_list_from_rocm_sos.
> 
> Change-Id: I71e06e3ea22d6420c9e4e500501c06e9a13398a8
> ---
>  gdb/solib-rocm.c | 92 ++++++++++++++++++++++--------------------------
>  1 file changed, 42 insertions(+), 50 deletions(-)
> 
> diff --git a/gdb/solib-rocm.c b/gdb/solib-rocm.c
> index 403173ecc998..5016c383a807 100644
> --- a/gdb/solib-rocm.c
> +++ b/gdb/solib-rocm.c
> @@ -243,21 +240,21 @@ rocm_solib_current_sos ()
>    so_list *head = svr4_so_ops.current_sos ();
>  
>    /* Then, the device-side shared library list.  */
> -  so_list *list = get_solib_info (current_inferior ())->solib_list;
> +  std::vector<rocm_so> &dev_sos = get_solib_info (current_inferior ())->solib_list;

There should be a line break (it gets over the line limit), and I think
this dev_sos can be marked const.

Best,
Lancelot.

>  
> -  if (list == nullptr)
> +  if (dev_sos.empty ())
>      return head;
>  
> -  list = rocm_solib_copy_list (list);
> +  so_list *dev_so_list = so_list_from_rocm_sos (dev_sos);
>  
>    if (head == nullptr)
> -    return list;
> +    return dev_so_list;
>  
>    /* Append our libraries to the end of the list.  */
>    so_list *tail;
>    for (tail = head; tail->next; tail = tail->next)
>      /* Nothing.  */;
> -  tail->next = list;
> +  tail->next = dev_so_list;
>  
>    return head;
>  }
  
Simon Marchi Oct. 14, 2023, 8 p.m. UTC | #2
On 10/13/23 14:35, Lancelot SIX wrote:
> Hi Simon,
> 
> When applying this patch, I have:
> 
>     Applying: gdb: make solib-rocm not use so_list internally
>     .git/rebase-apply/patch:171: indent with spaces.
>             = string_printf ("code_object_%ld", code_object_list[i].handle);
>     warning: 1 line adds whitespace errors.

Thanks, fixed that locally.

Simon
  
Pedro Alves Oct. 17, 2023, 3:23 p.m. UTC | #3
On 2023-10-10 21:40, Simon Marchi wrote:

> --- a/gdb/solib-rocm.c
> +++ b/gdb/solib-rocm.c
> @@ -126,14 +126,26 @@ rocm_solib_fd_cache::close (int fd, fileio_error *target_errno)
>  
>  /* ROCm-specific inferior data.  */
>  
> +struct rocm_so
> +{
> +  rocm_so (const char *name, std::string unique_name, lm_info_svr4_up lm_info)
> +    : name (name),
> +      unique_name (std::move(unique_name)),

Space after "move".
  
Simon Marchi Oct. 17, 2023, 3:32 p.m. UTC | #4
On 10/17/23 11:23, Pedro Alves wrote:
> On 2023-10-10 21:40, Simon Marchi wrote:
> 
>> --- a/gdb/solib-rocm.c
>> +++ b/gdb/solib-rocm.c
>> @@ -126,14 +126,26 @@ rocm_solib_fd_cache::close (int fd, fileio_error *target_errno)
>>  
>>  /* ROCm-specific inferior data.  */
>>  
>> +struct rocm_so
>> +{
>> +  rocm_so (const char *name, std::string unique_name, lm_info_svr4_up lm_info)
>> +    : name (name),
>> +      unique_name (std::move(unique_name)),
> 
> Space after "move".
> 

Thanks, fixed locally.

Simon
  

Patch

diff --git a/gdb/solib-rocm.c b/gdb/solib-rocm.c
index 403173ecc998..5016c383a807 100644
--- a/gdb/solib-rocm.c
+++ b/gdb/solib-rocm.c
@@ -126,14 +126,26 @@  rocm_solib_fd_cache::close (int fd, fileio_error *target_errno)
 
 /* ROCm-specific inferior data.  */
 
+struct rocm_so
+{
+  rocm_so (const char *name, std::string unique_name, lm_info_svr4_up lm_info)
+    : name (name),
+      unique_name (std::move(unique_name)),
+      lm_info (std::move (lm_info))
+  {}
+
+  std::string name, unique_name;
+  lm_info_svr4_up lm_info;
+};
+
 struct solib_info
 {
   explicit solib_info (inferior *inf)
-    : solib_list (nullptr), fd_cache (inf)
+    : fd_cache (inf)
   {};
 
   /* List of code objects loaded into the inferior.  */
-  so_list *solib_list;
+  std::vector<rocm_so> solib_list;
 
   /* Cache of opened FD in the inferior.  */
   rocm_solib_fd_cache fd_cache;
@@ -144,23 +156,6 @@  static const registry<inferior>::key<solib_info> rocm_solib_data;
 
 static target_so_ops rocm_solib_ops;
 
-/* Free the solib linked list.  */
-
-static void
-rocm_free_solib_list (struct solib_info *info)
-{
-  while (info->solib_list != nullptr)
-    {
-      struct so_list *next = info->solib_list->next;
-
-      free_so (*info->solib_list);
-      info->solib_list = next;
-    }
-
-  info->solib_list = nullptr;
-}
-
-
 /* Fetch the solib_info data for INF.  */
 
 static struct solib_info *
@@ -207,27 +202,29 @@  rocm_solib_handle_event ()
   rocm_update_solib_list ();
 }
 
-/* Make a deep copy of the solib linked list.  */
+/* Create so_list objects from rocm_so objects in SOS.  */
 
 static so_list *
-rocm_solib_copy_list (const so_list *src)
+so_list_from_rocm_sos (const std::vector<rocm_so> &sos)
 {
   struct so_list *dst = nullptr;
   struct so_list **link = &dst;
 
-  while (src != nullptr)
+  for (const rocm_so &so : sos)
     {
       so_list *newobj = new so_list;
-      memcpy (newobj, src, sizeof (struct so_list));
+      newobj->lm_info = new lm_info_svr4 (*so.lm_info);
+
+      strncpy (newobj->so_name, so.name.c_str (), sizeof (newobj->so_name));
+      newobj->so_name[sizeof (newobj->so_name) - 1] = '\0';
 
-      auto *src_li = gdb::checked_static_cast<lm_info_svr4 *> (src->lm_info);
-      newobj->lm_info = new lm_info_svr4 (*src_li);
+      strncpy (newobj->so_original_name, so.unique_name.c_str (),
+	       sizeof (newobj->so_original_name));
+      newobj->so_original_name[sizeof (newobj->so_original_name) - 1] = '\0';
 
       newobj->next = nullptr;
       *link = newobj;
       link = &newobj->next;
-
-      src = src->next;
     }
 
   return dst;
@@ -243,21 +240,21 @@  rocm_solib_current_sos ()
   so_list *head = svr4_so_ops.current_sos ();
 
   /* Then, the device-side shared library list.  */
-  so_list *list = get_solib_info (current_inferior ())->solib_list;
+  std::vector<rocm_so> &dev_sos = get_solib_info (current_inferior ())->solib_list;
 
-  if (list == nullptr)
+  if (dev_sos.empty ())
     return head;
 
-  list = rocm_solib_copy_list (list);
+  so_list *dev_so_list = so_list_from_rocm_sos (dev_sos);
 
   if (head == nullptr)
-    return list;
+    return dev_so_list;
 
   /* Append our libraries to the end of the list.  */
   so_list *tail;
   for (tail = head; tail->next; tail = tail->next)
     /* Nothing.  */;
-  tail->next = list;
+  tail->next = dev_so_list;
 
   return head;
 }
@@ -687,7 +684,7 @@  rocm_solib_bfd_open (const char *pathname)
 static void
 rocm_solib_create_inferior_hook (int from_tty)
 {
-  rocm_free_solib_list (get_solib_info (current_inferior ()));
+  get_solib_info (current_inferior ())->solib_list.clear ();
 
   svr4_so_ops.solib_create_inferior_hook (from_tty);
 }
@@ -703,8 +700,8 @@  rocm_update_solib_list ()
 
   solib_info *info = get_solib_info (inf);
 
-  rocm_free_solib_list (info);
-  struct so_list **link = &info->solib_list;
+  info->solib_list.clear ();
+  std::vector<rocm_so> &sos = info->solib_list;
 
   amd_dbgapi_code_object_id_t *code_object_list;
   size_t count;
@@ -736,24 +733,18 @@  rocm_update_solib_list ()
       if (status != AMD_DBGAPI_STATUS_SUCCESS)
 	continue;
 
-      so_list *so = new so_list;
-      lm_info_svr4 *li = new lm_info_svr4;
-      li->l_addr = l_addr;
-      so->lm_info = li;
+      gdb::unique_xmalloc_ptr<char> uri_bytes_holder (uri_bytes);
 
-      strncpy (so->so_name, uri_bytes, sizeof (so->so_name));
-      so->so_name[sizeof (so->so_name) - 1] = '\0';
-      xfree (uri_bytes);
+      lm_info_svr4_up li = gdb::make_unique<lm_info_svr4> ();
+      li->l_addr = l_addr;
 
-      /* Make so_original_name unique so that code objects with the same URI
-	 but different load addresses are seen by gdb core as different shared
+      /* Generate a unique name so that code objects with the same URI but
+	 different load addresses are seen by gdb core as different shared
 	 objects.  */
-      xsnprintf (so->so_original_name, sizeof (so->so_original_name),
-		 "code_object_%ld", code_object_list[i].handle);
+      std::string unique_name
+        = string_printf ("code_object_%ld", code_object_list[i].handle);
 
-      so->next = nullptr;
-      *link = so;
-      link = &so->next;
+      sos.emplace_back (uri_bytes, std::move (unique_name), std::move (li));
     }
 
   xfree (code_object_list);
@@ -778,7 +769,8 @@  rocm_update_solib_list ()
 static void
 rocm_solib_target_inferior_created (inferior *inf)
 {
-  rocm_free_solib_list (get_solib_info (inf));
+  get_solib_info (inf)->solib_list.clear ();
+
   rocm_update_solib_list ();
 
   /* Force GDB to reload the solibs.  */