ReactOS 0.4.15-dev-5669-g09dde2c
kdio.c File Reference
#include <ntoskrnl.h>
#include <reactos/buildno.h>
#include <debug.h>
Include dependency graph for kdio.c:

Go to the source code of this file.

Macros

#define NDEBUG
 
#define KdpBufferSize   (1024 * 512)
 
#define KdpScreenLineLengthDefault   80
 

Functions

KIRQL NTAPI KdbpAcquireLock (_In_ PKSPIN_LOCK SpinLock)
 
VOID NTAPI KdbpReleaseLock (_In_ PKSPIN_LOCK SpinLock, _In_ KIRQL OldIrql)
 
static VOID NTAPI KdpLoggerThread (PVOID Context)
 
static VOID NTAPI KdpPrintToLogFile (PCHAR String, ULONG Length)
 
VOID NTAPI KdpDebugLogInit (PKD_DISPATCH_TABLE DispatchTable, ULONG BootPhase)
 
static VOID NTAPI KdpSerialPrint (PCHAR String, ULONG Length)
 
VOID NTAPI KdpSerialInit (PKD_DISPATCH_TABLE DispatchTable, ULONG BootPhase)
 
VOID KdpScreenAcquire (VOID)
 
VOID KdpScreenRelease (VOID)
 
static VOID NTAPI KdpScreenPrint (PCHAR String, ULONG Length)
 
VOID NTAPI KdpScreenInit (PKD_DISPATCH_TABLE DispatchTable, ULONG BootPhase)
 
BOOLEAN NTAPI KdpPrintString (_In_ PSTRING Output)
 
VOID NTAPI KdSendPacket (_In_ ULONG PacketType, _In_ PSTRING MessageHeader, _In_opt_ PSTRING MessageData, _Inout_ PKD_CONTEXT Context)
 
KDSTATUS NTAPI KdReceivePacket (_In_ ULONG PacketType, _Out_ PSTRING MessageHeader, _Out_ PSTRING MessageData, _Out_ PULONG DataLength, _Inout_ PKD_CONTEXT Context)
 

Variables

static BOOLEAN KdpLoggingEnabled = FALSE
 
static BOOLEAN KdpLoggingStarting = FALSE
 
static PCHAR KdpDebugBuffer = NULL
 
static volatile ULONG KdpCurrentPosition = 0
 
static volatile ULONG KdpFreeBytes = 0
 
static KSPIN_LOCK KdpDebugLogSpinLock
 
static KEVENT KdpLoggerThreadEvent
 
static HANDLE KdpLogFileHandle
 
ANSI_STRING KdpLogFileName = RTL_CONSTANT_STRING("\\SystemRoot\\debug.log")
 
ULONG ExpInitializationPhase
 
static KSPIN_LOCK KdpSerialSpinLock
 
ULONG SerialPortNumber = DEFAULT_DEBUG_PORT
 
CPPORT SerialPortInfo = {0, DEFAULT_DEBUG_BAUD_RATE, 0}
 
static CHAR KdpScreenLineBuffer [KdpScreenLineLengthDefault+1] = ""
 
static ULONG KdpScreenLineBufferPos = 0
 
static ULONG KdpScreenLineLength = 0
 
const ULONG KdpDmesgBufferSize = 128 * 1024
 
PCHAR KdpDmesgBuffer = NULL
 
volatile ULONG KdpDmesgCurrentPosition = 0
 
volatile ULONG KdpDmesgFreeBytes = 0
 
volatile ULONG KdbDmesgTotalWritten = 0
 
volatile BOOLEAN KdbpIsInDmesgMode = FALSE
 
static KSPIN_LOCK KdpDmesgLogSpinLock
 
KDP_DEBUG_MODE KdpDebugMode
 
LIST_ENTRY KdProviders = {&KdProviders, &KdProviders}
 
KD_DISPATCH_TABLE DispatchTable [KdMax]
 
PKDP_INIT_ROUTINE InitRoutines [KdMax]
 
static ULONG KdbgNextApiNumber = DbgKdContinueApi
 
static CONTEXT KdbgContext
 
static EXCEPTION_RECORD64 KdbgExceptionRecord
 
static BOOLEAN KdbgFirstChanceException
 
static NTSTATUS KdbgContinueStatus = STATUS_SUCCESS
 
STRING KdbPromptString
 

Macro Definition Documentation

◆ KdpBufferSize

#define KdpBufferSize   (1024 * 512)

Definition at line 20 of file kdio.c.

◆ KdpScreenLineLengthDefault

#define KdpScreenLineLengthDefault   80

Definition at line 36 of file kdio.c.

◆ NDEBUG

#define NDEBUG

Definition at line 15 of file kdio.c.

Function Documentation

◆ KdbpAcquireLock()

KIRQL NTAPI KdbpAcquireLock ( _In_ PKSPIN_LOCK  SpinLock)

Definition at line 72 of file kdio.c.

74{
76
77 /* Acquire the spinlock without waiting at raised IRQL */
78 while (TRUE)
79 {
80 /* Loop until the spinlock becomes available */
81 while (!KeTestSpinLock(SpinLock));
82
83 /* Spinlock is free, raise IRQL to high level */
85
86 /* Try to get the spinlock */
88 break;
89
90 /* Someone else got the spinlock, lower IRQL back */
92 }
93
94 return OldIrql;
95}
#define TRUE
Definition: types.h:120
UCHAR KIRQL
Definition: env_spec_w32.h:591
#define KeRaiseIrql(irql, oldIrql)
Definition: env_spec_w32.h:597
#define HIGH_LEVEL
Definition: env_spec_w32.h:703
#define KeLowerIrql(oldIrql)
Definition: env_spec_w32.h:602
BOOLEAN FASTCALL KeTestSpinLock(IN PKSPIN_LOCK SpinLock)
Definition: spinlock.c:475
BOOLEAN FASTCALL KeTryToAcquireSpinLockAtDpcLevel(IN OUT PKSPIN_LOCK SpinLock)
Definition: spinlock.c:309
_Must_inspect_result_ _In_opt_ PWDF_OBJECT_ATTRIBUTES _Out_ WDFSPINLOCK * SpinLock
Definition: wdfsync.h:228
_Requires_lock_held_ Interrupt _Releases_lock_ Interrupt _In_ _IRQL_restores_ KIRQL OldIrql
Definition: kefuncs.h:792

