Allow dynamic trace on any platform that can support it.
This commit is contained in:
@@ -16,9 +16,6 @@ else()
|
|||||||
)
|
)
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
# Export symbols so backtrace_symbols() can resolve function names.
|
|
||||||
target_link_options(${DUSK_LIBRARY_TARGET_NAME} PUBLIC -rdynamic)
|
|
||||||
|
|
||||||
# Link required libraries.
|
# Link required libraries.
|
||||||
target_link_libraries(${DUSK_LIBRARY_TARGET_NAME} PUBLIC
|
target_link_libraries(${DUSK_LIBRARY_TARGET_NAME} PUBLIC
|
||||||
SDL2
|
SDL2
|
||||||
@@ -29,6 +26,8 @@ target_link_libraries(${DUSK_LIBRARY_TARGET_NAME} PUBLIC
|
|||||||
# CURL::libcurl
|
# CURL::libcurl
|
||||||
)
|
)
|
||||||
|
|
||||||
|
set(DUSK_BACKTRACE ON CACHE BOOL "Enable backtrace support for assert failures.")
|
||||||
|
|
||||||
# Define platform-specific macros.
|
# Define platform-specific macros.
|
||||||
target_compile_definitions(${DUSK_LIBRARY_TARGET_NAME} PUBLIC
|
target_compile_definitions(${DUSK_LIBRARY_TARGET_NAME} PUBLIC
|
||||||
DUSK_SDL2
|
DUSK_SDL2
|
||||||
|
|||||||
@@ -32,6 +32,13 @@ if(NOT yyjson_FOUND)
|
|||||||
endif()
|
endif()
|
||||||
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
|
# Includes
|
||||||
target_include_directories(${DUSK_LIBRARY_TARGET_NAME}
|
target_include_directories(${DUSK_LIBRARY_TARGET_NAME}
|
||||||
PUBLIC
|
PUBLIC
|
||||||
|
|||||||
+21
-19
@@ -8,24 +8,7 @@
|
|||||||
#include "assert.h"
|
#include "assert.h"
|
||||||
#include "log/log.h"
|
#include "log/log.h"
|
||||||
#include "util/string.h"
|
#include "util/string.h"
|
||||||
|
#include "util/memory.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
|
|
||||||
|
|
||||||
#ifndef DUSK_ASSERTIONS_FAKED
|
#ifndef DUSK_ASSERTIONS_FAKED
|
||||||
#ifdef DUSK_TEST_ASSERT
|
#ifdef DUSK_TEST_ASSERT
|
||||||
@@ -43,6 +26,25 @@
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
#else
|
#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(
|
void assertTrueImpl(
|
||||||
const char *file,
|
const char *file,
|
||||||
const int32_t line,
|
const int32_t line,
|
||||||
@@ -56,7 +58,7 @@
|
|||||||
line,
|
line,
|
||||||
message
|
message
|
||||||
);
|
);
|
||||||
#ifdef DUSK_LINUX
|
#ifdef DUSK_BACKTRACE
|
||||||
assertLogBacktrace();
|
assertLogBacktrace();
|
||||||
#endif
|
#endif
|
||||||
abort();
|
abort();
|
||||||
|
|||||||
@@ -13,11 +13,16 @@ size_t memoryGetAllocatedCount(void) {
|
|||||||
return MEMORY_POINTERS_IN_USE;
|
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) {
|
void * memoryAllocate(const size_t size) {
|
||||||
assertTrue(size > 0, "Cannot allocate 0 bytes of memory.");
|
assertTrue(size > 0, "Cannot allocate 0 bytes of memory.");
|
||||||
void *ptr = malloc(size);
|
void *ptr = malloc(size);
|
||||||
assertNotNull(ptr, "Memory allocation failed.");
|
assertNotNull(ptr, "Memory allocation failed.");
|
||||||
MEMORY_POINTERS_IN_USE++;
|
memoryTrack(ptr);
|
||||||
return ptr;
|
return ptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -26,7 +31,7 @@ void * memoryAlign(size_t alignment, size_t size) {
|
|||||||
assertTrue(size > 0, "Cannot allocate 0 bytes of memory.");
|
assertTrue(size > 0, "Cannot allocate 0 bytes of memory.");
|
||||||
void *ptr = memalign(alignment, size);
|
void *ptr = memalign(alignment, size);
|
||||||
assertNotNull(ptr, "Aligned memory allocation failed.");
|
assertNotNull(ptr, "Aligned memory allocation failed.");
|
||||||
MEMORY_POINTERS_IN_USE++;
|
memoryTrack(ptr);
|
||||||
return ptr;
|
return ptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -124,7 +129,7 @@ void memoryCopyInterleaved(
|
|||||||
uint8_t *d = (uint8_t *)dest;
|
uint8_t *d = (uint8_t *)dest;
|
||||||
const uint8_t *s = (const uint8_t *)src;
|
const uint8_t *s = (const uint8_t *)src;
|
||||||
for(size_t i = 0; i < count; i++) {
|
for(size_t i = 0; i < count; i++) {
|
||||||
memcpy(d, s, elementSize);
|
memoryCopy(d, s, elementSize);
|
||||||
d += destStride;
|
d += destStride;
|
||||||
s += srcStride;
|
s += srcStride;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -17,6 +17,13 @@ static size_t MEMORY_POINTERS_IN_USE = 0;
|
|||||||
*/
|
*/
|
||||||
size_t memoryGetAllocatedCount(void);
|
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.
|
* Allocates memory.
|
||||||
*
|
*
|
||||||
|
|||||||
Reference in New Issue
Block a user