PHP7內(nèi)核:源碼分析的環(huán)境與工具
本文主要介紹分析源碼的方式,其中包含環(huán)境的搭建、分析工具的安裝以及源碼調(diào)試的基本操作。
一、工具清單
- PHP7.0.12
- GDB
- CLion
二、源碼下載及安裝
$ 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
三、GDB的安裝與調(diào)試
3.1 安裝
本文介紹兩款調(diào)試工具,分別是GDB和CLion,前者為命令行調(diào)試工具,后者為圖形界面調(diào)試工具,后者依賴前者。兩者的安裝都很簡(jiǎn)單,Clion到官網(wǎng)下載即可,GDB也只需一行命令就可搞定。
$ sudo apt install gdb
3.2 調(diào)試
創(chuàng)建php文件
<?php
echo "Hello world!";
?>
打開gdb
$ gdb php #將顯示如下內(nèi)容
GNU gdb (Debian 7.12-6) 7.12.0.20161007-git
Copyright (C) 2016 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from php...done.
(gdb)
調(diào)試創(chuàng)建的php文件
# 斷點(diǎn)main函數(shù)
(gdb) b main
(gdb) run index.php
Starting 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:1172
1172 int exit_status = SUCCESS;
# next執(zhí)行下一行
(gdb) next
1173 int module_started = 0, sapi_started = 0;
(gdb) next
1174 char *php_optarg = NULL;
(gdb) next
1175 int php_optind = 1, use_extended_info = 0;
(gdb) next
1176 char *ini_path_override = NULL;
# print可以打印變量、表達(dá)式
(gdb) print php_optarg
$1 = 0x0
關(guān)于GDB的具體指令,可以參考官方文檔,這里不再一一贅述。
四、CLion的配置與調(diào)試
4.1 配置
CLion的安裝就不再贅述了,下面我來講述一下CLion是如何配置的。打開CLion,選中菜單欄中的File -> Import Project...,選擇下載的PHP源碼包,如圖所示,點(diǎn)擊確定。

導(dǎo)入之后,打開項(xiàng)目根目錄的CMakeLists.txt文件,將該文件替換為以下內(nèi)容,注意版本、源碼目錄要根據(jù)實(shí)際情況做調(diào)整
cmake_minimum_required(VERSION 3.13)
project(makefile)
set(CMAKE_CXX_STANDARD 11)
set(PHP_SOURCE /Users/enoch/Documents/source/php-7.3.4)
include_directories(${PHP_SOURCE}/main)
include_directories(${PHP_SOURCE}/Zend)
include_directories(${PHP_SOURCE}/sapi)
include_directories(${PHP_SOURCE}/pear)
include_directories(${PHP_SOURCE}/TSRM)
include_directories(${PHP_SOURCE})
add_custom_target(makefile COMMAND make && make install WORKING_DIRECTORY ${PROJECT_SOURCE_DIR})
完成后,打開菜單欄Run -> Edit Configurations...,Target選擇makefile、Executable選擇PHP的可執(zhí)行二進(jìn)制程序、Program arguments填寫要執(zhí)行的腳本名稱、Working Directory填寫要執(zhí)行腳本的存放目錄,配置見下圖。

4.2 調(diào)試
點(diǎn)擊完成,我們來驗(yàn)證一下配置是否成功。先在工作目錄創(chuàng)建index.php文件,內(nèi)容隨意輸入,只要是PHP代碼即可。例如:
<?php
echo 'Hello world';
?>
回到CLion,打開sapi/cli/php_cli.c文件,在main函數(shù)進(jìn)行斷點(diǎn),如下圖:

加入斷點(diǎn)后,點(diǎn)擊菜單Run -> Debug 'makefile',等待IDE編譯完成后,若出現(xiàn)下圖即大功告成。

在debug時(shí)可能會(huì)出現(xiàn)以下錯(cuò)誤,主要是因?yàn)闆]有操作php目錄權(quán)限的緣故,我們賦予/usr/local/php7權(quán)限即可。
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 denied
make[4]: *** [install-build] Error 1
make[4]: *** Waiting for unfinished jobs....
cp: /usr/local/php7/lib/php/extensions/debug-non-zts-20151012/#INST@82475#: Permission denied
解決方式:
$ sudo chmod -R 777 /usr/local/php7/
五、備注
5.1 常見問題
no acceptable C compiler found
$ sudo apt-get install build-essential
configure: error: xml2-config not found
$ sudo apt-get install libxml2-dev
ld: symbol(s) not found for architecture x86_64
vim Makefile
# 如果是因?yàn)閕conv中斷的話
# 搜索-liconv,然后替換為/usr/local/homebrew/Cellar/libiconv/1.15/lib/libiconv.dylib(不同機(jī)器與版本會(huì)不同)
# 如果因?yàn)槠渌麛U(kuò)展中斷也可以通過這種方式解決
5.2 Docker如何GDB調(diào)試
docker run --security-opt seccomp=unconfined -it ubuntu bash

浙公網(wǎng)安備 33010602011771號(hào)