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.

402 lines
13 KiB

  1. //+---------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1992 - 1995.
  5. //
  6. // File: debug.c
  7. //
  8. // Contents:
  9. //
  10. // Classes:
  11. //
  12. // Functions:
  13. //
  14. // History:
  15. //
  16. //----------------------------------------------------------------------------
  17. #include <spbase.h>
  18. #include <alloca.h>
  19. HANDLE g_hfLogFile = NULL;
  20. #if DBG /* NOTE: This file not compiled for retail builds */
  21. #include <stdio.h>
  22. #include <stdarg.h>
  23. #define WINDEBUG
  24. #ifndef min
  25. #define min(x,y) ((x)<(y)?(x):(y))
  26. #endif
  27. DWORD g_dwInfoLevel = 0;
  28. DWORD g_dwDebugBreak = 0;
  29. DWORD PctTraceIndent = 0;
  30. #define MAX_DEBUG_BUFFER 2048
  31. void
  32. BuildDebugHeader(
  33. DWORD Mask,
  34. PSTR pszHeader,
  35. PDWORD pcbHeader);
  36. void
  37. SPDebugOutput(char *szOutString)
  38. {
  39. DWORD dwWritten;
  40. if (NULL != g_hfLogFile)
  41. {
  42. WriteFile(
  43. g_hfLogFile,
  44. szOutString,
  45. lstrlen(szOutString),
  46. &dwWritten,
  47. NULL);
  48. }
  49. OutputDebugStringA(szOutString);
  50. }
  51. void
  52. DbgDumpHexString(const unsigned char *String, DWORD cbString)
  53. {
  54. DWORD i,count;
  55. CHAR digits[]="0123456789abcdef";
  56. CHAR pbLine[MAX_PATH];
  57. DWORD cbLine, cbHeader;
  58. DWORD_PTR address;
  59. BuildDebugHeader(DEB_BUFFERS, pbLine, &cbHeader);
  60. if(String == NULL && cbString != 0)
  61. {
  62. strcat(pbLine, "<null> buffer!!!\n");
  63. SPDebugOutput(pbLine);
  64. return;
  65. }
  66. for(; cbString ; cbString -= count, String += count)
  67. {
  68. count = (cbString > 16) ? 16:cbString;
  69. cbLine = cbHeader;
  70. address = (DWORD_PTR)String;
  71. #if defined(_WIN64)
  72. pbLine[cbLine++] = digits[(address >> 0x3c) & 0x0f];
  73. pbLine[cbLine++] = digits[(address >> 0x38) & 0x0f];
  74. pbLine[cbLine++] = digits[(address >> 0x34) & 0x0f];
  75. pbLine[cbLine++] = digits[(address >> 0x30) & 0x0f];
  76. pbLine[cbLine++] = digits[(address >> 0x2c) & 0x0f];
  77. pbLine[cbLine++] = digits[(address >> 0x28) & 0x0f];
  78. pbLine[cbLine++] = digits[(address >> 0x24) & 0x0f];
  79. pbLine[cbLine++] = digits[(address >> 0x20) & 0x0f];
  80. #endif
  81. pbLine[cbLine++] = digits[(address >> 0x1c) & 0x0f];
  82. pbLine[cbLine++] = digits[(address >> 0x18) & 0x0f];
  83. pbLine[cbLine++] = digits[(address >> 0x14) & 0x0f];
  84. pbLine[cbLine++] = digits[(address >> 0x10) & 0x0f];
  85. pbLine[cbLine++] = digits[(address >> 0x0c) & 0x0f];
  86. pbLine[cbLine++] = digits[(address >> 0x08) & 0x0f];
  87. pbLine[cbLine++] = digits[(address >> 0x04) & 0x0f];
  88. pbLine[cbLine++] = digits[(address ) & 0x0f];
  89. pbLine[cbLine++] = ' ';
  90. pbLine[cbLine++] = ' ';
  91. for(i = 0; i < count; i++)
  92. {
  93. pbLine[cbLine++] = digits[String[i]>>4];
  94. pbLine[cbLine++] = digits[String[i]&0x0f];
  95. if(i == 7)
  96. {
  97. pbLine[cbLine++] = ':';
  98. }
  99. else
  100. {
  101. pbLine[cbLine++] = ' ';
  102. }
  103. }
  104. #if 1
  105. for(; i < 16; i++)
  106. {
  107. pbLine[cbLine++] = ' ';
  108. pbLine[cbLine++] = ' ';
  109. pbLine[cbLine++] = ' ';
  110. }
  111. pbLine[cbLine++] = ' ';
  112. for(i = 0; i < count; i++)
  113. {
  114. if(String[i] < 32 || String[i] > 126)
  115. {
  116. pbLine[cbLine++] = '.';
  117. }
  118. else
  119. {
  120. pbLine[cbLine++] = String[i];
  121. }
  122. }
  123. #endif
  124. pbLine[cbLine++] = '\n';
  125. pbLine[cbLine++] = 0;
  126. SPDebugOutput(pbLine);
  127. }
  128. }
  129. char *aszSPDebugLevel[] = {
  130. "Error ",
  131. "Warning",
  132. "Trace ",
  133. "Mem ",
  134. "Result "
  135. };
  136. void
  137. BuildDebugHeader(
  138. DWORD Mask,
  139. PSTR pszHeader,
  140. PDWORD pcbHeader)
  141. {
  142. SYSTEMTIME stTime;
  143. DWORD Level = 0;
  144. ULONG ClientProcess;
  145. ULONG ClientThread;
  146. GetLocalTime(&stTime);
  147. Level = 0;
  148. while (!(Mask & 1))
  149. {
  150. Level++;
  151. Mask >>= 1;
  152. }
  153. if (Level >= sizeof(aszSPDebugLevel) / sizeof(char *))
  154. {
  155. Level = sizeof(aszSPDebugLevel) / sizeof(char *) - 1;
  156. }
  157. SslGetClientProcess(&ClientProcess);
  158. SslGetClientThread(&ClientThread);
  159. *pcbHeader = 0;
  160. if(g_dwInfoLevel & SP_LOG_TIMESTAMP)
  161. {
  162. *pcbHeader = wsprintf(
  163. pszHeader,
  164. "[%2d/%2d %02d:%02d:%02d.%03d] %d.%d> %s: ",
  165. stTime.wMonth, stTime.wDay,
  166. stTime.wHour, stTime.wMinute, stTime.wSecond,
  167. stTime.wMilliseconds,
  168. ClientProcess, ClientThread,
  169. aszSPDebugLevel[Level]);
  170. }
  171. else
  172. {
  173. *pcbHeader = wsprintf(
  174. pszHeader,
  175. "%d.%d> %s: ",
  176. ClientProcess, ClientThread,
  177. aszSPDebugLevel[Level]);
  178. }
  179. }
  180. void
  181. SPDebugLog(long Mask, const char *Format, ...)
  182. {
  183. va_list ArgList;
  184. int PrefixSize = 0;
  185. DWORD iOut;
  186. char szOutString[MAX_DEBUG_BUFFER];
  187. if (Mask & g_dwInfoLevel)
  188. {
  189. BuildDebugHeader(Mask, szOutString, &iOut);
  190. PrefixSize = min(60, PctTraceIndent * 3);
  191. FillMemory(szOutString+iOut, PrefixSize, ' ');
  192. PrefixSize += iOut;
  193. szOutString[PrefixSize] = '\0';
  194. va_start(ArgList, Format);
  195. if (wvsprintf(&szOutString[PrefixSize], Format, ArgList) < 0)
  196. {
  197. static char szOverFlow[] = "\n<256 byte OVERFLOW!>\n";
  198. // Less than zero indicates that the string would not fit into the
  199. // buffer. Output a special message indicating overflow.
  200. lstrcpy(
  201. &szOutString[sizeof(szOutString) - sizeof(szOverFlow)],
  202. szOverFlow);
  203. }
  204. va_end(ArgList);
  205. SPDebugOutput(szOutString);
  206. }
  207. }
  208. void
  209. SPLogDistinguishedName(
  210. DWORD LogLevel,
  211. LPSTR pszLabel,
  212. PBYTE pbName,
  213. DWORD cbName)
  214. {
  215. CERT_NAME_BLOB Name;
  216. LPSTR pszName;
  217. DWORD cchName;
  218. Name.pbData = pbName;
  219. Name.cbData = cbName;
  220. cchName = CertNameToStr(CRYPT_ASN_ENCODING,
  221. &Name,
  222. CERT_X500_NAME_STR | CERT_NAME_STR_NO_PLUS_FLAG,
  223. NULL,
  224. 0);
  225. if(cchName == 0)
  226. {
  227. return;
  228. }
  229. SafeAllocaAllocate(pszName, cchName);
  230. if(pszName == NULL)
  231. {
  232. return;
  233. }
  234. cchName = CertNameToStr(CRYPT_ASN_ENCODING,
  235. &Name,
  236. CERT_X500_NAME_STR | CERT_NAME_STR_NO_PLUS_FLAG,
  237. pszName,
  238. cchName);
  239. if(cchName == 0)
  240. {
  241. SafeAllocaFree(pszName);
  242. return;
  243. }
  244. DebugLog((LogLevel, pszLabel, pszName));
  245. SafeAllocaFree(pszName);
  246. }
  247. long
  248. SPLogErrorCode(
  249. long err,
  250. const char *szFile,
  251. long lLine)
  252. {
  253. char *szName = "Unknown";
  254. switch(err)
  255. {
  256. case PCT_ERR_OK: szName = "PCT_ERR_OK"; break;
  257. case PCT_ERR_BAD_CERTIFICATE: szName = "PCT_ERR_BAD_CERTIFICATE"; break;
  258. case PCT_ERR_CLIENT_AUTH_FAILED: szName = "PCT_ERR_CLIENT_AUTH_FAILED"; break;
  259. case PCT_ERR_ILLEGAL_MESSAGE: szName = "PCT_ERR_ILLEGAL_MESSAGE"; break;
  260. case PCT_ERR_INTEGRITY_CHECK_FAILED: szName = "PCT_ERR_INTEGRITY_CHECK_FAILED"; break;
  261. case PCT_ERR_SERVER_AUTH_FAILED: szName = "PCT_ERR_SERVER_AUTH_FAILED"; break;
  262. case PCT_ERR_SPECS_MISMATCH: szName = "PCT_ERR_SPECS_MISMATCH"; break;
  263. case PCT_ERR_SSL_STYLE_MSG: szName = "PCT_ERR_SSL_STYLE_MSG"; break;
  264. case PCT_ERR_RENEGOTIATE: szName = "PCT_ERR_RENEGOTIATE"; break;
  265. case PCT_ERR_UNKNOWN_CREDENTIAL: szName = "PCT_ERR_UNKNOWN_CREDENTIAL"; break;
  266. case PCT_INT_BUFF_TOO_SMALL: szName = "PCT_INT_BUFF_TOO_SMALL"; break;
  267. case PCT_INT_INCOMPLETE_MSG: szName = "PCT_INT_INCOMPLETE_MSG"; break;
  268. case PCT_INT_DROP_CONNECTION: szName = "PCT_INT_DROP_CONNECTION"; break;
  269. case PCT_INT_BAD_CERT: szName = "PCT_INT_BAD_CERT"; break;
  270. case PCT_INT_CLI_AUTH: szName = "PCT_INT_CLI_AUTH"; break;
  271. case PCT_INT_ILLEGAL_MSG: szName = "PCT_INT_ILLEGAL_MSG"; break;
  272. case PCT_INT_MSG_ALTERED: szName = "PCT_INT_MSG_ALTERED"; break;
  273. case PCT_INT_INTERNAL_ERROR: szName = "PCT_INT_INTERNAL_ERROR"; break;
  274. case PCT_INT_DATA_OVERFLOW: szName = "PCT_INT_DATA_OVERFLOW"; break;
  275. case PCT_INT_SPECS_MISMATCH: szName = "PCT_INT_SPECS_MISMATCH"; break;
  276. case PCT_INT_RENEGOTIATE: szName = "PCT_INT_RENEGOTIATE"; break;
  277. case PCT_INT_UNKNOWN_CREDENTIAL: szName = "PCT_INT_UNKNOWN_CREDENTIAL"; break;
  278. case SEC_E_INSUFFICIENT_MEMORY: szName = "SEC_E_INSUFFICIENT_MEMORY"; break;
  279. case SEC_E_INVALID_HANDLE: szName = "SEC_E_INVALID_HANDLE"; break;
  280. case SEC_E_UNSUPPORTED_FUNCTION: szName = "SEC_E_UNSUPPORTED_FUNCTION"; break;
  281. case SEC_E_TARGET_UNKNOWN: szName = "SEC_E_TARGET_UNKNOWN"; break;
  282. case SEC_E_INTERNAL_ERROR: szName = "SEC_E_INTERNAL_ERROR"; break;
  283. case SEC_E_SECPKG_NOT_FOUND: szName = "SEC_E_SECPKG_NOT_FOUND"; break;
  284. case SEC_E_NOT_OWNER: szName = "SEC_E_NOT_OWNER"; break;
  285. case SEC_E_CANNOT_INSTALL: szName = "SEC_E_CANNOT_INSTALL"; break;
  286. case SEC_E_INVALID_TOKEN: szName = "SEC_E_INVALID_TOKEN"; break;
  287. case SEC_E_CANNOT_PACK: szName = "SEC_E_CANNOT_PACK"; break;
  288. case SEC_E_QOP_NOT_SUPPORTED: szName = "SEC_E_QOP_NOT_SUPPORTED"; break;
  289. case SEC_E_NO_IMPERSONATION: szName = "SEC_E_NO_IMPERSONATION"; break;
  290. case SEC_E_LOGON_DENIED: szName = "SEC_E_LOGON_DENIED"; break;
  291. case SEC_E_UNKNOWN_CREDENTIALS: szName = "SEC_E_UNKNOWN_CREDENTIALS"; break;
  292. case SEC_E_NO_CREDENTIALS: szName = "SEC_E_NO_CREDENTIALS"; break;
  293. case SEC_E_MESSAGE_ALTERED: szName = "SEC_E_MESSAGE_ALTERED"; break;
  294. case SEC_E_OUT_OF_SEQUENCE: szName = "SEC_E_OUT_OF_SEQUENCE"; break;
  295. case SEC_E_NO_AUTHENTICATING_AUTHORITY: szName = "SEC_E_NO_AUTHENTICATING_AUTHORITY"; break;
  296. case SEC_I_CONTINUE_NEEDED: szName = "SEC_I_CONTINUE_NEEDED"; break;
  297. case SEC_I_COMPLETE_NEEDED: szName = "SEC_I_COMPLETE_NEEDED"; break;
  298. case SEC_I_COMPLETE_AND_CONTINUE: szName = "SEC_I_COMPLETE_AND_CONTINUE"; break;
  299. case SEC_I_LOCAL_LOGON: szName = "SEC_I_LOCAL_LOGON"; break;
  300. case SEC_E_BAD_PKGID: szName = "SEC_E_BAD_PKGID"; break;
  301. case SEC_E_CONTEXT_EXPIRED: szName = "SEC_E_CONTEXT_EXPIRED"; break;
  302. case SEC_E_INCOMPLETE_MESSAGE: szName = "SEC_E_INCOMPLETE_MESSAGE"; break;
  303. case SEC_E_INCOMPLETE_CREDENTIALS: szName = "SEC_E_INCOMPLETE_CREDENTIALS"; break;
  304. case SEC_E_BUFFER_TOO_SMALL: szName = "SEC_E_BUFFER_TOO_SMALL"; break;
  305. case SEC_I_INCOMPLETE_CREDENTIALS: szName = "SEC_I_INCOMPLETE_CREDENTIALS"; break;
  306. case SEC_I_RENEGOTIATE: szName = "SEC_I_RENEGOTIATE"; break;
  307. case SEC_E_WRONG_PRINCIPAL: szName = "SEC_E_WRONG_PRINCIPAL"; break;
  308. case SEC_I_NO_LSA_CONTEXT: szName = "SEC_I_NO_LSA_CONTEXT"; break;
  309. case CERT_E_EXPIRED: szName = "CERT_E_EXPIRED"; break;
  310. case CERT_E_UNTRUSTEDROOT: szName = "CERT_E_UNTRUSTEDROOT"; break;
  311. case CRYPT_E_REVOKED: szName = "CRYPT_E_REVOKED"; break;
  312. case CRYPT_E_NO_REVOCATION_CHECK: szName = "CRYPT_E_NO_REVOCATION_CHECK"; break;
  313. case CRYPT_E_REVOCATION_OFFLINE: szName = "CRYPT_E_REVOCATION_OFFLINE"; break;
  314. }
  315. SPDebugLog(SP_LOG_RES, "Result: %s (0x%lx) - %s, Line %d\n", szName, err, szFile, lLine);
  316. return err;
  317. }
  318. #pragma warning(disable:4206) /* Disable the empty translation unit */
  319. /* warning/error */
  320. void
  321. SPAssert(
  322. void * FailedAssertion,
  323. void * FileName,
  324. unsigned long LineNumber,
  325. char * Message)
  326. {
  327. UNREFERENCED_PARAMETER(Message);
  328. SPDebugLog(SP_LOG_ERROR,
  329. "Assertion FAILED, %s, %s : %d\n",
  330. FailedAssertion,
  331. FileName,
  332. LineNumber);
  333. DebugBreak();
  334. }
  335. #endif /* DBG */ /* NOTE: This file not compiled for retail builds */