ReactOS 0.4.16-dev-1067-ge98bba2
kdio.c File Reference
#include <ntoskrnl.h>
#include <reactos/buildno.h>
#include "kd.h"
#include "kdterminal.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 (_In_ PCCH String, _In_ ULONG Length)
 
NTSTATUS NTAPI KdpDebugLogInit (_In_ PKD_DISPATCH_TABLE DispatchTable, _In_ ULONG BootPhase)
 
static VOID NTAPI KdpSerialPrint (_In_ PCCH String, _In_ ULONG Length)
 
NTSTATUS NTAPI KdpSerialInit (_In_ PKD_DISPATCH_TABLE DispatchTable, _In_ ULONG BootPhase)
 
VOID KdpScreenAcquire (VOID)
 
VOID KdpScreenRelease (VOID)
 
static VOID NTAPI KdpScreenPrint (_In_ PCCH String, _In_ ULONG Length)
 
NTSTATUS NTAPI KdpScreenInit (_In_ PKD_DISPATCH_TABLE DispatchTable, _In_ ULONG BootPhase)
 
static VOID KdIoPrintString (_In_ PCCH String, _In_ ULONG Length)
 
VOID KdIoPuts (_In_ PCSTR String)
 
VOID __cdecl KdIoPrintf (_In_ PCSTR Format,...)
 
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 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")
 
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
 
KDP_DEBUG_MODE KdpDebugMode
 
LIST_ENTRY KdProviders = {&KdProviders, &KdProviders}
 
KD_DISPATCH_TABLE DispatchTable [KdMax] = {0}
 
PKDP_INIT_ROUTINE InitRoutines [KdMax]
 

Macro Definition Documentation

◆ KdpBufferSize

#define KdpBufferSize   (1024 * 512)

Definition at line 28 of file kdio.c.

◆ KdpScreenLineLengthDefault

#define KdpScreenLineLengthDefault   80

Definition at line 42 of file kdio.c.

◆ NDEBUG

#define NDEBUG

Definition at line 20 of file kdio.c.

Function Documentation

◆ KdbpAcquireLock()

KIRQL NTAPI KdbpAcquireLock ( _In_ PKSPIN_LOCK  SpinLock)

Definition at line 64 of file kdio.c.

66{
68
69 /* Acquire the spinlock without waiting at raised IRQL */
70 while (TRUE)
71 {
72 /* Loop until the spinlock becomes available */
73 while (!KeTestSpinLock(SpinLock));
74
75 /* Spinlock is free, raise IRQL to high level */
77
78 /* Try to get the spinlock */
80 break;
81
82 /* Someone else got the spinlock, lower IRQL back */
84 }
85
86 return OldIrql;
87}
#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:778

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

◆ KdbpReleaseLock()

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

Definition at line 91 of file kdio.c.

94{
95 /* Release the spinlock */
97 // KeReleaseSpinLockFromDpcLevel(SpinLock);
98
99 /* Restore the old IRQL */
101}
VOID FASTCALL KiReleaseSpinLock(IN PKSPIN_LOCK SpinLock)
Definition: spinlock.c:298

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

◆ KdIoPrintf()

VOID __cdecl KdIoPrintf ( _In_ PCSTR  Format,
  ... 
)

Definition at line 576 of file kdio.c.

579{
580 va_list ap;
582 CHAR Buffer[512];
583
584 /* Format the string */
587 sizeof(Buffer),
588 Format,
589 ap);
590 va_end(ap);
591
592 /* Send it to the display providers */
594}
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
static VOID KdIoPrintString(_In_ PCCH String, _In_ ULONG Length)
Definition: kdio.c:547
_In_ ULONG _In_ ULONG _In_ ULONG Length
Definition: ntddpcm.h:102
uint32_t ULONG
Definition: typedefs.h:59
void int int ULONGLONG int va_list * ap
Definition: winesup.h:36
#define _vsnprintf
Definition: xmlstorage.h:202
char CHAR
Definition: xmlstorage.h:175

Referenced by KdIoReadLine(), KdReceivePacket(), and KdSendPacket().

◆ KdIoPrintString()

static VOID KdIoPrintString ( _In_ PCCH  String,
_In_ ULONG  Length 
)
static

Definition at line 547 of file kdio.c.

550{
551 PLIST_ENTRY CurrentEntry;
552 PKD_DISPATCH_TABLE CurrentTable;
553
554 /* Call the registered providers */
555 for (CurrentEntry = KdProviders.Flink;
556 CurrentEntry != &KdProviders;
557 CurrentEntry = CurrentEntry->Flink)
558 {
559 CurrentTable = CONTAINING_RECORD(CurrentEntry,
561 KdProvidersList);
562
563 CurrentTable->KdpPrintRoutine(String, Length);
564 }
565}
LIST_ENTRY KdProviders
Definition: kdio.c:47
PKDP_PRINT_ROUTINE KdpPrintRoutine
Definition: kd.h:119
Definition: typedefs.h:120
struct _LIST_ENTRY * Flink
Definition: typedefs.h:121
#define CONTAINING_RECORD(address, type, field)
Definition: typedefs.h:260
_Must_inspect_result_ _In_ WDFDEVICE _In_ WDFSTRING String
Definition: wdfdevice.h:2433

