Leaked source code of windows server 2003
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.

354 lines
8.0 KiB

  1. //+---------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1996 - 1996.
  5. //
  6. // File: debug.cpp
  7. //
  8. // Contents: Debug sub system APIs implementation
  9. //
  10. //
  11. // 03/20/96 kevinr wrote it
  12. // 04/17/96 kevinr added OSS init
  13. // 05-Sep-1997 pberkman added sub-system debug.
  14. //
  15. //----------------------------------------------------------------------------
  16. #ifdef SMIME_V3
  17. #if DBG
  18. #include <windows.h>
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <stdarg.h>
  22. #include <memory.h>
  23. #include <string.h>
  24. #include <process.h>
  25. #include <time.h>
  26. #include <crtdbg.h>
  27. #include <asn1code.h>
  28. #include "badstrfunctions.h"
  29. //#include "regtest.h" // JLS
  30. #include "dbgdef.h"
  31. // set DEBUG_MASK=0x26
  32. LPSTR pszDEBUG_MASK = "DEBUG_MASK";
  33. #define DEBUG_MASK_DELAY_FREE_MEM _CRTDBG_DELAY_FREE_MEM_DF /* 0x02 */
  34. #define DEBUG_MASK_CHECK_ALWAYS _CRTDBG_CHECK_ALWAYS_DF /* 0x04 */
  35. #define DEBUG_MASK_LEAK_CHECK _CRTDBG_LEAK_CHECK_DF /* 0x20 */
  36. #define DEBUG_MASK_MEM \
  37. (DEBUG_MASK_DELAY_FREE_MEM | DEBUG_MASK_CHECK_ALWAYS | DEBUG_MASK_LEAK_CHECK)
  38. // from asn1code.h:
  39. // #define DEBUGPDU 0x02 /* produce tracing output */
  40. // #define DEBUG_ERRORS 0x10 /* print decoder errors to output */
  41. // set OSS_DEBUG_MASK=0x02
  42. // set OSS_DEBUG_MASK=0x10 - only print decoder errors
  43. LPSTR pszOSS_DEBUG_MASK = "OSS_DEBUG_MASK";
  44. // receives trace output
  45. LPSTR pszOSS_DEBUG_TRACEFILE = "OSS_DEBUG_TRACEFILE";
  46. static char *pszDEBUG_PRINT_MASK = "DEBUG_PRINT_MASK";
  47. static char *pszDefualtSSTag = "ISPU";
  48. static DBG_SS_TAG sSSTags[] = __DBG_SS_TAGS;
  49. #if 0 // JLS
  50. //
  51. //+-------------------------------------------------------------------------
  52. //
  53. // Pithy stubs to create stdcall proc from cdecl
  54. //
  55. //--------------------------------------------------------------------------
  56. void*
  57. _stdcall
  58. scMalloc( size_t size)
  59. {
  60. return malloc(size);
  61. }
  62. void*
  63. _stdcall
  64. scRealloc( void *memblock, size_t size)
  65. {
  66. return realloc(memblock, size);
  67. }
  68. void
  69. _stdcall
  70. scFree( void *memblock)
  71. {
  72. free(memblock);
  73. }
  74. //+-------------------------------------------------------------------------
  75. //
  76. // Function: DbgGetDebugFlags
  77. //
  78. // Synopsis: Get the debug flags.
  79. //
  80. // Returns: the debug flags
  81. //
  82. //--------------------------------------------------------------------------
  83. int
  84. WINAPI
  85. DbgGetDebugFlags()
  86. {
  87. char *pszEnvVar;
  88. char *p;
  89. int iDebugFlags = 0;
  90. if (pszEnvVar = getenv( pszDEBUG_MASK))
  91. iDebugFlags = strtol( pszEnvVar, &p, 16);
  92. return iDebugFlags;
  93. }
  94. //+-------------------------------------------------------------------------
  95. //
  96. // Function: DbgProcessAttach
  97. //
  98. // Synopsis: Handle process attach.
  99. //
  100. // Returns: TRUE
  101. //
  102. //--------------------------------------------------------------------------
  103. BOOL
  104. WINAPI
  105. DbgProcessAttach()
  106. {
  107. int tmpFlag;
  108. #ifdef _DEBUG
  109. tmpFlag = _CrtSetDbgFlag( _CRTDBG_REPORT_FLAG ); // get current
  110. tmpFlag |= DbgGetDebugFlags(); // enable flags
  111. tmpFlag &= ~_CRTDBG_CHECK_CRT_DF; // disable CRT block checking
  112. _CrtSetDbgFlag( tmpFlag); // set new value
  113. #endif
  114. return TRUE;
  115. }
  116. //+-------------------------------------------------------------------------
  117. //
  118. // Function: DbgProcessDetach
  119. //
  120. // Synopsis: Handle process detach.
  121. //
  122. // Returns: TRUE
  123. //
  124. //--------------------------------------------------------------------------
  125. BOOL
  126. WINAPI
  127. DbgProcessDetach()
  128. {
  129. return TRUE;
  130. }
  131. //+-------------------------------------------------------------------------
  132. //
  133. // Function: DbgInitOSS
  134. //
  135. // Synopsis: Do OSS init for debug.
  136. //
  137. // Returns: TRUE
  138. //
  139. // Note: Always FRONT_ALIGN encoding
  140. //
  141. //--------------------------------------------------------------------------
  142. BOOL
  143. WINAPI
  144. DbgInitOSS(
  145. OssGlobal *pog)
  146. {
  147. char *pszEnvVar;
  148. char *p;
  149. // from asn1code.h:
  150. // #define DEBUGPDU 0x02 /* produce tracing output */
  151. // #define DEBUG_ERRORS 0x10 /* print decoder errors to output */
  152. // set OSS_DEBUG_MASK=0x02
  153. // set OSS_DEBUG_MASK=0x10 - only print decoder errors
  154. if (pszEnvVar = getenv( pszOSS_DEBUG_MASK)) {
  155. unsigned long ulEnvVar;
  156. ulEnvVar = strtoul( pszEnvVar, &p, 16) & (DEBUGPDU | DEBUG_ERRORS);
  157. if ( ulEnvVar)
  158. ossSetDecodingFlags( pog, ulEnvVar | RELAXBER);
  159. if ( DEBUGPDU & ulEnvVar)
  160. ossSetEncodingFlags( pog, DEBUGPDU | FRONT_ALIGN);
  161. else
  162. ossSetEncodingFlags( pog, FRONT_ALIGN);
  163. } else {
  164. ossSetDecodingFlags( pog, DEBUG_ERRORS | RELAXBER);
  165. ossSetEncodingFlags( pog, FRONT_ALIGN);
  166. }
  167. if (pszEnvVar = getenv( pszOSS_DEBUG_TRACEFILE))
  168. ossOpenTraceFile( pog, pszEnvVar);
  169. #ifdef _DEBUG
  170. if (DbgGetDebugFlags() & DEBUG_MASK_MEM) {
  171. pog->mallocp = scMalloc;
  172. pog->reallocp = scRealloc;
  173. pog->freep = scFree;
  174. }
  175. #else
  176. pog->mallocp = scMalloc;
  177. pog->reallocp = scRealloc;
  178. pog->freep = scFree;
  179. #endif
  180. return TRUE;
  181. }
  182. //+-------------------------------------------------------------------------
  183. //
  184. // Function: DebugDllMain
  185. //
  186. // Synopsis: Initialize the debug DLL
  187. //
  188. // Returns: TRUE
  189. //
  190. //--------------------------------------------------------------------------
  191. BOOL
  192. WINAPI
  193. DebugDllMain(
  194. HMODULE hInst,
  195. ULONG ulReason,
  196. LPVOID lpReserved)
  197. {
  198. BOOL fRet = TRUE;
  199. switch (ulReason) {
  200. case DLL_PROCESS_ATTACH:
  201. fRet = DbgProcessAttach();
  202. // fRet &= RegTestInit(); // JLS
  203. break;
  204. case DLL_PROCESS_DETACH:
  205. fRet = DbgProcessDetach();
  206. // RegTestCleanup(); // JLS
  207. break;
  208. default:
  209. break;
  210. }
  211. return fRet;
  212. }
  213. const char *DbgGetSSString(DWORD dwSubSystemId)
  214. {
  215. DBG_SS_TAG *psSS;
  216. psSS = &sSSTags[0];
  217. while (psSS->dwSS > 0)
  218. {
  219. if ((psSS->dwSS & dwSubSystemId) > 0)
  220. {
  221. if (psSS->pszTag)
  222. {
  223. return(psSS->pszTag);
  224. }
  225. return(pszDefualtSSTag);
  226. }
  227. psSS++;
  228. }
  229. return(pszDefualtSSTag);
  230. }
  231. static BOOL DbgIsSSActive(DWORD dwSSIn)
  232. {
  233. char *pszEnvVar;
  234. DWORD dwEnv;
  235. dwEnv = 0;
  236. if (pszEnvVar = getenv(pszDEBUG_PRINT_MASK))
  237. {
  238. dwEnv = (DWORD)strtol(pszEnvVar, NULL, 16);
  239. }
  240. return((dwEnv & dwSSIn) > 0);
  241. }
  242. //+-------------------------------------------------------------------------
  243. //
  244. // Function: DbgPrintf
  245. //
  246. // Synopsis: outputs debug info to stdout and debugger
  247. //
  248. // Returns: number of chars output
  249. //
  250. //--------------------------------------------------------------------------
  251. int WINAPIV DbgPrintf(DWORD dwSubSystemId, LPCSTR lpFmt, ...)
  252. {
  253. va_list arglist;
  254. CHAR ach1[1024];
  255. CHAR ach2[1080];
  256. int cch;
  257. HANDLE hStdOut;
  258. DWORD cb;
  259. DWORD dwErr;
  260. dwErr = GetLastError();
  261. if (!(DbgIsSSActive(dwSubSystemId)))
  262. {
  263. SetLastError(dwErr);
  264. return(0);
  265. }
  266. _try
  267. {
  268. va_start(arglist, lpFmt);
  269. wvnsprintf( ach1, ARRAYSIZE(ach1), lpFmt, arglist);
  270. va_end(arglist);
  271. cch = wnsprintf(ach2, ARRAYSZIE(ach2),"%s: %s", DbgGetSSString(dwSubSystemId), ach1);
  272. hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
  273. if (hStdOut != INVALID_HANDLE_VALUE)
  274. {
  275. WriteConsole( hStdOut, ach2, strlen(ach2), &cb, NULL);
  276. }
  277. OutputDebugString(ach2);
  278. } _except( EXCEPTION_EXECUTE_HANDLER)
  279. {
  280. // return failure
  281. cch = 0;
  282. }
  283. SetLastError(dwErr);
  284. return cch;
  285. }
  286. #else // !0 // JLS
  287. int WINAPIV DbgPrintf(DWORD dwSubSystemId, LPCSTR lpFmt, ...)
  288. {
  289. return 0;
  290. }
  291. #endif // 0 JLS
  292. #endif // DBG
  293. #endif // SMIME_V3