1 Star2 Stars3 Stars4 Stars5 Stars (还没有评分)
Loading...

DTraceToolkit脚本解析(2)- kill.d

kill.d位于DTraceToolkitProc文件夹下,其功能是检查进程之间发送的信号值,以及信号发送是否成功。脚本代码如下(略去版权信息):

dtrace:::BEGIN
{
    /* Print header */
    printf("%5s %12s %5s %-6s %s\n",
        "FROM", "COMMAND", "SIG", "TO", "RESULT");
}

syscall::kill:entry
{
    /* Record target PID and signal */
    self->target = arg0;
    self->signal = arg1;
}

syscall::kill:return
{
    /* Print source, target, and result */
    printf("%5d %12s %5d %-6d %d\n",
        pid, execname, self->signal, self->target, (int)arg0);

    /* Cleanup memory */
    self->target = 0;
    self->signal = 0;
}

下面就对脚本做一个分析:
(1)

dtrace:::BEGIN
{
    /* Print header */
    printf("%5s %12s %5s %-6s %s\n",
        "FROM", "COMMAND", "SIG", "TO", "RESULT");
}

脚本开始运行时,打印每一列的名字:FROM是发送信号的进程IDCOMMAND是发送信号的进程名字,SIG是发送的信号值,TO是接收信号的进程IDRESULT是发送信号的结果(-1表示失败)。
(2)

syscall::kill:entry
{
    /* Record target PID and signal */
    self->target = arg0;
    self->signal = arg1;
}

当发生kill系统调用时,用thread local变量记录接收信号的进程IDarg0)和信号值(arg1)。
(3)

syscall::kill:return
{
    /* Print source, target, and result */
    printf("%5d %12s %5d %-6d %d\n",
        pid, execname, self->signal, self->target, (int)arg0);

    /* Cleanup memory */
    self->target = 0;
    self->signal = 0;
}

kill系统调用返回时,打印相应的值,同时释放thread local变量,防止内存泄漏。

最后执行脚本,输入“Ctrl-C”后,脚本程序退出,输出如下:

FROM      COMMAND   SIG TO     RESULT
  478         bash     9 567    -1
    1      launchd    30 16     0
    1      launchd    30 16     0
    1      launchd    30 16     0
    1      launchd    30 16     0

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.