diff options
| author | 2026-11-06 02:44:20 +0000 | |
|---|---|---|
| committer | 2026-11-06 02:44:20 +0000 | |
| commit | 1858f42578f82b1211d4b84e46b5d0f893b7c42d (patch) | |
| tree | 408e1bb1a5ae1e995fe4a2619959a9da79632b7f /src/dbg.c | |
| parent | feat(bits): add bit/memory manipulation utilities (diff) | |
| download | codebase-1858f42578f82b1211d4b84e46b5d0f893b7c42d.tar.gz | |
feat(dbg): add debugging utilities
Diffstat (limited to 'src/dbg.c')
| -rw-r--r-- | src/dbg.c | 204 |
1 files changed, 204 insertions, 0 deletions
diff --git a/src/dbg.c b/src/dbg.c new file mode 100644 index 0000000..3d95180 --- /dev/null +++ b/src/dbg.c | |||
| @@ -0,0 +1,204 @@ | |||
| 1 | // dbg.c --- debugging utilities -*- C -*- | ||
| 2 | // | ||
| 3 | // Copyright (c) 2026 Martin Michalec <martin@michalec.dev> | ||
| 4 | // | ||
| 5 | // SPDX-License-Identifier: MIT | ||
| 6 | // | ||
| 7 | // This file is part of COSMOS Codebase. | ||
| 8 | // | ||
| 9 | // Commentary: | ||
| 10 | // | ||
| 11 | // TODO(cmmm): somehow automatically _dbg_backtrace on exit code != 0. | ||
| 12 | // | ||
| 13 | // Code: | ||
| 14 | |||
| 15 | #include "dbg.h" | ||
| 16 | |||
| 17 | #ifndef NDEBUG | ||
| 18 | |||
| 19 | #endif // NDEBUG | ||
| 20 | |||
| 21 | #ifndef DBG_NO_FEAT_TRACE | ||
| 22 | |||
| 23 | #endif // DBG_NO_FEAT_TRACE | ||
| 24 | |||
| 25 | #ifndef DBG_NO_FEAT_BACKTRACE | ||
| 26 | |||
| 27 | #ifndef __TINYC__ | ||
| 28 | #include <backtrace.h> | ||
| 29 | #include <unistd.h> | ||
| 30 | struct bt_ctx { | ||
| 31 | int frame; | ||
| 32 | int fd; | ||
| 33 | }; | ||
| 34 | |||
| 35 | static void | ||
| 36 | bt_error_callback (void *data, const char *msg, int errnum) | ||
| 37 | { | ||
| 38 | struct bt_ctx *ctx = data; | ||
| 39 | char buf[512]; | ||
| 40 | int len; | ||
| 41 | |||
| 42 | if (errnum > 0) { | ||
| 43 | len = snprintf (buf, sizeof (buf), | ||
| 44 | " [libbacktrace error: %s (errno %d)]\n", | ||
| 45 | msg ? msg : "unknown error", errnum); | ||
| 46 | } else { | ||
| 47 | len = snprintf (buf, sizeof (buf), " [libbacktrace error: %s]\n", | ||
| 48 | msg ? msg : "unknown error"); | ||
| 49 | } | ||
| 50 | |||
| 51 | if (len > 0) | ||
| 52 | write (ctx->fd, buf, len); | ||
| 53 | } | ||
| 54 | |||
| 55 | static int | ||
| 56 | bt_full_callback (void *data, uintptr_t pc, | ||
| 57 | const char *file, int line, const char *func) | ||
| 58 | { | ||
| 59 | struct bt_ctx *ctx = data; | ||
| 60 | char buf[512]; | ||
| 61 | int len = 0; | ||
| 62 | |||
| 63 | if (pc == (uintptr_t) -1 || pc == 0) | ||
| 64 | return 1; | ||
| 65 | |||
| 66 | if (file && func) { | ||
| 67 | len = snprintf (buf, sizeof (buf), " #%d 0x%lx in %s at %s:%d\n", | ||
| 68 | ctx->frame++, pc, func, file, line); | ||
| 69 | } else if (func) { | ||
| 70 | len = snprintf (buf, sizeof (buf), " #%d 0x%lx in %s\n", | ||
| 71 | ctx->frame++, pc, func); | ||
| 72 | } else if (file) { | ||
| 73 | len = snprintf (buf, sizeof (buf), " #%d 0x%lx at %s:%d\n", | ||
| 74 | ctx->frame++, pc, file, line); | ||
| 75 | } else { | ||
| 76 | len = snprintf (buf, sizeof (buf), " #%d 0x%lx [unknown]\n", | ||
| 77 | ctx->frame++, pc); | ||
| 78 | } | ||
| 79 | |||
| 80 | if (len > 0) | ||
| 81 | write (ctx->fd, buf, len); | ||
| 82 | |||
| 83 | if (func && strcmp (func, "main") == 0) | ||
| 84 | return 1; | ||
| 85 | |||
| 86 | return 0; | ||
| 87 | } | ||
| 88 | |||
| 89 | int | ||
| 90 | _dbg_backtrace (int fd, int skip, const char *fmt, ...) | ||
| 91 | { | ||
| 92 | char buf[512]; | ||
| 93 | |||
| 94 | va_list ap; | ||
| 95 | va_start (ap, fmt); | ||
| 96 | int len = vsnprintf (buf, sizeof (buf), fmt, ap); | ||
| 97 | va_end (ap); | ||
| 98 | |||
| 99 | if (len >= (int) sizeof (buf)) { | ||
| 100 | len = sizeof (buf); | ||
| 101 | buf[len - 4] = '.'; | ||
| 102 | buf[len - 3] = '.'; | ||
| 103 | buf[len - 2] = '.'; | ||
| 104 | buf[len - 1] = '\n'; | ||
| 105 | } | ||
| 106 | |||
| 107 | if (len > 0) | ||
| 108 | write (fd, buf, len); | ||
| 109 | |||
| 110 | struct bt_ctx ctx = { 0, fd }; | ||
| 111 | struct backtrace_state *state = | ||
| 112 | backtrace_create_state (NULL, 0, bt_error_callback, &ctx); | ||
| 113 | backtrace_full (state, skip, bt_full_callback, bt_error_callback, &ctx); | ||
| 114 | return len; | ||
| 115 | } | ||
| 116 | #else // __TINYC__ | ||
| 117 | int | ||
| 118 | _dbg_backtrace (int fd, int skip, const char *fmt, ...) | ||
| 119 | { | ||
| 120 | // TODO(cmmm): implement custom backtracing for tcc. | ||
| 121 | return 0; | ||
| 122 | } | ||
| 123 | #endif // __TINYC__ | ||
| 124 | |||
| 125 | #ifndef __TCC_BACKTRACE__ | ||
| 126 | #include <signal.h> | ||
| 127 | void | ||
| 128 | dbg_backtrace_sighandler_abort (int signum) | ||
| 129 | { | ||
| 130 | _dbg_backtrace (2, 3, "Received signal: %d\nStack backtrace:\n", signum); | ||
| 131 | abort (); | ||
| 132 | } | ||
| 133 | |||
| 134 | BEFORE_MAIN void | ||
| 135 | dbg_backtrace_on_sigsegv (void) | ||
| 136 | { | ||
| 137 | signal (SIGSEGV, dbg_backtrace_sighandler_abort); | ||
| 138 | } | ||
| 139 | |||
| 140 | BEFORE_MAIN void | ||
| 141 | dbg_backtrace_on_sigterm (void) | ||
| 142 | { | ||
| 143 | signal (SIGTERM, dbg_backtrace_sighandler_abort); | ||
| 144 | } | ||
| 145 | #endif // __TCC_BACKTRACE__ | ||
| 146 | |||
| 147 | #endif // DBG_NO_FEAT_BACKTRACE | ||
| 148 | |||
| 149 | #ifndef DBG_NO_FEAT_ASSERT | ||
| 150 | #include <stdarg.h> | ||
| 151 | #include <stdio.h> | ||
| 152 | #include <string.h> | ||
| 153 | #include <unistd.h> | ||
| 154 | |||
| 155 | __attribute__ ((noreturn)) | ||
| 156 | __attribute__ ((__format__ (__printf__, 3, 4))) | ||
| 157 | void | ||
| 158 | _dbg_assert_failed (struct dbg_loc loc, const char *expr, const char *fmt, ...) | ||
| 159 | { | ||
| 160 | char buf[256]; | ||
| 161 | size_t len = snprintf ( | ||
| 162 | buf, sizeof (buf), "%s: error %s: Assertion `%s' failed%s", | ||
| 163 | loc.site, loc.func, expr, fmt ? ": " : ".\n"); | ||
| 164 | |||
| 165 | if (len == 0) | ||
| 166 | goto trace; | ||
| 167 | |||
| 168 | if (len >= sizeof (buf)) { | ||
| 169 | len = sizeof (buf) - 1; | ||
| 170 | goto output; | ||
| 171 | } | ||
| 172 | |||
| 173 | if (fmt) { | ||
| 174 | va_list ap; | ||
| 175 | va_start (ap, fmt); | ||
| 176 | len += vsnprintf (buf + len, sizeof (buf) - len, fmt, ap); | ||
| 177 | va_end (ap); | ||
| 178 | |||
| 179 | if (len >= sizeof (buf)) { | ||
| 180 | len = sizeof (buf); | ||
| 181 | buf[len - 1] = '\n'; | ||
| 182 | buf[len - 2] = '.'; | ||
| 183 | buf[len - 3] = '.'; | ||
| 184 | buf[len - 4] = '.'; | ||
| 185 | } else { | ||
| 186 | buf[len] = '\n'; | ||
| 187 | } | ||
| 188 | } | ||
| 189 | |||
| 190 | output: | ||
| 191 | write (STDERR_FILENO, buf, len); | ||
| 192 | |||
| 193 | trace: | ||
| 194 | #if !defined (DBG_NO_FEAT_BACKTRACE) && !defined (__TCC_BACKTRACE__) | ||
| 195 | if (strcmp (func, "main") != 0) | ||
| 196 | _dbg_backtrace (STDERR_FILENO, 3, "Stack backtrace:\n"); | ||
| 197 | #endif // !defined (DBG_NO_FEAT_BACKTRACE) && !defined (__TCC_BACKTRACE__) | ||
| 198 | |||
| 199 | abort (); | ||
| 200 | } | ||
| 201 | |||
| 202 | #endif // DBG_NO_FEAT_ASSERT | ||
| 203 | |||
| 204 | // dbg.c ends here | ||
