ReactOS  0.4.14-dev-838-g99f979d
kdprint.c File Reference
#include <ntoskrnl.h>
#include <debug.h>
Include dependency graph for kdprint.c:

Go to the source code of this file.

Macros

#define NDEBUG
 

Functions

BOOLEAN NTAPI KdpPrintString (_In_ PSTRING Output)
 
BOOLEAN NTAPI KdpPromptString (_In_ PSTRING PromptString, _In_ PSTRING ResponseString)
 
USHORT NTAPI KdpPrompt (_In_reads_bytes_(PromptLength) PCHAR PromptString, _In_ USHORT PromptLength, _Out_writes_bytes_(MaximumResponseLength) PCHAR ResponseString, _In_ USHORT MaximumResponseLength, _In_ KPROCESSOR_MODE PreviousMode, _In_ PKTRAP_FRAME TrapFrame, _In_ PKEXCEPTION_FRAME ExceptionFrame)
 
NTSTATUS NTAPI KdpPrint (_In_ ULONG ComponentId, _In_ ULONG Level, _In_reads_bytes_(Length) PCHAR String, _In_ USHORT Length, _In_ KPROCESSOR_MODE PreviousMode, _In_ PKTRAP_FRAME TrapFrame, _In_ PKEXCEPTION_FRAME ExceptionFrame, _Out_ PBOOLEAN Handled)
 
VOID __cdecl KdpDprintf (_In_ PCHAR Format,...)
 

Macro Definition Documentation

◆ NDEBUG

#define NDEBUG

Definition at line 13 of file kdprint.c.

Function Documentation

◆ KdpDprintf()

VOID __cdecl KdpDprintf ( _In_ PCHAR  Format,
  ... 
)

Definition at line 416 of file kdprint.c.

419 {
420  STRING String;
421  USHORT Length;
422  va_list ap;
423  CHAR Buffer[100];
424 
425  /* Format the string */
426  va_start(ap, Format);
428  sizeof(Buffer),
429  Format,
430  ap);
431  va_end(ap);
432 
433  /* Set it up */
434  String.Buffer = Buffer;
435  String.Length = String.MaximumLength = Length;
436 
437  /* Send it to the debugger directly */
439 }
char CHAR
Definition: xmlstorage.h:175
static WCHAR String[]
Definition: stringtable.c:55
BOOLEAN NTAPI KdpPrintString(_In_ PSTRING Output)
Definition: kdio.c:590
#define va_end(ap)
Definition: acmsvcex.h:90
char * va_list
Definition: acmsvcex.h:78
Definition: bufpool.h:45
_In_ ULONG _In_ ULONG _In_ ULONG Length
Definition: ntddpcm.h:101
std::wstring STRING
Definition: fontsub.cpp:33
unsigned short USHORT
Definition: pedump.c:61
#define va_start(ap, A)
Definition: acmsvcex.h:91
#define _vsnprintf
Definition: xmlstorage.h:202
void int int ULONGLONG int va_list * ap
Definition: winesup.h:32
IN BOOLEAN OUT PSTR Buffer
Definition: progress.h:34
ACPI_BUFFER *RetBuffer ACPI_BUFFER *RetBuffer char ACPI_WALK_RESOURCE_CALLBACK void *Context ACPI_BUFFER *RetBuffer UINT16 ACPI_RESOURCE **ResourcePtr ACPI_GENERIC_ADDRESS *Reg UINT32 *ReturnValue UINT8 UINT8 *Slp_TypB ACPI_PHYSICAL_ADDRESS PhysicalAddress64 UINT32 UINT32 *TimeElapsed UINT32 ACPI_STATUS const char * Format
Definition: acpixf.h:1212

◆ KdpPrint()

NTSTATUS NTAPI KdpPrint ( _In_ ULONG  ComponentId,
_In_ ULONG  Level,
_In_reads_bytes_(Length) PCHAR  String,
_In_ USHORT  Length,
_In_ KPROCESSOR_MODE  PreviousMode,
_In_ PKTRAP_FRAME  TrapFrame,
_In_ PKEXCEPTION_FRAME  ExceptionFrame,
_Out_ PBOOLEAN  Handled 
)

