CLIçã®PHPãgdbä¸ã§åããã¤ã¤ãå é¨çãªãã¼ã¿æ§é ãè¦ãè¦ããã¨ãã§ãããããªDockerã¤ã¡ã¼ã¸ãä½ã£ã¦Docker Hubã«ã¢ãããã¼ããã¾ãããDockerç°å¢ããããã°ããã«åãããã¨ãã§ãã¾ãã
ãã®ã¤ã¡ã¼ã¸ãåããã°Cã®ã³ã¼ããæ¸ããªãã¦ãPHPå
é¨ã®ãã¼ã¿æ§é ã確èªãããã¨ãã§ãã¾ããPHPã®å
é¨æ§é ã詳ããç¥ããããã¨ãããããªäººã¯åèã«ãã¦ã¿ã¦ãã ããã
æºå
Macã®äººã¯Docker for Macãç¨æãã¾ããããä»ã®OSä¸ã§ãåæ§ã«Dockerãã¤ã³ã¹ãã¼ã«ãã¦ãã ãããã¾ããã¤ã¡ã¼ã¸ã®å§ç¸®æãµã¤ãºã200MBç¨åº¦ããã¾ãã®ã§ããããªãã®ãããã¯ã¼ã¯ç°å¢ã§éã¶ãã¨ããªã¹ã¹ã¡ãã¾ãã
èµ·å
Docker 1.10以éã§ã¯seccompã«ããä¸é¨ã·ã¹ãã ã³ã¼ã«ãå¶éããã¦ãããããã³ã³ããå
ã§gdbã«ãããããã°ãã§ãã¾ãããæå¾
éãã«gdbãåããã«ã¯ã³ã³ããèµ·åæã«ã³ãã³ãã©ã¤ã³ãªãã·ã§ã³ã--cap-add=SYS_PTRACE --security-opt seccomp=unconfinedããä»ããå¿
è¦ãããã¾ãã
$ docker run -v $(pwd):/work -w /work --rm -it --cap-add=SYS_PTRACE --security-opt seccomp=unconfined yhnw/php-debug:7.1 /bin/bash
éã³æ¹
ã¨ããããzvalã®ä¸èº«ãè¦ãã¦ã¿ã¾ãããã
ã¾ãä½æ¥ãã£ã¬ã¯ããªã«ç§»åãã¦ãé©å½ãªPHPããã°ã©ã ãä½ãã¾ããä»åã¯ä¸èº«ãè¦ã¦ã¿ããå¤æ°ãåæãã¦var_dump()ãã¦ã¿ã¾ãããã
$ mkdir -p work/php-debug $ cd work/php-debug $ vim var_dump.php
ä»åä½æããvar_dump.phpã¯ä¸è¨ã®ãããªãã¡ã¤ã«ã§ãã
<?php $a = strtoupper("foo"); $b = [ $a, &$a, "FOO"]; $c = $a; var_dump($b); $a = 123; $b[1000] = 1234.5; var_dump($b);
ãã®ãã£ã¬ã¯ããªã§Dockerã¤ã¡ã¼ã¸ãyhnw/php-debug:7.1ããå®è¡ãã¦gdbãèµ·åãã¾ãã
$ docker run -v $(pwd):/work -w /work --rm -it --cap-add=SYS_PTRACE --security-opt seccomp=unconfined yhnw/php-debug:7.1 /bin/bash root@536f406bded6:/work# gdb php GNU gdb (Debian 7.7.1+dfsg-5) 7.7.1 Copyright (C) 2014 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.
ã¾ãPHPé¢æ°var_dump()ã«ãã¬ã¼ã¯ãã¤ã³ããè¨ç½®ãã¾ããPHPã®å
é¨é¢æ°ã¯Cã®ä¸çã§ã¯ãzif_ãã¨ãããã¬ãã£ãã¯ã¹ãã¤ãã¾ãã®ã§ãzif_var_dumpã¨ããé¢æ°åã«ãªãã¾ãã
(gdb) b zif_var_dump Breakpoint 1 at 0x8429a4: file /usr/src/php/ext/standard/var.c, line 205.
ã§ã¯å
ã»ã©ã®PHPããã°ã©ã ãå®è¡ãã¦ã¿ã¾ãããã
(gdb) run var_dump.php Starting program: /usr/local/bin/php var_dump.php [Thread debugging using libthread_db enabled] Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1". Breakpoint 1, zif_var_dump (execute_data=0x7ffff2214140, return_value=0x7fffffffb0b0) at /usr/src/php/ext/standard/var.c:205 205 if (zend_parse_parameters(ZEND_NUM_ARGS(), "+", &args, &argc) == FAILURE) { (gdb) n 209 for (i = 0; i < argc; i++) {
å®è¡ããã¨ãã¬ã¼ã¯ãã¤ã³ãã®è¨å®ã«ããPHPããã°ã©ã 5è¡ç®ã®var_dump()ã§åæ¢ãã¾ãã次ã«ãnããã¿ã¤ããã¦zend_parse_parameters()ãå®è¡ãã¦ãã¾ããããã«ããvar_dump()ã®ãã©ã¡ã¼ã¿ãargsã«ä»£å
¥ããã¾ãã
ãã¦ãã§ã¯var_dump()ã®ç¬¬ä¸å¼æ°$bã®ä¸èº«ãè¦ã¦ã¿ã¾ãããã
(gdb) printzv &args[0] [0x7ffff2214190] (refcount=2) array: Packed(3)[0x7ffff2258300]: { [0] 0 => [0x7ffff225fa08] (refcount=3) string: FOO [1] 1 => [0x7ffff225fa28] (refcount=2) reference: [0x7ffff2202120] (refcount=3) string: FOO [2] 2 => [0x7ffff225fa48] (refcount=0) string: FOO }
æ
å ±éãå¤ãã®ã§å
¨é¨ã¯èª¬æãã¾ãããã$bã¯3è¦ç´ ã®é
åã§ãããããã·ã¥ãã¼ãã«ãæããªãçã®é
åï¼Packedï¼ã§ãããã¨ããããã¾ããã¾ãã$b[0]ã®æåå"FOO"ã®åç
§ã«ã¦ã³ãã¯3ã§ãããã¨ããããã¾ããããã¯$b[0]ã¨ã$cã¨ã$aã®å®ä½ï¼$aããã³$b[1]ãå
±æãã¦ããï¼ã®3ã¤ããåç
§ããã¦ããããã§ãã$b[1]ã®åç
§ã«ã¦ã³ãã2ãªã®ã¯$aã¨$b[1]ãåãå®ä½ãåç
§ãã¦ããã¨ããæå³ã§ãã
$b[2]ã®åç §ã«ã¦ã³ãã¯0ã§ãããããã¯æååãªãã©ã«ãä»£å ¥ãã¦ãããã¨ãåå ã§ããæååãªãã©ã«ã¯ã¤ã³ã¿ã¼ã³åæååã«ãªããããåç §ã«ã¦ã³ãããã³GCã®å¯¾è±¡å¤ãªã®ã§ãï¼åèï¼ãPHPのインターン化文字列とは何か - hnwの日記ãï¼ã
ã§ã¯ã次ã®ãã¬ã¼ã¯ãã¤ã³ãã¾ã§ããã°ã©ã ãåéãã¦ã¿ã¾ãããã
(gdb) c Continuing. array(3) { [0]=> string(3) "FOO" [1]=> &string(3) "FOO" [2]=> string(3) "FOO" } Breakpoint 1, zif_var_dump (execute_data=0x7ffff2214140, return_value=0x7fffffffb0b0) at /usr/src/php/ext/standard/var.c:205 205 if (zend_parse_parameters(ZEND_NUM_ARGS(), "+", &args, &argc) == FAILURE) {
ããã°ã©ã 5è¡ç®ã®var_dump()ã®çµæã表示ããã8è¡ç®ã®var_dump()ã§åã³åæ¢ãã¾ãããæ¹ãã¦$bã®ä¸èº«ãè¦ã¦ã¿ã¾ãããã
(gdb) n 209 for (i = 0; i < argc; i++) { (gdb) printzv &args[0] [0x7ffff2214190] (refcount=2) array: Hash(4)[0x7ffff2258300]: { [0] 0 => [0x7ffff225fb60] (refcount=2) string: FOO [1] 1 => [0x7ffff225fb80] (refcount=2) reference: [0x7ffff2202120] long: 123 [2] 2 => [0x7ffff225fba0] (refcount=0) string: FOO [3] 1000 => [0x7ffff225fbc0] double: 1234.500000 }
$b[1000]ã«å¤ã代å
¥ãããã¨ã§$bã4è¦ç´ ã®é£æ³é
åï¼Hashï¼ã«å¤ãã£ããã¨ããããã¾ããPHPã¯æ·»åãå
¨é¨æ°åã§ãã£ã¦ãä¸èº«ãã¹ã«ã¹ã«ã«ãªã£ã¦ãã¾ããããªå ´åã¯å
é¨æ§é ã¨ãã¦é£æ³é
åãæ¡ç¨ãã¾ããåè¦ç´ ã®zvalã®ã¢ãã¬ã¹ãæ¸ãæãã£ã¦ãããã¨ããããã¼ã¿æ§é ã®å¤æ´ã«ä¼´ãè¦ç´ ã®ã¡ã¢ãªã³ãã¼ãçºçãã¦ãããã¨ããããã¾ããä»çµã¿ä¸å½ç¶ã®ãã¨ã§ãããPHP 7ã«ããã¦å½åé
åã¨ãã¦å©ç¨ãã¦ãããã®ãéä¸ããé£æ³é
åã¨ãã¦å©ç¨ããã®ã¯éå¹çãªã®ã§ãã
ã¾ãã$a[0]ã®åç §ã«ã¦ã³ãã1æ¸ã£ã¦2ã«ãªã£ã¦ãããã¨ããããã¾ããããã¯$aã®å®ä½ã123ã«æ¸ãæãããåç §å ãå¤ãã£ãããã§ãã
ã¡ãªã¿ã«printzvã¨ããã®ã¯PHPã«æ¨æºæ·»ä»ã®.gdbinitã§å®ç¾©ããã¦ããé¢æ°ã§ãzvalã®ä¸èº«ãè¦ãã®ã«é常ã«ä¾¿å©ã§ããããgdbã®æ¨æºã³ãã³ãã§$b[0]ã®ä¸èº«ãè¦ãã¨ããã°ã次ã®ãããªæé ãå¿
è¦ã«ãªãã¾ãã
(gdb) p args[0].u1.v.type $1 = 7 '\a' (gdb) p args[0].value.arr.arData[0]->val.u1.v.type $2 = 6 '\006' (gdb) p (char *)args[0].value.arr.arData[0]->val->value.str.val $3 = 0x7ffff22649d8 "FOO" (gdb) p args[0].value.arr.arData[0]->val->value.str->gc->refcount $4 = 2
ã¾ã$bãé
åã§ãããã¨ï¼type=7ï¼ã確èªãã次ã«$b[0]ãæååã§ãããã¨ï¼type=6ï¼ã確èªãã¦ãã®å¤ã«ã¢ã¯ã»ã¹ãã¦ãã¾ããzvalã§ã¯å
±ç¨ä½ãå¤ç¨ãã¦ããã®ã§ä»æ¹ãªãã®ã§ãããã¡ãã£ã¨é¢åããã¾ãããã
ã·ã³ãã«ãã¼ãã«ã確èªãã
ã·ã³ãã«ãã¼ãã«ã確èªããæ¹æ³ãç´¹ä»ãã¦ããã¾ããã°ãã¼ãã«ãªã·ã³ãã«ãã¼ãã«ï¼ã¤ã¾ãã°ãã¼ãã«å¤æ°ï¼ã¯æ¬¡ã®ããã«ç¢ºèªã§ãã¾ãã
(gdb) print_ht &executor_globals.symbol_table Hash(10)[0x1368530]: { [0] _GET => [0x7ffff2259100] (refcount=2) array: [1] _POST => [0x7ffff2259120] (refcount=2) array: [2] _COOKIE => [0x7ffff2259140] (refcount=2) array: [3] _FILES => [0x7ffff2259160] (refcount=2) array: [4] argv => [0x7ffff2259180] (refcount=2) array: [5] argc => [0x7ffff22591a0] long: 1 [6] _SERVER => [0x7ffff22591c0] (refcount=2) array: [7] a => [0x7ffff22591e0] indirect: [0x7ffff2214080] (refcount=2) reference: [0x7ffff2202120] long: 123 [8] b => [0x7ffff2259200] indirect: [0x7ffff2214090] (refcount=2) array: [9] c => [0x7ffff2259220] indirect: [0x7ffff22140a0] (refcount=2) string: FOO }
ãã®ã¹ã³ã¼ãã§ã®ã·ã³ãã«ãã¼ãã«ï¼ï¼ãã¼ã«ã«å¤æ°ï¼ã¯æ¬¡ã®ããã«ããã°è¦ãããã¨ãããã¾ãï¼PHP 5.2ã¾ã§ã¯å¸¸ã«ãã¼ã«ã«å¤æ°ã«å¯¾å¿ããã·ã³ãã«ãã¼ãã«ãä½ããã¦ããã®ã§ãããPHP 5.3以éã¯å¿
è¦ãªã¨ãã ãä½ãããããã§ãï¼ã
(gdb) print_ht execute_data.symbol_table
Dockerã¤ã¡ã¼ã¸ã«ã¤ãã¦
ã¡ãªã¿ã«ä»åä½ã£ãDockerã¤ã¡ã¼ã¸ã¯ãªãã£ã·ã£ã«ã®phpã¤ã¡ã¼ã¸ã®ãªãã¸ããªãforkãã¦ä½ãã¾ãããTravis CIãå©ç¨ãã¦ãããmasterã«ã³ãããããã¨CIã§Docker Hubã¸ã®ãããã¤ã¾ã§è¡ãããã«ãã¦ããã¾ãã®ã§ãåèã«ãã¦ã¿ã¦ãã ããã
ã¡ãªã¿ã«5.6ã¨7.0ã®ã¤ã¡ã¼ã¸ããããã¤æ¸ã¿ã§ãã
ã¾ã¨ã
Cãgdbãè¯ãããããªãã¦ããPHPããã°ã©ã ä¸ã®var_dump()ã®ã¿ã¤ãã³ã°ã§æ¢ããæ¹æ³ã¨ãprintzvãã ãã§çµæ§éã¹ãã®ã§ã¯ãªãã§ãããããããå°ãæ·±ãç¥ãããå ´åã¯gdbã®ããã¥ã¢ã«ããã³PHPã½ã¼ã¹ã³ã¼ãã®Zend/以ä¸ã調ã¹ã¦ã¿ã¦ãã ããã