OpenSolaris

Discussions Communities Projects Download Source Browser

Home » OpenSolaris Forums » dtrace » discuss

Thread: how to print process name in BEGIN ?

Welcome, Guest Help
Login Login
Guest Settings Guest Settings
Reply to this Thread Reply to this Thread Search Forum Search Forum Back to Thread List Back to Thread List

Permlink Replies: 1 - Last Post: Mar 10, 2008 10:23 AM by: Adam Leventhal
kellyc

Posts: 1
From:

Registered: 3/10/08
how to print process name in BEGIN ?
Posted: Mar 10, 2008 9:25 AM
To: Communities » dtrace » discuss
  Click to reply to this thread Reply

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 ?

Adam Leventhal
ahl@eng.sun.com
Re: how to print process name in BEGIN ?
Posted: Mar 10, 2008 10:23 AM   in response to: kellyc

  Click to reply to this thread Reply

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
_______________________________________________
dtrace-discuss mailing list
dtrace-discuss at opensolaris dot org





Terms of Use | Privacy | Trademarks | Copyright Policy | Site Guidelines
Your use of this web site or any of its content or software indicates your agreement to be bound by these Terms of Use.
Copyright © 1995-2005 Sun Microsystems, Inc.