Every now and then, you'll encounter a problem with an application or a script that is not clear straight away. After the normal troubleshooting, it can be helpful to see the actual system calls that occur when that script executes. Using a tool like strace (manpage) can help you in identifying what is causing the system to slow down or misbehave. In this blogpost I'll show you some examples where strace can be useful for you. Most of it will be with PHP code but they're easy enough anyone can understand them.
Installing straceStrace isn't installed by default on most distributions. To install, do a simple yum install strace when on CentOS/Red Hat or apt-get install strace on Debian/Ubuntu systems.
Getting output from straceYou can use strace in two different ways. You can attach it to an already running process or you can use it to start a custom application or script and follow all system calls from the very beginning. In short, here's how it goes. If you want to start your application and troubleshoot it from beginning to end, you do this: ~# strace -f $command
~# strace -f php -q somefile.phpOr you want to attach to a running process, use this: ~# strace -f -p $pid
~# strace -f -p 8151The -f parameter tells strace to follow any children or processes that are spawned/forked from the application.
Standard usage of straceBy default, strace will show you -all- system calls that your application or script is performing. That can get pretty overwhelming, but it's a good place to start. Take for instance the following simple script called 'test1.php'. <?php
/* Simple buggy script */
for ($i = 0; $i < 5; $i++)
sleep(1);
?>
Read more: # Defining Chaos In Order
QR:
Installing straceStrace isn't installed by default on most distributions. To install, do a simple yum install strace when on CentOS/Red Hat or apt-get install strace on Debian/Ubuntu systems.
Getting output from straceYou can use strace in two different ways. You can attach it to an already running process or you can use it to start a custom application or script and follow all system calls from the very beginning. In short, here's how it goes. If you want to start your application and troubleshoot it from beginning to end, you do this: ~# strace -f $command
~# strace -f php -q somefile.phpOr you want to attach to a running process, use this: ~# strace -f -p $pid
~# strace -f -p 8151The -f parameter tells strace to follow any children or processes that are spawned/forked from the application.
Standard usage of straceBy default, strace will show you -all- system calls that your application or script is performing. That can get pretty overwhelming, but it's a good place to start. Take for instance the following simple script called 'test1.php'. <?php
/* Simple buggy script */
for ($i = 0; $i < 5; $i++)
sleep(1);
?>
Read more: # Defining Chaos In Order
QR: