按名称GDB指定正确的类型时,存在多种类型的同名 [英] Specifying correct type by name in gdb when multiple types of same name exist

查看:496
本文介绍了按名称GDB指定正确的类型时,存在多种类型的同名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现,我得到两个不同的结果检查变量时,这取决于我是否或明或暗地使用GDB明白变量的类型为:

导航到我的堆栈帧

(GDB)框架2
#2 0x00007f6a4277e87d在PyCheckFile(FNAME = 0x7f6a338704b8/ usr / lib目录/调试/ sbin目录,CTX = 0x7f6a3803ddf8)
    在蟒蛇-fd.c:2054
2054 pFunc = PyDict_GetItemString(p_ctx-> pDictcheck_file); / *借用的引用* /

打印什么 p_ctx 点,因为GDB的理解,含蓄地使用任何GDB知道它的类型。

 (GDB)打印* p_ctx
$ 26 = {backup_level = 0,python_loaded =假,plugin_options =为0x0,module_path中=为0x0,MODULE_NAME =为0x0,
  FNAME =为0x0,链接=为0x0,OBJECT_NAME =为0x0,对象=为0x0,除preTER = 0x7f6a3802bb10,pModule =为0x0,
  pDict =为0x0,bpContext =为0x0}

询问GDB的类型名称

 (GDB)头朝下p_ctx
类型= plugin_ctx *

指定类型名称明确打印时 p_ctx ,我们得到一个非常不同的结果。

 (GDB)打印*((* plugin_ctx)p_ctx)
$ 27 = {偏移量= 0,PFD =为0x0,plugin_options =为0x0,FNAME =为0x0,读卡器=为0x0,作家=为0x0,
  其中,='\\ 000'<重复16次>中\\ 020 \\ 273 \\ 002 \\ 070j \\ 177,'\\ 000'<重复26次>中\"\\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@\
tBj\\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\"...,取代= 0}

请GDB告诉我们关于名为plugin_ctx类型:

 (GDB)信息类型^ $ plugin_ctx
所有类型匹配的正前pression^ $ plugin_ctx:文件bpipe-fd.c:
plugin_ctx;文件中的python-fd.c:
plugin_ctx;

那么有我们的问题;我们在python-fd.c,当我们明确指定类型名称,我们得到bpipe-FD的类型,而不是!

为证:

 (GDB)PTYPE p_ctx
键入=结构plugin_ctx {
    int32_t backup_level;
    布尔python_loaded;
    字符* plugin_options;
    字符* module_path中;
    字符* MODULE_NAME;
    字符* FNAME;
    字符*链接;
    字符* OBJECT_NAME;
    字符*对象;
    PyThreadState *间preTER;
    *的PyObject pModule;
    *的PyObject pDict;
    *的PyObject bpContext;
} *

相比于

 (GDB)PTYPE plugin_ctx
键入=结构plugin_ctx {
    boffset_t抵消;
    BPIPE * PFD;
    字符* plugin_options;
    字符* FNAME;
    字符*阅读器;
    字符*作家;
    焦炭其中[512];
    INT更换;
}

所以,当presented多种类型命名为 plugin_ctx ,怎么我告诉GDB使用哪一个?我试过:

 (GDB)打印*(('蟒蛇-fd.c':: plugin_ctx *)p_ctx)
在EX pression语法错误,近`*)p_ctx)。

这显然没有奏效。我还没有找到如何时,它适用于各类解决这种歧义的GDB手册东西。那么,在这种情况下,preferred的方法呢?


解决方案

我会充实这个答案举例,因为我得到它的工作,但基于对核心岗位@纳米的反馈和讨论中发现的信息<一href=\"http://stackoverflow.com/questions/7272558/can-we-define-a-new-data-type-in-a-gdb-session\">another螺纹,这里有一个解决方法:

创建你想要的类型的对象文件,用明确的名称。

 的#include&LT; Python.h&GT;
结构plugin_ctx2 {
    int32_t backup_level;
    布尔python_loaded;
    字符* plugin_options;
    字符* module_path中;
    字符* MODULE_NAME;
    字符* FNAME;
    字符*链接;
    字符* OBJECT_NAME;
    字符*对象;
    PyThreadState *间preTER;
    *的PyObject pModule;
    *的PyObject pDict;
    *的PyObject bpContext;
};

<醇开始=2>

  • 使用 GCC 添加符号文件命令,扳指目标文件到正在运行的进程。

  • 您现在应该能够使用新的类型。

  • 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:

    Navigate to my stack frame

    (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 */

    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}
    

    Ask GDB for the name of the type

    (gdb) whatis p_ctx
    type = plugin_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}
    

    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;
    

    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!

    As evidence:

    (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;
    } *
    

    compared to:

    (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;
    }
    

    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 )'.
    

    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?

    解决方案

    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:

    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;
    } ;
    

    1. Using gcc's add-symbol-file command, pull that object file into the running process.
    2. You should now be able to use the new type.

    这篇关于按名称GDB指定正确的类型时,存在多种类型的同名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