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.

358 lines
7.6 KiB

  1. /*++
  2. Copyright (c) 2000-2002 Microsoft Corporation
  3. Module Name:
  4. ext.cpp
  5. Abstract:
  6. Generic cross-platform and cross-processor extensions.
  7. Environment:
  8. User Mode
  9. --*/
  10. #include "mqext.h"
  11. #include <ntverp.h>
  12. #include <time.h>
  13. #include <lm.h>
  14. // To get _open to work
  15. #include <crt\io.h>
  16. #include <fcntl.h>
  17. #include <sys\types.h>
  18. #include <sys\stat.h>
  19. //
  20. // Valid for the lifetime of the debug session.
  21. //
  22. WINDBG_EXTENSION_APIS ExtensionApis;
  23. ULONG TargetMachine;
  24. BOOL Connected;
  25. ULONG g_TargetClass;
  26. ULONG g_TargetBuild;
  27. ULONG g_TargetPlatform;
  28. //
  29. // Valid only during an extension API call
  30. //
  31. PDEBUG_ADVANCED g_ExtAdvanced;
  32. PDEBUG_CLIENT g_ExtClient;
  33. PDEBUG_DATA_SPACES3 g_ExtData;
  34. PDEBUG_REGISTERS g_ExtRegisters;
  35. PDEBUG_SYMBOLS2 g_ExtSymbols;
  36. PDEBUG_SYSTEM_OBJECTS3 g_ExtSystem;
  37. // Version 3 Interfaces
  38. PDEBUG_CONTROL3 g_ExtControl;
  39. // Queries for all debugger interfaces.
  40. extern "C" HRESULT
  41. ExtQuery(PDEBUG_CLIENT Client)
  42. {
  43. HRESULT Status;
  44. if ((Status = Client->QueryInterface(__uuidof(IDebugAdvanced),
  45. (void **)&g_ExtAdvanced)) != S_OK)
  46. {
  47. goto Fail;
  48. }
  49. if ((Status = Client->QueryInterface(__uuidof(IDebugDataSpaces3),
  50. (void **)&g_ExtData)) != S_OK)
  51. {
  52. goto Fail;
  53. }
  54. if ((Status = Client->QueryInterface(__uuidof(IDebugRegisters),
  55. (void **)&g_ExtRegisters)) != S_OK)
  56. {
  57. goto Fail;
  58. }
  59. if ((Status = Client->QueryInterface(__uuidof(IDebugSymbols),
  60. (void **)&g_ExtSymbols)) != S_OK)
  61. {
  62. goto Fail;
  63. }
  64. if ((Status = Client->QueryInterface(__uuidof(IDebugSystemObjects3),
  65. (void **)&g_ExtSystem)) != S_OK)
  66. {
  67. goto Fail;
  68. }
  69. if ((Status = Client->QueryInterface(__uuidof(IDebugControl2),
  70. (void **)&g_ExtControl)) != S_OK)
  71. {
  72. goto Fail;
  73. }
  74. g_ExtClient = Client;
  75. return S_OK;
  76. Fail:
  77. ExtRelease();
  78. return Status;
  79. }
  80. // Cleans up all debugger interfaces.
  81. void
  82. ExtRelease(void)
  83. {
  84. g_ExtClient = NULL;
  85. EXT_RELEASE(g_ExtAdvanced);
  86. EXT_RELEASE(g_ExtData);
  87. EXT_RELEASE(g_ExtRegisters);
  88. EXT_RELEASE(g_ExtSymbols);
  89. EXT_RELEASE(g_ExtSystem);
  90. EXT_RELEASE(g_ExtControl);
  91. }
  92. // Normal output.
  93. void __cdecl
  94. ExtOut(PCSTR Format, ...)
  95. {
  96. va_list Args;
  97. va_start(Args, Format);
  98. g_ExtControl->OutputVaList(DEBUG_OUTPUT_NORMAL, Format, Args);
  99. va_end(Args);
  100. }
  101. // Error output.
  102. void __cdecl
  103. ExtErr(PCSTR Format, ...)
  104. {
  105. va_list Args;
  106. va_start(Args, Format);
  107. g_ExtControl->OutputVaList(DEBUG_OUTPUT_ERROR, Format, Args);
  108. va_end(Args);
  109. }
  110. // Warning output.
  111. void __cdecl
  112. ExtWarn(PCSTR Format, ...)
  113. {
  114. va_list Args;
  115. va_start(Args, Format);
  116. g_ExtControl->OutputVaList(DEBUG_OUTPUT_WARNING, Format, Args);
  117. va_end(Args);
  118. }
  119. // Verbose output.
  120. void __cdecl
  121. ExtVerb(PCSTR Format, ...)
  122. {
  123. va_list Args;
  124. va_start(Args, Format);
  125. g_ExtControl->OutputVaList(DEBUG_OUTPUT_VERBOSE, Format, Args);
  126. va_end(Args);
  127. }
  128. extern "C"
  129. HRESULT
  130. CALLBACK
  131. DebugExtensionInitialize(PULONG Version, PULONG Flags)
  132. {
  133. IDebugClient *DebugClient;
  134. PDEBUG_CONTROL DebugControl;
  135. HRESULT Hr;
  136. *Version = DEBUG_EXTENSION_VERSION(1, 0);
  137. *Flags = 0;
  138. if ((Hr = DebugCreate(__uuidof(IDebugClient),
  139. (void **)&DebugClient)) != S_OK)
  140. {
  141. return Hr;
  142. }
  143. if ((Hr = DebugClient->QueryInterface(__uuidof(IDebugControl),
  144. (void **)&DebugControl)) != S_OK)
  145. {
  146. return Hr;
  147. }
  148. ExtensionApis.nSize = sizeof (ExtensionApis);
  149. if ((Hr = DebugControl->GetWindbgExtensionApis64(&ExtensionApis)) != S_OK) {
  150. return Hr;
  151. }
  152. DebugControl->Release();
  153. DebugClient->Release();
  154. return S_OK;
  155. }
  156. extern "C"
  157. void
  158. CALLBACK
  159. DebugExtensionNotify(ULONG Notify, ULONG64 Argument)
  160. {
  161. //
  162. // The first time we actually connect to a target, get the page size
  163. //
  164. if ((Notify == DEBUG_NOTIFY_SESSION_ACCESSIBLE) && (!Connected))
  165. {
  166. IDebugClient *DebugClient;
  167. PDEBUG_DATA_SPACES DebugDataSpaces;
  168. PDEBUG_CONTROL DebugControl;
  169. HRESULT Hr;
  170. ULONG64 Page;
  171. if ((Hr = DebugCreate(__uuidof(IDebugClient),
  172. (void **)&DebugClient)) == S_OK)
  173. {
  174. //
  175. // Get the architecture type.
  176. //
  177. if ((Hr = DebugClient->QueryInterface(__uuidof(IDebugControl),
  178. (void **)&DebugControl)) == S_OK)
  179. {
  180. if ((Hr = DebugControl->GetActualProcessorType(
  181. &TargetMachine)) == S_OK)
  182. {
  183. Connected = TRUE;
  184. }
  185. ULONG MajorVer, SrvPack;
  186. if ((Hr = DebugControl->GetSystemVersion(
  187. &g_TargetPlatform, &MajorVer,
  188. &g_TargetBuild, NULL,
  189. 0, NULL,
  190. &SrvPack, NULL,
  191. 0, NULL)) == S_OK) {
  192. }
  193. ULONG Qualifier;
  194. if ((Hr = DebugControl->GetDebuggeeType(&g_TargetClass, &Qualifier)) == S_OK)
  195. {
  196. }
  197. DebugControl->Release();
  198. }
  199. DebugClient->Release();
  200. }
  201. }
  202. if (Notify == DEBUG_NOTIFY_SESSION_INACTIVE)
  203. {
  204. Connected = FALSE;
  205. TargetMachine = 0;
  206. }
  207. return;
  208. }
  209. extern "C"
  210. void
  211. CALLBACK
  212. DebugExtensionUninitialize(void)
  213. {
  214. return;
  215. }
  216. DllInit(
  217. HANDLE hModule,
  218. DWORD dwReason,
  219. DWORD dwReserved
  220. )
  221. {
  222. switch (dwReason) {
  223. case DLL_THREAD_ATTACH:
  224. break;
  225. case DLL_THREAD_DETACH:
  226. break;
  227. case DLL_PROCESS_DETACH:
  228. break;
  229. case DLL_PROCESS_ATTACH:
  230. break;
  231. }
  232. return TRUE;
  233. }
  234. extern char g_FormatName[MAX_PATH];
  235. extern BOOL g_bSend;
  236. BOOL
  237. GetArgs(int Argc, CHAR ** Argv);
  238. HRESULT
  239. SendMessageText(
  240. PWCHAR pwszMsmqFormat,
  241. PWCHAR pwszMesgLabel,
  242. PWCHAR pwszMesgText
  243. );
  244. DECLARE_API( initmq )
  245. {
  246. PCHAR ArgTokens[100];
  247. ULONG Argc;
  248. CHAR seps[] = " \t";
  249. PCHAR tok;
  250. CHAR LocArgs[1024];
  251. INIT_API();
  252. if (StringCchCopy(LocArgs, sizeof(LocArgs), args) != S_OK)
  253. {
  254. LocArgs[0] = 0;
  255. }
  256. Argc = 0;
  257. tok = strtok(LocArgs, seps);
  258. while (tok && (Argc < sizeof(ArgTokens)/sizeof(ArgTokens[0])))
  259. {
  260. ArgTokens[Argc++] = tok;
  261. tok = strtok(NULL, seps);
  262. }
  263. GetArgs(Argc, ArgTokens);
  264. EXIT_API();
  265. return S_OK;
  266. }
  267. DECLARE_API( send )
  268. {
  269. WCHAR Message[MAX_PATH], wszFormat[MAX_PATH];
  270. INIT_API();
  271. if ((StringCbPrintfW(Message, sizeof(Message), L"%S", args) != S_OK) ||
  272. (StringCbPrintfW(wszFormat, sizeof(wszFormat), L"%S", g_FormatName) != S_OK))
  273. {
  274. EXIT_API();
  275. return E_FAIL;
  276. }
  277. SendMessageText(wszFormat, L"MQEXT", Message);
  278. EXIT_API();
  279. return S_OK;
  280. }
  281. HRESULT
  282. _EFN_SendMQMessageText(
  283. LPWSTR pwszFormat,
  284. LPWSTR pwszLabel,
  285. LPWSTR pwszMessage
  286. )
  287. {
  288. return SendMessageText(pwszFormat, pwszLabel, pwszMessage);
  289. }