写PythonGDB扩展时尝试子类化gdb.Breakpoint时出错
我正在尝试为GDB写一个简单的Python扩展,目的是在每次触发断点时将信息输出到一个文件里。根据文档的说明,“gdb.Breakpoint类可以被子类化”(可以查看这个链接了解更多:http://sourceware.org/gdb/onlinedocs/gdb/Breakpoints-In-Python.html)
但是,当我尝试以下代码时,出现了一个错误:“TypeError: 调用元类基类时出错。类型'gdb.Breakpoint'不是一个可接受的基类型。”
class MyBreakpoint(gdb.Breakpoint):
def stop (self):
print "break"
return False
我在使用Ubuntu 11.04和gdb 7.2。如果有任何帮助或者更好的文档链接,我将非常感激。谢谢!
我的具体步骤是:
$ gdb
GNU gdb (Ubuntu/Linaro 7.2-1ubuntu11) 7.2
Copyright (C) 2010 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "show copying"
and "show warranty" for details.
This GDB was configured as "i686-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
(gdb) source t.py
Traceback (most recent call last):
File "t.py", line 3, in <module>
class MyBreakpoint(gdb.Breakpoint):
TypeError: Error when calling the metaclass bases
type 'gdb.Breakpoint' is not an acceptable base type
(gdb)
2 个回答
你的代码(经过缩进修正)在GDB-7.2和最近的GDB CVS快照上运行得很好:
$ cat t.py
class MyBreakpoint(gdb.Breakpoint):
def stop (self):
print "break"
return False
$ gdb-cvs
GNU gdb (GDB) 7.3.50.20110411-cvs
Copyright (C) 2011 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-unknown-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
(gdb) source t.py
(gdb) quit
如果你重复上面的步骤,是否看到什么不同的结果?如果没有,你到底是怎么做的才会出现TypeError
的?
编辑:这之所以有效,是因为我的GDB-7.2应用了一些上游的补丁。它在“原版”的7.2上是不能工作的。
这里有适合 gdb 7.2 的文档:
http://sourceware.org/gdb/download/onlinedocs/gdb/Breakpoints-In-Python.html#Breakpoints-In-Python
我猜 EmployedRussian 使用的是一个比较新的 gdb 7.2(比如 7.2.90 或者类似的版本,这个版本似乎包含了一些补丁)。
不过,这个版本其实并不是官方发布的 7.2,很多方面更像是 7.3 的预发布版本,因为它是在 7.3 分支出来前大约两周创建的。
所以它能在他的环境中工作,主要是因为 gdb 使用的是“在发布前的 7.3 分支”,而不是“在 7.2 发布后的 7.3 分支”这种模式。
所以如果你想在 7.2 上做到这一点,可能需要使用以下方法:
break foo
commands
python print "break"
end