5 ms·
On-Crash Backtraces in Swift
- yaglo 3y agoThe best part of is that you can get all this goodness even without having Swift code in your codebase. Just link your C++ (or whatever) project with The Swift runtime (swiftrt.o and libswiftCore.so), and there you have it without writing any Swift.
- krackers 3y agoIf you're on mac (where I assume the two are already available), what compiler flags need to be passed in to do this?
- torarnv 3y ago$ clang++ foo.cpp -o foo -framework Foundation -lswift_Backtracing also works, but dunno if that's "public API" $ codesign --force --sign - --entitlements entitlements.plist foo https://github.com/apple/swift/blob/main/docs/Backtracing.rst#macos https://github.com/apple/swift/blob/main/docs/Backtracing.rs... $ SWIFT_BACKTRACE="enable=yes" ./foo Very cool!
- eatonphil 3y agoLooks like <stacktrace> is already part of C++23 [0]. But if you must use an older C++, why link the entire Swift runtime rather than something GCC's builtin backtraces [1] or libbacktrace [2]? [0] https://www.sandordargo.com/blog/2022/09/21/cpp23-stacktrace-library https://www.sandordargo.com/blog/2022/09/21/cpp23-stacktrace... [1] https://www.gnu.org/software/libc/manual/html_node/Backtraces.html https://www.gnu.org/software/libc/manual/html_node/Backtrace... [2] https://github.com/ianlancetaylor/libbacktrace https://github.com/ianlancetaylor/libbacktrace
- favorited 3y agoIf you're on the Mac, your process is probably linking the Swift runtime anyway. Foundation.framework is being reimplemented in Swift, so if your process links Foundation (or even just CF), you've got the Swift runtime.
- anarazel 3y agoI don't think <stacktrace> is async signal safe, and thus shouldn't be used for in-process crash reporting.
- yaglo 3y agoIt’s a nicer experience during development out of the box, without having to do anything manually. Swift Backtracing is not only about the symbols, it’s also interactive and lets you attach the debugger after the process crashes, which is neat.
- saagarjha 3y ago> The backtracer also skips over system frames and Swift thunks by default. Boo! Show me my system frames! (Also, is this available in the Xcode toolchain yet?)
- deleted 3y ago[deleted]
- js2 3y agoPart of Swift 5.9 which is in Xcode 15, so, I think so, but it's opt-in: > This feature is also available on macOS but is disabled by default. To enable it, set SWIFT_BACKTRACE=enable=yes and sign your program with the com.apple.security.get-task-allow entitlement. https://www.swift.org/blog/swift-5.9-released/ https://www.swift.org/blog/swift-5.9-released/
- keyle 3y agoHah that's funny, I recently ported the Swift compiler to a Debian package using the Ubuntu package (aside: why is Debian not supported, that baffles me, especially for Swift on Linux server? But Ubuntu is?). I thought I did something wrong when the crash reported looked so different. I was unaware, that since the last time I did this (ubuntu->debian .deb), the compiler had changed its error reporting. Props to the Swift team doing the good work, and please add a .deb to the binaries artefacts!
- driggs 3y agoGreat that it lets you attach the debugger if it's running as interactive process. Does that get you a full Swift REPL like Python's debugger does?
- yaglo 3y agoYes, you can evaluate Swift code in LLDB. See https://stackoverflow.com/a/71882942 https://stackoverflow.com/a/71882942
- andrewjl 3y agoHere are some links to related discussions from the Swift forums https://forums.swift.org/t/backtraces-in-swift/61736 https://forums.swift.org/t/backtraces-in-swift/61736 https://forums.swift.org/t/pitch-swift-backtracing-api/62741 https://forums.swift.org/t/pitch-swift-backtracing-api/62741 I found the backtracing API design discussions in the second link especially illuminating
- rwmj 3y agoI'm not so keen on programs trying to be clever like this. How about making the system debugger better, which will benefit all programs? Actually that's not even necessary as abrt / coredumpctl on Linux are pretty good these days, even being able to automatically file a bug report with your distro.
- proto_lambda 3y ago> On Apple platforms or on Windows, you could look at the crash logs captured by the operating system’s built-in crash reporter, but on Linux that’s typically all you had to go on. On modern linux distros, simply typing `coredumpctl debug` puts you right into gdb with the core dump loaded. If debuginfod is set up (not sure what the defaults look like there across distros), it will even automatically download debug symbols for all libraries loaded at the time of the crash.
- ChrisMarshallNY 3y agoThis can be useful. I'll be interested in seeing how this gets integrated into the Xcode IDE, as I rarely find the need to go into the CLI, to debug, these days (although I had to, just a couple of days ago, to find a weird Apple crash, but I could work around it).
- yaglo 3y agoWithin Xcode, you don’t really need it because you’re already running with a debugger when you launch your program.
- ChrisMarshallNY 3y agoYeah, but when the problems go into the API or the Swift Library, things get real murky. The dumps from Xcode can be damn near useless.
- extraduder_ire 3y agoLooks cool. Glad they thought enough to only make 30s timeout happen if you're attached to a terminal. (unless they tried it the other way first, and their whole CI system locked up.)