Does xhprof log the file where the function came from?

553 Views Asked by At

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!

1

There are 1 best solutions below

2
On

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!