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.

314 lines
6.9 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. //
  20. // For ease of debugging the SPMgr, all the debug support functions have
  21. // been stuck here. Basically, we read info from win.ini, since that allows
  22. // us to configure the debug level via a text file (and DOS, for example).
  23. //
  24. // Format is:
  25. //
  26. // win.ini
  27. //
  28. // [SPMgr]
  29. // DebugFlags=<Flag>[<,Flag>]*
  30. // Package=<int>
  31. // BreakFlags=<BreakFlag>[<,BreakFlags>]*
  32. //
  33. // WHERE:
  34. // Flag is one of the following:
  35. // Error, Warning, Trace, Verbose, BreakOnError, Helpers,
  36. // RefMon, Locator, WAPI, Init, Audit, Db, Lsa
  37. //
  38. // Package is the name of the dll implementing the package, e.g.
  39. // NTLM=3
  40. //
  41. // BreakFlags will cause SPMgr to break, if BreakOnError is set in
  42. // DebugFlags:
  43. // InitBegin, InitEnd, Connect, Exception, Problem, Load
  44. //
  45. //
  46. #if DBG // NOTE: This file not compiled for retail builds
  47. #include "msgina.h"
  48. #include <stdio.h>
  49. #include <wchar.h>
  50. FILE * LogFile;
  51. DWORD BreakFlags = 0;
  52. extern DWORD NoUnload;
  53. DWORD GINAInfoLevel = 3;
  54. // Debugging support functions.
  55. // These two functions do not exist in Non-Debug builds. They are wrappers
  56. // to the commnot functions (maybe I should get rid of that as well...)
  57. // that echo the message to a log file.
  58. char szSection[] = "MSGina";
  59. char * DebLevel[] = {"GINA-Error",
  60. "GINA-Warn",
  61. "GINA-Trace",
  62. "GINA-Domain",
  63. "GINA-Cache"
  64. };
  65. typedef struct _DebugKeys {
  66. char * Name;
  67. DWORD Value;
  68. } DebugKeys, *PDebugKeys;
  69. DebugKeys DebugKeyNames[] = {
  70. {"Error", DEB_ERROR},
  71. {"Warning", DEB_WARN},
  72. {"Trace", DEB_TRACE},
  73. {"Domain", DEB_TRACE_DOMAIN},
  74. {"Cache", DEB_TRACE_CACHE}
  75. };
  76. #define NUM_DEBUG_KEYS sizeof(DebugKeyNames) / sizeof(DebugKeys)
  77. #define NUM_BREAK_KEYS sizeof(BreakKeyNames) / sizeof(DebugKeys)
  78. //+---------------------------------------------------------------------------
  79. //
  80. // Function: LogEvent
  81. //
  82. // Synopsis: Logs an event to the console and, optionally, a file.
  83. //
  84. // Effects:
  85. //
  86. // Arguments: [Mask] --
  87. // [Format] --
  88. // [Format] --
  89. //
  90. // Requires:
  91. //
  92. // Returns:
  93. //
  94. // Signals:
  95. //
  96. // Modifies:
  97. //
  98. // Algorithm:
  99. //
  100. // History: 4-29-93 RichardW Created
  101. //
  102. // Notes:
  103. //
  104. //----------------------------------------------------------------------------
  105. void
  106. LogEvent( long Mask,
  107. const char * Format,
  108. ...)
  109. {
  110. va_list ArgList;
  111. int Level = 0;
  112. int PrefixSize = 0;
  113. char szOutString[256];
  114. if (Mask & GINAInfoLevel)
  115. {
  116. while (!(Mask & 1))
  117. {
  118. Level++;
  119. Mask >>= 1;
  120. }
  121. if (Level >= (sizeof(DebLevel) / sizeof(char *)) )
  122. {
  123. Level = (sizeof(DebLevel) / sizeof(char *)) - 1;
  124. }
  125. //
  126. // Make the prefix first: "Process.Thread> GINA-XXX"
  127. //
  128. PrefixSize = sprintf(szOutString, "%d.%d> %s: ",
  129. GetCurrentProcessId(), GetCurrentThreadId(), DebLevel[Level]);
  130. va_start(ArgList, Format);
  131. if (_vsnprintf(&szOutString[PrefixSize], sizeof(szOutString) - PrefixSize,
  132. Format, ArgList) < 0)
  133. {
  134. //
  135. // Less than zero indicates that the string could not be
  136. // fitted into the buffer. Output a special message indicating
  137. // that:
  138. //
  139. OutputDebugStringA("GINA!LogEvent: Could not pack string into 256 bytes\n");
  140. }
  141. else
  142. {
  143. OutputDebugStringA(szOutString);
  144. }
  145. if (LogFile)
  146. {
  147. SYSTEMTIME stTime;
  148. FILETIME ftTime;
  149. FILETIME localtime;
  150. NtQuerySystemTime((PLARGE_INTEGER) &ftTime);
  151. FileTimeToLocalFileTime(&ftTime, &localtime);
  152. FileTimeToSystemTime(&localtime, &stTime);
  153. fprintf(LogFile, "%02d:%02d:%02d.%03d: %s\n",
  154. stTime.wHour, stTime.wMinute, stTime.wSecond,
  155. stTime.wMilliseconds, szOutString);
  156. fflush(LogFile);
  157. }
  158. }
  159. }
  160. void
  161. OpenLogFile(LPSTR pszLogFile)
  162. {
  163. LogFile = fopen(pszLogFile, "a");
  164. if (!LogFile)
  165. {
  166. OutputDebugStringA("GINA: Could not open logfile for append");
  167. OutputDebugStringA(pszLogFile);
  168. }
  169. DebugLog((DEB_TRACE, "Log file '%s' begins\n", pszLogFile));
  170. }
  171. DWORD
  172. GetDebugKeyValue(
  173. PDebugKeys KeyTable,
  174. int cKeys,
  175. LPSTR pszKey)
  176. {
  177. int i;
  178. for (i = 0; i < cKeys ; i++ )
  179. {
  180. if (_strcmpi(KeyTable[i].Name, pszKey) == 0)
  181. {
  182. return(KeyTable[i].Value);
  183. }
  184. }
  185. return(0);
  186. }
  187. //+---------------------------------------------------------------------------
  188. //
  189. // Function: LoadDebugParameters
  190. //
  191. // Synopsis: Loads debug parameters from win.ini
  192. //
  193. // Effects:
  194. //
  195. // Arguments: (none)
  196. //
  197. // Requires:
  198. //
  199. // Returns:
  200. //
  201. // Signals:
  202. //
  203. // Modifies:
  204. //
  205. // Algorithm:
  206. //
  207. // History: 4-29-93 RichardW Created
  208. //
  209. // Notes:
  210. //
  211. //----------------------------------------------------------------------------
  212. void
  213. LoadDebugParameters(void)
  214. {
  215. char szVal[128];
  216. char * pszDebug;
  217. int cbVal;
  218. cbVal = GetProfileStringA(szSection, "DebugFlags", "Error,Warning", szVal, ARRAYSIZE(szVal));
  219. pszDebug = strtok(szVal, ", \t");
  220. while (pszDebug)
  221. {
  222. GINAInfoLevel |= GetDebugKeyValue(DebugKeyNames, NUM_DEBUG_KEYS, pszDebug);
  223. pszDebug = strtok(NULL, ", \t");
  224. }
  225. cbVal = GetProfileStringA(szSection, "LogFile", "", szVal, ARRAYSIZE(szVal));
  226. if (cbVal)
  227. {
  228. OpenLogFile(szVal);
  229. }
  230. }
  231. //+---------------------------------------------------------------------------
  232. //
  233. // Function: InitDebugSupport
  234. //
  235. // Synopsis: Initializes debugging support for the GINAgr
  236. //
  237. // Effects:
  238. //
  239. // Arguments: (none)
  240. //
  241. // Requires:
  242. //
  243. // Returns:
  244. //
  245. // Signals:
  246. //
  247. // Modifies:
  248. //
  249. // Algorithm:
  250. //
  251. // History: 4-29-93 RichardW Created
  252. //
  253. // Notes:
  254. //
  255. //----------------------------------------------------------------------------
  256. void
  257. InitDebugSupport(void)
  258. {
  259. LoadDebugParameters();
  260. }
  261. #else // DBG
  262. #pragma warning(disable:4206) // Disable the empty transation unit
  263. // warning/error
  264. #endif // NOTE: This file not compiled for retail builds