Implement __assert_func and __assert, improve abort()

This commit is contained in:
Maschell 2023-11-23 12:16:21 +01:00
parent 5fc4875b35
commit a56d1698b3
2 changed files with 61 additions and 7 deletions

View File

@ -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 ";" \

View File

@ -1,6 +1,8 @@
#include "wups_reent.h"
#include "wups_thread_specific.h"
#include <cstddef>
#include <cstdio>
#include <cstring>
#include <stdint.h>
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 */
}