This commit is contained in:
63
ipdemo/src/assert.hpp
Normal file
63
ipdemo/src/assert.hpp
Normal file
@@ -0,0 +1,63 @@
|
||||
#pragma once
|
||||
|
||||
#include <iostream>
|
||||
#include <string_view>
|
||||
#include <source_location>
|
||||
#include <cxxabi.h>
|
||||
#include <libunwind.h>
|
||||
#include <dlfcn.h>
|
||||
|
||||
[[noreturn]] void print_stacktrace() {
|
||||
unw_cursor_t cursor;
|
||||
unw_context_t context;
|
||||
unw_word_t offset, pc;
|
||||
char sym[256];
|
||||
char *name = sym;
|
||||
int status;
|
||||
|
||||
// Initialize cursor to current frame
|
||||
unw_getcontext(&context);
|
||||
unw_init_local(&cursor, &context);
|
||||
|
||||
std::cerr << "Stack trace (most recent call first):\n";
|
||||
|
||||
// Walk up the stack
|
||||
while (unw_step(&cursor) > 0) {
|
||||
unw_get_reg(&cursor, UNW_REG_IP, &pc);
|
||||
if (pc == 0) {
|
||||
break;
|
||||
}
|
||||
|
||||
// Get symbol name
|
||||
if (unw_get_proc_name(&cursor, sym, sizeof(sym), &offset) == 0) {
|
||||
// Demangle the C++ name
|
||||
int status;
|
||||
size_t length = sizeof(sym);
|
||||
name = abi::__cxa_demangle(sym, nullptr, &length, &status);
|
||||
if (name == nullptr) {
|
||||
name = sym;
|
||||
}
|
||||
std::cerr << " 0x" << std::hex << pc << ": " << name << " + 0x" << offset << "\n";
|
||||
if (name != sym) {
|
||||
free(name);
|
||||
}
|
||||
} else {
|
||||
std::cerr << " 0x" << std::hex << pc << ": [unknown]\n";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[[noreturn]] void assert_failed(
|
||||
bool condition,
|
||||
std::string_view message,
|
||||
std::source_location location = std::source_location::current()
|
||||
) {
|
||||
if (!condition) {
|
||||
std::cerr << "Assertion failed at " << location.file_name() << ":" << location.line() << ": "
|
||||
<< location.function_name() << "(): " << message << "\n";
|
||||
print_stacktrace();
|
||||
std::abort();
|
||||
}
|
||||
}
|
||||
|
||||
#define ASSERT(condition, message) assert_failed(condition, message, std::source_location::current())
|
@@ -1,13 +1,12 @@
|
||||
#include <iostream>
|
||||
|
||||
#include <nlohmann/json.hpp>
|
||||
#include <libassert/assert.hpp>
|
||||
|
||||
#include "httplib.hpp"
|
||||
#include "version.hpp"
|
||||
#include "assert.hpp"
|
||||
|
||||
void crashy() {
|
||||
ASSERT(false,"SUCCESS!");
|
||||
ASSERT(false, "SUCCESS!");
|
||||
}
|
||||
|
||||
int main() {
|
||||
|
Reference in New Issue
Block a user