Referenced by KdIoPrintf(), KdIoPuts(), KdReceivePacket(), and KdSendPacket().

◆ KdIoPuts()

VOID KdIoPuts ( _In_ PCSTR  String)

Definition at line 568 of file kdio.c.

570{
572}
ACPI_SIZE strlen(const char *String)
Definition: utclib.c:269

Referenced by KdIoReadLine(), and KdReceivePacket().

◆ KdpDebugLogInit()

NTSTATUS NTAPI KdpDebugLogInit ( _In_ PKD_DISPATCH_TABLE  DispatchTable,
_In_ ULONG  BootPhase 
)

HACK for FILE_APPEND_DATA ** Remove once CORE-18789 is fixed. ** Enforce to go to the end of file

END OF HACK

Definition at line 197 of file kdio.c.

200{
202
203 if (!KdpDebugMode.File)
205
206 if (BootPhase == 0)
207 {
208 /* Write out the functions that we support for now */
209 DispatchTable->KdpPrintRoutine = KdpPrintToLogFile;
210
211 /* Register for BootPhase 1 initialization and as a Provider */
212 DispatchTable->KdpInitRoutine = KdpDebugLogInit;
213 InsertTailList(&KdProviders, &DispatchTable->KdProvidersList);
214 }
215 else if (BootPhase == 1)
216 {
217 /* Allocate a buffer for debug log */
220 TAG_KDBG);
221 if (!KdpDebugBuffer)
222 {
224 RemoveEntryList(&DispatchTable->KdProvidersList);
225 return STATUS_NO_MEMORY;
226 }
228
229 /* Initialize spinlock */
231
232 /* Register for later BootPhase 2 reinitialization */
233 DispatchTable->KdpInitRoutine = KdpDebugLogInit;
234
235 /* Announce ourselves */
236 HalDisplayString(" File log debugging enabled\r\n");
237 }
238 else if (BootPhase >= 2)
239 {
243 HANDLE ThreadHandle;
245
246 /* If we have already successfully opened the log file, bail out */
247 if (KdpLogFileHandle != NULL)
248 return STATUS_SUCCESS;
249
250 /* Setup the log name */
252 if (!NT_SUCCESS(Status))
253 goto Failure;
254
256 &FileName,
258 NULL,
259 NULL);
260
261 /* Create the log file */
262 Status = ZwCreateFile(&KdpLogFileHandle,
265 &Iosb,
266 NULL,
272 NULL,
273 0);
274
276
277 if (!NT_SUCCESS(Status))
278 {
279 DPRINT1("Failed to open log file: 0x%08lx\n", Status);
280
281 /* Schedule an I/O reinitialization if needed */
284 {
285 DispatchTable->KdpInitRoutine = KdpDebugLogInit;
286 return Status;
287 }
288 goto Failure;
289 }
290
294 {
296 FILE_POSITION_INFORMATION FilePosInfo;
297
298 Status = ZwQueryInformationFile(KdpLogFileHandle,
299 &Iosb,
300 &FileInfo,
301 sizeof(FileInfo),
303 DPRINT("Status: 0x%08lx - EOF offset: %I64d\n",
304 Status, FileInfo.EndOfFile.QuadPart);
305
306 Status = ZwQueryInformationFile(KdpLogFileHandle,
307 &Iosb,
308 &FilePosInfo,
309 sizeof(FilePosInfo),
311 DPRINT("Status: 0x%08lx - Position: %I64d\n",
312 Status, FilePosInfo.CurrentByteOffset.QuadPart);
313
314 FilePosInfo.CurrentByteOffset.QuadPart = FileInfo.EndOfFile.QuadPart;
315 Status = ZwSetInformationFile(KdpLogFileHandle,
316 &Iosb,
317 &FilePosInfo,
318 sizeof(FilePosInfo),
320 DPRINT("ZwSetInformationFile(FilePositionInfo) returned: 0x%08lx\n", Status);
321 }
325
326 /* Create the logger thread */
327 Status = PsCreateSystemThread(&ThreadHandle,
329 NULL,
330 NULL,
331 NULL,
333 NULL);
334 if (!NT_SUCCESS(Status))
335 {
336 DPRINT1("Failed to create log file thread: 0x%08lx\n", Status);
338 goto Failure;
339 }
340
342 ZwSetInformationThread(ThreadHandle,
344 &Priority,
345 sizeof(Priority));
346
347 ZwClose(ThreadHandle);
348 return Status;
349
350Failure:
351 KdpFreeBytes = 0;
355 RemoveEntryList(&DispatchTable->KdProvidersList);
356 }
357
358 return Status;
359}
LONG NTSTATUS
Definition: precomp.h:26
#define FILE_NON_DIRECTORY_FILE
Definition: constants.h:492
#define DPRINT1
Definition: precomp.h:8
IN PUNICODE_STRING IN POBJECT_ATTRIBUTES ObjectAttributes
Definition: conport.c:36
#define STATUS_NO_MEMORY
Definition: d3dkmdt.h:51
#define NULL
Definition: types.h:112
#define FALSE
Definition: types.h:117
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:33
@ 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:4403
#define RemoveEntryList(Entry)
Definition: env_spec_w32.h:986
#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:897
@ FilePositionInformation
Definition: from_kernel.h:75
#define FILE_SYNCHRONOUS_IO_NONALERT
Definition: from_kernel.h:31
#define FILE_WRITE_THROUGH
Definition: from_kernel.h:26
#define FILE_OPEN_IF
Definition: from_kernel.h:56
#define FILE_SEQUENTIAL_ONLY
Definition: from_kernel.h:27
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
#define HIGH_PRIORITY
static VOID NTAPI KdpLoggerThread(PVOID Context)
Definition: kdio.c:107
static volatile ULONG KdpFreeBytes
Definition: kdio.c:32
static HANDLE KdpLogFileHandle
Definition: kdio.c:35
KDP_DEBUG_MODE KdpDebugMode
Definition: kdio.c:46
#define KdpBufferSize
Definition: kdio.c:28
static PCHAR KdpDebugBuffer
Definition: kdio.c:30
NTSTATUS NTAPI KdpDebugLogInit(_In_ PKD_DISPATCH_TABLE DispatchTable, _In_ ULONG BootPhase)
Definition: kdio.c:197
static KSPIN_LOCK KdpDebugLogSpinLock
Definition: kdio.c:33
static KEVENT KdpLoggerThreadEvent
Definition: kdio.c:34
ANSI_STRING KdpLogFileName
Definition: kdio.c:36
static VOID NTAPI KdpPrintToLogFile(_In_ PCCH String, _In_ ULONG Length)
Definition: kdio.c:156
#define ExFreePoolWithTag(_P, _T)
Definition: module.h:1109
FORCEINLINE PVOID ExAllocatePoolZero(ULONG PoolType, SIZE_T NumberOfBytes, ULONG Tag)
Definition: precomp.h:45
#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 STATUS_PORT_DISCONNECTED
Definition: ntstatus.h:291
#define FileStandardInformation
Definition: propsheet.cpp:61
#define STATUS_SUCCESS
Definition: shellext.h:65
#define DPRINT
Definition: sndvol32.h:73
LARGE_INTEGER CurrentByteOffset
Definition: nt_native.h:955
UCHAR File
Definition: kd.h:106
#define TAG_KDBG
Definition: tag.h:38
#define STATUS_OBJECT_PATH_NOT_FOUND
Definition: udferr_usr.h:151
#define STATUS_OBJECT_NAME_NOT_FOUND
Definition: udferr_usr.h:149
LONGLONG QuadPart
Definition: typedefs.h:114
_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().

◆ KdpLoggerThread()

static VOID NTAPI KdpLoggerThread ( PVOID  Context)
static

Definition at line 107 of file kdio.c.

108{
109 ULONG beg, end, num;
111
113
115
116 while (TRUE)
117 {
119
120 /* Bug */
121 /* Keep KdpCurrentPosition and KdpFreeBytes values in local
122 * variables to avoid their possible change from Producer part,
123 * KdpPrintToLogFile function
124 */
127
128 /* Now securely calculate values, based on local variables */
129 beg = (end + num) % KdpBufferSize;
131
132 /* Nothing to do? */
133 if (num == 0)
134 continue;
135
136 if (end > beg)
137 {
139 KdpDebugBuffer + beg, num, NULL, NULL);
140 }
141 else
142 {
144 KdpDebugBuffer + beg, KdpBufferSize - beg, NULL, NULL);
145
148 }
149
151 }
152}
#define VOID
Definition: acefi.h:82
#define KeWaitForSingleObject(pEvt, foo, a, b, c)
Definition: env_spec_w32.h:478
#define ExGetPreviousMode
Definition: ex.h:143
#define InterlockedExchangeAddUL(Addend, Value)
Definition: ex.h:1550
GLuint GLuint end
Definition: gl.h:1545
GLuint GLuint num
Definition: glext.h:9618
static BOOLEAN KdpLoggingEnabled
Definition: kdio.c:29
static volatile ULONG KdpCurrentPosition
Definition: kdio.c:31
#define ASSERT(a)
Definition: mode.c:44
#define KernelMode
Definition: asm.h:38
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)
@ Executive
Definition: ketypes.h:415

