ReactOS 0.4.16-dev-295-g4aee028
kdpacket.c File Reference
#include "kdgdb.h"
Include dependency graph for kdpacket.c:

Go to the source code of this file.

Functions

static BOOLEAN FirstSendHandler (_In_ ULONG PacketType, _In_ PSTRING MessageHeader, _In_ PSTRING MessageData)
 
static BOOLEAN GetContextSendHandler (_In_ ULONG PacketType, _In_ PSTRING MessageHeader, _In_ PSTRING MessageData)
 
static KDSTATUS GetContextManipulateHandler (_Out_ DBGKD_MANIPULATE_STATE64 *State, _Out_ PSTRING MessageData, _Out_ PULONG MessageLength, _Inout_ PKD_CONTEXT KdContext)
 
static BOOLEAN SetContextSendHandler (_In_ ULONG PacketType, _In_ PSTRING MessageHeader, _In_ PSTRING MessageData)
 
KDSTATUS SetContextManipulateHandler (_Out_ DBGKD_MANIPULATE_STATE64 *State, _Out_ PSTRING MessageData, _Out_ PULONG MessageLength, _Inout_ PKD_CONTEXT KdContext)
 
static void send_kd_state_change (DBGKD_ANY_WAIT_STATE_CHANGE *StateChange)
 
static void send_kd_debug_io (_In_ DBGKD_DEBUG_IO *DebugIO, _In_ PSTRING String)
 
static void send_kd_state_manipulate (_In_ DBGKD_MANIPULATE_STATE64 *State, _In_ PSTRING MessageData)
 
KDSTATUS ContinueManipulateStateHandler (_Out_ DBGKD_MANIPULATE_STATE64 *State, _Out_ PSTRING MessageData, _Out_ PULONG MessageLength, _Inout_ PKD_CONTEXT KdContext)
 
static BOOLEAN GetVersionSendHandler (_In_ ULONG PacketType, _In_ PSTRING MessageHeader, _In_ PSTRING MessageData)
 
static KDSTATUS GetVersionManipulateStateHandler (_Out_ DBGKD_MANIPULATE_STATE64 *State, _Out_ PSTRING MessageData, _Out_ PULONG MessageLength, _Inout_ PKD_CONTEXT KdContext)
 
KDSTATUS NTAPI KdReceivePacket (_In_ ULONG PacketType, _Out_ PSTRING MessageHeader, _Out_ PSTRING MessageData, _Out_ PULONG DataLength, _Inout_ PKD_CONTEXT KdContext)
 
VOID NTAPI KdSendPacket (IN ULONG PacketType, IN PSTRING MessageHeader, IN PSTRING MessageData, IN OUT PKD_CONTEXT KdContext)
 

Variables

static BOOLEAN InException = FALSE
 
DBGKD_GET_VERSION64 KdVersion
 
KDDEBUGGER_DATA64KdDebuggerDataBlock
 
LIST_ENTRYProcessListHead
 
LIST_ENTRYModuleListHead
 
KDP_SEND_HANDLER KdpSendPacketHandler = FirstSendHandler
 
KDP_MANIPULATESTATE_HANDLER KdpManipulateStateHandler = NULL
 
DBGKD_ANY_WAIT_STATE_CHANGE CurrentStateChange
 
CONTEXT CurrentContext
 
PEPROCESS TheIdleProcess
 
PETHREAD TheIdleThread
 

Function Documentation

◆ ContinueManipulateStateHandler()

KDSTATUS ContinueManipulateStateHandler ( _Out_ DBGKD_MANIPULATE_STATE64 State,
_Out_ PSTRING  MessageData,
_Out_ PULONG  MessageLength,
_Inout_ PKD_CONTEXT  KdContext 
)

Definition at line 213 of file kdpacket.c.

