]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commitdiff
gdb, gdbsupport, gdbserver: add support for printf 't' length modifier
authorSimon Marchi <simon.marchi@polymtl.ca>
Mon, 12 Jan 2026 18:24:02 +0000 (13:24 -0500)
committerSimon Marchi <simon.marchi@efficios.com>
Thu, 15 Jan 2026 19:34:54 +0000 (14:34 -0500)
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>
gdb/printcmd.c
gdb/testsuite/gdb.base/printcmds.c
gdb/testsuite/gdb.base/printcmds.exp
gdb/ui-out.c
gdb/unittests/format_pieces-selftests.c
gdbserver/ax.cc
gdbsupport/format.cc
gdbsupport/format.h

index 783b83615d0220113056251b587b7b4996591901..7c97fc71992692655498efb6eb42042765ae893b 100644 (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);
index 8445fcc1aa2a67768c9e022a12e219b787bdc5d0..82796932e1d307c8277de02070516d5a6ca981dc 100644 (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 {
index bc100f93df9a03371eaf1936f7915fb4bbb94d4f..32a952f01a55d6c4e011687a279fa74b455a3138 100644 (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"
index 4f20e6b9347729c08c96012d040f2a214419a121..87874e22530f54a66139aee79694a5d773be4855 100644 (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;
index af574ccc900f60ffb87f33121fab7fa4440d9766..c5f3cf74f1dc657f36321d608c802b81628c40c3 100644 (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},
     });
 }
 
index 44fcf31572911674edc98d80dbbf08d955e1c039..4993bed2532c999bf81724b2289773fe5f97011e 100644 (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
index b515a95114e0e25a2189b5c2945083aa9ee44ee5..43995edf453e9200588e345583f9d8489e5dbb1e 100644 (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;
index 91bcf1db55a65d0c160fb494cc408d8a92a83771..ced10e46a716f01e4b4342860c7c835ad2df5532 100644 (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,