1mtrace(3) Library Functions Manual mtrace(3)
2
3
4
6 mtrace, muntrace - malloc tracing
7
9 Standard C library (libc, -lc)
10
12 #include <mcheck.h>
13
14 void mtrace(void);
15 void muntrace(void);
16
18 The mtrace() function installs hook functions for the memory-allocation
19 functions (malloc(3), realloc(3) memalign(3), free(3)). These hook
20 functions record tracing information about memory allocation and de‐
21 allocation. The tracing information can be used to discover memory
22 leaks and attempts to free nonallocated memory in a program.
23
24 The muntrace() function disables the hook functions installed by
25 mtrace(), so that tracing information is no longer recorded for the
26 memory-allocation functions. If no hook functions were successfully
27 installed by mtrace(), muntrace() does nothing.
28
29 When mtrace() is called, it checks the value of the environment vari‐
30 able MALLOC_TRACE, which should contain the pathname of a file in which
31 the tracing information is to be recorded. If the pathname is success‐
32 fully opened, it is truncated to zero length.
33
34 If MALLOC_TRACE is not set, or the pathname it specifies is invalid or
35 not writable, then no hook functions are installed, and mtrace() has no
36 effect. In set-user-ID and set-group-ID programs, MALLOC_TRACE is ig‐
37 nored, and mtrace() has no effect.
38
40 For an explanation of the terms used in this section, see at‐
41 tributes(7).
42
43 ┌──────────────────────────────────────────┬───────────────┬───────────┐
44 │Interface │ Attribute │ Value │
45 ├──────────────────────────────────────────┼───────────────┼───────────┤
46 │mtrace(), muntrace() │ Thread safety │ MT-Unsafe │
47 └──────────────────────────────────────────┴───────────────┴───────────┘
48
50 GNU.
51
53 In normal usage, mtrace() is called once at the start of execution of a
54 program, and muntrace() is never called.
55
56 The tracing output produced after a call to mtrace() is textual, but
57 not designed to be human readable. The GNU C library provides a Perl
58 script, mtrace(1), that interprets the trace log and produces human-
59 readable output. For best results, the traced program should be
60 compiled with debugging enabled, so that line-number information is
61 recorded in the executable.
62
63 The tracing performed by mtrace() incurs a performance penalty (if
64 MALLOC_TRACE points to a valid, writable pathname).
65
67 The line-number information produced by mtrace(1) is not always
68 precise: the line number references may refer to the previous or
69 following (nonblank) line of the source code.
70
72 The shell session below demonstrates the use of the mtrace() function
73 and the mtrace(1) command in a program that has memory leaks at two
74 different locations. The demonstration uses the following program:
75
76 $ cat t_mtrace.c
77 #include <mcheck.h>
78 #include <stdio.h>
79 #include <stdlib.h>
80
81 int
82 main(void)
83 {
84 mtrace();
85
86 for (unsigned int j = 0; j < 2; j++)
87 malloc(100); /* Never freed--a memory leak */
88
89 calloc(16, 16); /* Never freed--a memory leak */
90 exit(EXIT_SUCCESS);
91 }
92
93 When we run the program as follows, we see that mtrace() diagnosed mem‐
94 ory leaks at two different locations in the program:
95
96 $ cc -g t_mtrace.c -o t_mtrace
97 $ export MALLOC_TRACE=/tmp/t
98 $ ./t_mtrace
99 $ mtrace ./t_mtrace $MALLOC_TRACE
100 Memory not freed:
101 -----------------
102 Address Size Caller
103 0x084c9378 0x64 at /home/cecilia/t_mtrace.c:12
104 0x084c93e0 0x64 at /home/cecilia/t_mtrace.c:12
105 0x084c9448 0x100 at /home/cecilia/t_mtrace.c:16
106
107 The first two messages about unfreed memory correspond to the two mal‐
108 loc(3) calls inside the for loop. The final message corresponds to the
109 call to calloc(3) (which in turn calls malloc(3)).
110
112 mtrace(1), malloc(3), malloc_hook(3), mcheck(3)
113
114
115
116Linux man-pages 6.05 2023-07-20 mtrace(3)