macos 在 Mac 上调试分段错误?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8370468/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 08:40:00  来源:igfitidea点击:

Debugging Segmentation Faults on a Mac?

cmacosdebugginggccsegmentation-fault

提问by electrodruid

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:

我在 Mac 上运行时遇到导致分段错误的程序的一些问题。我正在为IOCCC整理一个条目,这意味着我的计划有以下几点是正确的:

  • It's a very small C program in a single file called prog.c
  • I won't post it here, because it won't help (and would probably render the contest entry invalid)
  • It compiles cleanly under gcc using "cc -o prog prog.c -Wall"
  • 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)
  • 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.
  • 它是一个名为 prog.c 的单个文件中的一个非常小的 C 程序
  • 我不会在这里发布它,因为它无济于事(并且可能会使参赛作品无效)
  • 它使用“cc -o prog prog.c -Wall”在gcc下干净地编译
  • 尽管(或者,更准确地说,因为)它包含了一堆非常奇怪的 C 用法,它的构造非常谨慎。我不知道它的任何部分对内存粗心(这并不是说不可能有错误,只是如果有它们就不太可能是明显的错误)
  • 我主要是 Windows 用户,但几年前我成功地编译并在几台 Windows 机器、几台 Mac 和一个 Linux 机器上运行它,没有任何问题。从那以后代码没有改变,但我不再可以访问这些机器。

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.

我没有要重新测试的 Linux 机器,但作为最后的测试,我尝试在 MacBook Pro - Mac OSX 10.6.7、Xcode 4.2(即 GCC 4.2.1)上编译和运行它。同样,它从命令行干净地编译。似乎在 Mac 上输入“prog”不会使编译的程序运行,但“open prog”似乎可以。大约 10 秒钟没有任何反应(我的程序在成功时需要大约一分钟才能运行),但随后它只是说“分段错误”,然后结束。

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

这是我尝试过的方法,使用主要从这个有用的 StackOverflow 线程中收集的答案来追踪问题:

  • 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)
  • 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.
  • 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?
  • 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:
  • 在 Windows 上,用 _ASSERTE(_CrtCheckMemory()) 填充代码;- 代码运行缓慢,但运行成功。没有触发任何断言(当我故意添加可怕的代码以确保 _CrtCheckMemory 和 _ASSERTE 按预期工作时,它们会触发,但不是其他情况)
  • 在 Mac 上,我尝试了 Mudflap。我尝试使用“g++ -fmudflap -fstack-protector-all -lmudflap -Wall -o prog prog.c”的变体来构建代码,它只会产生错误“cc1plus: error: mf-runtime.h: No such file或目录”。谷歌搜索这件事并没有得出任何结论,但似乎确实有人觉得 Mudflap 在 Mac 上不起作用。
  • 同样在 Mac 上,我尝试了 Valgrind。我安装并构建了它,并使用“cc -o prog -g -O0 prog.c”构建了我的代码。使用命令“valgrind --leak-check=yes prog”运行 Valgrind 会产生错误“valgrind: prog: command not found”。记住你让你在 Mac 上“打开”了一个可执行文件,我试过“valgrind --leak-check=yes open prog”,它似乎运行了程序,也运行了 Valgrind,它没有发现任何问题。但是,即使我使用专为触发错误消息而设计的程序运行它,Valgrind 也未能为我找到问题。我这在 Mac 上也坏了?
  • 我尝试在 Xcode 中运行该程序,在 Product->Edit Scheme... 菜单中勾选所有诊断复选框,并在 malloc_error_break 中设置一个符号断点。断点没有被击中,代码停止,调用堆栈包含一件事(“dlopen”),输出窗口中显示的唯一注意事项如下:

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

警告:无法恢复以前选择的帧。现在没有可用于编程的内存:调用 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?

我没主意了。我正在尝试设置 Cygwin(虽然需要几个小时),以查看是否有任何工具可以那样工作,但如果失败了,那么我就不知所措了。肯定有一些工具能够在 Mac 上追踪分段错误的原因?

回答by Brendan Shanks

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

你有没有-g在里面编译并运行它gdb?应用程序崩溃后,您可以获得回溯,bt该回溯应显示崩溃发生的位置