I'm working on updating my build of gdb to a later version, and testing using a trivial program is (surprisingly) failing.
#include <stdio.h>
int main()
{
int a;
for ( a = 10; a < 20; a = a + 1) {
printf("value of a: %d\n", a);
printf("another value of a: %d\n", a);
}
return 0;
}
GDB session
[root@localhost ~]# gcc -g main.c
[root@localhost ~]# gdb a.out
GNU gdb (GDB) 7.12.1
Copyright (C) 2017 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".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from a.out...done.
(gdb) l
1 #include <stdio.h>
2
3 int main()
4 {
5 int a;
6 for ( a = 10; a < 20; a = a + 1) {
7 printf("value of a: %d\n", a);
8 printf("another value of a: %d\n", a);
9 }
10 return 0;
(gdb) b 8
Breakpoint 1 at 0x4004ed: file main.c, line 8.
(gdb) r
Starting program: /root/a.out
value of a: 10
another value of a: 15
value of a: 11
another value of a: 15
value of a: 12
another value of a: 15
value of a: 13
another value of a: 15
value of a: 14
another value of a: 15
value of a: 15
another value of a: 15
value of a: 16
another value of a: 15
value of a: 17
another value of a: 15
value of a: 18
another value of a: 15
value of a: 19
another value of a: 15
[Inferior 1 (process 6083) exited normally]
(gdb)
As you can tell, the breakpoint I place on line 8 never gets triggered. What gives? It's almost like the symbol table is out of sync with the actual stack or something.
Any idea what flags might need to get set? Breakpoints work if I use an older build of gdb from CentOS 6 but unclear why new build would be different.
This GDB was configured as follows:
configure --host=x86_64-redhat-linux-gnu --target=x86_64-redhat-linux-gnu
--with-auto-load-dir=$debugdir:$datadir/auto-load
--with-auto-load-safe-path=$debugdir:$datadir/auto-load
--with-expat
--with-gdb-datadir=/usr/share/gdb (relocatable)
--with-jit-reader-dir=/usr/lib64/gdb (relocatable)
--without-libunwind-ia64
--without-lzma
--with-python=/usr (relocatable)
--without-guile
--with-separate-debug-dir=/usr/lib64/debug (relocatable)
--without-babeltrace
disas main
Dump of assembler code for function main:
0x00000000004004c8 <+0>: push %rbp
0x00000000004004c9 <+1>: mov %rsp,%rbp
0x00000000004004cc <+4>: sub $0x10,%rsp
0x00000000004004d0 <+8>: movl $0xa,-0x4(%rbp)
0x00000000004004d7 <+15>: jmp 0x400505 <main+61>
0x00000000004004d9 <+17>: mov -0x4(%rbp),%eax
0x00000000004004dc <+20>: mov %eax,%esi
0x00000000004004de <+22>: mov $0x4005d8,%edi
0x00000000004004e3 <+27>: mov $0x0,%eax
0x00000000004004e8 <+32>: callq 0x4003b0 <printf@plt>
0x00000000004004ed <+37>: mov -0x4(%rbp),%eax
0x00000000004004f0 <+40>: mov %eax,%esi
0x00000000004004f2 <+42>: mov $0x4005e8,%edi
0x00000000004004f7 <+47>: mov $0x0,%eax
0x00000000004004fc <+52>: callq 0x4003b0 <printf@plt>
0x0000000000400501 <+57>: addl $0x1,-0x4(%rbp)
0x0000000000400505 <+61>: cmpl $0x13,-0x4(%rbp)
0x0000000000400509 <+65>: jle 0x4004d9 <main+17>
0x000000000040050b <+67>: mov $0x0,%eax
0x0000000000400510 <+72>: leaveq
0x0000000000400511 <+73>: retq
End of assembler dump.
objdump
00000000004004c8 <main>:
4004c8: 55 push %rbp
4004c9: 48 89 e5 mov %rsp,%rbp
4004cc: 48 83 ec 10 sub $0x10,%rsp
4004d0: c7 45 fc 0a 00 00 00 movl $0xa,-0x4(%rbp)
4004d7: eb 2c jmp 400505 <main+0x3d>
4004d9: 8b 45 fc mov -0x4(%rbp),%eax
4004dc: 89 c6 mov %eax,%esi
4004de: bf d8 05 40 00 mov $0x4005d8,%edi
4004e3: b8 00 00 00 00 mov $0x0,%eax
4004e8: e8 c3 fe ff ff callq 4003b0 <printf@plt>
4004ed: 8b 45 fc mov -0x4(%rbp),%eax
4004f0: 89 c6 mov %eax,%esi
4004f2: bf e8 05 40 00 mov $0x4005e8,%edi
4004f7: b8 00 00 00 00 mov $0x0,%eax
4004fc: e8 af fe ff ff callq 4003b0 <printf@plt>
400501: 83 45 fc 01 addl $0x1,-0x4(%rbp)
400505: 83 7d fc 13 cmpl $0x13,-0x4(%rbp)
400509: 7e ce jle 4004d9 <main+0x11>
40050b: b8 00 00 00 00 mov $0x0,%eax
400510: c9 leaveq
400511: c3 retq
400512: 90 nop
400513: 90 nop
400514: 90 nop
400515: 90 nop
400516: 90 nop
400517: 90 nop
400518: 90 nop
400519: 90 nop
40051a: 90 nop
40051b: 90 nop
40051c: 90 nop
40051d: 90 nop
40051e: 90 nop
40051f: 90 nop
GCC version info: gcc (GCC) 7.3.1 20180303 (Red Hat 7.3.1-5)
Output outside of gdb:
[root@localhost ~]# ./a.out
value of a: 10
another value of a: 10
value of a: 11
another value of a: 11
value of a: 12
another value of a: 12
value of a: 13
another value of a: 13
value of a: 14
another value of a: 14
value of a: 15
another value of a: 15
value of a: 16
another value of a: 16
value of a: 17
another value of a: 17
value of a: 18
another value of a: 18
value of a: 19
another value of a: 19
Setting a breakpoint at main does not trigger, but output gets (somehow) even weirder:
[root@localhost ~]# gdb a.out
GNU gdb (GDB) 7.12.1
Copyright (C) 2017 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".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from a.out...done.
(gdb) b main
Breakpoint 1 at 0x4004d0: file main.c, line 6.
(gdb) r
Starting program: /root/a.out
value of a: 0
another value of a: 0
value of a: 1
another value of a: 1
value of a: 2
another value of a: 2
value of a: 3
another value of a: 3
value of a: 4
another value of a: 4
value of a: 5
another value of a: 5
value of a: 6
another value of a: 6
value of a: 7
another value of a: 7
value of a: 8
another value of a: 8
value of a: 9
another value of a: 9
value of a: 10
another value of a: 10
value of a: 11
another value of a: 11
value of a: 12
another value of a: 12
value of a: 13
another value of a: 13
value of a: 14
another value of a: 14
value of a: 15
another value of a: 15
value of a: 16
another value of a: 16
value of a: 17
another value of a: 17
value of a: 18
another value of a: 18
value of a: 19
another value of a: 19
[Inferior 1 (process 2484) exited normally]
Setting a breakpoint at the correct address sort of works, but triggers a SIGILL when continuing:
[root@localhost ~]# gdb a.out
GNU gdb (GDB) 7.12.1
Copyright (C) 2017 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".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from a.out...done.
(gdb) disas main
Dump of assembler code for function main:
0x00000000004004c8 <+0>: push %rbp
0x00000000004004c9 <+1>: mov %rsp,%rbp
0x00000000004004cc <+4>: sub $0x10,%rsp
0x00000000004004d0 <+8>: movl $0xa,-0x4(%rbp)
0x00000000004004d7 <+15>: jmp 0x400505 <main+61>
0x00000000004004d9 <+17>: mov -0x4(%rbp),%eax
0x00000000004004dc <+20>: mov %eax,%esi
0x00000000004004de <+22>: mov $0x4005d8,%edi
0x00000000004004e3 <+27>: mov $0x0,%eax
0x00000000004004e8 <+32>: callq 0x4003b0 <printf@plt>
0x00000000004004ed <+37>: mov -0x4(%rbp),%eax
0x00000000004004f0 <+40>: mov %eax,%esi
0x00000000004004f2 <+42>: mov $0x4005e8,%edi
0x00000000004004f7 <+47>: mov $0x0,%eax
0x00000000004004fc <+52>: callq 0x4003b0 <printf@plt>
0x0000000000400501 <+57>: addl $0x1,-0x4(%rbp)
0x0000000000400505 <+61>: cmpl $0x13,-0x4(%rbp)
0x0000000000400509 <+65>: jle 0x4004d9 <main+17>
0x000000000040050b <+67>: mov $0x0,%eax
0x0000000000400510 <+72>: leaveq
0x0000000000400511 <+73>: retq
End of assembler dump.
(gdb) b *0x00000000004004fc
Breakpoint 1 at 0x4004fc: file main.c, line 8.
(gdb) r
Starting program: /root/a.out
value of a: 10
Program received signal SIGILL, Illegal instruction.
0x00000000004004fe in main () at main.c:8
8 printf("another value of a: %d\n", a);
(gdb) print {a}
$1 = {10}
(gdb) c
Continuing.
Program terminated with signal SIGILL, Illegal instruction.
The program no longer exists.
Breakpoints at main cause even weirder behavior:
(gdb) break main
Breakpoint 1 at 0x40113f: file main.c, line 6.
(gdb) run
Starting program: /root/gcc731-updated-binutils-a.out
value of a: 0
another value of a: 0
value of a: 1
another value of a: 1
value of a: 2
another value of a: 2
value of a: 3
another value of a: 3
value of a: 4
another value of a: 4
value of a: 5
another value of a: 5
value of a: 6
another value of a: 6
value of a: 7
another value of a: 7
value of a: 8
another value of a: 8
value of a: 9
another value of a: 9
value of a: 10
another value of a: 10
value of a: 11
another value of a: 11
value of a: 12
another value of a: 12
value of a: 13
another value of a: 13
value of a: 14
another value of a: 14
value of a: 15
another value of a: 15
value of a: 16
another value of a: 16
value of a: 17
another value of a: 17
value of a: 18
another value of a: 18
value of a: 19
another value of a: 19
[Inferior 1 (process 1275) exited normally]