Implemented the trace reconstruction + bugs fixed

This commit is contained in:
Nemoumbra 2024-09-08 20:33:14 +03:00
parent 25f6b01d86
commit a26afb4c2f
4 changed files with 43 additions and 6 deletions

View File

@ -307,8 +307,10 @@ void IRFrontend::DoJit(u32 em_address, std::vector<IRInst> &instructions, u32 &m
else {
std::vector<IRInst> block_instructions = code->GetInstructions();
instructions.reserve(block_instructions.capacity());
block_instructions.push_back({ IROp::LogBlockHash, 0, 0, 0, 0 });
std::copy(block_instructions.begin(), block_instructions.end(), std::back_inserter(instructions));
// The first instruction is "Downcount"
instructions.push_back(block_instructions.front());
instructions.push_back({ IROp::LogBlockHash, 0, 0, 0, 0 });
std::copy(block_instructions.begin() + 1, block_instructions.end(), std::back_inserter(instructions));
}
if (logBlocks > 0 && dontLogBlocks == 0) {

View File

@ -53,7 +53,8 @@ void MIPSTracer::prepare_block(MIPSComp::IRBlock* block, MIPSComp::IRBlockCache&
if (it != mipsTracer.hash_to_index.end()) {
u32 index = it->second;
auto ir_ptr = (IRInst*)blocks.GetBlockInstructionPtr(*block);
ir_ptr->constant = index;
ir_ptr[1].constant = index;
return;
}
@ -80,16 +81,46 @@ void MIPSTracer::prepare_block(MIPSComp::IRBlock* block, MIPSComp::IRBlockCache&
hash_to_index.emplace(hash, index);
auto ir_ptr = (IRInst*)blocks.GetBlockInstructionPtr(*block);
ir_ptr->constant = index;
ir_ptr[1].constant = index;
}
bool MIPSTracer::flush_to_file() {
INFO_LOG(Log::JIT, "MIPSTracer ordered to flush the trace to a file...");
// Do nothing for now
output.open(logging_path, std::ios::out);
if (!output) {
WARN_LOG(Log::JIT, "MIPSTracer failed to open the file '%s'", logging_path);
return false;
}
auto trace = executed_blocks.get_content();
for (auto index : trace) {
auto& block_info = trace_info[index];
flush_block_to_file(block_info);
}
INFO_LOG(Log::JIT, "Trace flushed, closing the file...");
output.close();
clear();
return true;
}
void MIPSTracer::flush_block_to_file(TraceBlockInfo& block_info) {
static char buffer[1024];
// The log format is '0x{8 hex digits of the address}: {disassembled line}'
const auto prefix_size = 2 + 8 + 2;
u32 addr = block_info.virt_address;
u32 index = block_info.storage_index;
u32 end_addr = addr + block_info.size;
for (; addr < end_addr; addr += 4, ++index) {
snprintf(buffer, sizeof(buffer), "0x%08x: ", addr);
MIPSDisAsm(storage[index], addr, buffer + prefix_size, sizeof(buffer) - prefix_size, true);
output << std::string(buffer) << "\n";
}
}
void MIPSTracer::start_tracing() {
tracing_enabled = true;
INFO_LOG(Log::JIT, "MIPSTracer enabled");

View File

@ -21,6 +21,7 @@
#include <vector>
#include <string>
#include <iterator>
#include <fstream>
#include "Common/CommonTypes.h"
#include "Core/Opcode.h"
@ -135,6 +136,7 @@ struct MIPSTracer {
TraceBlockStorage storage;
std::string logging_path;
std::ofstream output;
bool tracing_enabled = false;
int in_storage_capacity = 0;
@ -152,6 +154,8 @@ struct MIPSTracer {
}
bool flush_to_file();
void flush_block_to_file(TraceBlockInfo& block);
void initialize(u32 storage_capacity, u32 max_trace_size);
void clear();

View File

@ -2148,7 +2148,7 @@ UI::EventReturn DeveloperToolsScreen::OnMIPSTracerEnabled(UI::EventParams &e) {
u32 trace_size = mipsTracer.in_max_trace_size;
mipsTracer.initialize(capacity, trace_size);
// mipsTracer.start_tracing();
mipsTracer.start_tracing();
}
else {
mipsTracer.stop_tracing();