2 * PROJECT: ReactOS Kernel
3 * LICENSE: GPL - See COPYING in the top level directory
4 * FILE: ntoskrnl/kd64/kdapi.c
5 * PURPOSE: KD64 Public Routines and Internal Support
6 * PROGRAMMERS: Alex Ionescu (alex.ionescu@reactos.org)
9 /* INCLUDES ******************************************************************/
15 /* PRIVATE FUNCTIONS *********************************************************/
19 KdpQueryMemory(IN PDBGKD_MANIPULATE_STATE64 State
,
22 PDBGKD_QUERY_MEMORY Memory
= &State
->u
.QueryMemory
;
24 NTSTATUS Status
= STATUS_SUCCESS
;
26 /* Validate the address space */
27 if (Memory
->AddressSpace
== DBGKD_QUERY_MEMORY_VIRTUAL
)
29 /* Check if this is process memory */
30 if ((PVOID
)(LONG_PTR
)Memory
->Address
< MmHighestUserAddress
)
33 Memory
->AddressSpace
= DBGKD_QUERY_MEMORY_PROCESS
;
37 /* FIXME: Check if it's session space */
38 Memory
->AddressSpace
= DBGKD_QUERY_MEMORY_KERNEL
;
42 Memory
->Flags
= DBGKD_QUERY_MEMORY_READ
|
43 DBGKD_QUERY_MEMORY_WRITE
|
44 DBGKD_QUERY_MEMORY_EXECUTE
;
49 Status
= STATUS_INVALID_PARAMETER
;
52 /* Return structure */
53 State
->ReturnStatus
= Status
;
57 Header
.Length
= sizeof(DBGKD_MANIPULATE_STATE64
);
58 Header
.Buffer
= (PCHAR
)State
;
61 KdSendPacket(PACKET_TYPE_KD_STATE_MANIPULATE
,
69 KdpWriteBreakpoint(IN PDBGKD_MANIPULATE_STATE64 State
,
73 PDBGKD_WRITE_BREAKPOINT64 Breakpoint
= &State
->u
.WriteBreakPoint
;
77 Header
.Length
= sizeof(DBGKD_MANIPULATE_STATE64
);
78 Header
.Buffer
= (PCHAR
)State
;
79 ASSERT(Data
->Length
== 0);
81 /* Create the breakpoint */
82 Breakpoint
->BreakPointHandle
=
83 KdpAddBreakpoint((PVOID
)(LONG_PTR
)Breakpoint
->BreakPointAddress
);
84 if (!Breakpoint
->BreakPointHandle
)
87 State
->ReturnStatus
= STATUS_UNSUCCESSFUL
;
92 State
->ReturnStatus
= STATUS_SUCCESS
;
96 KdSendPacket(PACKET_TYPE_KD_STATE_MANIPULATE
,
104 DumpTraceData(OUT PSTRING TraceData
)
106 /* Update the buffer */
107 TraceDataBuffer
[0] = TraceDataBufferPosition
;
109 /* Setup the trace data */
110 TraceData
->Length
= TraceDataBufferPosition
* sizeof(ULONG
);
111 TraceData
->Buffer
= (PCHAR
)TraceDataBuffer
;
113 /* Reset the buffer location */
114 TraceDataBufferPosition
= 1;
119 KdpGetStateChange(IN PDBGKD_MANIPULATE_STATE64 State
,
125 /* Check for success */
126 if (NT_SUCCESS(State
->u
.Continue2
.ContinueStatus
))
128 /* Check if we're tracing */
129 if (State
->u
.Continue2
.ControlSet
.TraceFlag
)
132 Context
->EFlags
|= EFLAGS_TF
;
137 Context
->EFlags
&= ~EFLAGS_TF
;
140 /* Loop all processors */
141 for (i
= 0; i
< KeNumberProcessors
; i
++)
143 /* Get the PRCB and update DR7 and DR6 */
144 Prcb
= KiProcessorBlock
[i
];
145 Prcb
->ProcessorState
.SpecialRegisters
.KernelDr7
=
146 State
->u
.Continue2
.ControlSet
.Dr7
;
147 Prcb
->ProcessorState
.SpecialRegisters
.KernelDr6
= 0;
150 /* Check if we have new symbol information */
151 if (State
->u
.Continue2
.ControlSet
.CurrentSymbolStart
!= 1)
154 KdpCurrentSymbolStart
=
155 State
->u
.Continue2
.ControlSet
.CurrentSymbolStart
;
156 KdpCurrentSymbolEnd
= State
->u
.Continue2
.ControlSet
.CurrentSymbolEnd
;
163 KdpSetCommonState(IN ULONG NewState
,
165 OUT PDBGKD_WAIT_STATE_CHANGE64 WaitStateChange
)
167 USHORT InstructionCount
;
168 BOOLEAN HadBreakpoints
;
170 /* Setup common stuff available for all CPU architectures */
171 WaitStateChange
->NewState
= NewState
;
172 WaitStateChange
->ProcessorLevel
= KeProcessorLevel
;
173 WaitStateChange
->Processor
= (USHORT
)KeGetCurrentPrcb()->Number
;
174 WaitStateChange
->NumberProcessors
= (ULONG
)KeNumberProcessors
;
175 WaitStateChange
->Thread
= (LONG_PTR
)KeGetCurrentThread();
177 WaitStateChange
->ProgramCounter
= (ULONG
)(LONG_PTR
)Context
->Eip
;
178 #elif defined(_M_AMD64)
179 WaitStateChange
->ProgramCounter
= (LONG_PTR
)Context
->Rip
;
181 #error Unknown platform
184 /* Zero out the Control Report */
185 RtlZeroMemory(&WaitStateChange
->ControlReport
,
186 sizeof(DBGKD_CONTROL_REPORT
));
188 /* Now copy the instruction stream and set the count */
189 RtlCopyMemory(&WaitStateChange
->ControlReport
.InstructionStream
[0],
190 (PVOID
)(ULONG_PTR
)WaitStateChange
->ProgramCounter
,
192 InstructionCount
= DBGKD_MAXSTREAM
;
193 WaitStateChange
->ControlReport
.InstructionCount
= InstructionCount
;
195 /* Clear all the breakpoints in this region */
197 KdpDeleteBreakpointRange((PVOID
)(LONG_PTR
)WaitStateChange
->ProgramCounter
,
198 (PVOID
)((ULONG_PTR
)WaitStateChange
->ProgramCounter
+
199 WaitStateChange
->ControlReport
.InstructionCount
- 1));
202 /* Copy the instruction stream again, this time without breakpoints */
203 RtlCopyMemory(&WaitStateChange
->ControlReport
.InstructionStream
[0],
204 (PVOID
)(ULONG_PTR
)WaitStateChange
->ProgramCounter
,
205 WaitStateChange
->ControlReport
.InstructionCount
);
211 KdpGetVersion(IN PDBGKD_MANIPULATE_STATE64 State
)
215 /* Fill out the header */
216 Header
.Length
= sizeof(DBGKD_MANIPULATE_STATE64
);
217 Header
.Buffer
= (PCHAR
)State
;
219 /* Get the version block */
220 KdpSysGetVersion(&State
->u
.GetVersion64
);
222 /* Fill out the state */
223 State
->ApiNumber
= DbgKdGetVersionApi
;
224 State
->ReturnStatus
= STATUS_SUCCESS
;
226 /* Send the packet */
227 KdSendPacket(PACKET_TYPE_KD_STATE_MANIPULATE
,
235 KdpReadVirtualMemory(IN PDBGKD_MANIPULATE_STATE64 State
,
240 ULONG Length
= State
->u
.ReadMemory
.TransferCount
;
241 NTSTATUS Status
= STATUS_SUCCESS
;
242 ULONG64 TargetBaseAddress
= State
->u
.ReadMemory
.TargetBaseAddress
;
244 /* Validate length */
245 if (Length
> (PACKET_MAX_SIZE
- sizeof(DBGKD_MANIPULATE_STATE64
)))
247 /* Overflow, set it to maximum possible */
248 Length
= PACKET_MAX_SIZE
- sizeof(DBGKD_MANIPULATE_STATE64
);
252 if (!MmIsAddressValid((PVOID
)(ULONG_PTR
)State
->u
.ReadMemory
.TargetBaseAddress
))
254 Ke386SetCr2(State
->u
.ReadMemory
.TargetBaseAddress
);
259 // HACK for x64, until KD stops sending bogus addresses to WinDbg
260 if (TargetBaseAddress
< (ULONG_PTR
)MM_LOWEST_SYSTEM_ADDRESS
)
262 FrLdrDbgPrint("Trying to read memory at 0x%p\n", TargetBaseAddress
);
263 // DPRINT1("Trying to read memory at 0x%p\n", TargetBaseAddress);
264 TargetBaseAddress
= 0;
267 if (!TargetBaseAddress
)
270 Status
= STATUS_UNSUCCESSFUL
;
274 RtlCopyMemory(Data
->Buffer
,
275 (PVOID
)(ULONG_PTR
)State
->u
.ReadMemory
.TargetBaseAddress
,
279 /* Fill out the header */
280 Data
->Length
= Length
;
281 Header
.Length
= sizeof(DBGKD_MANIPULATE_STATE64
);
282 Header
.Buffer
= (PCHAR
)State
;
284 /* Fill out the state */
285 State
->ReturnStatus
= Status
;
286 State
->u
.ReadMemory
.ActualBytesRead
= Length
;
288 /* Send the packet */
289 KdSendPacket(PACKET_TYPE_KD_STATE_MANIPULATE
,
297 KdpReadControlSpace(IN PDBGKD_MANIPULATE_STATE64 State
,
301 PDBGKD_READ_MEMORY64 ReadMemory
= &State
->u
.ReadMemory
;
303 ULONG Length
, RealLength
;
306 /* Setup the header */
307 Header
.Length
= sizeof(DBGKD_MANIPULATE_STATE64
);
308 Header
.Buffer
= (PCHAR
)State
;
309 ASSERT(Data
->Length
== 0);
311 /* Check the length requested */
312 Length
= ReadMemory
->TransferCount
;
313 if (Length
> (PACKET_MAX_SIZE
- sizeof(DBGKD_MANIPULATE_STATE64
)))
315 /* Use maximum allowed */
316 Length
= PACKET_MAX_SIZE
- sizeof(DBGKD_MANIPULATE_STATE64
);
319 #if defined (_M_AMD64)
320 if ((ULONG
)ReadMemory
->TargetBaseAddress
<= 2)
322 switch ((ULONG_PTR
)ReadMemory
->TargetBaseAddress
)
325 ControlStart
= &KiProcessorBlock
[State
->Processor
];
326 RealLength
= sizeof(PVOID
);
330 ControlStart
= &KiProcessorBlock
[State
->Processor
]->
331 ProcessorState
.SpecialRegisters
;
332 RealLength
= sizeof(KSPECIAL_REGISTERS
);
341 if (RealLength
< Length
) Length
= RealLength
;
343 /* Copy the memory */
344 RtlCopyMemory(Data
->Buffer
, ControlStart
, Length
);
345 Data
->Length
= Length
;
348 State
->ReturnStatus
= STATUS_SUCCESS
;
349 ReadMemory
->ActualBytesRead
= Data
->Length
;
352 /* Make sure that this is a valid request */
353 if (((ULONG
)ReadMemory
->TargetBaseAddress
< sizeof(KPROCESSOR_STATE
)) &&
354 (State
->Processor
< KeNumberProcessors
))
356 /* Get the actual length */
357 RealLength
= sizeof(KPROCESSOR_STATE
) -
358 (ULONG_PTR
)ReadMemory
->TargetBaseAddress
;
359 if (RealLength
< Length
) Length
= RealLength
;
361 /* Set the proper address */
362 ControlStart
= (PVOID
)((ULONG_PTR
)ReadMemory
->TargetBaseAddress
+
363 (ULONG_PTR
)&KiProcessorBlock
[State
->Processor
]->
366 /* Copy the memory */
367 RtlCopyMemory(Data
->Buffer
, ControlStart
, Length
);
368 Data
->Length
= Length
;
371 State
->ReturnStatus
= STATUS_SUCCESS
;
372 ReadMemory
->ActualBytesRead
= Data
->Length
;
377 /* Invalid request */
379 State
->ReturnStatus
= STATUS_UNSUCCESSFUL
;
380 ReadMemory
->ActualBytesRead
= 0;
384 KdSendPacket(PACKET_TYPE_KD_STATE_MANIPULATE
,
392 KdpWriteControlSpace(IN PDBGKD_MANIPULATE_STATE64 State
,
396 PDBGKD_WRITE_MEMORY64 WriteMemory
= &State
->u
.WriteMemory
;
401 /* Setup the header */
402 Header
.Length
= sizeof(DBGKD_MANIPULATE_STATE64
);
403 Header
.Buffer
= (PCHAR
)State
;
405 /* Make sure that this is a valid request */
406 Length
= WriteMemory
->TransferCount
;
407 if ((((ULONG
)WriteMemory
->TargetBaseAddress
+ Length
) <=
408 sizeof(KPROCESSOR_STATE
)) &&
409 (State
->Processor
< KeNumberProcessors
))
411 /* Set the proper address */
412 ControlStart
= (PVOID
)((ULONG_PTR
)WriteMemory
->TargetBaseAddress
+
413 (ULONG_PTR
)&KiProcessorBlock
[State
->Processor
]->
416 /* Copy the memory */
417 RtlCopyMemory(ControlStart
, Data
->Buffer
, Data
->Length
);
418 Length
= Data
->Length
;
421 State
->ReturnStatus
= STATUS_SUCCESS
;
422 WriteMemory
->ActualBytesWritten
= Length
;
426 /* Invalid request */
428 State
->ReturnStatus
= STATUS_UNSUCCESSFUL
;
429 WriteMemory
->ActualBytesWritten
= 0;
433 KdSendPacket(PACKET_TYPE_KD_STATE_MANIPULATE
,
441 KdpRestoreBreakpoint(IN PDBGKD_MANIPULATE_STATE64 State
,
445 PDBGKD_RESTORE_BREAKPOINT RestoreBp
= &State
->u
.RestoreBreakPoint
;
448 /* Fill out the header */
449 Header
.Length
= sizeof(DBGKD_MANIPULATE_STATE64
);
450 Header
.Buffer
= (PCHAR
)State
;
451 ASSERT(Data
->Length
== 0);
453 /* Get the version block */
454 if (KdpDeleteBreakpoint(RestoreBp
->BreakPointHandle
))
457 State
->ReturnStatus
= STATUS_SUCCESS
;
462 State
->ReturnStatus
= STATUS_UNSUCCESSFUL
;
465 /* Send the packet */
466 KdSendPacket(PACKET_TYPE_KD_STATE_MANIPULATE
,
474 KdpGetContext(IN PDBGKD_MANIPULATE_STATE64 State
,
481 /* Setup the header */
482 Header
.Length
= sizeof(DBGKD_MANIPULATE_STATE64
);
483 Header
.Buffer
= (PCHAR
)State
;
484 ASSERT(Data
->Length
== 0);
486 /* Make sure that this is a valid request */
487 if (State
->Processor
< KeNumberProcessors
)
489 /* Check if the request is for this CPU */
490 if (State
->Processor
== KeGetCurrentPrcb()->Number
)
492 /* We're just copying our own context */
493 ControlStart
= Context
;
497 /* SMP not yet handled */
502 /* Copy the memory */
503 RtlCopyMemory(Data
->Buffer
, ControlStart
, sizeof(CONTEXT
));
504 Data
->Length
= sizeof(CONTEXT
);
507 State
->ReturnStatus
= STATUS_SUCCESS
;
511 /* Invalid request */
512 State
->ReturnStatus
= STATUS_UNSUCCESSFUL
;
516 KdSendPacket(PACKET_TYPE_KD_STATE_MANIPULATE
,
524 KdpSetContext(IN PDBGKD_MANIPULATE_STATE64 State
,
531 /* Setup the header */
532 Header
.Length
= sizeof(DBGKD_MANIPULATE_STATE64
);
533 Header
.Buffer
= (PCHAR
)State
;
534 ASSERT(Data
->Length
== sizeof(CONTEXT
));
536 /* Make sure that this is a valid request */
537 if (State
->Processor
< KeNumberProcessors
)
539 /* Check if the request is for this CPU */
540 if (State
->Processor
== KeGetCurrentPrcb()->Number
)
542 /* We're just copying our own context */
543 ControlStart
= Context
;
547 /* SMP not yet handled */
552 /* Copy the memory */
553 RtlCopyMemory(ControlStart
, Data
->Buffer
, sizeof(CONTEXT
));
556 State
->ReturnStatus
= STATUS_SUCCESS
;
560 /* Invalid request */
561 State
->ReturnStatus
= STATUS_UNSUCCESSFUL
;
565 KdSendPacket(PACKET_TYPE_KD_STATE_MANIPULATE
,
573 KdpSendWaitContinue(IN ULONG PacketType
,
574 IN PSTRING SendHeader
,
575 IN PSTRING SendData OPTIONAL
,
576 IN OUT PCONTEXT Context
)
579 DBGKD_MANIPULATE_STATE64 ManipulateState
;
583 /* Setup the Manipulate State structure */
584 Header
.MaximumLength
= sizeof(DBGKD_MANIPULATE_STATE64
);
585 Header
.Buffer
= (PCHAR
)&ManipulateState
;
586 Data
.MaximumLength
= sizeof(KdpMessageBuffer
);
587 Data
.Buffer
= KdpMessageBuffer
;
588 //KdpContextSent = FALSE;
591 /* Send the Packet */
592 KdSendPacket(PacketType
, SendHeader
, SendData
, &KdpContext
);
594 /* If the debugger isn't present anymore, just return success */
595 if (KdDebuggerNotPresent
) return ContinueSuccess
;
597 /* Main processing Loop */
603 /* Wait to get a reply to our packet */
604 RecvCode
= KdReceivePacket(PACKET_TYPE_KD_STATE_MANIPULATE
,
610 /* If we got a resend request, do it */
611 if (RecvCode
== KdPacketNeedsResend
) goto SendPacket
;
612 } while (RecvCode
== KdPacketTimedOut
);
614 /* Now check what API we got */
615 switch (ManipulateState
.ApiNumber
)
617 case DbgKdReadVirtualMemoryApi
:
619 /* Read virtual memory */
620 KdpReadVirtualMemory(&ManipulateState
, &Data
, Context
);
623 case DbgKdWriteVirtualMemoryApi
:
626 Ke386SetCr2(DbgKdWriteVirtualMemoryApi
);
630 case DbgKdGetContextApi
:
632 /* Get the current context */
633 KdpGetContext(&ManipulateState
, &Data
, Context
);
636 case DbgKdSetContextApi
:
638 /* Set a new context */
639 KdpSetContext(&ManipulateState
, &Data
, Context
);
642 case DbgKdWriteBreakPointApi
:
644 /* Write the breakpoint */
645 KdpWriteBreakpoint(&ManipulateState
, &Data
, Context
);
648 case DbgKdRestoreBreakPointApi
:
651 KdpRestoreBreakpoint(&ManipulateState
, &Data
, Context
);
654 case DbgKdContinueApi
:
656 /* Simply continue */
657 return NT_SUCCESS(ManipulateState
.u
.Continue
.ContinueStatus
);
659 case DbgKdReadControlSpaceApi
:
661 /* Read control space */
662 KdpReadControlSpace(&ManipulateState
, &Data
, Context
);
665 case DbgKdWriteControlSpaceApi
:
668 KdpWriteControlSpace(&ManipulateState
, &Data
, Context
);
671 case DbgKdReadIoSpaceApi
:
674 Ke386SetCr2(DbgKdReadIoSpaceApi
);
678 case DbgKdWriteIoSpaceApi
:
681 Ke386SetCr2(DbgKdWriteIoSpaceApi
);
688 Ke386SetCr2(DbgKdRebootApi
);
692 case DbgKdContinueApi2
:
694 /* Check if caller reports success */
695 if (NT_SUCCESS(ManipulateState
.u
.Continue2
.ContinueStatus
))
697 /* Update the state */
698 KdpGetStateChange(&ManipulateState
, Context
);
699 return ContinueSuccess
;
703 /* Return an error */
704 return ContinueError
;
708 case DbgKdReadPhysicalMemoryApi
:
712 Ke386SetCr2(DbgKdReadPhysicalMemoryApi
);
716 case DbgKdWritePhysicalMemoryApi
:
719 Ke386SetCr2(DbgKdWritePhysicalMemoryApi
);
723 case DbgKdQuerySpecialCallsApi
:
726 Ke386SetCr2(DbgKdQuerySpecialCallsApi
);
730 case DbgKdSetSpecialCallApi
:
733 Ke386SetCr2(DbgKdSetSpecialCallApi
);
737 case DbgKdClearSpecialCallsApi
:
740 Ke386SetCr2(DbgKdClearSpecialCallsApi
);
744 case DbgKdSetInternalBreakPointApi
:
747 Ke386SetCr2(DbgKdSetInternalBreakPointApi
);
751 case DbgKdGetInternalBreakPointApi
:
754 Ke386SetCr2(DbgKdGetInternalBreakPointApi
);
758 case DbgKdReadIoSpaceExtendedApi
:
761 Ke386SetCr2(DbgKdReadIoSpaceExtendedApi
);
765 case DbgKdWriteIoSpaceExtendedApi
:
768 Ke386SetCr2(DbgKdWriteIoSpaceExtendedApi
);
772 case DbgKdGetVersionApi
:
774 /* Get version data */
775 KdpGetVersion(&ManipulateState
);
778 case DbgKdWriteBreakPointExApi
:
781 Ke386SetCr2(DbgKdWriteBreakPointExApi
);
785 case DbgKdRestoreBreakPointExApi
:
788 Ke386SetCr2(DbgKdRestoreBreakPointExApi
);
792 case DbgKdCauseBugCheckApi
:
795 Ke386SetCr2(DbgKdCauseBugCheckApi
);
799 case DbgKdSwitchProcessor
:
802 Ke386SetCr2(DbgKdSwitchProcessor
);
809 Ke386SetCr2(DbgKdPageInApi
);
813 case DbgKdReadMachineSpecificRegister
:
816 Ke386SetCr2(DbgKdReadMachineSpecificRegister
);
820 case DbgKdWriteMachineSpecificRegister
:
823 Ke386SetCr2(DbgKdWriteMachineSpecificRegister
);
830 Ke386SetCr2(OldVlm1
);
837 Ke386SetCr2(OldVlm2
);
841 case DbgKdSearchMemoryApi
:
844 Ke386SetCr2(DbgKdSearchMemoryApi
);
848 case DbgKdGetBusDataApi
:
851 Ke386SetCr2(DbgKdGetBusDataApi
);
855 case DbgKdSetBusDataApi
:
858 Ke386SetCr2(DbgKdSetBusDataApi
);
862 case DbgKdCheckLowMemoryApi
:
865 Ke386SetCr2(DbgKdCheckLowMemoryApi
);
869 case DbgKdClearAllInternalBreakpointsApi
:
871 /* Just clear the counter */
872 KdpNumInternalBreakpoints
= 0;
875 case DbgKdFillMemoryApi
:
878 Ke386SetCr2(DbgKdFillMemoryApi
);
882 case DbgKdQueryMemoryApi
:
885 KdpQueryMemory(&ManipulateState
, Context
);
888 case DbgKdSwitchPartition
:
891 Ke386SetCr2(DbgKdSwitchPartition
);
895 /* Unsupported Message */
898 /* Setup an empty message, with failure */
902 ManipulateState
.ReturnStatus
= STATUS_UNSUCCESSFUL
;
905 KdSendPacket(PACKET_TYPE_KD_STATE_MANIPULATE
,
916 KdpReportLoadSymbolsStateChange(IN PSTRING PathName
,
917 IN PKD_SYMBOLS_INFO SymbolInfo
,
919 IN OUT PCONTEXT Context
)
923 DBGKD_WAIT_STATE_CHANGE64 WaitStateChange
;
924 KCONTINUE_STATUS Status
;
926 /* Start wait loop */
929 /* Build the architecture common parts of the message */
930 KdpSetCommonState(DbgKdLoadSymbolsStateChange
,
934 /* Now finish creating the structure */
935 KdpSetContextState(&WaitStateChange
, Context
);
937 /* Fill out load data */
938 WaitStateChange
.u
.LoadSymbols
.UnloadSymbols
= Unload
;
939 WaitStateChange
.u
.LoadSymbols
.BaseOfDll
= (ULONGLONG
)(LONG_PTR
)SymbolInfo
->BaseOfDll
;
940 WaitStateChange
.u
.LoadSymbols
.ProcessId
= SymbolInfo
->ProcessId
;
941 WaitStateChange
.u
.LoadSymbols
.CheckSum
= SymbolInfo
->CheckSum
;
942 WaitStateChange
.u
.LoadSymbols
.SizeOfImage
= SymbolInfo
->SizeOfImage
;
944 /* Check if we have a symbol name */
947 /* Setup the information */
948 WaitStateChange
.u
.LoadSymbols
.PathNameLength
= PathName
->Length
;
949 RtlCopyMemory(KdpPathBuffer
, PathName
->Buffer
, PathName
->Length
);
950 Data
.Buffer
= KdpPathBuffer
;
951 Data
.Length
= WaitStateChange
.u
.LoadSymbols
.PathNameLength
;
957 WaitStateChange
.u
.LoadSymbols
.PathNameLength
= 0;
961 /* Setup the header */
962 Header
.Length
= sizeof(DBGKD_WAIT_STATE_CHANGE64
);
963 Header
.Buffer
= (PCHAR
)&WaitStateChange
;
965 /* Send the packet */
966 Status
= KdpSendWaitContinue(PACKET_TYPE_KD_STATE_CHANGE64
,
970 } while(Status
== ContinueProcessorReselected
);
978 KdpReportExceptionStateChange(IN PEXCEPTION_RECORD ExceptionRecord
,
979 IN OUT PCONTEXT Context
,
980 IN BOOLEAN SecondChanceException
)
983 DBGKD_WAIT_STATE_CHANGE64 WaitStateChange
;
986 /* Start report loop */
989 /* Build the architecture common parts of the message */
990 KdpSetCommonState(DbgKdExceptionStateChange
, Context
, &WaitStateChange
);
992 /* Convert the exception record to 64-bits and set First Chance flag */
993 ExceptionRecordTo64(ExceptionRecord
,
994 &WaitStateChange
.u
.Exception
.ExceptionRecord
);
995 WaitStateChange
.u
.Exception
.FirstChance
= !SecondChanceException
;
997 /* Now finish creating the structure */
998 KdpSetContextState(&WaitStateChange
, Context
);
1000 /* Setup the actual header to send to KD */
1001 Header
.Length
= sizeof(DBGKD_WAIT_STATE_CHANGE64
) - sizeof(CONTEXT
);
1002 Header
.Buffer
= (PCHAR
)&WaitStateChange
;
1004 /* Setup the trace data */
1005 DumpTraceData(&Data
);
1007 /* Send State Change packet and wait for a reply */
1008 Status
= KdpSendWaitContinue(PACKET_TYPE_KD_STATE_CHANGE64
,
1012 } while (Status
== KdPacketNeedsResend
);
1020 KdpTimeSlipDpcRoutine(IN PKDPC Dpc
,
1021 IN PVOID DeferredContext
,
1022 IN PVOID SystemArgument1
,
1023 IN PVOID SystemArgument2
)
1025 LONG OldSlip
, NewSlip
, PendingSlip
;
1027 /* Get the current pending slip */
1028 PendingSlip
= KdpTimeSlipPending
;
1031 /* Save the old value and either disable or enable it now. */
1032 OldSlip
= PendingSlip
;
1033 NewSlip
= OldSlip
> 1 ? 1 : 0;
1035 /* Try to change the value */
1036 } while (InterlockedCompareExchange(&KdpTimeSlipPending
,
1038 OldSlip
) != OldSlip
);
1040 /* If the New Slip value is 1, then do the Time Slipping */
1041 if (NewSlip
) ExQueueWorkItem(&KdpTimeSlipWorkItem
, DelayedWorkQueue
);
1046 KdpTimeSlipWork(IN PVOID Context
)
1049 LARGE_INTEGER DueTime
;
1051 /* Update the System time from the CMOS */
1052 ExAcquireTimeRefreshLock(FALSE
);
1053 ExUpdateSystemTimeFromCmos(FALSE
, 0);
1054 ExReleaseTimeRefreshLock();
1056 /* Check if we have a registered Time Slip Event and signal it */
1057 KeAcquireSpinLock(&KdpTimeSlipEventLock
, &OldIrql
);
1058 if (KdpTimeSlipEvent
) KeSetEvent(KdpTimeSlipEvent
, 0, FALSE
);
1059 KeReleaseSpinLock(&KdpTimeSlipEventLock
, OldIrql
);
1061 /* Delay the DPC until it runs next time */
1062 DueTime
.QuadPart
= -1800000000;
1063 KeSetTimer(&KdpTimeSlipTimer
, DueTime
, &KdpTimeSlipDpc
);
1068 KdpSwitchProcessor(IN PEXCEPTION_RECORD ExceptionRecord
,
1069 IN OUT PCONTEXT ContextRecord
,
1070 IN BOOLEAN SecondChanceException
)
1074 /* Save the port data */
1077 /* Report a state change */
1078 Status
= KdpReportExceptionStateChange(ExceptionRecord
,
1080 SecondChanceException
);
1082 /* Restore the port data and return */
1089 KdpQueryPerformanceCounter(IN PKTRAP_FRAME TrapFrame
)
1091 LARGE_INTEGER Null
= {{0}};
1093 /* Check if interrupts were disabled */
1094 if (!(TrapFrame
->EFlags
& EFLAGS_INTERRUPT_MASK
))
1096 /* Nothing to return */
1100 /* Otherwise, do the call */
1101 return KeQueryPerformanceCounter(NULL
);
1106 KdEnterDebugger(IN PKTRAP_FRAME TrapFrame
,
1107 IN PKEXCEPTION_FRAME ExceptionFrame
)
1111 /* Check if we have a trap frame */
1114 /* Calculate the time difference for the enter */
1115 KdTimerStop
= KdpQueryPerformanceCounter(TrapFrame
);
1116 KdTimerDifference
.QuadPart
= KdTimerStop
.QuadPart
-
1117 KdTimerStart
.QuadPart
;
1121 /* No trap frame, so can't calculate */
1122 KdTimerStop
.QuadPart
= 0;
1125 /* Save the current IRQL */
1126 KeGetCurrentPrcb()->DebuggerSavedIRQL
= KeGetCurrentIrql();
1128 /* Freeze all CPUs */
1129 Entered
= KeFreezeExecution(TrapFrame
, ExceptionFrame
);
1131 /* Lock the port, save the state and set debugger entered */
1132 KdpPortLocked
= KeTryToAcquireSpinLockAtDpcLevel(&KdpDebuggerLock
);
1134 KdEnteredDebugger
= TRUE
;
1136 /* Check freeze flag */
1137 if (KiFreezeFlag
& 1)
1139 /* Print out errror */
1140 DbgPrint("FreezeLock was jammed! Backup SpinLock was used!\n");
1143 /* Check processor state */
1144 if (KiFreezeFlag
& 2)
1146 /* Print out errror */
1147 DbgPrint("Some processors not frozen in debugger!\n");
1150 /* Make sure we acquired the port */
1151 if (!KdpPortLocked
) DbgPrint("Port lock was not acquired!\n");
1153 /* Return enter state */
1159 KdExitDebugger(IN BOOLEAN Entered
)
1163 /* Restore the state and unlock the port */
1165 if (KdpPortLocked
) KdpPortUnlock();
1167 /* Unfreeze the CPUs */
1168 KeThawExecution(Entered
);
1170 /* Compare time with the one from KdEnterDebugger */
1171 if (!KdTimerStop
.QuadPart
)
1173 /* We didn't get a trap frame earlier in so never got the time */
1174 KdTimerStart
= KdTimerStop
;
1178 /* Query the timer */
1179 KdTimerStart
= KeQueryPerformanceCounter(NULL
);
1182 /* Check if a Time Slip was on queue */
1183 TimeSlip
= InterlockedIncrement(&KdpTimeSlipPending
);
1186 /* Queue a DPC for the time slip */
1187 InterlockedIncrement(&KdpTimeSlipPending
);
1188 KeInsertQueueDpc(&KdpTimeSlipDpc
, NULL
, NULL
);
1194 KdEnableDebuggerWithLock(BOOLEAN NeedLock
)
1196 KIRQL OldIrql
= PASSIVE_LEVEL
;
1198 /* Check if we need to acquire the lock */
1202 KeRaiseIrql(DISPATCH_LEVEL
, &OldIrql
);
1206 /* Check if we're not disabled */
1207 if (!KdDisableCount
)
1209 /* Check if we had locked the port before */
1213 KeLowerIrql(OldIrql
);
1217 /* Fail: We're already enabled */
1218 return STATUS_INVALID_PARAMETER
;
1221 /* Decrease the disable count */
1222 if (!(--KdDisableCount
))
1224 /* We're now enabled again! Were we enabled before, too? */
1225 if (KdPreviouslyEnabled
)
1227 /* Reinitialize the Debugger */
1228 KdInitSystem(0, NULL
) ;
1229 KdpRestoreAllBreakpoints();
1233 /* Check if we had locked the port before */
1236 /* Yes, now unlock it */
1237 KeLowerIrql(OldIrql
);
1242 return STATUS_SUCCESS
;
1245 /* PUBLIC FUNCTIONS **********************************************************/
1252 KdEnableDebugger(VOID
)
1254 /* Use the internal routine */
1256 return KdEnableDebuggerWithLock(TRUE
);
1264 KdSystemDebugControl(IN SYSDBG_COMMAND Command
,
1265 IN PVOID InputBuffer
,
1266 IN ULONG InputBufferLength
,
1267 OUT PVOID OutputBuffer
,
1268 IN ULONG OutputBufferLength
,
1269 IN OUT PULONG ReturnLength
,
1270 IN KPROCESSOR_MODE PreviousMode
)
1273 return STATUS_SUCCESS
;
1281 KdChangeOption(IN KD_OPTION Option
,
1282 IN ULONG InBufferBytes OPTIONAL
,
1284 IN ULONG OutBufferBytes OPTIONAL
,
1285 OUT PVOID OutBuffer
,
1286 OUT PULONG OutBufferNeeded OPTIONAL
)
1289 return STATUS_SUCCESS
;
1297 KdPowerTransition(IN DEVICE_POWER_STATE NewState
)
1300 return STATUS_SUCCESS
;
1308 KdDisableDebugger(VOID
)
1311 return STATUS_SUCCESS
;
1319 KdRefreshDebuggerNotPresent(VOID
)
1322 return KdDebuggerNotPresent
;
1327 NtQueryDebugFilterState(ULONG ComponentId
,
1331 return STATUS_SUCCESS
;
1336 NtSetDebugFilterState(ULONG ComponentId
,
1341 return STATUS_SUCCESS
;