]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commitdiff
[gdbsupport] Reimplement phex and phex_nz as templates
authorTom de Vries <tdevries@suse.de>
Fri, 2 May 2025 20:10:53 +0000 (22:10 +0200)
committerTom de Vries <tdevries@suse.de>
Fri, 2 May 2025 20:10:53 +0000 (22:10 +0200)
Gdbsupport functions phex and phex_nz have a parameter sizeof_l:
...
extern const char *phex (ULONGEST l, int sizeof_l);
extern const char *phex_nz (ULONGEST l, int sizeof_l);
...
and a lot of calls use:
...
  phex (l, sizeof (l))
...

Make this easier by reimplementing the functions as a template, allowing us to
simply write:
...
  phex (l)
...

Simplify existing code using:
...
$ find gdb* -type f \
    | xargs sed -i 's/phex (\([^,]*\), sizeof (\1))/phex (\1)/'
$ find gdb* -type f \
    | xargs sed -i 's/phex_nz (\([^,]*\), sizeof (\1))/phex_nz (\1)/'
...
and manually review:
...
$ find gdb* -type f | xargs grep "phex (.*, sizeof.*)"
$ find gdb* -type f | xargs grep "phex_nz (.*, sizeof.*)"
...

Tested on x86_64-linux.

Approved-By: Tom Tromey <tom@tromey.com>
16 files changed:
gdb/aarch64-linux-tdep.c
gdb/aarch64-tdep.c
gdb/breakpoint.c
gdb/bsd-uthread.c
gdb/dwarf2/read.c
gdb/ravenscar-thread.c
gdb/remote.c
gdb/solib-svr4.c
gdb/tracefile-tfile.c
gdb/tracepoint.c
gdbserver/server.cc
gdbserver/target.cc
gdbserver/tracepoint.cc
gdbserver/utils.cc
gdbsupport/print-utils.cc
gdbsupport/print-utils.h

index b6cdcf03072ecf13c0d1ffff59c7607a0deb5251..a194ac809c23b5c9bb975d132631b55c6be99493 100644 (file)
@@ -2599,8 +2599,8 @@ aarch64_linux_fill_memtag_section (struct gdbarch *gdbarch, asection *osec)
                                 static_cast<int> (memtag_type::allocation)))
        {
          warning (_("Failed to read MTE tags from memory range [%s,%s)."),
-                    phex_nz (start_address, sizeof (start_address)),
-                    phex_nz (end_address, sizeof (end_address)));
+                    phex_nz (start_address),
+                    phex_nz (end_address));
          return false;
        }
 
index bf5b5d3bcf62eb05bcf189535bfa251039e7ff62..8d54e59f332a76e1ae768ba7e12b765b24054f27 100644 (file)
@@ -4236,7 +4236,7 @@ aarch64_memtag_to_string (struct gdbarch *gdbarch, struct value *tag_value)
 
   CORE_ADDR tag = value_as_address (tag_value);
 
-  return string_printf ("0x%s", phex_nz (tag, sizeof (tag)));
+  return string_printf ("0x%s", phex_nz (tag));
 }
 
 /* See aarch64-tdep.h.  */
index 23e505180b980b24efcd6285ed84af2196fcf4f2..f44ac457b22bc1a3c3c33d08e8ed076edcaff112 100644 (file)
@@ -10289,7 +10289,7 @@ masked_watchpoint::print_recreate (struct ui_file *fp) const
     }
 
   gdb_printf (fp, " %s mask 0x%s", exp_string.get (),
-             phex (hw_wp_mask, sizeof (CORE_ADDR)));
+             phex (hw_wp_mask));
   print_recreate_thread (fp);
 }
 
index 51dafeda34fe2732020ff117b5047adec690df86..341aea98fb773990fa55b18dcde21dcea19b4322 100644 (file)
@@ -536,7 +536,7 @@ bsd_uthread_target::pid_to_str (ptid_t ptid)
   if (ptid.tid () != 0)
     return string_printf ("process %d, thread 0x%s",
                          ptid.pid (),
-                         phex_nz (ptid.tid (), sizeof (ULONGEST)));
+                         phex_nz (ptid.tid ()));
 
   return normal_pid_to_str (ptid);
 }
