aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/libs/breakpad/src/client/linux/handler/exception_trap.h
blob: 1a9fdb7e17731915e53155cedb7926b9a14d9e9a (plain) (blame)
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
#pragma once

#include <array>
#include <atomic>
#include <exception>

#include "google_breakpad/common/minidump_format.h"

namespace google_breakpad {

namespace detail {

#ifdef __ANDROID__
// In some versions of Android NDK the 'catch' blocks may have no affect in functions
// with 'noexcept' specifier.
constexpr bool can_catch_within_noexcept = false;
#else
constexpr bool can_catch_within_noexcept = true;
#endif

} // namespace detail

// ExceptionDescriptor holds exception information and may be safely passed
// between terminate handler context and signal handler context.
//
class ExceptionDescriptor {
public:
    // Copy |type_name| and |message| content to ExceptionDescriptor memory.
    // Not thread-safe. Async-signal-safe.
    // May be used in terminate handler.
    void initialize(const char* type_name, const char* message) noexcept;

     // isEmpty, getTypeName, getMessage are async-signal-safe.
     // May be used in signal handler.
    bool isEmpty() const noexcept;
    const char* getTypeName() const noexcept;
    const char* getMessage() const noexcept;

private:
    std::array<char, MAX_EXC_TYPE_LEN> type_name_{{'\0'}};
    std::array<char, MAX_EXC_MSG_LEN> message_{{'\0'}};
};

// Singleton class. Wraps default terminate handler with ExceptionTrap::terminate_handler.
// If terminate is caused by uncaught exception, ExceptionTrap stores information about exception
// into ExceptionDescriptor before default terminate handler will call.
class ExceptionTrap {
public:
    // Initialize single instance of ExceptionTrap if not initialized and return it.
    // Must be called at least once before using it in terminate handler or signal handler.
    static ExceptionTrap& getInstance() noexcept;

    // Replace current terminate handler by ExceptionTrap::terminate_handler.
    static void setupTerminateHandler();

    // Return information about first exception that ExceptionTrap is "caught".
    // If no exception was "caught", the empty ExceptionDescriptor will returned.
    // Async-signal-safe.
    const ExceptionDescriptor& getCurrentException() const noexcept;

private:
    bool initializeOnce(const char* type_name, const char* message) noexcept;

    // Call original terminate handler and abort.
    [[ noreturn ]] void terminate() const noexcept(detail::can_catch_within_noexcept);
    [[ noreturn ]] static void terminate_handler() noexcept(detail::can_catch_within_noexcept);

    std::atomic_flag initialized_ = ATOMIC_FLAG_INIT;
    ExceptionDescriptor descriptor_{};
    std::terminate_handler original_terminate_handler_{nullptr};
};

} //namespace google_breakpad