Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

260 lines
8.3 KiB

  1. // dbgutil.h - Debug Functions and Macros
  2. /*
  3. The main macros are:
  4. ASSERT - display error if parameter evalulates to FALSE.
  5. e.g. ASSERT(x == y);
  6. ERROR_OUT - always print this error. Messagebox is optional.
  7. e.g. ERROR_OUT(("Unable to FooBar! Err=%d", dwErr));
  8. WARNING_OUT - warning message, not an error (App must call InitDebugModule)
  9. e.g. WARNING_OUT(("FooBar is not available. Using %s", szAlt));
  10. TRACE_OUT - debug message (App must call InitDebugModule)
  11. e.g. TRACE_OUT(("dwFoo=%d, dwBar=%d", dwFoo, dwBar));
  12. DBGMSG - debug message for a specific zone
  13. e.g. DBGMSG(ghZoneFoo, ZONE_BAR, ("Setting dwFoo=%d", dwFoo));
  14. Important Functions:
  15. VOID DbgInit(HDBGZONE * phDbgZone, PTCHAR * psz, UINT cZone);
  16. VOID DbgDeInit(HDBGZONE * phDbgZone);
  17. VOID WINAPI DbgPrintf(PCSTR pszPrefix, PCSTR pszFormat, va_list ap);
  18. PSTR WINAPI DbgZPrintf(HDBGZONE hZone, UINT iZone, PSTR pszFormat,...);
  19. Note: The strings in these functions, in particular the module and
  20. zone names, are always ANSI strings, even in Unicode components. The
  21. input strings to DBGINIT should not be wrapped in TEXT macros.
  22. * When Who What
  23. * -------- ------------------ ---------------------------------------
  24. * 2.26.98 Yoram Yaacovi Adapted from NetMeeting and modified (generalized)
  25. * 7.17.98 scottcot Added PDBGDATA::fRunningAsService support
  26. */
  27. #ifndef _DBGUTIL_H_
  28. #define _DBGUTIL_H_
  29. #include <mspputil.h>
  30. #include <stock.h>
  31. #ifdef __cplusplus
  32. extern "C" {
  33. #endif
  34. #include <pshpack8.h> /* Assume 8 byte packing throughout */
  35. // deal with including "debug.h" before and after
  36. #ifdef DEBUGMSG
  37. #undef DEBUGMSG
  38. #endif
  39. #define _DEBUG_H
  40. // MFC also defines this - use our version
  41. #ifdef ASSERT
  42. #undef ASSERT
  43. #endif
  44. #ifdef DEBUG
  45. #ifndef DEBUG_UTIL
  46. #define DEBUG_UTIL
  47. #endif
  48. #endif /* DEBUG */
  49. // Special NetMeeting Debug definitions
  50. #ifdef DEBUG_UTIL
  51. //////////////////////////////////////
  52. // Special Mutex Macros
  53. #define ACQMUTEX(hMutex, msec) \
  54. while (WaitForSingleObject(hMutex, msec) == WAIT_TIMEOUT) \
  55. { \
  56. ERROR_OUT(("Thread 0x%x waits on mutex\n", GetCurrentThreadId())); \
  57. } \
  58. #define RELMUTEX(hMutex) ReleaseMutex(hMutex)
  59. //////////////////////////////////////
  60. //Debug Zones
  61. #define MAXNUM_OF_MODULES 64
  62. #define MAXSIZE_OF_MODULENAME 32
  63. #define MAXNUM_OF_ZONES 16
  64. #define MAXSIZE_OF_ZONENAME 32
  65. #define ZONEINFO_SIGN 0x12490000
  66. // Zone information for a module
  67. typedef struct _ZoneInfo
  68. {
  69. ULONG ulSignature;
  70. ULONG ulRefCnt;
  71. ULONG ulZoneMask; //the zone mask
  72. BOOL bInUse;
  73. CHAR pszModule[MAXSIZE_OF_MODULENAME]; //name against which the zones are registered
  74. CHAR szZoneNames[MAXNUM_OF_ZONES][MAXSIZE_OF_ZONENAME]; //names of the zones
  75. CHAR szFile[MAX_PATH]; // output file, specific to this module
  76. }ZONEINFO,*PZONEINFO;
  77. // DBGZONEPARAM replaced by ZONEINFO
  78. #define DBGZONEINFO ZONEINFO
  79. #define PDBGZONEINFO PZONEINFO
  80. typedef PVOID HDBGZONE;
  81. // size of the memory mapped file
  82. #define CBMMFDBG (sizeof(ZONEINFO) * MAXNUM_OF_MODULES + sizeof(DBGDATA))
  83. // General information at end of memory-mapped file (after all zone data)
  84. typedef struct _DbgData {
  85. BOOL fOutputDebugString; // OutputDebugString is enabled
  86. BOOL fWinOutput; // Window Output is enabled
  87. HWND hwndCtrl; // Window that handles output
  88. UINT msgDisplay; // Message to post to hwndCtrl
  89. BOOL fFileOutput; // File Output is enabled
  90. CHAR szFile[MAX_PATH]; // File name for output
  91. BOOL fShowTime; // Dump Time (TickCount) with each message
  92. BOOL fShowThreadId; // Dump ThreadId with each message
  93. BOOL fShowModule; // Dump Module:Zone with each message
  94. BOOL fAssertBreak; // Break in AssertProc
  95. BOOL fRunningAsService; // running as a service? service-friendly message boxes then
  96. } DBGDATA;
  97. typedef DBGDATA * PDBGDATA;
  98. extern BOOL WINAPI DbgRegisterCtl(HWND hwnd, UINT uDisplayMsg);
  99. extern BOOL WINAPI DbgDeregisterCtl(HWND hwnd);
  100. extern BOOL WINAPI DbgSetLoggingOptions(HWND hwnd, UINT uOptions);
  101. extern void WINAPI DbgFlushFileLog();
  102. extern BOOL WINAPI DbgGetAllZoneParams(PDBGZONEINFO *plpZoneParam, UINT * puCnt);
  103. extern BOOL WINAPI DbgFreeZoneParams(PDBGZONEINFO pZoneParam);
  104. extern HDBGZONE WINAPI DbgCreateZone(LPSTR pszName);
  105. extern VOID WINAPI DbgDeleteZone(LPSTR pszName, HDBGZONE hDbgZone);
  106. extern BOOL WINAPI DbgSetZone(HDBGZONE hDbgZone,PDBGZONEINFO pZoneParam);
  107. extern PDBGDATA WINAPI GetPDbg(void);
  108. extern VOID WINAPI DbgSetZoneFileName(HDBGZONE hDbgZone, LPCSTR pszFile);
  109. extern PZONEINFO PROJINTERNAL FindZoneForModule(LPCSTR pszModule);
  110. extern PZONEINFO PROJINTERNAL AllocZoneForModule(LPCSTR pszModule);
  111. extern PZONEINFO PROJINTERNAL MapDebugZoneArea(void);
  112. extern VOID PROJINTERNAL UnMapDebugZoneArea(void);
  113. extern VOID PROJINTERNAL InitDbgZone(void);
  114. extern VOID PROJINTERNAL DeInitDbgZone(void);
  115. extern VOID PROJINTERNAL SetDbgFlags(void);
  116. extern VOID PROJINTERNAL SetDbgRunAsService(BOOL fRunningAsService);
  117. // Special reserved strings
  118. #define SZ_DBG_MAPPED_ZONE TEXT("_DebugZoneMap")
  119. #define SZ_DBG_FILE_MUTEX TEXT("_DbgFileMutex")
  120. #define SZ_DBG_ZONE_MUTEX TEXT("_DbgZoneMutex")
  121. #define GETZONEMASK(z) ((z) ? (((PZONEINFO)(z))->ulZoneMask) : 0 )
  122. #define IS_ZONE_ENABLED(z, f) ((((PZONEINFO)(z))->ulZoneMask) & (f))
  123. // Macro to check if zone is enabled: h = ghZone, i = zone index
  124. #define F_ZONE_ENABLED(h, i) ((NULL != h) && IS_ZONE_ENABLED(h, (1 << i)))
  125. // Standard Zones
  126. #define ZONE_WARNING 0
  127. #define ZONE_TRACE 1
  128. #define ZONE_FUNCTION 2
  129. #define ZONE_WARNING_FLAG 0x01
  130. #define ZONE_TRACE_FLAG 0x02
  131. #define ZONE_FUNCTION_FLAG 0x04
  132. ////////////////////////////////////////////
  133. // Functions
  134. VOID PROJINTERNAL AssertProc(PCSTR pszAssert, PCSTR pszFile, UINT uLine);
  135. VOID WINAPI DbgPrintf(PCSTR pszPrefix, PCSTR pszFormat, va_list ap);
  136. PSTR WINAPI DbgZPrintf(HDBGZONE hZone, UINT iZone, PSTR pszFormat,...);
  137. PSTR WINAPI DbgZVPrintf(HDBGZONE hZone, UINT iZone, PSTR pszFormat, va_list ap);
  138. VOID PROJINTERNAL DbgInitEx(HDBGZONE * phDbgZone, PCHAR * psz, UINT cZone, long ulZoneDefault);
  139. VOID PROJINTERNAL DbgDeInit(HDBGZONE * phDbgZone);
  140. INLINE VOID DbgInit(HDBGZONE * phDbgZone, PCHAR * psz, UINT cZones)
  141. {
  142. DbgInitEx(phDbgZone, psz, cZones, 0);
  143. }
  144. PSTR PszPrintf(PCSTR pszFormat,...);
  145. #endif /* DEBUG_UTIL */
  146. ////////////////////////////////////////////
  147. // Main Macros
  148. #ifdef DEBUG
  149. #define DBGINIT(phZone, psz) DbgInit(phZone, psz, (sizeof(psz)/sizeof(PCHAR))-1)
  150. #define DBGDEINIT(phZone) DbgDeInit(phZone)
  151. #define SETDBGRUNASSERVICE(f) SetDbgRunAsService(f)
  152. #define ASSERT(exp) ((!(exp)) ? AssertProc((PCSTR)#exp,__FILE__,__LINE__) : 0)
  153. #define SET_DEBUG_METHOD_NAME(name) const TCHAR *__szMethod = name;
  154. #define DEBUG_METHOD_NAME __szMethod
  155. PSTR WINAPI DbgZPrintError(PCSTR pszFile, UINT uLine, PSTR pszMsg);
  156. VOID WINAPI DbgZPrintWarning(PSTR pszFormat,...);
  157. VOID WINAPI DbgZPrintTrace(PSTR pszFormat,...);
  158. VOID WINAPI DbgZPrintFunction(PSTR pszFormat,...);
  159. VOID WINAPI RetailPrintfTrace(LPCSTR lpszFormat, ...);
  160. #define ERROR_OUT(s) LocalFree(DbgZPrintError(__FILE__, __LINE__, PszPrintf s))
  161. #define WARNING_OUT(s) DbgZPrintWarning s
  162. #define TRACE_OUT(s) DbgZPrintTrace s
  163. // RETAILMSG: Prints a message to the debug output
  164. // NOTE: available in all builds, depends on the registry flag
  165. #define RETAILMSG(x) RetailPrintfTrace x
  166. // DebugBreak based on a registry entry
  167. #define DEBUGTRAP DebugTrapFn()
  168. #define DBGMSG(z, i, s) \
  169. { \
  170. if ((NULL != z) && (((PZONEINFO)(z))->ulZoneMask & (1<<i)) ) \
  171. { \
  172. LocalFree(DbgZPrintf(z, i, PszPrintf s)); \
  173. } \
  174. }
  175. // e.g. DBGMSG(ghZone, ZONE_FOO, ("bar=%d", dwBar))
  176. #else
  177. #define DBGINIT(phZone, psz)
  178. #define DBGDEINIT(phZone)
  179. #define SETDBGRUNASSERVICE(f)
  180. #define ASSERT(exp)
  181. #define SET_DEBUG_METHOD_NAME(name)
  182. #define DEBUG_METHOD_NAME NULL
  183. #define ERROR_OUT(s)
  184. #define WARNING_OUT(s)
  185. #define TRACE_OUT(s)
  186. #ifndef DBGMSG
  187. #define DBGMSG(z, f, s)
  188. #endif
  189. #endif /* DEBUG */
  190. #include <poppack.h> /* End byte packing */
  191. #ifdef __cplusplus
  192. }
  193. #endif
  194. #endif /* _DBGUTIL_H_ */