【问题标题】:Does valgrind memcheck support checking mmapvalgrind memcheck 是否支持检查 mmap
【发布时间】:2013-02-26 08:53:02
【问题描述】:

我正在尝试使用 valgrind 来检测内存泄漏。它适用于堆泄漏(即来自 malloc 或 new 的内存分配)。但是,它是否支持在 linux 中检查 mmap 泄漏?

谢谢 张

【问题讨论】:

    标签: c++ c memory-leaks valgrind mmap


    【解决方案1】:

    遗憾的是,valgrind 的 memcheck 不支持 mmap 跟踪(至少不是开箱即用的),但还是有希望的。

    我最近遇到了 valgrind-mmt,这是一个用于跟踪 mmap 内存访问和分配的 valgrind 分支: https://nouveau.freedesktop.org/wiki/Valgrind-mmt

    它由envytools开发,似乎主要用于图形驱动开发。

    mmap 跟踪工具mmt 对所有对 mmap 内存的访问进行深度跟踪,包括加载和存储。这对于查找泄漏的 mmap 内存的工作来说可能太多了,并且需要对工具的输出进行处理和分析,但是通过一些仔细的工作,它可能对检测 mmap 泄漏情况很有用。就个人而言,我使用它只取得了部分成功,但也许其他人会更幸运。

    请注意,它可能不适合anonymous mmap allocations


    入门:

    【讨论】:

      【解决方案2】:

      不能直接调试,很难调试,看valgrind.h

         VALGRIND_MALLOCLIKE_BLOCK should be put immediately after the point where a
         heap block -- that will be used by the client program -- is allocated.
         It's best to put it at the outermost level of the allocator if possible;
         for example, if you have a function my_alloc() which calls
         internal_alloc(), and the client request is put inside internal_alloc(),
         stack traces relating to the heap block will contain entries for both
         my_alloc() and internal_alloc(), which is probably not what you want.
      
         For Memcheck users: if you use VALGRIND_MALLOCLIKE_BLOCK to carve out
         custom blocks from within a heap block, B, that has been allocated with
         malloc/calloc/new/etc, then block B will be *ignored* during leak-checking
         -- the custom blocks will take precedence.
      
         VALGRIND_FREELIKE_BLOCK is the partner to VALGRIND_MALLOCLIKE_BLOCK.  For
         Memcheck, it does two things:
      
         - It records that the block has been deallocated.  This assumes that the
           block was annotated as having been allocated via
           VALGRIND_MALLOCLIKE_BLOCK.  Otherwise, an error will be issued.
      
         - It marks the block as being unaddressable.
      
         VALGRIND_FREELIKE_BLOCK should be put immediately after the point where a
         heap block is deallocated.
      

      【讨论】:

        猜你喜欢
        • 2014-04-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-10-17
        • 2011-10-26
        • 2021-10-26
        • 2023-03-22
        • 2019-03-13
        相关资源
        最近更新 更多