gdb将原始计算结果传递给自定义python函数

gdb将原始计算结果传递给自定义python函数,python,gdb,Python,Gdb,amd64,寄存器$rdi是指向“/home/il/gammu-git/src/gammu/libgammu/tls/x86_64/libpthread.so.0”的指针 这里有一个例子: define foo py print gdb.execute("output $arg0", to_string=True).strip('"') end 预期: (gdb) foo (char*)$rdi /home/il/gammu-git/src/gammu/libgammu/tls/x86_64/

amd64,寄存器
$rdi
是指向
“/home/il/gammu-git/src/gammu/libgammu/tls/x86_64/libpthread.so.0”的指针

这里有一个例子:

define foo
py print gdb.execute("output $arg0", to_string=True).strip('"')
end
预期:

(gdb) foo (char*)$rdi
/home/il/gammu-git/src/gammu/libgammu/tls/x86_64/libpthread.so.0
但是,
output
命令也会打印字符串的地址:

(gdb) foo (char*)$rdi
0x7fffffffe180 "/home/il/gammu-git/src/gammu/libgammu/tls/x86_64/libpthread.so.0

您可以通过使用python来解决这个问题,即

py print gdb.execute("output $arg0", to_string=True).strip('"').split()[1]
或者(这是我更喜欢的)可能是通过定制
输出
调用
gdb
通过键入
help output
并遵循此处的线索,提供全面的在线帮助:

(gdb) help output
Like "print" but don't put in value history and don't print newline.
This is useful in user-defined commands.
(gdb) help print
Print value of expression EXP.
Variables accessible are those of the lexical environment of the selected
stack frame, plus all those whose scope is global or an entire file.

$NUM gets previous value number NUM.  $ and $$ are the last two values.
$$NUM refers to NUM'th value back from the last one.
Names starting with $ refer to registers (with the values they would have
if the program were to return to the stack frame now selected, restoring
all registers saved by frames farther in) or else to debugger
"convenience" variables (any such name not a known register).
Use assignment expressions to give values to convenience variables.

{TYPE}ADREXP refers to a datum of data type TYPE, located at address ADREXP.
@ is a binary operator for treating consecutive data objects
anywhere in memory as an array.  FOO@NUM gives an array whose first
element is FOO, whose second element is stored in the space following
where FOO is stored, etc.  FOO must be an expression whose value
resides in memory.

EXP may be preceded with /FMT, where FMT is a format letter
but no count or size letter (see "x" command).
(gdb) help x
Examine memory: x/FMT ADDRESS.
ADDRESS is an expression for the memory address to examine.
FMT is a repeat count followed by a format letter and a size letter.
Format letters are o(octal), x(hex), d(decimal), u(unsigned decimal),
  t(binary), f(float), a(address), i(instruction), c(char), s(string)
  and z(hex, zero padded on the left).
Size letters are b(byte), h(halfword), w(word), g(giant, 8 bytes).
The specified number of objects of the specified size are printed
according to the format.

Defaults for format and size letters are those previously used.
Default count is 1.  Default address is following last thing printed
with this command or "print".
顺便说一句:应该在前面提到这一点,但是:可能由于某种原因,地址打印设置为
上的
。因此:

set print addr off

可能会解决您的问题。

gdb语法混乱,比MS-DOS的命令行更糟糕

通过6个“简单”步骤,您可以将任意字符串传递给python:

define foo
    # reset convenience var., or get 'Too many array elements'
    set $foo_arg0 = 0
    # $arg0 expanded too early, can't use it directly in python
    set $foo_arg0 = $arg0
    #
    # parse_and_eval() affected by print settings, wtf?
    #
    # don't print address of a string
    set print addr off
    # print long string
    set print elements 0
    # get rid of '<repeats n times>'
    set print repeats unlimited
    #
    # parse_and_eval() returns quoted string. Unquote with eval
    py s = eval(str(gdb.parse_and_eval("$foo_arg0")))
    py print s
end

(gdb) foo "abc"
abc
(gdb) foo $val
def
(gdb) foo (char*)$rdi
/home/il/gammu-git/src/gammu/libgammu/tls/x86_64/libpthread.so.0
定义foo
#重置便利变量,或获取“太多数组元素”
设置$foo_arg0=0
#$arg0扩展太早,无法在python中直接使用
设置$foo_arg0=$arg0
#
#是否分析受打印设置影响的\u和\u eval(),wtf?
#
#不要打印字符串的地址
关闭打印地址
#打印长字符串
将打印元素设置为0
#除掉“
设置打印重复次数不受限制
#
#parse_和_eval()返回带引号的字符串。用eval取消报价
py s=eval(str(gdb.parse_和_eval($foo_arg0)))
py打印
结束
(gdb)foo“abc”
abc
(gdb)foo$val
def
(gdb)foo(char*)$rdi
/home/il/gammu-git/src/gammu/libgammu/tls/x86_64/libpthread.so.0

我尝试了p/s和p/r,但地址仍然打印出来。您在执行
/s
时是否省略了
(char*)
强制转换?哦。。。我忘了检查一件事。我已经更新了我的答案,在底部。你能试试吗?是的,那有点帮助。仍然必须在python中拆分
$32=“aaaa”
。如果python不能直接使用gdb变量,可以设置env var吗?