19

How can I automate setting a breakpoint on all methods in an Objective C class using lldb?

This is useful for learning the behavior of a complicated legacy class. I am using Xcode (includes lldb) for iOS development, and it is cumbersome to manually go through the (large) file in Xcode and click the gutter next to each method to set breakpoints.

tboyce12
  • 1,449
  • 14
  • 28

2 Answers2

27

One option is to use regex breakpoints.

breakpoint set -r '\[ClassName .*\]$'

You can play around with the regexp to suit your needs.

The command will create a breakpoint that stops on all methods implemented by that class. However, there will be no breakpoints on methods inherited from superclasses.

To get methods on the superclass, you'll have to use a conditional breakpoint. For example, if the superclass is UIViewController, you could do something like:

br s -r '\[UIViewController .*\]$' -c '(BOOL)[(id)$arg1 isKindOfClass:[CustomVC class]]'

For x86 change (id)$arg1 to *(id*)($ebp+8).

Finally, if you really want to learn about the control flow through various classes, check out dtrace. It's probably more suited to this than a debugger.

Dave Lee
  • 6,299
  • 1
  • 36
  • 36
  • I'm confused what you mean by `\[ClassName(\(.*\)) .*\]$`, however `\[FooViewController .*\]$` did the trick for me. I also like that omitting the $ causes breakpoints on blocks too. The regex for breaking on superclass methods is nice also. Thanks for the tips, and I'll be sure to check out dtrace. – tboyce12 May 02 '15 at 02:40
  • There should have been `?` after the `(\(.*\))`. That part is to match categories, e.g. `-[UIView(Geometry) setFrame:]`. I'll take it out because it shouldn't be necessary. – Dave Lee May 03 '15 at 05:27
  • Where I write this command. Please suggest On console or on terminal or in code. – Chandni Sep 19 '17 at 10:20
  • @ChandniSharma into lldb, which normally has "(lldb)" as the prompt. You have to have lldb running, which Xcode does by default. – Dave Lee Sep 20 '17 at 17:23
  • thanks for the reply but still not getting how and where should I write this command. Do I write it on Console? Yes I am using Xcode 8.3.3 so it used lldb debugger and llvm compiler. – Chandni Sep 21 '17 at 07:01
  • @ChandniSharma Xcode's menus, select View > Debug Area > Activate Console. From the console, you enter lldb commands when the app is paused, which you can do from the menus too, Debug > Pause. – Dave Lee Sep 25 '17 at 15:59
3
br se -f FooViewController.m -p '^@property|^ *- *\('

"br se" is short for "breakpoint set", pass your own filename to the -f argument, and the -p argument is a crude regex for properties and methods in Objective C.

Caveats: This doesn't seem to work for .h files, so if you have properties declared in the header that you want to watch then you may need to set watchpoints on their backing instance variables.

This is the best solution I have found so far, please post alternative solutions if you think they will be helpful.

tboyce12
  • 1,449
  • 14
  • 28