1
0
mirror of https://github.com/paboyle/Grid.git synced 2024-11-10 07:55:35 +00:00
Grid/lib/Grid_init.cc

133 lines
2.6 KiB
C++
Raw Normal View History

2015-03-04 13:43:19 +00:00
/****************************************************************************/
/* PAB: Signal magic. Processor state dump is x86-64 specific */
/****************************************************************************/
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <unistd.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <signal.h>
#include <iostream>
#include <Grid.h>
2015-03-04 13:43:19 +00:00
#undef __X86_64
#define MAC
#ifdef MAC
#include <execinfo.h>
#endif
2015-04-03 05:29:54 +01:00
namespace Grid {
2015-03-04 13:43:19 +00:00
std::streambuf *Grid_saved_stream_buf;
#if 0
void Grid_quiesce_nodes(void)
{
#ifdef GRID_COMMS_MPI
int me;
MPI_Comm_rank(MPI_COMM_WORLD,&me);
std::streambuf* Grid_saved_stream_buf = std::cout.rdbuf();
if ( me ) {
std::ofstream file("log.node");
std::cout.rdbuf(file.rdbuf());
}
#endif
}
#endif
void Grid_quiesce_nodes(void)
{
#ifdef GRID_COMMS_MPI
int me;
MPI_Comm_rank(MPI_COMM_WORLD,&me);
if ( me ) {
std::cout.setstate(std::ios::badbit);
}
#endif
}
void Grid_unquiesce_nodes(void)
{
#ifdef GRID_COMMS_MPI
std::cout.clear();
#endif
}
void Grid_init(int *argc,char ***argv)
2015-03-04 13:43:19 +00:00
{
#ifdef GRID_COMMS_MPI
MPI_Init(argc,argv);
#endif
2015-03-04 13:43:19 +00:00
Grid_debug_handler_init();
Grid_quiesce_nodes();
2015-03-04 13:43:19 +00:00
}
2015-04-10 04:22:36 +01:00
void Grid_finalize(void)
{
#ifdef GRID_COMMS_MPI
MPI_Finalize();
#endif
}
2015-03-04 13:43:19 +00:00
double usecond(void) {
struct timeval tv;
gettimeofday(&tv,NULL);
return 1.0*tv.tv_usec + 1.0e6*tv.tv_sec;
}
#define _NBACKTRACE (256)
void * Grid_backtrace_buffer[_NBACKTRACE];
2015-03-04 13:43:19 +00:00
void Grid_sa_signal_handler(int sig,siginfo_t *si,void * ptr)
{
printf("Caught signal %d\n",si->si_signo);
2015-05-05 22:09:22 +01:00
printf(" mem address %lx\n",(uint64_t)si->si_addr);
2015-03-04 13:43:19 +00:00
printf(" code %d\n",si->si_code);
#ifdef __X86_64
ucontext_t * uc= (ucontext_t *)ptr;
2015-03-04 13:43:19 +00:00
struct sigcontext *sc = (struct sigcontext *)&uc->uc_mcontext;
printf(" instruction %llx\n",(uint64_t)sc->rip);
#define REG(A) printf(" %s %lx\n",#A, sc-> A);
REG(rdi);
REG(rsi);
REG(rbp);
REG(rbx);
REG(rdx);
REG(rax);
REG(rcx);
REG(rsp);
REG(rip);
REG(r8);
REG(r9);
REG(r10);
REG(r11);
REG(r12);
REG(r13);
REG(r14);
REG(r15);
#endif
#ifdef MAC
int symbols = backtrace (Grid_backtrace_buffer,_NBACKTRACE);
char **strings = backtrace_symbols(Grid_backtrace_buffer,symbols);
for (int i = 0; i < symbols; i++){
printf ("%s\n", strings[i]);
2015-03-04 13:43:19 +00:00
}
#endif
exit(0);
2015-03-04 13:43:19 +00:00
return;
};
void Grid_debug_handler_init(void)
{
struct sigaction sa,osa;
sigemptyset (&sa.sa_mask);
sa.sa_sigaction= Grid_sa_signal_handler;
sa.sa_flags = SA_SIGINFO;
sigaction(SIGSEGV,&sa,NULL);
sigaction(SIGTRAP,&sa,NULL);
}
}