Skip to main content

Mojo struct

StackTrace

struct StackTrace

Holds a stack trace captured at a specific location.

A StackTrace instance always contains a valid stack trace. Use the collect_if_enabled() static method to conditionally capture a stack trace, which returns None if stack trace collection is disabled or unavailable.

Implemented traits

AnyType, Copyable, ImplicitlyDestructible, Movable, Stringable

comptime members

__copyinit__is_trivial

comptime __copyinit__is_trivial = False

__del__is_trivial

comptime __del__is_trivial = False

__moveinit__is_trivial

comptime __moveinit__is_trivial = False

Methods

__init__

__init__(out self, *, unsafe_from_raw_pointer: UnsafePointer[UInt8, origin_of()])

Construct a StackTrace from a raw pointer to a C string.

Safety: The pointer must be valid and point to a null-terminated string. The caller transfers ownership to this StackTrace.

Args:

  • unsafe_from_raw_pointer (UnsafePointer): A pointer to a null-terminated C string containing the stack trace. The StackTrace takes ownership.

__copyinit__

__copyinit__(out self, existing: Self)

Copy constructor - copies the stack trace string.

Args:

  • existing (Self): The existing StackTrace to copy from.

__moveinit__

__moveinit__(out self, deinit existing: Self)

Move constructor.

Args:

  • existing (Self): The existing StackTrace to move from.

collect_if_enabled

static collect_if_enabled(depth: Int = 0) -> Optional[StackTrace]

Collect a stack trace if enabled by environment variable.

This method checks the MOJO_ENABLE_STACK_TRACE_ON_ERROR environment variable and collects a stack trace only if it is enabled. Returns None if stack traces are disabled, on GPU, or if collection fails.

Args:

  • depth (Int): The maximum depth of the stack trace to collect. When depth is zero, the entire stack trace is collected. When depth is negative, no stack trace is collected.

Returns:

Optional: An Optional[StackTrace] containing the stack trace if collection succeeded, or None if disabled or unavailable.

__str__

__str__(self) -> String

Converts the StackTrace to string representation.

Returns:

String: A String of the stack trace.

Was this page helpful?