Referenced by KdpPrintToLogFile(), KdpScreenPrint(), and KdpSerialPrint().

◆ KdbpReleaseLock()

VOID NTAPI KdbpReleaseLock ( _In_ PKSPIN_LOCK  SpinLock,
_In_ KIRQL  OldIrql 
)

Definition at line 99 of file kdio.c.

102{
103 /* Release the spinlock */
105 // KeReleaseSpinLockFromDpcLevel(SpinLock);
106
107 /* Restore the old IRQL */
109}
VOID FASTCALL KiReleaseSpinLock(IN PKSPIN_LOCK SpinLock)
Definition: spinlock.c:298

Referenced by KdpPrintToLogFile(), KdpScreenPrint(), and KdpSerialPrint().

◆ KdpDebugLogInit()

VOID NTAPI KdpDebugLogInit ( PKD_DISPATCH_TABLE  DispatchTable,
ULONG  BootPhase 
)

Definition at line 218 of file kdio.c.

220{
225 HANDLE ThreadHandle;
227
228 if (!KdpDebugMode.File) return;
229
230 if (BootPhase == 0)
231 {
232 /* Write out the functions that we support for now */
233 DispatchTable->KdpInitRoutine = KdpDebugLogInit;
234 DispatchTable->KdpPrintRoutine = KdpPrintToLogFile;
235
236 /* Register as a Provider */
237 InsertTailList(&KdProviders, &DispatchTable->KdProvidersList);
238 }
239 else if (BootPhase == 1)
240 {
241 /* Allocate a buffer for debug log */
242 KdpDebugBuffer = ExAllocatePoolZero(NonPagedPool,
244 TAG_KDBG);
245 if (!KdpDebugBuffer)
246 {
247 KdpDebugMode.File = FALSE;
248 return;
249 }
251
252 /* Initialize spinlock */
254
255 HalDisplayString("\r\n File log debugging enabled\r\n\r\n");
256 }
257 else if (BootPhase == 3)
258 {
259 /* Setup the log name */
261 if (!NT_SUCCESS(Status)) return;
262
264 &FileName,
266 NULL,
267 NULL);
268
269 /* Create the log file */
270 Status = ZwCreateFile(&KdpLogFileHandle,
273 &Iosb,
274 NULL,
279 NULL,
280 0);
281
283
284 if (!NT_SUCCESS(Status))
285 {
286 DPRINT1("Failed to open log file: 0x%08x\n", Status);
287 return;
288 }
289
291
292 /* Create the logger thread */
293 Status = PsCreateSystemThread(&ThreadHandle,
295 NULL,
296 NULL,
297 NULL,
299 NULL);
300 if (!NT_SUCCESS(Status))
301 {
303 return;
304 }
305
306 Priority = 7;
307 ZwSetInformationThread(ThreadHandle,
309 &Priority,
310 sizeof(Priority));
311
312 ZwClose(ThreadHandle);
313 }
314}
LONG NTSTATUS
Definition: precomp.h:26
#define DPRINT1
Definition: precomp.h:8
IN PUNICODE_STRING IN POBJECT_ATTRIBUTES ObjectAttributes
Definition: conport.c:36
#define NULL
Definition: types.h:112
#define FALSE
Definition: types.h:117
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
@ ThreadPriority
Definition: compat.h:937
#define FILE_ATTRIBUTE_NORMAL
Definition: compat.h:137
LONG KPRIORITY
Definition: compat.h:803
#define FILE_SHARE_READ
Definition: compat.h:136
return Iosb
Definition: create.c:4402
#define InsertTailList(ListHead, Entry)
#define KeInitializeEvent(pEvt, foo, foo2)
Definition: env_spec_w32.h:477
#define NonPagedPool
Definition: env_spec_w32.h:307
#define KeInitializeSpinLock(sl)
Definition: env_spec_w32.h:604
struct _FileName FileName
Definition: fatprocs.h:896
#define FILE_SYNCHRONOUS_IO_NONALERT
Definition: from_kernel.h:31
#define FILE_WRITE_THROUGH
Definition: from_kernel.h:26
#define FILE_SUPERSEDE
Definition: from_kernel.h:53
Status
Definition: gdiplustypes.h:25
NTHALAPI VOID NTAPI HalDisplayString(PUCHAR String)
#define OBJ_KERNEL_HANDLE
Definition: winternl.h:231
#define OBJ_CASE_INSENSITIVE
Definition: winternl.h:228
static VOID NTAPI KdpLoggerThread(PVOID Context)
Definition: kdio.c:115
static volatile ULONG KdpFreeBytes
Definition: kdio.c:25
static HANDLE KdpLogFileHandle
Definition: kdio.c:28
LIST_ENTRY KdProviders
Definition: kdio.c:49
KDP_DEBUG_MODE KdpDebugMode
Definition: kdio.c:48
#define KdpBufferSize
Definition: kdio.c:20
static PCHAR KdpDebugBuffer
Definition: kdio.c:23
static KSPIN_LOCK KdpDebugLogSpinLock
Definition: kdio.c:26
static VOID NTAPI KdpPrintToLogFile(PCHAR String, ULONG Length)
Definition: kdio.c:164
static KEVENT KdpLoggerThreadEvent
Definition: kdio.c:27
ANSI_STRING KdpLogFileName
Definition: kdio.c:29
VOID NTAPI KdpDebugLogInit(PKD_DISPATCH_TABLE DispatchTable, ULONG BootPhase)
Definition: kdio.c:218
#define InitializeObjectAttributes(p, n, a, r, s)
Definition: reg.c:106
NTSYSAPI NTSTATUS NTAPI ZwClose(_In_ HANDLE Handle)
#define THREAD_ALL_ACCESS
Definition: nt_native.h:1339
#define SYNCHRONIZE
Definition: nt_native.h:61
NTSYSAPI NTSTATUS NTAPI RtlAnsiStringToUnicodeString(PUNICODE_STRING DestinationString, PANSI_STRING SourceString, BOOLEAN AllocateDestinationString)
#define FILE_APPEND_DATA
Definition: nt_native.h:634
NTSYSAPI VOID NTAPI RtlFreeUnicodeString(PUNICODE_STRING UnicodeString)
@ SynchronizationEvent
NTSTATUS NTAPI PsCreateSystemThread(OUT PHANDLE ThreadHandle, IN ACCESS_MASK DesiredAccess, IN POBJECT_ATTRIBUTES ObjectAttributes, IN HANDLE ProcessHandle, IN PCLIENT_ID ClientId, IN PKSTART_ROUTINE StartRoutine, IN PVOID StartContext)
Definition: thread.c:602
#define TAG_KDBG
Definition: tag.h:38
_In_ WDFINTERRUPT _In_ WDF_INTERRUPT_POLICY _In_ WDF_INTERRUPT_PRIORITY Priority
Definition: wdfinterrupt.h:655
_In_ PWDFDEVICE_INIT _In_ PWDF_PDO_EVENT_CALLBACKS DispatchTable
Definition: wdfpdo.h:248