Referenced by KdpDebugLogInit().

◆ KdpPrintToLogFile()

static VOID NTAPI KdpPrintToLogFile ( _In_ PCCH  String,
_In_ ULONG  Length 
)
static

Definition at line 156 of file kdio.c.

159{
161 ULONG beg, end, num;
162
163 if (KdpDebugBuffer == NULL) return;
164
165 /* Acquire the printing spinlock without waiting at raised IRQL */
167
168 beg = KdpCurrentPosition;
170 if (num != 0)
171 {
172 end = (beg + num) % KdpBufferSize;
174 KdpFreeBytes -= num;
175
176 if (end > beg)
177 {
179 }
180 else
181 {
184 }
185 }
186
187 /* Release the spinlock */
189
190 /* Signal the logger thread */
193}
#define KeSetEvent(pEvt, foo, foo2)
Definition: env_spec_w32.h:476
#define DISPATCH_LEVEL
Definition: env_spec_w32.h:696
VOID NTAPI KdbpReleaseLock(_In_ PKSPIN_LOCK SpinLock, _In_ KIRQL OldIrql)
Definition: kdio.c:91
KIRQL NTAPI KdbpAcquireLock(_In_ PKSPIN_LOCK SpinLock)
Definition: kdio.c:64
#define min(a, b)
Definition: monoChain.cc:55
#define RtlCopyMemory(Destination, Source, Length)
Definition: typedefs.h:263
#define IO_NO_INCREMENT
Definition: iotypes.h:598

