Debug segmentation errors on Mac?

I am having problems with a program that causes a segmentation error on startup on Mac. I am compiling an entry for IOCCC , which means that the following is true in my program:

  • This is a very small C program in a single file called prog.c
  • I will not post it here because it will not help (and will probably invalidate the entry)
  • It compiles easily under gcc using "cc -o prog prog.c -Wall"
  • Despite (or rather, because of) the fact that it contains a bunch of really bizarre uses of C, it has been built extremely carefully. I do not know any part of it that is careless with memory (which does not mean that there can be no errors, simply if they are not obvious)
  • I am primarily a Windows user, but several years ago I successfully compiled and ran it on several Windows machines, on several Mac and Linux computers, without any problems. Since then, the code has not changed, but I no longer have access to these machines.

I don’t have a Linux machine to recheck, but as one of the last tests I tried to build and run it on a MacBook Pro - Mac OSX 10.6.7, Xcode 4.2 (i.e. GCC 4.2.1). Again, it compiles from the command line. It seems that on the Mac keyboard “prog” will not run the compiled program, but “open prog” seems. Nothing happens for about 10 seconds (my program takes about a minute to work when it was successful), but then it just says “Segmentation Error” and ends.

Here is what I tried to track down the problem using answers mainly gleaned from https://stackoverflow.com/a/3/3/16/16 :

  • On Windows code overlay with _ASSERTE (_CrtCheckMemory ()); - The code worked slowly, but worked successfully. None of the statements worked (they do when I intentionally add horrible code to ensure that _CrtCheckMemory and _ASSERTE work properly, but not otherwise).
  • On Mac, I tried Mudflap. I tried to create the code using the options "g ++ -fmudflap -fstack-protector-all -lmudflap -Wall -o prog prog.c", which simply creates the error "cc1plus: error: mf-runtime.h: There is no such file or directory" . The population of this question did not bring anything convincing, but it seems that Mudflap just does not work on a Mac.
  • Also on 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" causes the error "valgrind: prog: command not found". Remembering that you opened "open" on Mac, I tried "valgrind --leak-check = yes open prog", which seems to run the program, and also runs Valgrind, which does not detect any problems. However, Valgrind does not find a problem for me, even when I run it with programs that are designed specifically to make error messages run. Am I broken on a Mac too?
  • I tried to run the program in Xcode, with all Diagnostics checkboxes checked in the Product-> Edit Scheme ... menu and with a symbolic breakpoint set to malloc_error_break. The breakpoint does not hit, the code is stopped using a call containing one thing ("dlopen"), and the only thing that appears in the output window is the following:

Warning: it is not possible to restore a previously selected frame. Now there is no memory for programming: it is not safe to call malloc

I have no ideas. I am trying to configure Cygwin (this takes several hours) to see if any of these tools work this way, but if that fails, I will be at a loss. Of course, there must be SOME tools that can track the causes of segmentation failures on a Mac?

+4
source share
1 answer

Have you compiled with -g and run it inside gdb ? After the application crashes, you can get backtrace with bt , which should show you where the crash happened

+7
source

Source: https://habr.com/ru/post/1384497/


All Articles