219{
220 /* Let's go on */
221 State->ApiNumber = DbgKdContinueApi;
222 State->ReturnStatus = STATUS_SUCCESS; /* ? */
224 State->ProcessorLevel = CurrentStateChange.ProcessorLevel;
225 if (MessageData)
226 MessageData->Length = 0;
227 *MessageLength = 0;
228 State->u.Continue.ContinueStatus = STATUS_SUCCESS;
229
230 /* We definitely are at the end of the send <-> receive loop, if any */
233 /* We're not handling an exception anymore */
235
236 return KdPacketReceived;
237}
#define NULL
Definition: types.h:112
#define FALSE
Definition: types.h:117
KDP_SEND_HANDLER KdpSendPacketHandler
Definition: kdpacket.c:25
KDP_MANIPULATESTATE_HANDLER KdpManipulateStateHandler
Definition: kdpacket.c:26
DBGKD_ANY_WAIT_STATE_CHANGE CurrentStateChange
Definition: kdpacket.c:28
static BOOLEAN InException
Definition: kdpacket.c:17
#define KdPacketReceived
Definition: kddll.h:5
#define STATUS_SUCCESS
Definition: shellext.h:65
#define DbgKdContinueApi
Definition: windbgkd.h:80

Referenced by handle_gdb_c(), and handle_gdb_s().

◆ FirstSendHandler()

static BOOLEAN FirstSendHandler ( _In_ ULONG  PacketType,
_In_ PSTRING  MessageHeader,
_In_ PSTRING  MessageData 
)
static

Definition at line 293 of file kdpacket.c.

297{
298 DBGKD_ANY_WAIT_STATE_CHANGE* StateChange = (DBGKD_ANY_WAIT_STATE_CHANGE*)MessageHeader->Buffer;
300
301 if (PacketType != PACKET_TYPE_KD_STATE_CHANGE64)
302 {
303 KDDBGPRINT("First KD packet is not a state change!\n");
304 return FALSE;
305 }
306
307 KDDBGPRINT("KDGDB: START!\n");
308
309 Thread = (PETHREAD)(ULONG_PTR)StateChange->Thread;
310
311 /* Set up the current state */
312 CurrentStateChange = *StateChange;
314#if MONOPROCESS
315 gdb_dbg_pid = 0;
316#else
318#endif
319 /* This is the idle process. Save it! */
322
323 KDDBGPRINT("Pid Tid of the first message: %" PRIxPTR", %" PRIxPTR ".\n", gdb_dbg_pid, gdb_dbg_tid);
324
325 /* The next receive call will be asking for the version data */
328 return TRUE;
329}
#define TRUE
Definition: types.h:120
#define KDDBGPRINT(...)
Definition: kddll.h:19
_In_opt_ PFILE_OBJECT _In_opt_ PETHREAD Thread
Definition: fltkernel.h:2653
UINT_PTR gdb_dbg_tid
Definition: gdb_input.c:21
UINT_PTR gdb_dbg_pid
Definition: gdb_input.c:20
#define PRIxPTR
Definition: inttypes.h:236
#define handle_to_gdb_pid
Definition: kdgdb.h:45
FORCEINLINE UINT_PTR handle_to_gdb_tid(HANDLE Handle)
Definition: kdgdb.h:41
PEPROCESS TheIdleProcess
Definition: kdpacket.c:30
PETHREAD TheIdleThread
Definition: kdpacket.c:31
static KDSTATUS GetVersionManipulateStateHandler(_Out_ DBGKD_MANIPULATE_STATE64 *State, _Out_ PSTRING MessageData, _Out_ PULONG MessageLength, _Inout_ PKD_CONTEXT KdContext)
Definition: kdpacket.c:273
if(dx< 0)
Definition: linetemp.h:194
struct _EPROCESS * PEPROCESS
Definition: nt_native.h:30
struct _ETHREAD * PETHREAD
Definition: nt_native.h:29
HANDLE NTAPI PsGetThreadId(IN PETHREAD Thread)
Definition: thread.c:705
HANDLE NTAPI PsGetThreadProcessId(IN PETHREAD Thread)
Definition: thread.c:745
KTHREAD Tcb
Definition: pstypes.h:1104
KAPC_STATE ApcState
Definition: ketypes.h:1778
uint32_t ULONG_PTR
Definition: typedefs.h:65
#define PACKET_TYPE_KD_STATE_CHANGE64
Definition: windbgkd.h:48

◆ GetContextManipulateHandler()

