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