Segfault只有优化 [英] Segfault only with optimization

查看:78
本文介绍了Segfault只有优化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个C程序,GCC在没有警告的情况下进行编译,并且只运行
罚款。但是,当我使用任何-O选项(-O0,-O1,

等)编译它时,它会在执行时出现段错误(仍然没有编译器警告或错误)。


我对编译器otimization一无所知,除了它让我的程序更好地运行
。任何关于如何调试这个或者寻找

的建议将不胜感激。


谢谢。

I have a C program that GCC compiles without warnings and it runs just
fine. However, when I compile it using any of the -O options (-O0, -O1,
etc.) it segfaults when executed (still no compiler warnings or errors).

I know nothing about compiler otimization, other than it makes my program
run faster. Any suggestions on how to debug this, or what to look for
would be greatly appreciated.

Thank you.

推荐答案

3月17日上午10:04 *,Jeff Taylor< dev.n ... @ spam.netwrote:
On Mar 17, 10:04*am, Jeff Taylor <dev.n...@spam.netwrote:

我有一个C程序,GCC在没有警告的情况下进行编译,并且只运行
罚款。 *但是,当我使用任何-O选项(-O0,-O1,

等)编译它时,它会在执行时出现段错误(仍然没有编译器警告或错误)。 *


我对编译器otimization一无所知,除了它使我的程序

运行得更快。 *任何有关如何调试此建议或寻找

的建议将不胜感激。
I have a C program that GCC compiles without warnings and it runs just
fine. *However, when I compile it using any of the -O options (-O0, -O1,
etc.) it segfaults when executed (still no compiler warnings or errors). *

I know nothing about compiler otimization, other than it makes my program
run faster. *Any suggestions on how to debug this, or what to look for
would be greatly appreciated.



您使用的编译开关是什么?

将警告调到最高级别。

将代码输入夹板:
http://www.splint.org/


Jeff Taylor写道:
Jeff Taylor wrote:

我有一个C程序,GCC编译没有警告,它只运行

罚款。但是,当我使用任何-O选项(-O0,-O1,

等)编译它时,它会在执行时出现段错误(仍然没有编译器警告或错误)。
I have a C program that GCC compiles without warnings and it runs just
fine. However, when I compile it using any of the -O options (-O0, -O1,
etc.) it segfaults when executed (still no compiler warnings or errors).



这个问题有几种方法


1)我通常采用的方法:

1.a包括编译时调试信息(通常使用-g)

1.b不要删除可执行文件

1.c启动gdb调试器并在调试器中运行程序。 br />
1.d当它崩溃时,请注意它崩溃的地方。具体的

gdb命令是回溯命令。据我记得。

1.e看看局部变量。请注意,gdb显示的值可能是错误的,因为优化和调试

不能很好地混合。

1.f重新编译模块所在的位置崩溃发生没有

任何优化

1.g重新链接

1.h重新运行。程序崩溃了吗?如果不是,您已找到故障所在的

模块。转到步骤1.j

如果是,则故障仍然存在。从backtrace中的模块中获取另一个模块

并重新编译。转到步骤1.g.

1.i如果您已经重新编译了回溯中的所有模块并且

崩溃仍然存在,请逐个重新编译其他模块/>
模块,直到崩溃消失。你重新编译的最后一个模块是带有故障的模块。

1.j隔离模块中的故障。这可能很棘手

,除非有办法告诉编译器在逐个函数的基础上启用/禁用
优化。


2)该组中的常客推荐的方法:

阅读源代码。如果你阅读得足够多,那么这个bug对你来说是显而易见的。

There are several approaches to this problem

1) The approach I usually take:
1.a Include the debug information when compiling (usually with -g)
1.b Do not strip the executable
1.c Start the gdb debugger and run your program within the debugger.
1.d When it crashes, take note of where it crashes. The specific
gdb command is "backtrace" as far as I remember.
1.e Look at the local variables. Note that the values displayed
by gdb are probably wrong since optimizations and debugging
do not mix well.
1.f Recompile the module where the crash happens WITHOUT
any optimizations
1.g Relink
1.h Rerun. Does the program crash? If no, you have found the
module where the fault is. Go to step 1.j
If yes, the fault is still there. Take another module from
the modules in the backtrace and recompile. Go to step 1.g.
1.i If you have recompiled all the modules in the backtrace and
the crash still persists, recompile one by one all other
modules until the crash disappears. The last module that
you recompiled is the module with the fault.
1.j Isolate the fault within the module. This can be tricky
unless there is a way to tell the compiler to enable/disable
optimizations in a function by function basis.

2) The approach recommended by the regulars in this group:
Read the source code. If you read hard enough the bug will be
obvious to you.


我对编译器otimization一无所知,除了它使我的程序

运行得更快。任何关于如何调试这个或者寻找

的建议将不胜感激。
I know nothing about compiler otimization, other than it makes my program
run faster. Any suggestions on how to debug this, or what to look for
would be greatly appreciated.



没有免费的午餐。对于编程错误而言,优化的程序比没有优化的程序更明智。此外,你自己暴露了优化器的bug,这很多。


如果你做直C,那么

优化器中有一个错误...

There is no free lunch. Optimized programs are more sensible to
programming errors than no optimized ones. Besides, you expose
yourself to the bugs of the optimizer, that are many.

If you are doing straight C though, it is highly unlikely that
there is a bug in the optimizer...


谢谢。
Thank you.



-

jacob navia

jacob at jacob point remcomp point fr

logiciels / informatique
http://www.cs.virginia .edu / ~lcc-win32


jacob navia写道:
jacob navia wrote:

Peter Nilsson写道:
Peter Nilsson wrote:

> Jeff Taylor< dev.n ... @ spam.netwrote:
>Jeff Taylor <dev.n...@spam.netwrote:

> ;>我有一个C程序,GCC编译时没有警告,并且它运行得很好。
>>I have a C program that GCC compiles without warnings and
it runs just fine.


意识到输出不是衡量正确性的唯一标准。


Realise that output is not the only measure of correctness.



有趣的有趣......


你能说出任何其他不依赖于
输出的措施吗?


Interesting interesting...

Can you name any other measure that doesn''t rely on
output?



调用缓冲区溢出的程序,但恰好产生预期的

输出肯定不正确?


不允许使用通灵媒体。

Using a psychic medium is not allowed.


> ;>但是,当我使用任何-O选项
(-O0,-O1等)编译它时,它会在执行时出现段错误(仍然没有编译器警告或错误)。
>> However, when I compile it using any of the -O options
(-O0, -O1, etc.) it segfaults when executed (still no
compiler warnings or errors).


我担心您有责任诊断代码中的错误,而不是编译器。


I''m afraid you are responsible for diagnosing errors in your
code, not your compiler.



深思熟虑。

Deep thought.



他的意思是语义错误。或者你的编译器是否为程序员编程?
? :-)


< snip>

He means semantic errors. Or does your compiler do the programming for
the programmer? :-)

<snip>


这篇关于Segfault只有优化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