Skip to content

Commit 502d428

Browse files
authored
Merge pull request #4376 from loganharbour/other_terminate
Add `libmesh_abort()`, separate exception content
2 parents b486db8 + 4b38f15 commit 502d428

11 files changed

Lines changed: 446 additions & 252 deletions

File tree

Makefile.in

Lines changed: 91 additions & 20 deletions
Large diffs are not rendered by default.

include/Makefile.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -632,6 +632,7 @@ include_HEADERS = \
632632
base/getpot.h \
633633
base/id_types.h \
634634
base/libmesh.h \
635+
base/libmesh_abort.h \
635636
base/libmesh_base.h \
636637
base/libmesh_common.h \
637638
base/libmesh_documentation.h \

include/base/libmesh.h

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ namespace Parallel {
6565
class Communicator;
6666
}
6767

68+
class PerfLog;
6869
enum SolverPackage : int;
6970

7071
/**
@@ -147,6 +148,15 @@ class LibMeshInit
147148
// protected and forces us to use a named destructor manually
148149
vtkMPIController * _vtk_mpi_controller;
149150
#endif
151+
152+
#ifdef LIBMESH_ENABLE_EXCEPTIONS
153+
static std::terminate_handler _old_terminate_handler;
154+
#endif
155+
156+
static PerfLog & perf_log();
157+
158+
friend void libmesh_abort();
159+
friend void libmesh_terminate_handler();
150160
};
151161

152162
/**
@@ -165,23 +175,6 @@ bool initialized ();
165175
*/
166176
bool closed ();
167177

168-
/**
169-
* A terminate handler. libMesh sets this to handle uncaught
170-
* exceptions; it can also be called manually to print stack traces
171-
* and perf logs and call MPI
172-
*/
173-
void libmesh_terminate_handler();
174-
175-
/**
176-
* Toggle hardware trap floating point exceptions
177-
*/
178-
void enableFPE(bool on);
179-
180-
/**
181-
* Toggle libMesh reporting of segmentation faults
182-
*/
183-
void enableSEGV(bool on);
184-
185178
/**
186179
* \returns \p true if the argument \p arg was specified on the command line,
187180
* \p false otherwise.

include/base/libmesh_abort.h

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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+
20+
#ifndef LIBMESH_LIBMESH_ABORT_H
21+
#define LIBMESH_LIBMESH_ABORT_H
22+
23+
#include "libmesh/libmesh_config.h"
24+
25+
namespace libMesh {
26+
27+
/**
28+
* Abort as soon as possible.
29+
*
30+
* This cleans up stream buffers, aborts with MPI_Abort (if MPI
31+
* is initialized), and falls back on std::abort afterward.
32+
*/
33+
[[noreturn]] void libmesh_abort();
34+
35+
}
36+
37+
#endif // LIBMESH_LIBMESH_ABORT_H

include/base/libmesh_exceptions.h

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,37 @@
2222

2323
#include "libmesh/libmesh_config.h"
2424

25+
#include "libmesh/libmesh_abort.h"
26+
2527
#include <stdexcept>
2628
#include <string>
2729
#include <sstream>
2830

