如何在DTrace中打印关联数组?

8
这个问题已经被概括地提出了。“dtrace 'print an associative array'”只有一个谷歌搜索结果,类似的搜索同样无用。
编辑:如果我使用聚合,我不知道是否仍然能够删除条目。我的应用程序要求我能够执行诸如此类的操作:
file_descriptors[0] = "stdin"
file_descriptors[3] = "service.log"

...
...


file_descriptors[3] = 0

...
...

# should print only those entries that have not been cleared.
print_array(file_descriptors)

我知道你可以清除整个聚合,但单个条目怎么办?
更新:
由于我是在OS X上进行此操作,并且我的应用程序是跟踪特定进程已打开的所有文件描述符,因此我能够拥有256个路径名的数组,如下所示:
syscall::open*:entry
/execname == $1/
{
    self->path = copyinstr(arg0);
}

syscall::open*:return
/execname == $1/
{    
    opened[arg0] = self->path;
}

syscall::close*:entry
/execname == $1/
{
    opened[arg0] = 0;
}

tick-10sec
{
    printf("  0:  %s\n", opened[0]);
}

The above probe repeated 255 more times...

很糟糕,我真的希望有更好的东西。

3个回答

1

this 是 Google 找到的链接吗?因为这个建议听起来非常可靠:

我认为你想要的效果应该通过使用聚合而不是数组来实现。所以你实际上会做一些像这样的事情:

@requests[remote_ip,request] = count();

...然后:
profile:::tick-10sec
{
    /* print all of the requests */
    printa(@requests);

    /* Nuke the requests aggregation */
    trunc(@requests);
}

哦 - 我现在明白你在做什么了。聚合不是你想要的,除非你正在收集该键的一堆数据。抱歉,我误解了。 - Don
没问题。这个问题有点难以解决,我在帖子中可能没有表达清楚... - Sniggerfardimungus

1
使用关联数组和 sum(1)sum(-1) 代替 count()

0

print(array) 似乎被定义为执行你想要的操作:

print 动作根据 DTrace 所知的类型信息格式化输出。输出数据根据类型进行格式化。以下规则涵盖了输出格式:

数组中的每个条目都会被打印出来。

https://illumos.org/books/dtrace/chp-fmt.html


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接