Referenced by KdpDebugLogInit(), and KdpPrintToLogFile().

◆ KdpLoggerThread()

static VOID NTAPI KdpLoggerThread ( PVOID  Context)
static

Definition at line 115 of file kdio.c.

116{
117 ULONG beg, end, num;
119
121
123
124 while (TRUE)
125 {
127
128 /* Bug */
129 /* Keep KdpCurrentPosition and KdpFreeBytes values in local
130 * variables to avoid their possible change from Producer part,
131 * KdpPrintToLogFile function
132 */
135
136 /* Now securely calculate values, based on local variables */
137 beg = (end + num) % KdpBufferSize;
139
140 /* Nothing to do? */
141 if (num == 0)
142 continue;
143
144 if (end > beg)
145 {
147 KdpDebugBuffer + beg, num, NULL, NULL);
148 }
149 else
150 {
152 KdpDebugBuffer + beg, KdpBufferSize - beg, NULL, NULL);
153
156 }
157
159 }
160}
#define VOID
Definition: acefi.h:82
#define KeWaitForSingleObject(pEvt, foo, a, b, c)
Definition: env_spec_w32.h:478
#define ExGetPreviousMode
Definition: ex.h:139
#define InterlockedExchangeAddUL(Addend, Value)
Definition: ex.h:1532
GLuint GLuint end
Definition: gl.h:1545
GLuint GLuint num
Definition: glext.h:9618
static BOOLEAN KdpLoggingEnabled
Definition: kdio.c:21
static volatile ULONG KdpCurrentPosition
Definition: kdio.c:24
#define ASSERT(a)
Definition: mode.c:44
#define KernelMode
Definition: asm.h:34
NTSYSAPI NTSTATUS NTAPI NtWriteFile(IN HANDLE hFile, IN HANDLE hEvent OPTIONAL, IN PIO_APC_ROUTINE IoApcRoutine OPTIONAL, IN PVOID IoApcContext OPTIONAL, OUT PIO_STATUS_BLOCK pIoStatusBlock, IN PVOID WriteBuffer, IN ULONG WriteBufferLength, IN PLARGE_INTEGER FileOffset OPTIONAL, IN PULONG LockOperationKey OPTIONAL)
uint32_t ULONG
Definition: typedefs.h:59
@ Executive
Definition: ketypes.h:403

Referenced by KdpDebugLogInit().

◆ KdpPrintString()

BOOLEAN NTAPI KdpPrintString ( _In_ PSTRING  Output)

Definition at line 109 of file kdprint.c.

111{
113 DBGKD_DEBUG_IO DebugIo;
115
116 /* Copy the string */
118 Output->Buffer,
119 Output->Length);
120
121 /* Make sure we don't exceed the KD Packet size */
122 Length = Output->Length;
123 if ((sizeof(DBGKD_DEBUG_IO) + Length) > PACKET_MAX_SIZE)
124 {
125 /* Normalize length */
127 }
128
129 /* Build the packet header */
132 DebugIo.Processor = KeGetCurrentPrcb()->Number;
134 Header.Length = sizeof(DBGKD_DEBUG_IO);
135 Header.Buffer = (PCHAR)&DebugIo;
136
137 /* Build the data */
138 Data.Length = Length;
139 Data.Buffer = KdpMessageBuffer;
140
141 /* Send the packet */
143
144 /* Check if the user pressed CTRL+C */
146}
Definition: Header.h:9
std::wstring STRING
Definition: fontsub.cpp:33
BOOLEAN NTAPI KdpPollBreakInWithPortLock(VOID)
Definition: kdlock.c:35
KD_CONTEXT KdpContext
Definition: kddata.c:65
VOID NTAPI KdpMoveMemory(_In_ PVOID Destination, _In_ PVOID Source, _In_ SIZE_T Length)
Definition: kdapi.c:22
CHAR KdpMessageBuffer[KDP_MSG_BUFFER_SIZE]
Definition: kddata.c:127
VOID NTAPI KdSendPacket(IN ULONG PacketType, IN PSTRING MessageHeader, IN PSTRING MessageData, IN OUT PKD_CONTEXT KdContext)
Definition: kddll.c:314
#define PCHAR
Definition: match.c:90
FORCEINLINE struct _KPRCB * KeGetCurrentPrcb(VOID)
Definition: ketypes.h:1080
_In_ ULONG _In_ ULONG _In_ ULONG Length
Definition: ntddpcm.h:102
USHORT KeProcessorLevel
Definition: krnlinit.c:20
unsigned short USHORT
Definition: pedump.c:61
@ Output
Definition: arc.h:85
ULONG ApiNumber
Definition: windbgkd.h:421
USHORT Processor
Definition: windbgkd.h:423
USHORT ProcessorLevel
Definition: windbgkd.h:422
DBGKD_PRINT_STRING PrintString
Definition: windbgkd.h:426
union _DBGKD_DEBUG_IO::@3507 u
#define DbgKdPrintStringApi
Definition: windbgkd.h:122
#define PACKET_MAX_SIZE
Definition: windbgkd.h:18
struct _DBGKD_DEBUG_IO DBGKD_DEBUG_IO
#define PACKET_TYPE_KD_DEBUG_IO
Definition: windbgkd.h:44

