ReactOS 0.4.15-dev-7131-ge4d03f4
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/*
18 * WinDBG Debugger Worker State Machine data
19 */
21/*
22 * The following global variables must be visible through all the kernel
23 * because WinDBG explicitely search for them inside our symbols.
24 */
29
30/* FUNCTIONS *****************************************************************/
31
32/*
33 * WinDBG Debugger Worker State Machine
34 *
35 * This functionality is used whenever WinDBG wants to attach or kill a user-mode
36 * process from within live kernel-mode session, and/or page-in an address region.
37 * It is implemented as a state machine: when it is in "Ready" state, WinDBG can
38 * initialize the data for the state machine, then switch its state to "Start".
39 * The worker thread balance manager detects this, switches the state to "Initialized"
40 * and queues a worker thread. As long as the state is not "Ready" again, WinDBG
41 * prevents from requeuing a new thread. When the thread is started, it captures
42 * all the data, then resets the machine state to "Ready", thus allowing WinDBG
43 * to requeue another worker thread.
44 *
45 * WinDBG commands:
46 * .process /i <addr> (where <addr> is the address of the EPROCESS block for this process)
47 * .kill <addr> ( " " " " )
48 * .pagein <addr> (where <addr> is the address to page in)
49 */
50VOID
53{
54 PEPROCESS ProcessToAttach, ProcessToKill;
55 ULONG_PTR PageInAddress;
58
60
61 /* Be sure we were started in an initialized state */
62 ASSERTMSG("ExpDebuggerWorker being entered in non-initialized state!\n",
65 {
66 /* An error happened, so get a chance to restart proper */
68 return;
69 }
70
71 /* Get the processes to be attached or killed, and the address to page in */
72 ProcessToAttach = ExpDebuggerProcessAttach;
73 ProcessToKill = ExpDebuggerProcessKill;
74 PageInAddress = ExpDebuggerPageIn;
75
76 /* Reset the state machine to its ready state */
81
82 /* Default to the current process if we don't find the process to be attached or killed */
83 Process = NULL;
84
85 /* Check if we need to attach or kill some process */
86 if (ProcessToAttach != NULL || ProcessToKill != NULL)
87 {
88 /* Find the process in the list */
90 while (Process)
91 {
92 /* Is this the process we want to attach to? */
93 if (Process == ProcessToAttach)
94 {
95 /* Yes, attach ourselves to it */
97 break;
98 }
99 /* Or is this the process we want to kill? */
100 else if (Process == ProcessToKill)
101 {
102 /* Yes, kill and dereference it, then return */
105 return;
106 }
107
108 /* Get the next process */
110 }
111
112 /* We either have found a process, or we default to the current process */
113 }
114
115 /* If we have an address to page in... */
116 if (PageInAddress)
117 {
118 /* ... try to do it by attempting to read at this address */
120 {
121 ProbeForReadUchar(PageInAddress);
122 }
124 {
125 DPRINT1("Failed to page in address 0x%p, Status 0x%08lx\n", PageInAddress, _SEH2_GetExceptionCode());
126 }
127 _SEH2_END;
128 }
129
130 /* Break into the process (or the current one if Process == NULL) */
132
133 /* If we are attached to a process, not the current one... */
134 if (Process)
135 {
136 /* ... we can detach from the process */
138 /* Dereference the process which was referenced for us by PsGetNextProcess */
140 }
141}
142
143/*++
144 * @name NtSystemDebugControl
145 * @implemented
146 *
147 * Perform various queries to debugger.
148 * This API is subject to test-case creation to further evaluate its
149 * abilities (if needed to at all)
150 *
151 * See: http://www.osronline.com/showthread.cfm?link=93915
152 * http://void.ru/files/Ntexapi.h
153 * http://www.codeguru.com/code/legacy/system/ntexapi.zip
154 * http://www.securityfocus.com/bid/9694
155 *
156 * @param ControlCode
157 * Description of the parameter. Wrapped to more lines on ~70th
158 * column.
159 *
160 * @param InputBuffer
161 * FILLME
162 *
163 * @param InputBufferLength
164 * FILLME
165 *
166 * @param OutputBuffer
167 * FILLME
168 *
169 * @param OutputBufferLength
170 * FILLME
171 *
172 * @param ReturnLength
173 * FILLME
174 *
175 * @return STATUS_SUCCESS in case of success, proper error code otherwise
176 *
177 * @remarks None
178 *
179 *--*/
181NTAPI
188{
189 switch (ControlCode)
190 {
206 case SysDbgReadMsr:
207 case SysDbgWriteMsr:
213 case SysDbgBreakPoint:
229 default:
231 }
232}
LONG NTSTATUS
Definition: precomp.h:26
#define DPRINT1
Definition: precomp.h:8
PEPROCESS ExpDebuggerProcessAttach
Definition: dbgctrl.c:26
PEPROCESS ExpDebuggerProcessKill
Definition: dbgctrl.c:27
WINKD_WORKER_STATE ExpDebuggerWork
Definition: dbgctrl.c:25
ULONG_PTR ExpDebuggerPageIn
Definition: dbgctrl.c:28
VOID NTAPI ExpDebuggerWorker(IN PVOID Context)
Definition: dbgctrl.c:52
WORK_QUEUE_ITEM ExpDebuggerWorkItem
Definition: dbgctrl.c:20
NTSTATUS NTAPI NtSystemDebugControl(SYSDBG_COMMAND ControlCode, PVOID InputBuffer, ULONG InputBufferLength, PVOID OutputBuffer, ULONG OutputBufferLength, PULONG ReturnLength)
Definition: dbgctrl.c:182
#define NULL
Definition: types.h:112
#define ULONG_PTR
Definition: config.h:101
IN CINT OUT PVOID IN ULONG OUT PULONG ReturnLength
Definition: dumpinfo.c:43
enum _WINKD_WORKER_STATE WINKD_WORKER_STATE
@ WinKdWorkerReady
Definition: ex.h:62
@ WinKdWorkerInitialized
Definition: ex.h:64
#define _SEH2_END
Definition: filesup.c:22
#define _SEH2_TRY
Definition: filesup.c:19
_Must_inspect_result_ _In_ PLARGE_INTEGER _In_ PLARGE_INTEGER _In_ ULONG _In_ PFILE_OBJECT _In_ PVOID Process
Definition: fsrtlfuncs.h:223
#define EXCEPTION_EXECUTE_HANDLER
Definition: excpt.h:85
NTSTATUS NTAPI KdSystemDebugControl(_In_ SYSDBG_COMMAND Command, _In_ PVOID InputBuffer, _In_ ULONG InputBufferLength, _Out_ PVOID OutputBuffer, _In_ ULONG OutputBufferLength, _Inout_ PULONG ReturnLength, _In_ KPROCESSOR_MODE PreviousMode)
Definition: kdapi.c:2157
#define KeGetPreviousMode()
Definition: ketypes.h:1115
#define DBG_STATUS_WORKER
Definition: kdtypes.h:45
@ SysDbgCheckLowMemory
Definition: kdtypes.h:82
@ SysDbgQuerySpecialCalls
Definition: kdtypes.h:67
@ SysDbgSetTracepoint
Definition: kdtypes.h:64
@ SysDbgReadPhysical
Definition: kdtypes.h:72
@ SysDbgGetPrintBufferSize
Definition: kdtypes.h:87
@ SysDbgQueryTraceInformation
Definition: kdtypes.h:63
@ SysDbgClearSpecialCalls
Definition: kdtypes.h:66
@ SysDbgReadMsr
Definition: kdtypes.h:78
@ SysDbgWriteControlSpace
Definition: kdtypes.h:75
@ SysDbgGetKdBlockEnable
Definition: kdtypes.h:92
@ SysDbgWriteBusData
Definition: kdtypes.h:81
@ SysDbgEnableKernelDebugger
Definition: kdtypes.h:83
@ SysDbgWriteVirtual
Definition: kdtypes.h:71
@ SysDbgWritePhysical
Definition: kdtypes.h:73
@ SysDbgGetKdUmExceptionEnable
Definition: kdtypes.h:89
@ SysDbgQueryVersion
Definition: kdtypes.h:69
@ SysDbgReadControlSpace
Definition: kdtypes.h:74
@ SysDbgReadBusData
Definition: kdtypes.h:80
@ SysDbgBreakPoint
Definition: kdtypes.h:68
@ SysDbgSetKdUmExceptionEnable
Definition: kdtypes.h:90
@ SysDbgReadIoSpace
Definition: kdtypes.h:76
@ SysDbgGetAutoKdEnable
Definition: kdtypes.h:85
@ SysDbgWriteMsr
Definition: kdtypes.h:79
@ SysDbgReadVirtual
Definition: kdtypes.h:70
@ SysDbgSetPrintBufferSize
Definition: kdtypes.h:88
@ SysDbgQueryModuleInformation
Definition: kdtypes.h:62
@ SysDbgSetAutoKdEnable
Definition: kdtypes.h:86
@ SysDbgGetTriageDump
Definition: kdtypes.h:91
@ SysDbgDisableKernelDebugger
Definition: kdtypes.h:84
@ SysDbgWriteIoSpace
Definition: kdtypes.h:77
@ SysDbgSetSpecialCall
Definition: kdtypes.h:65
@ SysDbgSetKdBlockEnable
Definition: kdtypes.h:93
enum _SYSDBG_COMMAND SYSDBG_COMMAND
#define ASSERTMSG(msg, exp)
Definition: nt_native.h:431
#define UNREFERENCED_PARAMETER(P)
Definition: ntbasedef.h:317
_Out_ PKAPC_STATE ApcState
Definition: mm.h:1765
#define DBG_TERMINATE_PROCESS
Definition: ntstatus.h:51
#define STATUS_NOT_IMPLEMENTED
Definition: ntstatus.h:239
#define STATUS_INVALID_INFO_CLASS
Definition: ntstatus.h:240
VOID NTAPI KeStackAttachProcess(IN PKPROCESS Process, OUT PRKAPC_STATE ApcState)
Definition: procobj.c:704
VOID NTAPI KeUnstackDetachProcess(IN PRKAPC_STATE ApcState)
Definition: procobj.c:756
NTSTATUS NTAPI PsTerminateProcess(IN PEPROCESS Process, IN NTSTATUS ExitStatus)
Definition: kill.c:126
PEPROCESS NTAPI PsGetNextProcess(IN PEPROCESS OldProcess OPTIONAL)
Definition: process.c:128
#define _SEH2_GetExceptionCode()
Definition: pseh2_64.h:159
#define _SEH2_EXCEPT(...)
Definition: pseh2_64.h:34
#define ProbeForReadUchar(Ptr)
Definition: probe.h:61
uint32_t * PULONG
Definition: typedefs.h:59
#define NTAPI
Definition: typedefs.h:36
uint32_t ULONG_PTR
Definition: typedefs.h:65
#define IN
Definition: typedefs.h:39
uint32_t ULONG
Definition: typedefs.h:59
__analysis_noreturn NTSYSAPI VOID NTAPI DbgBreakPointWithStatus(_In_ ULONG Status)
_In_ WDFREQUEST _In_ size_t OutputBufferLength
Definition: wdfio.h:320
_In_ WDFREQUEST _In_ size_t _In_ size_t InputBufferLength
Definition: wdfio.h:322
_Must_inspect_result_ _In_ WDFIOTARGET _In_opt_ WDFREQUEST _In_opt_ PWDF_MEMORY_DESCRIPTOR OutputBuffer
Definition: wdfiotarget.h:863
_Must_inspect_result_ _In_ WDFIOTARGET _In_opt_ WDFREQUEST _In_opt_ PWDF_MEMORY_DESCRIPTOR InputBuffer
Definition: wdfiotarget.h:953
_IRQL_requires_same_ typedef _In_ ULONG ControlCode
Definition: wmitypes.h:55
KAPC_STATE
Definition: ketypes.h:1297
#define ObDereferenceObject
Definition: obfuncs.h:203