ReactOS 0.4.15-dev-8116-gf69e256
kdbg.c
Go to the documentation of this file.
1/*
2 * PROJECT: ReactOS KDBG Kernel Debugger
3 * LICENSE: GPL-2.0-or-later (https://spdx.org/licenses/GPL-2.0-or-later)
4 * PURPOSE: Kernel Debugger Initialization
5 * COPYRIGHT: Copyright 2020-2021 Hervé Poussineau <hpoussin@reactos.org>
6 * Copyright 2021 Jérôme Gardou <jerome.gardou@reactos.org>
7 * Copyright 2023 Hermès Bélusca-Maïto <hermes.belusca-maito@reactos.org>
8 */
9
10/* INCLUDES ******************************************************************/
11
12#include <ntoskrnl.h>
13#include "kdb.h"
14
15/* GLOBALS *******************************************************************/
16
22
23/* FUNCTIONS *****************************************************************/
24
28#undef KdD0Transition
29#define pKdD0Transition KdD0Transition
30{
31 /* Call KdTerm */
32 return pKdD0Transition();
33}
34
38#undef KdD3Transition
39#define pKdD3Transition KdD3Transition
40{
41 /* Call KdTerm */
42 return pKdD3Transition();
43}
44
48 _In_ BOOLEAN SleepTransition)
49#undef KdSave
50#define pKdSave KdSave
51{
52 /* Call KdTerm */
53 return pKdSave(SleepTransition);
54}
55
59 _In_ BOOLEAN SleepTransition)
60#undef KdRestore
61#define pKdRestore KdRestore
62{
63 /* Call KdTerm */
64 return pKdRestore(SleepTransition);
65}
66
67VOID
70 _In_ ULONG PacketType,
71 _In_ PSTRING MessageHeader,
72 _In_opt_ PSTRING MessageData,
74#undef KdSendPacket
75#define pKdSendPacket KdSendPacket
76{
77 if (PacketType == PACKET_TYPE_KD_DEBUG_IO)
78 {
79 /* Call KdTerm */
80 pKdSendPacket(PacketType, MessageHeader, MessageData, Context);
81 return;
82 }
83
84 /* Debugger-only packets */
85 if (PacketType == PACKET_TYPE_KD_STATE_CHANGE64)
86 {
87 PDBGKD_ANY_WAIT_STATE_CHANGE WaitStateChange = (PDBGKD_ANY_WAIT_STATE_CHANGE)MessageHeader->Buffer;
88 if (WaitStateChange->NewState == DbgKdLoadSymbolsStateChange)
89 {
90 /* Load or unload symbols */
91 PLDR_DATA_TABLE_ENTRY LdrEntry;
92 if (KdbpSymFindModule((PVOID)(ULONG_PTR)WaitStateChange->u.LoadSymbols.BaseOfDll, -1, &LdrEntry))
93 {
94 KdbSymProcessSymbols(LdrEntry, !WaitStateChange->u.LoadSymbols.UnloadSymbols);
95 }
96 return;
97 }
98 else if (WaitStateChange->NewState == DbgKdExceptionStateChange)
99 {
103 return;
104 }
105 }
106 else if (PacketType == PACKET_TYPE_KD_STATE_MANIPULATE)
107 {
108 PDBGKD_MANIPULATE_STATE64 ManipulateState = (PDBGKD_MANIPULATE_STATE64)MessageHeader->Buffer;
109 if (ManipulateState->ApiNumber == DbgKdGetContextApi)
110 {
112
113 /* Check if this is an assertion failure */
115 {
116 /* Bump EIP to the instruction following the int 2C */
118 }
119
121 KdbgContext.SegCs & 1,
124#if 0
125 /* Manually dump the stack for the user */
128#endif
131 else
134 return;
135 }
136 else if (ManipulateState->ApiNumber == DbgKdSetContextApi)
137 {
139 return;
140 }
141 }
142
143 KdbPrintf("%s: PacketType %d is UNIMPLEMENTED\n", __FUNCTION__, PacketType);
144 return;
145}
146
148NTAPI
150 _In_ ULONG PacketType,
151 _Out_ PSTRING MessageHeader,
152 _Out_ PSTRING MessageData,
155#undef KdReceivePacket
156#define pKdReceivePacket KdReceivePacket
157{
158 if (PacketType == PACKET_TYPE_KD_POLL_BREAKIN)
159 {
160 // FIXME TODO: Implement break-in for the debugger
161 // and return KdPacketReceived when handled properly.
162 return KdPacketTimedOut;
163 }
164
165 if (PacketType == PACKET_TYPE_KD_DEBUG_IO)
166 {
167 /* Call KdTerm */
168 return pKdReceivePacket(PacketType,
169 MessageHeader,
170 MessageData,
172 Context);
173 }
174
175 /* Debugger-only packets */
176 if (PacketType == PACKET_TYPE_KD_STATE_MANIPULATE)
177 {
178 PDBGKD_MANIPULATE_STATE64 ManipulateState = (PDBGKD_MANIPULATE_STATE64)MessageHeader->Buffer;
179 RtlZeroMemory(MessageHeader->Buffer, MessageHeader->MaximumLength);
181 {
182 ManipulateState->ApiNumber = DbgKdGetContextApi;
183 MessageData->Length = 0;
184 MessageData->Buffer = (PCHAR)&KdbgContext;
185 return KdPacketReceived;
186 }
188 {
189 ManipulateState->ApiNumber = DbgKdSetContextApi;
190 MessageData->Length = sizeof(KdbgContext);
191 MessageData->Buffer = (PCHAR)&KdbgContext;
192 return KdPacketReceived;
193 }
195 {
196 KdbPrintf("%s:%d is UNIMPLEMENTED\n", __FUNCTION__, __LINE__);
197 }
198 ManipulateState->ApiNumber = DbgKdContinueApi;
199 ManipulateState->u.Continue.ContinueStatus = KdbgContinueStatus;
200
201 /* Prepare for next time */
204
205 return KdPacketReceived;
206 }
207
208 KdbPrintf("%s: PacketType %d is UNIMPLEMENTED\n", __FUNCTION__, PacketType);
209 return KdPacketTimedOut;
210}
211
212/* EOF */
unsigned char BOOLEAN
LONG NTSTATUS
Definition: precomp.h:26
_In_ ULONG _In_opt_ WDFREQUEST _In_opt_ PVOID _In_ size_t _In_ PVOID _In_ size_t _Out_ size_t * DataLength
Definition: cdrom.h:1444
#define NULL
Definition: types.h:112
#define __FUNCTION__
Definition: types.h:116
#define KeRosDumpStackFrames(Frames, Count)
Definition: gdidebug.h:11
KD_CONTINUE_TYPE KdbEnterDebuggerException(IN PEXCEPTION_RECORD64 ExceptionRecord, IN KPROCESSOR_MODE PreviousMode, IN PCONTEXT Context, IN BOOLEAN FirstChance)
KDB Exception filter.
Definition: kdb.c:1262
enum _KD_CONTINUE_TYPE KD_CONTINUE_TYPE
@ kdHandleException
Definition: kdb.h:58
VOID KdbSymProcessSymbols(_Inout_ PLDR_DATA_TABLE_ENTRY LdrEntry, _In_ BOOLEAN Load)
Load symbols from image mapping. If this fails,.
Definition: kdb_symbols.c:297
BOOLEAN KdbpSymFindModule(IN PVOID Address OPTIONAL, IN INT Index OPTIONAL, OUT PLDR_DATA_TABLE_ENTRY *pLdrEntry)
Find a module...
Definition: kdb_symbols.c:76
VOID __cdecl KdbPrintf(_In_ PCSTR Format,...)
Definition: kdb_print.c:160
if(dx< 0)
Definition: linetemp.h:194
#define PCHAR
Definition: match.c:90
#define _Inout_
Definition: ms_sal.h:378
#define _Out_
Definition: ms_sal.h:345
#define _In_
Definition: ms_sal.h:308
#define _In_opt_
Definition: ms_sal.h:309
#define KeSetContextPc(Context, ProgramCounter)
Definition: ke.h:34
#define KeGetContextPc(Context)
Definition: ke.h:31
#define pKdD0Transition
NTSTATUS NTAPI KdD0Transition(VOID)
Definition: kdbg.c:27
NTSTATUS NTAPI KdD3Transition(VOID)
Definition: kdbg.c:37
static ULONG KdbgNextApiNumber
Definition: kdbg.c:17
#define pKdReceivePacket
static NTSTATUS KdbgContinueStatus
Definition: kdbg.c:21
VOID NTAPI KdSendPacket(_In_ ULONG PacketType, _In_ PSTRING MessageHeader, _In_opt_ PSTRING MessageData, _Inout_ PKD_CONTEXT Context)
Definition: kdbg.c:69
NTSTATUS NTAPI KdRestore(_In_ BOOLEAN SleepTransition)
Definition: kdbg.c:58
KDSTATUS NTAPI KdReceivePacket(_In_ ULONG PacketType, _Out_ PSTRING MessageHeader, _Out_ PSTRING MessageData, _Out_ PULONG DataLength, _Inout_ PKD_CONTEXT Context)
Definition: kdbg.c:149
static BOOLEAN KdbgFirstChanceException
Definition: kdbg.c:20
static EXCEPTION_RECORD64 KdbgExceptionRecord
Definition: kdbg.c:19
NTSTATUS NTAPI KdSave(_In_ BOOLEAN SleepTransition)
Definition: kdbg.c:47
#define pKdD3Transition
static CONTEXT KdbgContext
Definition: kdbg.c:18
#define pKdSave
#define pKdRestore
#define pKdSendPacket
#define STATUS_ASSERTION_FAILURE
Definition: ntstatus.h:960
#define KdPacketReceived
Definition: kddll.h:5
ULONG KDSTATUS
Definition: kddll.h:4
#define KdPacketTimedOut
Definition: kddll.h:6
#define STATUS_SUCCESS
Definition: shellext.h:65
ULONG SegCs
Definition: nt_native.h:1477
union _DBGKD_ANY_WAIT_STATE_CHANGE::@3554 u
DBGKM_EXCEPTION64 Exception
Definition: windbgkd.h:508
DBGKD_LOAD_SYMBOLS64 LoadSymbols
Definition: windbgkd.h:509
NTSTATUS ContinueStatus
Definition: windbgkd.h:579
union _DBGKD_MANIPULATE_STATE64::@3562 u
DBGKD_CONTINUE Continue
Definition: windbgkd.h:799
EXCEPTION_RECORD64 ExceptionRecord
Definition: windbgkd.h:312
NTSTATUS ExceptionCode
Definition: rtltypes.h:190
Definition: btrfs_drv.h:1876
uint32_t * PULONG
Definition: typedefs.h:59
#define NTAPI
Definition: typedefs.h:36
#define RtlZeroMemory(Destination, Length)
Definition: typedefs.h:262
uint32_t ULONG_PTR
Definition: typedefs.h:65
uint32_t ULONG
Definition: typedefs.h:59
#define STATUS_UNSUCCESSFUL
Definition: udferr_usr.h:132
#define DbgKdGetContextApi
Definition: windbgkd.h:76
#define DbgKdLoadSymbolsStateChange
Definition: windbgkd.h:60
#define DbgKdSetContextApi
Definition: windbgkd.h:77
#define PACKET_TYPE_KD_STATE_MANIPULATE
Definition: windbgkd.h:43
struct _DBGKD_ANY_WAIT_STATE_CHANGE * PDBGKD_ANY_WAIT_STATE_CHANGE
struct _DBGKD_MANIPULATE_STATE64 * PDBGKD_MANIPULATE_STATE64
#define DbgKdExceptionStateChange
Definition: windbgkd.h:59
#define PACKET_TYPE_KD_STATE_CHANGE64
Definition: windbgkd.h:48
#define DbgKdContinueApi
Definition: windbgkd.h:80
#define PACKET_TYPE_KD_POLL_BREAKIN
Definition: windbgkd.h:49
#define PACKET_TYPE_KD_DEBUG_IO
Definition: windbgkd.h:44
_At_(*)(_In_ PWSK_CLIENT Client, _In_opt_ PUNICODE_STRING NodeName, _In_opt_ PUNICODE_STRING ServiceName, _In_opt_ ULONG NameSpace, _In_opt_ GUID *Provider, _In_opt_ PADDRINFOEXW Hints, _Outptr_ PADDRINFOEXW *Result, _In_opt_ PEPROCESS OwningProcess, _In_opt_ PETHREAD OwningThread, _Inout_ PIRP Irp Result)(Mem)) NTSTATUS(WSKAPI *PFN_WSK_GET_ADDRESS_INFO
Definition: wsk.h:409