MFEM  v4.3.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-2021, Lawrence Livermore National Security, LLC. Produced
2 // at the Lawrence Livermore National Laboratory. All Rights reserved. See files
3 // LICENSE and NOTICE for details. LLNL-CODE-806117.
4 //
5 // This file is part of the MFEM library. For more information and source code
6 // availability visit https://mfem.org.
7 //
8 // MFEM is free software; you can redistribute it and/or modify it under the
9 // terms of the BSD-3 license. We welcome feedback and contributions, see file
10 // CONTRIBUTING.md for details.
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 namespace internal
78 {
79 // defined in globals.cpp
80 extern bool mfem_out_initialized, mfem_err_initialized;
81 }
82 
83 void mfem_backtrace(int mode, int depth)
84 {
85 #ifdef MFEM_USE_LIBUNWIND
86  char name[UNW_NAME_LEN];
87  unw_cursor_t cursor;
88  unw_context_t uc;
89  unw_word_t ip, offp;
90  std::ostream &merr = internal::mfem_err_initialized ? mfem::err : std::cerr;
91 
92  int err = unw_getcontext(&uc);
93  err = err ? err : unw_init_local(&cursor, &uc);
94 
96  while (unw_step(&cursor) > 0 && addrs.Size() != depth)
97  {
98  err = err ? err : unw_get_proc_name(&cursor, name, UNW_NAME_LEN, &offp);
99  err = err ? err : unw_get_reg(&cursor, UNW_REG_IP, &ip);
100  if (err) { break; }
101  char *name_p = name;
102  int demangle_status;
103 
104  // __cxa_demangle is not standard, but works with GCC, Intel, PGI, Clang
105  char *name_demangle =
106  abi::__cxa_demangle(name, NULL, NULL, &demangle_status);
107  if (demangle_status == 0) // use mangled name if something goes wrong
108  {
109  name_p = name_demangle;
110  }
111 
112  merr << addrs.Size() << ") [0x" << std::hex << ip - 1 << std::dec
113  << "]: " << name_p << std::endl;
114  addrs.Append(ip - 1);
115 
116  if (demangle_status == 0)
117  {
118  free(name_demangle);
119  }
120  }
121 #if defined(__APPLE__) || defined(__linux__)
122  if (addrs.Size() > 0 && (mode & 1))
123  {
124  merr << "\nLookup backtrace source lines:";
125  const char *fname = NULL;
126  for (int i = 0; i < addrs.Size(); i++)
127  {
128  Dl_info info;
129  err = !dladdr((void*)addrs[i], &info);
130  if (err)
131  {
132  fname = "<exe>";
133  }
134  else if (fname != info.dli_fname)
135  {
136  fname = info.dli_fname;
137  merr << '\n';
138 #ifdef __linux__
139  merr << "addr2line -C -e " << fname;
140 #else
141  merr << "atos -o " << fname << " -l "
142  << (err ? 0 : info.dli_fbase);
143 #endif
144  }
145  merr << " 0x" << std::hex << addrs[i] << std::dec;
146  }
147  merr << '\n';
148  }
149 #endif
150 #endif // MFEM_USE_LIBUNWIND
151 }
152 
153 void mfem_error(const char *msg)
154 {
155  std::ostream &merr = internal::mfem_err_initialized ? mfem::err : std::cerr;
156  if (msg)
157  {
158  // NOTE: By default, each call of the "operator <<" method of the
159  // mfem::err object results in flushing the I/O stream, which can be a
160  // very bad thing if all your processors try to do it at the same time.
161  merr << "\n\n" << msg << "\n";
162  }
163 
164 #ifdef MFEM_USE_LIBUNWIND
165  merr << "Backtrace:" << std::endl;
166  mfem_backtrace(1, -1);
167  merr << std::endl;
168 #endif
169 
170 #ifdef MFEM_USE_EXCEPTIONS
171  if (mfem_error_action == MFEM_ERROR_THROW)
172  {
173  throw ErrorException(msg);
174  }
175 #endif
176 
177 #ifdef MFEM_USE_MPI
178  int init_flag, fin_flag;
179  MPI_Initialized(&init_flag);
180  MPI_Finalized(&fin_flag);
181  if (init_flag && !fin_flag) { MPI_Abort(GetGlobalMPI_Comm(), 1); }
182 #endif
183  std::abort(); // force crash by calling abort
184 }
185 
186 void mfem_warning(const char *msg)
187 {
188  std::ostream &mout = internal::mfem_out_initialized ? mfem::out : std::cout;
189  if (msg)
190  {
191  mout << "\n\n" << msg << std::endl;
192  }
193 }
194 
195 }
int Size() const
Return the logical size of the array.
Definition: array.hpp:134
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:44
int Append(const T &el)
Append element &#39;el&#39; to array, resize if necessary.
Definition: array.hpp:746
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:153
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:26
OutStream err(std::cerr)
Global stream used by the library for standard error output. Initially it uses the same std::streambu...
Definition: globals.hpp:71
Host memory; using new[] and delete[].
void mfem_warning(const char *msg)
Function called by the macro MFEM_WARNING.
Definition: error.cpp:186
void mfem_backtrace(int mode, int depth)
Definition: error.cpp:83
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:66
MPI_Comm GetGlobalMPI_Comm()
Get MFEM&#39;s &quot;global&quot; MPI communicator.
Definition: globals.cpp:62