Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xcode/LLDB: How to get information about an exception that was just thrown?

OK, so imagine that my breakpoint in objc_exception_throw has just triggered. I'm sitting at the debugger prompt, and I want to get some more information about the exception object. Where do I find it?

like image 766
Karoy Lorentey Avatar asked Jul 25 '10 03:07

Karoy Lorentey


People also ask

What does LLDB mean in Xcode?

What is LLDB? A low-level debugger (LLDB) is the default debugger used in Xcode and is part of the LLVM project. LLDB replaced GDB in XCode 5 and has several advantages, such as performance and scriptability.

Does Xcode use LLDB?

Xcode uses the LLDB as the default debugging tool. The full form of LLDB is Low-level debugger. Breakpoints help a developer to stop the execution of the program at any point.


1 Answers

The exception object is passed in as the first argument to objc_exception_throw. LLDB provides $arg1..$argn variables to refer to arguments in the correct calling convention, making it simple to print the exception details:

(lldb) po $arg1 (lldb) po [$arg1 name] (lldb) po [$arg1 reason] 

Make sure to select the objc_exception_throw frame in the call stack before executing these commands. See the "Advanced Debugging and the Address Sanitizer" in the WWDC15 session videos to see this performed on stage.

Outdated Information

If you're on GDB, the syntax to refer to the first argument depends on the calling conventions of the architecture you're running on. If you're debugging on an actual iOS device, the pointer to the object is in register r0. To print it or send messages to it, use the following simple syntax:

(gdb) po $r0 (gdb) po [$r0 name] (gdb) po [$r0 reason] 

On the iPhone Simulator, all function arguments are passed on the stack, so the syntax is considerably more horrible. The shortest expression I could construct that gets to it is *(id *)($ebp + 8). To make things less painful, I suggest using a convenience variable:

(gdb) set $exception = *(id *)($ebp + 8) (gdb) po $exception (gdb) po [$exception name] (gdb) po [$exception reason] 

You can also set $exception automatically whenever the breakpoint is triggered by adding a command list to the objc_exception_throw breakpoint.

(Note that in all cases I tested, the exception object was also present in the eax and edx registers at the time the breakpoint hit. I'm not sure that'll always be the case, though.)

Added from comment below:

In lldb, select the stack frame for objc_exception_throw and then enter this command:

(lldb) po *(id *)($esp + 4) 
like image 192
Karoy Lorentey Avatar answered Sep 19 '22 06:09

Karoy Lorentey