ReactOS  0.4.12-dev-685-gf36cbf7
dbgctrl.c
Go to the documentation of this file.
1 /*
2  * PROJECT: ReactOS Kernel
3  * LICENSE: GPL - See COPYING in the top level directory
4  * FILE: ntoskrnl/ex/dbgctrl.c
5  * PURPOSE: System debug control
6  * PROGRAMMERS: Alex Ionescu
7  */
8 
9 /* INCLUDES *****************************************************************/
10 
11 #include <ntoskrnl.h>
12 #define NDEBUG
13 #include <debug.h>
14 
15 /* DATA **********************************************************************/
16 
17 // #ifdef _WINKD_
18 /*
19  * WinDBG Debugger Worker State Machine data
20  */
22 /*
23  * The following global variables must be visible through all the kernel
24  * because WinDBG explicitely search for them inside our symbols.
25  */
30 // #endif /* _WINKD_ */
31 
32 /* FUNCTIONS *****************************************************************/
33 
34 // #ifdef _WINKD_
35 /*
36  * WinDBG Debugger Worker State Machine
37  *
38  * This functionality is used whenever WinDBG wants to attach or kill a user-mode
39  * process from within live kernel-mode session, and/or page-in an address region.
40  * It is implemented as a state machine: when it is in "Ready" state, WinDBG can
41  * initialize the data for the state machine, then switch its state to "Start".
42  * The worker thread balance manager detects this, switches the state to "Initialized"
43  * and queues a worker thread. As long as the state is not "Ready" again, WinDBG
44  * prevents from requeuing a new thread. When the thread is started, it captures
45  * all the data, then resets the machine state to "Ready", thus allowing WinDBG
46  * to requeue another worker thread.
47  *
48  * WinDBG commands:
49  * .process /i <addr> (where <addr> is the address of the EPROCESS block for this process)
50  * .kill <addr> ( " " " " )
51  * .pagein <addr> (where <addr> is the address to page in)
52  */
53 VOID
54 NTAPI
56 {
57  PEPROCESS ProcessToAttach, ProcessToKill;
58  ULONG_PTR PageInAddress;
61 
63 
64  /* Be sure we were started in an initialized state */
65  ASSERTMSG("ExpDebuggerWorker being entered in non-initialized state!\n",
68  {
69  /* An error happened, so get a chance to restart proper */
71  return;
72  }
73 
74  /* Get the processes to be attached or killed, and the address to page in */
75  ProcessToAttach = ExpDebuggerProcessAttach;
76  ProcessToKill = ExpDebuggerProcessKill;
77  PageInAddress = ExpDebuggerPageIn;
78 
79  /* Reset the state machine to its ready state */
84 
85  /* Default to the current process if we don't find the process to be attached or killed */
86  Process = NULL;
87 
88  /* Check if we need to attach or kill some process */
89  if (ProcessToAttach != NULL || ProcessToKill != NULL)
90  {
91  /* Find the process in the list */
93  while (Process)
94  {
95  /* Is this the process we want to attach to? */
96  if (Process == ProcessToAttach)
97  {
98  /* Yes, attach ourselves to it */
100  break;
101  }
102  /* Or is this the process we want to kill? */
103  else if (Process == ProcessToKill)
104  {
105  /* Yes, kill and dereference it, then return */
108  return;
109  }
110 
111  /* Get the next process */
113  }
114 
115  /* We either have found a process, or we default to the current process */
116  }
117 
118  /* If we have an address to page in... */
119  if (PageInAddress)
120  {
121  /* ... try to do it by attempting to read at this address */
122  _SEH2_TRY
123  {
124  ProbeForReadUchar(PageInAddress);
125  }
127  {
128  DPRINT1("Failed to page in address 0x%p, Status 0x%08lx\n", PageInAddress, _SEH2_GetExceptionCode());
129  }
130  _SEH2_END;
131  }
132 
133  /* Break into the process (or the current one if Process == NULL) */
135 
136  /* If we are attached to a process, not the current one... */
137  if (Process)
138  {
139  /* ... we can detach from the process */
141  /* Dereference the process which was referenced for us by PsGetNextProcess */
143  }
144 }
145 // #endif /* _WINKD_ */
146 
147 /*++
148  * @name NtSystemDebugControl
149  * @implemented
150  *
151  * Perform various queries to debugger.
152  * This API is subject to test-case creation to further evaluate its
153  * abilities (if needed to at all)
154  *
155  * See: http://www.osronline.com/showthread.cfm?link=93915
156  * http://void.ru/files/Ntexapi.h
157  * http://www.codeguru.com/code/legacy/system/ntexapi.zip
158  * http://www.securityfocus.com/bid/9694
159  *
160  * @param ControlCode
161  * Description of the parameter. Wrapped to more lines on ~70th
162  * column.
163  *
164  * @param InputBuffer
165  * FILLME
166  *
167  * @param InputBufferLength
168  * FILLME
169  *
170  * @param OutputBuffer
171  * FILLME
172  *
173  * @param OutputBufferLength
174  * FILLME
175  *
176  * @param ReturnLength
177  * FILLME
178  *
179  * @return STATUS_SUCCESS in case of success, proper error code otherwise
180  *
181  * @remarks None
182  *
183  *--*/
184 NTSTATUS
185 NTAPI
192 {
193  switch (ControlCode)
194  {
197  case SysDbgSetTracepoint:
201  case SysDbgQueryVersion:
202  case SysDbgReadVirtual:
203  case SysDbgWriteVirtual:
204  case SysDbgReadPhysical:
205  case SysDbgWritePhysical:
208  case SysDbgReadIoSpace:
209  case SysDbgWriteIoSpace:
210  case SysDbgReadMsr:
211  case SysDbgWriteMsr:
212  case SysDbgReadBusData:
213  case SysDbgWriteBusData:
215  case SysDbgGetTriageDump:
216  return STATUS_NOT_IMPLEMENTED;
217  case SysDbgBreakPoint:
228  return KdSystemDebugControl(
229  ControlCode,
233  default:
235  }
236 }
IN CINT OUT PVOID IN ULONG OUT PULONG ReturnLength
Definition: dumpinfo.c:39
#define IN
Definition: typedefs.h:38
enum _WINKD_WORKER_STATE WINKD_WORKER_STATE
PEPROCESS ExpDebuggerProcessKill
Definition: dbgctrl.c:28
KAPC_STATE
Definition: ketypes.h:1273
#define DBG_TERMINATE_PROCESS
Definition: ntstatus.h:51
#define ProbeForReadUchar(Ptr)
Definition: probe.h:61
#define STATUS_NOT_IMPLEMENTED
Definition: ntstatus.h:225
#define UNREFERENCED_PARAMETER(P)
Definition: ntbasedef.h:323
#define KeGetPreviousMode()
Definition: ketypes.h:1081
LONG NTSTATUS
Definition: precomp.h:26
_Must_inspect_result_ _In_ PFILE_OBJECT _In_ ULONG _In_ ULONG _In_ ULONG OutputBufferLength
Definition: fltkernel.h:1374
VOID NTAPI ObDereferenceObject(IN PVOID Object)
Definition: obref.c:375
PEPROCESS ExpDebuggerProcessAttach
Definition: dbgctrl.c:27
#define DBG_STATUS_WORKER
Definition: kdtypes.h:45
CHAR InputBuffer[80]
Definition: conmgr.c:33
_SEH2_TRY
Definition: create.c:4250
uint32_t ULONG_PTR
Definition: typedefs.h:63
NTSTATUS(* NTAPI)(IN PFILE_FULL_EA_INFORMATION EaBuffer, IN ULONG EaLength, OUT PULONG ErrorOffset)
Definition: IoEaTest.cpp:117
VOID NTAPI KeStackAttachProcess(IN PKPROCESS Process, OUT PRKAPC_STATE ApcState)
Definition: procobj.c:701
#define ASSERTMSG(msg, exp)
Definition: nt_native.h:431
#define EXCEPTION_EXECUTE_HANDLER
Definition: excpt.h:85
enum _SYSDBG_COMMAND SYSDBG_COMMAND
smooth NULL
Definition: ftsmooth.c:416
_IRQL_requires_same_ typedef _In_ ULONG ControlCode
Definition: wmitypes.h:55
_Must_inspect_result_ __drv_aliasesMem _In_ PDEVICE_OBJECT _In_opt_ PVOID _In_ ULONG _Out_opt_ PVOID OutputBuffer
Definition: iofuncs.h:713
NTSTATUS NTAPI KdSystemDebugControl(IN SYSDBG_COMMAND Command, IN PVOID InputBuffer, IN ULONG InputBufferLength, OUT PVOID OutputBuffer, IN ULONG OutputBufferLength, IN OUT PULONG ReturnLength, IN KPROCESSOR_MODE PreviousMode)
Definition: kdmain.c:489
WORK_QUEUE_ITEM ExpDebuggerWorkItem
Definition: dbgctrl.c:21
NTSTATUS NTAPI PsTerminateProcess(IN PEPROCESS Process, IN NTSTATUS ExitStatus)
Definition: kill.c:129
NTSTATUS NTAPI NtSystemDebugControl(SYSDBG_COMMAND ControlCode, PVOID InputBuffer, ULONG InputBufferLength, PVOID OutputBuffer, ULONG OutputBufferLength, PULONG ReturnLength)
Definition: dbgctrl.c:186
ULONG_PTR ExpDebuggerPageIn
Definition: dbgctrl.c:29
#define STATUS_INVALID_INFO_CLASS
Definition: ntstatus.h:226
PEPROCESS NTAPI PsGetNextProcess(IN PEPROCESS OldProcess OPTIONAL)
Definition: process.c:128
_Must_inspect_result_ _In_ PFILE_OBJECT _In_ ULONG _In_ ULONG InputBufferLength
Definition: fltkernel.h:1372
_SEH2_END
Definition: create.c:4424
VOID NTAPI ExpDebuggerWorker(IN PVOID Context)
Definition: dbgctrl.c:55
VOID NTAPI KeUnstackDetachProcess(IN PRKAPC_STATE ApcState)
Definition: procobj.c:753
_Out_ PKAPC_STATE ApcState
Definition: mm.h:1484
unsigned int * PULONG
Definition: retypes.h:1
#define DPRINT1
Definition: precomp.h:8
_Must_inspect_result_ _In_ PLARGE_INTEGER _In_ PLARGE_INTEGER _In_ ULONG _In_ PFILE_OBJECT _In_ PVOID Process
Definition: fsrtlfuncs.h:219
WINKD_WORKER_STATE ExpDebuggerWork
Definition: dbgctrl.c:26
unsigned int ULONG
Definition: retypes.h:1
#define ULONG_PTR
Definition: config.h:101
#define _SEH2_EXCEPT(...)
Definition: pseh2_64.h:6
__analysis_noreturn NTSYSAPI VOID NTAPI DbgBreakPointWithStatus(_In_ ULONG Status)
#define _SEH2_GetExceptionCode()
Definition: pseh2_64.h:12