I am working on a project and it seems I am unable to debug my code. First I thought it was a configuration error in my IDE (Eclipse), but then it turned out it is not working at all, not even with gdb for such a single program like below.
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]
Do you have any idea what goes wrong here? Why am I unable to see the source line where I put the breakpoint? Why won't gdb show the source line being run when using step? Also why would it exit the program at the second step command? It should still be at b=c+3 line!
I have checked and the debug symbols are indeed in the executable.
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
I have gcc 4.8.1 and gdb 7.4-2012.04.
What you observe pretty clearly appears to be a bug in GDB:
(gdb) b test.c:5
Breakpoint 1 at 0x4004e9: file test.c, line 5.
Here GDB clearly knows that debug symbols are present, and that address 0x4004e9 corresponds to line 5 of test.cc. But when the breakpoint is actually hit:
Breakpoint 1, 0x00000000004004e9 in main ()
somehow GDB forgot what it knew just moments ago (assuming you didn't replace ./test between setting the breakpoint and running the binary).
Since gdb 7.4-2012.04 is pretty old, the first thing to do is try to update it (perhaps build gdb-7.6 from source) and see if the problem persists.
If it does, file a bug in GDB bugzilla, and attach your binary to that bug.