热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

【docker+gdb】调试PHP源码,看strval函数C实现

phpstrval函数的作用很简单,就是你给他一个值,他给你返回字符串类型。算是一个比较简单的函数了,我们来通过gdb来一探究竟。
php strval 函数的作用很简单,就是你给他一个值,他给你返回字符串类型。

算是一个比较简单的函数了,我们来通过 gdb 来一探究竟。

通过本文,你可以窥探下

● gdb 的简单使用

● gdb gui 模式初探

● 看看平时写的 PHP 代码在 C 语言里的样子

● 对使用 gdb 调试 php 代码有个初步了解

● 对了,文末有一些截图,不要错过

采购食材

● 电脑一台

● docker 和 docker-compose

gdb 也好, PHP 也好,都打包成 docker 镜像啦,开袋即食,甚好。

备菜环节

1、使用 docker 拉取环境

# 拉取准备好的环境
git clone https://github.com/rovast/docker-examples.git
# 进入项目
cd docker-examples/gdb-php-src/
# 启动,会经历一个漫长又不太漫长的等待,看你网速
docker-compose up -d

关于容器内的环境,大家可以看看 dockerfile

其实很简单,就是基于 gcc 官方镜像构建,然后增加了 vim gdb,并且下载了 php7.0.0 的源码,按照 debug 参数进行编译

显示如下

Creating network "gdb-php-src_default" with the default driver
Creating gdb-php-src ... done

2、进入容器

docker exec -it gdb-php-src bash
### 显示下面的东西,表示你已经进入到容器内了 ####
root@71a98d1bc1a6:/home#

我们看看容器内的环境(php 以及 gdb)

### 我们在容器内看看环境
root@71a98d1bc1a6:/home# ls
php-7.0.0  start.md
root@71a98d1bc1a6:/home# gdb -v
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".
root@71a98d1bc1a6:/home# php -v
PHP 7.0.0 (cli) (built: Apr 17 2019 13:33:30) ( NTS DEBUG )
Copyright (c) 1997-2015 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2015 Zend Technologies
root@71a98d1bc1a6:/home#

开火(请在容器内操作)

1、新建测试文件

root@71a98d1bc1a6:/home# vi test.php

输入以下内容

这个文件干的事情就比较简单了,就是把 -1234 [整形] 转换为 -1234 [字符串]

2、开始调试,进入 gdb

接下来车速较快,各位按步骤跟上

输入 gdb php,开始调试

root@71a98d1bc1a6:/home# 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)

3、打一些断点(敲命令时可以用 tab 补全)

(gdb) b zend_long_to_str
Breakpoint 1 at 0x810423: file /home/php-7.0.0/Zend/zend_operators.c, line 2743.
(gdb) b zend_print_ulong_to_buf
Breakpoint 2 at 0x5f387b: zend_print_ulong_to_buf. (13 locations)
(gdb)

这里在关键函数 zend_long_to_str 和 zend_print_ulong_to_buf 打了断点。

b 在 gdb 中是 breakpoint 缩写,后面可以加函数名,或者当前文件的行号都是可以的

4、执行,查看断点值

