Deliverer, the PHP code savior you have been waiting for for so long, is here!

醉折花枝作酒筹
Release: 2023-03-10 21:10:02
forward
1827 people have browsed it

When a piece of code cannot see the complete call chain, do you need a tool to assist in clearly querying the entire complete call chain? This time the editor will take you to learn about Deliverer in php, so you no longer have to worry about code troubleshooting.

Deliverer, the PHP code savior you have been waiting for for so long, is here!

A few days ago, a group friend took over a piece of ancestral code. After troubleshooting for a long time, it was not solved. He prepared to run away with the bucket and finally helped him solve it. The complete process can be found at https:/ /mengkang.net/1470.html However, the positioning of the final code is based on my personal experience. The actual link called is different from what I expected, and it is all based on guesswork. I didn't see the complete call chain, so I thought I needed a tool to assist in explicitly querying the entire call chain.

So I built this tool, mainly for unfamiliar projects and online scenarios that are difficult to reproduce in daily environments.

deliver The ancestral code escape rescuer https://github.com/zhoumengka...

If your project is not that bad and the daily environment is ok, then it is best to be familiar with a project The method is still xdebug. This tool is mainly used to troubleshoot online problems.
Similar tools include 360's phptrace implementation principle, which is slightly different

Function points

  • can be based on the function name, class name, Use the method name and route to filter the output

  • You can exit after querying the specified filtered content n times

  • You can play back the entire request based on the request id The complete call chain

  • The filtered content will be highlighted

  • If the call stack is deep, you can specify -l to hide the display of deep calls.

Principle

In fact, it is relatively simple. It is divided into two steps. The first step is to collect logs and the second step is to analyze the logs.

Step 1

In the PHP_MINIT stage, use zend_set_user_opcode_handler to set up the processing and analysis of three types of opcodes: ZEND_DO_UCALL, ZEND_DO_FCALL_BY_NAME, and ZEND_DO_FCALL.

Note that it will cover calls to some built-in functions and methods, and we can filter by type.

Then create a new log file in the PHP_RINIT stage and write the requested information

pid-ts sapi http_method http_url
Copy after login

During the request process, print the call stack information in the custom handler

Finally close the log in PHP_RSHUTDOWN Writing files

The second step

Use bin/deliverer to analyze and organize the collected logs. This is a php script, so I won’t go into details. .

Install and use

Compile

$ phpize $ ./configure --with-php-config=/usr/local/php/bin/php-config $ make && sudo make install
Copy after login

Configure php.ini

Append

[deliverer] extension=deliverer.so
Copy after login

Restart php-fpm

sudo service php-fpm restart
Copy after login

Use analysis tools

You can move ./bin/deliverer to where you think it is Suitable directory, if you run it in the current directory

$ chmod +x deliverer
Copy after login

with a piece of my own ancestral code (my blog) from long ago

$ ./bin/deliverer -t
Copy after login

, this will always monitor the execution of all php processes

Deliverer, the PHP code savior you have been waiting for for so long, is here!

$ ./bin/deliverer -tAction::initUser -n3 -l5
Copy after login

Deliverer, the PHP code savior you have been waiting for for so long, is here!

Deliverer, the PHP code savior you have been waiting for for so long, is here!

$ ./bin/deliverer -v7979-1624369150991941
Copy after login
##Parameter Value Explanation -t Action::initUser Filter requests containing this call -n 3 Count three times and exit -l 5 The function (method) calls the depth display, and up to 5 layers are displayed. The excess parts are marked at the end ## View the complete details through -v requestId Call stack

$ ./bin/deliverer -tSqlExecute::getAll -n1 -l3
Copy after login
Deliverer, the PHP code savior you have been waiting for for so long, is here!When querying a method, the function call stack is too deep and is not within the hierarchical query range, and its outer calls are displayed in red

Deliverer, the PHP code savior you have been waiting for for so long, is here!Recommended learning:

php video tutorial

The above is the detailed content of Deliverer, the PHP code savior you have been waiting for for so long, is here!. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:segmentfault.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!