Bug 31256 - Crash with basic 'list .'
Summary: Crash with basic 'list .'
Status: RESOLVED FIXED
Alias: None
Product: gdb
Classification: Unclassified
Component: gdb (show other bugs)
Version: HEAD
: P2 normal
Target Milestone: 14.2
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2024-01-18 04:57 UTC by Sam James
Modified: 2024-02-11 04:42 UTC (History)
4 users (show)

See Also:
Host:
Target:
Build:
Last reconfirmed:
Project(s) to access:
ssh public key:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Sam James 2024-01-18 04:57:05 UTC
Originally reported downstream in Gentoo at https://bugs.gentoo.org/922336.

Quoting the reproduction instructions which also trigger it for me:
"""
Test program:
```
int main() {}
```

Steps to reproduce:
1. emerge -1 =dev-debug/gdb-14.1
2. Compile test program (gcc/clang main.c)
3. gdb ./a.out
4. Set a breakpoint on main, `b main`
5. `run`
6. `list .` and the segfault should occur

Tested with: GCC 14, GCC 13, Clang 17 and all three resulted in a segmentation fault. The test program was compiled with GCC and Clang as well.
"""

```
$ gdb ./main
Reading symbols from ./main...
(No debugging symbols found in ./main)
(gdb) b main
Breakpoint 1 at 0x1161
(gdb) r
Starting program: /tmp/main
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/usr/lib64/libthread_db.so.1".

Breakpoint 1, 0x0000555555555161 in main ()
(gdb) list .


Fatal signal: Segmentation fault
----- Backtrace -----
0x56033a65d5aa gdb_internal_backtrace_1
        /usr/src/debug/dev-debug/gdb-14.1/gdb-14.1/gdb/bt-utils.c:122
0x56033a65d5aa _Z22gdb_internal_backtracev
        /usr/src/debug/dev-debug/gdb-14.1/gdb-14.1/gdb/bt-utils.c:168
0x56033a65d5aa _Z22gdb_internal_backtracev
        /usr/src/debug/dev-debug/gdb-14.1/gdb-14.1/gdb/bt-utils.c:154
0x56033a7d2704 handle_fatal_signal
        /usr/src/debug/dev-debug/gdb-14.1/gdb-14.1/gdb/event-top.c:889
0x56033a7d2902 handle_sigsegv
        /usr/src/debug/dev-debug/gdb-14.1/gdb-14.1/gdb/event-top.c:962
0x7f9c1ed8780f ???
0x56033abeadd4 _Z18symtab_to_fullnameP6symtab
        /usr/src/debug/dev-debug/gdb-14.1/gdb-14.1/gdb/source.c:1239
0x56033b3022bd _ZN12source_cache6ensureEP6symtab.constprop.0
        /usr/src/debug/dev-debug/gdb-14.1/gdb-14.1/gdb/source-cache.c:199
0x56033abebf59 _ZN12source_cache16get_source_linesEP6symtabiiPNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE
        /usr/src/debug/dev-debug/gdb-14.1/gdb-14.1/gdb/source-cache.c:364
0x56033abebf59 print_source_lines_base
        /usr/src/debug/dev-debug/gdb-14.1/gdb-14.1/gdb/source.c:1381
0x56033a6a401c _Z18print_source_linesP6symtab18source_lines_range10enum_flagsI23print_source_lines_flagE
        /usr/src/debug/dev-debug/gdb-14.1/gdb-14.1/gdb/source.c:1477
0x56033a6a401c list_around_line
        cli/cli-cmds.c:1222
0x56033a6a401c list_command
        cli/cli-cmds.c:1294
0x56033a6a34b4 _Z8cmd_funcP16cmd_list_elementPKci
        cli/cli-decode.c:2735
0x56033acea8c1 _Z15execute_commandPKci
        /usr/src/debug/dev-debug/gdb-14.1/gdb-14.1/gdb/top.c:575
0x56033a7e243e _Z15command_handlerPKc
        /usr/src/debug/dev-debug/gdb-14.1/gdb-14.1/gdb/event-top.c:552
0x56033ad0429e _Z20command_line_handlerOSt10unique_ptrIcN3gdb13xfree_deleterIcEEE
        /usr/src/debug/dev-debug/gdb-14.1/gdb-14.1/gdb/event-top.c:788
0x56033ad0429e tui_command_line_handler
        tui/tui-interp.c:104
0x56033a7d240a gdb_rl_callback_handler
        /usr/src/debug/dev-debug/gdb-14.1/gdb-14.1/gdb/event-top.c:259
0x7f9c1fc5be3c rl_callback_read_char
        /usr/src/debug/sys-libs/readline-8.2_p10/readline-8.2/callback.c:302
0x56033a7dbadb gdb_rl_callback_read_char_wrapper_noexcept
        /usr/src/debug/dev-debug/gdb-14.1/gdb-14.1/gdb/event-top.c:195
0x56033a7dbc1b gdb_rl_callback_read_char_wrapper
        /usr/src/debug/dev-debug/gdb-14.1/gdb-14.1/gdb/event-top.c:234
0x56033ad3301f stdin_event_handler
        /usr/src/debug/dev-debug/gdb-14.1/gdb-14.1/gdb/ui.c:155
0x56033b26af05 gdb_wait_for_event
        ../gdbsupport/event-loop.cc:716
0x56033b2fbe88 _Z16gdb_do_one_eventi.constprop.0
        ../gdbsupport/event-loop.cc:264
0x56033a96eb84 start_event_loop
        /usr/src/debug/dev-debug/gdb-14.1/gdb-14.1/gdb/main.c:407
0x56033a96eb84 captured_command_loop
        /usr/src/debug/dev-debug/gdb-14.1/gdb-14.1/gdb/main.c:471
0x56033a503214 captured_main
        /usr/src/debug/dev-debug/gdb-14.1/gdb-14.1/gdb/main.c:1324
0x56033a503214 _Z8gdb_mainP18captured_main_args
        /usr/src/debug/dev-debug/gdb-14.1/gdb-14.1/gdb/main.c:1343
0x56033a503214 main
        /usr/src/debug/dev-debug/gdb-14.1/gdb-14.1/gdb/gdb.c:39
---------------------
A fatal error internal to GDB has been detected, further
debugging is not possible.  GDB will now terminate.

This is a bug, please report it.  For instructions, see:
<https://bugs.gentoo.org/>.

^C
Segmentation fault (core dumped)
```
Comment 1 Sam James 2024-01-18 18:39:22 UTC
The original reporter Christopher Fore has bisected it to 

