运行程序出现Illegal instruction的定位和思考------又是printf string惹的火

来源:互联网 发布:ubuntu 命令行 菱形 编辑:程序博客网 时间:2024/05/22 14:31

        最近写了一个程序, 运行的时候出现了Illegal instruction错误, 觉得非常纳闷, 而且该打印的log并没有打印出来。 当时并没有报core dump错误(因为我的机器的core开关没有打开), 经过一段时间排查, 发现是程序出了问题。 下面, 我们看看最简化的程序(简化后, 一眼就可以看出问题):

#include <iostream>using namespace std;int main(){string s = "123";printf("xxxx is %s", s);}
        运行结果:

Illegal instruction

   

        奇怪了, 就用strace来定位, 发现:


--- SIGILL (Illegal instruction) @ 0 (0) ---
+++ killed by SIGILL +++
Process 19253 detached

        现在基本确定是程序有问题了.  此时, 可以继续加log,  找到log本应该出来但是没有出来的地方, 一看便知上述错误。

        后来打开机器的core开关, 果然就产生了core文件了和core dump提示了。 

        其实, 在编译的时候就有warning提示了: warning: cannot pass objects of non-POD type ‘struct std::string’ through ‘...’; call will abort at runtime  所以, 编译告警也有必要重视。


        昨天还苦苦纠结与茫茫程序, 睡一觉, 便很快解决。  看来还是要少加班!



阅读全文
1 0