This article mainly introduces the method of analyzing the source code, which includes the construction of the environment, the installation of the analysis tools and the basic operation of the source debugging.
I. List of tools
Second, the source code download and installation
$ wget http://php.net/distributions/php-7.0.12.tar.gz$ tar zxvf php-7.0.12.tar.gz$ cd php-7.0.12/$ ./configure --prefix=/usr/local/php7 --enable-debug --enable-fpm$ make && sudo make install
Iii. installation and commissioning of GDB 3.1 installation
This article introduces two debugging tools, namely GDB and Clion, which are command-line debugging tools, and the latter is a graphical interface debugging tool, which relies on the former. Both of the installation is very simple, clion to the official website to download, GDB also only need a line of command can be done.
$ sudo apt install gdb
3.2 Commissioning
Creating PHP Files
<?php echo "Hello world!";?>
Open GDB
$ gdb php #将显示如下内容GNU gdb (Debian 7.12-6) 7.12.0.20161007-gitCopyright (C) 2016 Free Software Foundation, Inc.License GPLv3+: GNU GPL version 3 or later
Debug a PHP file created
# 断点main函数(gdb) b main(gdb) run index.phpStarting program: /usr/local/bin/php index.php[Thread debugging using libthread_db enabled]Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".Breakpoint 1, main (argc=2, argv=0x7fffffffcd48) at /home/enoch/Source/php-7.0.12/sapi/cli/php_cli.c:11721172 int exit_status = SUCCESS;# next执行下一行(gdb) next1173 int module_started = 0, sapi_started = 0;(gdb) next1174 char *php_optarg = NULL;(gdb) next1175 int php_optind = 1, use_extended_info = 0;(gdb) next1176 char *ini_path_override = NULL;# print可以打印变量、表达式(gdb) print php_optarg$1 = 0x0
Detailed instructions on GDB can be found in the official documentation, which is no longer one by one.
Iv. configuration and commissioning of Clion 4.1 configuration Clion installation will not repeat, let me explain how clion is configured. Open Clion, select the downloaded PHP source package in the menu bar, File -> Import Project...
and click OK.
After importing, open the file in the project root directory CMakeLists.txt
and add the following code to the last line:
add_custom_target(makefile COMMAND make && make install WORKING_DIRECTORY ${PROJECT_SOURCE_DIR})
When finished, open the menu bar Run -> Edit Configurations...
, target select makefile, executable choose PHP executable binaries, program arguments fill in the script name to execute, working Directory fill in the directory where you want to execute the script, see Configuration.
4.2 Commissioning Click Finish to verify that the configuration is successful. Create the index.php file in the working directory, the content is arbitrary input, as long as the PHP code. For example:
<?php echo 'Hello world';?>
Back to Clion, open the sapi/cli/php_cli.c
file and make breakpoints in the main function, such as:
After adding a breakpoint, click on Run -> Debug 'makefile'
the menu and wait for the IDE to compile, and if it appears, you are done.
The following error may occur during debug, mainly because we do not have permission to operate the PHP directory /usr/local/php7
.
Installing build environment: /usr/local/php7/lib/php/build/Installing shared extensions: /usr/local/php7/lib/php/extensions/debug-non-zts-20151012/cp: /usr/local/php7/lib/php/build/#INST@82468#: Permission deniedmake[4]: *** [install-build] Error 1make[4]: *** Waiting for unfinished jobs....cp: /usr/local/php7/lib/php/extensions/debug-non-zts-20151012/#INST@82475#: Permission denied
How to resolve:
$ sudo chmod -R 777 /usr/local/php7/
V. Notes 5.1 FAQs No acceptable C compiler found
$ sudo apt-get install build-essential
Configure:error:xml2-config not found
$ sudo apt-get install libxml2-dev
5.2 Docker how gdb debugs docker run --security-opt seccomp=unconfined -it ubuntu bash