2024-04-18 12:59:11 +00:00
|
|
|
#pragma once
|
|
|
|
#include "dqn.h"
|
|
|
|
|
2024-03-25 05:11:57 +00:00
|
|
|
/*
|
2024-01-31 12:49:23 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// $$$$$$$\ $$$$$$$$\ $$$$$$$\ $$\ $$\ $$$$$$\
|
|
|
|
// $$ __$$\ $$ _____|$$ __$$\ $$ | $$ |$$ __$$\
|
|
|
|
// $$ | $$ |$$ | $$ | $$ |$$ | $$ |$$ / \__|
|
|
|
|
// $$ | $$ |$$$$$\ $$$$$$$\ |$$ | $$ |$$ |$$$$\
|
|
|
|
// $$ | $$ |$$ __| $$ __$$\ $$ | $$ |$$ |\_$$ |
|
|
|
|
// $$ | $$ |$$ | $$ | $$ |$$ | $$ |$$ | $$ |
|
|
|
|
// $$$$$$$ |$$$$$$$$\ $$$$$$$ |\$$$$$$ |\$$$$$$ |
|
|
|
|
// \_______/ \________|\_______/ \______/ \______/
|
|
|
|
//
|
|
|
|
// dqn_debug.h -- Tools for debugging
|
|
|
|
//
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// [$ASAN] Dqn_Asan -- Helpers to manually poison memory using ASAN
|
|
|
|
// [$STKT] Dqn_StackTrace -- Create stack traces
|
|
|
|
// [$DEBG] Dqn_Debug -- Allocation leak tracking API
|
|
|
|
//
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
2024-03-25 05:11:57 +00:00
|
|
|
*/
|
2024-01-31 12:49:23 +00:00
|
|
|
|
|
|
|
// NOTE: [$ASAN] Dqn_Asan //////////////////////////////////////////////////////////////////////////
|
2023-08-27 08:07:13 +00:00
|
|
|
#if !defined(DQN_ASAN_POISON)
|
|
|
|
#define DQN_ASAN_POISON 0
|
|
|
|
#endif
|
|
|
|
|
2023-08-31 14:18:53 +00:00
|
|
|
#if !defined(DQN_ASAN_VET_POISON)
|
|
|
|
#define DQN_ASAN_VET_POISON 0
|
2023-08-29 12:35:25 +00:00
|
|
|
#endif
|
|
|
|
|
2023-08-29 12:57:31 +00:00
|
|
|
#define DQN_ASAN_POISON_ALIGNMENT 8
|
2024-01-31 12:49:23 +00:00
|
|
|
|
2023-08-29 12:57:31 +00:00
|
|
|
#if !defined(DQN_ASAN_POISON_GUARD_SIZE)
|
|
|
|
#define DQN_ASAN_POISON_GUARD_SIZE 128
|
2023-08-27 08:07:13 +00:00
|
|
|
#endif
|
2023-08-29 12:57:31 +00:00
|
|
|
static_assert(Dqn_IsPowerOfTwoAligned(DQN_ASAN_POISON_GUARD_SIZE, DQN_ASAN_POISON_ALIGNMENT),
|
|
|
|
"ASAN poison guard size must be a power-of-two and aligned to ASAN's alignment"
|
|
|
|
"requirement (8 bytes)");
|
2023-08-27 08:07:13 +00:00
|
|
|
|
2024-01-31 12:49:23 +00:00
|
|
|
#if DQN_HAS_FEATURE(address_sanitizer) || defined(__SANITIZE_ADDRESS__)
|
2023-08-28 14:52:01 +00:00
|
|
|
#include <sanitizer/asan_interface.h>
|
|
|
|
#endif
|
2023-08-27 08:07:13 +00:00
|
|
|
|
2024-01-31 12:49:23 +00:00
|
|
|
// NOTE: [$STKT] Dqn_StackTrace ////////////////////////////////////////////////////////////////////
|
2023-08-30 15:03:48 +00:00
|
|
|
struct Dqn_StackTraceFrame
|
|
|
|
{
|
2023-10-24 13:11:48 +00:00
|
|
|
uint64_t address;
|
|
|
|
uint64_t line_number;
|
|
|
|
Dqn_Str8 file_name;
|
|
|
|
Dqn_Str8 function_name;
|
2023-08-30 15:03:48 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct Dqn_StackTraceRawFrame
|
|
|
|
{
|
|
|
|
void *process;
|
|
|
|
uint64_t base_addr;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct Dqn_StackTraceWalkResult
|
|
|
|
{
|
2023-08-31 10:07:27 +00:00
|
|
|
void *process; // [Internal] Windows handle to the process
|
|
|
|
uint64_t *base_addr; // The addresses of the functions in the stack trace
|
|
|
|
uint16_t size; // The number of `base_addr`'s stored from the walk
|
2023-08-30 15:03:48 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct Dqn_StackTraceWalkResultIterator
|
|
|
|
{
|
|
|
|
Dqn_StackTraceRawFrame raw_frame;
|
|
|
|
uint16_t index;
|
|
|
|
};
|
|
|
|
|
2024-01-31 12:49:23 +00:00
|
|
|
// NOTE: [$DEBG] Dqn_Debug /////////////////////////////////////////////////////////////////////////
|
|
|
|
enum Dqn_DebugAllocFlag
|
2023-08-16 11:59:38 +00:00
|
|
|
{
|
2024-01-31 12:49:23 +00:00
|
|
|
Dqn_DebugAllocFlag_Freed = 1 << 0,
|
|
|
|
Dqn_DebugAllocFlag_LeakPermitted = 1 << 1,
|
2023-08-16 11:59:38 +00:00
|
|
|
};
|
|
|
|
|
2024-01-31 12:49:23 +00:00
|
|
|
struct Dqn_DebugAlloc
|
2023-08-16 11:59:38 +00:00
|
|
|
{
|
2024-01-31 12:49:23 +00:00
|
|
|
void *ptr; // 8 Pointer to the allocation being tracked
|
|
|
|
Dqn_usize size; // 16 Size of the allocation
|
|
|
|
Dqn_usize freed_size; // 24 Store the size of the allocation when it is freed
|
|
|
|
Dqn_Str8 stack_trace; // 40 Stack trace at the point of allocation
|
|
|
|
Dqn_Str8 freed_stack_trace; // 56 Stack trace of where the allocation was freed
|
|
|
|
uint16_t flags; // 72 Bit flags from `Dqn_DebugAllocFlag`
|
2023-08-16 11:59:38 +00:00
|
|
|
};
|
2024-01-31 12:49:23 +00:00
|
|
|
|
|
|
|
static_assert(sizeof(Dqn_DebugAlloc) == 64 || sizeof(Dqn_DebugAlloc) == 32, // NOTE: 64 bit vs 32 bit pointers respectively
|
2023-08-16 11:59:38 +00:00
|
|
|
"We aim to keep the allocation record as light as possible as "
|
|
|
|
"memory tracking can get expensive. Enforce that there is no "
|
|
|
|
"unexpected padding.");
|
|
|
|
|
2024-01-31 12:49:23 +00:00
|
|
|
// NOTE: [$ASAN] Dqn_Asan //////////////////////////////////////////////////////////////////////////
|
|
|
|
DQN_API void Dqn_ASAN_PoisonMemoryRegion (void const volatile *ptr, Dqn_usize size);
|
|
|
|
DQN_API void Dqn_ASAN_UnpoisonMemoryRegion (void const volatile *ptr, Dqn_usize size);
|
2023-08-16 11:59:38 +00:00
|
|
|
|
2024-01-31 12:49:23 +00:00
|
|
|
// NOTE: [$STKT] Dqn_StackTrace ////////////////////////////////////////////////////////////////////
|
|
|
|
DQN_API Dqn_StackTraceWalkResult Dqn_StackTrace_Walk (Dqn_Arena *arena, uint16_t limit);
|
|
|
|
DQN_API Dqn_Str8 Dqn_StackTrace_WalkStr8CRT (uint16_t limit, uint16_t skip);
|
|
|
|
DQN_API bool Dqn_StackTrace_WalkResultIterate(Dqn_StackTraceWalkResultIterator *it, Dqn_StackTraceWalkResult const *walk);
|
|
|
|
DQN_API Dqn_Str8 Dqn_StackTrace_WalkResultStr8 (Dqn_Arena *arena, Dqn_StackTraceWalkResult const *walk, uint16_t skip);
|
|
|
|
DQN_API Dqn_Str8 Dqn_StackTrace_WalkResultStr8CRT(Dqn_StackTraceWalkResult const *walk, uint16_t skip);
|
|
|
|
DQN_API Dqn_Slice<Dqn_StackTraceFrame> Dqn_StackTrace_GetFrames (Dqn_Arena *arena, uint16_t limit);
|
|
|
|
DQN_API Dqn_StackTraceFrame Dqn_StackTrace_RawFrameToFrame (Dqn_Arena *arena, Dqn_StackTraceRawFrame raw_frame);
|
|
|
|
DQN_API void Dqn_StackTrace_Print (uint16_t limit);
|
|
|
|
DQN_API void Dqn_StackTrace_ReloadSymbols ();
|
|
|
|
|
|
|
|
// NOTE: [$DEBG] Dqn_Debug /////////////////////////////////////////////////////////////////////////
|
|
|
|
#if defined(DQN_LEAK_TRACKING)
|
|
|
|
DQN_API void Dqn_Debug_TrackAlloc (void *ptr, Dqn_usize size, bool alloc_can_leak);
|
|
|
|
DQN_API void Dqn_Debug_TrackDealloc (void *ptr);
|
|
|
|
DQN_API void Dqn_Debug_DumpLeaks ();
|
2023-08-16 11:59:38 +00:00
|
|
|
#else
|
2024-01-31 12:49:23 +00:00
|
|
|
#define Dqn_Debug_TrackAlloc(ptr, size, alloc_can_leak) do { (void)ptr; (void)size; (void)alloc_can_leak; } while (0)
|
|
|
|
#define Dqn_Debug_TrackDealloc(ptr) do { (void)ptr; } while (0)
|
|
|
|
#define Dqn_Debug_DumpLeaks() do { } while (0)
|
2023-08-16 11:59:38 +00:00
|
|
|
#endif
|
2024-01-31 12:49:23 +00:00
|
|
|
|
|
|
|
|