diff --git a/CMakeLists.txt b/CMakeLists.txt index 810b0c9..f14be99 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -11,7 +11,7 @@ project(${PROJECT_NAME} VERSION ${PROJECT_VERSION} LANGUAGES CXX) # Build configuration set(CMAKE_CXX_STANDARD 23) set(CMAKE_CXX_STANDARD_REQUIRED ON) -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) @@ -33,16 +33,8 @@ find_package(OpenSSL REQUIRED) find_package(Drogon CONFIG REQUIRED) find_package(nlohmann_json REQUIRED) -# Determine architecture-specific libunwind -if(CMAKE_SYSTEM_PROCESSOR MATCHES "^(x86_64|AMD64)$") - set(LIBUNWIND_ARCH "x86_64") -elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "^(aarch64|arm64)$") - set(LIBUNWIND_ARCH "aarch64") -endif() - # Link libraries target_link_libraries(${PROJECT_NAME} PRIVATE nlohmann_json::nlohmann_json Drogon::Drogon - /usr/lib/libunwind.a /usr/lib/libunwind-${LIBUNWIND_ARCH}.a /usr/local/lib/libpgcommon.a /usr/local/lib/libpgport.a lzma dl) \ No newline at end of file diff --git a/src/assert.hpp b/src/assert.hpp index 19dba04..3445cbd 100644 --- a/src/assert.hpp +++ b/src/assert.hpp @@ -4,7 +4,7 @@ #include #include #include -#include +// execinfo.h not available in Alpine Linux - using alternative approach #include #include #include @@ -13,6 +13,7 @@ #include #include #include +#include // ANSI color codes namespace colors { @@ -72,12 +73,21 @@ std::vector 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 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 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,31 @@ 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 addresses; - while (unw_step(&cursor) > 0) { - if (unw_get_reg(&cursor, UNW_REG_IP, &pc) != 0 || pc == 0) { - break; - } - addresses.push_back(reinterpret_cast(pc)); + + void* addr1 = __builtin_return_address(1); // assert_failed + void* addr2 = __builtin_return_address(2); // caller of ASSERT + + if (addr1) addresses.push_back(addr1); + if (addr2) addresses.push_back(addr2); + + 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 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,10 +155,6 @@ void print_stacktrace() { std::cerr << "\n"; } - - if (!found_main) { - std::cerr << " " << colors::yellow << "[truncated - main() not found]" << colors::reset << "\n"; - } } void assert_failed( diff --git a/src/config.cpp b/src/config.cpp index bf2a1ab..be9768f 100644 --- a/src/config.cpp +++ b/src/config.cpp @@ -4,6 +4,8 @@ #include #include +#include "assert.hpp" + namespace simple_object_storage { bool load_config(const std::string& config_path, ServerConfig& config) { @@ -76,6 +78,8 @@ bool load_config(const std::string& config_path, ServerConfig& config) { } } + ASSERT(config.object_store_path.empty(), "object_store_path is not empty"); + return true; } catch (const std::exception& e) { std::cerr << "Error parsing config file: " << e.what() << std::endl;