PHP7內核:源碼分析的環境與工具

本文主要介紹分析源碼的方式,其中包含環境的搭建、分析工具的安裝以及源碼調試的基本操做。php

1、工具清單

  • PHP7.0.12
  • GDB
  • CLion

2、源碼下載及安裝

$ 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

3、GDB的安裝與調試

3.1 安裝

本文介紹兩款調試工具,分別是GDB和CLion,前者爲命令行調試工具,後者爲圖形界面調試工具,後者依賴前者。二者的安裝都很簡單,Clion到官網下載便可,GDB也只需一行命令就可搞定。html

$ sudo apt install gdb

3.2 調試

建立php文件linux

<?php
    echo "Hello world!";
?>

打開gdbgit

$ 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文件redis

# 斷點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的具體指令,能夠參考官方文檔,這裏再也不一一贅述。docker

4、CLion的配置與調試

4.1 配置

CLion的安裝就再也不贅述了,下面我來說述一下CLion是如何配置的。打開CLion,選中菜單欄中的File -> Import Project...,選擇下載的PHP源碼包,如圖所示,點擊肯定。
ubuntu

導入以後,打開項目根目錄的CMakeLists.txt文件,將該文件替換爲如下內容,注意版本、源碼目錄要根據實際狀況作調整vim

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填寫要執行腳本的存放目錄,配置見下圖。api

4.2 調試

點擊完成,咱們來驗證一下配置是否成功。先在工做目錄建立index.php文件,內容隨意輸入,只要是PHP代碼便可。例如:bash

<?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、備註

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
相關文章
相關標籤/搜索