How to debug Segmentation Fault


(Maxim Veksler) #1

I had some code work great on Mac and seg fault on Linux, bug demo[1] and
bug report[2]. It took me some time to narrow it down using print()'s,
essentially running a binary search to identify the point at which program
election halts, leading to a seg fault.

My method, though effective is probably barbaric. It's been years since
I've touched code that can actually seg fault, and I'm rusty on how you
approach debugging such cases, I'm wondering both Mac and Linux, even
though my case is Linux only.

I'd appreciate any links and discussions of how to tackle this issue, as it
seems that the days of segmentation faulting code are big time coming back!
So let's get into shape.

Assuming the discussion turns into a valuable resource I'll work to
summarize it into some form of a blog post for the benefit of the community.

[1]
https://github.com/maximveksler/thank-you/tree/master/swift/DateFormatter
[2] https://bugs.swift.org/browse/SR-3828


(Jens Alfke) #2

You run the program in a debugger, probably gdb if this is Linux. Just run "gdb /path/to/binary”. Then at the “(gdb)” prompt enter “run”. (You can type command-line arguments after “run" and they’ll be passed to your program.) If the process segfaults or otherwise crashes, gdb takes over and prints another prompt. Now you can start debugging. The usual first thing I do is enter “bt” which will dump the stack. That itself should produce enough output to submit a bug report. There are lots of docs and tutorials for gdb online if you want to learn more.

gdb is no longer used on Mac (we have lldb instead), so further discussion of it would be off-topic here :slight_smile:

Another possibility, instead of gdb, is getting the OS to produce a crash report when the process terminates. On Mac this is enabled by default, but maybe you need to toggle some setting on Linux to get similar behavior.

—Jens

···

On Feb 1, 2017, at 5:00 PM, Maxim Veksler via swift-users <swift-users@swift.org> wrote:

My method, though effective is probably barbaric. It's been years since I've touched code that can actually seg fault, and I'm rusty on how you approach debugging such cases, I'm wondering both Mac and Linux, even though my case is Linux only.


(Jens Alfke) #3

Oops, never mind that … I didn’t notice this was swift-users and thought it was one of the Apple mailing lists. *facepalm*

—Jens

···

On Feb 1, 2017, at 5:25 PM, I wrote:

gdb is no longer used on Mac (we have lldb instead), so further discussion of it would be off-topic here :slight_smile:


(Jim Ingham) #4

There is a Linux port of lldb, the swift enabled version of which is included in the toolchains that are built for Swift on Linux. gdb will do fine for the C side of the world, but it knows nothing about Swift. So if you need to see the swift side of the world to understand your crash, you'll need to use that lldb. You use it in pretty much the same way as gdb, and there are tutorials for its use at:

http://lldb.llvm.org

Jim

···

On Feb 1, 2017, at 5:26 PM, Jens Alfke via swift-users <swift-users@swift.org> wrote:

On Feb 1, 2017, at 5:25 PM, I wrote:

gdb is no longer used on Mac (we have lldb instead), so further discussion of it would be off-topic here :slight_smile:

Oops, never mind that … I didn’t notice this was swift-users and thought it was one of the Apple mailing lists. *facepalm*

—Jens
_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users