|
| 1 | +Backtracing support in Swift |
| 2 | +============================ |
| 3 | + |
| 4 | +When things go wrong, it's always useful to be able to get a backtrace showing |
| 5 | +where the problem occurred in your program. |
| 6 | + |
| 7 | +Broadly speaking there are three circumstances where you might want a backtrace, |
| 8 | +namely: |
| 9 | + |
| 10 | + * Program crashes |
| 11 | + * Runtime errors |
| 12 | + * Specific user-defined program events |
| 13 | + |
| 14 | +Historically, Swift has tended to lean on operating system crash catching |
| 15 | +support for the first two of these, and hasn't really provided any built-in |
| 16 | +support for the latter. This is fine for Darwin, where the operating system |
| 17 | +provides a comprehensive system-wide crash catching facility; it's just about OK |
| 18 | +on Windows, which also has system-wide crash logging; but it isn't great |
| 19 | +elsewhere, in particular on Linux where a lot of server-side Swift programs |
| 20 | +currently rely on a separate package to provide them with some level of |
| 21 | +backtrace support when errors happen. |
| 22 | + |
| 23 | +What does Swift now support? |
| 24 | +---------------------------- |
| 25 | + |
| 26 | +Swift now supports: |
| 27 | + |
| 28 | + * Automatic crash catching and backtrace generation out of the box. |
| 29 | + * Built-in symbolication. |
| 30 | + * A choice of unwind algorithms, including "fast", DWARF and SEH. |
| 31 | + * Interactive(!) crash/runtime error catching. |
| 32 | + |
| 33 | +Crash catching is enabled by default, and won't interfere with any system-wide |
| 34 | +crash reporters you might be using. |
| 35 | + |
| 36 | +How do I configure backtracing? |
| 37 | +------------------------------- |
| 38 | + |
| 39 | +There is an environment variable, ``SWIFT_BACKTRACE``, that can be used to |
| 40 | +configure Swift's crash catching and backtracing support. The variable should |
| 41 | +contain a ``,``-separated list of ``key=value`` pairs. Supported keys are as |
| 42 | +follows: |
| 43 | + |
| 44 | ++-----------------+---------+--------------------------------------------------+ |
| 45 | +| Key | Default | Meaning | |
| 46 | ++=================+=========+==================================================+ |
| 47 | +| enable | yes* | Set to ``no`` to disable crash catching, or | |
| 48 | +| | | ``tty`` to enable only if stdin is a terminal. | |
| 49 | ++-----------------+---------+--------------------------------------------------+ |
| 50 | +| demangle | yes | Set to ``no`` to disable demangling. | |
| 51 | ++-----------------+---------+--------------------------------------------------+ |
| 52 | +| interactive | tty | Set to ``no`` to disable interaction, or ``yes`` | |
| 53 | +| | | to enable always. | |
| 54 | ++-----------------+---------+--------------------------------------------------+ |
| 55 | +| color | tty | Set to ``yes`` to enable always, or ``no`` to | |
| 56 | +| | | disable. Uses ANSI escape sequences. | |
| 57 | ++-----------------+---------+--------------------------------------------------+ |
| 58 | +| timeout | 30s | Time to wait for interaction when a crash | |
| 59 | +| | | occurs. Setting this to ``none`` or ``0s`` will | |
| 60 | +| | | disable interaction. | |
| 61 | ++-----------------+---------+--------------------------------------------------+ |
| 62 | +| unwind | auto | Specifies which unwind algorithm to use. | |
| 63 | +| | | ``auto`` means to choose appropriately for the | |
| 64 | +| | | platform. Other options are ``fast``, which | |
| 65 | +| | | does a naïve stack walk; and ``precise``, which | |
| 66 | +| | | uses exception handling data to perform an | |
| 67 | +| | | unwind. | |
| 68 | ++-----------------+---------+--------------------------------------------------+ |
| 69 | +| preset | auto | Specifies which set of preset formatting options | |
| 70 | +| | | to use. Options are ``friendly``, ``medium`` or | |
| 71 | +| | | ``full``. ``auto`` means to use ``friendly`` if | |
| 72 | +| | | interactive, and ``full`` otherwise. | |
| 73 | ++-----------------+---------+--------------------------------------------------+ |
| 74 | +| sanitize | preset | If ``yes``, we will try to process paths to | |
| 75 | +| | | remove PII. Exact behaviour is platform | |
| 76 | +| | | dependent. | |
| 77 | ++-----------------+---------+--------------------------------------------------+ |
| 78 | +| threads | preset | Options are ``all`` to show backtraces for every | |
| 79 | +| | | thread, or ``crashed`` to show only the crashing | |
| 80 | +| | | thread. | |
| 81 | ++-----------------+---------+--------------------------------------------------+ |
| 82 | +| registers | preset | Options are ``none``, ``all`` or ``crashed``. | |
| 83 | ++-----------------+---------+--------------------------------------------------+ |
| 84 | +| images | preset | Options are ``none``, ``all``, or ``mentioned``, | |
| 85 | +| | | which only displays images mentioned in a | |
| 86 | +| | | backtrace. | |
| 87 | ++-----------------+---------+--------------------------------------------------+ |
| 88 | +| limit | 64 | Limits the length of the captured backtrace. See | |
| 89 | +| | | below for a discussion of its behaviour. Can be | |
| 90 | +| | | set to ``none`` to mean no limit. | |
| 91 | ++-----------------+---------+--------------------------------------------------+ |
| 92 | +| top | 16 | Specify a minimum number of frames to capture | |
| 93 | +| | | from the top of the stack. See below for more. | |
| 94 | ++-----------------+---------+--------------------------------------------------+ |
| 95 | +| cache | yes | Set to ``no`` to disable symbol caching. This | |
| 96 | +| | | only has effect on platforms that have a symbol | |
| 97 | +| | | cache that can be controlled by the runtime. | |
| 98 | ++-----------------+---------+--------------------------------------------------+ |
| 99 | +| swift-backtrace | | If specified, gives the full path to the | |
| 100 | +| | | swift-backtrace binary to use for crashes. | |
| 101 | +| | | Otherwise, Swift will locate the binary relative | |
| 102 | +| | | to the runtime library, or using ``SWIFT_ROOT``. | |
| 103 | ++-----------------+---------+--------------------------------------------------+ |
| 104 | + |
| 105 | +(*) On macOS, this defaults to ``tty`` rather than ``yes``. |
| 106 | + |
| 107 | +Backtrace limits |
| 108 | +---------------- |
| 109 | + |
| 110 | +The limit settings are provided both to prevent runaway backtraces and to allow |
| 111 | +for a sensible backtrace to be produced even when a function has blown the stack |
| 112 | +through excessive recursion. |
| 113 | + |
| 114 | +Typically in the latter case you want to capture some frames at the top of the |
| 115 | +stack so that you can see how the recursion was entered, and the frames at the |
| 116 | +bottom of the stack where the actual fault occurred. |
| 117 | + |
| 118 | +1. There are ``limit`` or fewer frames. In this case we will display all |
| 119 | + the frames in the backtrace. Note that this _includes_ the case where there |
| 120 | + are exactly ``limit`` frames. |
| 121 | + |
| 122 | +2. There are more than ``limit`` frames. |
| 123 | + |
| 124 | + a. ``top`` is ``0``. We will display the first ``limit - 1`` frames followed |
| 125 | + by ``...`` to indicate that more frames exist. |
| 126 | + |
| 127 | + b. ``top`` is less than ``limit - 1``. We will display ``limit - 1 - top`` |
| 128 | + frames from the bottom of the stack, then a ``...``, then ``top`` frames |
| 129 | + from the top of the stack. |
| 130 | + |
| 131 | + c. ``top`` is greater or equal to ``limit - 1``. We will display ``...``, |
| 132 | + followed by ``limit - 1`` frames from the top of the stack. |
| 133 | + |
| 134 | +For example, let's say we have a stack containing 10 frames numbered here 1 to |
| 135 | +10, with 10 being the innermost frame. With ``limit`` set to 5, you would see:: |
| 136 | + |
| 137 | + 10 |
| 138 | + 9 |
| 139 | + 8 |
| 140 | + 7 |
| 141 | + ... |
| 142 | + |
| 143 | +With ``limit`` set to 5 and ``top`` to 2, you would instead see:: |
| 144 | + |
| 145 | + 10 |
| 146 | + 9 |
| 147 | + ... |
| 148 | + 2 |
| 149 | + 1 |
| 150 | + |
| 151 | +And with ``limit`` set to 5 and ``top`` to 4 or above, you would see:: |
| 152 | + |
| 153 | + ... |
| 154 | + 4 |
| 155 | + 3 |
| 156 | + 2 |
| 157 | + 1 |
| 158 | + |
| 159 | +What is the swift-backtrace binary? |
| 160 | +----------------------------------- |
| 161 | + |
| 162 | +``swift-backtrace`` is a program that gets invoked when your program crashes. |
| 163 | +We do this because when a program crashes, it is potentially in an invalid state |
| 164 | +and there is very little that is safe for us to do. By executing an external |
| 165 | +helper program, we ensure that we do not interfere with the way the program was |
| 166 | +going to crash (so that system-wide crash catchers will still generate the |
| 167 | +correct information), and we are also able to use any functionality we need to |
| 168 | +generate a decent backtrace, including symbolication (which might in general |
| 169 | +require memory allocation, fetching and reading remote files and so on). |
| 170 | + |
| 171 | +You shouldn't try to run ``swift-backtrace`` yourself; it has unusual |
| 172 | +requirements, which vary from platform to platform. Instead, it will be |
| 173 | +triggered automatically by the runtime. |
| 174 | + |
| 175 | +System specifics |
| 176 | +---------------- |
| 177 | + |
| 178 | +macOS |
| 179 | +^^^^^ |
| 180 | + |
| 181 | +On macOS, we catch crashes and other events using a signal handler. At time of |
| 182 | +writing, this is installed for the following signals: |
| 183 | + |
| 184 | ++--------------+--------------------------+-------------------------------------+ |
| 185 | +| Signal | Description | Comment | |
| 186 | ++====+=========+==========================+=====================================+ |
| 187 | +| 3 | SIGQUIT | Quit program | | |
| 188 | ++----+---------+--------------------------+-------------------------------------+ |
| 189 | +| 4 | SIGILL | Illegal instruction | | |
| 190 | ++----+---------+--------------------------+-------------------------------------+ |
| 191 | +| 5 | SIGTRAP | Trace trap | | |
| 192 | ++----+---------+--------------------------+-------------------------------------+ |
| 193 | +| 6 | SIGABRT | Abort program | | |
| 194 | ++----+---------+--------------------------+-------------------------------------+ |
| 195 | +| 8 | SIGFPE | Floating point exception | On Intel, integer divide by zero | |
| 196 | +| | | | also triggers this. | |
| 197 | ++----+---------+--------------------------+-------------------------------------+ |
| 198 | +| 10 | SIGBUS | Bus error | | |
| 199 | ++----+---------+--------------------------+-------------------------------------+ |
| 200 | +| 11 | SIGSEGV | Segmentation violation | | |
| 201 | ++----+---------+--------------------------+-------------------------------------+ |
| 202 | + |
| 203 | +If crash catching is enabled, the signal handler will be installed for any |
| 204 | +process that links the Swift runtime. If you replace the handlers for any of |
| 205 | +these signals, your program will no longer produce backtraces for program |
| 206 | +failures that lead to the handler you have replaced. |
| 207 | + |
| 208 | +Additionally, the runtime will configure an alternate signal handling stack, so |
| 209 | +that stack overflows can be successfully trapped. |
| 210 | + |
| 211 | +Note that the runtime will not install its signal handlers for a signal if it |
| 212 | +finds that there is already a handler for that signal. Similarly if something |
| 213 | +else has already configured an alternate signal stack, it will leave that |
| 214 | +stack alone. |
| 215 | + |
| 216 | +Once the backtracer has finished handling the crash, it will allow the crashing |
| 217 | +program to continue and crash normally, which will result in the usual Crash |
| 218 | +Reporter log file being generated. |
| 219 | + |
| 220 | +Crash catching *cannot* be enabled for setuid binaries. This is intentional as |
| 221 | +doing so might create a security hole. |
| 222 | + |
| 223 | +Other Darwin (iOS, tvOS) |
| 224 | +^^^^^^^^^^^^^^^^^^^^^^^^ |
| 225 | + |
| 226 | +Crash catching is not enabled for non-macOS Darwin. You should continue to look |
| 227 | +at the system-provided crash logs. |
0 commit comments