index 0eb248fd676837d6f6508add602afb088300bda5..951f9d26e4b419b5e775deda1185bc8b421e5428 100644 (file)
@@ -1322,7 +1322,7 @@ dwarf2_per_bfd::locate_sections (asection *sectp,
       bfd_size_type size = sectp->size;
       warning (_("Discarding section %s which has an invalid size (%s) "
                 "[in module %s]"),
-              bfd_section_name (sectp), phex_nz (size, sizeof (size)),
+              bfd_section_name (sectp), phex_nz (size),
               this->filename ());
     }
   else if (names.info.matches (sectp->name))
index 13258c46234bc4f31b8efe9b220742acd9233cb4..45205a0028a8e1a3f23cf6b79e0b448be0d411a0 100644 (file)
@@ -503,7 +503,7 @@ ravenscar_thread_target::pid_to_str (ptid_t ptid)
     return beneath ()->pid_to_str (ptid);
 
   return string_printf ("Ravenscar Thread 0x%s",
-                       phex_nz (ptid.tid (), sizeof (ULONGEST)));
+                       phex_nz (ptid.tid ()));
 }
 
 CORE_ADDR
index 73dc426b2e87dbd08d3f0dcfda7369ebfe321afd..1c49cdf0fc0a9750c7cf537434857e86e57ddda7 100644 (file)
@@ -11650,7 +11650,7 @@ remote_target::remote_write_qxfer (const char *object_name,
   i = snprintf (rs->buf.data (), max_size,
                "qXfer:%s:write:%s:%s:",
                object_name, annex ? annex : "",
-               phex_nz (offset, sizeof offset));
+               phex_nz (offset));
   max_size -= (i + 1);
 
   /* Escape as much data as fits into rs->buf.  */
@@ -11715,8 +11715,8 @@ remote_target::remote_read_qxfer (const char *object_name,
   snprintf (rs->buf.data (), get_remote_packet_size () - 4,
            "qXfer:%s:read:%s:%s,%s",
            object_name, annex ? annex : "",
-           phex_nz (offset, sizeof offset),
-           phex_nz (n, sizeof n));
+           phex_nz (offset),
+           phex_nz (n));
   i = putpkt (rs->buf);
   if (i < 0)
     return TARGET_XFER_E_IO;
@@ -12014,7 +12014,7 @@ remote_target::search_memory (CORE_ADDR start_addr, ULONGEST search_space_len,
   i = snprintf (rs->buf.data (), max_size,
                "qSearch:memory:%s;%s;",
                phex_nz (start_addr, addr_size),
-               phex_nz (search_space_len, sizeof (search_space_len)));
+               phex_nz (search_space_len));
   max_size -= (i + 1);
 
   /* Escape as much data as fits into rs->buf.  */
@@ -13763,7 +13763,7 @@ remote_target::download_tracepoint (struct bp_location *loc)
   encode_actions_rsp (loc, &tdp_actions, &stepping_actions);
 
   tpaddr = loc->address;
-  strcpy (addrbuf, phex (tpaddr, sizeof (CORE_ADDR)));
+  strcpy (addrbuf, phex (tpaddr));
   ret = snprintf (buf.data (), buf.size (), "QTDP:%x:%s:%c:%lx:%x",
                  b->number, addrbuf, /* address */
                  (b->enable_state == bp_enabled ? 'E' : 'D'),
@@ -14027,7 +14027,7 @@ remote_target::enable_tracepoint (struct bp_location *location)
 
   xsnprintf (rs->buf.data (), get_remote_packet_size (), "QTEnable:%x:%s",
             location->owner->number,
-            phex (location->address, sizeof (CORE_ADDR)));
+            phex (location->address));
   putpkt (rs->buf);
   remote_get_noisy_reply ();
   if (rs->buf[0] == '\0')
@@ -14043,7 +14043,7 @@ remote_target::disable_tracepoint (struct bp_location *location)
 
   xsnprintf (rs->buf.data (), get_remote_packet_size (), "QTDisable:%x:%s",
             location->owner->number,
-            phex (location->address, sizeof (CORE_ADDR)));
+            phex (location->address));
   putpkt (rs->buf);
   remote_get_noisy_reply ();
   if (rs->buf[0] == '\0')
