xref: /netbsd-src/external/cddl/dtracetoolkit/dist/Bin/py_syscolors.d (revision c29d51755812ace2e87aeefdb06cb2b4dac7087a)
1 #!/usr/sbin/dtrace -Zs
2 /*
3  * py_syscolors.d - trace Python function flow plus syscalls, in color.
4  *                  Written for the Python DTrace provider.
5  *
6  * $Id: py_syscolors.d,v 1.1.1.1 2015/09/30 22:01:07 christos Exp $
7  *
8  * USAGE: py_syscolors.d { -p PID | -c cmd }	# hit Ctrl-C to end
9  *
10  * This watches Python function entries and returns, and indents child
11  * function 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 Python program
18  *		LINE		Line number of filename
19  *		TYPE		Type of call (func/syscall)
20  *		NAME		Python function or syscall name
21  *
22  * Filename and function 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 
49 self int depth;
50 
51 dtrace:::BEGIN
52 {
53         color_python = "\033[2;35m";		/* violet, faint */
54         color_syscall = "\033[2;32m";		/* green, faint */
55         color_off = "\033[0m";			/* default */
56 
57 	self->depth = 0;
58 	printf("%s %6s %10s  %16s:%-4s %-8s -- %s\n", "C", "PID", "DELTA(us)",
59 	    "FILE", "LINE", "TYPE", "NAME");
60 }
61 
62 python$target:::function-entry,
63 python$target:::function-return,
64 syscall:::entry,
65 syscall:::return
66 /self->last == 0 && pid == $target/
67 {
68 	self->last = timestamp;
69 }
70 
71 python$target:::function-entry
72 {
73 	this->delta = (timestamp - self->last) / 1000;
74 	printf("%s%d %6d %10d  %16s:%-4d %-8s %*s-> %s%s\n", color_python,
75 	    cpu, pid, this->delta, basename(copyinstr(arg0)), arg2, "func",
76 	    self->depth * 2, "", copyinstr(arg1), color_off);
77 	self->depth++;
78 	self->last = timestamp;
79 }
80 
81 python$target:::function-return
82 {
83 	this->delta = (timestamp - self->last) / 1000;
84 	this->name = strjoin(strjoin(copyinstr(arg0), "::"), copyinstr(arg1));
85 	self->depth -= self->depth > 0 ? 1 : 0;
86 	printf("%s%d %6d %10d  %16s:%-4d %-8s %*s<- %s%s\n", color_python,
87 	    cpu, pid, this->delta, basename(copyinstr(arg0)), arg2, "func",
88 	    self->depth * 2, "", copyinstr(arg1), color_off);
89 	self->last = timestamp;
90 }
91 
92 syscall:::entry
93 /pid == $target/
94 {
95 	this->delta = (timestamp - self->last) / 1000;
96 	printf("%s%d %6d %10d  %16s:-    %-8s %*s-> %s%s\n", color_syscall,
97 	    cpu, pid, this->delta, "\"", "syscall", self->depth * 2, "",
98 	    probefunc, color_off);
99 	self->last = timestamp;
100 }
101 
102 syscall:::return
103 /pid == $target/
104 {
105 	this->delta = (timestamp - self->last) / 1000;
106 	printf("%s%d %6d %10d  %16s:-    %-8s %*s<- %s%s\n", color_syscall,
107 	    cpu, pid, this->delta, "\"", "syscall", self->depth * 2, "",
108 	    probefunc, color_off);
109 	self->last = timestamp;
110 }
111 
112 proc:::exit
113 /pid == $target/
114 {
115 	exit(0);
116 }
117