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, you may find one here:
18 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
19 * or you may search the http://www.gnu.org website for the version 2 license,
20 * or you may write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22 */
23
24#include <csignal>
25#include <string>
26#include <iostream>
28#ifdef __APPLE__
29#define BOOST_STACKTRACE_GNU_SOURCE_NOT_REQUIRED
30#endif
31#ifdef __linux__
32#define BOOST_STACKTRACE_USE_ADDR2LINE
33#endif
34#include <boost/stacktrace.hpp>
35#include <boost/test/unit_test_monitor.hpp>
36
37
38static std::string get_signal_string( int signum )
39{
40#ifdef _WIN32
41 switch( signum )
42 {
43 case SIGABRT: return "SIGABRT (Abnormal termination)";
44 case SIGFPE: return "SIGFPE (Floating-point exception)";
45 case SIGILL: return "SIGILL (Illegal instruction)";
46 case SIGINT: return "SIGINT (Interrupt)";
47 case SIGSEGV: return "SIGSEGV (Segmentation fault)";
48 case SIGTERM: return "SIGTERM (Termination request)";
49 default: return "Unknown signal";
50 }
51#else
52 return strsignal( signum );
53#endif
54}
55
56
57static void signal_handler( int signum )
58{
59 // Associate the signal number with a name
60 ::signal( signum, SIG_DFL ); // Re-register the default handler
61
62 std::cerr << std::endl << "Signal caught: " << get_signal_string( signum ) << " (" << signum << ")" << std::endl;
63 std::cerr << "Stack trace:" << std::endl;
64
65 // Print the stack trace to standard error
66 std::cerr << boost::stacktrace::stacktrace() << std::endl;
67
68 // Let the default handler terminate the program
69 ::raise( signum );
70}
71
72
74{
75 ::signal( SIGSEGV, &signal_handler );
76 ::signal( SIGABRT, &signal_handler );
77 ::signal( SIGFPE, &signal_handler );
78};
79
80
81static void translate_std_exception( const std::exception& e )
82{
83 std::cerr << std::endl << "Caught exception: " << e.what() << std::endl << std::endl;
84
85 // Print the stack trace
86 std::cerr << "Stack trace:" << std::endl;
87 std::cerr << boost::stacktrace::stacktrace() << std::endl << std::endl;
88
89 // Re-throw the exception to let Boost.Test handle it and fail the test
90 throw;
91}
92
93
95{
96 boost::unit_test::unit_test_monitor.register_exception_translator<std::exception>( &translate_std_exception );
97}
static std::string get_signal_string(int signum)
static void signal_handler(int signum)
static void translate_std_exception(const std::exception &e)