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