Introduction

STLinux supports user space memory leak checking on ST40 and ARM using mtrace.

To use mtrace in an application, simply include mcheck.h and call the function mtrace() to install handlers on malloc, realloc and free. The handlers can be optionally uninstalled with the function muntrace().

The simple program below illustrates its use:

#include <stdio.h>
#include <stdlib.h>
 
#include <mcheck.h>
 
int main(void)
{
        int *ptr;
 
        // Enable mtrace memory leak checking
        mtrace();
 
        // Allocate some memory, but don't free it
        ptr = (int*)malloc(4);
 
        return 0;
}

The environment variable MALLOC_TRACE defines a file where mtrace writes its output. This file must be writable to the user or mtrace does nothing. If the file is not empty, it is overwritten.

target% MALLOC_TRACE=/root/mtrace.txt ./hello

The output file is ASCII, but not very readable. It can be post-processed into a readable form on the host using the pretty-printer perl script mtrace:

host% /opt/STM/STLinux-2.3/host/bin/mtrace hello mtrace.txt
 
Memory not freed:
-----------------
   Address     Size     Caller
0x00411378      0x4  at /localhome2/smithc/mtrace/hello.c:13
host%

The pretty-print script is provided by the rpm stlinux23-host-mtrace. This was not included in the original STLinux 2.3 release and so may need to be explicitly installed.