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.

280 lines
6.2 KiB

  1. /*++
  2. Copyright (c) 1995 Microsoft Corporation
  3. Module Name:
  4. debug.c
  5. Abstract:
  6. Diagnositc/debug routines for Windows NT Setup module.
  7. Author:
  8. Ted Miller (tedm) 31-Mar-1995
  9. Revision History:
  10. --*/
  11. #include "setupp.h"
  12. #pragma hdrstop
  13. //
  14. // This can be turned on in the debugger so that we get debug spew on free builds.
  15. //
  16. bWriteDebugSpew = FALSE;
  17. #if DBG
  18. VOID
  19. AssertFail(
  20. IN PSTR FileName,
  21. IN UINT LineNumber,
  22. IN PSTR Condition
  23. )
  24. {
  25. int i;
  26. CHAR Name[MAX_PATH];
  27. PCHAR p;
  28. CHAR Msg[4096];
  29. //
  30. // Use dll name as caption
  31. //
  32. GetModuleFileNameA(MyModuleHandle,Name,MAX_PATH);
  33. if(p = strrchr(Name,'\\')) {
  34. p++;
  35. } else {
  36. p = Name;
  37. }
  38. wsprintfA(
  39. Msg,
  40. "Assertion failure at line %u in file %s: %s\n\nCall DebugBreak()?",
  41. LineNumber,
  42. FileName,
  43. Condition
  44. );
  45. i = MessageBoxA(
  46. NULL,
  47. Msg,
  48. p,
  49. MB_YESNO | MB_TASKMODAL | MB_ICONSTOP | MB_SETFOREGROUND
  50. );
  51. if(i == IDYES) {
  52. DebugBreak();
  53. }
  54. }
  55. #endif
  56. VOID
  57. pSetupDebugPrint(
  58. PWSTR FileName,
  59. ULONG LineNumber,
  60. PWSTR TagStr,
  61. PWSTR FormatStr,
  62. ...
  63. )
  64. {
  65. static WCHAR buf[4096];
  66. static HANDLE hFile = NULL;
  67. va_list arg_ptr;
  68. ULONG Bytes;
  69. PWSTR s,p;
  70. PSTR str;
  71. SYSTEMTIME CurrTime;
  72. DWORD Result;
  73. //
  74. // Note: If hFile is NULL, that means it's the first time we've been called,
  75. // and we may want to open the log file. If we set hFile to
  76. // INVALID_HANDLE_VALUE, that means we've decided not to write to the file.
  77. //
  78. #if DBG
  79. {
  80. //
  81. // If OobeSetup is FALSE when we are first called, and becomes TRUE at
  82. // some later point, logging doesn't work. This ASSERT makes sure that
  83. // doesn't happen.
  84. //
  85. static BOOL OobeSetOnFirstCall = FALSE;
  86. if ( hFile == NULL ) {
  87. OobeSetOnFirstCall = OobeSetup;
  88. }
  89. MYASSERT( OobeSetOnFirstCall == OobeSetup );
  90. }
  91. #endif
  92. GetLocalTime( &CurrTime );
  93. if (hFile == NULL) {
  94. if ( IsSetup || OobeSetup ) {
  95. Result = GetWindowsDirectory( buf, sizeof(buf)/sizeof(WCHAR) );
  96. if(Result == 0) {
  97. MYASSERT(FALSE);
  98. return;
  99. }
  100. pSetupConcatenatePaths( buf, L"setuplog.txt", sizeof(buf)/sizeof(WCHAR), NULL );
  101. //
  102. // If we're in OOBE, we want to append to the file that already exists
  103. //
  104. hFile = CreateFile(
  105. buf,
  106. GENERIC_READ | GENERIC_WRITE,
  107. FILE_SHARE_READ | FILE_SHARE_WRITE,
  108. NULL,
  109. OobeSetup ? OPEN_ALWAYS : CREATE_ALWAYS,
  110. FILE_ATTRIBUTE_NORMAL | FILE_FLAG_WRITE_THROUGH,
  111. NULL
  112. );
  113. if (hFile != INVALID_HANDLE_VALUE)
  114. {
  115. if (OobeSetup) {
  116. SetFilePointer (hFile, 0, NULL, FILE_END);
  117. }
  118. swprintf(buf, L"Time,File,Line,Tag,Message\r\n");
  119. Bytes = wcslen(buf) + 4;
  120. #if defined(_ENABLE_SAC_CHANNEL_LOGGING_)
  121. if (SacChannelGuiModeDebugEnabled) {
  122. SacChannelUnicodeWrite(
  123. SacChannelGuiModeDebugHandle,
  124. (PCWSTR)buf
  125. );
  126. }
  127. #endif
  128. str = MyMalloc(Bytes);
  129. if (str != NULL)
  130. {
  131. WideCharToMultiByte(
  132. CP_ACP,
  133. 0,
  134. buf,
  135. -1,
  136. str,
  137. Bytes,
  138. NULL,
  139. NULL
  140. );
  141. WriteFile(
  142. hFile,
  143. str,
  144. wcslen(buf),
  145. &Bytes,
  146. NULL
  147. );
  148. MyFree( str );
  149. }
  150. buf[0] = '\0';
  151. }
  152. } else { // !IsSetup
  153. //
  154. // Don't write to file, just do DbgPrintEx
  155. //
  156. hFile = INVALID_HANDLE_VALUE;
  157. }
  158. }
  159. _try {
  160. p = buf;
  161. *p = 0;
  162. swprintf( p, L"%02d/%02d/%04d %02d:%02d:%02d,%s,%d,%s,",
  163. CurrTime.wMonth,
  164. CurrTime.wDay,
  165. CurrTime.wYear,
  166. CurrTime.wHour,
  167. CurrTime.wMinute,
  168. CurrTime.wSecond,
  169. (NULL != FileName) ? FileName : L"",
  170. LineNumber,
  171. (NULL != TagStr) ? TagStr : L""
  172. );
  173. p += wcslen(p);
  174. va_start( arg_ptr, FormatStr );
  175. _vsnwprintf( p, 2048, FormatStr, arg_ptr );
  176. va_end( arg_ptr );
  177. p += wcslen(p);
  178. wcscat( p, L"\r\n" );
  179. } except(EXCEPTION_EXECUTE_HANDLER) {
  180. buf[0] = 0;
  181. }
  182. if (buf[0] == 0) {
  183. return;
  184. }
  185. Bytes = (wcslen( buf )*2) + 4;
  186. str = MyMalloc( Bytes );
  187. if (str == NULL) {
  188. return;
  189. }
  190. WideCharToMultiByte(
  191. CP_ACP,
  192. 0,
  193. buf,
  194. -1,
  195. str,
  196. Bytes,
  197. NULL,
  198. NULL
  199. );
  200. //
  201. // Write out the string to the debugger if the process is being debugged, or the
  202. // debug filter allows it.
  203. //
  204. if ( bWriteDebugSpew ) {
  205. OutputDebugString( buf );
  206. } else {
  207. #if DBG
  208. DbgPrintEx( DPFLTR_SETUP_ID, DPFLTR_INFO_LEVEL, str );
  209. #endif
  210. }
  211. #if defined(_ENABLE_SAC_CHANNEL_LOGGING_)
  212. if (SacChannelGuiModeDebugEnabled) {
  213. SacChannelUnicodeWrite(
  214. SacChannelGuiModeDebugHandle,
  215. (PCWSTR)buf
  216. );
  217. }
  218. #endif
  219. if (hFile == INVALID_HANDLE_VALUE) {
  220. MyFree( str );
  221. return;
  222. }
  223. WriteFile(
  224. hFile,
  225. str,
  226. wcslen(buf),
  227. &Bytes,
  228. NULL
  229. );
  230. MyFree( str );
  231. return;
  232. }