My dtrace script should be started with -p pid or -c command. When it starts, I want to print a banner showing the pid and the name of that process. BEGIN { Version = "x0.4"; start = timestamp; printf("ver %s - monitoring process %s[%d] - started %Y\n", Version, execname, $target, walltimestamp); printf("\nPress Ctrl/C to end sampling\n\n"); } But execname is always dtrace in the BEGIN{}. Any way to get the name of the process pointed to by $target ? -- This message posted from opensolaris.org
Adam Leventhal
2008-Mar-10 17:23 UTC
[dtrace-discuss] how to print process name in BEGIN ?
On Mon, Mar 10, 2008 at 09:25:44AM -0700, Kelly Caudill wrote:> My dtrace script should be started with -p pid or -c command. When it > starts, I want to print a banner showing the pid and the name of that process. > > BEGIN > { > Version = "x0.4"; > start = timestamp; > printf("ver %s - monitoring process %s[%d] - started %Y\n", Version, execname, $target, walltimestamp); > printf("\nPress Ctrl/C to end sampling\n\n"); > } > > But execname is always dtrace in the BEGIN{}. > > Any way to get the name of the process pointed to by $target ?There''s no good built-in support for that though it would probably be a good RFE to files. You can do the translation yourself, but it involves walking a hash chain so you will need to choose a fixed depth in your D script. ---8<--- BEGIN { this->pidp = `pidhash[$target & (`pid_hashsz - 1)]; this->pidname = "<error>"; } /* repeat this clause to accommodate longer hash chains. */ BEGIN /this->pidp->pid_id != $target && this->pidp->pid_link != 0/ { this->pidp = this->pidp->pid_link; } BEGIN /this->pidp->pid_id != $target && this->pidp->pid_link == 0/ { this->pidname = "<no such process>"; } BEGIN /this->pidp->pid_id != $target && this->pidp->pid_link != 0/ { this->pidname = "<hash chain too long>"; } BEGIN /this->pidp->pid_id == $target/ { /* workaround for 6465277 */ this->slot = (*(uint32_t *)this->pidp) >> 8; this->pidname = `procdir[this->slot].pe_proc->p_user.u_comm; } BEGIN { printf("%d %s", $target, this->pidname); } --->8--- Hardly beatiful, but it works... which has become something of a DTrace mantra for some of these stickier corner cases. Adam -- Adam Leventhal, Fishworks http://blogs.sun.com/ahl