@@ -15569,7 +15569,7 @@ remote_target::commit_requested_thread_options ()
 
       *obuf_p++ = ';';
       obuf_p += xsnprintf (obuf_p, obuf_endp - obuf_p, "%s",
-                          phex_nz (options, sizeof (options)));
+                          phex_nz (options));
       if (tp->ptid != magic_null_ptid)
        {
          *obuf_p++ = ':';
@@ -15808,8 +15808,8 @@ create_fetch_memtags_request (gdb::char_vector &packet, CORE_ADDR address,
 
   std::string request = string_printf ("qMemTags:%s,%s:%s",
                                       phex_nz (address, addr_size),
-                                      phex_nz (len, sizeof (len)),
-                                      phex_nz (type, sizeof (type)));
+                                      phex_nz (len),
+                                      phex_nz (type));
 
   strcpy (packet.data (), request.c_str ());
 }
@@ -15843,8 +15843,8 @@ create_store_memtags_request (gdb::char_vector &packet, CORE_ADDR address,
   /* Put together the main packet, address and length.  */
   std::string request = string_printf ("QMemTags:%s,%s:%s:",
                                       phex_nz (address, addr_size),
-                                      phex_nz (len, sizeof (len)),
-                                      phex_nz (type, sizeof (type)));
+                                      phex_nz (len),
+                                      phex_nz (type));
   request += bin2hex (tags.data (), tags.size ());
 
   /* Check if we have exceeded the maximum packet size.  */
index 458c4ba205ff2f30b467b4d39957a59a60dcd2ef..9f56d861354bdcb765a821f1f2f1584a87d46096 100644 (file)
@@ -2058,9 +2058,9 @@ solist_update_incremental (svr4_info *info, CORE_ADDR debug_base,
 
       /* Unknown key=value pairs are ignored by the gdbstub.  */
       xsnprintf (annex, sizeof (annex), "lmid=%s;start=%s;prev=%s",
-                phex_nz (debug_base, sizeof (debug_base)),
-                phex_nz (lm, sizeof (lm)),
-                phex_nz (prev_lm, sizeof (prev_lm)));
+                phex_nz (debug_base),
+                phex_nz (lm),
+                phex_nz (prev_lm));
       if (!svr4_current_sos_via_xfer_libraries (&library_list, annex))
        return 0;
 
