1
#include "source/exe/terminate_handler.h"
2

            
3
#include <cstdlib>
4

            
5
#include "envoy/common/exception.h"
6

            
7
#include "source/common/common/logger.h"
8
#include "source/common/common/thread.h"
9
#include "source/server/backtrace.h"
10

            
11
#include "absl/strings/str_format.h"
12

            
13
namespace Envoy {
14

            
15
49
std::terminate_handler TerminateHandler::logOnTerminate() const {
16
  // Pre-populate the address mapping so it doesn't require signal-unsafe file
17
  // actions during stack trace.
18
49
  BackwardsTrace::addrMapping(/*setup=*/true);
19
49
  return std::set_terminate([]() {
20
    logException(std::current_exception());
21
    BACKTRACE_LOG();
22
    std::abort();
23
  });
24
49
}
25

            
26
3
void TerminateHandler::logException(const std::exception_ptr current_exception) {
27
3
  if (current_exception != nullptr) {
28
2
    TRY_NEEDS_AUDIT { std::rethrow_exception(current_exception); }
29
2
    END_TRY
30
2
    MULTI_CATCH(
31
2
        const EnvoyException& e,
32
2
        {
33
2
          ENVOY_LOG(critical, "std::terminate called! Uncaught EnvoyException '{}', see trace.",
34
2
                    e.what());
35
2
        },
36
2
        { ENVOY_LOG(critical, "std::terminate called! Uncaught unknown exception, see trace."); });
37
2
  } else {
38
1
    ENVOY_LOG(critical, "std::terminate called! See trace.");
39
1
  }
40
3
}
41

            
42
} // namespace Envoy