xcodemacosprofilinginstruments

How can I attach Instruments.app to a command line program on OS X?


I've create a profiling profile in Instruments.app from the blank template, and I'd like to use it to profile a command line program. The problem is that the program exits rather quickly (in a matter of seconds), which means I can't simply select it in the Instruments process dropdown to attach to it while it's running.

Is there a way to use an existing Instruments profile while launching a command line application, and start collecting data immediately, instead of attaching to a running process?

I do not have an Xcode project, or even the source code. I simply want to attach to an existing application right after it starts up. If it is relevant I'm using Instruments 6.1 on OS X Yosemite.

If there was a way to somehow start the profiling directly from the command line, and not from the Instruments GUI, that would be even better, but I'm not sure if such thing is possible.


Solution

    1. Build a command line executable with debug symbols enabled:

      gcc -Wall -g -O3 profile_me.c -o profile_me

    2. Launch Instruments, select Time Profiler.

    enter image description here

    1. At the top left corner of the window, to the right of the pause button, you should see the name of your machine - click on this and then select Choose target... from the hierarchical menu to the right.

    enter image description here

    1. In the Choose target dialog, navigate to your executable and select it. Also set any command line options, environment variables, and the working directory, if needed.

    enter image description here

    1. Click on the Choose button to save the target options and dismiss the Choose target dialog - you should now see the target name to the right of your machine name in the top left corner of the Time Profiler window:

    enter image description here

    1. Hit the red "record" button to launch your executable and start profiling!

    enter image description here