00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
#include "ntos.h"
00023
#include "ki.h"
00024
#include "ntdbg.h"
00025
#include "string.h"
00026
#include "stdlib.h"
00027
#include "kdpcpu.h"
00028
00029
#if defined(_WIN64)
00030
#error "Build KD64 for 64-bit systems"
00031
#endif
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042 #define GLOBAL_BREAKPOINT_LIMIT 1610612736L // 1.5gigabytes
00043
00044
00045
00046
00047
00048 #define KD_BREAKPOINT_IN_USE 0x00000001
00049 #define KD_BREAKPOINT_NEEDS_WRITE 0x00000002
00050 #define KD_BREAKPOINT_SUSPENDED 0x00000004
00051 #define KD_BREAKPOINT_NEEDS_REPLACE 0x00000008
00052
00053 #define KD_BREAKPOINT_STATE_MASK 0x0000000f
00054 #define KD_BREAKPOINT_IA64_MASK 0x000f0000
00055 #define KD_BREAKPOINT_IA64_MODE 0x00010000 // IA64 mode
00056 #define KD_BREAKPOINT_IA64_MOVL 0x00020000 // MOVL instruction displaced
00057
00058
00059
00060
00061
00062 #define KDP_PACKET_RECEIVED 0
00063 #define KDP_PACKET_TIMEOUT 1
00064 #define KDP_PACKET_RESEND 2
00065
00066
00067 typedef struct _BREAKPOINT_ENTRY {
00068 ULONG
Flags;
00069 ULONG_PTR
DirectoryTableBase;
00070 PVOID
Address;
00071
KDP_BREAKPOINT_TYPE Content;
00072 }
BREAKPOINT_ENTRY, *
PBREAKPOINT_ENTRY;
00073
00074
00075
00076
00077
00078
00079 #define MAXIMUM_RETRIES 20
00080
00081 #define DBGKD_MAX_SPECIAL_CALLS 10
00082
00083 typedef struct _TRACE_DATA_SYM {
00084 ULONG
SymMin;
00085 ULONG
SymMax;
00086 }
TRACE_DATA_SYM, *
PTRACE_DATA_SYM;
00087
00088
00089
00090
00091
00092
VOID
00093
KdpReboot (
00094 VOID
00095 );
00096
00097 BOOLEAN
00098
KdpPrintString (
00099 IN PSTRING Output
00100 );
00101
00102 BOOLEAN
00103
KdpPromptString (
00104 IN PSTRING Output,
00105 IN OUT PSTRING Input
00106 );
00107
00108 ULONG
00109
KdpAddBreakpoint (
00110 IN PVOID Address
00111 );
00112
00113 BOOLEAN
00114
KdpDeleteBreakpoint (
00115 IN ULONG Handle
00116 );
00117
00118 BOOLEAN
00119
KdpDeleteBreakpointRange (
00120 IN PVOID Lower,
00121 IN PVOID Upper
00122 );
00123
00124
#if defined(_IA64_)
00125
00126 BOOLEAN
00127 KdpSuspendBreakpointRange (
00128 IN PVOID Lower,
00129 IN PVOID Upper
00130 );
00131
00132 BOOLEAN
00133 KdpRestoreBreakpointRange (
00134 IN PVOID Lower,
00135 IN PVOID Upper
00136 );
00137
#endif
00138
00139 ULONG
00140
KdpMoveMemory (
00141 IN PCHAR Destination,
00142 IN PCHAR Source,
00143 IN ULONG Length
00144 );
00145
00146
VOID
00147
KdpQuickMoveMemory (
00148 IN PCHAR Destination,
00149 IN PCHAR Source,
00150 IN ULONG Length
00151 );
00152
00153 ULONG
00154
KdpReceivePacket (
00155 IN ULONG ExpectedPacketType,
00156 OUT PSTRING MessageHeader,
00157 OUT PSTRING MessageData,
00158 OUT PULONG DataLength
00159 );
00160
00161
VOID
00162
KdpSetLoadState(
00163 IN PDBGKD_WAIT_STATE_CHANGE WaitStateChange,
00164 IN PCONTEXT ContextRecord
00165 );
00166
00167
VOID
00168
KdpSetStateChange(
00169 IN PDBGKD_WAIT_STATE_CHANGE WaitStateChange,
00170 IN PEXCEPTION_RECORD ExceptionRecord,
00171 IN PCONTEXT ContextRecord,
00172 IN BOOLEAN SecondChance
00173 );
00174
00175
VOID
00176
KdpGetStateChange(
00177 IN PDBGKD_MANIPULATE_STATE ManipulateState,
00178 IN PCONTEXT ContextRecord
00179 );
00180
00181
VOID
00182
KdpSendPacket (
00183 IN ULONG PacketType,
00184 IN PSTRING MessageHeader,
00185 IN PSTRING MessageData OPTIONAL
00186 );
00187
00188 BOOLEAN
00189
KdpStub (
00190 IN PKTRAP_FRAME TrapFrame,
00191 IN PKEXCEPTION_FRAME ExceptionFrame,
00192 IN PEXCEPTION_RECORD ExceptionRecord,
00193 IN PCONTEXT ContextRecord,
00194 IN KPROCESSOR_MODE PreviousMode,
00195 IN BOOLEAN SecondChance
00196 );
00197
00198 BOOLEAN
00199
KdpTrap (
00200 IN PKTRAP_FRAME TrapFrame,
00201 IN PKEXCEPTION_FRAME ExceptionFrame,
00202 IN PEXCEPTION_RECORD ExceptionRecord,
00203 IN PCONTEXT ContextRecord,
00204 IN KPROCESSOR_MODE PreviousMode,
00205 IN BOOLEAN SecondChance
00206 );
00207
00208
VOID
00209
KdpDisplayString (
00210 IN PCHAR Output
00211 );
00212
00213
VOID
00214
KdpWriteComPacket (
00215 USHORT,
00216 USHORT,
00217 PVOID,
00218 PVOID,
00219 PVOID
00220 );
00221
00222 BOOLEAN
00223
KdpReadComPacket (
00224 VOID
00225 );
00226
00227 BOOLEAN
00228
KdpSwitchProcessor (
00229 IN PEXCEPTION_RECORD ExceptionRecord,
00230 IN OUT PCONTEXT ContextRecord,
00231 IN BOOLEAN SecondChance
00232 );
00233
00234 BOOLEAN
00235
KdpReportExceptionStateChange (
00236 IN PEXCEPTION_RECORD ExceptionRecord,
00237 IN OUT PCONTEXT ContextRecord,
00238 IN BOOLEAN SecondChance
00239 );
00240
00241 BOOLEAN
00242
KdpReportLoadSymbolsStateChange (
00243 IN PSTRING PathName,
00244 IN
PKD_SYMBOLS_INFO SymbolInfo,
00245 IN BOOLEAN UnloadSymbols,
00246 IN OUT PCONTEXT ContextRecord
00247 );
00248
00249
KCONTINUE_STATUS
00250
KdpSendWaitContinue(
00251 IN ULONG PacketType,
00252 IN PSTRING MessageHeader,
00253 IN PSTRING MessageData OPTIONAL,
00254 IN OUT PCONTEXT ContextRecord
00255 );
00256
00257
VOID
00258
KdpReadVirtualMemory(
00259 IN PDBGKD_MANIPULATE_STATE m,
00260 IN PSTRING AdditionalData,
00261 IN PCONTEXT Context
00262 );
00263
00264
VOID
00265
KdpReadVirtualMemory64(
00266 IN PDBGKD_MANIPULATE_STATE m,
00267 IN PSTRING AdditionalData,
00268 IN PCONTEXT Context
00269 );
00270
00271
VOID
00272
KdpWriteVirtualMemory(
00273 IN PDBGKD_MANIPULATE_STATE m,
00274 IN PSTRING AdditionalData,
00275 IN PCONTEXT Context
00276 );
00277
00278
VOID
00279
KdpWriteVirtualMemory64(
00280 IN PDBGKD_MANIPULATE_STATE m,
00281 IN PSTRING AdditionalData,
00282 IN PCONTEXT Context
00283 );
00284
00285
VOID
00286
KdpReadPhysicalMemory(
00287 IN PDBGKD_MANIPULATE_STATE m,
00288 IN PSTRING AdditionalData,
00289 IN PCONTEXT Context
00290 );
00291
00292
VOID
00293
KdpWritePhysicalMemory(
00294 IN PDBGKD_MANIPULATE_STATE m,
00295 IN PSTRING AdditionalData,
00296 IN PCONTEXT Context
00297 );
00298
00299
VOID
00300
KdpGetContext(
00301 IN PDBGKD_MANIPULATE_STATE m,
00302 IN PSTRING AdditionalData,
00303 IN PCONTEXT Context
00304 );
00305
00306
VOID
00307
KdpSetContext(
00308 IN PDBGKD_MANIPULATE_STATE m,
00309 IN PSTRING AdditionalData,
00310 IN PCONTEXT Context
00311 );
00312
00313
VOID
00314
KdpWriteBreakpoint(
00315 IN PDBGKD_MANIPULATE_STATE m,
00316 IN PSTRING AdditionalData,
00317 IN PCONTEXT Context
00318 );
00319
00320
VOID
00321
KdpRestoreBreakpoint(
00322 IN PDBGKD_MANIPULATE_STATE m,
00323 IN PSTRING AdditionalData,
00324 IN PCONTEXT Context
00325 );
00326
00327
VOID
00328
KdpReadControlSpace(
00329 IN PDBGKD_MANIPULATE_STATE m,
00330 IN PSTRING AdditionalData,
00331 IN PCONTEXT Context
00332 );
00333
00334
VOID
00335
KdpWriteControlSpace(
00336 IN PDBGKD_MANIPULATE_STATE m,
00337 IN PSTRING AdditionalData,
00338 IN PCONTEXT Context
00339 );
00340
00341
VOID
00342
KdpReadIoSpace(
00343 IN PDBGKD_MANIPULATE_STATE m,
00344 IN PSTRING AdditionalData,
00345 IN PCONTEXT Context
00346 );
00347
00348
VOID
00349
KdpReadMachineSpecificRegister(
00350 IN PDBGKD_MANIPULATE_STATE m,
00351 IN PSTRING AdditionalData,
00352 IN PCONTEXT Context
00353 );
00354
00355
VOID
00356
KdpWriteIoSpace(
00357 IN PDBGKD_MANIPULATE_STATE m,
00358 IN PSTRING AdditionalData,
00359 IN PCONTEXT Context
00360 );
00361
00362
VOID
00363
KdpWriteMachineSpecificRegister(
00364 IN PDBGKD_MANIPULATE_STATE m,
00365 IN PSTRING AdditionalData,
00366 IN PCONTEXT Context
00367 );
00368
00369
#ifdef _ALPHA_
00370
00371
VOID
00372
KdpReadIoSpaceExtended (
00373 IN PDBGKD_MANIPULATE_STATE m,
00374 IN PSTRING AdditionalData,
00375 IN PCONTEXT Context
00376 );
00377
00378
VOID
00379
KdpWriteIoSpaceExtended (
00380 IN PDBGKD_MANIPULATE_STATE m,
00381 IN PSTRING AdditionalData,
00382 IN PCONTEXT Context
00383 );
00384
00385
#endif
00386
00387
00388
VOID
00389
KdpSuspendBreakpoint (
00390 ULONG Handle
00391 );
00392
00393
VOID
00394
KdpSuspendAllBreakpoints (
00395 VOID
00396 );
00397
00398
VOID
00399
KdpRestoreAllBreakpoints (
00400 VOID
00401 );
00402
00403
VOID
00404
KdpTimeSlipDpcRoutine (
00405
PKDPC Dpc,
00406 PVOID DeferredContext,
00407 PVOID SystemArgument1,
00408 PVOID SystemArgument2
00409 );
00410
00411
VOID
00412
KdpTimeSlipWork (
00413 IN PVOID Context
00414 );
00415
00416
00417
00418
00419
00420
00421
00422
00423
VOID
00424
RtlpBreakWithStatusInstruction (
00425 VOID
00426 );
00427
00428
00429
00430
00431
00432 #define KDP_MESSAGE_BUFFER_SIZE 4096
00433
00434 extern BREAKPOINT_ENTRY KdpBreakpointTable[BREAKPOINT_TABLE_SIZE];
00435 extern BOOLEAN
KdpControlCPending;
00436 extern KSPIN_LOCK
KdpDebuggerLock;
00437 extern PKDEBUG_ROUTINE KiDebugRoutine;
00438 extern PKDEBUG_SWITCH_ROUTINE KiDebugSwitchRoutine;
00439 extern KDP_BREAKPOINT_TYPE KdpBreakpointInstruction;
00440 extern UCHAR
KdpMessageBuffer[
KDP_MESSAGE_BUFFER_SIZE];
00441 extern UCHAR
KdpPathBuffer[
KDP_MESSAGE_BUFFER_SIZE];
00442 extern ULONG
KdpOweBreakpoint;
00443 extern ULONG
KdpNextPacketIdToSend;
00444 extern ULONG
KdpPacketIdExpected;
00445
00446 extern LARGE_INTEGER
KdPerformanceCounterRate;
00447 extern LARGE_INTEGER
KdTimerStart;
00448 extern LARGE_INTEGER
KdTimerStop;
00449 extern LARGE_INTEGER
KdTimerDifference;
00450
00451 extern BOOLEAN
BreakpointsSuspended;
00452 extern PVOID
KdpNtosImageBase;
00453 extern LIST_ENTRY
KdpDebuggerDataListHead;
00454
00455 typedef struct {
00456 ULONG_PTR Addr;
00457 ULONG Flags;
00458 ULONG Calls;
00459 ULONG CallsLastCheck;
00460 ULONG MaxCallsPerPeriod;
00461 ULONG MinInstructions;
00462 ULONG MaxInstructions;
00463 ULONG TotalInstructions;
00464 ULONG
Handle;
00465 PVOID Thread;
00466 ULONG_PTR ReturnAddress;
00467 }
DBGKD_INTERNAL_BREAKPOINT, *
PDBGKD_INTERNAL_BREAKPOINT;
00468
00469
00470 #define DBGKD_MAX_INTERNAL_BREAKPOINTS 20
00471 extern DBGKD_INTERNAL_BREAKPOINT KdpInternalBPs[
DBGKD_MAX_INTERNAL_BREAKPOINTS];
00472
00473 extern ULONG_PTR
KdpCurrentSymbolStart;
00474 extern ULONG_PTR
KdpCurrentSymbolEnd;
00475 extern LONG
KdpNextCallLevelChange;
00476 extern ULONG_PTR
KdSpecialCalls[];
00477 extern ULONG
KdNumberOfSpecialCalls;
00478 extern ULONG_PTR
InitialSP;
00479 extern ULONG
KdpNumInternalBreakpoints;
00480 extern KTIMER InternalBreakpointTimer;
00481 extern KDPC InternalBreakpointCheckDpc;
00482 extern BOOLEAN
KdpPortLocked;
00483 extern LARGE_INTEGER
KdpTimeEntered;
00484
00485 extern DBGKD_TRACE_DATA
TraceDataBuffer[];
00486 extern ULONG
TraceDataBufferPosition;
00487 extern TRACE_DATA_SYM TraceDataSyms[];
00488 extern UCHAR
NextTraceDataSym;
00489 extern UCHAR
NumTraceDataSyms;
00490 extern ULONG
IntBPsSkipping;
00491 extern BOOLEAN
WatchStepOver;
00492 extern PVOID
WSOThread;
00493 extern ULONG
WSOEsp;
00494 extern ULONG
WatchStepOverHandle;
00495 extern ULONG_PTR
WatchStepOverBreakAddr;
00496 extern BOOLEAN
WatchStepOverSuspended;
00497 extern ULONG
InstructionsTraced;
00498 extern BOOLEAN
SymbolRecorded;
00499 extern LONG
CallLevelChange;
00500 extern LONG
oldpc;
00501 extern BOOLEAN
InstrCountInternal;
00502
extern BOOLEAN
BreakpointsSuspended;
00503
extern BOOLEAN
KdpControlCPending;
00504 extern BOOLEAN
KdpControlCPressed;
00505 extern ULONG
KdpRetryCount;
00506 extern ULONG
KdpNumberRetries;
00507
00508
extern KDP_BREAKPOINT_TYPE KdpBreakpointInstruction;
00509
extern ULONG
KdpOweBreakpoint;
00510
extern ULONG
KdpNextPacketIdToSend;
00511
extern ULONG
KdpPacketIdExpected;
00512
extern PVOID
KdpNtosImageBase;
00513 extern UCHAR
KdPrintCircularBuffer[
KDPRINTBUFFERSIZE];
00514 extern PUCHAR
KdPrintWritePointer;
00515 extern ULONG
KdPrintRolloverCount;
00516 extern KSPIN_LOCK
KdpPrintSpinLock;
00517 extern DEBUG_PARAMETERS KdDebugParameters;
00518 extern KSPIN_LOCK
KdpDataSpinLock;
00519
extern LIST_ENTRY
KdpDebuggerDataListHead;
00520 extern KDDEBUGGER_DATA
KdDebuggerDataBlock;
00521 extern KDPC KdpTimeSlipDpc;
00522 extern WORK_QUEUE_ITEM KdpTimeSlipWorkItem;
00523 extern KTIMER KdpTimeSlipTimer;
00524 extern ULONG
KdpTimeSlipPending;
00525 extern KSPIN_LOCK
KdpTimeSlipEventLock;
00526 extern PVOID
KdpTimeSlipEvent;
00527 extern BOOLEAN
KdpDebuggerStructuresInitialized;
00528 extern ULONG
KdEnteredDebugger;
00529
00530
00531
00532
00533
00534
VOID
00535
KdpInitCom(
00536 VOID
00537 );
00538
00539
VOID
00540
KdpPortLock(
00541 VOID
00542 );
00543
00544
VOID
00545
KdpPortUnlock(
00546 VOID
00547 );
00548
00549 BOOLEAN
00550
KdpPollBreakInWithPortLock(
00551 VOID
00552 );
00553
00554
USHORT
00555
KdpReceivePacketLeader (
00556 IN ULONG PacketType,
00557 OUT PULONG PacketLeader
00558 );
00559
00560
#if DBG
00561
00562
#include <stdio.h>
00563
#define DPRINT(s) KdpDprintf s
00564
00565
VOID
00566 KdpDprintf(
00567 IN PCHAR f,
00568 ...
00569 );
00570
00571
#else
00572
00573 #define DPRINT(s)
00574
00575
#endif