TRUST 1.9.8
HPC thermohydraulic platform
Loading...
Searching...
No Matches
catch_and_trace.cpp
1/****************************************************************************
2* Copyright (c) 2026, CEA
3* All rights reserved.
4*
5* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
6* 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
7* 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
8* 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
9*
10* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
11* IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
12* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
13*
14*****************************************************************************/
15
16#include <catch_and_trace.h>
17
18// Wrapper struct for pclose to avoid function pointer in template argument of smart pointer:
19// https://stackoverflow.com/questions/76867698/what-does-ignoring-attributes-on-template-argument-mean-in-this-context
21{
22 int operator()(FILE *stream) const
23 {
24 return pclose(stream);
25 }
26};
27
28int exec_cmd_and_get_output(const char* cmd, std::string& result)
29{
30 std::array<char, 128> buffer;
31 std::unique_ptr<FILE, pipe_deleter> pipe(popen(cmd, "r"), pipe_deleter());
32 if (!pipe)
33 {
34 return 0; // popen failed - unable to execute system command
35 }
36 while (fgets(buffer.data(), (int)buffer.size(), pipe.get()) != nullptr)
37 {
38 result += buffer.data();
39 }
40 return 1;
41}
42
43static bool crit_err_hdlr_done = false;
44void crit_err_hdlr(int sig_num, siginfo_t * info, void * ucontext)
45{
46 if (crit_err_hdlr_done) return;
47 crit_err_hdlr_done = true;
48 Cerr << "======================================================================" << finl;
49 Cerr << "Error handler triggered!! See Journal - process ID: " << Process::me() << finl;
50 Cerr << "signal " << (int)sig_num << " (" << strsignal(sig_num) << ") " << finl;
51 if (sig_num == SIGFPE)
52 {
53 const char* fpe_type = "unknown FPE";
54 switch (info->si_code)
55 {
56 case FPE_INTDIV:
57 fpe_type = "FPE_INTDIV: integer divide by zero";
58 break;
59 case FPE_INTOVF:
60 fpe_type = "FPE_INTOVF: integer overflow";
61 break;
62 case FPE_FLTDIV:
63 fpe_type = "FPE_FLTDIV: floating-point divide by zero";
64 break;
65 case FPE_FLTOVF:
66 fpe_type = "FPE_FLTOVF: floating-point overflow";
67 break;
68 case FPE_FLTUND:
69 fpe_type = "FPE_FLTUND: floating-point underflow";
70 break;
71 case FPE_FLTRES:
72 fpe_type = "FPE_FLTRES: floating-point inexact result";
73 break;
74 case FPE_FLTINV:
75 fpe_type = "FPE_FLTINV: invalid floating-point operation (NaN, sqrt(-1)...)";
76 break;
77 case FPE_FLTSUB:
78 fpe_type = "FPE_FLTSUB: subscript out of range";
79 break;
80 }
81 Cerr << "FPE subtype: " << fpe_type << finl;
82 }
83
84 void * array[100];
85 int size = backtrace(array, 100);
86
87 //Cerr << __FUNCTION__ << " backtrace returned " << size << " frames\n" << finl;
88
89 // Extract symbols from backtrace
90 char ** messages = backtrace_symbols(array, size);
91
92 // Skip first stack frame (points here) and print out backtrace
93 for (int i = 1; i < size && messages != nullptr; ++i)
94 {
95 // Cerr << "[proc " << Process::me() << "]: (" << i << ") " << messages[i] << finl;
96 // Find first occurence of '(' or ' ' in message[i] and assume
97 // everything before that is the file name (don't go beyond 0 though, string terminator)
98 size_t p = 0;
99 while(messages[i][p] != '(' && messages[i][p] != ' ' && messages[i][p] != 0) ++p;
100
101 // Call 'addr2line' system utility to extract line number in the source code:
102 char syscom[256];
103 snprintf(syscom, 256, "addr2line %p --functions -e %.*s | tr '\n' ' ' | c++filt", array[i], (int)p, messages[i]);
104 std::string output;
105 int ret = exec_cmd_and_get_output(syscom, output);
106 if(!ret)
107 Cerr << "(Unable to execute 'addr2line' to get line number in source ...)" << finl;
108 else
109 Cerr << "[proc " << Process::me() << "]: " << output << finl;
110 }
111 Cerr << finl;
112 free(messages);
113
115}
116
117void install_handlers()
118{
119 struct sigaction sigact;
120 sigemptyset(&sigact.sa_mask);
121 sigact.sa_sigaction = crit_err_hdlr;
122 sigact.sa_flags = SA_RESTART | SA_SIGINFO;
123
124 if (sigaction(SIGABRT, &sigact, (struct sigaction *)nullptr) != 0)
125 {
126 std::cerr << "FATAL ERROR setting handler for signal " << SIGABRT
127 << " (" << strsignal(SIGABRT) << ")" << std::endl;
129 }
130
131 if (sigaction(SIGFPE, &sigact, (struct sigaction *)nullptr) != 0)
132 {
133 std::cerr << "FATAL ERROR setting handler for signal " << SIGFPE
134 << " (" << strsignal(SIGFPE) << ")" << std::endl;
136 }
137 std::cerr << "Custom error handlers correctly installed. SIGFPE and SIGABRT redirected." << std::endl;
138}
139
static int me()
renvoie mon rang dans le groupe de communication courant.
Definition Process.cpp:125
static void exit(int exit_code=-1)
Routine de sortie de TRUST dans une region Kokkos.
Definition Process.cpp:455
int operator()(FILE *stream) const