Posts Tagged ‘macosx’

Finding syscalls generated by PHP functions with Dtrace

August 4th, 2010

I wanted to check a list of syscalls that are generated by PHP for a bunch of functions. Dtrace saved the day again, in 10 minutes I had all I needed.

Here is a PHP script you can use for testing (syscalls.php) :

<?php
file_exists( __FILE__ );
file_get_contents( __FILE__ );
chdir( __DIR__ );
dir( __DIR__ );
getcwd();
opendir( __DIR__ );
scandir( __DIR__ );
new DirectoryIterator( __DIR__ );
stat( __DIR__ );
is_readable( __DIR__ );
is_writable( __DIR__ );
file_put_contents('/tmp/foo.txt', 'foo');
?>

And the small Dtrace (syscalls.d) script I used:#!/usr/sbin/dtrace -s

#pragma D option quiet

php*:::function-entry
/pid == $target && arg0/
{
 printf( "%s%s%s\n", copyinstr(arg3), copyinstr(arg4), copyinstr(arg0) );
 self->follow++;
}

syscall:::entry
/self->follow > 0/
{
 printf("  ->%s\n", probefunc);
}

php*:::function-return
/pid == $target && arg0/
{
 self->follow -= self->follow == 0 ? 0 : 1;
 printf("\n");
}

You can test the script by running :

sudo dtrace -s syscalls.d -c "php syscalls.php"

And you should get a list like this (at least on Mac Os X)

file_exists
 ->access

file_get_contents
 ->lstat
 ->lstat
 ->open
 ->fstat
 ->lseek
 ->fstat
 ->read
 ->read
 ->read
 ->close

chdir
 ->chdir

dir
 ->open_nocancel
 ->fcntl_nocancel
 ->__sysctl
 ->fstatfs

getcwd
 ->open_nocancel
 ->fstat64
 ->fcntl_nocancel
 ->close_nocancel
 ->stat64

opendir
 ->open_nocancel
 ->fcntl_nocancel
 ->fstatfs
 ->close_nocancel

scandir
 ->open_nocancel
 ->fcntl_nocancel
 ->fstatfs
 ->getdirentries
 ->getdirentries
 ->close_nocancel

DirectoryIterator::__construct
 ->open_nocancel
 ->fcntl_nocancel
 ->fstatfs
 ->getdirentries

 ->close_nocancel
stat
 ->stat

is_readable
 ->access

is_writable
 ->access

file_put_contents
 ->lstat
 ->lstat
 ->readlink
 ->lstat
 ->lstat
 ->open
 ->fstat
 ->lseek
 ->write
 ->close

If I have time I’ll post more Dtrace scripts useful for analysing what PHP does.

‘Hope that helps :)

Edit :

I fixed a small bug for the DirectoryOperator class.

Digg This
Reddit This
Stumble Now!
Buzz This
Vote on DZone
Share on Facebook
Bookmark this on Delicious
Kick It on DotNetKicks.com
Shout it
Share on LinkedIn
Bookmark this on Technorati
Post on Twitter
Google Buzz (aka. Google Reader)

Compiling PHP 5.3 on Mac OS X

July 18th, 2009

A few people find it hard to compile PHP on Mac OS X and prefer using tools like Mamp or MacPorts.

Even though MacPorts is useful I prefer compiling PHP by myself, here is a step by step tutorial.

Note 1: this tutorial has been written for Mac OS X 10.4 but I am sure it is the same for more recent versions.

Note 2 : with the configuration directives described below PHP is compiled in CGI/FastCGI mode if you want to use it as an Apache module this only thing you have is to do is to use the following extra argument :

--with-apxs2=/path/to/apache2/apxs

1. Downloading PHP 5.3

wget http://fr3.php.net/get/php-5.3.0.tar.gz/from/fr.php.net/mirror

2. Extracting the tarball

tar zxvf php-5.3.0.tar.gz

3. Configuring PHP


cd php-5.3.0

./configure \
–prefix=/opt/php5 \
–enable-exif \
–with-gd=/opt/local \
–with-freetype-dir=/opt/local \
–with-jpeg-dir=/opt/local \
–enable-gd-native-ttf \
–with-png-dir=/opt/local \
–with-t1lib=/opt/local \
–with-zlib=/opt/local \
–with-mysql=/usr/local/mysql \
–with-mysqli \
–with-ldap \
–enable-mbstring \
–with-mcrypt=/opt/local \
–with-bz2=/usr/bin \
–enable-ctype \
–enable-dom \
–enable-libxml \
–with-iconv \
–enable-pdo \
–with-pdo-sqlite \
–enable-posix \
–enable-simplexml \
–enable-xml \
–with-zlib \
–with-curl \
–enable-filter \
–with-pdo-mysql=/usr/local/mysql \
–with-openssl \
–with-xmlrpc \
–with-xsl \
–enable-soap \
–enable-ftp \
–enable-pcntl \
–enable-shmop

4. Compiling and installing

make && sudo make install

You can go to the kitchen and get a tea or a coffe while it is compiling.

If you do not like tea or coffee you can also try other fun activities like the following :

Source : http://xkcd.com/303/

Back from the kitchen, you are done. PHP 5.3 is now compiled and installed on your machine.

You can of course use a few extra CFLAGS or options to the configure script if you want to, but the most basic steps are here and you now know the required steps

Now a small tip, let us say I want to compile with the --with-mysql=/path/to/wrong/mysql/directory argument but unfortunately the configure script fails with such an error message :

configure: error: Cannot find MySQL header files under /path/to/wrong/directory.
Note that the MySQL client library is not bundled anymore!

How to find the correct path and fix the problem ?

1. I know that the mysql extension is available in the ext/mysql directory

2. the configuration related to this module is in config.m4

By opening ext/mysql/config.m4 I discover (line 77) that the mysql.h header is required and it is searched in the following directories (still on line 77) :

- /usr/local

- /usr/

The only thing I have to do is searching for mysql.h. Since I use the official MySQL binary I know it is installed in /usr/local/mysql/ so if I look in /usr/local/mysql/include/ I find the required mysql.h file. The config.m4 file searches for /include/mysql.h (line 81) so the only thing I have to do is to correct my –with-mysql argument and use --with-mysql=/usr/local/mysql. By relaunching the configure script it worked and we have been able to compile PHP with MySQL successfully :)

Since you now have compiled PHP by yourself the only limit for you now is your imagination :)

Digg This
Reddit This
Stumble Now!
Buzz This
Vote on DZone
Share on Facebook
Bookmark this on Delicious
Kick It on DotNetKicks.com
Shout it
Share on LinkedIn
Bookmark this on Technorati
Post on Twitter
Google Buzz (aka. Google Reader)