分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > IT知识

跟厂长学PHP内核(二):源码分析的环境与工具

发布时间:2023-09-06 02:12责任编辑:彭小芳关键词:PHP

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

一、工具清单

  • 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-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/#INST@82468#: Permission deniedmake[4]: *** [install-build] Error 1make[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

5.2 Docker如何GDB调试

docker run --security-opt seccomp=unconfined -it ubuntu bash

跟厂长学PHP内核(二):源码分析的环境与工具

原文地址:https://www.cnblogs.com/enochzzg/p/9547595.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved