public inbox for gdb-prs@sourceware.org
help / color / mirror / Atom feed
From: "scrouthtv at gmail dot com" <sourceware-bugzilla@sourceware.org>
To: gdb-prs@sourceware.org
Subject: [Bug gdb/27118] New: gdb off by a line when debugging a nasm application
Date: Sun, 27 Dec 2020 17:29:19 +0000	[thread overview]
Message-ID: <bug-27118-4717@http.sourceware.org/bugzilla/> (raw)

https://sourceware.org/bugzilla/show_bug.cgi?id=27118

            Bug ID: 27118
           Summary: gdb off by a line when debugging a nasm application
           Product: gdb
           Version: 10.1
            Status: UNCONFIRMED
          Severity: minor
          Priority: P2
         Component: gdb
          Assignee: unassigned at sourceware dot org
          Reporter: scrouthtv at gmail dot com
  Target Milestone: ---

I just encountered a weird issue with `gdb`, though I'm not sure, if I'm
missing something.
Let's suppose I have these two files:

`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`:
```
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 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. The next line
to be executed would be 5, which is `mov ebx, 5`.
**However**, the last line should've been `mov eax, 5` which should have
already been executed. 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).
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:
```
(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.
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`

-- 
You are receiving this mail because:
You are on the CC list for the bug.

             reply	other threads:[~2020-12-27 17:29 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-12-27 17:29 scrouthtv at gmail dot com [this message]
2020-12-27 17:40 ` [Bug gdb/27118] " scrouthtv at gmail dot com
2020-12-27 17:41 ` scrouthtv at gmail dot com
2020-12-27 17:42 ` scrouthtv at gmail dot com
2020-12-27 17:46 ` schwab@linux-m68k.org
2020-12-27 17:53 ` scrouthtv at gmail dot com

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bug-27118-4717@http.sourceware.org/bugzilla/ \
    --to=sourceware-bugzilla@sourceware.org \
    --cc=gdb-prs@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).