static KDSTATUS GetContextManipulateHandler ( _Out_ DBGKD_MANIPULATE_STATE64 State,
_Out_ PSTRING  MessageData,
_Out_ PULONG  MessageLength,
_Inout_ PKD_CONTEXT  KdContext 
)
static

Definition at line 62 of file kdpacket.c.

68{
69 State->ApiNumber = DbgKdGetContextApi;
71 State->ReturnStatus = STATUS_SUCCESS;
72 State->ProcessorLevel = CurrentStateChange.ProcessorLevel;
73 MessageData->Length = 0;
74
75 /* Update the send <-> receive loop handler */
78
79 return KdPacketReceived;
80}
static BOOLEAN GetContextSendHandler(_In_ ULONG PacketType, _In_ PSTRING MessageHeader, _In_ PSTRING MessageData)
Definition: kdpacket.c:37
#define DbgKdGetContextApi
Definition: windbgkd.h:76

Referenced by GetVersionSendHandler(), and send_kd_state_change().

◆ GetContextSendHandler()

static BOOLEAN GetContextSendHandler ( _In_ ULONG  PacketType,
_In_ PSTRING  MessageHeader,
_In_ PSTRING  MessageData 
)
static

Definition at line 37 of file kdpacket.c.

42{
44 const CONTEXT* Context = (const CONTEXT*)MessageData->Buffer;
45
46 if ((PacketType != PACKET_TYPE_KD_STATE_MANIPULATE)
47 || (State->ApiNumber != DbgKdGetContextApi)
48 || (MessageData->Length < sizeof(*Context)))
49 {
50 KDDBGPRINT("ERROR: Received wrong packet from KD.\n");
51 return FALSE;
52 }
53
54 /* Just copy it */
57 return TRUE;
58}
CONTEXT CurrentContext
Definition: kdpacket.c:29
#define RtlCopyMemory(Destination, Source, Length)
Definition: typedefs.h:263
#define PACKET_TYPE_KD_STATE_MANIPULATE
Definition: windbgkd.h:43

Referenced by GetContextManipulateHandler().

◆ GetVersionManipulateStateHandler()

static KDSTATUS GetVersionManipulateStateHandler ( _Out_ DBGKD_MANIPULATE_STATE64 State,
_Out_ PSTRING  MessageData,
_Out_ PULONG  MessageLength,
_Inout_ PKD_CONTEXT  KdContext 
)
static

Definition at line 273 of file kdpacket.c.

278{
279 /* Ask for the version data */
280 State->ApiNumber = DbgKdGetVersionApi;
282 State->ProcessorLevel = CurrentStateChange.ProcessorLevel;
283
284 /* The next send call will serve this query */
287
288 return KdPacketReceived;
289}
static BOOLEAN GetVersionSendHandler(_In_ ULONG PacketType, _In_ PSTRING MessageHeader, _In_ PSTRING MessageData)
Definition: kdpacket.c:241
#define DbgKdGetVersionApi
Definition: windbgkd.h:96

Referenced by FirstSendHandler().

◆ GetVersionSendHandler()

static BOOLEAN GetVersionSendHandler ( _In_ ULONG  PacketType,
_In_ PSTRING  MessageHeader,
_In_ PSTRING  MessageData 
)
static

Definition at line 241 of file kdpacket.c.

