-
Notifications
You must be signed in to change notification settings - Fork 8
/
signal_error_code.cpp
209 lines (185 loc) · 6.28 KB
/
signal_error_code.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/*
Copyright (c) 2016, Arvid Norberg
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the distribution.
* Neither the name of the author nor the names of its
contributors may be used to endorse or promote products derived
from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/
#include <system_error>
#include <string>
#include "signal_error_code.hpp"
namespace {
struct signal_error_category : std::error_category
{
const char* name() const noexcept override
{ return "signal"; }
std::string message(int ev) const noexcept override
{
#define SIGNAL_CASE(x) case sig::errors::error_code_enum:: x: return #x;
switch (ev)
{
SIGNAL_CASE(abort)
SIGNAL_CASE(alarm)
SIGNAL_CASE(arithmetic_exception)
SIGNAL_CASE(hangup)
SIGNAL_CASE(illegal)
SIGNAL_CASE(interrupt)
SIGNAL_CASE(kill)
SIGNAL_CASE(pipe)
SIGNAL_CASE(quit)
case sig::errors::error_code_enum::segmentation: return "segmentation fault";
SIGNAL_CASE(terminate)
SIGNAL_CASE(user1)
SIGNAL_CASE(user2)
SIGNAL_CASE(child)
SIGNAL_CASE(cont)
SIGNAL_CASE(stop)
SIGNAL_CASE(terminal_stop)
SIGNAL_CASE(terminal_in)
SIGNAL_CASE(terminal_out)
SIGNAL_CASE(bus)
#ifdef SIGPOLL
SIGNAL_CASE(poll)
#endif
SIGNAL_CASE(profiler)
SIGNAL_CASE(system_call)
SIGNAL_CASE(trap)
SIGNAL_CASE(urgent_data)
SIGNAL_CASE(virtual_timer)
SIGNAL_CASE(cpu_limit)
SIGNAL_CASE(file_size_limit)
default: return "unknown";
}
#undef SIGNAL_CASE
}
std::error_condition default_error_condition(int ev) const noexcept override
{ return {ev, *this}; }
};
} // anonymous namespace
namespace sig {
namespace errors {
std::error_code make_error_code(error_code_enum e)
{
return {e, sig_category()};
}
std::error_condition make_error_condition(error_code_enum e)
{
return {e, sig_category()};
}
} // namespace errors
std::error_category& sig_category()
{
static signal_error_category signal_category;
return signal_category;
}
#ifdef _WIN32
namespace {
sig::errors::error_code_enum map_exception_code(int const ev)
{
switch (ev)
{
case seh_errors::error_code_enum::access_violation:
case seh_errors::error_code_enum::array_bounds_exceeded:
case seh_errors::error_code_enum::guard_page:
case seh_errors::error_code_enum::stack_overflow:
case seh_errors::error_code_enum::flt_stack_check:
case seh_errors::error_code_enum::in_page_error:
return sig::errors::segmentation;
case seh_errors::error_code_enum::breakpoint:
case seh_errors::error_code_enum::single_step:
return sig::errors::trap;
case seh_errors::error_code_enum::datatype_misalignment:
return sig::errors::bus;
case seh_errors::error_code_enum::flt_denormal_operand:
case seh_errors::error_code_enum::flt_divide_by_zero:
case seh_errors::error_code_enum::flt_inexact_result:
case seh_errors::error_code_enum::flt_invalid_operation:
case seh_errors::error_code_enum::flt_overflow:
case seh_errors::error_code_enum::flt_underflow:
case seh_errors::error_code_enum::int_divide_by_zero:
case seh_errors::error_code_enum::int_overflow:
return sig::errors::arithmetic_exception;
case seh_errors::error_code_enum::illegal_instruction:
case seh_errors::error_code_enum::invalid_disposition:
case seh_errors::error_code_enum::priv_instruction:
case seh_errors::error_code_enum::noncontinuable_exception:
case seh_errors::error_code_enum::status_unwind_consolidate:
return sig::errors::illegal;
case seh_errors::error_code_enum::invalid_handle:
return sig::errors::pipe;
default:
return sig::errors::illegal;
}
}
struct seh_error_category : std::error_category
{
const char* name() const noexcept override
{ return "SEH"; }
std::string message(int ev) const noexcept override
{
#define SIGNAL_CASE(x) case sig::seh_errors::error_code_enum:: x: return #x;
switch (ev)
{
SIGNAL_CASE(access_violation)
SIGNAL_CASE(array_bounds_exceeded)
SIGNAL_CASE(guard_page)
SIGNAL_CASE(stack_overflow)
SIGNAL_CASE(flt_stack_check)
SIGNAL_CASE(in_page_error)
SIGNAL_CASE(breakpoint)
SIGNAL_CASE(single_step)
SIGNAL_CASE(datatype_misalignment)
SIGNAL_CASE(flt_denormal_operand)
SIGNAL_CASE(flt_divide_by_zero)
SIGNAL_CASE(flt_inexact_result)
SIGNAL_CASE(flt_invalid_operation)
SIGNAL_CASE(flt_overflow)
SIGNAL_CASE(flt_underflow)
SIGNAL_CASE(int_divide_by_zero)
SIGNAL_CASE(int_overflow)
SIGNAL_CASE(illegal_instruction)
SIGNAL_CASE(invalid_disposition)
SIGNAL_CASE(priv_instruction)
SIGNAL_CASE(noncontinuable_exception)
SIGNAL_CASE(status_unwind_consolidate)
SIGNAL_CASE(invalid_handle)
default: return "unknown";
}
#undef SIGNAL_CASE
}
std::error_condition default_error_condition(int ev) const noexcept override
{ return std::error_condition(map_exception_code(ev), sig_category()); }
};
} // anonymous namespace
namespace seh_errors {
std::error_code make_error_code(error_code_enum e)
{
return {static_cast<int>(e), seh_category()};
}
} // namespace errors
std::error_category& seh_category()
{
static seh_error_category seh_category;
return seh_category;
}
#endif
} // namespace sig