在Linux上编译简单信号代码时,从void*到void(*)int的无效转换错误

Cha*_*ani 0 c c++ linux signals c++11

我的代码:

#include "xception.h"
#include <iostream>
#include <stdio.h>
#include <signal.h>
#include <stdio.h>
#include <signal.h>
#include <execinfo.h>

void bt_sighandler(int sig, struct sigcontext ctx) 
{

    void *trace[16];
    char **messages = (char **)NULL;
    int i, trace_size = 0;

    trace_size = backtrace(trace, 16);
    /* overwrite sigaction with caller's address */
    trace[1] = (void *)ctx.eip;
    messages = backtrace_symbols(trace, trace_size);
    /* skip first stack frame (points here) */
    printf("[bt] Execution path:\n");
    for (i=1; i<trace_size; ++i)
    {
        printf("[bt] #%d %s\n", i, messages[i]);

        char syscom[256];
        sprintf(syscom,"addr2line %p -e sighandler", trace[i]); 
        system(syscom);
    }
}


void xception::initialize_xception()
{
    /* Install our signal handler */
    struct sigaction sa;

    sa.sa_handler = (void *)bt_sighandler;
    sigemptyset(&sa.sa_mask);
    sa.sa_flags = SA_RESTART;

    sigaction(SIGUSR1, &sa, NULL);
}
Run Code Online (Sandbox Code Playgroud)

这是给我错误的: r@r-HP-Mini-110:~/l33t/freeln/Xception/source$ g++ -std=c++11 -g -rdynamic -Wall -o xcep_app application.cpp xception.cpp xception.cpp: In static member function ‘static void xception::initialize_xception()’: xception.cpp:38:28: error: invalid conversion from ‘void*’ to ‘__sighandler_t {aka void (*)(int)}’ [-fpermissive]

奇怪的是,在放置时,如下所示编译的内容如下:

#include <stdio.h>
#include <signal.h>
#include <stdio.h>
#include <signal.h>
#include <execinfo.h>

void bt_sighandler(int sig, struct sigcontext ctx) 
{

  void *trace[16];
  char **messages = (char **)NULL;
  int i, trace_size = 0;

  trace_size = backtrace(trace, 16);
  /* overwrite sigaction with caller's address */
  trace[1] = (void *)ctx.eip;
  messages = backtrace_symbols(trace, trace_size);
  /* skip first stack frame (points here) */
  printf("[bt] Execution path:\n");
  for (i=1; i<trace_size; ++i)
  {
      printf("[bt] #%d %s\n", i, messages[i]);

      char syscom[256];
      sprintf(syscom,"addr2line %p -e sighandler", trace[i]); //last parameter is the name of this app
      system(syscom);
  }

}


int func_a(int a, char b) {

  char *p = (char *)0xdeadbeef;

  //a = a + b;
  //*p = 10;  /* CRASH here!! */
  a =9;

  return 2*a;
}


int func_b() {

  int res, a = 5;

  res = 5 + func_a(a, 't');

  raise(SIGUSR1);

  return res;
}


int main() {

  /* Install our signal handler */
  struct sigaction sa;

  sa.sa_handler = (void *)bt_sighandler;
  sigemptyset(&sa.sa_mask);
  sa.sa_flags = SA_RESTART;

  //sigaction(SIGSEGV, &sa, NULL);
  sigaction(SIGUSR1, &sa, NULL);
  /* ... add any other signal here */

  /* Do something */
  printf("%d\n", func_b());

  printf("%s\n", "not dead yet :)");
}
Run Code Online (Sandbox Code Playgroud)

你能指出我的错误吗?

AnT*_*AnT 5

无论你如何切片,你都无法使用

void bt_sighandler(int sig, struct sigcontext ctx)
Run Code Online (Sandbox Code Playgroud)

函数void (int)需要回调的地方.您可以通过使用强制转换来成功地抑制错误消息,这将使代码正式"编译",但这不会使您的程序在"工作"这个词的任何有意义的意义上工作.

sa.sa_handler回调函数必须是一个void (int)函数,句点.没有办法绕过它.如果您想将一些额外的外部数据"走私"到该功能中,您必须创建一个替代解决方案.您不能只是向回调函数添加任意参数,并期望调用者以某种方式神奇地为您添加的参数提供正确的参数.