1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
|
/* $Id: xmalloc-debug.c,v 1.22 2007/07/25 23:30:07 nicm Exp $ */
/*
* Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
* IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
* OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#ifdef DEBUG
#include <sys/types.h>
#include <ctype.h>
#include <dlfcn.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include "fdm.h"
/* Single xmalloc allocated block. */
struct xmalloc_blk {
void *caller;
void *ptr;
size_t size;
SPLAY_ENTRY(xmalloc_blk) entry;
};
/* Splay tree of allocated blocks. */
SPLAY_HEAD(xmalloc_tree, xmalloc_blk);
struct xmalloc_tree xmalloc_tree = SPLAY_INITIALIZER(&xmalloc_tree);
/* Various statistics. */
size_t xmalloc_allocated;
size_t xmalloc_freed;
size_t xmalloc_peak;
u_int xmalloc_frees;
u_int xmalloc_mallocs;
u_int xmalloc_reallocs;
/* Print function. */
#define XMALLOC_PRINT log_debug3
/* Bytes of unallocated blocks and number of allocated blocks to show. */
#define XMALLOC_BYTES 8
#define XMALLOC_LINES 32
/* Macro to update peek usage variable. */
#define XMALLOC_UPDATE() do { \
if (xmalloc_allocated - xmalloc_freed > xmalloc_peak) \
xmalloc_peak = xmalloc_allocated - xmalloc_freed; \
} while (0)
/* Tree functions. */
int xmalloc_cmp(struct xmalloc_blk *, struct xmalloc_blk *);
SPLAY_PROTOTYPE(xmalloc_tree, xmalloc_blk, entry, xmalloc_cmp);
SPLAY_GENERATE(xmalloc_tree, xmalloc_blk, entry, xmalloc_cmp);
/* Compare two blocks. */
int
xmalloc_cmp(struct xmalloc_blk *blk1, struct xmalloc_blk *blk2)
{
uintptr_t ptr1 = (uintptr_t) blk1->ptr;
uintptr_t ptr2 = (uintptr_t) blk2->ptr;
if (ptr1 < ptr2)
return (-1);
if (ptr1 > ptr2)
return (1);
return (0);
}
/* Clear statistics and block list; used to start fresh after fork(2). */
void
xmalloc_clear(void)
{
struct xmalloc_blk *blk;
xmalloc_allocated = 0;
xmalloc_freed = 0;
xmalloc_peak = 0;
xmalloc_frees = 0;
xmalloc_mallocs = 0;
xmalloc_reallocs = 0;
while (!SPLAY_EMPTY(&xmalloc_tree)) {
blk = SPLAY_ROOT(&xmalloc_tree);
SPLAY_REMOVE(xmalloc_tree, &xmalloc_tree, blk);
free(blk);
}
}
/* Print report of statistics and unfreed blocks. */
void
xmalloc_report(pid_t pid, const char *hdr)
{
struct xmalloc_blk *blk;
u_char *iptr;
char buf[4 * XMALLOC_BYTES + 1], *optr;
size_t len;
u_int n;
Dl_info info;
XMALLOC_PRINT("%s: %ld: allocated=%zu, freed=%zu, difference=%zd, "
"peak=%zu", hdr, (long) pid, xmalloc_allocated, xmalloc_freed,
xmalloc_allocated - xmalloc_freed, xmalloc_peak);
XMALLOC_PRINT("%s: %ld: mallocs=%u, reallocs=%u, frees=%u", hdr,
(long) pid, xmalloc_mallocs, xmalloc_reallocs, xmalloc_frees);
n = 0;
SPLAY_FOREACH(blk, xmalloc_tree, &xmalloc_tree) {
n++;
if (n >= XMALLOC_LINES)
continue;
len = blk->size;
if (len > XMALLOC_BYTES)
len = XMALLOC_BYTES;
memset(&info, 0, sizeof info);
if (dladdr(blk->caller, &info) == 0)
info.dli_sname = info.dli_saddr = NULL;
optr = buf;
iptr = blk->ptr;
for (; len > 0; len--) {
if (isascii(*iptr) && !iscntrl(*iptr)) {
*optr++ = *iptr++;
continue;
}
*optr++ = '\\';
*optr++ = '0' + ((*iptr >> 6) & 07);
*optr++ = '0' + ((*iptr >> 3) & 07);
*optr++ = '0' + (*iptr & 07);
iptr++;
}
*optr = '\0';
XMALLOC_PRINT("%s: %ld: %u, %s+0x%02tx: [%p %zu: %s]", hdr,
(long) pid, n, info.dli_sname, ((u_char *) blk->caller) -
((u_char *) info.dli_saddr), blk->ptr, blk->size, buf);
}
XMALLOC_PRINT("%s: %ld: %u unfreed blocks", hdr, (long) pid, n);
}
/* Record a newly created block. */
void
xmalloc_new(void *caller, void *ptr, size_t size)
{
struct xmalloc_blk *blk;
xmalloc_allocated += size;
XMALLOC_UPDATE();
if ((blk = malloc(sizeof *blk)) == NULL)
abort();
blk->ptr = ptr;
blk->size = size;
blk->caller = caller;
SPLAY_INSERT(xmalloc_tree, &xmalloc_tree, blk);
xmalloc_mallocs++;
XMALLOC_UPDATE();
}
/* Record changes to a block. */
void
xmalloc_change(void *caller, void *oldptr, void *newptr, size_t newsize)
{
struct xmalloc_blk *blk, key;
ssize_t change;
if (oldptr == NULL) {
xmalloc_new(caller, newptr, newsize);
return;
}
key.ptr = oldptr;
blk = SPLAY_FIND(xmalloc_tree, &xmalloc_tree, &key);
if (blk == NULL)
return;
change = newsize - blk->size;
if (change > 0)
xmalloc_allocated += change;
else
xmalloc_freed -= change;
XMALLOC_UPDATE();
SPLAY_REMOVE(xmalloc_tree, &xmalloc_tree, blk);
blk->ptr = newptr;
blk->size = newsize;
blk->caller = caller;
SPLAY_INSERT(xmalloc_tree, &xmalloc_tree, blk);
xmalloc_reallocs++;
XMALLOC_UPDATE();
}
/* Record a block free. */
void
xmalloc_free(void *ptr)
{
struct xmalloc_blk *blk, key;
key.ptr = ptr;
blk = SPLAY_FIND(xmalloc_tree, &xmalloc_tree, &key);
if (blk == NULL)
return;
xmalloc_freed += blk->size;
SPLAY_REMOVE(xmalloc_tree, &xmalloc_tree, blk);
free(blk);
xmalloc_frees++;
XMALLOC_UPDATE();
}
#endif /* DEBUG */
|