Browse Source

Remove need for allocator and MAX_FRAMES in `trace.frames`

gingerBill 1 year ago
parent
commit
c0b7dd7da6

+ 2 - 3
core/debug/trace/trace.odin

@@ -4,7 +4,6 @@ import "base:intrinsics"
 import "base:runtime"
 
 Frame :: distinct uintptr
-MAX_FRAMES :: 512
 
 Frame_Location :: struct {
 	using loc: runtime.Source_Code_Location,
@@ -32,8 +31,8 @@ destroy :: proc(ctx: ^Context) -> bool {
 }
 
 @(require_results)
-frames :: proc(ctx: ^Context, skip: uint, allocator: runtime.Allocator) -> []Frame {
-	return _frames(ctx, skip, allocator)
+frames :: proc(ctx: ^Context, skip: uint, frames_buffer: []Frame) -> []Frame {
+	return _frames(ctx, skip, frames_buffer)
 }
 
 @(require_results)

+ 9 - 8
core/debug/trace/trace_linux.odin

@@ -2,6 +2,7 @@
 //+build linux
 package debug_trace
 
+import "base:intrinsics"
 import "base:runtime"
 import "core:strings"
 import "core:fmt"
@@ -92,17 +93,16 @@ _destroy :: proc(ctx: ^Context) -> bool {
 }
 
 @(private="package")
-_frames :: proc(ctx: ^Context, skip: uint, allocator: runtime.Allocator) -> (frames: []Frame) {
+_frames :: proc "contextless" (ctx: ^Context, skip: uint, frames_buffer: []Frame) -> (frames: []Frame) {
 	Backtrace_Context :: struct {
 		ctx:         ^Context,
-		rt_ctx:      runtime.Context,
-		frames:      [MAX_FRAMES]Frame,
+		frames:      []Frame,
 		frame_count: int,
 	}
 
 	btc := &Backtrace_Context{
 		ctx = ctx,
-		rt_ctx = context,
+		frames = frames_buffer,
 	}
 	backtrace_simple(
 		ctx.impl.state,
@@ -113,6 +113,9 @@ _frames :: proc(ctx: ^Context, skip: uint, allocator: runtime.Allocator) -> (fra
 			if address == 0 {
 				return 1
 			}
+			if btc.frame_count == len(btc.frames) {
+				return 1
+			}
 			btc.frames[btc.frame_count] = address
 			btc.frame_count += 1
 			return 0
@@ -121,10 +124,8 @@ _frames :: proc(ctx: ^Context, skip: uint, allocator: runtime.Allocator) -> (fra
 		btc,
 	)
 
-	res := btc.frames[:btc.frame_count]
-	if len(res) > 0 {
-		frames = make([]Frame, btc.frame_count, allocator)
-		copy(frames, res)
+	if btc.frame_count > 0 {
+		frames = btc.frames[:btc.frame_count]
 	}
 	return
 }

+ 5 - 7
core/debug/trace/trace_windows.odin

@@ -28,16 +28,14 @@ _destroy :: proc "contextless" (ctx: ^Context) -> bool {
 	return true
 }
 
-_frames :: proc(ctx: ^Context, skip: uint, allocator: runtime.Allocator) -> []Frame {
-	buffer: [MAX_FRAMES]rawptr
-	frame_count := win32.RtlCaptureStackBackTrace(u32(skip) + 2, len(buffer), &buffer[0], nil)
-	frames := make([]Frame, frame_count, allocator)
-	for &f, i in frames {
+_frames :: proc "contextless" (ctx: ^Context, skip: uint, frames_buffer: []Frame) -> []Frame {
+	frame_count := win32.RtlCaptureStackBackTrace(u32(skip) + 2, len(frames_buffer), &frames_buffer[0], nil)
+	for i in 0..<frame_count {
 		// NOTE: Return address is one after the call instruction so subtract a byte to
 		// end up back inside the call instruction which is needed for SymFromAddr.
-		f = Frame(buffer[i]) - 1
+		frames_buffer[i] -= 1
 	}
-	return frames
+	return frames_buffer[:frame_count]
 }