How to print call stack in Swift?

xcode call stack
lldb call stack
backtrace call stack
show call stack lldb
first throw call stack
ios print call stack
recorded stack frame

In Objective-C, you can print the call stack by doing the following:

NSLog(@"%@", [NSThread callStackSymbols]);

How do you do this in Swift without using Foundation class?

As Jacobson says, use the following:

Swift 2:
Swift 3 / Swift 4:

That's Swift code. It's using a Foundation method, but so does 90%+ of what you do on iOS.


Note that the formatting looks better if you use:


From the debugger command line you can type

e Thread.callStackSymbols.forEach{print($0)}

How to print or see method call stack in xcode?, You can print the stack trace in the NSLog by NSLog(@"Stack trace : %@",[​NSThread callStackSymbols]);. EDIT: Swift code println("Stack trace:  For a linux-only solution you can use backtrace(3) that simply returns an array of void * (in fact each of these point to the return address from the corresponding stack frame). To translate these to something of use, there’s backtrace_symbols(3). Pay attention to the notes section in backtrace(3): options. option.

For Swift 3 use:


or for better formatting

for symbol: String in Thread.callStackSymbols {

Day 3 : How to print stack trace on console of XCode?, Lol. po Thread.callStackSymbols. That's one question, see you tomorrow. Swift and Objective C developer. Designing apps for iPhone and Apple TV. Follow. The call stack is (somewhat awkwardly) in a drop down list, not a list view like in visual studio. Once you get the hang of things you’ll find that Xcode does some things better and visual studio does some things better. You’re just more comfortable (currently) in visual studio. I’m also guessing you’re using Swift.

This improves the output a little.

for symbol: String in NSThread.callStackSymbols() {
    NSLog("%@", symbol)

Examining the Call Stack, Examining the Call Stack · Getting Information About the Current Frame · Inspecting Variables · Evaluating Expressions · Printing Modes  Run to a specific function from the Call Stack window In the Call Stack window, select the function, right-click, and then choose Run to Cursor. Set a breakpoint on the exit point of a function call See Set a breakpoint at a call stack function.

Here's a great utility class I found on github:

You get a tuple (class,method) of any stack trace symbol so you can do a clean printout.


callStackSymbols, Discussion. The return value describes the call stack backtrace of the current thread at the moment this method was called. For example, when debugging a Swift program, you can evaluate Swift code as if it were a read-eval-print (REPL) loop in the context of the current stack frame. This is a powerful way to introspect variables at different points during execution.

I needed to write the callstack to a log file so I tweaked it like so.

var ErrorStack = String()
Thread.callStackSymbols.forEach {
    ErrorStack = "\(ErrorStack)\n" + $0

Tracking tasks with stack traces in Swift, The backtrace function comes from the “execinfo.h” header and is implemented as part of libSystem on OS X and iOS (which all Swift programs  boost::stacktrace:: is the namespace that has all the classes and functions to work with stacktraces. stacktrace() is the default constructor call; constructor stores the current function call sequence inside the stacktrace class.

What's with the call stack in Xcode? : swift, In Visual Studio and VSCode it's easy to read and debug C# using the call stack to trace through, but Xcode has a lot of gibberish to sort through. Is it just bad or  In Xcode 6 you can click the button at the bottom left corner of the pane which shows the full stack trace. In Xcode 5 you can move the slider at the bottom of the pane which shows the stack trace. It controls how much of the struck trace is shown.

Stacktraces and deinitializers in Swift, Here is the Deinitialization example from the Swift Book with my call to print the stack trace on line 25. I had to add the import Foundation to  If you want symbolic information in the console there's some sample code from Apple. If you want to generate a stack trace at an arbitrary point in your code (and you're on Leopard), see the backtrace man page. Before Leopard, you actually had to dig through the call stack itself.

Print stack trace for fatalError - Discussion, In Objective-C, you can print the call stack by doing the following: NSLog(@"%@"​, [NSThread callStackSymbols]);. How do you do this in Swift  phptrace is a great tool to print PHP stack anytime when you want without installing any extensions. There are two major function of phptrace: first, print call stack of PHP which need not install anything, second, trace php execution flows which needs to install the extension it supplies.

  • Autocompletion in playground gives me: NSLog(NSThread.callStackSymbols()).
  • Sorry, I should clarify - without using Foundation class.
  • By using Foundation classes. It's not evil. Much of the power of Cocoa/Cocoa touch is built around the Foundation classes. Use them when needed.
  • @DuncanC Agree but want to know a way for a strictly Swift code.
  • There is no way I know of to do this without using Foundation classes. There is no way I know of to write an iOS or Mac OS GUI app without using Foundation Classes. You can call Foundation Class methods using purely Swift code, but you still need to use Foundation Classes.
  • that works great, but I get a messed up layout in the console window, looks like some tab / whitespace issue. Using Xcode 7.2
  • that seems to work, is there a way to get a more readable stack trace, like when you put a breakpoint in Xcode you can see the function calls with their parameters just like in the swift source file, but the 'dump' gives a less readable version with addresses etc
  • @AlexMan, As of Swift 3, yes. However, this post was made before Swift 3 was released.
  • I'm getting this: error: unknown command shorthand suffix: '(Thread.callStackSymbols)' error: Unrecognized command 'print(Thread.callStackSymbols)'.
  • @Kingalione, if you're trying to type it in the debugger, you need to type e print(Thread.callStackSymbols) or e Thread.callStackSymbols.forEach{print($0)}. e is short for expression, the LLDB command that tells the debugger to evaluate what you type as Swift statement.
  • or even more swift 3: Thread.callStackSymbols.forEach { print($0) }
  • Why not use Swift's print in the for loop to display each line rather than NSLog? NSLog is quite "noisy" (every line starts with a timestamp, info about the process and thread, etc.)
  • Unfortunately this doesn't work with Swift 3, and it looks like there is no easy way to "demangle" function names with Swift 3. Although this project might be a possibility (haven't tried it myself):