设为首页 加入收藏

TOP

使用vscode调试PHP底层C源码(一)
2023-07-23 13:26:14 】 浏览:102
Tags:使用 vscode 调试 PHP 底层 源码

使用vscode调试PHP底层C源码

一直想着有机会调试一下php底层代码来着,这周正好心血来潮,就跟着教程配置了一下。本篇文章是基于macOS,可能在编译php源码之前的步骤对使用windows的师傅没啥可参考的。

windows下比较麻烦,主要是在编译php源码这一步,最方便的办法是用docker来远程调试。具体可以参考这篇文章vscode远程调试php底层代码。使用p牛的dockerfile来自己建一个调试用docker。

说回mac下调试PHP源码需要的准备

下载并编译PHP

使用git来下载源码,这样切换PHP版本会较为方便。(不过我现在应该不会这么做,因为下载下来的源码并不能直接编译成功,需要自己修改。改完的源码不舍得切换了)

 git clone https://github.com/php/php-src
 cd php-src/
 git checkout PHP-7.3.67

当然,不排除个人环境的原因,通过checkout切换分支,重新编译一下还是很方便的,如果编译不出错的话。

如果是mac,编译PHP前需要安装一下最新版的bison,mac自带的版本太老。

brew install bison
# bison的具体路径可以通过brew list bison来查看
export PATH=/opt/homebrew/Cellar/bison/3.8.2/bin:$PATH

编译需要调试的PHP。像我这里就开启了debug模式,开启了phar扩展,如果需要开启别的扩展,需要再./configure命令后面自行指定。

 ./buildconf
 ./configure --disable-all --enable-debug --enable-phar --prefix=/source/php7.3.6/
 make
 make install

编译完后,编译结果都在/source/php7.3.6/文件夹下,/source/php7.3.6/bin/php为可执行文件。

编译好的PHP,执行./php -v 会显示NTS DEBUG

截屏2022-09-07 00.36.50

编译过程中的错误

在进行make编译的时候,碰到了两次报错。

第一个报错:

 /Users/niushaogang/jkbPhpPackage/php-5.4.45/main/reentrancy.c:139:23: error: too few arguments to function call, expected 3, have 2
        readdir_r(dirp, entry);
        ~~~~~~~~~            ^
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/dirent.h:110:5: note: 'readdir_r' declared here
int readdir_r(DIR *, struct dirent *, struct dirent **) __DARWIN_INODE64(readdir_r);
    ^
1 error generated.
make: *** [main/reentrancy.lo] Error 1

根据网上的教程,了解到这是php源码调用readdir_r函数的时候少传了一个参数。

查看php-src/main/reentrancy.c

函数定义:

? int readdir_r(DIR *, struct dirent *, struct dirent **)

php调用:

? readdir_r(dirp, entry)

readdir_r(dirp, entry) 修改为 readdir_r(dirp, entry,&entry)即可编译通过

image-20220912142049128

第二个报错:

/Users/dre0m1/CTF/学习笔记/PHP源码/php-src/Zend/zend_language_parser.y:1317:5: error: implicit declaration of function 'yystpcpy' is invalid in C99 [-Werror,-Wimplicit-function-declaration]
 
                                yystpcpy(yyres, "end of file");
 
                                ^
 
/Users/dre0m1/CTF/学习笔记/PHP源码/php-src/Zend/zend_language_parser.y:1317:5: note: did you mean 'stpcpy'?
 
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/string.h:130:7: note: 'stpcpy' declared here
 
char    *stpcpy(char *__dst, const char *__src);
 
         ^
 
/Users/dre0m1/CTF/学习笔记/PHP源码/php-src/Zend/zend_language_parser.y:1324:29: error: implicit declaration of function 'yystrlen' is invalid in C99 [-Werror,-Wimplicit-function-declaration]
 
                yystr_len = (unsigned int)yystrlen(yystr);
 
                                          ^
 
/Users/dre0m1/CTF/学习笔记/PHP源码/php-src/Zend/zend_language_parser.y:1324:29: note: did you mean 'strlen'?
 
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/string.h:82:9: note: 'strlen' declared here
 
size_t   strlen(const char *__s);
 
         ^
 
/Users/dre0m1/CTF/学习笔记/PHP源码/php-src/Zend/zend_language_parser.y:1345:4: error: implicit declaration of function 'yystpcpy' is invalid in C99 [-Werror,-Wimplicit-function-declaration]
 
                        yystpcpy(yyres, buffer);
 
                        ^
 
/Users/dre0m1/CTF/学习笔记/PHP源码/php-src/Zend/zend_language_parser.y:1352:10: error: implicit declaration of function 'yystrlen' is invalid in C99 [-Werror,-Wimplicit-function-declaration]
 
                return yystrlen(yystr) - (*yystr == '"' ? 2 : 0);
 
                       ^
 
/Users/dre0m1/CTF/学习笔记/PHP源码/php-src/Zend/zend_language_parser.y:1365:2: error: implicit declaration of function 'yystpcpy' is i
首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇浅谈PHP设计模式的组合模式 下一篇浅谈PHP设计模式的建造者模式

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目