Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions core/libs/prx/libc/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ target_sources(libc PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/src/CreationMask.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/specifics/gcc/StackGuard.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/specifics/windows/CrashReport.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/specifics/windows/CallTrace.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/specifics/linux/DlIteratePhdr.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/specifics/macos/GuestImages.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/Strings.cpp
Expand Down Expand Up @@ -411,6 +412,18 @@ if(BUILD_TESTING)
set_tests_properties(guest_errors PROPERTIES ENVIRONMENT_MODIFICATION
"PATH=path_list_prepend:$<TARGET_FILE_DIR:libc>")
endif()
if(WIN32)
add_test_executable(guest_call_trace_tests ${LIBS_INCLUDE_DIR}/tests/GuestCallTrace.cpp)
target_include_directories(guest_call_trace_tests PRIVATE ${LIBS_INCLUDE_DIR})
target_link_libraries(guest_call_trace_tests PRIVATE libc)
configure_windows_unwind(guest_call_trace_tests)
add_test(NAME guest_call_trace COMMAND guest_call_trace_tests)
add_test(NAME guest_call_trace_abort COMMAND guest_call_trace_tests abort)
set_tests_properties(guest_call_trace guest_call_trace_abort PROPERTIES ENVIRONMENT_MODIFICATION
"PATH=path_list_prepend:$<TARGET_FILE_DIR:libc>")
set_tests_properties(guest_call_trace_abort PROPERTIES TIMEOUT 20 PASS_REGULAR_EXPRESSION
"last traced system library calls[^~]*#1 thread [0-9]+ libTraced.prx Integers from [^~]*guest_call_trace_tests.exe[^~]*#2 thread [0-9]+ libOther.prx Blocks from[^~]*most called traced imports \\(2 calls in total\\):[^~]* 1 libTraced.prx Integers[^~]* 1 libOther.prx Blocks")
endif()
add_test_executable(guest_syslog_tests ${LIBS_INCLUDE_DIR}/tests/GuestSyslog.cpp)
target_include_directories(guest_syslog_tests PRIVATE ${LIBS_INCLUDE_DIR})
target_link_libraries(guest_syslog_tests PRIVATE libc)
Expand Down
27 changes: 27 additions & 0 deletions core/libs/prx/libc/include/CallTrace.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#ifndef CORE_LIBS_PRX_LIBC_INCLUDE_CALLTRACE_HPP
#define CORE_LIBS_PRX_LIBC_INCLUDE_CALLTRACE_HPP

#include <cstddef>
#include <cstdint>

struct CallTraceEntry {
std::uint64_t sequence;
std::uint64_t thread;
std::uint64_t returnAddress;
const char* name;
const char* library;
};

struct CallTraceCount {
std::uint64_t calls;
const char* name;
const char* library;
};

extern "C" {
void* TraceImport_nid_no_patch(void* target, const char* name, const char* library);
std::size_t CopyCallTrace_nid_no_patch(CallTraceEntry* entries, std::size_t capacity);
std::size_t CopyCallCounts_nid_no_patch(CallTraceCount* entries, std::size_t capacity, std::uint64_t* total);
}

#endif
202 changes: 202 additions & 0 deletions core/libs/prx/libc/src/specifics/windows/CallTrace.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,202 @@
#ifdef _WIN32
#include <windows.h>
#include "prx/libc/include/CallTrace.hpp"
#include "prx/libc/include/general/AsmMacros.hpp"
#include "prx/libc/include/general/VabiMacros.hpp"
#include <algorithm>
#include <atomic>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <memory>
#include <mutex>
#include <stdexcept>
#include <string>
#include <string_view>
#include <system_error>
#include <unordered_map>

extern "C" void CallTraceEnter_nid_no_patch();

namespace {

struct ImportRecord {
void* target;
std::string name;
std::string library;
mutable std::atomic<std::uint64_t> calls{0};
};

struct RingSlot {
std::atomic<std::uint64_t> sequence{0};
std::atomic<std::uint64_t> thread{0};
std::atomic<std::uint64_t> returnAddress{0};
std::atomic<const ImportRecord*> record{nullptr};
};

constexpr std::size_t RingCapacity = 65536;
constexpr std::size_t ThunkBytes = 24;
constexpr std::size_t ThunkAreaBytes = 1024 * 1024;

std::mutex g_mutex;
std::deque<ImportRecord> g_records;
std::unordered_map<void*, void*> g_thunkByTarget;
std::unique_ptr<RingSlot[]> g_ring;
std::atomic<std::uint64_t> g_sequence{0};
std::uint8_t* g_writable = nullptr;
std::uint8_t* g_executable = nullptr;
std::size_t g_thunks = 0;

bool Selected(std::string_view library) {
const char* value = std::getenv("APS5_TRACE_CALLS");
if (value == nullptr || *value == '\0') return false;
std::string_view rest(value);
if (library.ends_with(".prx")) library.remove_suffix(4);
while (!rest.empty()) {
const auto comma = rest.find(',');
auto token = rest.substr(0, comma);
rest = comma == std::string_view::npos ? std::string_view{} : rest.substr(comma + 1);
if (token.ends_with(".prx")) token.remove_suffix(4);
if (token == "all" || token == library) return true;
}
return false;
}

void MapThunkArea() {
const HANDLE section = CreateFileMappingW(INVALID_HANDLE_VALUE, nullptr, PAGE_EXECUTE_READWRITE, 0, static_cast<DWORD>(ThunkAreaBytes), nullptr);
if (section == nullptr) throw std::system_error(static_cast<int>(GetLastError()), std::system_category(), "call trace: CreateFileMappingW");
g_writable = static_cast<std::uint8_t*>(MapViewOfFile(section, FILE_MAP_WRITE, 0, 0, ThunkAreaBytes));
g_executable = static_cast<std::uint8_t*>(MapViewOfFile(section, FILE_MAP_READ | FILE_MAP_EXECUTE, 0, 0, ThunkAreaBytes));
const DWORD code = GetLastError();
CloseHandle(section);
if (g_writable == nullptr || g_executable == nullptr) throw std::system_error(static_cast<int>(code), std::system_category(), "call trace: MapViewOfFile");
}

void* WriteThunk(const ImportRecord& record) {
if ((g_thunks + 1) * ThunkBytes > ThunkAreaBytes) throw std::runtime_error("call trace: thunk area exhausted");
const std::size_t offset = g_thunks * ThunkBytes;
const std::uint8_t prefix[] = {0x49, 0xbb};
const std::uint8_t jump[] = {0xff, 0x25, 0, 0, 0, 0};
const auto recordAddress = reinterpret_cast<std::uint64_t>(&record);
const auto handler = reinterpret_cast<std::uint64_t>(&CallTraceEnter_nid_no_patch);
std::uint8_t* thunk = g_writable + offset;
std::memcpy(thunk, prefix, sizeof(prefix));
std::memcpy(thunk + 2, &recordAddress, sizeof(recordAddress));
std::memcpy(thunk + 10, jump, sizeof(jump));
std::memcpy(thunk + 16, &handler, sizeof(handler));
FlushInstructionCache(GetCurrentProcess(), g_executable + offset, ThunkBytes);
++g_thunks;
return g_executable + offset;
}

}

static_assert(offsetof(ImportRecord, target) == 0);

extern "C" {

void APS5_VABI CallTraceRecord_nid_no_patch(const void* record, std::uint64_t returnAddress) {
static_cast<const ImportRecord*>(record)->calls.fetch_add(1, std::memory_order_relaxed);
const std::uint64_t sequence = g_sequence.fetch_add(1, std::memory_order_relaxed) + 1;
RingSlot& slot = g_ring[sequence % RingCapacity];
slot.sequence.store(0, std::memory_order_release);
slot.thread.store(GetCurrentThreadId(), std::memory_order_relaxed);
slot.returnAddress.store(returnAddress, std::memory_order_relaxed);
slot.record.store(static_cast<const ImportRecord*>(record), std::memory_order_relaxed);
slot.sequence.store(sequence, std::memory_order_release);
}

void* TraceImport_nid_no_patch(void* target, const char* name, const char* library) {
if (target == nullptr || name == nullptr || library == nullptr || !Selected(library)) return target;
std::lock_guard lock(g_mutex);
if (g_ring == nullptr) {
MapThunkArea();
g_ring = std::make_unique<RingSlot[]>(RingCapacity);
}
if (const auto existing = g_thunkByTarget.find(target); existing != g_thunkByTarget.end()) return existing->second;
g_records.emplace_back(target, name, library);
void* thunk = WriteThunk(g_records.back());
g_thunkByTarget.emplace(target, thunk);
return thunk;
}

std::size_t CopyCallTrace_nid_no_patch(CallTraceEntry* entries, std::size_t capacity) {
if (entries == nullptr || capacity == 0 || g_ring == nullptr) return 0;
const std::uint64_t last = g_sequence.load(std::memory_order_acquire);
const std::uint64_t span = std::min<std::uint64_t>({last, capacity, RingCapacity});
std::size_t count = 0;
for (std::uint64_t sequence = last - span + 1; sequence <= last; ++sequence) {
const RingSlot& slot = g_ring[sequence % RingCapacity];
if (slot.sequence.load(std::memory_order_acquire) != sequence) continue;
const auto* record = slot.record.load(std::memory_order_relaxed);
const CallTraceEntry entry{sequence, slot.thread.load(std::memory_order_relaxed), slot.returnAddress.load(std::memory_order_relaxed), record->name.c_str(), record->library.c_str()};
if (slot.sequence.load(std::memory_order_acquire) != sequence) continue;
entries[count++] = entry;
}
return count;
}

std::size_t CopyCallCounts_nid_no_patch(CallTraceCount* entries, std::size_t capacity, std::uint64_t* total) {
if (total != nullptr) *total = g_sequence.load(std::memory_order_acquire);
if (entries == nullptr || capacity == 0) return 0;
std::unique_lock lock(g_mutex, std::try_to_lock);
if (!lock.owns_lock()) return 0;
std::size_t count = 0;
for (const auto& record : g_records) {
const std::uint64_t calls = record.calls.load(std::memory_order_relaxed);
if (calls == 0 || (count == capacity && calls <= entries[count - 1].calls)) continue;
std::size_t position = count < capacity ? count++ : count - 1;
while (position > 0 && entries[position - 1].calls < calls) {
entries[position] = entries[position - 1];
--position;
}
entries[position] = {calls, record.name.c_str(), record.library.c_str()};
}
return count;
}

}

asm(".text\n"
APS5_ASM_FUNCTION("CallTraceEnter_nid_no_patch")
" push %rdi\n"
" push %rsi\n"
" push %rdx\n"
" push %rcx\n"
" push %r8\n"
" push %r9\n"
" push %rax\n"
" push %r10\n"
" push %r11\n"
" sub $128, %rsp\n"
" movdqu %xmm0, 0(%rsp)\n"
" movdqu %xmm1, 16(%rsp)\n"
" movdqu %xmm2, 32(%rsp)\n"
" movdqu %xmm3, 48(%rsp)\n"
" movdqu %xmm4, 64(%rsp)\n"
" movdqu %xmm5, 80(%rsp)\n"
" movdqu %xmm6, 96(%rsp)\n"
" movdqu %xmm7, 112(%rsp)\n"
" mov %r11, %rdi\n"
" mov 200(%rsp), %rsi\n"
" call CallTraceRecord_nid_no_patch\n"
" movdqu 0(%rsp), %xmm0\n"
" movdqu 16(%rsp), %xmm1\n"
" movdqu 32(%rsp), %xmm2\n"
" movdqu 48(%rsp), %xmm3\n"
" movdqu 64(%rsp), %xmm4\n"
" movdqu 80(%rsp), %xmm5\n"
" movdqu 96(%rsp), %xmm6\n"
" movdqu 112(%rsp), %xmm7\n"
" add $128, %rsp\n"
" pop %r11\n"
" pop %r10\n"
" pop %rax\n"
" pop %r9\n"
" pop %r8\n"
" pop %rcx\n"
" pop %rdx\n"
" pop %rsi\n"
" pop %rdi\n"
" jmp *(%r11)\n");
#endif
26 changes: 26 additions & 0 deletions core/libs/prx/libc/src/specifics/windows/CrashReport.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#ifdef _WIN32
#include <windows.h>
#include <tlhelp32.h>
#include "prx/libc/include/CallTrace.hpp"
#include "prx/libc/include/GuestArena.hpp"
#include <atomic>
#include <chrono>
Expand All @@ -10,6 +11,7 @@
#include <cstdarg>
#include <cstdlib>
#include <cstring>
#include <iterator>
#include <utility>
#include <mutex>
#include "Sse4aEmulation.hpp"
Expand All @@ -32,6 +34,7 @@ void Report(const char* format, ...) {

void ReportAllThreads();
void ReportStackCandidates(std::uint64_t rsp, int limit, std::uint64_t span);
void ReportCallTrace();

void DescribeAddress(std::uint64_t address, char* buffer, std::size_t size) {
HMODULE module = nullptr;
Expand Down Expand Up @@ -325,12 +328,34 @@ LONG WINAPI ReportCrash(EXCEPTION_POINTERS* info) {
}
}
}
ReportCallTrace();
std::fflush(stderr);
ReportAllThreads();
std::fflush(stderr);
return EXCEPTION_CONTINUE_SEARCH;
}

