mcheck
Detect memory errors in programs
SYNOPSIS
C Program Usage:
#include <mcheck.h>
int mcheck(void (*abortfunc)(enum mcheck_status status));
Other related debugging functions:
void mtrace(void);
void muntrace(void);
int mprobe(void *ptr);
PARAMETERS
abortfunc
A pointer to a user-defined function that mcheck() will call if a memory consistency error is detected. This function receives an enum mcheck_status value indicating the type of error. If NULL is passed, mcheck() will call abort(3) upon detecting an error.
enum mcheck_status values
These are possible status codes passed to abortfunc:
MCHECK_OK: No error detected.
MCHECK_NOCONF: Not configured (e.g., mcheck() not called).
MCHECK_DISABLED: Checking is disabled.
MCHECK_VERBOSITY: Not an error, used for verbose mode.
MCHECK_FREE: Attempted to free an invalid pointer.
MCHECK_HEAD: Corrupted header of an allocated block.
MCHECK_TAIL: Corrupted tail of an allocated block.
DESCRIPTION
The mcheck function is part of the GNU C Library (glibc) and is used for debugging memory allocation errors. It is not a standalone shell command executed from the terminal, but rather a C library function called within a program.
When enabled, mcheck() validates the consistency of the memory allocator's internal data structures, such as those used by malloc() and free(). If an inconsistency or corruption is detected, mcheck() calls a specified error handling function, or aborts the program if no function is provided. It helps identify common memory issues like heap corruption, double-frees, or invalid frees.
For more comprehensive memory tracing, glibc also provides mtrace() and mprobe() functions, often used in conjunction with mcheck() for memory debugging workflows.
CAVEATS
mcheck() must be called before any calls to malloc(), free(), or other memory allocation functions are made in the program. Enabling memory checking can introduce significant performance overhead due to the constant validation of memory structures. It is primarily intended for debugging purposes and should not be used in production environments. These functions are generally not thread-safe without external synchronization.
USAGE EXAMPLE (C CODE)
Below is a simplified example of how mcheck() might be used within a C program:
#include <stdio.h>
#include <stdlib.h>
#include <mcheck.h>
void my_abort_handler(enum mcheck_status status) {
fprintf(stderr, "Memory consistency error detected: ");
// ... print status details based on 'status' ...
exit(1);
}
int main() {
if (mcheck(my_abort_handler) != 0) {
fprintf(stderr, "mcheck initialization failed!\n");
exit(1);
}
char *buf = malloc(10);
if (buf == NULL) {
perror("malloc");
return 1;
}
// Use buf...
free(buf);
// Uncommenting the next line would cause an error caught by mcheck:
// free(buf); // Double free example
printf("Program finished without explicit mcheck errors.\n");
return 0;
}
To compile and run:
gcc -g -o myprog myprog.c
./myprog
For mtrace(), set the MALLOC_TRACE environment variable before running the program (e.g., export MALLOC_TRACE=./memtrace.log). Then use the mtrace (shell script) command to analyze the generated log file.
HISTORY
The mcheck and related memory debugging functions (mtrace, mprobe) are part of the GNU C Library (glibc), which is a fundamental component of Linux systems. They were introduced to provide developers with built-in tools for detecting common memory allocation errors directly within their C programs, reducing reliance on external tools for basic heap debugging. Their development aligns with glibc's ongoing efforts to provide robust and comprehensive system programming interfaces.
SEE ALSO
mtrace(3), mprobe(3), malloc(3), free(3), abort(3)