Win32調(diào)試API第三部分

字號:

理論:
    如果你以前使用過調(diào)試器,那么你應(yīng)對跟蹤比較熟悉。當(dāng)"跟蹤"一個(gè)程序時(shí),程序在每執(zhí)行一條指令后將會停止,這使你有機(jī)會去檢查寄存器/內(nèi)存中的值。這種單步運(yùn)行的官方定義為跟蹤(tracing)。
    單步運(yùn)行的特色是由CPU本身提供的。標(biāo)志寄存器的第8位稱為陷阱標(biāo)志trap flag。如果該位設(shè)置,則CPU運(yùn)行于單步模式。CPU將在每條指令后產(chǎn)生一個(gè)debug異常。當(dāng)debug 異常產(chǎn)生后,陷阱標(biāo)志自動(dòng)清除。利用win32調(diào)試api,我們也可以單步運(yùn)行被調(diào)試程序。方法如下:
    調(diào)用GetThreadContext, 指定 ContextFlags為CONTEXT_CONTROL, 來獲得標(biāo)志寄存器的值
    設(shè)置CONTEXT結(jié)構(gòu)成員標(biāo)志寄存器regFlag中的陷阱標(biāo)志位
    調(diào)用 SetThreadContext
    等待調(diào)式事件。被調(diào)試程序?qū)磫尾侥J綀?zhí)行,在每執(zhí)行一條指令后,我們將得到調(diào)試 事件,u.Exception.pExceptionRecord.ExceptionCode值為EXCEPTION_SINGLE_STEP
    如果要跟蹤下一條指令,需要再次設(shè)置陷阱標(biāo)志位。
    例:
    .386
    .model flat,stdcall
    option casemap:none
    include \masm32\include\windows.inc
    include \masm32\include\kernel32.inc
    include \masm32\include\comdlg32.inc
    include \masm32\include\user32.inc
    includelib \masm32\lib\kernel32.lib
    includelib \masm32\lib\comdlg32.lib
    includelib \masm32\lib\user32.lib
    .data
    AppName db "Win32 Debug Example no.4",0
    ofn OPENFILENAME <>
    FilterString db "Executable Files",0,"*.exe",0
    db "All Files",0,"*.*",0,0
    ExitProc db "The debuggee exits",0Dh,0Ah
    db "Total Instructions executed : %lu",0
    TotalInstruction dd 0
    .data?
    buffer db 512 dup(?)
    startinfo STARTUPINFO <>
    pi PROCESS_INFORMATION <>
    DBEvent DEBUG_EVENT <>
    context CONTEXT <>
    .code
    start:
    mov ofn.lStructSize,SIZEOF ofn
    mov ofn.lpstrFilter, OFFSET FilterString
    mov ofn.lpstrFile, OFFSET buffer
    mov ofn.nMaxFile,512
    mov ofn.Flags, OFN_FILEMUSTEXIST or OFN_PATHMUSTEXIST or OFN_LONGNAMES or OFN_EXPLORER or OFN_HIDEREADONLY
    invoke GetOpenFileName, ADDR ofn
    .if eax==TRUE
    invoke GetStartupInfo,addr startinfo
    invoke CreateProcess, addr buffer, NULL, NULL, NULL, FALSE, DEBUG_PROCESS+ DEBUG_ONLY_THIS_PROCESS, NULL, NULL, addr startinfo, addr pi
    .while TRUE
    invoke WaitForDebugEvent, addr DBEvent, INFINITE
    .if DBEvent.dwDebugEventCode==EXIT_PROCESS_DEBUG_EVENT
    invoke wsprintf, addr buffer, addr ExitProc, TotalInstruction
    invoke MessageBox, 0, addr buffer, addr AppName, MB_OK+MB_ICONINFORMATION
    .break
    .elseif DBEvent.dwDebugEventCode==EXCEPTION_DEBUG_EVENT .if DBEvent.u.Exception.pExceptionRecord.ExceptionCode==EXCEPTION_BREAKPOINT
    mov context.ContextFlags, CONTEXT_CONTROL
    invoke GetThreadContext, pi.hThread, addr context
    or context.regFlag,100h
    invoke SetThreadContext,pi.hThread, addr context
    invoke ContinueDebugEvent, DBEvent.dwProcessId, DBEvent.dwThreadId, DBG_CONTINUE
    .continue
    .elseif DBEvent.u.Exception.pExceptionRecord.ExceptionCode==EXCEPTION_SINGLE_STEP
    inc TotalInstruction
    invoke GetThreadContext,pi.hThread,addr context or context.regFlag,100h
    invoke SetThreadContext,pi.hThread, addr context
    invoke ContinueDebugEvent, DBEvent.dwProcessId, DBEvent.dwThreadId,DBG_CONTINUE
    .continue
    .endif
    .endif
    invoke ContinueDebugEvent, DBEvent.dwProcessId, DBEvent.dwThreadId, DBG_EXCEPTION_NOT_HANDLED
    .endw
    .endif
    invoke CloseHandle,pi.hProcess
    invoke CloseHandle,pi.hThread
    invoke ExitProcess, 0
    end start
    分析:
    該程序先顯示一個(gè)打開文件對話框,當(dāng)用戶選擇了一個(gè)可執(zhí)行文件,它將單步執(zhí)行該程序,并記錄執(zhí)行的指令數(shù),直到被調(diào)試程序退出運(yùn)行。
    .elseif DBEvent.dwDebugEventCode==EXCEPTION_DEBUG_EVENT .if DBEvent.u.Exception.pExceptionRecord.ExceptionCode==EXCEPTION_BREAKPOINT
    利用該機(jī)會來設(shè)置被調(diào)試程序?yàn)閱尾竭\(yùn)行模式。記住,在執(zhí)行被調(diào)試程序的第一條指令前 windows將發(fā)送一個(gè)EXCEPTION_BREAKPOINT消息。
    mov context.ContextFlags, CONTEXT_CONTROL
    invoke GetThreadContext, pi.hThread, addr context
    調(diào)用GetThreadContext,以被調(diào)試程序的當(dāng)前寄存器內(nèi)容來填充CONTEXT 結(jié)構(gòu) 特別地,我們需要標(biāo)志寄存器的當(dāng)前值。
    or context.regFlag,100h
    設(shè)置標(biāo)志寄存器映象的陷阱位(第8位)
    invoke SetThreadContext,pi.hThread, addr context
    invoke ContinueDebugEvent, DBEvent.dwProcessId, DBEvent.dwThreadId, DBG_CONTINUE
    .continue
    然后調(diào)用SetThreadContext去覆蓋CONTEXT的值。再以DBG_CONTINUE調(diào)用 ContinueDebugEvent 來恢復(fù)被調(diào)試程序的運(yùn)行。
    .elseif DBEvent.u.Exception.pExceptionRecord.ExceptionCode==EXCEPTION_SINGLE_STEP
    inc TotalInstruction
    當(dāng)調(diào)試程序中一條指令執(zhí)行后,我們將接收到EXCEPTION_DEBUG_EVENT的調(diào)試事件, 必須要檢查u.Exception.pExceptionRecord.ExceptionCode的值。如果該值為 EXCEPTION_SINGLE_STEP,那么,該調(diào)試事件是單步運(yùn)行模式造成的。在這種情況 下,TotalInstruction加一,因?yàn)槲覀兇_切地知道此時(shí)被調(diào)試程序執(zhí)行了一條指令。
    invoke GetThreadContext,pi.hThread,addr context or context.regFlag,100h
    invoke SetThreadContext,pi.hThread, addr context
    invoke ContinueDebugEvent, DBEvent.dwProcessId, DBEvent.dwThreadId,DBG_CONTINUE
    .continue
    由于陷阱標(biāo)志在debug異常后自動(dòng)清除了,如果我們需要繼續(xù)保持單步運(yùn)行模式,則必須設(shè)置陷阱標(biāo)志位。
    警告: 不要用本教程中的此例子來調(diào)試大程序: 跟蹤是很慢的。你或許需要等待10 多分鐘才能關(guān)閉被調(diào)試程序。