如何运行此 DTrace 脚本来分析我的应用程序?
我在网上搜索一些东西来帮助我进行装配线分析。我搜索并在 http://www.webservertalk.com/message897404.html
上找到了 一些东西这个问题有两个部分;查找特定类型(inc、add、shl 等)的所有指令以确定分组,然后找出正在执行的指令并正确求和。第一位很棘手,除非通过反汇编程序进行分组就足够了。为了确定正在执行哪些指令,Dtrace 当然是您的朋友(至少在用户态)。
最好的方法是只在每个基本块的开始处插入工具;现在找到这些将是一个手动过程......但是,对每条指令进行检测对于小型应用程序来说是可行的。这是一个例子:
首先,我们正在测试的相当简单的 C 程序:
main()
{
int i;
for (i = 0; i < 100; i++)
getpid();
}
现在,我们稍微棘手的 D 脚本:
#pragma D option quiet
pid$target:a.out::entry
/address[probefunc] == 0/
{
address[probefunc]=uregs[R_PC];
}
pid$target:a.out::
/address[probefunc] != 0/
{
@a[probefunc,(uregs[R_PC]-address[probefunc]), uregs[R_PC]]=count();
}
END
{
printa("%s+%#x:\t%d\t%@d\n", @a);
}
<前><代码>主+0x1:1 主+0x3:1 主+0x6:1 主+0x9:1 主+0xe: 1 主+0x11:1 主+0x14:1 主+0x17:1 主+0x1a:1 主+0x1c:1 主+0x23:101 主+0x27:101 主+0x29:100 主+0x2e: 100 主+0x31:100 主+0x33:100 主+0x35:1 主+0x36:1 主+0x37:1
从给出的例子来看,这正是我所需要的。但是我不知道它在做什么,如何保存 DTrace 程序,如何执行我想要获得结果的代码。所以我打开这个希望一些具有良好 DTrace 背景的人可以帮助我理解代码,保存它,运行它并希望得到显示的结果。
I was searching online for something to help me do assembly line profiling. I searched and found something on http://www.webservertalk.com/message897404.html
There are two parts of to this problem; finding all instructions of a particular type (inc, add, shl, etc) to determine groupings and then figuring out which are getting executed and summing correcty. The first bit is tricky unless grouping by disassembler is sufficient. For figuring which instructions are being executed, Dtrace is of course your friend here( at least in userland).
The nicest way of doing this would be instrument only the begining of each basic block; finding these would be a manual process right now... however, instrumenting each instruction is feasible for small applications. Here's an example:
First, our quite trivial C program under test:
main()
{
int i;
for (i = 0; i < 100; i++)
getpid();
}
Now, our slightly tricky D script:
#pragma D option quiet
pid$target:a.out::entry
/address[probefunc] == 0/
{
address[probefunc]=uregs[R_PC];
}
pid$target:a.out::
/address[probefunc] != 0/
{
@a[probefunc,(uregs[R_PC]-address[probefunc]), uregs[R_PC]]=count();
}
END
{
printa("%s+%#x:\t%d\t%@d\n", @a);
}
main+0x1: 1 main+0x3: 1 main+0x6: 1 main+0x9: 1 main+0xe: 1 main+0x11: 1 main+0x14: 1 main+0x17: 1 main+0x1a: 1 main+0x1c: 1 main+0x23: 101 main+0x27: 101 main+0x29: 100 main+0x2e: 100 main+0x31: 100 main+0x33: 100 main+0x35: 1 main+0x36: 1 main+0x37: 1
From the example given, this is exactly what i need. However I have no idea what it is doing, how to save the DTrace program, how to execute with the code that i want to get the results of. So i opened this hoping some people with good DTrace background could help me understand the code, save it, run it and hopefully get the results shown.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您只想运行这个特定的 DTrace 脚本,只需将其保存到 .d 脚本文件并使用如下命令针对已编译的可执行文件运行它:
在其中替换
dtracescript.d
与您的脚本文件名。这假设您已经将 DTrace 作为系统的一部分(我运行的是 Mac OS X,自 Leopard 以来就已经有了它)。
如果您对它的工作原理感到好奇,我不久前写了一篇关于使用 DTrace for MacResearch 的两部分教程,可以在 此处 和此处。
If all you want to do is run this particular DTrace script, simply save it to a .d script file and use a command like the following to run it against your compiled executable:
where you replace
dtracescript.d
with your script file name.This assumes that you have DTrace as part of your system (I'm running Mac OS X, which has had it since Leopard).
If you're curious about how this works, I wrote a two-part tutorial on using DTrace for MacResearch a while ago, which can be found here and here.