245{
247 PLIST_ENTRY DebuggerDataList;
248
249 /* Confirm that all went well */
250 if ((PacketType != PACKET_TYPE_KD_STATE_MANIPULATE)
251 || (State->ApiNumber != DbgKdGetVersionApi)
252 || !NT_SUCCESS(State->ReturnStatus))
253 {
254 KDDBGPRINT("Wrong packet received after asking for data.\n");
255 return FALSE;
256 }
257
258 /* Copy the relevant data */
259 RtlCopyMemory(&KdVersion, &State->u.GetVersion64, sizeof(KdVersion));
260 DebuggerDataList = *(PLIST_ENTRY*)&KdVersion.DebuggerDataList;
261 KdDebuggerDataBlock = CONTAINING_RECORD(DebuggerDataList->Flink, KDDEBUGGER_DATA64, Header.List);
264
265 /* Now we can get the context for the current state */
268 return TRUE;
269}
Definition: Header.h:9
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:33
DBGKD_GET_VERSION64 KdVersion
Definition: kdpacket.c:20
KDDEBUGGER_DATA64 * KdDebuggerDataBlock
Definition: kdpacket.c:21
static KDSTATUS GetContextManipulateHandler(_Out_ DBGKD_MANIPULATE_STATE64 *State, _Out_ PSTRING MessageData, _Out_ PULONG MessageLength, _Inout_ PKD_CONTEXT KdContext)
Definition: kdpacket.c:62
LIST_ENTRY * ModuleListHead
Definition: kdpacket.c:23
LIST_ENTRY * ProcessListHead
Definition: kdpacket.c:22
ULONG64 DebuggerDataList
Definition: wdbgexts.h:167
ULPTR64 PsActiveProcessHead
Definition: wdbgexts.h:203
ULPTR64 PsLoadedModuleList
Definition: wdbgexts.h:202
Definition: typedefs.h:120
#define CONTAINING_RECORD(address, type, field)
Definition: typedefs.h:260

Referenced by GetVersionManipulateStateHandler().

◆ KdReceivePacket()

KDSTATUS NTAPI KdReceivePacket ( _In_ ULONG  PacketType,
_Out_ PSTRING  MessageHeader,
_Out_ PSTRING  MessageData,
_Out_ PULONG  DataLength,
_Inout_ PKD_CONTEXT  KdContext 
)

Definition at line 350 of file kdpacket.c.

356{
357 KDDBGPRINT("KdReceivePacket --> ");
358
359 if (PacketType == PACKET_TYPE_KD_POLL_BREAKIN)
360 {
361 static BOOLEAN firstTime = TRUE;
362 KDDBGPRINT("Polling break in.\n");
363 if (firstTime)
364 {
365 /* Force debug break on init */
366 firstTime = FALSE;
367 return KdPacketReceived;
368 }
369
370 return KdpPollBreakIn();
371 }
372
373 if (PacketType == PACKET_TYPE_KD_DEBUG_IO)
374 {
375 static BOOLEAN ignore = 0;
376 KDDBGPRINT("Debug prompt.\n");
377 /* HACK ! Debug prompt asks for break or ignore. First break, then ignore. */
378 MessageData->Length = 1;
379 MessageData->Buffer[0] = ignore ? 'i' : 'b';
380 ignore = !ignore;
381 return KdPacketReceived;
382 }
383
384 if (PacketType == PACKET_TYPE_KD_STATE_MANIPULATE)
385 {
387
388 KDDBGPRINT("State manipulation: ");
389
390 /* Maybe we are in a send<->receive loop that GDB doesn't need to know about */
392 {
393 KDDBGPRINT("We have a manipulate state handler.\n");
394 return KdpManipulateStateHandler(State, MessageData, DataLength, KdContext);
395 }
396
397 /* Receive data from GDB and interpret it */
398 KDDBGPRINT("Receiving data from GDB.\n");
399 return gdb_receive_and_interpret_packet(State, MessageData, DataLength, KdContext);
400 }
401
402 /* What should we do ? */
403 while (1);
404 return KdPacketNeedsResend;
405}
unsigned char BOOLEAN
_In_ ULONG _In_opt_ WDFREQUEST _In_opt_ PVOID _In_ size_t _In_ PVOID _In_ size_t _Out_ size_t * DataLength
Definition: cdrom.h:1444
KDSTATUS gdb_receive_and_interpret_packet(_Out_ DBGKD_MANIPULATE_STATE64 *State, _Out_ PSTRING MessageData, _Out_ PULONG MessageLength, _Inout_ PKD_CONTEXT KdContext)
Definition: gdb_input.c:994
KDP_STATUS NTAPI KdpPollBreakIn(VOID)
Definition: kdcom.c:343
#define KdPacketNeedsResend
Definition: kddll.h:7
#define PACKET_TYPE_KD_POLL_BREAKIN
Definition: windbgkd.h:49
#define PACKET_TYPE_KD_DEBUG_IO
Definition: windbgkd.h:44

