1235368Sgnn#!/usr/sbin/dtrace -Zs
2235368Sgnn/*
3235368Sgnn * py_syscolors.d - trace Python function flow plus syscalls, in color.
4235368Sgnn *                  Written for the Python DTrace provider.
5235368Sgnn *
6235368Sgnn * $Id: py_syscolors.d 27 2007-09-13 09:26:01Z brendan $
7235368Sgnn *
8235368Sgnn * USAGE: py_syscolors.d { -p PID | -c cmd }	# hit Ctrl-C to end
9235368Sgnn *
10235368Sgnn * This watches Python function entries and returns, and indents child
11235368Sgnn * function calls.
12235368Sgnn *
13235368Sgnn * FIELDS:
14235368Sgnn *		C		CPU-id
15235368Sgnn *		PID		Process ID
16235368Sgnn *		DELTA(us)	Elapsed time from previous line to this line
17235368Sgnn *		FILE		Filename of the Python program
18235368Sgnn *		LINE		Line number of filename
19235368Sgnn *		TYPE		Type of call (func/syscall)
20235368Sgnn *		NAME		Python function or syscall name
21235368Sgnn *
22235368Sgnn * Filename and function names are printed if available.
23235368Sgnn *
24235368Sgnn * WARNING: Watch the first column carefully, it prints the CPU-id. If it
25235368Sgnn * changes, then it is very likely that the output has been shuffled.
26235368Sgnn *
27235368Sgnn * COPYRIGHT: Copyright (c) 2007 Brendan Gregg.
28235368Sgnn *
29235368Sgnn * CDDL HEADER START
30235368Sgnn *
31235368Sgnn *  The contents of this file are subject to the terms of the
32235368Sgnn *  Common Development and Distribution License, Version 1.0 only
33235368Sgnn *  (the "License").  You may not use this file except in compliance
34235368Sgnn *  with the License.
35235368Sgnn *
36235368Sgnn *  You can obtain a copy of the license at Docs/cddl1.txt
37235368Sgnn *  or http://www.opensolaris.org/os/licensing.
38235368Sgnn *  See the License for the specific language governing permissions
39235368Sgnn *  and limitations under the License.
40235368Sgnn *
41235368Sgnn * CDDL HEADER END
42235368Sgnn *
43235368Sgnn * 09-Sep-2007	Brendan Gregg	Created this.
44235368Sgnn */
45235368Sgnn
46235368Sgnn#pragma D option quiet
47235368Sgnn#pragma D option switchrate=10
48235368Sgnn
49235368Sgnnself int depth;
50235368Sgnn
51235368Sgnndtrace:::BEGIN
52235368Sgnn{
53235368Sgnn        color_python = "\033[2;35m";		/* violet, faint */
54235368Sgnn        color_syscall = "\033[2;32m";		/* green, faint */
55235368Sgnn        color_off = "\033[0m";			/* default */
56235368Sgnn
57235368Sgnn	self->depth = 0;
58235368Sgnn	printf("%s %6s %10s  %16s:%-4s %-8s -- %s\n", "C", "PID", "DELTA(us)",
59235368Sgnn	    "FILE", "LINE", "TYPE", "NAME");
60235368Sgnn}
61235368Sgnn
62235368Sgnnpython$target:::function-entry,
63235368Sgnnpython$target:::function-return,
64235368Sgnnsyscall:::entry,
65235368Sgnnsyscall:::return
66235368Sgnn/self->last == 0 && pid == $target/
67235368Sgnn{
68235368Sgnn	self->last = timestamp;
69235368Sgnn}
70235368Sgnn
71235368Sgnnpython$target:::function-entry
72235368Sgnn{
73235368Sgnn	this->delta = (timestamp - self->last) / 1000;
74235368Sgnn	printf("%s%d %6d %10d  %16s:%-4d %-8s %*s-> %s%s\n", color_python,
75235368Sgnn	    cpu, pid, this->delta, basename(copyinstr(arg0)), arg2, "func",
76235368Sgnn	    self->depth * 2, "", copyinstr(arg1), color_off);
77235368Sgnn	self->depth++;
78235368Sgnn	self->last = timestamp;
79235368Sgnn}
80235368Sgnn
81235368Sgnnpython$target:::function-return
82235368Sgnn{
83235368Sgnn	this->delta = (timestamp - self->last) / 1000;
84235368Sgnn	this->name = strjoin(strjoin(copyinstr(arg0), "::"), copyinstr(arg1));
85235368Sgnn	self->depth -= self->depth > 0 ? 1 : 0;
86235368Sgnn	printf("%s%d %6d %10d  %16s:%-4d %-8s %*s<- %s%s\n", color_python,
87235368Sgnn	    cpu, pid, this->delta, basename(copyinstr(arg0)), arg2, "func",
88235368Sgnn	    self->depth * 2, "", copyinstr(arg1), color_off);
89235368Sgnn	self->last = timestamp;
90235368Sgnn}
91235368Sgnn
92235368Sgnnsyscall:::entry
93235368Sgnn/pid == $target/
94235368Sgnn{
95235368Sgnn	this->delta = (timestamp - self->last) / 1000;
96235368Sgnn	printf("%s%d %6d %10d  %16s:-    %-8s %*s-> %s%s\n", color_syscall,
97235368Sgnn	    cpu, pid, this->delta, "\"", "syscall", self->depth * 2, "",
98235368Sgnn	    probefunc, color_off);
99235368Sgnn	self->last = timestamp;
100235368Sgnn}
101235368Sgnn
102235368Sgnnsyscall:::return
103235368Sgnn/pid == $target/
104235368Sgnn{
105235368Sgnn	this->delta = (timestamp - self->last) / 1000;
106235368Sgnn	printf("%s%d %6d %10d  %16s:-    %-8s %*s<- %s%s\n", color_syscall,
107235368Sgnn	    cpu, pid, this->delta, "\"", "syscall", self->depth * 2, "",
108235368Sgnn	    probefunc, color_off);
109235368Sgnn	self->last = timestamp;
110235368Sgnn}
111235368Sgnn
112235368Sgnnproc:::exit
113235368Sgnn/pid == $target/
114235368Sgnn{
115235368Sgnn	exit(0);
116235368Sgnn}
117