aboutsummaryrefslogtreecommitdiff
path: root/src/libparsec/debug.cpp
blob: 856900d1c70e91cb4db950c67bd3c22e775ab708 (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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
/*
 * PARSEC - Debug Support Code
 *
 * $Author: uberlinuxguy $ - $Date: 2004/09/15 12:25:39 $
 *
 * Orginally written by:
 *   Copyright (c) Markus Hadwiger     <msh@parsec.org>   1996-2000
 *
 *  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
 */ 

// C library
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>

// global compilation options
#include "config.h"

// for cleanup function
#include "gd_type.h"
#ifdef PARSEC_SERVER
	#include "sys_err_sv.h"
#else
	#include "sys_err.h"
	#include "sys_glob.h"
#endif


// local module header
#include "debug.h"


// flags
//#define REGISTER_MCBDUMP_COMMAND
//#define WRITE_MALLOC_FREE_LOG



// log file names -------------------------------------------------------------
//
#ifdef WRITE_MALLOC_FREE_LOG
	static char use_log_name[] = "memuse.log";
#endif

#ifdef REGISTER_MCBDUMP_COMMAND
	static char mcb_log_name[] = "mcbdump.log";
#endif


#ifdef USE_ALIGNED_MEMBLOCKS // -----------------------------------------------


// alignment to enforce for all allocated memory blocks
#define MEMBLOCK_ALIGNMENT_VAL		0x1f		// align to 32 byte boundary
#define MEMBLOCK_ALIGNMENT_MASK		(~MEMBLOCK_ALIGNMENT_VAL)


// malloc wrapper that enforces a guaranteed block alignment ------------------
//
void* _AlignedMalloc( size_t size )
{
	void *temp = HEAP_ALLOC( size + ( MEMBLOCK_ALIGNMENT_VAL + 1 ) * 2 );

	if ( temp == NULL )
		return NULL;

	size_t alignmentpadding = ( ( (size_t)temp + MEMBLOCK_ALIGNMENT_VAL ) & MEMBLOCK_ALIGNMENT_MASK ) - (size_t)temp;

	size_t *alignhead = (size_t *) ( (size_t)temp + alignmentpadding );
	*alignhead = (size_t)temp;

#if defined( WRITE_MALLOC_FREE_LOG ) && !defined( LOG_MEMORY_BLOCKS )

	refframe_t SYSs_GetRefFrameCount();
	refframe_t currefframe = SYSs_GetRefFrameCount();

	FILE *fp = fopen( use_log_name, "a" );
	if ( fp != NULL ) {
		fprintf( fp, "[%03d.%03d.%03d.%03d] A_ALLOC: 0x%08X (%d)\n",
			( ( currefframe >> 24 ) & 0xff ),
			( ( currefframe >> 16 ) & 0xff ),
			( ( currefframe >>  8 ) & 0xff ),
			(   currefframe         & 0xff ),
			(size_t)alignhead + MEMBLOCK_ALIGNMENT_VAL + 1, size );
		fclose( fp );
	}

#endif

	return (void*)( (size_t)alignhead + MEMBLOCK_ALIGNMENT_VAL + 1 );
}


// free wrapper needed if aligned mallocs used --------------------------------
//
void _AlignedFree( void *mem )
{

#if defined( WRITE_MALLOC_FREE_LOG ) && !defined( LOG_MEMORY_BLOCKS )

	refframe_t SYSs_GetRefFrameCount();
	refframe_t currefframe = SYSs_GetRefFrameCount();

	FILE *fp = fopen( use_log_name, "a" );
	if ( fp != NULL ) {
		fprintf( fp, "[%03d.%03d.%03d.%03d] A_FREE : 0x%08X\n",
			( ( currefframe >> 24 ) & 0xff ),
			( ( currefframe >> 16 ) & 0xff ),
			( ( currefframe >>  8 ) & 0xff ),
			(   currefframe         & 0xff ),
			(size_t)mem );
		fclose( fp );
	}

#endif

	HEAP_FREE( (void*) ( *(size_t*)( (size_t)mem - ( MEMBLOCK_ALIGNMENT_VAL + 1 ) ) ) );
}


// redefine heap functions to enforce alignment -------------------------------
//
#undef HEAP_ALLOC
#undef HEAP_FREE

#define HEAP_ALLOC					_AlignedMalloc
#define HEAP_FREE					_AlignedFree


#endif // USE_ALIGNED_MEMBLOCKS -----------------------------------------------



// memory alloc/free/size counters

unsigned int Num_Allocs   = 0;
unsigned int Num_Frees	  = 0;
unsigned int Dyn_Mem_Size = 0;


// include system debugging functions -----------------------------------------
//
#include "debugsys.h"



#ifdef LOG_MEMORY_BLOCKS // ---------------------------------------------------


// structure for logging allocated memory blocks ------------------------------
//
struct MCB_Log {

	MCB_Log* next;		// next block in loglist
	size_t   size;		// size of memblock (incl. signature)
	void*    address;	// blockaddress (pointer to head-sig)
	void*    caller;	// address of ALLOCMEM call (owner)
};

MCB_Log *McbLogList = NULL;


#define MCB_SIG_SIZE 32
static char mcb_signature[] = "msh MCB BlockCheckerBoundarySig";


// insert new log-block into list ---------------------------------------------
//
void InsertMcbLog( void *address, size_t size, void *caller )
{
	MCB_Log *temp = (MCB_Log *) HEAP_ALLOC( sizeof( MCB_Log ) );
	ASSERT( temp != NULL );

	temp->address = address;
	temp->caller  = caller;
	temp->size	  = size;
	temp->next	  = McbLogList;
	McbLogList	  = temp;
}


// delete log-block from list -------------------------------------------------
//
void DeleteMcbLog( void *address )
{
	MCB_Log *prev = NULL;

	for ( MCB_Log *temp = McbLogList; temp; temp = temp->next ) {

		if ( temp->address == address ) {
			if ( prev == NULL ) {
				McbLogList = temp->next;
				HEAP_FREE( temp );
				return;
			} else {
				prev->next = temp->next;
				HEAP_FREE( temp );
				return;
			}
		}

		prev = temp;
	}

	ASSERT( 0 );
}


// get pointer to log-block ---------------------------------------------------
//
MCB_Log *GetMcbLog( void *address )
{
	for ( MCB_Log *temp = McbLogList; temp; temp = temp->next ) {

		if ( temp->address == address )
			return temp;
	}

//	  ASSERT( 0 );
	return NULL;
}


#endif	// LOG_MEMORY_BLOCKS --------------------------------------------------



#ifdef EXTENSIVE_MEMORY_CHECKS // ---------------------------------------------


#ifndef LOG_MEMORY_BLOCKS
	#error "inconsistent debugging specs."
#endif


// check if ref is a valid base address of any heap block ---------------------
//
void _CheckHeapBaseRef( void *ref )
{
	ref = (void *) ( (char*)ref - MCB_SIG_SIZE );
	ASSERT( GetMcbLog( ref ) != NULL );
}


// check if ref is a valid pointer into any heap block ------------------------
//
void _CheckHeapRef( void *ref )
{
	for ( MCB_Log *temp = McbLogList; temp; temp = temp->next ) {

		if ( ( (unsigned)temp->address <= (unsigned)ref-MCB_SIG_SIZE ) &&
			 ( ((unsigned)temp->address+temp->size-MCB_SIG_SIZE) > (unsigned)ref ) )
			return;
	}

	ASSERT( 0 );
}


// check integrity of all dynamically allocated memory blocks -----------------
//
void _CheckMemIntegrity()
{
	int nummcbs = 0;
	for ( MCB_Log *temp = McbLogList; temp; temp = temp->next, nummcbs++ ) {

		int corrupt1 = ( strcmp( (char*)temp->address, mcb_signature ) != 0 );
		int corrupt2 = ( strcmp( (char*)temp->address+temp->size-MCB_SIG_SIZE, mcb_signature ) != 0 );

		// make breakpoint setting easy
		if ( corrupt1 || corrupt2 ) {
			ASSERT( 0 );
		}
	}

	ASSERT( nummcbs == ( Num_Allocs - Num_Frees ) );
}


#endif // EXTENSIVE_MEMORY_CHECKS ---------------------------------------------



#ifdef LOG_MEMORY_BLOCKS // ---------------------------------------------------


// allocate memory block ------------------------------------------------------
//
void* _LogAllocMem( size_t size )
{
	// first thing: retrieve caller's address
	void *caller;
	GETCALLER( caller )

	ASSERT( size > 0 );

	size += MCB_SIG_SIZE * 2;

	void *temp = HEAP_ALLOC( size );
	ASSERT( temp != NULL );

	Num_Allocs++;
	Dyn_Mem_Size += size;

	// log mem allocation
	InsertMcbLog( temp, size, caller );

	unsigned *filldw = (unsigned *) temp;
	size_t siz4 = size / 4;
	while ( siz4-- > 0 )
		*filldw++ = 0xCCCCCCCC;

	unsigned char *fillb = (unsigned char *) filldw;
	siz4 = size % 4;
	while ( siz4-- > 0 )
		*fillb++ = 0xCC;

	strcpy( (char*)temp, mcb_signature );
	strcpy( (char*)temp + size - MCB_SIG_SIZE, mcb_signature );

	return (void *) ( (char*)temp + MCB_SIG_SIZE );
}


// free memory block ----------------------------------------------------------
//
void _LogFreeMem( void *mem )
{
	ASSERT( mem != NULL );

#ifdef CHECK_MEM_INTEGRITY_ON_FREE

#ifndef EXTENSIVE_MEMORY_CHECKS
	#error "inconsistent debugging specs."
#endif

	CHECKMEMINTEGRITY();

#endif

	mem = (void *) ( (char*)mem - MCB_SIG_SIZE );

	MCB_Log *log = GetMcbLog( mem );
	ASSERT( log != NULL );

	size_t size = log->size;

	DeleteMcbLog( mem );

	unsigned *filldw = (unsigned *) mem;
	size_t siz4 = size / 4;
	while ( siz4-- > 0 )
		*filldw++ = 0xCCCCCCCC;

	unsigned char *fillb = (unsigned char *) filldw;
	siz4 = size % 4;
	while ( siz4-- > 0 )
		*fillb++ = 0xCC;

	HEAP_FREE( mem );

	Num_Frees++;
	Dyn_Mem_Size -= size;
}


#endif // LOG_MEMORY_BLOCKS ---------------------------------------------------



#if defined( REGISTER_MCBDUMP_COMMAND ) && defined( LOG_MEMORY_BLOCKS ) // ----


// user command registration
#include "con_com.h"
#include "gd_help.h"
#include "gd_type.h"


// command "mcbdump" for dumping the entire mcb list to a log file ------------
//
PRIVATE
int Cmd_MCBDUMP( char *params )
{
	ASSERT( params != NULL );
	void CON_AddLine( char *text );
	USERCOMMAND_NOPARAM( params );

	refframe_t SYSs_GetRefFrameCount();
	refframe_t currefframe = SYSs_GetRefFrameCount();

	FILE *fp = fopen( mcb_log_name, "a" );
	if ( fp != NULL ) {

		fprintf( fp, "\n[%03d.%03d.%03d.%03d] --------------\n",
			( ( currefframe >> 24 ) & 0xff ),
			( ( currefframe >> 16 ) & 0xff ),
			( ( currefframe >>  8 ) & 0xff ),
			(   currefframe         & 0xff ) );

		int    nummcbs = 0;
		size_t mcbsum  = 0;

		for ( MCB_Log *temp = McbLogList; temp; temp = temp->next, nummcbs++ ) {

			int corrupt1 = ( strcmp( (char*)temp->address, mcb_signature ) != 0 );
			int corrupt2 = ( strcmp( (char*)temp->address+temp->size-MCB_SIG_SIZE, mcb_signature ) != 0 );

			mcbsum += temp->size;

			fprintf( fp, "[%d] adx=0x%08X own=0x%08X siz=%d", nummcbs, (size_t)temp->address, (size_t)temp->caller, temp->size );
			if ( corrupt1 )
				fprintf( fp, " **START CORRUPTED**" );
			if ( corrupt2 )
				fprintf( fp, " **END CORRUPTED**" );
			fprintf( fp, "\n" );
		}
		fprintf( fp, "\n%d bytes in %d entries.\n", mcbsum, nummcbs );

		fclose( fp );
	}

	return TRUE;
}


// register mcb dumping command -----------------------------------------------
//
REGISTER_MODULE( DEBUG )
{
	user_command_s regcom;
	memset( &regcom, 0, sizeof( user_command_s ) );

	regcom.command	 = "mcbdump";
	regcom.numparams = 0;
	regcom.execute	 = Cmd_MCBDUMP;
	regcom.statedump = NULL;
	CON_RegisterUserCommand( &regcom );
}


#endif // REGISTER_MCBDUMP_COMMAND && LOG_MEMORY_BLOCKS -----------------------



// check assertion, abort if assertion failed ---------------------------------
//
#ifdef SYSTEM_LINUX_UNUSED
	#define SYSABORT()		exit( EXIT_FAILURE )	// abort() crashes
#else
	#define SYSABORT()		abort()
#endif


// check assertion, abort if assertion failed ---------------------------------
//
void _SysAssert( const char *file, unsigned line )
{
	static int inSysAssert = FALSE;

	if ( !inSysAssert ) {
		inSysAssert = TRUE;

#if defined ( SYSTEM_TARGET_WINDOWS ) && !defined ( __MINGW32__ )
		__debugbreak();
#endif // SYSTEM_WIN32_UNUSED

		SYSs_CriticalCleanUp();

		fflush( NULL );
		fprintf( stderr, "\nAssertion failed: %s, line %u\n", file, line );
		fflush( stderr );

		// system-dependent handling
		SysAssertCallback( file, line );

		// abort if callback returned
		SYSABORT();
	}
}