diff options
| author | 2026-11-06 02:49:23 +0000 | |
|---|---|---|
| committer | 2026-11-06 02:49:23 +0000 | |
| commit | 908b70ab00fb03ea62d1c74b0dc07bc9c8df8cc0 (patch) | |
| tree | 7d7c8f41dd19513c412759e49a44890c51d8eeb0 /include | |
| parent | feat(lgr): add logger (diff) | |
| download | codebase-908b70ab00fb03ea62d1c74b0dc07bc9c8df8cc0.tar.gz | |
feat(unw): add stack unwinding
Diffstat (limited to 'include')
| -rw-r--r-- | include/unw.h | 89 |
1 files changed, 89 insertions, 0 deletions
diff --git a/include/unw.h b/include/unw.h new file mode 100644 index 0000000..21564d7 --- /dev/null +++ b/include/unw.h | |||
| @@ -0,0 +1,89 @@ | |||
| 1 | // unw.h --- stack unwinding -*- 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): keep track of DBG_LOC and or backtrace conditionally. | ||
| 12 | // This could be used for tracing unwinds. | ||
| 13 | // | ||
| 14 | // TODO(cmmm): save a msg, explaining why the specific unw happened, | ||
| 15 | // only in debug builds | ||
| 16 | // | ||
| 17 | // Code: | ||
| 18 | |||
| 19 | #ifndef INCLUDED_COSMOS_UNW_H | ||
| 20 | #define INCLUDED_COSMOS_UNW_H | ||
| 21 | |||
| 22 | #include <setjmp.h> | ||
| 23 | #include "macros.h" | ||
| 24 | #include "types.h" | ||
| 25 | #include "dbg.h" | ||
| 26 | |||
| 27 | struct unw_frame { | ||
| 28 | struct unw_frame *next; | ||
| 29 | jmp_buf jmp; | ||
| 30 | }; | ||
| 31 | |||
| 32 | struct unw_reason { | ||
| 33 | const char *sym; | ||
| 34 | const char *msg; | ||
| 35 | }; | ||
| 36 | |||
| 37 | #define UNW_DECLARE_REASON(sym) \ | ||
| 38 | extern const struct unw_reason UNW_REASON_ ## sym | ||
| 39 | #define UNW_DEFINE_REASON(SYM, MSG) \ | ||
| 40 | const struct unw_reason UNW_REASON_ ## SYM = { .sym = #SYM, .msg = (MSG) } | ||
| 41 | |||
| 42 | #define UNW_BEG() do { \ | ||
| 43 | struct unw_frame _unw_frame; \ | ||
| 44 | _unw_code = _unw_push_frame (&_unw_frame); \ | ||
| 45 | if (unw_code == 0) | ||
| 46 | |||
| 47 | #define UNW_MATCH(sym) \ | ||
| 48 | else if (_unw_reason == &UNW_REASON_ ## sym && !(_unw_reason = NULL)) | ||
| 49 | #define UNW_MATCH_ANY() else if (_unw_reason && !(_unw_reason = NULL)) | ||
| 50 | |||
| 51 | #define UNW_ALWAYS() // Execution continues here unconditionally | ||
| 52 | |||
| 53 | #define UNW_END() \ | ||
| 54 | if (_unw_reason) \ | ||
| 55 | _unw (_unw_reason, _unw_code); \ | ||
| 56 | } while (0) | ||
| 57 | |||
| 58 | #define UNW(sym, code, why) _unw (&UNW_REASON_ ## sym, code) | ||
| 59 | |||
| 60 | // Expose as R-values, so that user can't modify them. | ||
| 61 | #define UNW_REASON ((const struct *unw_reason) _unw_reason) | ||
| 62 | #define UNW_CODE (( int ) _unw_code ) | ||
| 63 | |||
| 64 | extern THREAD_LOCAL const struct unw_reason *_unw_reason; | ||
| 65 | extern THREAD_LOCAL int _unw_code; | ||
| 66 | extern THREAD_LOCAL struct unw_frame *_unw_top; | ||
| 67 | |||
| 68 | static inline int | ||
| 69 | unw_push_frame (struct unw_frame *frame) | ||
| 70 | { | ||
| 71 | frame->next = _unw_top; | ||
| 72 | _unw_top = frame; | ||
| 73 | return setjmp (frame->jmp); | ||
| 74 | } | ||
| 75 | |||
| 76 | static inline void | ||
| 77 | unw_pop_frame (struct unw_frame *frame) | ||
| 78 | { | ||
| 79 | DBG_ASSERT_EQ (frame, _unw_top, "%p", "%p"); | ||
| 80 | _unw_top = frame->next; | ||
| 81 | } | ||
| 82 | |||
| 83 | void _unw (const struct unw_reason *, int code) NO_RETURN; | ||
| 84 | |||
| 85 | UNW_DECLARE_REASON (INVAL); // Don't overuse! | ||
| 86 | |||
| 87 | #endif // INCLUDED_COSMOS_UNW_H | ||
| 88 | |||
| 89 | // unw.h ends here | ||
