简体   繁体   English

Valgrind和QEMU - 无法检测到内存泄漏

[英]Valgrind and QEMU - Unable to detect memory leak

I want to test my C++ code for memory leaks with Valgrind (memcheck) x86. 我想用Valgrind(memcheck)x86测试我的C ++代码是否存在内存泄漏。

But the software gets cross-compiled and is running on ARM. 但该软件经过交叉编译并在ARM上运行。

In order to do some automated testing I decided to emulate my ARM hardware via QEMU. 为了进行一些自动化测试,我决定通过QEMU模拟我的ARM硬件。

And I also decided to use the cpputest unit test ARM binaries to ensure a deterministic behaviour and search for memory leaks within the scope the unit test covers. 而且我还决定使用cpputest单元测试ARM二进制文件来确保确定性行为并在单元测试涵盖的范围内搜索内存泄漏。

All in all, I have an ARM binary which should be emulated via QEMU user mode. 总而言之,我有一个ARM二进制文件,应该通过QEMU用户模式进行模拟。

My call looks like that: 我的电话看起来像这样:

./valgrind --smc-check=all qemu-arm-static -L ... arm-ptest-binary

My C++ code looks like that. 我的C ++代码看起来像那样。 It has a memory leak of 20 byte and the valgrind call do not find this leak when using it with QEMU. 它有20字节的内存泄漏,并且在与QEMU一起使用时 ,valgrind调用没有发现此泄漏 After I insert a memory allocation and no freeing mechanism I'd have expected an memory leak 在我插入内存分配并且没有释放机制之后,我预计会发生内存泄漏

int test_func ()
{
  int *foo;
  foo = new int [5];
  printf("test_func called!\n");
  return 1;
}

Valgrind output: Valgrind输出:

==19300== HEAP SUMMARY:
==19300==     in use at exit: 1,103,129 bytes in 2,316 blocks
==19300==   total heap usage: 4,259 allocs, 1,943 frees, 1,866,916 bytes allocated
==19300== 
==19300== LEAK SUMMARY:
==19300==    definitely lost: 0 bytes in 0 blocks
==19300==    indirectly lost: 0 bytes in 0 blocks
==19300==      possibly lost: 304 bytes in 1 blocks
==19300==    still reachable: 1,102,825 bytes in 2,315 blocks
==19300==         suppressed: 0 bytes in 0 blocks
[...]

When I run this program on ARM hardware the valgrind-arm finds the leak with the exact same binary. 当我在ARM硬件上运行该程序时,valgrind-arm会使用完全相同的二进制文件找到泄漏。

Does anyone of you have an idea why Valgrind does not find the memory leak in combination with QEMU user mode? 你们有没有人知道为什么Valgrind与QEMU用户模式一起找不到内存泄漏?

Thanks in advance 提前致谢

You are running Valgrind on QEMU itself, which will cause valgrind to report memory leaks in QEMU's own code, but valgrind does not have sufficient visibility into what the guest program running under QEMU is doing to be able to report leaks in the guest. 您正在QEMU上运行Valgrind,这将导致valgrind报告QEMU自己的代码中的内存泄漏,但valgrind没有充分了解QEMU下运行的guest虚拟机程序正在做什么以报告guest虚拟机中的泄漏。 In particular, Valgrind works by intercepting calls to malloc, free, operator new, etc -- it will be doing this for the host QEMU process's (x86) allocation and free calls, but has no way to intercept the (arm) calls your guest process makes. 特别是,Valgrind通过拦截对malloc,free,operator new等的调用来工作 - 它将为主机QEMU进程(x86)分配和免费调用执行此操作,但无法拦截(arm)调用您的guest虚拟机过程使。

You might look at running an entire guest OS under QEMU's system emulation mode, and then running the Arm Valgrind inside that on your guest program. 您可能会考虑在QEMU的系统仿真模式下运行整个来宾OS,然后在来宾程序中的其中运行Arm Valgrind。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM