Allow dynamic trace on any platform that can support it.

This commit is contained in:
2026-05-28 11:21:36 -05:00
parent e1716a741f
commit 03eb328d81
5 changed files with 45 additions and 25 deletions
+7
View File
@@ -32,6 +32,13 @@ if(NOT yyjson_FOUND)
endif()
endif()
if(DUSK_BACKTRACE)
target_link_options(${DUSK_LIBRARY_TARGET_NAME} PUBLIC -rdynamic)
target_compile_definitions(${DUSK_BINARY_TARGET_NAME} PUBLIC
DUSK_BACKTRACE
)
endif()
# Includes
target_include_directories(${DUSK_LIBRARY_TARGET_NAME}
PUBLIC
+21 -19
View File
@@ -8,24 +8,7 @@
#include "assert.h"
#include "log/log.h"
#include "util/string.h"
#ifdef DUSK_LINUX
#include <execinfo.h>
#include <stdlib.h>
static void assertLogBacktrace(void) {
void *frames[64];
int count = backtrace(frames, 64);
char **symbols = backtrace_symbols(frames, count);
logError("Stack trace:\n");
if(symbols) {
for(int i = 0; i < count; i++) {
logError(" %s\n", symbols[i]);
}
free(symbols);
}
}
#endif
#include "util/memory.h"
#ifndef DUSK_ASSERTIONS_FAKED
#ifdef DUSK_TEST_ASSERT
@@ -43,6 +26,25 @@
);
}
#else
#ifdef DUSK_BACKTRACE
#include <execinfo.h>
#include <stdlib.h>
static void assertLogBacktrace(void) {
void *frames[64];
int count = backtrace(frames, 64);
char **symbols = backtrace_symbols(frames, count);
memoryTrack(symbols);
logError("Stack trace:\n");
if(symbols) {
for(int i = 0; i < count; i++) {
logError(" %s\n", symbols[i]);
}
memoryFree(symbols);
}
}
#endif
void assertTrueImpl(
const char *file,
const int32_t line,
@@ -56,7 +58,7 @@
line,
message
);
#ifdef DUSK_LINUX
#ifdef DUSK_BACKTRACE
assertLogBacktrace();
#endif
abort();
+8 -3
View File
@@ -13,11 +13,16 @@ size_t memoryGetAllocatedCount(void) {
return MEMORY_POINTERS_IN_USE;
}
void memoryTrack(void *ptr) {
assertNotNull(ptr, "Cannot track NULL pointer.");
MEMORY_POINTERS_IN_USE++;
}
void * memoryAllocate(const size_t size) {
assertTrue(size > 0, "Cannot allocate 0 bytes of memory.");
void *ptr = malloc(size);
assertNotNull(ptr, "Memory allocation failed.");
MEMORY_POINTERS_IN_USE++;
memoryTrack(ptr);
return ptr;
}
@@ -26,7 +31,7 @@ void * memoryAlign(size_t alignment, size_t size) {
assertTrue(size > 0, "Cannot allocate 0 bytes of memory.");
void *ptr = memalign(alignment, size);
assertNotNull(ptr, "Aligned memory allocation failed.");
MEMORY_POINTERS_IN_USE++;
memoryTrack(ptr);
return ptr;
}
@@ -124,7 +129,7 @@ void memoryCopyInterleaved(
uint8_t *d = (uint8_t *)dest;
const uint8_t *s = (const uint8_t *)src;
for(size_t i = 0; i < count; i++) {
memcpy(d, s, elementSize);
memoryCopy(d, s, elementSize);
d += destStride;
s += srcStride;
}
+7
View File
@@ -17,6 +17,13 @@ static size_t MEMORY_POINTERS_IN_USE = 0;
*/
size_t memoryGetAllocatedCount(void);
/**
* Track a pointer that was malloc'd outside of the dusk engine.
*
* @param ptr The pointer to track.
*/
void memoryTrack(void *ptr);
/**
* Allocates memory.
*