Linux(64 位)、nasm 和 gdb

发布于 2024-12-05 08:39:38 字数 603 浏览 1 评论 0原文

我正在搜索其他线程但没有运气。 我的问题也许很简单但令人沮丧。 我正在 64 位 Ubuntu 11.04 上编译两个文件:

  1. nasm -f elf64 -g file64.asm
  2. gcc -g -o file file.c file64.o

然后我使用 gdb 调试生成的可执行文件。 有了C,一切就OK了。 但是,在调试汇编时,源代码对调试器“不可见”。我得到以下输出:

(gdb) step
Single stepping until exit from function line,
which has no line number information.
0x0000000000400962 in convert ()

快速调查:

objdump --source file64.o

显示程序集源代码(和行信息)包含在文件中。

为什么我在调试会话中看不到它?我做错了什么? 这些问题是在迁移到 64 位 Ubuntu 后出现的。在 32 位 Linux 中它可以工作(正如它应该的那样)。

I was searching other threads without luck.
My problem is perhaps simple but frustrating.
I'm compiling two files on 64-bit Ubuntu 11.04:

  1. nasm -f elf64 -g file64.asm
  2. gcc -g -o file file.c file64.o

Then I debug the resulting executables with gdb.
With C, everything is OK.
However, when debugging assembly, the source code is "not visible" to the debugger. I'm getting the following output:

(gdb) step
Single stepping until exit from function line,
which has no line number information.
0x0000000000400962 in convert ()

A quick investigation with:

objdump --source file64.o

shows that the assembly source code (and line information) is contained in the file.

Why can't I see it in a debug session? What am I doing wrong?
These problems arose after moving to 64-bit Ubuntu. In the 32-bit Linux it worked (as it should).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

红颜悴 2024-12-12 08:39:38

借助 NASM,我在使用 dwarf 调试格式时在 gdb 中获得了更好的体验。然后,gdb 将汇编源代码视为任何其他语言(即,不需要反汇编命令)

nasm -f elf64 -g -F dwarf file64.asm

(版本 2.03.01 及更高版本会自动启用 -g,如果 -F 为指定。)

我正在使用 NASM 版本 2.10.07。我不确定这是否有影响。

With NASM, I've had much better experience in gdb when using the dwarf debugging format. gdb then treats the assembly source as if it were any other language (i.e., no disassemble commands necessary)

nasm -f elf64 -g -F dwarf file64.asm

(Versions 2.03.01 and later automatically enable -g if -F is specified.)

I'm using NASM version 2.10.07. I'm not sure if that makes a difference or not.

扭转时空 2024-12-12 08:39:38

GDB 是一个源代码级(或符号)调试器,这意味着它应该与“高级编程语言”一起工作......但事实并非如此!

但是等一下,因为从调试器的角度来看,调试 ASM 程序比高级语言容易得多:几乎没有什么可做的!程序二进制文件总是包含汇编指令,只是以机器格式编写,而不是 ascii 格式。

GDB 可以为您转换它。不要执行 list 来查看代码,而是使用 disassemble 来查看函数代码:

(gdb) disassemble <your symbol>
Dump of assembler code for function <your symbol>:
   0x000000000040051e <+0>: push   %rbp
   0x000000000040051f <+1>: mov    %rsp,%rbp
=> 0x0000000000400522 <+4>: mov    0x20042f(%rip),%rax        
   0x0000000000400529 <+11>:    mov    %rax,%rdx
   0x000000000040052c <+14>:    mov    $0x400678,%eax
   0x0000000000400531 <+19>:    mov    %rdx,%rcx

或使用 x/5i $pc 来查看 5 i 在您的 $pc 指令之后

(gdb) x/5i $pc
=> 0x400522 <main+4>:   mov    0x20042f(%rip),%rax
   0x400529 <main+11>:  mov    %rax,%rdx
   0x40052c <main+14>:  mov    $0x400678,%eax
   0x400531 <main+19>:  mov    %rdx,%rcx
   0x400534 <main+22>:  mov    $0xc,%edx

使用 stepi (si) 代替 stepnexti (ni) 而不是 next

display $pc 也可用于在下级停止时打印当前的 pc(即,在每个 nexti/stepi 之后)代码>.

GDB is a source-level (or symbolic) debugger, which means that it's supposed to work with 'high-level programming languages' ... which is not you're case!

But wait a second, because, from a debugger's point of view, debugging ASM programs is way easier than higher level languages: there's almost nothing to do! The program binary always contains the assembly instruction, there're just written in their machine format, instead of ascii format.

And GDB has the ability to convert it for you. Instead of executing list to see the code, use disassemble to see a function code:

(gdb) disassemble <your symbol>
Dump of assembler code for function <your symbol>:
   0x000000000040051e <+0>: push   %rbp
   0x000000000040051f <+1>: mov    %rsp,%rbp
=> 0x0000000000400522 <+4>: mov    0x20042f(%rip),%rax        
   0x0000000000400529 <+11>:    mov    %rax,%rdx
   0x000000000040052c <+14>:    mov    $0x400678,%eax
   0x0000000000400531 <+19>:    mov    %rdx,%rcx

or x/5i $pc to see 5 i nstruction after your $pc

(gdb) x/5i $pc
=> 0x400522 <main+4>:   mov    0x20042f(%rip),%rax
   0x400529 <main+11>:  mov    %rax,%rdx
   0x40052c <main+14>:  mov    $0x400678,%eax
   0x400531 <main+19>:  mov    %rdx,%rcx
   0x400534 <main+22>:  mov    $0xc,%edx

then use stepi (si) instread of step and nexti (ni) instead of next.

display $pc could also be useful to print the current pc whenever the inferior stops (ie, after each nexti/stepi.

浅语花开 2024-12-12 08:39:38

对于那些在 NASM 上遇到问题的人(该错误到目前为止尚未修复):只需下载 NASM git 存储库并切换到版本 2.7,这可能是最后一个运行良好的版本,即支持 gdb。从源代码构建这个过时的版本只是一种解决方法(例如,您不支持最新的 ISA),但对于大多数学生来说已经足够了。

For anyone else stuck with the broken things on NASM (the bug is not fixed so far): just download the NASM git repository and switch to version 2.7, which is probably the last version that works fine, i.e. supports gdb. Building from source this outdated version is only a workaround (you don't have support for the last ISA for example), but it's sufficient for most students.

单挑你×的.吻 2024-12-12 08:39:38

GDB 可能不知道在哪里搜索源文件。尝试用 directory 明确地告诉它。

GDB might not know where to search for your source files. Try to explicitly tell it with directory.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文