mirror of
https://github.com/VCMP-SqMod/SqMod.git
synced 2026-11-21 19:27:09 +01:00
Update libraries and make it build on windows.
Still gets some warnings because compilers have changed. But should work.
This commit is contained in:
1 parent
520a5eacc5
commit
f2b7499f85
3038 files changed
+251668
-273857
No files matched your search
+62
@@ -0,0 +1,62 @@
|
||||
//
|
||||
// Trace.h
|
||||
//
|
||||
// Library: Trace
|
||||
// Package: Trace
|
||||
// Module: Trace
|
||||
//
|
||||
// Basic definitions for the Poco Trace library.
|
||||
// This file must be the first file included by every other Trace
|
||||
// header file.
|
||||
//
|
||||
// Copyright (c) 2012, Applied Informatics Software Engineering GmbH.
|
||||
// and Contributors.
|
||||
//
|
||||
// SPDX-License-Identifier: BSL-1.0
|
||||
//
|
||||
|
||||
|
||||
#ifndef Trace_Trace_INCLUDED
|
||||
#define Trace_Trace_INCLUDED
|
||||
|
||||
|
||||
#include "Poco/Foundation.h"
|
||||
|
||||
|
||||
//
|
||||
// The following block is the standard way of creating macros which make exporting
|
||||
// from a DLL simpler. All files within this DLL are compiled with the Trace_EXPORTS
|
||||
// symbol defined on the command line. this symbol should not be defined on any project
|
||||
// that uses this DLL. This way any other project whose source files include this file see
|
||||
// Trace_API functions as being imported from a DLL, whereas this DLL sees symbols
|
||||
// defined with this macro as being exported.
|
||||
//
|
||||
#if defined(_WIN32) && defined(POCO_DLL)
|
||||
#if defined(Trace_EXPORTS)
|
||||
#define Trace_API __declspec(dllexport)
|
||||
#else
|
||||
#define Trace_API __declspec(dllimport)
|
||||
#endif
|
||||
#endif
|
||||
|
||||
|
||||
#if !defined(Trace_API)
|
||||
#if !defined(POCO_NO_GCC_API_ATTRIBUTE) && defined (__GNUC__) && (__GNUC__ >= 4)
|
||||
#define Trace_API __attribute__ ((visibility ("default")))
|
||||
#else
|
||||
#define Trace_API
|
||||
#endif
|
||||
#endif
|
||||
|
||||
|
||||
//
|
||||
// Automatically link Trace library.
|
||||
//
|
||||
#if defined(_MSC_VER)
|
||||
#if !defined(POCO_NO_AUTOMATIC_LIBS) && !defined(Trace_EXPORTS)
|
||||
#pragma comment(lib, "PocoTrace" POCO_LIB_SUFFIX)
|
||||
#endif
|
||||
#endif
|
||||
|
||||
|
||||
#endif // Trace_Trace_INCLUDED
|
||||
@@ -0,0 +1,496 @@
|
||||
#ifndef CPPTRACE_HPP
|
||||
#define CPPTRACE_HPP
|
||||
|
||||
#include <cstdint>
|
||||
#include <exception>
|
||||
#include <limits>
|
||||
#include <iosfwd>
|
||||
#include <string>
|
||||
#include <system_error>
|
||||
#include <type_traits>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
#ifdef _WIN32
|
||||
#define CPPTRACE_EXPORT_ATTR __declspec(dllexport)
|
||||
#define CPPTRACE_IMPORT_ATTR __declspec(dllimport)
|
||||
#else
|
||||
#define CPPTRACE_EXPORT_ATTR __attribute__((visibility("default")))
|
||||
#define CPPTRACE_IMPORT_ATTR __attribute__((visibility("default")))
|
||||
#endif
|
||||
|
||||
#ifdef CPPTRACE_STATIC_DEFINE
|
||||
# define CPPTRACE_EXPORT
|
||||
# define CPPTRACE_NO_EXPORT
|
||||
#else
|
||||
# ifndef CPPTRACE_EXPORT
|
||||
# ifdef cpptrace_lib_EXPORTS
|
||||
/* We are building this library */
|
||||
# define CPPTRACE_EXPORT CPPTRACE_EXPORT_ATTR
|
||||
# else
|
||||
/* We are using this library */
|
||||
# define CPPTRACE_EXPORT CPPTRACE_IMPORT_ATTR
|
||||
# endif
|
||||
# endif
|
||||
#endif
|
||||
|
||||
#ifndef CPPTRACE_NO_STD_FORMAT
|
||||
#if __cplusplus >= 202002L
|
||||
#ifdef __has_include
|
||||
#if __has_include(<format>)
|
||||
#define CPPTRACE_STD_FORMAT
|
||||
#include <format>
|
||||
#endif
|
||||
#endif
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#define CPPTRACE_FORCE_NO_INLINE __declspec(noinline)
|
||||
#else
|
||||
#define CPPTRACE_FORCE_NO_INLINE __attribute__((noinline))
|
||||
#endif
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#pragma warning(push)
|
||||
// warning C4251: using non-dll-exported type in dll-exported type, firing on std::vector<frame_ptr> and others for some
|
||||
// reason
|
||||
// 4275 is the same thing but for base classes
|
||||
#pragma warning(disable: 4251; disable: 4275)
|
||||
#endif
|
||||
|
||||
namespace cpptrace {
|
||||
struct object_trace;
|
||||
struct stacktrace;
|
||||
|
||||
// Some type sufficient for an instruction pointer, currently always an alias to std::uintptr_t
|
||||
using frame_ptr = std::uintptr_t;
|
||||
|
||||
struct CPPTRACE_EXPORT raw_trace {
|
||||
std::vector<frame_ptr> frames;
|
||||
static raw_trace current(std::size_t skip = 0);
|
||||
static raw_trace current(std::size_t skip, std::size_t max_depth);
|
||||
object_trace resolve_object_trace() const;
|
||||
stacktrace resolve() const;
|
||||
void clear();
|
||||
bool empty() const noexcept;
|
||||
|
||||
using iterator = std::vector<frame_ptr>::iterator;
|
||||
using const_iterator = std::vector<frame_ptr>::const_iterator;
|
||||
inline iterator begin() noexcept { return frames.begin(); }
|
||||
inline iterator end() noexcept { return frames.end(); }
|
||||
inline const_iterator begin() const noexcept { return frames.begin(); }
|
||||
inline const_iterator end() const noexcept { return frames.end(); }
|
||||
inline const_iterator cbegin() const noexcept { return frames.cbegin(); }
|
||||
inline const_iterator cend() const noexcept { return frames.cend(); }
|
||||
};
|
||||
|
||||
struct CPPTRACE_EXPORT object_frame {
|
||||
frame_ptr raw_address;
|
||||
frame_ptr object_address;
|
||||
std::string object_path;
|
||||
};
|
||||
|
||||
struct CPPTRACE_EXPORT object_trace {
|
||||
std::vector<object_frame> frames;
|
||||
static object_trace current(std::size_t skip = 0);
|
||||
static object_trace current(std::size_t skip, std::size_t max_depth);
|
||||
stacktrace resolve() const;
|
||||
void clear();
|
||||
bool empty() const noexcept;
|
||||
|
||||
using iterator = std::vector<object_frame>::iterator;
|
||||
using const_iterator = std::vector<object_frame>::const_iterator;
|
||||
inline iterator begin() noexcept { return frames.begin(); }
|
||||
inline iterator end() noexcept { return frames.end(); }
|
||||
inline const_iterator begin() const noexcept { return frames.begin(); }
|
||||
inline const_iterator end() const noexcept { return frames.end(); }
|
||||
inline const_iterator cbegin() const noexcept { return frames.cbegin(); }
|
||||
inline const_iterator cend() const noexcept { return frames.cend(); }
|
||||
};
|
||||
|
||||
// This represents a nullable integer type
|
||||
// The max value of the type is used as a sentinel
|
||||
// This is used over std::optional because the library is C++11 and also std::optional is a bit heavy-duty for this
|
||||
// use.
|
||||
template<typename T, typename std::enable_if<std::is_integral<T>::value, int>::type = 0>
|
||||
struct nullable {
|
||||
T raw_value;
|
||||
nullable& operator=(T value) {
|
||||
raw_value = value;
|
||||
return *this;
|
||||
}
|
||||
bool has_value() const noexcept {
|
||||
return raw_value != (std::numeric_limits<T>::max)();
|
||||
}
|
||||
T& value() noexcept {
|
||||
return raw_value;
|
||||
}
|
||||
const T& value() const noexcept {
|
||||
return raw_value;
|
||||
}
|
||||
T value_or(T alternative) const noexcept {
|
||||
return has_value() ? raw_value : alternative;
|
||||
}
|
||||
void swap(nullable& other) noexcept {
|
||||
std::swap(raw_value, other.raw_value);
|
||||
}
|
||||
void reset() noexcept {
|
||||
raw_value = (std::numeric_limits<T>::max)();
|
||||
}
|
||||
bool operator==(const nullable& other) const noexcept {
|
||||
return raw_value == other.raw_value;
|
||||
}
|
||||
bool operator!=(const nullable& other) const noexcept {
|
||||
return raw_value != other.raw_value;
|
||||
}
|
||||
constexpr static nullable null() noexcept {
|
||||
return { (std::numeric_limits<T>::max)() };
|
||||
}
|
||||
};
|
||||
|
||||
struct CPPTRACE_EXPORT stacktrace_frame {
|
||||
frame_ptr raw_address;
|
||||
frame_ptr object_address;
|
||||
nullable<std::uint32_t> line;
|
||||
nullable<std::uint32_t> column;
|
||||
std::string filename;
|
||||
std::string symbol;
|
||||
bool is_inline;
|
||||
|
||||
bool operator==(const stacktrace_frame& other) const {
|
||||
return raw_address == other.raw_address
|
||||
&& object_address == other.object_address
|
||||
&& line == other.line
|
||||
&& column == other.column
|
||||
&& filename == other.filename
|
||||
&& symbol == other.symbol;
|
||||
}
|
||||
|
||||
bool operator!=(const stacktrace_frame& other) const {
|
||||
return !operator==(other);
|
||||
}
|
||||
|
||||
object_frame get_object_info() const;
|
||||
|
||||
std::string to_string() const;
|
||||
friend std::ostream& operator<<(std::ostream& stream, const stacktrace_frame& frame);
|
||||
};
|
||||
|
||||
struct CPPTRACE_EXPORT stacktrace {
|
||||
std::vector<stacktrace_frame> frames;
|
||||
static stacktrace current(std::size_t skip = 0);
|
||||
static stacktrace current(std::size_t skip, std::size_t max_depth);
|
||||
void print() const;
|
||||
void print(std::ostream& stream) const;
|
||||
void print(std::ostream& stream, bool color) const;
|
||||
void print_with_snippets() const;
|
||||
void print_with_snippets(std::ostream& stream) const;
|
||||
void print_with_snippets(std::ostream& stream, bool color) const;
|
||||
void clear();
|
||||
bool empty() const noexcept;
|
||||
std::string to_string(bool color = false) const;
|
||||
friend std::ostream& operator<<(std::ostream& stream, const stacktrace& trace);
|
||||
|
||||
using iterator = std::vector<stacktrace_frame>::iterator;
|
||||
using const_iterator = std::vector<stacktrace_frame>::const_iterator;
|
||||
inline iterator begin() noexcept { return frames.begin(); }
|
||||
inline iterator end() noexcept { return frames.end(); }
|
||||
inline const_iterator begin() const noexcept { return frames.begin(); }
|
||||
inline const_iterator end() const noexcept { return frames.end(); }
|
||||
inline const_iterator cbegin() const noexcept { return frames.cbegin(); }
|
||||
inline const_iterator cend() const noexcept { return frames.cend(); }
|
||||
private:
|
||||
void print(std::ostream& stream, bool color, bool newline_at_end, const char* header) const;
|
||||
void print_with_snippets(std::ostream& stream, bool color, bool newline_at_end, const char* header) const;
|
||||
friend void print_terminate_trace();
|
||||
};
|
||||
|
||||
CPPTRACE_EXPORT raw_trace generate_raw_trace(std::size_t skip = 0);
|
||||
CPPTRACE_EXPORT raw_trace generate_raw_trace(std::size_t skip, std::size_t max_depth);
|
||||
CPPTRACE_EXPORT object_trace generate_object_trace(std::size_t skip = 0);
|
||||
CPPTRACE_EXPORT object_trace generate_object_trace(std::size_t skip, std::size_t max_depth);
|
||||
CPPTRACE_EXPORT stacktrace generate_trace(std::size_t skip = 0);
|
||||
CPPTRACE_EXPORT stacktrace generate_trace(std::size_t skip, std::size_t max_depth);
|
||||
|
||||
// Path max isn't so simple, so I'm choosing 4096 which seems to encompass what all major OS's expect and should be
|
||||
// fine in all reasonable cases.
|
||||
// https://eklitzke.org/path-max-is-tricky
|
||||
// https://insanecoding.blogspot.com/2007/11/pathmax-simply-isnt.html
|
||||
#define CPPTRACE_PATH_MAX 4096
|
||||
|
||||
// safe tracing interface
|
||||
// signal-safe
|
||||
CPPTRACE_EXPORT std::size_t safe_generate_raw_trace(
|
||||
frame_ptr* buffer,
|
||||
std::size_t size,
|
||||
std::size_t skip = 0
|
||||
);
|
||||
// signal-safe
|
||||
CPPTRACE_EXPORT std::size_t safe_generate_raw_trace(
|
||||
frame_ptr* buffer,
|
||||
std::size_t size,
|
||||
std::size_t skip,
|
||||
std::size_t max_depth
|
||||
);
|
||||
struct CPPTRACE_EXPORT safe_object_frame {
|
||||
frame_ptr raw_address;
|
||||
frame_ptr address_relative_to_object_start; // base must still be added
|
||||
char object_path[CPPTRACE_PATH_MAX + 1];
|
||||
// To be called outside a signal handler. Not signal safe.
|
||||
object_frame resolve() const;
|
||||
};
|
||||
// signal-safe
|
||||
CPPTRACE_EXPORT void get_safe_object_frame(frame_ptr address, safe_object_frame* out);
|
||||
CPPTRACE_EXPORT bool can_signal_safe_unwind();
|
||||
|
||||
// utilities:
|
||||
CPPTRACE_EXPORT std::string demangle(const std::string& name);
|
||||
CPPTRACE_EXPORT std::string get_snippet(
|
||||
const std::string& path,
|
||||
std::size_t line,
|
||||
std::size_t context_size,
|
||||
bool color = false
|
||||
);
|
||||
CPPTRACE_EXPORT bool isatty(int fd);
|
||||
|
||||
CPPTRACE_EXPORT extern const int stdin_fileno;
|
||||
CPPTRACE_EXPORT extern const int stderr_fileno;
|
||||
CPPTRACE_EXPORT extern const int stdout_fileno;
|
||||
|
||||
CPPTRACE_EXPORT void register_terminate_handler();
|
||||
|
||||
// configuration:
|
||||
CPPTRACE_EXPORT void absorb_trace_exceptions(bool absorb);
|
||||
CPPTRACE_EXPORT void enable_inlined_call_resolution(bool enable);
|
||||
|
||||
enum class cache_mode {
|
||||
// Only minimal lookup tables
|
||||
prioritize_memory = 0,
|
||||
// Build lookup tables but don't keep them around between trace calls
|
||||
hybrid = 1,
|
||||
// Build lookup tables as needed
|
||||
prioritize_speed = 2
|
||||
};
|
||||
|
||||
namespace experimental {
|
||||
CPPTRACE_EXPORT void set_cache_mode(cache_mode mode);
|
||||
}
|
||||
|
||||
// tracing exceptions:
|
||||
namespace detail {
|
||||
// This is a helper utility, if the library weren't C++11 an std::variant would be used
|
||||
class CPPTRACE_EXPORT lazy_trace_holder {
|
||||
bool resolved;
|
||||
union {
|
||||
raw_trace trace;
|
||||
stacktrace resolved_trace;
|
||||
};
|
||||
public:
|
||||
// constructors
|
||||
lazy_trace_holder() : resolved(false), trace() {}
|
||||
explicit lazy_trace_holder(raw_trace&& _trace) : resolved(false), trace(std::move(_trace)) {}
|
||||
explicit lazy_trace_holder(stacktrace&& _resolved_trace) : resolved(true), resolved_trace(std::move(_resolved_trace)) {}
|
||||
// logistics
|
||||
lazy_trace_holder(const lazy_trace_holder& other);
|
||||
lazy_trace_holder(lazy_trace_holder&& other) noexcept;
|
||||
lazy_trace_holder& operator=(const lazy_trace_holder& other);
|
||||
lazy_trace_holder& operator=(lazy_trace_holder&& other) noexcept;
|
||||
~lazy_trace_holder();
|
||||
// access
|
||||
const raw_trace& get_raw_trace() const;
|
||||
stacktrace& get_resolved_trace();
|
||||
const stacktrace& get_resolved_trace() const;
|
||||
private:
|
||||
void clear();
|
||||
};
|
||||
|
||||
CPPTRACE_EXPORT raw_trace get_raw_trace_and_absorb(std::size_t skip, std::size_t max_depth);
|
||||
CPPTRACE_EXPORT raw_trace get_raw_trace_and_absorb(std::size_t skip = 0);
|
||||
}
|
||||
|
||||
// Interface for a traced exception object
|
||||
class CPPTRACE_EXPORT exception : public std::exception {
|
||||
public:
|
||||
const char* what() const noexcept override = 0;
|
||||
virtual const char* message() const noexcept = 0;
|
||||
virtual const stacktrace& trace() const noexcept = 0;
|
||||
};
|
||||
|
||||
// Cpptrace traced exception object
|
||||
// I hate to have to expose anything about implementation detail but the idea here is that
|
||||
class CPPTRACE_EXPORT lazy_exception : public exception {
|
||||
mutable detail::lazy_trace_holder trace_holder;
|
||||
mutable std::string what_string;
|
||||
|
||||
public:
|
||||
explicit lazy_exception(
|
||||
raw_trace&& trace = detail::get_raw_trace_and_absorb()
|
||||
) : trace_holder(std::move(trace)) {}
|
||||
// std::exception
|
||||
const char* what() const noexcept override;
|
||||
// cpptrace::exception
|
||||
const char* message() const noexcept override;
|
||||
const stacktrace& trace() const noexcept override;
|
||||
};
|
||||
|
||||
class CPPTRACE_EXPORT exception_with_message : public lazy_exception {
|
||||
mutable std::string user_message;
|
||||
|
||||
public:
|
||||
explicit exception_with_message(
|
||||
std::string&& message_arg,
|
||||
raw_trace&& trace = detail::get_raw_trace_and_absorb()
|
||||
) noexcept : lazy_exception(std::move(trace)), user_message(std::move(message_arg)) {}
|
||||
|
||||
const char* message() const noexcept override;
|
||||
};
|
||||
|
||||
class CPPTRACE_EXPORT logic_error : public exception_with_message {
|
||||
public:
|
||||
explicit logic_error(
|
||||
std::string&& message_arg,
|
||||
raw_trace&& trace = detail::get_raw_trace_and_absorb()
|
||||
) noexcept
|
||||
: exception_with_message(std::move(message_arg), std::move(trace)) {}
|
||||
};
|
||||
|
||||
class CPPTRACE_EXPORT domain_error : public exception_with_message {
|
||||
public:
|
||||
explicit domain_error(
|
||||
std::string&& message_arg,
|
||||
raw_trace&& trace = detail::get_raw_trace_and_absorb()
|
||||
) noexcept
|
||||
: exception_with_message(std::move(message_arg), std::move(trace)) {}
|
||||
};
|
||||
|
||||
class CPPTRACE_EXPORT invalid_argument : public exception_with_message {
|
||||
public:
|
||||
explicit invalid_argument(
|
||||
std::string&& message_arg,
|
||||
raw_trace&& trace = detail::get_raw_trace_and_absorb()
|
||||
) noexcept
|
||||
: exception_with_message(std::move(message_arg), std::move(trace)) {}
|
||||
};
|
||||
|
||||
class CPPTRACE_EXPORT length_error : public exception_with_message {
|
||||
public:
|
||||
explicit length_error(
|
||||
std::string&& message_arg,
|
||||
raw_trace&& trace = detail::get_raw_trace_and_absorb()
|
||||
) noexcept
|
||||
: exception_with_message(std::move(message_arg), std::move(trace)) {}
|
||||
};
|
||||
|
||||
class CPPTRACE_EXPORT out_of_range : public exception_with_message {
|
||||
public:
|
||||
explicit out_of_range(
|
||||
std::string&& message_arg,
|
||||
raw_trace&& trace = detail::get_raw_trace_and_absorb()
|
||||
) noexcept
|
||||
: exception_with_message(std::move(message_arg), std::move(trace)) {}
|
||||
};
|
||||
|
||||
class CPPTRACE_EXPORT runtime_error : public exception_with_message {
|
||||
public:
|
||||
explicit runtime_error(
|
||||
std::string&& message_arg,
|
||||
raw_trace&& trace = detail::get_raw_trace_and_absorb()
|
||||
) noexcept
|
||||
: exception_with_message(std::move(message_arg), std::move(trace)) {}
|
||||
};
|
||||
|
||||
class CPPTRACE_EXPORT range_error : public exception_with_message {
|
||||
public:
|
||||
explicit range_error(
|
||||
std::string&& message_arg,
|
||||
raw_trace&& trace = detail::get_raw_trace_and_absorb()
|
||||
) noexcept
|
||||
: exception_with_message(std::move(message_arg), std::move(trace)) {}
|
||||
};
|
||||
|
||||
class CPPTRACE_EXPORT overflow_error : public exception_with_message {
|
||||
public:
|
||||
explicit overflow_error(
|
||||
std::string&& message_arg,
|
||||
raw_trace&& trace = detail::get_raw_trace_and_absorb()
|
||||
) noexcept
|
||||
: exception_with_message(std::move(message_arg), std::move(trace)) {}
|
||||
};
|
||||
|
||||
class CPPTRACE_EXPORT underflow_error : public exception_with_message {
|
||||
public:
|
||||
explicit underflow_error(
|
||||
std::string&& message_arg,
|
||||
raw_trace&& trace = detail::get_raw_trace_and_absorb()
|
||||
) noexcept
|
||||
: exception_with_message(std::move(message_arg), std::move(trace)) {}
|
||||
};
|
||||
|
||||
class CPPTRACE_EXPORT nested_exception : public lazy_exception {
|
||||
std::exception_ptr ptr;
|
||||
mutable std::string message_value;
|
||||
public:
|
||||
explicit nested_exception(
|
||||
const std::exception_ptr& exception_ptr,
|
||||
raw_trace&& trace = detail::get_raw_trace_and_absorb()
|
||||
) noexcept
|
||||
: lazy_exception(std::move(trace)), ptr(exception_ptr) {}
|
||||
|
||||
const char* message() const noexcept override;
|
||||
std::exception_ptr nested_ptr() const noexcept;
|
||||
};
|
||||
|
||||
class CPPTRACE_EXPORT system_error : public runtime_error {
|
||||
std::error_code ec;
|
||||
public:
|
||||
explicit system_error(
|
||||
int error_code,
|
||||
std::string&& message_arg,
|
||||
raw_trace&& trace = detail::get_raw_trace_and_absorb()
|
||||
) noexcept;
|
||||
const std::error_code& code() const noexcept;
|
||||
};
|
||||
|
||||
// [[noreturn]] must come first due to old clang
|
||||
[[noreturn]] CPPTRACE_EXPORT void rethrow_and_wrap_if_needed(std::size_t skip = 0);
|
||||
}
|
||||
|
||||
#if defined(CPPTRACE_STD_FORMAT) && defined(__cpp_lib_format)
|
||||
template <>
|
||||
struct std::formatter<cpptrace::stacktrace_frame> : std::formatter<std::string> {
|
||||
auto format(cpptrace::stacktrace_frame frame, format_context& ctx) const {
|
||||
return formatter<string>::format(frame.to_string(), ctx);
|
||||
}
|
||||
};
|
||||
|
||||
template <>
|
||||
struct std::formatter<cpptrace::stacktrace> : std::formatter<std::string> {
|
||||
auto format(cpptrace::stacktrace trace, format_context& ctx) const {
|
||||
return formatter<string>::format(trace.to_string(), ctx);
|
||||
}
|
||||
};
|
||||
#endif
|
||||
|
||||
// Exception wrapper utilities
|
||||
#define CPPTRACE_WRAP_BLOCK(statements) do { \
|
||||
try { \
|
||||
statements \
|
||||
} catch(...) { \
|
||||
::cpptrace::rethrow_and_wrap_if_needed(); \
|
||||
} \
|
||||
} while(0)
|
||||
|
||||
#define CPPTRACE_WRAP(expression) [&] () -> decltype((expression)) { \
|
||||
try { \
|
||||
return expression; \
|
||||
} catch(...) { \
|
||||
::cpptrace::rethrow_and_wrap_if_needed(1); \
|
||||
} \
|
||||
} ()
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#pragma warning(pop)
|
||||
#endif
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,131 @@
|
||||
#ifndef CPPTRACE_FROM_CURRENT_HPP
|
||||
#define CPPTRACE_FROM_CURRENT_HPP
|
||||
|
||||
#include <cpptrace/cpptrace.hpp>
|
||||
|
||||
namespace cpptrace {
|
||||
CPPTRACE_EXPORT const raw_trace& raw_trace_from_current_exception();
|
||||
CPPTRACE_EXPORT const stacktrace& from_current_exception();
|
||||
|
||||
namespace detail {
|
||||
// Trace switch is to prevent multiple tracing of stacks on call stacks with multiple catches that don't
|
||||
// immediately match
|
||||
inline bool& get_trace_switch() {
|
||||
static thread_local bool trace_switch = true;
|
||||
return trace_switch;
|
||||
}
|
||||
|
||||
class CPPTRACE_EXPORT try_canary {
|
||||
public:
|
||||
~try_canary() {
|
||||
// Fires when we exit a try block, either via normal means or during unwinding.
|
||||
// Either way: Flip the switch.
|
||||
get_trace_switch() = true;
|
||||
}
|
||||
};
|
||||
|
||||
CPPTRACE_EXPORT CPPTRACE_FORCE_NO_INLINE void collect_current_trace(std::size_t skip);
|
||||
|
||||
// this function can be void, however, a char return is used to prevent TCO of the collect_current_trace
|
||||
CPPTRACE_FORCE_NO_INLINE inline char exception_unwind_interceptor(std::size_t skip) {
|
||||
if(get_trace_switch()) {
|
||||
// Done during a search phase. Flip the switch off, no more traces until an unwind happens
|
||||
get_trace_switch() = false;
|
||||
collect_current_trace(skip + 1);
|
||||
}
|
||||
return 42;
|
||||
}
|
||||
|
||||
#ifdef _MSC_VER
|
||||
CPPTRACE_FORCE_NO_INLINE inline int exception_filter() {
|
||||
exception_unwind_interceptor(1);
|
||||
return 0; // EXCEPTION_CONTINUE_SEARCH
|
||||
}
|
||||
CPPTRACE_FORCE_NO_INLINE inline int unconditional_exception_filter() {
|
||||
collect_current_trace(1);
|
||||
return 0; // EXCEPTION_CONTINUE_SEARCH
|
||||
}
|
||||
#else
|
||||
class CPPTRACE_EXPORT unwind_interceptor {
|
||||
public:
|
||||
virtual ~unwind_interceptor();
|
||||
};
|
||||
class CPPTRACE_EXPORT unconditional_unwind_interceptor {
|
||||
public:
|
||||
virtual ~unconditional_unwind_interceptor();
|
||||
};
|
||||
|
||||
CPPTRACE_EXPORT void do_prepare_unwind_interceptor(char(*)(std::size_t));
|
||||
|
||||
#ifndef CPPTRACE_DONT_PREPARE_UNWIND_INTERCEPTOR_ON
|
||||
__attribute__((constructor)) inline void prepare_unwind_interceptor() {
|
||||
// __attribute__((constructor)) inline functions can be called for every source file they're #included in
|
||||
// there is still only one copy of the inline function in the final executable, though
|
||||
// LTO can make the redundant constructs fire only once
|
||||
// do_prepare_unwind_interceptor prevents against multiple preparations however it makes sense to guard
|
||||
// against it here too as a fast path, not that this should matter for performance
|
||||
static bool did_prepare = false;
|
||||
if(!did_prepare) {
|
||||
do_prepare_unwind_interceptor(exception_unwind_interceptor);
|
||||
did_prepare = true;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef _MSC_VER
|
||||
// this awful double-IILE is due to C2713 "You can't use structured exception handling (__try/__except) and C++
|
||||
// exception handling (try/catch) in the same function."
|
||||
#define CPPTRACE_TRY \
|
||||
try { \
|
||||
::cpptrace::detail::try_canary cpptrace_try_canary; \
|
||||
[&]() { \
|
||||
__try { \
|
||||
[&]() {
|
||||
#define CPPTRACE_CATCH(param) \
|
||||
}(); \
|
||||
} __except(::cpptrace::detail::exception_filter()) {} \
|
||||
}(); \
|
||||
} catch(param)
|
||||
#define CPPTRACE_TRYZ \
|
||||
try { \
|
||||
[&]() { \
|
||||
__try { \
|
||||
[&]() {
|
||||
#define CPPTRACE_CATCHZ(param) \
|
||||
}(); \
|
||||
} __except(::cpptrace::detail::unconditional_exception_filter()) {} \
|
||||
}(); \
|
||||
} catch(param)
|
||||
#else
|
||||
#define CPPTRACE_TRY \
|
||||
try { \
|
||||
_Pragma("GCC diagnostic push") \
|
||||
_Pragma("GCC diagnostic ignored \"-Wshadow\"") \
|
||||
::cpptrace::detail::try_canary cpptrace_try_canary; \
|
||||
_Pragma("GCC diagnostic pop") \
|
||||
try {
|
||||
#define CPPTRACE_CATCH(param) \
|
||||
} catch(::cpptrace::detail::unwind_interceptor&) {} \
|
||||
} catch(param)
|
||||
#define CPPTRACE_TRYZ \
|
||||
try { \
|
||||
try {
|
||||
#define CPPTRACE_CATCHZ(param) \
|
||||
} catch(::cpptrace::detail::unconditional_unwind_interceptor&) {} \
|
||||
} catch(param)
|
||||
#endif
|
||||
|
||||
#define CPPTRACE_CATCH_ALT(param) catch(param)
|
||||
|
||||
#ifdef CPPTRACE_UNPREFIXED_TRY_CATCH
|
||||
#define TRY CPPTRACE_TRY
|
||||
#define CATCH(param) CPPTRACE_CATCH(param)
|
||||
#define TRYZ CPPTRACE_TRYZ
|
||||
#define CATCHZ(param) CPPTRACE_CATCHZ(param)
|
||||
#define CATCH_ALT(param) CPPTRACE_CATCH_ALT(param)
|
||||
#endif
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,163 @@
|
||||
#ifndef CTRACE_H
|
||||
#define CTRACE_H
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#ifdef _WIN32
|
||||
#define CPPTRACE_EXPORT_ATTR __declspec(dllexport)
|
||||
#define CPPTRACE_IMPORT_ATTR __declspec(dllimport)
|
||||
#else
|
||||
#define CPPTRACE_EXPORT_ATTR __attribute__((visibility("default")))
|
||||
#define CPPTRACE_IMPORT_ATTR __attribute__((visibility("default")))
|
||||
#endif
|
||||
|
||||
#ifdef CPPTRACE_STATIC_DEFINE
|
||||
# define CPPTRACE_EXPORT
|
||||
# define CPPTRACE_NO_EXPORT
|
||||
#else
|
||||
# ifndef CPPTRACE_EXPORT
|
||||
# ifdef cpptrace_lib_EXPORTS
|
||||
/* We are building this library */
|
||||
# define CPPTRACE_EXPORT CPPTRACE_EXPORT_ATTR
|
||||
# else
|
||||
/* We are using this library */
|
||||
# define CPPTRACE_EXPORT CPPTRACE_IMPORT_ATTR
|
||||
# endif
|
||||
# endif
|
||||
#endif
|
||||
|
||||
#if defined(__cplusplus)
|
||||
#define CTRACE_BEGIN_DEFINITIONS extern "C" {
|
||||
#define CTRACE_END_DEFINITIONS }
|
||||
#else
|
||||
#define CTRACE_BEGIN_DEFINITIONS
|
||||
#define CTRACE_END_DEFINITIONS
|
||||
#endif
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#define CTRACE_FORCE_NO_INLINE __declspec(noinline)
|
||||
#else
|
||||
#define CTRACE_FORCE_NO_INLINE __attribute__((noinline))
|
||||
#endif
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#define CTRACE_FORCE_INLINE __forceinline
|
||||
#elif defined(__clang__) || defined(__GNUC__)
|
||||
#define CTRACE_FORCE_INLINE __attribute__((always_inline)) inline
|
||||
#else
|
||||
#define CTRACE_FORCE_INLINE inline
|
||||
#endif
|
||||
|
||||
/* See `CPPTRACE_PATH_MAX` for more info. */
|
||||
#define CTRACE_PATH_MAX 4096
|
||||
|
||||
CTRACE_BEGIN_DEFINITIONS
|
||||
|
||||
typedef struct ctrace_raw_trace ctrace_raw_trace;
|
||||
typedef struct ctrace_object_trace ctrace_object_trace;
|
||||
typedef struct ctrace_stacktrace ctrace_stacktrace;
|
||||
|
||||
/* Represents a boolean value, ensures a consistent ABI. */
|
||||
typedef int8_t ctrace_bool;
|
||||
/* A type that can represent a pointer, alias for `uintptr_t`. */
|
||||
typedef uintptr_t ctrace_frame_ptr;
|
||||
typedef struct ctrace_object_frame ctrace_object_frame;
|
||||
typedef struct ctrace_stacktrace_frame ctrace_stacktrace_frame;
|
||||
typedef struct ctrace_safe_object_frame ctrace_safe_object_frame;
|
||||
|
||||
/* Type-safe null-terminated string wrapper */
|
||||
typedef struct {
|
||||
const char* data;
|
||||
} ctrace_owning_string;
|
||||
|
||||
struct ctrace_object_frame {
|
||||
ctrace_frame_ptr raw_address;
|
||||
ctrace_frame_ptr obj_address;
|
||||
const char* obj_path;
|
||||
};
|
||||
|
||||
struct ctrace_stacktrace_frame {
|
||||
ctrace_frame_ptr raw_address;
|
||||
ctrace_frame_ptr object_address;
|
||||
uint32_t line;
|
||||
uint32_t column;
|
||||
const char* filename;
|
||||
const char* symbol;
|
||||
ctrace_bool is_inline;
|
||||
};
|
||||
|
||||
struct ctrace_safe_object_frame {
|
||||
ctrace_frame_ptr raw_address;
|
||||
ctrace_frame_ptr relative_obj_address;
|
||||
char object_path[CTRACE_PATH_MAX + 1];
|
||||
};
|
||||
|
||||
struct ctrace_raw_trace {
|
||||
ctrace_frame_ptr* frames;
|
||||
size_t count;
|
||||
};
|
||||
|
||||
struct ctrace_object_trace {
|
||||
ctrace_object_frame* frames;
|
||||
size_t count;
|
||||
};
|
||||
|
||||
struct ctrace_stacktrace {
|
||||
ctrace_stacktrace_frame* frames;
|
||||
size_t count;
|
||||
};
|
||||
|
||||
/* ctrace::string: */
|
||||
CPPTRACE_EXPORT ctrace_owning_string ctrace_generate_owning_string(const char* raw_string);
|
||||
CPPTRACE_EXPORT void ctrace_free_owning_string(ctrace_owning_string* string);
|
||||
|
||||
/* ctrace::generation: */
|
||||
CPPTRACE_EXPORT ctrace_raw_trace ctrace_generate_raw_trace(size_t skip, size_t max_depth);
|
||||
CPPTRACE_EXPORT ctrace_object_trace ctrace_generate_object_trace(size_t skip, size_t max_depth);
|
||||
CPPTRACE_EXPORT ctrace_stacktrace ctrace_generate_trace(size_t skip, size_t max_depth);
|
||||
|
||||
/* ctrace::freeing: */
|
||||
CPPTRACE_EXPORT void ctrace_free_raw_trace(ctrace_raw_trace* trace);
|
||||
CPPTRACE_EXPORT void ctrace_free_object_trace(ctrace_object_trace* trace);
|
||||
CPPTRACE_EXPORT void ctrace_free_stacktrace(ctrace_stacktrace* trace);
|
||||
|
||||
/* ctrace::resolve: */
|
||||
CPPTRACE_EXPORT ctrace_stacktrace ctrace_resolve_raw_trace(const ctrace_raw_trace* trace);
|
||||
CPPTRACE_EXPORT ctrace_object_trace ctrace_resolve_raw_trace_to_object_trace(const ctrace_raw_trace* trace);
|
||||
CPPTRACE_EXPORT ctrace_stacktrace ctrace_resolve_object_trace(const ctrace_object_trace* trace);
|
||||
|
||||
/* ctrace::safe: */
|
||||
CPPTRACE_EXPORT size_t ctrace_safe_generate_raw_trace(ctrace_frame_ptr* buffer, size_t size, size_t skip, size_t max_depth);
|
||||
CPPTRACE_EXPORT void ctrace_get_safe_object_frame(ctrace_frame_ptr address, ctrace_safe_object_frame* out);
|
||||
CPPTRACE_EXPORT ctrace_bool can_signal_safe_unwind(void);
|
||||
|
||||
/* ctrace::io: */
|
||||
CPPTRACE_EXPORT ctrace_owning_string ctrace_stacktrace_to_string(const ctrace_stacktrace* trace, ctrace_bool use_color);
|
||||
CPPTRACE_EXPORT void ctrace_print_stacktrace(const ctrace_stacktrace* trace, FILE* to, ctrace_bool use_color);
|
||||
|
||||
/* ctrace::utility: */
|
||||
CPPTRACE_EXPORT ctrace_owning_string ctrace_demangle(const char* mangled);
|
||||
CPPTRACE_EXPORT int ctrace_stdin_fileno(void);
|
||||
CPPTRACE_EXPORT int ctrace_stderr_fileno(void);
|
||||
CPPTRACE_EXPORT int ctrace_stdout_fileno(void);
|
||||
CPPTRACE_EXPORT ctrace_bool ctrace_isatty(int fd);
|
||||
|
||||
CPPTRACE_EXPORT ctrace_object_frame ctrace_get_object_info(const ctrace_stacktrace_frame* frame);
|
||||
|
||||
/* ctrace::config: */
|
||||
typedef enum {
|
||||
/* Only minimal lookup tables */
|
||||
ctrace_prioritize_memory = 0,
|
||||
/* Build lookup tables but don't keep them around between trace calls */
|
||||
ctrace_hybrid = 1,
|
||||
/* Build lookup tables as needed */
|
||||
ctrace_prioritize_speed = 2
|
||||
} ctrace_cache_mode;
|
||||
CPPTRACE_EXPORT void ctrace_set_cache_mode(ctrace_cache_mode mode);
|
||||
CPPTRACE_EXPORT void ctrace_enable_inlined_call_resolution(ctrace_bool enable);
|
||||
|
||||
CTRACE_END_DEFINITIONS
|
||||
|
||||
#endif
|
||||
Reference in new issue
Block a user