ReactOS 0.4.15-dev-6073-g59e7458
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]
 
static ULONG KdbgNextApiNumber = DbgKdContinueApi
 
static CONTEXT KdbgContext
 
static EXCEPTION_RECORD64 KdbgExceptionRecord
 
static BOOLEAN KdbgFirstChanceException
 
static NTSTATUS KdbgContinueStatus = STATUS_SUCCESS
 
const CSTRING KdbPromptStr
 

Macro Definition Documentation

◆ KdpBufferSize

#define KdpBufferSize   (1024 * 512)

Definition at line 25 of file kdio.c.

◆ KdpScreenLineLengthDefault

#define KdpScreenLineLengthDefault   80

Definition at line 39 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 67 of file kdio.c.

69{
71
72 /* Acquire the spinlock without waiting at raised IRQL */
73 while (TRUE)
74 {
75 /* Loop until the spinlock becomes available */
76 while (!KeTestSpinLock(SpinLock));
77
78 /* Spinlock is free, raise IRQL to high level */
80
81 /* Try to get the spinlock */
83 break;
84
85 /* Someone else got the spinlock, lower IRQL back */
87 }
88
89 return OldIrql;
90}
#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 KdbDebugPrint(), KdpPrintToLogFile(), and KdpSerialPrint().

◆ KdbpReleaseLock()

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

Definition at line 94 of file kdio.c.

97{
98 /* Release the spinlock */
100 // KeReleaseSpinLockFromDpcLevel(SpinLock);
101
102 /* Restore the old IRQL */
104}
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 579 of file kdio.c.

582{
583 va_list ap;
585 CHAR Buffer[512];
586
587 /* Format the string */
590 sizeof(Buffer),
591 Format,
592 ap);
593 va_end(ap);
594
595 /* Send it to the display providers */
597}
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:550
_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().

◆ KdIoPrintString()

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

Definition at line 550 of file kdio.c.

553{
554 PLIST_ENTRY CurrentEntry;
555 PKD_DISPATCH_TABLE CurrentTable;
556
557 /* Call the registered providers */
558 for (CurrentEntry = KdProviders.Flink;
559 CurrentEntry != &KdProviders;
560 CurrentEntry = CurrentEntry->Flink)
561 {
562 CurrentTable = CONTAINING_RECORD(CurrentEntry,
564 KdProvidersList);
565
566 CurrentTable->KdpPrintRoutine(String, Length);
567 }
568}
LIST_ENTRY KdProviders
Definition: kdio.c:44
PKDP_PRINT_ROUTINE KdpPrintRoutine
Definition: kd.h:115
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 571 of file kdio.c.

573{
575}
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 200 of file kdio.c.

