仅当不使用调试器时才出现段错误
我有一个多线程 C 程序,它始终在程序中的特定点生成分段错误。当我用 gdb 运行它时,没有显示任何错误。您能想到只有在不使用调试器时才会出现该错误的任何原因吗?无法用它来查找问题,真是太烦人了!
I have a multithreaded C program, which consistently generates a segmentation fault at a specific point in the program. When I run it with gdb, no fault is shown. Can you think of any reason why the fault might occur only when not using the debugger? It's pretty annoying not being able to use it to find the problem!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
经典的Heisenbug。来自维基百科:
调试器可能会改变时序并隐藏竞争条件。
在 Linux 上,GDB 还禁用地址空间随机化,并且您的崩溃可能特定于地址空间布局。尝试
(gdb)设置禁用随机化关闭
。最后,
ulimit -c unlimited
和事后调试(Robie 已经建议)可能会起作用。Classic Heisenbug. From Wikipedia:
The debugger may be changing timing, and hiding a race condition.
On Linux, GDB also disables address space randomization, and your crash may be specific to address space layout. Try
(gdb) set disable-randomization off
.Finally,
ulimit -c unlimited
and post-mortem debugging (already suggested by Robie) may work.也许当使用 gdb 时,内存被映射到一个位置,您的溢出/下流不会践踏导致崩溃的内存。或者它可能是不再被绊倒的竞争条件。虽然这听起来不直观,但您应该高兴您的程序足够好,足以崩溃。
一些建议
cppcheck
libefence
Perhaps when using
gdb
memory is mapped in a location which your over/under flow doesn't trample on memory that causes a crash. Or it could be a race condition that is no longer getting tripped. Although it sounds unintuitive, you should be happy your program was nice enough to crash on you.Some suggestions
cppcheck
libefence
通过调试它,您正在更改它运行的环境。听起来您正在处理某种竞争条件,并且通过调试它,事情的安排略有不同,因此您不会遇到问题。那,或者事物以稍微不同的方式存储,所以它不会发生。您能否在代码中添加一些调试输出来帮助找出问题?这可能影响较小,并且可以让您找到问题。
By debugging it you are changing the environment that it is running in. It sounds like you are dealing with some sort of race condition, and by debugging it things are scheduled slightly differently so you don't encounter the issue. That, or things are being stored in a slightly different way so it doesn't occur. Are you able to put some debugging output in the code to assist in figuring out the problem? That may have less of an impact and allow you to find your issue.
我以前完全遇到过这个问题!这是一个竞争条件,当我使用调试器单步执行代码时,我所在的线程足够慢,无法触发竞争条件。非常糟糕。
I have totally had this problem before! It was a race condition, and when I was stepping though the code with a debugger the thread i was in was slow enough to not trigger the race condition. Pretty awful.
如果您使用的是
gcc
,请尝试使用-Wall
选项来获取所有警告。如果您使用像 Eclipse 这样的 IDE,它会自动执行此操作。If you're using
gcc
, try using the-Wall
option to get all warnings. If you use an IDE like Eclipse, it would do that automatically.