[binutils-gdb] debuginfod-support.c: Replace globals with user_data

Aaron Merey amerey@sourceware.org
Thu Aug 13 21:52:12 GMT 2020


https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=002a3166d359a759b6cddfe4571d1da5b4f96dce

commit 002a3166d359a759b6cddfe4571d1da5b4f96dce
Author: Aaron Merey <amerey@redhat.com>
Date:   Thu Aug 13 17:47:05 2020 -0400

    debuginfod-support.c: Replace globals with user_data
    
    Store query information in user_data struct instead of global variables.
    
    gdb/ChangeLog:
    
            * debuginfod-support.c: Replace global variables with user_data.

Diff:
---
 gdb/ChangeLog            |  4 ++++
 gdb/debuginfod-support.c | 38 ++++++++++++++++++++++----------------
 2 files changed, 26 insertions(+), 16 deletions(-)

diff --git a/gdb/ChangeLog b/gdb/ChangeLog
index c1dc0ec0101..261a407b0a4 100644
--- a/gdb/ChangeLog
+++ b/gdb/ChangeLog
@@ -1,3 +1,7 @@
+2020-08-13  Aaron Merey  <amerey@redhat.com>
+
+	* debuginfod-support.c: Replace global variables with user_data.
+
 2020-08-13  Simon Marchi  <simon.marchi@polymtl.ca>
 
 	* maint.c (maintenance_selftest): Split args and pass array_view
diff --git a/gdb/debuginfod-support.c b/gdb/debuginfod-support.c
index f4a227b0403..0dc88c2c2a1 100644
--- a/gdb/debuginfod-support.c
+++ b/gdb/debuginfod-support.c
@@ -43,29 +43,37 @@ debuginfod_debuginfo_query (const unsigned char *build_id,
 #else
 #include <elfutils/debuginfod.h>
 
-/* TODO: Use debuginfod API extensions instead of these globals.  */
-static std::string desc;
-static std::string fname;
-static bool has_printed;
+struct user_data
+{
+  user_data (const char *desc, const char *fname)
+    : desc (desc), fname (fname), has_printed (false)
+  { }
+
+  const char * const desc;
+  const char * const fname;
+  bool has_printed;
+};
 
 static int
 progressfn (debuginfod_client *c, long cur, long total)
 {
+  user_data *data = static_cast<user_data *> (debuginfod_get_user_data (c));
+
   if (check_quit_flag ())
     {
       printf_filtered ("Cancelling download of %s %ps...\n",
-		       desc.c_str (),
-		       styled_string (file_name_style.style (), fname.c_str ()));
+		       data->desc,
+		       styled_string (file_name_style.style (), data->fname));
       return 1;
     }
 
-  if (!has_printed && total != 0)
+  if (!data->has_printed && total != 0)
     {
       /* Print this message only once.  */
-      has_printed = true;
+      data->has_printed = true;
       printf_filtered ("Downloading %s %ps...\n",
-		       desc.c_str (),
-		       styled_string (file_name_style.style (), fname.c_str ()));
+		       data->desc,
+		       styled_string (file_name_style.style (), data->fname));
     }
 
   return 0;
@@ -98,10 +106,9 @@ debuginfod_source_query (const unsigned char *build_id,
   if (c == nullptr)
     return scoped_fd (-ENOMEM);
 
-  desc = std::string ("source file");
-  fname = std::string (srcpath);
-  has_printed = false;
+  user_data data ("source file", srcpath);
 
+  debuginfod_set_user_data (c, &data);
   scoped_fd fd (debuginfod_find_source (c,
 					build_id,
 					build_id_len,
@@ -136,11 +143,10 @@ debuginfod_debuginfo_query (const unsigned char *build_id,
   if (c == nullptr)
     return scoped_fd (-ENOMEM);
 
-  desc = std::string ("separate debug info for");
-  fname = std::string (filename);
-  has_printed = false;
   char *dname = nullptr;
+  user_data data ("separate debug info for", filename);
 
+  debuginfod_set_user_data (c, &data);
   scoped_fd fd (debuginfod_find_debuginfo (c, build_id, build_id_len, &dname));
 
   if (fd.get () < 0 && fd.get () != -ENOENT)


More information about the Gdb-cvs mailing list