gdb将评估的原始结果传递给自定义python函数

时间:2015-06-20 09:09:40

标签: 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/libpthread.so.0

但是,output命令还会打印字符串的地址:

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

2 个答案:

答案 0 :(得分:1)

您可以通过在python中进行解决,即

py print gdb.execute("output $arg0", to_string=True).strip('"').split()[1]
可能通过自定义output来电,

或(以及我喜欢的)。 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".

按方式:之前应该提到这一点,但是:由于某种原因,地址打印可能设置为on。所以:

set print addr off

可能会解决您的问题。

答案 1 :(得分:1)

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