0

私のコード:

#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);
}

これは私にエラーを与えています: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 :)");
}

私の間違いを指摘していただけますか?

4

3 に答える 3

1

違いは、Cvoid *では aを他のポインター型に暗黙的に変換できるのに対し、 C++ では変換できないことです。

C++ ではvoid (*)(int)エラーにならないようにするには、実際の関数型 ( ) への明示的なキャストが必要です。

CC++の両方で、a を関数ポインターに変換する動作は未定義ですが、最初に互換性のある関数ポインターから変換されたvoid *場合は通常は機能することに注意してください。void *さらに、関数ポインターは、すべての引数の型がまったく同じである場合にのみ完全な互換性が保証されますが、異なる引数の型で動作するように見えるものを取得することがよくあります (未定義の動作が再び発生します)。

于 2013-06-05T18:13:44.850 に答える