4

I'm using xhprof for profiling an application. What I need to know is which file each function comes from, because some of the functions are global and some have duplicate names (it's legacy code, don't judge! ;) From what I could see xhprof doesn't return this data in its output, does anyone know otherwise?

I was hoping I won't have to modify the source code of xhprof to return this data... :-)

Thanks for your help!

Kat
  • 323
  • 2
  • 8

1 Answers1

4

Just wanted to say how I solved this in case other people find it useful.

I had to modify the xhprof source in the end, but it was a small change.

In xhprof.c, find a function called *hp_get_function_name and add the following to the list of variables right at the beginning after "int len;" :

  char              *finalName = NULL;
  int                finalLen;

Then right at the end of that function, swap the line that says "return ret;" for

finalLen = strlen(ret) + strlen(ops->filename) + 10;
finalName = (char*)emalloc(finalLen);
snprintf(finalName, finalLen, "%s|%s", ops->filename, ret);

return finalName;

This will prepend the file path followed by a pipe ( | ) to each function name, for example /path/to/foo.php|foo==>bar (without this modification xhprof would output just foo==>bar).

It's been a while since I've done C programming so there might be a better way of doing this, but it did the job for me.

It's easy to then parse this output in your implementation of iXHProfRuns - you just need to explode the string by | to get the file path and function names separately.

Hope this helps!

Kat
  • 323
  • 2
  • 8
  • Where can the result be seen or where is it logged? – Vidz Aug 07 '14 at 11:08
  • Same place as when you're using the unmodified xhprof, ie. if you use XHProfRuns_Default it'll be saved to filesystem. Have a look at https://github.com/phacility/xhprof/blob/master/xhprof_html%2Fdocs%2Findex.html under "a) Saving XHProf data persistently" to see an example of how it's done – Kat Aug 11 '14 at 10:55