Definition at line 331 of file kdprint.c.

340 {
342  BOOLEAN Enable;
343  STRING OutputString;
344  CHAR CapturedString[512];
345 
347  {
348  /* Mask validation failed */
349  *Handled = TRUE;
350  return STATUS_SUCCESS;
351  }
352 
353  /* Assume failure */
354  *Handled = FALSE;
355 
356  /* Normalize the length */
357  Length = min(Length, sizeof(CapturedString));
358 
359  /* Check if we need to verify the string */
360  if (PreviousMode != KernelMode)
361  {
362  /* Capture user-mode buffers */
363  _SEH2_TRY
364  {
365  /* Probe and capture the string */
367  KdpMoveMemory(CapturedString, String, Length);
368  String = CapturedString;
369  }
371  {
372  /* Bad string pointer, bail out */
374  }
375  _SEH2_END;
376  }
377 
378  /* Setup the output string */
379  OutputString.Buffer = String;
380  OutputString.Length = OutputString.MaximumLength = Length;
381 
382  /* Log the print */
383  //KdLogDbgPrint(&OutputString);
384 
385  /* Check for a debugger */
387  {
388  /* Fail */
389  *Handled = TRUE;
391  }
392 
393  /* Enter the debugger */
394  Enable = KdEnterDebugger(TrapFrame, ExceptionFrame);
395 
396  /* Print the string */
397  if (KdpPrintString(&OutputString))
398  {
399  /* User pressed CTRL-C, breakpoint on return */
401  }
402  else
403  {
404  /* String was printed */
406  }
407 
408  /* Exit the debugger and return */
410  *Handled = TRUE;
411  return Status;
412 }
#define TRUE
Definition: types.h:120
VOID NTAPI KdpMoveMemory(_In_ PVOID Destination, _In_ PVOID Source, _In_ SIZE_T Length)
Definition: kdapi.c:22
char CHAR
Definition: xmlstorage.h:175
LONG NTSTATUS
Definition: precomp.h:26
_In_ ULONGLONG _In_ ULONGLONG _In_ BOOLEAN Enable
Definition: ntddpcm.h:140
_IRQL_requires_same_ typedef _In_ ULONG _In_ UCHAR Level
Definition: wmitypes.h:55
ACPI_BUFFER *RetBuffer ACPI_BUFFER *RetBuffer char ACPI_WALK_RESOURCE_CALLBACK void *Context ACPI_BUFFER *RetBuffer UINT16 ACPI_RESOURCE **ResourcePtr ACPI_GENERIC_ADDRESS *Reg UINT32 *ReturnValue UINT8 UINT8 *Slp_TypB ACPI_PHYSICAL_ADDRESS PhysicalAddress64 UINT32 UINT32 *TimeElapsed UINT32 ACPI_STATUS const char UINT32 ACPI_STATUS const char UINT32 const char const char UINT32 ComponentId
Definition: acpixf.h:1270
static WCHAR String[]
Definition: stringtable.c:55
VOID NTAPI KdExitDebugger(IN BOOLEAN Enable)
Definition: kdmain.c:317
_SEH2_TRY
Definition: create.c:4250
BOOLEAN NTAPI KdpPrintString(_In_ PSTRING Output)
Definition: kdio.c:590
NTSTATUS NTAPI NtQueryDebugFilterState(_In_ ULONG ComponentId, _In_ ULONG Level)
Definition: kdapi.c:2334
#define EXCEPTION_EXECUTE_HANDLER
Definition: excpt.h:85
unsigned char BOOLEAN
#define STATUS_BREAKPOINT
Definition: ntstatus.h:172
#define STATUS_DEVICE_NOT_CONNECTED
Definition: udferr_usr.h:160
_In_ KPROCESSOR_MODE PreviousMode
Definition: sefuncs.h:103
#define _SEH2_YIELD(STMT_)
Definition: pseh2_64.h:8
_In_ BOOLEAN Handled
Definition: ketypes.h:337
_In_ ULONG _In_ ULONG _In_ ULONG Length
Definition: ntddpcm.h:101
std::wstring STRING
Definition: fontsub.cpp:33
VOID NTAPI ProbeForRead(IN CONST VOID *Address, IN SIZE_T Length, IN ULONG Alignment)
Definition: exintrin.c:102
Status
Definition: gdiplustypes.h:24
_SEH2_END
Definition: create.c:4424
#define STATUS_ACCESS_VIOLATION
Definition: ntstatus.h:228
#define min(a, b)
Definition: monoChain.cc:55
BOOLEAN NTAPI KdEnterDebugger(IN PKTRAP_FRAME TrapFrame, IN PKEXCEPTION_FRAME ExceptionFrame)
Definition: kdmain.c:309
#define _SEH2_EXCEPT(...)
Definition: pseh2_64.h:6
return STATUS_SUCCESS
Definition: btrfs.c:2938
BOOLEAN KdDebuggerNotPresent
Definition: kdmain.c:50

