Skip to content

Swift Backtrace API - Swift 6.2Β #162

Description

@maatheusgois-dd

Image

Accepted Proposal Summaries
SE-0419: Swift Backtrace API
Links: πŸ“ Proposal | πŸ’¬ Review | βœ… Acceptance

On a first look, this proposal reads like one could build a crash reporting tool based on a new SymbolicatedBacktrace struct it introduces. But then it states this:

(...) the API presented here is not async-signal-safe, and it is not an appropriate tool with which to build a general purpose crash reporter. The intended use case for this functionality is the programmatic capture of backtraces during normal execution.

While backtracing and symbolication sound complex, usage can be as simple as:

import Runtime

enum SomeNamespace {
static func doSomething() {
do {
try somethingThatCanFail()
} catch {
let bt = Backtrace.capture().symbolicated()
if let sl = bt.frames[0].symbolInfo?.sourceLocation {
print("Called from file (sl.path) at line (sl.line)")
}
}
}
}
Note that you need to import Runtime to use the new backtracing capabilities. As you can see in the code above, it's easy to access the file and line of the current execution context. Think of frames like the left sidebar showing the whole call stack when you run into a breakpoint or your app crashes during debugging.

With Swift Backtrace APIs, things like debugging and performance analysis can be hugely improved because they make it much easier to diagnose our code. In complex applications, capturing a backtrace at the point of error or exception can even help us trace issues without halting the application. I can also imagine this to be super helpful for debugging & error handling for Swift on Server.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    Status
    Backlog

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions