tinycodinglm / cpp /cpp-debug-003.jsonl
winapiadmin's picture
Upload folder using huggingface_hub (part 15)
b27bfb3 verified
|
Raw History Blame
2.94 kB
opencode trace
System You are an expert software engineer and technical assistant. You provide accurate, detailed, and practical answers. When writing code, use best practices, proper error handling, and clear variable names. When analyzing c...

You are an expert software engineer and technical assistant. You provide accurate, detailed, and practical answers. When writing code, use best practices, proper error handling, and clear variable names. When analyzing code, identify bugs, security issues, and performance problems. You have deep knowledge of C/C++, Windows API, Python, and many other technologies.

User

Compare C++ debug trace libraries

Assistant

Debug Trace Libraries Comparison:

Library Platform Features Setup
cpptrace All Pretty prints, snippets Easy
dbgutil All File/line Easy
libcsdbg Linux Demangled names Moderate
dbghelp Windows Full Windows support Moderate

1. cpptrace (Recommended):

cpp
#include <cpptrace/cpptrace.hpp>

// Simple
auto trace = cpptrace::generate_trace();
trace.print();

// With options
trace.print(std::cout, cpptrace::trace_options{}
    .with_colors(true)
    .with_snippets(true)  // show code snippets
    .with_max_frames(50));

2. dbgutil:

cpp
#include <dbgutil.h>

// Simple print
dbg::stacktrace();

// Custom output
dbg::stacktrace([](std::string_view frame) {
    log(frame);
});

3. POSIX backtrace (Linux/macOS):

cpp
#include <execinfo.h>
#include <cxxabi.h>

void print_backtrace() {
    void* callstack[128];
    int frames = backtrace(callstack, 128);
    char** symbols = backtrace_symbols(callstack, frames);
    
    for (int i = 0; i < frames; ++i) {
        int status;
        char* demangled = abi::__cxa_demangle(
            symbols[i], nullptr, nullptr, &status
        );
        std::cout << (status == 0 ? demangled : symbols[i]) << "\n";
        free(demangled);
    }
    free(symbols);
}

4. Windows dbghelp:

cpp
#include <windows.h>
#include <dbghelp.h>

#pragma comment(lib, "dbghelp.lib")

void print_trace() {
    SymInitialize(GetCurrentProcess(), NULL, TRUE);
    void* stack[100];
    USHORT frames = CaptureStackBackTrace(0, 100, stack, NULL);
    
    SYMBOL_INFO* symbol = (SYMBOL_INFO*)calloc(
        sizeof(SYMBOL_INFO) + 256, 1
    );
    symbol->MaxNameLen = 255;
    symbol->SizeOfStruct = sizeof(SYMBOL_INFO);
    
    for (USHORT i = 0; i < frames; ++i) {
        SymFromAddr(GetCurrentProcess(), (DWORD64)stack[i], 0, symbol);
        std::cout << symbol->Name << "\n";
    }
    free(symbol);
}

Best Practices:

  • Use cpptrace for new projects
  • Enable debug symbols (-g, /DEBUG)
  • Strip symbols in release builds
  • Store symbol files separately