在 gdb 中执行 instruction/command

Execute an instruction/command in gdb

看起来有一种方法可以通过 compile...code 将 C 代码注入 gdb。

有没有办法在命令行中注入单个指令?例如,类似于:

>>> mov %rax, %rbx
# Undefined command: "mov".  Try "help".

据我所知,这不是一种简单的交互方式;我认为 GDB 的命令解析器没有 assembler built-in,只是命令从内存中删除 assemble 代码。理论上可能是某种外部 assemble + 在某处加载。

您当然可以 set $rbx = $rax 复制特定 mov 的效果。

(GDB 使用 $ 作为 GDB 表达式中寄存器名称的修饰符。)

要使用单个指令并查看它们的作用,最好的办法是将它们放在 .s 文件中并将它们构建为静态可执行文件,然后您可以 single-step 使用 GDB .

$ cat > foo.s
mov %rax, %rbx
(hit control-d here for EOF)
$ gcc -static -nostdlib foo.s
/usr/bin/ld: warning: cannot find entry symbol _start; defaulting to 0000000000401000
  (That's fine, the top of the .text section is where you want the entry point)
$ gdb ./a.out
(gdb) starti    # stop before the first instruction
(gdb) set $rax = 0x1234     # or whatever to make the effect visible
(gdb) si
(gdb) quit  # or starti to restart or whatever

如果让执行从那里继续,它当然会出现段错误,因为它将文本部分后面的 00 00 字节解码为 add %al, (%rax)

如果入口点没有符号,最近的 GDB 似乎在 starti 上崩溃 layout reg 在你的 ~/.gdbinit 中,不幸的是,所以你可能需要使用 .globl _start / _start: 在你的.s.