mtrace是glibc內提供的工具,其實它的原理很簡單,就是把你程式中malloc()與free()的位址全部下來,最後兩兩配對,殘留下來沒有配對到的就是leak。
1. 安裝glibc-utils
2. 在程式中include header file並在程式最前面call mtrace()
e.g. test.c
#include#include int main(void) { char *p; mtrace(); p = malloc(5); // 要一塊記憶體,但沒有釋放 return 0; }
3. compile
$ gcc -g -o test test.c一定要加-g
4. run program
$ MALLOC_TRACE=output.log ./testMALLOC_TRACE指向output file
5. 抓leak
$ mtrace ./test ./output.log結果:
Memory not freed: ----------------- Address Size Caller 0x0000000000c2d460 0x5 at /tmp/test.c:8很清楚看到test.c第8行allocate 5 byte未釋放
--
不過mtrace算是很陽春的工具,如果是間接allocte記憶體,如call object_new()這種init function,那mtrace就沒辦法表示得那麼清楚了。
e.g. test2.c
#include#include #include int main(void) { GHashTable *ht; mtrace(); ht = g_hash_table_new(NULL, NULL); return 0; }
執行mtrace結果:
Memory not freed: ----------------- Address Size Caller 0x0000000012a24460 0xfc at 0x2b267c24f3b1 0x0000000012a24570 0x1f8 at 0x2b267c24f3b1 0x0000000012a24770 0x1f8 at 0x2b267c24f3b1 0x0000000012a24970 0x7f0 at 0x2b267c24f3b1 0x0000000012a25170 0xc0 at 0x2b267c24f3b1 0x0000000012a25400 0x3f0 at 0x2b267c262ce1 0x0000000012a25800 0x3f0 at 0x2b267c262ce1
這樣的訊息對我們來說沒什麼幫助,這時候可以借助更強大的工具valgrind或heap checker。
Gümüşhane
回覆刪除Karaman
Kocaeli
Sakarya
Samsun
QEC5X
van
回覆刪除erzincan
sivas
ağrı
manisa
Z868S
95335
回覆刪除binance %20 indirim
3372F
回覆刪除https://referanskodunedir.com.tr/
5F634
回覆刪除Şefaatli
Uluborlu
Beşiktaş
Ayvacık
Erciş
Türbanlı İfşa
Mamak
Atabey
Bozdoğan
BB190
回覆刪除ücretli show görüntülü
776D4
回覆刪除görüntülü şov whatsapp numarası
C069329DCF
回覆刪除cam show
whatsapp ücretli show
görüntülü şov
telegram görüntülü şov
görüntülü şov whatsapp numarası
ücretli şov
cam şov
canli cam show
şov
017A2286ED
回覆刪除bufalo içecek
kaldırıcı
vigrande
geciktirici
performans arttırıcı
canli cam show
green temptation
sinegra
görüntülü şov whatsapp numarası
E65CDB1DFC
回覆刪除bufalo içecek
whatsapp ücretli show
sinegra 100 mg
degra 100 mg
canli cam show
cialis
kaldırıcı hap
canli web cam show
skype şov
033EEE0FB9
回覆刪除twitter beğeni satın al