読者です 読者をやめる 読者になる 読者になる

DELAEMON BLOG

Live as if you were to die tomorrow. Learn as if you were to live forever.

PHPソースコードリーティング準備

必要に迫られてPHPソースコード読まないと行けなくなったので、自分のMBA(MountainLion)で準備。

PHPソースコードをダウンロードしてビルド

$ git clone https://github.com/php/php-src.git Cloning into 'php-src'...
$ cd php-src/
$ ./buildconf  
buildconf: checking installation...
buildconf: autoconf not found.
           You need autoconf version 2.59 or newer installed
           to build PHP from SVN.
make: *** [buildmk.stamp] Error 1
/Users/dela/Documents/develop/Study/php/php-src sudo ./buildconf
Password:
buildconf: checking installation...
buildconf: autoconf not found.
           You need autoconf version 2.59 or newer installed
           to build PHP from SVN.
make: *** [buildmk.stamp] Error 1

You need autoconf version 2.59 or newer installed
ってことらしいので素直に従う。
ダウンロード http://ftp.gnu.org/gnu/autoconf/autoconf-latest.tar.gz

$ cd Downloads/autoconf-2.69/
$ ./configure
$ make
$ make install
$ source ~/.zshrc

再度、PHPをビルド

$ ./buildconf
$ php-src ./configure --disable-all --enable-debug
$ make

テスト

$ sapi/cli/php -r "echo 'hell';"
hell

GDBを試す
まず起動

gdb sapi/cli/php    
GNU gdb 6.3.50-20050815 (Apple version gdb-1820) (Sat Jun 16 02:40:11 UTC 2012)
Copyright 2004 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB.  Type "show warranty" for details.
This GDB was configured as "x86_64-apple-darwin"...Reading symbols for shared libraries ... done

time関数にブレークポイント入れる

(gdb) break zif_time
Breakpoint 1 at 0x100001545: file php_date.c, line 1616.

コマンドラインから実行

(gdb) run -r "echo time();"
Starting program: /Users/dela/Documents/develop/Study/php/php-src/sapi/cli/php -r "echo time();"

Breakpoint 1, zif_time (ht=0, return_value=0x101036c28, return_value_ptr=0x0, this_ptr=0x0, return_value_used=1) at php_date.c:1616
1616		RETURN_LONG((long)time(NULL));

cgdbをインストール

$ brew install cgdb
Warning: It appears you have MacPorts or Fink installed.
Software installed with other package managers causes known problems for
Homebrew. If a formula fails to build, uninstall MacPorts/Fink and try again.
==> Downloading http://downloads.sourceforge.net/project/cgdb/cgdb/cgdb-0.6.6/cgdb-0.6.6.tar.gz
######################################################################## 100.0%
==> ./configure --prefix=/usr/local/Cellar/cgdb/0.6.6 --with-readline=/usr/local/Cellar/readline/6.2.4
==> make install
/usr/local/Cellar/cgdb/0.6.6: 10 files, 400K, built in 16 seconds

f:id:delaemon:20121006142456p:plain

vimキーバインドで操作しやすいし
コード見ながらブレイクポイントを入れたり、
実行時に入れたブレイクポイントの行に飛んでくれて
なかなかいい感じ。