418509e97a0f750fb34bcfad04259b49b1e58a81
[platform/upstream/bash.git] / lib / malloc / trace.c
1 /* trace.c - tracing functions for malloc */
2
3 /* Copyright (C) 2001 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 #ifdef MALLOC_TRACE
33
34 FILE *_mtrace_fp = NULL;
35
36 void
37 mtrace_alloc (tag, mem, size, file, line)
38      const char *tag;
39      PTR_T mem;
40      size_t size;
41      const char *file;
42      int line;
43 {
44   if (_mtrace_fp == NULL)
45     _mtrace_fp = stderr;
46
47   if (_mtrace_verbose)
48     fprintf (_mtrace_fp, "alloc: %s: %p (%d bytes) from '%s:%d'\n",
49                 tag, mem, size, file ? file : "unknown", line);
50   else
51     fprintf (_mtrace_fp, "alloc:%p:%d:%s:%d\n",
52                 mem, size, file ? file : "unknown", line);
53 }
54
55 void
56 mtrace_free (mem, size, file, line)
57      PTR_T mem;
58      int size;
59      const char *file;
60      int line;
61 {
62   if (_mtrace_fp == NULL)
63     _mtrace_fp = stderr;
64
65   if (_mtrace_verbose)
66     fprintf (_mtrace_fp, "free: %p (%d bytes) from '%s:%d'\n",
67                 mem, size, file ? file : "unknown", line);
68   else
69     fprintf (_mtrace_fp, "free:%p:%d:%s:%d\n",
70                 mem, size, file ? file : "unknown", line);
71 }
72 #endif /* MALLOC_TRACE */
73
74 int
75 malloc_set_trace(n)
76      int n;
77 {
78   int old;
79
80   old = malloc_trace;
81   malloc_trace = n;
82   _mtrace_verbose = (n > 1);
83   return old;
84 }
85
86 void
87 malloc_set_tracefp(fp)
88      FILE *fp;
89 {
90 #ifdef MALLOC_TRACE
91   _mtrace_fp = fp ? fp : stderr;
92 #endif
93 }