P4C
The P4 Compiler
Loading...
Searching...
No Matches
backtrace_exception.h
1/*
2 * SPDX-FileCopyrightText: 2019 Barefoot Networks, Inc.
3 * Copyright 2019-present Barefoot Networks, Inc.
4 *
5 * SPDX-License-Identifier: Apache-2.0
6 */
7
8#ifndef LIB_BACKTRACE_EXCEPTION_H_
9#define LIB_BACKTRACE_EXCEPTION_H_
10
11#include <exception>
12#include <string>
13
14#include "absl/debugging/stacktrace.h"
15#include "config.h"
16
17namespace P4 {
18
19void backtrace_fill_stacktrace(std::string &msg, void *const *backtrace, int size);
20
21template <class E>
22class backtrace_exception : public E {
23 static constexpr int buffer_size = 64;
24 void *backtrace_buffer[buffer_size];
25 int backtrace_size;
26 mutable std::string message;
27
28 public:
29 template <class... Args>
30 explicit backtrace_exception(Args &&...args) : E(std::forward<Args>(args)...) {
31 backtrace_size = absl::GetStackTrace(backtrace_buffer, buffer_size, 1);
32 }
33
34 const char *what() const noexcept {
35 try {
36 message = E::what();
37 if (backtrace_size > 0)
38 backtrace_fill_stacktrace(message, backtrace_buffer, backtrace_size);
39 return message.c_str();
40 } catch (...) {
41 }
42 return E::what();
43 }
44};
45
46} // namespace P4
47
48#endif /* LIB_BACKTRACE_EXCEPTION_H_ */
TODO: this is not really specific to BMV2, it should reside somewhere else.
Definition applyOptionsPragmas.cpp:13