Windows內核調試器原理淺析(一)
——
在進一步介紹WinDBG之前,先介紹兩個函數:KdpTrace、KdpStub,我在《windows異常處理流程》一文里簡單提過這兩個函數。現在再提一下,當異常發生于內核態下,會調用KiDebugRoutine兩次,異常發生于用戶態下,會調用KiDebugRoutine一次,而且第一次調用都是剛開始處理異常的時候。
當WinDBG未被加載時KiDebugRoutine為KdpStub,處理也很簡單,主要是對由int 0x2d引起的異常如DbgPrint、DbgPrompt、加載卸載SYMBOLS(關于int 0x2d引起的異常將在后面詳細介紹)等,把Context.Eip加1,跳過int 0x2d后面跟著的int 0x3指令。
真正實現了WinDBG功能的函數是KdpTrap,它負責處理所有STATUS_BREAKPOINT和STATUS_SINGLE_STEP(單步)異常。STATUS_BREAKPOINT的異常包括int 0x3、DbgPrint、DbgPrompt、加載卸載SYMBOLS。DbgPrint的處理最簡單,KdpTrap直接向調試器發含有字符串的包。DbgPrompt因為是要輸出并接收字符串,所以先將含有字符串的包發送出去,再陷入循環等待接收來自調試器的含有回復字符串的包。SYMBOLS的加載和卸載通過調用KdpReportSymbolsStateChange,int 0x3斷點異常和int 0x1單步異常(這兩個異常基本上是內核調試器處理得最多的異常)通過調用KdpReportExceptionStateChange,這兩個函數很相似,都是通過調用KdpSendWaitContinue函數。KdpSendWaitContinue可以說是內核調試器功能的大管家,負責各個功能的分派。這個函數向內核調試器發送要發送的信息,比如當前所有寄存器狀態,每次單步后我們都可以發現寄存器的信息被更新,就是內核調試器接受它發出的包含最新機器狀態的包;還有SYMBOLS的狀態,這樣加載和卸載了SYMBOLS我們都能在內核調試器里看到相應的反應。然后KdpSendWaitContinue等待從內核調試器發來的包含命令的包,決定下一步該干什么。讓我們來看看KdpSendWaitContinue都能干些什么:
case DbgKdReadVirtualMemoryApi:
KdpReadVirtualMemory(&ManipulateState,&MessageData,ContextRecord);
break;
case DbgKdReadVirtualMemory64Api:
KdpReadVirtualMemory64(&ManipulateState,&MessageData,ContextRecord);
break;
case DbgKdWriteVirtualMemoryApi:
KdpWriteVirtualMemory(&ManipulateState,&MessageData,ContextRecord);
break;
case DbgKdWriteVirtualMemory64Api:
KdpWriteVirtualMemory64(&ManipulateState,&MessageData,ContextRecord);
break;
case DbgKdReadPhysicalMemoryApi:
KdpReadPhysicalMemory(&ManipulateState,&MessageData,ContextRecord);
break;
case DbgKdWritePhysicalMemoryApi:
KdpWritePhysicalMemory(&ManipulateState,&MessageData,ContextRecord);
break;
case DbgKdGetContextApi:
KdpGetContext(&ManipulateState,&MessageData,ContextRecord);
break;
case DbgKdSetContextApi:
KdpSetContext(&ManipulateState,&MessageData,ContextRecord);
break;
case DbgKdWriteBreakPointApi:
KdpWriteBreakpoint(&ManipulateState,&MessageData,ContextRecord);
break;
case DbgKdRestoreBreakPointApi:
KdpRestoreBreakpoin(&ManipulateState,&MessageData,ContextRecord);
break;
case DbgKdReadControlSpaceApi:
KdpReadControlSpace(&ManipulateState,&MessageData,ContextRecord);
break;
case DbgKdWriteControlSpaceApi:
KdpWriteControlSpace(&ManipulateState,&MessageData,ContextRecord);
break;
case DbgKdReadIoSpaceApi:
KdpReadIoSpace(&ManipulateState,&MessageData,ContextRecord);
break;
case DbgKdWriteIoSpaceApi:
KdpWriteIoSpace(&ManipulateState,&MessageData,ContextRecord);
break;
--------------------------------------------------------------------------------
評論