PLaSK library

Loading...
Searching...
No Matches
exceptions.cpp
Go to the documentation of this file.
1/*
2 * This file is part of PLaSK (https://plask.app) by Photonics Group at TUL
3 * Copyright (c) 2022 Lodz University of Technology
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, version 3.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 */
14#if defined(_WIN32) || defined(__WIN32__) || defined(WIN32) // win32 support
15#include <win_printstack.hpp>
16#include <signal.h> /* signal, raise, sig_atomic_t */
17#endif
18
19#include "exceptions.hpp"
20
21#include <plask/config.hpp>
22
23#ifdef PRINT_STACKTRACE_ON_EXCEPTION
24
25#if defined(_WIN32) || defined(__WIN32__) || defined(WIN32) // win32 support
26
27 inline void print_current_exception() {
28 std::exception_ptr p = std::current_exception();
29 if (p) {
30 try {
31 std::rethrow_exception (p);
32 } catch (std::exception& e) {
33 printf("Current exception: %s\n", e.what());
34 } catch (...) {
35 printf("%s\n", "Current exception is not std one.");
36 }
37 } else
38 printf("%s\n", "There is no current exception.");
39}
40
41 const char* sig_name(int sig_nr) {
42 switch (sig_nr) {
43 case SIGABRT: return "SIGABRT";
44 case SIGSEGV: return "SIGSEGV";
45 case SIGTERM: return "SIGTERM";
46 }
47 return "unknown";
48}
49
50 void plask_win_signal_handler (int param) {
51 #pragma omp critical (winbacktrace)
52 {
53 printf("Signal %s (%d) handler:\n", sig_name(param), param);
54 print_current_exception();
55 //SIG_DFL(param); //call default signal handler
56 printStack(); //print stack-trace
57 }
58}
59
60 void plask_win_terminate_handler () {
61 #pragma omp critical (winbacktrace)
62 {
63 printf("Terminate handler:\n");
64 print_current_exception();
65 printStack(); //print stack-trace
66 abort(); // forces abnormal termination
67 }
68}
69
70 struct PlaskWinRegisterSignalHandler {
71 PlaskWinRegisterSignalHandler() {
72 signal(SIGABRT, plask_win_signal_handler);
73 signal(SIGSEGV, plask_win_signal_handler);
74 signal(SIGTERM, plask_win_signal_handler);
75 std::set_terminate (plask_win_terminate_handler);
76 }
77} __plaskWinRegisterSignalHandler;
78
79#else //non-windows systems:
80
81#include <backward.hpp>
82 namespace backward {
83 backward::SignalHandling sh;
84} // print backtrace on segfault, etc.
85
86#endif //other systems support
87
88#endif
89
90 namespace plask {
91
92 plask::Exception::Exception(const std::string &msg): std::runtime_error(msg) {
93}
94
95} // namespace plask