1 #!/usr/sbin/dtrace -Zs 2 /* 3 * rb_syscolors.d - trace Ruby method flow plus syscalls, in color. 4 * Written for the Ruby DTrace provider. 5 * 6 * $Id: rb_syscolors.d,v 1.1.1.1 2015/09/30 22:01:09 christos Exp $ 7 * 8 * USAGE: rb_syscolors.d { -p PID | -c cmd } # hit Ctrl-C to end 9 * 10 * This watches Ruby method entries and returns, and indents child 11 * function calls. 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 Ruby program 18 * LINE Line number of filename 19 * TYPE Type of call (method/line/syscall) 20 * NAME Ruby method or syscall name 21 * 22 * Filename and method names are printed if available. 23 * 24 * WARNING: Watch the first column carefully, it prints the CPU-id. If it 25 * changes, then it is very likely that the output has been shuffled. 26 * 27 * COPYRIGHT: Copyright (c) 2007 Brendan Gregg. 28 * 29 * CDDL HEADER START 30 * 31 * The contents of this file are subject to the terms of the 32 * Common Development and Distribution License, Version 1.0 only 33 * (the "License"). You may not use this file except in compliance 34 * with the License. 35 * 36 * You can obtain a copy of the license at Docs/cddl1.txt 37 * or http://www.opensolaris.org/os/licensing. 38 * See the License for the specific language governing permissions 39 * and limitations under the License. 40 * 41 * CDDL HEADER END 42 * 43 * 09-Sep-2007 Brendan Gregg Created this. 44 */ 45 46 #pragma D option quiet 47 #pragma D option switchrate=10 48 49 self int depth; 50 51 dtrace:::BEGIN 52 { 53 /* 54 * The following are terminal color escape sequences. 55 * Change them to whatever you prefer, eg HTML font tags. 56 */ 57 color_ruby = "\033[2;35m"; /* violet, faint */ 58 color_line = "\033[1;35m"; /* violet, bold */ 59 color_syscall = "\033[2;32m"; /* green, faint */ 60 color_off = "\033[0m"; /* default */ 61 62 printf("%s %6s %10s %16s:%-4s %-8s -- %s\n", "C", "PID", "DELTA(us)", 63 "FILE", "LINE", "TYPE", "NAME"); 64 } 65 66 ruby$target:::function-entry, 67 ruby$target:::function-return, 68 ruby$target:::line, 69 syscall:::entry, 70 syscall:::return 71 /self->last == 0 && pid == $target/ 72 { 73 self->last = timestamp; 74 } 75 76 ruby$target:::function-entry 77 { 78 this->delta = (timestamp - self->last) / 1000; 79 this->name = strjoin(strjoin(copyinstr(arg0), "::"), copyinstr(arg1)); 80 printf("%s%d %6d %10d %16s:%-4d %-8s %*s-> %s%s\n", color_ruby, 81 cpu, pid, this->delta, basename(copyinstr(arg2)), arg3, "method", 82 self->depth * 2, "", this->name, color_off); 83 self->depth++; 84 self->last = timestamp; 85 } 86 87 ruby$target:::function-return 88 { 89 this->delta = (timestamp - self->last) / 1000; 90 this->name = strjoin(strjoin(copyinstr(arg0), "::"), copyinstr(arg1)); 91 self->depth--; 92 printf("%s%d %6d %10d %16s:%-4d %-8s %*s<- %s%s\n", color_ruby, 93 cpu, pid, this->delta, basename(copyinstr(arg2)), arg3, "method", 94 self->depth * 2, "", this->name, color_off); 95 self->last = timestamp; 96 } 97 98 ruby$target:::line 99 { 100 this->delta = (timestamp - self->last) / 1000; 101 printf("%s%d %6d %10d %16s:%-4d %-8s %*s-- %s\n", color_line, 102 cpu, pid, this->delta, basename(copyinstr(arg0)), arg1, "line", 103 self->depth * 2, "", color_off); 104 self->last = timestamp; 105 } 106 107 syscall:::entry 108 /pid == $target/ 109 { 110 this->delta = (timestamp - self->last) / 1000; 111 printf("%s%d %6d %10d %16s:- %-8s %*s-> %s%s\n", color_syscall, 112 cpu, pid, this->delta, "\"", "syscall", self->depth * 2, "", 113 probefunc, color_off); 114 self->depth++; 115 self->last = timestamp; 116 } 117 118 syscall:::return 119 /pid == $target/ 120 { 121 this->delta = (timestamp - self->last) / 1000; 122 self->depth--; 123 printf("%s%d %6d %10d %16s:- %-8s %*s<- %s%s\n", color_syscall, 124 cpu, pid, this->delta, "\"", "syscall", self->depth * 2, "", 125 probefunc, color_off); 126 self->last = timestamp; 127 } 128 129 proc:::exit 130 /pid == $target/ 131 { 132 exit(0); 133 } 134