简体   繁体   English

如何使用GDB在给定函数的范围内声明变量?

[英]How to declare a variable in the scope of a given function with GDB?

I know that gdb allows for an already declared variable to be set using the set command. 我知道gdb允许使用set命令设置已声明的变量。

Is it possible for gdb to dynamically declare a new variable inside the scope of a given function? 是否有可能gdb在给定函数的范围内动态声明一个新变量?

You can dynamically allocate some space and use it to store a new variable. 您可以动态分配一些空间并使用它来存储新变量。 Depending on what you mean by "scope of the current function" it may not be what you want. 根据“当前函数的范围”的含义,它可能不是您想要的。

But here is how it looks like, when you have function func() that takes a pointer to an output parameter: 但是当你有一个带有指向输出参数的指针的函数func()时,它的外观如下:

set $foo = malloc(sizeof(struct funcOutStruct))
call func($foo)
p *$foo
call free($foo)

For C (and probably C++) code, that would be very hard, since doing so in most implementations would involve shifting the stack pointer, which would make the function's exit code fail due to it no longer matching the size of the stack frame. 对于C(可能还有C ++)代码,这将是非常困难的,因为在大多数实现中这样做会涉及移动堆栈指针,这会使函数的退出代码失败,因为它不再匹配堆栈帧的大小。 Also all the code in the function that accesses local variables would suddenly risk hitting the wrong location, which is also bad. 此外,函数中访问局部变量的所有代码都会突然冒险到达错误的位置,这也很糟糕。

So, I don't think so, no. 所以,我不这么认为,不。

that's how I used to print variables 这就是我用来打印变量的方式

(gdb) set $path=((ngx_path_t     **)ngx_cycle->paths.elts)[2]
(gdb) print *$path
    $16 = {
        name = {
            len = 29,
            data = 0x80ed15c "/usr/local/nginx/fastcgi_temp"
            },
        len = 5,
        level = {1, 2, 0},
        manager = 0,
        loader = 0,
        data = 0x0,
        conf_file = 0x0,
        line = 0
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM