How to find C++ language constructs allocating dynamic memory?

让人想犯罪 __ 提交于 2020-02-24 10:23:27

问题


I work on an embedded system firmware code-basis which currently uses dynamic memory allocation. Now, all dynamic memory allocation shall be removed for reliability reasons.

As a first step, I removed the _sbrk syscall implementation, such that all dynamic allocations fail to link. Since I compile with -ffunction-sections -fdata-sections and link with -Wl,--gc-sections I expect the link step to complete as long as no dynamic memory allocation occurs. (This worked for me in past C projects, unsure about C++ here.)

Although the following linker error message is expected, it does not help to find the C++ construct which triggers the dynamic allocation.

[...]arm-none-eabi/lib/thumb/v7e-m/fpv4-sp/hard/libc.a(lib_a-sbrkr.o): In function `_sbrk_r':
sbrkr.c:(.text._sbrk_r+0xc): undefined reference to `_sbrk'
collect2: error: ld returned 1 exit status

Is there an effective way to find C++ language constructs, which use dynamic heap allocation (apart from the obvious use of new)?

EDIT: I am looking for a static tool, not a runtime instrumentation/analysis technique.

EDIT2: (Since somebody removed the appropriate tags:) The project targets an STM32F4 MCU and is compiled and linked with an GCC-based arm-none-eabi-* toolchain.


回答1:


The problem with blocking linkage to sbrk is that sbrk is the very bottom of the heap management process and is not necessarily called for all allocations. It is simply a means of adding to the existing heap pool.

The new operator may be overloaded, or placement-new, so need not even perform dynamic allocation or allocation via the system heap. However instances of new that do will at least call malloc().

The GNU linker --cref output may help; this will tell you for each symbol, what files reference them. Unfortunately the GNU linker does not have the --callgraph option that the ARM linker has, which will show the complete call path that leads to any particular function.

Avoiding explicit linkage of dynamic memory allocation probably means removing most of the C++ specific parts of the standard library - certainly STL container classes, std::string.




回答2:


Is there an effective way to find C++ language constructs, which use dynamic heap allocation?

Yes there is: massif.

Massif is a memory profiler which will enable you to pinpoint where memory is allocated in your code base. With such a listing, you can change all incriminated code and check again there is no dynamic allocation left.

From Massif (valgrind) documentation :

Massif is a heap profiler. It measures how much heap memory your program uses. [...] Importantly, Massif tells you not only how much heap memory your program is using, it also gives very detailed information that indicates which parts of your program are responsible for allocating the heap memory.

By default, Massif does not measure [lower-level system calls used directly to allocate memory] However, if you wish to measure all the memory used by your program, you can use the --pages-as-heap=yes.

Since you work on embedded system, it might make massif work with your code base a bit difficult; but it is possible, either by having a linux/windows port or by having massif running on your specific target.



来源:https://stackoverflow.com/questions/49812983/how-to-find-c-language-constructs-allocating-dynamic-memory

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!