標籤:change sudo php7.0 redis tar 項目 main函數 index 代碼
本文主要介紹分析源碼的方式,其中包含環境的搭建、分析工具的安裝以及源碼調試的基本操作。
一、工具清單
二、源碼下載及安裝
$ 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-gitCopyright (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.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
關於GDB的具體指令,可以參考官方文檔,這裡不再一一贅述。
四、CLion的配置與調試4.1 配置
CLion的安裝就不再贅述了,下面我來講述一下CLion是如何配置的。開啟CLion,選中功能表列中的File -> Import Project...
,選擇下載的PHP源碼包,,點擊確定。
匯入之後,開啟項目根目錄的CMakeLists.txt
檔案,在最後一行加入以下代碼:
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/#[email protected]#: Permission deniedmake[4]: *** [install-build] Error 1make[4]: *** Waiting for unfinished jobs....cp: /usr/local/php7/lib/php/extensions/debug-non-zts-20151012/#[email protected]#: 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
5.2 Docker如何GDB調試
docker run --security-opt seccomp=unconfined -it ubuntu bash
跟廠長學PHP核心(二):源碼分析的環境與工具