00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
#include "kdp.h"
00025
00026
00027
00028
00029
00030 #define BREAKPOINT_CODE_MASK 0xffff
00031
00032
00033
00034
00035 ULONG
KdpPageInAddress;
00036 WORK_QUEUE_ITEM KdpPageInWorkItem;
00037
00038
00039
00040
00041 extern BOOLEAN
KdpControlCPressed;
00042
00043
00044
00045
#pragma optimize( "", off )
00046
VOID
00047 KdpPageInData (
00048 IN PUCHAR
volatile DataAddress
00049 )
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069 {
00070
if (
MmIsSystemAddressAccessable(DataAddress)) {
00071 UCHAR
c = *DataAddress;
00072 DataAddress = &
c;
00073 }
00074
KdpControlCPending =
TRUE;
00075 }
00076
#pragma optimize( "", on )
00077
00078
00079 BOOLEAN
00080 KdpTrap (
00081 IN PKTRAP_FRAME TrapFrame,
00082 IN PKEXCEPTION_FRAME ExceptionFrame,
00083 IN PEXCEPTION_RECORD ExceptionRecord,
00084 IN PCONTEXT ContextRecord,
00085 IN KPROCESSOR_MODE PreviousMode,
00086 IN BOOLEAN SecondChance
00087 )
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121 {
00122
00123 BOOLEAN Completion;
00124 BOOLEAN Enable;
00125 BOOLEAN UnloadSymbols =
FALSE;
00126 ULONG OldIar;
00127 STRING Input;
00128 STRING Output;
00129 PKPRCB Prcb;
00130
00131
00132
00133
00134
00135
00136 re_enter_debugger:
00137 Enable =
KdEnterDebugger(TrapFrame, ExceptionFrame);
00138 Prcb =
KeGetCurrentPrcb();
00139
KiSaveProcessorState(TrapFrame, ExceptionFrame);
00140
KeFlushCurrentTb();
00141
00142
00143
00144
00145
00146
00147
if ((ExceptionRecord->ExceptionCode == STATUS_BREAKPOINT) &&
00148 ((ExceptionRecord->ExceptionInformation[0] &
BREAKPOINT_CODE_MASK)
00149 >= DEBUG_PRINT_BREAKPOINT)) {
00150
00151
00152
00153
00154
00155
switch (ExceptionRecord->ExceptionInformation[0] &
BREAKPOINT_CODE_MASK) {
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
case DEBUG_PRINT_BREAKPOINT:
00167 ContextRecord->Iar += 4;
00168 Output.Buffer = (PCHAR)ContextRecord->Gpr3;
00169 Output.Length = (
USHORT)ContextRecord->Gpr4;
00170
if (
KdDebuggerNotPresent ==
FALSE) {
00171
if (
KdpPrintString(&Output)) {
00172 ContextRecord->Gpr3 = (ULONG)STATUS_BREAKPOINT;
00173
00174 }
else {
00175 ContextRecord->Gpr3 = (ULONG)STATUS_SUCCESS;
00176 }
00177
00178 }
else {
00179 ContextRecord->Gpr3 = (ULONG)STATUS_DEVICE_NOT_CONNECTED;
00180 }
00181
00182
KiRestoreProcessorState(TrapFrame, ExceptionFrame);
00183
KdExitDebugger(Enable);
00184
return TRUE;
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
case DEBUG_PROMPT_BREAKPOINT:
00196 ContextRecord->Iar += 4;
00197 Output.Buffer = (PCHAR)ContextRecord->Gpr3;
00198 Output.Length = (
USHORT)ContextRecord->Gpr4;
00199 Input.Buffer = (PCHAR)ContextRecord->Gpr5;
00200 Input.MaximumLength = (
USHORT)ContextRecord->Gpr6;
00201
KdpPromptString(&Output, &Input);
00202 ContextRecord->Gpr3 = Input.Length;
00203
KiRestoreProcessorState(TrapFrame, ExceptionFrame);
00204
KdExitDebugger(Enable);
00205
return TRUE;
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216
case DEBUG_UNLOAD_SYMBOLS_BREAKPOINT:
00217 UnloadSymbols =
TRUE;
00218
00219
00220
00221
00222
00223
case DEBUG_LOAD_SYMBOLS_BREAKPOINT:
00224 OldIar = ContextRecord->Iar;
00225
if (
KdDebuggerNotPresent ==
FALSE) {
00226
KdpReportLoadSymbolsStateChange((PSTRING)ContextRecord->Gpr3,
00227 (
PKD_SYMBOLS_INFO) ContextRecord->Gpr4,
00228 UnloadSymbols,
00229 &Prcb->ProcessorState.ContextFrame);
00230
00231 }
00232
00233 RtlCopyMemory(ContextRecord,
00234 &Prcb->ProcessorState.ContextFrame,
00235
sizeof(CONTEXT));
00236
00237
KiRestoreProcessorState(TrapFrame, ExceptionFrame);
00238
KdExitDebugger(Enable);
00239
00240
00241
00242
00243
00244
00245
if (ContextRecord->Iar == OldIar) {
00246 ContextRecord->Iar += 4;
00247 }
00248
00249
return TRUE;
00250
00251
00252
00253
00254
00255
default:
00256
break;
00257 }
00258 }
00259
00260
00261
00262
00263
00264 Completion =
KdpReportExceptionStateChange(
00265 ExceptionRecord,
00266 &Prcb->ProcessorState.ContextFrame,
00267 SecondChance);
00268
00269 RtlCopyMemory(ContextRecord,
00270 &Prcb->ProcessorState.ContextFrame,
00271
sizeof(CONTEXT));
00272
00273
KiRestoreProcessorState(TrapFrame, ExceptionFrame);
00274
KdExitDebugger(Enable);
00275
00276
00277
00278
00279
00280
if (
KdpPageInAddress) {
00281
00282
if (KeGetCurrentIrql() <=
APC_LEVEL) {
00283
00284
00285
00286
00287
00288
00289
00290
KdpPageInData( (PUCHAR)
KdpPageInAddress );
00291
KdpPageInAddress = 0;
00292
KdpControlCPending =
FALSE;
00293
goto re_enter_debugger;
00294
00295 }
else {
00296
00297
00298
00299
00300
00301
00302
00303
00304
00305
if (
KdpControlCPressed) {
00306
ExInitializeWorkItem(
00307 &
KdpPageInWorkItem,
00308 (
PWORKER_THREAD_ROUTINE)
KdpPageInData,
00309 (PVOID)
KdpPageInAddress
00310 );
00311
ExQueueWorkItem( &
KdpPageInWorkItem,
DelayedWorkQueue );
00312
KdpPageInAddress = 0;
00313 }
00314 }
00315 }
00316
00317
KdpControlCPressed =
FALSE;
00318
00319
return Completion;
00320 }
00321
00322 BOOLEAN
00323 KdIsThisAKdTrap (
00324 IN PEXCEPTION_RECORD ExceptionRecord,
00325 IN PCONTEXT ContextRecord,
00326 IN KPROCESSOR_MODE PreviousMode
00327 )
00328
00329
00330
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340
00341
00342
00343
00344
00345
00346
00347
00348
00349
00350
00351
00352 {
00353
00354 ULONG BreakpointCode;
00355
00356
00357
00358
00359
00360
if (ContextRecord->Dr6 !=0) {
00361
if ((PreviousMode ==
KernelMode) ||
00362 (
KeGetCurrentThread()->DebugActive == 0)) {
00363
return TRUE;
00364 }
else {
00365
return FALSE;
00366 }
00367 }
00368
00369
00370
00371
00372
00373
00374
00375 BreakpointCode = ExceptionRecord->ExceptionInformation[0] &
BREAKPOINT_CODE_MASK;
00376
00377
00378
00379
00380
00381
switch (BreakpointCode) {
00382
00383
00384
00385
00386
00387
case BREAKIN_BREAKPOINT:
00388
case KERNEL_BREAKPOINT:
00389
00390
#if DEVL
00391
00392
return TRUE;
00393
00394
#else
00395
00396
if (PreviousMode ==
KernelMode) {
00397
return TRUE;
00398
00399 }
else {
00400
return FALSE;
00401 }
00402
00403
#endif
00404
00405
00406
00407
00408
00409
case DEBUG_PRINT_BREAKPOINT:
00410
return TRUE;
00411
00412
00413
00414
00415
00416
case DEBUG_PROMPT_BREAKPOINT:
00417
return TRUE;
00418
00419
00420
00421
00422
00423
case SINGLE_STEP_BREAKPOINT:
00424
case DEBUG_STOP_BREAKPOINT:
00425
00426
#if DEVL
00427
00428
return TRUE;
00429
00430
#else
00431
00432
if (PreviousMode ==
KernelMode) {
00433
return TRUE;
00434
00435 }
else {
00436
return FALSE;
00437 }
00438
00439
#endif
00440
00441
00442
00443
00444
00445
case DEBUG_LOAD_SYMBOLS_BREAKPOINT:
00446
if (PreviousMode ==
KernelMode) {
00447
return TRUE;
00448
00449 }
else {
00450
return FALSE;
00451 }
00452
00453
00454
00455
00456
00457
case DEBUG_UNLOAD_SYMBOLS_BREAKPOINT:
00458
if (PreviousMode ==
KernelMode) {
00459
return TRUE;
00460
00461 }
else {
00462
return FALSE;
00463 }
00464
00465
00466
00467
00468
00469
default:
00470
return FALSE;
00471 }
00472 }
00473
00474 BOOLEAN
00475 KdpStub (
00476 IN PKTRAP_FRAME TrapFrame,
00477 IN PKEXCEPTION_FRAME ExceptionFrame,
00478 IN PEXCEPTION_RECORD ExceptionRecord,
00479 IN PCONTEXT ContextRecord,
00480 IN KPROCESSOR_MODE PreviousMode,
00481 IN BOOLEAN SecondChance
00482 )
00483
00484
00485
00486
00487
00488
00489
00490
00491
00492
00493
00494
00495
00496
00497
00498
00499
00500
00501
00502
00503
00504
00505
00506
00507
00508
00509
00510
00511
00512
00513
00514
00515
00516 {
00517
00518 ULONG BreakpointCode;
00519
00520
00521
00522
00523
00524
if ((ExceptionRecord->ExceptionCode != STATUS_BREAKPOINT) ||
00525 (ExceptionRecord->NumberParameters == 0)) {
00526
return FALSE;
00527 }
00528
00529
00530
00531
00532
00533
00534
00535 BreakpointCode = ExceptionRecord->ExceptionInformation[0] &
BREAKPOINT_CODE_MASK;
00536
00537
00538
00539
00540
00541
00542
00543
if ((BreakpointCode == DEBUG_PRINT_BREAKPOINT) ||
00544 (BreakpointCode == DEBUG_LOAD_SYMBOLS_BREAKPOINT) ||
00545 (BreakpointCode == DEBUG_UNLOAD_SYMBOLS_BREAKPOINT)) {
00546 ContextRecord->Iar += 4;
00547
return TRUE;
00548
00549 }
else {
00550
return FALSE;
00551 }
00552 }