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.

608 lines
17 KiB

  1. //////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright (c) 2000 Microsoft Corporation
  4. //
  5. // Module Name:
  6. // SWTracingSrc.cpp
  7. //
  8. // Description:
  9. // Software tracing functions.
  10. //
  11. // Documentation:
  12. // Spec\Admin\Debugging.ppt
  13. //
  14. // Maintained By:
  15. // Galen Barbee (GalenB) 05-DEC-2000
  16. //
  17. //////////////////////////////////////////////////////////////////////////////
  18. #include <stdio.h>
  19. #include <tchar.h>
  20. //#define DebugDumpWMITraceFlags( _arg ) // NOP
  21. //
  22. // TODO: remove this when WMI is more dynamic
  23. //
  24. static const int cchDEBUG_OUTPUT_BUFFER_SIZE = 512;
  25. //****************************************************************************
  26. //
  27. // WMI Tracing Routines
  28. //
  29. // These routines are only in the DEBUG version.
  30. //
  31. //****************************************************************************
  32. //////////////////////////////////////////////////////////////////////////////
  33. //++
  34. //
  35. // void
  36. // DebugDumpWMITraceFlags(
  37. // DEBUG_WMI_CONTROL_GUIDS * pdwcgIn
  38. // )
  39. //
  40. // Description:
  41. // Dumps the currently set flags.
  42. //
  43. // Arguments:
  44. // pdwcgIn - Pointer to the DEBUG_WMI_CONTROL_GUIDS to explore.
  45. //
  46. // Return Values:
  47. // None.
  48. //
  49. //--
  50. //////////////////////////////////////////////////////////////////////////////
  51. void
  52. DebugDumpWMITraceFlags(
  53. DEBUG_WMI_CONTROL_GUIDS * pdwcgIn
  54. )
  55. {
  56. DWORD dwFlag;
  57. signed int nCount;
  58. if ( pdwcgIn->pMapFlagsToComments == NULL )
  59. {
  60. DebugMsg( TEXT("DEBUG: no flag mappings available.") );
  61. return; // NOP
  62. } // if: no mapping table
  63. dwFlag = 0x80000000;
  64. for ( nCount = 31; nCount >= 0; nCount-- )
  65. {
  66. if ( pdwcgIn->dwFlags & dwFlag )
  67. {
  68. DebugMsg( TEXT("DEBUG: 0x%08x = %s"),
  69. dwFlag,
  70. ( pdwcgIn->pMapFlagsToComments[ nCount ].pszName != NULL ? pdwcgIn->pMapFlagsToComments[ nCount ].pszName : g_szUnknown )
  71. );
  72. } // if: flag set
  73. dwFlag = dwFlag >> 1;
  74. } // for: nCount
  75. } //*** DebugDumpWMITraceFlags( )
  76. //****************************************************************************
  77. //
  78. // WMI Tracing Routines
  79. //
  80. // These routines are in both DEBUG and RETAIL versions.
  81. //
  82. //****************************************************************************
  83. //////////////////////////////////////////////////////////////////////////////
  84. //++
  85. //
  86. // ULONG
  87. // UlWMIControlCallback(
  88. // IN WMIDPREQUESTCODE RequestCode,
  89. // IN PVOID RequestContext,
  90. // IN OUT ULONG * BufferSize,
  91. // IN OUT PVOID Buffer
  92. // )
  93. //
  94. // Description:
  95. // WMI's tracing control callback entry point.
  96. //
  97. // Arguments:
  98. // See EVNTRACE.H
  99. //
  100. // Return Values:
  101. // ERROR_SUCCESS - success.
  102. // ERROR_INVALID_PARAMETER - invalid RequestCode passed in.
  103. //
  104. //--
  105. //////////////////////////////////////////////////////////////////////////////
  106. ULONG
  107. WINAPI
  108. UlWMIControlCallback(
  109. IN WMIDPREQUESTCODE RequestCode,
  110. IN PVOID RequestContext,
  111. IN OUT ULONG * BufferSize,
  112. IN OUT PVOID Buffer
  113. )
  114. {
  115. ULONG ulResult = ERROR_SUCCESS;
  116. DEBUG_WMI_CONTROL_GUIDS * pdwcg = (DEBUG_WMI_CONTROL_GUIDS *) RequestContext;
  117. switch ( RequestCode )
  118. {
  119. case WMI_ENABLE_EVENTS:
  120. {
  121. TRACEHANDLE hTrace;
  122. if ( pdwcg->hTrace == NULL )
  123. {
  124. hTrace = GetTraceLoggerHandle( Buffer );
  125. } // if:
  126. else
  127. {
  128. hTrace = pdwcg->hTrace;
  129. } // else:
  130. pdwcg->dwFlags = GetTraceEnableFlags( hTrace );
  131. pdwcg->bLevel = GetTraceEnableLevel( hTrace );
  132. if ( pdwcg->dwFlags == 0 )
  133. {
  134. if ( pdwcg->pMapLevelToFlags != NULL
  135. && pdwcg->bSizeOfLevelList != 0
  136. )
  137. {
  138. if ( pdwcg->bLevel >= pdwcg->bSizeOfLevelList )
  139. {
  140. pdwcg->bLevel = pdwcg->bSizeOfLevelList - 1;
  141. }
  142. pdwcg->dwFlags = pdwcg->pMapLevelToFlags[ pdwcg->bLevel ].dwFlags;
  143. DebugMsg( TEXT("DEBUG: WMI tracing level set to %u - %s, flags = 0x%08x"),
  144. pdwcg->pszName,
  145. pdwcg->bLevel,
  146. pdwcg->pMapLevelToFlags[ pdwcg->bLevel ].pszName,
  147. pdwcg->dwFlags
  148. );
  149. } // if: level->flag mapping available
  150. else
  151. {
  152. DebugMsg( TEXT("DEBUG: WMI tracing level set to %u, flags = 0x%08x"),
  153. pdwcg->pszName,
  154. pdwcg->bLevel,
  155. pdwcg->dwFlags
  156. );
  157. } // else: no mappings
  158. } // if: no flags set
  159. else
  160. {
  161. DebugMsg( TEXT("DEBUG: WMI tracing level set to %u, flags = 0x%08x"),
  162. pdwcg->pszName,
  163. pdwcg->bLevel,
  164. pdwcg->dwFlags
  165. );
  166. } // else: flags set
  167. DebugDumpWMITraceFlags( pdwcg );
  168. pdwcg->hTrace = hTrace;
  169. } // WMI_ENABLE_EVENTS
  170. break;
  171. case WMI_DISABLE_EVENTS:
  172. pdwcg->dwFlags = 0;
  173. pdwcg->bLevel = 0;
  174. pdwcg->hTrace = NULL;
  175. DebugMsg( TEXT("DEBUG: %s WMI tracing disabled."), pdwcg->pszName );
  176. break;
  177. default:
  178. ulResult = ERROR_INVALID_PARAMETER;
  179. break;
  180. } // switch: RequestCode
  181. return ulResult;
  182. } //*** UlWMIControlCallback( )
  183. //////////////////////////////////////////////////////////////////////////////
  184. //++
  185. //
  186. // void
  187. // WMIInitializeTracing(
  188. // DEBUG_WMI_CONTROL_GUIDS dwcgControlListIn[],
  189. // int nCountOfCOntrolGuidsIn
  190. // )
  191. //
  192. // Description:
  193. // Initialize the WMI tracing.
  194. //
  195. // Arguments:
  196. // None.
  197. //
  198. // Return Values:
  199. // None.
  200. //
  201. //--
  202. //////////////////////////////////////////////////////////////////////////////
  203. void
  204. WMIInitializeTracing(
  205. DEBUG_WMI_CONTROL_GUIDS dwcgControlListIn[],
  206. int nCountOfControlGuidsIn
  207. )
  208. {
  209. TCHAR szPath[ MAX_PATH ];
  210. ULONG ulStatus;
  211. int nCount;
  212. #if defined( DEBUG )
  213. static BOOL fRegistered = FALSE;
  214. AssertMsg( fRegistered == FALSE, "Re-entrance into InitializeWMITracing!!!" );
  215. #endif // DEBUG
  216. GetModuleFileName( g_hInstance, szPath, MAX_PATH );
  217. for( nCount = 0; nCount < nCountOfControlGuidsIn; nCount++ )
  218. {
  219. TRACE_GUID_REGISTRATION * pList;
  220. pList = (TRACE_GUID_REGISTRATION *)
  221. HeapAlloc( GetProcessHeap(), 0,
  222. sizeof( TRACE_GUID_REGISTRATION ) * dwcgControlListIn[ nCount ].dwSizeOfTraceList );
  223. if ( pList != NULL )
  224. {
  225. CopyMemory( pList,
  226. dwcgControlListIn[ nCount ].pTraceList,
  227. sizeof( TRACE_GUID_REGISTRATION ) * dwcgControlListIn[ nCount ].dwSizeOfTraceList );
  228. ulStatus = RegisterTraceGuids( UlWMIControlCallback, // IN RequestAddress
  229. dwcgControlListIn, // IN RequestContext
  230. dwcgControlListIn[ nCount ].guidControl, // IN ControlGuid
  231. dwcgControlListIn[ nCount ].dwSizeOfTraceList, // IN GuidCount
  232. pList, // IN GuidReg
  233. (LPCTSTR) szPath, // IN MofImagePath
  234. __MODULE__, // IN MofResourceName
  235. &dwcgControlListIn[ nCount ].hRegistration // OUT RegistrationHandle
  236. );
  237. AssertMsg( ulStatus == ERROR_SUCCESS, "Trace registration failed" );
  238. HeapFree( GetProcessHeap(), 0, pList );
  239. } // if: list allocated successfully
  240. } // for: each control GUID
  241. #if defined( DEBUG )
  242. fRegistered = TRUE;
  243. #endif // DEBUG
  244. } //*** WMIInitializeTracing( )
  245. //////////////////////////////////////////////////////////////////////////////
  246. //++
  247. //
  248. // void
  249. // WMITerminateTracing(
  250. // DEBUG_WMI_CONTROL_GUIDS dwcgControlListIn[],
  251. // int nCountOfCOntrolGuidsIn
  252. // )
  253. //
  254. // Description:
  255. // Terminates WMI tracing and unregisters the interfaces.
  256. //
  257. // Arguments:
  258. // dwcgControlListIn -
  259. // nCountOfControlGuidsIn -
  260. //
  261. // Return Values:
  262. // None.
  263. //
  264. //--
  265. //////////////////////////////////////////////////////////////////////////////
  266. void
  267. WMITerminateTracing(
  268. DEBUG_WMI_CONTROL_GUIDS dwcgControlListIn[],
  269. int nCountOfControlGuidsIn
  270. )
  271. {
  272. int nCount;
  273. for( nCount = 0; nCount < nCountOfControlGuidsIn; nCount++ )
  274. {
  275. dwcgControlListIn[ nCount ].dwFlags = 0;
  276. dwcgControlListIn[ nCount ].bLevel = 0;
  277. dwcgControlListIn[ nCount ].hTrace = NULL;
  278. UnregisterTraceGuids( dwcgControlListIn[ nCount ].hRegistration );
  279. } // for: each control GUID
  280. } //*** WMITerminateTracing( )
  281. typedef struct
  282. {
  283. EVENT_TRACE_HEADER EHeader; // storage for the WMI trace event header
  284. BYTE bSize; // Size of the string - MAX 255!
  285. WCHAR szMsg[ cchDEBUG_OUTPUT_BUFFER_SIZE ]; // Message
  286. } DEBUG_WMI_EVENT_W;
  287. typedef struct
  288. {
  289. EVENT_TRACE_HEADER EHeader; // storage for the WMI trace event header
  290. BYTE bSize; // Size of the string - MAX 255!
  291. CHAR szMsg[ cchDEBUG_OUTPUT_BUFFER_SIZE ]; // Message
  292. } DEBUG_WMI_EVENT_A;
  293. //////////////////////////////////////////////////////////////////////////////
  294. //++
  295. //
  296. // void
  297. // WMIMessageByFlags(
  298. // DEBUG_WMI_CONTROL_GUIDS * pEntryIn,
  299. // const DWORD dwFlagsIn,
  300. // LPCWSTR pszFormatIn,
  301. // ...
  302. // )
  303. //
  304. // Description:
  305. //
  306. // Arguments:
  307. // pEntry -
  308. // dwFlagsIn -
  309. // pszFormatIn -
  310. // ... -
  311. //
  312. // Return Values:
  313. // None.
  314. //
  315. //--
  316. //////////////////////////////////////////////////////////////////////////////
  317. void
  318. __cdecl
  319. WMIMessageByFlags(
  320. DEBUG_WMI_CONTROL_GUIDS * pEntry,
  321. const DWORD dwFlagsIn,
  322. LPCWSTR pszFormatIn,
  323. ...
  324. )
  325. {
  326. va_list valist;
  327. if ( pEntry->hTrace == NULL )
  328. {
  329. return; // NOP
  330. } // if: no trace logger
  331. if ( dwFlagsIn == mtfALWAYS
  332. || pEntry->dwFlags & dwFlagsIn
  333. )
  334. {
  335. ULONG ulStatus;
  336. DEBUG_WMI_EVENT_W DebugEvent;
  337. PEVENT_TRACE_HEADER peth = (PEVENT_TRACE_HEADER ) &DebugEvent;
  338. PWNODE_HEADER pwnh = (PWNODE_HEADER) &DebugEvent;
  339. ZeroMemory( &DebugEvent, sizeof( DebugEvent ) );
  340. #ifndef UNICODE
  341. TCHAR szFormat[ cchDEBUG_OUTPUT_BUFFER_SIZE ];
  342. wcstombs( szFormat, pszFormatIn, StrLenW( pszFormatIn ) + 1 );
  343. va_start( valist, pszFormatIn );
  344. _vsntprintf( DebugEvent.szMsg, 256, szFormat, valist );
  345. va_end( valist );
  346. #else
  347. va_start( valist, pszFormatIn );
  348. _vsntprintf( DebugEvent.szMsg, 256, pszFormatIn, valist );
  349. va_end( valist );
  350. #endif // UNICODE
  351. //
  352. // Fill in the blanks
  353. //
  354. Assert( wcslen( DebugEvent.szMsg ) <= 255 ); // make sure the cast doesn't fail.
  355. DebugEvent.bSize = (BYTE) wcslen( DebugEvent.szMsg );
  356. peth->Size = sizeof( DebugEvent );
  357. peth->Class.Type = 10;
  358. peth->Class.Level = pEntry->bLevel;
  359. // peth->Class.Version = 0;
  360. // peth->ThreadId = GetCurrentThreadId( ); - done by system
  361. pwnh->Guid = *pEntry->guidControl;
  362. // peth->ClientContext = NULL;
  363. pwnh->Flags = WNODE_FLAG_TRACED_GUID;
  364. // GetSystemTimeAsFileTime( (FILETIME *) &peth->TimeStamp ); - done by the system
  365. ulStatus = TraceEvent( pEntry->hTrace, (PEVENT_TRACE_HEADER) &DebugEvent );
  366. } // if: flags set
  367. } //*** WMIMsg( )
  368. //////////////////////////////////////////////////////////////////////////////
  369. //++
  370. //
  371. // void
  372. // WMIMessageByFlagsAndLevel(
  373. // DEBUG_WMI_CONTROL_GUIDS * pEntry,
  374. // const DWORD dwFlagsIn,
  375. // const BYTE bLogLevelIn,
  376. // LPCWSTR pszFormatIn,
  377. // ...
  378. // )
  379. //
  380. // Description:
  381. //
  382. // Arguments:
  383. // pEntry -
  384. // dwFlagsIn -
  385. // bLogLevelIn -
  386. // pszFormatIn -
  387. // ... -
  388. //
  389. // Return Values:
  390. // None.
  391. //
  392. //--
  393. //////////////////////////////////////////////////////////////////////////////
  394. void
  395. __cdecl
  396. WMIMessageByFlagsAndLevel(
  397. DEBUG_WMI_CONTROL_GUIDS * pEntry,
  398. const DWORD dwFlagsIn,
  399. const BYTE bLogLevelIn,
  400. LPCWSTR pszFormatIn,
  401. ...
  402. )
  403. {
  404. va_list valist;
  405. if ( pEntry->hTrace == NULL )
  406. {
  407. return; // NOP
  408. } // if: no trace logger
  409. if ( bLogLevelIn > pEntry->bLevel )
  410. {
  411. return; // NOP
  412. } // if: level not high enough
  413. if ( dwFlagsIn == mtfALWAYS
  414. || pEntry->dwFlags & dwFlagsIn
  415. )
  416. {
  417. ULONG ulStatus;
  418. DEBUG_WMI_EVENT_W DebugEvent;
  419. PEVENT_TRACE_HEADER peth = (PEVENT_TRACE_HEADER ) &DebugEvent;
  420. PWNODE_HEADER pwnh = (PWNODE_HEADER) &DebugEvent;
  421. ZeroMemory( &DebugEvent, sizeof( DebugEvent ) );
  422. #ifndef UNICODE
  423. TCHAR szFormat[ cchDEBUG_OUTPUT_BUFFER_SIZE ];
  424. wcstombs( szFormat, pszFormatIn, StrLenW( pszFormatIn ) + 1 );
  425. va_start( valist, pszFormatIn );
  426. _vsntprintf( DebugEvent.szMsg, 256,szFormat, valist);
  427. va_end( valist );
  428. #else
  429. va_start( valist, pszFormatIn );
  430. _vsntprintf( DebugEvent.szMsg, 256,pszFormatIn, valist);
  431. va_end( valist );
  432. #endif // UNICODE
  433. //
  434. // Fill in the blanks
  435. //
  436. Assert( wcslen( DebugEvent.szMsg ) <= 255 ); // make sure the cast doesn't fail.
  437. DebugEvent.bSize = (BYTE) wcslen( DebugEvent.szMsg );
  438. peth->Size = sizeof( DebugEvent );
  439. peth->Class.Type = 10;
  440. peth->Class.Level = pEntry->bLevel;
  441. // peth->Class.Version = 0;
  442. // peth->ThreadId = GetCurrentThreadId( ); - done by system
  443. pwnh->Guid = *pEntry->guidControl;
  444. // peth->ClientContext = NULL;
  445. pwnh->Flags = WNODE_FLAG_TRACED_GUID;
  446. // GetSystemTimeAsFileTime( (FILETIME *) &peth->TimeStamp ); - done by the system
  447. ulStatus = TraceEvent( pEntry->hTrace, (PEVENT_TRACE_HEADER) &DebugEvent );
  448. } // if: flags set
  449. } //*** WMIMessageByFlagsAndLevel( )
  450. //////////////////////////////////////////////////////////////////////////////
  451. //++
  452. //
  453. // void
  454. // WMIMessageByLevel(
  455. // DEBUG_WMI_CONTROL_GUIDS * pEntry,
  456. // const BYTE bLogLevelIn,
  457. // LPCWSTR pszFormatIn,
  458. // ...
  459. // )
  460. //
  461. // Description:
  462. //
  463. // Arguments:
  464. // pEntry -
  465. // bLogLevelIn -
  466. // pszFormatIn -
  467. // ... -
  468. //
  469. // Return Values:
  470. // None.
  471. //
  472. //--
  473. //////////////////////////////////////////////////////////////////////////////
  474. void
  475. __cdecl
  476. WMIMessageByLevel(
  477. DEBUG_WMI_CONTROL_GUIDS * pEntry,
  478. const BYTE bLogLevelIn,
  479. LPCWSTR pszFormatIn,
  480. ...
  481. )
  482. {
  483. ULONG ulStatus;
  484. DEBUG_WMI_EVENT_W DebugEvent;
  485. PEVENT_TRACE_HEADER peth = (PEVENT_TRACE_HEADER ) &DebugEvent;
  486. PWNODE_HEADER pwnh = (PWNODE_HEADER) &DebugEvent;
  487. va_list valist;
  488. if ( pEntry->hTrace == NULL )
  489. {
  490. return; // NOP
  491. } // if: no trace logger
  492. if ( bLogLevelIn > pEntry->bLevel )
  493. {
  494. return; // NOP
  495. } // if: level no high enough
  496. ZeroMemory( &DebugEvent, sizeof( DebugEvent ) );
  497. #ifndef UNICODE
  498. TCHAR szFormat[ cchDEBUG_OUTPUT_BUFFER_SIZE ];
  499. wcstombs( szFormat, pszFormatIn, StrLenW( pszFormatIn ) + 1 );
  500. va_start( valist, pszFormatIn );
  501. _vsntprintf( DebugEvent.szMsg, 256,szFormat, valist);
  502. va_end( valist );
  503. #else
  504. va_start( valist, pszFormatIn );
  505. _vsntprintf( DebugEvent.szMsg, 256,pszFormatIn, valist);
  506. va_end( valist );
  507. #endif // UNICODE
  508. //
  509. // Fill in the blanks
  510. //
  511. Assert( wcslen( DebugEvent.szMsg ) <= 255 ); // make sure the cast doesn't fail.
  512. DebugEvent.bSize = (BYTE) wcslen( DebugEvent.szMsg );
  513. peth->Size = sizeof( DebugEvent );
  514. peth->Class.Type = 10;
  515. peth->Class.Level = pEntry->bLevel;
  516. // peth->Class.Version = 0;
  517. // peth->ThreadId = GetCurrentThreadId( ); - done by system
  518. pwnh->Guid = *pEntry->guidControl;
  519. // peth->ClientContext = NULL;
  520. pwnh->Flags = WNODE_FLAG_TRACED_GUID;
  521. // GetSystemTimeAsFileTime( (FILETIME *) &peth->TimeStamp ); - done by the system
  522. ulStatus = TraceEvent( pEntry->hTrace, (PEVENT_TRACE_HEADER) &DebugEvent );
  523. } //*** WMIMessageByLevel( )