Home  >  Article  >  Backend Development  >  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!

醉折花枝作酒筹
醉折花枝作酒筹forward
2021-06-24 16:05:491897browse

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

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

Configure php.ini

Append

[deliverer]
extension=deliverer.so

Restart php-fpm

sudo service php-fpm restart

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

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

$ ./bin/deliverer -t

, 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

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!

##ParameterValue Explanation-tAction::initUserFilter requests containing this call-n3Count three times and exit-l 5The function (method) calls the depth display, and up to 5 layers are displayed. The excess parts are marked at the end##
$ ./bin/deliverer -v7979-1624369150991941
View the complete details through -v requestId Call stack

$ ./bin/deliverer -tSqlExecute::getAll -n1 -l3
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!

Statement:
This article is reproduced at:segmentfault.com. If there is any infringement, please contact admin@php.cn delete