xref: /reactos/drivers/base/kdgdb/kdgdb.h (revision 0ab44664)
1 /*
2  * COPYRIGHT:       GPL, see COPYING in the top level directory
3  * PROJECT:         ReactOS kernel
4  * FILE:            drivers/base/kddll/kddll.h
5  * PURPOSE:         Base definitions for the kernel debugger.
6  */
7 
8 #ifndef _KDGDB_H_
9 #define _KDGDB_H_
10 
11 #define NOEXTAPI
12 #include <ntifs.h>
13 #include <halfuncs.h>
14 #include <stdio.h>
15 #include <arc/arc.h>
16 #include <inttypes.h>
17 #include <windbgkd.h>
18 #include <kddll.h>
19 
20 #include <pstypes.h>
21 
22 // #define KDDEBUG /* uncomment to enable debugging this dll */
23 
24 /* To undefine once https://sourceware.org/bugzilla/show_bug.cgi?id=17397 is resolved */
25 #define MONOPROCESS 1
26 
27 #ifndef KDDEBUG
28 #define KDDBGPRINT(...)
29 #else
30 extern ULONG KdpDbgPrint(const char* Format, ...);
31 #define KDDBGPRINT KdpDbgPrint
32 #endif
33 
34 /* GDB doesn't like pid - tid 0, so +1 them */
gdb_tid_to_handle(UINT_PTR Tid)35 FORCEINLINE HANDLE gdb_tid_to_handle(UINT_PTR Tid)
36 {
37     return (HANDLE)(Tid - 1);
38 }
39 #define gdb_pid_to_handle gdb_tid_to_handle
40 
handle_to_gdb_tid(HANDLE Handle)41 FORCEINLINE UINT_PTR handle_to_gdb_tid(HANDLE Handle)
42 {
43     return (UINT_PTR)Handle + 1;
44 }
45 #define handle_to_gdb_pid handle_to_gdb_tid
46 
47 FORCEINLINE
48 VOID
InitManipulateFromStateChange(_In_ ULONG ApiNumber,_In_ const DBGKD_ANY_WAIT_STATE_CHANGE * StateChange,_Out_ DBGKD_MANIPULATE_STATE64 * Manipulate)49 InitManipulateFromStateChange(
50     _In_ ULONG ApiNumber,
51     _In_ const DBGKD_ANY_WAIT_STATE_CHANGE* StateChange,
52     _Out_ DBGKD_MANIPULATE_STATE64* Manipulate)
53 {
54     Manipulate->ApiNumber = ApiNumber;
55     Manipulate->Processor = StateChange->Processor;
56     Manipulate->ProcessorLevel = StateChange->ProcessorLevel;
57 }
58 
59 /* Callbacks to simulate a KdReceive <-> KdSend loop without GDB being aware of it */
60 typedef BOOLEAN (*KDP_SEND_HANDLER)(
61     _In_ ULONG PacketType,
62     _In_ PSTRING MessageHeader,
63     _In_ PSTRING MessageData
64 );
65 typedef KDSTATUS (*KDP_MANIPULATESTATE_HANDLER)(
66     _Out_ DBGKD_MANIPULATE_STATE64* State,
67     _Out_ PSTRING MessageData,
68     _Out_ PULONG MessageLength,
69     _Inout_ PKD_CONTEXT KdContext
70 );
71 
72 /* gdb_input.c */
73 extern UINT_PTR gdb_dbg_tid;
74 extern UINT_PTR gdb_dbg_pid;
75 extern KDSTATUS gdb_receive_and_interpret_packet(_Out_ DBGKD_MANIPULATE_STATE64* State, _Out_ PSTRING MessageData, _Out_ PULONG MessageLength, _Inout_ PKD_CONTEXT KdContext);
76 
77 /* gdb_receive.c */
78 extern CHAR gdb_input[];
79 KDSTATUS NTAPI gdb_receive_packet(_Inout_ PKD_CONTEXT KdContext);
80 char hex_value(char ch);
81 
82 /* gdb_send.c */
83 KDSTATUS send_gdb_packet(_In_ const CHAR* Buffer);
84 void start_gdb_packet(void);
85 void send_gdb_partial_packet(_In_ const CHAR* Buffer);
86 KDSTATUS finish_gdb_packet(void);
87 KDSTATUS send_gdb_memory(_In_ const VOID* Buffer, size_t Length);
88 void send_gdb_partial_memory(_In_ const VOID* Buffer, _In_ size_t Length);
89 ULONG send_gdb_partial_binary(_In_ const VOID* Buffer, _In_ size_t Length);
90 KDSTATUS gdb_send_debug_io(_In_ PSTRING String, _In_ BOOLEAN WithPrefix);
91 KDSTATUS gdb_send_exception(void);
92 void send_gdb_ntstatus(_In_ NTSTATUS Status);
93 extern const char hex_chars[];
94 
95 /* kdcom.c */
96 KDSTATUS NTAPI KdpPollBreakIn(VOID);
97 VOID NTAPI KdpSendByte(_In_ UCHAR Byte);
98 KDSTATUS NTAPI KdpReceiveByte(_Out_ PUCHAR OutByte);
99 KDSTATUS NTAPI KdpPollByte(OUT PUCHAR OutByte);
100 
101 /* kdpacket.c */
102 extern DBGKD_ANY_WAIT_STATE_CHANGE CurrentStateChange;
103 extern CONTEXT CurrentContext;
104 extern DBGKD_GET_VERSION64 KdVersion;
105 extern KDDEBUGGER_DATA64* KdDebuggerDataBlock;
106 extern LIST_ENTRY* ProcessListHead;
107 extern LIST_ENTRY* ModuleListHead;
108 extern KDP_SEND_HANDLER KdpSendPacketHandler;
109 extern KDP_MANIPULATESTATE_HANDLER KdpManipulateStateHandler;
110 /* Common ManipulateState handlers */
111 extern KDSTATUS ContinueManipulateStateHandler(_Out_ DBGKD_MANIPULATE_STATE64* State, _Out_ PSTRING MessageData, _Out_ PULONG MessageLength, _Inout_ PKD_CONTEXT KdContext);
112 extern KDSTATUS SetContextManipulateHandler(_Out_ DBGKD_MANIPULATE_STATE64* State, _Out_ PSTRING MessageData, _Out_ PULONG MessageLength, _Inout_ PKD_CONTEXT KdContext);
113 extern PEPROCESS TheIdleProcess;
114 extern PETHREAD TheIdleThread;
115 
116 /* utils.c */
117 extern PEPROCESS find_process( _In_ UINT_PTR Pid);
118 extern PETHREAD find_thread(_In_ UINT_PTR Pid, _In_ UINT_PTR Tid);
119 
120 /* arch_sup.c */
121 extern KDSTATUS gdb_send_register(void);
122 extern KDSTATUS gdb_send_registers(void);
123 
124 /* Architecture specific defines. See ntoskrnl/include/internal/arch/ke.h */
125 #ifdef _M_IX86
126 /* Handling passing over the breakpoint instruction */
127 #  define KdpGetContextPc(Context) \
128     ((Context)->Eip)
129 #  define KdpSetContextPc(Context, ProgramCounter) \
130     ((Context)->Eip = (ProgramCounter))
131 #  define KD_BREAKPOINT_TYPE        UCHAR
132 #  define KD_BREAKPOINT_SIZE        sizeof(UCHAR)
133 #  define KD_BREAKPOINT_VALUE       0xCC
134 /* Single step mode */
135 #  define KdpSetSingleStep(Context) \
136     ((Context)->EFlags |= EFLAGS_TF)
137 #elif defined(_M_AMD64)
138 #  define KdpGetContextPc(Context) \
139     ((Context)->Rip)
140 #  define KdpSetContextPc(Context, ProgramCounter) \
141     ((Context)->Rip = (ProgramCounter))
142 #  define KD_BREAKPOINT_TYPE        UCHAR
143 #  define KD_BREAKPOINT_SIZE        sizeof(UCHAR)
144 #  define KD_BREAKPOINT_VALUE       0xCC
145 /* Single step mode */
146 #  define KdpSetSingleStep(Context) \
147     ((Context)->EFlags |= EFLAGS_TF)
148 #else
149 #  error "Please define relevant macros for your architecture"
150 #endif
151 
152 #endif /* _KDGDB_H_ */
153