Tracing functions with Instruments

Tracing functions with Instruments

In Squish we need to intercept the events when recording user interactions and on play back we have to send the events again. So we have to know some details on how the event system in the toolkits work.

To get those details, it is sometimes helpful if you could actually see the functions an application calls. When this need arised on the Mac, I used to use the Activity Monitor which has the ability to sample the application. This is unfortunately not very exact since the samples happen on certain intervals, so you are never sure if the relevant functions are actually sampled.

But Leopard includes the DTrace facility and the graphical frontend Instruments. This allows you to trace a lot of internals of the application, including the functions an application called.

For example you can create in Instruments a new “instrument” that traces all Objective-C calls: simply choose “Build New Instrument…” from the “Instrument” menu and choose the “Probe” of type “Objective-C”. In the dialog I also chose to record the data for “Function” and “Module”. Then just select a target application and press the “Record” button and all the Objective-C calls (including the call stack) are recorded in instruments.

Pretty useful.

But you can also record C function calls: just create a new instrument, this time of type “User Process”. If you record now, you get all the C function calls. This is getting really big really fast (and as a result it the application gets really slow). So what you probably want to do in this case is to limit the functions recorded. I was particularily interested in the functions called in the CoreFoundation framework, so I entered in the line edit after the “hits” text “CoreFoundation” and now the instrument only records functions called in that framework.


Leave a reply

Your email address will not be published. Required fields are marked *