Strace for MacOSX (dtruss)

For some reasons, sometimes we want to use strace on MacOSX. But strace is absent in MacOSX. It not sad thing, because we can use dtruss!

In this example we try to trace php script under Apache.
At first we try to connect to server with telnet:

Second step is:
After that we open another tab of terminal and use lsof command:

Search “telnet…TCP localhost:49931->localhost:http (ESTABLISHED)”.
We need 49931.
In third step:
We use lsof instead of netstat.
sudo lsof -Pn | grep 49933 – on MacOSX
netstat -nap | grep 49933 – on linux
Search “httpd 1155 _www…TCP 127.0.0.1:80->127.0.0.1:49933 (ESTABLISHED)”.
We need 1155.
The final step is:
sudo dtruss -aeodf -p 1156 -n php – on MacOSX
strace -o /tmp/output.txt -f -r -s4096 -p 1155 – on Linux
After that we need to switch to first terminal tab (with telnet) and type:

Where second parameter (/?parent=509) is our php script address. It can be /script.php for example.
Finally tap Return twice.
After that you can abort connection and switch to second tab of terminal and see many lines of trace.
Enjoy learning 😉

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.