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 <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".
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
輸入以下內容
<?php
strval(1234);
這個檔案乾的事情就比較簡單了,就是把 -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 <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)
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 = '-'; │
│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 '1' 50 '2' 51 '3' 52 '4' 0 '\000' 0 '\000' 0 '\000' 0 '\000'
0x7fffffffafe8: 0 '\000' 65 'A'
(gdb)
我們看到,函式返回的 buf 是字串型別的 '1234'
我們看看函式
zend_print_ulong_to_buf
,其實就是從高位到低位,按個取模(除以10,取整數部分),然後塞到 buf 緩衝區。比較有意思的是,buf 初始化的時候指向的是緩衝區的末尾,所以填充的時候高位在最後,然後逐步往前填充低位。
最後結束的時候,buf 就是我們需要的字串類容了
消化
其實,本文就是使用 gdb 除錯了 PHP 程式碼,僅此而已。
更多的是給大家提供了一個直接上手玩玩的機會,你所需要的只是個 docker,然後動動手除錯,很有意思。
動手試試吧,甚至,去看 C 原始碼吧!
附錄
本作品採用《CC 協議》,轉載必須註明作者和本文連結