📄 kdbreak.c
字号:
/*++Copyright (c) 1990-2000 Microsoft Corporation. All rights reserved.Module Name: kdbreak.cAbstract: This module implements machine dependent functions to add and delete breakpoints from the kernel debugger breakpoint table.Revision History:--*/#include "kdp.h"extern PROCESS *kdProcArray;#ifdef ALLOC_PRAGMA#pragma alloc_text(PAGEKD, KdpAddBreakpoint)#pragma alloc_text(PAGEKD, KdpDeleteBreakpoint)#pragma alloc_text(PAGEKD, KdpDeleteBreakpointRange)#endif// The following variables are global for a reason. Do not move them to the stack or bad things happen// when flushing instructions.KDP_BREAKPOINT_TYPE Content;KDP_BREAKPOINT_TYPE KContent;ULONG KdpAddBreakpoint(IN PVOID Address)/*++Routine Description: This routine adds an entry to the breakpoint table and returns a handle to the breakpoint table entry.Arguments: Address - Supplies the address where to set the breakpoint.Return Value: A value of zero is returned if the specified address is already in the breakpoint table, there are no free entries in the breakpoint table, the specified address is not correctly aligned, or the specified address is not valid. Otherwise, the index of the assigned breakpoint table entry plus one is returned as the function value.--*/{ ULONG Handle = 0; ULONG Index; BOOLEAN Accessible = FALSE;#ifdef ARM BOOLEAN Mode16Bit = FALSE; // used for ARM/Thumb#endif BOOLEAN KAccessible = FALSE; PVOID KAddress = NULL; KDP_BREAKPOINT_TYPE KdpBreakpointInstruction = KDP_BREAKPOINT_VALUE; ULONG Length = sizeof(KDP_BREAKPOINT_TYPE);#if defined(THUMBSUPPORT) // // update the breakpoint Instruction and Length if stopped within // 16-bit code. (16-bit code indicated by LSB of Address) // if (((ULONG)Address & 1) != 0) { DEBUGGERMSG( KDZONE_BREAK,(L"16 Bit breakpoint %8.8lx\r\n", Address)); Length = sizeof(KDP_BREAKPOINT_16BIT_TYPE); KdpBreakpointInstruction = KDP_BREAKPOINT_16BIT_VALUE; Address = (PVOID) ((ULONG)Address & ~1); Mode16Bit = TRUE; } #endif Content = 0; KContent = 0; // // If the specified address is not properly aligned, then return zero. // DEBUGGERMSG(KDZONE_BREAK,(L"Trying to set BP at %8.8lx\r\n", Address)); if (((ULONG)Address & (Length-1)) != 0) { DEBUGGERMSG(KDZONE_BREAK, (L"Address not aligned\r\n")); return 0; } if ( (((ulong)Address & 0x80000000) == 0) && ZeroPtr(Address) >= (ULONG)DllLoadBase) { // If Addr is not physical and Address is in DLL shared space then Get Kernel Address (slot 0) DEBUGGERMSG( KDZONE_BREAK,(L"Is Dll %8.8lx ", Address)); KAddress = (PVOID)(ZeroPtr(Address) + kdProcArray[0].dwVMBase); // Get Slot 0 (current process) address based DEBUGGERMSG( KDZONE_BREAK,(L"converted to %8.8lx \r\n", KAddress)); } // // Get the instruction to be replaced. If the instruction cannot be read, // then mark breakpoint as not accessible. // if (KdpMoveMemory( (PCHAR)&Content, (PCHAR)Address, Length ) != Length) { Accessible = FALSE; } else { DEBUGGERMSG(KDZONE_BREAK,(L"Successfully read %8.8lx at %8.8lx \r\n", Content, Address)); Accessible = TRUE; } // if we got a Kernel Address: try to get its instruction if (KAddress != NULL) { if (KdpMoveMemory( (PCHAR)&KContent, (PCHAR)KAddress, Length ) != Length) { KAccessible = FALSE; } else { DEBUGGERMSG(KDZONE_BREAK,(L"Successfully read %8.8lx at %8.8lx \r\n", Content, KAddress)); KAccessible = TRUE; } if (Content != KContent) { // assert(FALSE); // if contents are different DEBUGGERMSG(KDZONE_BREAK,(L"Content %8.8lx != KContent at %8.8lx \r\n", Content, KContent, KAddress)); if (!Content) { Content = KContent; DEBUGGERMSG(KDZONE_BREAK,(L"Set Content to %8.8lx \r\n", KContent)); } } } // // Search the breakpoint table for a free entry and check if the specified // address is already in the breakpoint table. // if (Content == KdpBreakpointInstruction) { DEBUGGERMSG( KDZONE_BREAK,(L"Already found a BP %8.8lx \r\n",Address)); for (Index = 0; Index < BREAKPOINT_TABLE_SIZE; Index += 1) { if (KdpBreakpointTable[Index].Address == Address || (KAddress != NULL && KdpBreakpointTable[Index].KAddress == KAddress)) { Handle = Index + 1; DEBUGGERMSG( KDZONE_BREAK,(L"return Handle %d\r\n", Handle)); return Handle; } } }#if 0 NKOtherPrintfW(L"Add, Before\r\n"); for (Index = 0; Index < 3; Index += 1) { NKOtherPrintfW(L"table[%i].flags = %i, Addr = %x, KAddr = %x, Content = %x\r\n", Index, KdpBreakpointTable[Index].Flags, KdpBreakpointTable[Index].Address, KdpBreakpointTable[Index].KAddress, KdpBreakpointTable[Index].Content); }#endif for (Index = 0; Index < BREAKPOINT_TABLE_SIZE; Index += 1) { if (KdpBreakpointTable[Index].Flags == 0 ) { Handle = Index + 1; break; } } // // If a free entry was found, then write breakpoint and return the handle // value plus one. Otherwise, return zero. // if (Handle) { if ( Accessible || KAccessible) { // // If the specified address is not write accessible, then return zero. // if (!DbgVerify(Address, DV_SETBP)) { DEBUGGERMSG(KDZONE_BREAK, (L"Addresses not writable %8.8lx %8.8lx\r\n", Address, KAddress)); Address = NULL; } if (KAddress != NULL && !DbgVerify(KAddress, DV_SETBP)) { DEBUGGERMSG(KDZONE_BREAK, (L"Addresses not writable %8.8lx %8.8lx\r\n", Address, KAddress)); KAddress = NULL; } KdpBreakpointTable[Handle - 1].Address = Address; KdpBreakpointTable[Handle - 1].KAddress = KAddress; KdpBreakpointTable[Handle - 1].Content = Content; KdpBreakpointTable[Handle - 1].Flags = KD_BREAKPOINT_IN_USE;#if defined(THUMBSUPPORT) if (Mode16Bit){ KdpBreakpointTable[Handle-1].Flags |= KD_BREAKPOINT_16BIT; }#endif//jvp#if 0 NKOtherPrintfW(L"Add, After\r\n"); for (Index = 0; Index < 3; Index += 1) { NKOtherPrintfW(L"table[%i].flags = %i, Addr = %x, KAddr = %x, Content = %x\r\n", Index, KdpBreakpointTable[Index].Flags, KdpBreakpointTable[Index].Address, KdpBreakpointTable[Index].KAddress, KdpBreakpointTable[Index].Content); }#endif if (!bGlobalBreakPoint) KdpBreakpointTable[Handle - 1].pThrd = pCurThread; else KdpBreakpointTable[Handle - 1].pThrd = 0; if (Address != NULL) { if (KdpMoveMemory( (PCHAR)Address, (PCHAR)&KdpBreakpointInstruction, Length ) == Length) { DEBUGGERMSG(KDZONE_BREAK,(L"Successfully Set BP Handle %x %8.8lx\r\n", Handle - 1, Address)); } else { DEBUGGERMSG(KDZONE_BREAK,(L"Failed to Set BP Handle %x %8.8lx\r\n", Handle - 1, Address)); } } if (KAddress != NULL) { if (KdpMoveMemory( (PCHAR)KAddress, (PCHAR)&KdpBreakpointInstruction, Length ) == Length ) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -