|
| 1 | +/* |
| 2 | + * Copyright 2024 Redpanda Data, Inc. |
| 3 | + * |
| 4 | + * Use of this software is governed by the Business Source License |
| 5 | + * included in the file licenses/BSL.md |
| 6 | + * |
| 7 | + * As of the Change Date specified in that file, in accordance with |
| 8 | + * the Business Source License, use of this software will be governed |
| 9 | + * by the Apache License, Version 2.0 |
| 10 | + */ |
| 11 | + |
| 12 | +#include "crash_tracker/api.h" |
| 13 | + |
| 14 | +#include "base/vassert.h" |
| 15 | +#include "config/node_config.h" |
| 16 | +#include "crash_tracker/logger.h" |
| 17 | +#include "crash_tracker/service.h" |
| 18 | +#include "hashing/xx.h" |
| 19 | +#include "serde/rw/envelope.h" |
| 20 | +#include "utils/file_io.h" |
| 21 | + |
| 22 | +#include <seastar/core/memory.hh> |
| 23 | +#include <seastar/util/print_safe.hh> |
| 24 | + |
| 25 | +#include <fmt/core.h> |
| 26 | + |
| 27 | +#include <chrono> |
| 28 | +#include <iterator> |
| 29 | +#include <stdio.h> |
| 30 | + |
| 31 | +using namespace std::chrono_literals; |
| 32 | + |
| 33 | +namespace crash_tracker { |
| 34 | + |
| 35 | +namespace { |
| 36 | + |
| 37 | +service& instance() { |
| 38 | + static service inst; |
| 39 | + return inst; |
| 40 | +} |
| 41 | + |
| 42 | +} // namespace |
| 43 | + |
| 44 | +ss::future<> initialize() { return instance().start(); } |
| 45 | + |
| 46 | +ss::future<> record_clean_shutdown() { return instance().stop(); } |
| 47 | + |
| 48 | +namespace { |
| 49 | +std::string_view failure_msg_prefix(int signo) { |
| 50 | + if (signo == SIGSEGV) { |
| 51 | + return "Segmentation fault"; |
| 52 | + } else if (signo == SIGILL) { |
| 53 | + return "Illegal instruction"; |
| 54 | + } else { |
| 55 | + return "Aborting"; |
| 56 | + } |
| 57 | +} |
| 58 | +} // namespace |
| 59 | + |
| 60 | +void record_signal_crash(crash_description& cd, int signo) { |
| 61 | + auto& format_buf = cd._crash_reason; |
| 62 | + fmt::format_to_n( |
| 63 | + format_buf.begin(), |
| 64 | + format_buf.size(), |
| 65 | + "{} on shard {}. Backtrace: {}", |
| 66 | + failure_msg_prefix(signo), |
| 67 | + ss::this_shard_id(), |
| 68 | + ss::current_backtrace()); |
| 69 | + |
| 70 | + ss::backtrace([&cd](ss::frame f) { cd._stacktrace.push_back(f.addr); }); |
| 71 | +} |
| 72 | + |
| 73 | +void record_sigsegv_crash() { |
| 74 | + instance().record_crash( |
| 75 | + [](crash_description& cd) { record_signal_crash(cd, SIGSEGV); }); |
| 76 | +} |
| 77 | + |
| 78 | +void record_sigabrt_crash() { |
| 79 | + instance().record_crash( |
| 80 | + [](crash_description& cd) { record_signal_crash(cd, SIGABRT); }); |
| 81 | +} |
| 82 | + |
| 83 | +void record_sigill_crash() { |
| 84 | + instance().record_crash( |
| 85 | + [](crash_description& cd) { record_signal_crash(cd, SIGILL); }); |
| 86 | +} |
| 87 | + |
| 88 | +} // namespace crash_tracker |
0 commit comments