102 Patches: Detours to the Rescue
C reference for DttR maintainers and modders.
Loading...
Searching...
No Matches
crashdump.c File Reference
#include <dttr_config.h>
#include <dttr_crashdump.h>
#include <dttr_errors.h>
#include <dttr_imgui.h>
#include <dttr_path.h>
#include <dttr_sdl.h>
#include <Zydis/Zydis.h>
#include <dbghelp.h>
#include <dttr_log.h>
#include <sds.h>
#include <stdint.h>

Go to the source code of this file.

Data Structures

struct  CrashDisasmLine

Macros

#define MAX_STACK_FRAMES   64
#define SYMBOL_NAME_CAPACITY   256
#define SYMBOL_BUFFER_SIZE   (sizeof(IMAGEHLP_SYMBOL) + SYMBOL_NAME_CAPACITY)
#define DISASM_BYTES_BEFORE   16u
#define DISASM_BYTES_AFTER   32u
#define DISASM_MAX_BYTES   (DISASM_BYTES_BEFORE + DISASM_BYTES_AFTER)
#define DISASM_MAX_INSTRUCTIONS   6
#define DISASM_MAX_LINES   (DISASM_BYTES_BEFORE + DISASM_MAX_INSTRUCTIONS)
#define DISASM_TEXT_CAPACITY   160

Functions

static BOOL init_dbghelp_lock (PINIT_ONCE, PVOID, PVOID *)
static void enter_dbghelp_lock ()
static void leave_dbghelp_lock ()
static void invoke_symbol_provider (HANDLE process)
void DTTR_CrashDump_SetSymbolProvider (DTTR_CrashDump_SymbolProvider provider, void *context)
 Registers a synchronous symbol provider used by crash stack formatting.
void DTTR_CrashDump_ClearSymbolProvider ()
static bool memory_protection_is_readable (DWORD protect)
static sds append_disassembly_unavailable (sds message, const char *reason)
static sds append_disassembly_line (sds message, DWORD address, const char *text, bool failed)
static bool format_instruction_at (ZydisDecoder *decoder, ZydisFormatter *formatter, const uint8_t *bytes, size_t size, DWORD address, ZydisDecodedInstruction *instruction, char *text, size_t text_size)
static sds append_failed_instruction_decode (sds message, ZydisDecoder *decoder, ZydisFormatter *formatter, const uint8_t *bytes, size_t size, DWORD failed_eip)
static sds append_disassembly_from_bytes (sds message, const uint8_t *bytes, size_t size, DWORD runtime_base, DWORD failed_eip)
static sds append_registers (sds message, const CONTEXT *ctx)
static sds append_disassembly_window (sds message, HANDLE process, DWORD failed_eip)
static sds append_crash_diagnostics (sds message, HANDLE process, const CONTEXT *context)
static MINIDUMP_TYPE minidump_type ()
static sds build_crash_message (DWORD code, const char *filename)
sds DTTR_CrashDump_AppendReportMessage (sds message, const char *stack_trace)
sds DTTR_CrashDump_Write (HANDLE process, DWORD pid, DWORD tid, EXCEPTION_POINTERS *exception_info)
void DTTR_CrashDump_LogAndTraceReport (const char *message)
sds DTTR_CrashDump_FormatStackTrace (HANDLE process, HANDLE thread, const CONTEXT *context)
 Formats a stack trace from a thread context. Caller frees the returned sds.
static LONG unhandled_exception_filter (EXCEPTION_POINTERS *const exception_info)
static bool set_dump_dir (const char *base_dir)
void DTTR_CrashDump_Init (const char *const dump_dir)

Variables

static char crash_dump_dir [MAX_PATH]
static INIT_ONCE dbghelp_lock_once = INIT_ONCE_STATIC_INIT
static CRITICAL_SECTION dbghelp_lock
static DTTR_CrashDump_SymbolProvider crash_symbol_provider
static voidcrash_symbol_provider_context

Macro Definition Documentation

◆ DISASM_BYTES_AFTER

#define DISASM_BYTES_AFTER   32u

Definition at line 61 of file crashdump.c.

Referenced by append_disassembly_window().

◆ DISASM_BYTES_BEFORE

#define DISASM_BYTES_BEFORE   16u

Definition at line 60 of file crashdump.c.

Referenced by append_disassembly_window().

◆ DISASM_MAX_BYTES

#define DISASM_MAX_BYTES   (DISASM_BYTES_BEFORE + DISASM_BYTES_AFTER)

