Created
April 10, 2024 22:57
-
-
Save laytan/f06fe6e993ed07e01c6460b0b55ba994 to your computer and use it in GitHub Desktop.
Stack traces using Odin instrumentation features
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import "core:runtime" | |
main :: proc() { | |
context.assertion_failure_proc = print_call_frames | |
foo() | |
} | |
foo :: proc() { | |
bar() | |
} | |
bar :: proc() { | |
assert(false) | |
} | |
print_call_frames :: proc(prefix, message: string, loc := #caller_location) -> ! { | |
for f := frame; f != nil; f = f.prev { | |
runtime.print_caller_location(f.loc) | |
runtime.print_string(" in ") | |
runtime.print_string(f.loc.procedure) | |
runtime.print_string("\n") | |
} | |
runtime.default_assertion_failure_proc(prefix, message, loc) | |
} | |
Frame :: struct { | |
prev: ^Frame, | |
loc: runtime.Source_Code_Location, | |
} | |
@(thread_local) | |
frame: ^Frame | |
@(instrumentation_enter) | |
hi :: #force_inline proc "contextless" (_, _: rawptr, loc: runtime.Source_Code_Location) { | |
frame = &Frame{ | |
prev = frame, | |
loc = loc, | |
} | |
} | |
@(instrumentation_exit) | |
bye :: #force_inline proc "contextless" (_, _: rawptr, loc: runtime.Source_Code_Location) { | |
frame = frame.prev | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment