如何启用 mtrace()
(和 MALLOC_TRACE
env)用于没有源的二进制程序?
mtrace 是 glibc 的特性:http://www.gnu.org/s/hello/manual/libc/Allocation-Debugging.html
谢谢
最佳答案
跟踪文件
#include <mcheck.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
void __mtracer_on () __attribute__((constructor));
void __mtracer_off () __attribute__((destructor));
void __mtracer_on ()
{
char *p=getenv("MALLOC_TRACE");
char tracebuf[1023];
if(!p)
p="malloc_trace";
sprintf(tracebuf, "%s.%d", p, getpid());
setenv("MALLOC_TRACE",tracebuf, 1);
atexit(&__mtracer_off);
mtrace();
}
void __mtracer_off ()
{
muntrace();
}
编译
gcc mtrace.c -fPIC -shared -o libmmtrace.so
运行
MALLOC_TRACE=echo LD_PRELOAD=./libmmtrace.so /bin/echo 42
或者
LD_PRELOAD=./libmmtrace.so /bin/echo 42
你没事吧?
关于malloc - 为二进制程序启用 mtrace (MALLOC_TRACE),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2593284/