Imported from ../bash-3.0.tar.gz.
[platform/upstream/bash.git] / lib / malloc / trace.c
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
26 #include "imalloc.h"
27
28 extern int malloc_trace;
29
30 static int _mtrace_verbose = 0;
31
32 extern FILE *_imalloc_fopen __P((char *, char *, char *, char *, size_t));
33
34 #ifdef MALLOC_TRACE
35
36 FILE *_mtrace_fp = NULL;
37 extern char _malloc_trace_buckets[];
38
39 void
40 mtrace_alloc (tag, mem, size, file, line)
41      const char *tag;
42      PTR_T mem;
43      size_t size;
44      const char *file;
45      int line;
46 {
47   if (_mtrace_fp == NULL)
48     _mtrace_fp = stderr;
49
50   if (_mtrace_verbose)
51     fprintf (_mtrace_fp, "alloc: %s: %p (%d bytes) from '%s:%d'\n",
52                 tag, mem, size, file ? file : "unknown", line);
53   else
54     fprintf (_mtrace_fp, "alloc:%p:%d:%s:%d\n",
55                 mem, size, file ? file : "unknown", line);
56 }
57
58 void
59 mtrace_free (mem, size, file, line)
60      PTR_T mem;
61      int size;
62      const char *file;
63      int line;
64 {
65   if (_mtrace_fp == NULL)
66     _mtrace_fp = stderr;
67
68   if (_mtrace_verbose)
69     fprintf (_mtrace_fp, "free: %p (%d bytes) from '%s:%d'\n",
70                 mem, size, file ? file : "unknown", line);
71   else
72     fprintf (_mtrace_fp, "free:%p:%d:%s:%d\n",
73                 mem, size, file ? file : "unknown", line);
74 }
75 #endif /* MALLOC_TRACE */
76
77 int
78 malloc_set_trace (n)
79      int n;
80 {
81   int old;
82
83   old = malloc_trace;
84   malloc_trace = n;
85   _mtrace_verbose = (n > 1);
86   return old;
87 }
88
89 void
90 malloc_set_tracefp (fp)
91      FILE *fp;
92 {
93 #ifdef MALLOC_TRACE
94   _mtrace_fp = fp ? fp : stderr;
95 #endif
96 }
97
98 void
99 malloc_trace_bin (n)
100      int n;
101 {
102 #ifdef MALLOC_TRACE
103   _malloc_trace_buckets[n] = 1;
104 #endif
105 }
106
107 #define TRACEROOT "/var/tmp/maltrace/trace."
108
109 void
110 malloc_set_tracefn (s, fn)
111      char *s;
112      char *fn;
113 {
114 #ifdef MALLOC_TRACE
115   FILE *fp;
116   char defname[sizeof (TRACEROOT) + 64];
117
118   fp = _imalloc_fopen (s, fn, TRACEROOT, defname, sizeof (defname));
119   if (fp)
120     malloc_set_tracefp (fp);
121 #endif
122 }