1235368Sgnn#!/usr/bin/ksh
2235368Sgnn#
3235368Sgnn# lastwords - print last few syscalls for dying processes.
4235368Sgnn#             Written using DTrace (Solaris 10 3/05).
5235368Sgnn#
6235368Sgnn# $Id: lastwords 3 2007-08-01 10:50:08Z brendan $
7235368Sgnn#
8235368Sgnn# This prints the last few system calls for processes matching
9235368Sgnn# the given name, when they exit. This makes use of a ring buffer
10235368Sgnn# so that the impact on the system is minimised.
11235368Sgnn#
12235368Sgnn# USAGE: lastwords command
13235368Sgnn#    eg,
14235368Sgnn#        lastwords netscape
15235368Sgnn#
16235368Sgnn# FIELDS:
17235368Sgnn#           TIME     Time of syscall return, ns
18235368Sgnn#           PID      Process ID
19235368Sgnn#           EXEC     Process name (execname)
20235368Sgnn#           SYSCALL  System call
21235368Sgnn#           RETURN   Return value for system call
22235368Sgnn#           ERR      errno for system call
23235368Sgnn#
24235368Sgnn# BASED ON: /usr/demo/dtrace/ring.d
25235368Sgnn#
26235368Sgnn# SEE ALSO: DTrace Guide "Buffers and Buffering" chapter (docs.sun.com)
27235368Sgnn#           dtruss (DTraceToolkit)
28235368Sgnn#
29235368Sgnn# PORTIONS: Copyright (c) 2005, 2006 Brendan Gregg.
30235368Sgnn#
31235368Sgnn# CDDL HEADER START
32235368Sgnn#
33235368Sgnn#  The contents of this file are subject to the terms of the
34235368Sgnn#  Common Development and Distribution License, Version 1.0 only
35235368Sgnn#  (the "License").  You may not use this file except in compliance
36235368Sgnn#  with the License.
37235368Sgnn#
38235368Sgnn#  You can obtain a copy of the license at Docs/cddl1.txt
39235368Sgnn#  or http://www.opensolaris.org/os/licensing.
40235368Sgnn#  See the License for the specific language governing permissions
41235368Sgnn#  and limitations under the License.
42235368Sgnn#
43235368Sgnn# CDDL HEADER END
44235368Sgnn#
45235368Sgnn# 09-Jun-2005	Brendan Gregg	Created this.
46235368Sgnn# 20-Apr-2006	   "      "	Last update.
47235368Sgnn#
48235368Sgnn
49235368Sgnn### Usage
50235368Sgnnfunction usage
51235368Sgnn{
52235368Sgnn	cat <<-END >&2
53235368Sgnn	USAGE: lastwords command
54235368Sgnn	   eg,
55235368Sgnn	       lastwords netscape
56235368Sgnn	END
57235368Sgnn	exit 1
58235368Sgnn}
59235368Sgnn
60235368Sgnn### Process arguments
61235368Sgnnif (( $# != 1 )); then
62235368Sgnn        usage
63235368Sgnnfi
64235368Sgnncommand=$1
65235368Sgnn
66235368Sgnnprint "Tracing... Waiting for $command to exit..."
67235368Sgnn
68235368Sgnn### Run DTrace
69235368Sgnn/usr/sbin/dtrace -n '
70235368Sgnn #pragma D option quiet
71235368Sgnn #pragma D option bufpolicy=ring
72235368Sgnn #pragma D option bufsize=16k
73235368Sgnn
74235368Sgnn syscall:::return
75235368Sgnn /execname == $$1/
76235368Sgnn {
77235368Sgnn	/* buffer syscall details */
78235368Sgnn	printf("%-18d %5d %12s %12s %10x %3d\n",
79235368Sgnn	    timestamp,pid,execname,probefunc,(int)arg0,errno);
80235368Sgnn }
81235368Sgnn
82235368Sgnn proc::proc_exit:exit
83235368Sgnn /execname == $$1/
84235368Sgnn {
85235368Sgnn	/* print, erm, footer */
86235368Sgnn	printf("%-18s %5s %12s %12s %10s %3s\n",
87235368Sgnn	    "TIME","PID","EXEC","SYSCALL","RETURN","ERR");
88235368Sgnn	exit(0);
89235368Sgnn }
90235368Sgnn' "$command"
91