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.

306 lines
7.6 KiB

  1. //+---------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1992 - 1993.
  5. //
  6. // File: debug.c
  7. //
  8. // Contents: Debugging support functions
  9. //
  10. // Classes:
  11. //
  12. // Functions:
  13. //
  14. // Note: This file is not compiled for retail builds
  15. //
  16. // History: 4-29-93 RichardW Created
  17. //
  18. //----------------------------------------------------------------------------
  19. // #if DBG // NOTE: This file not compiled for retail builds
  20. #include "precomp.h"
  21. #pragma hdrstop
  22. #if DBG // NOTE: This file not compiled for retail builds
  23. FILE * LogFile;
  24. DWORD WinlogonInfoLevel = 3;
  25. // Debugging support functions.
  26. // These two functions do not exist in Non-Debug builds. They are wrappers
  27. // to the commnot functions (maybe I should get rid of that as well...)
  28. // that echo the message to a log file.
  29. char * DebLevel[] = { "Winlogon-Error",
  30. "Winlogon-Warn",
  31. "Winlogon-Trace",
  32. "Winlogon-Trace-Init",
  33. "Winlogon-Trace-Timeout",
  34. "Winlogon-Trace-SAS",
  35. "Winlogon-Trace-State",
  36. "Winlogon-Trace-MPR",
  37. "Should-not-see",
  38. "Winlogon-Trace-Profile",
  39. "Should-not-see",
  40. "Should-not-see",
  41. "Should-not-see",
  42. "Winlogon-Trace-Migrate",
  43. "Should-not-see",
  44. "Winlogon-Trace-Setup"
  45. };
  46. typedef struct _DebugKeys {
  47. char * Name;
  48. DWORD Value;
  49. } DebugKeys, *PDebugKeys;
  50. DebugKeys DebugKeyNames[] = {
  51. {"Error", DEB_ERROR},
  52. {"Warning", DEB_WARN},
  53. {"Trace", DEB_TRACE},
  54. {"Init", DEB_TRACE_INIT},
  55. {"Timeout", DEB_TRACE_TIMEOUT},
  56. {"Sas", DEB_TRACE_SAS},
  57. {"State", DEB_TRACE_STATE},
  58. {"MPR", DEB_TRACE_MPR},
  59. {"CoolSwitch", DEB_COOL_SWITCH},
  60. {"Profile", DEB_TRACE_PROFILE},
  61. {"DebugLsa", DEB_DEBUG_LSA},
  62. {"DebugSpm", DEB_DEBUG_LSA},
  63. {"DebugMpr", DEB_DEBUG_MPR},
  64. {"DebugGo", DEB_DEBUG_NOWAIT},
  65. {"Migrate", DEB_TRACE_MIGRATE},
  66. {"DebugServices", DEB_DEBUG_SERVICES},
  67. {"Setup", DEB_TRACE_SETUP}
  68. };
  69. #define NUM_DEBUG_KEYS sizeof(DebugKeyNames) / sizeof(DebugKeys)
  70. #define NUM_BREAK_KEYS sizeof(BreakKeyNames) / sizeof(DebugKeys)
  71. //+---------------------------------------------------------------------------
  72. //
  73. // Function: LogEvent
  74. //
  75. // Synopsis: Logs an event to the console and, optionally, a file.
  76. //
  77. // Effects:
  78. //
  79. // Arguments: [Mask] --
  80. // [Format] --
  81. // [Format] --
  82. //
  83. // Requires:
  84. //
  85. // Returns:
  86. //
  87. // Signals:
  88. //
  89. // Modifies:
  90. //
  91. // Algorithm:
  92. //
  93. // History: 4-29-93 RichardW Created
  94. //
  95. // Notes:
  96. //
  97. //----------------------------------------------------------------------------
  98. void
  99. LogEvent( long Mask,
  100. const char * Format,
  101. ...)
  102. {
  103. va_list ArgList;
  104. int Level = 0;
  105. int PrefixSize = 0;
  106. char szOutString[256];
  107. long OriWinlogonlMask = Mask;
  108. if (Mask & WinlogonInfoLevel)
  109. {
  110. while (!(Mask & 1))
  111. {
  112. Level++;
  113. Mask >>= 1;
  114. }
  115. if (Level >= (sizeof(DebLevel) / sizeof(char *)) )
  116. {
  117. Level = (sizeof(DebLevel) / sizeof(char *)) - 1;
  118. }
  119. //
  120. // Make the prefix first: "Process.Thread> Winlogon-XXX"
  121. //
  122. PrefixSize = sprintf(szOutString, "%d.%d> %s: ",
  123. GetCurrentProcessId(), GetCurrentThreadId(), DebLevel[Level]);
  124. va_start(ArgList, Format);
  125. if (_vsnprintf(&szOutString[PrefixSize], sizeof(szOutString) - PrefixSize,
  126. Format, ArgList) < 0)
  127. {
  128. //
  129. // Less than zero indicates that the string could not be
  130. // fitted into the buffer. Output a special message indicating
  131. // that:
  132. //
  133. OutputDebugStringA("Winlogon!LogEvent: Could not pack string into 256 bytes\n");
  134. }
  135. else
  136. {
  137. OutputDebugStringA(szOutString);
  138. }
  139. if (LogFile)
  140. {
  141. SYSTEMTIME stTime;
  142. FILETIME ftTime;
  143. FILETIME localtime;
  144. NtQuerySystemTime((PLARGE_INTEGER) &ftTime);
  145. FileTimeToLocalFileTime(&ftTime, &localtime);
  146. FileTimeToSystemTime(&localtime, &stTime);
  147. fprintf(LogFile, "%02d:%02d:%02d.%03d: %s\n",
  148. stTime.wHour, stTime.wMinute, stTime.wSecond,
  149. stTime.wMilliseconds, szOutString);
  150. fflush(LogFile);
  151. }
  152. }
  153. }
  154. void
  155. OpenLogFile(LPSTR pszLogFile)
  156. {
  157. LogFile = fopen(pszLogFile, "a");
  158. if (!LogFile)
  159. {
  160. OutputDebugStringA("Winlogon: Could not open logfile for append");
  161. OutputDebugStringA(pszLogFile);
  162. }
  163. DebugLog((DEB_TRACE, "Log file '%s' begins\n", pszLogFile));
  164. }
  165. DWORD
  166. GetDebugKeyValue(
  167. PDebugKeys KeyTable,
  168. int cKeys,
  169. LPSTR pszKey)
  170. {
  171. int i;
  172. for (i = 0; i < cKeys ; i++ )
  173. {
  174. if (_strcmpi(KeyTable[i].Name, pszKey) == 0)
  175. {
  176. return(KeyTable[i].Value);
  177. }
  178. }
  179. return(0);
  180. }
  181. //+---------------------------------------------------------------------------
  182. //
  183. // Function: LoadDebugParameters
  184. //
  185. // Synopsis: Loads debug parameters from win.ini
  186. //
  187. // Effects:
  188. //
  189. // Arguments: (none)
  190. //
  191. // Requires:
  192. //
  193. // Returns:
  194. //
  195. // Signals:
  196. //
  197. // Modifies:
  198. //
  199. // Algorithm:
  200. //
  201. // History: 4-29-93 RichardW Created
  202. //
  203. // Notes:
  204. //
  205. //----------------------------------------------------------------------------
  206. void
  207. LoadDebugParameters(char * szSection)
  208. {
  209. char szVal[128];
  210. char * pszDebug;
  211. int cbVal;
  212. cbVal = GetProfileStringA(szSection, "DebugFlags", "Error,Warning", szVal, sizeof(szVal));
  213. pszDebug = strtok(szVal, ", \t");
  214. while (pszDebug)
  215. {
  216. WinlogonInfoLevel |= GetDebugKeyValue(DebugKeyNames, NUM_DEBUG_KEYS, pszDebug);
  217. pszDebug = strtok(NULL, ", \t");
  218. }
  219. cbVal = GetProfileStringA(szSection, "LogFile", "", szVal, sizeof(szVal));
  220. if (cbVal)
  221. {
  222. OpenLogFile(szVal);
  223. }
  224. }
  225. //+---------------------------------------------------------------------------
  226. //
  227. // Function: InitDebugSupport
  228. //
  229. // Synopsis: Initializes debugging support for the Winlogon
  230. //
  231. // Effects:
  232. //
  233. // Arguments: (none)
  234. //
  235. // Requires:
  236. //
  237. // Returns:
  238. //
  239. // Signals:
  240. //
  241. // Modifies:
  242. //
  243. // Algorithm:
  244. //
  245. // History: 4-29-93 RichardW Created
  246. //
  247. // Notes:
  248. //
  249. //----------------------------------------------------------------------------
  250. void
  251. InitDebugSupport(void)
  252. {
  253. LoadDebugParameters("WinlogonDebug");
  254. LoadDebugParameters("Winlogon");
  255. }
  256. #else // DBG
  257. //#pragma warning(disable:4206) // Disable the empty transation unit
  258. // warning/error
  259. #endif // NOTE: This file not compiled for retail builds