Change Variables On The Fly While Debugging With XCode and LLDB

Somewhere around Xcode 4.3 lldb became the default debugger. I’ve been spending some time learning how lldb differs from gdb and stumbled upon a trick to allow changing the value of a variable while in a breakpoint.

For example, in the code below I print the value of testStr, I then change its value using the expr command in lldb, I then print the value of the string again:

NSString *testStr = @"fubar";
NSLog(@"testStr before: %@", testStr);
// Set breakpoint here, run the 'expr' shown below
// Show testStr again
NSLog(@"testStr after: %@", testStr);

When the breakpoint occurs, the first output is as follows (line 2):

testStr before: fubar

Next, run the following at the lldb prompt:

expr testStr = @"Foobar"

This will change the value of testStr (line 7):

testStr after: Foobar

The box highlighted in green shows how to change the value of an NSString object using the expr command in lldb.

Here is more complete list of lldb commands.

  1. Using lldb can be really useful. There are just some things that don’t work well enough in Xcode’s user interface. I always recommend to another developers visiting my blog to take the time to learn some basic commands such as:

Comments are closed.