blob: 6d93f99c9449f0e44df2566eb7f3a2f4527468a4 [file] [log] [blame]
Jari Aaltof73dda02001-11-13 17:56:06 +00001/* trace.c - tracing functions for malloc */
2
Jari Aaltob80f6442004-07-27 13:29:18 +00003/* Copyright (C) 2001-2003 Free Software Foundation, Inc.
Jari Aaltof73dda02001-11-13 17:56:06 +00004
5 This file is part of GNU Bash, the Bourne Again SHell.
6
Jari Aalto31859422009-01-12 13:36:28 +00007 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.
Jari Aaltof73dda02001-11-13 17:56:06 +000011
Jari Aalto31859422009-01-12 13:36:28 +000012 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.
Jari Aaltof73dda02001-11-13 17:56:06 +000016
Jari Aalto31859422009-01-12 13:36:28 +000017 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
Jari Aaltof73dda02001-11-13 17:56:06 +000021#ifdef HAVE_CONFIG_H
22# include <config.h>
23#endif
24
25#include <stdio.h>
Jari Aalto95732b42005-12-07 14:08:12 +000026#ifdef HAVE_UNISTD_H
27# include <unistd.h>
28#endif
Jari Aaltof73dda02001-11-13 17:56:06 +000029
30#include "imalloc.h"
31
32extern int malloc_trace;
33
34static int _mtrace_verbose = 0;
35
36#ifdef MALLOC_TRACE
37
Jari Aalto95732b42005-12-07 14:08:12 +000038extern FILE *_imalloc_fopen __P((char *, char *, char *, char *, size_t));
39
Jari Aaltof73dda02001-11-13 17:56:06 +000040FILE *_mtrace_fp = NULL;
Jari Aalto7117c2d2002-07-17 14:10:11 +000041extern char _malloc_trace_buckets[];
Jari Aaltof73dda02001-11-13 17:56:06 +000042
43void
44mtrace_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
62void
63mtrace_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
81int
Jari Aalto7117c2d2002-07-17 14:10:11 +000082malloc_set_trace (n)
Jari Aaltof73dda02001-11-13 17:56:06 +000083 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
93void
Jari Aalto7117c2d2002-07-17 14:10:11 +000094malloc_set_tracefp (fp)
Jari Aaltof73dda02001-11-13 17:56:06 +000095 FILE *fp;
96{
97#ifdef MALLOC_TRACE
98 _mtrace_fp = fp ? fp : stderr;
99#endif
100}
Jari Aalto7117c2d2002-07-17 14:10:11 +0000101
102void
103malloc_trace_bin (n)
104 int n;
105{
106#ifdef MALLOC_TRACE
107 _malloc_trace_buckets[n] = 1;
108#endif
109}
Jari Aaltob80f6442004-07-27 13:29:18 +0000110
111#define TRACEROOT "/var/tmp/maltrace/trace."
112
113void
114malloc_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}