1#!/usr/sbin/dtrace -Zs
2/*
3 * pl_syscolors.d - trace Perl subroutine flow plus syscalls, in color.
4 *                  Written for the Perl DTrace provider.
5 *
6 * $Id: pl_syscolors.d 27 2007-09-13 09:26:01Z brendan $
7 *
8 * USAGE: pl_syscolors.d { -p PID | -c cmd }	# hit Ctrl-C to end
9 *
10 * This watches Perl subroutine entries and returns, and indents child
11 * subroutine calls.
12 *
13 * FIELDS:
14 *		C		CPU-id
15 *		PID		Process ID
16 *		DELTA(us)	Elapsed time from previous line to this line
17 *		FILE		Filename of the Perl program
18 *		LINE		Line number of filename
19 *		TYPE		Type of call (sub/syscall)
20 *		NAME		Perl subroutine or syscall name
21 *
22 * Filename and subroutine names are printed if available.
23 *
24 * WARNING: Watch the first column carefully, it prints the CPU-id. If it
25 * changes, then it is very likely that the output has been shuffled.
26 *
27 * COPYRIGHT: Copyright (c) 2007 Brendan Gregg.
28 *
29 * CDDL HEADER START
30 *
31 *  The contents of this file are subject to the terms of the
32 *  Common Development and Distribution License, Version 1.0 only
33 *  (the "License").  You may not use this file except in compliance
34 *  with the License.
35 *
36 *  You can obtain a copy of the license at Docs/cddl1.txt
37 *  or http://www.opensolaris.org/os/licensing.
38 *  See the License for the specific language governing permissions
39 *  and limitations under the License.
40 *
41 * CDDL HEADER END
42 *
43 * 09-Sep-2007	Brendan Gregg	Created this.
44 */
45
46#pragma D option quiet
47#pragma D option switchrate=10
48
49self int depth;
50
51dtrace:::BEGIN
52{
53	/*
54	 * The following are terminal color escape sequences.
55	 * Change them to whatever you prefer, eg HTML font tags.
56	 */
57        color_perl = "\033[2;35m";		/* violet, faint */
58        color_syscall = "\033[2;32m";		/* green, faint */
59        color_off = "\033[0m";			/* default */
60
61	printf("%s %6s %10s  %16s:%-4s %-8s -- %s\n", "C", "PID", "DELTA(us)",
62	    "FILE", "LINE", "TYPE", "NAME");
63}
64
65perl$target:::sub-entry,
66perl$target:::sub-return,
67syscall:::entry,
68syscall:::return
69/self->last == 0 && pid == $target/
70{
71	self->last = timestamp;
72}
73
74perl$target:::sub-entry
75{
76	this->delta = (timestamp - self->last) / 1000;
77	printf("%s%d %6d %10d  %16s:%-4d %-8s %*s-> %s%s\n", color_perl,
78	    cpu, pid, this->delta, basename(copyinstr(arg0)), arg2, "sub",
79	    self->depth * 2, "", copyinstr(arg1), color_off);
80	self->depth++;
81	self->last = timestamp;
82}
83
84perl$target:::sub-return
85{
86	this->delta = (timestamp - self->last) / 1000;
87	this->name = strjoin(strjoin(copyinstr(arg0), "::"), copyinstr(arg1));
88	self->depth -= self->depth > 0 ? 1 : 0;
89	printf("%s%d %6d %10d  %16s:%-4d %-8s %*s<- %s%s\n", color_perl,
90	    cpu, pid, this->delta, basename(copyinstr(arg0)), arg2, "sub",
91	    self->depth * 2, "", copyinstr(arg1), color_off);
92	self->last = timestamp;
93}
94
95syscall:::entry
96/pid == $target/
97{
98	this->delta = (timestamp - self->last) / 1000;
99	printf("%s%d %6d %10d  %16s:-    %-8s %*s-> %s%s\n", color_syscall,
100	    cpu, pid, this->delta, "\"", "syscall", self->depth * 2, "",
101	    probefunc, color_off);
102	self->last = timestamp;
103}
104
105syscall:::return
106/pid == $target/
107{
108	this->delta = (timestamp - self->last) / 1000;
109	printf("%s%d %6d %10d  %16s:-    %-8s %*s<- %s%s\n", color_syscall,
110	    cpu, pid, this->delta, "\"", "syscall", self->depth * 2, "",
111	    probefunc, color_off);
112	self->last = timestamp;
113}
114
115proc:::exit
116/pid == $target/
117{
118	exit(0);
119}
120