Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cocoa: Break on every method call?

Often when debugging, it's important for me to know what methods of a class are being called in what order. The naive solution (that I've been using thus far) is to pop an NSLog at the top of each method. But this is time consuming, repetitive, ugly, and makes my code look juvenile if I forget to remove the logs after debugging.

A cleaner solution is to set breakpoints on each of my methods, configure their actions to issue the debugger command: po NSStringFromSelector(_cmd) and set them to automatically continue. This is prettier and saves me from having to remember to remove all those NSLogs, but is no less repetitive or time consuming.

What I really want is a way to set a symbolic breakpoint that breaks on every method (of a class? Of a module?). Any debugging/runtime masters have a solution or tips on where to start looking?

like image 487
jemmons Avatar asked Aug 05 '11 18:08

jemmons


People also ask

How to make a symbolic breakpoint?

In the Breakpoint navigator, click the Add button (+) in the lower-left corner, and choose Symbolic Breakpoint. Enter the object and symbol in the Symbol field, using the format of the example text. The debugger pauses when the app or your code calls the symbol you specify.

How to set breakpoint in Swift?

To make this happen, press Cmd+8 to choose the breakpoint navigator – it's on the left of your screen, where the project navigator normally sits. Now click the + button in the bottom-left corner and choose "Exception Breakpoint." That's it!

How to debug Xcode?

When you run an application in Xcode, the debugger is automatically started and attached to the process of the application. Click the Run button in the top left or press Command + R. From the moment the application is up and running, we can start inspecting the process and, if necessary, debug it.

How to see variable values in Xcode?

See variable values in code and the variable viewer When your app pauses at a breakpoint, hover over a variable in your source code to view its current value. If the variable is an image or other type that isn't expressible as text, click the Quick Look button at the upper-right to see a preview of the variable.


2 Answers

All Objective-C method calls go through one of the C runtime calls objc_msgSend, objc_msgSend_stret, objc_msgSendSuper and objc_msgSendSuper_stret per the 'Sending Messages' section of the Objective-C Runtime Reference. So you should be able to trap those, and give them actions to log the relevant parts of the first two parameters (it's target and selector for the normal sends, a struct describing the superclass that contains both the target and the class type for super calls).

like image 104
Tommy Avatar answered Oct 15 '22 10:10

Tommy


Use a logging system like Log4Cocoa. Logging the method, line number, and file from which it was called is a basic function of such logging systems.

Each logging call is given a detail level, specifying under what conditions it should log. You would then set the detail level to get the desired amount of information. Products built for release, for examples, would only log errors and perhaps warnings; debug level would log everything.

like image 20
Joshua Nozzi Avatar answered Oct 15 '22 09:10

Joshua Nozzi