ReactOS  0.4.15-dev-2979-gfd8baca
kdtrap.c File Reference
#include <ntoskrnl.h>
#include <debug.h>
Include dependency graph for kdtrap.c:

Go to the source code of this file.

Macros

#define NDEBUG
 

Functions

BOOLEAN NTAPI KdpReport (IN PKTRAP_FRAME TrapFrame, IN PKEXCEPTION_FRAME ExceptionFrame, IN PEXCEPTION_RECORD ExceptionRecord, IN PCONTEXT ContextRecord, IN KPROCESSOR_MODE PreviousMode, IN BOOLEAN SecondChanceException)
 
BOOLEAN NTAPI KdpTrap (IN PKTRAP_FRAME TrapFrame, IN PKEXCEPTION_FRAME ExceptionFrame, IN PEXCEPTION_RECORD ExceptionRecord, IN PCONTEXT ContextRecord, IN KPROCESSOR_MODE PreviousMode, IN BOOLEAN SecondChanceException)
 
BOOLEAN NTAPI KdpStub (IN PKTRAP_FRAME TrapFrame, IN PKEXCEPTION_FRAME ExceptionFrame, IN PEXCEPTION_RECORD ExceptionRecord, IN PCONTEXT ContextRecord, IN KPROCESSOR_MODE PreviousMode, IN BOOLEAN SecondChanceException)
 
BOOLEAN NTAPI KdIsThisAKdTrap (IN PEXCEPTION_RECORD ExceptionRecord, IN PCONTEXT Context, IN KPROCESSOR_MODE PreviousMode)
 

Macro Definition Documentation

◆ NDEBUG

#define NDEBUG

Definition at line 13 of file kdtrap.c.

Function Documentation

◆ KdIsThisAKdTrap()

BOOLEAN NTAPI KdIsThisAKdTrap ( IN PEXCEPTION_RECORD  ExceptionRecord,
IN PCONTEXT  Context,
IN KPROCESSOR_MODE  PreviousMode 
)

Definition at line 325 of file kdtrap.c.

328 {
329 #ifdef _WINKD_
330  /*
331  * Determine if this is a valid debug service call and make sure that
332  * it isn't a software breakpoint
333  */
334  if ((ExceptionRecord->ExceptionCode == STATUS_BREAKPOINT) &&
335  (ExceptionRecord->NumberParameters > 0) &&
336  (ExceptionRecord->ExceptionInformation[0] != BREAKPOINT_BREAK))
337  {
338  /* Then we have to handle it */
339  return TRUE;
340  }
341  else
342  {
343  /* We don't have to handle it */
344  return FALSE;
345  }
346 #else
347  /* KDBG has its own mechanism for ignoring user mode exceptions */
348  return FALSE;
349 #endif
350 }
#define TRUE
Definition: types.h:120
#define FALSE
Definition: types.h:117
#define STATUS_BREAKPOINT
Definition: ntstatus.h:184
#define BREAKPOINT_BREAK
Definition: kdtypes.h:50

Referenced by KiDispatchException().

◆ KdpReport()

BOOLEAN NTAPI KdpReport ( IN PKTRAP_FRAME  TrapFrame,
IN PKEXCEPTION_FRAME  ExceptionFrame,
IN PEXCEPTION_RECORD  ExceptionRecord,
IN PCONTEXT  ContextRecord,
IN KPROCESSOR_MODE  PreviousMode,
IN BOOLEAN  SecondChanceException 
)

Definition at line 52 of file kdtrap.c.

