ReactOS  0.4.15-dev-321-g2d9b385
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:591
#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:1216

◆ 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:1274
static WCHAR String[]
Definition: stringtable.c:55
VOID NTAPI KdExitDebugger(IN BOOLEAN Enable)
Definition: kdmain.c:320
_SEH2_TRY
Definition: create.c:4250
BOOLEAN NTAPI KdpPrintString(_In_ PSTRING Output)
Definition: kdio.c:591
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:312
#define _SEH2_EXCEPT(...)
Definition: pseh2_64.h:6
return STATUS_SUCCESS
Definition: btrfs.c:3014
BOOLEAN KdDebuggerNotPresent
Definition: kdmain.c:50

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

◆ KdpPrintString()

BOOLEAN NTAPI KdpPrintString ( _In_ PSTRING  Output)

Definition at line 591 of file kdio.c.

593 {
594  PLIST_ENTRY CurrentEntry;
595  PKD_DISPATCH_TABLE CurrentTable;
596 
597  if (!KdpDebugMode.Value) return FALSE;
598 
599  /* Call the registered handlers */
600  CurrentEntry = KdProviders.Flink;
601  while (CurrentEntry != &KdProviders)
602  {
603  /* Get the current table */
604  CurrentTable = CONTAINING_RECORD(CurrentEntry,
605  KD_DISPATCH_TABLE,
606  KdProvidersList);
607 
608  /* Call it */
609  CurrentTable->KdpPrintRoutine(Output->Buffer, Output->Length);
610 
611  /* Next Table */
612  CurrentEntry = CurrentEntry->Flink;
613  }
614 
615  /* Call the Wrapper Routine */
616  if (WrapperTable.KdpPrintRoutine)
617  WrapperTable.KdpPrintRoutine(Output->Buffer, Output->Length);
618 
619  return FALSE;
620 }
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:120
Definition: arc.h:85
Definition: typedefs.h:118
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:626
VOID NTAPI KdExitDebugger(IN BOOLEAN Enable)
Definition: kdmain.c:320
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:312
#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 626 of file kdio.c.

629 {
630 #ifdef KDBG
631  KIRQL OldIrql;
632  STRING StringChar;
633  CHAR Response;
634  USHORT i;
635  ULONG DummyScanCode;
636 
637  StringChar.Buffer = &Response;
638  StringChar.Length = StringChar.MaximumLength = sizeof(Response);
639 
640  /* Display the string and print a new line for log neatness */
641  KdpPrintString(PromptString);
642  *StringChar.Buffer = '\n';
643  KdpPrintString(&StringChar);
644 
645  /* Print the kdb prompt */
647 
648  // TODO: Use an improved KdbpReadCommand() function for our purposes.
649 
650  /* Acquire the printing spinlock without waiting at raised IRQL */
652 
654  KbdDisableMouse();
655 
656  /* Loop the whole string */
657  for (i = 0; i < ResponseString->MaximumLength; i++)
658  {
659  /* Check if this is serial debugging mode */
661  {
662  /* Get the character from serial */
663  do
664  {
666  } while (Response == -1);
667  }
668  else
669  {
670  /* Get the response from the keyboard */
671  do
672  {
673  Response = KdbpTryGetCharKeyboard(&DummyScanCode, MAXULONG);
674  } while (Response == -1);
675  }
676 
677  /* Check for return */
678  if (Response == '\r')
679  {
680  /*
681  * We might need to discard the next '\n'.
682  * Wait a bit to make sure we receive it.
683  */
685 
686  /* Check the mode */
688  {
689  /* Read and discard the next character, if any */
691  }
692  else
693  {
694  /* Read and discard the next character, if any */
695  KdbpTryGetCharKeyboard(&DummyScanCode, 5);
696  }
697 
698  /*
699  * Null terminate the output string -- documentation states that
700  * DbgPrompt does not null terminate, but it does
701  */
702  *(PCHAR)(ResponseString->Buffer + i) = 0;
703  break;
704  }
705 
706  /* Write it back and print it to the log */
707  *(PCHAR)(ResponseString->Buffer + i) = Response;
709  KdpPrintString(&StringChar);
711  }
712 
713  /* Return the length */
714  ResponseString->Length = i;
715 
717  KbdEnableMouse();
718 
719  /* Release the spinlock */
721 
722  /* Print a new line */
723  *StringChar.Buffer = '\n';
724  KdpPrintString(&StringChar);
725 #endif
726 
727  /* Success; we don't need to resend */
728  return FALSE;
729 }
ULONG KdbDebugState
Definition: kdb.c:45
char CHAR
Definition: xmlstorage.h:175
KIRQL NTAPI KdpAcquireLock(IN PKSPIN_LOCK SpinLock)
Definition: kdio.c:117
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:143
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:591
unsigned short USHORT
Definition: pedump.c:61
static KSPIN_LOCK KdpSerialSpinLock
Definition: kdio.c:30
unsigned int ULONG
Definition: retypes.h:1
struct Response Response
VOID NTAPI KeStallExecutionProcessor(IN ULONG MicroSeconds)
Definition: ntoskrnl.c:95

Referenced by KdpPrompt().