GDB如何从内置python更改便利变量

2024-10-01 22:31:15 发布

您现在位置:Python中文网/ 问答频道 /正文

如何从gdb的内置python向gdb便利变量赋值?我认为它应该很简单,因为它是在尝试实现用户定义函数时自然产生的。但是我什么也没找到。在

UPD:添加了一个简化示例。但我的实际功能更复杂。不管怎样,将一个变量从python转移到gdb的方法会有很大帮助。在

用法示例(achtung!-它会引发一个错误):

#accept a string, return it's lenght
define RetLenOfArg
  py arg0 = gdb.parse_and_eval("$arg0")
  set $Retrn = py len(arg0)
end

Tags: 方法函数用户py功能示例用法定义
2条回答

exist gdb的python API来获取/设置便利变量:来自documentation

Function: gdb.convenience_variable (name) Return the value of the convenience variable (see Convenience Vars) named name. name must be a string. The name should not include the ‘$’ that is used to mark a convenience variable in an expression. If the convenience variable does not exist, then None is returned.

Function: gdb.set_convenience_variable (name, value) Set the value of the convenience variable (see Convenience Vars) named name. name must be a string. The name should not include the ‘$’ that is used to mark a convenience variable in an expression. If value is None, then the convenience variable is removed. Otherwise, if value is not a gdb.Value (see Values From Inferior), it is is converted using the gdb.Value constructor.

accept a string, return it's lenght

如果确实需要strlen,可以使用$_strlen-一个内置的gdb便利函数,用于计算字符串长度:

(gdb) set $Retrn=$_strlen("test")
(gdb) p $Retrn
$4 = 4

how to assign something to a GDB convenience variable from a built-in python?

你可以用gdb.执行公司名称:

^{pr2}$

这是一个测试:

>gdb -q -x my_own_len.py
(gdb) p $_strlen("test")
$1 = 4
(gdb) p $my_own_len("test")
$2 = 4
(gdb) p $Retrn
$3 = 4
(gdb)

相关问题 更多 >

    热门问题