58 {
60  PKPRCB Prcb;
62 
63  /*
64  * Determine whether to pass the exception to the debugger.
65  * First, check if this is a "debug exception", meaning breakpoint
66  * (including debug service), single step and assertion failure exceptions.
67  */
68  ExceptionCode = ExceptionRecord->ExceptionCode;
72  {
73  /* This is a debug exception; we always pass them to the debugger */
74  }
76  {
77  /*
78  * Not a debug exception, but the stop-on-exception flag is set,
79  * meaning the debugger requests that we pass it first chance
80  * exceptions. However, some exceptions are always passed to the
81  * exception handler first, namely exceptions with a code that isn't
82  * an error or warning code, and also exceptions with the special
83  * STATUS_PORT_DISCONNECTED code (an error code).
84  */
85  if ((SecondChanceException == FALSE) &&
88  {
89  /* Let the exception handler, if any, try to handle it */
90  return FALSE;
91  }
92  }
93  else if (SecondChanceException == FALSE)
94  {
95  /*
96  * This isn't a debug exception and the stop-on-exception flag isn't set,
97  * so don't bother handling it
98  */
99  return FALSE;
100  }
101 
102  /* Enter the debugger */
103  Enable = KdEnterDebugger(TrapFrame, ExceptionFrame);
104 
105  /*
106  * Get the KPRCB and save the CPU Control State manually instead of
107  * using KiSaveProcessorState, since we already have a valid CONTEXT.
108  */
109  Prcb = KeGetCurrentPrcb();
113  sizeof(CONTEXT));
114 
115  /* Report the new state */
116  Handled = KdpReportExceptionStateChange(ExceptionRecord,
117  &Prcb->ProcessorState.
118  ContextFrame,
119  SecondChanceException);
120 
121  /* Now restore the processor state, manually again. */
124  sizeof(CONTEXT));
126 
127  /* Exit the debugger and clear the CTRL-C state */
130  return Handled;
131 }
_Inout_ PIRP _In_ NTSTATUS ExceptionCode
Definition: cdprocs.h:1772
VOID NTAPI KdpMoveMemory(_In_ PVOID Destination, _In_ PVOID Source, _In_ SIZE_T Length)
Definition: kdapi.c:22
LONG NTSTATUS
Definition: precomp.h:26
_In_ ULONGLONG _In_ ULONGLONG _In_ BOOLEAN Enable
Definition: ntddpcm.h:140
#define STATUS_SINGLE_STEP
Definition: ntstatus.h:185
FORCEINLINE struct _KPRCB * KeGetCurrentPrcb(VOID)
Definition: ketypes.h:1079
VOID NTAPI KdExitDebugger(IN BOOLEAN Enable)
Definition: kdapi.c:1924
BOOLEAN KdpControlCPressed
Definition: kddata.c:68
#define FALSE
Definition: types.h:117
CONTEXT ContextFrame
Definition: ketypes.h:535
unsigned char BOOLEAN
#define STATUS_BREAKPOINT
Definition: ntstatus.h:184
VOID NTAPI KiRestoreProcessorControlState(IN PKPROCESSOR_STATE ProcessorState)
#define STATUS_ASSERTION_FAILURE
Definition: ntstatus.h:960
_In_ BOOLEAN Handled
Definition: ketypes.h:337
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
KPROCESSOR_STATE ProcessorState
Definition: ketypes.h:583
_IRQL_requires_same_ _In_ PVOID _Inout_ struct _CONTEXT * ContextRecord
Definition: ntbasedef.h:654
VOID NTAPI KiSaveProcessorControlState(OUT PKPROCESSOR_STATE ProcessorState)
Definition: cpu.c:362
#define STATUS_PORT_DISCONNECTED
Definition: ntstatus.h:291
BOOLEAN NTAPI KdpReportExceptionStateChange(IN PEXCEPTION_RECORD ExceptionRecord, IN OUT PCONTEXT Context, IN BOOLEAN SecondChanceException)
Definition: kdapi.c:1730
BOOLEAN NTAPI KdEnterDebugger(IN PKTRAP_FRAME TrapFrame, IN PKEXCEPTION_FRAME ExceptionFrame)
Definition: kdapi.c:1871
#define FLG_STOP_ON_EXCEPTION
Definition: pstypes.h:56
ULONG NtGlobalFlag
Definition: init.c:52

Referenced by KdpTrap().

◆ KdpStub()

