在DirectFBCreate

时间:2017-09-01 11:51:24

标签: c linux

在我的应用程序中,我希望根据这篇文章实现对分段错误的回溯: How to generate a stacktrace when my gcc C++ app crashes

但我遇到了一个问题。我的应用程序使用DirectFB进行图形处理。通过调用DirectFBCreate初始化DirectFB后,信号处理程序停止被调用。无论信号处理程序在何处注册。请比较以下代码中的main1,main2和main3函数:

#include <stdio.h>
#include <execinfo.h>
#include <signal.h>
#include <stdlib.h>
#include <unistd.h>
#include <directfb.h>

void handler(int sig) {
  void *array[10];
  size_t size;

  // get void*'s for all entries on the stack
  size = backtrace(array, 10);

  // print out all the frames to stderr
  fprintf(stderr, "Error: signal %d:\n", sig);
  backtrace_symbols_fd(array, size, STDERR_FILENO);
  exit(1);
}

void baz() {
 int *foo = (int*)-1; // make a bad pointer
  printf("%d\n", *foo);       // causes segfault
}

void bar() { baz(); }
void foo() { bar(); }


int main1(int argc, char **argv) {
  signal(SIGSEGV, handler);   // install our handler

  // if the foo() function is called here,
  // everything works as it should
  foo();

  IDirectFB *dfb = NULL;
  DFBCHECK (DirectFBInit (&argc, &argv));
  DFBCHECK (DirectFBCreate (&dfb));
}

int main2(int argc, char **argv) {
  signal(SIGSEGV, handler);   // install our handler

  IDirectFB *dfb = NULL;
  DFBCHECK (DirectFBInit (&argc, &argv));
  DFBCHECK (DirectFBCreate (&dfb));

  // but calling the foo() function after DirectFBCreate causes
  // that the handler is not called
  foo();
}

int main2(int argc, char **argv) {


  IDirectFB *dfb = NULL;
  DFBCHECK (DirectFBInit (&argc, &argv));
  DFBCHECK (DirectFBCreate (&dfb));

  signal(SIGSEGV, handler);   // install our handler
  // calling the foo() function after DirectFBCreate causes,
  // that the handler is not called
  // no matter the signal handler is registered after DirectFBCreate calling
  foo();
}

我还尝试了sigaction函数而不是signal函数,结果相同。

我也尝试使用sigprocmask(SIG_SETMASK, &mask, NULL)解锁信号。但这也没有帮助(我预期)。

最后我发现了这篇帖子signal handler not working, 这似乎解决了类似的问题,通过调用zsys_handler_set(NULL);禁用库的信号处理程序。所以我尝试了signal(SIGSEGV, NULL);signal(SIGSEGV, SIG_DFL);。再一次没有成功。我没有在DirectFB中找到任何处理程序禁用功能。虽然我在DirectFB配置中找到了[no-] sighandler参数并使用了它,但这并没有帮助更好(这让我感到很惊讶)。

我的问题是:如果DirectFB能够处理我的处理程序,我该如何取回它?

1 个答案:

答案 0 :(得分:1)

我使用了评论中提到的strace。我发现DirectFB不会调用sigaction系统调用,但会阻塞一些信号,包括SIGSEGV。在DirectFB初始化之后解锁信号。

// DirectFb initialization
IDirectFB *dfb = NULL;
DFBCHECK (DirectFBInit (&argc, &argv));
DFBCHECK (DirectFBCreate (&dfb));

// Unblock the signal
sigset_t sa_mask;
sigemptyset(&sa_mask);
sigaddset(&sa_mask, SIGSEGV);
sigprocmask(SIG_UNBLOCK, &sa_mask, NULL);
    // here is important to use SIG_UNBLOCK flag
    // not SIG_SETMASK as I did in my question!!!

// Now this causes the handler call
foo();