1235368Sgnn#!/usr/sbin/dtrace -Zs
2235368Sgnn/*
3235368Sgnn * py_flowinfo.d - snoop Python function flow with info using DTrace.
4235368Sgnn *                 Written for the Python DTrace provider.
5235368Sgnn *
6235368Sgnn * $Id: py_flowinfo.d 41 2007-09-17 02:20:10Z brendan $
7235368Sgnn *
8235368Sgnn * This traces activity from all Python programs on the system that are
9235368Sgnn * running with Python provider support.
10235368Sgnn *
11235368Sgnn * USAGE: py_flowinfo.d 	# hit Ctrl-C to end
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)
20235368Sgnn *		FUNC		Python function
21235368Sgnn *
22235368Sgnn * LEGEND:
23235368Sgnn *		->		function entry
24235368Sgnn *		<-		function return
25235368Sgnn *
26235368Sgnn * Filename and function names are printed if available.
27235368Sgnn *
28235368Sgnn * WARNING: Watch the first column carefully, it prints the CPU-id. If it
29235368Sgnn * changes, then it is very likely that the output has been shuffled.
30235368Sgnn *
31235368Sgnn * COPYRIGHT: Copyright (c) 2007 Brendan Gregg.
32235368Sgnn *
33235368Sgnn * CDDL HEADER START
34235368Sgnn *
35235368Sgnn *  The contents of this file are subject to the terms of the
36235368Sgnn *  Common Development and Distribution License, Version 1.0 only
37235368Sgnn *  (the "License").  You may not use this file except in compliance
38235368Sgnn *  with the License.
39235368Sgnn *
40235368Sgnn *  You can obtain a copy of the license at Docs/cddl1.txt
41235368Sgnn *  or http://www.opensolaris.org/os/licensing.
42235368Sgnn *  See the License for the specific language governing permissions
43235368Sgnn *  and limitations under the License.
44235368Sgnn *
45235368Sgnn * CDDL HEADER END
46235368Sgnn *
47235368Sgnn * 09-Sep-2007	Brendan Gregg	Created this.
48235368Sgnn */
49235368Sgnn
50235368Sgnn#pragma D option quiet
51235368Sgnn#pragma D option switchrate=10
52235368Sgnn
53235368Sgnnself int depth;
54235368Sgnn
55235368Sgnndtrace:::BEGIN
56235368Sgnn{
57235368Sgnn	printf("%s %6s %10s  %16s:%-4s %-8s -- %s\n", "C", "PID", "DELTA(us)",
58235368Sgnn	    "FILE", "LINE", "TYPE", "FUNC");
59235368Sgnn}
60235368Sgnn
61235368Sgnnpython*:::function-entry,
62235368Sgnnpython*:::function-return
63235368Sgnn/self->last == 0/
64235368Sgnn{
65235368Sgnn	self->last = timestamp;
66235368Sgnn}
67235368Sgnn
68235368Sgnnpython*:::function-entry
69235368Sgnn{
70235368Sgnn	this->delta = (timestamp - self->last) / 1000;
71235368Sgnn	printf("%d %6d %10d  %16s:%-4d %-8s %*s-> %s\n", cpu, pid, this->delta,
72235368Sgnn	    basename(copyinstr(arg0)), arg2, "func", self->depth * 2, "",
73235368Sgnn	    copyinstr(arg1));
74235368Sgnn	self->depth++;
75235368Sgnn	self->last = timestamp;
76235368Sgnn}
77235368Sgnn
78235368Sgnnpython*:::function-return
79235368Sgnn{
80235368Sgnn	this->delta = (timestamp - self->last) / 1000;
81235368Sgnn	self->depth -= self->depth > 0 ? 1 : 0;
82235368Sgnn	printf("%d %6d %10d  %16s:%-4d %-8s %*s<- %s\n", cpu, pid, this->delta,
83235368Sgnn	    basename(copyinstr(arg0)), arg2, "func", self->depth * 2, "",
84235368Sgnn	    copyinstr(arg1));
85235368Sgnn	self->last = timestamp;
86235368Sgnn}
87