1N/A#!/usr/bin/sh
1N/A#
1N/A# dapptrace - trace user and library function usage.
1N/A# Written using DTrace (Solaris 10 3/05).
1N/A#
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#
1N/A# $Id: dapptrace 65 2007-10-04 11:09:40Z brendan $
1N/A#
1N/A# USAGE: dapptrace [-acdeFlhoU] [-u lib] { -p PID | command }
1N/A#
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# -l # print pid/lwpid per line
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# eg,
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#
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#
1N/A# SEE ALSO: dappprof # DTraceToolkit
1N/A# dtruss # DTraceToolkit
1N/A# apptrace
1N/A#
1N/A# COPYRIGHT: Copyright (c) 2005 Brendan Gregg.
1N/A#
1N/A# CDDL HEADER START
1N/A#
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# with the License.
1N/A#
1N/A# You can obtain a copy of the license at Docs/cddl1.txt
1N/A# or http://www.opensolaris.org/os/licensing.
1N/A# See the License for the specific language governing permissions
1N/A# and limitations under the License.
1N/A#
1N/A# CDDL HEADER END
1N/A#
1N/A# Author: Brendan Gregg [Sydney, Australia]
1N/A#
1N/A# 16-May-2005 Brendan Gregg Created this.
1N/A# 17-Jul-2005 " " Last update.
1N/A#
1N/A
1N/A
1N/A##############################
1N/A# --- Process Arguments ---
1N/A#
1N/A
1N/A### Default variables
1N/Aopt_pid=0; pid=0; opt_indent=0; opt_lib=0; lib=""
1N/Aopt_elapsed=0; opt_cpu=0; opt_counts=0;
1N/Aopt_relative=0; opt_printid=0; opt_liball=0
1N/Aopt_command=0; command=""; opt_buf=0; buf="4m"
1N/A
1N/A### Process options
1N/Awhile getopts ab:cdeFhlop:u:U name
1N/Ado
1N/A case $name in
1N/A a) opt_liball=1; opt_counts=1; opt_relative=1; opt_elapsed=1
1N/A opt_indent=1; opt_printid=1; opt_cpu=1 ;;
1N/A b) opt_buf=1; buf=$OPTARG ;;
1N/A p) opt_pid=1; pid=$OPTARG ;;
1N/A u) opt_lib=1; lib=$OPTARG ;;
1N/A U) opt_liball=1 ;;
1N/A c) opt_counts=1 ;;
1N/A d) opt_relative=1 ;;
1N/A e) opt_elapsed=1 ;;
1N/A F) opt_indent=1 ;;
1N/A l) opt_printid=1 ;;
1N/A o) opt_cpu=1 ;;
1N/A h|?) cat <<-END >&2
1N/A USAGE: dapptrace [-acdeholFLU] [-u lib] { -p PID | command }
1N/A
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 eg,
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/A END
1N/A exit 1
1N/A esac
1N/Adone
1N/Ashift `expr $OPTIND - 1`
1N/A
1N/A### Option logic
1N/Aif [ $opt_pid -eq 0 ]; then
1N/A opt_command=1
1N/A if [ "$*" = "" ]; then
1N/A $0 -h
1N/A exit
1N/A fi
1N/A command="$*"
1N/Afi
1N/A
1N/A### Probe logic
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/Aelse
1N/A probe_entry='pid$target:a.out::entry'
1N/A probe_return='pid$target:a.out::return'
1N/Afi
1N/A
1N/A#################################
1N/A# --- Main Program, DTrace ---
1N/A#
1N/A
1N/A### Define D Script
1N/Adtrace='
1N/A #pragma D option quiet
1N/A
1N/A /*
1N/A * Command line arguments
1N/A */
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
1N/A dtrace:::BEGIN
1N/A {
1N/A /* print header */
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
1N/A /* indent depth */
1N/A depth = 0;
1N/A }
1N/A
1N/A /*
1N/A * Save syscall entry info
1N/A */
1N/A '$probe_entry'
1N/A {
1N/A /* set function depth */
1N/A this->fdepth = ++fdepth[probefunc];
1N/A depth += 2;
1N/A
1N/A /* set start details */
1N/A self->start[probefunc,this->fdepth] = timestamp;
1N/A self->vstart[probefunc,this->fdepth] = vtimestamp;
1N/A
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
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
1N/A /* print main data */
1N/A printf("-> ");
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
1N/A }
1N/A
1N/A /*
1N/A * Print return data
1N/A */
1N/A /* print 3 arg output - default */
1N/A '$probe_return'
1N/A /self->start[probefunc,fdepth[probefunc]]/
1N/A {
1N/A /* fetch function depth */
1N/A this->fdepth = fdepth[probefunc];
1N/A
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
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
1N/A /* print main data */
1N/A printf("<- ");
1N/A OPT_liball ? printf("%s:",probemod) : 1;
1N/A printf("%s = %d\n",probefunc,(int)arg0);
1N/A depth -= 2;
1N/A fdepth[probefunc]--;
1N/A }
1N/A
1N/A /* reset indent depth */
1N/A profile:::tick-1sec
1N/A {
1N/A /*
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 */
1N/A depth = 0;
1N/A }
1N/A
1N/A /* print counts */
1N/A dtrace:::END
1N/A {
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/A }
1N/A'
1N/A
1N/A### Run DTrace
1N/Aif [ $opt_command -eq 1 ]; then
1N/A /usr/sbin/dtrace -x dynvarsize=$buf -x evaltime=exec -n "$dtrace" \
1N/A -c "$command" >&2
1N/Aelse
1N/A /usr/sbin/dtrace -x dynvarsize=$buf -n "$dtrace" -p "$pid" >&2
1N/Afi
1N/A