Referenced by KdpDprintf(), KdpPrint(), and KdReceivePacket().

◆ KdpPrintToLogFile()

static VOID NTAPI KdpPrintToLogFile ( PCHAR  String,
ULONG  Length 
)
static

Definition at line 164 of file kdio.c.

166{
168 ULONG beg, end, num;
169 BOOLEAN DoReinit = FALSE;
170
171 if (KdpDebugBuffer == NULL) return;
172
173 /* Acquire the printing spinlock without waiting at raised IRQL */
175
176 beg = KdpCurrentPosition;
178 if (Length < num)
179 num = Length;
180
181 if (num != 0)
182 {
183 end = (beg + num) % KdpBufferSize;
185 KdpFreeBytes -= num;
186
187 if (end > beg)
188 {
190 }
191 else
192 {
195 }
196 }
197
198 /* Release the spinlock */
200 {
201 DoReinit = TRUE;
202 }
204
205 if (DoReinit)
206 {
209 }
210
211 /* Signal the logger thread */
214}
unsigned char BOOLEAN
#define PASSIVE_LEVEL
Definition: env_spec_w32.h:693
#define KeSetEvent(pEvt, foo, foo2)
Definition: env_spec_w32.h:476
#define DISPATCH_LEVEL
Definition: env_spec_w32.h:696
ULONG ExpInitializationPhase
Definition: init.c:68
VOID NTAPI KdbpReleaseLock(_In_ PKSPIN_LOCK SpinLock, _In_ KIRQL OldIrql)
Definition: kdio.c:99
KIRQL NTAPI KdbpAcquireLock(_In_ PKSPIN_LOCK SpinLock)
Definition: kdio.c:72
static BOOLEAN KdpLoggingStarting
Definition: kdio.c:22
#define RtlCopyMemory(Destination, Source, Length)
Definition: typedefs.h:263
_Must_inspect_result_ _In_ WDFDEVICE _In_ WDFSTRING String
Definition: wdfdevice.h:2433
#define IO_NO_INCREMENT
Definition: iotypes.h:598

Referenced by KdpDebugLogInit().

◆ KdpScreenAcquire()

VOID KdpScreenAcquire ( VOID  )

Definition at line 380 of file kdio.c.

381{
382 if (InbvIsBootDriverInstalled() /* &&
383 !InbvCheckDisplayOwnership() */)
384 {
385 /* Acquire ownership and reset the display */
393 }
394}
VOID NTAPI InbvAcquireDisplayOwnership(VOID)
Definition: inbv.c:289
VOID NTAPI InbvInstallDisplayStringFilter(_In_ INBV_DISPLAY_STRING_FILTER DisplayFilter)
Definition: inbv.c:386
VOID NTAPI InbvSetTextColor(_In_ ULONG Color)
Definition: inbv.c:459
BOOLEAN NTAPI InbvEnableDisplayString(_In_ BOOLEAN Enable)
Definition: inbv.c:369
BOOLEAN NTAPI InbvResetDisplay(VOID)
Definition: inbv.c:430
VOID NTAPI InbvSetScrollRegion(_In_ ULONG Left, _In_ ULONG Top, _In_ ULONG Right, _In_ ULONG Bottom)
Definition: inbv.c:447
VOID NTAPI InbvSolidColorFill(_In_ ULONG Left, _In_ ULONG Top, _In_ ULONG Right, _In_ ULONG Bottom, _In_ ULONG Color)
Definition: inbv.c:484
BOOLEAN NTAPI InbvIsBootDriverInstalled(VOID)
Definition: inbv.c:395
#define SCREEN_WIDTH
Definition: pc98video.c:27
#define SCREEN_HEIGHT
Definition: pc98video.c:28
#define BV_COLOR_WHITE
Definition: display.h:30
#define BV_COLOR_BLACK
Definition: display.h:15

Referenced by KdbpInternalEnter(), and KdpScreenInit().

◆ KdpScreenInit()

VOID NTAPI KdpScreenInit ( PKD_DISPATCH_TABLE  DispatchTable,
ULONG  BootPhase 
)

Definition at line 512 of file kdio.c.

514{
515 if (!KdpDebugMode.Screen) return;
516
517 if (BootPhase == 0)
518 {
519 /* Write out the functions that we support for now */
520 DispatchTable->KdpInitRoutine = KdpScreenInit;
521 DispatchTable->KdpPrintRoutine = KdpScreenPrint;
522
523 /* Register as a Provider */
524 InsertTailList(&KdProviders, &DispatchTable->KdProvidersList);
525 }
526 else if (BootPhase == 1)
527 {
528 /* Allocate a buffer for dmesg log buffer. +1 for terminating null,
529 * see kdbp_cli.c:KdbpCmdDmesg()/2
530 */
531 KdpDmesgBuffer = ExAllocatePoolZero(NonPagedPool,
533 TAG_KDBG);
534 /* Ignore failure if KdpDmesgBuffer is NULL */
537
538 /* Take control of the display */
540
541 /* Initialize spinlock */
543
544 HalDisplayString("\r\n Screen debugging enabled\r\n\r\n");
545 }
546}
static VOID NTAPI KdpScreenPrint(PCHAR String, ULONG Length)
Definition: kdio.c:418
static KSPIN_LOCK KdpDmesgLogSpinLock
Definition: kdio.c:46
const ULONG KdpDmesgBufferSize
Definition: kdio.c:40
volatile ULONG KdpDmesgFreeBytes
Definition: kdio.c:43
PCHAR KdpDmesgBuffer
Definition: kdio.c:41
volatile ULONG KdbDmesgTotalWritten
Definition: kdio.c:44
VOID KdpScreenAcquire(VOID)
Definition: kdio.c:380
VOID NTAPI KdpScreenInit(PKD_DISPATCH_TABLE DispatchTable, ULONG BootPhase)
Definition: kdio.c:512