Referenced by KdpEnterDebuggerException(), KdpServiceDispatcher(), and KdpTrap().

◆ KdpPrintString()

BOOLEAN NTAPI KdpPrintString ( _In_ PSTRING  Output)

Definition at line 590 of file kdio.c.

592 {
593  PLIST_ENTRY CurrentEntry;
594  PKD_DISPATCH_TABLE CurrentTable;
595 
596  if (!KdpDebugMode.Value) return FALSE;
597 
598  /* Call the registered handlers */
599  CurrentEntry = KdProviders.Flink;
600  while (CurrentEntry != &KdProviders)
601  {
602  /* Get the current table */
603  CurrentTable = CONTAINING_RECORD(CurrentEntry,
604  KD_DISPATCH_TABLE,
605  KdProvidersList);
606 
607  /* Call it */
608  CurrentTable->KdpPrintRoutine(Output->Buffer, Output->Length);
609 
610  /* Next Table */
611  CurrentEntry = CurrentEntry->Flink;
612  }
613 
614  /* Call the Wrapper Routine */
615  if (WrapperTable.KdpPrintRoutine)
616  WrapperTable.KdpPrintRoutine(Output->Buffer, Output->Length);
617 
618  return FALSE;
619 }
KDP_DEBUG_MODE KdpDebugMode
Definition: kdinit.c:25
LIST_ENTRY KdProviders
Definition: kdinit.c:29
PFLT_MESSAGE_WAITER_QUEUE CONTAINING_RECORD(Csq, DEVICE_EXTENSION, IrpQueue)) -> WaiterQ.mLock) _IRQL_raises_(DISPATCH_LEVEL) VOID NTAPI FltpAcquireMessageWaiterLock(_In_ PIO_CSQ Csq, _Out_ PKIRQL Irql)
Definition: Messaging.c:560
struct _LIST_ENTRY * Flink
Definition: typedefs.h:119
Definition: arc.h:85
Definition: typedefs.h:117
KD_DISPATCH_TABLE WrapperTable
Definition: kdinit.c:28

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

◆ KdpPrompt()

USHORT NTAPI KdpPrompt ( _In_reads_bytes_(PromptLength) PCHAR  PromptString,
_In_ USHORT  PromptLength,
_Out_writes_bytes_(MaximumResponseLength) PCHAR  ResponseString,
_In_ USHORT  MaximumResponseLength,
_In_ KPROCESSOR_MODE  PreviousMode,
_In_ PKTRAP_FRAME  TrapFrame,
_In_ PKEXCEPTION_FRAME  ExceptionFrame 
)

Definition at line 234 of file kdprint.c.

242 {
243  STRING PromptBuffer, ResponseBuffer;
244  BOOLEAN Enable, Resend;
245  PCHAR SafeResponseString;
246  CHAR CapturedPrompt[512];
247  CHAR SafeResponseBuffer[512];
248 
249  /* Normalize the lengths */
250  PromptLength = min(PromptLength,
251  sizeof(CapturedPrompt));
253  sizeof(SafeResponseBuffer));
254 
255  /* Check if we need to verify the string */
256  if (PreviousMode != KernelMode)
257  {
258  /* Handle user-mode buffers safely */
259  _SEH2_TRY
260  {
261  /* Probe and capture the prompt */
262  ProbeForRead(PromptString, PromptLength, 1);
263  KdpMoveMemory(CapturedPrompt, PromptString, PromptLength);
264  PromptString = CapturedPrompt;
265 
266  /* Probe and make room for the response */
267  ProbeForWrite(ResponseString, MaximumResponseLength, 1);
268  SafeResponseString = SafeResponseBuffer;
269  }
271  {
272  /* Bad string pointer, bail out */
273  _SEH2_YIELD(return 0);
274  }
275  _SEH2_END;
276  }
277  else
278  {
279  SafeResponseString = ResponseString;
280  }
281 
282  /* Setup the prompt and response buffers */
283  PromptBuffer.Buffer = PromptString;
284  PromptBuffer.Length = PromptBuffer.MaximumLength = PromptLength;
285  ResponseBuffer.Buffer = SafeResponseString;
286  ResponseBuffer.Length = 0;
287  ResponseBuffer.MaximumLength = MaximumResponseLength;
288 
289  /* Log the print */
290  //KdLogDbgPrint(&PromptBuffer);
291 
292  /* Enter the debugger */
293  Enable = KdEnterDebugger(TrapFrame, ExceptionFrame);
294 
295  /* Enter prompt loop */
296  do
297  {
298  /* Send the prompt and receive the response */
299  Resend = KdpPromptString(&PromptBuffer, &ResponseBuffer);
300 
301  /* Loop while we need to resend */
302  } while (Resend);
303 
304  /* Exit the debugger */
306 
307  /* Copy back the response if required */
308  if (PreviousMode != KernelMode)
309  {
310  _SEH2_TRY
311  {
312  /* Safely copy back the response to user mode */
313  KdpMoveMemory(ResponseString,
314  ResponseBuffer.Buffer,
315  ResponseBuffer.Length);
316  }
318  {
319  /* String became invalid after we exited, fail */
320  _SEH2_YIELD(return 0);
321  }
322  _SEH2_END;
323  }
324 
325  /* Return the number of characters received */
326  return ResponseBuffer.Length;
327 }
signed char * PCHAR
Definition: retypes.h:7
VOID NTAPI KdpMoveMemory(_In_ PVOID Destination, _In_ PVOID Source, _In_ SIZE_T Length)
Definition: kdapi.c:22
char CHAR
Definition: xmlstorage.h:175
_In_ ULONGLONG _In_ ULONGLONG _In_ BOOLEAN Enable
Definition: ntddpcm.h:140
BOOLEAN NTAPI KdpPromptString(_In_ PSTRING PromptString, _In_ PSTRING ResponseString)
Definition: kdio.c:625
VOID NTAPI KdExitDebugger(IN BOOLEAN Enable)
Definition: kdmain.c:317
VOID NTAPI ProbeForWrite(IN PVOID Address, IN SIZE_T Length, IN ULONG Alignment)
Definition: exintrin.c:143
_SEH2_TRY
Definition: create.c:4250
#define EXCEPTION_EXECUTE_HANDLER
Definition: excpt.h:85
unsigned char BOOLEAN
_In_ ULONG MaximumResponseLength
Definition: kdfuncs.h:11
_In_ KPROCESSOR_MODE PreviousMode
Definition: sefuncs.h:103
#define _SEH2_YIELD(STMT_)
Definition: pseh2_64.h:8
std::wstring STRING
Definition: fontsub.cpp:33
VOID NTAPI ProbeForRead(IN CONST VOID *Address, IN SIZE_T Length, IN ULONG Alignment)
Definition: exintrin.c:102
_SEH2_END
Definition: create.c:4424
#define min(a, b)
Definition: monoChain.cc:55
BOOLEAN NTAPI KdEnterDebugger(IN PKTRAP_FRAME TrapFrame, IN PKEXCEPTION_FRAME ExceptionFrame)
Definition: kdmain.c:309
#define _SEH2_EXCEPT(...)
Definition: pseh2_64.h:6

Referenced by KdpEnterDebuggerException(), and KdpTrap().

◆ KdpPromptString()

BOOLEAN NTAPI KdpPromptString ( _In_ PSTRING  PromptString,
_In_ PSTRING  ResponseString 
)

Definition at line 625 of file kdio.c.

