在gdb中执行指令/命令

问题描述

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

是否可以在命令行中插入一条指令?例如,类似:

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

解决方法

我不知道这是一种简单的交互方式;我不认为GDB在其命令解析器中内置有汇编程序,而只是用于从内存中反汇编代码的命令。从理论上讲,可能是某种形式的外部组装+在某处加载。

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

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

要处理单个指令并查看它们的功能,最好的选择是将它们放入.s文件中,并将其构建为静态可执行文件,然后可以与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在您的.s中。

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...