DEBUGMALLOC(2) DEBUGMALLOC(2)
NAME
showleak, malloc, mallocz, free, realloc, calloc, msize,
setmalloctag, setrealloctag, getmalloctag, getrealloctag,
malloctopoolblock - debugging memory allocator
SYNOPSIS
$LD $OFILES -ldebugmalloc
/sys/src/libdebugmalloc/showleak logfile [ v.out ]
DESCRIPTION
Debugmalloc is a variant of the standard memory allocation
routines that is intended to help debug malloc-related prob-
lems. For function prototypes, see malloc(2). It is invoked
by specifying -ldebugmalloc at link time.
Like the standard memory allocation routines, debugmalloc is
based on the pool(2) interface; the difference is that it
turns on the POOL_PARANOIA, POOL_ANTAGONISM, and
POOL_LOGGING flags, which cause the pool routines to recheck
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.
The effects of POOL_PARANOIA and POOL_ANTAGONISM are fully
defined in pool(2); what is new in debugmalloc is the log-
ging facility. If the environment variable MALLOCFD is set
to a positive number corresponding to an open file descrip-
tor, a textual log entry is written to that file descriptor
at each call to malloc, realloc, or free. On supported
architectures, a log line containing a stack trace will be
written before the log line for each call. Here is an exam-
ple log fragment.
#stack 0001190a 000139be 000116b5 000133d8
poolalloc 1a350 8256 = 24d90
#stack 0001190a 000139be 000116b5 00013748
poolfree 1a350 24d90
#stack 0001190a 000139be 000116b5 00013614
poolrealloc 1a350 0 24 = 24d90
The log lines that are not stack traces always begin with
the function name, the value of the pool pointer, and then
their arguments. Poolalloc and poolrealloc also log their
returned blocks.
This log file can be processed by showleak to produce a
listing of unfreed memory blocks, sorted by frequency of
allocation. A sample listing might look like:
Page 1 Plan 9 (printed 12/22/25)
DEBUGMALLOC(2) DEBUGMALLOC(2)
15 * 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);
Each of the lines ends with a string of commands to give to
acid(1) to print the stack at the time the leaked block was
allocated. If the optional program file argument is passed
to showleak, it will filter calls to malloc and the pool
routines from the stack trace.
EXAMPLE
% 8c leak.c
% 8l leak.8 -ldebugmalloc
% MALLOCFD=3 8.out >[3] mlog
% /sys/src/libdebugmalloc/showleak mlog 8.out
20 * 9 bytes = 180 bytes src(0x1354); src(0x10ff); src(0x1036);
10 * 18 bytes = 180 bytes src(0x1354); src(0x10ff); src(0x1036);
%
The setting of the POOL_PARANOIA flag slows down the alloca-
tor substantially. To use debugmalloc without that setting,
add the line
sbrkmem->flags &= ~POOL_PARANOIA;
near the beginning of your program.
SOURCE
/sys/src/libdebugmalloc
SEE ALSO
leak(1), brk(2), malloc(2), pool(2)
BUGS
The stack trace can include some false positives.
Stack traces are only supported on the Intel x86.
Page 2 Plan 9 (printed 12/22/25)