[gdbsupport] Reimplement phex and phex_nz as templates

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>
This commit is contained in:
Tom de Vries
2025-05-02 22:10:53 +02:00
parent a048980c4e
commit 9c1f84c9b4
16 changed files with 68 additions and 48 deletions

View 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;
}

View 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. */

View 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);
}

View 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);
}

View 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))

View 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

View 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. */

View 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;

View 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)));

View 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"));

View 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+");

View 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 ());

View 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':

View 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);
}

View 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;
}

View 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 a ULONGEST into a HEX string, like %lx, without leading zeros.
The result is stored in a circular static buffer, NUMCELLS deep. */
/* 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. */
extern const char *phex_nz (ULONGEST l, int sizeof_l);
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. */
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. */