by jrockway · 15y ago · 0 comments
Here's what you do. Step 1: compile PHP with debugging symbols. Then run the test case in GDB:
$ gdb `which php`
(gdb) set args testcase.php
(gdb) run
<program hangs>
Then hit C-c, and see where the program is:
^C
Program received signal SIGINT, Interrupt.
0x0000000000703898 in ?? () #0 0x0000000000703898 in ?? ()
#1 0x00000000006aae40 in execute ()
#2 0x00007ffff4400116 in ?? () from /usr/lib/php5/20090626/suhosin.so
#3 0x000000000068290d in zend_execute_scripts ()
#4 0x000000000062e1a8 in php_execute_script ()
#5 0x000000000071317a in ?? ()
#6 0x00007ffff5475c4d in __libc_start_main (main=<value optimized out>, argc=<value optimized out>, ubp_av=<value optimized out>,
init=<value optimized out>, fini=<value optimized out>, rtld_fini=<value optimized out>, stack_end=0x7fffffffe9c8)
at libc-start.c:228
#7 0x000000000042d4b9 in _start ()
Now you have some idea of where to look. (Note: this is not the actual bug, as I can't reproduce it on my machine. This is <?php while(1){} ?>, which is just as good for demonstration purposes. Also, no debugging symbols, so we don't really know what's going on.)No offense, but this is like programming 101.
Thats a bit harsh. You could have happily posted the above (useful) comment without that dig at the end.
It might be if you grew up in a C/C++ environment, many people these days start with scripting languages and don't venture out (myself included, but learning C is my new year's resolution).
require 'rubygems'
require 'ruby-debug'
Debugger.start
Signal.trap('INT') { debugger } # this line makes the debugger run when you hit ctrl-c
# YOUR CODE HERE INSTEAD!
loop {}
Simply hit ctrl-C (sometimes twice) when your code is hung or whatever and then issue the `where` command to rdb. Just make sure you have the ruby-debug gem installed.It's actually easier, as you obviously don't have to worry about debugging symbols. You can also attach rdb to running interpreter processes and all that good stuff.
http://bashdb.sourceforge.net/ruby-debug.html
EDIT: I've just realised that this will obviously only catch bugs in ruby code. For bugs in the ruby interpreter itself, like the php bug under discussion, you will need to use gdb on the interpreter binary like jrockway showed. Still the point is that you shouldn't consider this stuff over your head just because you only hack dynamic languages :)