33

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?

5
  • 11
    What good would it do? The code wouldn't be aware of its existence. Commented Apr 23, 2012 at 16:06
  • Agree with @JamesMcLaughlin anyway to be clear: no, it's only for inspection. Commented Apr 23, 2012 at 16:09
  • 7
    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. Commented Apr 23, 2012 at 16:14
  • it's not only for inspection. you can change variable values in gdb: stackoverflow.com/questions/3305164/…. delorie.com/gnu/docs/gdb/gdb_118.html. you can't declare new variables though, as far as i know. Commented Feb 10, 2014 at 7:55
  • Does this answer your question? GDB: Create local variable? Commented Jan 15, 2021 at 12:13

3 Answers 3

39

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 (void) free($foo)
Sign up to request clarification or add additional context in comments.

6 Comments

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 Thanks - corrected the example, added call - it executes an expression and discards the result. And fixed malloc.
It seems now you have to cast to void when a function such as free is use like in ``` (gdb) call free(0) 'free' has unknown return type; cast the call to its declared return type (gdb) call (void) free(0) ```
@aitzkora Thank you, updated answer. What about the call func($foo) part? It would be strange for gdb to require an explicit type to be specified there. But, at the same time, it is unclear to me, why it would need it for the free call.
You right, I have also to add a cast (void) to call func($foo) if that function has no return type. I do not know if it is due to my gdb implementation, but it is also needed
|
21

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.

1 Comment

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

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
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.