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

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

  1. Unsupervised learning, attention, and other mysteries

    Unsupervised learning, attention, and other mysteries Get notified when our free report “Future of M ...

  2. dtrace.org

    http://dtrace.org/blogs/rm/2016/09/15/turtles-on-the-wire-understanding-how-the-os-uses-the-modern-n ...

  3. Solved Unable to copy the source file ./installer/services.sh to the destination file /etc/vmware-t

    Sometimes when you intall vmwaretools there will be some problems such as "Unable to copy the s ...

  4. FreeBSD打开DTrace支持

    主要翻译自:https://wiki.freebsd.org/DTrace FreeBSD跟Linux发行版一个比较大的差异,就是提倡源码构建.因此这里提到比较多的编译开关设置.自2012年5月后,D ...

  5. 在Oracle Linux上使用DTrace的相关指导

    如果你使用的Oracle Linux,因为sun被Oracle收购后,Oracle Linux版本的DTrace可以直接在Oracle官网进行下载. 下载地址 http://www.oracle.co ...

  6. Adding DTrace Probes to PHP Extensions

      By cj on Dec 06, 2012 The powerful DTrace tracing facility has some PHP-specific probes that can b ...

  7. 动态追踪技术(中) - Dtrace、SystemTap、火焰图

    http://openresty.org/cn/presentations.html http://weibo.com/agentzh?is_all=1 http://openresty.org/po ...

  8. DTrace patch for Python 2.7.x and 3.x

    DTrace patch for Python 2.7.x and 3.x Última Actualización: 21 de septiembre de 2015 https://www.jce ...

  9. python 2016 大会 pyconsk ppt ---python dtrace

    https://github.com/pyconsk/2016-slides PyCon SK 2016 - March 2016 1DTrace and PythonJesús Cea Aviónj ...

随机推荐

  1. COGS 827. [Tyvj Feb11] 网站计划

    输入文件:web.in   输出文件:web.out   简单对比时间限制:1 s   内存限制:128 MB 描述 Description     Tyvj的Admin--zhq同学将在寒假开始实行 ...

  2. 基于SAE的Python+Django部署

    本文主要参考:http://www.cnblogs.com/qtsharp/archive/2012/01/12/2320774.html,另外包括自己的实际操作. 一.申请SAE帐号以及创建应用ya ...

  3. MVC之在实例中的应用

    MVC模式在Java Web应用程序中的实例分析 1. 结合六个基本质量属性 1)可用性 2)可修改性 3)性能 4)安全性 5)可测试性 6)易用性 2. 分析具体功能模块的MVC设计实现(例如登录 ...

  4. ElasticSearch的常用方法

    关键词  cluster  集群  shards  索引分片    replicas  索引的副本    recovery  数据重新分布 gateway  索引的持久化方式 Transport 交互 ...

  5. Vue项目结构梳理

    Vue项目结构图: 简单介绍目录结构 build目录是一些webpack的文件,配置参数什么的,一般不用动 config是vue项目的基本配置文件 node_modules是项目中安装的依赖模块 sr ...

  6. c++:delete或free报错,语法正常。

    #include <stdio.h> #include <iostream> int _tmain(int argc, _TCHAR* argv[]) { ]; memcpy( ...

  7. 背包DP || Codeforces 544C Writing Code

    程序员写bug的故事23333 题意:n个程序员,一共写m行程序,最多产生b个bug,问方案数 思路:f[i][j]表示写了i行,产生了j个bug的方案数,因为每个人都是可以独立的,所以i循环到n都做 ...

  8. ios之coredata

    Core Data数据持久化是对SQLite的一个升级,它是ios集成的,在说Core Data之前,我们先说说在CoreData中使用的几个类. (1)NSManagedObjectModel(被管 ...

  9. luogu P1462 通往奥格瑞玛的道路--spfa+二分答案

    P1462 通往奥格瑞玛的道路 题目背景 在艾泽拉斯大陆上有一位名叫歪嘴哦的神奇术士,他是部落的中坚力量 有一天他醒来后发现自己居然到了联盟的主城暴风城 在被众多联盟的士兵攻击后,他决定逃回自己的家乡 ...

  10. spring-mvc junit测试

    import org.junit.runner.RunWith; import org.springframework.test.context.ContextConfiguration; impor ...