1N/A#!/usr/bin/ksh
1N/A#
1N/A# lastwords - print last few syscalls for dying processes.
1N/A# Written using DTrace (Solaris 10 3/05).
1N/A#
1N/A# $Id: lastwords 3 2007-08-01 10:50:08Z brendan $
1N/A#
1N/A# This prints the last few system calls for processes matching
1N/A# the given name, when they exit. This makes use of a ring buffer
1N/A# so that the impact on the system is minimised.
1N/A#
1N/A# USAGE: lastwords command
1N/A# eg,
1N/A# lastwords netscape
1N/A#
1N/A# FIELDS:
1N/A# TIME Time of syscall return, ns
1N/A# PID Process ID
1N/A# EXEC Process name (execname)
1N/A# SYSCALL System call
1N/A# RETURN Return value for system call
1N/A# ERR errno for system call
1N/A#
1N/A# BASED ON: /usr/demo/dtrace/ring.d
1N/A#
2N/A# SEE ALSO: DTrace Guide "Buffers and Buffering" chapter (docs.oracle.com)
1N/A# dtruss (DTraceToolkit)
1N/A#
1N/A# PORTIONS: Copyright (c) 2005, 2006 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# 09-Jun-2005 Brendan Gregg Created this.
1N/A# 20-Apr-2006 " " Last update.
1N/A#
1N/A
1N/A### Usage
1N/Afunction usage
1N/A{
1N/A cat <<-END >&2
1N/A USAGE: lastwords command
1N/A eg,
1N/A lastwords netscape
1N/A END
1N/A exit 1
1N/A}
1N/A
1N/A### Process arguments
1N/Aif (( $# != 1 )); then
1N/A usage
1N/Afi
1N/Acommand=$1
1N/A
1N/Aprint "Tracing... Waiting for $command to exit..."
1N/A
1N/A### Run DTrace
1N/A/usr/sbin/dtrace -n '
1N/A #pragma D option quiet
1N/A #pragma D option bufpolicy=ring
1N/A #pragma D option bufsize=16k
1N/A
1N/A syscall:::return
1N/A /execname == $$1/
1N/A {
1N/A /* buffer syscall details */
1N/A printf("%-18d %5d %12s %12s %10x %3d\n",
1N/A timestamp,pid,execname,probefunc,(int)arg0,errno);
1N/A }
1N/A
1N/A proc::proc_exit:exit
1N/A /execname == $$1/
1N/A {
1N/A /* print, erm, footer */
1N/A printf("%-18s %5s %12s %12s %10s %3s\n",
1N/A "TIME","PID","EXEC","SYSCALL","RETURN","ERR");
1N/A exit(0);
1N/A }
1N/A' "$command"