[英]GDB is off by a line when debugging a nasm application
I just encountered a weird issue with gdb
, though I'm not sure, if I'm missing something.我刚刚遇到了
gdb
的一个奇怪问题,但我不确定是否遗漏了什么。 Let's suppose I have these two files:假设我有这两个文件:
somefile.asm
: somefile.asm
:
01 section .text
02
03 funca:
04 mov eax, 5
05 mov ebx, 5
06 cmp eax, ebx
07 je aisequal
08 mov ecx, 13
09 mov edx, 19
10 ret
11
12 aisequal:
13 mov ecx, 17
14 mov edx, 21
15 ret
and somefile_test.asm
:和
somefile_test.asm
:
01 %include "somefile.asm"
02
03 section .text
04 global _start
05
06 _start:
07 xor eax, eax
08 xor ebx, ebx
09 xor ecx, ecx
10 xor edx, edx
11 call funca
12
13 mov eax, 1
14 mov ebx, 0
15 int 0x80
I compile and link it using我编译并链接它使用
nasm -f elf -g -F dwarf somefile_test.asm
ld -m elf_i386 -o somefile_test.out somefile_test.o
And then debug my application using gdb
:然后使用
gdb
调试我的应用程序:
gdb somefile_test.out
I now set a breakpoint in the imported file:我现在在导入的文件中设置断点:
GNU gdb (GDB) 10.1
(gdb) b somefile.asm:5
Breakpoint 1 at 0x8049000: file somefile.asm, line 5.
(gdb) r
Starting program: /<bla>/somefile_test.out
Breakpoint , funca () at somefile.asm:5
5 mov ebx, 5
Now appearantly, the execution stopped at the correct position.现在看来,执行在正确的 position 处停止。 The next line to be executed would be 5, which is
mov ebx, 5
.要执行的下一行是 5,即
mov ebx, 5
。 However , the last line should've been mov eax, 5
which should have already been executed.但是,最后一行应该是
mov eax, 5
应该已经执行了。 It was not:不是:
(gdb) i r eax
eax 0x0 0
It gets even weirder:它变得更加奇怪:
(gdb) si
6 cmp eax, ebx
(gdb) i r eax ebx
eax 0x5 5
ebx 0x0 0
Now, eax
is set, but ebx
is not (yet).现在,
eax
已设置,但ebx
尚未设置。 If I execute the next line, it is set:如果我执行下一行,则设置为:
(gdb) si
7 je aisequal
(gdb) i r eax ebx
eax 0x5 5
ebx 0x5 5
However, I'd expect the program to jump to line 12 (aisequal) now, but it doesn't:但是,我希望程序现在跳转到第 12 行(aisequal),但它没有:
(gdb) si
8 mov ecx, 13
On the next instruction, it suddenly goes to the right line:在下一条指令中,它突然转到正确的行:
(gdb) si
14 mov edx, 21
(gdb) i r eax ebx edx
eax 0x5 5
ebx 0x5 5
edx 0x0 0
And so on:等等:
(gdb) si
15 ret
(gdb) i r eax ebx ecx edx
eax 0x5 5
ebx 0x5 5
ecx 0x11 17
edx 0x0 0
If I put all my code in a single file, everything works as expected:如果我将所有代码放在一个文件中,一切都会按预期工作:
01 section .text
02 global _start
03
04 _start:
05 xor eax, eax
06 xor ebx, ebx
07 xor ecx, ecx
08 xor edx, edx
09 call funca
10
11 mov eax, 1
12 mov ebx, 0
13 int 0x80
14
15 funca:
16 mov eax, 5
17 mov ebx, 5
18 cmp eax, ebx
19 je aisequal
20 mov ecx, 13
21 mov edx, 19
22 ret
23
24 aisequal:
25 mov ecx, 17
26 mov edx, 21
27 ret
GBU gdb (GDB) 10.1
(gdb) b 16
Breakpoint 1 at 0x8049019: file singlefile.asm, line 16.
(gdb) r
Starting program: /<bla>/singlefile.out
Breakpoint 1, funca () at singlefile.asm:16
16 mov eax, 5
(gdb) i r eax ebx ecx edx
eax 0x0 0
ebx 0x0 0
ecx 0x0 0
edx 0x0 0
(gdb) si
17 mov ebx, 5
(gdb) i r eax ebx ecx edx
eax 0x5 5
ebx 0x0 0
ecx 0x0 0
edx 0x0 0
(gdb) si
18 cmp eax, ebx
(gdb) si
19 je aisequal
(gdb) si
25 mov ecx, 17
(gdb) si
26 mov edx, 21
(gdb) i r eax ebx ecx edx
eax 0x5 5
ebx 0x5 5
ecx 0x11 17
edx 0x0 0
(gdb) si
aisequal () at singlefile.asm:27
27 ret
(gdb) i r eax ebx ecx edx
eax 0x5 5
ebx 0x5 5
ecx 0x11 17
edx 0x15 21
(gdb) si
_start () at singlefile.asm:11
11 mov eax, 1
Now I've only picked up gdb
two days ago, so I'm not that familiar with it.现在两天前才拿到
gdb
,所以不是很熟悉。 Can someone explain to me what's happening?有人可以向我解释发生了什么吗? Is this a bug or am I missing something?
这是一个错误还是我错过了什么?
I am using我在用
nasm 2.15.05-1
binutils 2.35.1-1
gdb 10.1-4
gcc 10.2.0-4
on Linux 5.9.14-arch1-1 #1 SMP PREEMPT Sat, 12 Dec 2020 14:37:12 +0000 x86_64 GNU/Linux
在
Linux 5.9.14-arch1-1 #1 SMP PREEMPT Sat, 12 Dec 2020 14:37:12 +0000 x86_64 GNU/Linux
Apparently, this is a regression in nasm.显然,这是 nasm 的回归。
This looks like a bug in nasm
.这看起来像
nasm
中的错误。 It didn't reproduce for me using nasm-2.14
.它没有为我使用
nasm-2.14
重现。
GDB will only display source info that the compiler/assembler tells it. GDB 只会显示编译器/汇编器告诉它的源信息。 If the assembler puts out incorrect info, then GDB will display that incorrect info and can't do anything about it.
如果汇编器输出了不正确的信息,那么 GDB 将显示不正确的信息并且不能做任何事情。
To verify that the problem is in nasm
, run objdump -dS somefile_test.o
and compare the assembly and source listing.要验证问题出在
nasm
中,请运行objdump -dS somefile_test.o
并比较程序集和源代码列表。 If they are also off by one, it's a bug in nasm
.如果它们也相差一个,那是
nasm
中的一个错误。
Here is what I see:这是我看到的:
somefile_test.o: file format elf32-i386
Disassembly of section .text:
00000000 <funca>:
section .text
funca:
mov eax, 5
0: b8 05 00 00 00 mov $0x5,%eax
mov ebx, 5
5: bb 05 00 00 00 mov $0x5,%ebx
cmp eax, ebx
a: 39 d8 cmp %ebx,%eax
je aisequal
c: 74 0b je 19 <aisequal>
Note how instructions and source perfectly line up.请注意说明和源代码是如何完美排列的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.