Failed assertions output a backtrace, furthermore added a small library that outputs backtraces on sigsegv
This commit is contained in:
parent
04be739b2e
commit
f3d3d70a5b
@ -11,11 +11,21 @@
|
||||
#include "AssertFailed.h"
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <execinfo.h>
|
||||
|
||||
namespace cpputils {
|
||||
namespace _assert {
|
||||
inline std::string format(const char *expr, const char *message, const char *file, int line) {
|
||||
return std::string()+"Assertion ["+expr+"] failed in "+file+":"+std::to_string(line)+": "+message;
|
||||
// get void*'s for all entries on the stack
|
||||
void *array[10];
|
||||
size_t size = backtrace(array, sizeof(array));
|
||||
char **backtrace_str = backtrace_symbols(array, sizeof(backtrace_str));
|
||||
std::string result = std::string()+"Assertion ["+expr+"] failed in "+file+":"+std::to_string(line)+": "+message+"\n\n";
|
||||
for (unsigned int i = 0; i < size; ++i) {
|
||||
result += std::string(backtrace_str[i]) + "\n";
|
||||
}
|
||||
free(backtrace_str);
|
||||
return result;
|
||||
}
|
||||
|
||||
inline void assert_fail_release [[noreturn]] (const char *expr, const char *message, const char *file, int line) {
|
||||
|
21
assert/backtrace.cpp
Normal file
21
assert/backtrace.cpp
Normal file
@ -0,0 +1,21 @@
|
||||
#include "backtrace.h"
|
||||
#include <execinfo.h>
|
||||
#include <signal.h>
|
||||
#include <iostream>
|
||||
#include <unistd.h>
|
||||
|
||||
namespace cpputils {
|
||||
|
||||
void sigsegv_handler(int) {
|
||||
void *array[100];
|
||||
size_t size = backtrace(array, sizeof(array));
|
||||
|
||||
std::cerr << "Error: SIGSEGV" << std::endl;
|
||||
backtrace_symbols_fd(array, size, STDERR_FILENO);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
void showBacktraceOnSigSegv() {
|
||||
signal(SIGSEGV, sigsegv_handler);
|
||||
}
|
||||
}
|
8
assert/backtrace.h
Normal file
8
assert/backtrace.h
Normal file
@ -0,0 +1,8 @@
|
||||
#ifndef MESSMER_CPPUTILS_ASSERT_BACKTRACE_H
|
||||
#define MESSMER_CPPUTILS_ASSERT_BACKTRACE_H
|
||||
|
||||
namespace cpputils {
|
||||
void showBacktraceOnSigSegv();
|
||||
}
|
||||
|
||||
#endif
|
Loading…
Reference in New Issue
Block a user