1/* $KAME: debugrm.c,v 1.6 2001/12/13 16:07:46 sakane Exp $ */ 2 3/* 4 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. Neither the name of the project nor the names of its contributors 16 * may be used to endorse or promote products derived from this software 17 * without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 */ 31 32#define NONEED_DRM 33 34#include <sys/types.h> 35#include <sys/param.h> 36 37#include <stdio.h> 38#include <string.h> 39#include <stdlib.h> 40#include <time.h> 41#include <err.h> 42 43#include "debugrm.h" 44 45#include "vmbuf.h" /* need to mask vmbuf.c functions. */ 46 47#define DRMLISTSIZE 1024 48 49struct drm_list_t { 50 void *ptr; 51 char msg[100]; 52}; 53static struct drm_list_t drmlist[DRMLISTSIZE]; 54 55static int drm_unknown; 56 57static void DRM_add __P((void *, char *)); 58static void DRM_del __P((void *)); 59static void DRM_setmsg __P((char *, int, void *, int, char *, int, char *)); 60 61void 62DRM_init() 63{ 64 int i; 65 drm_unknown = 0; 66 for (i = 0; i < sizeof(drmlist)/sizeof(drmlist[0]); i++) 67 drmlist[i].ptr = 0; 68} 69 70void 71DRM_dump() 72{ 73 FILE *fp; 74 int i; 75 76 fp = fopen(DRMDUMPFILE, "w"); 77 if (fp == NULL) 78 err(1, "fopen"); 79 fprintf(fp, "drm_unknown=%d\n", drm_unknown); 80 for (i = 0; i < sizeof(drmlist)/sizeof(drmlist[0]); i++) { 81 if (drmlist[i].ptr) 82 fprintf(fp, "%s\n", drmlist[i].msg); 83 } 84 fclose(fp); 85} 86 87static void 88DRM_add(p, msg) 89 void *p; 90 char *msg; 91{ 92 int i; 93 for (i = 0; i < sizeof(drmlist)/sizeof(drmlist[0]); i++) { 94 if (!drmlist[i].ptr) { 95 drmlist[i].ptr = p; 96 strlcpy(drmlist[i].msg, msg, sizeof(drmlist[i].msg)); 97 return; 98 } 99 } 100} 101 102static void 103DRM_del(p) 104 void *p; 105{ 106 int i; 107 108 if (!p) 109 return; 110 111 for (i = 0; i < sizeof(drmlist)/sizeof(drmlist[0]); i++) { 112 if (drmlist[i].ptr == p) { 113 drmlist[i].ptr = 0; 114 return; 115 } 116 } 117 drm_unknown++; 118} 119 120static void 121DRM_setmsg(buf, buflen, ptr, size, file, line, func) 122 char *buf, *file, *func; 123 int buflen, size, line; 124 void *ptr; 125{ 126 time_t t; 127 struct tm *tm; 128 int len; 129 130 t = time(NULL); 131 tm = localtime(&t); 132 len = strftime(buf, buflen, "%Y/%m/%d:%T ", tm); 133 134 snprintf(buf + len, buflen - len, "%p %6d %s:%d:%s", 135 ptr, size, file , line, func); 136} 137 138void * 139DRM_malloc(file, line, func, size) 140 char *file, *func; 141 int line; 142 size_t size; 143{ 144 void *p; 145 146 p = malloc(size); 147 if (p) { 148 char buf[1024]; 149 DRM_setmsg(buf, sizeof(buf), p, size, file, line, func); 150 DRM_add(p, buf); 151 } 152 153 return p; 154} 155 156void * 157DRM_calloc(file, line, func, number, size) 158 char *file, *func; 159 int line; 160 size_t number, size; 161{ 162 void *p; 163 164 p = calloc(number, size); 165 if (p) { 166 char buf[1024]; 167 DRM_setmsg(buf, sizeof(buf), p, number * size, file, line, func); 168 DRM_add(p, buf); 169 } 170 return p; 171} 172 173void * 174DRM_realloc(file, line, func, ptr, size) 175 char *file, *func; 176 int line; 177 void *ptr; 178 size_t size; 179{ 180 void *p; 181 182 p = realloc(ptr, size); 183 if (p) { 184 char buf[1024]; 185 if (ptr && p != ptr) { 186 DRM_del(ptr); 187 DRM_setmsg(buf, sizeof(buf), p, size, file, line, func); 188 DRM_add(p, buf); 189 } 190 } 191 192 return p; 193} 194 195void 196DRM_free(file, line, func, ptr) 197 char *file, *func; 198 int line; 199 void *ptr; 200{ 201 DRM_del(ptr); 202 free(ptr); 203} 204 205/* 206 * mask vmbuf.c functions. 207 */ 208void * 209DRM_vmalloc(file, line, func, size) 210 char *file, *func; 211 int line; 212 size_t size; 213{ 214 void *p; 215 216 p = vmalloc(size); 217 if (p) { 218 char buf[1024]; 219 DRM_setmsg(buf, sizeof(buf), p, size, file, line, func); 220 DRM_add(p, buf); 221 } 222 223 return p; 224} 225 226void * 227DRM_vrealloc(file, line, func, ptr, size) 228 char *file, *func; 229 int line; 230 void *ptr; 231 size_t size; 232{ 233 void *p; 234 235 p = vrealloc(ptr, size); 236 if (p) { 237 char buf[1024]; 238 if (ptr && p != ptr) { 239 DRM_del(ptr); 240 DRM_setmsg(buf, sizeof(buf), p, size, file, line, func); 241 DRM_add(p, buf); 242 } 243 } 244 245 return p; 246} 247 248void 249DRM_vfree(file, line, func, ptr) 250 char *file, *func; 251 int line; 252 void *ptr; 253{ 254 DRM_del(ptr); 255 vfree(ptr); 256} 257 258void * 259DRM_vdup(file, line, func, ptr) 260 char *file, *func; 261 int line; 262 void *ptr; 263{ 264 void *p; 265 266 p = vdup(ptr); 267 if (p) { 268 char buf[1024]; 269 DRM_setmsg(buf, sizeof(buf), p, 0, file, line, func); 270 DRM_add(p, buf); 271 } 272 273 return p; 274} 275