2931
namespace libMesh {
3032

33+
/**
34+
* A terminate handler. libMesh sets this to handle uncaught
35+
* exceptions; it can also be called manually to cleanup, print
36+
* any diagnostics, do cleanup, and abort.
37+
*
38+
* If an uncaught exception is a TerminationException, as thrown by
39+
* libmesh_terminate(), the handler avoids any diagnostic output.
40+
*
41+
* If an uncaught exception is a std::exception, its message is
42+
* printed, followed by stack trace and performance log output.
43+
*/
44+
void libmesh_terminate_handler();
45+
46+
/**
47+
* Toggle hardware trap floating point exceptions
48+
*/
49+
void enableFPE(bool on);
50+
51+
/**
52+
* Toggle libMesh reporting of segmentation faults
53+
*/
54+
void enableSEGV(bool on);
55+
3156
/**
3257
* A class to represent the internal "this should never happen"
3358
* errors, to be thrown by "libmesh_error();"
@@ -194,7 +219,7 @@ class TerminationException
194219

195220
#else
196221

197-
#define LIBMESH_THROW(e) do { libMesh::err << e.what(); std::abort(); } while (0)
222+
#define LIBMESH_THROW(e) do { libMesh::err << e.what(); libmesh_abort(); } while (0)
198223
#define libmesh_rethrow
199224
#define libmesh_try
200225
#define libmesh_catch(e) if (0)

include/include_HEADERS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ include_HEADERS = \
2626
base/getpot.h \
2727
base/id_types.h \
2828
base/libmesh.h \
29+
base/libmesh_abort.h \
2930
base/libmesh_base.h \
3031
base/libmesh_common.h \
3132
base/libmesh_documentation.h \

include/libmesh/Makefile.am

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ BUILT_SOURCES = \
1515
getpot.h \
1616
id_types.h \
1717
libmesh.h \
18+
libmesh_abort.h \
1819
libmesh_augment_std_namespace.h \
1920
libmesh_base.h \
2021
libmesh_common.h \
@@ -643,6 +644,9 @@ id_types.h: $(top_srcdir)/include/base/id_types.h
643644
libmesh.h: $(top_srcdir)/include/base/libmesh.h
644645
$(AM_V_GEN)rm -f $@ && $(LN_S) -f $< $@
645646

647+
libmesh_abort.h: $(top_srcdir)/include/base/libmesh_abort.h
648+
$(AM_V_GEN)rm -f $@ && $(LN_S) -f $< $@
649+
646650
libmesh_augment_std_namespace.h: $(top_srcdir)/include/base/libmesh_augment_std_namespace.h
647651
$(AM_V_GEN)rm -f $@ && $(LN_S) -f $< $@
648652

include/libmesh/Makefile.in

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -528,11 +528,11 @@ vtkversion = @vtkversion@
528528
EXTRA_DIST = rebuild_makefile.sh
529529
BUILT_SOURCES = dirichlet_boundaries.h dof_map.h dof_map_base.h \
530530
dof_object.h factory.h float128_shims.h getpot.h id_types.h \
531-
libmesh.h libmesh_augment_std_namespace.h libmesh_base.h \
532-
libmesh_common.h libmesh_documentation.h libmesh_exceptions.h \
533-
libmesh_logging.h libmesh_singleton.h libmesh_version.h \
534-
multi_predicates.h periodic_boundaries.h periodic_boundary.h \
535-
periodic_boundary_base.h print_trace.h \
531+
libmesh.h libmesh_abort.h libmesh_augment_std_namespace.h \
532+
libmesh_base.h libmesh_common.h libmesh_documentation.h \
533+
libmesh_exceptions.h libmesh_logging.h libmesh_singleton.h \
534+
libmesh_version.h multi_predicates.h periodic_boundaries.h \
535+
periodic_boundary.h periodic_boundary_base.h print_trace.h \
536536
reference_counted_object.h reference_counter.h \
537537
single_predicates.h sparsity_pattern.h variable.h \
538538
variant_filter_iterator.h enum_convergence_flags.h \
@@ -979,6 +979,9 @@ id_types.h: $(top_srcdir)/include/base/id_types.h
979979
libmesh.h: $(top_srcdir)/include/base/libmesh.h
980980
$(AM_V_GEN)rm -f $@ && $(LN_S) -f $< $@
981981

982+
libmesh_abort.h: $(top_srcdir)/include/base/libmesh_abort.h
983+
$(AM_V_GEN)rm -f $@ && $(LN_S) -f $< $@
984+
982985
libmesh_augment_std_namespace.h: $(top_srcdir)/include/base/libmesh_augment_std_namespace.h
983986
$(AM_V_GEN)rm -f $@ && $(LN_S) -f $< $@
984987

0 commit comments

Comments
 (0)