6

I'm following these steps to locate the current PHP function call as below:

  1. Run dummy script:

    $ gdb -ex run --args php -r "sleep(10);"
    
  2. Pressed Ctrl+C to get back to gdb to run:

    (gdb) bt full
    #1  0x00007ffff6007dd4 in __sleep (seconds=0) at ../sysdeps/unix/sysv/linux/sleep.c:137
            ts = {tv_sec = 8, tv_nsec = 306649388}
            set = {__val = {65536, 0 <repeats 15 times>}}
            oset = {__val = {0, 4469319, 4294967295, 8081486, 140737319884960, 140737354070488, 15761488, 15454080, 15337134, 
                140737354001040, 0, 7307048, 16048064, 206158430232, 140737488342304, 140737488342096}}
            result = <optimized out>
    #2  0x00000000006156ef in zif_sleep ()
    No symbol table info available.
    #3  0x00000000006ddd7b in dtrace_execute_internal ()
    No symbol table info available.
    #4  0x000000000079dde5 in ?? ()
    No symbol table info available.
    #5  0x0000000000717b18 in execute_ex ()
    No symbol table info available.
    #6  0x00000000006ddc79 in dtrace_execute_ex ()
    No symbol table info available.
    #7  0x00000000006e1b0a in zend_eval_stringl ()
    No symbol table info available.
    #8  0x00000000006e1bf9 in zend_eval_stringl_ex ()
    ...
    (gdb) frame 2
    #2  0x00000000006156ef in zif_sleep ()
    (gdb) print (char *)(executor_globals.function_state_ptr->function)->common.function_name
    Attempt to extract a component of a value that is not a structure.      
    (gdb) print (char *)(executor_globals.function_state_ptr->function)
    Attempt to extract a component of a value that is not a structure.
    (gdb) print (char *)(executor_globals)
    $2 = 0xffffffffffffcf48 <error: Cannot access memory at address 0xffffffffffffcf48>
    

    So it seems executor_globals symbol is not available. Is it because the binary has been optimized, I'm in the wrong frame or something else? Or I should use lldb instead?

4
  • If the symbol was unavailable, you'd get a different error message, about an unknown symbol. (Try print (char *) (blarflgrumpf)). This seems like your binary has been compiled with optimization on (which could put some variables into registers, without gdb knowing, and relying on the (wrong) stack value). Try to compile/link php with -O0 and -g. Also, unless you cut some parts of the backtrace, your stack is probably broken, as execute_ex seems to have been called out of thin air. This broken stack may be the reason for all sorts of unexpected behaviour. Commented Aug 5, 2015 at 13:35
  • @GuntramBlohm Thanks, yes, there are more frames in bt (up to #10 with ??), I've re-added. Yes, unknown symbol is printing different message. So that means I can't access/extract that data about the current function without re-compiling the PHP?
    – kenorb
    Commented Aug 5, 2015 at 13:41
  • Well, some things seem a bit fishy to me: the instructions tell you to move the frame to the last execute call, while you only have execute_ex; if executor_globals is indeed a global variable, the stack frame shouldn't matter, but if it's local, you shouldn't find it in the zif_sleep frame. I wonder how up-to-date the site is you linked to. But as the value of executor_globals is wrong, and your PHP site says "Important! To get a backtrace with correct information you must have PHP configured with --enable-debug! ", I think you need to recompile. Commented Aug 6, 2015 at 12:21
  • 1
    Highly relevant stackoverflow.com/a/31240030/308851
    – chx
    Commented Dec 18, 2018 at 20:12

1 Answer 1

9

According to this link, it should be possible to find the function in use with the following steps:

  1. Attach gdb to the currently-running PHP process: gdb -p <processid>
  2. Load in the PHP .gdbinit file for your version of PHP (available from here)
  3. Use the zbacktrace command to display the currently-running PHP script

For example:

gdb -p 4584
(gdb) source PHP_5_5/.gdbinit
(gdb) zbacktrace
[0xec906090] addOne() /tmp/yourscript.php:9
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.