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

发布于 2024-08-22 21:20:59 字数 897 浏览 2 评论 0原文

这个问题几乎概括了这一点。 “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...

太糟糕了。我真的很想有更好的东西。

The question pretty much sums it up. "dtrace 'print an associative array'" has exactly one google hit and the similar searches are equally useless.

EDIT:

If I were to use an aggregation, I'm not aware that I'd still be able to remove entries. My application requires that I be able to do things like:

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)

I know that you can clear an entire aggregation, but what about a single entry?

UPDATE:

Since I'm doing this in OS X and my application is to track all of the file descriptors that have been opened by a particular process, I was able to have an array of 256 pathnames, thusly:

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...

It sucks. I'd really like to have something better.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

简单爱 2024-08-29 21:21:00

使用关联数组以及 sum(1)sum(-1) 而不是 count()

Use an associative array and sum(1) and sum(-1) instead of count().

七七 2024-08-29 21:21:00

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

打印操作根据 DTrace 知道的类型信息来格式化输出。输出数据根据类型进行格式化。以下规则涵盖输出格式:

打印数组中的每个条目。

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

print(array) seems to be defined to do what you want:

The print action formats output based on the type information that DTrace knows about. The output data is formatted based on the type. The following rules cover the output format:

Every entry in an array is printed.

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

坏尐絯℡ 2024-08-29 21:20:59

是 Google 找到的链接吗?因为这个建议看起来很合理:

我认为您正在寻找的效果应该通过使用来实现
聚合而不是数组。所以你实际上会做类似的事情:

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

...然后:

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

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

Is this the link Google found? Because the advice seems pretty sound:

I think the effect you're looking for should be achieved by using an
aggregation rather than an array. So you'd actually do something like:

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

... and then:

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

    /* Nuke the requests aggregation */
    trunc(@requests);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文