Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement __assert_func and __assert, improve abort() #61

Merged
merged 1 commit into from
Nov 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 2 additions & 7 deletions include/wups/meta.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 ";" \
Expand Down
59 changes: 59 additions & 0 deletions libraries/libwups/main.cpp
Original file line number Diff line number Diff line change
@@ -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 *);
Expand Down Expand Up @@ -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 */
}
Loading