gdb/dwarf: add dwarf2_per_bfd::filename and use it where possible

Message ID 20250226042429.2270992-1-simon.marchi@polymtl.ca
State New
Headers
Series gdb/dwarf: add dwarf2_per_bfd::filename and use it where possible |

Checks

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

Commit Message

Simon Marchi Feb. 26, 2025, 4:24 a.m. UTC
  From: Simon Marchi <simon.marchi@efficios.com>

I noticed we quite often use:

    bfd_get_filename (per_bfd->obfd)

Add a shortcut for that.

Change-Id: I4e33925a481fd44088386510b01936d38e1d7d38
---
 gdb/dwarf2/dwz.c         | 14 +++++---------
 gdb/dwarf2/index-cache.c |  9 ++++-----
 gdb/dwarf2/read.c        |  8 ++++----
 gdb/dwarf2/read.h        |  4 ++++
 4 files changed, 17 insertions(+), 18 deletions(-)


base-commit: 2f0521c0d6f6ea6fecef96cf825656263abb570d
  

Comments

Andrew Burgess Feb. 28, 2025, 4:30 p.m. UTC | #1
simon.marchi@polymtl.ca writes:

> From: Simon Marchi <simon.marchi@efficios.com>
>
> I noticed we quite often use:
>
>     bfd_get_filename (per_bfd->obfd)
>
> Add a shortcut for that.

Looks good.

Approved-By: Andrew Burgess <aburgess@redhat.com>

Thanks,
Andrew


>
> Change-Id: I4e33925a481fd44088386510b01936d38e1d7d38
> ---
>  gdb/dwarf2/dwz.c         | 14 +++++---------
>  gdb/dwarf2/index-cache.c |  9 ++++-----
>  gdb/dwarf2/read.c        |  8 ++++----
>  gdb/dwarf2/read.h        |  4 ++++
>  4 files changed, 17 insertions(+), 18 deletions(-)
>
> diff --git a/gdb/dwarf2/dwz.c b/gdb/dwarf2/dwz.c
> index af265505c019..e1e407f774c7 100644
> --- a/gdb/dwarf2/dwz.c
> +++ b/gdb/dwarf2/dwz.c
> @@ -213,8 +213,7 @@ dwarf2_read_dwz_file (dwarf2_per_objfile *per_objfile)
>  
>    if (!IS_ABSOLUTE_PATH (filename.c_str ()))
>      {
> -      gdb::unique_xmalloc_ptr<char> abs
> -	= gdb_realpath (bfd_get_filename (per_bfd->obfd));
> +      gdb::unique_xmalloc_ptr<char> abs = gdb_realpath (per_bfd->filename ());
>  
>        filename = ldirname (abs.get ()) + SLASH_STRING + filename;
>      }
> @@ -241,12 +240,9 @@ dwarf2_read_dwz_file (dwarf2_per_objfile *per_objfile)
>    if (dwz_bfd == nullptr)
>      {
>        gdb::unique_xmalloc_ptr<char> alt_filename;
> -      const char *origname = bfd_get_filename (per_bfd->obfd);
> -
> -      scoped_fd fd (debuginfod_debuginfo_query (buildid,
> -						buildid_len,
> -						origname,
> -						&alt_filename));
> +      scoped_fd fd
> +	= debuginfod_debuginfo_query (buildid, buildid_len,
> +				      per_bfd->filename (), &alt_filename);
>  
>        if (fd.get () >= 0)
>  	{
> @@ -263,7 +259,7 @@ dwarf2_read_dwz_file (dwarf2_per_objfile *per_objfile)
>  
>    if (dwz_bfd == NULL)
>      error (_("could not find '.gnu_debugaltlink' file for %s"),
> -	   bfd_get_filename (per_bfd->obfd));
> +	   per_bfd->filename ());
>  
>    auto result = std::make_unique<dwz_file> (std::move (dwz_bfd));
>  
> diff --git a/gdb/dwarf2/index-cache.c b/gdb/dwarf2/index-cache.c
> index 9e3396ebd88c..6c344d7920d5 100644
> --- a/gdb/dwarf2/index-cache.c
> +++ b/gdb/dwarf2/index-cache.c
> @@ -103,8 +103,7 @@ index_cache_store_context::index_cache_store_context (const index_cache &ic,
>    const bfd_build_id *build_id = build_id_bfd_get (per_bfd->obfd);
>    if (build_id == nullptr)
>      {
> -      index_cache_debug ("objfile %s has no build id",
> -			 bfd_get_filename (per_bfd->obfd));
> +      index_cache_debug ("objfile %s has no build id", per_bfd->filename ());
>        m_enabled = false;
>        return;
>      }
> @@ -149,7 +148,7 @@ index_cache_store_context::index_cache_store_context (const index_cache &ic,
>    catch (const gdb_exception_error &except)
>      {
>        index_cache_debug ("couldn't store index cache for objfile %s: %s",
> -			 bfd_get_filename (per_bfd->obfd), except.what ());
> +			 per_bfd->filename (), except.what ());
>        m_enabled = false;
>      }
>  }
> @@ -169,7 +168,7 @@ index_cache_store_context::store () const
>    try
>      {
>        index_cache_debug ("writing index cache for objfile %s",
> -			 bfd_get_filename (m_per_bfd->obfd));
> +			 m_per_bfd->filename ());
>  
>        /* Write the index itself to the directory, using the build id as the
>  	 filename.  */
> @@ -180,7 +179,7 @@ index_cache_store_context::store () const
>    catch (const gdb_exception_error &except)
>      {
>        index_cache_debug ("couldn't store index cache for objfile %s: %s",
> -			 bfd_get_filename (m_per_bfd->obfd), except.what ());
> +			 m_per_bfd->filename (), except.what ());
>      }
>  }
>  
> diff --git a/gdb/dwarf2/read.c b/gdb/dwarf2/read.c
> index e044d1eb0e4d..613caae520fc 100644
> --- a/gdb/dwarf2/read.c
> +++ b/gdb/dwarf2/read.c
> @@ -3110,7 +3110,7 @@ lookup_dwo_unit (dwarf2_cu *cu, die_info *comp_unit_die, const char *dwo_name)
>  	error (_(DWARF_ERROR_PREFIX
>  		 "missing dwo_id for dwo_name %s"
>  		 " [in module %s]"),
> -	       dwo_name, bfd_get_filename (per_cu->per_bfd->obfd));
> +	       dwo_name, per_cu->per_bfd->filename ());
>  
>        dwo_unit = lookup_dwo_comp_unit (cu, dwo_name, comp_dir, *signature);
>      }
> @@ -7755,7 +7755,7 @@ create_dwp_v2_or_v5_section (dwarf2_per_bfd *per_bfd,
>  	       "Bad DWP V2 or V5 section info, doesn't fit in section %s"
>  	       " [in module %s]"),
>  	     sectp ? bfd_section_name (sectp) : "<unknown>",
> -	     bfd_get_filename (per_bfd->obfd));
> +	     per_bfd->filename ());
>      }
>  
>    result.virtual_offset = offset;
> @@ -8219,7 +8219,7 @@ try_open_dwop_file (dwarf2_per_bfd *per_bfd, const char *file_name, int is_dwp,
>      search_path = per_bfd->captured_debug_dir.c_str ();
>  
>    /* Add the path for the executable binary to the list of search paths.  */
> -  std::string objfile_dir = ldirname (bfd_get_filename (per_bfd->obfd));
> +  std::string objfile_dir = ldirname (per_bfd->filename ());
>    search_path_holder.reset (concat (objfile_dir.c_str (),
>  				    dirname_separator_string,
>  				    search_path, nullptr));
> @@ -20920,7 +20920,7 @@ dwarf2_find_containing_comp_unit (sect_offset sect_off,
>  	error (_(DWARF_ERROR_PREFIX
>  		 "could not find CU containing offset %s [in module %s]"),
>  	       sect_offset_str (sect_off),
> -	       bfd_get_filename (per_bfd->obfd));
> +	       per_bfd->filename ());
>  
>        gdb_assert (per_bfd->all_units[low-1]->sect_off
>  		  <= sect_off);
> diff --git a/gdb/dwarf2/read.h b/gdb/dwarf2/read.h
> index e0c0e1fe3f0a..6e03dbcd1dbe 100644
> --- a/gdb/dwarf2/read.h
> +++ b/gdb/dwarf2/read.h
> @@ -465,6 +465,10 @@ struct dwarf2_per_bfd
>  
>    DISABLE_COPY_AND_ASSIGN (dwarf2_per_bfd);
>  
> +  /* Return the filename of the BFD.  */
> +  const char *filename () const
> +  { return bfd_get_filename (this->obfd); }
> +
>    /* Return the CU given its index.  */
>    dwarf2_per_cu_data *get_cu (int index) const
>    {
>
> base-commit: 2f0521c0d6f6ea6fecef96cf825656263abb570d
> -- 
> 2.48.1
  
Simon Marchi Feb. 28, 2025, 7:23 p.m. UTC | #2
On 2025-02-28 11:30, Andrew Burgess wrote:
> simon.marchi@polymtl.ca writes:
> 
>> From: Simon Marchi <simon.marchi@efficios.com>
>>
>> I noticed we quite often use:
>>
>>     bfd_get_filename (per_bfd->obfd)
>>
>> Add a shortcut for that.
> 
> Looks good.
> 
> Approved-By: Andrew Burgess <aburgess@redhat.com>
> 
> Thanks,
> Andrew

Thanks, pushed.

Simon
  

Patch

diff --git a/gdb/dwarf2/dwz.c b/gdb/dwarf2/dwz.c
index af265505c019..e1e407f774c7 100644
--- a/gdb/dwarf2/dwz.c
+++ b/gdb/dwarf2/dwz.c
@@ -213,8 +213,7 @@  dwarf2_read_dwz_file (dwarf2_per_objfile *per_objfile)
 
   if (!IS_ABSOLUTE_PATH (filename.c_str ()))
     {
-      gdb::unique_xmalloc_ptr<char> abs
-	= gdb_realpath (bfd_get_filename (per_bfd->obfd));
+      gdb::unique_xmalloc_ptr<char> abs = gdb_realpath (per_bfd->filename ());
 
       filename = ldirname (abs.get ()) + SLASH_STRING + filename;
     }
@@ -241,12 +240,9 @@  dwarf2_read_dwz_file (dwarf2_per_objfile *per_objfile)
   if (dwz_bfd == nullptr)
     {
       gdb::unique_xmalloc_ptr<char> alt_filename;
-      const char *origname = bfd_get_filename (per_bfd->obfd);
-
-      scoped_fd fd (debuginfod_debuginfo_query (buildid,
-						buildid_len,
-						origname,
-						&alt_filename));
+      scoped_fd fd
+	= debuginfod_debuginfo_query (buildid, buildid_len,
+				      per_bfd->filename (), &alt_filename);
 
       if (fd.get () >= 0)
 	{
@@ -263,7 +259,7 @@  dwarf2_read_dwz_file (dwarf2_per_objfile *per_objfile)
 
   if (dwz_bfd == NULL)
     error (_("could not find '.gnu_debugaltlink' file for %s"),
-	   bfd_get_filename (per_bfd->obfd));
+	   per_bfd->filename ());
 
   auto result = std::make_unique<dwz_file> (std::move (dwz_bfd));
 
diff --git a/gdb/dwarf2/index-cache.c b/gdb/dwarf2/index-cache.c
index 9e3396ebd88c..6c344d7920d5 100644
--- a/gdb/dwarf2/index-cache.c
+++ b/gdb/dwarf2/index-cache.c
@@ -103,8 +103,7 @@  index_cache_store_context::index_cache_store_context (const index_cache &ic,
   const bfd_build_id *build_id = build_id_bfd_get (per_bfd->obfd);
   if (build_id == nullptr)
     {
-      index_cache_debug ("objfile %s has no build id",
-			 bfd_get_filename (per_bfd->obfd));
+      index_cache_debug ("objfile %s has no build id", per_bfd->filename ());
       m_enabled = false;
       return;
     }
@@ -149,7 +148,7 @@  index_cache_store_context::index_cache_store_context (const index_cache &ic,
   catch (const gdb_exception_error &except)
     {
       index_cache_debug ("couldn't store index cache for objfile %s: %s",
-			 bfd_get_filename (per_bfd->obfd), except.what ());
+			 per_bfd->filename (), except.what ());
       m_enabled = false;
     }
 }
@@ -169,7 +168,7 @@  index_cache_store_context::store () const
   try
     {
       index_cache_debug ("writing index cache for objfile %s",
-			 bfd_get_filename (m_per_bfd->obfd));
+			 m_per_bfd->filename ());
 
       /* Write the index itself to the directory, using the build id as the
 	 filename.  */
@@ -180,7 +179,7 @@  index_cache_store_context::store () const
   catch (const gdb_exception_error &except)
     {
       index_cache_debug ("couldn't store index cache for objfile %s: %s",
-			 bfd_get_filename (m_per_bfd->obfd), except.what ());
+			 m_per_bfd->filename (), except.what ());
     }
 }
 
diff --git a/gdb/dwarf2/read.c b/gdb/dwarf2/read.c
index e044d1eb0e4d..613caae520fc 100644
--- a/gdb/dwarf2/read.c
+++ b/gdb/dwarf2/read.c
@@ -3110,7 +3110,7 @@  lookup_dwo_unit (dwarf2_cu *cu, die_info *comp_unit_die, const char *dwo_name)
 	error (_(DWARF_ERROR_PREFIX
 		 "missing dwo_id for dwo_name %s"
 		 " [in module %s]"),
-	       dwo_name, bfd_get_filename (per_cu->per_bfd->obfd));
+	       dwo_name, per_cu->per_bfd->filename ());
 
       dwo_unit = lookup_dwo_comp_unit (cu, dwo_name, comp_dir, *signature);
     }
@@ -7755,7 +7755,7 @@  create_dwp_v2_or_v5_section (dwarf2_per_bfd *per_bfd,
 	       "Bad DWP V2 or V5 section info, doesn't fit in section %s"
 	       " [in module %s]"),
 	     sectp ? bfd_section_name (sectp) : "<unknown>",
-	     bfd_get_filename (per_bfd->obfd));
+	     per_bfd->filename ());
     }
 
   result.virtual_offset = offset;
@@ -8219,7 +8219,7 @@  try_open_dwop_file (dwarf2_per_bfd *per_bfd, const char *file_name, int is_dwp,
     search_path = per_bfd->captured_debug_dir.c_str ();
 
   /* Add the path for the executable binary to the list of search paths.  */
-  std::string objfile_dir = ldirname (bfd_get_filename (per_bfd->obfd));
+  std::string objfile_dir = ldirname (per_bfd->filename ());
   search_path_holder.reset (concat (objfile_dir.c_str (),
 				    dirname_separator_string,
 				    search_path, nullptr));
@@ -20920,7 +20920,7 @@  dwarf2_find_containing_comp_unit (sect_offset sect_off,
 	error (_(DWARF_ERROR_PREFIX
 		 "could not find CU containing offset %s [in module %s]"),
 	       sect_offset_str (sect_off),
-	       bfd_get_filename (per_bfd->obfd));
+	       per_bfd->filename ());
 
       gdb_assert (per_bfd->all_units[low-1]->sect_off
 		  <= sect_off);
diff --git a/gdb/dwarf2/read.h b/gdb/dwarf2/read.h
index e0c0e1fe3f0a..6e03dbcd1dbe 100644
--- a/gdb/dwarf2/read.h
+++ b/gdb/dwarf2/read.h
@@ -465,6 +465,10 @@  struct dwarf2_per_bfd
 
   DISABLE_COPY_AND_ASSIGN (dwarf2_per_bfd);
 
+  /* Return the filename of the BFD.  */
+  const char *filename () const
+  { return bfd_get_filename (this->obfd); }
+
   /* Return the CU given its index.  */
   dwarf2_per_cu_data *get_cu (int index) const
   {