# printer.py
import gdb
class PrintCppStruct(gdb.Command):
def __init__(self):
super(PrintCppStruct, self).__init__("print_cpp_struct", gdb.COMMAND_USER)
def invoke(self, arg, from_tty):
try:
cl = gdb.parse_and_eval(arg)
if cl.type.code == gdb.TYPE_CODE_STRUCT and str(cl.type) == 'Point':
print_point(cl)
elif cl.type.code == gdb.TYPE_CODE_STRUCT and str(cl.type) == 'Person':
print_person(cl)
else:
print("Unknown C++ structure type.")
except gdb.error as e:
print(f"Error: {e}")
PrintCppStruct()
Python
中使用gdb
模块,注册函数供gdb
调用。
// point.cpp
#include <iostream>
struct Point {
int x;
int y;
};
int main() {
Point myPoint = {10, 20};
// 执行其他操作
return 0;
}