Search code examples
clinuxmemory-leaksptrace

Memory debugger with ptrace


I would like to expand an existing tool with the functionality of a memory debugger (just leak detection).

I know that some memory debuggers work by replacing malloc/free and keeping track of what is pending to be freed and who allocated it; or by running the process on sort of a virtual machine and monitoring memory accesses.

I want to know if it makes sense to use ptrace() to set breakpoints on malloc/free, instead rebuilding with dmalloc for example, and monitoring allocations in a separate process. Would it be too slow? Does any other tool work this way?


Solution

  • It is not practical to use a debugger and trap malloc/free calls for a few of reasons:

    1. The overhead of switching from one process to the other one is just to great on nontrivial programs.

    2. You'll end up spending a similar amount of memory to store ownership information than with other methods. (This is what I actually wanted to improve)

    3. There are quite a few functions that work the heap, and it could be easy to miss some.