Coverage Report

Created: 2025-06-16 19:37

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