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.

371 lines
12 KiB

  1. #ifndef WPP_MOF_RESOURCENAME
  2. #define WPP_REG_TRACE_REGKEY L"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Tracing"
  3. #define WPP_REG_TRACE_ENABLED L"EnableTracing"
  4. #define WPP_REG_TRACE_LOG_FILE_NAME L"LogFileName"
  5. #define WPP_REG_TRACE_LOG_SESSION_NAME L"LogSessionName"
  6. #define WPP_REG_TRACE_LOG_BUFFER_SIZE L"BufferSize"
  7. #define WPP_REG_TRACE_LOG_MIN_BUFFERS L"MinBuffers"
  8. #define WPP_REG_TRACE_LOG_MAX_BUFFERS L"MaxBuffers"
  9. #define WPP_REG_TRACE_LOG_MAX_FILESIZE L"MaxFileSize"
  10. #define WPP_REG_TRACE_LOG_MAX_HISTORY L"MaxHistorySize"
  11. #define WPP_REG_TRACE_LOG_MAX_BACKUPS L"MaxBackups"
  12. #define WPP_REG_TRACE_ACTIVE L"Active"
  13. #define WPP_REG_TRACE_CONTROL L"ControlFlags"
  14. #define WPP_REG_TRACE_LEVEL L"Level"
  15. #define WPP_REG_TRACE_GUID L"Guid"
  16. #define WPP_REG_TRACE_BITNAMES L"BitNames"
  17. #endif // #ifndef WPP_MOF_RESOURCENAME
  18. #define DEFAULT_LOGGER_NAME L"stdout"
  19. #if !defined(WppDebug)
  20. # define WppDebug(a,b)
  21. #endif
  22. #if !defined(WPPINIT_STATIC)
  23. # define WPPINIT_STATIC
  24. #endif
  25. #if !defined(WPPINIT_EXPORT)
  26. # define WPPINIT_EXPORT
  27. #endif
  28. #define WPP_GUID_FORMAT "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x"
  29. #define WPP_GUID_ELEMENTS(p) \
  30. p->Data1, p->Data2, p->Data3,\
  31. p->Data4[0], p->Data4[1], p->Data4[2], p->Data4[3],\
  32. p->Data4[4], p->Data4[5], p->Data4[6], p->Data4[7]
  33. WPPINIT_STATIC
  34. void WppIntToHex(
  35. LPWSTR Buf,
  36. unsigned int value,
  37. int digits
  38. )
  39. {
  40. static LPCWSTR hexDigit = L"0123456789abcdef";
  41. while (--digits >= 0) {
  42. Buf[digits] = hexDigit[ value & 15 ];
  43. value /= 16; // compiler is smart enough to change it to bitshift
  44. }
  45. }
  46. #define WPP_TEXTGUID_LEN 37
  47. // b1e5deaf-1524-4a04-82c4-c9dfbce6cf97<NULL>
  48. // 0 1 2 3
  49. // 0123456789012345678901234567890123456
  50. WPPINIT_STATIC
  51. void WppGuidToStr(LPWSTR buf, LPCGUID guid) {
  52. WppIntToHex(buf + 0, guid->Data1, 8);
  53. buf[8] = '-';
  54. WppIntToHex(buf + 9, guid->Data2, 4);
  55. buf[13] = '-';
  56. WppIntToHex(buf + 14, guid->Data3, 4);
  57. buf[18] = '-';
  58. WppIntToHex(buf + 19, guid->Data4[0], 2);
  59. WppIntToHex(buf + 21, guid->Data4[1], 2);
  60. buf[23] = '-';
  61. WppIntToHex(buf + 24, guid->Data4[2], 2);
  62. WppIntToHex(buf + 26, guid->Data4[3], 2);
  63. WppIntToHex(buf + 28, guid->Data4[4], 2);
  64. WppIntToHex(buf + 30, guid->Data4[5], 2);
  65. WppIntToHex(buf + 32, guid->Data4[6], 2);
  66. WppIntToHex(buf + 34, guid->Data4[7], 2);
  67. buf[36] = 0;
  68. }
  69. #ifndef WPP_MOF_RESOURCENAME
  70. WPPINIT_STATIC
  71. ULONG
  72. WppPublishTraceInfo(
  73. PWPP_REGISTRATION_BLOCK Head,
  74. LPCWSTR ProductName)
  75. {
  76. DWORD status;
  77. DWORD disposition;
  78. HKEY TracingKey = 0;
  79. HKEY ProductKey = 0;
  80. DWORD dwValue;
  81. status = RegCreateKeyExW(HKEY_LOCAL_MACHINE,
  82. WPP_REG_TRACE_REGKEY,
  83. 0,
  84. NULL, // Class
  85. REG_OPTION_NON_VOLATILE,
  86. KEY_READ | KEY_CREATE_SUB_KEY,
  87. NULL, // Sec Attributes
  88. &TracingKey,
  89. &disposition
  90. );
  91. if (status != ERROR_SUCCESS) {
  92. WppDebug(1,("[WppInit] Failed to create Trace Key, %1!d!", status) );
  93. goto exit_gracefully;
  94. }
  95. status = RegCreateKeyExW(TracingKey,
  96. ProductName,
  97. 0,
  98. NULL, // Class
  99. REG_OPTION_NON_VOLATILE,
  100. KEY_WRITE,
  101. NULL, // Sec Attributes
  102. &ProductKey,
  103. &disposition
  104. );
  105. if (status != ERROR_SUCCESS) {
  106. WppDebug(1,("[WppInit] Failed to create Trace Key, %1!d!", status) );
  107. goto exit_gracefully;
  108. }
  109. status = RegSetValueExW(ProductKey,
  110. WPP_REG_TRACE_LOG_SESSION_NAME,
  111. 0, // Reserved
  112. REG_EXPAND_SZ,
  113. (const BYTE*)DEFAULT_LOGGER_NAME,
  114. sizeof(DEFAULT_LOGGER_NAME) );
  115. if (status != ERROR_SUCCESS) {
  116. WppDebug(1,("[WppInit] Failed to create LogSession value, %1!d!", status) );
  117. goto exit_gracefully;
  118. }
  119. dwValue = 1;
  120. status = RegSetValueExW(ProductKey,
  121. WPP_REG_TRACE_ACTIVE,
  122. 0, // Reserved
  123. REG_DWORD,
  124. (const BYTE*)&dwValue,
  125. sizeof(dwValue) );
  126. if (status != ERROR_SUCCESS) {
  127. WppDebug(1, ("[WppInit] Failed to create Active value, %1!d!", status) );
  128. goto exit_gracefully;
  129. }
  130. dwValue = 1;
  131. status = RegSetValueExW(ProductKey,
  132. WPP_REG_TRACE_CONTROL,
  133. 0, // Reserved
  134. REG_DWORD,
  135. (const BYTE*)&dwValue,
  136. sizeof(dwValue) );
  137. if (status != ERROR_SUCCESS) {
  138. WppDebug(1,("[WppInit] Failed to create Control value, %1!d!", status) );
  139. goto exit_gracefully;
  140. }
  141. for(;Head;Head = Head->Next) {
  142. HKEY ComponentKey;
  143. status = RegCreateKeyExW(ProductKey,
  144. Head->FriendlyName,
  145. 0,
  146. NULL, // Class
  147. REG_OPTION_NON_VOLATILE,
  148. KEY_WRITE,
  149. NULL, // Sec Attributes
  150. &ComponentKey,
  151. &disposition
  152. );
  153. if (status != ERROR_SUCCESS) {
  154. WppDebug(1,("[WppInit] Failed to create %ws Key, %d",
  155. Head->FriendlyName, status) );
  156. } else {
  157. GUID guid;
  158. WCHAR guidBuf[WPP_TEXTGUID_LEN];
  159. guid = *Head->ControlGuid;
  160. WppGuidToStr(guidBuf, &guid);
  161. status = RegSetValueExW(ComponentKey,
  162. WPP_REG_TRACE_GUID,
  163. 0, // Reserved
  164. REG_SZ,
  165. (const BYTE*)guidBuf,
  166. sizeof(guidBuf) );
  167. if (status != ERROR_SUCCESS) {
  168. WppDebug(1,("[WppInit] Failed to create GUID value, %1!d!", status) );
  169. }
  170. status = RegSetValueExW(ComponentKey,
  171. WPP_REG_TRACE_BITNAMES,
  172. 0, // Reserved
  173. REG_SZ,
  174. (const BYTE*)Head->BitNames,
  175. (DWORD)(wcslen(Head->BitNames) * sizeof(WCHAR)) );
  176. if (status != ERROR_SUCCESS) {
  177. WppDebug(1,("[WppInit] Failed to create GUID value, %1!d!", status) );
  178. }
  179. RegCloseKey(ComponentKey);
  180. }
  181. }
  182. exit_gracefully:
  183. if (ProductKey) {
  184. RegCloseKey(ProductKey);
  185. }
  186. if (TracingKey) {
  187. RegCloseKey(TracingKey);
  188. }
  189. return status;
  190. }
  191. #endif // #ifndef WPP_MOF_RESOURCENAME
  192. ULONG
  193. WINAPI
  194. WppControlCallback(
  195. IN WMIDPREQUESTCODE RequestCode,
  196. IN PVOID Context,
  197. IN OUT ULONG *InOutBufferSize,
  198. IN OUT PVOID Buffer
  199. )
  200. {
  201. PWPP_TRACE_CONTROL_BLOCK Ctx = (PWPP_TRACE_CONTROL_BLOCK)Context;
  202. TRACEHANDLE Logger;
  203. UCHAR Level;
  204. DWORD Flags;
  205. *InOutBufferSize = 0;
  206. switch (RequestCode)
  207. {
  208. case WMI_ENABLE_EVENTS:
  209. {
  210. Logger = GetTraceLoggerHandle( Buffer );
  211. Level = GetTraceEnableLevel(Logger);
  212. Flags = GetTraceEnableFlags(Logger);
  213. WppDebug(1, ("[WppInit] WMI_ENABLE_EVENTS Ctx %p Flags %x"
  214. " Lev %d Logger %I64x\n",
  215. Ctx, Flags, Level, Logger) );
  216. break;
  217. }
  218. case WMI_DISABLE_EVENTS:
  219. {
  220. Logger = 0;
  221. Flags = 0;
  222. Level = 0;
  223. WppDebug(1, ("[WppInit] WMI_DISABLE_EVENTS Ctx 0x%08p\n", Ctx));
  224. break;
  225. }
  226. default:
  227. {
  228. return(ERROR_INVALID_PARAMETER);
  229. }
  230. }
  231. if (Ctx->Options & WPP_VER_WIN2K_CB_FORWARD_PTR && Ctx->Win2kCb) {
  232. Ctx->Win2kCb->Logger = Logger;
  233. Ctx->Win2kCb->Level = Level;
  234. Ctx->Win2kCb->Flags = Flags;
  235. } else {
  236. if (Ctx->Options & WPP_VER_WHISTLER_CB_FORWARD_PTR && Ctx->Cb) {
  237. Ctx = Ctx->Cb; // use forwarding address
  238. }
  239. Ctx->Logger = Logger;
  240. Ctx->Level = Level;
  241. Ctx->Flags[0] = Flags;
  242. }
  243. return(ERROR_SUCCESS);
  244. }
  245. WPPINIT_EXPORT
  246. VOID WppInitUm(LPCWSTR AppName, PWPP_REGISTRATION_BLOCK Registration)
  247. {
  248. PWPP_REGISTRATION_BLOCK p = Registration;
  249. #ifdef WPP_MOF_RESOURCENAME
  250. HMODULE hModule = NULL ;
  251. WCHAR ImagePath[4 * MAX_PATH] = {UNICODE_NULL} ;
  252. WCHAR WppMofResourceName[] = WPP_MOF_RESOURCENAME ;
  253. #endif //#ifdef WPP_MOF_RESOURCENAME
  254. #ifndef WPP_MOF_RESOURCENAME
  255. if (AppName) {
  256. WppPublishTraceInfo(Registration, AppName);
  257. }
  258. #endif // #ifndef WPP_MOF_RESOURCENAME
  259. WppDebug(1, ("Registering %ws\n", AppName) );
  260. for(; p; p = p->Next) {
  261. TRACE_GUID_REGISTRATION FakeTraceGuid;
  262. WPP_REGISTRATION_BLOCK RegBlock = *p;
  263. PWPP_TRACE_CONTROL_BLOCK cb = (PWPP_TRACE_CONTROL_BLOCK)p;
  264. ULONG status;
  265. WppDebug(1,(WPP_GUID_FORMAT " %ws : %d:%ws\n",
  266. WPP_GUID_ELEMENTS(p->ControlGuid),
  267. p->FriendlyName, p->FlagsLen, p->BitNames));
  268. ZeroMemory(cb, sizeof(WPP_TRACE_CONTROL_BLOCK)
  269. + sizeof(ULONG) * (RegBlock.FlagsLen - 1) );
  270. p->Next = RegBlock.Next;
  271. cb->FlagsLen = RegBlock.FlagsLen;
  272. cb->Options = RegBlock.Options;
  273. cb->Ptr = RegBlock.Ptr;
  274. // Jee, do we need this fake trace guid? //
  275. FakeTraceGuid.Guid = RegBlock.ControlGuid;
  276. FakeTraceGuid.RegHandle = 0;
  277. #ifdef WPP_MOF_RESOURCENAME
  278. if (AppName != NULL) {
  279. DWORD Status ;
  280. #ifdef WPP_DLL
  281. if ((hModule = GetModuleHandleW(AppName)) != NULL) {
  282. Status = GetModuleFileNameW(hModule, ImagePath, MAX_PATH) ;
  283. if (Status == 0) {
  284. WppDebug(1,("RegisterTraceGuids => GetModuleFileName(DLL) Failed 0x%08X\n",GetLastError()));
  285. }
  286. } else {
  287. WppDebug(1,("RegisterTraceGuids => GetModuleHandleW failed for %ws (0x%08X)\n",AppName,GetLastError()));
  288. }
  289. #else // #ifdef WPP_DLL
  290. Status = GetModuleFileNameW(NULL,ImagePath,MAX_PATH);
  291. if (Status == 0) {
  292. WppDebug(1,("GetModuleFileName(EXE) Failed 0x%08X\n",GetLastError()));
  293. }
  294. #endif // #ifdef WPP_DLL
  295. }
  296. WppDebug(1,("registerTraceGuids => registering with WMI, App=%ws, Mof=%ws, ImagePath=%ws\n",AppName,WppMofResourceName,ImagePath));
  297. status = RegisterTraceGuidsW( // Always use Unicode
  298. #else // ifndef WPP_MOF_RESOURCENAME
  299. status = RegisterTraceGuids( // So no change for existing users
  300. #endif // ifndef WPP_MOF_RESOURCENAME
  301. WppControlCallback,
  302. cb, // Context for the callback
  303. RegBlock.ControlGuid, // Control Guid
  304. 1, &FakeTraceGuid, // #TraceGuids, TraceGuid
  305. #ifndef WPP_MOF_RESOURCENAME
  306. 0, //ImagePath,
  307. 0, //ResourceName,
  308. #else // #ifndef WPP_MOF_RESOURCENAME
  309. ImagePath,
  310. WppMofResourceName,
  311. #endif // #ifndef WPP_MOF_RESOURCENAME
  312. &cb->UmRegistrationHandle
  313. );
  314. WppDebug(1, ("RegisterTraceGuid => %d\n", status) );
  315. }
  316. }
  317. WPPINIT_EXPORT
  318. VOID WppCleanupUm(PWPP_REGISTRATION_BLOCK Registration)
  319. {
  320. PWPP_TRACE_CONTROL_BLOCK x = (PWPP_TRACE_CONTROL_BLOCK)Registration;
  321. WppDebug(1, ("Cleanup\n") );
  322. for(; x; x = x->Next) {
  323. WppDebug(1,("UnRegistering %I64x\n", x->UmRegistrationHandle) );
  324. if (x->UmRegistrationHandle) {
  325. UnregisterTraceGuids(x->UmRegistrationHandle);
  326. }
  327. }
  328. }