1235368Sgnn#!/usr/sbin/dtrace -Zs 2235368Sgnn/* 3235368Sgnn * pl_syscolors.d - trace Perl subroutine flow plus syscalls, in color. 4235368Sgnn * Written for the Perl DTrace provider. 5235368Sgnn * 6235368Sgnn * $Id: pl_syscolors.d 27 2007-09-13 09:26:01Z brendan $ 7235368Sgnn * 8235368Sgnn * USAGE: pl_syscolors.d { -p PID | -c cmd } # hit Ctrl-C to end 9235368Sgnn * 10235368Sgnn * This watches Perl subroutine entries and returns, and indents child 11235368Sgnn * subroutine calls. 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 Perl program 18235368Sgnn * LINE Line number of filename 19235368Sgnn * TYPE Type of call (sub/syscall) 20235368Sgnn * NAME Perl subroutine or syscall name 21235368Sgnn * 22235368Sgnn * Filename and subroutine names are printed if available. 23235368Sgnn * 24235368Sgnn * WARNING: Watch the first column carefully, it prints the CPU-id. If it 25235368Sgnn * changes, then it is very likely that the output has been shuffled. 26235368Sgnn * 27235368Sgnn * COPYRIGHT: Copyright (c) 2007 Brendan Gregg. 28235368Sgnn * 29235368Sgnn * CDDL HEADER START 30235368Sgnn * 31235368Sgnn * The contents of this file are subject to the terms of the 32235368Sgnn * Common Development and Distribution License, Version 1.0 only 33235368Sgnn * (the "License"). You may not use this file except in compliance 34235368Sgnn * with the License. 35235368Sgnn * 36235368Sgnn * You can obtain a copy of the license at Docs/cddl1.txt 37235368Sgnn * or http://www.opensolaris.org/os/licensing. 38235368Sgnn * See the License for the specific language governing permissions 39235368Sgnn * and limitations under the License. 40235368Sgnn * 41235368Sgnn * CDDL HEADER END 42235368Sgnn * 43235368Sgnn * 09-Sep-2007 Brendan Gregg Created this. 44235368Sgnn */ 45235368Sgnn 46235368Sgnn#pragma D option quiet 47235368Sgnn#pragma D option switchrate=10 48235368Sgnn 49235368Sgnnself int depth; 50235368Sgnn 51235368Sgnndtrace:::BEGIN 52235368Sgnn{ 53235368Sgnn /* 54235368Sgnn * The following are terminal color escape sequences. 55235368Sgnn * Change them to whatever you prefer, eg HTML font tags. 56235368Sgnn */ 57235368Sgnn color_perl = "\033[2;35m"; /* violet, faint */ 58235368Sgnn color_syscall = "\033[2;32m"; /* green, faint */ 59235368Sgnn color_off = "\033[0m"; /* default */ 60235368Sgnn 61235368Sgnn printf("%s %6s %10s %16s:%-4s %-8s -- %s\n", "C", "PID", "DELTA(us)", 62235368Sgnn "FILE", "LINE", "TYPE", "NAME"); 63235368Sgnn} 64235368Sgnn 65235368Sgnnperl$target:::sub-entry, 66235368Sgnnperl$target:::sub-return, 67235368Sgnnsyscall:::entry, 68235368Sgnnsyscall:::return 69235368Sgnn/self->last == 0 && pid == $target/ 70235368Sgnn{ 71235368Sgnn self->last = timestamp; 72235368Sgnn} 73235368Sgnn 74235368Sgnnperl$target:::sub-entry 75235368Sgnn{ 76235368Sgnn this->delta = (timestamp - self->last) / 1000; 77235368Sgnn printf("%s%d %6d %10d %16s:%-4d %-8s %*s-> %s%s\n", color_perl, 78235368Sgnn cpu, pid, this->delta, basename(copyinstr(arg0)), arg2, "sub", 79235368Sgnn self->depth * 2, "", copyinstr(arg1), color_off); 80235368Sgnn self->depth++; 81235368Sgnn self->last = timestamp; 82235368Sgnn} 83235368Sgnn 84235368Sgnnperl$target:::sub-return 85235368Sgnn{ 86235368Sgnn this->delta = (timestamp - self->last) / 1000; 87235368Sgnn this->name = strjoin(strjoin(copyinstr(arg0), "::"), copyinstr(arg1)); 88235368Sgnn self->depth -= self->depth > 0 ? 1 : 0; 89235368Sgnn printf("%s%d %6d %10d %16s:%-4d %-8s %*s<- %s%s\n", color_perl, 90235368Sgnn cpu, pid, this->delta, basename(copyinstr(arg0)), arg2, "sub", 91235368Sgnn self->depth * 2, "", copyinstr(arg1), color_off); 92235368Sgnn self->last = timestamp; 93235368Sgnn} 94235368Sgnn 95235368Sgnnsyscall:::entry 96235368Sgnn/pid == $target/ 97235368Sgnn{ 98235368Sgnn this->delta = (timestamp - self->last) / 1000; 99235368Sgnn printf("%s%d %6d %10d %16s:- %-8s %*s-> %s%s\n", color_syscall, 100235368Sgnn cpu, pid, this->delta, "\"", "syscall", self->depth * 2, "", 101235368Sgnn probefunc, color_off); 102235368Sgnn self->last = timestamp; 103235368Sgnn} 104235368Sgnn 105235368Sgnnsyscall:::return 106235368Sgnn/pid == $target/ 107235368Sgnn{ 108235368Sgnn this->delta = (timestamp - self->last) / 1000; 109235368Sgnn printf("%s%d %6d %10d %16s:- %-8s %*s<- %s%s\n", color_syscall, 110235368Sgnn cpu, pid, this->delta, "\"", "syscall", self->depth * 2, "", 111235368Sgnn probefunc, color_off); 112235368Sgnn self->last = timestamp; 113235368Sgnn} 114235368Sgnn 115235368Sgnnproc:::exit 116235368Sgnn/pid == $target/ 117235368Sgnn{ 118235368Sgnn exit(0); 119235368Sgnn} 120