Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 615
Jakub Arnold Avatar asked Nov 28 '14 22:11

Jakub Arnold


People also ask

How do I play an instrument on my Mac?

Instruments will be installed to your system drive, in /Developer/Applications/ -- run it from there, or from the Xcode application menu item select Open Developer Tool->Instruments (in newer versions of Xcode). When Instruments first starts, you must make a template choice.

How do I use instruments in Xcode?

Instrumenting AllocationsPress Command-I in Xcode, select Allocations from the list and press Choose. After a moment, you'll see the Allocations instrument. It should look familiar because it looks a lot like Time Profiler. Click the record button in the top-left corner to run the app.

What is instruments on Mac?

Instruments is a performance-analysis and testing tool for iOS, iPadOS, watchOS, tvOS, and macOS apps.


1 Answers

  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 you 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

like image 198
Paul R Avatar answered Oct 02 '22 23:10

Paul R