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.

170 lines
3.8 KiB

  1. //----------------------------------------------------------------------------
  2. //
  3. // Simple process list through the debug engine interface
  4. // process enumeration methods.
  5. //
  6. // Copyright (C) Microsoft Corporation, 2000.
  7. //
  8. //----------------------------------------------------------------------------
  9. #include <stdio.h>
  10. #include <dbgeng.h>
  11. PDEBUG_CLIENT g_Client;
  12. PSTR g_ProcName;
  13. void DECLSPEC_NORETURN
  14. Panic(HRESULT Status, char* Format, ...)
  15. {
  16. va_list Args;
  17. fprintf(stderr, "Error 0x%08X: ", Status);
  18. va_start(Args, Format);
  19. vfprintf(stderr, Format, Args);
  20. va_end(Args);
  21. exit(1);
  22. }
  23. void DECLSPEC_NORETURN
  24. UsageExit(void)
  25. {
  26. printf("Usage: rtlist <Options>\n");
  27. printf("Options are:\n");
  28. printf(" -premote <Options> - Connect to process server\n");
  29. printf(" -pn <Name> - Look for process name\n");
  30. exit(2);
  31. }
  32. #define MAX_IDS 16384
  33. void
  34. List(ULONG64 Server)
  35. {
  36. HRESULT Status;
  37. ULONG Ids[MAX_IDS];
  38. ULONG IdCount;
  39. ULONG i;
  40. if (g_ProcName != NULL)
  41. {
  42. if ((Status = g_Client->
  43. GetRunningProcessSystemIdByExecutableName
  44. (Server, g_ProcName, DEBUG_GET_PROC_ONLY_MATCH, &Ids[0])) != S_OK)
  45. {
  46. Panic(Status, "GetRunningProcessSystemIdByExecutableName\n");
  47. }
  48. IdCount = 1;
  49. }
  50. else
  51. {
  52. if ((Status = g_Client->
  53. GetRunningProcessSystemIds(Server, Ids, MAX_IDS,
  54. &IdCount)) != S_OK)
  55. {
  56. Panic(Status, "GetRunningProcessSystemIds\n");
  57. }
  58. if (IdCount > MAX_IDS)
  59. {
  60. fprintf(stderr, "Process list missing %d processes\n",
  61. IdCount - MAX_IDS);
  62. IdCount = MAX_IDS;
  63. }
  64. }
  65. for (i = 0; i < IdCount; i++)
  66. {
  67. char ExeName[MAX_PATH];
  68. if ((Status = g_Client->
  69. GetRunningProcessDescription(Server, Ids[i],
  70. DEBUG_PROC_DESC_DEFAULT,
  71. ExeName, sizeof(ExeName), NULL,
  72. NULL, 0, NULL)) != S_OK)
  73. {
  74. sprintf(ExeName, "Error 0x%08X", Status);
  75. }
  76. if (Ids[i] >= 0x80000000)
  77. {
  78. printf("0x%x %s\n", Ids[i], ExeName);
  79. }
  80. else
  81. {
  82. printf("0n%d %s\n", Ids[i], ExeName);
  83. }
  84. }
  85. }
  86. void __cdecl
  87. main(int Argc, char** Argv)
  88. {
  89. HRESULT Status;
  90. if ((Status = DebugCreate(__uuidof(IDebugClient),
  91. (void**)&g_Client)) != S_OK)
  92. {
  93. Panic(Status, "DebugCreate\n");
  94. }
  95. BOOL Usage = FALSE;
  96. ULONG64 Server = 0;
  97. while (--Argc > 0)
  98. {
  99. Argv++;
  100. if (!strcmp(*Argv, "-premote"))
  101. {
  102. if (Argc < 2 || Server != 0)
  103. {
  104. Usage = TRUE;
  105. }
  106. else
  107. {
  108. Argc--;
  109. Argv++;
  110. if ((Status = g_Client->
  111. ConnectProcessServer(*Argv, &Server)) != S_OK)
  112. {
  113. Panic(Status, "ConnectProcessServer\n");
  114. }
  115. }
  116. }
  117. else if (!strcmp(*Argv, "-pn"))
  118. {
  119. if (Argc < 2)
  120. {
  121. Usage = 2;
  122. }
  123. else
  124. {
  125. Argc--;
  126. Argv++;
  127. g_ProcName = *Argv;
  128. }
  129. }
  130. else
  131. {
  132. Usage = TRUE;
  133. break;
  134. }
  135. }
  136. if (Usage)
  137. {
  138. UsageExit();
  139. }
  140. List(Server);
  141. if (Server != 0)
  142. {
  143. g_Client->DisconnectProcessServer(Server);
  144. }
  145. g_Client->Release();
  146. }