我有一个多线程 C 程序,它始终在程序的特定点生成段错误。当我用 gdb 运行它时,没有显示任何错误。您能想到为什么只有在不使用调试器时才会出现故障吗?不能用它来发现问题是很烦人的!
最佳答案
经典Heisenbug .来自维基百科:
Time can also be a factor in heisenbugs. Executing a program under control of a debugger can change the execution timing of the program as compared to normal execution. Time-sensitive bugs such as race conditions may not reproduce when the program is slowed down by single-stepping source lines in the debugger. This is particularly true when the behavior involves interaction with an entity not under the control of a debugger, such as when debugging network packet processing between two machines and only one is under debugger control.
调试器可能正在改变时序,并隐藏竞争条件。
在 Linux 上,GDB 还禁用地址空间随机化,您的崩溃可能与地址空间布局有关。尝试 (gdb) set disable-randomization off
。
最后,ulimit -c unlimited
和事后调试(已由 Robie 建议)可能会起作用。
关于c - 仅在不使用调试器时出现段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4628521/