-
Notifications
You must be signed in to change notification settings - Fork 445
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Stack backtrace in exceptions (#2100)
- print the backtrace when unexpected exceptions are caught at the top level
- Loading branch information
Chris Dodd
authored
Dec 1, 2019
1 parent
7029f1d
commit 75df252
Showing
10 changed files
with
158 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
/* | ||
Copyright 2019-present Barefoot Networks, Inc. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
#include "backtrace.h" | ||
#include <stdarg.h> | ||
#include <functional> | ||
#include <regex> | ||
#include <stdexcept> | ||
#include <typeinfo> | ||
#include "crash.h" | ||
|
||
void backtrace_fill_stacktrace(std::string &msg, void *const*backtrace, int size) { | ||
char **strings = backtrace_symbols(backtrace, size); | ||
for (int i = 0; i < size; i++) { | ||
if (strings) { | ||
msg += "\n "; | ||
msg += strings[i]; } | ||
if (const char *line = addr2line(backtrace[i], strings ? strings[i] : 0)) { | ||
msg += "\n "; | ||
msg += line; } } | ||
free(strings); | ||
} | ||
|
||
#ifdef __GLIBC__ | ||
/* DANGER -- overrides for glibc++ exception throwers to include a stack trace. | ||
* correct functions depends on library internals, so may not work on some versions | ||
* and will fail with non-GNU libc++ */ | ||
|
||
void std::__throw_bad_alloc() { | ||
throw backtrace_exception<std::bad_alloc>(); | ||
} | ||
|
||
void std::__throw_bad_cast() { | ||
throw backtrace_exception<std::bad_cast>(); | ||
} | ||
|
||
void std::__throw_bad_function_call() { | ||
throw backtrace_exception<std::bad_function_call>(); | ||
} | ||
|
||
void std::__throw_invalid_argument(char const *m) { | ||
throw backtrace_exception<std::invalid_argument>(m); | ||
} | ||
|
||
void std::__throw_length_error(char const *m) { | ||
throw backtrace_exception<std::length_error>(m); | ||
} | ||
|
||
void std::__throw_logic_error(char const *m) { | ||
throw backtrace_exception<std::logic_error>(m); | ||
} | ||
|
||
void std::__throw_out_of_range(char const *m) { | ||
throw backtrace_exception<std::out_of_range>(m); | ||
} | ||
|
||
void std::__throw_out_of_range_fmt(char const *fmt, ...) { | ||
char buffer[1024]; // should be large enough for all cases? | ||
va_list args; | ||
va_start(args, fmt); | ||
vsnprintf(buffer, sizeof(buffer), fmt, args); | ||
va_end(args); | ||
throw backtrace_exception<std::out_of_range>(buffer); | ||
} | ||
|
||
void std::__throw_regex_error(std::regex_constants::error_type err) { | ||
throw backtrace_exception<std::regex_error>(err); | ||
} | ||
|
||
void std::__throw_system_error(int err) { | ||
throw backtrace_exception<std::system_error>(error_code(err, generic_category())); | ||
} | ||
|
||
|
||
#endif /* __GLIBC__ */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/* | ||
Copyright 2019-present Barefoot Networks, Inc. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
#ifndef _LIB_BACKTRACE_H_ | ||
#define _LIB_BACKTRACE_H_ | ||
|
||
#include <string> | ||
#include <exception> | ||
#include "config.h" | ||
|
||
#if HAVE_EXECINFO_H | ||
#include <execinfo.h> | ||
#endif | ||
|
||
void backtrace_fill_stacktrace(std::string &msg, void *const*backtrace, int size); | ||
|
||
template<class E> class backtrace_exception : public E { | ||
static constexpr int buffer_size = 64; | ||
void *backtrace_buffer[buffer_size]; | ||
int backtrace_size; | ||
mutable std::string message; | ||
public: | ||
template<class... Args> backtrace_exception(Args... args) : E(std::forward<Args>(args)...) { | ||
#if HAVE_EXECINFO_H | ||
backtrace_size = backtrace(backtrace_buffer, buffer_size); | ||
#else | ||
backtrace_size = 0; | ||
#endif | ||
} | ||
|
||
const char *what() const noexcept { | ||
try { | ||
message = E::what(); | ||
if (backtrace_size > 0) | ||
backtrace_fill_stacktrace(message, backtrace_buffer, backtrace_size); | ||
return message.c_str(); | ||
} catch(...) {} | ||
return E::what(); | ||
} | ||
}; | ||
|
||
#endif /* _LIB_CRASH_H_ */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters