1235368Sgnn#!/usr/sbin/dtrace -Zs
2235368Sgnn/*
3235368Sgnn * rb_flow.d - snoop Ruby execution showing method flow using DTrace.
4235368Sgnn *             Written for the Ruby DTrace provider.
5235368Sgnn *
6235368Sgnn * $Id: rb_flow.d 41 2007-09-17 02:20:10Z brendan $
7235368Sgnn *
8235368Sgnn * This traces activity from all Ruby programs on the system that are
9235368Sgnn * running with Ruby provider support.
10235368Sgnn *
11235368Sgnn * USAGE: rb_flow.d 		# hit Ctrl-C to end
12235368Sgnn *
13235368Sgnn * FIELDS:
14235368Sgnn *		C		CPU-id
15235368Sgnn *		TIME(us)	Time since boot, us
16235368Sgnn *		FILE		Filename that this method belongs to
17235368Sgnn *		CLASS::METHOD	Ruby classname and method
18235368Sgnn *
19235368Sgnn * LEGEND:
20235368Sgnn *		->		method entry
21235368Sgnn *		<-		method return
22235368Sgnn *
23235368Sgnn * Filename and method names are printed if available.
24235368Sgnn *
25235368Sgnn * WARNING: Watch the first column carefully, it prints the CPU-id. If it
26235368Sgnn * changes, then it is very likely that the output has been shuffled.
27235368Sgnn *
28235368Sgnn * COPYRIGHT: Copyright (c) 2007 Brendan Gregg.
29235368Sgnn *
30235368Sgnn * CDDL HEADER START
31235368Sgnn *
32235368Sgnn *  The contents of this file are subject to the terms of the
33235368Sgnn *  Common Development and Distribution License, Version 1.0 only
34235368Sgnn *  (the "License").  You may not use this file except in compliance
35235368Sgnn *  with the License.
36235368Sgnn *
37235368Sgnn *  You can obtain a copy of the license at Docs/cddl1.txt
38235368Sgnn *  or http://www.opensolaris.org/os/licensing.
39235368Sgnn *  See the License for the specific language governing permissions
40235368Sgnn *  and limitations under the License.
41235368Sgnn *
42235368Sgnn * CDDL HEADER END
43235368Sgnn *
44235368Sgnn * 09-Sep-2007	Brendan Gregg	Created this.
45235368Sgnn */
46235368Sgnn
47235368Sgnn#pragma D option quiet
48235368Sgnn#pragma D option switchrate=10
49235368Sgnn
50235368Sgnnself int depth;
51235368Sgnn
52235368Sgnndtrace:::BEGIN
53235368Sgnn{
54235368Sgnn	printf("%3s %-16s %-22s -- %s\n", "C", "TIME(us)", "FILE",
55235368Sgnn	    "CLASS::METHOD");
56235368Sgnn}
57235368Sgnn
58235368Sgnnruby*:::function-entry
59235368Sgnn{
60235368Sgnn	printf("%3d %-16d %-22s %*s-> %s::%s\n", cpu, timestamp / 1000,
61235368Sgnn	    basename(copyinstr(arg2)), self->depth * 2, "", copyinstr(arg0),
62235368Sgnn	    copyinstr(arg1));
63235368Sgnn	self->depth++;
64235368Sgnn}
65235368Sgnn
66235368Sgnnruby*:::function-return
67235368Sgnn{
68235368Sgnn	self->depth -= self->depth > 0 ? 1 : 0;
69235368Sgnn	printf("%3d %-16d %-22s %*s<- %s::%s\n", cpu, timestamp / 1000,
70235368Sgnn	    basename(copyinstr(arg2)), self->depth * 2, "", copyinstr(arg0),
71235368Sgnn	    copyinstr(arg1));
72235368Sgnn}
73