MFEM  v4.0
Finite element discretization library
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Pages
error.cpp
Go to the documentation of this file.
1 // Copyright (c) 2010, Lawrence Livermore National Security, LLC. Produced at
2 // the Lawrence Livermore National Laboratory. LLNL-CODE-443211. All Rights
3 // reserved. See file COPYRIGHT for details.
4 //
5 // This file is part of the MFEM library. For more information and source code
6 // availability see http://mfem.org.
7 //
8 // MFEM is free software; you can redistribute it and/or modify it under the
9 // terms of the GNU Lesser General Public License (as published by the Free
10 // Software Foundation) version 2.1 dated February 1999.
11 
12 #include "error.hpp"
13 #include "globals.hpp"
14 #include "array.hpp"
15 #include <cstdlib>
16 #include <iostream>
17 
18 #ifdef MFEM_USE_LIBUNWIND
19 #define UNW_LOCAL_ONLY
20 #define UNW_NAME_LEN 512
21 #include <libunwind.h>
22 #include <cxxabi.h>
23 #if defined(__APPLE__) || defined(__linux__)
24 #ifndef _GNU_SOURCE
25 #define _GNU_SOURCE
26 #endif
27 #include <dlfcn.h>
28 #endif
29 #endif // MFEM_USE_LIBUNWIND
30 
31 #ifdef MFEM_USE_MPI
32 #include <mpi.h>
33 #endif
34 
35 namespace mfem
36 {
37 
38 #ifdef MFEM_USE_EXCEPTIONS
39 const char* ErrorException::what() const throw()
40 {
41  return msg.c_str();
42 }
43 
44 static ErrorAction mfem_error_action = MFEM_ERROR_THROW;
45 #else
46 static ErrorAction mfem_error_action = MFEM_ERROR_ABORT;
47 #endif
48 
50 {
51  // Check if 'action' is valid.
52  switch (action)
53  {
54  case MFEM_ERROR_ABORT: break;
55  case MFEM_ERROR_THROW:
56 #ifdef MFEM_USE_EXCEPTIONS
57  break;
58 #else
59  mfem_error("set_error_action: MFEM_ERROR_THROW requires the build "
60  "option MFEM_USE_EXCEPTIONS=YES");
61  return;
62 #endif
63  default:
64  mfem::err << "\n\nset_error_action: invalid action: " << action
65  << '\n';
66  mfem_error();
67  return;
68  }
69  mfem_error_action = action;
70 }
71 
73 {
74  return mfem_error_action;
75 }
76 
77 void mfem_backtrace(int mode, int depth)
78 {
79 #ifdef MFEM_USE_LIBUNWIND
80  char name[UNW_NAME_LEN];
81  unw_cursor_t cursor;
82  unw_context_t uc;
83  unw_word_t ip, offp;
84 
85  int err = unw_getcontext(&uc);
86  err = err ? err : unw_init_local(&cursor, &uc);
87 
88  Array<unw_word_t> addrs;
89  while (unw_step(&cursor) > 0 && addrs.Size() != depth)
90  {
91  err = err ? err : unw_get_proc_name(&cursor, name, UNW_NAME_LEN, &offp);
92  err = err ? err : unw_get_reg(&cursor, UNW_REG_IP, &ip);
93  if (err) { break; }
94  char *name_p = name;
95  int demangle_status;
96 
97  // __cxa_demangle is not standard, but works with GCC, Intel, PGI, Clang
98  char *name_demangle =
99  abi::__cxa_demangle(name, NULL, NULL, &demangle_status);
100  if (demangle_status == 0) // use mangled name if something goes wrong
101  {
102  name_p = name_demangle;
103  }
104 
105  mfem::err << addrs.Size() << ") [0x" << std::hex << ip - 1 << std::dec
106  << "]: " << name_p << std::endl;
107  addrs.Append(ip - 1);
108 
109  if (demangle_status == 0)
110  {
111  free(name_demangle);
112  }
113  }
114 #if defined(__APPLE__) || defined(__linux__)
115  if (addrs.Size() > 0 && (mode & 1))
116  {
117  mfem::err << "\nLookup backtrace source lines:";
118  const char *fname = NULL;
119  for (int i = 0; i < addrs.Size(); i++)
120  {
121  Dl_info info;
122  err = !dladdr((void*)addrs[i], &info);
123  if (err)
124  {
125  fname = "<exe>";
126  }
127  else if (fname != info.dli_fname)
128  {
129  fname = info.dli_fname;
130  mfem::err << '\n';
131 #ifdef __linux__
132  mfem::err << "addr2line -C -e " << fname;
133 #else
134  mfem::err << "atos -o " << fname << " -l "
135  << (err ? 0 : info.dli_fbase);
136 #endif
137  }
138  mfem::err << " 0x" << std::hex << addrs[i] << std::dec;
139  }
140  mfem::err << '\n';
141  }
142 #endif
143 #endif // MFEM_USE_LIBUNWIND
144 }
145 
146 void mfem_error(const char *msg)
147 {
148  if (msg)
149  {
150  // NOTE: By default, each call of the "operator <<" method of the
151  // mfem::err object results in flushing the I/O stream, which can be a
152  // very bad thing if all your processors try to do it at the same time.
153  mfem::err << "\n\n" << msg << "\n";
154  }
155 
156 #ifdef MFEM_USE_LIBUNWIND
157  mfem::err << "Backtrace:" << std::endl;
158  mfem_backtrace(1, -1);
159  mfem::err << std::endl;
160 #endif
161 
162 #ifdef MFEM_USE_EXCEPTIONS
163  if (mfem_error_action == MFEM_ERROR_THROW)
164  {
165  throw ErrorException(msg);
166  }
167 #endif
168 
169 #ifdef MFEM_USE_MPI
170  int init_flag, fin_flag;
171  MPI_Initialized(&init_flag);
172  MPI_Finalized(&fin_flag);
173  if (init_flag && !fin_flag) { MPI_Abort(GetGlobalMPI_Comm(), 1); }
174 #endif
175  std::abort(); // force crash by calling abort
176 }
177 
178 void mfem_warning(const char *msg)
179 {
180  if (msg)
181  {
182  mfem::out << "\n\n" << msg << std::endl;
183  }
184 }
185 
186 }
int Size() const
Logical size of the array.
Definition: array.hpp:118
virtual const char * what() const
Definition: error.cpp:39
ErrorAction get_error_action()
Get the action MFEM takes when an error is encountered.
Definition: error.cpp:72
Exception class thrown when MFEM encounters an error and the current ErrorAction is set to MFEM_ERROR...
Definition: error.hpp:41
int Append(const T &el)
Append element to array, resize if necessary.
Definition: array.hpp:690
void mfem_error(const char *msg)
Function called when an error is encountered. Used by the macros MFEM_ABORT, MFEM_ASSERT, MFEM_VERIFY.
Definition: error.cpp:146
void set_error_action(ErrorAction action)
Set the action MFEM takes when an error is encountered.
Definition: error.cpp:49
ErrorAction
Action to take when MFEM encounters an error.
Definition: error.hpp:23
OutStream err(std::cerr)
Global stream used by the library for standard error output. Initially it uses the same std::streambu...
Definition: globals.hpp:69
void mfem_warning(const char *msg)
Function called by the macro MFEM_WARNING.
Definition: error.cpp:178
void mfem_backtrace(int mode, int depth)
Definition: error.cpp:77
OutStream out(std::cout)
Global stream used by the library for standard output. Initially it uses the same std::streambuf as s...
Definition: globals.hpp:64
MPI_Comm GetGlobalMPI_Comm()
Get MFEM&#39;s &quot;global&quot; MPI communicator.
Definition: globals.cpp:39