Skip to content

Instantly share code, notes, and snippets.

@laytan
Created April 10, 2024 22:57
Show Gist options
  • Save laytan/f06fe6e993ed07e01c6460b0b55ba994 to your computer and use it in GitHub Desktop.
Save laytan/f06fe6e993ed07e01c6460b0b55ba994 to your computer and use it in GitHub Desktop.
Stack traces using Odin instrumentation features
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