¡¡¡¡Ê¹ÓÃMtraceÀ´µ÷ÊÔ³ÌÐòÓÐ4¸ö»ù±¾µÄ²½Ö裬ÐèÒªÓõ½GNU C º¯Êý¿âÀïÃæµÄһЩ¸¨ÖúµÄº¯Êý¹¦ÄÜ¡£
¡¡¡¡1£® ÔÚÐèÒª¸ú×ٵijÌÐòÖÐÐèÒª°üº¬Í·Îļþ£¬¶øÇÒÔÚmain()º¯ÊýµÄ×ʼ°üº¬Ò»¸öº¯Êýµ÷Óãºmtrace()¡£ÓÉÓÚÔÚmainº¯ÊýµÄ×ͷµ÷ÓÃÁËmtrace()£¬ËùÒԸýø³ÌºóÃæµÄÒ»ÇзÖÅäºÍÊÍ·ÅÄÚ´æµÄ²Ù×÷¶¼¿ÉÒÔÓÉmtraceÀ´¸ú×ٺͷÖÎö¡£
¡¡¡¡2£® ¶¨ÒåÒ»¸ö»·¾³±äÁ¿£¬ÓÃÀ´Ö¸Ê¾Ò»¸öÎļþ¡£¸ÃÎļþÓÃÀ´Êä³ölogÐÅÏ¢¡£ÈçϵÄÀý×Ó£º
¡¡¡¡$export MALLOC_TRACE=mymemory.log
¡¡¡¡3£® Õý³£ÔËÐгÌÐò¡£´Ëʱ³ÌÐòÖеĹØÓÚÄÚ´æ·ÖÅäºÍÊͷŵIJÙ×÷¶¼¿ÉÒԼǼÏÂÀ´¡£
¡¡¡¡4£® È»ºóÓÃmtraceʹÓù¤¾ßÀ´·ÖÎölogÎļþ¡£ÀýÈ磺
¡¡¡¡$mtrace testmem $MALLOC_TRACE
¡¡¡¡ÏÂÃæÊǾßÌåÒ»¸öÀý×Ó£¬´ó¼Ò¿ÉÒÔ¿´Ò»Ï¡£
ÒýÓÃ:[hwang@langchao test]$ cat testmtrace.c
#include
#include
#include
int main()
{
char *hello;
mtrace();
hello = (char*) malloc(20);
sprintf(hello,"
hello world!");
return 1;
}
[hwang@langchao test]$export MALLOC_TRACE=mytrace.log
[hwang@langchao test]$ gcc testmtrace.c -o testmtrace
[hwang@langchao test]$./testmtrace
[hwang@langchao test]$ mtrace testmtrace mytrace.log
Memory not freed:
-----------------
Address Size Caller
0x08049860 0x14 at /usr/src/build/53700-i386/BUILD/glibc-2.2.4/csu/init.c:0