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.

406 lines
19 KiB

  1. /**MOD+**********************************************************************/
  2. /* Module: wtrcapi.c */
  3. /* */
  4. /* Purpose: External tracing functions - Windows specific */
  5. /* */
  6. /* Copyright(C) Microsoft Corporation 1997 */
  7. /* */
  8. /****************************************************************************/
  9. /** Changes:
  10. * $Log: Y:/logs/trc/wtrcapi.c_v $
  11. *
  12. * Rev 1.7 29 Aug 1997 09:42:02 ENH
  13. * SFR1259: Changed SystemError
  14. *
  15. * Rev 1.6 12 Aug 1997 09:50:52 MD
  16. * SFR1002: Remove kernel tracing code
  17. *
  18. * Rev 1.5 10 Jul 1997 18:04:26 AK
  19. * SFR1016: Initial changes to support Unicode
  20. *
  21. * Rev 1.4 10 Jul 1997 17:34:10 KH
  22. * SFR1022: Get 16-bit trace working
  23. *
  24. * Rev 1.3 03 Jul 1997 13:28:50 AK
  25. * SFR0000: Initial development completed
  26. **/
  27. /**MOD-**********************************************************************/
  28. /****************************************************************************/
  29. /* */
  30. /* CONTENTS */
  31. /* ======== */
  32. /* TRC_SystemError */
  33. /* TRC_Initialize */
  34. /* TRC_Terminate */
  35. /* */
  36. /****************************************************************************/
  37. /****************************************************************************/
  38. /* Standard includes. */
  39. /****************************************************************************/
  40. #include <adcg.h>
  41. /****************************************************************************/
  42. /* Define TRC_FILE and TRC_GROUP. */
  43. /****************************************************************************/
  44. #define TRC_FILE "wtrcapi"
  45. #define TRC_GROUP TRC_GROUP_TRACE
  46. /****************************************************************************/
  47. /* Trace specific includes. */
  48. /****************************************************************************/
  49. #include <atrcapi.h>
  50. #include <atrcint.h>
  51. /****************************************************************************/
  52. /* */
  53. /* DATA */
  54. /* */
  55. /****************************************************************************/
  56. #define DC_INCLUDE_DATA
  57. #include <atrcdata.c>
  58. #undef DC_INCLUDE_DATA
  59. /****************************************************************************/
  60. /* */
  61. /* FUNCTIONS */
  62. /* */
  63. /****************************************************************************/
  64. /**PROC+*********************************************************************/
  65. /* TRC_SystemError(...) */
  66. /* */
  67. /* See wtrcapi.h for description. */
  68. /**PROC-*********************************************************************/
  69. DCVOID DCAPI DCEXPORT TRC_SystemError(DCUINT traceComponent,
  70. DCUINT lineNumber,
  71. PDCTCHAR funcName,
  72. PDCTCHAR fileName,
  73. PDCTCHAR string)
  74. {
  75. /************************************************************************/
  76. /* The process of getting the system error is clearly platform specific */
  77. /* so we call the platform specific function. */
  78. /************************************************************************/
  79. TRCSystemError(traceComponent,
  80. lineNumber,
  81. funcName,
  82. fileName,
  83. string);
  84. return;
  85. } /* TRC_SystemError */
  86. /**PROC+*********************************************************************/
  87. /* TRC_Initialize(...) */
  88. /* */
  89. /* See atrcapi.h for description. */
  90. /**PROC-*********************************************************************/
  91. DCUINT32 DCAPI DCEXPORT TRC_Initialize(DCBOOL initShared)
  92. {
  93. DCUINT rc = 0;
  94. #ifndef OS_WINCE
  95. SECURITY_ATTRIBUTES sa;
  96. SECURITY_DESCRIPTOR sd;
  97. OSVERSIONINFO ver;
  98. PSID psidEveryone = NULL;
  99. SID_IDENTIFIER_AUTHORITY sidEveryoneAuthority = SECURITY_WORLD_SID_AUTHORITY;
  100. DCUINT32 dwDaclLength;
  101. PACL pDacl = NULL;
  102. #endif
  103. BOOL releaseMutex = FALSE;
  104. DC_IGNORE_PARAMETER(initShared);
  105. /************************************************************************/
  106. /* Create the mutex object which protects the shared memory mapped */
  107. /* file. If the mutex has already been created then CreateMutex simply */
  108. /* returns a handle to the existing mutex. */
  109. /* */
  110. /* The mutex will be created with a NULL Dacl so processes running it in*/
  111. /* any context can have access. */
  112. /************************************************************************/
  113. #ifndef OS_WINCE
  114. ver.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
  115. GetVersionEx(&ver);
  116. if (ver.dwPlatformId == VER_PLATFORM_WIN32_NT) {
  117. /************************************************************************/
  118. /* Get the SID for the Everyone group */
  119. /************************************************************************/
  120. if (!AllocateAndInitializeSid (
  121. &sidEveryoneAuthority, // pIdentifierAuthority
  122. 1, // count of subauthorities
  123. SECURITY_WORLD_RID, // subauthority 0
  124. 0, 0, 0, 0, 0, 0, 0, // subauthorities n
  125. &psidEveryone)) { // pointer to pointer to SID
  126. rc = TRC_RC_CREATE_MUTEX_FAILED;
  127. OutputDebugString(_T("AllocateAndInitializeSid failed.\n"));
  128. DC_QUIT;
  129. }
  130. /************************************************************************/
  131. /* Allocate the Dacl */
  132. /************************************************************************/
  133. dwDaclLength = sizeof(ACL);
  134. dwDaclLength += (sizeof(ACCESS_DENIED_ACE) - sizeof(DWORD)) +
  135. GetLengthSid(psidEveryone);
  136. dwDaclLength += (sizeof(ACCESS_ALLOWED_ACE) - sizeof(DWORD)) +
  137. GetLengthSid(psidEveryone);
  138. pDacl = (PACL)LocalAlloc(LMEM_FIXED, dwDaclLength);
  139. if (pDacl == NULL) {
  140. rc = TRC_RC_CREATE_MUTEX_FAILED;
  141. OutputDebugString(_T("Can't allocate Dacl.\n"));
  142. DC_QUIT;
  143. }
  144. /************************************************************************/
  145. /* Initialize it. */
  146. /************************************************************************/
  147. if (!InitializeAcl(pDacl, dwDaclLength, ACL_REVISION)) {
  148. rc = TRC_RC_CREATE_MUTEX_FAILED;
  149. OutputDebugString(_T("InitializeAcl failed.\n"));
  150. DC_QUIT;
  151. }
  152. /************************************************************************/
  153. /* Allow all access */
  154. /************************************************************************/
  155. if (!AddAccessAllowedAce(
  156. pDacl,
  157. ACL_REVISION,
  158. GENERIC_ALL,
  159. psidEveryone)) {
  160. rc = TRC_RC_CREATE_MUTEX_FAILED;
  161. OutputDebugString(_T("AddAccessAllowedAce failed.\n"));
  162. DC_QUIT;
  163. }
  164. /************************************************************************/
  165. /* Block Write-DACL Access */
  166. /************************************************************************/
  167. if (!AddAccessDeniedAce(
  168. pDacl,
  169. ACL_REVISION,
  170. WRITE_DAC,
  171. psidEveryone)) {
  172. rc = TRC_RC_CREATE_MUTEX_FAILED;
  173. OutputDebugString(_T("AddAccessDeniedAce failed.\n"));
  174. DC_QUIT;
  175. }
  176. /************************************************************************/
  177. /* Create the Mutex */
  178. /************************************************************************/
  179. InitializeSecurityDescriptor(&sd, SECURITY_DESCRIPTOR_REVISION);
  180. SetSecurityDescriptorDacl(&sd, TRUE, pDacl, FALSE);
  181. sa.lpSecurityDescriptor = &sd;
  182. trchMutex = TRCCreateMutex(&sa,
  183. FALSE,
  184. TRC_MUTEX_NAME);
  185. }
  186. else {
  187. #endif
  188. trchMutex = TRCCreateMutex(NULL,
  189. FALSE,
  190. TRC_MUTEX_NAME);
  191. #ifndef OS_WINCE
  192. }
  193. #endif
  194. /************************************************************************/
  195. /* Check that we created the mutex successfully. */
  196. /************************************************************************/
  197. if (NULL == trchMutex)
  198. {
  199. DWORD lastError = GetLastError();
  200. OutputDebugString(_T("Failed to create mutex.\n"));
  201. rc = TRC_RC_CREATE_MUTEX_FAILED;
  202. DC_QUIT;
  203. }
  204. /************************************************************************/
  205. /* Now that we've created the mutex, grab it. */
  206. /************************************************************************/
  207. TRCGrabMutex();
  208. releaseMutex = TRUE;
  209. /************************************************************************/
  210. /* Check the current trace DLL state. Another thread may have */
  211. /* concurrently called TRC_Initialize - if it has then we should exit */
  212. /* as it will perform the initialization. */
  213. /************************************************************************/
  214. if (TRC_STATE_UNINITIALIZED != trcState)
  215. {
  216. TRCDebugOutput(_T("Trace DLL already initialized!\n"));
  217. DC_QUIT;
  218. }
  219. /************************************************************************/
  220. /* We need to open the shared data memory mapped file. */
  221. /************************************************************************/
  222. rc = TRCOpenSharedData();
  223. /************************************************************************/
  224. /* Check that the shared data MMF was created and opened successfully. */
  225. /************************************************************************/
  226. if (0 != rc)
  227. {
  228. DC_QUIT;
  229. }
  230. /************************************************************************/
  231. /* Now open the memory mapped trace files. */
  232. /************************************************************************/
  233. rc = TRCOpenAllFiles();
  234. /************************************************************************/
  235. /* Check that the trace MMFs were opened successfully. */
  236. /************************************************************************/
  237. if (0 != rc)
  238. {
  239. DC_QUIT;
  240. }
  241. /************************************************************************/
  242. /* Write out the trace DLL initialized trace line. */
  243. /************************************************************************/
  244. TRCInternalTrace(TRC_TRACE_DLL_INITIALIZE);
  245. /************************************************************************/
  246. /* Update our internal state. */
  247. /************************************************************************/
  248. trcState = TRC_STATE_INITIALIZED;
  249. /************************************************************************/
  250. /* Load the debug symbols. */
  251. /************************************************************************/
  252. rc = TRCSymbolsLoad();
  253. if (0 != rc)
  254. {
  255. DC_QUIT;
  256. }
  257. DC_EXIT_POINT:
  258. /************************************************************************/
  259. /* Release the mutex. */
  260. /************************************************************************/
  261. if( releaseMutex )
  262. {
  263. TRCReleaseMutex();
  264. }
  265. #ifndef OS_WINCE
  266. if (pDacl) LocalFree(pDacl);
  267. if (psidEveryone) FreeSid(psidEveryone);
  268. #endif
  269. return(rc);
  270. } /* TRC_Initialize */
  271. /**PROC+*********************************************************************/
  272. /* TRC_Terminate(...) */
  273. /* */
  274. /* See atrcapi.h for description. */
  275. /**PROC-*********************************************************************/
  276. DCVOID DCAPI DCEXPORT TRC_Terminate(DCBOOL termShared)
  277. {
  278. DC_IGNORE_PARAMETER(termShared);
  279. /************************************************************************/
  280. /* Grab the mutex. Note that this function is only called from the */
  281. /* process detach case in the <DllMain> function - therefore we can be */
  282. /* sure that this function gets called only once per process. */
  283. /************************************************************************/
  284. TRCGrabMutex();
  285. /************************************************************************/
  286. /* Unload the symbols if we've loaded them. */
  287. /************************************************************************/
  288. if (TEST_FLAG(trcProcessStatus, TRC_STATUS_SYMBOLS_LOADED))
  289. {
  290. TRCSymbolsUnload();
  291. }
  292. /************************************************************************/
  293. /* Write out the trace DLL terminated trace line. */
  294. /************************************************************************/
  295. TRCInternalTrace(TRC_TRACE_DLL_TERMINATE);
  296. /************************************************************************/
  297. /* Close the trace files. */
  298. /************************************************************************/
  299. TRCCloseAllFiles();
  300. /************************************************************************/
  301. /* Now we need to close the shared data area. */
  302. /************************************************************************/
  303. TRCCloseSharedData();
  304. /************************************************************************/
  305. /* We're no longer initialized so update our per-process flags. */
  306. /************************************************************************/
  307. trcState = TRC_STATE_TERMINATED;
  308. /************************************************************************/
  309. /* Release the mutex. */
  310. /************************************************************************/
  311. TRCReleaseMutex();
  312. /************************************************************************/
  313. /* Close the mutex handle. The mutex object is automatically destroyed */
  314. /* when the last handle is closed. */
  315. /************************************************************************/
  316. TRCCloseHandle(trchMutex);
  317. trchMutex = NULL;
  318. return;
  319. } /* TRC_Terminate */
  320. #if defined(OS_WINCE) || defined(TRC_CONVERTOANSI)
  321. /**PROC+*********************************************************************/
  322. /* Name: TRC_ConvertAndSprintf */
  323. /* */
  324. /* Purpose: Convert ANSI trace format string to Unicode and do sprintf */
  325. /* Windows CE only */
  326. /* */
  327. /* Returns: None */
  328. /* */
  329. /* Params: OUT outBuf - output buffer */
  330. /* IN format - ANSI format string */
  331. /* IN ... - parameters */
  332. /* */
  333. /**PROC-*********************************************************************/
  334. DCVOID DCAPI TRC_ConvertAndSprintf(PDCTCHAR outBuf, const PDCACHAR format,...)
  335. {
  336. va_list vaArgs;
  337. DCINT count;
  338. static DCTCHAR stackBuffer[TRC_LINE_BUFFER_SIZE];
  339. count = DC_MIN(mbstowcs(NULL, format, 0), TRC_LINE_BUFFER_SIZE);
  340. va_start(vaArgs, format);
  341. mbstowcs(stackBuffer, format, count);
  342. vswprintf(outBuf, stackBuffer, vaArgs);
  343. va_end(vaArgs);
  344. return;
  345. }
  346. #else
  347. /****************************************************************************/
  348. /* Dummy stub function, to enable a common DEF file to be used. */
  349. /****************************************************************************/
  350. DCVOID DCAPI TRC_ConvertAndSprintf(DCVOID)
  351. {
  352. return;
  353. }
  354. #endif // OS_WINCE