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