This is a mirror of official site: http://jasper-net.blogspot.com/

Linux application/script debugging with ‘strace’

| Wednesday, January 25, 2012
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 strace

Strace 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 strace

You 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.php

Or you want to attach to a running process, use this:

    ~# strace -f -p $pid
    ~# strace -f -p 8151

The -f parameter tells strace to follow any children or processes that are spawned/forked from the application.
Standard usage of strace

By 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: http://chart.googleapis.com/chart?chs=80x80&cht=qr&choe=UTF-8&chl=http://mattiasgeniar.be/2012/01/21/linux-application-script-debugging-with-strace/

Posted via email from Jasper-net

0 comments: