loader: Move NSO module tracking to AppLoader

Also cleanup of general stuff
This commit is contained in:
Zach Hilman 2019-05-26 11:40:41 -04:00
parent cdf52b9374
commit b77fde7c5c
22 changed files with 147 additions and 80 deletions

View file

@ -9,6 +9,7 @@
#include "common/logging/log.h"
#include "core/arm/arm_interface.h"
#include "core/core.h"
#include "core/loader/loader.h"
#include "core/memory.h"
namespace Core {
@ -80,15 +81,17 @@ Symbols GetSymbols(VAddr text_offset) {
const auto value = Memory::Read64(dynamic_index + 0x8);
dynamic_index += 0x10;
if (tag == ELF_DYNAMIC_TAG_NULL)
if (tag == ELF_DYNAMIC_TAG_NULL) {
break;
}
if (tag == ELF_DYNAMIC_TAG_STRTAB)
if (tag == ELF_DYNAMIC_TAG_STRTAB) {
string_table_offset = value;
else if (tag == ELF_DYNAMIC_TAG_SYMTAB)
} else if (tag == ELF_DYNAMIC_TAG_SYMTAB) {
symbol_table_offset = value;
else if (tag == ELF_DYNAMIC_TAG_SYMENT)
} else if (tag == ELF_DYNAMIC_TAG_SYMENT) {
symbol_entry_size = value;
}
}
if (string_table_offset == 0 || symbol_table_offset == 0 || symbol_entry_size == 0) {
@ -126,8 +129,10 @@ std::optional<std::string> GetSymbolName(const Symbols& symbols, VAddr func_addr
return func_address >= symbol.value && func_address < end_address;
});
if (iter == symbols.end())
if (iter == symbols.end()) {
return std::nullopt;
}
return iter->second;
}
@ -150,7 +155,12 @@ std::vector<ARM_Interface::BacktraceEntry> ARM_Interface::GetBacktrace() const {
fp = Memory::Read64(fp);
}
const auto& modules{System::GetInstance().GetRegisteredNSOModules()};
std::map<VAddr, std::string> modules;
auto& loader{System::GetInstance().GetAppLoader()};
if (loader.ReadNSOModules(modules) != Loader::ResultStatus::Success) {
return {};
}
std::map<std::string, Symbols> symbols;
for (const auto& module : modules) {
symbols.insert_or_assign(module.second, GetSymbols(module.first));
@ -158,7 +168,8 @@ std::vector<ARM_Interface::BacktraceEntry> ARM_Interface::GetBacktrace() const {
for (auto& entry : out) {
VAddr base = 0;
for (const auto& module : modules) {
for (auto iter = modules.rbegin(); iter != modules.rend(); ++iter) {
const auto& module{*iter};
if (entry.original_address >= module.first) {
entry.module = module.second;
base = module.first;
@ -191,7 +202,7 @@ void ARM_Interface::LogBacktrace() const {
LOG_ERROR(Core_ARM, "Backtrace, sp={:016X}, pc={:016X}", sp, pc);
LOG_ERROR(Core_ARM, "{:20}{:20}{:20}{:20}{}", "Module Name", "Address", "Original Address",
"Offset", "Symbol");
LOG_ERROR(Core_ARM, "{}", std::string(100, '-'));
LOG_ERROR(Core_ARM, "");
const auto backtrace = GetBacktrace();
for (const auto& entry : backtrace) {

View file

@ -83,7 +83,7 @@ FileSys::VirtualFile GetGameFileFromPath(const FileSys::VirtualFilesystem& vfs,
return vfs->OpenFile(path, FileSys::Mode::Read);
}
struct System::Impl {
explicit Impl(System& system) : kernel{system}, cpu_core_manager{system} {}
explicit Impl(System& system) : kernel{system}, cpu_core_manager{system}, reporter{system} {}
Cpu& CurrentCpuCore() {
return cpu_core_manager.GetCurrentCore();
@ -271,7 +271,6 @@ struct System::Impl {
/// Telemetry session for this emulation session
std::unique_ptr<Core::TelemetrySession> telemetry_session;
std::map<VAddr, std::string, std::greater<>> modules;
Reporter reporter;
ResultStatus status = ResultStatus::Success;
@ -513,14 +512,6 @@ void System::ClearContentProvider(FileSys::ContentProviderUnionSlot slot) {
impl->content_provider->ClearSlot(slot);
}
void System::RegisterNSOModule(std::string name, VAddr start_address) {
impl->modules.insert_or_assign(start_address, name);
}
const std::map<VAddr, std::string, std::greater<>>& System::GetRegisteredNSOModules() const {
return impl->modules;
}
const Reporter& System::GetReporter() const {
return impl->reporter;
}

View file

@ -287,10 +287,6 @@ public:
void ClearContentProvider(FileSys::ContentProviderUnionSlot slot);
void RegisterNSOModule(std::string name, VAddr start_address);
const std::map<VAddr, std::string, std::greater<>>& GetRegisteredNSOModules() const;
const Reporter& GetReporter() const;
private:

View file

@ -37,17 +37,18 @@ AppletDataBroker::~AppletDataBroker() = default;
AppletDataBroker::RawChannelData AppletDataBroker::PeekDataToAppletForDebug() const {
std::vector<std::vector<u8>> out_normal;
std::vector<std::vector<u8>> out_interactive;
for (const auto& storage : in_channel) {
out_normal.push_back(storage->GetData());
}
std::vector<std::vector<u8>> out_interactive;
for (const auto& storage : in_interactive_channel) {
out_interactive.push_back(storage->GetData());
}
return {out_normal, out_interactive};
return {std::move(out_normal), std::move(out_interactive)};
}
std::unique_ptr<IStorage> AppletDataBroker::PopNormalDataToGame() {

View file

@ -34,7 +34,7 @@ private:
class StubApplet final : public Applet {
public:
StubApplet(AppletId id);
explicit StubApplet(AppletId id);
~StubApplet() override;
void Initialize() override;

View file

@ -50,7 +50,7 @@ private:
void SaveReportWithUserOld(Kernel::HLERequestContext& ctx) {
IPC::RequestParser rp{ctx};
const auto user_id = rp.PopRaw<u128>();
const auto unk1 = rp.PopRaw<u64>();
const auto process_id = rp.PopRaw<u64>();
const auto data1 = ctx.ReadBuffer(0);
const auto data2 = ctx.ReadBuffer(1);
@ -58,10 +58,10 @@ private:
LOG_DEBUG(
Service_PREPO,
"called, user_id={:016X}{:016X}, unk1={:016X}, data1_size={:016X}, data2_size={:016X}",
user_id[1], user_id[0], unk1, data1.size(), data2.size());
user_id[1], user_id[0], process_id, data1.size(), data2.size());
const auto& reporter{Core::System::GetInstance().GetReporter()};
reporter.SavePlayReport(Core::CurrentProcess()->GetTitleID(), unk1, {data1, data2},
reporter.SavePlayReport(Core::CurrentProcess()->GetTitleID(), process_id, {data1, data2},
user_id);
IPC::ResponseBuilder rb{ctx, 2};

View file

@ -141,6 +141,7 @@ AppLoader_DeconstructedRomDirectory::LoadResult AppLoader_DeconstructedRomDirect
const FileSys::PatchManager pm(metadata.GetTitleID());
// Load NSO modules
modules.clear();
const VAddr base_address = process.VMManager().GetCodeRegionBaseAddress();
VAddr next_load_addr = base_address;
for (const auto& module : {"rtld", "main", "subsdk0", "subsdk1", "subsdk2", "subsdk3",
@ -159,6 +160,7 @@ AppLoader_DeconstructedRomDirectory::LoadResult AppLoader_DeconstructedRomDirect
}
next_load_addr = *tentative_next_load_addr;
modules.insert_or_assign(load_addr, module);
LOG_DEBUG(Loader, "loaded module {} @ 0x{:X}", module, load_addr);
// Register module with GDBStub
GDBStub::RegisterModule(module, load_addr, next_load_addr - 1, false);
@ -212,4 +214,13 @@ bool AppLoader_DeconstructedRomDirectory::IsRomFSUpdatable() const {
return false;
}
ResultStatus AppLoader_DeconstructedRomDirectory::ReadNSOModules(Modules& modules) {
if (!is_loaded) {
return ResultStatus::ErrorNotInitialized;
}
modules = this->modules;
return ResultStatus::Success;
}
} // namespace Loader

View file

@ -45,6 +45,8 @@ public:
ResultStatus ReadTitle(std::string& title) override;
bool IsRomFSUpdatable() const override;
ResultStatus ReadNSOModules(Modules& modules) override;
private:
FileSys::ProgramMetadata metadata;
FileSys::VirtualFile romfs;
@ -54,6 +56,8 @@ private:
std::string name;
u64 title_id{};
bool override_update;
Modules modules;
};
} // namespace Loader

View file

@ -278,6 +278,12 @@ public:
return ResultStatus::ErrorNotImplemented;
}
using Modules = std::map<VAddr, std::string>;
virtual ResultStatus ReadNSOModules(Modules& modules) {
return ResultStatus::ErrorNotImplemented;
}
protected:
FileSys::VirtualFile file;
bool is_loaded = false;

View file

@ -94,4 +94,8 @@ ResultStatus AppLoader_NAX::ReadLogo(std::vector<u8>& buffer) {
return nca_loader->ReadLogo(buffer);
}
ResultStatus AppLoader_NAX::ReadNSOModules(Modules& modules) {
return nca_loader->ReadNSOModules(modules);
}
} // namespace Loader

View file

@ -42,6 +42,8 @@ public:
ResultStatus ReadBanner(std::vector<u8>& buffer) override;
ResultStatus ReadLogo(std::vector<u8>& buffer) override;
ResultStatus ReadNSOModules(Modules& modules) override;
private:
std::unique_ptr<FileSys::NAX> nax;
std::unique_ptr<AppLoader_NCA> nca_loader;

View file

@ -105,4 +105,13 @@ ResultStatus AppLoader_NCA::ReadLogo(std::vector<u8>& buffer) {
buffer = logo->GetFile("NintendoLogo.png")->ReadAllBytes();
return ResultStatus::Success;
}
ResultStatus AppLoader_NCA::ReadNSOModules(Modules& modules) {
if (directory_loader == nullptr) {
return ResultStatus::ErrorNotInitialized;
}
return directory_loader->ReadNSOModules(modules);
}
} // namespace Loader

View file

@ -42,6 +42,8 @@ public:
ResultStatus ReadBanner(std::vector<u8>& buffer) override;
ResultStatus ReadLogo(std::vector<u8>& buffer) override;
ResultStatus ReadNSOModules(Modules& modules) override;
private:
std::unique_ptr<FileSys::NCA> nca;
std::unique_ptr<AppLoader_DeconstructedRomDirectory> directory_loader;

View file

@ -164,9 +164,6 @@ std::optional<VAddr> AppLoader_NSO::LoadModule(Kernel::Process& process,
// Register module with GDBStub
GDBStub::RegisterModule(file.GetName(), load_base, load_base);
// Register module for ARMInterface with System
Core::System::GetInstance().RegisterNSOModule(file.GetName(), load_base);
return load_base + image_size;
}
@ -175,11 +172,15 @@ AppLoader_NSO::LoadResult AppLoader_NSO::Load(Kernel::Process& process) {
return {ResultStatus::ErrorAlreadyLoaded, {}};
}
modules.clear();
// Load module
const VAddr base_address = process.VMManager().GetCodeRegionBaseAddress();
if (!LoadModule(process, *file, base_address, true)) {
return {ResultStatus::ErrorLoadingNSO, {}};
}
modules.insert_or_assign(base_address, file->GetName());
LOG_DEBUG(Loader, "loaded module {} @ 0x{:X}", file->GetName(), base_address);
is_loaded = true;
@ -187,4 +188,9 @@ AppLoader_NSO::LoadResult AppLoader_NSO::Load(Kernel::Process& process) {
LoadParameters{Kernel::THREADPRIO_DEFAULT, Memory::DEFAULT_STACK_SIZE}};
}
ResultStatus AppLoader_NSO::ReadNSOModules(Modules& modules) {
modules = this->modules;
return ResultStatus::Success;
}
} // namespace Loader

View file

@ -85,6 +85,11 @@ public:
std::optional<FileSys::PatchManager> pm = {});
LoadResult Load(Kernel::Process& process) override;
ResultStatus ReadNSOModules(Modules& modules) override;
private:
Modules modules;
};
} // namespace Loader

View file

@ -183,4 +183,8 @@ ResultStatus AppLoader_NSP::ReadLogo(std::vector<u8>& buffer) {
return secondary_loader->ReadLogo(buffer);
}
ResultStatus AppLoader_NSP::ReadNSOModules(Modules& modules) {
return secondary_loader->ReadNSOModules(modules);
}
} // namespace Loader

View file

@ -49,6 +49,8 @@ public:
ResultStatus ReadBanner(std::vector<u8>& buffer) override;
ResultStatus ReadLogo(std::vector<u8>& buffer) override;
ResultStatus ReadNSOModules(Modules& modules) override;
private:
std::unique_ptr<FileSys::NSP> nsp;
std::unique_ptr<AppLoader> secondary_loader;

View file

@ -149,4 +149,8 @@ ResultStatus AppLoader_XCI::ReadLogo(std::vector<u8>& buffer) {
return nca_loader->ReadLogo(buffer);
}
ResultStatus AppLoader_XCI::ReadNSOModules(Modules& modules) {
return nca_loader->ReadNSOModules(modules);
}
} // namespace Loader

View file

@ -49,6 +49,8 @@ public:
ResultStatus ReadBanner(std::vector<u8>& buffer) override;
ResultStatus ReadLogo(std::vector<u8>& buffer) override;
ResultStatus ReadNSOModules(Modules& modules) override;
private:
std::unique_ptr<FileSys::XCI> xci;
std::unique_ptr<AppLoader_NCA> nca_loader;

View file

@ -11,6 +11,7 @@
#include "core/core.h"
#include "core/hle/kernel/hle_ipc.h"
#include "core/hle/kernel/process.h"
#include "core/hle/result.h"
#include "core/reporter.h"
#include "core/settings.h"
#include "fmt/time.h"
@ -30,12 +31,12 @@ std::string GetTimestamp() {
using namespace nlohmann;
void SaveToFile(const json& json, const std::string& filename) {
FileUtil::CreateFullPath(filename);
if (!FileUtil::CreateFullPath(filename))
LOG_ERROR(Core, "Failed to create path for '{}' to save report!", filename);
std::ofstream file(
FileUtil::SanitizePath(filename, FileUtil::DirectorySeparator::PlatformDefault));
file << std::setw(4) << json << std::endl;
file.flush();
file.close();
}
json GetYuzuVersionData() {
@ -62,7 +63,7 @@ json GetReportCommonData(u64 title_id, ResultCode result, const std::string& tim
};
if (user_id.has_value())
out["user_id"] = fmt::format("{:016X}{:016X}", (*user_id)[1], (*user_id)[0]);
return std::move(out);
return out;
}
json GetProcessorStateData(const std::string& architecture, u64 entry_point, u64 sp, u64 pc,
@ -91,13 +92,13 @@ json GetProcessorStateData(const std::string& architecture, u64 entry_point, u64
out["backtrace"] = std::move(backtrace_out);
}
return std::move(out);
return out;
}
json GetProcessorStateDataAuto() {
const auto* process{Core::CurrentProcess()};
json GetProcessorStateDataAuto(Core::System& system) {
const auto* process{system.CurrentProcess()};
const auto& vm_manager{process->VMManager()};
auto& arm{Core::CurrentArmInterface()};
auto& arm{system.CurrentArmInterface()};
Core::ARM_Interface::ThreadContext context{};
arm.SaveContext(context);
@ -107,9 +108,9 @@ json GetProcessorStateDataAuto() {
context.pstate, context.cpu_registers);
}
json GetBacktraceData() {
json GetBacktraceData(Core::System& system) {
auto out = json::array();
const auto& backtrace{Core::CurrentArmInterface().GetBacktrace()};
const auto& backtrace{system.CurrentArmInterface().GetBacktrace()};
for (const auto& entry : backtrace) {
out.push_back({
{"module", entry.module},
@ -120,18 +121,18 @@ json GetBacktraceData() {
});
}
return std::move(out);
return out;
}
json GetFullDataAuto(const std::string& timestamp, u64 title_id) {
json GetFullDataAuto(const std::string& timestamp, u64 title_id, Core::System& system) {
json out;
out["yuzu_version"] = GetYuzuVersionData();
out["report_common"] = GetReportCommonData(title_id, RESULT_SUCCESS, timestamp);
out["processor_state"] = GetProcessorStateDataAuto();
out["backtrace"] = GetBacktraceData();
out["processor_state"] = GetProcessorStateDataAuto(system);
out["backtrace"] = GetBacktraceData(system);
return std::move(out);
return out;
}
template <bool read_value, typename DescriptorType>
@ -152,7 +153,7 @@ json GetHLEBufferDescriptorData(const std::vector<DescriptorType>& buffer) {
buffer_out.push_back(std::move(entry));
}
return std::move(buffer_out);
return buffer_out;
}
json GetHLERequestContextData(Kernel::HLERequestContext& ctx) {
@ -177,7 +178,7 @@ json GetHLERequestContextData(Kernel::HLERequestContext& ctx) {
namespace Core {
Reporter::Reporter() = default;
Reporter::Reporter(Core::System& system) : system(system) {}
Reporter::~Reporter() = default;
@ -189,7 +190,7 @@ void Reporter::SaveCrashReport(u64 title_id, ResultCode result, u64 set_flags, u
if (!IsReportingEnabled())
return;
const auto timestamp{GetTimestamp()};
const auto timestamp = GetTimestamp();
json out;
out["yuzu_version"] = GetYuzuVersionData();
@ -214,9 +215,9 @@ void Reporter::SaveSvcBreakReport(u32 type, bool signal_debugger, u64 info1, u64
if (!IsReportingEnabled())
return;
const auto timestamp{GetTimestamp()};
const auto title_id{Core::CurrentProcess()->GetTitleID()};
auto out = GetFullDataAuto(timestamp, title_id);
const auto timestamp = GetTimestamp();
const auto title_id = system.CurrentProcess()->GetTitleID();
auto out = GetFullDataAuto(timestamp, title_id, system);
auto break_out = json{
{"type", fmt::format("{:08X}", type)},
@ -240,9 +241,9 @@ void Reporter::SaveUnimplementedFunctionReport(Kernel::HLERequestContext& ctx, u
if (!IsReportingEnabled())
return;
const auto timestamp{GetTimestamp()};
const auto title_id{Core::CurrentProcess()->GetTitleID()};
auto out = GetFullDataAuto(timestamp, title_id);
const auto timestamp = GetTimestamp();
const auto title_id = system.CurrentProcess()->GetTitleID();
auto out = GetFullDataAuto(timestamp, title_id, system);
auto function_out = GetHLERequestContextData(ctx);
function_out["command_id"] = command_id;
@ -261,9 +262,9 @@ void Reporter::SaveUnimplementedAppletReport(
if (!IsReportingEnabled())
return;
const auto timestamp{GetTimestamp()};
const auto title_id{Core::CurrentProcess()->GetTitleID()};
auto out = GetFullDataAuto(timestamp, title_id);
const auto timestamp = GetTimestamp();
const auto title_id = system.CurrentProcess()->GetTitleID();
auto out = GetFullDataAuto(timestamp, title_id, system);
out["applet_common_args"] = {
{"applet_id", fmt::format("{:02X}", applet_id)},
@ -290,12 +291,12 @@ void Reporter::SaveUnimplementedAppletReport(
SaveToFile(std::move(out), GetPath("unimpl_applet_report", title_id, timestamp));
}
void Reporter::SavePlayReport(u64 title_id, u64 unk1, std::vector<std::vector<u8>> data,
void Reporter::SavePlayReport(u64 title_id, u64 process_id, std::vector<std::vector<u8>> data,
std::optional<u128> user_id) const {
if (!IsReportingEnabled())
return;
const auto timestamp{GetTimestamp()};
const auto timestamp = GetTimestamp();
json out;
out["yuzu_version"] = GetYuzuVersionData();
@ -306,7 +307,7 @@ void Reporter::SavePlayReport(u64 title_id, u64 unk1, std::vector<std::vector<u8
data_out.push_back(Common::HexVectorToString(d));
}
out["play_report_unk1"] = fmt::format("{:016X}", unk1);
out["play_report_process_id"] = fmt::format("{:016X}", process_id);
out["play_report_data"] = std::move(data_out);
SaveToFile(std::move(out), GetPath("play_report", title_id, timestamp));
@ -318,13 +319,13 @@ void Reporter::SaveErrorReport(u64 title_id, ResultCode result,
if (!IsReportingEnabled())
return;
const auto timestamp{GetTimestamp()};
const auto timestamp = GetTimestamp();
json out;
out["yuzu_version"] = GetYuzuVersionData();
out["report_common"] = GetReportCommonData(title_id, result, timestamp);
out["processor_state"] = GetProcessorStateDataAuto();
out["backtrace"] = GetBacktraceData();
out["processor_state"] = GetProcessorStateDataAuto(system);
out["backtrace"] = GetBacktraceData(system);
out["error_custom_text"] = {
{"main", *custom_text_main},
@ -338,10 +339,11 @@ void Reporter::SaveUserReport() const {
if (!IsReportingEnabled())
return;
const auto timestamp{GetTimestamp()};
const auto title_id{Core::CurrentProcess()->GetTitleID()};
const auto timestamp = GetTimestamp();
const auto title_id = system.CurrentProcess()->GetTitleID();
SaveToFile(GetFullDataAuto(timestamp, title_id), GetPath("user_report", title_id, timestamp));
SaveToFile(GetFullDataAuto(timestamp, title_id, system),
GetPath("user_report", title_id, timestamp));
}
bool Reporter::IsReportingEnabled() const {

View file

@ -7,7 +7,8 @@
#include <optional>
#include <vector>
#include "common/common_types.h"
#include "core/hle/result.h"
union ResultCode;
namespace Kernel {
class HLERequestContext;
@ -17,7 +18,7 @@ namespace Core {
class Reporter {
public:
Reporter();
explicit Reporter(Core::System& system);
~Reporter();
void SaveCrashReport(u64 title_id, ResultCode result, u64 set_flags, u64 entry_point, u64 sp,
@ -37,7 +38,7 @@ public:
std::vector<std::vector<u8>> normal_channel,
std::vector<std::vector<u8>> interactive_channel) const;
void SavePlayReport(u64 title_id, u64 unk1, std::vector<std::vector<u8>> data,
void SavePlayReport(u64 title_id, u64 process_id, std::vector<std::vector<u8>> data,
std::optional<u128> user_id = {}) const;
void SaveErrorReport(u64 title_id, ResultCode result,
@ -48,6 +49,8 @@ public:
private:
bool IsReportingEnabled() const;
Core::System& system;
};
} // namespace Core

View file

@ -465,16 +465,18 @@ void Config::ReadDataStorageValues() {
}
void Config::ReadDebuggingValues() {
qt_config->beginGroup(QStringLiteral("Debugging"));
qt_config->beginGroup(QStringLiteral("Debugging"));
Settings::values.use_gdbstub = ReadSetting(QStringLiteral("use_gdbstub"), false).toBool();
Settings::values.gdbstub_port = ReadSetting(QStringLiteral("gdbstub_port"), 24689).toInt();
Settings::values.program_args =
ReadSetting(QStringLiteral("program_args"), QStringLiteral("")).toString().toStdString();
Settings::values.dump_exefs = ReadSetting(QStringLiteral("dump_exefs"), false).toBool();
Settings::values.dump_nso = ReadSetting(QStringLiteral("dump_nso"), false).toBool();
Settings::values.reporting_services = ReadSetting("reporting_services", false).toBool();
qt_config->endGroup();
Settings::values.use_gdbstub = ReadSetting(QStringLiteral("use_gdbstub"), false).toBool();
Settings::values.gdbstub_port = ReadSetting(QStringLiteral("gdbstub_port"), 24689).toInt();
Settings::values.program_args =
ReadSetting(QStringLiteral("program_args"), QStringLiteral("")).toString().toStdString();
Settings::values.dump_exefs = ReadSetting(QStringLiteral("dump_exefs"), false).toBool();
Settings::values.dump_nso = ReadSetting(QStringLiteral("dump_nso"), false).toBool();
Settings::values.reporting_services =
ReadSetting(QStringLiteral("reporting_services"), false).toBool();
qt_config->endGroup();
}
void Config::ReadDisabledAddOnValues() {
@ -691,7 +693,7 @@ void Config::ReadValues() {
ReadDataStorageValues();
ReadSystemValues();
ReadMiscellaneousValues();
ReadDebugValues();
ReadDebuggingValues();
ReadWebServiceValues();
ReadDisabledAddOnValues();
ReadUIValues();