Referenced by KdpScreenInit().

◆ KdpScreenPrint()

static VOID NTAPI KdpScreenPrint ( PCHAR  String,
ULONG  Length 
)
static

Definition at line 418 of file kdio.c.

420{
421 PCHAR pch = String;
423 ULONG beg, end, num;
424
425 while (pch < String + Length && *pch)
426 {
427 if (*pch == '\b')
428 {
429 /* HalDisplayString does not support '\b'. Workaround it and use '\r' */
430 if (KdpScreenLineLength > 0)
431 {
432 /* Remove last character from buffer */
435
436 /* Clear row and print line again */
437 HalDisplayString("\r");
439 }
440 }
441 else
442 {
445 }
446
448 {
449 /* Print buffered characters */
452
453 /* Clear line buffer */
454 KdpScreenLineBuffer[0] = '\0';
456 }
457
458 ++pch;
459 }
460
461 /* Print buffered characters */
463 {
466 }
467
468 /* Dmesg: store the string in the buffer to show it later */
470 return;
471
472 if (KdpDmesgBuffer == NULL)
473 return;
474
475 /* Acquire the printing spinlock without waiting at raised IRQL */
477
478 /* Invariant: always_true(KdpDmesgFreeBytes == KdpDmesgBufferSize);
479 * set num to min(KdpDmesgFreeBytes, Length).
480 */
483 if (num != 0)
484 {
485 end = (beg + num) % KdpDmesgBufferSize;
486 if (end > beg)
487 {
489 }
490 else
491 {
494 }
496
497 /* Counting the total bytes written */
499 }
500
501 /* Release the spinlock */
503
504 /* Optional step(?): find out a way to notify about buffer exhaustion,
505 * and possibly fall into kbd to use dmesg command: user will read
506 * debug strings before they will be wiped over by next writes.
507 */
508}
static ULONG KdpScreenLineBufferPos
Definition: kdio.c:38
volatile ULONG KdpDmesgCurrentPosition
Definition: kdio.c:42
static CHAR KdpScreenLineBuffer[KdpScreenLineLengthDefault+1]
Definition: kdio.c:37
volatile BOOLEAN KdbpIsInDmesgMode
Definition: kdio.c:45
static ULONG KdpScreenLineLength
Definition: kdio.c:38
#define KdpScreenLineLengthDefault
Definition: kdio.c:36
#define pch(ap)
Definition: match.c:418
char * PCHAR
Definition: typedefs.h:51

Referenced by KdpScreenInit().

◆ KdpScreenRelease()

VOID KdpScreenRelease ( VOID  )

Definition at line 399 of file kdio.c.

400{
403 {
404 /* Release the display */
405 // InbvSetDisplayOwnership(FALSE);
407 }
408}
VOID NTAPI InbvNotifyDisplayOwnershipLost(_In_ INBV_RESET_DISPLAY_PARAMETERS Callback)
Definition: inbv.c:403
BOOLEAN NTAPI InbvCheckDisplayOwnership(VOID)
Definition: inbv.c:315

Referenced by KdbpInternalEnter().

◆ KdpSerialInit()

VOID NTAPI KdpSerialInit ( PKD_DISPATCH_TABLE  DispatchTable,
ULONG  BootPhase 
)

Definition at line 346 of file kdio.c.

348{
349 if (!KdpDebugMode.Serial) return;
350
351 if (BootPhase == 0)
352 {
353 /* Write out the functions that we support for now */
354 DispatchTable->KdpInitRoutine = KdpSerialInit;
355 DispatchTable->KdpPrintRoutine = KdpSerialPrint;
356
357 /* Initialize the Port */
359 {
360 KdpDebugMode.Serial = FALSE;
361 return;
362 }
364
365 /* Initialize spinlock */
367
368 /* Register as a Provider */
369 InsertTailList(&KdProviders, &DispatchTable->KdProvidersList);
370 }
371 else if (BootPhase == 1)
372 {
373 HalDisplayString("\r\n Serial debugging enabled\r\n\r\n");
374 }
375}
PUCHAR KdComPortInUse
Definition: usage.c:17
BOOLEAN NTAPI KdPortInitializeEx(PCPPORT PortInformation, ULONG ComPortNumber)
ULONG SerialPortNumber
Definition: kdio.c:33
VOID NTAPI KdpSerialInit(PKD_DISPATCH_TABLE DispatchTable, ULONG BootPhase)
Definition: kdio.c:346
static KSPIN_LOCK KdpSerialSpinLock
Definition: kdio.c:32
CPPORT SerialPortInfo
Definition: kdio.c:34
static VOID NTAPI KdpSerialPrint(PCHAR String, ULONG Length)
Definition: kdio.c:320
PUCHAR Address
Definition: cportlib.h:29

Referenced by KdpSerialInit().

◆ KdpSerialPrint()

static VOID NTAPI KdpSerialPrint ( PCHAR  String,
ULONG  Length 
)
static

Definition at line 320 of file kdio.c.

322{
323 PCHAR pch = String;
325
326 /* Acquire the printing spinlock without waiting at raised IRQL */
328
329 /* Output the string */
330 while (pch < String + Length && *pch)
331 {
332 if (*pch == '\n')
333 {
335 }
337 pch++;
338 }
339
340 /* Release the spinlock */
342}
VOID NTAPI KdPortPutByteEx(PCPPORT PortInformation, UCHAR ByteToSend)

Referenced by KdpSerialInit().

◆ KdReceivePacket()

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

Definition at line 690 of file kdio.c.

