1

Without valgrind, how can I find where allocated a large chunk of memory (e.g. malloc?) Is it possible that set a gdb break point for a large memory allocation?

I can't use valgrind, because some dependency library forbids use of valgrind (will crash valgrind as it does not recognize its special stack)

bugs king
  • 566
  • 5
  • 13
  • Possible duplicate of [track C++ memory allocations](https://stackoverflow.com/questions/910172/track-c-memory-allocations) – Jake Askeland Jun 13 '17 at 04:11
  • Like I said, we forbid use of valgrind massif. – bugs king Jun 13 '17 at 04:42
  • 3
    The other way to track memory allocation is to implement new operator and call malloc from that method. That's what we do to keep track of memory allocation and deallocation. Then that class should be a base class of your classes so that it would invoke that new operator whenever you plan to allocate memory using new operator. – Asesh Jun 13 '17 at 05:23
  • 2
    can you use an external allocator? e.g. [jemalloc](https://github.com/jemalloc/jemalloc/wiki/Getting-Started) . it is able to print statistics and it is open source – Alexander Jun 13 '17 at 05:44
  • no use of external allocator, the existing tool is gdb. no tcmalloc or other malloc. – bugs king Jun 13 '17 at 07:03
  • 2
    What OS and machine architecture? `break malloc if $rdi > 5000000` may do what you want. – Mark Plotnick Jun 13 '17 at 07:10
  • linux box, 64bit arch – bugs king Jun 13 '17 at 07:47
  • Can you share the issues with the stack and Valgrind? – Paul Floyd Jun 13 '17 at 12:10

1 Answers1

0

A shared library interposer will do job nicely. Here is an excellent article that gives a perfect example of what you need.

If a function is in a shared library, the runtime linker can be instructed to call another 'interposed' function instead. The interposer can totally replace the functionality or it can augment it. A great example is the malloc family of functions. In your case, you can have the interposer check for the malloc size and take special action. gdb can be used to place breakpoint in the interpose library itself, so you can put a breakpoint on the special logic to fulfill your requirements.

Interposers only work for shared (.so) libraries. Static (.a) libraries directly link into the executable and the calls cannot be easily intercepted. The malloc family is normally linked from a shard library in Linux so this should not be an issue in your case.

All major flavors of Linux support interposers using the LD_PRELOAD functionality.

Matthew Fisher
  • 2,258
  • 2
  • 14
  • 23