-
Notifications
You must be signed in to change notification settings - Fork 157
/
C++_Debug.cpp
120 lines (95 loc) · 2.76 KB
/
C++_Debug.cpp
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/* Copyright (C) 2006 Imperial College London and others.
Please see the AUTHORS file in the main source directory for a full list
of copyright holders.
Prof. C Pain
Applied Modelling and Computation Group
Department of Earth Science and Engineering
Imperial College London
amcgsoftware@imperial.ac.uk
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation,
version 2.1 of the License.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
USA
*/
#include "confdefs.h"
#ifdef HAVE_MPI
#include <mpi.h>
#endif
#include "fmangle.h"
#include "c++debug.h"
#include <algorithm>
#include <cassert>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <vector>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#ifdef _GNU_SOURCE
#include <execinfo.h>
#endif
using namespace std;
static std::map<int, double> totaltime;
static std::map<int, double> starttime;
void print_backtrace(){
#ifdef _GNU_SOURCE
cerr<<"Backtrace will follow if it is available:\n";
void *bt[40];
size_t btsize = backtrace(bt, 40);
char **symbls = backtrace_symbols (bt, btsize);
if(symbls!=NULL){
for(size_t i=0;i<btsize;i++){
cerr<<symbls[i]<<endl;
}
free(symbls);
}
cerr<<"Use addr2line -e <binary> <address> to decipher.\n";
#endif
return;
}
void FLAbort(const char *ErrorStr, const char *FromFile, int LineNumber){
cerr<<"*** FLUIDITY ERROR ***\n"
<<"Source location: ("<<FromFile<<", "<<LineNumber<<")\n"
<<"Error message: "<<ErrorStr<<endl;
print_backtrace();
cerr<<"Error is terminal.";
#ifdef HAVE_MPI
MPI::COMM_WORLD.Abort(MPI::ERR_OTHER);
#endif
}
void FLExit(const char *ErrorStr, const char *FromFile, int LineNumber){
cerr<<"*** ERROR ***\n"
#ifndef NDEBUG
<<"Source location: ("<<FromFile<<", "<<LineNumber<<")\n"
#endif
<<"Error message: "<<ErrorStr<<endl;
#ifdef HAVE_MPI
MPI::COMM_WORLD.Abort(MPI::ERR_OTHER);
#endif
}
extern "C"{
void fprint_backtrace_fc(){
#ifdef _GNU_SOURCE
void *bt[40];
size_t btsize = backtrace(bt, 40);
char **symbls = backtrace_symbols (bt, btsize);
if(symbls!=NULL){
for(size_t i=0;i<btsize;i++){
cerr<<symbls[i]<<endl;
}
free(symbls);
}
return;
#endif
}
}