How to use GDB to debug a PHP program _php instance

Source: Internet
Author: User
Tags print print sapi usleep
In general, GDB accomplishes the following four main functions:

(1) Start your program, you can follow your custom requirements to run the program at will.
(2) allows the program to be debugged to stop at the breakpoint where you specified the reset. (Breakpoint can be a conditional expression)
(3) When the program is stopped, you can check what happens in your program at this time.
(4) Dynamically change the execution environment of your 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

1

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&lt ;
      
       

Set Breakpoint Break linenumber

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) while (n--) {result*=n;21} (gdb) watch Nhardware watchpoint 2:n (gdb) Watch Resulthardware watchpoint 3:result (GD b) Ccontinuing.hardware watchpoint 3:resultold value = 0New value = 1factorial (n=4) at k.cpp:1818 while (n--) (GDB) Contin Uing. Hardware watchpoint 2:nold value = 4New value = 30x08048764 in factorial (n=3) at k.cpp:1818 while (n--) (GDB) Continuing.H Ardware watchpoint 3:resultold value = 1New value = 3factorial (n=3) at k.cpp:1818 while (n--) (gdb) Continuing.hardware WA Tchpoint 2:nold value = 3New value = 20x08048764 in factorial (n=2) at k.cpp:1818 while (n--) (gdb) Continuing.hardware wat  ChPoint 3:resultold value = 3New value = 6factorial (n=2) at k.cpp:1818 while (n--) (GDB) Continuing.hardware Watchpoint 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 when (n--) (gdb) Continuing.watchpoint 2 deleted because the program ha S 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

Some quick commands

L–list
P–print Print {variable}
C–continue
S–step
B-break break Line_number/break [File_name]:line_number/break [File_name]:func_name
R-run
Set =
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 isn't treats function as a single instruction, instead goes into the function and execute s 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 Foundat Ion, 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_sleep 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.

We can also use the built-in Gdbinit to debug

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 Dtrace_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/src/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

Continue execution

To Execute_internal, you can look at a state of the current function

View current Hashtable

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 = 0x8ae755 4}, Op_array = {type = 1 ' \001 ', function_name = 0x8af1841 "In_array", scope = 0x0, Fn_flags =, prototype = 0x0, num_a RGS = 3, Required_num_args = 2, Arg_info = 0x8ae7554, RefCount = 0x842691d, opcodes = 0x8bcf120, last = 0, VARs = 0x0, las T_var = 0, T = 1, nested_calls = 3086618796, Used_stack = 0, Brk_cont_array = 0x0, Last_brk_cont = 1, Try_catch_array = 0x B7FA10DD, Last_try_catch = $, Has_finally_block = \240 ', Static_variables = 0x0, This_var = 11482064, filename = 0xa F1ff4 "|\035\257", Line_start = 11482016, Line_end = 146381272, doc_comment = 0xbffff238 "x\362\377\277\244\ay\b\021", doc _comment_len = 10305959, early_binding = 11085989, literals = 0x8b7a0a0, last_literal = 140062666, Run_time_cache = 0xb7fa 10D4, Last_cache_slot =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 = 0x84269  1d
        
         
          , 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.

The above is a small series to share about how to use GDB debugging PHP program all the content, I hope you like.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.