1、跟踪函数
- 启用跟踪
void mtrace (void)
- 停止跟踪
void muntrace (void)
2、环境变量 MALLOC_TRACE
- 设定跟踪日志为标准输出
$ export MALLOC_TRACE=/dev/stdout
- 设定跟踪日志为文件
$ export MALLOC_TRACE=mtrace.log
3、测试
①、测试程序 test.c
#include <stdlib.h>
#include <mcheck.h>
int main(int argc, char **argv)
{
mtrace();
void *ptr = malloc(3);
mutrace();
free(ptr);
return 0;
}
②、执行测试
$ gcc test.c -lmcheck -o test
$ ./test
③、测试结果
= Start
@ ./test:[0x80484e6] + 0x8ae83c0 0x3
= End