Home | History | Annotate | Line # | Download | only in Python
      1 #!/usr/sbin/dtrace -Zs
      2 /*
      3  * py_flowinfo.d - snoop Python function flow with info using DTrace.
      4  *                 Written for the Python DTrace provider.
      5  *
      6  * $Id: py_flowinfo.d,v 1.1.1.1 2015/09/30 22:01:09 christos Exp $
      7  *
      8  * This traces activity from all Python programs on the system that are
      9  * running with Python provider support.
     10  *
     11  * USAGE: py_flowinfo.d 	# hit Ctrl-C to end
     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)
     20  *		FUNC		Python function
     21  *
     22  * LEGEND:
     23  *		->		function entry
     24  *		<-		function return
     25  *
     26  * Filename and function names are printed if available.
     27  *
     28  * WARNING: Watch the first column carefully, it prints the CPU-id. If it
     29  * changes, then it is very likely that the output has been shuffled.
     30  *
     31  * COPYRIGHT: Copyright (c) 2007 Brendan Gregg.
     32  *
     33  * CDDL HEADER START
     34  *
     35  *  The contents of this file are subject to the terms of the
     36  *  Common Development and Distribution License, Version 1.0 only
     37  *  (the "License").  You may not use this file except in compliance
     38  *  with the License.
     39  *
     40  *  You can obtain a copy of the license at Docs/cddl1.txt
     41  *  or http://www.opensolaris.org/os/licensing.
     42  *  See the License for the specific language governing permissions
     43  *  and limitations under the License.
     44  *
     45  * CDDL HEADER END
     46  *
     47  * 09-Sep-2007	Brendan Gregg	Created this.
     48  */
     49 
     50 #pragma D option quiet
     51 #pragma D option switchrate=10
     52 
     53 self int depth;
     54 
     55 dtrace:::BEGIN
     56 {
     57 	printf("%s %6s %10s  %16s:%-4s %-8s -- %s\n", "C", "PID", "DELTA(us)",
     58 	    "FILE", "LINE", "TYPE", "FUNC");
     59 }
     60 
     61 python*:::function-entry,
     62 python*:::function-return
     63 /self->last == 0/
     64 {
     65 	self->last = timestamp;
     66 }
     67 
     68 python*:::function-entry
     69 {
     70 	this->delta = (timestamp - self->last) / 1000;
     71 	printf("%d %6d %10d  %16s:%-4d %-8s %*s-> %s\n", cpu, pid, this->delta,
     72 	    basename(copyinstr(arg0)), arg2, "func", self->depth * 2, "",
     73 	    copyinstr(arg1));
     74 	self->depth++;
     75 	self->last = timestamp;
     76 }
     77 
     78 python*:::function-return
     79 {
     80 	this->delta = (timestamp - self->last) / 1000;
     81 	self->depth -= self->depth > 0 ? 1 : 0;
     82 	printf("%d %6d %10d  %16s:%-4d %-8s %*s<- %s\n", cpu, pid, this->delta,
     83 	    basename(copyinstr(arg0)), arg2, "func", self->depth * 2, "",
     84 	    copyinstr(arg1));
     85 	self->last = timestamp;
     86 }
     87