aboutsummaryrefslogtreecommitdiff
path: root/src/debug.c
blob: 4c64ff1a7c4b9c827b311e79262edefe75850a85 (plain)
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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
/***************************************************************************
 * Debug functions.
 * Copyright (C) 2003 Joe Wingbermuehle
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 ***************************************************************************/

#include "debug.h"

/***************************************************************************
 * Emit a message.
 ***************************************************************************/
void
Debug(const char *str, ...)
{
#ifdef DEBUG
	va_list ap;
	va_start(ap, str);

	Assert(str);

	fprintf(stderr, "DEBUG: ");
	vfprintf(stderr, str, ap);
	fprintf(stderr, "\n");

	va_end(ap);
#endif
}

#ifdef DEBUG

#define CHECKPOINT_LIST_SIZE 8

typedef struct MemoryType {
	const char *file;
	unsigned int line;
	size_t size;
	void *pointer;
	struct MemoryType *next;
} MemoryType;

static MemoryType *allocations = NULL;

typedef struct ResourceType {
	int resource;
	const char *allocationFiles[CHECKPOINT_LIST_SIZE];
	unsigned int allocationLines[CHECKPOINT_LIST_SIZE];
	const char *releaseFiles[CHECKPOINT_LIST_SIZE];
	unsigned int releaseLines[CHECKPOINT_LIST_SIZE];
	unsigned int allocationOffset;
	unsigned int releaseOffset;
	size_t count;
	struct ResourceType *next;
} ResourceType;

static ResourceType *resources = NULL;

static const char *checkpointFile[CHECKPOINT_LIST_SIZE];
static unsigned int checkpointLine[CHECKPOINT_LIST_SIZE];
static int checkpointOffset;

static void DEBUG_PrintResourceStack(ResourceType *rp);

/***************************************************************************
 * Start the debugger.
 ***************************************************************************/
void
DEBUG_StartDebug(const char *file, unsigned int line)
{
	int x;

	Debug("%s[%u]: debug mode started", file, line);

	checkpointOffset = 0;
	for(x = 0; x < CHECKPOINT_LIST_SIZE; x++) {
		checkpointFile[x] = NULL;
		checkpointLine[x] = 0;
	}

}

/***************************************************************************
 * Stop the debugger.
 ***************************************************************************/
void
DEBUG_StopDebug(const char *file, unsigned int line)
{
	MemoryType *mp;
	ResourceType *rp;
	unsigned int count = 0;

	Debug("%s[%u]: debug mode stopped", file, line);

	if(allocations) {
		Debug("MEMORY: memory leaks follow");
		for(mp = allocations; mp; mp = mp->next) {
			Debug("        %u bytes in %s at line %u",
				mp->size, mp->file, mp->line);
			++count;
		}
		if(count == 1) {
			Debug("MEMORY: 1 memory leak");
		} else {
			Debug("MEMORY: %u memory leaks", count);
		}
	} else {
		Debug("MEMORY: no memory leaks");
	}

	if(resources) {
		for(rp = resources; rp; rp = rp->next) {
			if(rp->count > 0) {
				Debug("RESOURCE: resource %d has reference count %u",
					rp->resource, rp->count);
				DEBUG_PrintResourceStack(rp);
			}
		}
	}

}

/***************************************************************************
 * Print the resource allocation/release stacks for a resource.
 ***************************************************************************/
void
DEBUG_PrintResourceStack(ResourceType *rp)
{
	unsigned int x, offset;

	Debug("          Allocation stack: (oldest)");
	offset = rp->allocationOffset;
	for(x = 0; x < CHECKPOINT_LIST_SIZE; x++) {
		if(rp->allocationFiles[offset]) {
			Debug("             %s line %u", rp->allocationFiles[offset],
				rp->allocationLines[offset]);
		}
		offset = (offset + 1) % CHECKPOINT_LIST_SIZE;
	}
	Debug("          Release stack: (oldest)");
	offset = rp->releaseOffset;
	for(x = 0; x < CHECKPOINT_LIST_SIZE; x++) {
		if(rp->releaseFiles[offset]) {
			Debug("             %s line %u", rp->releaseFiles[offset],
				rp->releaseLines[offset]);
		}
		offset = (offset + 1) % CHECKPOINT_LIST_SIZE;
	}
}

/***************************************************************************
 * Set a checkpoint.
 ***************************************************************************/
void
DEBUG_SetCheckpoint(const char *file, unsigned int line)
{

	checkpointFile[checkpointOffset] = file;
	checkpointLine[checkpointOffset] = line;

	checkpointOffset = (checkpointOffset + 1) % CHECKPOINT_LIST_SIZE;

}

/***************************************************************************
 * Display the location of the last checkpoint.
 ***************************************************************************/
void
DEBUG_ShowCheckpoint()
{
	int x, offset;

	Debug("CHECKPOINT LIST (oldest)");
	offset = checkpointOffset;
	for(x = 0; x < CHECKPOINT_LIST_SIZE; x++) {
		if(checkpointFile[offset]) {
			Debug("   %s[%u]", checkpointFile[offset], checkpointLine[offset]);
		}
		offset = (offset + 1) % CHECKPOINT_LIST_SIZE;
	}
	Debug("END OF CHECKPOINT LIST (most recent)");

}

/***************************************************************************
 * Allocate memory and log.
 ***************************************************************************/
void *DEBUG_Allocate(size_t size, const char *file, unsigned int line) {
	MemoryType *mp;

	if(size <= 0) {
		Debug("MEMORY: %s[%u]: Attempt to allocate %d bytes of memory",
			file, line, size);
	}

	mp = (MemoryType*)malloc(sizeof(MemoryType));
	Assert(mp);

	mp->file = file;
	mp->line = line;
	mp->size = size;

	mp->pointer = malloc(size + sizeof(char));
	if(!mp->pointer) {
		Debug("MEMORY: %s[%u]: Memory allocation failed (%d bytes)",
			file, line, size);
		Assert(0);
	}

	/* Make uninitialized accesses easy to find. */
	memset(mp->pointer, 85, size);

	/* Canary value for buffer overflow checking. */
	((char*)mp->pointer)[size] = 42;

	mp->next = allocations;
	allocations = mp;

	return mp->pointer;
}

/***************************************************************************
 * Reallocate memory and log.
 ***************************************************************************/
void *DEBUG_Reallocate(void *ptr, size_t size, const char *file,
	unsigned int line) {

	MemoryType *mp;

	if(size <= 0) {
		Debug("MEMORY: %s[%u]: Attempt to reallocate %d bytes of memory",
			file, line, size);
	}
	if(!ptr) {
		Debug("MEMORY: %s[%u]: Attempt to reallocate NULL pointer. "
			"Calling Allocate...", file, line);
		return DEBUG_Allocate(size, file, line);
	} else {

		for(mp = allocations; mp; mp = mp->next) {
			if(mp->pointer == ptr) {

				if(((char*)ptr)[mp->size] != 42) {
					Debug("MEMORY: %s[%u]: The canary is dead.", file, line);
				}

				mp->file = file;
				mp->line = line;
				mp->size = size;
				mp->pointer = realloc(ptr, size + sizeof(char));
				if(!mp->pointer) {
					Debug("MEMORY: %s[%u]: Failed to reallocate %d bytes.",
						file, line, size);
					Assert(0);
				}
				((char*)mp->pointer)[size] = 42;
				return mp->pointer;
			}
		}

		Debug("MEMORY: %s[%u]: Attempt to reallocate unallocated pointer",
			file, line);
		mp = malloc(sizeof(MemoryType));
		Assert(mp);
		mp->file = file;
		mp->line = line;
		mp->size = size;
		mp->pointer = malloc(size + sizeof(char));
		if(!mp->pointer) {
			Debug("MEMORY: %s[%u]: Failed to reallocate %d bytes.",
				file, line, size);
			Assert(0);
		}
		memset(mp->pointer, 85, size);
		((char*)mp->pointer)[size] = 42;

		mp->next = allocations;
		allocations = mp;

		return mp->pointer;
	}

}

/***************************************************************************
 * Release memory and log.
 ***************************************************************************/
void
DEBUG_Release(void **ptr, const char *file, unsigned int line)
{
	MemoryType *mp, *last;

	if(!ptr) {
		Debug("MEMORY: %s[%u]: Invalid attempt to release", file, line);
	} else if(!*ptr) {
		Debug("MEMORY: %s[%u]: Attempt to delete NULL pointer",
			file, line);
	} else {
		last = NULL;
		for(mp = allocations; mp; mp = mp->next) {
			if(mp->pointer == *ptr) {
				if(last) {
					last->next = mp->next;
				} else {
					allocations = mp->next;
				}

				if(((char*)*ptr)[mp->size] != 42) {
					Debug("MEMORY: %s[%u]: The canary is dead.", file, line);
				}

				memset(*ptr, 0xFF, mp->size);
				free(mp);
				free(*ptr);
				*ptr = NULL;
				return;
			}
			last = mp;
		}
		Debug("MEMORY: %s[%u]: Attempt to delete unallocated pointer",
			file, line);
		memset(*ptr, 0xFF, mp->size);
		free(*ptr);

		/* This address should cause a segfault or bus error. */
		*ptr = (void*)1;

	}
}

/***************************************************************************
 * Add a resource.
 ***************************************************************************/
void
DEBUG_AllocateResource(int resource, const char *file,
	unsigned int line)
{

	ResourceType *rp;

	for(rp = resources; rp; rp = rp->next) {
		if(rp->resource == resource) {

			rp->allocationFiles[rp->allocationOffset] = file;
			rp->allocationLines[rp->allocationOffset] = line;
			rp->allocationOffset
				= (rp->allocationOffset + 1) % CHECKPOINT_LIST_SIZE;

			++rp->count;
			return;
		}
	}

	rp = malloc(sizeof(ResourceType));
	memset(rp, 0, sizeof(ResourceType));
	rp->resource = resource;
	rp->allocationFiles[0] = file;
	rp->allocationLines[0] = line;
	rp->allocationOffset = 1;
	rp->releaseOffset = 0;
	rp->count = 1;

	rp->next = resources;
	resources = rp;

}

/***************************************************************************
 * Remove a resource.
 ***************************************************************************/
void
DEBUG_ReleaseResource(int resource, const char *file,
	unsigned int line)
{

	ResourceType *rp;

	for(rp = resources; rp; rp = rp->next) {
		if(rp->resource == resource) {
			rp->releaseFiles[rp->releaseOffset] = file;
			rp->releaseLines[rp->releaseOffset] = line;
			rp->releaseOffset = (rp->releaseOffset + 1) % CHECKPOINT_LIST_SIZE;
			if(rp->count <= 0) {
				Debug("RESOURCE: Multiple attempts to release resource %d",
					resource);
				DEBUG_PrintResourceStack(rp);
			} else {
				--rp->count;
			}
			return;
		}
	}

	Debug("RESOURCE: Attempt to release unallocated resource %d",
		resource);
	Debug("          in %s at line %u", file, line);

}

#undef CHECKPOINT_LIST_SIZE

#endif