【问题标题】:Cannot call GDB user defined function with Python无法使用 Python 调用 GDB 用户定义函数
【发布时间】:2015-08-17 14:56:06
【问题描述】:

我使用 Python API 为 GDB 定义了一个便利函数,

import gdb

verbose = True

class HCall(gdb.Function):
    def __init__(self, funcname):
        super(HCall,self).__init__(funcname)

    def log_call(cmd):
        if verbose:
            print(cmd)
        try:
            gdb.execute(cmd)
        except Exception, e:
            print (e)
            import traceback
            # traceback.print_stack()
            traceback.format_exc()


class NewCVar(HCall):
   """ allocates a c variable in heap """
   def __init__(self):
       super(NewCVar,self).__init__("newcvar")

   def invoke(self, name, oftype):
       cmd = "call gdb.execute(set $" + name + " = malloc(sizeof(" + oftype + "))"
       log_call(cmd)
       return "$" + name

NewCVar()

我可以使用“source usefunction.py”加载此文件,并使用“function newcvar”打印帮助文本。尽管如此,GDB 并不知道 $newcvar,正如我所料 (https://sourceware.org/gdb/onlinedocs/gdb/Functions-In-Python.html)。

有人知道我做错了什么吗?

提前致谢!

【问题讨论】:

  • 您最终拥有gdb.execute("call gdb.execute(set $" + name + " = malloc(sizeof(" + oftype + "))")。但是 gdb call 命令用于调用正在调试的目标中的函数。运行时 gdb 调试的目标是什么?
  • 一个标准的 C 程序。出于某种原因,我需要分配变量并在它们上调用函数。这就是 NewCVar 的动机。但这不是问题。当我尝试调用它时,GDB 说,它不知道 $newcvar,尽管命令函数知道它。

标签: python linux debugging gdb


【解决方案1】:

您可能应该准确发布发生的事情以及您期望发生的事情。

我在 gdb 中尝试了您的程序,gdb 确实看到了该功能;但由于函数中存在错误,因此它实际上不起作用。例如我试过:

(gdb) p $newcvar("x", "int")
Traceback (most recent call last):
  File "/tmp/q.py", line 23, in invoke
    cmd = "call gdb.execute(set $" + name + " = malloc(sizeof(" + oftype + "))"
gdb.error: Argument to arithmetic operation not a number or boolean.
Error occurred in Python convenience function: Argument to arithmetic operation not a number or boolean.

错误是你试图gdb.execute 一个看起来像call gdb.execute(...) 的字符串。这很奇怪。 call 计算低级中的表达式,因此将其与包含 gdb.execute 的参数一起使用是不正确的。相反,NewCVar.invoke 应该创建一个类似 set variable $mumble = ... 的字符串。

这里返回一个字符串也很奇怪。

我想知道为什么你希望这是一个函数而不是一个新的 gdb 命令。

【讨论】:

    猜你喜欢
    • 2011-05-10
    • 1970-01-01
    • 1970-01-01
    • 2021-01-06
    • 1970-01-01
    • 1970-01-01
    • 2011-03-21
    • 2014-06-06
    • 2022-11-02
    相关资源
    最近更新 更多