From a56d1698b3cf7206c183534d03e1b3c9cfddb1e0 Mon Sep 17 00:00:00 2001 From: Maschell Date: Thu, 23 Nov 2023 12:16:21 +0100 Subject: [PATCH] Implement __assert_func and __assert, improve abort() --- include/wups/meta.h | 9 ++---- libraries/libwups/main.cpp | 59 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+), 7 deletions(-) diff --git a/include/wups/meta.h b/include/wups/meta.h index 0d200fe..e935c71 100644 --- a/include/wups/meta.h +++ b/include/wups/meta.h @@ -48,14 +48,9 @@ extern "C" { WUPS_USE_WUT_STDCPP(); \ WUPS___INIT_WRAPPER(); \ WUPS___FINI_WRAPPER(); \ - __EXTERN_C_MACRO void abort(); \ - __EXTERN_C_MACRO void OSFatal(const char *msg); \ - void abort() { \ - OSFatal(__plugin_name ": abort() called. Uncaught exception?"); \ - while (1) \ - ; \ - } \ WUPS_META(buildtimestamp, __DATE__ " " __TIME__); \ + extern const char wups_meta_plugin_name[] WUPS_SECTION("meta"); \ + const char wups_meta_plugin_name[] = __plugin_name; \ extern const char wups_meta_info_dump[] WUPS_SECTION("meta"); \ const char wups_meta_info_dump[] = "(plugin: " __plugin_name ";" \ "wups " WUPS_VERSION_STR ";" \ diff --git a/libraries/libwups/main.cpp b/libraries/libwups/main.cpp index b788672..3415e6e 100644 --- a/libraries/libwups/main.cpp +++ b/libraries/libwups/main.cpp @@ -1,6 +1,8 @@ #include "wups_reent.h" #include "wups_thread_specific.h" #include +#include +#include #include extern "C" void OSFatal(const char *); @@ -53,3 +55,60 @@ extern "C" void *__attribute__((weak)) wut_get_thread_specific(__wut_thread_spec void *wut_get_thread_specific(__wut_thread_specific_id id) { return wups_get_thread_specific(id); } + + +extern "C" const char wups_meta_plugin_name[]; +extern "C" void __attribute__((weak)) abort(void); +extern "C" void __attribute__((weak)) __assert_func(const char *file, int line, const char *func, const char *failedexpr); +extern "C" void __attribute__((weak)) __assert(const char *file, int line, const char *failedexpr); + +void __attribute__((weak)) +abort(void) { + char buffer[512] = {}; + strcat(buffer, "Wii U Plugin System (plugin: \""); + strcat(buffer, wups_meta_plugin_name); + strcat(buffer, "\"):\n Abort called. Uncaught exception?"); + OSFatal(buffer); + /* NOTREACHED */ + while (1) + ; +} + +void __attribute__((weak)) +__assert_func(const char *file, + int line, + const char *func, + const char *failedexpr) { + char tmp[512] = {}; + char buffer[512] = {}; + + snprintf(tmp, sizeof(tmp), "Wii U Plugin System (plugin: \"%s\"):\n\n" + "assertion \"%s\" failed:\n\n" + "file \"%s\", line %d%s%s", + wups_meta_plugin_name, failedexpr, file, line, func ? ", function: " : "", func ? func : ""); + + // make sure to add a \n every 64 characters to fit on the DRC screen. + char *target_ptr = buffer; + int i = 0, j = 0, lineLength = 0; + while (tmp[i] != '\0' && j < (int) sizeof(buffer) - 2) { + if (tmp[i] == '\n') { + lineLength = 0; + } else if (lineLength >= 64) { + target_ptr[j++] = '\n'; + lineLength = 0; + } + target_ptr[j++] = tmp[i++]; + lineLength++; + } + + OSFatal(buffer); + /* NOTREACHED */ +} + +void __attribute__((weak)) +__assert(const char *file, + int line, + const char *failedexpr) { + __assert_func(file, line, NULL, failedexpr); + /* NOTREACHED */ +}