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.

6
  • 1
    What would be the point? Commented Nov 25, 2011 at 23:38
  • To mess around, like you can do with the Interactive Ruby Shell (IRB). Commented Nov 25, 2011 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? Commented Nov 25, 2011 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. Commented Nov 26, 2011 at 0: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). Commented Nov 26, 2011 at 0:51

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

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 *$

1 Comment

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.

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.