/root/doris/be/src/common/signal_handler.h
Line | Count | Source (jump to first uncovered line) |
1 | | // Copyright (c) 2008, Google Inc. |
2 | | // All rights reserved. |
3 | | // |
4 | | // Redistribution and use in source and binary forms, with or without |
5 | | // modification, are permitted provided that the following conditions are |
6 | | // met: |
7 | | // |
8 | | // * Redistributions of source code must retain the above copyright |
9 | | // notice, this list of conditions and the following disclaimer. |
10 | | // * Redistributions in binary form must reproduce the above |
11 | | // copyright notice, this list of conditions and the following disclaimer |
12 | | // in the documentation and/or other materials provided with the |
13 | | // distribution. |
14 | | // * Neither the name of Google Inc. nor the names of its |
15 | | // contributors may be used to endorse or promote products derived from |
16 | | // this software without specific prior written permission. |
17 | | // |
18 | | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
19 | | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
20 | | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
21 | | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
22 | | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
23 | | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
24 | | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
25 | | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
26 | | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
27 | | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
28 | | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
29 | | // |
30 | | // Author: Satoru Takabayashi |
31 | | // |
32 | | // Implementation of InstallFailureSignalHandler(). |
33 | | |
34 | | #pragma once |
35 | | |
36 | | #include <glog/logging.h> |
37 | | #include <gutil/macros.h> |
38 | | |
39 | | #include <boost/stacktrace.hpp> |
40 | | #include <csignal> |
41 | | #include <ctime> |
42 | | |
43 | | #include "common/version_internal.h" |
44 | | #ifdef HAVE_UCONTEXT_H |
45 | | #include <ucontext.h> |
46 | | #endif |
47 | | #ifdef HAVE_SYS_UCONTEXT_H |
48 | | #include <sys/ucontext.h> |
49 | | #endif |
50 | | #include <algorithm> |
51 | | |
52 | | namespace doris::signal { |
53 | | |
54 | | inline thread_local uint64 query_id_hi; |
55 | | inline thread_local uint64 query_id_lo; |
56 | | inline thread_local int64_t tablet_id = 0; |
57 | | inline thread_local bool is_nereids = false; |
58 | | |
59 | | namespace { |
60 | | |
61 | | // We'll install the failure signal handler for these signals. We could |
62 | | // use strsignal() to get signal names, but we don't use it to avoid |
63 | | // introducing yet another #ifdef complication. |
64 | | // |
65 | | // The list should be synced with the comment in signalhandler.h. |
66 | | const struct { |
67 | | int number; |
68 | | const char* name = nullptr; |
69 | | } kFailureSignals[] = { |
70 | | {SIGSEGV, "SIGSEGV"}, {SIGILL, "SIGILL"}, {SIGFPE, "SIGFPE"}, |
71 | | {SIGABRT, "SIGABRT"}, {SIGBUS, "SIGBUS"}, {SIGTERM, "SIGTERM"}, |
72 | | }; |
73 | | |
74 | | static bool kFailureSignalHandlerInstalled = false; |
75 | | |
76 | | /* |
77 | | * Copyright (c) Meta Platforms, Inc. and affiliates. |
78 | | * |
79 | | * Licensed under the Apache License, Version 2.0 (the "License"); |
80 | | * you may not use this file except in compliance with the License. |
81 | | * You may obtain a copy of the License at |
82 | | * |
83 | | * http://www.apache.org/licenses/LICENSE-2.0 |
84 | | * |
85 | | * Unless required by applicable law or agreed to in writing, software |
86 | | * distributed under the License is distributed on an "AS IS" BASIS, |
87 | | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
88 | | * See the License for the specific language governing permissions and |
89 | | * limitations under the License. |
90 | | * These signal explainer is copied from Meta's Folly |
91 | | */ |
92 | 0 | const char* sigill_reason(int si_code) { |
93 | 0 | switch (si_code) { |
94 | 0 | case ILL_ILLOPC: |
95 | 0 | return "illegal opcode"; |
96 | 0 | case ILL_ILLOPN: |
97 | 0 | return "illegal operand"; |
98 | 0 | case ILL_ILLADR: |
99 | 0 | return "illegal addressing mode"; |
100 | 0 | case ILL_ILLTRP: |
101 | 0 | return "illegal trap"; |
102 | 0 | case ILL_PRVOPC: |
103 | 0 | return "privileged opcode"; |
104 | 0 | case ILL_PRVREG: |
105 | 0 | return "privileged register"; |
106 | 0 | case ILL_COPROC: |
107 | 0 | return "coprocessor error"; |
108 | 0 | case ILL_BADSTK: |
109 | 0 | return "internal stack error"; |
110 | 0 |
|
111 | 0 | default: |
112 | 0 | return nullptr; |
113 | 0 | } |
114 | 0 | } Unexecuted instantiation: memtable_flush_executor.cpp:_ZN5doris6signal12_GLOBAL__N_113sigill_reasonEi Unexecuted instantiation: schema_change.cpp:_ZN5doris6signal12_GLOBAL__N_113sigill_reasonEi Unexecuted instantiation: tablet.cpp:_ZN5doris6signal12_GLOBAL__N_113sigill_reasonEi Unexecuted instantiation: load_stream.cpp:_ZN5doris6signal12_GLOBAL__N_113sigill_reasonEi Unexecuted instantiation: internal_service.cpp:_ZN5doris6signal12_GLOBAL__N_113sigill_reasonEi Unexecuted instantiation: vtablet_writer_v2.cpp:_ZN5doris6signal12_GLOBAL__N_113sigill_reasonEi Unexecuted instantiation: vtablet_writer.cpp:_ZN5doris6signal12_GLOBAL__N_113sigill_reasonEi |
115 | | |
116 | 0 | const char* sigfpe_reason(int si_code) { |
117 | 0 | switch (si_code) { |
118 | 0 | case FPE_INTDIV: |
119 | 0 | return "integer divide by zero"; |
120 | 0 | case FPE_INTOVF: |
121 | 0 | return "integer overflow"; |
122 | 0 | case FPE_FLTDIV: |
123 | 0 | return "floating-point divide by zero"; |
124 | 0 | case FPE_FLTOVF: |
125 | 0 | return "floating-point overflow"; |
126 | 0 | case FPE_FLTUND: |
127 | 0 | return "floating-point underflow"; |
128 | 0 | case FPE_FLTRES: |
129 | 0 | return "floating-point inexact result"; |
130 | 0 | case FPE_FLTINV: |
131 | 0 | return "floating-point invalid operation"; |
132 | 0 | case FPE_FLTSUB: |
133 | 0 | return "subscript out of range"; |
134 | 0 |
|
135 | 0 | default: |
136 | 0 | return nullptr; |
137 | 0 | } |
138 | 0 | } Unexecuted instantiation: memtable_flush_executor.cpp:_ZN5doris6signal12_GLOBAL__N_113sigfpe_reasonEi Unexecuted instantiation: schema_change.cpp:_ZN5doris6signal12_GLOBAL__N_113sigfpe_reasonEi Unexecuted instantiation: tablet.cpp:_ZN5doris6signal12_GLOBAL__N_113sigfpe_reasonEi Unexecuted instantiation: load_stream.cpp:_ZN5doris6signal12_GLOBAL__N_113sigfpe_reasonEi Unexecuted instantiation: internal_service.cpp:_ZN5doris6signal12_GLOBAL__N_113sigfpe_reasonEi Unexecuted instantiation: vtablet_writer_v2.cpp:_ZN5doris6signal12_GLOBAL__N_113sigfpe_reasonEi Unexecuted instantiation: vtablet_writer.cpp:_ZN5doris6signal12_GLOBAL__N_113sigfpe_reasonEi |
139 | | |
140 | 0 | const char* sigsegv_reason(int si_code) { |
141 | 0 | switch (si_code) { |
142 | 0 | case SEGV_MAPERR: |
143 | 0 | return "address not mapped to object"; |
144 | 0 | case SEGV_ACCERR: |
145 | 0 | return "invalid permissions for mapped object"; |
146 | 0 |
|
147 | 0 | default: |
148 | 0 | return nullptr; |
149 | 0 | } |
150 | 0 | } Unexecuted instantiation: memtable_flush_executor.cpp:_ZN5doris6signal12_GLOBAL__N_114sigsegv_reasonEi Unexecuted instantiation: schema_change.cpp:_ZN5doris6signal12_GLOBAL__N_114sigsegv_reasonEi Unexecuted instantiation: tablet.cpp:_ZN5doris6signal12_GLOBAL__N_114sigsegv_reasonEi Unexecuted instantiation: load_stream.cpp:_ZN5doris6signal12_GLOBAL__N_114sigsegv_reasonEi Unexecuted instantiation: internal_service.cpp:_ZN5doris6signal12_GLOBAL__N_114sigsegv_reasonEi Unexecuted instantiation: vtablet_writer_v2.cpp:_ZN5doris6signal12_GLOBAL__N_114sigsegv_reasonEi Unexecuted instantiation: vtablet_writer.cpp:_ZN5doris6signal12_GLOBAL__N_114sigsegv_reasonEi |
151 | | |
152 | 0 | const char* sigbus_reason(int si_code) { |
153 | 0 | switch (si_code) { |
154 | 0 | case BUS_ADRALN: |
155 | 0 | return "invalid address alignment"; |
156 | 0 | case BUS_ADRERR: |
157 | 0 | return "nonexistent physical address"; |
158 | 0 | case BUS_OBJERR: |
159 | 0 | return "object-specific hardware error"; |
160 | 0 |
|
161 | 0 | // MCEERR_AR and MCEERR_AO: in sigaction(2) but not in headers. |
162 | 0 |
|
163 | 0 | default: |
164 | 0 | return nullptr; |
165 | 0 | } |
166 | 0 | } Unexecuted instantiation: memtable_flush_executor.cpp:_ZN5doris6signal12_GLOBAL__N_113sigbus_reasonEi Unexecuted instantiation: schema_change.cpp:_ZN5doris6signal12_GLOBAL__N_113sigbus_reasonEi Unexecuted instantiation: tablet.cpp:_ZN5doris6signal12_GLOBAL__N_113sigbus_reasonEi Unexecuted instantiation: load_stream.cpp:_ZN5doris6signal12_GLOBAL__N_113sigbus_reasonEi Unexecuted instantiation: internal_service.cpp:_ZN5doris6signal12_GLOBAL__N_113sigbus_reasonEi Unexecuted instantiation: vtablet_writer_v2.cpp:_ZN5doris6signal12_GLOBAL__N_113sigbus_reasonEi Unexecuted instantiation: vtablet_writer.cpp:_ZN5doris6signal12_GLOBAL__N_113sigbus_reasonEi |
167 | | |
168 | 0 | const char* signal_reason(int signum, int si_code) { |
169 | 0 | switch (signum) { |
170 | 0 | case SIGILL: |
171 | 0 | return sigill_reason(si_code); |
172 | 0 | case SIGFPE: |
173 | 0 | return sigfpe_reason(si_code); |
174 | 0 | case SIGSEGV: |
175 | 0 | return sigsegv_reason(si_code); |
176 | 0 | case SIGBUS: |
177 | 0 | return sigbus_reason(si_code); |
178 | 0 | default: |
179 | 0 | return nullptr; |
180 | 0 | } |
181 | 0 | } Unexecuted instantiation: memtable_flush_executor.cpp:_ZN5doris6signal12_GLOBAL__N_113signal_reasonEii Unexecuted instantiation: schema_change.cpp:_ZN5doris6signal12_GLOBAL__N_113signal_reasonEii Unexecuted instantiation: tablet.cpp:_ZN5doris6signal12_GLOBAL__N_113signal_reasonEii Unexecuted instantiation: load_stream.cpp:_ZN5doris6signal12_GLOBAL__N_113signal_reasonEii Unexecuted instantiation: internal_service.cpp:_ZN5doris6signal12_GLOBAL__N_113signal_reasonEii Unexecuted instantiation: vtablet_writer_v2.cpp:_ZN5doris6signal12_GLOBAL__N_113signal_reasonEii Unexecuted instantiation: vtablet_writer.cpp:_ZN5doris6signal12_GLOBAL__N_113signal_reasonEii |
182 | | |
183 | | // The class is used for formatting error messages. We don't use printf() |
184 | | // as it's not async signal safe. |
185 | | class MinimalFormatter { |
186 | | public: |
187 | | MinimalFormatter(char* buffer, size_t size) |
188 | 0 | : buffer_(buffer), cursor_(buffer), end_(buffer + size) {} Unexecuted instantiation: memtable_flush_executor.cpp:_ZN5doris6signal12_GLOBAL__N_116MinimalFormatterC2EPcm Unexecuted instantiation: schema_change.cpp:_ZN5doris6signal12_GLOBAL__N_116MinimalFormatterC2EPcm Unexecuted instantiation: tablet.cpp:_ZN5doris6signal12_GLOBAL__N_116MinimalFormatterC2EPcm Unexecuted instantiation: load_stream.cpp:_ZN5doris6signal12_GLOBAL__N_116MinimalFormatterC2EPcm Unexecuted instantiation: internal_service.cpp:_ZN5doris6signal12_GLOBAL__N_116MinimalFormatterC2EPcm Unexecuted instantiation: vtablet_writer_v2.cpp:_ZN5doris6signal12_GLOBAL__N_116MinimalFormatterC2EPcm Unexecuted instantiation: vtablet_writer.cpp:_ZN5doris6signal12_GLOBAL__N_116MinimalFormatterC2EPcm |
189 | | |
190 | | // Returns the number of bytes written in the buffer. |
191 | 0 | std::size_t num_bytes_written() const { return static_cast<std::size_t>(cursor_ - buffer_); } Unexecuted instantiation: memtable_flush_executor.cpp:_ZNK5doris6signal12_GLOBAL__N_116MinimalFormatter17num_bytes_writtenEv Unexecuted instantiation: schema_change.cpp:_ZNK5doris6signal12_GLOBAL__N_116MinimalFormatter17num_bytes_writtenEv Unexecuted instantiation: tablet.cpp:_ZNK5doris6signal12_GLOBAL__N_116MinimalFormatter17num_bytes_writtenEv Unexecuted instantiation: load_stream.cpp:_ZNK5doris6signal12_GLOBAL__N_116MinimalFormatter17num_bytes_writtenEv Unexecuted instantiation: internal_service.cpp:_ZNK5doris6signal12_GLOBAL__N_116MinimalFormatter17num_bytes_writtenEv Unexecuted instantiation: vtablet_writer_v2.cpp:_ZNK5doris6signal12_GLOBAL__N_116MinimalFormatter17num_bytes_writtenEv Unexecuted instantiation: vtablet_writer.cpp:_ZNK5doris6signal12_GLOBAL__N_116MinimalFormatter17num_bytes_writtenEv |
192 | | |
193 | | // Appends string from "str" and updates the internal cursor. |
194 | 0 | void AppendString(const char* str) { |
195 | 0 | ptrdiff_t i = 0; |
196 | 0 | while (str[i] != '\0' && cursor_ + i < end_) { |
197 | 0 | cursor_[i] = str[i]; |
198 | 0 | ++i; |
199 | 0 | } |
200 | 0 | cursor_ += i; |
201 | 0 | } Unexecuted instantiation: memtable_flush_executor.cpp:_ZN5doris6signal12_GLOBAL__N_116MinimalFormatter12AppendStringEPKc Unexecuted instantiation: schema_change.cpp:_ZN5doris6signal12_GLOBAL__N_116MinimalFormatter12AppendStringEPKc Unexecuted instantiation: tablet.cpp:_ZN5doris6signal12_GLOBAL__N_116MinimalFormatter12AppendStringEPKc Unexecuted instantiation: load_stream.cpp:_ZN5doris6signal12_GLOBAL__N_116MinimalFormatter12AppendStringEPKc Unexecuted instantiation: internal_service.cpp:_ZN5doris6signal12_GLOBAL__N_116MinimalFormatter12AppendStringEPKc Unexecuted instantiation: vtablet_writer_v2.cpp:_ZN5doris6signal12_GLOBAL__N_116MinimalFormatter12AppendStringEPKc Unexecuted instantiation: vtablet_writer.cpp:_ZN5doris6signal12_GLOBAL__N_116MinimalFormatter12AppendStringEPKc |
202 | | |
203 | | // Formats "number" in "radix" and updates the internal cursor. |
204 | | // Lowercase letters are used for 'a' - 'z'. |
205 | 0 | void AppendUint64(uint64 number, unsigned radix) { |
206 | 0 | unsigned i = 0; |
207 | 0 | while (cursor_ + i < end_) { |
208 | 0 | const uint64 tmp = number % radix; |
209 | 0 | number /= radix; |
210 | 0 | cursor_[i] = static_cast<char>(tmp < 10 ? '0' + tmp : 'a' + tmp - 10); |
211 | 0 | ++i; |
212 | 0 | if (number == 0) { |
213 | 0 | break; |
214 | 0 | } |
215 | 0 | } |
216 | 0 | // Reverse the bytes written. |
217 | 0 | std::reverse(cursor_, cursor_ + i); |
218 | 0 | cursor_ += i; |
219 | 0 | } Unexecuted instantiation: memtable_flush_executor.cpp:_ZN5doris6signal12_GLOBAL__N_116MinimalFormatter12AppendUint64Emj Unexecuted instantiation: schema_change.cpp:_ZN5doris6signal12_GLOBAL__N_116MinimalFormatter12AppendUint64Emj Unexecuted instantiation: tablet.cpp:_ZN5doris6signal12_GLOBAL__N_116MinimalFormatter12AppendUint64Emj Unexecuted instantiation: load_stream.cpp:_ZN5doris6signal12_GLOBAL__N_116MinimalFormatter12AppendUint64Emj Unexecuted instantiation: internal_service.cpp:_ZN5doris6signal12_GLOBAL__N_116MinimalFormatter12AppendUint64Emj Unexecuted instantiation: vtablet_writer_v2.cpp:_ZN5doris6signal12_GLOBAL__N_116MinimalFormatter12AppendUint64Emj Unexecuted instantiation: vtablet_writer.cpp:_ZN5doris6signal12_GLOBAL__N_116MinimalFormatter12AppendUint64Emj |
220 | | |
221 | | private: |
222 | | char* buffer_ = nullptr; |
223 | | char* cursor_ = nullptr; |
224 | | const char* const end_; |
225 | | }; |
226 | | |
227 | | // Writes the given data with the size to the standard error. |
228 | 0 | void WriteToStderr(const char* data, size_t size) { |
229 | 0 | if (write(STDERR_FILENO, data, size) < 0) { |
230 | 0 | // Ignore errors. |
231 | 0 | } |
232 | 0 | } Unexecuted instantiation: memtable_flush_executor.cpp:_ZN5doris6signal12_GLOBAL__N_113WriteToStderrEPKcm Unexecuted instantiation: schema_change.cpp:_ZN5doris6signal12_GLOBAL__N_113WriteToStderrEPKcm Unexecuted instantiation: tablet.cpp:_ZN5doris6signal12_GLOBAL__N_113WriteToStderrEPKcm Unexecuted instantiation: load_stream.cpp:_ZN5doris6signal12_GLOBAL__N_113WriteToStderrEPKcm Unexecuted instantiation: internal_service.cpp:_ZN5doris6signal12_GLOBAL__N_113WriteToStderrEPKcm Unexecuted instantiation: vtablet_writer_v2.cpp:_ZN5doris6signal12_GLOBAL__N_113WriteToStderrEPKcm Unexecuted instantiation: vtablet_writer.cpp:_ZN5doris6signal12_GLOBAL__N_113WriteToStderrEPKcm |
233 | | |
234 | | // The writer function can be changed by InstallFailureWriter(). |
235 | | void (*g_failure_writer)(const char* data, size_t size) = WriteToStderr; |
236 | | |
237 | | // Dumps time information. We don't dump human-readable time information |
238 | | // as localtime() is not guaranteed to be async signal safe. |
239 | 0 | void DumpTimeInfo() { |
240 | 0 | time_t time_in_sec = time(nullptr); |
241 | 0 | char buf[256]; // Big enough for time info. |
242 | 0 | MinimalFormatter formatter(buf, sizeof(buf)); |
243 | 0 | formatter.AppendString("*** Query id: "); |
244 | 0 | formatter.AppendUint64(query_id_hi, 16); |
245 | 0 | formatter.AppendString("-"); |
246 | 0 | formatter.AppendUint64(query_id_lo, 16); |
247 | 0 | formatter.AppendString(" ***\n"); |
248 | 0 | formatter.AppendString("*** is nereids: "); |
249 | 0 | formatter.AppendUint64(is_nereids, 10); |
250 | 0 | formatter.AppendString(" ***\n"); |
251 | 0 | formatter.AppendString("*** tablet id: "); |
252 | 0 | formatter.AppendUint64(tablet_id, 10); |
253 | 0 | formatter.AppendString(" ***\n"); |
254 | 0 | formatter.AppendString("*** Aborted at "); |
255 | 0 | formatter.AppendUint64(static_cast<uint64>(time_in_sec), 10); |
256 | 0 | formatter.AppendString(" (unix time)"); |
257 | 0 | formatter.AppendString(" try \"date -d @"); |
258 | 0 | formatter.AppendUint64(static_cast<uint64>(time_in_sec), 10); |
259 | 0 | formatter.AppendString("\" if you are using GNU date ***\n"); |
260 | 0 | formatter.AppendString("*** Current BE git commitID: "); |
261 | 0 | formatter.AppendString(version::doris_build_short_hash()); |
262 | 0 | formatter.AppendString(" ***\n"); |
263 | 0 | g_failure_writer(buf, formatter.num_bytes_written()); |
264 | 0 | } Unexecuted instantiation: memtable_flush_executor.cpp:_ZN5doris6signal12_GLOBAL__N_112DumpTimeInfoEv Unexecuted instantiation: schema_change.cpp:_ZN5doris6signal12_GLOBAL__N_112DumpTimeInfoEv Unexecuted instantiation: tablet.cpp:_ZN5doris6signal12_GLOBAL__N_112DumpTimeInfoEv Unexecuted instantiation: load_stream.cpp:_ZN5doris6signal12_GLOBAL__N_112DumpTimeInfoEv Unexecuted instantiation: internal_service.cpp:_ZN5doris6signal12_GLOBAL__N_112DumpTimeInfoEv Unexecuted instantiation: vtablet_writer_v2.cpp:_ZN5doris6signal12_GLOBAL__N_112DumpTimeInfoEv Unexecuted instantiation: vtablet_writer.cpp:_ZN5doris6signal12_GLOBAL__N_112DumpTimeInfoEv |
265 | | |
266 | | // Dumps information about the signal to STDERR. |
267 | 0 | void DumpSignalInfo(int signal_number, siginfo_t* siginfo) { |
268 | 0 | // Get the signal name. |
269 | 0 | const char* signal_name = nullptr; |
270 | 0 | for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kFailureSignals); ++i) { |
271 | 0 | if (signal_number == kFailureSignals[i].number) { |
272 | 0 | signal_name = kFailureSignals[i].name; |
273 | 0 | } |
274 | 0 | } |
275 | 0 |
|
276 | 0 | char buf[256]; // Big enough for signal info. |
277 | 0 | MinimalFormatter formatter(buf, sizeof(buf)); |
278 | 0 |
|
279 | 0 | formatter.AppendString("*** "); |
280 | 0 | if (signal_name) { |
281 | 0 | formatter.AppendString(signal_name); |
282 | 0 | } else { |
283 | 0 | // Use the signal number if the name is unknown. The signal name |
284 | 0 | // should be known, but just in case. |
285 | 0 | formatter.AppendString("Signal "); |
286 | 0 | formatter.AppendUint64(static_cast<uint64>(signal_number), 10); |
287 | 0 | } |
288 | 0 | formatter.AppendString(" "); |
289 | 0 | // Detail reason explain |
290 | 0 | auto reason = signal_reason(signal_number, siginfo->si_code); |
291 | 0 |
|
292 | 0 | // If we can't find a reason code make a best effort to print the (int) code. |
293 | 0 | if (reason != nullptr) { |
294 | 0 | formatter.AppendString(reason); |
295 | 0 | } else { |
296 | 0 | formatter.AppendString("unknown detail explain"); |
297 | 0 | } |
298 | 0 | formatter.AppendString(" (@0x"); |
299 | 0 | formatter.AppendUint64(reinterpret_cast<uintptr_t>(siginfo->si_addr), 16); |
300 | 0 | formatter.AppendString(")"); |
301 | 0 | formatter.AppendString(" received by PID "); |
302 | 0 | formatter.AppendUint64(static_cast<uint64>(getpid()), 10); |
303 | 0 | formatter.AppendString(" (TID "); |
304 | 0 | uint64_t tid; |
305 | 0 | #ifdef __APPLE__ |
306 | 0 | pthread_threadid_np(nullptr, &tid); |
307 | 0 | #else |
308 | 0 | tid = syscall(SYS_gettid); |
309 | 0 | #endif |
310 | 0 | formatter.AppendUint64(tid, 10); |
311 | 0 | formatter.AppendString(" OR 0x"); |
312 | 0 | // We assume pthread_t is an integral number or a pointer, rather |
313 | 0 | // than a complex struct. In some environments, pthread_self() |
314 | 0 | // returns an uint64 but in some other environments pthread_self() |
315 | 0 | // returns a pointer. |
316 | 0 | pthread_t id = pthread_self(); |
317 | 0 | formatter.AppendUint64(reinterpret_cast<uint64>(reinterpret_cast<const char*>(id)), 16); |
318 | 0 | formatter.AppendString(") "); |
319 | 0 | // Only linux has the PID of the signal sender in si_pid. |
320 | 0 | formatter.AppendString("from PID "); |
321 | 0 | formatter.AppendUint64(static_cast<uint64>(siginfo->si_pid), 10); |
322 | 0 | formatter.AppendString("; "); |
323 | 0 | formatter.AppendString("stack trace: ***\n"); |
324 | 0 | g_failure_writer(buf, formatter.num_bytes_written()); |
325 | 0 | } Unexecuted instantiation: memtable_flush_executor.cpp:_ZN5doris6signal12_GLOBAL__N_114DumpSignalInfoEiP9siginfo_t Unexecuted instantiation: schema_change.cpp:_ZN5doris6signal12_GLOBAL__N_114DumpSignalInfoEiP9siginfo_t Unexecuted instantiation: tablet.cpp:_ZN5doris6signal12_GLOBAL__N_114DumpSignalInfoEiP9siginfo_t Unexecuted instantiation: load_stream.cpp:_ZN5doris6signal12_GLOBAL__N_114DumpSignalInfoEiP9siginfo_t Unexecuted instantiation: internal_service.cpp:_ZN5doris6signal12_GLOBAL__N_114DumpSignalInfoEiP9siginfo_t Unexecuted instantiation: vtablet_writer_v2.cpp:_ZN5doris6signal12_GLOBAL__N_114DumpSignalInfoEiP9siginfo_t Unexecuted instantiation: vtablet_writer.cpp:_ZN5doris6signal12_GLOBAL__N_114DumpSignalInfoEiP9siginfo_t |
326 | | |
327 | | // Invoke the default signal handler. |
328 | 0 | void InvokeDefaultSignalHandler(int signal_number) { |
329 | 0 | struct sigaction sig_action; |
330 | 0 | memset(&sig_action, 0, sizeof(sig_action)); |
331 | 0 | sigemptyset(&sig_action.sa_mask); |
332 | 0 | sig_action.sa_handler = SIG_DFL; |
333 | 0 | sigaction(signal_number, &sig_action, nullptr); |
334 | 0 | kill(getpid(), signal_number); |
335 | 0 | } Unexecuted instantiation: memtable_flush_executor.cpp:_ZN5doris6signal12_GLOBAL__N_126InvokeDefaultSignalHandlerEi Unexecuted instantiation: schema_change.cpp:_ZN5doris6signal12_GLOBAL__N_126InvokeDefaultSignalHandlerEi Unexecuted instantiation: tablet.cpp:_ZN5doris6signal12_GLOBAL__N_126InvokeDefaultSignalHandlerEi Unexecuted instantiation: load_stream.cpp:_ZN5doris6signal12_GLOBAL__N_126InvokeDefaultSignalHandlerEi Unexecuted instantiation: internal_service.cpp:_ZN5doris6signal12_GLOBAL__N_126InvokeDefaultSignalHandlerEi Unexecuted instantiation: vtablet_writer_v2.cpp:_ZN5doris6signal12_GLOBAL__N_126InvokeDefaultSignalHandlerEi Unexecuted instantiation: vtablet_writer.cpp:_ZN5doris6signal12_GLOBAL__N_126InvokeDefaultSignalHandlerEi |
336 | | |
337 | | // This variable is used for protecting FailureSignalHandler() from |
338 | | // dumping stuff while another thread is doing it. Our policy is to let |
339 | | // the first thread dump stuff and let other threads wait. |
340 | | // See also comments in FailureSignalHandler(). |
341 | | static pthread_t* g_entered_thread_id_pointer = nullptr; |
342 | | |
343 | | // Wrapper of __sync_val_compare_and_swap. If the GCC extension isn't |
344 | | // defined, we try the CPU specific logics (we only support x86 and |
345 | | // x86_64 for now) first, then use a naive implementation, which has a |
346 | | // race condition. |
347 | | template <typename T> |
348 | 0 | T sync_val_compare_and_swap(T* ptr, T oldval, T newval) { |
349 | 0 | #if defined(HAVE___SYNC_VAL_COMPARE_AND_SWAP) |
350 | 0 | return __sync_val_compare_and_swap(ptr, oldval, newval); |
351 | 0 | #elif defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__)) |
352 | 0 | T ret; |
353 | 0 | __asm__ __volatile__("lock; cmpxchg %1, (%2);" |
354 | 0 | : "=a"(ret) |
355 | 0 | // GCC may produces %sil or %dil for |
356 | 0 | // constraint "r", but some of apple's gas |
357 | 0 | // dosn't know the 8 bit registers. |
358 | 0 | // We use "q" to avoid these registers. |
359 | 0 | : "q"(newval), "q"(ptr), "a"(oldval) |
360 | 0 | : "memory", "cc"); |
361 | 0 | return ret; |
362 | 0 | #else |
363 | 0 | T ret = *ptr; |
364 | 0 | if (ret == oldval) { |
365 | 0 | *ptr = newval; |
366 | 0 | } |
367 | 0 | return ret; |
368 | 0 | #endif |
369 | 0 | } Unexecuted instantiation: memtable_flush_executor.cpp:_ZN5doris6signal12_GLOBAL__N_125sync_val_compare_and_swapIPmEET_PS4_S4_S4_ Unexecuted instantiation: schema_change.cpp:_ZN5doris6signal12_GLOBAL__N_125sync_val_compare_and_swapIPmEET_PS4_S4_S4_ Unexecuted instantiation: tablet.cpp:_ZN5doris6signal12_GLOBAL__N_125sync_val_compare_and_swapIPmEET_PS4_S4_S4_ Unexecuted instantiation: load_stream.cpp:_ZN5doris6signal12_GLOBAL__N_125sync_val_compare_and_swapIPmEET_PS4_S4_S4_ Unexecuted instantiation: internal_service.cpp:_ZN5doris6signal12_GLOBAL__N_125sync_val_compare_and_swapIPmEET_PS4_S4_S4_ Unexecuted instantiation: vtablet_writer_v2.cpp:_ZN5doris6signal12_GLOBAL__N_125sync_val_compare_and_swapIPmEET_PS4_S4_S4_ Unexecuted instantiation: vtablet_writer.cpp:_ZN5doris6signal12_GLOBAL__N_125sync_val_compare_and_swapIPmEET_PS4_S4_S4_ |
370 | | |
371 | | // Dumps signal and stack frame information, and invokes the default |
372 | | // signal handler once our job is done. |
373 | 0 | void FailureSignalHandler(int signal_number, siginfo_t* signal_info, void* ucontext) { |
374 | 0 | // First check if we've already entered the function. We use an atomic |
375 | 0 | // compare and swap operation for platforms that support it. For other |
376 | 0 | // platforms, we use a naive method that could lead to a subtle race. |
377 | 0 |
|
378 | 0 | // We assume pthread_self() is async signal safe, though it's not |
379 | 0 | // officially guaranteed. |
380 | 0 | pthread_t my_thread_id = pthread_self(); |
381 | 0 | // NOTE: We could simply use pthread_t rather than pthread_t* for this, |
382 | 0 | // if pthread_self() is guaranteed to return non-zero value for thread |
383 | 0 | // ids, but there is no such guarantee. We need to distinguish if the |
384 | 0 | // old value (value returned from __sync_val_compare_and_swap) is |
385 | 0 | // different from the original value (in this case NULL). |
386 | 0 | pthread_t* old_thread_id_pointer = sync_val_compare_and_swap( |
387 | 0 | &g_entered_thread_id_pointer, static_cast<pthread_t*>(nullptr), &my_thread_id); |
388 | 0 | if (old_thread_id_pointer != nullptr) { |
389 | 0 | // We've already entered the signal handler. What should we do? |
390 | 0 | if (pthread_equal(my_thread_id, *g_entered_thread_id_pointer)) { |
391 | 0 | // It looks the current thread is reentering the signal handler. |
392 | 0 | // Something must be going wrong (maybe we are reentering by another |
393 | 0 | // type of signal?). Kill ourself by the default signal handler. |
394 | 0 | InvokeDefaultSignalHandler(signal_number); |
395 | 0 | } |
396 | 0 | // Another thread is dumping stuff. Let's wait until that thread |
397 | 0 | // finishes the job and kills the process. |
398 | 0 | while (true) { |
399 | 0 | sleep(1); |
400 | 0 | } |
401 | 0 | } |
402 | 0 | // This is the first time we enter the signal handler. We are going to |
403 | 0 | // do some interesting stuff from here. |
404 | 0 | // TODO(satorux): We might want to set timeout here using alarm(), but |
405 | 0 | // mixing alarm() and sleep() can be a bad idea. |
406 | 0 |
|
407 | 0 | // First dump time info. |
408 | 0 | DumpTimeInfo(); |
409 | 0 | DumpSignalInfo(signal_number, signal_info); |
410 | 0 |
|
411 | 0 | // *** TRANSITION *** |
412 | 0 | // |
413 | 0 | // BEFORE this point, all code must be async-termination-safe! |
414 | 0 | // (See WARNING above.) |
415 | 0 | // |
416 | 0 | // AFTER this point, we do unsafe things, like using LOG()! |
417 | 0 | // The process could be terminated or hung at any time. We try to |
418 | 0 | // do more useful things first and riskier things later. |
419 | 0 |
|
420 | 0 | // Use boost stacktrace to print more detail info |
421 | 0 | std::cout << boost::stacktrace::stacktrace() << std::endl; |
422 | 0 |
|
423 | 0 | // Flush the logs before we do anything in case 'anything' |
424 | 0 | // causes problems. |
425 | 0 | google::FlushLogFilesUnsafe(0); |
426 | 0 |
|
427 | 0 | // Kill ourself by the default signal handler. |
428 | 0 | InvokeDefaultSignalHandler(signal_number); |
429 | 0 | } Unexecuted instantiation: memtable_flush_executor.cpp:_ZN5doris6signal12_GLOBAL__N_120FailureSignalHandlerEiP9siginfo_tPv Unexecuted instantiation: schema_change.cpp:_ZN5doris6signal12_GLOBAL__N_120FailureSignalHandlerEiP9siginfo_tPv Unexecuted instantiation: tablet.cpp:_ZN5doris6signal12_GLOBAL__N_120FailureSignalHandlerEiP9siginfo_tPv Unexecuted instantiation: load_stream.cpp:_ZN5doris6signal12_GLOBAL__N_120FailureSignalHandlerEiP9siginfo_tPv Unexecuted instantiation: internal_service.cpp:_ZN5doris6signal12_GLOBAL__N_120FailureSignalHandlerEiP9siginfo_tPv Unexecuted instantiation: vtablet_writer_v2.cpp:_ZN5doris6signal12_GLOBAL__N_120FailureSignalHandlerEiP9siginfo_tPv Unexecuted instantiation: vtablet_writer.cpp:_ZN5doris6signal12_GLOBAL__N_120FailureSignalHandlerEiP9siginfo_tPv |
430 | | |
431 | | } // namespace |
432 | | |
433 | 64 | inline void set_signal_task_id(PUniqueId tid) { |
434 | 64 | query_id_hi = tid.hi(); |
435 | 64 | query_id_lo = tid.lo(); |
436 | 64 | } |
437 | | |
438 | 0 | inline void set_signal_task_id(TUniqueId tid) { |
439 | 0 | query_id_hi = tid.hi; |
440 | 0 | query_id_lo = tid.lo; |
441 | 0 | } |
442 | | |
443 | 0 | inline void set_signal_is_nereids(bool is_nereids_arg) { |
444 | 0 | is_nereids = is_nereids_arg; |
445 | 0 | } |
446 | | |
447 | 0 | inline void InstallFailureSignalHandler() { |
448 | 0 | // Build the sigaction struct. |
449 | 0 | struct sigaction sig_action; |
450 | 0 | memset(&sig_action, 0, sizeof(sig_action)); |
451 | 0 | sigemptyset(&sig_action.sa_mask); |
452 | 0 | sig_action.sa_flags |= SA_SIGINFO; |
453 | 0 | sig_action.sa_sigaction = &FailureSignalHandler; |
454 | 0 |
|
455 | 0 | for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kFailureSignals); ++i) { |
456 | 0 | CHECK_ERR(sigaction(kFailureSignals[i].number, &sig_action, nullptr)); |
457 | 0 | } |
458 | 0 | kFailureSignalHandlerInstalled = true; |
459 | 0 | } |
460 | | |
461 | | } // namespace doris::signal |