Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 1 | /* trace.c - tracing functions for malloc */ |
| 2 | |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 3 | /* Copyright (C) 2001-2003 Free Software Foundation, Inc. |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 4 | |
| 5 | This file is part of GNU Bash, the Bourne Again SHell. |
| 6 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 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. |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 11 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 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. |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 16 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 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 | |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 21 | #ifdef HAVE_CONFIG_H |
| 22 | # include <config.h> |
| 23 | #endif |
| 24 | |
| 25 | #include <stdio.h> |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 26 | #ifdef HAVE_UNISTD_H |
| 27 | # include <unistd.h> |
| 28 | #endif |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 29 | |
| 30 | #include "imalloc.h" |
| 31 | |
| 32 | extern int malloc_trace; |
| 33 | |
| 34 | static int _mtrace_verbose = 0; |
| 35 | |
| 36 | #ifdef MALLOC_TRACE |
| 37 | |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 38 | extern FILE *_imalloc_fopen __P((char *, char *, char *, char *, size_t)); |
| 39 | |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 40 | FILE *_mtrace_fp = NULL; |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 41 | extern char _malloc_trace_buckets[]; |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 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 |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 82 | malloc_set_trace (n) |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 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 |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 94 | malloc_set_tracefp (fp) |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 95 | FILE *fp; |
| 96 | { |
| 97 | #ifdef MALLOC_TRACE |
| 98 | _mtrace_fp = fp ? fp : stderr; |
| 99 | #endif |
| 100 | } |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 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 | } |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 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 | } |