◆ KdSendPacket()

VOID NTAPI KdSendPacket ( IN ULONG  PacketType,
IN PSTRING  MessageHeader,
IN PSTRING  MessageData,
IN OUT PKD_CONTEXT  KdContext 
)

Definition at line 409 of file kdpacket.c.

414{
415 /* Override if we have some debug print from KD. */
416 if (PacketType == PACKET_TYPE_KD_DEBUG_IO)
417 {
418 send_kd_debug_io((DBGKD_DEBUG_IO*)MessageHeader->Buffer, MessageData);
419 return;
420 }
421
422 /* Maybe we are in a send <-> receive loop that GDB doesn't need to know about */
424 && KdpSendPacketHandler(PacketType, MessageHeader, MessageData))
425 {
426 return;
427 }
428
429 switch (PacketType)
430 {
432 send_kd_state_change((DBGKD_ANY_WAIT_STATE_CHANGE*)MessageHeader->Buffer);
433 return;
435 send_kd_debug_io((DBGKD_DEBUG_IO*)MessageHeader->Buffer, MessageData);
436 break;
438 send_kd_state_manipulate((DBGKD_MANIPULATE_STATE64*)MessageHeader->Buffer, MessageData);
439 break;
440 default:
441 KDDBGPRINT("Unknown packet type %u.\n", PacketType);
442 while (1);
443 }
444}
static void send_kd_debug_io(_In_ DBGKD_DEBUG_IO *DebugIO, _In_ PSTRING String)
Definition: kdpacket.c:173
static void send_kd_state_manipulate(_In_ DBGKD_MANIPULATE_STATE64 *State, _In_ PSTRING MessageData)
Definition: kdpacket.c:194
static void send_kd_state_change(DBGKD_ANY_WAIT_STATE_CHANGE *StateChange)
Definition: kdpacket.c:137

◆ send_kd_debug_io()

static void send_kd_debug_io ( _In_ DBGKD_DEBUG_IO DebugIO,
_In_ PSTRING  String 
)
static

Definition at line 173 of file kdpacket.c.

176{
177 if (InException)
178 return;
179
180 switch (DebugIO->ApiNumber)
181 {
185 break;
186 default:
187 KDDBGPRINT("Unknown ApiNumber %u.\n", DebugIO->ApiNumber);
188 while (1);
189 }
190}
KDSTATUS gdb_send_debug_io(_In_ PSTRING String, _In_ BOOLEAN WithPrefix)
Definition: gdb_send.c:168
_Must_inspect_result_ _In_ WDFDEVICE _In_ WDFSTRING String
Definition: wdfdevice.h:2433
#define DbgKdPrintStringApi
Definition: windbgkd.h:122
#define DbgKdGetStringApi
Definition: windbgkd.h:123

Referenced by KdSendPacket().

◆ send_kd_state_change()

static void send_kd_state_change ( DBGKD_ANY_WAIT_STATE_CHANGE StateChange)
static

Definition at line 137 of file kdpacket.c.

