views:

238

answers:

3

In python, one can trace all the statements that are executed by a command line script using the trace module. In bash you can do the same with set -x. We have a PHP script that we're running from the command line, like a normal bash / python / perl / etc script. Nothing web-y is going on.

Is there anyway to get a trace of all the lines of code that are being executes?

+1  A: 

Not in pure-PHP, no -- as far as i know.

But you can use a debugger ; a nice way to do that is with

  • The extension Xdebug, which can be used as a debugger
  • and some graphical IDE that integrates some debugging tools, like Eclipse PDT

Both of those are free, btw.

With those, you can do step by step, set up breakpoints, watch the content of variables, view stack traces, ... And it works both for Web and CLI scripts ;-)

Of course, it means having Eclipse running on the machine you are executing your script... But if you are executing it on your development machine, you probably have a GUI and all that, so it should be fine...
(I know that, for web applications, you can have Eclipse running on a different machine than the one with the PHP webserver -- don't know if it's possible in CLI, though)


As a sidenote : maybe you can integrate Xdebug with a CLI-based debugger ; see the page I linked to earlier for a list of supported tools.

Pascal MARTIN
I found one of the best ways to use xdebug was to just analyze the trace files it spits out. You can run diffs on them, parse them with perl, etc. If you've got two similar use cases, and one works and the other doesn't, a diff can be useful to show where they start to diverge. I found `vimdiff` (which is a built-in mode of operation of the ubiquitous `vim` editor) was quite capable in this respect, able to deal with very large files (eg a module-laden Drupal install) without balking.
intuited
Also it is quite possible to use xdebug in pure CLI mode. You can configure it to do traces on php code invoked from the command line, and actually you can hook vim up as a debugger. I guess that's not pure cli, since vim runs in curses. Anyway there is info on using vim as a PHP debugger here: http://tech.blog.box.net/2007/06/20/how-to-debug-php-with-vim-and-xdebug-on-linux/#comment-39681
intuited
Err whoops, realized you were talking about doing *remote* debugging with cli. I didn't try to do this, but I think that it would be possible to *debug* it remotely but you'd have to ssh in to actually run the command.
intuited
A: 

I'm kinda blind here but I guess one way you could do it is to write all the relevant code inside custom functions and call debug_backtrace(). debug_print_backtrace may also be useful.

I hope it helps.

Alix Axel
Yes that's one option, but I was looking for something more high level than 'lots of print statemtents'
Rory
+2  A: 

There is a PECL extension, apd, that will generate a trace file.

Ken Keenan