DTrace C++ Mysteries Solved 转

I’ve been using DTrace on Leopard in my recent work, and while it’s a great tool, the C++ support is confusing and I couldn’t find proper documentation. But eventually I found sketchy documentation that gave me the answers, so in the interest of saving others from pain:

Basic Call Profiling. One of the most basic profiling tasks is recording function call entries and returns. And DTrace is very good at this, using the pid provider. For example, if you have a simple C program ‘prog’ that contains a function ‘foo’, you can get DTrace to trace calls to ‘foo’ like this:

sudo dtrace -c ‘./prog‘ -n ‘pid$target:prog:foo:entry‘

Unpacking this, the -c option starts a program and the process id in a special DTrace variable-type-thing called $target. The -n option specifies a probe to trace using DTrace’s standard 4-part syntax. In this case, the 4 parts are: (1) the provider, pid$target, which means user function call tracing in process $target, (2)prog, the module to trace functions in, (3) the function to trace,foo, and (4) entry, meaning we want function entries (as opposed to returns or other instructions). (Thanks to Vlad for giving me the verbal tutorial on this part.)

This time, with C++. So far, so good, but when you try to do this for C++ programs, some weirdness sets in. Let’s say our C++ program has a class C that contains a method bar that takes an int argument. This means the C-style symbol that the linker operates on is the mangled name, some junk like __ZN1C1barEi. According to Sun’s article on DTrace with C++, which is all I could find by Googling “dtrace c++” you use the mangled name in the provider specification, as in

sudo dtrace -c ‘./prog‘ -n ‘pid$target:prog:__ZN1C1barEi:entry‘

But that didn’t work at all for me on Leopard. DTrace said “invalid probe specifier, blah, blah, blah”. I figured out part of the answer by trial and error, but I didn’t get the full answer until I remembered that Vlad suggested the support for demangled names might have been added as part of Objective C support, Googled “dtrace objective c”, found a blog post, followed a link from there to the Apple dtrace man page, saw the “-xmangled” option, Googled “xmangled”, and found an Apple mailing list thread. Ugh. Is there no better way to find documentation? (I guess I should be comparing with the effort of searching a wall full of manuals, and instead of complaining, I should be grateful that once again, Teh Mighty Interwebs have proven infinitely wise.)

Anyway, the answer is that on Leopard you can specify probes for C++ functions using either mangled names or unmangled names, with appropriate obscure incantations either way.

Unmangled:

sudo dtrace -c ‘./prog‘ -n ‘pid$target:prog:C??f(int):entry‘

The key is that you have to give the whole signature for the method, but you can’t have colons in there because the probe specification parser gets confused, so you use the ? wildcard instead. You can also abbreviate using other wildcards, as in C??f*.

Mangled:

sudo dtrace -c ‘./prog‘ -n ‘pid$target:prog:__ZN1C1barEi:entry‘ -xmangled

-xmangled tells DTrace to use the mangled names. If you do it this way, you’ll also see the mangled names in the output.

0
时间: 2024-08-07 21:20:31

DTrace C++ Mysteries Solved 转的相关文章

dtrace.org

http://dtrace.org/blogs/rm/2016/09/15/turtles-on-the-wire-understanding-how-the-os-uses-the-modern-nic/ Turtles on the Wire: Understanding how the OS uses the Modern NIC The modern networking card (NIC) has evolved quite a bit from the simple Etherne

Unsupervised learning, attention, and other mysteries

Unsupervised learning, attention, and other mysteries Get notified when our free report “Future of Machine Intelligence: Perspectives from Leading Practitioners” is available for download. The following interview is one of many that will be included

DTRACE简介(2)

By samwan on 三月 21, 2007 通过上一次的介绍,相信大家对DTRACE已经有了一个初步的认识.上一次结束时专门留了一个例子,可能大家第一次看有很多不明白的地方,没有关系,随着我们对DTRACE更多的介绍,很快就会"云开雾散"了. D语言作为一种编程语言,自然就有其语法.关键字.数据结构.运算符.函数等,我将一一介绍. D语言中标志符名称与C语言类似,由字母.数字和下划线组成,其中第一个字符必须是字母或者下划线.D语言预留了一些关键字供DTRACE本身使用,关键字不能

DTRACE简介之完结篇3

https://blogs.oracle.com/swan/entry/dtrace%E7%AE%80%E4%BB%8B_3 DTRACE简介之完结篇 By samwan on 四月 13, 2007 已经有好长一段时间没有更新blog了,不是我懒,确实是这段时间太忙.工作加上生活,算了,不找借口了,还是来把DTRACE简介作个完结吧.本来开始写的时候只准备用一篇文章来描述,等真正写出来就发现,不行了,Dtrace实在是太强大了,即便是加上今天的,也没有完全讲到,遗漏的地方就只有请各位看官自己去

DTRACE简介(1)

https://blogs.oracle.com/swan/entry/dtrace%E7%AE%80%E4%BB%8B By samwan on 三月 20, 2007 记得几年前看过一部美国大片叫<全民公敌(Enemy of the State)>,在里面,谋杀国会议员的主谋强沃特和他的属下,为了取回记录着其犯罪事实的磁碟片,用高科技的卫星监视,使主人公史密斯的行踪处于严密的监控中.当时就对美国高科技跟踪系统惊叹不已.当然作为一个普通公民,是不希望自己受到监视的.但是对于计算机系统,如果能

Mac OS X的利器dtrace,能实现process/file monitor,特别便于排错

例如我想看看为什么Android的make系统出错, 先开启工具追踪瞬间启动的进程命令行,然后做自己想做的事,例如android的mm编译命令 sudo newproc.d ####结果############   这是实时显示的 2015 Oct  2 20:35:07 54743 <54052> 64b  make -C /Users/q/Documents/android_src -f build/core/main.mk all_modules 2015 Oct  2 20:35:07

在Oracle Linux上安装dtrace

http://www.ohsdba.cn/index.php?g=Home&m=Article&a=show&id=171 时间: 2016-10-09 00:40:04  | 作者: ohsdba  | English 如非注明,本站文章皆为原创.欢迎转载,转载时请注明出处和作者信息. DTrace(dynamic tracing)是Sun Solaris系统上主要的性能诊断工具,可以对kernel和用户应用程序进行动态跟踪,并且对系统运行不构成任何危险的技术,后被Oracle公

MySQL 5.6.20-4 and Oracle Linux DTrace

https://blogs.oracle.com/wim/entry/mysql_5_6_20_4?utm_source=tuicool&utm_medium=referral By WimCoekaerts-Oracle on Jul 31, 2014 The MySQL team just released MySQL 5.6.20. One of the cool new things for Oracle Linux users is the addition of MySQL DTra

在 Oracle Linux 上使用 DTrace

作者:Richard Friedman 简要介绍适用于 Oracle Linux 的 DTrace 探测器和提供程序,以及与 Oracle Solaris 中 DTrace 探测器和提供程序的区别.还介绍了 DTrace 命令和脚本编写. 2013 年 6 月发布 DTrace 是一个全面的动态跟踪工具,最初是为 Oracle Solaris 操作系统开发的,现在 Oracle Linux 客户也可以使用.DTrace 旨在提供运营洞察力,允许用户实时动态调整和排除操作系统和应用程序故障.DTr