【发布时间】:2014-01-12 21:09:14
【问题描述】:
我正在处理一个项目,但我似乎无法调试我的代码。首先,我认为这是我的 IDE(Eclipse)中的配置错误, 但后来发现它根本不起作用,甚至对于像下面这样的单个程序的 gdb 也不行。
test.c
void main() {
int a=1;
int b=2;
int c=3;
a=b+2; // line 5: breakpoint is set here
c=a+b;
b=c+3;
return;
}
user@mycomputer:/home/user/test$ gcc -g -O0 -c test.c
user@mycomputer:/home/user/test$ gcc -g -O0 test.o -o test
user@mycomputer:/home/user/test$ gdb test
GNU gdb (Ubuntu/Linaro 7.4-2012.04-0ubuntu2.1) 7.4-2012.04
Copyright (C) 2012 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-linux-gnu".
For bug reporting instructions, please see:
<http://bugs.launchpad.net/gdb-linaro/>...
Reading symbols from /home/user/test/test...done.
(gdb) b test.c:5
Breakpoint 1 at 0x4004e9: file test.c, line 5.
(gdb) run
Starting program: /home/user/test/test
warning: no loadable sections found in added symbol-file system-supplied DSO at 0x7ffff7ffa000
Breakpoint 1, 0x00000000004004e9 in main ()
(gdb) step
Single stepping until exit from function main,
which has no line number information.
0x00007ffff7a3b76d in __libc_start_main () from /lib/x86_64-linux-gnu/libc.so.6
(gdb) step
Single stepping until exit from function __libc_start_main,
which has no line number information.
[Inferior 1 (process 3306) exited with code 011]
你知道这里出了什么问题吗?为什么我看不到放置断点的源代码行? 为什么使用 step 时 gdb 不显示正在运行的源代码行? 另外为什么它会在第二步命令中退出程序?它应该还在 b=c+3 行!
我已经检查过了,调试符号确实在可执行文件中。
user@mycomputer:/home/user/test$ file test
test: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.24, BuildID[sha1]=0x37bb8d43d3a8394ce3bb9031e1e090d6c6d5aea7, not stripped
我有 gcc 4.8.1 和 gdb 7.4-2012.04。
【问题讨论】:
-
您是否真的在启用调试信息的情况下进行编译?我的意思是,您是如何检查调试符号是否在可执行文件中的?
-
顺便说一句,
void main()是不合法的 C. -
添加
printf语句以打印出您的变量 - 即使使用-O0,您的虚拟代码也可能会被优化掉。