Segmentation fault when calling standard library functions via `print` or `call` while debugging a `main()` function that accepts arguments

Bug #2061849 reported by Aaron Rainbolt
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
gdb (Ubuntu)
New
Undecided
Unassigned
Jammy
New
Undecided
Unassigned

Bug Description

Steps to reproduce:

1: Create a file `test.c` with the following very simple program:

    #include <stdio.h> /* for printf */
    #include <string.h> /* for strlen */

    int main(int argc, char **argv) {
        const char *statstr = "hello there!";
        printf("%s\n", statstr);
    }

2: Save this file and compile it with `gcc -g test.c`.
3: Debug it with `gdb a.out`.
4: Run `b test.c:6` to set a breakpoint on the printf call.
5: Run `r` to start the program.
6: Run `print strlen(statstr)` to attempt to get the length of the statstr string.

Expected result: The length of the string should be printed like so: `$1 = 12`

Actual result: gdb segfaults.

Segfault backtrace and info:

```
(gdb) print strlen(statstr)

Fatal signal: Segmentation fault
----- Backtrace -----
0x5ac45237f077 ???
0x5ac452481859 ???
0x5ac452481a22 ???
0x72ec6204251f ???
        ./signal/../sysdeps/unix/sysv/linux/x86_64/libc_sigaction.c:0
0x5ac45232f020 ???
0x5ac4524d6637 ???
0x5ac45247f24a ???
0x5ac45247b827 ???
0x5ac45259e79f ???
0x5ac45259ecef ???
0x5ac4523b4654 ???
0x5ac4526aa394 ???
0x5ac4524829a4 ???
0x5ac452482d43 ???
0x5ac4524834c6 ???
0x72ec633a7e0d ???
0x5ac452481a95 ???
0x5ac452483374 ???
0x5ac45248168b ???
0x5ac452833815 ???
0x5ac452833caa ???
0x5ac45253f36c ???
0x5ac452541054 ???
0x5ac4522d715f ???
0x72ec62029d8f __libc_start_call_main
        ../sysdeps/nptl/libc_start_call_main.h:58
0x72ec62029e3f __libc_start_main_impl
        ../csu/libc-start.c:392
0x5ac4522dcbf4 ???
0xffffffffffffffff ???
---------------------
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://www.gnu.org/software/gdb/bugs/>.

Segmentation fault (core dumped)
```

Additional info:

* You can create a function inside `test.c` that accepts a string as input and returns the result of running `strlen` on it. Calling that function via `print len(statstr)` does NOT segfault, but behaves as expected.
* If you remove the `int argc, char **argv` from the `main()` function's argument list, calling `strlen(statstr)` in step 6 does NOT segfault, but behaves as expected.

ProblemType: Bug
DistroRelease: Ubuntu 22.04
Package: gdb 12.1-0ubuntu1~22.04
ProcVersionSignature: Ubuntu 6.5.0-25.25~22.04.1-generic 6.5.13
Uname: Linux 6.5.0-25-generic x86_64
ApportVersion: 2.20.11-0ubuntu82.5
Architecture: amd64
CasperMD5CheckResult: unknown
CurrentDesktop: KDE
Date: Tue Apr 16 09:51:33 2024
InstallationDate: Installed on 2024-03-30 (17 days ago)
InstallationMedia: Kubuntu 22.04.3 LTS "Jammy Jellyfish" (20231113)
SourcePackage: gdb
UpgradeStatus: No upgrade log present (probably fresh install)

Revision history for this message
Aaron Rainbolt (arraybolt3) wrote :
Revision history for this message
Aaron Rainbolt (arraybolt3) wrote (last edit ):

This does NOT occur on Noble. Trying it there, the following happens (note that I allowed debuginfod to be used but I'm not sure if that makes any difference):

```
(gdb) print strlen(statstr)
'strlen' has unknown return type; cast the call to its declared return type
(gdb) print (size_t)strlen(statstr)
$1 = 12
```

However, attempting to use `print (size_t)strlen(statstr)` under Jammy still segfaults.

To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.