gdb, gdbsupport, gdbserver: add support for printf 't' length modifier

Tom de Vries reported [1] that a use of the `t` printf length modifier
(used for ptrdiff_t) breaks "set debug dwarf-die 1":

    +break -qualified main
    Reading Reading compcomp unit at offset  unit at offset 0x00x39

    Reading comp unit at offset 0x1a8
    Reading comp unit at offset 0x1e9
    Reading comp unit at offset 0x2c5
    Reading comp unit at offset 0x2a2
    Reading comp unit at offset 0x904
    Unrecognized format specifier 't' in printf

This use is in dwarf2/read.c:

      gdb_printf (gdb_stdlog, "Read die from %s@0x%tx of %s:\n",
		  m_die_section->get_name (),
		  (begin_info_ptr - m_die_section->buffer),
		  bfd_get_filename (m_abfd));

Add support for the `t` length modifier in format_pieces, which
gdb_printf ultimately uses (through ui_out::vmessage).  Modify the three
users of format_pieces: gdb's printf command, gdb's ui_out::vmessage
function and gdbserver's ax_printf function.

The implementation is mostly copied from what we do for size_t.

Since format_pieces is also used to implement the printf command, this
patch brings user-visible changes.  Before:

    (gdb) printf "%td\n", -123
    ️ Unrecognized format specifier 't' in printf

After:

    (gdb) printf "%td\n", -123
    -123

[1] https://inbox.sourceware.org/gdb-patches/d4ccce34-aad5-4e3d-8fc9-efb2fc11275c@suse.de/

Change-Id: Ie9fce78f5f48082d8f8a9ca2847b5ae26acaa60d
Approved-By: Tom Tromey <tom@tromey.com>
This commit is contained in:
Simon Marchi
2026-01-12 13:24:02 -05:00
committed by Simon Marchi
parent ffb4550b9f
commit f6022f5390
8 changed files with 65 additions and 6 deletions

View File

