在追查非法指令问题时(不易复现),需要验证自己的调试方式是否可行,就需要制造4) SIGILL非法指令问题进行测试和验证方法的可行性,然后添加到代码中当程序崩溃时打印出有用的信息,下面列出几种方法。
非法函数指针
typedef void(*FUNC)(void);
int main(void)
{
const static unsigned char insn[4] = { 0xff, 0xff, 0xff, 0xff };
FUNC function = (FUNC) insn;
function();
}
提示:非法指令(吐核),Signal Error! Signal is 4
打印非法string
#include <iostream>
#include <stdio.h>
using namespace std;
int main()
{
string s = "123";
printf("xxxx is %s\n", s);
}
我的gcc版本是:gcc 版本 4.8.5 20150623 (Red Hat 4.8.5-28) (GCC),报编译错误。
main.c: 在函数‘int main()’中:
main.c:10:26: 错误:不能通过‘...’传递有不能平凡复制的类型‘std::string {aka class std::basic_string<char>}’
printf("xxxx is %s\n", s);
^
如果你的编译器编译通过且只报警告的话就能出发程序崩溃。
warning: cannot pass objects of non-POD type ‘struct std::string’ through ‘...’; call will abort at runtime