debug.h
Upload User: xhy777
Upload Date: 2007-02-14
Package Size: 24088k
Code Size: 8k
Category:

Windows Kernel

Development Platform:

Visual C++

  1. /*
  2.  * debug.h - Debug macros and their retail translations.
  3.  */
  4. /* Macros
  5.  *********/
  6. /* debug output macros */
  7. /*
  8.  * Do not call SPEW_OUT directly.  Instead, call TRACE_OUT, WARNING_OUT,
  9.  * ERROR_OUT, or FATAL_OUT.
  10.  */
  11. /*
  12.  * call like printf(), but with an extra pair of parentheses:
  13.  *
  14.  * ERROR_OUT(("'%s' too big by %d bytes.", pszName, nExtra));
  15.  */
  16. #ifdef DEBUG
  17. #define SPEW_OUT(args) 
  18.    ((void)(GpcszSpewFile = TEXT(__FILE__), GuSpewLine = __LINE__, SpewOut args, 0))
  19. #define PLAIN_TRACE_OUT(args) 
  20.    (GdwSpewFlags = 0, GuSpewSev = SPEW_TRACE, SPEW_OUT(args))
  21. #define TRACE_OUT(args) 
  22.    (GdwSpewFlags = SPEW_FL_SPEW_PREFIX, GuSpewSev = SPEW_TRACE, SPEW_OUT(args))
  23. #define WARNING_OUT(args) 
  24.    (GdwSpewFlags = SPEW_FL_SPEW_PREFIX | SPEW_FL_SPEW_LOCATION, GuSpewSev = SPEW_WARNING, SPEW_OUT(args))
  25. #define ERROR_OUT(args) 
  26.    (GdwSpewFlags = SPEW_FL_SPEW_PREFIX | SPEW_FL_SPEW_LOCATION, GuSpewSev = SPEW_ERROR, SPEW_OUT(args))
  27. #define FATAL_OUT(args) 
  28.    (GdwSpewFlags = SPEW_FL_SPEW_PREFIX | SPEW_FL_SPEW_LOCATION, GuSpewSev = SPEW_FATAL, SPEW_OUT(args))
  29. #else
  30. #define PLAIN_TRACE_OUT(args)
  31. #define TRACE_OUT(args)
  32. #define WARNING_OUT(args)
  33. #define ERROR_OUT(args)
  34. #define FATAL_OUT(args)
  35. #endif
  36. /* parameter validation macros */
  37. /*
  38.  * call as:
  39.  *
  40.  * bPTwinOK = IS_VALID_READ_PTR(ptwin, CTWIN);
  41.  *
  42.  * bHTwinOK = IS_VALID_HANDLE(htwin, TWIN);
  43.  */
  44. #ifdef DEBUG
  45. #define IS_VALID_READ_PTR(ptr, type) 
  46.    (IsBadReadPtr((ptr), sizeof(type)) ? 
  47.     (ERROR_OUT((TEXT("invalid %s read pointer - %#08lx"), (LPCTSTR)TEXT("P")TEXT(#type), (ptr))), FALSE) : 
  48.     TRUE)
  49. #define IS_VALID_WRITE_PTR(ptr, type) 
  50.    (IsBadWritePtr((PVOID)(ptr), sizeof(type)) ? 
  51.     (ERROR_OUT((TEXT("invalid %s write pointer - %#08lx"), (LPCTSTR)TEXT("P")TEXT(#type), (ptr))), FALSE) : 
  52.     TRUE)
  53. #define IS_VALID_STRING_PTRA(ptr, type) 
  54.    (IsBadStringPtrA((ptr), (UINT)-1) ? 
  55.     (ERROR_OUT((TEXT("invalid %s pointer - %#08lx"), (LPCTSTR)TEXT("P")TEXT(#type), (ptr))), FALSE) : 
  56.     TRUE)
  57. #define IS_VALID_STRING_PTRW(ptr, type) 
  58.    (IsBadStringPtrW((ptr), (UINT)-1) ? 
  59.     (ERROR_OUT((TEXT("invalid %s pointer - %#08lx"), (LPCTSTR)TEXT("P")TEXT(#type), (ptr))), FALSE) : 
  60.     TRUE)
  61. #ifdef UNICODE
  62. #define IS_VALID_STRING_PTR(ptr, type) IS_VALID_STRING_PTRW(ptr, type)
  63. #else
  64. #define IS_VALID_STRING_PTR(ptr, type) IS_VALID_STRING_PTRA(ptr, type)
  65. #endif
  66. #define IS_VALID_CODE_PTR(ptr, type) 
  67.    (IsBadCodePtr((PROC)(ptr)) ? 
  68.     (ERROR_OUT((TEXT("invalid %s code pointer - %#08lx"), (LPCTSTR)TEXT(#type), (ptr))), FALSE) : 
  69.     TRUE)
  70. #define IS_VALID_READ_BUFFER_PTR(ptr, type, len) 
  71.    (IsBadReadPtr((ptr), len) ? 
  72.     (ERROR_OUT((TEXT("invalid %s read pointer - %#08lx"), (LPCTSTR)TEXT("P")TEXT(#type), (ptr))), FALSE) : 
  73.     TRUE)
  74. #define IS_VALID_WRITE_BUFFER_PTR(ptr, type, len) 
  75.    (IsBadWritePtr((ptr), len) ? 
  76.     (ERROR_OUT((TEXT("invalid %s write pointer - %#08lx"), (LPCTSTR)TEXT("P")TEXT(#type), (ptr))), FALSE) : 
  77.     TRUE)
  78. #define FLAGS_ARE_VALID(dwFlags, dwAllFlags) 
  79.    (((dwFlags) & (~(dwAllFlags))) ? 
  80.     (ERROR_OUT((TEXT("invalid flags set - %#08lx"), ((dwFlags) & (~(dwAllFlags))))), FALSE) : 
  81.     TRUE)
  82. #else
  83. #define IS_VALID_READ_PTR(ptr, type) 
  84.    (! IsBadReadPtr((ptr), sizeof(type)))
  85. #define IS_VALID_WRITE_PTR(ptr, type) 
  86.    (! IsBadWritePtr((PVOID)(ptr), sizeof(type)))
  87. #define IS_VALID_STRING_PTR(ptr, type) 
  88.    (! IsBadStringPtr((ptr), (UINT)-1))
  89. #define IS_VALID_CODE_PTR(ptr, type) 
  90.    (! IsBadCodePtr((PROC)(ptr)))
  91. #define IS_VALID_READ_BUFFER_PTR(ptr, type, len) 
  92.    (! IsBadReadPtr((ptr), len))
  93. #define IS_VALID_WRITE_BUFFER_PTR(ptr, type, len) 
  94.    (! IsBadWritePtr((ptr), len))
  95. #define FLAGS_ARE_VALID(dwFlags, dwAllFlags) 
  96.    (((dwFlags) & (~(dwAllFlags))) ? FALSE : TRUE)
  97. #endif
  98. /* handle validation macros */
  99. #define IS_VALID_HANDLE(hnd, type) 
  100.    (IsValidH##type(hnd))
  101. /* structure validation macros */
  102. #ifdef VSTF
  103. #ifdef DEBUG
  104. #define IS_VALID_STRUCT_PTR(ptr, type) 
  105.    (IsValidP##type(ptr) ? 
  106.     TRUE : 
  107.     (ERROR_OUT((TEXT("invalid %s pointer - %#08lx"), (LPCTSTR)TEXT("P")TEXT(#type), (ptr))), FALSE))
  108. #else
  109. #define IS_VALID_STRUCT_PTR(ptr, type) 
  110.    (IsValidP##type(ptr))
  111. #endif
  112. #else
  113. #define IS_VALID_STRUCT_PTR(ptr, type) 
  114.    (! IsBadReadPtr((ptr), sizeof(type)))
  115. #endif
  116. /* debug assertion macro */
  117. /*
  118.  * ASSERT() may only be used as a statement, not as an expression.
  119.  *
  120.  * call as:
  121.  *
  122.  * ASSERT(pszRest);
  123.  */
  124. #ifdef DEBUG
  125. #define ASSERT(exp) 
  126.    if (exp) 
  127.       ; 
  128.    else 
  129.       ERROR_OUT((TEXT("assertion failed '%s'"), (LPCTSTR)TEXT(#exp)))
  130. #else
  131. #define ASSERT(exp)
  132. #endif
  133. /* debug evaluation macro */
  134. /*
  135.  * EVAL() may be used as an expression.
  136.  *
  137.  * call as:
  138.  *
  139.  * if (EVAL(pszFoo))
  140.  *    bResult = TRUE;
  141.  */
  142. #ifdef DEBUG
  143. #define EVAL(exp) 
  144.    ((exp) || (ERROR_OUT((TEXT("evaluation failed '%s'"), (LPCTSTR)TEXT(#exp))), 0))
  145. #else
  146. #define EVAL(exp) 
  147.    (exp)
  148. #endif
  149. /* debug break */
  150. #ifndef DEBUG
  151. #define DebugBreak()
  152. #endif
  153. /* debug exported function entry */
  154. #ifdef DEBUG
  155. #define DebugEntry(szFunctionName) 
  156.    (TRACE_OUT((TEXT(#szFunctionName) TEXT("() entered."))), StackEnter())
  157. #else
  158. #define DebugEntry(szFunctionName)
  159. #endif
  160. /* debug exported function exit */
  161. #ifdef DEBUG
  162. #define DebugExitVOID(szFunctionName) 
  163.    (StackLeave(), TRACE_OUT((TEXT("%s() exiting."), TEXT(#szFunctionName))))
  164. #define DebugExit(szFunctionName, szResult) 
  165.    (StackLeave(), TRACE_OUT((TEXT("%s() exiting, returning %s."), TEXT(#szFunctionName), szResult)))
  166. #define DebugExitINT(szFunctionName, n) 
  167.    DebugExit(szFunctionName, GetINTString(n))
  168. #define DebugExitULONG(szFunctionName, ul) 
  169.    DebugExit(szFunctionName, GetULONGString(ul))
  170. #define DebugExitBOOL(szFunctionName, bool) 
  171.    DebugExit(szFunctionName, GetBOOLString(bool))
  172. #define DebugExitHRESULT(szFunctionName, hr) 
  173.    DebugExit(szFunctionName, GetHRESULTString(hr))
  174. #define DebugExitCOMPARISONRESULT(szFunctionName, cr) 
  175.    DebugExit(szFunctionName, GetCOMPARISONRESULTString(cr))
  176. #define DebugExitTWINRESULT(szFunctionName, tr) 
  177.    DebugExit(szFunctionName, GetTWINRESULTString(tr))
  178. #define DebugExitRECRESULT(szFunctionName, rr) 
  179.    DebugExit(szFunctionName, GetRECRESULTString(rr))
  180. #else
  181. #define DebugExitVOID(szFunctionName)
  182. #define DebugExit(szFunctionName, szResult)
  183. #define DebugExitINT(szFunctionName, n)
  184. #define DebugExitULONG(szFunctionName, ul)
  185. #define DebugExitBOOL(szFunctionName, bool)
  186. #define DebugExitHRESULT(szFunctionName, hr)
  187. #define DebugExitCOMPARISONRESULT(szFunctionName, cr)
  188. #define DebugExitTWINRESULT(szFunctionName, tr)
  189. #define DebugExitRECRESULT(szFunctionName, rr)
  190. #endif
  191. /* Types
  192.  ********/
  193. /* GdwSpewFlags flags */
  194. typedef enum _spewflags
  195. {
  196.    SPEW_FL_SPEW_PREFIX     = 0x0001,
  197.    SPEW_FL_SPEW_LOCATION   = 0x0002,
  198.    ALL_SPEW_FLAGS          = (SPEW_FL_SPEW_PREFIX |
  199.                               SPEW_FL_SPEW_LOCATION)
  200. }
  201. SPEWFLAGS;
  202. /* GuSpewSev values */
  203. typedef enum _spewsev
  204. {
  205.    SPEW_TRACE              = 1,
  206.    SPEW_WARNING            = 2,
  207.    SPEW_ERROR              = 3,
  208.    SPEW_FATAL              = 4
  209. }
  210. SPEWSEV;
  211. /* Prototypes
  212.  *************/
  213. /* debug.c */
  214. #ifdef DEBUG
  215. extern BOOL SetDebugModuleIniSwitches(void);
  216. extern BOOL InitDebugModule(void);
  217. extern void ExitDebugModule(void);
  218. extern void StackEnter(void);
  219. extern void StackLeave(void);
  220. extern ULONG GetStackDepth(void);
  221. extern void __cdecl SpewOut(LPCTSTR pcszFormat, ...);
  222. #endif
  223. /* Global Variables
  224.  *******************/
  225. #ifdef DEBUG
  226. /* debug.c */
  227. extern DWORD GdwSpewFlags;
  228. extern UINT GuSpewSev;
  229. extern UINT GuSpewLine;
  230. extern LPCTSTR GpcszSpewFile;
  231. /* defined by client */
  232. extern LPCTSTR GpcszSpewModule;
  233. #endif