P4C
The P4 Compiler
Loading...
Searching...
No Matches
backtrace_exception.h
1/*
2Copyright 2019-present Barefoot Networks, Inc.
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16
17#ifndef LIB_BACKTRACE_EXCEPTION_H_
18#define LIB_BACKTRACE_EXCEPTION_H_
19
20#include <exception>
21#include <string>
22
23#include "config.h"
24
25#if HAVE_EXECINFO_H
26#include <execinfo.h>
27#endif
28
29namespace P4 {
30
31void backtrace_fill_stacktrace(std::string &msg, void *const *backtrace, int size);
32
33template <class E>
34class backtrace_exception : public E {
35 static constexpr int buffer_size = 64;
36 void *backtrace_buffer[buffer_size];
37 int backtrace_size;
38 mutable std::string message;
39
40 public:
41 template <class... Args>
42 explicit backtrace_exception(Args &&...args) : E(std::forward<Args>(args)...) {
43#if HAVE_EXECINFO_H
44 backtrace_size = backtrace(backtrace_buffer, buffer_size);
45#else
46 backtrace_size = 0;
47#endif
48 }
49
50 const char *what() const noexcept {
51 try {
52 message = E::what();
53 if (backtrace_size > 0)
54 backtrace_fill_stacktrace(message, backtrace_buffer, backtrace_size);
55 return message.c_str();
56 } catch (...) {
57 }
58 return E::what();
59 }
60};
61
62} // namespace P4
63
64#endif /* LIB_BACKTRACE_EXCEPTION_H_ */
Definition backtrace_exception.h:34
TODO: this is not really specific to BMV2, it should reside somewhere else.
Definition applyOptionsPragmas.cpp:24