libMesh
Loading...
Searching...
No Matches
libmesh_common.C
Go to the documentation of this file.
1// The libMesh Finite Element Library.
2// Copyright (C) 2002-2026 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
42namespace libMesh
43{
44
45namespace MacroFunctions
46{
47void 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
59void 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
77void 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 libMesh::MacroFunctions::here(file, line, date, time, os);
95
97 libMesh::on_command_line("--print-trace"))
99 else
101
102 reporting_error = false;
103}
104
105} // namespace MacroFunctions
106
107// demangle() is used by the process_trace() helper function. It is
108// also used by the Parameters class and cast_xxx functions for demangling typeid's. If
109// configure determined that your compiler does not support
110// demangling, it simply returns the input string.
111#if defined(LIBMESH_HAVE_GCC_ABI_DEMANGLE)
112std::string demangle(const char * name)
113{
114 int status = 0;
115 std::string ret = name;
116
117 // Actually do the demangling
118 char * demangled_name = abi::__cxa_demangle(name, 0, 0, &status);
119
120 // If demangling returns non-nullptr, save the result in a string.
121 if (demangled_name)
122 ret = demangled_name;
123
124 // According to cxxabi.h docs, the caller is responsible for
125 // deallocating memory.
126 std::free(demangled_name);
127
128 return ret;
129}
130#else
131std::string demangle(const char * name) { return std::string(name); }
132#endif
133
134} // namespace libMesh
void here(const char *file, int line, const char *date, const char *time, std::ostream &os=libMesh::err)
Threads::spin_mutex report_error_spin_mtx
void stop(const char *file, int line, const char *date, const char *time)
void report_error(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.
processor_id_type global_processor_id()
void write_traceout()
Writes a stack trace to a uniquely named file if –enable-tracefiles has been set by configure,...
std::string demangle(const char *name)
Mostly system independent demangler.
OStreamProxy out
processor_id_type global_n_processors()
void print_trace(std::ostream &out_stream=std::cerr)
Print a stack trace (for code compiled with gcc)
bool on_command_line(std::string arg)
Definition libmesh.C:934