summaryrefslogtreecommitdiffstats
path: root/mandoc_dbg.c
blob: 647ebf7daf58fd5c646dea32d924add832bdd5da (plain) (blame)
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
/* $Id$ */
/*
 * Copyright (c) 2021, 2022 Ingo Schwarze <schwarze@openbsd.org>
 *
 * 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 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.
 */
#include "config.h"

#include <sys/types.h>

#if HAVE_ERR
#include <err.h>
#endif
#include <stdarg.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#if HAVE_OHASH
#include <ohash.h>
#else
#include "compat_ohash.h"
#endif

#define DEBUG_NODEF 1
#include "mandoc_aux.h"
#include "mandoc.h"

/* Store information about one allocation. */
struct dhash_entry {
	const char	*file;
	int		 line;
	const char	*func;
	size_t		 num;
	size_t		 size;
	void		*ptr;
};

/* Store information about all allocations. */
static struct ohash	  dhash_table;
static FILE		 *dhash_fp;
static int		  dhash_aflag;
static int		  dhash_fflag;
static int		  dhash_lflag;
static int		  dhash_nflag;
static int		  dhash_sflag;

static	void		 *dhash_alloc(size_t, void *);
static	void		 *dhash_calloc(size_t, size_t, void *);
static	void		  dhash_free(void *, void *);
static	unsigned int	  dhash_slot(void *);
static	void		  dhash_register(const char *, int, const char *,
				size_t, size_t, void *, const char *);
static	void		  dhash_print(struct dhash_entry *);
static	void		  dhash_purge(const char *, int, const char *, void *);


/* *** Debugging wrappers of public API functions. ************************ */

int
mandoc_dbg_asprintf(const char *file, int line,
    char **dest, const char *fmt, ...)
{
	va_list	 ap;
	int	 ret;

	va_start(ap, fmt);
	ret = vasprintf(dest, fmt, ap);
	va_end(ap);

	if (ret == -1)
		err((int)MANDOCLEVEL_SYSERR, NULL);

	dhash_register(file, line, "asprintf", 1, strlen(*dest) + 1,
	    *dest, *dest);

	return ret;
}

void *
mandoc_dbg_calloc(size_t num, size_t size, const char *file, int line)
{
	void *ptr = mandoc_calloc(num, size);
	dhash_register(file, line, "calloc", num, size, ptr, NULL);
	return ptr;
}

void *
mandoc_dbg_malloc(size_t size, const char *file, int line)
{
	void *ptr = mandoc_malloc(size);
	dhash_register(file, line, "malloc", 1, size, ptr, NULL);
	return ptr;
}

void *
mandoc_dbg_realloc(void *ptr, size_t size, const char *file, int line)
{
	dhash_purge(file, line, "realloc", ptr);
	ptr = mandoc_realloc(ptr, size);
	dhash_register(file, line, "realloc", 1, size, ptr, NULL);
	return ptr;
}

void *
mandoc_dbg_reallocarray(void *ptr, size_t num, size_t size,
    const char *file, int line)
{
	dhash_purge(file, line, "reallocarray", ptr);
	ptr = mandoc_reallocarray(ptr, num, size);
	dhash_register(file, line, "reallocarray", num, size, ptr, NULL);
	return ptr;
}

void *
mandoc_dbg_recallocarray(void *ptr, size_t oldnum, size_t num, size_t size,
    const char *file, int line)
{
	dhash_purge(file, line, "recallocarray", ptr);
	ptr = mandoc_recallocarray(ptr, oldnum, num, size);
	dhash_register(file, line, "recallocarray", num, size, ptr, NULL);
	return ptr;
}

char *
mandoc_dbg_strdup(const char *ptr, const char *file, int line)
{
	char *p = mandoc_strdup(ptr);
	dhash_register(file, line, "strdup", 1, strlen(p) + 1, p, ptr);
	return p;
}

char *
mandoc_dbg_strndup(const char *ptr, size_t sz, const char *file, int line)
{
	char *p = mandoc_strndup(ptr, sz);
	dhash_register(file, line, "strndup", 1, strlen(p) + 1, p, NULL);
	return p;
}

void
mandoc_dbg_free(void *ptr, const char *file, int line)
{
	dhash_purge(file, line, "free", ptr);
	free(ptr);
}


/* *** Memory allocation callbacks for the debugging table. *************** */

static void *
dhash_alloc(size_t sz, void *arg)
{
        return malloc(sz);
}

static void *
dhash_calloc(size_t n, size_t sz, void *arg)
{
        return calloc(n, sz);
}

static void
dhash_free(void *p, void *arg)
{
        free(p);
}


/* *** Debugging utility functions. *************************************** */

