Shamrock 2025.10.0
Astrophysical Code
Loading...
Searching...
No Matches
SignalCatch.cpp
Go to the documentation of this file.
1// -------------------------------------------------------//
2//
3// SHAMROCK code for hydrodynamics
4// Copyright (c) 2021-2026 Timothée David--Cléris <tim.shamrock@proton.me>
5// SPDX-License-Identifier: CeCILL Free Software License Agreement v2.1
6// Shamrock is licensed under the CeCILL 2.1 License, see LICENSE for more information
7//
8// -------------------------------------------------------//
9
18#include "shamcmdopt/env.hpp"
19#include "shamcmdopt/tty.hpp"
23#include <csignal>
24#include <stdexcept>
25
26/*
27feature test for strsignal()
28
29strsignal():
30 Since glibc 2.10:
31 _XOPEN_SOURCE >= 700 || _POSIX_C_SOURCE >= 200809L
32 Before glibc 2.10:
33 _GNU_SOURCE
34
35*/
36#if defined(_XOPEN_SOURCE) && defined(_POSIX_C_SOURCE) && _XOPEN_SOURCE >= 700 \
37 && _POSIX_C_SOURCE >= 200809L
38 #define HAVE_STRSIGNAL
39#elif defined(_GNU_SOURCE)
40 #define HAVE_STRSIGNAL
41#endif
42
43std::string SHAM_CRASH_REPORT_FILE = shamcmdopt::getenv_str_default("SHAM_CRASH_REPORT_FILE", "");
44
45bool crash_report = false;
46std::string crash_report_filename = "";
47
48namespace shamsys::details {
49
56 const char *get_signal_name(int signum) {
57#ifdef HAVE_STRSIGNAL
58 const char *signame = strsignal(signum);
59#else
60 const char *signame = "UNKNOWN";
61#endif
62 return signame;
63 }
64
82 void signal_callback_handler(int signum) {
83
84 std::string log_start = fmt::format(
85 "!!! Received signal : {} (code {}) from world rank {}\n",
86 get_signal_name(signum),
87 signum,
89
90 std::string report = crash_report_backtrace();
91
92 std::string log_end = "exiting ...";
93
94 if (crash_report) {
95 std::ofstream outfile(crash_report_filename);
96 outfile << log_start << std::endl;
97 outfile << report << std::endl;
98 outfile << log_end << std::endl;
99 outfile.close();
100 std::cout << shambase::format(
101 "{}"
102 "Crash report written to {}",
103 log_start,
104 crash_report_filename)
105 << std::endl;
106 } else {
107 std::string merged_log = log_start + report + log_end;
108 std::cout << merged_log << std::endl;
109 }
110
111 // raise signal again since the handler was reset to the default (see SA_RESETHAND)
112 raise(signum);
113 }
114
115} // namespace shamsys::details
116
117namespace shamsys {
118 void register_signals() {
119
120 if (SHAM_CRASH_REPORT_FILE != "") {
121 crash_report = true;
122 crash_report_filename
123 = fmt::format("{}_rank_{}.txt", SHAM_CRASH_REPORT_FILE, shamcomm::world_rank());
124 }
125
126 init_backtrace_utilities(shambase::term_colors::colors_enabled() && !crash_report);
127
128 struct sigaction sa = {};
129 sa.sa_handler = details::signal_callback_handler;
130 sigemptyset(&sa.sa_mask);
131 // SA_RESETHAND resets the signal action to the default before calling the handler.
132 sa.sa_flags = SA_RESETHAND;
133
134 std::array catched_signals = {SIGTERM, SIGINT, SIGSEGV, SIGIOT};
135 for (auto signum : catched_signals) {
136 if (sigaction(signum, &sa, NULL) != 0) {
138 "Failed to register {} signal handler", details::get_signal_name(signum)));
139 }
140 }
141 }
142} // namespace shamsys
Header file describing a Node Instance.
const char * get_signal_name(int signum)
Name the received signal.
void signal_callback_handler(int signum)
The handler that will be called when a signal is catched.
This header file contains utility functions related to exception handling in the code.
void throw_with_loc(std::string message, SourceLocation loc=SourceLocation{})
Throw an exception and append the source location to it.
std::string getenv_str_default(const char *env_var, std::string default_val)
Get the content of the environment variable if it exist, otherwise return the default value.
Definition env.hpp:40
i32 world_rank()
Gives the rank of the current process in the MPI communicator.
Definition worldInfo.cpp:40
namespace for the system handling
void init_backtrace_utilities(bool enable_colors)
Initialize the backtrace utilities.
std::string crash_report_backtrace()
Generate a backtrace for the crash report.
This file contains the definition for the stacktrace related functionality.
Utilities to generate a backtrace for the crash report.
This file contains tty info getters.