696{
697#ifdef KDBG
699 STRING ResponseString;
700 PDBGKD_DEBUG_IO DebugIo;
701 CHAR MessageBuffer[512];
702#endif
703
704 if (PacketType == PACKET_TYPE_KD_STATE_MANIPULATE)
705 {
706 PDBGKD_MANIPULATE_STATE64 ManipulateState = (PDBGKD_MANIPULATE_STATE64)MessageHeader->Buffer;
707 RtlZeroMemory(MessageHeader->Buffer, MessageHeader->MaximumLength);
709 {
710 ManipulateState->ApiNumber = DbgKdGetContextApi;
711 MessageData->Length = 0;
712 MessageData->Buffer = (PCHAR)&KdbgContext;
713 return KdPacketReceived;
714 }
716 {
717 ManipulateState->ApiNumber = DbgKdSetContextApi;
718 MessageData->Length = sizeof(KdbgContext);
719 MessageData->Buffer = (PCHAR)&KdbgContext;
720 return KdPacketReceived;
721 }
723 {
725 }
726 ManipulateState->ApiNumber = DbgKdContinueApi;
727 ManipulateState->u.Continue.ContinueStatus = KdbgContinueStatus;
728
729 /* Prepare for next time */
732
733 return KdPacketReceived;
734 }
735
736 if (PacketType != PACKET_TYPE_KD_DEBUG_IO)
737 return KdPacketTimedOut;
738
739#ifdef KDBG
740 DebugIo = (PDBGKD_DEBUG_IO)MessageHeader->Buffer;
741
742 /* Validate API call */
743 if (MessageHeader->MaximumLength != sizeof(DBGKD_DEBUG_IO))
744 return KdPacketNeedsResend;
745 if (DebugIo->ApiNumber != DbgKdGetStringApi)
746 return KdPacketNeedsResend;
747
748 /* NOTE: We cannot use directly MessageData->Buffer here as it points
749 * to the temporary KdpMessageBuffer scratch buffer that is being
750 * shared with all the possible I/O KD operations that may happen. */
751 ResponseString.Buffer = MessageBuffer;
752 ResponseString.Length = 0;
753 ResponseString.MaximumLength = min(sizeof(MessageBuffer),
754 MessageData->MaximumLength);
755 ResponseString.MaximumLength = min(ResponseString.MaximumLength,
757
758 /* The prompt string has been printed by KdSendPacket; go to
759 * new line and print the kdb prompt -- for SYSREG2 support. */
761 KdpPrintString(&KdbPromptString); // Alternatively, use "Input> "
762
765
766 /* Read a line of user input and retrieve the length.
767 * The output string is NULL-terminated -- documentation states
768 * that DbgPrompt() does not NULL-terminate, but it does. */
769 *DataLength = KdbpReadCommand(ResponseString.Buffer,
770 ResponseString.MaximumLength);
771
774
775 /* Return the length */
777 MessageData->Length = DebugIo->u.GetString.LengthOfStringRead = *DataLength;
778
779 /* Only now we can copy back the data into MessageData->Buffer */
780 RtlCopyMemory(MessageData->Buffer, ResponseString.Buffer, *DataLength);
781#endif
782
783 return KdPacketReceived;
784}
@ NewLine
Definition: asmpp.cpp:33
#define UNIMPLEMENTED
Definition: debug.h:115
_In_ ULONG _In_opt_ WDFREQUEST _In_opt_ PVOID _In_ size_t _In_ PVOID _In_ size_t _Out_ size_t * DataLength
Definition: cdrom.h:1444
ULONG KdbDebugState
Definition: kdb.c:52
@ KD_DEBUG_KDSERIAL
Definition: kdb.h:57
VOID KbdDisableMouse(VOID)
Definition: kdb_keyboard.c:98
VOID KbdEnableMouse(VOID)
Definition: kdb_keyboard.c:93
SIZE_T KdbpReadCommand(_Out_ PCHAR Buffer, _In_ SIZE_T Size)
Reads a line of user input from the terminal.
Definition: kdb_cli.c:3351
static ULONG KdbgNextApiNumber
Definition: kdio.c:62
static NTSTATUS KdbgContinueStatus
Definition: kdio.c:66
BOOLEAN NTAPI KdpPrintString(_In_ PSTRING Output)
Definition: kdprint.c:109
static CONTEXT KdbgContext
Definition: kdio.c:63
STRING KdbPromptString
Definition: kdb_cli.c:167
if(dx< 0)
Definition: linetemp.h:194
#define min(a, b)
Definition: monoChain.cc:55
#define KdPacketReceived
Definition: kddll.h:5
#define KdPacketNeedsResend
Definition: kddll.h:7
#define KdPacketTimedOut
Definition: kddll.h:6
#define STATUS_SUCCESS
Definition: shellext.h:65
NTSTATUS ContinueStatus
Definition: windbgkd.h:579
DBGKD_GET_STRING GetString
Definition: windbgkd.h:427
ULONG LengthOfStringRead
Definition: windbgkd.h:413
DBGKD_CONTINUE Continue
Definition: windbgkd.h:799
union _DBGKD_MANIPULATE_STATE64::@3518 u
#define RTL_CONSTANT_STRING(s)
Definition: tunneltest.c:14
#define RtlZeroMemory(Destination, Length)
Definition: typedefs.h:262
#define DbgKdGetContextApi
Definition: windbgkd.h:76
#define DbgKdSetContextApi
Definition: windbgkd.h:77
#define PACKET_TYPE_KD_STATE_MANIPULATE
Definition: windbgkd.h:43
#define DbgKdGetStringApi
Definition: windbgkd.h:123
struct _DBGKD_MANIPULATE_STATE64 * PDBGKD_MANIPULATE_STATE64
#define DbgKdContinueApi
Definition: windbgkd.h:80
struct _DBGKD_DEBUG_IO * PDBGKD_DEBUG_IO
char CHAR
Definition: xmlstorage.h:175

◆ KdSendPacket()

VOID NTAPI KdSendPacket ( _In_ ULONG  PacketType,
_In_ PSTRING  MessageHeader,
_In_opt_ PSTRING  MessageData,
_Inout_ PKD_CONTEXT  Context 
)

Definition at line 578 of file kdio.c.

583{
584 if (PacketType == PACKET_TYPE_KD_DEBUG_IO)
585 {
586 ULONG ApiNumber = ((PDBGKD_DEBUG_IO)MessageHeader->Buffer)->ApiNumber;
587 PLIST_ENTRY CurrentEntry;
588 PKD_DISPATCH_TABLE CurrentTable;
589
590 /* Validate API call */
591 if (MessageHeader->Length != sizeof(DBGKD_DEBUG_IO))
592 return;
593 if ((ApiNumber != DbgKdPrintStringApi) &&
594 (ApiNumber != DbgKdGetStringApi))
595 {
596 return;
597 }
598 if (!MessageData)
599 return;
600
601 /* NOTE: MessageData->Length should be equal to
602 * DebugIo.u.PrintString.LengthOfString, or to
603 * DebugIo.u.GetString.LengthOfPromptString */
604
605 if (!KdpDebugMode.Value)
606 return;
607
608 /* Call the registered handlers */
609 CurrentEntry = KdProviders.Flink;
610 while (CurrentEntry != &KdProviders)
611 {
612 /* Get the current table */
613 CurrentTable = CONTAINING_RECORD(CurrentEntry,
614 KD_DISPATCH_TABLE,
615 KdProvidersList);
616
617 /* Call it */
618 CurrentTable->KdpPrintRoutine(MessageData->Buffer, MessageData->Length);
619
620 /* Next Table */
621 CurrentEntry = CurrentEntry->Flink;
622 }
623 return;
624 }
625 else if (PacketType == PACKET_TYPE_KD_STATE_CHANGE64)
626 {
627 PDBGKD_ANY_WAIT_STATE_CHANGE WaitStateChange = (PDBGKD_ANY_WAIT_STATE_CHANGE)MessageHeader->Buffer;
628 if (WaitStateChange->NewState == DbgKdLoadSymbolsStateChange)
629 {
630#ifdef KDBG
631 PLDR_DATA_TABLE_ENTRY LdrEntry;
632 /* Load symbols. Currently implemented only for KDBG! */
633 if (KdbpSymFindModule((PVOID)(ULONG_PTR)WaitStateChange->u.LoadSymbols.BaseOfDll, -1, &LdrEntry))
634 {
635 KdbSymProcessSymbols(LdrEntry, !WaitStateChange->u.LoadSymbols.UnloadSymbols);
636 }
637#endif
638 return;
639 }
640 else if (WaitStateChange->NewState == DbgKdExceptionStateChange)
641 {
645 return;
646 }
647 }
648 else if (PacketType == PACKET_TYPE_KD_STATE_MANIPULATE)
649 {
650 PDBGKD_MANIPULATE_STATE64 ManipulateState = (PDBGKD_MANIPULATE_STATE64)MessageHeader->Buffer;
651 if (ManipulateState->ApiNumber == DbgKdGetContextApi)
652 {
653 KD_CONTINUE_TYPE Result;
654
655#ifdef KDBG
656 /* Check if this is an assertion failure */
658 {
659 /* Bump EIP to the instruction following the int 2C */
661 }
662
664 KdbgContext.SegCs & 1,
667#else
668 /* We'll manually dump the stack for the user... */
670 Result = kdHandleException;
671#endif
672 if (Result != kdHandleException)
674 else
677 return;
678 }
679 else if (ManipulateState->ApiNumber == DbgKdSetContextApi)
680 {
682 return;
683 }
684 }
686}
#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:1265
VOID KdbSymProcessSymbols(_Inout_ PLDR_DATA_TABLE_ENTRY LdrEntry, _In_ BOOLEAN Load)
Load symbols from image mapping. If this fails,.
Definition: kdb_symbols.c:298
BOOLEAN KdbpSymFindModule(IN PVOID Address OPTIONAL, IN INT Index OPTIONAL, OUT PLDR_DATA_TABLE_ENTRY *pLdrEntry)
Find a module...
Definition: kdb_symbols.c:75
static BOOLEAN KdbgFirstChanceException
Definition: kdio.c:65
static EXCEPTION_RECORD64 KdbgExceptionRecord
Definition: kdio.c:64
#define KeSetContextPc(Context, ProgramCounter)
Definition: ke.h:34
#define KeGetContextPc(Context)
Definition: ke.h:31
#define STATUS_ASSERTION_FAILURE
Definition: ntstatus.h:960
ULONG SegCs
Definition: nt_native.h:1477
union _DBGKD_ANY_WAIT_STATE_CHANGE::@3510 u
DBGKM_EXCEPTION64 Exception
Definition: windbgkd.h:508
DBGKD_LOAD_SYMBOLS64 LoadSymbols
Definition: windbgkd.h:509
EXCEPTION_RECORD64 ExceptionRecord
Definition: windbgkd.h:312
NTSTATUS ExceptionCode
Definition: rtltypes.h:190
Definition: btrfs_drv.h:1876
Definition: typedefs.h:120
struct _LIST_ENTRY * Flink
Definition: typedefs.h:121
uint32_t ULONG_PTR
Definition: typedefs.h:65
#define CONTAINING_RECORD(address, type, field)
Definition: typedefs.h:260
#define STATUS_UNSUCCESSFUL
Definition: udferr_usr.h:132
#define DbgKdLoadSymbolsStateChange
Definition: windbgkd.h:60
struct _DBGKD_ANY_WAIT_STATE_CHANGE * PDBGKD_ANY_WAIT_STATE_CHANGE
#define DbgKdExceptionStateChange
Definition: windbgkd.h:59
#define PACKET_TYPE_KD_STATE_CHANGE64
Definition: windbgkd.h:48
_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:426

Variable Documentation

◆ DispatchTable

KD_DISPATCH_TABLE DispatchTable[KdMax]

Definition at line 50 of file kdio.c.

◆ ExpInitializationPhase

ULONG ExpInitializationPhase
extern

Definition at line 68 of file init.c.

Referenced by KdpPrintToLogFile().

◆ InitRoutines

PKDP_INIT_ROUTINE InitRoutines[KdMax]
Initial value:

Definition at line 52 of file kdio.c.

Referenced by KdDebuggerInitialize0().

◆ KdbDmesgTotalWritten

volatile ULONG KdbDmesgTotalWritten = 0

Definition at line 44 of file kdio.c.

Referenced by KdbpCmdDmesg(), KdpScreenInit(), and KdpScreenPrint().

◆ KdbgContext

CONTEXT KdbgContext
static

Definition at line 63 of file kdio.c.

Referenced by KdReceivePacket(), and KdSendPacket().

◆ KdbgContinueStatus

NTSTATUS KdbgContinueStatus = STATUS_SUCCESS
static

Definition at line 66 of file kdio.c.

Referenced by KdReceivePacket(), and KdSendPacket().

◆ KdbgExceptionRecord

EXCEPTION_RECORD64 KdbgExceptionRecord
static

Definition at line 64 of file kdio.c.

Referenced by KdSendPacket().

◆ KdbgFirstChanceException

BOOLEAN KdbgFirstChanceException
static

Definition at line 65 of file kdio.c.

Referenced by KdSendPacket().

◆ KdbgNextApiNumber

ULONG KdbgNextApiNumber = DbgKdContinueApi
static

Definition at line 62 of file kdio.c.

Referenced by KdReceivePacket(), and KdSendPacket().

◆ KdbpIsInDmesgMode

volatile BOOLEAN KdbpIsInDmesgMode = FALSE

Definition at line 45 of file kdio.c.

Referenced by KdbpCmdDmesg(), and KdpScreenPrint().

◆ KdbPromptString

◆ KdpCurrentPosition

volatile ULONG KdpCurrentPosition = 0
static

Definition at line 24 of file kdio.c.

Referenced by KdpLoggerThread(), and KdpPrintToLogFile().

◆ KdpDebugBuffer

PCHAR KdpDebugBuffer = NULL
static

Definition at line 23 of file kdio.c.

Referenced by KdpDebugLogInit(), KdpLoggerThread(), and KdpPrintToLogFile().

◆ KdpDebugLogSpinLock

KSPIN_LOCK KdpDebugLogSpinLock
static

Definition at line 26 of file kdio.c.

Referenced by KdpDebugLogInit(), and KdpPrintToLogFile().

◆ KdpDebugMode

◆ KdpDmesgBuffer

PCHAR KdpDmesgBuffer = NULL

Definition at line 41 of file kdio.c.

Referenced by KdbpCmdDmesg(), KdpScreenInit(), and KdpScreenPrint().

◆ KdpDmesgBufferSize

const ULONG KdpDmesgBufferSize = 128 * 1024

Definition at line 40 of file kdio.c.

Referenced by KdbpCmdDmesg(), KdpScreenInit(), and KdpScreenPrint().

◆ KdpDmesgCurrentPosition

volatile ULONG KdpDmesgCurrentPosition = 0

Definition at line 42 of file kdio.c.

Referenced by KdbpCmdDmesg(), and KdpScreenPrint().

◆ KdpDmesgFreeBytes

volatile ULONG KdpDmesgFreeBytes = 0

Definition at line 43 of file kdio.c.

Referenced by KdbpCmdDmesg(), KdpScreenInit(), and KdpScreenPrint().

◆ KdpDmesgLogSpinLock

KSPIN_LOCK KdpDmesgLogSpinLock
static

Definition at line 46 of file kdio.c.

Referenced by KdpScreenInit(), and KdpScreenPrint().

◆ KdpFreeBytes

volatile ULONG KdpFreeBytes = 0
static

Definition at line 25 of file kdio.c.

Referenced by KdpDebugLogInit(), KdpLoggerThread(), and KdpPrintToLogFile().

◆ KdpLogFileHandle

HANDLE KdpLogFileHandle
static

Definition at line 28 of file kdio.c.

Referenced by KdpDebugLogInit(), and KdpLoggerThread().

◆ KdpLogFileName

ANSI_STRING KdpLogFileName = RTL_CONSTANT_STRING("\\SystemRoot\\debug.log")

Definition at line 29 of file kdio.c.

Referenced by KdpDebugLogInit(), and KdpGetDebugMode().

◆ KdpLoggerThreadEvent

KEVENT KdpLoggerThreadEvent
static

Definition at line 27 of file kdio.c.

Referenced by KdpDebugLogInit(), KdpLoggerThread(), and KdpPrintToLogFile().

◆ KdpLoggingEnabled

BOOLEAN KdpLoggingEnabled = FALSE
static

Definition at line 21 of file kdio.c.

Referenced by KdpLoggerThread(), and KdpPrintToLogFile().

◆ KdpLoggingStarting

BOOLEAN KdpLoggingStarting = FALSE
static

Definition at line 22 of file kdio.c.

Referenced by KdpPrintToLogFile().

◆ KdProviders

LIST_ENTRY KdProviders = {&KdProviders, &KdProviders}

◆ KdpScreenLineBuffer

CHAR KdpScreenLineBuffer[KdpScreenLineLengthDefault+1] = ""
static

Definition at line 37 of file kdio.c.

Referenced by KdpScreenPrint().

◆ KdpScreenLineBufferPos

ULONG KdpScreenLineBufferPos = 0
static

Definition at line 38 of file kdio.c.

Referenced by KdpScreenPrint().

◆ KdpScreenLineLength

ULONG KdpScreenLineLength = 0
static

Definition at line 38 of file kdio.c.

Referenced by KdpScreenPrint().

◆ KdpSerialSpinLock

KSPIN_LOCK KdpSerialSpinLock
static

Definition at line 32 of file kdio.c.

Referenced by KdpSerialInit(), and KdpSerialPrint().

◆ SerialPortInfo

CPPORT SerialPortInfo = {0, DEFAULT_DEBUG_BAUD_RATE, 0}

Definition at line 34 of file kdio.c.

Referenced by KdbpTryGetCharSerial(), KdpGetDebugMode(), KdpSerialInit(), and KdpSerialPrint().

◆ SerialPortNumber

ULONG SerialPortNumber = DEFAULT_DEBUG_PORT

Definition at line 33 of file kdio.c.

Referenced by KdpGetDebugMode(), and KdpSerialInit().