/* Initialize the debugging table, to be called from the top of main(). */
void
mandoc_dbg_init(int argc, char *argv[])
{
	struct ohash_info	  info;
	char			 *dhash_fn;
	int			  argi;

	info.alloc = dhash_alloc;
	info.calloc = dhash_calloc;
	info.free = dhash_free;
	info.data = NULL;
	info.key_offset = offsetof(struct dhash_entry, ptr);
	ohash_init(&dhash_table, 18, &info);

	dhash_fp = stderr;
	if ((dhash_fn = getenv("DEBUG_MEMORY")) == NULL)
		return;

	dhash_sflag = 1;
	for(;; dhash_fn++) {
		switch (*dhash_fn) {
		case '\0':
			break;
		case 'A':
			dhash_aflag = 1;
			continue;
		case 'F':
			dhash_fflag = 1;
			continue;
		case 'L':
			dhash_lflag = 1;
			continue;
		case 'N':
			dhash_nflag = 1;
			continue;
		case '/':
			if ((dhash_fp = fopen(dhash_fn, "a+e")) == NULL)
				err((int)MANDOCLEVEL_SYSERR, "%s", dhash_fn);
			break;
		default:
			errx((int)MANDOCLEVEL_BADARG,
			    "invalid char '%c' in $DEBUG_MEMORY",
			    *dhash_fn);
		}
		break;
	}
	if (setvbuf(dhash_fp, NULL, _IOLBF, 0) != 0)
		err((int)MANDOCLEVEL_SYSERR, "setvbuf");

	fprintf(dhash_fp, "P %d", getpid());
	for (argi = 0; argi < argc; argi++)
		fprintf(dhash_fp, " [%s]", argv[argi]);
	fprintf(dhash_fp, "\n");
}

void
mandoc_dbg_name(const char *name)
{
	if (dhash_nflag)
		fprintf(dhash_fp, "N %s\n", name);
}

/* Hash a pointer and return the table slot currently used for it. */
static unsigned int
dhash_slot(void *ptr)
{
	const char	*ks, *ke;
	uint32_t	 hv;

	ks = (const char *)&ptr;
	ke = ks + sizeof(ptr);
	hv = ohash_interval(ks, &ke);
	return ohash_lookup_memory(&dhash_table, ks, sizeof(ptr), hv);
}

/* Record one allocation in the debugging table. */
static void
dhash_register(const char *file, int line, const char *func,
    size_t num, size_t size, void *ptr, const char *str)
{
	struct dhash_entry	*e;
	unsigned int		 slot;

	slot = dhash_slot(ptr);
	e = ohash_find(&dhash_table, slot);
	if (dhash_aflag || e != NULL) {
		fprintf(dhash_fp, "A %s:%d %s(%zu, %zu) = %p",
		    file, line, func, num, size, ptr);
		if (str != NULL)
			fprintf(dhash_fp, " \"%s\"", str);
		fprintf(dhash_fp, "\n");
	}
	if (e != NULL) {
		dhash_print(e);
		fprintf(dhash_fp, "E duplicate address %p\n", e->ptr);
		errx((int)MANDOCLEVEL_BADARG, "duplicate address %p", e->ptr);
	}

	if ((e = malloc(sizeof(*e))) == NULL)
		err(1, NULL);
	e->file = file;
	e->line = line;
	e->func = func;
	e->num  = num;
	e->size = size;
	e->ptr  = ptr;

	ohash_insert(&dhash_table, slot, e);
}

/* Remove one allocation from the debugging table. */
static void
dhash_purge(const char *file, int line, const char *func, void *ptr)
{
	struct dhash_entry	*e;
	unsigned int		 slot;

	if (ptr == NULL)
		return;

	if (dhash_fflag)
		fprintf(dhash_fp, "F %s:%d %s(%p)\n", file, line, func, ptr);

	slot = dhash_slot(ptr);
	e = ohash_remove(&dhash_table, slot);
	free(e);
}

/* Pretty-print information about one allocation. */
static void
dhash_print(struct dhash_entry *e)
{
	fprintf(dhash_fp, "L %s:%d %s(%zu, %zu) = %p\n",
	    e->file, e->line, e->func, e->num, e->size, e->ptr);
}

/* Pretty-print information about all active allocations. */
void
mandoc_dbg_finish(void)
{
	struct dhash_entry	*e;
	unsigned int		 errcount, slot;

	errcount = ohash_entries(&dhash_table);
	e = ohash_first(&dhash_table, &slot);
	while (e != NULL) {
		if (dhash_lflag)
			dhash_print(e);
		free(e);
		e = ohash_next(&dhash_table, &slot);
	}
	ohash_delete(&dhash_table);
	if (dhash_sflag)
		fprintf(dhash_fp, "S %u memory leaks found\n", errcount);
	if (dhash_fp != stderr)
		fclose(dhash_fp);
}