]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commitdiff
gdb: use builtin_func_ptr for `$_` set by "info breakpoints" and "info line"
authorTankut Baris Aktemur <tankut.baris.aktemur@intel.com>
Thu, 5 Mar 2026 15:07:53 +0000 (16:07 +0100)
committerTankut Baris Aktemur <tankut.baris.aktemur@intel.com>
Thu, 5 Mar 2026 15:08:17 +0000 (16:08 +0100)
The `$_` convenience var, as set by the "info breakpoints" and
"info line" commands, has the type builtin_data_ptr (i.e. `void *`).
However, both of the aforementioned commands deal with code addresses.
Hence, it makes more sense to use builtin_func_ptr (i.e.
`void (*)()`).

With this change:

    (gdb) b main
    Breakpoint 2 at 0x402547: file test.cpp, line 20.
    (gdb) info breakpoints
    Num     Type           Disp Enb Address            What
    2       breakpoint     keep y   0x0000000000402547 in main(int, char**) at test.cpp:20
    (gdb) p $_
    $2 = (void (*)(void)) 0x402547 <main(int, char**)+39>
    (gdb) ptype $_
    type = void (*)(void)
    (gdb) ptype &main
    type = int (*)(int, char **)
    (gdb) info line 22
    Line 22 of "test.cpp" starts at address 0x40256d <main(int, char**)+77> and ends at 0x4025bd <main(int, char**)+157>.
    (gdb) p $_
    $3 = (void (*)(void)) 0x40256d <main(int, char**)+77>
    (gdb) ptype $_
    type = void (*)(void)
    (gdb)

This also matches the type of PC:

    (gdb) ptype $pc
    type = void (*)(void)

Also add test cases to check that "info breakpoints" and "info line"
set the `$_` var.

Reviewed-By: Eli Zaretskii <eliz@gnu.org>
Reviewed-By: Keith Seitz <keiths@redhat.com>
Approved-By: Tom Tromey <tom@tromey.com>
gdb/doc/gdb.texinfo
gdb/printcmd.c
gdb/testsuite/gdb.base/gdbvars.c
gdb/testsuite/gdb.base/gdbvars.exp

index 3cba156cf898fe1b40dcb731b1a5e667eed3dc01..c711133a7562587cd2458a460ae0a618a0c21efc 100644 (file)
@@ -13111,7 +13111,7 @@ The variable @code{$_} is automatically set by the @code{x} command to
 the last address examined (@pxref{Memory, ,Examining Memory}).  Other
 commands which provide a default address for @code{x} to examine also
 set @code{$_} to that address; these commands include @code{info line}
-and @code{info breakpoint}.  The type of @code{$_} is @code{void *}
+and @code{info breakpoint}.  The type of @code{$_} is @code{void (*)()}
 except when set by the @code{x} command, in which case it is a pointer
 to the type of @code{$__}.
 
index 666ed5a8a6404d26d02bc9ba5839e2f41245e14a..f637bccc58ab7b417d3c09652d69f6a25b04c59f 100644 (file)
@@ -526,12 +526,12 @@ print_scalar_formatted (const gdb_byte *valaddr, struct type *type,
 }
 
 /* Specify default address for `x' command.
-   The `info lines' command uses this.  */
+   The `info lines' and `info breakpoints' commands use this.  */
 
 void
 set_next_address (struct gdbarch *gdbarch, CORE_ADDR addr)
 {
-  struct type *ptr_type = builtin_type (gdbarch)->builtin_data_ptr;
+  type *ptr_type = builtin_type (gdbarch)->builtin_func_ptr;
 
   next_gdbarch = gdbarch;
   next_address = addr;
index 46fa84b122249e51349f283f01aa1abf6e402e2e..4c69778a82bf53dd428ca54c8a16399c5a037434 100644 (file)
@@ -19,5 +19,5 @@ int
 main ()
 {
   p = &p;
-  return 0;
+  return 0; /* last line */
 }
index 4256b9fa2cc615c98aaa4a2fd61b298610fe3382..b9a5f1078c403a7bd01217081f11d44c802a59da 100644 (file)
@@ -37,12 +37,30 @@ proc test_convenience_variables {} {
     gdb_test "print \$foo"             " = 301" \
        "print new contents of convenience variable"
 
+    gdb_test "print \$_" "= void" \
+       "convenience var \$_ is not set yet"
+
+    # "info breakpoints" sets $_ to a (void (*)()).
+    set last_line [gdb_get_line_number "last line"]
+    gdb_breakpoint $last_line
+    gdb_test "info breakpoint" ".*" "info break"
+    delete_breakpoints
+
+    set re_fptr [string_to_regexp "(void (*)())"]
+    gdb_test "print \$_" "= $re_fptr $::hex.*" \
+       "convenience var \$_ is set to a function pointer"
+
     gdb_test_no_output "set \$_ = 11" \
        "set convenience variable \$_"
 
     gdb_test "print \$_"               " = 11" \
        "print contents of convenience variable \$_"
 
+    # "info line" sets $_ to a (void (*)()), too.
+    gdb_test "info line $last_line" ".*"
+    gdb_test "print \$_" "= $re_fptr $::hex.*" \
+       "convenience var \$_ is set to an address again"
+
     gdb_test "print \$foo + 10"        " = 311" \
        "use convenience variable in arithmetic expression"