Allow dynamic trace on any platform that can support it.
This commit is contained in:
@@ -16,9 +16,6 @@ else()
|
||||
)
|
||||
endif()
|
||||
|
||||
# Export symbols so backtrace_symbols() can resolve function names.
|
||||
target_link_options(${DUSK_LIBRARY_TARGET_NAME} PUBLIC -rdynamic)
|
||||
|
||||
# Link required libraries.
|
||||
target_link_libraries(${DUSK_LIBRARY_TARGET_NAME} PUBLIC
|
||||
SDL2
|
||||
@@ -29,6 +26,8 @@ target_link_libraries(${DUSK_LIBRARY_TARGET_NAME} PUBLIC
|
||||
# CURL::libcurl
|
||||
)
|
||||
|
||||
set(DUSK_BACKTRACE ON CACHE BOOL "Enable backtrace support for assert failures.")
|
||||
|
||||
# Define platform-specific macros.
|
||||
target_compile_definitions(${DUSK_LIBRARY_TARGET_NAME} PUBLIC
|
||||
DUSK_SDL2
|
||||
|
||||
@@ -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
@@ -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();
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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.
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user