Linux command
mcheck 命令
文本
复制后可按需替换文件名、目录或参数。
常用示例
Enable heap checking in a C program
mcheck(NULL)
Enable heap checking with a custom error handler
mcheck([handler_func])
Enable pedantic checking on every allocation
mcheck_pedantic(NULL)
Check all allocated blocks immediately
mcheck_check_all()
Explicitly check a specific allocated block
mprobe([ptr])
Link with mcheck to enable automatic checking
cc [program.c] -lmcheck
说明
The mcheck family of functions installs debugging hooks for the malloc(3) memory allocation functions in glibc. These hooks perform consistency checks on the heap, detecting common errors such as double-free, buffer overruns that corrupt malloc bookkeeping data, and use of freed memory. The mcheck() function must be called before the first call to malloc(). Linking the program with -lmcheck inserts an implicit call to mcheck(NULL) before the first allocation, which is easier than modifying source code. The mprobe() function returns a status indicating the block's state: MCHECK_OK (no inconsistency), MCHECK_HEAD (memory before the block was clobbered), MCHECK_TAIL (memory after the block was clobbered), or MCHECK_FREE (the block was freed twice).
参数
- mcheck _abortfunc_
- Install debugging hooks for malloc. Pass NULL to use the default handler that prints a message and calls abort(). Must be called before the first malloc().
- mcheck_pedantic _abortfunc_
- Like mcheck(), but performs consistency checks on all allocated blocks on every call to malloc(), realloc(), or free(). Very slow.
- mcheck_check_all
- Trigger an immediate consistency check on all allocated blocks. Only effective if mcheck() was called first.
- mprobe _ptr_
- Explicitly check the block pointed to by ptr for consistency.
FAQ
What is the mcheck command used for?
The mcheck family of functions installs debugging hooks for the malloc(3) memory allocation functions in glibc. These hooks perform consistency checks on the heap, detecting common errors such as double-free, buffer overruns that corrupt malloc bookkeeping data, and use of freed memory. The mcheck() function must be called before the first call to malloc(). Linking the program with -lmcheck inserts an implicit call to mcheck(NULL) before the first allocation, which is easier than modifying source code. The mprobe() function returns a status indicating the block's state: MCHECK_OK (no inconsistency), MCHECK_HEAD (memory before the block was clobbered), MCHECK_TAIL (memory after the block was clobbered), or MCHECK_FREE (the block was freed twice).
How do I run a basic mcheck example?
Run `mcheck(NULL)` in a terminal, then adjust file names, paths, flags, or remote targets for your system.
What does mcheck _abortfunc_ do in mcheck?
Install debugging hooks for malloc. Pass NULL to use the default handler that prints a message and calls abort(). Must be called before the first malloc().