void ReportCallTrace() {
static CallTraceEntry entries[64];
const std::size_t count = CopyCallTrace_nid_no_patch(entries, std::size(entries));
if (count == 0) return;
char line[MAX_PATH + 64];
Report(" last traced system library calls (APS5_TRACE_CALLS):\n");
for (std::size_t index = 0; index < count; ++index) {
DescribeAddress(entries[index].returnAddress, line, sizeof(line));
Report(" #%llu thread %llu %s %s from %s\n", static_cast<unsigned long long>(entries[index].sequence),
static_cast<unsigned long long>(entries[index].thread), entries[index].library, entries[index].name, line);
}
static CallTraceCount counts[32];
std::uint64_t total = 0;
const std::size_t counted = CopyCallCounts_nid_no_patch(counts, std::size(counts), &total);
if (counted == 0) return;
Report(" most called traced imports (%llu calls in total):\n", static_cast<unsigned long long>(total));
for (std::size_t index = 0; index < counted; ++index) {
Report(" %12llu %s %s\n", static_cast<unsigned long long>(counts[index].calls), counts[index].library, counts[index].name);
}
}

std::size_t CaptureFrames(const CONTEXT* context, std::uint64_t* frames, std::size_t capacity) {
std::size_t count = 0;
std::uint64_t frame = context->Rbp;
Expand Down Expand Up @@ -451,6 +476,7 @@ void ReportBacktrace(const char* what, bool reportThreads) {
Report(" #%u %s\n", static_cast<unsigned>(i), line);
}
ReportStackCandidates(reinterpret_cast<std::uint64_t>(__builtin_frame_address(0)), 32, 0x4000);
ReportCallTrace();
if (reportThreads) ReportAllThreads();
std::fflush(stderr);
}
Expand Down
Loading
Loading