@@ -2815,6 +2815,16 @@ ui_printf (const char *arg, struct ui_file *stream)
{
size_t val = value_as_long (val_args[i]);
DIAGNOSTIC_PUSH
DIAGNOSTIC_IGNORE_FORMAT_NONLITERAL
gdb_printf (stream, current_substring, val);
DIAGNOSTIC_POP
break;
}
case ptrdiff_t_arg:
{
ptrdiff_t val = value_as_long (val_args[i]);
DIAGNOSTIC_PUSH
DIAGNOSTIC_IGNORE_FORMAT_NONLITERAL
gdb_printf (stream, current_substring, val);

View File

@@ -2,6 +2,8 @@
It is explicitly unsigned to avoid differences due to native characters
being either signed or unsigned. */
#include <stdlib.h>
#include <stddef.h>
unsigned char ctable1[256] = {
0000, 0001, 0002, 0003, 0004, 0005, 0006, 0007,
0010, 0011, 0012, 0013, 0014, 0015, 0016, 0017,
@@ -77,6 +79,10 @@ charptr teststring2 = "more contents";
const char *teststring3 = "this is a longer test string that we can use";
/* For testing printf with %z (size_t) and %t (ptrdiff_t) length modifiers. */
size_t test_size = 1234;
ptrdiff_t test_ptrdiff = -5678;
/* Test printing of a struct containing character arrays. */
struct some_arrays {

View File

@@ -908,6 +908,10 @@ proc test_printf {} {
# PR cli/14977.
gdb_test "printf \"%s\\n\", 0" "\\(null\\)"
# Test %z (size_t) and %t (ptrdiff_t) length modifiers.
gdb_test "printf \"%zu\\n\", test_size" "1234"
gdb_test "printf \"%td\\n\", test_ptrdiff" "-5678"
with_max_value_size 20 {
gdb_test {printf "%s", teststring3} \
"^printed string requires 45 bytes, which is more than max-value-size"

View File

@@ -682,6 +682,24 @@ ui_out::vmessage (const ui_file_style &in_style, const char *format,
}
}
break;
case ptrdiff_t_arg:
{
ptrdiff_t val = va_arg (args, ptrdiff_t);
switch (piece.n_int_args)
{
case 0:
call_do_message (style, current_substring, val);
break;
case 1:
call_do_message (style, current_substring, intvals[0], val);
break;
case 2:
call_do_message (style, current_substring,
intvals[0], intvals[1], val);
break;
}
}
break;
case double_arg:
call_do_message (style, current_substring, va_arg (args, double));
break;

View File

@@ -105,40 +105,44 @@ test_gdb_formats ()
static void
test_format_int_sizes ()
{
check ("Hello\\t %hu%lu%llu%zu", /* ARI: %ll */
check ("Hello\\t %hu%lu%llu%zu%tu", /* ARI: %ll */
{
{"Hello\t ", literal_piece, 0},
{"%hu", int_arg, 0},
{"%lu", long_arg, 0},
{"%" LL "u", long_long_arg, 0},
{"%zu", size_t_arg, 0},
{"%tu", ptrdiff_t_arg, 0},
});
check ("Hello\\t %hx%lx%llx%zx", /* ARI: %ll */
check ("Hello\\t %hx%lx%llx%zx%tx", /* ARI: %ll */
{
{"Hello\t ", literal_piece, 0},
{"%hx", int_arg, 0},
{"%lx", long_arg, 0},
{"%" LL "x", long_long_arg, 0},
{"%zx", size_t_arg, 0},
{"%tx", ptrdiff_t_arg, 0},
});
check ("Hello\\t %ho%lo%llo%zo", /* ARI: %ll */
check ("Hello\\t %ho%lo%llo%zo%to", /* ARI: %ll */
{
{"Hello\t ", literal_piece, 0},
{"%ho", int_arg, 0},
{"%lo", long_arg, 0},
{"%" LL "o", long_long_arg, 0},
{"%zo", size_t_arg, 0},
{"%to", ptrdiff_t_arg, 0},
});
check ("Hello\\t %hd%ld%lld%zd", /* ARI: %ll */
check ("Hello\\t %hd%ld%lld%zd%td", /* ARI: %ll */
{
{"Hello\t ", literal_piece, 0},
{"%hd", int_arg, 0},
{"%ld", long_arg, 0},
{"%" LL "d", long_long_arg, 0},
{"%zd", size_t_arg, 0},
{"%td", ptrdiff_t_arg, 0},
});
}

View File

@@ -908,6 +908,14 @@ ax_printf (CORE_ADDR fn, CORE_ADDR chan, const char *format,
break;
}
case ptrdiff_t_arg:
{
ptrdiff_t val = args[i];
printf (current_substring, val);
break;
}
case literal_piece:
/* Print a portion of the format string that has no
directives. Note that this will not include any

View File

@@ -111,6 +111,7 @@ format_pieces::format_pieces (const char **arg, bool gdb_extensions,
int seen_big_l = 0, seen_h = 0, seen_big_h = 0;
int seen_big_d = 0, seen_double_big_d = 0;
int seen_size_t = 0;
int seen_ptrdiff_t = 0;
int bad = 0;
int n_int_args = 0;
bool seen_i64 = false;
@@ -224,6 +225,11 @@ format_pieces::format_pieces (const char **arg, bool gdb_extensions,
seen_size_t = 1;
f++;
break;
case 't':
/* For ptrdiff_t. */
seen_ptrdiff_t = 1;
f++;
break;
case 'I':
/* Support the Windows '%I64' extension, because an
earlier call to format_pieces might have converted %lld
@@ -257,6 +263,8 @@ format_pieces::format_pieces (const char **arg, bool gdb_extensions,
case 'i':
if (seen_size_t)
this_argclass = size_t_arg;
else if (seen_ptrdiff_t)
this_argclass = ptrdiff_t_arg;
else if (lcount == 0)
this_argclass = int_arg;
else if (lcount == 1)
@@ -334,7 +342,8 @@ format_pieces::format_pieces (const char **arg, bool gdb_extensions,
if (lcount > 1 || seen_h || seen_big_h || seen_big_h
|| seen_big_d || seen_double_big_d || seen_size_t
|| seen_prec || seen_zero || seen_space || seen_plus)
|| seen_ptrdiff_t || seen_prec || seen_zero || seen_space
|| seen_plus)
bad = 1;
this_argclass = value_arg;

View File

@@ -36,7 +36,7 @@
enum argclass
{
literal_piece,
int_arg, long_arg, long_long_arg, size_t_arg, ptr_arg,
int_arg, long_arg, long_long_arg, size_t_arg, ptrdiff_t_arg, ptr_arg,
string_arg, wide_string_arg, wide_char_arg,
double_arg, long_double_arg,
dec32float_arg, dec64float_arg, dec128float_arg,