Skip to content
CaptureStackTrace

CaptureStackTrace

Description

Capture up to max_frames stack frames into out. Two shapes:

CaptureStackTrace(out, max, skip) – raw, returns size CaptureStackTrace(out_vec, skip) – vec, returns bool

CaptureStackTrace’s own frame is always skipped on top of whatever skip discards.

Success

Raw form returns the number of frames written into out (0..max). Vec form returns true; *out_vec is grown and populated with the captured frames.

Failure

Raw form returns 0 when no frame can be captured (e.g. unwind library refuses, or max == 0). Vec form returns false on allocator failure while growing *out_vec; the vec is left in whatever partial state the failed growth produced.

Usage example (Cross-references)

Usage examples (Cross-references)
            // frame (1 frame).
            StackFrame frames[32];
            size       n     = CaptureStackTrace(frames, 32, 1);
            Str        trace = StrInit(&h);
            // FormatStackTrace takes `Allocator *` -- legitimate erasure
            if (depth > DEBUG_ALLOCATOR_MAX_TRACE)
                depth = DEBUG_ALLOCATOR_MAX_TRACE;
            rec.alloc_trace_n = (u32)CaptureStackTrace(rec.alloc_trace, depth, 2);
        }
                if (depth > DEBUG_ALLOCATOR_MAX_TRACE)
                    depth = DEBUG_ALLOCATOR_MAX_TRACE;
                entry.free_trace_n = (u32)CaptureStackTrace(entry.free_trace, depth, 3);
            }
            VecPushBack(&self->freed, entry);
    // captured trace.
    static __attribute__((noinline)) size bt_capture_with_helper(StackFrame *frames, size max) {
        return CaptureStackTrace(frames, max, 0);
    }
    // raw helpers above, just routed through the StackFrames overload.
    static __attribute__((noinline)) bool bt_vec_capture_with_helper(StackFrames *out) {
        return CaptureStackTrace(out, 0);
    }
    // function.
    static __attribute__((noinline)) size fp_capture_inner(StackFrame *frames, size max) {
        return CaptureStackTrace(frames, max, 0);
    }
Last updated on