index c2b5e3b711f4f89ce4153c41ff75c1b92687e3df..5f8b338df4cc8757e47282ecf437dd3dd1daa49d 100644 (file)
@@ -194,12 +194,12 @@ tfile_write_status (struct trace_file_writer *self,
   if (ts->start_time)
     {
       fprintf (writer->fp, ";starttime:%s",
-      phex_nz (ts->start_time, sizeof (ts->start_time)));
+      phex_nz (ts->start_time));
     }
   if (ts->stop_time)
     {
       fprintf (writer->fp, ";stoptime:%s",
-      phex_nz (ts->stop_time, sizeof (ts->stop_time)));
+      phex_nz (ts->stop_time));
     }
   if (ts->notes != NULL)
     {
@@ -254,7 +254,7 @@ tfile_write_uploaded_tp (struct trace_file_writer *self,
   char buf[MAX_TRACE_UPLOAD];
 
   fprintf (writer->fp, "tp T%x:%s:%c:%x:%x",
-          utp->number, phex_nz (utp->addr, sizeof (utp->addr)),
+          utp->number, phex_nz (utp->addr),
           (utp->enabled ? 'E' : 'D'), utp->step, utp->pass);
   if (utp->type == bp_fast_tracepoint)
     fprintf (writer->fp, ":F%x", utp->orig_size);
@@ -265,10 +265,10 @@ tfile_write_uploaded_tp (struct trace_file_writer *self,
   fprintf (writer->fp, "\n");
   for (const auto &act : utp->actions)
     fprintf (writer->fp, "tp A%x:%s:%s\n",
-            utp->number, phex_nz (utp->addr, sizeof (utp->addr)), act.get ());
+            utp->number, phex_nz (utp->addr), act.get ());
   for (const auto &act : utp->step_actions)
     fprintf (writer->fp, "tp S%x:%s:%s\n",
-            utp->number, phex_nz (utp->addr, sizeof (utp->addr)), act.get ());
+            utp->number, phex_nz (utp->addr), act.get ());
   if (utp->at_string)
     {
       encode_source_string (utp->number, utp->addr,
@@ -290,7 +290,7 @@ tfile_write_uploaded_tp (struct trace_file_writer *self,
       fprintf (writer->fp, "tp Z%s\n", buf);
     }
   fprintf (writer->fp, "tp V%x:%s:%x:%s\n",
-          utp->number, phex_nz (utp->addr, sizeof (utp->addr)),
+          utp->number, phex_nz (utp->addr),
           utp->hit_count,
           phex_nz (utp->traceframe_usage,
                    sizeof (utp->traceframe_usage)));
index a2f1a750a44289ba90cd220bca4738d101f49fa7..b43146816847f9d145c9b62fea2147db27eb4265 100644 (file)
@@ -2818,7 +2818,7 @@ encode_source_string (int tpnum, ULONGEST addr,
   if (80 + strlen (srctype) > buf_size)
     error (_("Buffer too small for source encoding"));
   sprintf (buf, "%x:%s:%s:%x:%x:",
-          tpnum, phex_nz (addr, sizeof (addr)),
+          tpnum, phex_nz (addr),
           srctype, 0, (int) strlen (src));
   if (strlen (buf) + strlen (src) * 2 >= buf_size)
     error (_("Source string too long for buffer"));
index 4b1f4b2afbc27440e460811f07a250dc299f7af8..ab69400f97b98fa2f706918e262168b79628fd64 100644 (file)
@@ -2864,7 +2864,7 @@ handle_query (char *own_buf, int packet_len, int *new_packet_len_p)
        {
          char *end_buf = own_buf + strlen (own_buf);
          sprintf (end_buf, ";QThreadOptions=%s",
-                  phex_nz (supported_options, sizeof (supported_options)));
+                  phex_nz (supported_options));
        }
 
       strcat (own_buf, ";QThreadEvents+");
index 4838b39da4392823e2f8440463ac4392fe429816..e812785d39709eee593497048f809ac3b86fa6b6 100644 (file)
@@ -258,7 +258,7 @@ target_pid_to_str (ptid_t ptid)
   else if (ptid.tid () != 0)
     return string_printf("Thread %d.0x%s",
                         ptid.pid (),
-                        phex_nz (ptid.tid (), sizeof (ULONGEST)));
+                        phex_nz (ptid.tid ()));
   else if (ptid.lwp () != 0)
     return string_printf("LWP %d.%ld",
                         ptid.pid (), ptid.lwp ());
index 715cc6344c497a9d8ea16e664a5ceb1b2c2863a4..b308c821e8fbf08fe7cfd40f8f77a4439a22e717 100644 (file)
@@ -3461,8 +3461,8 @@ cmd_qtstatus (char *packet)
           free_space (), phex_nz (trace_buffer_hi - trace_buffer_lo, 0),
           circular_trace_buffer,
           disconnected_tracing,
-          phex_nz (tracing_start_time, sizeof (tracing_start_time)),
-          phex_nz (tracing_stop_time, sizeof (tracing_stop_time)),
+          phex_nz (tracing_start_time),
+          phex_nz (tracing_stop_time),
           buf1, buf2);
 }
 
@@ -4990,7 +4990,7 @@ build_traceframe_info_xml (char blocktype, unsigned char *dataptr, void *data)
        dataptr += sizeof (mlen);
        string_xml_appendf (*buffer,
                            "<memory start=\"0x%s\" length=\"0x%s\"/>\n",
-                           paddress (maddr), phex_nz (mlen, sizeof (mlen)));
+                           paddress (maddr), phex_nz (mlen));
        break;
       }
     case 'V':
index 092fe47f8e81d3b0f4ac006aac20f1a44367e71b..a86405e0a6a6d29daf803c027f9a308540b312c4 100644 (file)
@@ -102,5 +102,5 @@ internal_vwarning (const char *file, int line, const char *fmt, va_list args)
 const char *
 paddress (CORE_ADDR addr)
 {
-  return phex_nz (addr, sizeof (CORE_ADDR));
+  return phex_nz (addr);
 }
index 84a7485362bacecfcdae59211062004e5e8705b8..a798713b2484e0015a6a91f7a874741264431774 100644 (file)
@@ -145,7 +145,7 @@ static int thirty_two = 32;
 /* See print-utils.h.  */
 
 const char *
