libexcept 1.1.19
Stack trace along C++ exceptions
report_signal.cpp
Go to the documentation of this file.
1// Copyright (c) 2019-2025 Made to Order Software Corp. All Rights Reserved
2//
3// https://snapwebsites.org/project/libexcept
4// contact@m2osw.com
5//
6// This program is free software; you can redistribute it and/or modify
7// it under the terms of the GNU General Public License as published by
8// the Free Software Foundation; either version 2 of the License, or
9// (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 along
17// with this program; if not, write to the Free Software Foundation, Inc.,
18// 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19
20// self
21//
23
25
26
27// C++
28//
29#include <cstdint>
30#include <iostream>
31#include <memory>
32
33
34// C
35//
36#include <signal.h>
37
38
53namespace libexcept
54{
55
56
57
58namespace
59{
60
61typedef struct sigaction sigaction_t;
62typedef std::shared_ptr<sigaction_t> sigaction_ptr_t;
63
65
67 int sig
68 , siginfo_t * info
69 , void * context)
70{
71 // unused parameters
72 static_cast<void>(info);
73 static_cast<void>(context);
74
75 auto const trace(collect_stack_trace());
76 for(auto const & stack_line : trace)
77 {
78 std::cerr
79 << "report_signal():"
80 << sig
81 << ": backtrace="
82 << stack_line
83 << "\n";
84 }
85
86 // Abort
87 //
88 abort();
89}
90
91
92} // no name namespace
93
94
95
115{
116 constexpr std::int64_t sigs(
117 (1 << SIGHUP)
118 | (1 << SIGILL)
119 | (1 << SIGTRAP)
120 | (1 << SIGBUS)
121 | (1 << SIGFPE)
122 | (1 << SIGSEGV)
123 );
124 for(size_t i(0); i < std::size(g_signal_actions); ++i)
125 {
126 if((sigs & (1L << i)) != 0)
127 {
128 sigaction_t action = sigaction_t();
129 action.sa_sigaction = report_signal;
130 action.sa_flags = SA_SIGINFO | SA_RESETHAND;
131
132 g_signal_actions[i] = std::make_shared<sigaction_t>();
133 sigaction(i, &action, g_signal_actions[i].get());
134 }
135 }
136}
137
138
139
140}
141// namespace libexcept
142// vim: ts=4 sw=4 et
void report_signal(int sig, siginfo_t *info, void *context)
stack_trace_t collect_stack_trace(int stack_trace_depth)
Collect the raw stack trace in a list of strings.
void init_report_signal()
Setup the callbacks.
Report crashing signals if they are emitted.
Declarations of the stack trace functions.

This document is part of the Snap! Websites Project.

Copyright by Made to Order Software Corp.