php 前端工具源码,PHP7内核:源码分析的环境与工具

本文详细介绍了如何搭建PHP源码分析环境,包括下载安装PHP源码、配置GDB调试工具以及使用CLion进行图形化调试。在GDB中设置断点并执行调试步骤,同时展示了CLion的项目导入、配置及调试过程,帮助读者掌握PHP源码级别的调试技巧。
摘要由CSDN通过智能技术生成

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

一、工具清单

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

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

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:

.

Find the GDB manual and other documentation resources online at:

.

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源码包,如图所示,点击确定。

96bb53e83bd139e35bd19fc71d787c1d.png

导入之后,打开项目根目录的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填写要执行脚本的存放目录,配置见下图。

cd9ef3d5636c8ffec34d7af8b1dbbde2.png

4.2 调试

点击完成,我们来验证一下配置是否成功。先在工作目录创建index.php文件,内容随意输入,只要是PHP代码即可。例如:

echo 'Hello world';

?>

回到CLion,打开sapi/cli/php_cli.c文件,在main函数进行断点,如下图:

2a88c0fc5e4ae49c2f2a35be094e8608.png

加入断点后,点击菜单Run -> Debug 'makefile',等待IDE编译完成后,若出现下图即大功告成。

a9f97b5607be389ba0fa987232850569.png

在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

作者:平也

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值