Strace example script

From wiki.mikejung.biz
Jump to navigation Jump to search

Liquidweb 728x90.jpg

Strace example script

An Strace script that will help determine why a file, such as index.php is hanging, or being slow.

#! /bin/bash
programstrace=`strace -tt -T php index.php 2>&1`
for calllen in `echo "$programstrace"|rev|grep "^>"|cut -d '>' -f2| cut -d '<' -f1|rev|sort -nr| head -20`
do echo "$programstrace" | grep $calllen -B1
done


Strace Overview

Strace is a program that traces system calls, it's able to attach to running processes and show a lot of detail about what is going on.

Install Strace on CentOS

yum install strace

Strace Commands

Find Strace Version

This command will tell you what version of strace you are running. It's a good idea to make sure you are using the updated version of strace.

strace -V

Use Strace to attach to running process

You can use ps faux to list all running processes, once you find the process you want to attach to, simply replace $PID with the process ID.

strace -p $PID

Save Strace Output to a File

If you would rather save strace info to a file for later review, you can do so by using the -o option and specifying a name. You can specify any name you want.

strace -o $strace.file -p $PID

Strace Timestamp Options

There are three Strace options for placing timestamps on each event that happens while using strace. -t is the most basic option, but you can use -tt to get microsecond granularity, or -ttt to get millisecond granularity.

-t
-tt
-ttt