本文主要介紹分析源碼的方式,其中包含環境的搭建、分析工具的安裝以及源碼調試的基本操作。
一、工具清單
- 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的安裝與調試
3.1 安裝
本文介紹兩款調試工具,分別是GDB和CLion,前者為命令行調試工具,后者為圖形界面調試工具,后者依賴前者。兩者的安裝都很簡單,Clion到官網下載即可,GDB也只需一行命令就可搞定。
$ sudo apt install gdb
3.2 調試
創建php文件
<?php
echo "Hello world!";
?>
打開gdb
$ gdb php #將顯示如下內容
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)
調試創建的php文件
# 斷點main函數
(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執行下一行
(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可以打印變量、表達式
(gdb) print php_optarg
$1 = 0x0
關於GDB的具體指令,可以參考官方文檔,這里不再一一贅述。
四、CLion的配置與調試
4.1 配置
CLion的安裝就不再贅述了,下面我來講述一下CLion是如何配置的。打開CLion,選中菜單欄中的File -> Import Project...
,選擇下載的PHP源碼包,如圖所示,點擊確定。
導入之后,打開項目根目錄的CMakeLists.txt
文件,將該文件替換為以下內容,注意版本、源碼目錄要根據實際情況做調整
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的可執行二進制程序、Program arguments填寫要執行的腳本名稱、Working Directory填寫要執行腳本的存放目錄,配置見下圖。
4.2 調試
點擊完成,我們來驗證一下配置是否成功。先在工作目錄創建index.php文件,內容隨意輸入,只要是PHP代碼即可。例如:
<?php
echo 'Hello world';
?>
回到CLion,打開sapi/cli/php_cli.c
文件,在main函數進行斷點,如下圖:
加入斷點后,點擊菜單Run -> Debug 'makefile'
,等待IDE編譯完成后,若出現下圖即大功告成。
在debug時可能會出現以下錯誤,主要是因為沒有操作php目錄權限的緣故,我們賦予/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 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
# 如果是因為iconv中斷的話
# 搜索-liconv,然后替換為/usr/local/homebrew/Cellar/libiconv/1.15/lib/libiconv.dylib(不同機器與版本會不同)
# 如果因為其他擴展中斷也可以通過這種方式解決
5.2 Docker如何GDB調試
docker run --security-opt seccomp=unconfined -it ubuntu bash