Previous: mgauge, Up: Tools

8.5 Memory allocation tracing

This file defines mtrace() and muntrace(), two functions which enable and disable memory allocation tracing respectively. These should be called in matching pairs but will have no effect unless the MALLOC_TRACE environment variable is set to the filename of the trace file to use. The resulting trace files can be processed by the mtrace perl script which is distributed with the GNU C library.

      1  /*
      2   * Illustrates the use of mpatrol's memory allocation tracing
      3   * tool.
      4   */
      7  #include "mpatrol/mtrace.h"
     10  int main(void)
     11  {
     12      void *p;
     14      mtrace();
     15      p = malloc(16);
     16      free(p);
     17      p = malloc(8);
     18      muntrace();
     19      return EXIT_SUCCESS;
     20  }

If this is run with the MALLOC_TRACE environment variable set to a valid filename then the resulting trace file should look something like that displayed below, which can then be processed by the mtrace perl script.

     = Start
     @ ./a.out:(main+0x40)[12a94] + 2c578 0x10
     @ ./a.out:(main+0x6c)[12ac0] - 2c578
     @ ./a.out:(main+0xa0)[12af4] + 2c578 0x8
     = End