Referenced by KdpDebugLogInit().

◆ KdpScreenAcquire()

VOID KdpScreenAcquire ( VOID  )

Definition at line 431 of file kdio.c.

432{
433 if (InbvIsBootDriverInstalled() /* &&
434 !InbvCheckDisplayOwnership() */)
435 {
436 /* Acquire ownership and reset the display */
444 }
445}
VOID NTAPI InbvAcquireDisplayOwnership(VOID)
Definition: inbv.c:293
VOID NTAPI InbvInstallDisplayStringFilter(_In_ INBV_DISPLAY_STRING_FILTER DisplayFilter)
Definition: inbv.c:390
VOID NTAPI InbvSetTextColor(_In_ ULONG Color)
Definition: inbv.c:463
BOOLEAN NTAPI InbvEnableDisplayString(_In_ BOOLEAN Enable)
Definition: inbv.c:373
BOOLEAN NTAPI InbvResetDisplay(VOID)
Definition: inbv.c:434
VOID NTAPI InbvSetScrollRegion(_In_ ULONG Left, _In_ ULONG Top, _In_ ULONG Right, _In_ ULONG Bottom)
Definition: inbv.c:451
VOID NTAPI InbvSolidColorFill(_In_ ULONG Left, _In_ ULONG Top, _In_ ULONG Right, _In_ ULONG Bottom, _In_ ULONG Color)
Definition: inbv.c:488
BOOLEAN NTAPI InbvIsBootDriverInstalled(VOID)
Definition: inbv.c:399
#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()

NTSTATUS NTAPI KdpScreenInit ( _In_ PKD_DISPATCH_TABLE  DispatchTable,
_In_ ULONG  BootPhase 
)

Definition at line 515 of file kdio.c.

518{
519 if (!KdpDebugMode.Screen)
521
522 if (BootPhase == 0)
523 {
524 /* Write out the functions that we support for now */
525 DispatchTable->KdpPrintRoutine = KdpScreenPrint;
526
527 /* Register for BootPhase 1 initialization and as a Provider */
528 DispatchTable->KdpInitRoutine = KdpScreenInit;
529 InsertTailList(&KdProviders, &DispatchTable->KdProvidersList);
530 }
531 else if (BootPhase == 1)
532 {
533 /* Take control of the display */
535
536 /* Announce ourselves */
537 HalDisplayString(" Screen debugging enabled\r\n");
538 }
539
540 return STATUS_SUCCESS;
541}
NTSTATUS NTAPI KdpScreenInit(_In_ PKD_DISPATCH_TABLE DispatchTable, _In_ ULONG BootPhase)
Definition: kdio.c:515
VOID KdpScreenAcquire(VOID)
Definition: kdio.c:431
static VOID NTAPI KdpScreenPrint(_In_ PCCH String, _In_ ULONG Length)
Definition: kdio.c:463
UCHAR Screen
Definition: kd.h:104

Referenced by KdpScreenInit().

◆ KdpScreenPrint()

static VOID NTAPI KdpScreenPrint ( _In_ PCCH  String,
_In_ ULONG  Length 
)
static