commit 3e3a1874fcec34bcf51b4baf4be09aebab561bff
Author: Bruno Larsen <blarsen@redhat.com>
Date:   Thu Jun 15 12:14:22 2023 +0200

    gdb/cli: add '.' as an argument for 'list' command

    Currently, after the user has used the list command once, there is no
    self-contained way to ask GDB to print the location where the inferior is
    stopped.  The current best options require either using a separate
    command to scope out where the inferior is stopped, or using "list *$pc"
    requiring knowledge of GDB standard registers.  This commit adds a way
    to do that using '.' as a new argument for the 'list' command.  If the
    inferior isn't running, the command prints around the main function.

    Because this necessitated having the inferior running and the test was
    (seemingly unnecessarily) using printf in a non-essential way and it
    would make the resulting log harder to read for no benefit, it was
    replaced by a different statement.

    Reviewed-By: Eli Zaretskii <eliz@gnu.org>
    Approved-By: Tom Tromey <tom@tromey.com>

.. which added the feature
Comment 2 Christopher Fore 2024-01-18 22:33:40 UTC
After some trial and error I discovered that the root cause seems to be the `list .` command does not properly function when a program is compiled without debugging symbols. When you compile the test program with `-ggdb3` it functions normally.
Comment 3 Guinevere Larsen 2024-01-19 08:39:10 UTC
I just reproduced it with upstream GDB, and see it is still there.

Sorry, when I was developing the "list ." command, I never
Comment 4 Guinevere Larsen 2024-01-19 08:44:02 UTC
(...) I never tested with an inferior without debuginfo.

IMO, the whole "list" command doesn't handle missing debug information all too well. I'll try to tackle both and fix this as soon as possible.

PS: sorry for double updates, it was a misclick
Comment 5 Guinevere Larsen 2024-01-23 15:19:55 UTC
I have an upstream patch already for this issue here[1]. This should be fixed as soon as people look/approve it.

[1] https://inbox.sourceware.org/gdb-patches/20240123105043.39408-1-blarsen@redhat.com/T/#u
Comment 6 Christopher Fore 2024-01-23 15:43:17 UTC
Thank you! Just tested on head and 14.1 and both worked.
Comment 7 Sourceware Commits 2024-01-23 15:59:16 UTC
The master branch has been updated by Guinevere Larsen <blarsen@sourceware.org>:

https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=0068bd6fb3579dd8df7561e038cb3fe27f122b0e

commit 0068bd6fb3579dd8df7561e038cb3fe27f122b0e
Author: Guinevere Larsen <blarsen@redhat.com>
Date:   Mon Jan 22 10:13:52 2024 +0100

    gdb: fix "list ." related crash
    
    When a user attempts to use the "list ." command with an inferior that
    doesn't have debug symbols, GDB would crash. This was reported as PR
    gdb/31256.
    
    The crash would happen when attempting to get the current symtab_and_line
    for the stop location, because the symtab would return a null pointer
    and we'd attempt to dereference it to print the line.
    
    This commit fixes that by checking for an empty symtab and erroring out
    of the function if it happens.
    
    Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=31256
    Approved-By: Tom Tromey <tom@tromey.com>
Comment 8 Guinevere Larsen 2024-01-23 16:10:52 UTC
The commit above fixes the issue. It will also be backported to the gdb-14 branch.
Comment 9 Sourceware Commits 2024-01-23 16:25:48 UTC
The gdb-14-branch branch has been updated by Guinevere Larsen <blarsen@sourceware.org>:

https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=4fe1a40c7c464bde0212aa859774fe59fb71ccfe

commit 4fe1a40c7c464bde0212aa859774fe59fb71ccfe
Author: Guinevere Larsen <blarsen@redhat.com>
Date:   Mon Jan 22 10:13:52 2024 +0100

    gdb: fix "list ." related crash
    
    When a user attempts to use the "list ." command with an inferior that
    doesn't have debug symbols, GDB would crash. This was reported as PR
    gdb/31256.
    
    The crash would happen when attempting to get the current symtab_and_line
    for the stop location, because the symtab would return a null pointer
    and we'd attempt to dereference it to print the line.
    
    This commit fixes that by checking for an empty symtab and erroring out
    of the function if it happens.
    
    Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=31256
    Approved-By: Tom Tromey <tom@tromey.com>
Comment 10 Joel Brobecker 2024-02-11 04:42:36 UTC
Added the 14.2 "target milestone" in order for our release scripts to know that the fix was included in that release too.