⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 bp.c

📁 Linux下的类似softice的调试工具
💻 C
📖 第 1 页 / 共 3 页
字号:
/****************************************************************************** * * Copyright (c) 2003 Gerhard W. Gruber * * PROJECT: pICE * $Source: /cvsroot/pice/pice/module/bp.c,v $ * $Revision: 1.5 $ * $Date: 2004/02/17 23:07:36 $ * $Author: lightweave $ * $Name:  $ * * $Log: bp.c,v $ * Revision 1.5  2004/02/17 23:07:36  lightweave * * Improved the DEBUG facillity and replaced the configuration handler with a * new code which now can read MS Windows INI style files. See CHANGES.txt for * more details. * Also added a macro which prevents compiling for kernels before 2.4.19. * * Revision 1.4  2003/06/18 22:00:21  lightweave * DEBUG and DEBUG_SERIAL added * * *****************************************************************************/static char *ident = "$Header: /cvsroot/pice/pice/module/bp.c,v 1.5 2004/02/17 23:07:36 lightweave Exp $";/*++Copyright (c) 1998-2001 Klaus P. GerlicherModule Name:    bp.cAbstract:    setting, listing and removing breakpointsEnvironment:    LINUX 2.2.X    Kernel mode onlyAuthor:     Klaus P. GerlicherRevision History:    13-Nov-1999:	created    15-Nov-2000:    general cleanup of source filesCopyright notice:  This file may be distributed under the terms of the GNU Public License.--*/////////////////////////////////////////////////////// INCLUDES////#include "remods.h"#include <asm/io.h>#include <linux/interrupt.h>#include "precomp.h"////////////////////////////////////////////////////// DEFINES////#define BREAKPOINT_VECTOR (0x03)#define	INT3_OPCODE	(0xCC)////////////////////////////////////////////////////// GLOBALS////ULONG ulOldBreakpointHandler=0;SW_BP aSwBreakpoints[64]={{0,0,0,0},};static char tempBp[256];//************************************************************************* // HandleHardwareBreakPoint() // //************************************************************************* ULONG HandleHardwareBreakPoint(EXCEPTION_FRAME* pFrame){   ULONG ulReason;   ENTER_FUNC();    // disable HW breakpoints    __asm__(        "movl %%dr6,%%eax\n"        "movl %%eax,%0\n"	"xorl %%eax,%%eax\n"	"movl %%eax,%%dr6 \n"	"movl %%eax,%%dr7\n"	:"=m" (ulReason)	:        :"eax"    );     DPRINT(PICE_DEBUG, DBT_BP, DBL_INFO,"REASON_HARDWARE_BP: %x\n", (ulReason&0xF));    // HW breakpoint DR1 (skip: only used in init_module detection)    if(ulReason&0x2)    {        pFrame->eflags |= RF_FLAG; // set resume flag		TryToInstallVirtualSWBreakpoints();		LEAVE_FUNC();		return 0;    }    // HW breakpoint DR0    else if(ulReason&0x1)    {    	ULONG ulAddressCurrent;        // we came here while stepping through source code block        if(bStepThroughSource)        {            ULONG ulLineNumber;            LPSTR pSrc,pFileName;            DPRINT(PICE_DEBUG, DBT_BP, DBL_INFO, "stepping through source! [2]\n");	    	ulAddressCurrent = GetLinearAddress(pFrame->cs,pFrame->eip);            // look up the corresponding source line            // if there isn't any or the source line number has changed            // we break back into the debugger			if(bShowSrc)		        pSrc = FindSourceLineForAddress(ulAddressCurrent,&ulLineNumber,NULL,NULL,&pFileName);			else				pSrc = NULL;            DPRINT(PICE_DEBUG, DBT_BP, DBL_INFO,"line #%u pSrc=%x (old line #%u) [2]\n",ulLineNumber,(ULONG)pSrc,g_ulLineNumberStart);            // if we have found a source line there            if(pSrc && ulLineNumber==g_ulLineNumberStart)            {                DPRINT(PICE_DEBUG, DBT_BP, DBL_INFO,"stepping through line #%u in file = %s! [2]\n",ulLineNumber,pFileName);                if(bStepInto)                    StepInto(pFrame,NULL);                else                    StepOver(pFrame,NULL);								return 0;            }            bStepThroughSource = FALSE;        }        bNotifyToExit = FALSE;     }	LEAVE_FUNC();	return 1;}//************************************************************************* // HandleSoftwareBreakPoint() // //************************************************************************* ULONG HandleSoftwareBreakPoint(EXCEPTION_FRAME* pFrame){	ULONG ulAddress;	LPSTR pFind;    ENTER_FUNC();    // make a flat address	ulAddress = GetLinearAddress(pFrame->cs,pFrame->eip - 1);    DPRINT(PICE_DEBUG, DBT_BP, DBL_INFO,"INT3 @ %.8X\n",ulAddress);	// decide if this breakpoint has been set by PICE or is embedded    // if there's a breakpoint installed at current EIP remove it    if(DeInstallSWBreakpoint(ulAddress) )    {        PSW_BP p;	DPRINT(PICE_DEBUG, DBT_BP, DBL_INFO,"INT3 @ %.8X removed\n", ulAddress);	// need to go back to start of INT3 instruction	pFrame->eip--;        // if it's permanent (must be Printk() ) skip the DebuggerShell() and        // do a callback        if( (p = IsPermanentSWBreakpoint(ulAddress)) )        {	   DPRINT(PICE_DEBUG, DBT_BP, DBL_INFO, "permanent breakpoint\n");           	   OldFrame[GetProcessor()].cs	= pFrame->cs;	   OldFrame[GetProcessor()].eip = pFrame->eip;	   // if there's a callback for the breakpoint, call it	   if(p->Callback)	      (*p->Callback)(pFrame);	   // skip DebuggerShell()	   LEAVE_FUNC();	   return 0;        }        else        {            if(FindSymbolByAddress(&pFind,GetLinearAddress(pFrame->cs,pFrame->eip)))            {			    PICE_sprintf(tempBp,"pICE: SW Breakpoint at %s (%.4X:%.8X)\n",pFind,pFrame->cs,pFrame->eip);             }            else            {			    PICE_sprintf(tempBp,"pICE: SW Breakpoint at %.4X:%.8X\n",pFrame->cs,pFrame->eip);             }			SetForegroundColor(COLOR_HILITE);			Print(OUTPUT_WINDOW,tempBp); 			ResetColor();        }    }    else    {		// we don't want to catch the breakpoint		if(!bInt3Here)		{			LEAVE_FUNC();			// let system catch it			return 0;		}				// is this userland?        if(ulAddress < TASK_SIZE)                    {			// usermode debugger attached, so let it catch the breakpoint#if LINUX_VERSION_CODE < 0x020400                                                    			if(current->flags & PF_PTRACED)#else // LINUX_VERSION_CODE                                                                                 			if(current->ptrace & PT_PTRACED)           #endif // LINUX_VERSION_CODE                                                                                			{				LEAVE_FUNC();				// let system catch it				return 0;			}			if(FindSymbolByAddress(&pFind,GetLinearAddress(pFrame->cs,pFrame->eip - 1)))			{				PICE_sprintf(tempBp,"pICE: break due to embedded INT 3 at %s (%.4X:%.8X)\n",pFind,pFrame->cs,pFrame->eip - 1); 			}			else			{				PICE_sprintf(tempBp,"pICE: break due to embedded INT 3 at user-mode address %.4X:%.8X\n",pFrame->cs,pFrame->eip - 1); 			}			SetForegroundColor(COLOR_HILITE);			Print(OUTPUT_WINDOW,tempBp); 			ResetColor();        }		// we're in kernel-mode        else        {            if(FindSymbolByAddress(&pFind,GetLinearAddress(pFrame->cs,pFrame->eip - 1)))            {	    		PICE_sprintf(tempBp,"pICE: break due to embedded INT 3 at (%s) %.4X:%.8X\n",                             pFind,pFrame->cs,pFrame->eip - 1);             }            else            {	    		PICE_sprintf(tempBp,"pICE: break due to embedded INT 3 at kernel-mode address %.4X:%.8X\n",                             pFrame->cs,pFrame->eip - 1);             }			SetForegroundColor(COLOR_HILITE);			Print(OUTPUT_WINDOW,tempBp); 			ResetColor();        }    }	LEAVE_FUNC();	// let noone else see it	return 1;}//************************************************************************* // FindSwBp() // //************************************************************************* PSW_BP FindSwBp(ULONG ulAddress){    ULONG i;	ENTER_FUNC();    for(i=0;i<DIM(aSwBreakpoints);i++)    {        if(aSwBreakpoints[i].ulAddress == ulAddress && aSwBreakpoints[i].bUsed==TRUE && aSwBreakpoints[i].bVirtual==FALSE)	{	   DPRINT(PICE_DEBUG, DBT_BP, DBL_INFO,"FindSwBp(): found index %u\n",i);	   return &aSwBreakpoints[i];	}    }    DPRINT(PICE_DEBUG, DBT_BP, DBL_INFO,"not found\n");	LEAVE_FUNC();    return NULL;}//************************************************************************* // FindEmptySwBpSlot() // //************************************************************************* PSW_BP FindEmptySwBpSlot(void){    ULONG i;    for(i=0;i<(sizeof(aSwBreakpoints)/sizeof(SW_BP));i++)    {        if(aSwBreakpoints[i].bUsed == FALSE)        {            return &aSwBreakpoints[i];        }    }    return NULL;}//************************************************************************* // FindVirtualSwBp() // //************************************************************************* PSW_BP FindVirtualSwBp(LPSTR ModName,LPSTR szFunctionName){    ULONG i;    PSW_BP p;    for(i=0;i<(sizeof(aSwBreakpoints)/sizeof(SW_BP));i++)    {        p = &aSwBreakpoints[i];        if(p->bUsed == TRUE &&           p->bVirtual == TRUE &&           PICE_strcmpi(p->szModName,ModName)==0 &&           PICE_strcmpi(p->szFunctionName,szFunctionName)==0)        {            return p;        }    }    return NULL;}//************************************************************************* // PageSwapInReInstallSWBreakpoints() // //************************************************************************* BOOLEAN PageSwapInReInstallSWBreakpoints(ULONG ulAddress){   PSW_BP p;   ULONG i;   BOOLEAN rc = FALSE;   ENTER_FUNC();   p = aSwBreakpoints;   for(i=0;i<(sizeof(aSwBreakpoints)/sizeof(SW_BP));i++,p++)   {		if(PICE_strcmpi(p->szProcessName,current->comm) == 0 )		{			 DPRINT(PICE_DEBUG, DBT_BP, DBL_INFO,"candidate process %s found\n", p->szProcessName);			 DPRINT(PICE_DEBUG, DBT_BP, DBL_INFO,"bUsed %x bInstalled %x bVirtual %x\n", p->bUsed, p->bInstalled, p->bVirtual);			 DPRINT(PICE_DEBUG, DBT_BP, DBL_INFO,"%lx == %lx?\n", ulAddress & PAGE_MASK, p->ulAddress & PAGE_MASK);			 if(p->bUsed == TRUE && p->bInstalled == FALSE && p->bVirtual == TRUE && 				((ulAddress & PAGE_MASK) == (p->ulAddress & PAGE_MASK)) )			 {				  DPRINT(PICE_DEBUG, DBT_BP, DBL_INFO, "candidate %x found\n",p->ulAddress);				  rc = TRUE;				  goto Quit;			 }		}   }   Quit:   LEAVE_FUNC();      return FALSE;}//************************************************************************* // ReInstallSWBreakpointsInPage() // //************************************************************************* void ReInstallSWBreakpointsInPage(ULONG ulAddress){   PSW_BP p;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -