0

I want to debug with stander library in gdb. I ran gdb with argument -d to specify the source file of standard libary.

$ gdb ./test -d /usr/src/glibc/glibc-2.27/

test.cpp:

#include<iostream>
#include<string>

using namespace std;

int main(){
    string hex("0x0010");
    long hex_number = strtol(hex.c_str(), NULL, 16);
    cout << hex_number << endl;
    return 0;
}

However, gdb told me it can not find source file strtol.c.

(gdb) b 8                                                                                                                                                                                     
Breakpoint 1 at 0xc8c: file /home/purin/Desktop/CodeWork/adv_unix_programming/hw1/test.cpp, line 8.                                                                                           
(gdb) run                                                                                                                                                                                     
Starting program: /home/purin/Desktop/CodeWork/adv_unix_programming/hw1/test                                                                                                                  

Breakpoint 1, main () at /home/purin/Desktop/CodeWork/adv_unix_programming/hw1/test.cpp:8
8           long hex_number = strtol(hex.c_str(), NULL, 16);
(gdb) s
__strtol (nptr=0x7fffffffd820 "0x0010", endptr=0x0, base=16) at ../stdlib/strtol.c:106
106     ../stdlib/strtol.c: file or directory does not exits
(gdb) info source
Current source file is ../stdlib/strtol.c
Compilation directory is /build/glibc-OTsEL5/glibc-2.27/stdlib
Source language is c.
Producer is GNU C11 7.3.0 -mtune=generic -march=x86-64 -g -O2 -O3 -std=gnu11 -fgnu89-inline -fmerge-all-constants -frounding-math -fstack-protector-strong -fPIC -ftls-model=initial-exec -fstack-protector-strong.
Compiled with DWARF 2 debugging format.
Does not include preprocessor macro info.

Since gdb can find source code if I execute this command:

(gdb) dir /usr/src/glibc/glibc-2.27/stdlib/

I am sure that I have strtol.c in path /usr/src/glibc/glibc-2.27/stdlib/strtol.c and the library has debug information.

Why gdb cannot search the directories under /usr/src/glibc/glibc-2.27/ to find out stdlib/strtol.c?

CodePurin
  • 183
  • 6
  • See if this works (from [GDB complaining about missing raise.c](https://stackoverflow.com/a/48287761/2554472)): `set substitute-path /build/glibc-OTsEL5/glibc-2.27 /usr/src/glibc/glibc-2.27` – Mark Plotnick Mar 26 '20 at 17:54

1 Answers1

0

Maybe the reason is that if you join two paths /usr/src/glibc/glibc-2.27/ and ../stdlib/strtol.c you get the path /usr/src/glibc/stdlib/strtol.c, but not the expected path /usr/src/glibc/glibc-2.27/stdlib/strtol.c. The reason why ../stdlib/strtol.c is stored in the debug info, very likely is a build directory /build/glibc-OTsEL5/glibc-2.27/build used, and ../stdlib/strtol.c is the relative path to the build directory.

One of solutions is run

$ gdb ./test -d /usr/src/glibc/glibc-2.27/stdlib/
273K
  • 29,503
  • 10
  • 41
  • 64