使断点在将来共享库加载时挂起? (y或[n])

时间:2019-01-12 16:45:53

标签: c++ linux gdb

这是我的代码和操作,我想一步一步地调试我的代码(以下代码只是一个例子) main.cpp

#include <iostream>
#include <string>

extern int addd(int ,int);

int main()
{
    std::string str = "Hello";

    std::cout << str << std::endl;

    int a = 10,b = 20;

    std::cout << a + b << std::endl;

    return 0;
}

opr.cpp

int add(int a,int b)
{
    return a + b;
}

我使用模板生成文件格式makefiletemplate只是在执行g ++命令时修改了一些不常见的内容:

[root@centos-linux-10 52coder]# make
g++ -std=c++11 -g   -O3 -Wall -Wextra      -c opr.cpp -o opr.o
g++ -std=c++11 -g   -O3 -Wall -Wextra      -c main.cpp -o main.o
g++ -std=c++11 -g   -O3 -Wall -Wextra         ./opr.o ./main.o  -Wl,--gc-sections -Wl,--strip-all   -o torun
Type ./torun to execute the program.

我使用-O3 2 1,都弄错了。 我只想逐步调试函数主代码,就像这样:

[root@centos-linux-10 52coder]# gdb torun 
GNU gdb (GDB) Red Hat Enterprise Linux 7.6.1-110.el7
Copyright (C) 2013 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-redhat-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from /root/52coder/patchtool...(no debugging symbols found)...done.
(gdb) b main
Function "main" not defined.
Make breakpoint pending on future shared library load? (y or [n]) n
(gdb) b /root/52coder.cpp:8
No symbol table is loaded.  Use the "file" command.
Make breakpoint pending on future shared library load? (y or [n]) n
(gdb) show directories
Source directories searched: $cdir:$cwd

请允许我告诉我该问题该怎么办,

2 个答案:

答案 0 :(得分:3)

您无法(轻松)调试stripped可执行文件。因为GDB调试器需要其中的DWARF调试信息。

因此只需将您的代码链接:

 g++ -std=c++11 -g   -O3 -Wall -Wextra ./opr.o ./main.o  -o torun

使用较少的compiler optimizations(例如,-O0-Og-O1,而不是-O3),您可能会发现调试程序更容易。

PS。有一些方法可以将调试信息放入不同文件中,但这是一个不同的问题(特定于Linux)。

答案 1 :(得分:2)

您的源文件未称为/root/52coder.cpp,因此GDB不会将其识别为在调试程序期间自动加载的源文件也就不足为奇了。

我猜你是说b /root/52coder/main.cpp:8还是b main.cpp:8

您还需要摆脱Basile所描述的符号剥离,否则即使使用正确的文件名,调试器也将根本无法使用您的源代码。