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 <iostream>using namespace std;long factorial (int n); int main () { int n (0); cin>>n; Long val=factorial (n); cout<<val<<endl; Cin.get (); return 0;} long factorial (int n) { long result (1); while (n--) { result*=n; } return result;}
Compile
g++ k.cpp-g-wall-werror-o Main
Start debugging
[[email protected] code]# gdb./maingnu gdb (gdb) Red Hat Enterprise Linux (7.2-83.el6) Copyright (C) free software Fo Undation, Inc.license gplv3+: GNU GPL version 3 or later
long factorial (int n), int. int main () 6 {7 int n (0); 8 cin>>n;9 long val=factorial (n); 10
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 break line_number/break [File_name]:line_number/break [File_name]:func_namer-runset <var> = <value>watch <var>enter:pressing ENTER key would execute the PR eviously 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
<?php. for ($i = 0; $i < $i + +) { echo $i. \ n "; Sleep (3); if (In_array ($i, [1,9,20])) { Print_r ($i * $i); Var_dump ($i * $i); Print $i * $i; } }
Start debugging, plus breakpoints
[[email protected] code]# gdb php GNU gdb (gdb) Red Hat Enterprise Linux (7.2-83.el6) Copyright (C) Free SOFTW Is Foundation, inc.license gplv3+: GNU GPL version 3 or later 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 <address 0X5A5A5A5A out of bounds> 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 = 0x5a5a5a5a <address 0x5a5a5a5a out of Bounds>, len = 1515870810}, HT = 0x5a 5a5a5a, 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 <zif_in_array>, module = 0x8bcf120}} (GDB) P Execute_data_ptr->fu Nction_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
PHP uses GDB to debug PHP programs