1 | /* trace.c - tracing functions for malloc */
|
---|
2 |
|
---|
3 | /* Copyright (C) 2001-2003 Free Software Foundation, Inc.
|
---|
4 |
|
---|
5 | This file is part of GNU Bash, the Bourne Again SHell.
|
---|
6 |
|
---|
7 | Bash is free software; you can redistribute it and/or modify it under
|
---|
8 | the terms of the GNU General Public License as published by the Free
|
---|
9 | Software Foundation; either version 2, or (at your option) any later
|
---|
10 | version.
|
---|
11 |
|
---|
12 | Bash is distributed in the hope that it will be useful, but WITHOUT ANY
|
---|
13 | WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
---|
14 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
---|
15 | for more details.
|
---|
16 |
|
---|
17 | You should have received a copy of the GNU General Public License along
|
---|
18 | with Bash; see the file COPYING. If not, write to the Free Software
|
---|
19 | Foundation, 59 Temple Place, Suite 330, Boston, MA 02111 USA. */
|
---|
20 | #ifdef HAVE_CONFIG_H
|
---|
21 | # include <config.h>
|
---|
22 | #endif
|
---|
23 |
|
---|
24 | #include <stdio.h>
|
---|
25 | #ifdef HAVE_UNISTD_H
|
---|
26 | # include <unistd.h>
|
---|
27 | #endif
|
---|
28 |
|
---|
29 | #include "imalloc.h"
|
---|
30 |
|
---|
31 | extern int malloc_trace;
|
---|
32 |
|
---|
33 | static int _mtrace_verbose = 0;
|
---|
34 |
|
---|
35 | #ifdef MALLOC_TRACE
|
---|
36 |
|
---|
37 | extern FILE *_imalloc_fopen __P((char *, char *, char *, char *, size_t));
|
---|
38 |
|
---|
39 | FILE *_mtrace_fp = NULL;
|
---|
40 | extern char _malloc_trace_buckets[];
|
---|
41 |
|
---|
42 | void
|
---|
43 | mtrace_alloc (tag, mem, size, file, line)
|
---|
44 | const char *tag;
|
---|
45 | PTR_T mem;
|
---|
46 | size_t size;
|
---|
47 | const char *file;
|
---|
48 | int line;
|
---|
49 | {
|
---|
50 | if (_mtrace_fp == NULL)
|
---|
51 | _mtrace_fp = stderr;
|
---|
52 |
|
---|
53 | if (_mtrace_verbose)
|
---|
54 | fprintf (_mtrace_fp, "alloc: %s: %p (%d bytes) from '%s:%d'\n",
|
---|
55 | tag, mem, size, file ? file : "unknown", line);
|
---|
56 | else
|
---|
57 | fprintf (_mtrace_fp, "alloc:%p:%d:%s:%d\n",
|
---|
58 | mem, size, file ? file : "unknown", line);
|
---|
59 | }
|
---|
60 |
|
---|
61 | void
|
---|
62 | mtrace_free (mem, size, file, line)
|
---|
63 | PTR_T mem;
|
---|
64 | int size;
|
---|
65 | const char *file;
|
---|
66 | int line;
|
---|
67 | {
|
---|
68 | if (_mtrace_fp == NULL)
|
---|
69 | _mtrace_fp = stderr;
|
---|
70 |
|
---|
71 | if (_mtrace_verbose)
|
---|
72 | fprintf (_mtrace_fp, "free: %p (%d bytes) from '%s:%d'\n",
|
---|
73 | mem, size, file ? file : "unknown", line);
|
---|
74 | else
|
---|
75 | fprintf (_mtrace_fp, "free:%p:%d:%s:%d\n",
|
---|
76 | mem, size, file ? file : "unknown", line);
|
---|
77 | }
|
---|
78 | #endif /* MALLOC_TRACE */
|
---|
79 |
|
---|
80 | int
|
---|
81 | malloc_set_trace (n)
|
---|
82 | int n;
|
---|
83 | {
|
---|
84 | int old;
|
---|
85 |
|
---|
86 | old = malloc_trace;
|
---|
87 | malloc_trace = n;
|
---|
88 | _mtrace_verbose = (n > 1);
|
---|
89 | return old;
|
---|
90 | }
|
---|
91 |
|
---|
92 | void
|
---|
93 | malloc_set_tracefp (fp)
|
---|
94 | FILE *fp;
|
---|
95 | {
|
---|
96 | #ifdef MALLOC_TRACE
|
---|
97 | _mtrace_fp = fp ? fp : stderr;
|
---|
98 | #endif
|
---|
99 | }
|
---|
100 |
|
---|
101 | void
|
---|
102 | malloc_trace_bin (n)
|
---|
103 | int n;
|
---|
104 | {
|
---|
105 | #ifdef MALLOC_TRACE
|
---|
106 | _malloc_trace_buckets[n] = 1;
|
---|
107 | #endif
|
---|
108 | }
|
---|
109 |
|
---|
110 | #define TRACEROOT "/var/tmp/maltrace/trace."
|
---|
111 |
|
---|
112 | void
|
---|
113 | malloc_set_tracefn (s, fn)
|
---|
114 | char *s;
|
---|
115 | char *fn;
|
---|
116 | {
|
---|
117 | #ifdef MALLOC_TRACE
|
---|
118 | FILE *fp;
|
---|
119 | char defname[sizeof (TRACEROOT) + 64];
|
---|
120 |
|
---|
121 | fp = _imalloc_fopen (s, fn, TRACEROOT, defname, sizeof (defname));
|
---|
122 | if (fp)
|
---|
123 | malloc_set_tracefp (fp);
|
---|
124 | #endif
|
---|
125 | }
|
---|