henesyinit from p9f0ed2a895 years agoHistory
137 lines · 3.2 KB · Text
RawDownload
.TH DEBUGMALLOC 2.SH NAMEshowleak, malloc, mallocz, free, realloc, calloc, msize, setmalloctag,setrealloctag, getmalloctag, getrealloctag, malloctopoolblock \- debugging memory allocator.SH SYNOPSIS.B$LD $OFILES -ldebugmalloc.PP.B /sys/src/libdebugmalloc/showleak.I logfile[.I v.out].SH DESCRIPTION.I Debugmallocis a variant of the standard memory allocation routines that is intended to help debug .IR malloc -relatedproblems.For function prototypes, see.IR malloc (2).It is invoked by specifying .B -ldebugmallocat link time..PPLike the standard memory allocation routines, .I debugmallocis based on the .IR pool (2)interface; the difference is that it turns on the .BR POOL_PARANOIA ,.BR POOL_ANTAGONISM ,and .BR POOL_LOGGING flags, which cause the pool routines torecheck the whole of their data structures at each call,fill non-zeroed memory with garbage at allocation time,fill memory with garbage at free time,and give the allocator logging information..PPThe effects of .B POOL_PARANOIAand.B POOL_ANTAGONISMare fully defined in .IR pool (2);what is new in.I debugmallocis the logging facility.If the environment variable.B MALLOCFDis set to a positive numbercorresponding to an open file descriptor,a textual log entry is written tothat file descriptor at each call to .IR malloc ,.IR realloc ,or.IR free .On supported architectures, a log line containinga stack trace will be written before the log line for each call.Here is an example log fragment..PP.EX#stack 0001190a 000139be 000116b5 000133d8poolalloc 1a350 8256 = 24d90#stack 0001190a 000139be 000116b5 00013748poolfree 1a350 24d90#stack 0001190a 000139be 000116b5 00013614poolrealloc 1a350 0 24 = 24d90.EE.PPThe log lines that are not stack traces always begin withthe function name, the value of the pool pointer,and then their arguments..I Poolallocand.I poolreallocalso log their returned blocks..PPThis log file can be processed by .I showleak to produce a listing of unfreed memory blocks, sortedby frequency of allocation.A sample listing might look like:.PP.EX15 * 24 bytes = 360 bytes src(0x1190a); src(0x116b5);10 * 68 bytes = 680 bytes src(0x1190a); src(0x116b5);5 * 40 bytes = 200 bytes src(0x1190a); src(0x116b5);.EE.PPEach of the lines ends with a string of commands to give to.IR acid (1) to print the stack at the time the leaked block was allocated.If the optional program file argument is passed to.IR showleak ,it will filter calls to.I mallocand the pool routinesfrom the stack trace..SH EXAMPLE.EX% 8c leak.c% 8l leak.8 -ldebugmalloc% MALLOCFD=3 8.out >[3] mlog% /sys/src/libdebugmalloc/showleak mlog 8.out20 * 9 bytes = 180 bytes src(0x1354); src(0x10ff); src(0x1036);10 * 18 bytes = 180 bytes src(0x1354); src(0x10ff); src(0x1036);% .EE.PPThe setting of the.B POOL_PARANOIAflag slows down the allocator substantially.To use .I debugmallocwithout that setting, add the line.EX	sbrkmem->flags &= ~POOL_PARANOIA;.EEnear the beginning of your program..SH SOURCE.B /sys/src/libdebugmalloc.SH SEE ALSO.IR leak (1),.IR brk (2),.IR malloc (2),.IR pool (2).SH BUGS.PPThe stack trace can include somefalse positives..PPStack traces are only supported on the Intel.IR x86 .