aboutsummaryrefslogtreecommitdiff
path: root/src/espik_debug.c
blob: c6e8a26077ec5654ae428f194ddf6c98e1ee01c1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include "espik_debug.h"

#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>

#ifdef __GLIBC__
#	include <execinfo.h>
#endif		/* __GLIBC__ */

#if _ESPIK_DEBUG
#	include <signal.h>
#endif		/* _ESPIK_DEBUG */

inline void espik_debug_bt (int num)
{
#ifdef __GLIBC__ && _ESPIK_DEBUG
	void*	array[128];
	size_t	size;
	char**	strings;
	size_t	i;

/*	if (!debug_bt) return; */

	fprintf(stderr, "\n***** Backtrace (Signal %d) *****\n", num);
	size = backtrace(array, 128);
	strings = backtrace_symbols(array, size);
	for (i = 0 ; i < size ; i++)
		fprintf(stderr, "%s\n", strings[i]);

	if (strings)
		free(strings);
#else
#	ifndef __GLIBC__
	fprintf(stderr, "Your system doesn't have glibc. Backtraces disabled. But program receive signal %d.\n", num);
#	endif	/* ! __GLIBC__ */
#endif		/* __GLIBC__ && _ESPIK_DEBUG */
}

void espik_debug_init()
{
#if _ESPIK_DEBUG
	struct sigaction sa;

	sa.sa_handler = espik_debug_bt;
	sigaction(SIGSEGV, &sa, (struct sigaction *)0);
	sigaction(SIGINT,  &sa, (struct sigaction *)0);
	sigaction(SIGKILL, &sa, (struct sigaction *)0);
#endif
}

void kill_me (short num)
{
	espik_debug_bt(num);
	exit (num);
}