Lightweight memory leak debugging on linux

前端 未结 8 1300
别那么骄傲
别那么骄傲 2021-01-30 09:33

I looked for existing answers first and saw that Valgrind is everyone’s favorite tool for memory leak debugging on linux. Unfortunately Valgrind does not seem

8条回答
  •  醉话见心
    2021-01-30 10:00

    GNU libc has built-in malloc debugging:

    http://www.gnu.org/software/libc/manual/html_node/Allocation-Debugging.html

    Use LD_PRELOAD to call mtrace() from your own .so:

    #include 
    static void prepare(void) __attribute__((constructor));
    static void prepare(void)
    {
        mtrace();
    }
    

    Compile it with:

    gcc -shared -fPIC dbg.c -o dbg.so
    

    Run it with:

    export MALLOC_TRACE=out.txt
    LD_PRELOAD=./dbg.so ./my-leaky-program
    

    Later inspect the output file:

    mtrace ./my-leaky-program out.txt
    

    And you will get something like:

    Memory not freed:
    -----------------
               Address     Size     Caller
    0x0000000001bda460     0x96  at /tmp/test/src/test.c:7
    

    Of course, feel free to write your own malloc hooks that dump the entire stack (calling backtrace() if you think that's going to help).

    Lines numbers and/or function names will be obtainable if you kept debug info for the binary somewhere (e.g. the binary has some debug info built in, or you did objcopy --only-keep-debug my-leaky-program my-leaky-program.debug).


    Also, you could try Boehm's GC, it works as a leak detector too:

    http://www.hpl.hp.com/personal/Hans_Boehm/gc/leak.html

提交回复
热议问题