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.

448 lines
11 KiB

  1. //*************************************************************
  2. //
  3. // Debugging functions
  4. //
  5. // Microsoft Confidential
  6. // Copyright (c) Microsoft Corporation 1995
  7. // All rights reserved
  8. //
  9. //*************************************************************
  10. #include "uenv.h"
  11. #include "rsopdbg.h"
  12. #include "strsafe.h"
  13. //
  14. // Global Variable containing the debugging level.
  15. //
  16. DWORD dwDebugLevel;
  17. DWORD dwRsopLoggingLevel = 1; // Rsop logging setting
  18. //
  19. // Debug strings
  20. //
  21. const TCHAR c_szUserEnv[] = TEXT("USERENV(%x.%x) %02d:%02d:%02d:%03d ");
  22. const TCHAR c_szCRLF[] = TEXT("\r\n");
  23. //
  24. // Registry debug information
  25. //
  26. #define DEBUG_REG_LOCATION TEXT("Software\\Microsoft\\Windows NT\\CurrentVersion\\winlogon")
  27. #define DEBUG_KEY_NAME TEXT("UserEnvDebugLevel")
  28. #define RSOP_KEY_NAME TEXT("RsopLogging")
  29. //
  30. // Log files
  31. //
  32. TCHAR szLogFileName[] = L"%SystemRoot%\\Debug\\UserMode\\userenv.log"; // Current log
  33. TCHAR szBackupLogFileName[] = L"%SystemRoot%\\Debug\\UserMode\\userenv.bak"; // Backup/previous log
  34. CDebug dbgCommon;
  35. //*************************************************************
  36. //
  37. // InitDebugSupport()
  38. //
  39. // Purpose: Sets the debugging level.
  40. // Also checks the registry for a debugging level.
  41. //
  42. // Parameters: dwLoadFlags - If this is being loaded by winlogon
  43. // or setup.exe
  44. //
  45. // Return: void
  46. //
  47. // Comments:
  48. //
  49. //
  50. // History: Date Author Comment
  51. // 5/25/95 ericflo Created
  52. // 04/10/2002 mingzhu Changed the default level to none
  53. //
  54. //*************************************************************
  55. void InitDebugSupport( DWORD dwLoadFlags )
  56. {
  57. LONG lResult;
  58. HKEY hKey;
  59. DWORD dwType, dwSize, dwRet;
  60. WIN32_FILE_ATTRIBUTE_DATA FileData;
  61. //
  62. // Initialize the debug level to normal for checked builds, and none for retail builds.
  63. //
  64. #if DBG
  65. dwDebugLevel = DL_NORMAL | DL_LOGFILE | DL_DEBUGGER;
  66. #else
  67. dwDebugLevel = DL_NONE;
  68. #endif
  69. dwRsopLoggingLevel = 1;
  70. //
  71. // Check the registry
  72. //
  73. lResult = RegOpenKey (HKEY_LOCAL_MACHINE, DEBUG_REG_LOCATION,
  74. &hKey);
  75. if (lResult == ERROR_SUCCESS) {
  76. dwSize = sizeof(dwDebugLevel);
  77. RegQueryValueEx(hKey, DEBUG_KEY_NAME, NULL, &dwType,
  78. (LPBYTE)&dwDebugLevel, &dwSize);
  79. dwSize = sizeof(dwRsopLoggingLevel);
  80. RegQueryValueEx(hKey, RSOP_KEY_NAME, NULL, &dwType,
  81. (LPBYTE)&dwRsopLoggingLevel, &dwSize);
  82. RegCloseKey(hKey);
  83. }
  84. lResult = RegOpenKey (HKEY_LOCAL_MACHINE, SYSTEM_POLICIES_KEY,
  85. &hKey);
  86. if (lResult == ERROR_SUCCESS) {
  87. dwSize = sizeof(dwDebugLevel);
  88. RegQueryValueEx(hKey, DEBUG_KEY_NAME, NULL, &dwType,
  89. (LPBYTE)&dwDebugLevel, &dwSize);
  90. dwSize = sizeof(dwRsopLoggingLevel);
  91. RegQueryValueEx(hKey, RSOP_KEY_NAME, NULL, &dwType,
  92. (LPBYTE)&dwRsopLoggingLevel, &dwSize);
  93. RegCloseKey(hKey);
  94. }
  95. //
  96. // To avoid a huge log file, copy current log file to backup
  97. // file if the log file is over 300K
  98. //
  99. if ( dwLoadFlags == WINLOGON_LOAD ) {
  100. TCHAR szExpLogFileName[MAX_PATH];
  101. TCHAR szExpBackupLogFileName[MAX_PATH];
  102. dwRet = ExpandEnvironmentStrings ( szLogFileName, szExpLogFileName, MAX_PATH);
  103. if ( dwRet == 0 || dwRet > MAX_PATH)
  104. return;
  105. if (!GetFileAttributesEx(szExpLogFileName, GetFileExInfoStandard, &FileData)) {
  106. return;
  107. }
  108. if ( FileData.nFileSizeLow < (300 * 1024) ) {
  109. return;
  110. }
  111. dwRet = ExpandEnvironmentStrings ( szBackupLogFileName, szExpBackupLogFileName, MAX_PATH);
  112. if ( dwRet == 0 || dwRet > MAX_PATH)
  113. return;
  114. dwRet = MoveFileEx( szExpLogFileName, szExpBackupLogFileName, MOVEFILE_REPLACE_EXISTING);
  115. if ( dwRet == 0 ) {
  116. DebugMsg((DM_VERBOSE, TEXT("Moving log file to backup failed with 0x%x"), GetLastError()));
  117. return;
  118. }
  119. }
  120. }
  121. //*************************************************************
  122. //
  123. // DebugMsg()
  124. //
  125. // Purpose: Displays debug messages based on the debug level
  126. // and type of debug message.
  127. //
  128. // Parameters: mask - debug message type
  129. // pszMsg - debug message
  130. // ... - variable number of parameters
  131. //
  132. // Return: void
  133. //
  134. //
  135. // Comments:
  136. //
  137. //
  138. // History: Date Author Comment
  139. // 5/25/95 ericflo Created
  140. //
  141. //*************************************************************
  142. void _DebugMsg(UINT mask, LPCTSTR pszMsg, ...)
  143. {
  144. BOOL bOutput;
  145. TCHAR szDebugTitle[60];
  146. LPTSTR lpDebugBuffer;
  147. va_list marker;
  148. DWORD dwErrCode;
  149. SYSTEMTIME systime;
  150. BOOL bDebugOutput = FALSE;
  151. BOOL bLogfileOutput = FALSE;
  152. //
  153. // Save the last error code (so the debug output doesn't change it).
  154. //
  155. dwErrCode = GetLastError();
  156. //
  157. // Determine the correct amount of debug output
  158. //
  159. switch (LOWORD(dwDebugLevel)) {
  160. case DL_VERBOSE:
  161. bOutput = TRUE;
  162. break;
  163. case DL_NORMAL:
  164. //
  165. // Normal debug output. Don't
  166. // display verbose stuff, but
  167. // do display warnings/asserts.
  168. //
  169. if (mask != DM_VERBOSE) {
  170. bOutput = TRUE;
  171. } else {
  172. bOutput = FALSE;
  173. }
  174. break;
  175. case DL_NONE:
  176. default:
  177. //
  178. // Only display asserts
  179. //
  180. if (mask == DM_ASSERT) {
  181. bOutput = TRUE;
  182. } else {
  183. bOutput = FALSE;
  184. }
  185. break;
  186. }
  187. //
  188. // Display the error message if appropriate
  189. //
  190. bDebugOutput = dwDebugLevel & DL_DEBUGGER;
  191. bLogfileOutput = dwDebugLevel & DL_LOGFILE;
  192. if (bOutput) {
  193. lpDebugBuffer = (LPTSTR) LocalAlloc (LPTR, 2048 * sizeof(TCHAR));
  194. if (lpDebugBuffer) {
  195. GetLocalTime (&systime);
  196. StringCchPrintf(szDebugTitle, ARRAYSIZE(szDebugTitle), c_szUserEnv,
  197. GetCurrentProcessId(), GetCurrentThreadId(),
  198. systime.wHour, systime.wMinute, systime.wSecond,
  199. systime.wMilliseconds);
  200. if ( bDebugOutput)
  201. OutputDebugString(szDebugTitle);
  202. va_start(marker, pszMsg);
  203. StringCchVPrintf (lpDebugBuffer, 2048, pszMsg, marker);
  204. if ( bDebugOutput) {
  205. OutputDebugString(lpDebugBuffer);
  206. OutputDebugString(c_szCRLF);
  207. }
  208. va_end(marker);
  209. if ( bLogfileOutput ) {
  210. HANDLE hFile;
  211. DWORD dwBytesWritten;
  212. LPTSTR szExpLogFileName;
  213. szExpLogFileName = (LPTSTR) LocalAlloc (LPTR, MAX_PATH * sizeof(TCHAR));
  214. if (szExpLogFileName)
  215. {
  216. DWORD dwRet = ExpandEnvironmentStrings ( szLogFileName, szExpLogFileName, MAX_PATH);
  217. if ( dwRet != 0 && dwRet <= MAX_PATH) {
  218. hFile = CreateFile( szExpLogFileName,
  219. FILE_WRITE_DATA | FILE_APPEND_DATA,
  220. FILE_SHARE_READ,
  221. NULL,
  222. OPEN_ALWAYS,
  223. FILE_ATTRIBUTE_NORMAL,
  224. NULL);
  225. if (hFile != INVALID_HANDLE_VALUE) {
  226. if (SetFilePointer (hFile, 0, NULL, FILE_END) != 0xFFFFFFFF) {
  227. WriteFile (hFile, (LPCVOID) szDebugTitle,
  228. lstrlen (szDebugTitle) * sizeof(TCHAR),
  229. &dwBytesWritten,
  230. NULL);
  231. WriteFile (hFile, (LPCVOID) lpDebugBuffer,
  232. lstrlen (lpDebugBuffer) * sizeof(TCHAR),
  233. &dwBytesWritten,
  234. NULL);
  235. WriteFile (hFile, (LPCVOID) c_szCRLF,
  236. lstrlen (c_szCRLF) * sizeof(TCHAR),
  237. &dwBytesWritten,
  238. NULL);
  239. }
  240. CloseHandle (hFile);
  241. }
  242. }
  243. LocalFree(szExpLogFileName);
  244. }
  245. }
  246. LocalFree (lpDebugBuffer);
  247. }
  248. }
  249. //
  250. // Restore the last error code
  251. //
  252. SetLastError(dwErrCode);
  253. //
  254. // Break to the debugger if appropriate
  255. //
  256. #if DBG
  257. if (mask == DM_ASSERT) {
  258. DebugBreak();
  259. }
  260. #endif
  261. }
  262. //*************************************************************
  263. //
  264. // RsopLoggingEnabled()
  265. //
  266. // Purpose: Checks if Rsop logging is enabled.
  267. //
  268. //*************************************************************
  269. extern "C"
  270. BOOL RsopLoggingEnabled()
  271. {
  272. return dwRsopLoggingLevel != 0;
  273. }
  274. //*************************************************************
  275. //
  276. // DeletePreviousLogFiles()
  277. //
  278. // Purpose: Try to delete the previous userenv log file during
  279. // setup if the debug level is NONE
  280. //
  281. // Parameters:
  282. //
  283. // Return: S_OK on success, else for failure
  284. //
  285. // Comments: Called by DetermineProfileLocation(), which is the first
  286. // function call during setup.
  287. //
  288. // History: Date Author Comment
  289. // 04/10/2002 mingzhu Created
  290. //
  291. //*************************************************************
  292. HRESULT DeletePreviousLogFiles()
  293. {
  294. HRESULT hr = E_FAIL;
  295. TCHAR szExpLogFileName[MAX_PATH];
  296. //
  297. // First check the debug level, if it is not NONE, do nothing
  298. //
  299. if (LOWORD(dwDebugLevel) != DL_NONE)
  300. {
  301. hr = S_OK;
  302. goto Exit;
  303. }
  304. //
  305. // Delete the log file
  306. //
  307. hr = SafeExpandEnvironmentStrings(szLogFileName, szExpLogFileName, ARRAYSIZE(szExpLogFileName));
  308. if (FAILED(hr))
  309. {
  310. goto Exit;
  311. }
  312. if (!DeleteFile (szExpLogFileName))
  313. {
  314. DWORD dwErr = GetLastError();
  315. if (dwErr != ERROR_FILE_NOT_FOUND)
  316. {
  317. hr = HRESULT_FROM_WIN32(dwErr);
  318. goto Exit;
  319. }
  320. }
  321. //
  322. // Delete the backup log file
  323. //
  324. hr = SafeExpandEnvironmentStrings(szBackupLogFileName, szExpLogFileName, ARRAYSIZE(szExpLogFileName));
  325. if (FAILED(hr))
  326. {
  327. goto Exit;
  328. }
  329. if (!DeleteFile (szExpLogFileName))
  330. {
  331. DWORD dwErr = GetLastError();
  332. if (dwErr != ERROR_FILE_NOT_FOUND)
  333. {
  334. hr = HRESULT_FROM_WIN32(dwErr);
  335. goto Exit;
  336. }
  337. }
  338. hr = S_OK;
  339. Exit:
  340. return hr;
  341. }