Objectivist-C

I’m Bavarious, (sporadically) writing about Objective-C, Apple frameworks and OS X.

Jan 18

Stack-trace-dumping regular-expression-based symbolic breakpoints in LLDB

Even though LLDB supports regular expressions when setting breakpoints, Xcode (as of 4.5.2) does not. Regular expressions can be useful when one’s trying to examine how a certain class is used overall.

In order to use a regular expression in a breakpoint, use the --func-regex option. For example:

(lldb) breakpoint set --func-regex NSLayoutManager
Breakpoint created: 2: regex = 'NSLayoutManager', locations = 312, resolved = 312
(lldb)

Breakpoint number 2 matches NSLayoutManager in 312 locations in this example. Inspecting all locations is easy:

(lldb) breakpoint list 2
…

LLDB stopping at each NSLayoutManager method quickly becomes annoying. In general, if I’m using a regular expression, I want to examine the stack trace to identify which methods have led to the execution of each location and automatically continue execution after the breakpoint is hit. This is easily accomplished in Xcode by editing a breakpoint, setting the Debugger Command action to bt and checking the Automatically continue after evaluating checkbox.

Since we’re not using Xcode, here’s how to obtain the same behaviour in LLDB. Recall that the example created a breakpoint numbered 2. We’ll add a three-line command to that breakpoint:

(lldb) breakpoint command add 2
Enter your debugger command(s).  Type 'DONE' to end.
> script print "----------"
> bt
> continue
> DONE
(lldb)

Voilà! Now we can resume program execution by issuing continue and LLDB will gladly dump a stack trace whenever a method/function whose name matches NSLayoutManager is executed. Note that this breakpoint does not match methods declared in superclasses unless they have been overridden by NSLayoutManager.

If you want to be more strict and, say, avoid C functions whose names contain NSLayoutManager, e.g. NSLayoutManagerLogDebug, add [ to the regular expression since Objective-C methods are represented by -[…] or +[…]. Recall that [ is a special character in POSIX regular expressions, so it needs to be escaped:

(lldb) breakpoint set --func-regex "\[NSLayoutManager"
Breakpoint created: 2: regex = '\[NSLayoutManager', locations = 311, resolved = 311

Tailor the regular expression to suit your needs, be prepared for many stack traces in the console log, and happy debugging!


  1. jeremytregunna reblogged this from objectivistc
  2. objectivistc posted this