$ gcc --version gcc (GCC) 10.0.0 20190517 (experimental) Copyright (C) 2019 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. $ gdb --version GNU gdb (Ubuntu 8.1-0ubuntu3) 8.1.0.20180409-git Copyright (C) 2018 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later $ cat small.c #include <stdio.h> int main(int argc, char **argv) { if (argc == 0) { int *ptr; label: { } } if (argc == 1) { printf("hello\n"); } return 0; } $ gcc -g small.c; ./a.out hello $ gdb -batch -x cmds a.out Breakpoint 1 at 0x400501: file small.c, line 8. Breakpoint 1, main (argc=1, argv=0x7fffffffde58) at small.c:8 8 label: ptr = <optimized out> Kill the program being debugged? (y or n) [answered Y; input not from terminal] $ cat cmds b 8 r info locals kill q Line 8 in the body of the "if (argc==0)" is not executed according to the program output. Thus, when we set breakpoint in Line #8, gdb should not stop. However, in this case, it stopped and print something. Thus, I was wondering this should be a bug in gdb.
This problem is still exist in latest development version of gdb $ gdb -v GNU gdb (GDB) 9.0.50.20191210-git Copyright (C) 2019 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. $ gdb ./a.out -q Reading symbols from ./a.out... (gdb) break 8 Breakpoint 1 at 0x1148: file small.c, line 8. (gdb) run Starting program: /home/yibiao/tgdb/a.out Breakpoint 1, main (argc=1, argv=0x7fffffffe008) at small.c:8 8 label: (gdb)
This problem is no longer exist in latest development version of gdb $ gdb --version GNU gdb (GDB) 11.0.50.20201224-git Copyright (C) 2020 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.
Not sure whether this one is fixed in the latest version.
You're probably looking at a bug in debug info generation. I can reproduce this with gcc 9.3.1, and not anymore with gcc 10.3.0. GDB just reads the line info as generated by the compiler and uses it, I don't see anything wrong with what GDB does.