Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trace where code is coming from (PHP)

Tags:

php

trace

I'm going through a customer's server, running crazy proprietary forum software (vBulletin) and even worse SEO mods (vbseo). I cannot figure out where the php code for a page is coming from! How to trace this URL back to a PHP page: http://www.example.com/forum/members/connie.html I just joined a project with the code based on a heavily modified vBullitin install with the VBSEO plugin. This particular plugin is horrific spaghetti code with tens of include()s, .htaccess redirects and possibly .httpd.conf changes. Then it pulls strings from a database so I cannot even use grep to find the code file!

Is there any way to stack-trace PHP to log all the code that runs to produce a page? I have root access but I am not supposed to stop or restart the server. A simple list of the include() hierarchy of files that went into producing the page would suffice.

Note that I cannot use debug_backtrace because I don't know where the code I'm looking for is! The debug_backtrace function is the exact opposite of what I need.

Thanks.

like image 233
dotancohen Avatar asked Aug 28 '11 16:08

dotancohen


People also ask

How can I trace a PHP program?

You can use a PHP extension called : XHProf, developed by Facebook. It is capable of reporting function-level call counts and inclusive and exclusive wall time, CPU time and memory usage. Xhprof is very useful for performance tracing on local and production environment but not for debugging other problems.

What is PHP stack trace?

But, what is a stack trace? In essence, it is a rundown of every file and function that is called leading up to the error. To be clear, a stack trace doesn't include the files and functions that are touched before the error occurred, only the chain of methods that are called as the error happened.

What is xdebug trace?

Xdebug allows you to log all function calls, including parameters and return values to a file in different formats. Those so-called "function traces" can be a help for when you are new to an application or when you are trying to figure out what exactly is going on when your application is running.

What is the Backtrace in PHP?

Definition and UsageThe debug_backtrace() function generates a PHP backtrace. This function displays data from the code that led up to the debug_backtrace() function. The current call type.


2 Answers

Sounds like you need to step through it with Xdebug. Most common IDE's support it such as Netbeans and PHPStorm.

Resources:

  • Tracing PHP apps with Xdebug
  • Xdebug with Netbeans
  • Xdebug with PHPstorm (I recommend)
  • Xdebug with Eclipse
  • Chrome Xdebug extension (I recommend)
  • Firefox Xdebug plug-in

In both the above mentioned IDE's, you can CTRL+Click a function/method and it will take you to the line in the file where it is defined. You can also track usages for both functions and variables.

Tracing code is built-in to xdebug. Here's an example from Zend:

<?php

  xdebug_start_trace('c:/data/fac.xt');

  print fac(7);

  function fac($x)
  {
    if (0 == $x) return 1;
    return $x * fac($x - 1);
  }

  xdebug_stop_trace();

?>

Trace file output:

TRACE START [2007-10-26 12:18:48]
    0.0068      53384     -> fac() C:\www\fac.php:5
    0.0069      53584       -> fac() C:\www\fac.php:10
    0.0069      53840         -> fac() C:\www\fac.php:10
    0.0070      54096           -> fac() C:\www\fac.php:10
    0.0070      54376             -> fac() C:\www\fac.php:10
    0.0071      54656               -> fac() C:\www\fac.php:10
    0.0072      54936                 -> fac() C:\www\fac.php:10
    0.0072      55216                   -> fac() C:\www\fac.php:10
    0.0073      55392     -> xdebug_stop_trace() C:\www\fac.php:13
    0.0237      55392
TRACE END   [2007-10-26 12:18:48]
like image 111
AlienWebguy Avatar answered Oct 23 '22 10:10

AlienWebguy


Check out the debug_backtrace function - this should always be available, even on production servers.

like image 37
Robin Avatar answered Oct 23 '22 10:10

Robin