203{
205
206 if (!KdpDebugMode.File)
208
209 if (BootPhase == 0)
210 {
211 /* Write out the functions that we support for now */
212 DispatchTable->KdpPrintRoutine = KdpPrintToLogFile;
213
214 /* Register for BootPhase 1 initialization and as a Provider */
215 DispatchTable->KdpInitRoutine = KdpDebugLogInit;
216 InsertTailList(&KdProviders, &DispatchTable->KdProvidersList);
217 }
218 else if (BootPhase == 1)
219 {
220 /* Allocate a buffer for debug log */
221 KdpDebugBuffer = ExAllocatePoolZero(NonPagedPool,
223 TAG_KDBG);
224 if (!KdpDebugBuffer)
225 {
227 RemoveEntryList(&DispatchTable->KdProvidersList);
228 return STATUS_NO_MEMORY;
229 }
231
232 /* Initialize spinlock */
234
235 /* Register for later BootPhase 2 reinitialization */
236 DispatchTable->KdpInitRoutine = KdpDebugLogInit;
237
238 /* Announce ourselves */
239 HalDisplayString(" File log debugging enabled\r\n");
240 }
241 else if (BootPhase >= 2)
242 {
246 HANDLE ThreadHandle;
248
249 /* If we have already successfully opened the log file, bail out */
250 if (KdpLogFileHandle != NULL)
251 return STATUS_SUCCESS;
252
253 /* Setup the log name */
255 if (!NT_SUCCESS(Status))
256 goto Failure;
257
259 &FileName,
261 NULL,
262 NULL);
263
264 /* Create the log file */
265 Status = ZwCreateFile(&KdpLogFileHandle,
268 &Iosb,
269 NULL,
275 NULL,
276 0);
277
279
280 if (!NT_SUCCESS(Status))
281 {
282 DPRINT1("Failed to open log file: 0x%08lx\n", Status);
283
284 /* Schedule an I/O reinitialization if needed */
287 {
288 DispatchTable->KdpInitRoutine = KdpDebugLogInit;
289 return Status;
290 }
291 goto Failure;
292 }
293
297 {
299 FILE_POSITION_INFORMATION FilePosInfo;
300
301 Status = ZwQueryInformationFile(KdpLogFileHandle,
302 &Iosb,
303 &FileInfo,
304 sizeof(FileInfo),
306 DPRINT("Status: 0x%08lx - EOF offset: %I64d\n",
307 Status, FileInfo.EndOfFile.QuadPart);
308
309 Status = ZwQueryInformationFile(KdpLogFileHandle,
310 &Iosb,
311 &FilePosInfo,
312 sizeof(FilePosInfo),
314 DPRINT("Status: 0x%08lx - Position: %I64d\n",
315 Status, FilePosInfo.CurrentByteOffset.QuadPart);
316
317 FilePosInfo.CurrentByteOffset.QuadPart = FileInfo.EndOfFile.QuadPart;
318 Status = ZwSetInformationFile(KdpLogFileHandle,
319 &Iosb,
320 &FilePosInfo,
321 sizeof(FilePosInfo),
323 DPRINT("ZwSetInformationFile(FilePositionInfo) returned: 0x%08lx\n", Status);
324 }
328
329 /* Create the logger thread */
330 Status = PsCreateSystemThread(&ThreadHandle,
332 NULL,
333 NULL,
334 NULL,
336 NULL);
337 if (!NT_SUCCESS(Status))
338 {
339 DPRINT1("Failed to create log file thread: 0x%08lx\n", Status);
341 goto Failure;
342 }
343
345 ZwSetInformationThread(ThreadHandle,
347 &Priority,
348 sizeof(Priority));
349
350 ZwClose(ThreadHandle);
351 return Status;
352
353Failure:
354 KdpFreeBytes = 0;
358 RemoveEntryList(&DispatchTable->KdProvidersList);
359 }
360
361 return Status;
362}
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 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 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:896
@ 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:110
static volatile ULONG KdpFreeBytes
Definition: kdio.c:29
static HANDLE KdpLogFileHandle
Definition: kdio.c:32
KDP_DEBUG_MODE KdpDebugMode
Definition: kdio.c:43
#define KdpBufferSize
Definition: kdio.c:25
static PCHAR KdpDebugBuffer
Definition: kdio.c:27
NTSTATUS NTAPI KdpDebugLogInit(_In_ PKD_DISPATCH_TABLE DispatchTable, _In_ ULONG BootPhase)
Definition: kdio.c:200
static KSPIN_LOCK KdpDebugLogSpinLock
Definition: kdio.c:30
static KEVENT KdpLoggerThreadEvent
Definition: kdio.c:31
ANSI_STRING KdpLogFileName
Definition: kdio.c:33
static VOID NTAPI KdpPrintToLogFile(_In_ PCCH String, _In_ ULONG Length)
Definition: kdio.c:159
#define ExFreePoolWithTag(_P, _T)
Definition: module.h:1109
#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_NO_MEMORY
Definition: ntstatus.h:260
#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:71
LARGE_INTEGER CurrentByteOffset
Definition: nt_native.h:955
UCHAR File
Definition: kd.h:102
#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 110 of file kdio.c.

111{
112 ULONG beg, end, num;
114
116
118
119 while (TRUE)
120 {
122
123 /* Bug */
124 /* Keep KdpCurrentPosition and KdpFreeBytes values in local
125 * variables to avoid their possible change from Producer part,
126 * KdpPrintToLogFile function
127 */
130
131 /* Now securely calculate values, based on local variables */
132 beg = (end + num) % KdpBufferSize;
134
135 /* Nothing to do? */
136 if (num == 0)
137 continue;
138
139 if (end > beg)
140 {
142 KdpDebugBuffer + beg, num, NULL, NULL);
143 }
144 else
145 {
147 KdpDebugBuffer + beg, KdpBufferSize - beg, NULL, NULL);
148
151 }
152
154 }
155}
#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:26
static volatile ULONG KdpCurrentPosition
Definition: kdio.c:28
#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)
@ Executive
Definition: ketypes.h:403

