Sigaction検出セグメントエラー例

1548 ワード

#include 
#include 
#include 
#include 
#include 
#include 
#include 

#define ARRAY_SIZE(a) sizeof(a)/sizeof(a[0])

#define DEBUG

#ifdef DEBUG
	#define LOG(fmt,args...) printf("%s():%d " fmt, __FUNCTION__, __LINE__, ##args)
#else
	#define LOG(fmt,args...) 
#endif


extern void dump_backtrace(void);
#include "common.h"

void dump_backtrace(void)
{
	int j, nptrs;
	void *buffer[32];
	char **strings;

	nptrs = backtrace(buffer,  ARRAY_SIZE(buffer) );
	printf("backtrace() returned %d addresses:
", nptrs); strings = backtrace_symbols(buffer, nptrs); if (NULL == strings) { perror("backtrace_symbols"); exit(-1); } for (j = 0; j < nptrs; j++) { printf(" %s
", strings[j]); } free(strings); }
#include "common.h"


void test()
{
	*((int*)0x04)  = 0;
}

void siginfo_handler(int signum, siginfo_t * siginfo, void * context)
{
	LOG("Signal Number: %d
", signum); LOG("Address: %p
", siginfo->si_addr); dump_backtrace(); exit(-1); } int main() { int ret = 0; struct sigaction act; act.sa_flags = SA_SIGINFO; act.sa_sigaction = siginfo_handler; sigemptyset(&act.sa_mask); ret = sigaction(SIGSEGV, &act, NULL); if(0 != ret) { perror("sigaction() failed"); } test(); return 0; }