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.

291 lines
6.2 KiB

  1. //----------------------------------------------------------------------------
  2. //
  3. // Starts a process server and sleeps forever.
  4. //
  5. // Copyright (C) Microsoft Corporation, 2000.
  6. //
  7. //----------------------------------------------------------------------------
  8. #include <stdio.h>
  9. #include <nt.h>
  10. #include <ntrtl.h>
  11. #include <nturtl.h>
  12. #define _ADVAPI32_
  13. #define _KERNEL32_
  14. #include <windows.h>
  15. #define INITGUID
  16. #include <objbase.h>
  17. #define NOEXTAPI
  18. #include <wdbgexts.h>
  19. #include <ntdbg.h>
  20. #include <cmnutil.hpp>
  21. #include <dllimp.h>
  22. #include <dbgrpc.hpp>
  23. #include <dbgsvc.h>
  24. #include <dbgsvc.hpp>
  25. // The .CRT section is generated when static intializers,
  26. // such as global class instances, exist. It needs to
  27. // be merged into .data to avoid a linker warning.
  28. #pragma comment(linker, "/merge:.CRT=.data")
  29. void DECLSPEC_NORETURN
  30. PanicVa(HRESULT Status, char* Format, va_list Args)
  31. {
  32. char Msg[256];
  33. _vsnprintf(Msg, sizeof(Msg), Format, Args);
  34. DbgPrint("Error 0x%08X: %s\n", Status, Msg);
  35. NtTerminateProcess(NtCurrentProcess(), (NTSTATUS)Status);
  36. }
  37. void DECLSPEC_NORETURN
  38. Panic(HRESULT Status, char* Format, ...)
  39. {
  40. va_list Args;
  41. va_start(Args, Format);
  42. PanicVa(Status, Format, Args);
  43. va_end(Args);
  44. }
  45. #if DBG
  46. void
  47. DbgAssertionFailed(PCSTR File, int Line, PCSTR Str)
  48. {
  49. Panic(E_FAIL, "Assertion failed: %s(%d)\n %s\n",
  50. File, Line, Str);
  51. }
  52. #endif
  53. //----------------------------------------------------------------------------
  54. //
  55. // Proxy and stub support.
  56. //
  57. //----------------------------------------------------------------------------
  58. // Generated headers.
  59. #include "dbgsvc_p.hpp"
  60. #include "dbgsvc_s.hpp"
  61. void
  62. DbgRpcInitializeClient(void)
  63. {
  64. DbgRpcInitializeStubTables_dbgsvc(DBGRPC_SIF_DBGSVC_FIRST);
  65. }
  66. DbgRpcStubFunction
  67. DbgRpcGetStub(USHORT StubIndex)
  68. {
  69. USHORT If = (USHORT) DBGRPC_STUB_INDEX_INTERFACE(StubIndex);
  70. USHORT Mth = (USHORT) DBGRPC_STUB_INDEX_METHOD(StubIndex);
  71. DbgRpcStubFunctionTable* Table;
  72. if (If >= DBGRPC_SIF_DBGSVC_FIRST &&
  73. If >= DBGRPC_SIF_DBGSVC_LAST)
  74. {
  75. Table = g_DbgRpcStubs_dbgsvc;
  76. If -= DBGRPC_SIF_DBGSVC_FIRST;
  77. }
  78. else
  79. {
  80. return NULL;
  81. }
  82. if (Mth >= Table[If].Count)
  83. {
  84. return NULL;
  85. }
  86. return Table[If].Functions[Mth];
  87. }
  88. #if DBG
  89. PCSTR
  90. DbgRpcGetStubName(USHORT StubIndex)
  91. {
  92. USHORT If = (USHORT) DBGRPC_STUB_INDEX_INTERFACE(StubIndex);
  93. USHORT Mth = (USHORT) DBGRPC_STUB_INDEX_METHOD(StubIndex);
  94. DbgRpcStubFunctionTable* Table;
  95. PCSTR** Names;
  96. if (If >= DBGRPC_SIF_DBGSVC_FIRST &&
  97. If >= DBGRPC_SIF_DBGSVC_LAST)
  98. {
  99. Table = g_DbgRpcStubs_dbgsvc;
  100. Names = g_DbgRpcStubNames_dbgsvc;
  101. If -= DBGRPC_SIF_DBGSVC_FIRST;
  102. }
  103. else
  104. {
  105. return "!InvalidInterface!";
  106. }
  107. if (Mth >= Table[If].Count)
  108. {
  109. return "!InvalidStubIndex!";
  110. }
  111. return Names[If][Mth];
  112. }
  113. #endif // #if DBG
  114. HRESULT
  115. DbgRpcPreallocProxy(REFIID InterfaceId, PVOID* Interface,
  116. DbgRpcProxy** Proxy, PULONG IfUnique)
  117. {
  118. return DbgRpcPreallocProxy_dbgsvc(InterfaceId, Interface,
  119. Proxy, IfUnique);
  120. }
  121. void
  122. DbgRpcDeleteProxy(class DbgRpcProxy* Proxy)
  123. {
  124. // All proxies used here are similar simple single
  125. // vtable proxy objects so IDebugClient can represent them all.
  126. delete (ProxyIUserDebugServices*)Proxy;
  127. }
  128. HRESULT
  129. DbgRpcServerThreadInitialize(void)
  130. {
  131. // Nothing to do.
  132. return S_OK;
  133. }
  134. void
  135. DbgRpcServerThreadUninitialize(void)
  136. {
  137. // Nothing to do.
  138. }
  139. void
  140. DbgRpcError(char* Format, ...)
  141. {
  142. va_list Args;
  143. va_start(Args, Format);
  144. PanicVa(E_FAIL, Format, Args);
  145. va_end(Args);
  146. }
  147. DBGRPC_SIMPLE_FACTORY(LiveUserDebugServices, __uuidof(IUserDebugServices), \
  148. "Remote Process Server", (TRUE))
  149. LiveUserDebugServicesFactory g_LiveUserDebugServicesFactory;
  150. #ifdef _M_IA64
  151. #pragma section(".CRT$XCA",long,read)
  152. #pragma section(".CRT$XCZ",long,read)
  153. #pragma section(".CRT$XIA",long,read)
  154. #pragma section(".CRT$XIZ",long,read)
  155. #define _CRTALLOC(x) __declspec(allocate(x))
  156. #else /* ndef _M_IA64 */
  157. #define _CRTALLOC(x)
  158. #endif /* ndef _M_IA64 */
  159. typedef void (__cdecl *_PVFV)(void);
  160. extern "C"
  161. {
  162. // C initializers collect here.
  163. #pragma data_seg(".CRT$XIA")
  164. _CRTALLOC(".CRT$XIA") _PVFV __xi_a[] = { NULL };
  165. #pragma data_seg(".CRT$XIZ")
  166. _CRTALLOC(".CRT$XIZ") _PVFV __xi_z[] = { NULL };
  167. // C++ initializers collect here.
  168. #pragma data_seg(".CRT$XCA")
  169. _CRTALLOC(".CRT$XCA") _PVFV __xc_a[] = { NULL };
  170. #pragma data_seg(".CRT$XCZ")
  171. _CRTALLOC(".CRT$XCZ") _PVFV __xc_z[] = { NULL };
  172. };
  173. void __cdecl
  174. _initterm (_PVFV * pfbegin, _PVFV * pfend)
  175. {
  176. /*
  177. * walk the table of function pointers from the bottom up, until
  178. * the end is encountered. Do not skip the first entry. The initial
  179. * value of pfbegin points to the first valid entry. Do not try to
  180. * execute what pfend points to. Only entries before pfend are valid.
  181. */
  182. while ( pfbegin < pfend )
  183. {
  184. /*
  185. * if current table entry is non-NULL, call thru it.
  186. */
  187. if ( *pfbegin != NULL )
  188. {
  189. (**pfbegin)();
  190. }
  191. ++pfbegin;
  192. }
  193. }
  194. void __cdecl
  195. main(int Argc, char** Argv)
  196. {
  197. PSTR AppName;
  198. PSTR Options;
  199. HRESULT Status;
  200. // Manually invoke C and C++ initializers.
  201. _initterm( __xi_a, __xi_z );
  202. _initterm( __xc_a, __xc_z );
  203. AppName = Argv[0];
  204. while (--Argc > 0)
  205. {
  206. Argv++;
  207. break;
  208. }
  209. if (Argc != 1)
  210. {
  211. Panic(E_INVALIDARG, "Usage: dbgsrv <transport>");
  212. }
  213. Options = *Argv;
  214. DbgPrint("Running %s with '%s'\n", AppName, Options);
  215. if ((Status = InitDynamicCalls(&g_NtDllCallsDesc)) != S_OK)
  216. {
  217. Panic(Status, "InitDynamicCalls");
  218. }
  219. ULONG Flags;
  220. if ((Status = g_LiveUserDebugServices.Initialize(&Flags)) != S_OK)
  221. {
  222. Panic(Status, "LiveUserDebugServices::Initialize");
  223. }
  224. if ((Status = DbgRpcCreateServer(Options,
  225. &g_LiveUserDebugServicesFactory)) != S_OK)
  226. {
  227. Panic(Status, "StartProcessServer");
  228. }
  229. for (;;)
  230. {
  231. Sleep(1000);
  232. if (g_UserServicesUninitialized)
  233. {
  234. break;
  235. }
  236. }
  237. DbgRpcDeregisterServers();
  238. }