ReactOS 0.4.16-dev-306-g647d351
kdb_print.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: IO interface for KDBG. Provides local KDBG versions
5 * of KdpPrintString, KdpPromptString and KdpDprintf.
6 * COPYRIGHT: Copyright 2023 Hermès Bélusca-Maïto <hermes.belusca-maito@reactos.org>
7 */
8
9/* INCLUDES ******************************************************************/
10
11#include <ntoskrnl.h>
12#include "kdb.h"
13
14/* FUNCTIONS *****************************************************************/
15
17
18#undef KdSendPacket
19#define pKdSendPacket KdSendPacket
20
21#undef KdReceivePacket
22#define pKdReceivePacket KdReceivePacket
23
24static VOID
26 _In_ const CSTRING* Output,
27 _In_ ULONG ApiNumber,
31{
33 C_ASSERT(PACKET_MAX_SIZE >= sizeof(*DebugIo));
34
35 ASSERT((ApiNumber == DbgKdPrintStringApi) ||
36 (ApiNumber == DbgKdGetStringApi));
37
38 /* Make sure we don't exceed the KD Packet size */
39 Length = min(Output->Length, PACKET_MAX_SIZE - sizeof(*DebugIo));
40
41 /* Build the packet header */
42 DebugIo->ApiNumber = ApiNumber;
43 DebugIo->ProcessorLevel = 0; // (USHORT)KeProcessorLevel;
44 DebugIo->Processor = KeGetCurrentPrcb()->Number;
45
46 if (ApiNumber == DbgKdPrintStringApi)
47 DebugIo->u.PrintString.LengthOfString = Length;
48 else // if (ApiNumber == DbgKdGetStringApi)
49 DebugIo->u.GetString.LengthOfPromptString = Length;
50
51 Header->Length = sizeof(*DebugIo);
52 Header->Buffer = (PCHAR)DebugIo;
53
54 /* Build the data */
55 Data->Length = Length;
56 Data->Buffer = (PCHAR)Output->Buffer;
57
58 /* Send the packet */
59 /* IO packet: call KdTerm */
61}
62
63VOID
65 _In_ const CSTRING* Output)
66{
67 DBGKD_DEBUG_IO DebugIo;
69
71 &DebugIo, &Header, &Data);
72}
73
74static BOOLEAN
76 _In_ const CSTRING* PromptString,
77 _Inout_ PSTRING ResponseString)
78{
79 DBGKD_DEBUG_IO DebugIo;
83
84 /* Print the prompt */
85 // DebugIo.u.GetString.LengthOfPromptString = Length;
86 DebugIo.u.GetString.LengthOfStringRead = ResponseString->MaximumLength;
88 &DebugIo, &Header, &Data);
89
90 /* Set the maximum lengths for the receive */
91 Header.MaximumLength = sizeof(DebugIo);
92 Data.MaximumLength = ResponseString->MaximumLength;
93 /* Build the data */
94 Data.Buffer = ResponseString->Buffer;
95
96 /* Enter receive loop */
97 do
98 {
99 /* Get our reply */
100 /* IO packet: call KdTerm */
102 &Header,
103 &Data,
104 &Length,
106
107 /* Return TRUE if we need to resend */
109 return TRUE;
110
111 /* Loop until we succeed */
112 } while (Status != KdPacketReceived);
113
114 /* Don't copy back a larger response than there is room for */
115 Length = min(Length, ResponseString->MaximumLength);
116
117 /* We've got the string back; return the length */
118 ResponseString->Length = (USHORT)Length;
119
120 /* Success; we don't need to resend */
121 return FALSE;
122}
123
124USHORT
126 _In_ const CSTRING* PromptString,
127 _Inout_ PSTRING ResponseString)
128{
129 /* Enter prompt loop: send the prompt and receive the response */
130 ResponseString->Length = 0;
131 while (KdbPromptStringWorker(PromptString, ResponseString))
132 {
133 /* Loop while we need to resend */
134 }
135 return ResponseString->Length;
136}
137
138
139VOID
143{
145
146 Output.Buffer = String;
147 Output.Length = Output.MaximumLength = Length;
149}
150
151VOID
154{
155 KdbPutsN(String, (USHORT)strnlen(String, MAXUSHORT - sizeof(ANSI_NULL)));
156}
157
158VOID
162 ...)
163{
164 va_list ap;
166 CHAR Buffer[1024];
167
168 /* Format the string */
171 sizeof(Buffer),
172 Format,
173 ap);
174 Length = min(Length, MAXUSHORT - sizeof(ANSI_NULL));
175 va_end(ap);
176
177 /* Send it to the debugger directly */
179}
180
181SIZE_T
183 _In_ PCSTR Prompt,
186{
187 CSTRING PromptString;
188 STRING ResponseBuffer;
189
190 PromptString.Buffer = Prompt;
191 PromptString.Length = PromptString.MaximumLength =
192 (USHORT)strnlen(Prompt, MAXUSHORT - sizeof(ANSI_NULL));
193
194 ResponseBuffer.Buffer = Buffer;
195 ResponseBuffer.Length = 0;
196 ResponseBuffer.MaximumLength = (USHORT)min(Size, MAXUSHORT);
197
198 return KdbPromptString(&PromptString, &ResponseBuffer);
199}
200
201/* EOF */
unsigned char BOOLEAN
#define __cdecl
Definition: accygwin.h:79
char * va_list
Definition: acmsvcex.h:78
#define va_end(ap)
Definition: acmsvcex.h:90
#define va_start(ap, A)
Definition: acmsvcex.h:91
Definition: bufpool.h:45
Definition: Header.h:9
#define TRUE
Definition: types.h:120
#define FALSE
Definition: types.h:117
std::wstring STRING
Definition: fontsub.cpp:33
Status
Definition: gdiplustypes.h:25
#define C_ASSERT(e)
Definition: intsafe.h:73
static VOID KdbPrintStringWorker(_In_ const CSTRING *Output, _In_ ULONG ApiNumber, _Inout_ PDBGKD_DEBUG_IO DebugIo, _Inout_ PSTRING Header, _Inout_ PSTRING Data)
Definition: kdb_print.c:25
#define pKdReceivePacket
Definition: kdb_print.c:22
VOID KdbPrintString(_In_ const CSTRING *Output)
Definition: kdb_print.c:64
USHORT KdbPromptString(_In_ const CSTRING *PromptString, _Inout_ PSTRING ResponseString)
Definition: kdb_print.c:125
static BOOLEAN KdbPromptStringWorker(_In_ const CSTRING *PromptString, _Inout_ PSTRING ResponseString)
Definition: kdb_print.c:75
VOID KdbPuts(_In_ PCSTR String)
Definition: kdb_print.c:152
VOID KdbPutsN(_In_ PCCH String, _In_ USHORT Length)
Definition: kdb_print.c:140
VOID __cdecl KdbPrintf(_In_ PCSTR Format,...)
Definition: kdb_print.c:160
static KD_CONTEXT KdbgKdContext
Definition: kdb_print.c:16
SIZE_T KdbPrompt(_In_ PCSTR Prompt, _Out_ PCHAR Buffer, _In_ SIZE_T Size)
Definition: kdb_print.c:182
#define pKdSendPacket
Definition: kdb_print.c:19
#define PCHAR
Definition: match.c:90
#define ASSERT(a)
Definition: mode.c:44
#define min(a, b)
Definition: monoChain.cc:55
FORCEINLINE struct _KPRCB * KeGetCurrentPrcb(VOID)
Definition: ketypes.h:1150
#define _Inout_
Definition: no_sal2.h:162
#define _Out_
Definition: no_sal2.h:160
#define _In_
Definition: no_sal2.h:158
#define ANSI_NULL
CONST CHAR * PCCH
Definition: ntbasedef.h:400
_In_ ULONG _In_ ULONG _In_ ULONG Length
Definition: ntddpcm.h:102
unsigned short USHORT
Definition: pedump.c:61
@ Output
Definition: arc.h:85
#define KdPacketReceived
Definition: kddll.h:5
#define KdPacketNeedsResend
Definition: kddll.h:7
ULONG KDSTATUS
Definition: kddll.h:4
USHORT MaximumLength
Definition: umtypes.h:158
CONST CHAR * Buffer
Definition: umtypes.h:159
USHORT Length
Definition: umtypes.h:157
DBGKD_GET_STRING GetString
Definition: windbgkd.h:427
union _DBGKD_DEBUG_IO::@3546 u
ULONG LengthOfStringRead
Definition: windbgkd.h:413
ULONG_PTR SIZE_T
Definition: typedefs.h:80
const char * PCSTR
Definition: typedefs.h:52
#define MAXUSHORT
Definition: typedefs.h:83
uint32_t ULONG
Definition: typedefs.h:59
char * PCHAR
Definition: typedefs.h:51
_Must_inspect_result_ _In_ WDFDEVICE _In_ PWDF_DEVICE_PROPERTY_DATA _In_ DEVPROPTYPE _In_ ULONG Size
Definition: wdfdevice.h:4533
_Must_inspect_result_ _In_ WDFDEVICE _In_ WDFSTRING String
Definition: wdfdevice.h:2433
#define DbgKdPrintStringApi
Definition: windbgkd.h:122
#define PACKET_MAX_SIZE
Definition: windbgkd.h:18
#define DbgKdGetStringApi
Definition: windbgkd.h:123
#define PACKET_TYPE_KD_DEBUG_IO
Definition: windbgkd.h:44
void int int ULONGLONG int va_list * ap
Definition: winesup.h:36
#define _vsnprintf
Definition: xmlstorage.h:202
char CHAR
Definition: xmlstorage.h:175