Definition at line 463 of file kdio.c.

466{
467 PCCH pch = String;
468
469 while (pch < String + Length && *pch)
470 {
471 if (*pch == '\b')
472 {
473 /* HalDisplayString does not support '\b'. Workaround it and use '\r' */
474 if (KdpScreenLineLength > 0)
475 {
476 /* Remove last character from buffer */
479
480 /* Clear row and print line again */
481 HalDisplayString("\r");
483 }
484 }
485 else
486 {
489 }
490
492 {
493 /* Print buffered characters */
496
497 /* Clear line buffer */
498 KdpScreenLineBuffer[0] = '\0';
500 }
501
502 ++pch;
503 }
504
505 /* Print buffered characters */
507 {
510 }
511}
static ULONG KdpScreenLineBufferPos
Definition: kdio.c:44
static CHAR KdpScreenLineBuffer[KdpScreenLineLengthDefault+1]
Definition: kdio.c:43
static ULONG KdpScreenLineLength
Definition: kdio.c:44
#define KdpScreenLineLengthDefault
Definition: kdio.c:42
#define pch(ap)
Definition: match.c:418
CONST CHAR * PCCH
Definition: ntbasedef.h:400

Referenced by KdpScreenInit().

◆ KdpScreenRelease()

VOID KdpScreenRelease ( VOID  )

Definition at line 450 of file kdio.c.

451{
454 {
455 /* Release the display */
456 // InbvSetDisplayOwnership(FALSE);
458 }
459}
VOID NTAPI InbvNotifyDisplayOwnershipLost(_In_ INBV_RESET_DISPLAY_PARAMETERS Callback)
Definition: inbv.c:407
BOOLEAN NTAPI InbvCheckDisplayOwnership(VOID)
Definition: inbv.c:319

Referenced by KdbpInternalEnter().

◆ KdpSerialInit()

NTSTATUS NTAPI KdpSerialInit ( _In_ PKD_DISPATCH_TABLE  DispatchTable,
_In_ ULONG  BootPhase 
)

Definition at line 392 of file kdio.c.

395{
396 if (!KdpDebugMode.Serial)
398
399 if (BootPhase == 0)
400 {
401 /* Write out the functions that we support for now */
402 DispatchTable->KdpPrintRoutine = KdpSerialPrint;
403
404 /* Initialize the Port */
406 {
409 }
411
412 /* Initialize spinlock */
414
415 /* Register for BootPhase 1 initialization and as a Provider */
416 DispatchTable->KdpInitRoutine = KdpSerialInit;
417 InsertTailList(&KdProviders, &DispatchTable->KdProvidersList);
418 }
419 else if (BootPhase == 1)
420 {
421 /* Announce ourselves */
422 HalDisplayString(" Serial debugging enabled\r\n");
423 }
424
425 return STATUS_SUCCESS;
426}
PUCHAR KdComPortInUse
Definition: usage.c:17
BOOLEAN NTAPI KdPortInitializeEx(PCPPORT PortInformation, ULONG ComPortNumber)
ULONG SerialPortNumber
Definition: kdio.c:39
NTSTATUS NTAPI KdpSerialInit(_In_ PKD_DISPATCH_TABLE DispatchTable, _In_ ULONG BootPhase)
Definition: kdio.c:392
static KSPIN_LOCK KdpSerialSpinLock
Definition: kdio.c:38
CPPORT SerialPortInfo
Definition: kdio.c:40
static VOID NTAPI KdpSerialPrint(_In_ PCCH String, _In_ ULONG Length)
Definition: kdio.c:365
#define STATUS_DEVICE_DOES_NOT_EXIST
Definition: ntstatus.h:428
PUCHAR Address
Definition: cportlib.h:29
UCHAR Serial
Definition: kd.h:105

Referenced by KdpSerialInit().

◆ KdpSerialPrint()

static VOID NTAPI KdpSerialPrint ( _In_ PCCH  String,
_In_ ULONG  Length 
)
static

Definition at line 365 of file kdio.c.

368{
369 PCCH pch = String;
371
372 /* Acquire the printing spinlock without waiting at raised IRQL */
374
375 /* Output the string */
376 while (pch < String + Length && *pch)
377 {
378 if (*pch == '\n')
379 {
381 }
383 ++pch;
384 }
385
386 /* Release the spinlock */
388}
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 707 of file kdio.c.