BOOLEAN NTAPI KdpStub ( IN PKTRAP_FRAME  TrapFrame,
IN PKEXCEPTION_FRAME  ExceptionFrame,
IN PEXCEPTION_RECORD  ExceptionRecord,
IN PCONTEXT  ContextRecord,
IN KPROCESSOR_MODE  PreviousMode,
IN BOOLEAN  SecondChanceException 
)

Definition at line 274 of file kdtrap.c.

280 {
281  ULONG_PTR ExceptionCommand;
282 
283  /* Check if this was a breakpoint due to DbgPrint or Load/UnloadSymbols */
284  ExceptionCommand = ExceptionRecord->ExceptionInformation[0];
285  if ((ExceptionRecord->ExceptionCode == STATUS_BREAKPOINT) &&
286  (ExceptionRecord->NumberParameters > 0) &&
287  ((ExceptionCommand == BREAKPOINT_LOAD_SYMBOLS) ||
288  (ExceptionCommand == BREAKPOINT_UNLOAD_SYMBOLS) ||
289  (ExceptionCommand == BREAKPOINT_COMMAND_STRING) ||
290  (ExceptionCommand == BREAKPOINT_PRINT)))
291  {
292  /* This we can handle: simply bump the Program Counter */
295  return TRUE;
296  }
297  else if (KdPitchDebugger)
298  {
299  /* There's no debugger, fail. */
300  return FALSE;
301  }
302  else if ((KdAutoEnableOnEvent) &&
304  !(KdDebuggerEnabled) &&
307  {
308  /* Debugging was Auto-Enabled. We can now send this to KD. */
309  return KdpTrap(TrapFrame,
310  ExceptionFrame,
311  ExceptionRecord,
313  PreviousMode,
314  SecondChanceException);
315  }
316  else
317  {
318  /* FIXME: All we can do in this case is trace this exception */
319  return FALSE;
320  }
321 }
BOOLEAN KdDebuggerEnabled
Definition: kddata.c:83
BOOLEAN NTAPI KdpTrap(IN PKTRAP_FRAME TrapFrame, IN PKEXCEPTION_FRAME ExceptionFrame, IN PEXCEPTION_RECORD ExceptionRecord, IN PCONTEXT ContextRecord, IN KPROCESSOR_MODE PreviousMode, IN BOOLEAN SecondChanceException)
Definition: kdtrap.c:135
#define TRUE
Definition: types.h:120
BOOLEAN KdAutoEnableOnEvent
Definition: kddata.c:84
#define KeGetContextPc(Context)
Definition: ke.h:31
BOOLEAN KdPreviouslyEnabled
Definition: kddata.c:87
BOOLEAN KdPitchDebugger
Definition: kddata.c:81
uint32_t ULONG_PTR
Definition: typedefs.h:65
NTSTATUS NTAPI KdEnableDebugger(VOID)
Definition: kdapi.c:2130
#define FALSE
Definition: types.h:117
#define BREAKPOINT_UNLOAD_SYMBOLS
Definition: kdtypes.h:54
#define STATUS_BREAKPOINT
Definition: ntstatus.h:184
_In_ KPROCESSOR_MODE PreviousMode
Definition: sefuncs.h:103
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
#define KeSetContextPc(Context, ProgramCounter)
Definition: ke.h:34
_IRQL_requires_same_ _In_ PVOID _Inout_ struct _CONTEXT * ContextRecord
Definition: ntbasedef.h:654
#define BREAKPOINT_PRINT
Definition: kdtypes.h:51
#define BREAKPOINT_LOAD_SYMBOLS
Definition: kdtypes.h:53
#define BREAKPOINT_COMMAND_STRING
Definition: kdtypes.h:55
#define KD_BREAKPOINT_SIZE
Definition: ke.h:118

Referenced by KdDisableDebuggerWithLock(), and KdInitSystem().

◆ KdpTrap()

BOOLEAN NTAPI KdpTrap ( IN PKTRAP_FRAME  TrapFrame,
IN PKEXCEPTION_FRAME  ExceptionFrame,
IN PEXCEPTION_RECORD  ExceptionRecord,
IN PCONTEXT  ContextRecord,
IN KPROCESSOR_MODE  PreviousMode,
IN BOOLEAN  SecondChanceException 
)

Definition at line 135 of file kdtrap.c.

141 {
142  BOOLEAN Unload;
143  ULONG_PTR ProgramCounter;
145  NTSTATUS ReturnStatus;
148 
149  /* Raise if we have to. */
152 
153  /*
154  * Check if we got a STATUS_BREAKPOINT with a SubID for Print, Prompt or
155  * Load/Unload symbols. Make sure it isn't a software breakpoints as those
156  * are handled by KdpReport.
157  */
158  if ((ExceptionRecord->ExceptionCode == STATUS_BREAKPOINT) &&
159  (ExceptionRecord->ExceptionInformation[0] != BREAKPOINT_BREAK))
160  {
161  /* Save Program Counter */
162  ProgramCounter = KeGetContextPc(ContextRecord);
163 
164  /* Check what kind of operation was requested from us */
165  Unload = FALSE;
166  switch (ExceptionRecord->ExceptionInformation[0])
167  {
168  /* DbgPrint */
169  case BREAKPOINT_PRINT:
170 
171  /* Call the worker routine */
172  ReturnStatus = KdpPrint((ULONG)KdpGetParameterThree(ContextRecord),
173  (ULONG)KdpGetParameterFour(ContextRecord),
174  (PCHAR)ExceptionRecord->ExceptionInformation[1],
175  (USHORT)ExceptionRecord->ExceptionInformation[2],
176  PreviousMode,
177  TrapFrame,
178  ExceptionFrame,
179  &Handled);
180 
181  /* Update the return value for the caller */
183  break;
184 
185  /* DbgPrompt */
186  case BREAKPOINT_PROMPT:
187 
188  /* Call the worker routine */
189  ReturnLength = KdpPrompt((PCHAR)ExceptionRecord->ExceptionInformation[1],
190  (USHORT)ExceptionRecord->ExceptionInformation[2],
191  (PCHAR)KdpGetParameterThree(ContextRecord),
192  (USHORT)KdpGetParameterFour(ContextRecord),
193  PreviousMode,
194  TrapFrame,
195  ExceptionFrame);
196  Handled = TRUE;
197 
198  /* Update the return value for the caller */
200  break;
201 
202  /* DbgUnLoadImageSymbols */
204 
205  /* Drop into the load case below, with the unload parameter */
206  Unload = TRUE;
207 
208  /* DbgLoadImageSymbols */
210 
211  /* Call the worker routine */
212  KdpSymbol((PSTRING)ExceptionRecord->ExceptionInformation[1],
213  (PKD_SYMBOLS_INFO)ExceptionRecord->ExceptionInformation[2],
214  Unload,
215  PreviousMode,
217  TrapFrame,
218  ExceptionFrame);
219  Handled = TRUE;
220  break;
221 
222  /* DbgCommandString */
224 
225  /* Call the worker routine */
226  KdpCommandString((PSTRING)ExceptionRecord->ExceptionInformation[1],
227  (PSTRING)ExceptionRecord->ExceptionInformation[2],
228  PreviousMode,
230  TrapFrame,
231  ExceptionFrame);
232  Handled = TRUE;
233  break;
234 
235  /* Anything else, do nothing */
236  default:
237 
238  /* Invalid debug service! Don't handle this! */
239  Handled = FALSE;
240  break;
241  }
242 
243  /*
244  * If the PC was not updated, we'll increment it ourselves so execution
245  * continues past the breakpoint.
246  */
247  if (ProgramCounter == KeGetContextPc(ContextRecord))
248  {
249  /* Update it */
251  ProgramCounter + KD_BREAKPOINT_SIZE);
252  }
253  }
254  else
255  {
256  /* Call the worker routine */
257  Handled = KdpReport(TrapFrame,
258  ExceptionFrame,
259  ExceptionRecord,
261  PreviousMode,
262  SecondChanceException);
263  }
264 
265  if (OldIrql < DISPATCH_LEVEL)
267 
268  /* Return TRUE or FALSE to caller */
269  return Handled;
270 }
#define KeGetCurrentIrql()
Definition: env_spec_w32.h:706
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: kdprint.c:358
signed char * PCHAR
Definition: retypes.h:7
IN CINT OUT PVOID IN ULONG OUT PULONG ReturnLength
Definition: dumpinfo.c:39
IN OUT PLONG IN OUT PLONG Addend IN OUT PLONG IN LONG IN OUT PLONG IN LONG Increment KeRaiseIrqlToDpcLevel
Definition: CrNtStubs.h:67
#define KeLowerIrql(oldIrql)
Definition: env_spec_w32.h:602
VOID NTAPI Unload(PDRIVER_OBJECT DriverObject)
Definition: csqtest.c:160
#define TRUE
Definition: types.h:120
LONG NTSTATUS
Definition: precomp.h:26
#define KeGetContextPc(Context)
Definition: ke.h:31
BOOLEAN NTAPI KdpReport(IN PKTRAP_FRAME TrapFrame, IN PKEXCEPTION_FRAME ExceptionFrame, IN PEXCEPTION_RECORD ExceptionRecord, IN PCONTEXT ContextRecord, IN KPROCESSOR_MODE PreviousMode, IN BOOLEAN SecondChanceException)
Definition: kdtrap.c:52
uint32_t ULONG_PTR
Definition: typedefs.h:65
VOID NTAPI KdpCommandString(IN PSTRING NameString, IN PSTRING CommandString, IN KPROCESSOR_MODE PreviousMode, IN PCONTEXT ContextRecord, IN PKTRAP_FRAME TrapFrame, IN PKEXCEPTION_FRAME ExceptionFrame)
Definition: kdprint.c:137
#define KeSetContextReturnRegister(Context, ReturnValue)
Definition: ke.h:43
UCHAR KIRQL
Definition: env_spec_w32.h:591
#define FALSE
Definition: types.h:117
#define BREAKPOINT_UNLOAD_SYMBOLS
Definition: kdtypes.h:54
unsigned char BOOLEAN
#define STATUS_BREAKPOINT
Definition: ntstatus.h:184
#define BREAKPOINT_PROMPT
Definition: kdtypes.h:52
KIRQL OldIrql
Definition: mm.h:1502
_In_ KPROCESSOR_MODE PreviousMode
Definition: sefuncs.h:103
VOID NTAPI KdpSymbol(IN PSTRING DllPath, IN PKD_SYMBOLS_INFO SymbolInfo, IN BOOLEAN Unload, IN KPROCESSOR_MODE PreviousMode, IN PCONTEXT ContextRecord, IN PKTRAP_FRAME TrapFrame, IN PKEXCEPTION_FRAME ExceptionFrame)
Definition: kdprint.c:176
_In_ BOOLEAN Handled
Definition: ketypes.h:337
#define KeSetContextPc(Context, ProgramCounter)
Definition: ke.h:34
_IRQL_requires_same_ _In_ PVOID _Inout_ struct _CONTEXT * ContextRecord
Definition: ntbasedef.h:654
#define BREAKPOINT_PRINT
Definition: kdtypes.h:51
#define DISPATCH_LEVEL
Definition: env_spec_w32.h:696
unsigned short USHORT
Definition: pedump.c:61
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: kdprint.c:217
#define BREAKPOINT_LOAD_SYMBOLS
Definition: kdtypes.h:53
unsigned int ULONG
Definition: retypes.h:1
#define BREAKPOINT_COMMAND_STRING
Definition: kdtypes.h:55
#define KD_BREAKPOINT_SIZE
Definition: ke.h:118
#define BREAKPOINT_BREAK
Definition: kdtypes.h:50

Referenced by KdInitSystem(), and KdpStub().