title: Debugging Segmentation Faults on a Mac?
body: <p>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 <a href="http://www.ioccc.org/" rel="noreferrer">IOCCC</a>, which means the following things are true about my program:</p>
  
  <ul>
  <li>It's a very small C program in a single file called prog.c</li>
  <li>I won't post it here, because it won't help (and would probably render the contest entry invalid)</li>
  <li>It compiles cleanly under gcc using "cc -o prog prog.c -Wall"</li>
  <li>Despite (or, more accurately, because of) the fact it contains a bunch of really bizarre uses of C, it has been constructed extremely carefully. I don't know of any part of it which is careless with memory (which is not to say that there can't possibly be bugs, just that if there are they're not likely to be obvious ones)</li>
  <li>I'm primarily a Windows user, but several years ago I successfully compiled and ran it on several windows machines, a couple of Macs and a Linux box, with no problems. The code hasn't changed since then, but I no longer have access to those machines.</li>
  </ul>
  
  <p>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.</p>
  
  <p>Here is what I've tried, to track down the problem, using answers mostly gleaned from <a href="https://stackoverflow.com/questions/530278/c-program-compiled-with-cygwin-in-windows-works-segmentation-fault-under-linux">this useful StackOverflow thread</a>:</p>
  
  <ul>
  <li>On Windows, peppered the code with _ASSERTE(_CrtCheckMemory()); - The code ran dog-slow, but ran successfully. None of the asserts fired (they do when I deliberately add horrible code to ensure that _CrtCheckMemory and _ASSERTE are working as expected, but not otherwise)</li>
  <li>On the Mac, I tried Mudflap. I tried to build the code using variations of "g++ -fmudflap -fstack-protector-all -lmudflap -Wall -o prog prog.c", which just produces the error "cc1plus: error: mf-runtime.h: No such file or directory". Googling the matter didn't bring up anything conclusive, but there does seem to be a feeling that Mudflap just doesn't work on Macs.</li>
  <li>Also on the Mac, I tried Valgrind. I installed and built it, and built my code using "cc -o prog -g -O0 prog.c". Running Valgrind with the command "valgrind --leak-check=yes prog" produces the error "valgrind: prog: command not found". Remembering you have you "open" an exectable on a Mac I tried "valgrind --leak-check=yes open prog", which appears to run the program, and also runs Valgrind, which finds no problems. However, Valgrind is failing to find problems for me even when I run it with programs which are designed specifically to make it trigger error messages. I this also broken on Macs?</li>
  <li>I tried running the program in Xcode, with all the Diagnostics checkboxes ticked in the Product->Edit Scheme... menu, and with a symbolic breakpoint set in malloc_error_break. The breakpoint doesn't get hit, the code stops with a callstack containing one thing ("dlopen"), and the only thing of note that shows up in the output window is the following:</li>
  </ul>
  
  <blockquote>
    <p>Warning: Unable to restore previously selected frame.
    No memory available to program now: unsafe to call malloc</p>
  </blockquote>
  
  <p>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?</p>