libMesh
libmesh_common.C
Go to the documentation of this file.
1 // The libMesh Finite Element Library.
2 // Copyright (C) 2002-2025 Benjamin S. Kirk, John W. Peterson, Roy H. Stogner
3 
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either
7 // version 2.1 of the License, or (at your option) any later version.
8 
9 // This library is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 // Lesser General Public License for more details.
13 
14 // You should have received a copy of the GNU Lesser General Public
15 // License along with this library; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 
18 
19 // libmesh includes
20 #include "libmesh/libmesh.h"
21 #include "libmesh/libmesh_common.h"
22 #include "libmesh/print_trace.h"
23 #include "libmesh/threads.h"
24 
25 // C/C++ includes
26 #ifdef LIBMESH_HAVE_SYS_TYPES_H
27 #include <sys/types.h>
28 #endif
29 #ifdef LIBMESH_HAVE_UNISTD_H
30 #include <unistd.h> // needed for getpid()
31 #endif
32 
33 #ifdef LIBMESH_HAVE_CSIGNAL
34 #include <csignal>
35 #endif
36 
37 #if defined(LIBMESH_HAVE_GCC_ABI_DEMANGLE)
38 #include <cxxabi.h>
39 #include <cstdlib>
40 #endif
41 
42 namespace libMesh
43 {
44 
45 namespace MacroFunctions
46 {
47 void here(const char * file, int line, const char * date, const char * time, std::ostream & os)
48 {
49  os << "[" << static_cast<std::size_t>(libMesh::global_processor_id()) << "] "
50  << file
51  << ", line " << line
52  << ", compiled " << date
53  << " at " << time
54  << std::endl;
55 }
56 
57 
58 
59 void stop(const char * file, int line, const char * date, const char * time)
60 {
62  {
63  libMesh::MacroFunctions::here(file, line, date, time);
64 #if defined(LIBMESH_HAVE_CSIGNAL) && defined(SIGSTOP)
65  libMesh::out << "Stopping process " << getpid() << "..." << std::endl;
66  std::raise(SIGSTOP);
67  libMesh::out << "Continuing process " << getpid() << "..." << std::endl;
68 #else
69  libMesh::out << "WARNING: libmesh_stop() does not work; no operating system support." << std::endl;
70 #endif
71  }
72 }
73 
74 
76 
77 void report_error(const char * file, int line, const char * date, const char * time, std::ostream & os)
78 {
79  // Avoid a race condition on that static bool
80  Threads::spin_mutex::scoped_lock lock(report_error_spin_mtx);
81 
82  // It is possible to have an error *inside* report_error; e.g. from
83  // print_trace. We don't want to infinitely recurse.
84  static bool reporting_error = false;
85  if (reporting_error)
86  {
87  // I heard you like error reporting, so we put an error report
88  // in report_error() so you can report errors from the report.
89  os << "libMesh encountered an error while attempting to report_error." << std::endl;
90  return;
91  }
92  reporting_error = true;
93 
94  if (libMesh::global_n_processors() == 1 ||
95  libMesh::on_command_line("--print-trace"))
97  else
99  libMesh::MacroFunctions::here(file, line, date, time, os);
100 
101  reporting_error = false;
102 }
103 
104 } // namespace MacroFunctions
105 
106 // demangle() is used by the process_trace() helper function. It is
107 // also used by the Parameters class and cast_xxx functions for demangling typeid's. If
108 // configure determined that your compiler does not support
109 // demangling, it simply returns the input string.
110 #if defined(LIBMESH_HAVE_GCC_ABI_DEMANGLE)
111 std::string demangle(const char * name)
112 {
113  int status = 0;
114  std::string ret = name;
115 
116  // Actually do the demangling
117  char * demangled_name = abi::__cxa_demangle(name, 0, 0, &status);
118 
119  // If demangling returns non-nullptr, save the result in a string.
120  if (demangled_name)
121  ret = demangled_name;
122 
123  // According to cxxabi.h docs, the caller is responsible for
124  // deallocating memory.
125  std::free(demangled_name);
126 
127  return ret;
128 }
129 #else
130 std::string demangle(const char * name) { return std::string(name); }
131 #endif
132 
133 } // namespace libMesh
std::string name(const ElemQuality q)
This function returns a string containing some name for q.
Definition: elem_quality.C:42
processor_id_type global_n_processors()
Definition: libmesh_base.h:75
void here(const char *file, int line, const char *date, const char *time, std::ostream &os=libMesh::err)
The libMesh namespace provides an interface to certain functionality in the library.
void write_traceout()
Writes a stack trace to a uniquely named file if –enable-tracefiles has been set by configure...
Definition: print_trace.C:240
MPI_Status status
void report_error(const char *file, int line, const char *date, const char *time, std::ostream &os=libMesh::err)
void stop(const char *file, int line, const char *date, const char *time)
Threads::spin_mutex report_error_spin_mtx
std::string demangle(const char *name)
Mostly system independent demangler.
void print_trace(std::ostream &out_stream=std::cerr)
Print a stack trace (for code compiled with gcc)
Definition: print_trace.C:202
OStreamProxy out
processor_id_type global_processor_id()
Definition: libmesh_base.h:85
bool on_command_line(std::string arg)
Definition: libmesh.C:987