Swift debugging on Linux - Missing Backtrace

Viewed 1668

I have a crash which I am trying to locate, and even building the application as debug and executing it I don't seem to get any symbolic information.

The output I receive at the console is:

fatal error: unexpectedly found nil while unwrapping an Optional value
Current stack trace:
0    libswiftCore.so                    0x00007f1c51ca51c0 swift_reportError + 120
1    libswiftCore.so                    0x00007f1c51cbfbe0 _swift_stdlib_reportFatalError + 62
2    libswiftCore.so                    0x00007f1c51ab9be6 <unavailable> + 1186790
3    libswiftCore.so                    0x00007f1c51c18ead <unavailable> + 2625197
4    libswiftCore.so                    0x00007f1c51ab9be6 <unavailable> + 1186790
5    libswiftCore.so                    0x00007f1c51bd4060 specialized _fatalErrorMessage(StaticString, StaticString, file : StaticString, line : UInt, flags : UInt32) -> Never + 96
6    myProgram                          0x000000000045909e <unavailable> + 364702
7    myProgram                          0x0000000000423f37 <unavailable> + 147255
8    libdispatch.so                     0x00007f1c5276e177 <unavailable> + 278903
9    libdispatch.so                     0x00007f1c52779edd <unavailable> + 327389
10   libdispatch.so                     0x00007f1c5277a890 <unavailable> + 329872
11   libdispatch.so                     0x00007f1c5277c5f4 <unavailable> + 337396
12   libdispatch.so                     0x00007f1c5279aa18 <unavailable> + 461336
13   libpthread.so.0                    0x00007f1c50c6f6ba <unavailable> + 30394
14   libc.so.6                          0x00007f1c4f7b9370 clone + 109
Illegal instruction (core dumped)

I simply cannot find any way to expand on this information, and although I could go looking through a map file. I feel that we have moved very much beyond that territory now with modern tooling.

Does anyone have any pointers, or indeed knows how to produce a build with more information in it.

Ubuntu 16.04, Swift 3.1.1

2 Answers

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.swift
func 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.

Related