713{
714#ifdef KDBG
715 PDBGKD_DEBUG_IO DebugIo;
716 STRING ResponseString;
717 CHAR MessageBuffer[512];
718#endif
719
720 if (PacketType == PACKET_TYPE_KD_POLL_BREAKIN)
721 {
722 /* We don't support breaks-in */
723 return KdPacketTimedOut;
724 }
725
726 if (PacketType == PACKET_TYPE_KD_STATE_MANIPULATE)
727 {
728 PDBGKD_MANIPULATE_STATE64 ManipulateState = (PDBGKD_MANIPULATE_STATE64)MessageHeader->Buffer;
729 RtlZeroMemory(MessageHeader->Buffer, MessageHeader->MaximumLength);
730
731 /* The exception (notified via DbgKdExceptionStateChange in
732 * KdSendPacket()) cannot be handled: return a failure code */
733 ManipulateState->ApiNumber = DbgKdContinueApi;
735 return KdPacketReceived;
736 }
737
738 if (PacketType != PACKET_TYPE_KD_DEBUG_IO)
739 {
740 KdIoPrintf("%s: PacketType %d is UNIMPLEMENTED\n", __FUNCTION__, PacketType);
741 return KdPacketTimedOut;
742 }
743
744#ifdef KDBG
745 DebugIo = (PDBGKD_DEBUG_IO)MessageHeader->Buffer;
746
747 /* Validate API call */
748 if (MessageHeader->MaximumLength != sizeof(DBGKD_DEBUG_IO))
749 return KdPacketNeedsResend;
750 if (DebugIo->ApiNumber != DbgKdGetStringApi)
751 return KdPacketNeedsResend;
752
753 /* NOTE: We cannot use directly MessageData->Buffer here as it points
754 * to the temporary KdpMessageBuffer scratch buffer that is being
755 * shared with all the possible I/O KD operations that may happen. */
756 ResponseString.Buffer = MessageBuffer;
757 ResponseString.Length = 0;
758 ResponseString.MaximumLength = min(sizeof(MessageBuffer),
759 MessageData->MaximumLength);
760 ResponseString.MaximumLength = min(ResponseString.MaximumLength,
762
763 /* The prompt string has been printed by KdSendPacket; go to
764 * new line and print the kdb prompt -- for SYSREG2 support. */
765 KdIoPrintString("\n", 1);
766 KdIoPuts(KdbPromptStr.Buffer); // Alternatively, use "Input> "
767
770
771 /*
772 * Read a NULL-terminated line of user input and retrieve its length.
773 * Official documentation states that DbgPrompt() includes a terminating
774 * newline character but does not NULL-terminate. However, experiments
775 * show that this behaviour is left at the discretion of WinDbg itself.
776 * WinDbg NULL-terminates the string unless its buffer is too short,
777 * in which case the string is simply truncated without NULL-termination.
778 */
779 ResponseString.Length =
780 (USHORT)KdIoReadLine(ResponseString.Buffer,
781 ResponseString.MaximumLength);
782
785
786 /* Adjust and return the string length */
787 *DataLength = min(ResponseString.Length + sizeof(ANSI_NULL),
789 MessageData->Length = DebugIo->u.GetString.LengthOfStringRead = *DataLength;
790
791 /* Only now we can copy back the data into MessageData->Buffer */
792 RtlCopyMemory(MessageData->Buffer, ResponseString.Buffer, *DataLength);
793#endif
794
795 return KdPacketReceived;
796}
_In_ ULONG _In_opt_ WDFREQUEST _In_opt_ PVOID _In_ size_t _In_ PVOID _In_ size_t _Out_ size_t * DataLength
Definition: cdrom.h:1444
#define __FUNCTION__
Definition: types.h:116
std::wstring STRING
Definition: fontsub.cpp:33
SIZE_T KdIoReadLine(_Out_ PCHAR Buffer, _In_ SIZE_T Size)
Reads a line of user input from the terminal.
Definition: kdprompt.c:51
const CSTRING KdbPromptStr
Definition: kdb_cli.c:149
VOID __cdecl KdIoPrintf(_In_ PCSTR Format,...)
Definition: kdio.c:576
VOID KdIoPuts(_In_ PCSTR String)
Definition: kdio.c:568
VOID KbdDisableMouse(VOID)
Definition: kdps2kbd.c:98
VOID KbdEnableMouse(VOID)
Definition: kdps2kbd.c:93
ULONG KdbDebugState
Definition: kdterminal.c:32
@ KD_DEBUG_KDSERIAL
Definition: kdterminal.h:36
if(dx< 0)
Definition: linetemp.h:194
#define ANSI_NULL
#define DBG_EXCEPTION_NOT_HANDLED
Definition: ntstatus.h:57
unsigned short USHORT
Definition: pedump.c:61
#define KdPacketReceived
Definition: kddll.h:5
#define KdPacketNeedsResend
Definition: kddll.h:7
#define KdPacketTimedOut
Definition: kddll.h:6
CONST CHAR * Buffer
Definition: umtypes.h:159
NTSTATUS ContinueStatus
Definition: windbgkd.h:579
DBGKD_GET_STRING GetString
Definition: windbgkd.h:427
ULONG ApiNumber
Definition: windbgkd.h:421
union _DBGKD_DEBUG_IO::@3640 u
ULONG LengthOfStringRead
Definition: windbgkd.h:413
union _DBGKD_MANIPULATE_STATE64::@3651 u
DBGKD_CONTINUE Continue
Definition: windbgkd.h:799
#define RtlZeroMemory(Destination, Length)
Definition: typedefs.h:262
#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
#define PACKET_TYPE_KD_POLL_BREAKIN
Definition: windbgkd.h:49
struct _DBGKD_DEBUG_IO * PDBGKD_DEBUG_IO
#define PACKET_TYPE_KD_DEBUG_IO
Definition: windbgkd.h:44

◆ KdSendPacket()

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

Definition at line 602 of file kdio.c.

607{
608 PDBGKD_DEBUG_IO DebugIo;
609
610 if (PacketType == PACKET_TYPE_KD_STATE_CHANGE32 ||
611 PacketType == PACKET_TYPE_KD_STATE_CHANGE64)
612 {
613 PDBGKD_ANY_WAIT_STATE_CHANGE WaitStateChange = (PDBGKD_ANY_WAIT_STATE_CHANGE)MessageHeader->Buffer;
614
615 if (WaitStateChange->NewState == DbgKdLoadSymbolsStateChange)
616 return; // Ignore: invoked anytime a new module is loaded.
617
618 /* We should not get there, unless an exception has been raised */
619 if (WaitStateChange->NewState == DbgKdExceptionStateChange)
620 {
621 PEXCEPTION_RECORD64 ExceptionRecord = &WaitStateChange->u.Exception.ExceptionRecord;
622
623 /*
624 * Claim the debugger to be present, so that KdpSendWaitContinue()
625 * can call back KdReceivePacket(PACKET_TYPE_KD_STATE_MANIPULATE),
626 * which, in turn, informs KD that the exception cannot be handled.
627 */
629 SharedUserData->KdDebuggerEnabled |= 0x00000002;
630
631 KdIoPrintf("%s: Got exception 0x%08lx @ 0x%p, Flags 0x%08x, %s - Info[0]: 0x%p\n",
633 ExceptionRecord->ExceptionCode,
634 (PVOID)(ULONG_PTR)ExceptionRecord->ExceptionAddress,
635 ExceptionRecord->ExceptionFlags,
636 WaitStateChange->u.Exception.FirstChance ? "FirstChance" : "LastChance",
637 ExceptionRecord->ExceptionInformation[0]);
638#if defined(_M_IX86) || defined(_M_AMD64) || defined(_M_ARM) || defined(_M_ARM64)
640 if ((ExceptionRecord->ExceptionCode == STATUS_BREAKPOINT) &&
642 {
643 PCONTEXT ContextRecord = &KeGetCurrentPrcb()->ProcessorState.ContextFrame;
644 ULONG Status =
645#if defined(_M_IX86)
646 ContextRecord->Eax;
647#elif defined(_M_AMD64)
648 (ULONG)ContextRecord->Rcx;
649#elif defined(_M_ARM)
650 ContextRecord->R0;
651#else // defined(_M_ARM64)
652 (ULONG)ContextRecord->X0;
653#endif
654 KdIoPrintf("STATUS_BREAKPOINT Status 0x%08lx\n", Status);
655 }
656// #else
657// #error Unknown architecture
658#endif
659 return;
660 }
661
662 KdIoPrintf("%s: PACKET_TYPE_KD_STATE_CHANGE32/64 NewState %d is UNIMPLEMENTED\n",
663 __FUNCTION__, WaitStateChange->NewState);
664 return;
665 }
666 else
667 if (PacketType == PACKET_TYPE_KD_STATE_MANIPULATE)
668 {
669 PDBGKD_MANIPULATE_STATE64 ManipulateState = (PDBGKD_MANIPULATE_STATE64)MessageHeader->Buffer;
670 KdIoPrintf("%s: PACKET_TYPE_KD_STATE_MANIPULATE for ApiNumber %lu\n",
671 __FUNCTION__, ManipulateState->ApiNumber);
672 return;
673 }
674
675 if (PacketType != PACKET_TYPE_KD_DEBUG_IO)
676 {
677 KdIoPrintf("%s: PacketType %d is UNIMPLEMENTED\n", __FUNCTION__, PacketType);
678 return;
679 }
680
681 DebugIo = (PDBGKD_DEBUG_IO)MessageHeader->Buffer;
682
683 /* Validate API call */
684 if (MessageHeader->Length != sizeof(DBGKD_DEBUG_IO))
685 return;
686 if ((DebugIo->ApiNumber != DbgKdPrintStringApi) &&
687 (DebugIo->ApiNumber != DbgKdGetStringApi))
688 {
689 return;
690 }
691 if (!MessageData)
692 return;
693
694 /* NOTE: MessageData->Length should be equal to
695 * DebugIo.u.PrintString.LengthOfString, or to
696 * DebugIo.u.GetString.LengthOfPromptString */
697
698 if (!KdpDebugMode.Value)
699 return;
700
701 /* Print the string proper */
702 KdIoPrintString(MessageData->Buffer, MessageData->Length);
703}
VOID NTAPI RtlpBreakWithStatusInstruction(VOID)
FORCEINLINE struct _KPRCB * KeGetCurrentPrcb(VOID)
Definition: ketypes.h:1182
_IRQL_requires_same_ _In_ PVOID _Inout_ struct _CONTEXT * ContextRecord
Definition: ntbasedef.h:662
#define STATUS_BREAKPOINT
Definition: ntstatus.h:184
#define SharedUserData
union _DBGKD_ANY_WAIT_STATE_CHANGE::@3643 u
DBGKM_EXCEPTION64 Exception
Definition: windbgkd.h:508
EXCEPTION_RECORD64 ExceptionRecord
Definition: windbgkd.h:312
NTSTATUS ExceptionCode
Definition: rtltypes.h:190
ULONG64 ExceptionInformation[EXCEPTION_MAXIMUM_PARAMETERS]
Definition: rtltypes.h:196
ULONG64 ExceptionAddress
Definition: rtltypes.h:193
ULONG Value
Definition: kd.h:110
#define NTAPI
Definition: typedefs.h:36
uint32_t ULONG_PTR
Definition: typedefs.h:65
#define DbgKdLoadSymbolsStateChange
Definition: windbgkd.h:60
#define DbgKdPrintStringApi
Definition: windbgkd.h:122
#define PACKET_TYPE_KD_STATE_CHANGE32
Definition: windbgkd.h:42
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
#define KD_DEBUGGER_NOT_PRESENT
Definition: kdfuncs.h:133

Variable Documentation

◆ DispatchTable

Definition at line 48 of file kdio.c.

◆ InitRoutines

PKDP_INIT_ROUTINE InitRoutines[KdMax]
Initial value:

Definition at line 50 of file kdio.c.

Referenced by KdDebuggerInitialize0().

◆ KdpCurrentPosition

volatile ULONG KdpCurrentPosition = 0
static

Definition at line 31 of file kdio.c.

Referenced by KdpLoggerThread(), and KdpPrintToLogFile().

◆ KdpDebugBuffer

PCHAR KdpDebugBuffer = NULL
static

Definition at line 30 of file kdio.c.

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

◆ KdpDebugLogSpinLock

KSPIN_LOCK KdpDebugLogSpinLock
static

Definition at line 33 of file kdio.c.

Referenced by KdpDebugLogInit(), and KdpPrintToLogFile().

◆ KdpDebugMode

◆ KdpFreeBytes

volatile ULONG KdpFreeBytes = 0
static

Definition at line 32 of file kdio.c.

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

◆ KdpLogFileHandle

HANDLE KdpLogFileHandle
static

Definition at line 35 of file kdio.c.

Referenced by KdpDebugLogInit(), and KdpLoggerThread().

◆ KdpLogFileName

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

Definition at line 36 of file kdio.c.

Referenced by KdpDebugLogInit(), and KdpGetDebugMode().

◆ KdpLoggerThreadEvent

KEVENT KdpLoggerThreadEvent
static

Definition at line 34 of file kdio.c.

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

◆ KdpLoggingEnabled

BOOLEAN KdpLoggingEnabled = FALSE
static

Definition at line 29 of file kdio.c.

Referenced by KdpLoggerThread(), and KdpPrintToLogFile().

◆ KdProviders

LIST_ENTRY KdProviders = {&KdProviders, &KdProviders}

◆ KdpScreenLineBuffer

CHAR KdpScreenLineBuffer[KdpScreenLineLengthDefault+1] = ""
static

Definition at line 43 of file kdio.c.

Referenced by KdpScreenPrint().

◆ KdpScreenLineBufferPos

ULONG KdpScreenLineBufferPos = 0
static

Definition at line 44 of file kdio.c.

Referenced by KdpScreenPrint().

◆ KdpScreenLineLength

ULONG KdpScreenLineLength = 0
static

Definition at line 44 of file kdio.c.

Referenced by KdpScreenPrint().

◆ KdpSerialSpinLock

KSPIN_LOCK KdpSerialSpinLock
static

Definition at line 38 of file kdio.c.

Referenced by KdpSerialInit(), and KdpSerialPrint().

◆ SerialPortInfo

◆ SerialPortNumber

ULONG SerialPortNumber = DEFAULT_DEBUG_PORT

Definition at line 39 of file kdio.c.

Referenced by KdpGetDebugMode(), and KdpSerialInit().