xref: /netbsd-src/external/cddl/dtracetoolkit/dist/Python/py_flow.d (revision c29d51755812ace2e87aeefdb06cb2b4dac7087a)
1*c29d5175Schristos #!/usr/sbin/dtrace -Zs
2*c29d5175Schristos /*
3*c29d5175Schristos  * py_flow.d - snoop Python execution showing function flow.
4*c29d5175Schristos  *             Written for the Python DTrace provider.
5*c29d5175Schristos  *
6*c29d5175Schristos  * $Id: py_flow.d,v 1.1.1.1 2015/09/30 22:01:09 christos Exp $
7*c29d5175Schristos  *
8*c29d5175Schristos  * This traces Python activity from all Python programs on the system
9*c29d5175Schristos  * running with Python provider support.
10*c29d5175Schristos  *
11*c29d5175Schristos  * USAGE: py_flow.d			# hit Ctrl-C to end
12*c29d5175Schristos  *
13*c29d5175Schristos  * This watches Python function entries and returns, and indents child
14*c29d5175Schristos  * function calls.
15*c29d5175Schristos  *
16*c29d5175Schristos  * FIELDS:
17*c29d5175Schristos  *		C		CPU-id
18*c29d5175Schristos  *		TIME(us)	Time since boot, us
19*c29d5175Schristos  *		FILE		Filename that this function belongs to
20*c29d5175Schristos  *		FUNC		Function name
21*c29d5175Schristos  *
22*c29d5175Schristos  * LEGEND:
23*c29d5175Schristos  *		->		function entry
24*c29d5175Schristos  *		<-		function return
25*c29d5175Schristos  *
26*c29d5175Schristos  * WARNING: Watch the first column carefully, it prints the CPU-id. If it
27*c29d5175Schristos  * changes, then it is very likely that the output has been shuffled.
28*c29d5175Schristos  *
29*c29d5175Schristos  * COPYRIGHT: Copyright (c) 2007 Brendan Gregg.
30*c29d5175Schristos  *
31*c29d5175Schristos  * CDDL HEADER START
32*c29d5175Schristos  *
33*c29d5175Schristos  *  The contents of this file are subject to the terms of the
34*c29d5175Schristos  *  Common Development and Distribution License, Version 1.0 only
35*c29d5175Schristos  *  (the "License").  You may not use this file except in compliance
36*c29d5175Schristos  *  with the License.
37*c29d5175Schristos  *
38*c29d5175Schristos  *  You can obtain a copy of the license at Docs/cddl1.txt
39*c29d5175Schristos  *  or http://www.opensolaris.org/os/licensing.
40*c29d5175Schristos  *  See the License for the specific language governing permissions
41*c29d5175Schristos  *  and limitations under the License.
42*c29d5175Schristos  *
43*c29d5175Schristos  * CDDL HEADER END
44*c29d5175Schristos  *
45*c29d5175Schristos  * 09-Sep-2007	Brendan Gregg	Created this.
46*c29d5175Schristos  */
47*c29d5175Schristos 
48*c29d5175Schristos #pragma D option quiet
49*c29d5175Schristos #pragma D option switchrate=10
50*c29d5175Schristos 
51*c29d5175Schristos self int depth;
52*c29d5175Schristos 
53*c29d5175Schristos dtrace:::BEGIN
54*c29d5175Schristos {
55*c29d5175Schristos 	printf("%3s %-16s %-16s -- %s\n", "C", "TIME(us)", "FILE", "FUNC");
56*c29d5175Schristos }
57*c29d5175Schristos 
58*c29d5175Schristos python*:::function-entry
59*c29d5175Schristos {
60*c29d5175Schristos 	printf("%3d %-16d %-16s %*s-> %s\n", cpu, timestamp / 1000,
61*c29d5175Schristos 	    basename(copyinstr(arg0)), self->depth * 2, "", copyinstr(arg1));
62*c29d5175Schristos 	self->depth++;
63*c29d5175Schristos }
64*c29d5175Schristos 
65*c29d5175Schristos python*:::function-return
66*c29d5175Schristos {
67*c29d5175Schristos 	self->depth -= self->depth > 0 ? 1 : 0;
68*c29d5175Schristos 	printf("%3d %-16d %-16s %*s<- %s\n", cpu, timestamp / 1000,
69*c29d5175Schristos 	    basename(copyinstr(arg0)), self->depth * 2, "", copyinstr(arg1));
70*c29d5175Schristos }
71