AIX Tip of the Week

truss Command

Audience: Administrators

Date: June 22, 2001

The "truss" utility traces system calls. It's a popular System V tool that allows administrators to identify performance bottlenecks within applications.

The truss command is available in AIX starting in at version 5.1. The following shell script emulates the truss functionality in AIX 4.3.


#!/bin/ksh
# Name: truss.sh
# Purpose: to make AIX trace look like the truss command
# Caveat:  Unsupported tool. Use at your own risk.

show_usage()
{
 echo "Usage: $0 [-P] [-n] [-p pid] [-t tempfile][-s sleeptime | command]"
        echo "         -P           show process id's in the output"
        echo "         -n           show process names in the output"
        echo "         -p pid       trcrpt only for this pid"
 echo "         -s seconds   trace for  period of time"
 echo "         -t tempfile  path name to file that will be used for trace"
        echo "         command      execute this command and stop trace\c"
 echo " after command is done.\n"
 exit 0
}

[ "$#" = 0 ] && show_usage
set -- `getopt t:s:p:nP "$@"` || show_usage

while :; do
        case $1 in
                -s)     sleeptime=$2
                        shift 2;;
  -p) pid=$2
   shift 2;;
  -n)     EXEC="exec=on,"
   shift;;
  -P) PIDNUM="pid=on,"
   shift;;
                --)     shift
                        break;;
        esac
done

command="$*"
[ -n "$command" -a -n "$sleeptime" ] && show_usage
[ -n "$pid" ] && PID="-p $pid"
 
hooks="101,104,107,106,134,139,15B,130,19C,163,169,120,122,108,12E,14C,154,\
152,15F,14E,137,135,13A,19B,13E,174,175,176,177,178,179,17A,17B,17D,17E,\
17F,1A7,1A8,1A4,1A5,1A6,180,18F,195,18E,1A9,1AA,1AC,1AB,1F0,1AF,1AE,1AD"

do_trace()
{
logsize=$1;bufsize=$2;
trace -n -a -L $logsize -T $bufsize -j $hooks -do trace.out || return $?
}

do_trace 8000000 4000000 || {
 echo "You do not have privilege as this uid to allocate a large trace buffer"
 echo "Trying with a smaller buffer, but you may lose data"
 do_trace 8000000 1000000 || {
  echo "You do not have privilege as this uid to allocate a large trace buffer"
  trcstop
  exit 1
 }
}

trcon

if [ -n "$sleeptime" ]; then
 sleep $sleeptime
else
 $command  # run the command
fi
# do whatever you want here
trcstop
trcrpt -k 106 ${PID} -h -O ${EXEC}${PIDNUM}ids=0,timestamp=3 trace.out  # > trcrpt.out


Bruce Spencer,
baspence@us.ibm.com