2017-10-12 14:20:17 -07:00
|
|
|
/*
|
|
|
|
|
Simple DirectMedia Layer
|
2024-01-01 13:15:26 -08:00
|
|
|
Copyright (C) 1997-2024 Sam Lantinga <slouken@libsdl.org>
|
2017-10-12 14:20:17 -07:00
|
|
|
|
|
|
|
|
This software is provided 'as-is', without any express or implied
|
|
|
|
|
warranty. In no event will the authors be held liable for any damages
|
|
|
|
|
arising from the use of this software.
|
|
|
|
|
|
|
|
|
|
Permission is granted to anyone to use this software for any purpose,
|
|
|
|
|
including commercial applications, and to alter it and redistribute it
|
|
|
|
|
freely, subject to the following restrictions:
|
|
|
|
|
|
|
|
|
|
1. The origin of this software must not be misrepresented; you must not
|
|
|
|
|
claim that you wrote the original software. If you use this software
|
|
|
|
|
in a product, an acknowledgment in the product documentation would be
|
|
|
|
|
appreciated but is not required.
|
|
|
|
|
2. Altered source versions must be plainly marked as such, and must not be
|
|
|
|
|
misrepresented as being the original software.
|
|
|
|
|
3. This notice may not be removed or altered from any source distribution.
|
|
|
|
|
*/
|
2022-11-26 20:43:38 -08:00
|
|
|
#include <SDL3/SDL_test.h>
|
2017-10-12 14:20:17 -07:00
|
|
|
|
|
|
|
|
#ifdef HAVE_LIBUNWIND_H
|
test: Fix building with libunwind under autotools
There are two issues which are stopping the SDL tests from building on
my machine:
- libunwind is not being linked
- Even if it is, it is missing several symbols.
The first is fixed by having the test programs link against libunwind if
available. Technically, SDL2_test should be linking against it, as it's
used in SDL_test_memory.c, but as SDL2_test is a static library, it
can't itself import libunwind. We just assume that if it's present on
the system, we should link it directly to the test programs. This should
strictly be an improvement, as the only case where this'd fail is if
SDL2 was compiled when libunwind was present, but the tests are being
compiled without it, and that'd fail anyway.
The second is fixed by #define-ing UNW_LOCAL_ONLY before including
libunwind.h: this is required to make libunwind link to predicatable
symbols, in what can only be described as a bit of a farce. There are a
few more details in the libunwind man page, but the gist of it is that
it disables support for "remote unwinding": unwinding stack frames in a
different process (and possibly from a different architecture?):
http://www.nongnu.org/libunwind/man/libunwind(3).html
Note that I haven't tried this with CMake: I suspect that it'll work,
though, as the CMakeLists.txt seems to have SDL2 link against libunwind if
it's present. This adds an ugly extra dependency to SDL2, but does mean
that issue 1 isn't present. The UNW_LOCAL_ONLY change shouldn't be
build-system-specific.
2021-10-23 14:46:03 +08:00
|
|
|
#define UNW_LOCAL_ONLY
|
2017-10-12 14:20:17 -07:00
|
|
|
#include <libunwind.h>
|
2024-08-30 21:29:54 +02:00
|
|
|
#ifndef unw_get_proc_name_by_ip
|
2024-08-30 23:29:09 +02:00
|
|
|
#define SDLTEST_UNWIND_NO_PROC_NAME_BY_IP
|
2024-09-18 07:52:28 -07:00
|
|
|
static bool s_unwind_symbol_names = true;
|
2024-08-30 21:29:54 +02:00
|
|
|
#endif
|
2017-10-12 14:20:17 -07:00
|
|
|
#endif
|
|
|
|
|
|
2024-01-24 20:37:00 +03:00
|
|
|
#ifdef SDL_PLATFORM_WIN32
|
2023-09-01 23:19:46 +02:00
|
|
|
#include <windows.h>
|
|
|
|
|
#include <dbghelp.h>
|
|
|
|
|
|
2024-06-30 13:11:07 +02:00
|
|
|
static struct {
|
2024-10-01 11:11:40 -04:00
|
|
|
SDL_SharedObject *module;
|
2024-06-30 13:11:07 +02:00
|
|
|
BOOL (WINAPI *pSymInitialize)(HANDLE hProcess, PCSTR UserSearchPath, BOOL fInvadeProcess);
|
|
|
|
|
BOOL (WINAPI *pSymFromAddr)(HANDLE hProcess, DWORD64 Address, PDWORD64 Displacement, PSYMBOL_INFO Symbol);
|
|
|
|
|
BOOL (WINAPI *pSymGetLineFromAddr64)(HANDLE hProcess, DWORD64 qwAddr, PDWORD pdwDisplacement, PIMAGEHLP_LINE64 Line);
|
|
|
|
|
} dyn_dbghelp;
|
2023-09-01 23:19:46 +02:00
|
|
|
|
2023-11-02 20:33:02 +03:00
|
|
|
/* older SDKs might not have this: */
|
|
|
|
|
__declspec(dllimport) USHORT WINAPI RtlCaptureStackBackTrace(ULONG FramesToSkip, ULONG FramesToCapture, PVOID* BackTrace, PULONG BackTraceHash);
|
|
|
|
|
#define CaptureStackBackTrace RtlCaptureStackBackTrace
|
|
|
|
|
|
2023-09-01 23:19:46 +02:00
|
|
|
#endif
|
|
|
|
|
|
2017-10-12 14:20:17 -07:00
|
|
|
/* This is a simple tracking allocator to demonstrate the use of SDL's
|
|
|
|
|
memory allocation replacement functionality.
|
|
|
|
|
|
|
|
|
|
It gets slow with large numbers of allocations and shouldn't be used
|
|
|
|
|
for production code.
|
|
|
|
|
*/
|
|
|
|
|
|
2023-09-01 23:19:46 +02:00
|
|
|
#define MAXIMUM_TRACKED_STACK_DEPTH 32
|
|
|
|
|
|
2017-10-12 14:20:17 -07:00
|
|
|
typedef struct SDL_tracked_allocation
|
|
|
|
|
{
|
|
|
|
|
void *mem;
|
|
|
|
|
size_t size;
|
2023-09-01 23:19:46 +02:00
|
|
|
Uint64 stack[MAXIMUM_TRACKED_STACK_DEPTH];
|
2017-10-12 14:20:17 -07:00
|
|
|
struct SDL_tracked_allocation *next;
|
2024-08-30 23:29:09 +02:00
|
|
|
#ifdef SDLTEST_UNWIND_NO_PROC_NAME_BY_IP
|
2024-08-30 21:29:54 +02:00
|
|
|
char stack_names[MAXIMUM_TRACKED_STACK_DEPTH][256];
|
|
|
|
|
#endif
|
2017-10-12 14:20:17 -07:00
|
|
|
} SDL_tracked_allocation;
|
|
|
|
|
|
|
|
|
|
static SDLTest_Crc32Context s_crc32_context;
|
|
|
|
|
static SDL_malloc_func SDL_malloc_orig = NULL;
|
|
|
|
|
static SDL_calloc_func SDL_calloc_orig = NULL;
|
|
|
|
|
static SDL_realloc_func SDL_realloc_orig = NULL;
|
|
|
|
|
static SDL_free_func SDL_free_orig = NULL;
|
|
|
|
|
static int s_previous_allocations = 0;
|
|
|
|
|
static SDL_tracked_allocation *s_tracked_allocations[256];
|
2024-09-18 07:52:28 -07:00
|
|
|
static bool s_randfill_allocations = false;
|
2024-07-30 12:28:08 +02:00
|
|
|
static SDL_AtomicInt s_lock;
|
|
|
|
|
|
|
|
|
|
#define LOCK_ALLOCATOR() \
|
|
|
|
|
do { \
|
2024-09-16 23:21:31 -07:00
|
|
|
if (SDL_CompareAndSwapAtomicInt(&s_lock, 0, 1)) { \
|
2024-07-30 12:28:08 +02:00
|
|
|
break; \
|
|
|
|
|
} \
|
|
|
|
|
SDL_CPUPauseInstruction(); \
|
2024-09-18 07:52:28 -07:00
|
|
|
} while (true)
|
2024-09-16 23:21:31 -07:00
|
|
|
#define UNLOCK_ALLOCATOR() do { SDL_SetAtomicInt(&s_lock, 0); } while (0)
|
2017-10-12 14:20:17 -07:00
|
|
|
|
|
|
|
|
static unsigned int get_allocation_bucket(void *mem)
|
|
|
|
|
{
|
|
|
|
|
CrcUint32 crc_value;
|
|
|
|
|
unsigned int index;
|
|
|
|
|
SDLTest_Crc32Calc(&s_crc32_context, (CrcUint8 *)&mem, sizeof(mem), &crc_value);
|
|
|
|
|
index = (crc_value & (SDL_arraysize(s_tracked_allocations) - 1));
|
|
|
|
|
return index;
|
|
|
|
|
}
|
2022-11-30 12:51:59 -08:00
|
|
|
|
2023-08-27 20:05:43 +01:00
|
|
|
static SDL_tracked_allocation* SDL_GetTrackedAllocation(void *mem)
|
2017-10-12 14:20:17 -07:00
|
|
|
{
|
|
|
|
|
SDL_tracked_allocation *entry;
|
2024-07-30 12:28:08 +02:00
|
|
|
LOCK_ALLOCATOR();
|
2017-10-12 14:20:17 -07:00
|
|
|
int index = get_allocation_bucket(mem);
|
|
|
|
|
for (entry = s_tracked_allocations[index]; entry; entry = entry->next) {
|
|
|
|
|
if (mem == entry->mem) {
|
2024-07-30 12:28:08 +02:00
|
|
|
UNLOCK_ALLOCATOR();
|
2023-08-27 20:05:43 +01:00
|
|
|
return entry;
|
2017-10-12 14:20:17 -07:00
|
|
|
}
|
|
|
|
|
}
|
2024-07-30 12:28:08 +02:00
|
|
|
UNLOCK_ALLOCATOR();
|
2023-08-27 20:05:43 +01:00
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static size_t SDL_GetTrackedAllocationSize(void *mem)
|
|
|
|
|
{
|
|
|
|
|
SDL_tracked_allocation *entry = SDL_GetTrackedAllocation(mem);
|
|
|
|
|
|
|
|
|
|
return entry ? entry->size : SIZE_MAX;
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-18 07:52:28 -07:00
|
|
|
static bool SDL_IsAllocationTracked(void *mem)
|
2023-08-27 20:05:43 +01:00
|
|
|
{
|
|
|
|
|
return SDL_GetTrackedAllocation(mem) != NULL;
|
2017-10-12 14:20:17 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void SDL_TrackAllocation(void *mem, size_t size)
|
|
|
|
|
{
|
|
|
|
|
SDL_tracked_allocation *entry;
|
|
|
|
|
int index = get_allocation_bucket(mem);
|
|
|
|
|
|
|
|
|
|
if (SDL_IsAllocationTracked(mem)) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
entry = (SDL_tracked_allocation *)SDL_malloc_orig(sizeof(*entry));
|
2023-11-09 22:29:15 +01:00
|
|
|
if (!entry) {
|
2017-10-12 14:20:17 -07:00
|
|
|
return;
|
|
|
|
|
}
|
2024-07-30 12:28:08 +02:00
|
|
|
LOCK_ALLOCATOR();
|
2017-10-12 14:20:17 -07:00
|
|
|
entry->mem = mem;
|
|
|
|
|
entry->size = size;
|
|
|
|
|
|
|
|
|
|
/* Generate the stack trace for the allocation */
|
2019-07-31 05:11:40 +03:00
|
|
|
SDL_zeroa(entry->stack);
|
2017-10-12 14:20:17 -07:00
|
|
|
#ifdef HAVE_LIBUNWIND_H
|
|
|
|
|
{
|
|
|
|
|
int stack_index;
|
|
|
|
|
unw_cursor_t cursor;
|
|
|
|
|
unw_context_t context;
|
|
|
|
|
|
|
|
|
|
unw_getcontext(&context);
|
|
|
|
|
unw_init_local(&cursor, &context);
|
|
|
|
|
|
|
|
|
|
stack_index = 0;
|
|
|
|
|
while (unw_step(&cursor) > 0) {
|
2024-07-27 20:02:43 +02:00
|
|
|
unw_word_t pc;
|
2024-08-30 23:29:09 +02:00
|
|
|
#ifdef SDLTEST_UNWIND_NO_PROC_NAME_BY_IP
|
2024-08-30 21:29:54 +02:00
|
|
|
unw_word_t offset;
|
|
|
|
|
char sym[236];
|
|
|
|
|
#endif
|
2017-10-12 14:20:17 -07:00
|
|
|
|
|
|
|
|
unw_get_reg(&cursor, UNW_REG_IP, &pc);
|
|
|
|
|
entry->stack[stack_index] = pc;
|
|
|
|
|
|
2024-08-30 23:29:09 +02:00
|
|
|
#ifdef SDLTEST_UNWIND_NO_PROC_NAME_BY_IP
|
|
|
|
|
if (s_unwind_symbol_names && unw_get_proc_name(&cursor, sym, sizeof(sym), &offset) == 0) {
|
2024-08-30 21:29:54 +02:00
|
|
|
SDL_snprintf(entry->stack_names[stack_index], sizeof(entry->stack_names[stack_index]), "%s+0x%llx", sym, (unsigned long long)offset);
|
|
|
|
|
}
|
|
|
|
|
#endif
|
2017-10-12 14:20:17 -07:00
|
|
|
++stack_index;
|
|
|
|
|
|
|
|
|
|
if (stack_index == SDL_arraysize(entry->stack)) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-01-24 20:37:00 +03:00
|
|
|
#elif defined(SDL_PLATFORM_WIN32)
|
2023-09-01 23:19:46 +02:00
|
|
|
{
|
|
|
|
|
Uint32 count;
|
|
|
|
|
PVOID frames[63];
|
|
|
|
|
Uint32 i;
|
|
|
|
|
|
|
|
|
|
count = CaptureStackBackTrace(1, SDL_arraysize(frames), frames, NULL);
|
|
|
|
|
|
2024-04-03 21:25:04 +01:00
|
|
|
count = SDL_min(count, MAXIMUM_TRACKED_STACK_DEPTH);
|
|
|
|
|
for (i = 0; i < count; i++) {
|
2023-09-01 23:19:46 +02:00
|
|
|
entry->stack[i] = (Uint64)(uintptr_t)frames[i];
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-10-12 14:20:17 -07:00
|
|
|
#endif /* HAVE_LIBUNWIND_H */
|
|
|
|
|
|
|
|
|
|
entry->next = s_tracked_allocations[index];
|
|
|
|
|
s_tracked_allocations[index] = entry;
|
2024-07-30 12:28:08 +02:00
|
|
|
UNLOCK_ALLOCATOR();
|
2017-10-12 14:20:17 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void SDL_UntrackAllocation(void *mem)
|
|
|
|
|
{
|
|
|
|
|
SDL_tracked_allocation *entry, *prev;
|
|
|
|
|
int index = get_allocation_bucket(mem);
|
|
|
|
|
|
2024-07-30 12:28:08 +02:00
|
|
|
LOCK_ALLOCATOR();
|
2017-10-12 14:20:17 -07:00
|
|
|
prev = NULL;
|
|
|
|
|
for (entry = s_tracked_allocations[index]; entry; entry = entry->next) {
|
|
|
|
|
if (mem == entry->mem) {
|
|
|
|
|
if (prev) {
|
|
|
|
|
prev->next = entry->next;
|
|
|
|
|
} else {
|
|
|
|
|
s_tracked_allocations[index] = entry->next;
|
|
|
|
|
}
|
|
|
|
|
SDL_free_orig(entry);
|
2024-07-30 12:28:08 +02:00
|
|
|
UNLOCK_ALLOCATOR();
|
2017-10-12 14:20:17 -07:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
prev = entry;
|
|
|
|
|
}
|
2024-07-30 12:28:08 +02:00
|
|
|
UNLOCK_ALLOCATOR();
|
2017-10-12 14:20:17 -07:00
|
|
|
}
|
|
|
|
|
|
2023-08-27 20:05:43 +01:00
|
|
|
static void rand_fill_memory(void* ptr, size_t start, size_t end)
|
|
|
|
|
{
|
|
|
|
|
Uint8* mem = (Uint8*) ptr;
|
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
|
|
if (!s_randfill_allocations)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
for (i = start; i < end; ++i) {
|
|
|
|
|
mem[i] = SDLTest_RandomUint8();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-30 12:51:59 -08:00
|
|
|
static void *SDLCALL SDLTest_TrackedMalloc(size_t size)
|
2017-10-12 14:20:17 -07:00
|
|
|
{
|
|
|
|
|
void *mem;
|
|
|
|
|
|
|
|
|
|
mem = SDL_malloc_orig(size);
|
|
|
|
|
if (mem) {
|
|
|
|
|
SDL_TrackAllocation(mem, size);
|
2023-08-27 20:05:43 +01:00
|
|
|
rand_fill_memory(mem, 0, size);
|
2017-10-12 14:20:17 -07:00
|
|
|
}
|
|
|
|
|
return mem;
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-30 12:51:59 -08:00
|
|
|
static void *SDLCALL SDLTest_TrackedCalloc(size_t nmemb, size_t size)
|
2017-10-12 14:20:17 -07:00
|
|
|
{
|
|
|
|
|
void *mem;
|
|
|
|
|
|
|
|
|
|
mem = SDL_calloc_orig(nmemb, size);
|
|
|
|
|
if (mem) {
|
|
|
|
|
SDL_TrackAllocation(mem, nmemb * size);
|
|
|
|
|
}
|
|
|
|
|
return mem;
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-30 12:51:59 -08:00
|
|
|
static void *SDLCALL SDLTest_TrackedRealloc(void *ptr, size_t size)
|
2017-10-12 14:20:17 -07:00
|
|
|
{
|
|
|
|
|
void *mem;
|
2023-08-27 20:05:43 +01:00
|
|
|
size_t old_size = 0;
|
|
|
|
|
if (ptr) {
|
|
|
|
|
old_size = SDL_GetTrackedAllocationSize(ptr);
|
|
|
|
|
SDL_assert(old_size != SIZE_MAX);
|
|
|
|
|
}
|
2017-10-12 14:20:17 -07:00
|
|
|
mem = SDL_realloc_orig(ptr, size);
|
2023-08-27 20:05:43 +01:00
|
|
|
if (ptr) {
|
|
|
|
|
SDL_UntrackAllocation(ptr);
|
|
|
|
|
}
|
|
|
|
|
if (mem) {
|
2017-10-12 14:20:17 -07:00
|
|
|
SDL_TrackAllocation(mem, size);
|
2023-08-27 20:05:43 +01:00
|
|
|
if (size > old_size) {
|
|
|
|
|
rand_fill_memory(mem, old_size, size);
|
|
|
|
|
}
|
2017-10-12 14:20:17 -07:00
|
|
|
}
|
|
|
|
|
return mem;
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-13 09:50:04 -07:00
|
|
|
static void SDLCALL SDLTest_TrackedFree(void *ptr)
|
2017-10-12 14:20:17 -07:00
|
|
|
{
|
2023-11-09 22:29:15 +01:00
|
|
|
if (!ptr) {
|
2017-10-12 14:20:17 -07:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!s_previous_allocations) {
|
|
|
|
|
SDL_assert(SDL_IsAllocationTracked(ptr));
|
|
|
|
|
}
|
|
|
|
|
SDL_UntrackAllocation(ptr);
|
|
|
|
|
SDL_free_orig(ptr);
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-27 20:05:43 +01:00
|
|
|
void SDLTest_TrackAllocations(void)
|
2017-10-12 14:20:17 -07:00
|
|
|
{
|
|
|
|
|
if (SDL_malloc_orig) {
|
2023-08-27 20:05:43 +01:00
|
|
|
return;
|
2017-10-12 14:20:17 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SDLTest_Crc32Init(&s_crc32_context);
|
|
|
|
|
|
|
|
|
|
s_previous_allocations = SDL_GetNumAllocations();
|
|
|
|
|
if (s_previous_allocations != 0) {
|
|
|
|
|
SDL_Log("SDLTest_TrackAllocations(): There are %d previous allocations, disabling free() validation", s_previous_allocations);
|
|
|
|
|
}
|
2024-08-30 23:29:09 +02:00
|
|
|
#ifdef SDLTEST_UNWIND_NO_PROC_NAME_BY_IP
|
|
|
|
|
do {
|
|
|
|
|
/* Don't use SDL_GetHint: SDL_malloc is off limits. */
|
2024-09-13 17:00:15 -07:00
|
|
|
const char *env_trackmem = SDL_getenv_unsafe("SDL_TRACKMEM_SYMBOL_NAMES");
|
2024-08-30 23:29:09 +02:00
|
|
|
if (env_trackmem) {
|
|
|
|
|
if (SDL_strcasecmp(env_trackmem, "1") == 0 || SDL_strcasecmp(env_trackmem, "yes") == 0 || SDL_strcasecmp(env_trackmem, "true") == 0) {
|
2024-09-18 07:52:28 -07:00
|
|
|
s_unwind_symbol_names = true;
|
2024-08-30 23:29:09 +02:00
|
|
|
} else if (SDL_strcasecmp(env_trackmem, "0") == 0 || SDL_strcasecmp(env_trackmem, "no") == 0 || SDL_strcasecmp(env_trackmem, "false") == 0) {
|
2024-09-18 07:52:28 -07:00
|
|
|
s_unwind_symbol_names = false;
|
2024-08-30 23:29:09 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} while (0);
|
|
|
|
|
|
|
|
|
|
#elif defined(SDL_PLATFORM_WIN32)
|
2024-06-30 13:11:07 +02:00
|
|
|
do {
|
|
|
|
|
dyn_dbghelp.module = SDL_LoadObject("dbghelp.dll");
|
|
|
|
|
if (!dyn_dbghelp.module) {
|
|
|
|
|
goto dbghelp_failed;
|
2023-09-01 23:19:46 +02:00
|
|
|
}
|
2024-06-30 13:11:07 +02:00
|
|
|
dyn_dbghelp.pSymInitialize = (void *)SDL_LoadFunction(dyn_dbghelp.module, "SymInitialize");
|
|
|
|
|
dyn_dbghelp.pSymFromAddr = (void *)SDL_LoadFunction(dyn_dbghelp.module, "SymFromAddr");
|
|
|
|
|
dyn_dbghelp.pSymGetLineFromAddr64 = (void *)SDL_LoadFunction(dyn_dbghelp.module, "SymGetLineFromAddr64");
|
|
|
|
|
if (!dyn_dbghelp.pSymInitialize || !dyn_dbghelp.pSymFromAddr || !dyn_dbghelp.pSymGetLineFromAddr64) {
|
|
|
|
|
goto dbghelp_failed;
|
|
|
|
|
}
|
|
|
|
|
if (!dyn_dbghelp.pSymInitialize(GetCurrentProcess(), NULL, TRUE)) {
|
|
|
|
|
goto dbghelp_failed;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
dbghelp_failed:
|
|
|
|
|
if (dyn_dbghelp.module) {
|
|
|
|
|
SDL_UnloadObject(dyn_dbghelp.module);
|
|
|
|
|
dyn_dbghelp.module = NULL;
|
|
|
|
|
}
|
|
|
|
|
} while (0);
|
2023-09-01 23:19:46 +02:00
|
|
|
#endif
|
2017-10-12 14:20:17 -07:00
|
|
|
|
|
|
|
|
SDL_GetMemoryFunctions(&SDL_malloc_orig,
|
|
|
|
|
&SDL_calloc_orig,
|
|
|
|
|
&SDL_realloc_orig,
|
|
|
|
|
&SDL_free_orig);
|
|
|
|
|
|
|
|
|
|
SDL_SetMemoryFunctions(SDLTest_TrackedMalloc,
|
|
|
|
|
SDLTest_TrackedCalloc,
|
|
|
|
|
SDLTest_TrackedRealloc,
|
|
|
|
|
SDLTest_TrackedFree);
|
2023-08-27 20:05:43 +01:00
|
|
|
}
|
|
|
|
|
|
2024-07-17 22:09:32 +04:00
|
|
|
void SDLTest_RandFillAllocations(void)
|
2023-08-27 20:05:43 +01:00
|
|
|
{
|
|
|
|
|
SDLTest_TrackAllocations();
|
|
|
|
|
|
2024-09-18 07:52:28 -07:00
|
|
|
s_randfill_allocations = true;
|
2017-10-12 14:20:17 -07:00
|
|
|
}
|
|
|
|
|
|
2023-03-08 16:12:45 +01:00
|
|
|
void SDLTest_LogAllocations(void)
|
2017-10-12 14:20:17 -07:00
|
|
|
{
|
|
|
|
|
char *message = NULL;
|
|
|
|
|
size_t message_size = 0;
|
2024-09-01 02:19:39 +02:00
|
|
|
char line[256], *tmp;
|
2017-10-12 14:20:17 -07:00
|
|
|
SDL_tracked_allocation *entry;
|
|
|
|
|
int index, count, stack_index;
|
|
|
|
|
Uint64 total_allocated;
|
|
|
|
|
|
|
|
|
|
if (!SDL_malloc_orig) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-02 21:11:33 +01:00
|
|
|
message = SDL_realloc_orig(NULL, 1);
|
|
|
|
|
if (!message) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
*message = 0;
|
|
|
|
|
|
2022-11-30 12:51:59 -08:00
|
|
|
#define ADD_LINE() \
|
|
|
|
|
message_size += (SDL_strlen(line) + 1); \
|
2017-10-12 14:20:17 -07:00
|
|
|
tmp = (char *)SDL_realloc_orig(message, message_size); \
|
2022-11-30 12:51:59 -08:00
|
|
|
if (!tmp) { \
|
|
|
|
|
return; \
|
|
|
|
|
} \
|
|
|
|
|
message = tmp; \
|
2017-10-12 14:20:17 -07:00
|
|
|
SDL_strlcat(message, line, message_size)
|
|
|
|
|
|
2023-03-09 15:10:00 -08:00
|
|
|
SDL_strlcpy(line, "Memory allocations:\n", sizeof(line));
|
2017-10-12 14:20:17 -07:00
|
|
|
ADD_LINE();
|
|
|
|
|
|
|
|
|
|
count = 0;
|
|
|
|
|
total_allocated = 0;
|
|
|
|
|
for (index = 0; index < SDL_arraysize(s_tracked_allocations); ++index) {
|
|
|
|
|
for (entry = s_tracked_allocations[index]; entry; entry = entry->next) {
|
2023-03-09 15:10:00 -08:00
|
|
|
(void)SDL_snprintf(line, sizeof(line), "Allocation %d: %d bytes\n", count, (int)entry->size);
|
2017-10-12 14:20:17 -07:00
|
|
|
ADD_LINE();
|
|
|
|
|
/* Start at stack index 1 to skip our tracking functions */
|
|
|
|
|
for (stack_index = 1; stack_index < SDL_arraysize(entry->stack); ++stack_index) {
|
2024-07-27 20:02:43 +02:00
|
|
|
char stack_entry_description[256] = "???";
|
|
|
|
|
|
2017-10-12 14:20:17 -07:00
|
|
|
if (!entry->stack[stack_index]) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
2024-07-27 20:02:43 +02:00
|
|
|
#ifdef HAVE_LIBUNWIND_H
|
|
|
|
|
{
|
2024-08-30 23:29:09 +02:00
|
|
|
#ifdef SDLTEST_UNWIND_NO_PROC_NAME_BY_IP
|
|
|
|
|
if (s_unwind_symbol_names) {
|
|
|
|
|
(void)SDL_snprintf(stack_entry_description, sizeof(stack_entry_description), "%s", entry->stack_names[stack_index]);
|
|
|
|
|
}
|
2024-08-30 21:29:54 +02:00
|
|
|
#else
|
2024-07-27 20:02:43 +02:00
|
|
|
char name[256] = "???";
|
2024-08-30 21:29:54 +02:00
|
|
|
unw_word_t offset = 0;
|
2024-07-27 20:02:43 +02:00
|
|
|
unw_get_proc_name_by_ip(unw_local_addr_space, entry->stack[stack_index], name, sizeof(name), &offset, NULL);
|
|
|
|
|
(void)SDL_snprintf(stack_entry_description, sizeof(stack_entry_description), "%s+0x%llx", name, (long long unsigned int)offset);
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
#elif defined(SDL_PLATFORM_WIN32)
|
|
|
|
|
{
|
|
|
|
|
DWORD64 dwDisplacement = 0;
|
|
|
|
|
char symbol_buffer[sizeof(SYMBOL_INFO) + MAX_SYM_NAME * sizeof(TCHAR)];
|
|
|
|
|
PSYMBOL_INFO pSymbol = (PSYMBOL_INFO)symbol_buffer;
|
|
|
|
|
DWORD lineColumn = 0;
|
|
|
|
|
pSymbol->SizeOfStruct = sizeof(SYMBOL_INFO);
|
|
|
|
|
pSymbol->MaxNameLen = MAX_SYM_NAME;
|
|
|
|
|
IMAGEHLP_LINE64 dbg_line;
|
|
|
|
|
dbg_line.SizeOfStruct = sizeof(dbg_line);
|
2024-07-31 18:35:59 -07:00
|
|
|
dbg_line.FileName = "";
|
|
|
|
|
dbg_line.LineNumber = 0;
|
2024-07-27 20:02:43 +02:00
|
|
|
|
|
|
|
|
if (dyn_dbghelp.module) {
|
|
|
|
|
if (!dyn_dbghelp.pSymFromAddr(GetCurrentProcess(), entry->stack[stack_index], &dwDisplacement, pSymbol)) {
|
|
|
|
|
SDL_strlcpy(pSymbol->Name, "???", MAX_SYM_NAME);
|
|
|
|
|
dwDisplacement = 0;
|
|
|
|
|
}
|
2024-07-31 18:35:59 -07:00
|
|
|
dyn_dbghelp.pSymGetLineFromAddr64(GetCurrentProcess(), (DWORD64)entry->stack[stack_index], &lineColumn, &dbg_line);
|
2024-07-27 20:02:43 +02:00
|
|
|
}
|
|
|
|
|
SDL_snprintf(stack_entry_description, sizeof(stack_entry_description), "%s+0x%I64x %s:%u", pSymbol->Name, dwDisplacement, dbg_line.FileName, (Uint32)dbg_line.LineNumber);
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
(void)SDL_snprintf(line, sizeof(line), "\t0x%" SDL_PRIx64 ": %s\n", entry->stack[stack_index], stack_entry_description);
|
|
|
|
|
|
2017-10-12 14:20:17 -07:00
|
|
|
ADD_LINE();
|
|
|
|
|
}
|
|
|
|
|
total_allocated += entry->size;
|
|
|
|
|
++count;
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-03-09 15:10:00 -08:00
|
|
|
(void)SDL_snprintf(line, sizeof(line), "Total: %.2f Kb in %d allocations\n", total_allocated / 1024.0, count);
|
2017-10-12 14:20:17 -07:00
|
|
|
ADD_LINE();
|
|
|
|
|
#undef ADD_LINE
|
|
|
|
|
|
|
|
|
|
SDL_Log("%s", message);
|
|
|
|
|
}
|