628 {
629  KIRQL OldIrql;
630  STRING StringChar;
631  CHAR Response;
632  USHORT i;
633  ULONG DummyScanCode;
634 
635  StringChar.Buffer = &Response;
636  StringChar.Length = StringChar.MaximumLength = sizeof(Response);
637 
638  /* Display the string and print a new line for log neatness */
639  KdpPrintString(PromptString);
640  *StringChar.Buffer = '\n';
641  KdpPrintString(&StringChar);
642 
643  /* Print the kdb prompt */
645 
646  // TODO: Use an improved KdbpReadCommand() function for our purposes.
647 
648  /* Acquire the printing spinlock without waiting at raised IRQL */
650 
652  KbdDisableMouse();
653 
654  /* Loop the whole string */
655  for (i = 0; i < ResponseString->MaximumLength; i++)
656  {
657  /* Check if this is serial debugging mode */
659  {
660  /* Get the character from serial */
661  do
662  {
664  } while (Response == -1);
665  }
666  else
667  {
668  /* Get the response from the keyboard */
669  do
670  {
671  Response = KdbpTryGetCharKeyboard(&DummyScanCode, MAXULONG);
672  } while (Response == -1);
673  }
674 
675  /* Check for return */
676  if (Response == '\r')
677  {
678  /*
679  * We might need to discard the next '\n'.
680  * Wait a bit to make sure we receive it.
681  */
683 
684  /* Check the mode */
686  {
687  /* Read and discard the next character, if any */
689  }
690  else
691  {
692  /* Read and discard the next character, if any */
693  KdbpTryGetCharKeyboard(&DummyScanCode, 5);
694  }
695 
696  /*
697  * Null terminate the output string -- documentation states that
698  * DbgPrompt does not null terminate, but it does
699  */
700  *(PCHAR)(ResponseString->Buffer + i) = 0;
701  break;
702  }
703 
704  /* Write it back and print it to the log */
705  *(PCHAR)(ResponseString->Buffer + i) = Response;
707  KdpPrintString(&StringChar);
709  }
710 
711  /* Return the length */
712  ResponseString->Length = i;
713 
715  KbdEnableMouse();
716 
717  /* Release the spinlock */
719 
720  /* Print a new line */
721  *StringChar.Buffer = '\n';
722  KdpPrintString(&StringChar);
723 
724  /* Success; we don't need to resend */
725  return FALSE;
726 }
ULONG KdbDebugState
Definition: kdb.c:45
char CHAR
Definition: xmlstorage.h:175
KIRQL NTAPI KdpAcquireLock(IN PKSPIN_LOCK SpinLock)
Definition: kdio.c:116
CHAR KdbpTryGetCharSerial(ULONG Retry)
Definition: kdb_serial.c:19
CHAR KdbpTryGetCharKeyboard(PULONG ScanCode, ULONG Retry)
Definition: kdb_keyboard.c:104
UCHAR KIRQL
Definition: env_spec_w32.h:591
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint i
Definition: glfuncs.h:248
STRING KdbPromptString
Definition: kdb_cli.c:136
VOID KbdEnableMouse(VOID)
#define PCHAR
Definition: match.c:90
VOID KbdDisableMouse(VOID)
VOID NTAPI KdpReleaseLock(IN PKSPIN_LOCK SpinLock, IN KIRQL OldIrql)
Definition: kdio.c:142
Definition: ncftp.h:89
std::wstring STRING
Definition: fontsub.cpp:33
_Requires_lock_held_ Interrupt _Releases_lock_ Interrupt _In_ _IRQL_restores_ KIRQL OldIrql
Definition: kefuncs.h:790
#define MAXULONG
Definition: typedefs.h:250
BOOLEAN NTAPI KdpPrintString(_In_ PSTRING Output)
Definition: kdio.c:590
unsigned short USHORT
Definition: pedump.c:61
static KSPIN_LOCK KdpSerialSpinLock
Definition: kdio.c:29
unsigned int ULONG
Definition: retypes.h:1
struct Response Response
VOID NTAPI KeStallExecutionProcessor(IN ULONG MicroSeconds)
Definition: ntoskrnl.c:99

Referenced by KdpPrompt().