This commit is contained in:
parent
35c3c512b2
commit
091c1f0850
@ -1,54 +1,31 @@
|
||||
cmake_minimum_required(VERSION 3.16)
|
||||
|
||||
# Generate version from current date
|
||||
execute_process(
|
||||
COMMAND date "+%Y.%m%d.%H%M"
|
||||
OUTPUT_VARIABLE PROJECT_VERSION
|
||||
OUTPUT_STRIP_TRAILING_WHITESPACE
|
||||
)
|
||||
|
||||
# Project setup
|
||||
if(NOT DEFINED PROJECT_NAME)
|
||||
message(FATAL_ERROR "PROJECT_NAME is not defined. Pass it via -DPROJECT_NAME=<name>")
|
||||
endif()
|
||||
|
||||
string(TIMESTAMP PROJECT_VERSION "%Y.%m%d.%H%M")
|
||||
project(${PROJECT_NAME} VERSION ${PROJECT_VERSION} LANGUAGES CXX)
|
||||
|
||||
# C++ standard and build configuration
|
||||
# Build configuration
|
||||
set(CMAKE_CXX_STANDARD 23)
|
||||
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
||||
|
||||
# Static linking configuration
|
||||
set(CMAKE_EXE_LINKER_FLAGS "-static -Wl,--allow-multiple-definition")
|
||||
set(CMAKE_EXE_LINKER_FLAGS "-static")
|
||||
set(CMAKE_FIND_LIBRARY_SUFFIXES ".a")
|
||||
set(BUILD_SHARED_LIBS OFF)
|
||||
|
||||
# Configure version.hpp
|
||||
configure_file(
|
||||
"${CMAKE_CURRENT_SOURCE_DIR}/src/version.hpp.in"
|
||||
"${CMAKE_CURRENT_BINARY_DIR}/src/autogen/version.hpp"
|
||||
@ONLY
|
||||
)
|
||||
|
||||
# Pre-build script
|
||||
add_custom_target(run_prebuild_script ALL
|
||||
# Configure version.hpp and create executable
|
||||
configure_file("src/version.hpp.in" "src/autogen/version.hpp" @ONLY)
|
||||
add_custom_target(run_prebuild_script ALL
|
||||
COMMAND bash ${CMAKE_CURRENT_SOURCE_DIR}/cmake_prebuild.sh
|
||||
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
|
||||
)
|
||||
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
|
||||
|
||||
# Source files
|
||||
file(GLOB_RECURSE SOURCES "src/*.cpp")
|
||||
set_source_files_properties(${SOURCES} PROPERTIES GENERATED TRUE)
|
||||
|
||||
# Create executable
|
||||
add_executable(${PROJECT_NAME} ${SOURCES})
|
||||
add_dependencies(${PROJECT_NAME} run_prebuild_script)
|
||||
|
||||
# Include directories
|
||||
target_include_directories(${PROJECT_NAME} PRIVATE
|
||||
${CMAKE_CURRENT_BINARY_DIR}/src/autogen
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/src
|
||||
)
|
||||
${CMAKE_CURRENT_BINARY_DIR}/src/autogen src)
|
||||
|
||||
# Find packages
|
||||
set(CMAKE_PREFIX_PATH /usr/local)
|
||||
@ -58,12 +35,6 @@ find_package(nlohmann_json REQUIRED)
|
||||
|
||||
# Link libraries
|
||||
target_link_libraries(${PROJECT_NAME} PRIVATE
|
||||
nlohmann_json::nlohmann_json
|
||||
Drogon::Drogon
|
||||
/usr/lib/libunwind.a
|
||||
/usr/lib/libunwind-x86_64.a
|
||||
/usr/local/lib/libpgcommon.a
|
||||
/usr/local/lib/libpgport.a
|
||||
lzma
|
||||
dl
|
||||
)
|
||||
nlohmann_json::nlohmann_json Drogon::Drogon
|
||||
/usr/local/lib/libpgcommon.a /usr/local/lib/libpgport.a
|
||||
lzma dl)
|
@ -4,7 +4,7 @@
|
||||
#include <string_view>
|
||||
#include <source_location>
|
||||
#include <cxxabi.h>
|
||||
#include <libunwind.h>
|
||||
// execinfo.h not available in Alpine Linux - using alternative approach
|
||||
#include <dlfcn.h>
|
||||
#include <cstring>
|
||||
#include <memory>
|
||||
@ -13,6 +13,7 @@
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
#include <unistd.h>
|
||||
#include <vector>
|
||||
|
||||
// ANSI color codes
|
||||
namespace colors {
|
||||
@ -72,12 +73,21 @@ std::vector<SourceInfo> get_source_info_batch(const char* executable, const std:
|
||||
// Demangle function name
|
||||
int status = 0;
|
||||
char* demangled = abi::__cxa_demangle(buffer, nullptr, nullptr, &status);
|
||||
std::string func_name;
|
||||
if (demangled) {
|
||||
results[current_frame].function = demangled;
|
||||
func_name = demangled;
|
||||
free(demangled);
|
||||
} else {
|
||||
results[current_frame].function = buffer;
|
||||
func_name = buffer;
|
||||
}
|
||||
|
||||
// Simplify function signature: replace argument list with (...)
|
||||
size_t paren_pos = func_name.find('(');
|
||||
if (paren_pos != std::string::npos) {
|
||||
func_name = func_name.substr(0, paren_pos) + "(...)";
|
||||
}
|
||||
|
||||
results[current_frame].function = func_name;
|
||||
|
||||
// Read file and line number
|
||||
if (fgets(buffer, sizeof(buffer), pipe)) {
|
||||
@ -100,14 +110,10 @@ std::vector<SourceInfo> get_source_info_batch(const char* executable, const std:
|
||||
}
|
||||
|
||||
void print_stacktrace() {
|
||||
unw_cursor_t cursor;
|
||||
unw_context_t context;
|
||||
unw_word_t pc;
|
||||
// Simple stacktrace implementation that doesn't segfault
|
||||
std::cerr << colors::yellow << "Stack trace:" << colors::reset << "\n";
|
||||
|
||||
char exe_path[1024] = {0};
|
||||
bool found_main = false;
|
||||
std::vector<SourceInfo> frames;
|
||||
|
||||
// Get the path to the current executable
|
||||
ssize_t count = readlink("/proc/self/exe", exe_path, sizeof(exe_path) - 1);
|
||||
if (count == -1) {
|
||||
strcpy(exe_path, "[unknown]");
|
||||
@ -115,46 +121,37 @@ void print_stacktrace() {
|
||||
exe_path[count] = '\0';
|
||||
}
|
||||
|
||||
// Initialize cursor to current frame
|
||||
unw_getcontext(&context);
|
||||
unw_init_local(&cursor, &context);
|
||||
|
||||
// First pass: collect frames until we find main
|
||||
// Get just a couple of stack frames safely
|
||||
std::vector<void*> addresses;
|
||||
while (unw_step(&cursor) > 0) {
|
||||
if (unw_get_reg(&cursor, UNW_REG_IP, &pc) != 0 || pc == 0) {
|
||||
break;
|
||||
}
|
||||
addresses.push_back(reinterpret_cast<void*>(pc));
|
||||
|
||||
void* addr1 = __builtin_return_address(1); // assert_failed
|
||||
void* addr2 = __builtin_return_address(2); // caller of ASSERT
|
||||
|
||||
// Adjust addresses to point to call site instead of return address
|
||||
// Subtract a small offset to get the call instruction instead of the return address
|
||||
if (addr1) {
|
||||
addresses.push_back(static_cast<char*>(addr1) - 1);
|
||||
}
|
||||
if (addr2) {
|
||||
addresses.push_back(static_cast<char*>(addr2) - 1);
|
||||
}
|
||||
|
||||
if (addresses.empty()) {
|
||||
std::cerr << " " << colors::red << "[no frames available]" << colors::reset << "\n";
|
||||
return;
|
||||
}
|
||||
|
||||
// Get source info for all addresses in batch
|
||||
// Get source info
|
||||
std::vector<SourceInfo> results = get_source_info_batch(exe_path, addresses);
|
||||
|
||||
// Process results and check for main
|
||||
for (auto& info : results) {
|
||||
// Stop collecting after main()
|
||||
if (!info.function.empty() &&
|
||||
(info.function.find("main") != std::string::npos ||
|
||||
info.function.find("__libc_start_main") != std::string::npos)) {
|
||||
found_main = true;
|
||||
frames.push_back(info);
|
||||
break;
|
||||
}
|
||||
frames.push_back(info);
|
||||
}
|
||||
|
||||
// Print the collected frames in reverse order (most recent last)
|
||||
std::cerr << colors::yellow << "Stack trace (most recent call last):" << colors::reset << "\n";
|
||||
|
||||
for (size_t i = frames.size(); i-- > 0; ) {
|
||||
const auto& frame = frames[i];
|
||||
std::cerr << " "; // Indent each line
|
||||
for (size_t i = 0; i < results.size(); ++i) {
|
||||
const auto& frame = results[i];
|
||||
std::cerr << " ";
|
||||
|
||||
if (frame.function.empty()) {
|
||||
std::cerr << colors::red << "[unknown function]" << colors::reset;
|
||||
} else {
|
||||
if (!frame.function.empty()) {
|
||||
std::cerr << colors::green << frame.function << colors::reset;
|
||||
} else {
|
||||
std::cerr << colors::red << "[unknown function]" << colors::reset;
|
||||
}
|
||||
|
||||
if (!frame.file.empty() && frame.line > 0) {
|
||||
@ -164,20 +161,16 @@ void print_stacktrace() {
|
||||
|
||||
std::cerr << "\n";
|
||||
}
|
||||
|
||||
if (!found_main) {
|
||||
std::cerr << " " << colors::yellow << "[truncated - main() not found]" << colors::reset << "\n";
|
||||
}
|
||||
}
|
||||
|
||||
void assert_failed(
|
||||
bool condition,
|
||||
std::string_view message,
|
||||
std::source_location location = std::source_location::current()
|
||||
std::source_location location
|
||||
) {
|
||||
if (!condition) {
|
||||
std::cerr << "Assertion failed at " << location.file_name() << ":" << location.line() << ": "
|
||||
<< location.function_name() << ": " << message << "\n";
|
||||
std::cerr << colors::red << "Assertion failed at " << location.file_name() << ":" << location.line() << ": "
|
||||
<< location.function_name() << ": " << message << colors::reset << "\n";
|
||||
print_stacktrace();
|
||||
std::abort();
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user