From 9c590e215f145139c9f6717e98e2bdf094e82f70 Mon Sep 17 00:00:00 2001 From: "Admiral H. Curtiss" Date: Wed, 25 Nov 2020 02:59:42 +0100 Subject: [PATCH 1/2] IOFile: Add WriteString() method to replace fprintf string writing. --- Source/Core/Common/File.h | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Source/Core/Common/File.h b/Source/Core/Common/File.h index 01b59af257..daa4e90cce 100644 --- a/Source/Core/Common/File.h +++ b/Source/Core/Common/File.h @@ -7,6 +7,7 @@ #include #include #include +#include #include "Common/CommonTypes.h" @@ -67,6 +68,8 @@ public: return WriteArray(reinterpret_cast(data), length); } + bool WriteString(std::string_view str) { return WriteBytes(str.data(), str.size()); } + bool IsOpen() const { return nullptr != m_file; } // m_good is set to false when a read, write or other function fails bool IsGood() const { return m_good; } From 45d4746a5dac65110f073a490a4ad41b948274f9 Mon Sep 17 00:00:00 2001 From: "Admiral H. Curtiss" Date: Wed, 25 Nov 2020 03:00:20 +0100 Subject: [PATCH 2/2] IOFile: Replace all fprintf string writing with calls to WriteString. --- Source/Core/Core/PowerPC/Jit64/Jit.cpp | 2 +- Source/Core/Core/PowerPC/JitInterface.cpp | 16 ++++++++----- Source/Core/Core/PowerPC/PPCSymbolDB.cpp | 24 ++++++++++--------- Source/Core/Core/PowerPC/PPCTables.cpp | 10 ++++---- .../PowerPC/SignatureDB/CSVSignatureDB.cpp | 8 ++++--- 5 files changed, 34 insertions(+), 26 deletions(-) diff --git a/Source/Core/Core/PowerPC/Jit64/Jit.cpp b/Source/Core/Core/PowerPC/Jit64/Jit.cpp index 24fef0f553..efaec810f0 100644 --- a/Source/Core/Core/PowerPC/Jit64/Jit.cpp +++ b/Source/Core/Core/PowerPC/Jit64/Jit.cpp @@ -473,7 +473,7 @@ static void ImHere() if (!f) f.Open("log64.txt", "w"); - fprintf(f.GetHandle(), "%08x\n", PC); + f.WriteString(fmt::format("{0:08x}\n", PC)); } if (been_here.find(PC) != been_here.end()) { diff --git a/Source/Core/Core/PowerPC/JitInterface.cpp b/Source/Core/Core/PowerPC/JitInterface.cpp index 10ebd1ac9f..288dbce066 100644 --- a/Source/Core/Core/PowerPC/JitInterface.cpp +++ b/Source/Core/Core/PowerPC/JitInterface.cpp @@ -16,6 +16,8 @@ #include "Common/PerformanceCounter.h" #endif +#include + #include "Common/ChunkFile.h" #include "Common/CommonTypes.h" #include "Common/File.h" @@ -103,17 +105,19 @@ void WriteProfileResults(const std::string& filename) PanicAlert("Failed to open %s", filename.c_str()); return; } - fprintf(f.GetHandle(), "origAddr\tblkName\trunCount\tcost\ttimeCost\tpercent\ttimePercent\tOvAlli" - "nBlkTime(ms)\tblkCodeSize\n"); + f.WriteString("origAddr\tblkName\trunCount\tcost\ttimeCost\tpercent\ttimePercent\tOvAllinBlkTime(" + "ms)\tblkCodeSize\n"); for (auto& stat : prof_stats.block_stats) { std::string name = g_symbolDB.GetDescription(stat.addr); double percent = 100.0 * (double)stat.cost / (double)prof_stats.cost_sum; double timePercent = 100.0 * (double)stat.tick_counter / (double)prof_stats.timecost_sum; - fprintf(f.GetHandle(), - "%08x\t%s\t%" PRIu64 "\t%" PRIu64 "\t%" PRIu64 "\t%.2f\t%.2f\t%.2f\t%i\n", stat.addr, - name.c_str(), stat.run_count, stat.cost, stat.tick_counter, percent, timePercent, - (double)stat.tick_counter * 1000.0 / (double)prof_stats.countsPerSec, stat.block_size); + f.WriteString(fmt::format("{0:08x}\t{1}\t{2}\t{3}\t{4}\t{5:.2f}\t{6:.2f}\t{7:.2f}\t{8}\n", + stat.addr, name, stat.run_count, stat.cost, stat.tick_counter, + percent, timePercent, + static_cast(stat.tick_counter) * 1000.0 / + static_cast(prof_stats.countsPerSec), + stat.block_size)); } } diff --git a/Source/Core/Core/PowerPC/PPCSymbolDB.cpp b/Source/Core/Core/PowerPC/PPCSymbolDB.cpp index 30a2f5b857..3b6410d9a2 100644 --- a/Source/Core/Core/PowerPC/PPCSymbolDB.cpp +++ b/Source/Core/Core/PowerPC/PPCSymbolDB.cpp @@ -12,6 +12,8 @@ #include #include +#include + #include "Common/CommonTypes.h" #include "Common/File.h" #include "Common/Logging/Log.h" @@ -438,21 +440,21 @@ bool PPCSymbolDB::SaveSymbolMap(const std::string& filename) const } // Write .text section - fprintf(f.GetHandle(), ".text section layout\n"); + f.WriteString(".text section layout\n"); for (const auto& symbol : function_symbols) { // Write symbol address, size, virtual address, alignment, name - fprintf(f.GetHandle(), "%08x %08x %08x %i %s\n", symbol->address, symbol->size, symbol->address, - 0, symbol->name.c_str()); + f.WriteString(fmt::format("{0:08x} {1:08x} {2:08x} {3} {4}\n", symbol->address, symbol->size, + symbol->address, 0, symbol->name)); } // Write .data section - fprintf(f.GetHandle(), "\n.data section layout\n"); + f.WriteString("\n.data section layout\n"); for (const auto& symbol : data_symbols) { // Write symbol address, size, virtual address, alignment, name - fprintf(f.GetHandle(), "%08x %08x %08x %i %s\n", symbol->address, symbol->size, symbol->address, - 0, symbol->name.c_str()); + f.WriteString(fmt::format("{0:08x} {1:08x} {2:08x} {3} {4}\n", symbol->address, symbol->size, + symbol->address, 0, symbol->name)); } return true; @@ -471,7 +473,7 @@ bool PPCSymbolDB::SaveCodeMap(const std::string& filename) const return false; // Write ".text" at the top - fprintf(f.GetHandle(), ".text\n"); + f.WriteString(".text\n"); u32 next_address = 0; for (const auto& function : m_functions) @@ -482,20 +484,20 @@ bool PPCSymbolDB::SaveCodeMap(const std::string& filename) const if (symbol.address + symbol.size <= next_address) { // At least write the symbol name and address - fprintf(f.GetHandle(), "// %08x beginning of %s\n", symbol.address, symbol.name.c_str()); + f.WriteString(fmt::format("// {0:08x} beginning of {1}\n", symbol.address, symbol.name)); continue; } // Write the symbol full name - fprintf(f.GetHandle(), "\n%s:\n", symbol.name.c_str()); + f.WriteString(fmt::format("\n{0}:\n", symbol.name)); next_address = symbol.address + symbol.size; // Write the code for (u32 address = symbol.address; address < next_address; address += 4) { const std::string disasm = debugger->Disassemble(address); - fprintf(f.GetHandle(), "%08x %-*.*s %s\n", address, SYMBOL_NAME_LIMIT, SYMBOL_NAME_LIMIT, - symbol.name.c_str(), disasm.c_str()); + f.WriteString(fmt::format("{0:08x} {1:<{2}.{3}} {4}\n", address, symbol.name, + SYMBOL_NAME_LIMIT, SYMBOL_NAME_LIMIT, disasm)); } } return true; diff --git a/Source/Core/Core/PowerPC/PPCTables.cpp b/Source/Core/Core/PowerPC/PPCTables.cpp index dd100aabac..36c17c88b8 100644 --- a/Source/Core/Core/PowerPC/PPCTables.cpp +++ b/Source/Core/Core/PowerPC/PPCTables.cpp @@ -171,8 +171,8 @@ void LogCompiledInstructions() GekkoOPInfo* pInst = m_allInstructions[i]; if (pInst->compileCount > 0) { - fprintf(f.GetHandle(), "%s\t%i\t%" PRId64 "\t%08x\n", pInst->opname, pInst->compileCount, - pInst->runCount, pInst->lastUse); + f.WriteString(fmt::format("{0}\t{1}\t{2}\t{3:08x}\n", pInst->opname, pInst->compileCount, + pInst->runCount, pInst->lastUse)); } } @@ -182,8 +182,8 @@ void LogCompiledInstructions() GekkoOPInfo* pInst = m_allInstructions[i]; if (pInst->compileCount == 0) { - fprintf(f.GetHandle(), "%s\t%i\t%" PRId64 "\n", pInst->opname, pInst->compileCount, - pInst->runCount); + f.WriteString( + fmt::format("{0}\t{1}\t{2}\n", pInst->opname, pInst->compileCount, pInst->runCount)); } } @@ -191,7 +191,7 @@ void LogCompiledInstructions() f.Open(fmt::format("{}" OP_TO_LOG "_at{}.txt", File::GetUserPath(D_LOGS_IDX), time), "w"); for (auto& rsplocation : rsplocations) { - fprintf(f.GetHandle(), OP_TO_LOG ": %08x\n", rsplocation); + f.WriteString(fmt::format(OP_TO_LOG ": {0:08x}\n", rsplocation)); } #endif diff --git a/Source/Core/Core/PowerPC/SignatureDB/CSVSignatureDB.cpp b/Source/Core/Core/PowerPC/SignatureDB/CSVSignatureDB.cpp index 12fe486e46..757f588022 100644 --- a/Source/Core/Core/PowerPC/SignatureDB/CSVSignatureDB.cpp +++ b/Source/Core/Core/PowerPC/SignatureDB/CSVSignatureDB.cpp @@ -8,6 +8,8 @@ #include #include +#include + #include "Common/File.h" #include "Common/FileUtil.h" #include "Common/Logging/Log.h" @@ -70,9 +72,9 @@ bool CSVSignatureDB::Save(const std::string& file_path) const { // The object name/location are unused for the time being. // To be implemented. - fprintf(f.GetHandle(), "%08x\t%08x\t%s\t%s\t%s\n", func.first, func.second.size, - func.second.name.c_str(), func.second.object_location.c_str(), - func.second.object_name.c_str()); + f.WriteString(fmt::format("{0:08x}\t{1:08x}\t{2}\t{3}\t{4}\n", func.first, func.second.size, + func.second.name, func.second.object_location, + func.second.object_name)); } INFO_LOG(SYMBOLS, "CSV database save successful");