PHP7内核:源码分析的环境与工具

本文主要介绍分析源码的方式,其中包含环境的搭建、分析工具的安装以及源码调试的基本操作。

一、工具清单

  • 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
posted @ 2018-08-28 13:06  MARIOOW  阅读(5062)  评论(2编辑  收藏  举报