【发布时间】:2014-02-06 11:39:39
【问题描述】:
我正在尝试从我的程序中捕获 SIGSEGV。我遇到了一个问题,我的 signal_handler 没有捕捉到信号。
void handler(int sig){
printf("catch SIGSEGV");
exit(EXIT_FAILURE);
}
void foo(){
struct sigaction sa;
sa.sa_flags = SA_SIGINFO;
sigemptyset(&sa.sa_mask);
sa.sa_handler = handler;
if(sigaction(SIGSEGV, &sa, NULL) == -1){
handle_error("sigaction");
}
/* if SIGSEGV happen here, I can catch it */
bar();
}
void bar() {
/* if SIGSEGV happen here, I cannot catch it */
}
这是否意味着我必须在 bar 内安装另一个信号处理程序?
但是如果我有一堆函数想要捕获相同的信号呢?我必须多次安装信号处理程序?
更新:
我尝试直接在函数中安装处理程序,但仍然无法捕获它。所以我认为这可能是其他问题。但这很奇怪。我使用gdb运行并获取
Program received signal SIGSEGV, Segmentation fault.
0x080499b1 in is_printable_string (
str=0xb80fe768 <Address 0xb80fe768 out of bounds>)
at trace/trace.c:259
259 while(str[index]!='\0'){
这是我的is_printable_String
int is_printable_string(char *str){
struct sigaction sa;
sa.sa_flags = SA_SIGINFO;
sigemptyset(&sa.sa_mask);
sa.sa_sigaction = handler;
if(sigaction(SIGSEGV, &sa, NULL) == -1){
handle_error("sigaction");
}
int index;
index=0;
while(str[index]!='\0'){
if(!isprint(str[index])){
return -1;
}
index++;
}
/* continue... */
这似乎是我遇到了 SEG 错误,但我无法捕捉到它
我故意传递了那个指针,所以str 参数没有错。
【问题讨论】:
标签: c signals signal-processing interrupt