next up previous contents index
Next: 19.1.1 Memdebug Library Configuration Up: 19 Memory Debugging Utilities: Previous: 19 Memory Debugging Utilities:

19.1 Introduction

The Memory Debug Utilities Library is a set of functions which replace the standard OSKit memory allocation functions, see Section 9.5, of the minimal C library. The replacement routines detect problems with memory allocation, and can print out file and line information, along with a back-trace to the offending allocation.

All of the standard functions are covered: malloc, memalign, calloc, realloc, free, and smalloc, smemalign, and sfree.

To use the library, just include -lmemdebug on the linker command line before the standard C library (or wherever it is the standard allocation routines are coming from).

The memdebug library implements a fence-post style malloc debug library. It detects the following problems:

Whenever a problem is encountered a back-trace (in the form of program counter values) is dumped (back-tracing from the allocation of the memory). File and line number information from where the allocation call was made are also printed (if available). If the failure was detected in a call to free, the file and line of that call are printed. This is called a ``bogosity dump.''

When correctable errors are detected (e.g., sfree'ing a malloc'd block, or sfree'ing with the wrong size block). the correct thing will be done, and the program will continue as normal (except for the bogosity dump).

Note that file and line number information is only available if you're using the macro wrappers for the allocators defined in malloc_debug.h. The call stack trace is always available.

One of the shortcomings of the library is that errors are only detected during explicit calls into the library, and not at the time that they happen. The memdebug_sweep function will check the validity of all allocated blocks, and by judiciously sprinkling calls throughout your code you can narrow down memory trashing problems. Similarly, the memdebug_ptrchk function will run a sanity check on a single pointer. Both functions, when printing ``bogosity dumps'' will also print the file and line at which they were called.

To help detect leaks of unfreed memory, use memdebug_mark and memdebug_check. memdebug_mark tags all allocated blocks, and then memdebug_check will check for untagged blocks. In this way, you can ``mark'' all blocks as okay and at a later point when all memory allocated after the mark should have been released, insert a ``check''. The library will print a bogosity dump for any allocation that is untagged.

To help detect accesses after memory is released, or accesses to uninitialized memory, the library sets all bytes of an allocation to:




next up previous contents index
Next: 19.1.1 Memdebug Library Configuration Up: 19 Memory Debugging Utilities: Previous: 19 Memory Debugging Utilities:

University of Utah Flux Research Group