added: stack trace logging to logging allocator
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Sat, 10 Jun 2023 02:19:57 +0000 (21:19 -0500)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Sat, 10 Jun 2023 02:19:57 +0000 (21:19 -0500)
core/alloc/logging.onyx
core/runtime/info/stack_trace.onyx

index 2b62d4171f6676c016da0560275dc95d33473c5a..f952a7fb99150f2501489e86964b33787a601952 100644 (file)
@@ -5,6 +5,7 @@ package core.alloc.log
 // allocator.
 
 use core
+use runtime
 
 #local
 Allocation_Action_Strings := str.[
@@ -24,6 +25,12 @@ logging_allocator_proc :: (data: rawptr, aa: AllocationAction, size: u32, align:
 
     log(.Info, "Core", msg);
 
+    trace := runtime.info.get_stack_trace();
+    defer if trace do delete(&trace);
+    for trace {
+        log(.Info, "Core", tprintf("in {} ({}:{})", it.func_name, it.file, it.line));
+    }
+
     return res;
 }
 
index 95c929dffc3876666c47cc347a645f93aaf1542c..fa6032b89285d43aac12f0cddb0e3437c442fa82 100644 (file)
@@ -1,5 +1,16 @@
 package runtime.info
+
 use runtime
+use core {alloc} // These can included because this file is only included in non-custom runtimes.
+
+//
+// This file contains the code for working with runtime stack traces.
+// The only function in this file is `get_stack_trace`, which returns
+// an array of stack nodes, allocated from the threads temporary allocator.
+// This is not the `context.temp_allocator`, because certain builtin in
+// allocators use `get_stack_trace`, and can accidentally cause infinite
+// recursion.
+//
 
 Stack_Node :: struct {
     file: str;
@@ -15,8 +26,8 @@ Stack_Trace :: struct {
 
 #if runtime.Stack_Trace_Enabled {
 
-get_stack_trace :: () -> [] &Stack_Node {
-    trace: [..] &Stack_Node;
+get_stack_trace :: () -> [..] &Stack_Node {
+    trace := make([..] &Stack_Node, 8, alloc.temp_allocator);
 
     walker := __stack_trace.prev;
     while walker {