138{
140
141 switch (StateChange->NewState)
142 {
145 {
146 PETHREAD Thread = (PETHREAD)(ULONG_PTR)StateChange->Thread;
147 /* Save current state for later GDB queries */
148 CurrentStateChange = *StateChange;
149 KDDBGPRINT("Exception 0x%08x in thread p%p.%p.\n",
153 /* Set the current debugged process/thread accordingly */
155#if MONOPROCESS
156 gdb_dbg_pid = 0;
157#else
159#endif
161 /* Next receive call will ask for the context */
163 break;
164 }
165 default:
166 KDDBGPRINT("Unknown StateChange %u.\n", StateChange->NewState);
167 while (1);
168 }
169}
KDSTATUS gdb_send_exception()
Definition: gdb_send.c:197
union _DBGKD_ANY_WAIT_STATE_CHANGE::@3548 u
DBGKM_EXCEPTION64 Exception
Definition: windbgkd.h:508
EXCEPTION_RECORD64 ExceptionRecord
Definition: windbgkd.h:312
NTSTATUS ExceptionCode
Definition: rtltypes.h:190
#define DbgKdLoadSymbolsStateChange
Definition: windbgkd.h:60
#define DbgKdExceptionStateChange
Definition: windbgkd.h:59

Referenced by KdSendPacket().

◆ send_kd_state_manipulate()

static void send_kd_state_manipulate ( _In_ DBGKD_MANIPULATE_STATE64 State,
_In_ PSTRING  MessageData 
)
static

Definition at line 194 of file kdpacket.c.

197{
198 switch (State->ApiNumber)
199 {
200#if 0
202 /* This is an answer to a 'g' GDB request */
203 gdb_send_registers((CONTEXT*)MessageData->Buffer);
204 return;
205#endif
206 default:
207 KDDBGPRINT("Unknown ApiNumber %u.\n", State->ApiNumber);
208 while (1);
209 }
210}
KDSTATUS gdb_send_registers(void)
Definition: amd64_sup.c:144

Referenced by KdSendPacket().

◆ SetContextManipulateHandler()

KDSTATUS SetContextManipulateHandler ( _Out_ DBGKD_MANIPULATE_STATE64 State,
_Out_ PSTRING  MessageData,
_Out_ PULONG  MessageLength,
_Inout_ PKD_CONTEXT  KdContext 
)

Definition at line 107 of file kdpacket.c.

113{
114 State->ApiNumber = DbgKdSetContextApi;
116 State->ReturnStatus = STATUS_SUCCESS;
117 State->ProcessorLevel = CurrentStateChange.ProcessorLevel;
118 MessageData->Length = sizeof(CurrentContext);
119
120 if (MessageData->MaximumLength < sizeof(CurrentContext))
121 {
122 KDDBGPRINT("Wrong message length %u.\n", MessageData->MaximumLength);
123 while (1);
124 }
125
126 RtlCopyMemory(MessageData->Buffer, &CurrentContext, sizeof(CurrentContext));
127
128 /* Update the send <-> receive loop handlers */
131
132 return KdPacketReceived;
133}
static BOOLEAN SetContextSendHandler(_In_ ULONG PacketType, _In_ PSTRING MessageHeader, _In_ PSTRING MessageData)
Definition: kdpacket.c:84
#define DbgKdSetContextApi
Definition: windbgkd.h:77

Referenced by handle_gdb_c(), and handle_gdb_s().

◆ SetContextSendHandler()

static BOOLEAN SetContextSendHandler ( _In_ ULONG  PacketType,
_In_ PSTRING  MessageHeader,
_In_ PSTRING  MessageData 
)
static

Definition at line 84 of file kdpacket.c.

89{
91
92 /* We just confirm that all went well */
93 if ((PacketType != PACKET_TYPE_KD_STATE_MANIPULATE)
94 || (State->ApiNumber != DbgKdSetContextApi)
95 || (State->ReturnStatus != STATUS_SUCCESS))
96 {
97 /* Should we bugcheck ? */
98 KDDBGPRINT("BAD BAD BAD not manipulating state for sending context.\n");
99 return FALSE;
100 }
101
103 return TRUE;
104}

Referenced by SetContextManipulateHandler().

Variable Documentation

◆ CurrentContext

◆ CurrentStateChange

◆ InException

BOOLEAN InException = FALSE
static

◆ KdDebuggerDataBlock

KDDEBUGGER_DATA64* KdDebuggerDataBlock

Definition at line 21 of file kdpacket.c.

Referenced by GetVersionSendHandler().

◆ KdpManipulateStateHandler

◆ KdpSendPacketHandler

◆ KdVersion

Definition at line 20 of file kdpacket.c.

Referenced by GetVersionSendHandler().

◆ ModuleListHead

◆ ProcessListHead

◆ TheIdleProcess

PEPROCESS TheIdleProcess

Definition at line 30 of file kdpacket.c.

Referenced by find_process(), find_thread(), FirstSendHandler(), and QSI_DEF().

◆ TheIdleThread

PETHREAD TheIdleThread

Definition at line 31 of file kdpacket.c.

Referenced by find_thread(), and FirstSendHandler().