典型技术

debug method

日志

常用方案

  • printf : 具有隐藏锁,性能不咋地,最原始

  • CLFS : windows 系统支持的日志系统

  • ETW (Event Tracing for Windows): 是Windows提供的原生的事件跟踪日志系统。由于采用内核(Kernel)层面的缓冲和日志记录机制,所以ETW提供了一种非常高效的事件跟踪日志解决方案。

ETW

事件追踪

etw

  • Provider : 事件提供者,会提前在 ETW 框架中进行注册。提供者程序在某个时刻触发事件,并将标准化定义的事件提供给ETW框架。
  • Consumer : 解析跟踪文件(ETL 文件)或者实时捕获活动跟踪会话中的事件,并处理事件。官方推荐软件 Windows Performance Analyzer (WPA)
  • Event Tracing Session : ETW 框架的核心,作为 Provider 与 Consumer 的中介,负责事件的转发。
  • Controller : 控制 ETW 框架的启动和停止。官方推荐 Windows Performance Recorder (WPR)

转储

  • 对象转储:将问题对象序列化到本地
  • 生成 dmp 文件

轨迹记录

  • 数据追溯:设置硬件断点
  • 栈追溯:打印堆栈
CPP
#include <windows.h>
#include <dbghelp.h>
#include <stdio.h>

void ShowTraceStack(char* szBriefInfo)
{
    static const int MAX_STACK_FRAMES = 12;
    void *pStack[MAX_STACK_FRAMES];
    static char szStackInfo[STACK_INFO_LEN * MAX_STACK_FRAMES];
    static char szFrameInfo[STACK_INFO_LEN];

    HANDLE process = GetCurrentProcess();
    SymInitialize(process, NULL, TRUE);
    WORD frames = CaptureStackBackTrace(0, MAX_STACK_FRAMES, pStack, NULL);
    strcpy(szStackInfo, szBriefInfo == NULL ? "stack traceback:\n" : szBriefInfo);

    for (WORD i = 0; i < frames; ++i) {
        DWORD64 address = (DWORD64)(pStack[i]);

        DWORD64 displacementSym = 0;
        char buffer[sizeof(SYMBOL_INFO)+MAX_SYM_NAME * sizeof(TCHAR)];
        PSYMBOL_INFO pSymbol = (PSYMBOL_INFO)buffer;
        pSymbol->SizeOfStruct = sizeof(SYMBOL_INFO);
        pSymbol->MaxNameLen = MAX_SYM_NAME;

        DWORD displacementLine = 0;
        IMAGEHLP_LINE64 line;
        line.SizeOfStruct = sizeof(IMAGEHLP_LINE64);

        if (SymFromAddr(process, address, &displacementSym, pSymbol) && 
        	SymGetLineFromAddr64(process, address, &displacementLine, &line))
        {
            snprintf(szFrameInfo, sizeof(szFrameInfo), "\t%s() at %s:%d(0x%x)\n", 
            	pSymbol->Name, line.FileName, line.LineNumber, pSymbol->Address);
        }
        else
        {
            snprintf(szFrameInfo, sizeof(szFrameInfo), "\terror: %d\n", GetLastError());
        }
        strcat(szStackInfo, szFrameInfo);
    }

    printf("%s", szStackInfo); // 输出到控制台,也可以打印到日志文件中
}

License

Author: 海拉鲁的三角

Link: http://localhost:1313/cpp_notes/posts/windbg/debugability/

License: MIT

只要学不死,就往死里学