1 
2 /***************************************************************************
3  *                    __            __ _ ___________                       *
4  *                    \ \          / /| |____   ____|                      *
5  *                     \ \        / / | |    | |                           *
6  *                      \ \  /\  / /  | |    | |                           *
7  *                       \ \/  \/ /   | |    | |                           *
8  *                        \  /\  /    | |    | |                           *
9  *                         \/  \/     |_|    |_|                           *
10  *                                                                         *
11  *                           Wiimms ISO Tools                              *
12  *                         http://wit.wiimm.de/                            *
13  *                                                                         *
14  ***************************************************************************
15  *                                                                         *
16  *   This file is part of the WIT project.                                 *
17  *   Visit http://wit.wiimm.de/ for project details and sources.           *
18  *                                                                         *
19  *   Copyright (c) 2009-2013 by Dirk Clemens <wiimm@wiimm.de>              *
20  *                                                                         *
21  ***************************************************************************
22  *                                                                         *
23  *   This program is free software; you can redistribute it and/or modify  *
24  *   it under the terms of the GNU General Public License as published by  *
25  *   the Free Software Foundation; either version 2 of the License, or     *
26  *   (at your option) any later version.                                   *
27  *                                                                         *
28  *   This program is distributed in the hope that it will be useful,       *
29  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
30  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
31  *   GNU General Public License for more details.                          *
32  *                                                                         *
33  *   See file gpl-2.0.txt or http://www.gnu.org/licenses/gpl-2.0.txt       *
34  *                                                                         *
35  ***************************************************************************/
36 
37 #ifndef SZS_DEBUG_H
38 #define SZS_DEBUG_H 1
39 
40 //
41 ///////////////////////////////////////////////////////////////////////////////
42 ///////////////			DEBUG and TRACING		///////////////
43 ///////////////////////////////////////////////////////////////////////////////
44 //
45 //  -------------------
46 //   DEBUG and TRACING
47 //  -------------------
48 //
49 //  If symbol 'DEBUG' or symbol _DEBUG' is defined, then and only then
50 //  DEBUG and TRACING is enabled.
51 //
52 //  There are to function like macros defined:
53 //
54 //     TRACE ( ccp format, ... );
55 //        Print to console only if TRACING is enabled.
56 //        Ignored when TRACING is disabled.
57 //        It works like the well known printf() function and include flushing.
58 //
59 //      TRACE_IF ( bool condition, ccp format, ... );
60 //        Like TRACE(), but print only if 'condition' is true.
61 //
62 //      TRACELINE
63 //        Print out current line and source.
64 //
65 //      TRACE_SIZEOF ( object_or_type );
66 //        Print out `sizeof(object_or_type)�
67 //
68 //      ASSERT(cond);
69 //	  If condition is false: print info and exit program.
70 //
71 //
72 //  There are more macros with a preceding 'no' defined:
73 //
74 //      noTRACE ( ccp format, ... );
75 //      noTRACE_IF ( bool condition, ccp format, ... );
76 //      noTRACELINE ( bool condition, ccp format, ... );
77 //      noTRACE_SIZEOF ( object_or_type );
78 //      noASSERT(cond);
79 //
80 //      If you add a 'no' before a TRACE-Call it is disabled always.
81 //      This makes the usage and disabling of multi lines traces very easy.
82 //
83 ///////////////////////////////////////////////////////////////////////////////
84 ///////////////////////////////////////////////////////////////////////////////
85 
86 #include <stdio.h>
87 #include <stdarg.h>
88 
89 #ifndef WIIMM_BASIC_TYPES
90   #define WIIMM_BASIC_TYPES 1
91   typedef const char *  ccp;
92   typedef unsigned char uchar;
93   typedef unsigned int  uint;
94   typedef unsigned long ulong;
95 #endif
96 
97 ///////////////////////////////////////////////////////////////////////////////
98 
99 #if defined(RELEASE)
100     #undef TESTTRACE
101     #undef DEBUG
102     #undef _DEBUG
103     #undef DEBUG_ASSERT
104     #undef _DEBUG_ASSERT
105     #undef WAIT_ENABLED
106 #endif
107 
108 ///////////////////////////////////////////////////////////////////////////////
109 
110 #if defined(IGNORE_DEBUG)
111     #undef DEBUG
112     #undef _DEBUG
113 #endif
114 
115 ///////////////////////////////////////////////////////////////////////////////
116 
117 #undef TRACE
118 #undef TRACE_IF
119 #undef TRACELINE
120 #undef TRACE_SIZEOF
121 
122 ///////////////////////////////////////////////////////////////////////////////
123 
124 #ifdef WIN_SZS_LIB
125   #define __attribute__(x)
126 #endif
127 
128 extern FILE * TRACE_FILE;
129 void TRACE_FUNC ( ccp format, ... )
130 	__attribute__ ((__format__(__printf__,1,2)));
131 void TRACE_ARG_FUNC ( ccp format, va_list arg );
132 
133 void PRINT_FUNC ( ccp format, ... )
134 	__attribute__ ((__format__(__printf__,1,2)));
135 void PRINT_ARG_FUNC ( ccp format, va_list arg );
136 
137 void WAIT_FUNC ( ccp format, ... )
138 	__attribute__ ((__format__(__printf__,1,2)));
139 void WAIT_ARG_FUNC ( ccp format, va_list arg );
140 
141 ///////////////////////////////////////////////////////////////////////////////
142 
143 #undef TEST0 // never defined
144 
145 #ifdef TESTTRACE
146 
147     #undef DEBUG
148     #define DEBUG 1
149 
150     #undef TEST
151     #define TEST 1
152 
153     #undef WAIT_ENABLED
154     #define WAIT_ENABLED 1
155 
156     #undef NEW_FEATURES
157     #define NEW_FEATURES 1
158 
159     #define TRACE_FUNC printf
160     #define PRINT_FUNC printf
161     #define WAIT_FUNC  printf
162 
163     #define TRACE_ARG_FUNC vprintf
164     #define PRINT_ARG_FUNC vprintf
165     #define WAIT_ARG_FUNC  vprintf
166 
167 #endif
168 
169 ///////////////////////////////////////////////////////////////////////////////
170 
171 #if defined(DEBUG) || defined(_DEBUG)
172 
173     #define HAVE_TRACE 1
174 
175     #undef DEBUG
176     #define DEBUG 1
177 
178     #undef DEBUG_ASSERT
179     #define DEBUG_ASSERT 1
180 
181     #define TRACE(...) TRACE_FUNC(__VA_ARGS__)
182     #define TRACE_IF(cond,...) if (cond) TRACE_FUNC(__VA_ARGS__)
183     #define TRACELINE TRACE_FUNC("line #%d @ %s\n",__LINE__,__FILE__)
184     #define TRACE_SIZEOF(t) TRACE_FUNC("%7zd ==%6zx/hex == sizeof(%s)\n",sizeof(t),sizeof(t),#t)
185 
186     #define HEXDUMP(i,a,af,rl,d,c) HexDump(stdout,i,a,af,rl,d,c);
187     #define HEXDUMP16(i,a,d,c) HexDump16(stdout,i,a,d,c);
188     #define TRACE_HEXDUMP(i,a,af,rl,d,c) HexDump(TRACE_FILE,i,a,af,rl,d,c);
189     #define TRACE_HEXDUMP16(i,a,d,c) HexDump16(TRACE_FILE,i,a,d,c);
190 
191 #else
192 
193     #define HAVE_TRACE 0
194 
195     #undef DEBUG
196 
197     #define TRACE(...)
198     #define TRACE_IF(cond,...)
199     #define TRACELINE
200     #define TRACE_SIZEOF(t)
201     #define HEXDUMP(i,a,af,rl,d,c)
202     #define HEXDUMP16(a,i,d,c)
203     #define TRACE_HEXDUMP(i,a,af,rl,d,c)
204     #define TRACE_HEXDUMP16(i,a,d,c)
205 
206 #endif
207 
208 ///////////////////////////////////////////////////////////////////////////////
209 
210 #undef ASSERT
211 #undef ASSERT_MSG
212 
213 #if defined(DEBUG_ASSERT) || defined(_DEBUG_ASSERT)
214 
215     #define HAVE_ASSERT 1
216 
217     #undef DEBUG_ASSERT
218     #define DEBUG_ASSERT 1
219 
220     #define ASSERT(a) if (!(a)) ERROR0(ERR_FATAL,"ASSERTION FAILED !!!\n")
221     #define ASSERT_MSG(a,...) if (!(a)) ERROR0(ERR_FATAL,__VA_ARGS__)
222 
223 #else
224 
225     #define HAVE_ASSERT 0
226 
227     #undef DEBUG_ASSERT
228     #define ASSERT(cond)
229     #define ASSERT_MSG(a,...)
230 
231 #endif
232 
233 ///////////////////////////////////////////////////////////////////////////////
234 
235 #undef PRINT
236 #undef PRINT_IF
237 #undef BINGO
238 #undef HAVE_PRINT
239 
240 #undef HAVE_PRINT0	// always false
241 #define HAVE_PRINT0 0
242 
243 #if defined(DEBUG) && defined(TEST)
244 
245     #define HAVE_PRINT 1
246 
247     #define PRINT(...) PRINT_FUNC(__VA_ARGS__)
248     #define PRINT_IF(cond,...) if (cond) PRINT_FUNC(__VA_ARGS__)
249     #define BINGO PRINT_FUNC("BINGO! %s() #%d @ %s\n",__FUNCTION__,__LINE__,__FILE__)
250 
251 #else
252 
253     #define HAVE_PRINT	HAVE_TRACE
254 
255     #define PRINT	TRACE
256     #define PRINT_IF	TRACE_IF
257     #define BINGO	TRACELINE
258 
259 #endif
260 
261 ///////////////////////////////////////////////////////////////////////////////
262 
263 #undef WAIT
264 #undef WAIT_IF
265 #undef HAVE_WAIT
266 
267 #if defined(WAIT_ENABLED)
268 
269     #define HAVE_WAIT 1
270 
271     #define WAIT(...) WAIT_FUNC(__VA_ARGS__)
272     #define WAIT_IF(cond,...) if (cond) WAIT_FUNC(__VA_ARGS__)
273 
274 #else
275 
276     #define HAVE_WAIT 0
277 
278     #define WAIT(...)
279     #define WAIT_IF(cond,...)
280 
281 #endif
282 
283 ///////////////////////////////////////////////////////////////////////////////
284 
285 #undef DASSERT
286 #undef DASSERT_MSG
287 #undef HAVE_DASSERT
288 
289 #if defined(DEBUG) || defined(TEST)
290 
291     #define HAVE_DASSERT 1
292 
293     #define DASSERT ASSERT
294     #define DASSERT_MSG ASSERT_MSG
295 
296 #else
297 
298     #define HAVE_DASSERT 0
299 
300     #define DASSERT(cond)
301     #define DASSERT_MSG(a,...)
302 
303 #endif
304 
305 ///////////////////////////////////////////////////////////////////////////////
306 
307 // prefix 'no' deactivates traces
308 
309 #undef noTRACE
310 #undef noTRACE_IF
311 #undef noTRACELINE
312 #undef noTRACE_SIZEOF
313 #undef noPRINT
314 #undef noPRINT_IF
315 #undef noWAIT
316 #undef noWAIT_IF
317 #undef noASSERT
318 
319 
320 #ifdef TESTTRACE
321 
322     #define noTRACE		TRACE
323     #define noTRACE_IF		TRACE_IF
324     #define noTRACELINE		TRACELINE
325     #define noTRACE_SIZEOF	TRACE_SIZEOF
326     #define noPRINT		PRINT
327     #define noPRINT_IF		PRINT_IF
328     #define noWAIT		WAIT
329     #define noWAIT_IF		WAIT_IF
330     #define noASSERT		ASSERT
331     #define noASSERT_MSG	ASSERT_MSG
332 
333 #else
334 
335     #define noTRACE(...)
336     #define noTRACE_IF(cond,...)
337     #define noTRACELINE
338     #define noTRACE_SIZEOF(t)
339     #define noPRINT(...)
340     #define noPRINT_IF(...)
341     #define noWAIT(...)
342     #define noWAIT_IF(...)
343     #define noASSERT(cond)
344     #define noASSERT_MSG(cond,...)
345 
346 #endif
347 
348 //
349 ///////////////////////////////////////////////////////////////////////////////
350 ///////////////			alloc/free system		///////////////
351 ///////////////////////////////////////////////////////////////////////////////
352 
353 // define TRACE_ALLOC_MODE
354 //
355 //  0: standard malloc without 'out of memory' detection (don't use it!)
356 //  1: standard malloc with 'out of memory' detection
357 //  2: standard malloc with 'out of memory' detection and source identification
358 //  3: like mode #1, but alloc debuging enabled too
359 //  4: like mode #2, but alloc tracing enabled too
360 
361 // define ENABLE_MEM_CHECK (check specific memory areas)
362 //  0: disabled
363 //  1: enable a watch point for a memory location
364 
365 #ifdef TEST
366     #define TRACE_ALLOC_MODE 3
367     #define ENABLE_MEM_CHECK 0
368 #elif DEBUG
369     #define TRACE_ALLOC_MODE 2
370     #define ENABLE_MEM_CHECK 0
371 #else
372     #define TRACE_ALLOC_MODE 2
373     #define ENABLE_MEM_CHECK 0
374 #endif
375 
376 ///////////////////////////////////////////////////////////////////////////////
377 
378 #if ENABLE_MEM_CHECK
379     extern void MemCheckSetup ( const void * ptr, unsigned int size );
380     extern void MemCheck ( ccp func, ccp file, unsigned int line );
381     #define MEM_CHECK_SETUP(p,s) MemCheckSetup(p,s)
382     #define MEM_CHECK MemCheck(__FUNCTION__,__FILE__,__LINE__)
383 #else
384     #define MEM_CHECK_SETUP(p,s)
385     #define MEM_CHECK
386 #endif
387 
388 ///////////////////////////////////////////////////////////////////////////////
389 
390 void my_free ( void * ptr );
391 
392 #if TRACE_ALLOC_MODE > 1
393     void * my_calloc  ( ccp,ccp,uint, size_t nmemb, size_t size );
394     void * my_malloc  ( ccp,ccp,uint, size_t size );
395     void * my_realloc ( ccp,ccp,uint, void * ptr, size_t size );
396     char * my_strdup  ( ccp,ccp,uint, ccp src );
397     char * my_strdup2 ( ccp,ccp,uint, ccp src1, ccp src2 );
398     char * my_strdup3 ( ccp,ccp,uint, ccp src1, ccp src2, ccp src3 );
399     void * my_memdup  ( ccp,ccp,uint, const void * src, size_t copylen );
400 #else
401     void * my_calloc  ( size_t nmemb, size_t size );
402     void * my_malloc  ( size_t size );
403     void * my_realloc ( void * ptr, size_t size );
404     char * my_strdup  ( ccp src );
405     char * my_strdup2 ( ccp src1, ccp src2 );
406     char * my_strdup3 ( ccp src1, ccp src2, ccp src3 );
407     void * my_memdup  ( const void * src, size_t copylen );
408 #endif
409 
410 void   trace_free    ( ccp,ccp,uint, void * ptr );
411 void * trace_calloc  ( ccp,ccp,uint, size_t nmemb, size_t size );
412 void * trace_malloc  ( ccp,ccp,uint, size_t size );
413 void * trace_realloc ( ccp,ccp,uint, void *ptr, size_t size );
414 char * trace_strdup  ( ccp,ccp,uint, ccp src );
415 char * trace_strdup2 ( ccp,ccp,uint, ccp src1, ccp src2 );
416 char * trace_strdup3 ( ccp,ccp,uint, ccp src1, ccp src2, ccp src3 );
417 void * trace_memdup  ( ccp,ccp,uint, const void * src, size_t copylen );
418 
419 #if TRACE_ALLOC_MODE > 2
420     void InitializeTraceAlloc();
421     int  CheckTraceAlloc ( ccp func, ccp file, unsigned int line );
422     void DumpTraceAlloc ( ccp func, ccp file, unsigned int line, FILE * f );
423 #endif
424 
425 ///////////////////////////////////////////////////////////////////////////////
426 
427 #if TRACE_ALLOC_MODE > 2
428     #define FREE(ptr) trace_free(__FUNCTION__,__FILE__,__LINE__,ptr)
429     #define CALLOC(nmemb,size) trace_calloc(__FUNCTION__,__FILE__,__LINE__,nmemb,size)
430     #define MALLOC(size) trace_malloc(__FUNCTION__,__FILE__,__LINE__,size)
431     #define REALLOC(ptr,size) trace_realloc(__FUNCTION__,__FILE__,__LINE__,ptr,size)
432     #define STRDUP(src) trace_strdup(__FUNCTION__,__FILE__,__LINE__,src)
433     #define STRDUP2(src1,src2) trace_strdup2(__FUNCTION__,__FILE__,__LINE__,src1,src2)
434     #define STRDUP3(src1,src2,src3) trace_strdup3(__FUNCTION__,__FILE__,__LINE__,src1,src2,src3)
435     #define MEMDUP(src,size) trace_memdup(__FUNCTION__,__FILE__,__LINE__,src,size)
436     #define INIT_TRACE_ALLOC  InitializeTraceAlloc()
437     #define CHECK_TRACE_ALLOC CheckTraceAlloc(__FUNCTION__,__FILE__,__LINE__)
438     #define DUMP_TRACE_ALLOC(f)  DumpTraceAlloc(__FUNCTION__,__FILE__,__LINE__,f)
439 #elif TRACE_ALLOC_MODE > 1
440     #define FREE(ptr) my_free(ptr)
441     #define CALLOC(nmemb,size) my_calloc(__FUNCTION__,__FILE__,__LINE__,nmemb,size)
442     #define MALLOC(size) my_malloc(__FUNCTION__,__FILE__,__LINE__,size)
443     #define REALLOC(ptr,size) my_realloc(__FUNCTION__,__FILE__,__LINE__,ptr,size)
444     #define STRDUP(src) my_strdup(__FUNCTION__,__FILE__,__LINE__,src)
445     #define STRDUP2(src1,src2) my_strdup2(__FUNCTION__,__FILE__,__LINE__,src1,src2)
446     #define STRDUP3(src1,src2,src3) my_strdup2(__FUNCTION__,__FILE__,__LINE__,src1,src2,src3)
447     #define MEMDUP(src,size) my_memdup(__FUNCTION__,__FILE__,__LINE__,src,size)
448     #define INIT_TRACE_ALLOC
449     #define CHECK_TRACE_ALLOC
450     #define DUMP_TRACE_ALLOC(f)
451 #else
452     #define FREE(ptr) my_free(ptr)
453     #define CALLOC(nmemb,size) my_calloc(nmemb,size)
454     #define MALLOC(size) my_malloc(size)
455     #define REALLOC(ptr,size) my_realloc(ptr,size)
456     #define STRDUP(src) my_strdup(src)
457     #define STRDUP2(src1,src2) my_strdup2(src1,src2)
458     #define STRDUP3(src1,src2,src3) my_strdup2(src1,src2,src3)
459     #define MEMDUP(src,size) my_memdup(src,size)
460     #define INIT_TRACE_ALLOC
461     #define CHECK_TRACE_ALLOC
462     #define DUMP_TRACE_ALLOC(f)
463 #endif
464 
465 #ifndef WIIMM_DEBUG_C
466     #define free	do_not_use_free
467     #define calloc	do_not_use_calloc
468     #define malloc	do_not_use_malloc
469     #define realloc	do_not_use_realloc
470     #undef  strdup
471     #define strdup	do_not_use_strdup
472 #endif
473 
474 //
475 ///////////////////////////////////////////////////////////////////////////////
476 ///////////////			    END				///////////////
477 ///////////////////////////////////////////////////////////////////////////////
478 
479 #endif // SZS_DEBUG_H
480