added: current_line to Stack_Frame
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Wed, 14 Jun 2023 16:48:30 +0000 (11:48 -0500)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Wed, 14 Jun 2023 16:48:30 +0000 (11:48 -0500)
compiler/src/wasm_emit.c
core/alloc/heap.onyx
core/alloc/logging.onyx
core/runtime/common.onyx
core/runtime/info/stack_trace.onyx

index 028e76fca37816b16268b5070b7d49a3f91b989a..ea9f945990135675cb57fc90238f78ebff8f60bf 100644 (file)
@@ -2191,6 +2191,10 @@ EMIT_FUNC(call, AstCall* call) {
     if (cc == CC_Return_Stack) reserve_size += return_size;
 
     if (context.options->stack_trace_enabled) {
+        emit_stack_address(mod, &code, mod->stack_trace_idx, NULL);
+        WIL(NULL, WI_I32_CONST, call->token->pos.line);
+        emit_store_instruction(mod, &code, &basic_types[Basic_Kind_U32], 8);
+
         u64 stack_trace_pass_global = bh_imap_get(&mod->index_map, (u64) &builtin_stack_trace);
         emit_stack_address(mod, &code, mod->stack_trace_idx, NULL);
         WIL(NULL, WI_GLOBAL_SET, stack_trace_pass_global);
index c9508f296d1646af1dd6eaea45a010300d44aece..bc2c887ecedf690f0a6058c444712b5cae9a421e 100644 (file)
@@ -201,7 +201,7 @@ get_freed_size :: () => {
                 #if Enable_Stack_Trace {
                     trace := runtime.info.get_stack_trace();
                     for trace {
-                        log(.Error, "Core", core.tprintf("in {} ({}:{})", it.func_name, it.file, it.line));
+                        log(.Error, "Core", core.tprintf("in {} ({}:{})", it.info.func_name, it.info.file, it.current.line));
                     }
                 }
             }
index f952a7fb99150f2501489e86964b33787a601952..786b43b2238133ee6f7cf7785cd4cfc95f6dc67a 100644 (file)
@@ -28,7 +28,7 @@ logging_allocator_proc :: (data: rawptr, aa: AllocationAction, size: u32, align:
     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));
+        log(.Info, "Core", tprintf("in {} ({}:{})", it.info.func_name, it.info.file, it.current_line));
     }
 
     return res;
index 0f36e8b66921d82ada1663941b85526cff79ec07..498fdb9ad4137fc0dbb84ad6673fae51b90b4102 100644 (file)
@@ -9,6 +9,18 @@ use runtime.platform { __output_string }
 // Every platform should define this, even if is it just '() {}'.
 #export "_start" platform.__start
 
+#local
+__output_uint :: (n: u64) {
+    buf: [128] u8;
+    i := 127;
+    while n > 0 && i >= 0 {
+        buf[i] = ~~('0' + n % 10);
+        i -= 1;
+        n /= 10;
+    }
+
+    __output_string(buf[i .. 128]);
+}
 
 // The default assert handler. This assumes that __output_string
 // and __exit are defined in the 'runtime' package.
@@ -22,9 +34,11 @@ __assert_handler :: (msg: str, site: CallSite) {
         trace := info.get_stack_trace();
         for trace[1..trace.length] {
             __output_string(" in ");
-            __output_string(it.func_name);
+            __output_string(it.info.func_name);
             __output_string(" (");
-            __output_string(it.file);
+            __output_string(it.info.file);
+            __output_string(":");
+            __output_uint(~~it.current_line);
             __output_string(")\n");
         }
 
@@ -104,4 +118,4 @@ __thread_initialize :: () {
         raw_free(alloc.heap_allocator, __tls_base);
         core.thread.__exited(id);
     }
-}
\ No newline at end of file
+}
index fa6032b89285d43aac12f0cddb0e3437c442fa82..8b2e00413958a3f16a7fed7db2d8bae74c61d847 100644 (file)
@@ -22,16 +22,22 @@ Stack_Node :: struct {
 Stack_Trace :: struct {
     prev: &Stack_Trace;
     data: &Stack_Node;
+    current_line: u32;
+}
+
+Stack_Frame :: struct {
+    info: &Stack_Node;
+    current_line: u32;
 }
 
 #if runtime.Stack_Trace_Enabled {
 
-get_stack_trace :: () -> [..] &Stack_Node {
-    trace := make([..] &Stack_Node, 8, alloc.temp_allocator);
+get_stack_trace :: () -> [..] Stack_Frame {
+    trace := make([..] Stack_Frame, 8, alloc.temp_allocator);
 
     walker := __stack_trace.prev;
     while walker {
-        trace << walker.data;
+        trace << .{ walker.data, walker.current_line };
         walker = walker.prev;
     }
 
@@ -40,7 +46,7 @@ get_stack_trace :: () -> [..] &Stack_Node {
 
 } else {
 
-get_stack_trace :: () -> [] &Stack_Node {
+get_stack_trace :: () -> [] Stack_Frame {
     return .[];
 }