flatten 20260225

This commit is contained in:
Timothy Prepscius
2026-02-25 12:43:06 -05:00
commit 996151a56a
521 changed files with 46722 additions and 0 deletions

View File

@@ -0,0 +1,81 @@
// License: Modified MIT (NON-AI)
// See the LICENSE file in the root directory for license information.
// Copyright 2025 Timothy Prepscius
#ifdef TJP_CORE_HEADER_ONLY
#pragma once
#endif
#include "../../header_only/compile.h"
#include "../Stack.h"
#include "../../system/System.h"
#include <unwind.h>
#include <dlfcn.h>
#include <iomanip>
#include <sstream>
// https://stackoverflow.com/questions/8115192/android-ndk-getting-the-backtrace
struct BacktraceState
{
void** current;
void** end;
};
TJP_CORE_HEADER_ONLY_INLINE
static _Unwind_Reason_Code unwindCallback(struct _Unwind_Context* context, void* arg)
{
BacktraceState* state = static_cast<BacktraceState*>(arg);
uintptr_t pc = _Unwind_GetIP(context);
if (pc) {
if (state->current == state->end) {
return _URC_END_OF_STACK;
} else {
*state->current++ = reinterpret_cast<void*>(pc);
}
}
return _URC_NO_REASON;
}
TJP_CORE_HEADER_ONLY_INLINE
size_t captureBacktrace(void** buffer, size_t max)
{
BacktraceState state = {buffer, buffer + max};
_Unwind_Backtrace(unwindCallback, &state);
return state.current - buffer;
}
TJP_CORE_HEADER_ONLY_INLINE
void dumpBacktrace(std::ostream& os, void** buffer, size_t count)
{
for (size_t idx = 0; idx < count; ++idx) {
const void* addr = buffer[idx];
const char* symbol = "";
Dl_info info;
if (dladdr(addr, &info) && info.dli_sname) {
symbol = info.dli_sname;
}
os << " #" << std::setw(2) << idx << ": " << addr << " " << symbol << "\n";
}
}
namespace tjp::core {
TJP_CORE_HEADER_ONLY_INLINE
std::string getStackString(int maxStackDepth)
{
auto buffer = (void **)alloca(sizeof(void *) * maxStackDepth);
auto count = captureBacktrace(buffer, maxStackDepth);
std::ostringstream oss;
dumpBacktrace(oss, buffer, count);
return oss.str();
}
} // namespace