28

I know that gdb allows for an already declared variable to be set using the set command.

Is it possible for gdb to dynamically declare a new variable inside the scope of a given function?

Randomblue
  • 105,985
  • 138
  • 338
  • 537
  • 11
    What good would it do? The code wouldn't be aware of its existence. – James McLaughlin Apr 23 '12 at 16:06
  • Agree with @JamesMcLaughlin anyway to be clear: **no**, it's only for inspection. – Adriano Repetti Apr 23 '12 at 16:09
  • 6
    Are you sure you don't want a gdb variable? You *can* create variables in the context of gdb for your convenience, like `set $foo = ...` and later reference `$foo`. Obviously such variables are in no way visible to the running code, however. – FatalError Apr 23 '12 at 16:14
  • it's not only for inspection. you can change variable values in gdb: http://stackoverflow.com/questions/3305164/how-to-modify-memory-contents-using-gdb. http://www.delorie.com/gnu/docs/gdb/gdb_118.html. you can't declare new variables though, as far as i know. – thang Feb 10 '14 at 07:55
  • Does this answer your question? [GDB: Create local variable?](https://stackoverflow.com/questions/8275135/gdb-create-local-variable) – xhienne Jan 15 '21 at 12:13

3 Answers3

31

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:

set $foo = malloc(sizeof(struct funcOutStruct))
call func($foo)
p *$foo
call free($foo)
Ilya Bobyr
  • 680
  • 6
  • 15
  • In my case, calling `func($foo)` from gdb did not work (`Undefined command: "func".`), but calling `set $garbage = func($foo)` worked. In addition, I had to use `malloc` instead of `alloc`. – nullromo Dec 02 '18 at 22:40
  • 1
    @nullromo Thanks - corrected the example, added `call` - it executes an expression and discards the result. And fixed `malloc`. – Ilya Bobyr Dec 19 '18 at 05:55
20

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

unwind
  • 378,987
  • 63
  • 458
  • 590
  • Strictly speaking this is correct, it would be a pain to get the variables on the stack. But if you're ok with static variables, Ilya's solution works. – r_2 Dec 16 '18 at 23:46
7

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
    }
laalto
  • 144,748
  • 64
  • 275
  • 293
jasonxiaole
  • 71
  • 1
  • 1