Problem with gprof on OS X: [program] is not of th

2020-02-26 09:17发布

问题:

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?

回答1:

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 use shark instead, and they've been very lax about fixing bugs and such with gprof.

In short: Install Xcode, man shark



回答2:

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.



回答3:

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.



标签: macos gcc gprof