From d94f674bbb04be38931f1915156e91a2f3a3faeb Mon Sep 17 00:00:00 2001 From: Paul Scharnofske <31994781+asynts@users.noreply.github.com> Date: Tue, 13 Oct 2020 18:34:27 +0200 Subject: [PATCH] Use new format functions in remaining DevTools. (#3755) * AK: Add formatter for JsonValue. * Inspector: Use new format functions. * Profiler: Use new format functions. * UserspaceEmulator: Use new format functions. --- AK/JsonValue.h | 8 ++++++ DevTools/Inspector/RemoteObjectGraphModel.cpp | 6 ++--- .../Inspector/RemoteObjectPropertyModel.cpp | 6 ++--- DevTools/Inspector/RemoteProcess.cpp | 8 +++--- DevTools/Inspector/main.cpp | 4 +-- DevTools/Profiler/DisassemblyModel.cpp | 4 +-- DevTools/Profiler/Profile.cpp | 6 ++--- DevTools/Profiler/main.cpp | 4 +-- DevTools/UserspaceEmulator/Emulator.cpp | 6 ++--- DevTools/UserspaceEmulator/SoftCPU.cpp | 26 +++++++++---------- DevTools/UserspaceEmulator/main.cpp | 4 +-- 11 files changed, 45 insertions(+), 37 deletions(-) diff --git a/AK/JsonValue.h b/AK/JsonValue.h index 7d3eef87f9..4cf90ff888 100644 --- a/AK/JsonValue.h +++ b/AK/JsonValue.h @@ -261,6 +261,14 @@ private: } m_value; }; +template<> +struct Formatter : Formatter { + void format(TypeErasedFormatParams& params, FormatBuilder& builder, const JsonValue& value) + { + Formatter::format(params, builder, value.to_string()); + } +}; + } using AK::JsonValue; diff --git a/DevTools/Inspector/RemoteObjectGraphModel.cpp b/DevTools/Inspector/RemoteObjectGraphModel.cpp index bdc2da1049..32caac30ec 100644 --- a/DevTools/Inspector/RemoteObjectGraphModel.cpp +++ b/DevTools/Inspector/RemoteObjectGraphModel.cpp @@ -110,9 +110,9 @@ GUI::Variant RemoteObjectGraphModel::data(const GUI::ModelIndex& index, GUI::Mod return m_layout_icon; return m_object_icon; } - if (role == GUI::ModelRole::Display) { - return String::format("%s{%p}", remote_object->class_name.characters(), remote_object->address); - } + if (role == GUI::ModelRole::Display) + return String::formatted("{}({:p})", remote_object->class_name, remote_object->address); + return {}; } diff --git a/DevTools/Inspector/RemoteObjectPropertyModel.cpp b/DevTools/Inspector/RemoteObjectPropertyModel.cpp index 4ecbc3ba67..6c9e82f3a7 100644 --- a/DevTools/Inspector/RemoteObjectPropertyModel.cpp +++ b/DevTools/Inspector/RemoteObjectPropertyModel.cpp @@ -77,9 +77,9 @@ GUI::Variant RemoteObjectPropertyModel::data(const GUI::ModelIndex& index, GUI:: case Column::Value: { auto data = path->resolve(m_object.json); if (data.is_array()) - return String::format("" : "s>"); + return String::formatted("" : "s>"); if (data.is_object()) - return String::format("" : "ies>"); + return String::formatted("" : "ies>"); return data; } } @@ -189,7 +189,7 @@ GUI::ModelIndex RemoteObjectPropertyModel::parent_index(const GUI::ModelIndex& i return create_index(index_in_parent, 0, cpath); } - dbg() << "No cached path found for path " << path.to_string(); + dbgln("No cached path found for path {}", path.to_string()); return {}; } diff --git a/DevTools/Inspector/RemoteProcess.cpp b/DevTools/Inspector/RemoteProcess.cpp index 6b783ad149..9e19892815 100644 --- a/DevTools/Inspector/RemoteProcess.cpp +++ b/DevTools/Inspector/RemoteProcess.cpp @@ -145,7 +145,7 @@ void RemoteProcess::update() m_socket->on_ready_to_read = [this] { if (m_socket->eof()) { - dbg() << "Disconnected from PID " << m_pid; + dbgln("Disconnected from PID {}", m_pid); m_socket->close(); return; } @@ -166,13 +166,13 @@ void RemoteProcess::update() } ASSERT(data.size() == length); - dbg() << "Got data size " << length << " and read that many bytes"; + dbgln("Got data size {} and read that many bytes", length); auto json_value = JsonValue::from_string(data); ASSERT(json_value.has_value()); ASSERT(json_value.value().is_object()); - dbg() << "Got JSON response " << json_value.value().to_string(); + dbgln("Got JSON response {}", json_value.value()); auto& response = json_value.value().as_object(); @@ -193,7 +193,7 @@ void RemoteProcess::update() auto success = m_socket->connect(Core::SocketAddress::local(String::format("/tmp/rpc/%d", m_pid))); if (!success) { - fprintf(stderr, "Couldn't connect to PID %d\n", m_pid); + warnln("Couldn't connect to PID {}", m_pid); exit(1); } } diff --git a/DevTools/Inspector/main.cpp b/DevTools/Inspector/main.cpp index 19d469a628..27ecd68cb4 100644 --- a/DevTools/Inspector/main.cpp +++ b/DevTools/Inspector/main.cpp @@ -44,7 +44,7 @@ using namespace Inspector; [[noreturn]] static void print_usage_and_exit() { - printf("usage: Inspector \n"); + outln("usage: Inspector "); exit(0); } @@ -117,7 +117,7 @@ int main(int argc, char** argv) remote_process.on_update = [&] { if (!remote_process.process_name().is_null()) - window->set_title(String::format("%s (%d) - Inspector", remote_process.process_name().characters(), remote_process.pid())); + window->set_title(String::formatted("{} ({}) - Inspector", remote_process.process_name(), remote_process.pid())); }; auto& tree_view = splitter.add(); diff --git a/DevTools/Profiler/DisassemblyModel.cpp b/DevTools/Profiler/DisassemblyModel.cpp index 89a338225a..13b848642f 100644 --- a/DevTools/Profiler/DisassemblyModel.cpp +++ b/DevTools/Profiler/DisassemblyModel.cpp @@ -167,11 +167,11 @@ GUI::Variant DisassemblyModel::data(const GUI::ModelIndex& index, GUI::ModelRole return insn.event_count; } if (index.column() == Column::Address) - return String::format("%#08x", insn.address); + return String::formatted("{:p}", insn.address); if (index.column() == Column::InstructionBytes) { StringBuilder builder; for (auto ch : insn.bytes) { - builder.appendf("%02x ", (u8)ch); + builder.appendff("{:02x} ", (u8)ch); } return builder.to_string(); } diff --git a/DevTools/Profiler/Profile.cpp b/DevTools/Profiler/Profile.cpp index cb7208de07..685671ce51 100644 --- a/DevTools/Profiler/Profile.cpp +++ b/DevTools/Profiler/Profile.cpp @@ -166,14 +166,14 @@ OwnPtr Profile::load_from_perfcore_file(const StringView& path) { auto file = Core::File::construct(path); if (!file->open(Core::IODevice::ReadOnly)) { - fprintf(stderr, "Unable to open %s, error: %s\n", path.to_string().characters(), file->error_string()); + warnln("Unable to open {}, error: {}", path, file->error_string()); return nullptr; } auto json = JsonValue::from_string(file->read_all()); ASSERT(json.has_value()); if (!json.value().is_object()) { - fprintf(stderr, "Invalid perfcore format (not a JSON object)\n"); + warnln("Invalid perfcore format (not a JSON object)"); return nullptr; } @@ -182,7 +182,7 @@ OwnPtr Profile::load_from_perfcore_file(const StringView& path) MappedFile elf_file(executable_path); if (!elf_file.is_valid()) { - fprintf(stderr, "Unable to open executable '%s' for symbolication.\n", executable_path.characters()); + warnln("Unable to open executable '{}' for symbolication.", executable_path); return nullptr; } diff --git a/DevTools/Profiler/main.cpp b/DevTools/Profiler/main.cpp index daccbb9798..cd42414b91 100644 --- a/DevTools/Profiler/main.cpp +++ b/DevTools/Profiler/main.cpp @@ -74,7 +74,7 @@ int main(int argc, char** argv) auto profile = Profile::load_from_perfcore_file(path); if (!profile) { - fprintf(stderr, "Unable to load profile '%s'\n", path); + warnln("Unable to load profile '{}'", path); return 1; } @@ -170,7 +170,7 @@ bool generate_profile(pid_t pid) if (profiling_enable(pid) < 0) { int saved_errno = errno; - GUI::MessageBox::show(nullptr, String::format("Unable to profile PID %d: %s", pid, strerror(saved_errno)), "Profiler", GUI::MessageBox::Type::Error); + GUI::MessageBox::show(nullptr, String::formatted("Unable to profile PID {}: {}", pid, strerror(saved_errno)), "Profiler", GUI::MessageBox::Type::Error); return false; } diff --git a/DevTools/UserspaceEmulator/Emulator.cpp b/DevTools/UserspaceEmulator/Emulator.cpp index f9f36ca327..8aeafd900c 100644 --- a/DevTools/UserspaceEmulator/Emulator.cpp +++ b/DevTools/UserspaceEmulator/Emulator.cpp @@ -29,8 +29,8 @@ #include "SharedBufferRegion.h" #include "SimpleRegion.h" #include "SoftCPU.h" +#include #include -#include #include #include #include @@ -171,7 +171,7 @@ int Emulator::exec() auto insn = X86::Instruction::from_stream(m_cpu, true, true); if (trace) - out() << (const void*)m_cpu.base_eip() << " \033[33;1m" << insn.to_string(m_cpu.base_eip(), &symbol_provider) << "\033[0m"; + outln("{:p} \033[33;1m{}\033[0m", m_cpu.base_eip(), insn.to_string(m_cpu.base_eip(), &symbol_provider)); (m_cpu.*insn.handler())(insn); @@ -1070,7 +1070,7 @@ void Emulator::register_signal_handlers() int Emulator::virt$sigaction(int signum, FlatPtr act, FlatPtr oldact) { if (signum == SIGKILL) { - dbg() << "Attempted to sigaction() with SIGKILL"; + dbgln("Attempted to sigaction() with SIGKILL"); return -EINVAL; } diff --git a/DevTools/UserspaceEmulator/SoftCPU.cpp b/DevTools/UserspaceEmulator/SoftCPU.cpp index dbedd5effc..84f7ac5476 100644 --- a/DevTools/UserspaceEmulator/SoftCPU.cpp +++ b/DevTools/UserspaceEmulator/SoftCPU.cpp @@ -1342,13 +1342,13 @@ void SoftCPU::DIV_RM16(const X86::Instruction& insn) { auto divisor = insn.modrm().read16(*this, insn); if (divisor.value() == 0) { - warn() << "Divide by zero"; + warnln("Divide by zero"); TODO(); } u32 dividend = ((u32)dx().value() << 16) | ax().value(); auto quotient = dividend / divisor.value(); if (quotient > NumericLimits::max()) { - warn() << "Divide overflow"; + warnln("Divide overflow"); TODO(); } @@ -1363,13 +1363,13 @@ void SoftCPU::DIV_RM32(const X86::Instruction& insn) { auto divisor = insn.modrm().read32(*this, insn); if (divisor.value() == 0) { - warn() << "Divide by zero"; + warnln("Divide by zero"); TODO(); } u64 dividend = ((u64)edx().value() << 32) | eax().value(); auto quotient = dividend / divisor.value(); if (quotient > NumericLimits::max()) { - warn() << "Divide overflow"; + warnln("Divide overflow"); TODO(); } @@ -1384,13 +1384,13 @@ void SoftCPU::DIV_RM8(const X86::Instruction& insn) { auto divisor = insn.modrm().read8(*this, insn); if (divisor.value() == 0) { - warn() << "Divide by zero"; + warnln("Divide by zero"); TODO(); } u16 dividend = ax().value(); auto quotient = dividend / divisor.value(); if (quotient > NumericLimits::max()) { - warn() << "Divide overflow"; + warnln("Divide overflow"); TODO(); } @@ -1405,7 +1405,7 @@ void SoftCPU::ENTER32(const X86::Instruction&) { TODO_INSN(); } void SoftCPU::ESCAPE(const X86::Instruction&) { - dbg() << "FIXME: x87 floating-point support"; + dbgln("FIXME: x87 floating-point support"); m_emulator.dump_backtrace(); TODO(); } @@ -1546,13 +1546,13 @@ void SoftCPU::IDIV_RM16(const X86::Instruction& insn) auto divisor_with_shadow = insn.modrm().read16(*this, insn); auto divisor = (i16)divisor_with_shadow.value(); if (divisor == 0) { - warn() << "Divide by zero"; + warnln("Divide by zero"); TODO(); } i32 dividend = (i32)(((u32)dx().value() << 16) | (u32)ax().value()); i32 result = dividend / divisor; if (result > NumericLimits::max() || result < NumericLimits::min()) { - warn() << "Divide overflow"; + warnln("Divide overflow"); TODO(); } @@ -1566,13 +1566,13 @@ void SoftCPU::IDIV_RM32(const X86::Instruction& insn) auto divisor_with_shadow = insn.modrm().read32(*this, insn); auto divisor = (i32)divisor_with_shadow.value(); if (divisor == 0) { - warn() << "Divide by zero"; + warnln("Divide by zero"); TODO(); } i64 dividend = (i64)(((u64)edx().value() << 32) | (u64)eax().value()); i64 result = dividend / divisor; if (result > NumericLimits::max() || result < NumericLimits::min()) { - warn() << "Divide overflow"; + warnln("Divide overflow"); TODO(); } @@ -1586,13 +1586,13 @@ void SoftCPU::IDIV_RM8(const X86::Instruction& insn) auto divisor_with_shadow = insn.modrm().read8(*this, insn); auto divisor = (i8)divisor_with_shadow.value(); if (divisor == 0) { - warn() << "Divide by zero"; + warnln("Divide by zero"); TODO(); } i16 dividend = ax().value(); i16 result = dividend / divisor; if (result > NumericLimits::max() || result < NumericLimits::min()) { - warn() << "Divide overflow"; + warnln("Divide overflow"); TODO(); } diff --git a/DevTools/UserspaceEmulator/main.cpp b/DevTools/UserspaceEmulator/main.cpp index 7b5dfbf596..185a233dfa 100644 --- a/DevTools/UserspaceEmulator/main.cpp +++ b/DevTools/UserspaceEmulator/main.cpp @@ -26,8 +26,8 @@ #include "Emulator.h" #include "SoftCPU.h" +#include #include -#include #include #include #include @@ -49,7 +49,7 @@ int main(int argc, char** argv, char** env) MappedFile mapped_file(executable_path); if (!mapped_file.is_valid()) { - warn() << "Unable to map " << executable_path; + warnln("Unable to map {}", executable_path); return 1; }