Linux add backtrace, fixing scaling / DPI problems

This commit is contained in:
Krzosa Karol
2025-12-14 17:05:55 +01:00
parent 2d79790d83
commit 5a12ab8d8c
12 changed files with 78 additions and 97 deletions

View File

@@ -14,11 +14,58 @@
#include <stdlib.h>
#include <spawn.h>
#include <poll.h>
#include <execinfo.h>
#include <backtrace.h>
API void (*Error)(const char *, ...);
struct backtrace_state *backtrace_state = NULL;
void os_core_backtrace_error_callback(void *data, const char *msg, int errnum) {
Error("libbacktrace error: %s (errnum: %d)\n", msg, errnum);
}
int os_core_backtrace_print_callback(void *data, uintptr_t pc, const char *filename, int lineno, const char *function) {
bool printed = false;
if (filename != NULL) {
char buffer[512];
char *f = realpath(filename, buffer);
printf("%s:%d:1: ", f, lineno);
printed = true;
}
if (function != NULL) {
printf("%s", function);
printed = true;
}
if (printed) {
printf("\n");
}
return 0;
}
void os_unix_crash_handler(int signal, siginfo_t* info, void* context) {
backtrace_full(backtrace_state, 2, os_core_backtrace_print_callback, os_core_backtrace_error_callback, NULL);
exit(1);
}
void os_unix_register_crash_handler(void) {
backtrace_state = backtrace_create_state(NULL, 1, os_core_backtrace_error_callback, NULL);
struct sigaction sa;
sa.sa_sigaction = os_unix_crash_handler;
sigemptyset(&sa.sa_mask);
sa.sa_flags = SA_RESTART | SA_SIGINFO;
sigaction(SIGSEGV, &sa, NULL);
sigaction(SIGABRT, &sa, NULL);
sigaction(SIGBUS, &sa, NULL);
sigaction(SIGILL, &sa, NULL);
sigaction(SIGFPE, &sa, NULL);
}
API void InitOS(void (*error_proc)(const char *, ...)) {
Error = error_proc;
os_unix_register_crash_handler();
}
API String ReadFile(Allocator al, String path) {
@@ -171,9 +218,9 @@ API void Advance(FileIter *it) {
const char *dir_char_ending = it->is_directory ? "/" : "";
const char *separator = it->path.data[it->path.len - 1] == '/' ? "" : "/";
it->relative_path = Format(it->allocator, "%.*s%s%s%s", FmtString(it->path), separator, file->d_name, dir_char_ending);
it->relative_path = Format(it->allocator, "%S%s%s%s", it->path, separator, file->d_name, dir_char_ending);
it->absolute_path = GetAbsolutePath(it->allocator, it->relative_path);
if (it->is_directory) it->absolute_path = Format(it->allocator, "%.*s/", FmtString(it->absolute_path));
if (it->is_directory) it->absolute_path = Format(it->allocator, "%S/", it->absolute_path);
it->is_valid = true;
return;
}