KiCad PCB EDA Suite
Loading...
Searching...
No Matches
error_handlers.cpp
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright The KiCad Developers, see AUTHORS.TXT for contributors.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 */
19
20#include <csignal>
21#include <string>
22#include <iostream>
24#if defined(__APPLE__) || defined(__FreeBSD__)
25#define BOOST_STACKTRACE_GNU_SOURCE_NOT_REQUIRED
26#endif
27#ifdef __linux__
28#define BOOST_STACKTRACE_USE_ADDR2LINE
29#endif
30#include <boost/stacktrace.hpp>
31#include <boost/test/unit_test_monitor.hpp>
32
33
34static std::string get_signal_string( int signum )
35{
36#ifdef _WIN32
37 switch( signum )
38 {
39 case SIGABRT: return "SIGABRT (Abnormal termination)";
40 case SIGFPE: return "SIGFPE (Floating-point exception)";
41 case SIGILL: return "SIGILL (Illegal instruction)";
42 case SIGINT: return "SIGINT (Interrupt)";
43 case SIGSEGV: return "SIGSEGV (Segmentation fault)";
44 case SIGTERM: return "SIGTERM (Termination request)";
45 default: return "Unknown signal";
46 }
47#else
48 return strsignal( signum );
49#endif
50}
51
52
53static void signal_handler( int signum )
54{
55 // Associate the signal number with a name
56 ::signal( signum, SIG_DFL ); // Re-register the default handler
57
58 std::cerr << std::endl << "Signal caught: " << get_signal_string( signum ) << " (" << signum << ")" << std::endl;
59 std::cerr << "Stack trace:" << std::endl;
60
61 // Print the stack trace to standard error
62 std::cerr << boost::stacktrace::stacktrace() << std::endl;
63
64 // Let the default handler terminate the program
65 ::raise( signum );
66}
67
68
70{
71 ::signal( SIGSEGV, &signal_handler );
72 ::signal( SIGABRT, &signal_handler );
73 ::signal( SIGFPE, &signal_handler );
74};
75
76
77static void translate_std_exception( const std::exception& e )
78{
79 std::cerr << std::endl << "Caught exception: " << e.what() << std::endl << std::endl;
80
81 // Print the stack trace
82 std::cerr << "Stack trace:" << std::endl;
83 std::cerr << boost::stacktrace::stacktrace() << std::endl << std::endl;
84
85 // Re-throw the exception to let Boost.Test handle it and fail the test
86 throw;
87}
88
89
91{
92 boost::unit_test::unit_test_monitor.register_exception_translator<std::exception>( &translate_std_exception );
93}
static std::string get_signal_string(int signum)
static void signal_handler(int signum)
static void translate_std_exception(const std::exception &e)