-phex (ULONGEST l, int sizeof_l)
+phex_ulongest (ULONGEST l, int sizeof_l)
 {
   char *str;
 
@@ -170,7 +170,7 @@ phex (ULONGEST l, int sizeof_l)
       xsnprintf (str, PRINT_CELL_SIZE, "%02x", (unsigned short) (l & 0xff));
       break;
     default:
-      return phex (l, sizeof (l));
+      return phex (l);
       break;
     }
 
@@ -180,7 +180,7 @@ phex (ULONGEST l, int sizeof_l)
 /* See print-utils.h.  */
 
 const char *
-phex_nz (ULONGEST l, int sizeof_l)
+phex_nz_ulongest (ULONGEST l, int sizeof_l)
 {
   char *str;
 
@@ -212,7 +212,7 @@ phex_nz (ULONGEST l, int sizeof_l)
       xsnprintf (str, PRINT_CELL_SIZE, "%x", (unsigned short) (l & 0xff));
       break;
     default:
-      return phex_nz (l, sizeof (l));
+      return phex_nz (l);
       break;
     }
 
@@ -226,7 +226,7 @@ hex_string (LONGEST num)
 {
   char *result = get_print_cell ();
 
-  xsnprintf (result, PRINT_CELL_SIZE, "0x%s", phex_nz (num, sizeof (num)));
+  xsnprintf (result, PRINT_CELL_SIZE, "0x%s", phex_nz (num));
   return result;
 }
 
@@ -237,7 +237,7 @@ hex_string_custom (LONGEST num, int width)
 {
   char *result = get_print_cell ();
   char *result_end = result + PRINT_CELL_SIZE - 1;
-  const char *hex = phex_nz (num, sizeof (num));
+  const char *hex = phex_nz (num);
   int hex_len = strlen (hex);
 
   if (hex_len > width)
@@ -305,7 +305,7 @@ core_addr_to_string (const CORE_ADDR addr)
   char *str = get_print_cell ();
 
   strcpy (str, "0x");
-  strcat (str, phex (addr, sizeof (addr)));
+  strcat (str, phex (addr));
   return str;
 }
 
@@ -317,7 +317,7 @@ core_addr_to_string_nz (const CORE_ADDR addr)
   char *str = get_print_cell ();
 
   strcpy (str, "0x");
-  strcat (str, phex_nz (addr, sizeof (addr)));
+  strcat (str, phex_nz (addr));
   return str;
 }
 
index e50d96fc7f92b4b9d66caf832533ae5d3ff739f6..dc5011c8c2fef032e47bbe869a0b507274eaa2d9 100644 (file)
@@ -34,15 +34,35 @@ extern const char *pulongest (ULONGEST u);
 
 extern const char *plongest (LONGEST l);
 
-/* Convert a ULONGEST into a HEX string, like %lx, with leading zeros.
+/* Convert L (of type ULONGEST) into a hex string, like %lx, with leading
+   zeros.  The result is stored in a circular static buffer, NUMCELLS
+   deep.  */
+
+extern const char *phex_ulongest (ULONGEST l, int sizeof_l);
+
+/* Convert L into a HEX string, like %lx, with leading zeros.
    The result is stored in a circular static buffer, NUMCELLS deep.  */
 
-extern const char *phex (ULONGEST l, int sizeof_l);
+template<typename T>
+const char *phex (T l, int sizeof_l = sizeof (T))
+{
+  return phex_ulongest (l, sizeof_l);
+}
+
+/* Convert L (of type ULONGEST) into a hex string, like %lx, without leading
+   zeros.  The result is stored in a circular static buffer, NUMCELLS
+   deep.  */
 
-/* Convert a ULONGEST into a HEX string, like %lx, without leading zeros.
-   The result is  stored in a circular static buffer, NUMCELLS deep.  */
+extern const char *phex_nz_ulongest (ULONGEST l, int sizeof_l);
+
+/* Convert L into a hex string, like %lx, without leading zeros.
+   The result is stored in a circular static buffer, NUMCELLS deep.  */
 
-extern const char *phex_nz (ULONGEST l, int sizeof_l);
+template<typename T>
+const char *phex_nz (T l, int sizeof_l = sizeof (T))
+{
+  return phex_nz_ulongest (l, sizeof_l);
+}
 
 /* Converts a LONGEST to a C-format hexadecimal literal and stores it
    in a static string.  Returns a pointer to this string.  */