普通程序中的GDB断点不会触发

时间:2018-12-20 23:12:46

标签: c gcc gdb

我正在将我的gdb构建更新到更高版本,并且使用普通程序进行的测试(令人惊讶地)失败了。

#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会话

[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)

您可以看到,我放在第8行从不的断点被触发。是什么赋予了?几乎就像符号表与实际堆栈或其他内容不同步。

任何想法都可能需要设置哪些标志?如果我使用的是CentOS 6的旧版gdb,但无法断点为何新的版本会有所不同,则可以使用断点。

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版本信息:     gcc(GCC)7.3.1 20180303(Red Hat 7.3.1-5)

在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

在main处设置断点不会触发,但输出会(以某种方式)变得更奇怪:

[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]

在正确的地址处设置断点的工作原理,但是在继续操作时会触发SIGILL:

[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.

main 处的断点甚至导致更奇怪的行为:

(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]

0 个答案:

没有答案