PDA

View Full Version : gprof not working


JoshG
10-18-2005, 05:02 PM
I have a new (just a few months old) iMac G5 running OS 10.4.2. I'm trying to profile a C application and I cannot get gprof to work! It's driving me nuts! NOTHING I have done works!

Basically, gprof is telling me that all my applications don't take any time to run. Here's sample output:


% cumulative self self total
time seconds seconds calls ms/call ms/call name
0.0 0.00 0.00 11455 0.00 0.00 _is_empty [25]
0.0 0.00 0.00 8250 0.00 0.00 _make_nonempty [26]
0.0 0.00 0.00 1600 0.00 0.00 _insert_list [27]
0.0 0.00 0.00 4 0.00 0.00 _make_empty [28]
0.0 0.00 0.00 3 0.00 0.00 _get_num [29]
0.0 0.00 0.00 2 0.00 0.00 ___stub_getrealaddr [30]
0.0 0.00 0.00 2 0.00 0.00 _append_lists [31]
0.0 0.00 0.00 2 0.00 0.00 _make_list [32]
0.0 0.00 0.00 2 0.00 0.00 _make_list_helper [33]
0.0 0.00 0.00 2 0.00 0.00 _print_data [34]
0.0 0.00 0.00 2 0.00 0.00 _print_list [35]
0.0 0.00 0.00 1 0.00 0.00 _get_data [36]
0.0 0.00 0.00 1 0.00 0.00 _main [37]
0.0 0.00 0.00 1 0.00 0.00 _sort_data [38]
0.0 0.00 0.00 1 0.00 0.00 _sort_list [39]


I've tried this with several apps... nothing works... I have no idea what's going on. Here's the code that I'm trying to profile (just sample code to see if I can use gprof):


/* Example program for time profiling. */


#include <stdio.h>
#include <stdlib.h>

struct AList {
int first;
struct AList * rest;
};
typedef struct AList * List;

List data;

List make_empty()
{
return NULL;
}

List make_nonempty(int first, List rest)
{
List list = (List) malloc(sizeof(struct AList));
if (list == NULL) {
fprintf(stderr,"Couldn't allocate cons-cell.\n");
exit(1);
}

list->first = first;
list->rest = rest;

return list;
}

/* Returning an int, instead of bool, so that we can use the
* non-C99 compliant cc. */
int is_empty(List list)
{
return (list == NULL);
}

List make_list_helper(unsigned int size, List accum)
{
if (size == 1)
return make_nonempty(size,accum);
else
return make_list_helper(size-1,make_nonempty(size,accum));
}

List make_list(unsigned int size)
{
return make_list_helper(size,make_empty());
}

List append_lists(List list1, List list2)
{
List return_list;

if (is_empty(list1))
return list2;
else {
return_list = make_nonempty(list1->first,
append_lists(list1->rest,list2));
free(list1);
return return_list;
}
}

unsigned int get_num()
{
unsigned int n;
scanf("%u",&n);
return n;
}

/* Do interesting stuff to get some data. */
void get_data()
{
unsigned int size;

data = make_empty();

printf("To create some data, enter a sequence of positive numbers.\n");
printf("Terminate the list with a zero.\n");
printf("Use at least moderately large numbers to create enough data for good profiling.\n");
while ((size=get_num()) != 0)
data = append_lists(data,make_list(size));
}

List insert_list(int n, List list)
{
List return_list;

if (is_empty(list))
return make_nonempty(n,make_empty());
else if (n <= list->first)
return make_nonempty(n,list);
else {
return_list = make_nonempty(list->first,insert_list(n,list->rest));
free(list);
return return_list;
}
}

List sort_list(List list)
{
List return_list;

if (is_empty(list))
return list;
else {
return_list = insert_list(list->first,sort_list(list->rest));
free(list);
return return_list;
}
}

void sort_data()
{
data = sort_list(data);
}

void print_list(List list)
{
if (is_empty(list))
printf("\n");
else {
printf("%d ",list->first);
print_list(list->rest);
}
}

/* Print out whatever data we have. */
void print_data()
{
printf("The data:\n");
print_list(data);
}


int main()
{
get_data();
print_data();

/* Do something interesting on the data. */
sort_data();

print_data();

return 0;
}


Here's what I do to test gprof:


$ gcc -pg sample.c
$ ./a.out
(results of program)
$ gprof a.out gmon.out


I have tried setting the output of gcc to "sample", that doesn't work. I've tried using CC, that doesn't work. I would try to reinstall gprof but I can't find the source. I'm really lost here. :(

Thanks,
Josh

szymczyk
10-18-2005, 08:33 PM
Try the program Saturn, which is part of the CHUD Tools. Saturn works similarly to gprof. It takes gprof's gmon.out files as input. If Saturn reports 0 time, use Shark to profile your program. It's also part of the CHUD Tools, and it's more powerful than gprof.

I don't know why gprof would report your program as taking 0 time. The only thing that comes to mind is you didn't run the program long enough because gprof did report the number of times you called each function. But I'm assuming you did run it long enough.

JoshG
10-18-2005, 11:54 PM
I tried Shark and it works great... thanks so much :)

something
03-06-2008, 08:18 PM
I realize that my reply is being posted several years after the original
question. However, I have been having this same mac osx xcode gprof
problem all day long so I thought I would post what I found on as many
forums as possible (within the next 10 minutes anyway).

I apologize if my comments are not directly applicable to the original
situation of the person who started this thread. However, this forum thread
kept coming up in my google searches today, so there have got to be other
people with MY problem that also end up here.

gprof only works on the Mac if you are NOT using an Intel mac. If you are
using an Intel Mac, then STOP WASTING your time trying to fix the gprof "all
zero seconds" problem. Apple says that gprof does not work for the Intel
Mac.

Saturn is what you want to use. People may have told you to use Shark, but
shark does sampling, which is not what gprof does. Saturn is much more
similar to Shark.

go to
developer.apple.com

search for Shark (this is part of Developer Performance Tools "CHUD Tools").

Find and download SaturnUserGuide.pdf (2007-10-31 Copyright 2007 Apple Inc.
All Rights Reserved).

You will have to use this flag:
-finstrument-functions

I am using that flag under "C flags" and under "linker flags" (in Target Info
"Build" tab)

ALSO: you have to GET RID OF the "-pg" flag that you were probably trying to
use when you still thought there was hope to get gprof working.

something
03-07-2008, 12:31 PM
arg !!

I typed Shark where I meant to type Saturn. Wow, that almost completely
undermined my point....

Where I said:

>> go to
>> developer.apple.com
>>
>> search for Shark

I MEANT TO SAY:

>> go to
>> developer.apple.com
>>
>> search for SATURN

something
03-07-2008, 12:35 PM
Wow, I just noticed another typo in my post from yesterday.

I must have been even more frazzled than I thought after having struggled with the mac version of gprof all day.

Where I typed:

>> Saturn is much more similar to Shark.

I SHOULD HAVE TYPED:

>> Saturn is much more similar to gprof.