Referenced by KdpDebugLogInit().

◆ KdpPrintToLogFile()

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

Definition at line 159 of file kdio.c.

162{
164 ULONG beg, end, num;
165
166 if (KdpDebugBuffer == NULL) return;
167
168 /* Acquire the printing spinlock without waiting at raised IRQL */
170
171 beg = KdpCurrentPosition;
173 if (num != 0)
174 {
175 end = (beg + num) % KdpBufferSize;
177 KdpFreeBytes -= num;
178
179 if (end > beg)
180 {
182 }
183 else
184 {
187 }
188 }
189
190 /* Release the spinlock */
192
193 /* Signal the logger thread */
196}
#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:94
KIRQL NTAPI KdbpAcquireLock(_In_ PKSPIN_LOCK SpinLock)
Definition: kdio.c:67
#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 434 of file kdio.c.

435{
436 if (InbvIsBootDriverInstalled() /* &&
437 !InbvCheckDisplayOwnership() */)
438 {
439 /* Acquire ownership and reset the display */
447 }
448}
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()

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

Definition at line 518 of file kdio.c.

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

Referenced by KdpScreenInit().

◆ KdpScreenPrint()

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

Definition at line 466 of file kdio.c.

469{
470 PCCH pch = String;
471
472 while (pch < String + Length && *pch)
473 {
474 if (*pch == '\b')
475 {
476 /* HalDisplayString does not support '\b'. Workaround it and use '\r' */
477 if (KdpScreenLineLength > 0)
478 {
479 /* Remove last character from buffer */
482
483 /* Clear row and print line again */
484 HalDisplayString("\r");
486 }
487 }
488 else
489 {
492 }
493
495 {
496 /* Print buffered characters */
499
500 /* Clear line buffer */
501 KdpScreenLineBuffer[0] = '\0';
503 }
504
505 ++pch;
506 }
507
508 /* Print buffered characters */
510 {
513 }
514}
static ULONG KdpScreenLineBufferPos
Definition: kdio.c:41
static CHAR KdpScreenLineBuffer[KdpScreenLineLengthDefault+1]
Definition: kdio.c:40
static ULONG KdpScreenLineLength
Definition: kdio.c:41
#define KdpScreenLineLengthDefault
Definition: kdio.c:39
#define pch(ap)
Definition: match.c:418
CONST CHAR * PCCH
Definition: ntbasedef.h:392

Referenced by KdpScreenInit().

◆ KdpScreenRelease()

VOID KdpScreenRelease ( VOID  )

Definition at line 453 of file kdio.c.

454{
457 {
458 /* Release the display */
459 // InbvSetDisplayOwnership(FALSE);
461 }
462}
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()

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

Definition at line 395 of file kdio.c.

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

Referenced by KdpSerialInit().

◆ KdpSerialPrint()

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

Definition at line 368 of file kdio.c.

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

