当存在多个相同名称的类型时,在gdb中按名称指定正确的类型

我发现在检查变量时会得到两个不同的结果,这取决于我是否隐式或显式地使用类型GDB理解该变量为:

导航到我的堆栈框架

(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"); /* Borrowed reference */

打印p_ctx指向的内容,正如GDB所理解的那样,隐式使用GDB知道的类型。

 (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时明确指定该类型名称,我们得到一个非常不同的输出。

 (gdb) print * ( (plugin_ctx *) p_ctx ) $27 = {offset = 0, pfd = 0x0, plugin_options = 0x0, fname = 0x0, reader = 0x0, writer = 0x0, where = '\000' , "\020\273\002\070j\177", '\000' , "\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的类型:

 (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的类型!

作为证据:

 (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使用哪一个? 我试过了:

 (gdb) print * ( ('python-fd.c'::plugin_ctx *) p_ctx ) A syntax error in expression, near `*) p_ctx )'. 

这显然不起作用。 我在GDB的手册中没有找到任何关于如何在适用于类型时解决这种消歧的方法。 那么在这种情况下首选的方法是什么?

我将通过示例充实这个答案,因为我开始工作,但基于@ nm对核心post的反馈讨论以及在另一个线程中找到的信息,这是一个解决方法:

使用所需类型创建一个具有明确名称的对象文件。

 #include  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; } ; 
  1. 使用gccadd-symbol-file命令,将该对象文件拉入正在运行的进程中。
  2. 您现在应该可以使用新类型。