Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

crashlog: add method names from bindings #625

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
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
16 changes: 16 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,22 @@ if (PROJECT_IS_TOP_LEVEL)
if (SUPPORTS_W_NO_EVERYTHING)
target_compile_options(geode-loader PRIVATE -Wno-inconsistent-missing-override)
endif()
# On windows, prepare "CodegenData.txt" for embedding it into the binary (used for crashlog symbols)
if (WIN32)
file(STRINGS ${CMAKE_BINARY_DIR}/bindings/bindings/Geode/CodegenData.txt CODEGEN_DATA)
set(CODEGEN_DATA_INCLUDE "#pragma once\n#include <unordered_map>\n\nstatic std::unordered_map<uintptr_t, const char*> s_codegenData = {\n")
foreach(line IN LISTS CODEGEN_DATA)
string(REGEX MATCH "([a-zA-Z0-9:_]+) - 0x([0-9a-fA-F]+)" MATCHED ${line})
if (NOT MATCHED)
continue()
endif()
set(FUNC_NAME ${CMAKE_MATCH_1})
set(FUNC_ADDR ${CMAKE_MATCH_2})
set(CODEGEN_DATA_INCLUDE "${CODEGEN_DATA_INCLUDE}\t{0x${FUNC_ADDR}, \"${FUNC_NAME}\"},\n")
endforeach()
set(CODEGEN_DATA_INCLUDE "${CODEGEN_DATA_INCLUDE}}\;")
file(WRITE ${CMAKE_BINARY_DIR}/bindings/bindings/Geode/CodegenData.hpp ${CODEGEN_DATA_INCLUDE})
endif()
elseif (EXISTS ${GEODE_PLATFORM_BIN_PATH})
target_link_libraries(${PROJECT_NAME} INTERFACE "${GEODE_PLATFORM_BIN_PATH}")
if (NOT GEODE_DISABLE_PRECOMPILED_HEADERS)
Expand Down
28 changes: 28 additions & 0 deletions loader/src/platform/windows/crashlog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include <string>
#include <fmt/core.h>
#include "ehdata_structs.hpp"
#include <Geode/CodegenData.hpp>

using namespace geode::prelude;

Expand Down Expand Up @@ -90,6 +91,18 @@ static Mod* modFromAddress(PVOID exceptionAddress) {
return nullptr;
}

static uintptr_t findMethodStart(void const* address, size_t maxSearch = 0x1000) {
// Backtrack until we hit a 0xCC (int3) instruction
uintptr_t start = reinterpret_cast<uintptr_t>(address);
while (start > reinterpret_cast<uintptr_t>(address) - maxSearch) {
if (*reinterpret_cast<uint8_t*>(start) == 0xCC) {
return start + 1;
}
start--;
}
return reinterpret_cast<uintptr_t>(address);
}

static void printAddr(std::ostream& stream, void const* addr, bool fullPath = true) {
HMODULE module = nullptr;

Expand Down Expand Up @@ -135,6 +148,21 @@ static void printAddr(std::ostream& stream, void const* addr, bool fullPath = tr
}

stream << ")";
} else if (module == GetModuleHandle(nullptr)) {
uintptr_t methodStart = findMethodStart(addr);
if (methodStart == reinterpret_cast<uintptr_t>(addr)) {
return;
}

uintptr_t baseOffset = methodStart - reinterpret_cast<uintptr_t>(module);
uintptr_t methodOffset = reinterpret_cast<uintptr_t>(addr) - methodStart;

auto it = s_codegenData.find(baseOffset);
if (it != s_codegenData.end()) {
stream << " (" << it->second << " + " << std::hex << methodOffset << std::dec << ")";
} else {
stream << " (<method " << std::hex << baseOffset << "> + " << methodOffset << std::dec << ")";
}
}
}
}
Expand Down