Swift debugging on Linux - Missing Backtrace Swift debugging on Linux - Missing Backtrace linux linux

Swift debugging on Linux - Missing Backtrace


So, sadly this seems to be a bug in lldb, which uses local symbols in ELF executables, and dladdr cannot find them on linux platforms.

See the bug report here:https://bugs.swift.org/browse/SR-755

ATTENTION:

Great news though, there is a bash script which does all the work for you. Instruction's are slight, but this is what you need to do:

Download this script:https://raw.githubusercontent.com/apple/swift/master/utils/symbolicate-linux-fatal

Exectute your crashing app.

$ myApp &> crash.log

Then execute the script.

$ ./symbolicate-linux-fatal myApp crash.log

It has helped me out no end.

Using LLDB

You can also use the swift debugger to help if still in the development stage.

Also if you execute lldb yourexecutable, then run. You will pause and able to fully debug your application.


Enable Core Dumps on Linux, and use gdb to get a back trace.

  1. Enable core dumps by modifying /etc/security/limits.conf

    • In that file, add the following two lines
      • * soft core unlimited
      • * hard core unlimited
  2. Log out of the shell, then log back in (no restart required)

  3. Double check the modification worked
    • $ ulimit -c should return unlimited
  4. Run your program, when it crashes, a file name core will appear in the same directory
  5. debug using $ gdb <executable> <core-file>

Example

$ mkdir testCore && cd testCore

$ touch app.swift

// app.swiftfunc thisWillCrash(){    fatalError("crashing on purpose")}func wrapperFn(){    thisWillCrash()}wrapperFn()

$ swiftc -g app.swift

This will output an executable named app. Run it:

$ ./app

It will crash and now you will have a file named core alongside app.swift and app

Use gdb to get the backtrace:

$ gdb app core

After gdb starts and you get to the gdb prompt, run the bt command. On my Raspberry Pi 3B running Raspbian Buster, running Swift 5.1.5 this is the back trace I get:

(gdb) bt#0  0x76d5c974 in $ss17_assertionFailure__4file4line5flagss5NeverOs12StaticStringV_SSAHSus6UInt32VtFTf4xnnnn_n () from /home/pi/swift/usr/lib/swift/linux/libswiftCore.so#1  0x76aa07b8 in $ss17_assertionFailure__4file4line5flagss5NeverOs12StaticStringV_SSAHSus6UInt32VtF    () from /home/pi/swift/usr/lib/swift/linux/libswiftCore.so#2  0x004f4ab8 in $s3app13thisWillCrashyyF ()#3  0x004f4ad0 in $s3app9wrapperFnyyF ()#4  0x004f49c4 in main ()

When I do this on a real project with multiple files and using spm to build, the back trace includes correct file names and line numbers.