aboutsummaryrefslogblamecommitdiff
path: root/src/espik_debug.c
blob: 80f1fd1d9fb2401472714e668bee03969bbd54dd (plain) (tree)
1
2
3
4
5
6
7
8
9


                   

                        
 
                

                       
 
                    

                           
 
                              
 

                                    
                
                    
 



                           
 
                                 








                                                                      
 
                           
                                                    


                       
                                                                                                                      
                       



                       
                    



                                                       
                                                       
                                                       
                           
 





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

#include "espik_debug.h"

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

#ifdef _ESPIK_DEBUG_
#include <signal.h>
#endif	/* _ESPIK_DEBUG_ */

//static int indent_level = 0;

inline void espik_debug_bt (int num)
{
#ifdef __GLIBC__
#ifdef _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);

#endif	/* _ESPIK_DEBUG_ */
	num ++;		/* Just for avoid warning */
#endif	/* __GLIBC__ */

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

void espik_debug_init()
{
#ifdef _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	/* _ESPIK_DEBUG_ */
}

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