(gdb) r test.php # 执行我们刚才的那个 PHP 文件
Starting program: /usr/local/bin/php test.php
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
warning: File "/usr/local/lib64/libstdc++.so.6.0.25-gdb.py" auto-loading has been declined by your `auto-load safe-path' set to "$debugdir:$datadir/auto-load".
To enable execution of this file add
   add-auto-load-safe-path /usr/local/lib64/libstdc++.so.6.0.25-gdb.py
line to your configuration file "/root/.gdbinit".
To completely disable this security protection add
   set auto-load safe-path /
line to your configuration file "/root/.gdbinit".
For more information about this security protection see the
"Auto-loading safe path" section in the GDB manual.  E.g., run from the shell:
   info "(gdb)Auto-loading safe path"
Breakpoint 1, zend_long_to_str (num=-1234) at /home/php-7.0.0/Zend/zend_operators.c:2743
2743      char *res = zend_print_long_to_buf(buf + sizeof(buf) - 1, num);
(gdb)

看的好像不明了嘛, ctrl + x 后再按 a 进入 gui 模式看看

 ┌──/home/php-7.0.0/Zend/zend_operators.c────────────────────────────────────────────────────────────────────────────────────────────────┐
   │2731    ZEND_API void ZEND_FASTCALL zend_locale_sprintf_double(zval *op ZEND_FILE_LINE_DC) /* {{{ */                                   │
   │2732    {                                                                                                                              │
   │2733            zend_string *str;                                                                                                      │
   │2734                                                                                                                                   │
   │2735            str = zend_strpprintf(0, "%.*G", (int) EG(precision), (double)Z_DVAL_P(op));                                           │
   │2736            ZVAL_NEW_STR(op, str);                                                                                                 │
   │2737    }                                                                                                                              │
   │2738    /* }}} */                                                                                                                      │
   │2739                                                                                                                                   │
   │2740    ZEND_API zend_string* ZEND_FASTCALL zend_long_to_str(zend_long num) /* {{{ */                                                  │
   │2741    {                                                                                                                              │
   │2742            char buf[MAX_LENGTH_OF_LONG + 1];                                                                                      │
B+>│2743            char *res = zend_print_long_to_buf(buf + sizeof(buf) - 1, num);                                                        │
   │2744            return zend_string_init(res, buf + sizeof(buf) - 1 - res, 0);                                                          │
   │2745    }                                                                                                                              │
   │2746    /* }}} */                                                                                                                      │
   │2747                                                                                                                                   │
   │2748    ZEND_API zend_uchar ZEND_FASTCALL is_numeric_str_function(const zend_string *str, zend_long *lval, double *dval) /* {{{ */ {   │
   │2749        return is_numeric_string_ex(ZSTR_VAL(str), ZSTR_LEN(str), lval, dval, -1, NULL);                                           │
   │2750    }                                                                                                                              │
   │2751    /* }}} */                                                                                                                      │
   │2752                                                                                                                                   │
   │2753    ZEND_API zend_uchar ZEND_FASTCALL _is_numeric_string_ex(const char *str, size_t length, zend_long *lval, double *dval, int allo│
   │2754    {                                                                                                                              │
   │2755            const char *ptr;                                                                                                       │
   │2756            int digits = 0, dp_or_e = 0;                                                                                           │
   │2757            double local_dval = 0.0;                                                                                               │
   └───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
multi-thre Thread 0x7ffff7fe37 In: zend_long_to_str                                                                      L2743 PC: 0x810423
(gdb)

有点意思了,函数在 2743 行断住了,我们来看看 num 的值

(gdb) p num
$1 = -1234
(gdb)

对嘛,这个就是我们要处理的值,我们全速运行到 zend_print_long_to_buf 里看看

(gdb) c
Continuing.

显示如下

(gdb) c
  ┌──/home/php-7.0.0/Zend/zend_operators.h────────────────────────────────────────────────────────────────────────────────────────────────┐
   │781             else                                                                                                   \               │
   │782             ZEND_TRY_BINARY_OP2_OBJECT_OPERATION(opcode)                                                                           │
   │783                                                                                                                                    │
   │784     #define ZEND_TRY_UNARY_OBJECT_OPERATION(opcode)                                                            \                   │
   │785             if (UNEXPECTED(Z_TYPE_P(op1) == IS_OBJECT)                                                             \               │
   │786                     && UNEXPECTED(Z_OBJ_HANDLER_P(op1, do_operation))                                                  \           │
   │787                     && EXPECTED(SUCCESS == Z_OBJ_HANDLER_P(op1, do_operation)(opcode, result, op1, NULL))) { \                     │
   │788                     return SUCCESS;                                                                                    \           │
   │789             }                                                                                                                      │
   │790                                                                                                                                    │
   │791     /* buf points to the END of the buffer */                                                                                      │
   │792     static zend_always_inline char *zend_print_ulong_to_buf(char *buf, zend_ulong num) {                                           │
B+>│793             *buf = '\0';                                                                                                           │
   │794             do {                                                                                                                   │
   │795                     *--buf = (char) (num % 10) + '0';                                                                              │
   │796                     num /= 10;                                                                                                     │
   │797             } while (num > 0);                                                                                                     │
   │798             return buf;                                                                                                            │
   │799     }                                                                                                                              │
   │800                                                                                                                                    │
   │801     /* buf points to the END of the buffer */                                                                                      │
   │802     static zend_always_inline char *zend_print_long_to_buf(char *buf, zend_long num) {                                             │
   │803             if (num <0) {                                                                                                         │
   │804                 char *result = zend_print_ulong_to_buf(buf, ~((zend_ulong) num) + 1);                                              │
   │805                 *--result = &#39;-&#39;;                                                                                                   │
   │806                     return result;                                                                                                 │
   │807             } else {                                                                                                               │
   └───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
multi-thre Thread 0x7ffff7fe37 In: zend_print_ulong_to_buf                                                               L793  PC: 0x8041fd

接下来,我们来进行一些单步调试,看看内存里的值

Breakpoint 1, zend_long_to_str (num=-1234) at /home/php-7.0.0/Zend/zend_operators.c:2743
(gdb) c  #-------------> 表示继续执行
Continuing.
Breakpoint 2, zend_print_ulong_to_buf (buf=0x7fffffffafe4 "", num=1234) at /home/php-7.0.0/Zend/zend_operators.h:793
(gdb) b 798  #-------------> 798行打个断点
Breakpoint 6 at 0x5f38e2: /home/php-7.0.0/Zend/zend_operators.h:798. (13 locations)
(gdb) c 
Continuing.
Breakpoint 6, zend_print_ulong_to_buf (buf=0x7fffffffafe0 "1234", num=0) at /home/php-7.0.0/Zend/zend_operators.h:798
(gdb) p buf  #-------------> 查看 buf 的值
$2 = 0x7fffffffafe0 "1234"
(gdb) x/10c buf  #-------------> 查看 buf 位置开始,连续 10 个以 char 为单位的内存值
0x7fffffffafe0: 49 &#39;1&#39;  50 &#39;2&#39;  51 &#39;3&#39;  52 &#39;4&#39;  0 &#39;\000&#39;        0 &#39;\000&#39;        0 &#39;\000&#39;        0 &#39;\000&#39;
0x7fffffffafe8: 0 &#39;\000&#39;        65 &#39;A&#39;
(gdb)

我们看到,函数返回的 buf 是字符串类型的 &#39;1234&#39;

我们看看函数 zend_print_ulong_to_buf,其实就是从高位到低位,按个取模(除以 10,取整数部分),然后塞到 buf 缓冲区。

比较有意思的是,buf 初始化的时候指向的是缓冲区的末尾,所以填充的时候高位在最后,然后逐步往前填充低位。

最后结束的时候,buf 就是我们需要的字符串类容了

消化

其实,本文就是使用 gdb 调试了 PHP 代码,仅此而已。

更多的是给大家提供了一个直接上手玩玩的机会,你所需要的只是个 docker,然后动动手调试,很有意思。

动手试试吧,甚至,去看 C 源码吧!

附录

手摸手带你看 strval 函数 C 实现

以上就是【docker+gdb】调试 PHP 源码,看 strval 函数 C 实现的详细内容,更多请关注 第一PHP社区 其它相关文章!


推荐阅读
  • 在Docker中,将主机目录挂载到容器中作为volume使用时,常常会遇到文件权限问题。这是因为容器内外的UID不同所导致的。本文介绍了解决这个问题的方法,包括使用gosu和suexec工具以及在Dockerfile中配置volume的权限。通过这些方法,可以避免在使用Docker时出现无写权限的情况。 ... [详细]
  • (九)Docker常用安装
    一、总体步骤1、搜索镜像2、拉取镜像3、查看镜像4、启动镜像5、停止镜像6、移除镜像二、安装tomcat1、dockerhub上面查找tomcat镜像 dockersearchto ... [详细]
  • 有意向可以发简历到邮箱内推.简历直达组内Leader.能做同事的话,内推奖励全给你. ... [详细]
  • MySQL5.6.40在CentOS764下安装过程 ... [详细]
  • Java验证码——kaptcha的使用配置及样式
    本文介绍了如何使用kaptcha库来实现Java验证码的配置和样式设置,包括pom.xml的依赖配置和web.xml中servlet的配置。 ... [详细]
  • imx6ull开发板驱动MT7601U无线网卡的方法和步骤详解
    本文详细介绍了在imx6ull开发板上驱动MT7601U无线网卡的方法和步骤。首先介绍了开发环境和硬件平台,然后说明了MT7601U驱动已经集成在linux内核的linux-4.x.x/drivers/net/wireless/mediatek/mt7601u文件中。接着介绍了移植mt7601u驱动的过程,包括编译内核和配置设备驱动。最后,列举了关键词和相关信息供读者参考。 ... [详细]
  • CentOS7.8下编译muduo库找不到Boost库报错的解决方法
    本文介绍了在CentOS7.8下编译muduo库时出现找不到Boost库报错的问题,并提供了解决方法。文章详细介绍了从Github上下载muduo和muduo-tutorial源代码的步骤,并指导如何编译muduo库。最后,作者提供了陈硕老师的Github链接和muduo库的简介。 ... [详细]
  • Redis的默认端口、数据库使用和多端口配置
    本文介绍了Redis的默认端口、数据库使用和多端口配置的方法。通过选择不同的数据库和使用flushdb命令可以实现对不同数据库的访问和清除数据。同时,本文还介绍了在同一台机器上启用多个Redis实例的方法,并讨论了配置认证密码的步骤和注意事项。 ... [详细]
  • 大坑|左上角_pycharm连接服务器同步写代码(图文详细过程)
    篇首语:本文由编程笔记#小编为大家整理,主要介绍了pycharm连接服务器同步写代码(图文详细过程)相关的知识,希望对你有一定的参考价值。pycharm连接服务 ... [详细]
  • 现在比较流行使用静态网站生成器来搭建网站,博客产品着陆页微信转发页面等。但每次都需要对服务器进行配置,也是一个重复但繁琐的工作。使用DockerWeb,只需5分钟就能搭建一个基于D ... [详细]
  • Hello,Imaintainawebcluster,withDebiani386andDebianAMD64nodes(itwa ... [详细]
  • docker容器的数据管理一:数据卷实现数据的永久化,完全独立于容 ... [详细]
  • systemd-nspawn可以创建最轻量级的容器(ns的意思就是namespace),本文的实验平台是Ubuntu16.04,x86_64机器。本文的目的是:在Ubuntu中用syst ... [详细]
  • docker安装到基本使用
    记录docker概念,安装及入门日常使用Docker安装查看官方文档,在&quot;Debian上安装Docker&quot;,其他平台在&quot;这里查 ... [详细]
  • 这篇文章给大家介绍怎么从源码启动和编译IoTSharp ,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。IoTSharp项目是 ... [详细]
author-avatar
手机用户2502929415
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有