OpenCBM
debug.h
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version
5  * 2 of the License, or (at your option) any later version.
6  *
7  * Copyright 2004-2008 Spiro Trikaliotis
8  */
9 
18 #ifndef DBG_H
19 #define DBG_H
20 
21 /* Make sure at least one of DBG_KERNELMODE or DBG_USERMODE is defined */
22 #if !defined(DBG_KERNELMODE) && !defined(DBG_USERMODE)
23  #error DBG_KERNELMODE or DBG_USERMODE must be specified!
24 #endif
25 
26 /* Make sure DBG_KERNELMODE and DBG_USERMODE are not defined simultaneously */
27 #if defined(DBG_KERNELMODE) && defined(DBG_USERMODE)
28  #error Only one of DBG_KERNELMODE and DBG_USERMODE must be specified!
29 #endif
30 
31 #ifndef DBG
32  #ifdef _DEBUG
33  #define DBG 1
34  #define __FUNCTION__ ""
35  #endif
36 #endif
37 
44 #if defined(_PREFAST_)
45  #define DBG_I_BREAKPOINT() __debugbreak
46 #elif defined(DBG_KERNELMODE)
47  #define DBG_I_BREAKPOINT() DbgBreakPoint()
48 #else
49  #ifdef __WIN32
50  #define DBG_I_BREAKPOINT() DebugBreak()
51  #else
52  #define DBG_I_BREAKPOINT()
53  #endif
54 #endif
55 
56 #if DBG
57 
58 #ifndef _WIN32
59 
60 #include <stdlib.h>
61 #include <stdio.h>
62 #define OutputDebugString(_xxx) printf( "%s\n", _xxx);
63 
64 #define IN const
65 #define OUT
66 #define STATUS_SUCCESS 0
67 
68 typedef void VOID;
69 typedef char CHAR;
70 typedef unsigned char UCHAR;
71 typedef long LONG;
72 typedef unsigned long ULONG;
73 typedef char* PCHAR;
74 typedef unsigned long* PULONG;
75 
76 typedef int NTSTATUS;
77 typedef void* PDEVICE_EXTENSION;
78 
79 #endif
80 
81  extern void dbg_memdump(const char *Where,
82  const unsigned char *InputBuffer,
83  const unsigned int Count);
84 
105  #define DBG_MEMDUMP(_Where, _Buffer, _Count) dbg_memdump(_Where, _Buffer, _Count)
106 
108  #define DBG_MAX_BUFFERLEN 4096
109 
110 #ifdef DBG_KERNELMODE
111 
113  #define DBG_MAX_BUFFER 32 /* This *MUST* be a power of 2! */
114 
115 #else
116 
118  #define DBG_MAX_BUFFER 1 /* This *MUST* be a power of 2! */
119 
120 #endif // #ifdef DBG_KERNELMODE
121 
122  /* define the various flags for debugging */
123 
125  #define DBGF_BREAK 0x80000000
126 
128  #define DBGF_ENTER 0x40000000
129 
131  #define DBGF_LEAVE 0x20000000
132 
135  #define DBGF_LEAVE_FAILURE 0x10000000
136 
138  #define DBGF_PARAM 0x08000000
139 
140 #ifdef DBG_KERNELMODE
141 
143  #define DBGF_IEC 0x04000000
144 
146  #define DBGF_IRQ 0x02000000
147 
149  #define DBGF_ASSERTIRQL 0x01000000
150 
152  #define DBGF_PORT 0x00800000
153 
155  #define DBGF_THREAD 0x00400000
156 
158  #define DBGF_IRPPATH 0x00200000
159 
161  #define DBGF_IRP 0x00100000
162 
164  #define DBGF_DPC 0x00080000
165 
167  #define DBGF_CABLE 0x00040000
168 
169 
171  #define DBGF_DBGMEMBUF 0x00020000
172 
174  #define DBGF_DBGPRINT 0x00010000
175 
176 #endif // #ifdef DBG_KERNELMODE
177 
179  #define DBGF_PPORT 0x0010
180 
182  #define DBGF_SUCCESS 0x0008
183 
185  #define DBGF_WARNING 0x0004
186 
188  #define DBGF_ERROR 0x0002
189 
191  #define DBGF_ASSERT 0x0001
192 
193 #ifdef DBG_DLL
194 /* Make the linker happy if the DLL is to be generated */
195 int __cdecl main(int argc, char *argv[])
196 {
197 }
198 #endif
199 
200 
202 #ifdef _WIN32
203 #define DBG_I_ASSUME(_x) __assume(_x)
204 #else
205 #define DBG_I_ASSUME(_x)
206 #endif
207 
208 #ifdef DBG_KERNELMODE
209 
211  extern const UCHAR *DebugNtStatus(NTSTATUS Value);
212 
213 #endif // #ifdef DBG_KERNELMODE
214 
215 
216  extern unsigned char DbgBuffer[DBG_MAX_BUFFER][DBG_MAX_BUFFERLEN];
217  extern int DbgBufferPos[];
218  extern unsigned long DbgFlags;
219 
220  extern void DbgOutputIntoBuffer(unsigned long BufferNumber, const char * const Format, ...);
221 
222 
223  /* Some makros for handling the various debug conditions */
224 
225  #define ISDBG_BREAK() (DbgFlags & DBGF_BREAK)
226  #define ISDBG_ENTER() (DbgFlags & DBGF_ENTER)
227  #define ISDBG_LEAVE() (DbgFlags & DBGF_LEAVE)
228  #define ISDBG_LEAVE_FAILURE() (DbgFlags & (DBGF_LEAVE|DBGF_LEAVE_FAILURE))
229  #define ISDBG_PARAM() (DbgFlags & DBGF_PARAM)
230 #ifdef DBG_KERNELMODE
231  #define ISDBG_IEC() (DbgFlags & DBGF_IEC)
232  #define ISDBG_IRQ() (DbgFlags & DBGF_IRQ)
233  #define ISDBG_ASSERTIRQL() (DbgFlags & DBGF_ASSERTIRQL)
234  #define ISDBG_PORT() (DbgFlags & DBGF_PORT)
235  #define ISDBG_THREAD() (DbgFlags & DBGF_THREAD)
236  #define ISDBG_IRPPATH() (DbgFlags & DBGF_IRPPATH)
237  #define ISDBG_IRP() (DbgFlags & DBGF_IRP)
238  #define ISDBG_DPC() (DbgFlags & DBGF_DPC)
239  #define ISDBG_CABLE() (DbgFlags & DBGF_CABLE)
240  #define ISDBG_DBGMEMBUF() (DbgFlags & DBGF_DBGMEMBUF)
241  #define ISDBG_DBGPRINT() (DbgFlags & DBGF_DBGPRINT)
242 #endif // #ifdef DBG_KERNELMODE
243  #define ISDBG_PPORT() (DbgFlags & DBGF_PPORT)
244  #define ISDBG_SUCCESS() (DbgFlags & DBGF_SUCCESS)
245  #define ISDBG_WARN() (DbgFlags & DBGF_WARNING)
246  #define ISDBG_ERROR() (DbgFlags & DBGF_ERROR)
247  #define ISDBG_ASSERT() (DbgFlags & DBGF_ASSERT)
248  #define ISDBG_PANIC() (1)
250 /* Now, abstract from some differences between user-mode and kernel-mode */
251 
252 #ifdef DBG_KERNELMODE
253 
254  extern VOID DbgInit(VOID);
255  extern VOID DbgAllocateMemoryBuffer(VOID);
256  extern VOID DbgFreeMemoryBuffer(VOID);
257  extern VOID DbgOutputMemoryBuffer(const char *String);
258 
260  #define _DBG_PERFORM(_xxx) \
261  if (ISDBG_DBGMEMBUF()) \
262  { \
263  DbgOutputMemoryBuffer(_xxx); \
264  } \
265  if (ISDBG_DBGPRINT()) \
266  { \
267  DbgPrint("%s", _xxx); \
268  }
269 
271  #define FUNC_DEF ULONG DebugBufferNo = 0;
272 
275 #define _DBG_START_ADD { ULONG BufferUsed; \
276  do { \
277  DebugBufferNo = InterlockedIncrement(&DebugBufferCounter) & (DBG_MAX_BUFFER-1); \
278  BufferUsed = InterlockedExchange(&DebugBufferUsed[DebugBufferNo], 1); \
279  } while (BufferUsed); }
280 
283 #define _DBG_END_ADD InterlockedExchange(&DebugBufferUsed[DebugBufferNo], 0);
284 
285  extern LONG DebugBufferCounter;
286  extern LONG DebugBufferUsed[];
287 
290  #define DEBUG_BUFFER_NO DebugBufferNo
291 
292 #else // #ifdef DBG_KERNELMODE
293 
294 #ifdef _WIN32
295  #include <windows.h>
296 #endif
297 
299  #define _DBG_PERFORM(_xxx) OutputDebugString(_xxx);
300 
302  #define FUNC_DEF
303 
306  #define _DBG_START_ADD
307 
310  #define _DBG_END_ADD
311 
314  #define DEBUG_BUFFER_NO 0
315 
316 #endif // #ifdef DBG_KERNELMODE
317 
320  #define DBG_PREFIX DEBUG_BUFFER_NO,
321 
323  #define _DBGO(_xxx) DbgOutputIntoBuffer _xxx
324 
325 #ifdef DBG_KERNELMODE
326 
328  #define _DBG_START() _DBG_START_ADD DbgBufferPos[DEBUG_BUFFER_NO] = 0; DbgOutputIntoBuffer(DBG_PREFIX "%s(%u,%02x)," __FUNCTION__ "(%u): ", DBG_PROGNAME, CbmGetCurrentProcessorNumber(), DebugBufferNo, __LINE__)
329 
330 #else // #if DBG_KERNELMODE
331 
333  #define _DBG_START() _DBG_START_ADD DbgBufferPos[DEBUG_BUFFER_NO] = 0; DbgOutputIntoBuffer(DBG_PREFIX "%s," __FUNCTION__ "(%u): ", DBG_PROGNAME, __LINE__)
334 
335 #endif // #if DBG_KERNELMODE
336 
338  #define _DBG_END() DbgOutputIntoBuffer(DBG_PREFIX "\n"); _DBG_PERFORM(&DbgBuffer[DEBUG_BUFFER_NO][0]) _DBG_END_ADD
339 
341  #define DBGO(_xxx) { _DBG_START(); _DBGO(_xxx); _DBG_END() }
342 
344  #define DBG_BREAKPOINT() { if (ISDBG_BREAK()) { DBG_I_BREAKPOINT(); }; }
345 
347  #define FUNC_ENTER() FUNC_DEF { if (ISDBG_ENTER()) { DBGO(( DBG_PREFIX "Entering %s", __FUNCTION__ )); } }
348 
349  #define FUNC_LEAVE() { if (ISDBG_LEAVE()) { DBGO(( DBG_PREFIX "Leaving %s", __FUNCTION__ )); } return; }
350 
351  #define FUNC_PARAM( _xxx ) { if (ISDBG_PARAM()) { _DBG_START(); _DBGO(( DBG_PREFIX "Parameter for %s: ", __FUNCTION__ )); _DBGO( _xxx ); _DBG_END() } }
352 
354  #define FUNC_LEAVE_BOOL( _xxx ) { const BOOL _OUT_ = _xxx; if (ISDBG_LEAVE()) { DBGO(( DBG_PREFIX "Leaving %s with BOOL=%s", __FUNCTION__, (_OUT_)?"TRUE":"FALSE" )); } return _OUT_; }
355 
356  #define FUNC_LEAVE_BOOLEAN(_xxx ) { const BOOLEAN _OUT_ = _xxx; if (ISDBG_LEAVE()) { DBGO(( DBG_PREFIX "Leaving %s with BOOL=%s", __FUNCTION__, (_OUT_)?"TRUE":"FALSE" )); } return _OUT_; }
357 
358  #define FUNC_LEAVE_INT( _xxx ) { const int _OUT_ = _xxx; if (ISDBG_LEAVE()) { DBGO(( DBG_PREFIX "Leaving %s with INT=%u (%d)", __FUNCTION__, (unsigned int)(_OUT_), (signed int)(_OUT_) )); } return _OUT_; }
359 
360  #define FUNC_LEAVE_UINT( _xxx ) { const unsigned int _OUT_ = _xxx; if (ISDBG_LEAVE()) { DBGO(( DBG_PREFIX "Leaving %s with INT=%u (%d)", __FUNCTION__, _OUT_, _OUT_ )); } return _OUT_; }
361 
362  #define FUNC_LEAVE_USHORT( _xxx ) { const USHORT _OUT_ = _xxx; if (ISDBG_LEAVE()) { DBGO(( DBG_PREFIX "Leaving %s with INT=%u (%d)", __FUNCTION__, (unsigned int)(_OUT_), (signed int)(_OUT_) )); } return _OUT_; }
363 
364  #define FUNC_LEAVE_UCHAR( _xxx ) { const UCHAR _OUT_ = _xxx; if (ISDBG_LEAVE()) { DBGO(( DBG_PREFIX "Leaving %s with UCHAR=%u", __FUNCTION__, (unsigned int)(_OUT_) )); } return _OUT_; }
365 
366  #define FUNC_LEAVE_HANDLE( _xxx ) { const HANDLE _OUT_ = _xxx; if (ISDBG_LEAVE()) { DBGO(( DBG_PREFIX "Leaving %s with HANDLE=%u", __FUNCTION__, (unsigned int)(_OUT_) )); } return _OUT_; }
367 
368  #define FUNC_LEAVE_STRING( _xxx ) { const char * _OUT_ = _xxx; if (ISDBG_LEAVE()) { DBGO(( DBG_PREFIX "Leaving %s with '%s'", __FUNCTION__, (_OUT_) )); } return _xxx; }
369 
370  #define FUNC_LEAVE_ULONG( _xxx ) { const ULONG _OUT_ = _xxx; if (ISDBG_LEAVE()) { DBGO(( DBG_PREFIX "Leaving %s with ULONG=%ul", __FUNCTION__, (ULONG) (_OUT_) )); } return _OUT_; }
371 
372  #define FUNC_LEAVE_LONG( _xxx ) { const LONG _OUT_ = _xxx; if (ISDBG_LEAVE()) { DBGO(( DBG_PREFIX "Leaving %s with LONG=%l", __FUNCTION__, (LONG) (_OUT_) )); } return _OUT_; }
373 
374  #define FUNC_LEAVE_TYPE( _xxx, _TYPE, _FORMAT) { _TYPE _OUT_ = _xxx; if (ISDBG_LEAVE()) { DBGO(( DBG_PREFIX "Leaving %s with " #_TYPE "=" _FORMAT, __FUNCTION__, (_OUT_) )); } return _OUT_; }
375 
376  #define FUNC_LEAVE_PTR( _xxx, _TYPE ) FUNC_LEAVE_TYPE( _xxx, _TYPE, "0x%p")
377 
378  #ifdef DBG_KERNELMODE
379 
380  #define FUNC_LEAVE_NTSTATUS_CONST( _xxx ) { if (ISDBG_LEAVE() || (ISDBG_LEAVE_FAILURE() && (_xxx != STATUS_SUCCESS))) { DBGO(( DBG_PREFIX "Leaving %s with NTSTATUS=%s", __FUNCTION__, #_xxx )); } return _xxx; }
381 
382  #define FUNC_LEAVE_NTSTATUS( _xxx ) { NTSTATUS _OUT_ = _xxx; if (ISDBG_LEAVE() || (ISDBG_LEAVE_FAILURE() && (_xxx != STATUS_SUCCESS))) { DBGO(( DBG_PREFIX "Leaving %s with NTSTATUS=%s", __FUNCTION__, DebugNtStatus(_OUT_) )); } return _OUT_; }
383 
384  #define DBG_IRQL( _xxx ) { if (!(KeGetCurrentIrql() _xxx)) if (ISDBG_ASSERTIRQL()) { DBGO(( DBG_PREFIX "***IRQL ASSERTION FAILED!***: '%s' in %s:%s(%u)", #_xxx, __FILE__, __FUNCTION__, __LINE__ )); DBG_BREAKPOINT(); } }
385  #else
386 
387  #define FUNC_LEAVE_HMODULE( _xxx ) FUNC_LEAVE_TYPE( _xxx, HMODULE, "0x%08x")
388  #endif
389 
391  #define DBG_PPORT( _xxx ) { if (ISDBG_PPORT()){ DBGO( _xxx ); } }
392 
393  #define DBG_SUCCESS( _xxx ) { if (ISDBG_SUCCESS()) { _DBG_START(); _DBGO( _xxx ); _DBGO((DBG_PREFIX " SUCCESS")); _DBG_END(); } }
394 
395  #define DBG_WARN( _xxx ) { if (ISDBG_WARN()) { _DBG_START(); _DBGO(( DBG_PREFIX "Warning: ")); _DBGO( _xxx ); _DBG_END(); } }
396 
397  #define DBG_ERROR( _xxx ) { if (ISDBG_ERROR()) { _DBG_START(); _DBGO(( DBG_PREFIX "***ERROR***: ")); _DBGO( _xxx ); _DBG_END(); } }
398 
399  #define DBG_PANIC( _xxx ) { if (ISDBG_PANIC()) { _DBG_START(); _DBGO(( DBG_PREFIX "***PANIC***: ")); _DBGO( _xxx ); _DBG_END(); } }
400 
401  #define DBG_ASSERT( _xxx ) { if (!(_xxx)) if (ISDBG_ASSERT()) { DBGO(( DBG_PREFIX "***ASSERTION FAILED!***: %s in %s:%s(%u)", #_xxx, __FILE__, __FUNCTION__, __LINE__ )); DBG_BREAKPOINT(); } DBG_I_ASSUME(_xxx); }
402 
403  #define DBG_PRINT( _xxx ) { DBGO( _xxx ); }
404 
406  #define DBG_VERIFY( _xxx ) DBG_ASSERT( _xxx )
407 
409  #define DBGDO( _xxx ) _xxx
410 
411  #ifdef DBG_KERNELMODE
412 
414  #define DBG_INIT() DbgInit()
415 
416 
418  #define DBG_IEC( _xxx ) { if (ISDBG_IEC()) { DBGO( _xxx ); } }
419 
420  #define DBG_IRQ( _xxx ) { if (ISDBG_IRQ()) { DBGO(( DBG_PREFIX _xxx )); } }
421 
422  #define DBG_PORT( _xxx ) { if (ISDBG_PORT()) { _DBG_START(); _DBGO((DBG_PREFIX "Port Command: ")); _DBGO(_xxx); _DBG_END(); } }
423 
424  #define DBG_THREAD( _xxx ) /* { if (ISDBG_THREAD()) DBGO(( _xxx )); } */
425 
426  #define DBG_IRPPATH( _xxx ) { if (ISDBG_IRPPATH()) { DBGO(_xxx); } }
427 
428  #define DBG_IRP( _xxx ) { if (ISDBG_IRP()) { DBGO(( DBG_PREFIX "Got IRP: " #_xxx )); } }
429 
430  #define DBG_DPC( _xxx ) { if (ISDBG_DPC()) { DBGO( _xxx ); } }
431 
432  #define DBG_CABLE( _xxx ) { if (ISDBG_CABLE()) { DBGO( _xxx ); } }
433  #endif
434 
435  extern void cbm_get_debugging_flags(const char * ModuleName);
436 
437 
438 #else // #if DBG
439 
441  #define DBG_MEMDUMP(_Where, _Buffer, _Count)
442 
444  #define DBG_BREAKPOINT
445 
447  #define FUNC_ENTER()
448 
450  #define FUNC_LEAVE( ) return
451 
453  #define FUNC_PARAM( _xxx )
454 
456  #define FUNC_LEAVE_BOOL( _xxx ) return _xxx
457 
459  #define FUNC_LEAVE_BOOLEAN( _xxx ) return _xxx
460 
462  #define FUNC_LEAVE_INT( _xxx ) return _xxx
463 
465  #define FUNC_LEAVE_UINT( _xxx ) return _xxx
466 
468  #define FUNC_LEAVE_USHORT( _xxx ) return _xxx
469 
471  #define FUNC_LEAVE_UCHAR( _xxx ) return _xxx
472 
474  #define FUNC_LEAVE_HANDLE( _xxx ) return _xxx
475 
477  #define FUNC_LEAVE_STRING( _xxx ) return _xxx
478 
480  #define FUNC_LEAVE_ULONG( _xxx ) return _xxx
481 
483  #define FUNC_LEAVE_LONG( _xxx ) return _xxx
484 
486  #define FUNC_LEAVE_TYPE( _xxx, _TYPE, _FORMAT) return _xxx
487 
489  #define FUNC_LEAVE_PTR( _xxx, _yyy ) return _xxx
490 
491  #ifdef DBG_KERNELMODE
492  #define FUNC_LEAVE_NTSTATUS_CONST( _xxx ) return _xxx
494 
496  #define FUNC_LEAVE_NTSTATUS( _xxx ) return _xxx
497  #endif
498 
500  #define DBG_PREFIX
501 
503  #define DBG_PPORT( _xxx )
504 
506  #define DBG_SUCCESS( _xxx )
507 
509  #define DBG_WARN( _xxx )
510 
512  #define DBG_ERROR( _xxx )
513 
515  #define DBG_PANIC( _xxx )
516 
518  #define DBG_ASSERT( _xxx )
519 
521  #define DBG_PRINT( _xxx )
522 
524  #define DBGDO( _xxx )
525 
527  #define DBG_VERIFY( _xxx ) _xxx
528 
529  #ifdef DBG_KERNELMODE
530 
532  #define DBG_INIT()
533 
535  #define DBG_IEC( _xxx )
536 
538  #define DBG_IRQ( _xxx )
539 
541  #define DBG_IRQL( _xxx )
542 
544  #define DBG_PORT( _xxx )
545 
547  #define DBG_THREAD( _xxx )
548 
550  #define DBG_IRPPATH( _xxx )
551 
553  #define DBG_IRP( _xxx )
554 
556  #define DBG_DPC( _xxx )
557 
559  #define DBG_CABLE( _xxx )
560 
561  #else
562 
564  #define FUNC_LEAVE_HMODULE( _xxx ) return _xxx;
565 
566  #endif
567 
568 #endif // #if DBG
569 
571 #define DBG_IRPPATH_PROCESS( _Where_ ) DBG_IRPPATH((DBG_PREFIX "IrpPath: + Processing IRP %08x in " _Where_, (char*)Irp))
572 
574 #define DBG_IRPPATH_COMPLETE( _Where_ ) DBG_IRPPATH((DBG_PREFIX "IrpPath: - Completing IRP %08x in " _Where_ " with ntStatus = %s", (char*)Irp, (char*)DebugNtStatus(ntStatus)))
575 
577 #define DBG_IRPPATH_CANCEL( _Where_ ) DBG_IRPPATH((DBG_PREFIX "IrpPath: - CANCELLING IRP %08x in " _Where_, (char*)Irp))
578 
580 #define DBG_IRPPATH_EXECUTE( _Where_ ) DBG_IRPPATH((DBG_PREFIX "IrpPath: = Executing IRP %08x in " _Where_, (char*)Irp))
581 
582 #endif // #ifndef DBG_H
#define DBG_MAX_BUFFERLEN
Maximum size of a debugging buffer.
Definition: debug.h:108
unsigned long DbgFlags
int DbgBufferPos[]
VOID DbgAllocateMemoryBuffer(VOID)
Get storage area for debugging output.
int ARCH_MAINDECL main(int argc, char **argv)
Initialize the xum1541 device This function tries to find and identify the xum1541 device...
Definition: usbcfg.c:38
VOID DbgOutputMemoryBuffer(const char *String)
Output into the debugging buffer.
void DbgOutputIntoBuffer(unsigned long BufferNumber, const char *const Format,...)
Append something to the DebugBuffer.
void dbg_memdump(const char *Where, const unsigned char *InputBuffer, const unsigned int Count)
output a memory dump to the debugging system
Definition: dbghelp.c:64
const UCHAR * DebugNtStatus(NTSTATUS Value)
Return the description of an NTSTATUS code.
unsigned char DbgBuffer[DBG_MAX_BUFFER][DBG_MAX_BUFFERLEN]
void cbm_get_debugging_flags(const char *ModuleName)
Set the debugging flags.
Definition: registry.c:97
struct DEVICE_EXTENSION_s * PDEVICE_EXTENSION
@@@
Definition: cbm_driver.h:23
VOID DbgInit(VOID)
Initialise debugging system.
#define DBG_MAX_BUFFER
the maximum number of buffers to be used for debugging
Definition: debug.h:118
VOID DbgFreeMemoryBuffer(VOID)
Free storage area for debugging output.