如何在gdb中传递基本参数

How to do basic parameter passing in gdb

我在 .gdbinit 中定义了以下内容,以便在我想以十进制格式查看时更容易打印“堆栈”:

define s
   x/5gd $rsp
end

现在我可以输入如下内容:

>>> s
0x7fffffffe408: 10  8
0x7fffffffe418: 6   4
0x7fffffffe428: 2

默认情况下,它将打印 5 个 8 字节的值。如何使用输入参数来使用我传递的数字而不是 5?例如,类似于:

define s(d=5)
   x/%sgd $rsp % d
end

此外,我熟悉 python,所以只要我可以访问输入参数,我也可以使用它,即:

def stack():
    return "x/%sgd" % ('5' if not argv[1].isdigit() else argv[1])

你想要的是 $argc 和 $arg0 $arg1 .....
您可以在 https://sourceware.org/gdb/onlinedocs/gdb/Define.html 中找到如何实施 User-defined 命令。

gdb 中 define 命令的参数可作为 $arg0$arg1 等访问。参数数量在 $argc 中。请注意 $arg0 是第一个参数(不是 C 命令行参数中的命令。)所以你可以写

define s
    if $argc == 0
        x/5gd $rsp
    else
        x/$arg0 $rsp
    end
end