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 de‐
19 allocation. The tracing information can be used to discover memory
20 leaks 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 ig‐
35 nored, and mtrace() has no effect.
36
38 For an explanation of the terms used in this section, see at‐
39 tributes(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 mtrace();
82
83 for (int j = 0; j < 2; j++)
84 malloc(100); /* Never freed--a memory leak */
85
86 calloc(16, 16); /* Never freed--a memory leak */
87 exit(EXIT_SUCCESS);
88 }
89
90 When we run the program as follows, we see that mtrace() diagnosed mem‐
91 ory leaks at two different locations in the program:
92
93 $ cc -g t_mtrace.c -o t_mtrace
94 $ export MALLOC_TRACE=/tmp/t
95 $ ./t_mtrace
96 $ mtrace ./t_mtrace $MALLOC_TRACE
97 Memory not freed:
98 -----------------
99 Address Size Caller
100 0x084c9378 0x64 at /home/cecilia/t_mtrace.c:12
101 0x084c93e0 0x64 at /home/cecilia/t_mtrace.c:12
102 0x084c9448 0x100 at /home/cecilia/t_mtrace.c:16
103
104 The first two messages about unfreed memory correspond to the two mal‐
105 loc(3) calls inside the for loop. The final message corresponds to the
106 call to calloc(3) (which in turn calls malloc(3)).
107
109 mtrace(1), malloc(3), malloc_hook(3), mcheck(3)
110
112 This page is part of release 5.10 of the Linux man-pages project. A
113 description of the project, information about reporting bugs, and the
114 latest version of this page, can be found at
115 https://www.kernel.org/doc/man-pages/.
116
117
118
119GNU 2020-11-01 MTRACE(3)