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#include <wx/thread.h>
25#if defined(__APPLE__) || defined(__FreeBSD__)
26#define BOOST_STACKTRACE_GNU_SOURCE_NOT_REQUIRED
27#endif
28#ifdef __linux__
29#define BOOST_STACKTRACE_USE_ADDR2LINE
30#endif
31#include <boost/stacktrace.hpp>
32#include <boost/test/unit_test_monitor.hpp>
33
34
35static std::string get_signal_string( int signum )
36{
37#ifdef _WIN32
38 switch( signum )
39 {
40 case SIGABRT: return "SIGABRT (Abnormal termination)";
41 case SIGFPE: return "SIGFPE (Floating-point exception)";
42 case SIGILL: return "SIGILL (Illegal instruction)";
43 case SIGINT: return "SIGINT (Interrupt)";
44 case SIGSEGV: return "SIGSEGV (Segmentation fault)";
45 case SIGTERM: return "SIGTERM (Termination request)";
46 default: return "Unknown signal";
47 }
48#else
49 return strsignal( signum );
50#endif
51}
52
53
54static void signal_handler( int signum )
55{
56 // Associate the signal number with a name
57 ::signal( signum, SIG_DFL ); // Re-register the default handler
58
59 std::cerr << std::endl << "Signal caught: " << get_signal_string( signum ) << " (" << signum << ")"
60 << ( wxThread::IsMain() ? "" : " on a worker thread" ) << std::endl;
61 std::cerr << "Stack trace:" << std::endl;
62
63 // Print the stack trace to standard error
64 std::cerr << boost::stacktrace::stacktrace() << std::endl;
65
66 // Let the default handler terminate the program
67 ::raise( signum );
68}
69
70
71// Boost.Test installs its own handlers around every test case, so these only survive when the
72// runner is started with --catch_system_errors=no. That is the way to get a usable stack out of
73// a crash in threaded code; Boost's handler long jumps into the main thread's context, which is
74// undefined behaviour when the fault happened on a thread pool worker
76{
77 ::signal( SIGSEGV, &signal_handler );
78 ::signal( SIGABRT, &signal_handler );
79 ::signal( SIGFPE, &signal_handler );
80};
81
82
83static void translate_std_exception( const std::exception& e )
84{
85 std::cerr << std::endl << "Caught exception: " << e.what() << std::endl << std::endl;
86
87 // Print the stack trace
88 std::cerr << "Stack trace:" << std::endl;
89 std::cerr << boost::stacktrace::stacktrace() << std::endl << std::endl;
90
91 // Re-throw the exception to let Boost.Test handle it and fail the test
92 throw;
93}
94
95
97{
98 boost::unit_test::unit_test_monitor.register_exception_translator<std::exception>( &translate_std_exception );
99}
static std::string get_signal_string(int signum)
static void signal_handler(int signum)
static void translate_std_exception(const std::exception &e)