tags:

views:

1357

answers:

3

I'm having trouble running gprof on OS X. The file test.c is:

#include <stdio.h>

int main() {
  printf("Hello, World!\n");
  return 0;
}

and my terminal looks like:

$ gcc -pg test.c
$ gcc -pg -o test test.c
$ ./test
Hello, World!
$ gprof test
gprof: file: test is not of the host architecture

Edit: also, it doesn't generate the file gmon.out.

What's going on here?

+2  A: 

It sounds like test is built using an architecture that gprof doesn't expect. Try the following:

$ cat > test2.c
#include <stdio.h>
int main() { printf("test\n"); return 0; }
^D
$ gcc -arch i386 -pg -o test2 test2.c
$ file test2
test2: Mach-O executable i386
$ ./test2
test
$ gprof test2
... bunch of output ...
$ gcc -arch ppc -pg -o test2 test2.c
$ file test2
test: Mach-O executable ppc
$ ./test2
test
$ gprof test2
gprof: file: test2 is not of the host architecture
$ arch -ppc gprof test2
... same bunch of output ...

The newer MacOS supports running executables from either the IBM PPC and Intel x86 architecture. Some of the tool chain seems to be a bit dense about this. Gprof seems to expect the executable to be in the native architecture. However, if you use the arch utility to force the non-native architecture to be executed, then it seems to work properly. There was a discussion about this in another context a little while ago. I included some useful links and some more information there.

D.Shawley
When I compile either with -arch i386 or -arch ppc, I get the same error from gprof (and no gmon.out file is generated in either case)
Jesse Beder
That's strange... usually one or the other will work. What hardware and OS version are you running? The error is definitely caused by a mismatch between the executable's architecture and the host architecture. This is mentioned explicitly in the `gprof` manual page under _UNIVERSAL FILE SUPPORT_ on my MacBook Pro - Intel Core 2 Duo, OS 10.5.7.You can also use `arch` without arguments to see what the native architecture is and look at `gcc -v` to see what the `--target=` specification for the compiler is.
D.Shawley
I'm also using OS 10.5.7, Intel Core 2 Duo. Calling `arch` returns `i386`, and `gcc -v` shows `Target: i686-apple-darwin9`
Jesse Beder
You could try making a fat file by specifying more than one architecture. Something like `gcc -arch ppc -arch i686 -pg -o test test.c` will product a binary that contains both architecture. Gprof handles this file on my system. From what you say, `gcc` should be producing a i686 executable which is what `gprof` expects. Take a look at the `arch` man page. They describe a bunch of different system settings (env and others) that might be changing this.
D.Shawley
+2  A: 

Unfortunately, gprof does not work on Mac OS X. You'll probably want to use Shark instead. It is part of the developer tools in /Developer/Applications/Performance Tools/Shark.

Update: It appears as though gprof is now working on Mac OS X 10.6 (Snow Leopard), using the latest Developer Tools.

mark4o
Any reason why gprof doesn't work on OS X? Isn't OS X supposed to be UNIX tool-friendly?
Jesse Beder
+1  A: 

The series of events here is supposed to work as follows:

  1. Compile code with -pg option
  2. Link code with -pg option
  3. Run program
  4. Program generates gmon.out file
  5. Run gprof

The problem is that step 4 never happens. There's very little information out about this specific failure. The general consensus over the past few years seems to be that Apple would rather you use shark instead, and they've been very lax about fixing bugs and such with gprof.

In short: Install Xcode, man shark

Curtis Tasker
I'll have to check out shark. I've never had a problem getting a program to generate `gmon.out` provided that the program actually exits cleanly. IIRC, the profiler command line option inserts code into the initialization and termination of the runtime to trigger the capture of runtime counters and, ultimately, the writing of the output file. As long as the program exits via return from `main` or explicit call to `exit()`, it should generate an output file.
D.Shawley