简体   繁体   English

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

[英]GDB: how to change a convenience variable from a built-in python

How do I assign something to a gdb convenience variable from its built-in python?如何从其内置的 python 中为 gdb便利变量分配一些东西? I thought it should be simple, because it naturally arises while trying to implement a user-defined function.我认为它应该很简单,因为它在尝试实现用户定义的功能时自然会出现。 However I couldn't find anything.但是我找不到任何东西。

UPD: Added a simplified example. UPD:添加了一个简化示例。 My real function is more complex though.不过,我的实际功能更复杂。 Anyway, a way to transfer a variable from python up to gdb would help greatly.无论如何,将变量从 python 传输到 gdb 的方法将有很大帮助。

An example of usage (achtung! — it raises an error):使用示例(achtung!-它会引发错误):

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

There exists gdb's python API to get/set convenience variable: from the documentation存在 gdb 的 python API 来获取/设置便利变量:来自文档

Function: gdb.convenience_variable (name) Return the value of the convenience variable (see Convenience Vars) named name.功能: gdb.convenience_variable(name)返回名为name 的便利变量(见Convenience Vars)的值。 name must be a string. name 必须是字符串。 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.如果便利变量不存在,则返回 None 。

Function: gdb.set_convenience_variable (name, value) Set the value of the convenience variable (see Convenience Vars) named name.功能: gdb.set_convenience_variable(name, value)设置名为name的便利变量(见Convenience Vars)的值。 name must be a string. name 必须是字符串。 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.如果值为 None,则删除便利变量。 Otherwise, if value is not a gdb.Value (see Values From Inferior), it is is converted using the gdb.Value constructor.否则,如果 value 不是 gdb.Value(参见 Values From Inferior),则使用 gdb.Value 构造函数对其进行转换。

accept a string, return it's lenght接受一个字符串,返回它的长度

If you indeed need strlen you can use $_strlen - a built in gdb convenience function that compute string length:如果您确实需要 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?如何从内置 python 为 GDB 便利变量分配一些东西?

You can use gdb.execute:您可以使用 gdb.execute:

>more my_own_len.py
class my_own_len (gdb.Function):
   def __init__ (self):
     super (my_own_len, self).__init__ ("my_own_len")

   def invoke (self, arg):
      res=len(arg.string())
      gdb.execute( "set $Retrn=" + str(res))
      return res

my_own_len()

This is a test:这是一个测试:

>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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM