【问题标题】:gdb cannot find source file of libararygdb 找不到库的源文件
【发布时间】:2020-07-06 19:05:57
【问题描述】:

我想使用gdb 中的标准库进行调试。 我用参数-d 运行gdb 来指定标准库的源文件。

$ 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;
}

但是,gdb 告诉我找不到源文件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.

如果我执行这个命令,gdb 可以找到源代码:

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

我确定我在路径/usr/src/glibc/glibc-2.27/stdlib/strtol.c 中有strtol.c,并且库有调试信息。

为什么gdb无法搜索/usr/src/glibc/glibc-2.27/下的目录找到stdlib/strtol.c

【问题讨论】:

标签: c++ c linux debugging gdb


【解决方案1】:

也许原因是如果你加入两条路径/usr/src/glibc/glibc-2.27/../stdlib/strtol.c,你会得到路径/usr/src/glibc/stdlib/strtol.c,但不是预期的路径/usr/src/glibc/glibc-2.27/stdlib/strtol.c../stdlib/strtol.c之所以存放在调试信息中,很可能是使用了构建目录/build/glibc-OTsEL5/glibc-2.27/build,而../stdlib/strtol.c是构建目录的相对路径。

其中一个解决方案正在运行

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

【讨论】:

    猜你喜欢
    • 2015-09-11
    • 2016-03-27
    • 1970-01-01
    • 2013-12-04
    • 2018-04-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多