4

I'm using Xcode's debugger. While stopped at a breakpoint, is there a command I can type in the GDB command prompt to create a local variable? If so, how? Please provide an example.

I know I can do it in the code and then recompile the program, but I'm looking for a faster way.

ma11hew28
  • 121,420
  • 116
  • 450
  • 651
  • 1
    What would be the point? – Neil Nov 25 '11 at 23:38
  • To mess around, like you can do with the Interactive Ruby Shell (IRB). – ma11hew28 Nov 25 '11 at 23:43
  • 1
    Sorry, your question is not quite clear. I noticed only after I saw Neil's reply. From the fact that you mentioned you were aware the option to recompile your code to introduce a "local variable" I assumed that you meant a stack variable. Did you, or did Neil get the question right by assuming you meant a GDB convenience variable? – 0xC0000022L Nov 25 '11 at 23:55
  • I'm not really sure what either of those two are. I just meant like, how can I create an `NSArray` that doesn't exist yet from GDB. I'll try with the GDB convenience variable. – ma11hew28 Nov 26 '11 at 00:43
  • 1
    @MattDiPasquale: the convenience variables can be used as an alias/shortcut for other names. But they don't allow you to store more stuff on the stack while the code remains valid. Again, the opcodes that have been created by the assembler/compiler will be tied to a certain stack layout for each activation context (function/stack frame). – 0xC0000022L Nov 26 '11 at 00:51
  • Possible duplicate of [Can I declare a variable with gdb?](http://stackoverflow.com/questions/10284103/can-i-declare-a-variable-with-gdb) – Ciro Santilli OurBigBook.com Apr 26 '17 at 12:33

3 Answers3

7

If you don't need to reference the variable in your code but just want to do some ad-hoc investigation, you can use Convenience Variables by using the set command with a variable name starting with $:

(gdb) set $foo = method_that_makes_something()
(gdb) set $bar = 15
(gdb) p $bar
$4 = 15

You'll notice when you print things it's prefixed with a numeric variable - you can use these to refer to that value later as well:

(gdb) p $4
$5 = 15

To reiterate: this doesn't actually affect the program's stack, and it can't, as that would break a lot of things. But it's useful if you just need a local playground, some loop variables, etc.

While you can't modify the stack, you can interact with the program's memory space - you can call functions (including malloc) and construct objects, but these will all live in static memory, not as local variables on the stack.

cincodenada
  • 2,877
  • 25
  • 35
1

Since a local variable would require stack space and the (compiled) code is tied to the stack layout, no you can't.

Comparing this with scripting languages is not quite appropriate.

0xC0000022L
  • 20,597
  • 9
  • 86
  • 152
1

Values printed by the print command are saved in the GDB "value history". This allows you to refer to them in other expressions.

For example, suppose you have just printed a pointer to a structure and want to see the contents of the structure. It suffices to type

p *$
Neil
  • 54,642
  • 8
  • 60
  • 72
  • Hmm, I'm confused. In hindsight, his question is not quite clear. Does he mean a local stack variable (my assumption) or a GDB convenience variable (your assumption). Gonna add a comment to the question. – 0xC0000022L Nov 25 '11 at 23:53