electrodruid
electrodruid

Reputation: 1103

Debugging Segmentation Faults on a Mac?

I'm having some problems with a program causing a segmentation fault when run on a Mac. I'm putting together an entry for the IOCCC, which means the following things are true about my program:

I don't have a Linux machine to re-test on, but as one final test, I tried compiling and running it on a MacBook Pro - Mac OSX 10.6.7, Xcode 4.2 (i.e. GCC 4.2.1). Again, it compiles cleanly from the command line. It seems that on a Mac typing "prog" won't make the compiled program run, but "open prog" seems to. Nothing happens for about 10 seconds (my program takes about a minute to run when it's successful), but then it just says "Segmentation fault", and ends.

Here is what I've tried, to track down the problem, using answers mostly gleaned from this useful StackOverflow thread:

Warning: Unable to restore previously selected frame. No memory available to program now: unsafe to call malloc

I'm out of ideas. I'm trying to get Cygwin set up (it's taking hours though) to see if any of the tools will work that way, but if that fails then I'm at a loss. Surely there must be SOME tools which are capable of tracking down the causes of Segmentation faults on a Mac?

Upvotes: 11

Views: 27856

Answers (3)

nybon
nybon

Reputation: 9601

In many cases, macOS stores the recent program crash logs under ~/Library/Logs/DiagnosticReports/ folder.

Usually I will try the following steps when doing troubleshooting on macOS:

  1. Clean the existing crash logs under the ~/Library/Logs/DiagnosticReports/
  2. Run the program again to reproduce the issue
  3. Wait for a few seconds, the crash log will appear under the folder. The crash log is named like {your_program}_{crashing_date}_{id}_{your_host}.crash
  4. Open the crash log with your text editor, search for the keyword Crashed to locate the thread causing the crash. It will show you the stack trace during crash, and in many cases, the exact line of source code causing the crash will be recorded as well.

Some links:

[1] https://mac-optimization.bestreviews.net/analyze-mac-crash-reports/

Upvotes: 6

Roy Shilkrot
Roy Shilkrot

Reputation: 3548

For the more modern lldb flavor

$ lldb --file /path/to/program
...
(lldb) r
Process 89510 launched
...
(lldb) bt
* thread #1, queue = 'com.apple.main-thread', stop reason = EXC_BAD_ACCESS (code=1, address=0x726f00)
  * frame #0: 0x00007fff73856e52 libsystem_platform.dylib`_platform_strlen + 18
...

Upvotes: 13

Brendan Shanks
Brendan Shanks

Reputation: 3236

Have you compiled with -g and run it inside gdb? Once the app crashes, you can get a backtrace with bt that should show you where the crash occurs

Upvotes: 11

Related Questions