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.

281 lines
7.7 KiB

  1. //----------------------------------------------------------------------------
  2. //
  3. // Controls the current kernel debugger.
  4. //
  5. // Copyright (C) Microsoft Corporation, 2001-2002.
  6. //
  7. //----------------------------------------------------------------------------
  8. #include <stdlib.h>
  9. #include <stdio.h>
  10. #include <nt.h>
  11. #include <ntrtl.h>
  12. #include <nturtl.h>
  13. #include <windows.h>
  14. #include <cmnutil.hpp>
  15. PSTR g_AppName;
  16. void DECLSPEC_NORETURN
  17. ShowUsage(void)
  18. {
  19. printf("Usage: %s <options>\n", g_AppName);
  20. printf("Options:\n");
  21. printf(" -c - Check kernel debugger enable\n");
  22. printf(" -ca - Check kernel debugger auto-enable\n");
  23. printf(" -cdb - Check kernel DbgPrint buffer size\n");
  24. printf(" -cu - Check kernel debugger user exception handling\n");
  25. printf(" -cx - Check kernel debugger enable and exit with status\n");
  26. printf(" -d - Disable kernel debugger\n");
  27. printf(" -da - Disable kernel debugger auto-enable\n");
  28. printf(" -du - Disable kernel debugger user exception handling\n");
  29. printf(" -e - Enable kernel debugger\n");
  30. printf(" -ea - Enable kernel debugger auto-enable\n");
  31. printf(" -eu - Enable kernel debugger user exception handling\n");
  32. printf(" -sdb # - Set kernel DbgPrint buffer size\n");
  33. exit(1);
  34. }
  35. void
  36. QueryKdInfo(BOOL Exit)
  37. {
  38. NTSTATUS NtStatus;
  39. SYSTEM_KERNEL_DEBUGGER_INFORMATION KdInfo;
  40. NtStatus =
  41. NtQuerySystemInformation(SystemKernelDebuggerInformation,
  42. &KdInfo, sizeof(KdInfo), NULL);
  43. if (Exit)
  44. {
  45. if (!NT_SUCCESS(NtStatus))
  46. {
  47. exit((int)NtStatus);
  48. }
  49. else
  50. {
  51. exit(KdInfo.KernelDebuggerEnabled ?
  52. (int)DBG_EXCEPTION_HANDLED : (int)DBG_CONTINUE);
  53. }
  54. }
  55. else if (!NT_SUCCESS(NtStatus))
  56. {
  57. HRESULT Status = HRESULT_FROM_NT(NtStatus);
  58. printf("Unable to check kernel debugger status, %s\n %s\n",
  59. FormatStatusCode(Status), FormatStatus(Status));
  60. }
  61. else
  62. {
  63. printf("Kernel debugger is %s\n",
  64. KdInfo.KernelDebuggerEnabled ? "enabled" : "disabled");
  65. }
  66. }
  67. void
  68. SdcSimpleCall(SYSDBG_COMMAND Command, PSTR Success, PSTR Failure)
  69. {
  70. NTSTATUS NtStatus;
  71. NtStatus = NtSystemDebugControl(Command, NULL, 0, NULL, 0, NULL);
  72. if (!NT_SUCCESS(NtStatus))
  73. {
  74. HRESULT Status = HRESULT_FROM_NT(NtStatus);
  75. printf("%s, %s\n %s\n",
  76. Failure, FormatStatusCode(Status), FormatStatus(Status));
  77. }
  78. else
  79. {
  80. printf("%s\n", Success);
  81. }
  82. }
  83. void
  84. SdcOutputBool(SYSDBG_COMMAND Command,
  85. PSTR Name)
  86. {
  87. NTSTATUS NtStatus;
  88. ULONG Value = 0;
  89. NtStatus = NtSystemDebugControl(Command, NULL, 0,
  90. &Value, sizeof(BOOLEAN),
  91. NULL);
  92. if (!NT_SUCCESS(NtStatus))
  93. {
  94. HRESULT Status = HRESULT_FROM_NT(NtStatus);
  95. printf("Unable to get %s, %s\n %s\n",
  96. Name, FormatStatusCode(Status), FormatStatus(Status));
  97. }
  98. else
  99. {
  100. printf("%s: %s\n", Name, Value ? "true" : "false");
  101. }
  102. }
  103. void
  104. SdcSetBool(SYSDBG_COMMAND Command, BOOL Value,
  105. PSTR Name)
  106. {
  107. NTSTATUS NtStatus;
  108. // Force value to canonical form.
  109. Value = Value ? TRUE : FALSE;
  110. NtStatus = NtSystemDebugControl(Command, &Value, sizeof(BOOLEAN),
  111. NULL, 0, NULL);
  112. if (!NT_SUCCESS(NtStatus))
  113. {
  114. HRESULT Status = HRESULT_FROM_NT(NtStatus);
  115. printf("Unable to set %s, %s\n %s\n",
  116. Name, FormatStatusCode(Status), FormatStatus(Status));
  117. }
  118. else
  119. {
  120. printf("%s set to: %s\n", Name, Value ? "true" : "false");
  121. }
  122. }
  123. void
  124. SdcOutputUlong(SYSDBG_COMMAND Command,
  125. PSTR Name)
  126. {
  127. NTSTATUS NtStatus;
  128. ULONG Value;
  129. NtStatus = NtSystemDebugControl(Command, NULL, 0,
  130. &Value, sizeof(Value),
  131. NULL);
  132. if (!NT_SUCCESS(NtStatus))
  133. {
  134. HRESULT Status = HRESULT_FROM_NT(NtStatus);
  135. printf("Unable to get %s, %s\n %s\n",
  136. Name, FormatStatusCode(Status), FormatStatus(Status));
  137. }
  138. else
  139. {
  140. printf("%s: 0x%x\n", Name, Value);
  141. }
  142. }
  143. void
  144. SdcSetUlong(SYSDBG_COMMAND Command, ULONG Value,
  145. PSTR Name)
  146. {
  147. NTSTATUS NtStatus;
  148. NtStatus = NtSystemDebugControl(Command, &Value, sizeof(Value),
  149. NULL, 0, NULL);
  150. if (!NT_SUCCESS(NtStatus))
  151. {
  152. HRESULT Status = HRESULT_FROM_NT(NtStatus);
  153. printf("Unable to set %s, %s\n %s\n",
  154. Name, FormatStatusCode(Status), FormatStatus(Status));
  155. }
  156. else
  157. {
  158. printf("%s set to: 0x%x\n", Name, Value);
  159. }
  160. }
  161. int __cdecl
  162. main(int Argc, char** Argv)
  163. {
  164. BOOL Usage = FALSE;
  165. HRESULT Status;
  166. g_AppName = *Argv;
  167. if ((Status = EnableDebugPrivilege()) != S_OK)
  168. {
  169. printf("Unable to enable debug privilege, %s\n %s\n",
  170. FormatStatusCode(Status), FormatStatus(Status));
  171. return 1;
  172. }
  173. while (--Argc > 0 && !Usage)
  174. {
  175. Argv++;
  176. if (!strcmp(*Argv, "-?"))
  177. {
  178. Usage = TRUE;
  179. }
  180. else if (!strcmp(*Argv, "-c") ||
  181. !strcmp(*Argv, "-cx"))
  182. {
  183. QueryKdInfo(Argv[0][2] == 'x');
  184. }
  185. else if (!strcmp(*Argv, "-ca"))
  186. {
  187. SdcOutputBool(SysDbgGetAutoKdEnable,
  188. "Kernel debugger auto-enable");
  189. }
  190. else if (!strcmp(*Argv, "-cdb"))
  191. {
  192. SdcOutputUlong(SysDbgGetPrintBufferSize,
  193. "Kernel DbgPrint buffer size");
  194. }
  195. else if (!strcmp(*Argv, "-cu"))
  196. {
  197. SdcOutputBool(SysDbgGetKdUmExceptionEnable,
  198. "Kernel debugger user exception enable");
  199. }
  200. else if (!strcmp(*Argv, "-d"))
  201. {
  202. SdcSimpleCall(SysDbgDisableKernelDebugger,
  203. "Kernel debugger disabled",
  204. "Unable to disable kernel debugger");
  205. }
  206. else if (!strcmp(*Argv, "-da"))
  207. {
  208. SdcSetBool(SysDbgSetAutoKdEnable, FALSE,
  209. "Kernel debugger auto-enable");
  210. }
  211. else if (!strcmp(*Argv, "-du"))
  212. {
  213. SdcSetBool(SysDbgSetKdUmExceptionEnable, FALSE,
  214. "Kernel debugger user exception enable");
  215. }
  216. else if (!strcmp(*Argv, "-e"))
  217. {
  218. SdcSimpleCall(SysDbgEnableKernelDebugger,
  219. "Kernel debugger enabled",
  220. "Unable to enable kernel debugger");
  221. }
  222. else if (!strcmp(*Argv, "-ea"))
  223. {
  224. SdcSetBool(SysDbgSetAutoKdEnable, TRUE,
  225. "Kernel debugger auto-enable");
  226. }
  227. else if (!strcmp(*Argv, "-eu"))
  228. {
  229. SdcSetBool(SysDbgSetKdUmExceptionEnable, TRUE,
  230. "Kernel debugger user exception enable");
  231. }
  232. else if (!strcmp(*Argv, "-sdb"))
  233. {
  234. if (Argc < 2)
  235. {
  236. Usage = TRUE;
  237. break;
  238. }
  239. Argc--;
  240. Argv++;
  241. SdcSetUlong(SysDbgSetPrintBufferSize, strtoul(*Argv, NULL, 0),
  242. "Kernel DbgPrint buffer size");
  243. }
  244. else
  245. {
  246. Usage = TRUE;
  247. }
  248. }
  249. if (Usage)
  250. {
  251. ShowUsage();
  252. }
  253. return 0;
  254. }