diff options
| -rw-r--r-- | include/dbg.h | 205 | ||||
| -rw-r--r-- | src/dbg.c | 204 |
2 files changed, 409 insertions, 0 deletions
diff --git a/include/dbg.h b/include/dbg.h new file mode 100644 index 0000000..4ab0801 --- /dev/null +++ b/include/dbg.h | |||
| @@ -0,0 +1,205 @@ | |||
| 1 | // dbg.h --- 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 | // Everything prefixed with DBG_ is replaced with a stub when NDEBUG | ||
| 12 | // is defined. | ||
| 13 | // | ||
| 14 | // What's included: | ||
| 15 | // - traces | ||
| 16 | // - backtraces | ||
| 17 | // - assertions | ||
| 18 | // | ||
| 19 | // Code: | ||
| 20 | |||
| 21 | #ifndef INCLUDED_COSMOS_DBG_H | ||
| 22 | #define INCLUDED_COSMOS_DBG_H | ||
| 23 | |||
| 24 | #ifdef NDEBUG | ||
| 25 | #define DBG_NO_FEAT_TRACE | ||
| 26 | #define DBG_NO_FEAT_BACKTRACE | ||
| 27 | #define DBG_NO_FEAT_ASSERT | ||
| 28 | #endif // NDEBUG | ||
| 29 | |||
| 30 | #ifndef NDEBUG | ||
| 31 | #include <stdio.h> | ||
| 32 | #include <stdlib.h> | ||
| 33 | #include "macros.h" | ||
| 34 | |||
| 35 | struct dbg_loc { | ||
| 36 | const char *site; | ||
| 37 | const char *func; | ||
| 38 | }; | ||
| 39 | |||
| 40 | #define DBG_LOC ((struct dbg_loc) { __FILE__ ":" TO_STRING (__LINE__), __func__ }) | ||
| 41 | |||
| 42 | #define DBG_DIE(fmt, ...) do { \ | ||
| 43 | fprintf (stderr, __FILE__ ":" TO_STRING (__LINE__) ": %s: " fmt "\n", \ | ||
| 44 | __func__, ##__VA_ARGS__); abort (); } while (0) | ||
| 45 | #define DBG_TODO(fmt, ...) DBG_DIE ("TODO: " fmt, ##__VA_ARGS__) | ||
| 46 | |||
| 47 | #else // NDEBUG | ||
| 48 | |||
| 49 | #define DBG_DIE( fmt, ...) abort () | ||
| 50 | #define DBG_TODO(fmt, ...) abort () | ||
| 51 | |||
| 52 | #endif // NDEBUG | ||
| 53 | |||
| 54 | #define DBG_TRACE(type, fmt, ...) do { \ | ||
| 55 | DBG_TRACE_BEG (type, ""); \ | ||
| 56 | DBG_TRACE_END (fmt, ##__VA_ARGS__); \ | ||
| 57 | } while (0) | ||
| 58 | |||
| 59 | #define DBG_TRACE_LOC(loc, type, fmt, ...) do { \ | ||
| 60 | DBG_TRACE_BEG_LOC (loc, type, ""); \ | ||
| 61 | DBG_TRACE_END (fmt, ##__VA_ARGS__); \ | ||
| 62 | } while (0) | ||
| 63 | |||
| 64 | #ifndef DBG_NO_FEAT_TRACE | ||
| 65 | |||
| 66 | #define DBG_TRACE_BEG(type, fmt, ...) \ | ||
| 67 | fprintf (stderr, "%s:%d: %s: %s: " fmt, \ | ||
| 68 | __FILE__, __LINE__, type, __func__, ##__VA_ARGS__) | ||
| 69 | #define DBG_TRACE_BEG_LOC(loc, type, fmt, ...) \ | ||
| 70 | fprintf (stderr, "%s: %s: %s: " fmt, \ | ||
| 71 | (loc).site, type, (loc).func, ##__VA_ARGS__) | ||
| 72 | #define DBG_TRACE_ADD(fmt, ...) fprintf (stderr, fmt, ##__VA_ARGS__); | ||
| 73 | #define DBG_TRACE_PAD(n ) fprintf (stderr, "%*s", n, ""); | ||
| 74 | #define DBG_TRACE_END(fmt, ...) fprintf (stderr, fmt "\n", ##__VA_ARGS__); | ||
| 75 | |||
| 76 | #else // DBG_NO_FEAT_TRACE | ||
| 77 | |||
| 78 | #define DBG_TRACE_BEG( type, fmt, ...) NOP () | ||
| 79 | #define DBG_TRACE_BEG_LOC(type, fmt, ...) NOP () | ||
| 80 | #define DBG_TRACE_ADD( fmt, ...) NOP () | ||
| 81 | #define DBG_TRACE_END( fmt, ...) NOP () | ||
| 82 | |||
| 83 | #endif // DBG_NO_FEAT_TRACE | ||
| 84 | |||
| 85 | #ifndef DBG_NO_FEAT_BACKTRACE | ||
| 86 | |||
| 87 | #ifndef __TCC_BACKTRACE__ | ||
| 88 | #define DBG_BACKTRACE(type, fmt, ...) _dbg_backtrace ( \ | ||
| 89 | 2, 0, "%s:%d: %s: %s: " fmt "\nStack Backtrace:\n", \ | ||
| 90 | __FILE__, __LINE__, type, __func__, ##__VA_ARGS__) | ||
| 91 | #else // __TCC_BACKTRACE__ | ||
| 92 | #define DBG_BACKTRACE(type, fmt, ...) | ||
| 93 | #endif // __TCC_BACKTRACE__ | ||
| 94 | |||
| 95 | __attribute__ ((__format__ (__printf__, 3, 4))) | ||
| 96 | int _dbg_backtrace (int fd, int skip, const char *fmt, ...); | ||
| 97 | |||
| 98 | #else // DBG_NO_FEAT_BACKTRACE | ||
| 99 | |||
| 100 | #define DBG_BACKTRACE(...) DBG_TRACE (__VA_ARGS__) | ||
| 101 | |||
| 102 | #endif // DBG_NO_FEAT_BACKTRACE | ||
| 103 | |||
| 104 | #ifndef DBG_NO_FEAT_ASSERT | ||
| 105 | |||
| 106 | __attribute__ ((noreturn)) | ||
| 107 | __attribute__ ((__format__ (__printf__, 3, 4))) | ||
| 108 | void _dbg_assert_failed (struct dbg_loc, const char *expr, const char *fmt, ...); | ||
| 109 | |||
| 110 | #define DBG_ASSERT(expr) \ | ||
| 111 | do { if (UNLIKELY (!(expr))) \ | ||
| 112 | _dbg_assert_failed (DBG_LOC, #expr, NULL); } while (0) | ||
| 113 | |||
| 114 | #define DBG_ASSERT_MSG(expr, msg, ...) \ | ||
| 115 | do { if (UNLIKELY (!(expr))) \ | ||
| 116 | _dbg_assert_failed (DBG_LOC, #expr, msg "\n", ##__VA_ARGS__); } while (0) | ||
| 117 | |||
| 118 | #define DBG_UNREACHABLE() DBG_ASSERT (!"Reached the unreachable!") | ||
| 119 | #define DBG_UNREACHABLE_MSG(fmt, ...) \ | ||
| 120 | DBG_ASSERT_MSG (!"Reached the unreachable!", fmt, ##__VA_ARGS__) | ||
| 121 | |||
| 122 | // REVIEW(cmmm): intricate assertions, | ||
| 123 | // there should be a less macro heavy way to do this. | ||
| 124 | #define DBG_ASSERT_BINOP(op, lexpr, rexpr, lstr, rstr, lfmt, rfmt) \ | ||
| 125 | do { \ | ||
| 126 | typeof (lexpr) lval = lexpr; \ | ||
| 127 | typeof (rexpr) rval = rexpr; \ | ||
| 128 | if (UNLIKELY (!(lval op rval))) \ | ||
| 129 | _dbg_assert_failed (DBG_LOC, \ | ||
| 130 | lstr " " #op " " rstr, \ | ||
| 131 | lfmt " " #op " " rfmt "\n", lval, rval); \ | ||
| 132 | } while (0) | ||
| 133 | |||
| 134 | #define DBG_ASSERT_BINOP_MSG(op, lexpr, rexpr, lstr, rstr, lfmt, rfmt, msg, ...) \ | ||
| 135 | do { \ | ||
| 136 | typeof (lexpr) lval = lexpr; \ | ||
| 137 | typeof (rexpr) rval = rexpr; \ | ||
| 138 | if (UNLIKELY (!(lval op rval))) \ | ||
| 139 | _dbg_assert_failed (DBG_LOC, \ | ||
| 140 | lstr " " #op " " rstr, \ | ||
| 141 | lfmt " " #op " " rfmt ": " \ | ||
| 142 | msg "\n", lval, rval, ##__VA_ARGS__); \ | ||
| 143 | } while (0) | ||
| 144 | |||
| 145 | #define DBG_ASSERT_TRIOP(lop, rop, lexpr, mexpr, rexpr, lstr, mstr, rstr, lfmt, mfmt, rfmt) \ | ||
| 146 | do { \ | ||
| 147 | typeof (lexpr) lval = lexpr; \ | ||
| 148 | typeof (mexpr) mval = mexpr; \ | ||
| 149 | typeof (rexpr) rval = rexpr; \ | ||
| 150 | if (UNLIKELY (!(lval lop mval && mval rop rval))) \ | ||
| 151 | _dbg_assert_failed (DBG_LOC, \ | ||
| 152 | lstr " " #lop " " mstr " " #rop " " rstr, \ | ||
| 153 | lfmt " " #lop " " mfmt " " #rop " " rfmt "\n", \ | ||
| 154 | lval, mval, rval); \ | ||
| 155 | } while (0) | ||
| 156 | |||
| 157 | #define DBG_ASSERT_TRIOP_MSG(lop, rop, lexpr, mexpr, rexpr, lstr, mstr, rstr, lfmt, mfmt, rfmt, msg, ...) \ | ||
| 158 | do { \ | ||
| 159 | typeof (lexpr) lval = lexpr; \ | ||
| 160 | typeof (mexpr) mval = mexpr; \ | ||
| 161 | typeof (rexpr) rval = rexpr; \ | ||
| 162 | if (UNLIKELY (!(lval lop mval && mval rop rval))) \ | ||
| 163 | _dbg_assert_failed (DBG_LOC, \ | ||
| 164 | lstr " " #lop " " mstr " " #rop " " rstr, \ | ||
| 165 | lfmt " " #lop " " mfmt " " #rop " " rfmt ": " \ | ||
| 166 | msg "\n", lval, mval, rval, ##__VA_ARGS__); \ | ||
| 167 | } while (0) | ||
| 168 | |||
| 169 | #define DBG_ASSERT_LT(lexpr, rexpr, lfmt, rfmt) DBG_ASSERT_BINOP ( <, lexpr, rexpr, #lexpr, #rexpr, lfmt, rfmt) | ||
| 170 | #define DBG_ASSERT_GT(lexpr, rexpr, lfmt, rfmt) DBG_ASSERT_BINOP ( >, lexpr, rexpr, #lexpr, #rexpr, lfmt, rfmt) | ||
| 171 | #define DBG_ASSERT_EQ(lexpr, rexpr, lfmt, rfmt) DBG_ASSERT_BINOP (==, lexpr, rexpr, #lexpr, #rexpr, lfmt, rfmt) | ||
| 172 | #define DBG_ASSERT_NE(lexpr, rexpr, lfmt, rfmt) DBG_ASSERT_BINOP (!=, lexpr, rexpr, #lexpr, #rexpr, lfmt, rfmt) | ||
| 173 | #define DBG_ASSERT_LE(lexpr, rexpr, lfmt, rfmt) DBG_ASSERT_BINOP (<=, lexpr, rexpr, #lexpr, #rexpr, lfmt, rfmt) | ||
| 174 | #define DBG_ASSERT_GE(lexpr, rexpr, lfmt, rfmt) DBG_ASSERT_BINOP (>=, lexpr, rexpr, #lexpr, #rexpr, lfmt, rfmt) | ||
| 175 | |||
| 176 | #define DBG_ASSERT_LT_MSG(lexpr, rexpr, lfmt, rfmt, msg, ...) DBG_ASSERT_BINOP_MSG ( <, lexpr, rexpr, #lexpr, #rexpr, lfmt, rfmt, msg, ##__VA_ARGS__) | ||
| 177 | #define DBG_ASSERT_GT_MSG(lexpr, rexpr, lfmt, rfmt, msg, ...) DBG_ASSERT_BINOP_MSG ( >, lexpr, rexpr, #lexpr, #rexpr, lfmt, rfmt, msg, ##__VA_ARGS__) | ||
| 178 | #define DBG_ASSERT_EQ_MSG(lexpr, rexpr, lfmt, rfmt, msg, ...) DBG_ASSERT_BINOP_MSG (==, lexpr, rexpr, #lexpr, #rexpr, lfmt, rfmt, msg, ##__VA_ARGS__) | ||
| 179 | #define DBG_ASSERT_NE_MSG(lexpr, rexpr, lfmt, rfmt, msg, ...) DBG_ASSERT_BINOP_MSG (!=, lexpr, rexpr, #lexpr, #rexpr, lfmt, rfmt, msg, ##__VA_ARGS__) | ||
| 180 | #define DBG_ASSERT_LE_MSG(lexpr, rexpr, lfmt, rfmt, msg, ...) DBG_ASSERT_BINOP_MSG (<=, lexpr, rexpr, #lexpr, #rexpr, lfmt, rfmt, msg, ##__VA_ARGS__) | ||
| 181 | #define DBG_ASSERT_GE_MSG(lexpr, rexpr, lfmt, rfmt, msg, ...) DBG_ASSERT_BINOP_MSG (>=, lexpr, rexpr, #lexpr, #rexpr, lfmt, rfmt, msg, ##__VA_ARGS__) | ||
| 182 | |||
| 183 | #define DBG_ASSERT_EZ(expr, fmt) DBG_ASSERT_BINOP (==, (int) expr, 0, #expr, "0", fmt, "%d") | ||
| 184 | #define DBG_ASSERT_NZ(expr, fmt) DBG_ASSERT_BINOP (!=, (int) expr, 0, #expr, "0", fmt, "%d") | ||
| 185 | #define DBG_ASSERT_EN(expr ) DBG_ASSERT_BINOP (==, (void *) expr, NULL, #expr, "NULL", "%p", "%p") | ||
| 186 | #define DBG_ASSERT_NN(expr ) DBG_ASSERT_BINOP (!=, (void *) expr, NULL, #expr, "NULL", "%p", "%p") | ||
| 187 | |||
| 188 | #define DBG_ASSERT_EZ_MSG(expr, fmt, msg, ...) DBG_ASSERT_BINOP_MSG (==, expr, 0, #expr, "0", fmt, "%d", msg, ##__VA_ARGS__) | ||
| 189 | #define DBG_ASSERT_NZ_MSG(expr, fmt, msg, ...) DBG_ASSERT_BINOP_MSG (!=, expr, 0, #expr, "0", fmt, "%d", msg, ##__VA_ARGS__) | ||
| 190 | #define DBG_ASSERT_EN_MSG(expr, msg, ...) DBG_ASSERT_BINOP_MSG (==, expr, NULL, #expr, "NULL", "%p", "%p", msg, ##__VA_ARGS__) | ||
| 191 | #define DBG_ASSERT_NN_MSG(expr, msg, ...) DBG_ASSERT_BINOP_MSG (!=, expr, NULL, #expr, "NULL", "%p", "%p", msg, ##__VA_ARGS__) | ||
| 192 | |||
| 193 | #define DBG_ASSERT_IN(lexpr, mexpr, rexpr, lfmt, mfmt, rfmt) DBG_ASSERT_TRIOP (<=, <=, lexpr, mexpr, rexpr, #lexpr, #mexpr, #rexpr, lfmt, mfmt, rfmt) | ||
| 194 | |||
| 195 | #define DBG_ASSERT_IN_MSG(lexpr, mexpr, rexpr, lfmt, mfmt, rfmt, msg, ...) DBG_ASSERT_TRIOP_MSG (<=, <=, lexpr, mexpr, rexpr, #lexpr, #mexpr, #rexpr, lfmt, mfmt, rfmt, msg, ##__VA_ARGS__) | ||
| 196 | |||
| 197 | #else // DBG_NO_FEAT_ASSERT | ||
| 198 | |||
| 199 | // TODO(cmmm): add stubs | ||
| 200 | |||
| 201 | #endif // DBG_NO_FEAT_ASSERT | ||
| 202 | |||
| 203 | #endif // INCLUDED_COSMOS_DBG_H | ||
| 204 | |||
| 205 | // dbg.h ends here | ||
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 | ||
