Reproduce the call stack for PHP core

Source: Internet
Author: User

Previously, I have described how to get information through the core file of PHP: How to debug the core of PHP to get the basic information , for the invocation of parameters this block, the introduction of the acquisition method is more complex.

So today I made an improvement for PHP 5.4 's. Gdbinit, and if you encounter the core of PHP 5.4, then you can simply get the information of the function call stack that contains the parameters when the core of PHP 5.4 occurs.

Suppose for the following script:

  1. <?php
  2. class Test {
  3. }
  4. function a($i) {
  5. b(new Test, 2.3432, "reader");
  6. }
  7. function b($i) {
  8. C(array(1,2,3));
  9. }
  10. function C($i) {
  11. D(TRUE);
  12. }
  13. function D($i) {
  14. $fp = fopen("/tmp/1.php", "R");
  15. e($fp);
  16. }
  17. function e($i) {
  18. Sleep(+);
  19. }
  20. a ();

After running in the background, PHP5.4 will sleep in the sleep of the E function, and if we use gdb attach up,

    1. GDB --pid= XXX //Use PS to get the PID of running script in background

Then, source PHP sources the following. Gdbinit:

    1. (gdb) Source Php54-src/.gdbini

Then, let's try calling the next zbacktrace to see what the results are:

    1. (GDB) Zbacktrace
    2. [0x2a95dac5e0] Sleep (/tmp/1.php:21)
    3. [0X2A95DAC4C0] e (Resource (#5))/tmp/1.php:17
    4. [0x2a95dac3f0] D (True)/tmp/1.php:13
    5. [0x2a95dac300] C (Array (3) [0x2a95de7db0])/tmp/1.php:10
    6. [0x2a95dac1c0] B (object[0x2a95de7840], 2.343200, "reader")/tmp/1.php:7
    7. [0x2a95dac0e8] A ()/tmp/1.php:2

Well, for the array and object, because we're not going to be messing around, so it's not unfolding, but if we want to see what this array is specifically about, you can do that, notice the above: Array (3) [0x2a95de7db0]:

    1. (GDB) print ((Zval *) 0x2a95de7db0)
    2. $4 = (struct _zval_struct *) 0x2a95de7db0
    3. (GDB) Printzv $4
    4. [0x2a95de7db0] (refcount=2) Array (3): {
    5. 0 = [0x2a95de79d0] (refcount=1) long:1
    6. 1 = [0x2a95de7b80] (refcount=1) long:2
    7. 2 = [0x2a95de7c98] (refcount=1) long:3

Similarly, for object, note the above: object[0x2a95de7840]

    1. (GDB) print ((Zval *) 0x2a95de7840)
    2. $ = (struct _zval_struct *) 0x2a95de7840
    3. (GDB) Printzv
    4. [0x2a95de7840] (refcount=2) object
    5. (Test) #1 "No properties found

One thing to note is that for object, if you are in the modal core file instead of attach to a running process, then the above attempt will get an error:

    1. (GDB) Printzv
    2. [0x2a95de7840] (refcount=2) objectyou can ' t do this without a process to debug

However, even so, we have a way, but it is more troublesome. Under NTS:

    1. (GDB) p ((Zval *) 0x2a95de7840)->value.obj.handle
    2. $6 = 1
    3. Note that the following value is used for this $6:1
    4. (GDB) p (zend_object*) Executor_globals->objects_store.object_buckets[1].bucket.obj.object
    5. $7 = (struct _zend_object *) 0x2a95de3ec0
    6. (GDB) P $9->ce->name
    7. $8 = 0x2a95e200b0 "Test

Oh, how, with this information, analysis of the reasons for the core, is it more simple? enjoy~

Finally, keep in mind that PHP 5.4 is still in development, and any new features may be adjusted or changed before final release. If you have any suggestions, also welcome feedback, help us make PHP better

Reproduce the call stack for PHP core

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.