Home | History | Annotate | Line # | Download | only in Shell
      1 #!/usr/sbin/dtrace -Zs
      2 /*
      3  * sh_syscolors.d - trace Bourne shell flow plus syscalls, in color.
      4  *                  Written for the sh DTrace provider.
      5  *
      6  * $Id: sh_syscolors.d,v 1.1.1.1 2015/09/30 22:01:09 christos Exp $
      7  *
      8  * USAGE: sh_syscolors.d { -p PID | -c cmd }	# hit Ctrl-C to end
      9  *
     10  * This watches shell function entries and returns, and indents child
     11  * function calls. Shell builtins, commands and lines are also printed.
     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 shell script
     18  *		LINE		Line number of filename
     19  *		TYPE		Type of call (func/builtin/cmd/line/shell)
     20  *		NAME		Shell function, builtin or command name
     21  *
     22  * The filename for syscalls may be printed as the shell name, if the
     23  * script was invoked using the form "shell filename" rather than running
     24  * the script with an interpreter line.
     25  *
     26  * WARNING: Watch the first column carefully, it prints the CPU-id. If it
     27  * changes, then it is very likely that the output has been shuffled.
     28  *
     29  * COPYRIGHT: Copyright (c) 2007 Brendan Gregg.
     30  *
     31  * CDDL HEADER START
     32  *
     33  *  The contents of this file are subject to the terms of the
     34  *  Common Development and Distribution License, Version 1.0 only
     35  *  (the "License").  You may not use this file except in compliance
     36  *  with the License.
     37  *
     38  *  You can obtain a copy of the license at Docs/cddl1.txt
     39  *  or http://www.opensolaris.org/os/licensing.
     40  *  See the License for the specific language governing permissions
     41  *  and limitations under the License.
     42  *
     43  * CDDL HEADER END
     44  *
     45  * 09-Sep-2007	Brendan Gregg	Created this.
     46  */
     47 
     48 #pragma D option quiet
     49 #pragma D option switchrate=10
     50 
     51 self int depth;
     52 
     53 dtrace:::BEGIN
     54 {
     55         color_shell = "\033[2;35m";		/* violet, faint */
     56         color_line = "\033[1;35m";		/* violet, bold */
     57         color_syscall = "\033[2;32m";		/* green, faint */
     58         color_off = "\033[0m";			/* default */
     59 
     60 	printf("%s %6s %10s  %16s:%-4s %-8s -- %s\n", "C", "PID", "DELTA(us)",
     61 	    "FILE", "LINE", "TYPE", "NAME");
     62 }
     63 
     64 sh$target:::function-entry,
     65 sh$target:::function-return,
     66 sh$target:::builtin-entry,
     67 sh$target:::command-entry,
     68 sh$target:::line,
     69 syscall:::entry,
     70 syscall:::return
     71 /self->last == 0 && pid == $target/
     72 {
     73 	self->last = timestamp;
     74 }
     75 
     76 sh$target:::function-entry
     77 {
     78 	this->delta = (timestamp - self->last) / 1000;
     79 	printf("%s%d %6d %10d  %16s:%-4d %-8s %*s-> %s%s\n", color_shell,
     80 	    cpu, pid, this->delta, basename(copyinstr(arg0)), arg2, "func",
     81 	    self->depth * 2, "", copyinstr(arg1), color_off);
     82 	self->depth++;
     83 	self->last = timestamp;
     84 }
     85 
     86 sh$target:::function-return
     87 {
     88 	this->delta = (timestamp - self->last) / 1000;
     89 	self->depth -= self->depth > 0 ? 1 : 0;
     90 	printf("%s%d %6d %10d  %16s:-    %-8s %*s<- %s%s\n", color_shell,
     91 	    cpu, pid, this->delta, basename(copyinstr(arg0)), "func",
     92 	    self->depth * 2, "", copyinstr(arg1), color_off);
     93 	self->last = timestamp;
     94 }
     95 
     96 sh$target:::builtin-entry
     97 {
     98 	this->delta = (timestamp - self->last) / 1000;
     99 	printf("%s%d %6d %10d  %16s:%-4d %-8s %*s-> %s%s\n", color_shell,
    100 	    cpu, pid, this->delta, basename(copyinstr(arg0)), arg2, "builtin",
    101 	    self->depth * 2, "", copyinstr(arg1), color_off);
    102 	self->depth++;
    103 	self->last = timestamp;
    104 }
    105 
    106 sh$target:::builtin-return
    107 {
    108 	this->delta = (timestamp - self->last) / 1000;
    109 	self->depth -= self->depth > 0 ? 1 : 0;
    110 	printf("%s%d %6d %10d  %16s:%-4d %-8s %*s<- %s%s\n", color_shell,
    111 	    cpu, pid, this->delta, basename(copyinstr(arg0)), arg2, "builtin",
    112 	    self->depth * 2, "", copyinstr(arg1), color_off);
    113 	self->last = timestamp;
    114 }
    115 
    116 sh$target:::command-entry
    117 {
    118 	this->delta = (timestamp - self->last) / 1000;
    119 	printf("%s%d %6d %10d  %16s:%-4d %-8s %*s-> %s%s\n", color_shell,
    120 	    cpu, pid, this->delta, basename(copyinstr(arg0)), arg2, "cmd",
    121 	    self->depth * 2, "", copyinstr(arg1), color_off);
    122 	self->depth++;
    123 	self->last = timestamp;
    124 }
    125 
    126 sh$target:::command-return
    127 {
    128 	this->delta = (timestamp - self->last) / 1000;
    129 	self->depth -= self->depth > 0 ? 1 : 0;
    130 	printf("%s%d %6d %10d  %16s:%-4d %-8s %*s<- %s%s\n", color_shell,
    131 	    cpu, pid, this->delta, basename(copyinstr(arg0)), arg2, "cmd",
    132 	    self->depth * 2, "", copyinstr(arg1), color_off);
    133 	self->last = timestamp;
    134 }
    135 
    136 sh$target:::line
    137 {
    138 	this->delta = (timestamp - self->last) / 1000;
    139 	printf("%s%d %6d %10d  %16s:%-4d %-8s %*s-- %s\n", color_line,
    140 	    cpu, pid, this->delta, basename(copyinstr(arg0)), arg1, "line",
    141 	    self->depth * 2, "", color_off);
    142 	self->last = timestamp;
    143 }
    144 
    145 syscall:::entry
    146 /pid == $target/
    147 {
    148 	this->delta = (timestamp - self->last) / 1000;
    149 	printf("%s%d %6d %10d  %16s:-    %-8s %*s-> %s%s\n", color_syscall,
    150 	    cpu, pid, this->delta, basename(execname), "syscall",
    151 	    self->depth * 2, "", probefunc, color_off);
    152 	self->last = timestamp;
    153 }
    154 
    155 syscall:::return
    156 /pid == $target/
    157 {
    158 	this->delta = (timestamp - self->last) / 1000;
    159 	printf("%s%d %6d %10d  %16s:-    %-8s %*s<- %s%s\n", color_syscall,
    160 	    cpu, pid, this->delta, basename(execname), "syscall",
    161 	    self->depth * 2, "", probefunc, color_off);
    162 	self->last = timestamp;
    163 }
    164 
    165 proc:::exit
    166 /pid == $target/
    167 {
    168 	exit(0);
    169 }
    170