[PHP]hello worldの内部的な挙動を追ってみた
PHPのソースコードをコンパイルした後に、
M-x gdb
Run gdb (like this): gdb -i=mi ./sapi/cli/php
main関数にブレークポイントをしかけて
(gdb) b main
ワンライナーでPHPコードを実行
(gdb) run -r 'echo "hello"'
この後ひたすらstep実行を続けて、それっぽいところまできたらbacktrace
(gdb) bt
#0 sapi_flush () at /home/vagrant/src/php-src/main/SAPI.c:994
#1 0x000000000075f232 in php_output_op (op=0, str=0x7ffff7ed0d20 "hello", len=5) at /home/vagrant/src/php-src/main/output.c:1082
#2 0x000000000075d38a in php_output_write (str=0x7ffff7ed0d20 "hello", len=5) at /home/vagrant/src/php-src/main/output.c:255
#3 0x0000000000745d35 in php_output_wrapper (str=0x7ffff7ed0d20 "hello", str_length=5) at /home/vagrant/src/php-src/main/main.c:1870
#4 0x00000000007dc2a3 in zend_print_zval_ex (write_func=0x745d14 <php_output_wrapper>, expr=0x7ffff7fe6fc8, indent=0) at /home/vagrant/src/php-src/Zend/zen\
d.c:336
#5 0x00000000007dc21c in zend_print_zval (expr=0x7ffff7fe6fc8, indent=0) at /home/vagrant/src/php-src/Zend/zend.c:317
#6 0x00000000007db137 in zend_print_variable (var=0x7ffff7fe6fc8) at /home/vagrant/src/php-src/Zend/zend_variables.c:162
#7 0x0000000000827061 in ZEND_ECHO_SPEC_CONST_HANDLER (execute_data=0x7ffff7faf160) at /home/vagrant/src/php-src/Zend/zend_vm_execute.h:2193
#8 0x0000000000821f34 in execute_ex (execute_data=0x7ffff7faf160) at /home/vagrant/src/php-src/Zend/zend_vm_execute.h:363
#9 0x0000000000821fb7 in zend_execute (op_array=0x7ffff7fe5cb0) at /home/vagrant/src/php-src/Zend/zend_vm_execute.h:388
#10 0x00000000007ca928 in zend_eval_stringl (str=0xf27930 "echo \"hello\";", str_len=13, retval_ptr=0x0, string_name=0xc0479c "Command line code") at /home/\
vagrant/src/php-src/Zend/zend_execute_API.c:1200
#11 0x00000000007cabd2 in zend_eval_stringl_ex (str=0xf27930 "echo \"hello\";", str_len=13, retval_ptr=0x0, string_name=0xc0479c "Command line code", handle\
_exceptions=1) at /home/vagrant/src/php-src/Zend/zend_execute_API.c:1247
#12 0x00000000007cac5c in zend_eval_string_ex (str=0xf27930 "echo \"hello\";", retval_ptr=0x0, string_name=0xc0479c "Command line code", handle_exceptions=1\
) at /home/vagrant/src/php-src/Zend/zend_execute_API.c:1258
#13 0x00000000008928ea in do_cli (argc=3, argv=0xf278b0) at /home/vagrant/src/php-src/sapi/cli/php_cli.c:1034
#14 0x000000000089387a in main (argc=3, argv=0xf278b0) at /home/vagrant/src/php-src/sapi/cli/php_cli.c:1378
とまあこんな感じです。ここから先は・・・きっとどなたかが解説してくれることでしょう!(他力本願)
明日はoasynnoumさんです。
カテゴリ:
PHP