"PHP" Debugging PHP program using GDB, GDB debugging PHP Program
1. Introduction
GDB is a powerful UNIX program debugging tool released by the GNU Open source organization. If you are doing software under the UNIX platform, you will find that GDB has a more powerful debugging tool than VC, BCB's graphical debugger. GDB also has a graphical debug side such as DDD.
2. Debugging and C + + program
directly on the code.
#include
Using namespace std;long factorial (int n); int main () {int n (0); cin>>n; Long val=factorial (n); cout<
Compile
g++ k.cpp-g-wall-werror-o Main
Start debugging
[Root@localhost code]# gdb./maingnu gdb (gdb) Red Hat Enterprise Linux (7.2-83.el6) Copyright (C) free software Found ation, Inc.license gplv3+: GNU GPL version 3 or later
This was free software:you was free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law. Type "Show copying" and "show warranty" for details. This GDB is configured as "I686-redhat-linux-gnu". For bugs reporting instructions, please see:
... Reading symbols From/code/main...done. (GDB) L Warning:source file is more recent than executable.1 #include
2 using namespace std;3 long factorial (int n), int main () 6 {7 int n (0); 8 cin >>n;9 long val=factorial (n); cout<
Set Breakpoint Break linenumber
(GDB) b 9Breakpoint 1 at 0x80486f9:file K.cpp, line 9. (GDB) rstarting program:/code/main 4Breakpoint 1, Main () at k.cpp:99 long val=factorial (n);
Set observation Point Watch Var
(GDB) sfactorial (n=4) at k.cpp:1717 Long result (1);(gdb) l12 return 0;13}1415 long factorial (int n)-{+ Long result (1); n--{result*=n;21} (GDB) Watch Nhardware watchpoint 2:n (gdb) Watch Resulthardware watchpoint 3:result (gdb) ccontinuing.hardware watchpoint 3:re Sultold value = 0New value = 1factorial (n=4) at k.cpp:1818 while (n--) (gdb) Continuing.hardware watchpoint 2:nol D value = 4New value = 30x08048764 in factorial (n=3) at k.cpp:1818 while (n--) (GDB) Continuing.hardware Watchpoin T 3:resultold value = 1New value = 3factorial (n=3) at k.cpp:1818 while (n--) (GDB) Continuing.hardware Watchpoint 2:nold value = 3New value = 20x08048764 in factorial (n=2) at k.cpp:1818 while (n--) (gdb) Continuing.hardware WA Tchpoint 3:resultold value = 3New value = 6factorial (n=2) at k.cpp:1818 while (n--) (gdb) Continuing.hardware wat ChPoint 2:nold value =2New value = 10x08048764 in factorial (n=1) at k.cpp:1818 while (n--) (gdb) Continuing.hardware watchpoint 2:nold Value = 1New value = 00x08048764 in factorial (n=0) at k.cpp:1818 while (n--) (gdb) Continuing.watchpoint 2 deleted Because the program have left the block Inwhich it expression is valid. Watchpoint 3 deleted because the program have left the block inwhich its expression was valid.0x08048705 in main () at K.cpp : Val=factorial long (n);(gdb) P val$1 = 11476980 (GDB)
Can see is while there, causing N to cross over, fix
while (n>0)//DOESN ' t-let n reach 0{ result*=n; n--; Decrements only after the evaluation}
Some quick commands
L–listp–print Print {Variable}c–continues–stepb-break =
watch
ENTER: pressing enter key would execute the previously executed command again.
The difference between C/N/S
- C or Continue:debugger would continue executing until the next break point.
- N or Next:debugger would execute the next line as a single instruction.
- S or Step:same as Next, but does does treats function as a single instruction instead goes into the function and executes It line by line
3. Debug PHP Program
PHP code
Start debugging, plus breakpoints
[Root@localhost code]# gdb php GNU gdb (gdb) Red Hat Enterprise Linux (7.2-83.el6) Copyright (C) free software Foun Dation, Inc.license gplv3+: GNU GPL version 3 or later
This was free software:you was free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law. Type "Show copying" and "show warranty" for details. This GDB is configured as "I686-redhat-linux-gnu". For bugs reporting instructions, please see:
... Reading symbols From/usr/bin/php...done. (GDB) b zif_sleepbreakpoint 1 at 0X8435180:FILE/USR/LOCAL/SRC/PHP-5.5.23/EXT/STANDARD/BASIC_FUNCTIONS.C, line 4449. (GDB) b zif_in_arraybreakpoint 2 at 0X8426923:FILE/USR/LOCAL/SRC/PHP-5.5.23/EXT/STANDARD/ARRAY.C, line 1215. (GDB) b zif_print_rbreakpoint 3 at 0X8438273:FILE/USR/LOCAL/SRC/PHP-5.5.23/EXT/STANDARD/BASIC_FUNCTIONS.C, line 5553. (GDB) b zif_var_dumpbreakpoint 4 at 0X847D296:FILE/USR/LOCAL/SRC/PHP-5.5.23/EXT/STANDARD/VAR.C, line 178. (GDB) b zif_printffunction "zif_printf" not defined. Make breakpoint pending on the future shared library load? (Y or [n]) n (gdb) b zif_sprintffunction "zif_sprintf" not defined. Make breakpoint pending on the future shared library load? (Y or [n]) n (gdb) b printfbreakpoint 5 at 0x806a390 (GDB) b memcpybreakpoint 6 at 0x8069390 (GDB) b zif_printfunction "zif_p Rint "not defined. Make breakpoint pending on the future shared library load? (Y or [n]) n (gdb) b zif_echo Function "Zif_echo" not defined. Make Breakpoint PenDing on the future shared library load? (Y or [n]) n (gdb) info bnum Type Disp Enb Address What1 breakpoint keep y 0x08435180 in zif_s Leep at/usr/local/src/php-5.5.23/ext/standard/basic_functions.c:44492 Breakpoint Keep y 0x08426923 in Zif_in_ Array at/usr/local/src/php-5.5.23/ext/standard/array.c:12153 breakpoint keep y 0x08438273 in Zif_print_r at/ usr/local/src/php-5.5.23/ext/standard/basic_functions.c:55534 Breakpoint Keep y 0x0847d296 in Zif_var_dump at /usr/local/src/php-5.5.23/ext/standard/var.c:1785 Breakpoint Keep y 0x0806a390
6 Breakpoint Keep y 0x08069390
(gdb)
Add a few breakpoints to test syntax:break [File_name]:func_name, here's a general look at echo print and so on is not a function
and start debugging.
(GDB) Run kk.php (or set args./kk.php && R) starting program:/usr/bin/php kk.php[thread debugging using Libthrea d_db Enabled]0breakpoint 3, Zif_sleep (Ht=1, return_value=0xb7fbd6f0, return_value_ptr=0x0, this_ptr=0x0, Return_value _used=0) at/usr/local/src/php-5.5.23/ext/standard/basic_functions.c:44494449
Here we can take a look at what the Zif_sleep function return_value return
(GDB) P *return_value$1 = {value = {lval = 1515870810, dval = 1.7838867517321418e+127, str = {val = 0x5a5a5a5a, len = 1515870810}, ht = 0x5a5a5a5a, obj = {handle = 1515870810, handlers = 0x5a5a5a5a}}, refcount__gc = 1, type = 0 ' \000 ', is_ ref__gc = 0 ' \000 '} (GDB) p return_value->value$2 = {lval = 1515870810, dval = 1.7838867517321418e+127, str = {val = 0x5 A5A5A5A, len = 1515870810}, ht = 0x5a5a5a5a, obj = {handle = 1515870810, handlers = 0x5a5a5a5a}} (GDB) P return_value- >value->lval$3 = 1515870810
We can also use the built-in Gdbinit to debug
(GDB) Source/usr/local/src/php-5.5.23/.gdbinit (GDB) zbacktrace[0xb7fa1144] Sleep (3)/code/kk.php:4
View current stack, PHP kernel execution process
(GDB) bt#0 zif_sleep (ht=1, return_value=0xb7fbd6f0, return_value_ptr=0x0, this_ptr=0x0, return_value_used=0) at/usr/ Local/src/php-5.5.23/ext/standard/basic_functions.c:4449#1 0x085f6870 in Execute_internal (execute_data_ptr= 0xb7fa1144, fci=0x0, return_value_used=0) at/usr/local/src/php-5.5.23/zend/zend_execute.c:1484#2 0x085aea5f in Dtrac E_execute_internal (execute_data_ptr=0xb7fa1144, fci=0x0, return_value_used=0) at/usr/local/src/php-5.5.23/zend/ Zend_dtrace.c:97#3 0x00935c33 in Pt_execute_core (internal=1, execute_data=0xb7fa1144, fci=0x0, rvu=0) at/usr/local/s Rc/trace-0.3.0/extension/trace.c:941#4 0x00935e49 in Pt_execute_internal (execute_data=0xb7fa1144, fci=0x0, Return_ value_used=0) at/usr/local/src/trace-0.3.0/extension/trace.c:1005#5 0x085f7523 in Zend_do_fcall_common_helper_SPEC (execute_data=0xb7fa1144) at/usr/local/src/php-5.5.23/zend/zend_vm_execute.h:552#6 0x085fb2a9 in ZEND_DO_FCALL_ Spec_const_handler (execute_data=0xb7fa1144) AT/USR/LOCAL/SRC/php-5.5.23/zend/zend_vm_execute.h:2332#7 0x085f6deb in EXECUTE_EX (execute_data=0xb7fa1144) at/usr/local/src/ Php-5.5.23/zend/zend_vm_execute.h:363#8 0X085AE9DC in DTRACE_EXECUTE_EX (execute_data=0xb7fa1144) at/usr/local/src/ Php-5.5.23/zend/zend_dtrace.c:73#9 0x00935c5e in Pt_execute_core (internal=0, execute_data=0xb7fa1144, fci=0x0, rvu=0 ) at/usr/local/src/trace-0.3.0/extension/trace.c:946#10 0x00935e10 in PT_EXECUTE_EX (execute_data=0xb7fa1144) at/usr /local/src/trace-0.3.0/extension/trace.c:1000#11 0x085f6e4a in Zend_execute (OP_ARRAY=0XB7FBC7B4) at/usr/local/src/ Php-5.5.23/zend/zend_vm_execute.h:388#12 0x085c1cf2 in Zend_execute_scripts (type=8, retval=0x0, file_count=3) at/usr /local/src/php-5.5.23/zend/zend.c:1327#13 0x085470f9 in Php_execute_script (PRIMARY_FILE=0XBFFFF4A4) at/usr/local/ Src/php-5.5.23/main/main.c:2525#14 0x0865af46 in Do_cli (argc=2, argv=0x8b9b908) at/usr/local/src/php-5.5.23/sapi/ Cli/php_cli.c:994#15 0x0865bff3 in Main (argc=2, argv=0x8b9b908) at /usr/local/src/php-5.5.23/sapi/cli/php_cli.c:1378
Viewing Code Snippets
(GDB) l4444 Delay for a given number of seconds */4445 php_function (sleep) 4446 {4447 long num;44484 449 if (Zend_parse_parameters (Zend_num_args () tsrmls_cc, "L", &num) = = FAILURE) {4450 RE turn_false;4451}4452 if (num < 0) {4453 php_error_docref (NULL tsrmls_cc, E_wa Rning, "Number of seconds must is greater than or equal to 0");(gdb) L 44504445 php_function (sleep) 4446 {4447 Long num;44484449 if (Zend_parse_parameters (Zend_num_args () tsrmls_cc, "L", &num) = = FAILURE) {4450 return_false;4451}4452 if (num < 0) {4453 php_error_docref (N ULL tsrmls_cc, E_warning, "Number of seconds must is greater than or equal to 0"); 4454 Return_false; (GD b) L zif_usleep4463/*}}} */44644465/* {{{proto void usleep (int micro_seconds) 4466 Delay for a given number of micro seconds */4467 Php_function (usleep) 4468 {4469 #if have_usleep4470 long num;44714472 if (zend_parse_parame Ters (Zend_num_args () tsrmls_cc, "L", &num) = = FAILURE) {
Continue execution
(GDB) n4452 if (num < 0) {(GDB) p num$6 = 3 (gdb) n4457 return_long (php_sleep (num));(gdb) n4462 } (GDB) nexec Ute_internal (execute_data_ptr=0xb7fa1144, fci=0x0, return_value_used=0) at/usr/local/src/php-5.5.23/zend/zend_ execute.c:14881488 }
To Execute_internal, you can look at a state of the current function
(GDB) P execute_data_ptr$7 = (Zend_execute_data *) 0xb7fa1144 (GDB) P *execute_data_ptr$8 = {opline = 0XB7FBCACC, Function_ State = {function = 0x8bcf3e8, arguments = 0xb7fa119c}, Op_array = 0xb7fbc7b4, Object = 0x0, symbol_table = 0X8B99CDC, Prev_execute_data = 0x0, old_error_reporting = 0x0, nested = 0 ' \000 ', original_return_value = 0X38B4AC9, Current_sco PE = 0x49, Current_called_scope = 0x45, Current_this = 0x0, Fast_ret = 0x0, call_slots = 0xb7fa1188, call = 0xb7fa1188 } (GDB) p *execute_data_ptr->function_state.function->common->function_name$9 = "s" (GDB) p Execute_data_ Ptr->function_state.function->common->function_name $ = 0x8af03c9 "Sleep" (GDB) p EXECUTE_DATA_PTR->OP_ array->filename$11 = 0xb7fbc8e8 "/code/kk.php"
View current Hashtable
(GDB) P *execute_data_ptr->symbol_table$16 = {ntablesize = +, Ntablemask = Nnumofelements = 8, nnextfreeelement = 0, Pinternalpointer = 0xb7fb924c, plisthead = 0xb7fb924c, Plisttail = 0xb7fbd228, arbuckets = 0xb7fb9120, pDestructor = 0x85bf06f <_zval_ptr_dtor_wrapper>, persistent = 0 ' \000 ', Napplycount = 0 ' \000 ', bapplyprotection = 1 ' \001 ' , inconsistent = 0}
After you continue with output C, you can also see the execution information for the In_array
(GDB) P *execute_data_ptr->function_state.function$24 = {type = 1 ' \001 ', common = {type = 1 ' \001 ', function_name = 0x 8af1841 "In_array", scope = 0x0, Fn_flags = $, prototype = 0x0, Num_args = 3, Required_num_args = 2, Arg_info = 0x8a e7554}, Op_array = {type = 1 ' \001 ', function_name = 0x8af1841 "In_array", scope = 0x0, Fn_flags =, prototype = 0x 0, Num_args = 3, Required_num_args = 2, Arg_info = 0x8ae7554, RefCount = 0x842691d, opcodes = 0x8bcf120, last = 0, var s = 0x0, Last_var = 0, T = 1, nested_calls = 3086618796, Used_stack = 0, Brk_cont_array = 0x0, Last_brk_cont = 1, Try_ Catch_array = 0xb7fa10dd, Last_try_catch =, Has_finally_block = \240 ', Static_variables = 0x0, This_var = 1148 2064, filename = 0xaf1ff4 "|\035\257", Line_start = 11482016, Line_end = 146381272, doc_comment = 0xbffff238 "x\36 2\377\277\244\ay\b\021 ", Doc_comment_len = 10305959, early_binding = 11085989, literals = 0x8b7a0a0, last_literal = 14 0062666, Run_time_cache= 0xb7fa10d4, Last_cache_slot = all, reserved = {0x9, 0x8b5f7ac, 0x796, 0x0}}, internal_function = {type = 1 ' \001 ', Function_name = 0x8af1841 "In_array", scope = 0x0, Fn_flags = $, prototype = 0x0, Num_args = 3, Required_num_args = 2, Arg_info = 0x8ae7554, handler = 0x842691d
, module = 0x8bcf120}} (GDB) p execute_data_ptr->function_state.function->common->function_name $26 = 0x8af1841 "In_array" (GDB) p Execute_data_ptr->op_array->filename $27 = 0xb7fbc8e8 "/code/kk.php"
You can also add monitoring watch, set some debug variable set, etc.
Other debugging tools include strace to view system calls, Ltrace to view class library calls, vld view opcode
Reference articles
Http://www.cprogramming.com/gcc.html
http://www.thegeekstuff.com/2010/03/debug-c-program-using-gdb/
Http://www.cprogramming.com/gdb.html
Http://www.laruence.com/2011/06/23/2057.html
Http://derickrethans.nl/what-is-php-doing.html
http://www.bkjia.com/PHPjc/1078398.html www.bkjia.com true http://www.bkjia.com/PHPjc/1078398.html techarticle "PHP" Debugging PHP program using GDB, GDB debugging PHP 1, Introduction GDB is the GNU Open source organization released a powerful UNIX program debugging tools. If you are doing software under the UNIX platform, ...