当存在多个类型的相同名称时,在gdb中通过名称指定正确的类型 [英] Specifying correct type by name in gdb when multiple types of same name exist
问题描述
我发现在检查一个变量时,我得到两个不同的结果,取决于我是隐式还是显式使用类型GDB理解该变量是:
I discovered that I get two different results when examining a variable, depending on whether I implicitly or explicitly use the type GDB understands that variable to be:
导航到我的堆栈框架
(gdb)frame 2
#2 0x00007f6a4277e87d in PyCheckFile(fname = 0x7f6a338704b8/ usr / lib / debug / sbin,ctx = 0x7f6a3803ddf8)
at python-fd.c:2054
2054 pFunc = PyDict_GetItemString(p_ctx-> pDict,check_file); / *借用引用* /
打印 p_ctx
指向的目标理解它,隐式使用任何GDB知道它的类型。
Print the what p_ctx
points to, as GDB understands it, implicitly using whatever GDB knows about its type.
(gdb) print *p_ctx
$26 = {backup_level = 0, python_loaded = false, plugin_options = 0x0, module_path = 0x0, module_name = 0x0,
fname = 0x0, link = 0x0, object_name = 0x0, object = 0x0, interpreter = 0x7f6a3802bb10, pModule = 0x0,
pDict = 0x0, bpContext = 0x0}
询问GDB类型的名称
(gdb) whatis p_ctx
type = plugin_ctx *
在打印 p_ctx
时明确指定类型名称,我们得到了非常不同的输出。
Specify that type name explicitly when printing p_ctx
, and we get a very different output.
(gdb) print * ( (plugin_ctx *) p_ctx )
$27 = {offset = 0, pfd = 0x0, plugin_options = 0x0, fname = 0x0, reader = 0x0, writer = 0x0,
where = '\000' <repeats 16 times>, "\020\273\002\070j\177", '\000' <repeats 26 times>, "\225\031\327Mj\177\000\000\240\000\000\000\000\000\000\000%\001\000\000\000\000\000\000@e\317Mj\177\000\000\370&\322Mj\177\000\000\375\377\377\377\377\377\377\377\260\234\337Mj\177\000\000\001\000\000\000\000\000\000\000@\ntBj\177\000\000\060\000\000\000\000\000\000\000*\000\000\000\000\000\000\000\177\000\000\000\000\000\000\000@\322\000\070j\177\000\000P\aCBj\177\000\000Ȋ\260\270i\225\fbЉ\342Mj\177\000\000\000\035c\001\000\000\000\000َ\372<\375\364\343\372\300\237\342Mj\177\000\000\000\337\325"..., replace = 0}
要求GDB告诉我们有关名为plugin_ctx的类型:
Ask GDB to tell us about types named plugin_ctx:
(gdb) info types ^plugin_ctx$
All types matching regular expression "^plugin_ctx$":
File bpipe-fd.c:
plugin_ctx;
File python-fd.c:
plugin_ctx;
这是我们的问题;我们在python-fd.c中,当我们明确指定一个类型名称时,我们得到bpipe-fd的类型。
Well there's our problem; we're in python-fd.c, and when we explicitly specify a type name, we get bpipe-fd's type instead!
作为证据:
(gdb) ptype p_ctx
type = struct plugin_ctx {
int32_t backup_level;
bool python_loaded;
char *plugin_options;
char *module_path;
char *module_name;
char *fname;
char *link;
char *object_name;
char *object;
PyThreadState *interpreter;
PyObject *pModule;
PyObject *pDict;
PyObject *bpContext;
} *
相比:
(gdb) ptype plugin_ctx
type = struct plugin_ctx {
boffset_t offset;
BPIPE *pfd;
char *plugin_options;
char *fname;
char *reader;
char *writer;
char where[512];
int replace;
}
因此,当出现多个类型 plugin_ctx
,我该如何告诉gdb使用哪一个?我试过:
So, when presented with multiple types named plugin_ctx
, how to I tell gdb which one to use? I've tried:
(gdb) print * ( ('python-fd.c'::plugin_ctx *) p_ctx )
A syntax error in expression, near `*) p_ctx )'.
这显然不起作用。我没有找到任何在GDB的手册,如何解决这种消歧时适用于类型。那么这种情况下的首选方法是什么?
which obviously did not work. I have not found anything in GDB's manual on how to address this kind of disambiguation when it applies to types. So what's the preferred approach in this situation?
推荐答案
我将通过示例来演示这个答案,基于@ n.m对核心帖子的反馈讨论,以及另一个线程,这里是一个解决方法:
I'll flesh out this answer with examples as I get it working, but based on @n.m's feedback discussion on the core post and the information found within another thread, here's a workaround:
使用所需的类型创建一个对象文件, p>
Create an object file with the type you want, with an unambiguous name.
#include <Python.h>
struct plugin_ctx2 {
int32_t backup_level;
bool python_loaded;
char *plugin_options;
char *module_path;
char *module_name;
char *fname;
char *link;
char *object_name;
char *object;
PyThreadState *interpreter;
PyObject *pModule;
PyObject *pDict;
PyObject *bpContext;
} ;
- 使用
gcc
'add-symbol-file
命令,将该对象文件拖入正在运行的进程。 - 现在可以使用新类型。
- Using
gcc
'sadd-symbol-file
command, pull that object file into the running process. - You should now be able to use the new type.
这篇关于当存在多个类型的相同名称时,在gdb中通过名称指定正确的类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!