linuxassemblygdbx86-64yasm

Why is yasm generating incorrect debugging information?


I have an x86_64 assembly program I'm trying to debug on Linux, but when I try to use gdb, it skips around randomly and loops through the same couple instructions or repeats instructions. It also seems to loop through different instructions depending on where I set a breakpoint.

I tried researching this problem online, and I saw a number of people having this same issue with C++ when compilers were optimizing too aggressively and generating incorrect debugging information. I didn't see anything about assembly, but I believe yasm might be the problem here as well.

Here's my Makefile.

myprog : myprog.o
    gcc -static -fdwarf2-cfi-asm myprog.o -o myprog
myprog.o : myprog.asm
    yasm -f elf64 -g dwarf2 myprog.asm -o myprog.o

Note that I'm statically linking because I can't get dynamic linking to work. I might ask a separate question about that in the future.

And here's more or less what the gdb session looks like.

...
(gdb)@ n
65  call findrepl
(gdb)@ n
73  mov rdi, str3
(gdb)@ n
75  call findrepl
(gdb)@ n
75  call findrepl
(gdb)@ n
65  call findrepl
...

Using s, si, or ni all do the same as n shown above. I haven't had this issue with my previous assembly programs. Sometimes gdb will say that I've executed something like xor eax, eax but then show the output from a call I made to printf.

I'm relatively new to assembly programming and gdb, so in the back of my mind I wonder if this is my fault. Is there a way to fix this? I would also like to know if there are any workarounds, as I can't seem to debug it without using gdb.


Solution

  • After much trial and error, I discovered the '-tui' option for gdb and the 'layout asm' command. There's probably a better way to do this, but here's basically what you type in to make it work:

    [user@comp ~/prog/]$ gdb -tui myprog
    (gdb)@ layout asm
    (gdb)@ break main
    (gdb)@ run
    

    And then you can go wild. By default, it's disassembled into AT&T notation. You can check which notation it's currently displaying with show disassembly-flavor. You can change it with set disassembly-flavor intel or set disassembly-flavor att.

    For more information, run help tui, help layout, or help set disassembly-flavor in gdb.