1N/A# dapptrace - trace user and library function usage. 1N/A# Written using DTrace (Solaris 10 3/05). 1N/A# The default output traces user functions as they are called. Options 1N/A# can be used to examine libraries and timestamps. 1N/A# $Id: dapptrace 65 2007-10-04 11:09:40Z brendan $ 1N/A# USAGE: dapptrace [-acdeFlhoU] [-u lib] { -p PID | command } 1N/A# -p PID # examine this PID 1N/A# -a # print all details 1N/A# -c # print call counts 1N/A# -d # print relative timestamps (us) 1N/A# -e # print elapsed times (us) 1N/A# -F # print flow indentation 1N/A# -o # print on cpu times (us) 1N/A# -u lib # trace this library instead 1N/A# -U # trace all libraries + user functions 1N/A# -b bufsize # dynamic variable buf size (default is "4m") 1N/A# dapptrace df -h # run and examine the "df -h" command 1N/A# dapptrace -p 1871 # examine PID 1871 1N/A# dapptrace -Fp 1871 # print using flow indents 1N/A# dapptrace -eop 1871 # print elapsed and CPU times 1N/A# The elapsed times are interesting, to help identify calls that take 1N/A# some time to complete (during which the process may have context 1N/A# switched off the CPU). 1N/A# SEE ALSO: dappprof # DTraceToolkit 1N/A# dtruss # DTraceToolkit 1N/A# COPYRIGHT: Copyright (c) 2005 Brendan Gregg. 1N/A# The contents of this file are subject to the terms of the 1N/A# Common Development and Distribution License, Version 1.0 only 1N/A# (the "License"). You may not use this file except in compliance 1N/A# See the License for the specific language governing permissions 1N/A# and limitations under the License. 1N/A# Author: Brendan Gregg [Sydney, Australia] 1N/A# 16-May-2005 Brendan Gregg Created this. 1N/A# 17-Jul-2005 " " Last update. 1N/A############################## 1N/A# --- Process Arguments --- 1N/A### Default variables 1N/A USAGE: dapptrace [-acdeholFLU] [-u lib] { -p PID | command } 1N/A -p PID # examine this PID 1N/A -a # print all details 1N/A -c # print syscall counts 1N/A -d # print relative times (us) 1N/A -e # print elapsed times (us) 1N/A -F # print flow indentation 1N/A -l # print pid/lwpid 1N/A -o # print CPU on cpu times 1N/A -u lib # trace this library instead 1N/A -U # trace all libraries + user funcs 1N/A -b bufsize # dynamic variable buf size 1N/A dapptrace df -h # run and examine "df -h" 1N/A dapptrace -p 1871 # examine PID 1871 1N/A dapptrace -Fp 1871 # print using flow indents 1N/A dapptrace -eop 1871 # print elapsed and CPU times 1N/Ashift `expr $OPTIND - 1` 1N/Aif [ $opt_pid -eq 0 ]; then 1N/A if [ "$*" = "" ]; then 1N/Aif [ $opt_liball -eq 1 ]; then 1N/A probe_entry='pid$target:::entry' 1N/A probe_return='pid$target:::return' 1N/Aelif [ $opt_lib -eq 1 ]; then 1N/A probe_entry='pid$target:'$lib'::entry' 1N/A probe_return='pid$target:'$lib'::return' 1N/A probe_entry='pid$target:a.out::entry' 1N/A probe_return='pid$target:a.out::return' 1N/A################################# 1N/A# --- Main Program, DTrace --- 1N/A #pragma D option quiet 1N/A * Command line arguments 1N/A inline int OPT_command = '$opt_command'; 1N/A inline int OPT_liball = '$opt_liball'; 1N/A inline int OPT_indent = '$opt_indent'; 1N/A inline int OPT_printid = '$opt_printid'; 1N/A inline int OPT_relative = '$opt_relative'; 1N/A inline int OPT_elapsed = '$opt_elapsed'; 1N/A inline int OPT_cpu = '$opt_cpu'; 1N/A inline int OPT_counts = '$opt_counts'; 1N/A inline int OPT_pid = '$opt_pid'; 1N/A inline int PID = '$pid'; 1N/A inline string NAME = "'$pname'"; 1N/A OPT_printid ? printf("%-8s ","PID/LWP") : 1; 1N/A OPT_relative ? printf("%8s ","RELATIVE") : 1; 1N/A OPT_elapsed ? printf("%7s ","ELAPSD") : 1; 1N/A OPT_cpu ? printf("%6s ","CPU") : 1; 1N/A printf("CALL(args) \t\t = return\n"); 1N/A * Save syscall entry info 1N/A /* set function depth */ 1N/A this->fdepth = ++fdepth[probefunc]; 1N/A /* set start details */ 1N/A self->start[probefunc,this->fdepth] = timestamp; 1N/A self->vstart[probefunc,this->fdepth] = vtimestamp; 1N/A /* count occurances */ 1N/A OPT_counts && OPT_liball ? @Counts[probemod,probefunc] = count() : 1; 1N/A OPT_counts && ! OPT_liball ? @Counts[probefunc] = count() : 1; 1N/A /* print optional fields */ 1N/A OPT_printid ? printf("%5d/%d: ",pid,tid) : 1; 1N/A OPT_relative ? printf("%8d ",vtimestamp/1000) : 1; 1N/A OPT_elapsed ? printf(" . ") : 1; 1N/A OPT_cpu ? printf(" . ") : 1; 1N/A OPT_indent ? printf("%*s",depth,"") : 1; 1N/A /* print main data */ 1N/A OPT_liball ? printf("%s:",probemod) : 1; 1N/A printf("%s(0x%X, 0x%X, 0x%X)\t\t\n",probefunc,arg0,arg1,arg2); 1N/A /* print 3 arg output - default */ 1N/A /self->start[probefunc,fdepth[probefunc]]/ 1N/A /* fetch function depth */ 1N/A this->fdepth = fdepth[probefunc]; 1N/A /* calculate elapsed time */ 1N/A this->elapsed = timestamp - self->start[probefunc,this->fdepth]; 1N/A self->start[probefunc,this->fdepth] = 0; 1N/A this->cpu = vtimestamp - self->vstart[probefunc,this->fdepth]; 1N/A self->vstart[probefunc,this->fdepth] = 0; 1N/A /* print optional fields */ 1N/A OPT_printid ? printf("%5d/%d: ",pid,tid) : 1; 1N/A OPT_relative ? printf("%8d ",vtimestamp/1000) : 1; 1N/A OPT_elapsed ? printf("%7d ",this->elapsed/1000) : 1; 1N/A OPT_cpu ? printf("%6d ",this->cpu/1000) : 1; 1N/A OPT_indent ? printf("%*s",depth,"") : 1; 1N/A /* print main data */ 1N/A OPT_liball ? printf("%s:",probemod) : 1; 1N/A printf("%s = %d\n",probefunc,(int)arg0); 1N/A fdepth[probefunc]--; 1N/A /* reset indent depth */ 1N/A * some probes generated by the pid provider have entries 1N/A * but not returns. this is a klude to fix that problem. this 1N/A * also explains fdepth[probefunc] rather than a single depth. 1N/A OPT_counts ? printf("\n%-49s %16s\n","CALL","COUNT") : 1; 1N/A OPT_counts && OPT_liball ? printa("%-16s %-32s %@16d\n",@Counts) : 1; 1N/A OPT_counts && ! OPT_liball ? printa("%-49s %@16d\n",@Counts) : 1; 1N/Aif [ $opt_command -eq 1 ]; then 1N/A /usr/sbin/dtrace -x dynvarsize=$buf -x evaltime=exec -n "$dtrace" \ 1N/A /usr/sbin/dtrace -x dynvarsize=$buf -n "$dtrace" -p "$pid" >&2