Definition at line 62 of file crashdump.c.

Referenced by append_disassembly_window().

◆ DISASM_MAX_INSTRUCTIONS

#define DISASM_MAX_INSTRUCTIONS   6

Definition at line 63 of file crashdump.c.

Referenced by append_disassembly_from_bytes(), and append_failed_instruction_decode().

◆ DISASM_MAX_LINES

#define DISASM_MAX_LINES   (DISASM_BYTES_BEFORE + DISASM_MAX_INSTRUCTIONS)

Definition at line 64 of file crashdump.c.

Referenced by append_disassembly_from_bytes().

◆ DISASM_TEXT_CAPACITY

#define DISASM_TEXT_CAPACITY   160

Definition at line 65 of file crashdump.c.

Referenced by append_failed_instruction_decode().

◆ MAX_STACK_FRAMES

#define MAX_STACK_FRAMES   64

Definition at line 57 of file crashdump.c.

Referenced by DTTR_CrashDump_FormatStackTrace().

◆ SYMBOL_BUFFER_SIZE

#define SYMBOL_BUFFER_SIZE   (sizeof(IMAGEHLP_SYMBOL) + SYMBOL_NAME_CAPACITY)

Definition at line 59 of file crashdump.c.

Referenced by DTTR_CrashDump_FormatStackTrace().

◆ SYMBOL_NAME_CAPACITY

#define SYMBOL_NAME_CAPACITY   256

Definition at line 58 of file crashdump.c.

Referenced by DTTR_CrashDump_FormatStackTrace().

Function Documentation

◆ append_crash_diagnostics()

sds append_crash_diagnostics ( sds message,
HANDLE process,
const CONTEXT * context )
static

Definition at line 381 of file crashdump.c.

References append_disassembly_window(), and append_registers().

Referenced by DTTR_CrashDump_FormatStackTrace().

◆ append_disassembly_from_bytes()

sds append_disassembly_from_bytes ( sds message,
const uint8_t * bytes,
size_t size,
DWORD runtime_base,
DWORD failed_eip )
static

◆ append_disassembly_line()

sds append_disassembly_line ( sds message,
DWORD address,
const char * text,
bool failed )
static

Definition at line 99 of file crashdump.c.

References DWORD.

Referenced by append_disassembly_from_bytes(), and append_failed_instruction_decode().

◆ append_disassembly_unavailable()

sds append_disassembly_unavailable ( sds message,
const char * reason )
static

◆ append_disassembly_window()

sds append_disassembly_window ( sds message,
HANDLE process,
DWORD failed_eip )
static

◆ append_failed_instruction_decode()

sds append_failed_instruction_decode ( sds message,
ZydisDecoder * decoder,
ZydisFormatter * formatter,
const uint8_t * bytes,
size_t size,
DWORD failed_eip )
static

◆ append_registers()

sds append_registers ( sds message,
const CONTEXT * ctx )
static

Definition at line 272 of file crashdump.c.

References ctx.

Referenced by append_crash_diagnostics().

◆ build_crash_message()

sds build_crash_message ( DWORD code,
const char * filename )
static

Definition at line 399 of file crashdump.c.

References DWORD.

Referenced by unhandled_exception_filter().

◆ DTTR_CrashDump_AppendReportMessage()

sds DTTR_CrashDump_AppendReportMessage ( sds message,
const char * stack_trace )

Definition at line 416 of file crashdump.c.

References DTTR_REPORT_SUFFIX.

Referenced by unhandled_exception_filter(), and write_child_dump().

◆ DTTR_CrashDump_ClearSymbolProvider()

void DTTR_CrashDump_ClearSymbolProvider ( )

Definition at line 53 of file crashdump.c.

References DTTR_CrashDump_SetSymbolProvider(), and NULL.

Referenced by dttr_pcdogs_crash_symbols_clear().

◆ DTTR_CrashDump_FormatStackTrace()

sds DTTR_CrashDump_FormatStackTrace ( HANDLE process,
HANDLE thread,
const CONTEXT * context )

◆ DTTR_CrashDump_Init()

void DTTR_CrashDump_Init ( const char * dump_dir)

Installs an exception filter that writes dumps under the given directory and shows a crash dialog.

Definition at line 636 of file crashdump.c.

References crash_dump_dir, DTTR_LOG_DEBUG, DTTR_LOG_ERROR, set_dump_dir(), and unhandled_exception_filter().

Referenced by DTTR_Hook_WinMainCallback(), and dttr_launcher_main().

◆ DTTR_CrashDump_LogAndTraceReport()

void DTTR_CrashDump_LogAndTraceReport ( const char * message)

Definition at line 486 of file crashdump.c.

References DTTR_LOG_ERROR.

Referenced by unhandled_exception_filter(), and write_child_dump().

◆ DTTR_CrashDump_SetSymbolProvider()

void DTTR_CrashDump_SetSymbolProvider ( DTTR_CrashDump_SymbolProvider provider,
void * context )

Registers a synchronous symbol provider used by crash stack formatting.

Definition at line 43 of file crashdump.c.

References crash_symbol_provider, crash_symbol_provider_context, enter_dbghelp_lock(), and leave_dbghelp_lock().

Referenced by DTTR_CrashDump_ClearSymbolProvider(), and dttr_pcdogs_crash_symbols_register().

◆ DTTR_CrashDump_Write()

sds DTTR_CrashDump_Write ( HANDLE process,
DWORD pid,
DWORD tid,
EXCEPTION_POINTERS * exception_info )

Writes a process minidump and returns the dump filename. The caller should free the returned sds.

Definition at line 428 of file crashdump.c.

References BOOL, crash_dump_dir, DTTR_LOG_ERROR, DTTR_LOG_INFO, DWORD, enter_dbghelp_lock(), FALSE, leave_dbghelp_lock(), minidump_type(), NULL, and st.

Referenced by dttr_sidecar_write_exception_report(), unhandled_exception_filter(), and write_child_dump().

◆ enter_dbghelp_lock()

void enter_dbghelp_lock ( )
static

◆ format_instruction_at()

bool format_instruction_at ( ZydisDecoder * decoder,
ZydisFormatter * formatter,
const uint8_t * bytes,
size_t size,
DWORD address,
ZydisDecodedInstruction * instruction,
char * text,
size_t text_size )
static

Definition at line 112 of file crashdump.c.

References decoder, DWORD, and size.

Referenced by append_disassembly_from_bytes(), and append_failed_instruction_decode().

◆ init_dbghelp_lock()

BOOL init_dbghelp_lock ( PINIT_ONCE ,
PVOID ,
PVOID *  )
static

Definition at line 23 of file crashdump.c.

References BOOL, and dbghelp_lock.

Referenced by enter_dbghelp_lock().

◆ invoke_symbol_provider()

void invoke_symbol_provider ( HANDLE process)
static

Definition at line 37 of file crashdump.c.

References crash_symbol_provider, and crash_symbol_provider_context.

Referenced by DTTR_CrashDump_FormatStackTrace().

◆ leave_dbghelp_lock()

void leave_dbghelp_lock ( )
static

◆ memory_protection_is_readable()

bool memory_protection_is_readable ( DWORD protect)
static

Definition at line 67 of file crashdump.c.

References DWORD.

Referenced by append_disassembly_window().

◆ minidump_type()

MINIDUMP_TYPE minidump_type ( )
static

Definition at line 391 of file crashdump.c.

References dttr_config, and DTTR_MINIDUMP_DETAILED.

Referenced by draw_general_tab(), and DTTR_CrashDump_Write().

◆ set_dump_dir()

bool set_dump_dir ( const char * base_dir)
static

◆ unhandled_exception_filter()

Variable Documentation

◆ crash_dump_dir

char crash_dump_dir[MAX_PATH]
static

Definition at line 16 of file crashdump.c.

Referenced by DTTR_CrashDump_Init(), DTTR_CrashDump_Write(), and set_dump_dir().

◆ crash_symbol_provider

DTTR_CrashDump_SymbolProvider crash_symbol_provider
static

Definition at line 20 of file crashdump.c.

Referenced by DTTR_CrashDump_SetSymbolProvider(), and invoke_symbol_provider().

◆ crash_symbol_provider_context

void* crash_symbol_provider_context
static

Definition at line 21 of file crashdump.c.

Referenced by DTTR_CrashDump_SetSymbolProvider(), and invoke_symbol_provider().

◆ dbghelp_lock

CRITICAL_SECTION dbghelp_lock
static

Definition at line 19 of file crashdump.c.

Referenced by enter_dbghelp_lock(), init_dbghelp_lock(), and leave_dbghelp_lock().

◆ dbghelp_lock_once

INIT_ONCE dbghelp_lock_once = INIT_ONCE_STATIC_INIT
static

Definition at line 18 of file crashdump.c.

Referenced by enter_dbghelp_lock().