基本的调试器框架

逻辑

逻辑

关键的API、数据结构

//调试事件
typedef struct _DEBUG_EVENT {
  DWORD dwDebugEventCode;
  DWORD dwProcessId;
  DWORD dwThreadId;
  union {
    EXCEPTION_DEBUG_INFO Exception;
    CREATE_THREAD_DEBUG_INFO CreateThread;
    CREATE_PROCESS_DEBUG_INFO CreateProcessInfo;
    EXIT_THREAD_DEBUG_INFO ExitThread;
    EXIT_PROCESS_DEBUG_INFO ExitProcess;
    LOAD_DLL_DEBUG_INFO LoadDll;
    UNLOAD_DLL_DEBUG_INFO UnloadDll;
    OUTPUT_DEBUG_STRING_INFO DebugString;
    RIP_INFO RipInfo;
  } u;
} DEBUG_EVENT, *LPDEBUG_EVENT;

CreateProcess

WaitForDebugEvent

ContinueDebugEvent

效果

效果

代码

#include "stdafx.h"
#include <iostream>
#include <Windows.h>
using namespace std;

void main(){
    PROCESS_INFORMATION process_info;
    STARTUPINFO startup_info;
    memset(&process_info, 0, sizeof(process_info));
    memset(&startup_info, 0, sizeof(startup_info));
    startup_info.cb = sizeof(STARTUPINFO);
    if(CreateProcess(TEXT("E:\\myCode\\helloworld\\Debug\\helloworld.exe"),NULL, NULL, NULL, FALSE,
        DEBUG_ONLY_THIS_PROCESS | CREATE_NEW_CONSOLE,NULL, NULL, &startup_info, &process_info)){
            //ResumeThread(process_info.hThread);
            while(true){
                DEBUG_EVENT debug_info;
                if(!WaitForDebugEvent(&debug_info, INFINITE))
                    break;
                switch (debug_info.dwDebugEventCode)
                {
                case CREATE_PROCESS_DEBUG_EVENT://创建进程
                    cout<<"CREATE_PROCESS_DEBUG_EVENT"<<endl;
                    break;
                case CREATE_THREAD_DEBUG_EVENT://创建线程
                    cout<<"CREATE_THREAD_DEBUG_EVENT"<<endl;
                    break;
                case EXIT_THREAD_DEBUG_EVENT://退出线程
                    cout<<"EXIT_THREAD_DEBUG_EVENT"<<endl;
                    break;
                case EXIT_PROCESS_DEBUG_EVENT://退出进程
                    cout<<"EXIT_PROCESS_DEBUG_EVENT"<<endl;
                    break;
                case EXCEPTION_DEBUG_EVENT://发生异常
                    cout<<"EXCEPTION_DEBUG_EVENT"<<endl;
                    break;
                case OUTPUT_DEBUG_STRING_EVENT://调用OutputDebugString函数
                    cout<<"OUTPUT_DEBUG_STRING_EVENT"<<endl;
                    break;
                case RIP_EVENT://发生系统调试错误
                    cout<<"RIP_EVENT"<<endl;
                    break;
                case LOAD_DLL_DEBUG_EVENT://加载dll
                    cout<<"LOAD_DLL_DEBUG_EVENT"<<endl;
                    break;
                case UNLOAD_DLL_DEBUG_EVENT://卸载dll
                    cout<<"UNLOAD_DLL_DEBUG_EVENT"<<endl;
                    break;
                }
                if(debug_info.dwDebugEventCode == EXIT_PROCESS_DEBUG_EVENT)
                    break;
                ContinueDebugEvent(debug_info.dwProcessId, debug_info.dwThreadId, DBG_CONTINUE);
            }
            CloseHandle(process_info.hThread);
            CloseHandle(process_info.hProcess);
    }else{
        cout<<"Can't create process."<<endl;
    }
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值