diff --git a/runtime/shims/d3d12_xs_device.cpp b/runtime/shims/d3d12_xs_device.cpp index 187406e3d8..50995c4b6e 100644 --- a/runtime/shims/d3d12_xs_device.cpp +++ b/runtime/shims/d3d12_xs_device.cpp @@ -36,6 +36,7 @@ #include #include #include +#include #include #include #include @@ -86,6 +87,18 @@ struct HeapProxy { std::atomic g_hits[kMaxSlots]; std::atomic g_first[kMaxSlots]; +// The return address of the first call of each slot that reached the generic forwarding branch (0: none yet). +std::atomic g_first_caller[kMaxSlots]; +// Names a caller address (set_device_slot_caller_formatter); null: the bare address in hex. gta6rt sets +// rt::shims::module_offset, so this file does not depend on the module table and test targets link it alone. +std::atomic g_caller_formatter{nullptr}; + +std::string format_caller(std::uint64_t address) { + if (const CallerFormatter formatter = g_caller_formatter.load(std::memory_order_relaxed)) return formatter(address); + char text[32]; + std::snprintf(text, sizeof(text), "0x%llx", static_cast(address)); + return text; +} std::atomic g_beyond_warned[kMaxSlots]; std::atomic g_heap_hits[kHeapSlots]; std::atomic g_heap_first[kHeapSlots]; @@ -155,11 +168,18 @@ const char* slot_name(unsigned slot) { } } -void note_slot(unsigned slot) { +// caller: the title code that called the slot, for the slots the proxy forwards by number (RT-526). The first call +// then also names it as module+offset, so a new Xbox-only slot can be found in the executable without a debugger. +void note_slot(unsigned slot, const void* caller = nullptr) { if (slot >= kMaxSlots) return; count_hit(g_hits[slot]); const bool first = first_call(g_first[slot]); - if (first) { + if (first && caller) { + const auto address = reinterpret_cast(caller); + g_first_caller[slot].store(address, std::memory_order_relaxed); + std::fprintf(stderr, "[rt] d3d12_xs: device slot %u (%s) first call from %s\n", slot, slot_name(slot), + format_caller(address).c_str()); + } else if (first) { std::fprintf(stderr, "[rt] d3d12_xs: device slot %u (%s) first call\n", slot, slot_name(slot)); } else if (slot >= 3 && trace_enabled()) { std::fprintf(stderr, "[rt] d3d12_xs: device slot %u (%s) call\n", slot, slot_name(slot)); @@ -1865,7 +1885,7 @@ std::uint64_t WINAPI proxy_slot(DeviceProxy* self, std::uint64_t a1, std::uint64 reinterpret_cast(a5), reinterpret_cast(a6), reinterpret_cast(a7))); } else { - note_slot(Slot); + note_slot(Slot, _ReturnAddress()); // Xbox device slots, before the PC vtable is indexed: the public HLE's 78/79/80 would otherwise reach // CreateReservedResource2/CreateSampler2 or fall past an 80-slot table, and 48/73 would reach unrelated PC // methods. The check is before the slot-count refusal because slot 80 is outside a PC ID3D12Device11 table. @@ -2162,6 +2182,15 @@ bool device_slot_first_call_seen(unsigned slot) { return slot < kMaxSlots && g_first[slot].load(std::memory_order_relaxed); } +std::string device_slot_first_caller(unsigned slot) { + const std::uint64_t address = slot < kMaxSlots ? g_first_caller[slot].load(std::memory_order_relaxed) : 0; + return address ? format_caller(address) : std::string(); +} + +void set_device_slot_caller_formatter(CallerFormatter formatter) { + g_caller_formatter.store(formatter, std::memory_order_relaxed); +} + unsigned long device_proxy_ref_count(const void* device) { if (!is_device_proxy(device)) return 0; return static_cast(device)->refs.load(std::memory_order_relaxed); @@ -2201,6 +2230,7 @@ void reset_device_proxy_stats() { for (unsigned slot = 0; slot < kMaxSlots; ++slot) { g_hits[slot].store(0, std::memory_order_relaxed); g_first[slot].store(false, std::memory_order_relaxed); + g_first_caller[slot].store(0, std::memory_order_relaxed); g_beyond_warned[slot].store(false, std::memory_order_relaxed); } for (unsigned slot = 0; slot < kHeapSlots; ++slot) { diff --git a/runtime/shims/d3d12_xs_device.h b/runtime/shims/d3d12_xs_device.h index ac0437215d..04e448a6ed 100644 --- a/runtime/shims/d3d12_xs_device.h +++ b/runtime/shims/d3d12_xs_device.h @@ -51,6 +51,7 @@ #include #include +#include namespace rt::d3d12_xs { @@ -193,6 +194,16 @@ unsigned long device_proxy_ref_count(const void* device); // counts. Calls made before that are not counted (the counter stays 0); they set only the first-call flags. std::uint64_t device_slot_hits(unsigned slot); bool device_slot_first_call_seen(unsigned slot); +// For a slot that enters the proxy's generic branch (no compile-time handler of its own; Xbox slots routed at run +// time and slots refused past the PC table included): the caller of its first call since the last +// reset_device_proxy_stats, named by the formatter below, the same text the first-call log line shows; "" before +// one. Diagnostic only: the first-call flag is set before the caller is stored, so a concurrent reader can see +// device_slot_first_call_seen true with this still "". +std::string device_slot_first_caller(unsigned slot); +// Names a caller address in the first-call log line and device_slot_first_caller. gta6rt sets +// rt::shims::module_offset (module+offset); with none set (the default, and in tests) the address is shown in hex. +using CallerFormatter = std::string (*)(std::uint64_t address); +void set_device_slot_caller_formatter(CallerFormatter formatter); std::uint64_t heap_slot_hits(unsigned slot); bool heap_slot_first_call_seen(unsigned slot); void reset_device_proxy_stats(); diff --git a/runtime/src/main.cpp b/runtime/src/main.cpp index fbb4b3043e..0e49736e62 100644 --- a/runtime/src/main.cpp +++ b/runtime/src/main.cpp @@ -167,6 +167,8 @@ int wmain(int argc, wchar_t** argv) { rt::d3d12_trace::set_enabled(trace_d3d12); // Slot 133 imports the title's pages; xmem commits them on first touch, so they are committed first. rt::d3d12_xs::set_placed_at_va_pages_hook(&rt::xmem::commit_physical_range); + // The first call of a forwarded device slot names its caller as module+offset (RT-526). + rt::d3d12_xs::set_device_slot_caller_formatter(&rt::shims::module_offset); if (int trace_error = 0; !trace_file.empty() && !rt::startup_trace::open(trace_file.c_str(), &trace_error)) { std::fprintf(stderr, trace_error == EEXIST ? "error: the trace file already exists; --trace only creates a new file\n" : "error: cannot create the trace file\n"); diff --git a/runtime/test/xs_proxy_mock_test.cpp b/runtime/test/xs_proxy_mock_test.cpp index 6af7ad8046..07394777fd 100644 --- a/runtime/test/xs_proxy_mock_test.cpp +++ b/runtime/test/xs_proxy_mock_test.cpp @@ -23,6 +23,7 @@ #include #include #include +#include #include #include @@ -824,6 +825,13 @@ int check_xbox_slots() { // 78 SetFrameIntervalX(output, interval_us, latency, flags): S_OK, not forwarded to PC slot 78. EXPECT(hr_of(raw(device, 78, 0, 16667, 1, 0)) == S_OK); EXPECT(mock->calls[78] == 0 && xs::device_slot_hits(78) == 1); + // RT-526: the first call of a slot in the generic branch keeps its caller (no formatter here: hex), a reset + // forgets it with the first-call flag, and the next call records it again. + EXPECT(xs::device_slot_first_call_seen(78) && xs::device_slot_first_caller(78).rfind("0x", 0) == 0); + xs::reset_device_proxy_stats(); + EXPECT(!xs::device_slot_first_call_seen(78) && xs::device_slot_first_caller(78).empty()); + EXPECT(hr_of(raw(device, 78, 0, 16667, 1, 0)) == S_OK); + EXPECT(xs::device_slot_first_call_seen(78) && xs::device_slot_first_caller(78).rfind("0x", 0) == 0); // 79 ScheduleFrameEventX(event, offset_us, pEvent, flags): S_OK, not forwarded to PC slot 79. EXPECT(hr_of(raw(device, 79, 0, 0, 0x1234, 0)) == S_OK); EXPECT(mock->calls[79] == 0 && xs::device_slot_hits(79) == 1);