ReactOS 0.4.16-dev-2491-g3dc6630
amd64_sup.c
Go to the documentation of this file.
1/*
2 * PROJECT: ReactOS KD dll - GDB stub
3 * LICENSE: GPL-2.0-or-later (https://spdx.org/licenses/GPL-2.0-or-later)
4 * PURPOSE: Base functions for the kernel debugger
5 * COPYRIGHT: Copyright 2021 Jérôme Gardou
6 */
7
8#include "kdgdb.h"
9
11{
16 CS, SS, DS, ES, FS, GS,
19};
20
21static const unsigned char reg_size[] =
22{
23 8, 8, 8, 8, 8, 8, 8, 8,
24 8, 8, 8, 8, 8, 8, 8, 8,
25 8,
26 4,
27 4, 4, 4, 4, 4, 4,
28 10, 10, 10, 10, 10, 10, 10, 10,
29 8, 8, 8, 8, 8, 8, 8, 8
30};
31
32static
33const void*
35{
36 switch (name)
37 {
38 case RAX: return &ctx->Rax;
39 case RBX: return &ctx->Rbx;
40 case RCX: return &ctx->Rcx;
41 case RDX: return &ctx->Rdx;
42 case RSP: return &ctx->Rsp;
43 case RBP: return &ctx->Rbp;
44 case RSI: return &ctx->Rsi;
45 case RDI: return &ctx->Rdi;
46 case RIP: return &ctx->Rip;
47 case R8: return &ctx->R8;
48 case R9: return &ctx->R9;
49 case R10: return &ctx->R10;
50 case R11: return &ctx->R11;
51 case R12: return &ctx->R12;
52 case R13: return &ctx->R13;
53 case R14: return &ctx->R14;
54 case R15: return &ctx->R15;
55 case EFLAGS: return &ctx->EFlags;
56 case CS: return &ctx->SegCs;
57 case DS: return &ctx->SegSs;
58 case ES: return &ctx->SegEs;
59 case FS: return &ctx->SegFs;
60 case GS: return &ctx->SegGs;
61 case SS: return &ctx->SegSs;
62 }
63 return NULL;
64}
65
66static
67const void*
69{
70 static const void* NullValue = NULL;
71
72#if 0
73 if (Thread->Tcb.TrapFrame)
74 {
75 PKTRAP_FRAME TrapFrame = Thread->Tcb.TrapFrame;
76
77 switch (reg_name)
78 {
79 case RAX: return &TrapFrame->Rax;
80 case RBX: return &TrapFrame->Rbx;
81 case RCX: return &TrapFrame->Rcx;
82 case RDX: return &TrapFrame->Rdx;
83 case RSP: return &TrapFrame->Rsp;
84 case RBP: return &TrapFrame->Rbp;
85 case RSI: return &TrapFrame->Rsi;
86 case RDI: return &TrapFrame->Rdi;
87 case RIP: return &TrapFrame->Rip;
88 case R8: return &TrapFrame->R8;
89 case R9: return &TrapFrame->R9;
90 case R10: return &TrapFrame->R10;
91 case R11: return &TrapFrame->R11;
92 case EFLAGS: return &TrapFrame->EFlags;
93 case CS: return &TrapFrame->SegCs;
94 case DS: return &TrapFrame->SegSs;
95 case ES: return &TrapFrame->SegEs;
96 case FS: return &TrapFrame->SegFs;
97 case GS: return &TrapFrame->SegGs;
98 case SS: return &TrapFrame->SegSs;
99 default:
100 KDDBGPRINT("Unhandled regname: %d.\n", reg_name);
101 }
102 }
103 else
104#endif
105 if (!Thread->Tcb.InitialStack)
106 {
107 /* Terminated thread? */
108 switch (reg_name)
109 {
110 case RSP:
111 case RBP:
112 case RIP:
113 KDDBGPRINT("Returning NULL for register %d.\n", reg_name);
114 return &NullValue;
115 default:
116 return NULL;
117 }
118 }
119 else
120 {
121 switch (reg_name)
122 {
123 case RSP: return &Thread->Tcb.KernelStack;
124 case RIP:
125 {
127 return &Rsp[3];
128 }
129 case RBP:
130 {
132 return &Rsp[4];
133 }
134 default:
135 return NULL;
136 }
137 }
138
139 return NULL;
140}
141
144{
145 CHAR RegisterStr[17];
146 const UCHAR* RegisterPtr;
147 unsigned short i;
148 unsigned short size;
149
151
152 KDDBGPRINT("Sending registers of thread %" PRIxPTR ".\n", gdb_dbg_tid);
153 KDDBGPRINT("Current thread_id: %p.\n", PsGetThreadId((PETHREAD)(ULONG_PTR)CurrentStateChange.Thread));
154 if (((gdb_dbg_pid == 0) && (gdb_dbg_tid == 0)) ||
156 {
157 for (i = 0; i < 24; i++)
158 {
159 RegisterPtr = ctx_to_reg(&CurrentContext, i);
160 size = reg_size[i] * 2;
161 RegisterStr[size] = 0;
162 while (size)
163 {
164 size--;
165 RegisterStr[size] = hex_chars[RegisterPtr[size/2] & 0xF];
166 size--;
167 RegisterStr[size] = hex_chars[RegisterPtr[size/2] >> 4];
168 }
169
170 send_gdb_partial_packet(RegisterStr);
171 }
172 }
173 else
174 {
175 PETHREAD DbgThread;
176
177 DbgThread = find_thread(gdb_dbg_pid, gdb_dbg_tid);
178
179 if (DbgThread == NULL)
180 {
181 /* Thread is dead */
183 return finish_gdb_packet();
184 }
185
186 for (i = 0; i < 24; i++)
187 {
188 RegisterPtr = thread_to_reg(DbgThread, i);
189 size = reg_size[i] * 2;
190 RegisterStr[size] = 0;
191 while (size)
192 {
193 if (RegisterPtr)
194 {
195 size--;
196 RegisterStr[size] = hex_chars[RegisterPtr[size/2] & 0xF];
197 size--;
198 RegisterStr[size] = hex_chars[RegisterPtr[size/2] >> 4];
199 }
200 else
201 {
202 size--;
203 RegisterStr[size] = 'x';
204 size--;
205 RegisterStr[size] = 'x';
206 }
207 }
208
209 send_gdb_partial_packet(RegisterStr);
210 }
211 }
212
213 return finish_gdb_packet();
214}
215
218{
219 enum reg_name reg_name;
220 const void* ptr;
221
222 /* Get the GDB register name (gdb_input = "pXX") */
224
225 if (((gdb_dbg_pid == 0) && (gdb_dbg_tid == 0)) ||
227 {
228 /* We can get it from the context of the current exception */
230 }
231 else
232 {
233 PETHREAD DbgThread;
234
235 DbgThread = find_thread(gdb_dbg_pid, gdb_dbg_tid);
236
237 if (DbgThread == NULL)
238 {
239 /* Thread is dead */
240 return send_gdb_packet("E03");
241 }
242
243 ptr = thread_to_reg(DbgThread, reg_name);
244 }
245
246 if (!ptr)
247 {
248 unsigned char size = reg_size[reg_name];
250 while (size--)
252 return finish_gdb_packet();
253 }
254 else
255 {
256 KDDBGPRINT("KDGDB: Sending registers as memory.\n");
258 }
259}
KDSTATUS gdb_send_registers(void)
Definition: amd64_sup.c:143
static const unsigned char reg_size[]
Definition: amd64_sup.c:21
reg_name
Definition: amd64_sup.c:11
@ RDX
Definition: amd64_sup.c:12
@ ST7
Definition: amd64_sup.c:17
@ EFLAGS
Definition: amd64_sup.c:15
@ RSP
Definition: amd64_sup.c:12
@ FSTAT
Definition: amd64_sup.c:18
@ FOOFF
Definition: amd64_sup.c:18
@ FTAG
Definition: amd64_sup.c:18
@ R15
Definition: amd64_sup.c:13
@ R13
Definition: amd64_sup.c:13
@ RSI
Definition: amd64_sup.c:12
@ FOSEG
Definition: amd64_sup.c:18
@ ST2
Definition: amd64_sup.c:17
@ FCTRL
Definition: amd64_sup.c:18
@ RBX
Definition: amd64_sup.c:12
@ ST1
Definition: amd64_sup.c:17
@ R12
Definition: amd64_sup.c:13
@ R9
Definition: amd64_sup.c:13
@ FS
Definition: amd64_sup.c:16
@ ES
Definition: amd64_sup.c:16
@ FISEG
Definition: amd64_sup.c:18
@ ST5
Definition: amd64_sup.c:17
@ CS
Definition: amd64_sup.c:16
@ FIOFF
Definition: amd64_sup.c:18
@ RDI
Definition: amd64_sup.c:12
@ RCX
Definition: amd64_sup.c:12
@ RIP
Definition: amd64_sup.c:14
@ GS
Definition: amd64_sup.c:16
@ RBP
Definition: amd64_sup.c:12
@ ST4
Definition: amd64_sup.c:17
@ ST6
Definition: amd64_sup.c:17
@ DS
Definition: amd64_sup.c:16
@ R14
Definition: amd64_sup.c:13
@ FOP
Definition: amd64_sup.c:18
@ SS
Definition: amd64_sup.c:16
@ RAX
Definition: amd64_sup.c:12
@ R8
Definition: amd64_sup.c:13
@ ST3
Definition: amd64_sup.c:17
@ R11
Definition: amd64_sup.c:13
@ R10
Definition: amd64_sup.c:13
@ ST0
Definition: amd64_sup.c:17
static const void * thread_to_reg(PETHREAD Thread, enum reg_name reg_name)
Definition: amd64_sup.c:68
KDSTATUS gdb_send_register(void)
Definition: amd64_sup.c:217
static const void * ctx_to_reg(CONTEXT *ctx, enum reg_name name)
Definition: amd64_sup.c:34
#define NULL
Definition: types.h:112
#define PRIxPTR
Definition: inttypes.h:227
#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
CHAR gdb_input[0x1000]
Definition: gdb_receive.c:11
char hex_value(char ch)
Definition: gdb_receive.c:15
KDSTATUS send_gdb_memory(_In_ const VOID *Buffer, _In_ size_t Length)
Definition: gdb_send.c:158
void send_gdb_partial_packet(_In_ const CHAR *Buffer)
Definition: gdb_send.c:60
void start_gdb_packet(void)
Definition: gdb_send.c:52
KDSTATUS finish_gdb_packet(void)
Definition: gdb_send.c:74
const char hex_chars[]
Definition: gdb_send.c:11
KDSTATUS send_gdb_packet(_In_ const CHAR *Buffer)
Definition: gdb_send.c:100
GLsizeiptr size
Definition: glext.h:5919
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint i
Definition: glfuncs.h:248
CONTEXT CurrentContext
Definition: kdpacket.c:29
DBGKD_ANY_WAIT_STATE_CHANGE CurrentStateChange
Definition: kdpacket.c:28
FORCEINLINE HANDLE gdb_tid_to_handle(UINT_PTR Tid)
Definition: kdgdb.h:35
PETHREAD find_thread(_In_ UINT_PTR Pid, _In_ UINT_PTR Tid)
Definition: utils.c:41
static PVOID ptr
Definition: dispmode.c:27
HANDLE NTAPI PsGetThreadId(IN PETHREAD Thread)
Definition: thread.c:705
ULONG KDSTATUS
Definition: kddll.h:4
KTHREAD Tcb
Definition: pstypes.h:1192
PKTRAP_FRAME TrapFrame
Definition: ketypes.h:1902
PVOID InitialStack
Definition: ketypes.h:1792
PVOID KernelStack
Definition: ketypes.h:1803
UINT64 Rsp
Definition: ketypes.h:485
UINT64 Rdi
Definition: ketypes.h:469
UINT64 Rbp
Definition: ketypes.h:471
UINT64 Rsi
Definition: ketypes.h:470
UINT64 Rdx
Definition: ketypes.h:419
ULONG EFlags
Definition: ketypes.h:483
UINT64 Rbx
Definition: ketypes.h:468
USHORT SegSs
Definition: ketypes.h:486
USHORT SegCs
Definition: ketypes.h:479
UINT64 Rax
Definition: ketypes.h:417
UINT64 R10
Definition: ketypes.h:422
UINT64 Rip
Definition: ketypes.h:478
USHORT SegEs
Definition: ketypes.h:464
USHORT SegFs
Definition: ketypes.h:465
USHORT SegGs
Definition: ketypes.h:466
UINT64 R9
Definition: ketypes.h:421
UINT64 R8
Definition: ketypes.h:420
UINT64 Rcx
Definition: ketypes.h:418
UINT64 R11
Definition: ketypes.h:423
Definition: name.c:39
uint32_t * PULONG_PTR
Definition: typedefs.h:65
uint32_t ULONG_PTR
Definition: typedefs.h:65
unsigned char UCHAR
Definition: xmlstorage.h:181
char CHAR
Definition: xmlstorage.h:175