Atomic SPU LS capture writes

This commit is contained in:
Eladash 2021-02-23 06:29:11 +02:00 committed by Ivan
parent d4af8dd89a
commit 9ccf39b27f
3 changed files with 28 additions and 7 deletions

View file

@ -765,8 +765,12 @@ static void ppu_check_patch_spu_images(const ppu_segment& seg)
if (g_cfg.core.spu_debug) if (g_cfg.core.spu_debug)
{ {
fs::file dump_file(fs::get_cache_dir() + "/spu_progs/" + vfs::escape(name.substr(name.find_last_of('/') + 1)) + '_' + hash.substr(4) + ".elf", fs::rewrite); fs::pending_file temp(fs::get_cache_dir() + "/spu_progs/" + vfs::escape(name.substr(name.find_last_of('/') + 1)) + '_' + hash.substr(4) + ".elf");
obj.save(dump_file);
if (!temp.file || !(temp.file.write(obj.save()), temp.commit()))
{
ppu_loader.error("Failed to dump SPU program from PPU executable: name='%s', hash=%s", name, hash);
}
} }
// Try to patch each segment, will only succeed if the address exists in SPU local storage // Try to patch each segment, will only succeed if the address exists in SPU local storage

View file

@ -4661,21 +4661,34 @@ bool spu_thread::capture_local_storage() const
}; };
auto elf_path = get_filename(); auto elf_path = get_filename();
fs::file dump_file(elf_path, fs::create + fs::excl + fs::write);
if (!dump_file) if (fs::exists(elf_path))
{ {
// Wait 1 second so current_time_narrow() will return a different string // Wait 1 second so current_time_narrow() will return a different string
std::this_thread::sleep_for(1s); std::this_thread::sleep_for(1s);
if (elf_path = get_filename(); !dump_file.open(elf_path, fs::create + fs::excl + fs::write)) if (elf_path = get_filename(); fs::exists(elf_path))
{ {
spu_log.error("Failed to create '%s' (error=%s)", elf_path, fs::g_tls_error); spu_log.error("Failed to create '%s' (error=%s)", elf_path, fs::g_tls_error);
return false; return false;
} }
} }
spu_exec.save(dump_file); fs::pending_file temp(elf_path);
if (!temp.file)
{
spu_log.error("Failed to create temporary file for '%s' (error=%s)", elf_path, fs::g_tls_error);
return false;
}
temp.file.write(spu_exec.save());
if (!temp.commit(false))
{
spu_log.error("Failed to create rename temporary file to '%s' (error=%s)", elf_path, fs::g_tls_error);
return false;
}
spu_log.success("SPU Local Storage image saved to '%s'", elf_path); spu_log.success("SPU Local Storage image saved to '%s'", elf_path);
return true; return true;

View file

@ -278,8 +278,10 @@ public:
return m_error = elf_error::ok; return m_error = elf_error::ok;
} }
void save(const fs::file& stream) const std::vector<u8> save(std::vector<u8>&& init = std::vector<u8>{}) const
{ {
fs::file stream = fs::make_stream<std::vector<u8>>(std::move(init));
// Write header // Write header
ehdr_t header{}; ehdr_t header{};
header.e_magic = "\177ELF"_u32; header.e_magic = "\177ELF"_u32;
@ -322,6 +324,8 @@ public:
{ {
stream.write(prog.bin); stream.write(prog.bin);
} }
return std::move(static_cast<fs::container_stream<std::vector<u8>>*>(stream.release().get())->obj);
} }
elf_object& clear() elf_object& clear()