707{
708#ifdef KDBG
709 STRING ResponseString;
710 PDBGKD_DEBUG_IO DebugIo;
711 CHAR MessageBuffer[512];
712#endif
713
714 if (PacketType == PACKET_TYPE_KD_STATE_MANIPULATE)
715 {
716 PDBGKD_MANIPULATE_STATE64 ManipulateState = (PDBGKD_MANIPULATE_STATE64)MessageHeader->Buffer;
717 RtlZeroMemory(MessageHeader->Buffer, MessageHeader->MaximumLength);
719 {
720 ManipulateState->ApiNumber = DbgKdGetContextApi;
721 MessageData->Length = 0;
722 MessageData->Buffer = (PCHAR)&KdbgContext;
723 return KdPacketReceived;
724 }
726 {
727 ManipulateState->ApiNumber = DbgKdSetContextApi;
728 MessageData->Length = sizeof(KdbgContext);
729 MessageData->Buffer = (PCHAR)&KdbgContext;
730 return KdPacketReceived;
731 }
733 {
735 }
736 ManipulateState->ApiNumber = DbgKdContinueApi;
737 ManipulateState->u.Continue.ContinueStatus = KdbgContinueStatus;
738
739 /* Prepare for next time */
742
743 return KdPacketReceived;
744 }
745
746 if (PacketType != PACKET_TYPE_KD_DEBUG_IO)
747 return KdPacketTimedOut;
748
749#ifdef KDBG
750 DebugIo = (PDBGKD_DEBUG_IO)MessageHeader->Buffer;
751
752 /* Validate API call */
753 if (MessageHeader->MaximumLength != sizeof(DBGKD_DEBUG_IO))
754 return KdPacketNeedsResend;
755 if (DebugIo->ApiNumber != DbgKdGetStringApi)
756 return KdPacketNeedsResend;
757
758 /* NOTE: We cannot use directly MessageData->Buffer here as it points
759 * to the temporary KdpMessageBuffer scratch buffer that is being
760 * shared with all the possible I/O KD operations that may happen. */
761 ResponseString.Buffer = MessageBuffer;
762 ResponseString.Length = 0;
763 ResponseString.MaximumLength = min(sizeof(MessageBuffer),
764 MessageData->MaximumLength);
765 ResponseString.MaximumLength = min(ResponseString.MaximumLength,
767
768 /* The prompt string has been printed by KdSendPacket; go to
769 * new line and print the kdb prompt -- for SYSREG2 support. */
770 KdIoPrintString("\n", 1);
771 KdIoPuts(KdbPromptStr.Buffer); // Alternatively, use "Input> "
772
775
776 /*
777 * Read a NULL-terminated line of user input and retrieve its length.
778 * Official documentation states that DbgPrompt() includes a terminating
779 * newline character but does not NULL-terminate. However, experiments
780 * show that this behaviour is left at the discretion of WinDbg itself.
781 * WinDbg NULL-terminates the string unless its buffer is too short,
782 * in which case the string is simply truncated without NULL-termination.
783 */
784 ResponseString.Length =
785 (USHORT)KdIoReadLine(ResponseString.Buffer,
786 ResponseString.MaximumLength);
787
790
791 /* Adjust and return the string length */
792 *DataLength = min(ResponseString.Length + sizeof(ANSI_NULL),
794 MessageData->Length = DebugIo->u.GetString.LengthOfStringRead = *DataLength;
795
796 /* Only now we can copy back the data into MessageData->Buffer */
797 RtlCopyMemory(MessageData->Buffer, ResponseString.Buffer, *DataLength);
798#endif
799
800 return KdPacketReceived;
801}
#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
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
static ULONG KdbgNextApiNumber
Definition: kdio.c:57
static NTSTATUS KdbgContinueStatus
Definition: kdio.c:61
static CONTEXT KdbgContext
Definition: kdio.c:58
const CSTRING KdbPromptStr
Definition: kdb_cli.c:148
VOID KdIoPuts(_In_ PCSTR String)
Definition: kdio.c:571
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 PCHAR
Definition: match.c:90
#define ANSI_NULL
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
union _DBGKD_DEBUG_IO::@3511 u
ULONG ApiNumber
Definition: windbgkd.h:421
ULONG LengthOfStringRead
Definition: windbgkd.h:413
union _DBGKD_MANIPULATE_STATE64::@3522 u
DBGKD_CONTINUE Continue
Definition: windbgkd.h:799
#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
#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 604 of file kdio.c.

609{
610 if (PacketType == PACKET_TYPE_KD_DEBUG_IO)
611 {
612 ULONG ApiNumber = ((PDBGKD_DEBUG_IO)MessageHeader->Buffer)->ApiNumber;
613
614 /* Validate API call */
615 if (MessageHeader->Length != sizeof(DBGKD_DEBUG_IO))
616 return;
617 if ((ApiNumber != DbgKdPrintStringApi) &&
618 (ApiNumber != DbgKdGetStringApi))
619 {
620 return;
621 }
622 if (!MessageData)
623 return;
624
625 /* NOTE: MessageData->Length should be equal to
626 * DebugIo.u.PrintString.LengthOfString, or to
627 * DebugIo.u.GetString.LengthOfPromptString */
628
629 if (!KdpDebugMode.Value)
630 return;
631
632 /* Print the string proper */
633 KdIoPrintString(MessageData->Buffer, MessageData->Length);
634 return;
635 }
636 else if (PacketType == PACKET_TYPE_KD_STATE_CHANGE64)
637 {
638 PDBGKD_ANY_WAIT_STATE_CHANGE WaitStateChange = (PDBGKD_ANY_WAIT_STATE_CHANGE)MessageHeader->Buffer;
639 if (WaitStateChange->NewState == DbgKdLoadSymbolsStateChange)
640 {
641#ifdef KDBG
642 PLDR_DATA_TABLE_ENTRY LdrEntry;
643 /* Load symbols. Currently implemented only for KDBG! */
644 if (KdbpSymFindModule((PVOID)(ULONG_PTR)WaitStateChange->u.LoadSymbols.BaseOfDll, -1, &LdrEntry))
645 {
646 KdbSymProcessSymbols(LdrEntry, !WaitStateChange->u.LoadSymbols.UnloadSymbols);
647 }
648#endif
649 return;
650 }
651 else if (WaitStateChange->NewState == DbgKdExceptionStateChange)
652 {
656 return;
657 }
658 }
659 else if (PacketType == PACKET_TYPE_KD_STATE_MANIPULATE)
660 {
661 PDBGKD_MANIPULATE_STATE64 ManipulateState = (PDBGKD_MANIPULATE_STATE64)MessageHeader->Buffer;
662 if (ManipulateState->ApiNumber == DbgKdGetContextApi)
663 {
664 KD_CONTINUE_TYPE Result;
665
666#ifdef KDBG
667 /* Check if this is an assertion failure */
669 {
670 /* Bump EIP to the instruction following the int 2C */
672 }
673
675 KdbgContext.SegCs & 1,
678#else
679 /* We'll manually dump the stack for the user... */
681 Result = kdHandleException;
682#endif
683 if (Result != kdHandleException)
685 else
688 return;
689 }
690 else if (ManipulateState->ApiNumber == DbgKdSetContextApi)
691 {
693 return;
694 }
695 }
697}
#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:1262
VOID KdbSymProcessSymbols(_Inout_ PLDR_DATA_TABLE_ENTRY LdrEntry, _In_ BOOLEAN Load)
Load symbols from image mapping. If this fails,.
Definition: kdb_symbols.c:297
BOOLEAN KdbpSymFindModule(IN PVOID Address OPTIONAL, IN INT Index OPTIONAL, OUT PLDR_DATA_TABLE_ENTRY *pLdrEntry)
Find a module...
Definition: kdb_symbols.c:76
static BOOLEAN KdbgFirstChanceException
Definition: kdio.c:60
static EXCEPTION_RECORD64 KdbgExceptionRecord
Definition: kdio.c:59
#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::@3514 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
ULONG Value
Definition: kd.h:106
Definition: btrfs_drv.h:1876
uint32_t ULONG_PTR
Definition: typedefs.h:65
#define STATUS_UNSUCCESSFUL
Definition: udferr_usr.h:132
#define DbgKdLoadSymbolsStateChange
Definition: windbgkd.h:60
#define DbgKdPrintStringApi
Definition: windbgkd.h:122
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

Definition at line 45 of file kdio.c.

◆ InitRoutines

PKDP_INIT_ROUTINE InitRoutines[KdMax]
Initial value:

Definition at line 47 of file kdio.c.

Referenced by KdDebuggerInitialize0().

◆ KdbgContext

CONTEXT KdbgContext
static

Definition at line 58 of file kdio.c.

Referenced by KdReceivePacket(), and KdSendPacket().

◆ KdbgContinueStatus

NTSTATUS KdbgContinueStatus = STATUS_SUCCESS
static

Definition at line 61 of file kdio.c.

Referenced by KdReceivePacket(), and KdSendPacket().

◆ KdbgExceptionRecord

EXCEPTION_RECORD64 KdbgExceptionRecord
static

Definition at line 59 of file kdio.c.

Referenced by KdSendPacket().

◆ KdbgFirstChanceException

BOOLEAN KdbgFirstChanceException
static

Definition at line 60 of file kdio.c.

Referenced by KdSendPacket().

◆ KdbgNextApiNumber

ULONG KdbgNextApiNumber = DbgKdContinueApi
static

Definition at line 57 of file kdio.c.

Referenced by KdReceivePacket(), and KdSendPacket().

◆ KdbPromptStr

◆ KdpCurrentPosition

volatile ULONG KdpCurrentPosition = 0
static

Definition at line 28 of file kdio.c.

Referenced by KdpLoggerThread(), and KdpPrintToLogFile().

◆ KdpDebugBuffer

PCHAR KdpDebugBuffer = NULL
static

Definition at line 27 of file kdio.c.

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

◆ KdpDebugLogSpinLock

KSPIN_LOCK KdpDebugLogSpinLock
static

Definition at line 30 of file kdio.c.

Referenced by KdpDebugLogInit(), and KdpPrintToLogFile().

◆ KdpDebugMode

◆ KdpFreeBytes

volatile ULONG KdpFreeBytes = 0
static

Definition at line 29 of file kdio.c.

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

◆ KdpLogFileHandle

HANDLE KdpLogFileHandle
static

Definition at line 32 of file kdio.c.

Referenced by KdpDebugLogInit(), and KdpLoggerThread().

◆ KdpLogFileName

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

Definition at line 33 of file kdio.c.

Referenced by KdpDebugLogInit(), and KdpGetDebugMode().

◆ KdpLoggerThreadEvent

KEVENT KdpLoggerThreadEvent
static

Definition at line 31 of file kdio.c.

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

◆ KdpLoggingEnabled

BOOLEAN KdpLoggingEnabled = FALSE
static

Definition at line 26 of file kdio.c.

Referenced by KdpLoggerThread(), and KdpPrintToLogFile().

◆ KdProviders

LIST_ENTRY KdProviders = {&KdProviders, &KdProviders}

◆ KdpScreenLineBuffer

CHAR KdpScreenLineBuffer[KdpScreenLineLengthDefault+1] = ""
static

Definition at line 40 of file kdio.c.

Referenced by KdpScreenPrint().

◆ KdpScreenLineBufferPos

ULONG KdpScreenLineBufferPos = 0
static

Definition at line 41 of file kdio.c.

Referenced by KdpScreenPrint().

◆ KdpScreenLineLength

ULONG KdpScreenLineLength = 0
static

Definition at line 41 of file kdio.c.

Referenced by KdpScreenPrint().

◆ KdpSerialSpinLock

KSPIN_LOCK KdpSerialSpinLock
static

Definition at line 35 of file kdio.c.

Referenced by KdpSerialInit(), and KdpSerialPrint().

◆ SerialPortInfo

◆ SerialPortNumber

ULONG SerialPortNumber = DEFAULT_DEBUG_PORT

Definition at line 36 of file kdio.c.

Referenced by KdpGetDebugMode(), and KdpSerialInit().