分解C ++代码大小 [英] Break down C++ code size

查看:205
本文介绍了分解C ++代码大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在寻找一个不错的Stack 溢出风格的答案在旧博客帖子的第一个问题 C ++代码大小 ,我将在下面重复:

I'm looking for a nice Stack Overflow-style answer to the first question in the old blog post C++ Code Size, which I'll repeat below:


我真的很喜欢一些工具(理想情况下,基于g ++),它显示了编译/链接代码的哪些部分是从C ++源代码的哪些部分生成的。例如,查看特定模板是否正在为数百种不同类型(通过模板专门化可修复)或代码是否被过度内联,或特定函数是否大于预期而被实例化。

I’d really like some tool (ideally, g++ based) that shows me what parts of compiled/linked code are generated from what parts of C++ source code. For instance, to see whether a particular template is being instantiated for hundreds of different types (fixable via a template specialization) or whether code is being inlined excessively, or whether particular functions are larger than expected.


推荐答案

看起来像这样的东西应该存在,但我没有使用任何类似的东西。我可以告诉你我怎么去一起编写脚本,虽然。

It does seem like something like this should exist, but I haven't used anything like it. I can tell you how I'd go about scripting this together, though. There are probably swifter and/or sexier ways to do it.

addr2line命令接收地址,并可以告诉您机器代码在哪里实现源代码。可执行文件需要使用调试符号来构建,你可能不想对其进行优化(-O0,-O1或-Os可能是你想要的最大值)。 addr2line有几个标志,你想读取它的手册页,但是你肯定需要使用-C或者--demangle,如果你想看到在输出中有意义的C ++函数名。

The addr2line command takes in an address and can tell you where the source code that the machine code there implements. The executable needs to be built with debugging symbols, and you'll probably not want to optimize it much (-O0, -O1, or -Os is probably as high as you'd want to go at first anyway). addr2line has several flags, and you'll want to read its manual page, but you will definitely need to use -C or --demangle if you want to see C++ function names that make sense in the output.

objdump命令可以打印出许多类型的对象文件中的各种有趣的东西。它可以做的事情之一是打印一个表,表示一个对象文件(或包括可执行文件)中引用的符号。

The objdump command can print out all kinds of interesting things about the stuff in many types of object files. One of the things it can do is print out a table representing the symbols in or referred to by an object file (including executables).

你想要的是objdump告诉你.text段的地址和大小。这是实际可执行机器代码生活的地方。有几种方法可以做到这一点,但最简单的(对于这个,无论如何)可能是你要做的:

What you'll want to is for objdump to tell you the address and size of the .text section. This is where actual executable machine code lives. There are several ways to do this, but the easiest (for this, anyway) is probably for you to do:

objdump -h my_exe | grep text

这样应该会产生:

 12  .text       0000049  000000f000  0000000f000 00000400  2**4

如果你没有grep它,它会给你一个标题:

If you didn't grep it it would give you a heading like:

Idx  Name        Size     VMA         LMA         File off  Algn

我认为对于可执行文件,VMA和LMA应该是一样的, ,但是我认为LMA是最好的。

I think for executables the VMA and LMA should be the same, so it won't matter which you use, but I think LMA is the best. You'll also want the size.

使用LMA和大小,您可以反复调用addr2line查询机器码的源代码。我不知道如果你通过一个指令内的地址,这将如何工作,但我认为它应该工作。

With the LMA and size you can repeatedly call addr2line asking for the source code origin of the machine code. I'm not sure how this would work if you passed an address that was within one instruction, but I think it should work.

addr2line -e my_exe <address>

这个输出将是一个路径/文件名,冒号和行号。
如果要计算每个唯一路径/文件的出现次数,您应该能够查看具有最高计数的路径/文件。
Perl 散列使用路径/文件:num作为键和计数器作为价值将是一个简单的方法来实现这一点,虽然有更快的方式,如果你发现运行太慢。
你也可以过滤掉你可以确定不需要提前包含的东西。
为了显示你的输出,你可能想从同一个函数中过滤出不同的行,但是你可能会注意到一个函数中不同的行有不同的计数,这可能很有趣。无论如何,这可以通过使addr2line告诉你函数名或使用objdump -t在第一步,并一次工作一个函数。

The output from this will be a path/filename, a colon, and a line number. If you were to count the occurrence of each unique path/file:num you should be able to look at the ones that have the highest counts. Perl hashes using the path/file:num as the key and a counter as the value would be an easy way to implement this, though there are faster ways if you find that runs too slow. You could also filter out things that you can determine don't need to be included early. For displaying your output you may want to filter out different lines from the same function, but you may notice that different lines within one function have different counts, which could be interesting. Anyway, that could be done either by making addr2line tell you the function name or using objdump -t in the first step and work one function at a time.

如果你看到一些模板代码或其他代码行显示在您的可执行文件中比您认为它们应该更频繁,那么您可以轻松地找到它们,并仔细看看。

If you see that some template code or other code lines are showing up in your executables more often than you think they should then you can easily locate them and have a closer look. Macros and inline functions may show end up manifesting themselves differently than you expect.

如果你不知道,objdump和addr2line来自 GNU binutils 包,其中包含其他一些有用的工具。

If you didn't know, objdump and addr2line are from the GNU binutils package, which includes several other useful tools.

这篇关于分解C ++代码大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