Formatting keys in log output

This commit is contained in:
Nav
2025-02-02 14:54:17 +00:00
parent f3cd55e53f
commit b06e8cc9ad
17 changed files with 140 additions and 151 deletions

View File

@@ -48,7 +48,7 @@ namespace DebugServer::Gdb::CommandPackets
const auto peripheralDescriptorOpt = targetDescriptor.tryGetPeripheralDescriptor(peripheralKey); const auto peripheralDescriptorOpt = targetDescriptor.tryGetPeripheralDescriptor(peripheralKey);
if (!peripheralDescriptorOpt.has_value()) { if (!peripheralDescriptorOpt.has_value()) {
throw Exception{"Unknown peripheral key \"" + peripheralKey + "\""}; throw Exception{"Unknown peripheral key `" + peripheralKey + "`"};
} }
const auto& peripheralDescriptor = peripheralDescriptorOpt->get(); const auto& peripheralDescriptor = peripheralDescriptorOpt->get();
@@ -67,7 +67,7 @@ namespace DebugServer::Gdb::CommandPackets
); );
if (!registerGroupDescriptorOpt.has_value()) { if (!registerGroupDescriptorOpt.has_value()) {
throw Exception{"Unknown register group key \"" + registerGroupKey + "\""}; throw Exception{"Unknown register group key `" + registerGroupKey + "`"};
} }
const auto& registerGroupDescriptor = registerGroupDescriptorOpt->get(); const auto& registerGroupDescriptor = registerGroupDescriptorOpt->get();
@@ -99,10 +99,8 @@ namespace DebugServer::Gdb::CommandPackets
"---------- \"" + StringService::applyTerminalColor( "---------- \"" + StringService::applyTerminalColor(
peripheralDescriptor.name, peripheralDescriptor.name,
StringService::TerminalColor::DARK_GREEN StringService::TerminalColor::DARK_GREEN
) + "\" (`" + StringService::applyTerminalColor( ) + "\" (" + StringService::formatKey(peripheralDescriptor.key)
peripheralDescriptor.key, + ") peripheral registers ----------\n\n"
StringService::TerminalColor::DARK_YELLOW
) + "`) peripheral registers ----------\n\n"
)}); )});
for (const auto& [groupKey, groupDescriptor] : peripheralDescriptor.registerGroupDescriptorsByKey) { for (const auto& [groupKey, groupDescriptor] : peripheralDescriptor.registerGroupDescriptorsByKey) {
@@ -115,13 +113,11 @@ namespace DebugServer::Gdb::CommandPackets
DebugSession& debugSession DebugSession& debugSession
) { ) {
for (const auto* registerDescriptor : this->sortRegisterDescriptors(groupDescriptor.registerDescriptorsByKey)) { for (const auto* registerDescriptor : this->sortRegisterDescriptors(groupDescriptor.registerDescriptorsByKey)) {
auto output = std::string{"`" + registerDescriptor->absoluteGroupKey + "`, "}; auto output = StringService::formatKey(registerDescriptor->absoluteGroupKey) + ", ";
output += "`" + registerDescriptor->key + "`, "; output += StringService::formatKey(registerDescriptor->key) + ", ";
output += "\"" + registerDescriptor->name + "\", "; output += "\"" + registerDescriptor->name + "\", ";
output += StringService::applyTerminalColor( output += "0x" + StringService::asciiToUpper(StringService::toHex(registerDescriptor->startAddress))
"0x" + StringService::asciiToUpper(StringService::toHex(registerDescriptor->startAddress)), + ", ";
StringService::TerminalColor::BLUE
) + ", ";
output += std::to_string(registerDescriptor->size * 8) + "-bit"; output += std::to_string(registerDescriptor->size * 8) + "-bit";
if (registerDescriptor->description.has_value()) { if (registerDescriptor->description.has_value()) {

View File

@@ -47,7 +47,7 @@ namespace DebugServer::Gdb::CommandPackets
const auto peripheralDescriptorOpt = targetDescriptor.tryGetPeripheralDescriptor(peripheralKey); const auto peripheralDescriptorOpt = targetDescriptor.tryGetPeripheralDescriptor(peripheralKey);
if (!peripheralDescriptorOpt.has_value()) { if (!peripheralDescriptorOpt.has_value()) {
throw Exception{"Unknown peripheral key \"" + peripheralKey + "\""}; throw Exception{"Unknown peripheral key `" + peripheralKey + "`"};
} }
const auto& peripheralDescriptor = peripheralDescriptorOpt->get(); const auto& peripheralDescriptor = peripheralDescriptorOpt->get();
@@ -67,7 +67,7 @@ namespace DebugServer::Gdb::CommandPackets
if (!registerGroupDescriptorOpt.has_value()) { if (!registerGroupDescriptorOpt.has_value()) {
if (peripheralDescriptor.registerGroupDescriptorsByKey.size() != 1 || argCount >= 4) { if (peripheralDescriptor.registerGroupDescriptorsByKey.size() != 1 || argCount >= 4) {
throw Exception{"Unknown register group key \"" + registerGroupKey + "\""}; throw Exception{"Unknown register group key `" + registerGroupKey + "`"};
} }
registerGroupDescriptorOpt = peripheralDescriptor.registerGroupDescriptorsByKey.begin()->second; registerGroupDescriptorOpt = peripheralDescriptor.registerGroupDescriptorsByKey.begin()->second;
@@ -89,7 +89,7 @@ namespace DebugServer::Gdb::CommandPackets
const auto registerDescriptorOpt = registerGroupDescriptor.tryGetRegisterDescriptor(*registerKey); const auto registerDescriptorOpt = registerGroupDescriptor.tryGetRegisterDescriptor(*registerKey);
if (!registerDescriptorOpt.has_value()) { if (!registerDescriptorOpt.has_value()) {
throw Exception{"Unknown register key \"" + *registerKey + "\""}; throw Exception{"Unknown register key `" + *registerKey + "`"};
} }
this->handleSingleRegisterOutput(registerDescriptorOpt->get(), debugSession, targetControllerService); this->handleSingleRegisterOutput(registerDescriptorOpt->get(), debugSession, targetControllerService);
@@ -112,10 +112,8 @@ namespace DebugServer::Gdb::CommandPackets
TargetControllerService& targetControllerService TargetControllerService& targetControllerService
) { ) {
auto output = std::string{"\nName: \"" + registerDescriptor.name + "\"\n"}; auto output = std::string{"\nName: \"" + registerDescriptor.name + "\"\n"};
output += "Address: " + StringService::applyTerminalColor( output += "Address: 0x" + StringService::asciiToUpper(StringService::toHex(registerDescriptor.startAddress))
"0x" + StringService::asciiToUpper(StringService::toHex(registerDescriptor.startAddress)), + "\n";
StringService::TerminalColor::BLUE
) + "\n";
output += "Width: " + std::to_string(registerDescriptor.size * 8) + "-bit\n\n"; output += "Width: " + std::to_string(registerDescriptor.size * 8) + "-bit\n\n";
output += "----------- Value -----------\n"; output += "----------- Value -----------\n";
@@ -177,13 +175,11 @@ namespace DebugServer::Gdb::CommandPackets
TargetControllerService& targetControllerService TargetControllerService& targetControllerService
) { ) {
for (const auto* registerDescriptor : this->sortRegisterDescriptors(groupDescriptor.registerDescriptorsByKey)) { for (const auto* registerDescriptor : this->sortRegisterDescriptors(groupDescriptor.registerDescriptorsByKey)) {
auto output = std::string{"`" + registerDescriptor->absoluteGroupKey + "`, "}; auto output = StringService::formatKey(registerDescriptor->absoluteGroupKey) + ", ";
output += "`" + registerDescriptor->key + "`, "; output += StringService::formatKey(registerDescriptor->key) + ", ";
output += "\"" + registerDescriptor->name + "\", "; output += "\"" + registerDescriptor->name + "\", ";
output += StringService::applyTerminalColor( output += "0x" + StringService::asciiToUpper(StringService::toHex(registerDescriptor->startAddress))
"0x" + StringService::asciiToUpper(StringService::toHex(registerDescriptor->startAddress)), + ", ";
StringService::TerminalColor::BLUE
) + ", ";
output += std::to_string(registerDescriptor->size * 8) + "-bit | "; output += std::to_string(registerDescriptor->size * 8) + "-bit | ";
if (registerDescriptor->access.readable) { if (registerDescriptor->access.readable) {

View File

@@ -38,13 +38,9 @@ namespace DebugServer::Gdb::CommandPackets
debugSession.connection.writePacket(ResponsePacket{Services::StringService::toHex( debugSession.connection.writePacket(ResponsePacket{Services::StringService::toHex(
"Target reset complete\n" "Target reset complete\n"
"Current PC: " + StringService::applyTerminalColor( "Current PC: 0x" + StringService::asciiToUpper(
"0x" + StringService::asciiToUpper( StringService::toHex(targetControllerService.getProgramCounter())
StringService::toHex(targetControllerService.getProgramCounter()) ) + "\nUse the 'continue' command to begin execution\n"
),
StringService::TerminalColor::BLUE
) + "\n"
"Use the 'continue' command to begin execution\n"
)}); )});
} catch (const Exception& exception) { } catch (const Exception& exception) {

View File

@@ -49,7 +49,7 @@ namespace DebugServer::Gdb::CommandPackets
const auto peripheralDescriptorOpt = targetDescriptor.tryGetPeripheralDescriptor(peripheralKey); const auto peripheralDescriptorOpt = targetDescriptor.tryGetPeripheralDescriptor(peripheralKey);
if (!peripheralDescriptorOpt.has_value()) { if (!peripheralDescriptorOpt.has_value()) {
throw Exception{"Unknown peripheral key \"" + peripheralKey + "\""}; throw Exception{"Unknown peripheral key `" + peripheralKey + "`"};
} }
const auto& peripheralDescriptor = peripheralDescriptorOpt->get(); const auto& peripheralDescriptor = peripheralDescriptorOpt->get();
@@ -63,7 +63,7 @@ namespace DebugServer::Gdb::CommandPackets
const auto& registerGroupKey = this->commandArguments[2]; const auto& registerGroupKey = this->commandArguments[2];
registerGroupDescriptorOpt = peripheralDescriptor.tryGetRegisterGroupDescriptor(registerGroupKey); registerGroupDescriptorOpt = peripheralDescriptor.tryGetRegisterGroupDescriptor(registerGroupKey);
if (!registerGroupDescriptorOpt.has_value()) { if (!registerGroupDescriptorOpt.has_value()) {
throw Exception{"Unknown absolute register group key \"" + registerGroupKey + "\""}; throw Exception{"Unknown absolute register group key `" + registerGroupKey + "`"};
} }
} else { } else {
@@ -83,7 +83,7 @@ namespace DebugServer::Gdb::CommandPackets
const auto& registerKey = registerGroupKeyProvided ? this->commandArguments[3] : this->commandArguments[2]; const auto& registerKey = registerGroupKeyProvided ? this->commandArguments[3] : this->commandArguments[2];
const auto registerDescriptorOpt = registerGroupDescriptor.tryGetRegisterDescriptor(registerKey); const auto registerDescriptorOpt = registerGroupDescriptor.tryGetRegisterDescriptor(registerKey);
if (!registerDescriptorOpt.has_value()) { if (!registerDescriptorOpt.has_value()) {
throw Exception{"Unknown register key \"" + registerKey + "\""}; throw Exception{"Unknown register key `" + registerKey + "`"};
} }
const auto& registerDescriptor = registerDescriptorOpt->get(); const auto& registerDescriptor = registerDescriptorOpt->get();
@@ -103,7 +103,7 @@ namespace DebugServer::Gdb::CommandPackets
const auto& bitFieldKey = registerGroupKeyProvided ? this->commandArguments[4] : this->commandArguments[3]; const auto& bitFieldKey = registerGroupKeyProvided ? this->commandArguments[4] : this->commandArguments[3];
const auto bitFieldDescriptorOpt = registerDescriptor.tryGetBitFieldDescriptor(bitFieldKey); const auto bitFieldDescriptorOpt = registerDescriptor.tryGetBitFieldDescriptor(bitFieldKey);
if (!bitFieldDescriptorOpt.has_value()) { if (!bitFieldDescriptorOpt.has_value()) {
throw Exception{"Unknown bit field key \"" + bitFieldKey + "\""}; throw Exception{"Unknown bit field key `" + bitFieldKey + "`"};
} }
const auto& bitFieldDescriptor = bitFieldDescriptorOpt->get(); const auto& bitFieldDescriptor = bitFieldDescriptorOpt->get();
@@ -181,10 +181,8 @@ namespace DebugServer::Gdb::CommandPackets
"0x" + newValueHex, "0x" + newValueHex,
StringService::TerminalColor::DARK_YELLOW StringService::TerminalColor::DARK_YELLOW
) + " (" + std::to_string(registerDescriptor.size * 8) + "-bit" + ") to \"" + registerDescriptor.name ) + " (" + std::to_string(registerDescriptor.size * 8) + "-bit" + ") to \"" + registerDescriptor.name
+ "\" register, at address " + StringService::applyTerminalColor( + "\" register, at address 0x" + StringService::toHex(registerDescriptor.startAddress) + ", via "
"0x" + StringService::toHex(registerDescriptor.startAddress), + StringService::formatKey(registerDescriptor.addressSpaceKey) + " address space...\n"
StringService::TerminalColor::BLUE
) + ", via `" + registerDescriptor.addressSpaceKey + "` address space...\n"
)}); )});
targetControllerService.writeRegister(registerDescriptor, dynamicValue.data()); targetControllerService.writeRegister(registerDescriptor, dynamicValue.data());

View File

@@ -48,7 +48,7 @@ namespace DebugServer::Gdb::CommandPackets
const auto peripheralDescriptorOpt = targetDescriptor.tryGetPeripheralDescriptor(peripheralKey); const auto peripheralDescriptorOpt = targetDescriptor.tryGetPeripheralDescriptor(peripheralKey);
if (!peripheralDescriptorOpt.has_value()) { if (!peripheralDescriptorOpt.has_value()) {
throw Exception{"Unknown peripheral key \"" + peripheralKey + "\""}; throw Exception{"Unknown peripheral key `" + peripheralKey + "`"};
} }
const auto& peripheralDescriptor = peripheralDescriptorOpt->get(); const auto& peripheralDescriptor = peripheralDescriptorOpt->get();
@@ -62,7 +62,7 @@ namespace DebugServer::Gdb::CommandPackets
const auto& registerGroupKey = this->commandArguments[2]; const auto& registerGroupKey = this->commandArguments[2];
registerGroupDescriptorOpt = peripheralDescriptor.tryGetRegisterGroupDescriptor(registerGroupKey); registerGroupDescriptorOpt = peripheralDescriptor.tryGetRegisterGroupDescriptor(registerGroupKey);
if (!registerGroupDescriptorOpt.has_value()) { if (!registerGroupDescriptorOpt.has_value()) {
throw Exception{"Unknown absolute register group key \"" + registerGroupKey + "\""}; throw Exception{"Unknown absolute register group key `" + registerGroupKey + "`"};
} }
} else { } else {
@@ -82,7 +82,7 @@ namespace DebugServer::Gdb::CommandPackets
const auto& registerKey = registerGroupKeyProvided ? this->commandArguments[3] : this->commandArguments[2]; const auto& registerKey = registerGroupKeyProvided ? this->commandArguments[3] : this->commandArguments[2];
const auto registerDescriptorOpt = registerGroupDescriptor.tryGetRegisterDescriptor(registerKey); const auto registerDescriptorOpt = registerGroupDescriptor.tryGetRegisterDescriptor(registerKey);
if (!registerDescriptorOpt.has_value()) { if (!registerDescriptorOpt.has_value()) {
throw Exception{"Unknown register key \"" + registerKey + "\""}; throw Exception{"Unknown register key `" + registerKey + "`"};
} }
const auto& registerDescriptor = registerDescriptorOpt->get(); const auto& registerDescriptor = registerDescriptorOpt->get();
@@ -121,10 +121,8 @@ namespace DebugServer::Gdb::CommandPackets
"0x" + StringService::toHex(value).substr(16 - (registerDescriptor.size * 2)), "0x" + StringService::toHex(value).substr(16 - (registerDescriptor.size * 2)),
StringService::TerminalColor::DARK_YELLOW StringService::TerminalColor::DARK_YELLOW
) + " (" + std::to_string(buffer.size() * 8) + "-bit" + ") to \"" + registerDescriptor.name ) + " (" + std::to_string(buffer.size() * 8) + "-bit" + ") to \"" + registerDescriptor.name
+ "\" register, at address " + StringService::applyTerminalColor( + "\" register, at address 0x" + StringService::toHex(registerDescriptor.startAddress) + ", via "
"0x" + StringService::toHex(registerDescriptor.startAddress), + StringService::formatKey(registerDescriptor.addressSpaceKey) + " address space...\n"
StringService::TerminalColor::BLUE
) + ", via `" + registerDescriptor.addressSpaceKey + "` address space...\n"
)}); )});
targetControllerService.writeRegister(registerDescriptor, buffer); targetControllerService.writeRegister(registerDescriptor, buffer);

View File

@@ -59,7 +59,7 @@ namespace DebugServer::Gdb::RiscVGdb::CommandPackets
throw Exception{"Memory segment (\"" + segmentDescriptor.name + "\") not writable in programming mode"}; throw Exception{"Memory segment (\"" + segmentDescriptor.name + "\") not writable in programming mode"};
} }
Logger::debug("Erase segment key: `" + segmentDescriptor.key + "`"); Logger::debug("Erase segment key: " + Services::StringService::formatKey(segmentDescriptor.key));
targetControllerService.enableProgrammingMode(); targetControllerService.enableProgrammingMode();

View File

@@ -1,9 +1,5 @@
#include "EdbgAvrIspInterface.hpp" #include "EdbgAvrIspInterface.hpp"
#include "src/TargetController/Exceptions/TargetOperationFailure.hpp"
#include "src/Exceptions/InternalFatalErrorException.hpp"
#include "src/Logger/Logger.hpp"
// Command frames // Command frames
#include "CommandFrames/AvrIsp/EnterProgrammingMode.hpp" #include "CommandFrames/AvrIsp/EnterProgrammingMode.hpp"
#include "CommandFrames/AvrIsp/LeaveProgrammingMode.hpp" #include "CommandFrames/AvrIsp/LeaveProgrammingMode.hpp"
@@ -12,6 +8,11 @@
#include "CommandFrames/AvrIsp/ReadLock.hpp" #include "CommandFrames/AvrIsp/ReadLock.hpp"
#include "CommandFrames/AvrIsp/ProgramFuse.hpp" #include "CommandFrames/AvrIsp/ProgramFuse.hpp"
#include "src/Services/StringService.hpp"
#include "src/TargetController/Exceptions/TargetOperationFailure.hpp"
#include "src/Exceptions/InternalFatalErrorException.hpp"
namespace DebugToolDrivers::Microchip::Protocols::Edbg::Avr namespace DebugToolDrivers::Microchip::Protocols::Edbg::Avr
{ {
using namespace Targets::Microchip::Avr8; using namespace Targets::Microchip::Avr8;
@@ -155,7 +156,8 @@ namespace DebugToolDrivers::Microchip::Protocols::Edbg::Avr
} }
throw Exceptions::InternalFatalErrorException{ throw Exceptions::InternalFatalErrorException{
"Could not resolve fuse type from register descriptor (key: \"" + fuseRegisterDescriptor.key + "\")" "Could not resolve fuse type from register descriptor (key: "
+ Services::StringService::formatKey(fuseRegisterDescriptor.key) + ")"
}; };
} }
} }

View File

@@ -8,6 +8,7 @@
#include <utility> #include <utility>
#include "src/Services/PathService.hpp" #include "src/Services/PathService.hpp"
#include "src/Services/StringService.hpp"
#include "src/Logger/Logger.hpp" #include "src/Logger/Logger.hpp"
#include "src/Exceptions/Exception.hpp" #include "src/Exceptions/Exception.hpp"
@@ -49,6 +50,8 @@ void RetrieveMemorySnapshots::run(TargetControllerService& targetControllerServi
} }
std::vector<MemorySnapshot> RetrieveMemorySnapshots::getSnapshots() { std::vector<MemorySnapshot> RetrieveMemorySnapshots::getSnapshots() {
using Services::StringService;
constexpr auto MAX_SNAPSHOTS = 30; constexpr auto MAX_SNAPSHOTS = 30;
const auto snapshotDir = QDir{QString::fromStdString(Services::PathService::memorySnapshotsPath())}; const auto snapshotDir = QDir{QString::fromStdString(Services::PathService::memorySnapshotsPath())};
@@ -68,8 +71,8 @@ std::vector<MemorySnapshot> RetrieveMemorySnapshots::getSnapshots() {
if (snapshots.size() >= MAX_SNAPSHOTS) { if (snapshots.size() >= MAX_SNAPSHOTS) {
Logger::warning( Logger::warning(
"The total number of \"" + this->memorySegmentDescriptor.key "The total number of `" +this->memorySegmentDescriptor.key
+ "\" snapshots exceeds the hard limit of " + std::to_string(MAX_SNAPSHOTS) + "` snapshots exceeds the hard limit of " + std::to_string(MAX_SNAPSHOTS)
+ ". Only the most recent " + std::to_string(MAX_SNAPSHOTS) + " snapshots will be loaded." + ". Only the most recent " + std::to_string(MAX_SNAPSHOTS) + " snapshots will be loaded."
); );
break; break;

View File

@@ -397,8 +397,8 @@ void InsightWindow::selectDefaultVariant() {
} else { } else {
Logger::error( Logger::error(
"Invalid target variant key \"" + *(this->insightConfig.defaultVariantKey) "Invalid target variant key `" + *(this->insightConfig.defaultVariantKey)
+ "\" - no such variant with the given key was found" + "` - no such variant with the given key was found"
); );
} }
} }

View File

@@ -176,76 +176,65 @@ namespace Services
return {std::ranges::begin(range), std::ranges::end(range)}; return {std::ranges::begin(range), std::ranges::end(range)};
} }
std::string StringService::applyTerminalColor(const std::string& string, TerminalColor color) { std::string_view StringService::colorCode(const TerminalColor color) {
auto colorCode = std::string{};
switch (color) { switch (color) {
case TerminalColor::BLACK: { case TerminalColor::BLACK: {
colorCode = "\033[30m"; return "\033[30m";
break;
} }
case TerminalColor::DARK_RED: { case TerminalColor::DARK_RED: {
colorCode = "\033[31m"; return "\033[31m";
break;
} }
case TerminalColor::DARK_GREEN: { case TerminalColor::DARK_GREEN: {
colorCode = "\033[32m"; return "\033[32m";
break;
} }
case TerminalColor::DARK_YELLOW: { case TerminalColor::DARK_YELLOW: {
colorCode = "\033[33m"; return "\033[33m";
break;
} }
case TerminalColor::DARK_BLUE: { case TerminalColor::DARK_BLUE: {
colorCode = "\033[34m"; return "\033[34m";
break;
} }
case TerminalColor::DARK_MAGENTA: { case TerminalColor::DARK_MAGENTA: {
colorCode = "\033[35m"; return "\033[35m";
break;
} }
case TerminalColor::DARK_CYAN: { case TerminalColor::DARK_CYAN: {
colorCode = "\033[36m"; return "\033[36m";
break;
} }
case TerminalColor::LIGHT_GRAY: { case TerminalColor::LIGHT_GRAY: {
colorCode = "\033[37m"; return "\033[37m";
break;
} }
case TerminalColor::DARK_GRAY: { case TerminalColor::DARK_GRAY: {
colorCode = "\033[90m"; return "\033[90m";
break;
} }
case TerminalColor::RED: { case TerminalColor::RED: {
colorCode = "\033[91m"; return "\033[91m";
break;
} }
case TerminalColor::GREEN: { case TerminalColor::GREEN: {
colorCode = "\033[92m"; return "\033[92m";
break;
} }
case TerminalColor::ORANGE: { case TerminalColor::ORANGE: {
colorCode = "\033[93m"; return "\033[93m";
break;
} }
case TerminalColor::BLUE: { case TerminalColor::BLUE: {
colorCode = "\033[94m"; return "\033[94m";
break;
} }
case TerminalColor::MAGENTA: { case TerminalColor::MAGENTA: {
colorCode = "\033[95m"; return "\033[95m";
break;
} }
case TerminalColor::CYAN: { case TerminalColor::CYAN: {
colorCode = "\033[96m"; return "\033[96m";
break;
} }
case TerminalColor::WHITE: { case TerminalColor::WHITE:
colorCode = "\033[97m"; default: {
break; return "\033[97m";
} }
} }
}
return colorCode + string + "\033[0m"; std::string StringService::applyTerminalColor(const std::string& string, TerminalColor color) {
return std::string{StringService::colorCode(color)} + string + "\033[0m";
}
std::string StringService::formatKey(const std::string_view key) {
return "`" + StringService::applyTerminalColor(std::string{key}, StringService::TerminalColor::BLUE) + "`";
} }
} }

View File

@@ -91,6 +91,8 @@ namespace Services
WHITE, WHITE,
}; };
static std::string_view colorCode(TerminalColor color);
static std::string applyTerminalColor(const std::string& string, TerminalColor color); static std::string applyTerminalColor(const std::string& string, TerminalColor color);
static std::string formatKey(std::string_view key);
}; };
} }

View File

@@ -1151,8 +1151,8 @@ namespace TargetController
auto& segmentCache = this->getProgramMemoryCache(operation.memorySegmentDescriptor); auto& segmentCache = this->getProgramMemoryCache(operation.memorySegmentDescriptor);
Logger::info( Logger::info(
std::to_string(operation.deltaSegments.size()) + " delta segment(s) to be flushed to `" std::to_string(operation.deltaSegments.size()) + " delta segment(s) to be flushed to "
+ operation.memorySegmentDescriptor.key + "`" + StringService::formatKey(operation.memorySegmentDescriptor.key)
); );
for (const auto& deltaSegment : operation.deltaSegments) { for (const auto& deltaSegment : operation.deltaSegments) {
Logger::info( Logger::info(

View File

@@ -81,6 +81,8 @@ namespace Targets::RiscV
} }
TargetRegisterDescriptorAndValuePairs RiscV::readRegisters(const TargetRegisterDescriptors& descriptors) { TargetRegisterDescriptorAndValuePairs RiscV::readRegisters(const TargetRegisterDescriptors& descriptors) {
using Services::StringService;
auto output = TargetRegisterDescriptorAndValuePairs{}; auto output = TargetRegisterDescriptorAndValuePairs{};
/* /*
@@ -101,7 +103,7 @@ namespace Targets::RiscV
&& !this->csrMemorySegmentDescriptor.addressRange.contains(descriptor->startAddress) && !this->csrMemorySegmentDescriptor.addressRange.contains(descriptor->startAddress)
) { ) {
throw Exceptions::Exception{ throw Exceptions::Exception{
"Cannot access CPU CSR `" + descriptor->key + "` - unknown memory segment" "Cannot access CPU CSR " + StringService::formatKey(descriptor->key) + " - unknown memory segment"
}; };
} }
@@ -110,7 +112,7 @@ namespace Targets::RiscV
&& !this->gprMemorySegmentDescriptor.addressRange.contains(descriptor->startAddress) && !this->gprMemorySegmentDescriptor.addressRange.contains(descriptor->startAddress)
) { ) {
throw Exceptions::Exception{ throw Exceptions::Exception{
"Cannot access CPU GPR `" + descriptor->key + "` - unknown memory segment" "Cannot access CPU GPR " + StringService::formatKey(descriptor->key) + " - unknown memory segment"
}; };
} }
@@ -120,7 +122,7 @@ namespace Targets::RiscV
if (descriptor->addressSpaceId != this->sysAddressSpaceDescriptor.id) { if (descriptor->addressSpaceId != this->sysAddressSpaceDescriptor.id) {
throw Exceptions::Exception{ throw Exceptions::Exception{
"Cannot access register `" + descriptor->key + "` - unknown address space" "Cannot access register " + StringService::formatKey(descriptor->key) + " - unknown address space"
}; };
} }
@@ -148,6 +150,8 @@ namespace Targets::RiscV
} }
void RiscV::writeRegisters(const TargetRegisterDescriptorAndValuePairs& registers) { void RiscV::writeRegisters(const TargetRegisterDescriptorAndValuePairs& registers) {
using Services::StringService;
for (const auto& pair : registers) { for (const auto& pair : registers) {
const auto& descriptor = pair.first; const auto& descriptor = pair.first;
@@ -160,7 +164,7 @@ namespace Targets::RiscV
&& !this->csrMemorySegmentDescriptor.addressRange.contains(descriptor.startAddress) && !this->csrMemorySegmentDescriptor.addressRange.contains(descriptor.startAddress)
) { ) {
throw Exceptions::Exception{ throw Exceptions::Exception{
"Cannot access CPU CSR `" + descriptor.key + "` - unknown memory segment" "Cannot access CPU CSR " + StringService::formatKey(descriptor.key) + " - unknown memory segment"
}; };
} }
@@ -169,7 +173,8 @@ namespace Targets::RiscV
&& !this->gprMemorySegmentDescriptor.addressRange.contains(descriptor.startAddress) && !this->gprMemorySegmentDescriptor.addressRange.contains(descriptor.startAddress)
) { ) {
throw Exceptions::Exception{ throw Exceptions::Exception{
"Cannot access CPU GPR `" + descriptor.key + "` - unknown memory segment" "Cannot access CPU GPR " + StringService::formatKey(descriptor.key)
+ " - unknown memory segment"
}; };
} }
@@ -178,7 +183,10 @@ namespace Targets::RiscV
} }
if (descriptor.addressSpaceId != this->sysAddressSpaceDescriptor.id) { if (descriptor.addressSpaceId != this->sysAddressSpaceDescriptor.id) {
throw Exceptions::Exception{"Cannot access register `" + descriptor.key + "` - unknown address space"}; throw Exceptions::Exception{
"Cannot access register " + StringService::formatKey(descriptor.key)
+ " - unknown address space"
};
} }
auto value = pair.second; auto value = pair.second;
@@ -385,6 +393,8 @@ namespace Targets::RiscV
const TargetRegisterDescriptor& regDescriptor, const TargetRegisterDescriptor& regDescriptor,
const TargetAddressSpaceDescriptor& addressSpaceDescriptor const TargetAddressSpaceDescriptor& addressSpaceDescriptor
) { ) {
using Services::StringService;
const auto segmentDescriptors = addressSpaceDescriptor.getIntersectingMemorySegmentDescriptors( const auto segmentDescriptors = addressSpaceDescriptor.getIntersectingMemorySegmentDescriptors(
TargetMemoryAddressRange{ TargetMemoryAddressRange{
regDescriptor.startAddress, regDescriptor.startAddress,
@@ -394,13 +404,15 @@ namespace Targets::RiscV
if (segmentDescriptors.empty()) { if (segmentDescriptors.empty()) {
throw Exceptions::Exception{ throw Exceptions::Exception{
"Cannot access system register `" + regDescriptor.key + "` - unknown memory segment" "Cannot access system register " + StringService::formatKey(regDescriptor.key)
+ " - unknown memory segment"
}; };
} }
if (segmentDescriptors.size() != 1) { if (segmentDescriptors.size() != 1) {
throw Exceptions::Exception{ throw Exceptions::Exception{
"Cannot access system register `" + regDescriptor.key + "` - register spans multiple memory segments" "Cannot access system register " + StringService::formatKey(regDescriptor.key)
+ " - register spans multiple memory segments"
}; };
} }

View File

@@ -87,8 +87,8 @@ namespace Targets::RiscV::Wch
} }
Logger::info( Logger::info(
"Selected program memory segment: \"" + this->selectedProgramSegmentDescriptor.name + "\" (`" "Selected program memory segment: \"" + this->selectedProgramSegmentDescriptor.name + "\" ("
+ this->selectedProgramSegmentDescriptor.key + "`)" + Services::StringService::formatKey(this->selectedProgramSegmentDescriptor.key) + ")"
); );
if ( if (
@@ -198,8 +198,9 @@ namespace Targets::RiscV::Wch
) { ) {
if (!this->selectedProgramSegmentDescriptor.programmingModeAccess.writeable) { if (!this->selectedProgramSegmentDescriptor.programmingModeAccess.writeable) {
throw Exceptions::Exception{ throw Exceptions::Exception{
"The selected program memory segment (`" + this->selectedProgramSegmentDescriptor.key "The selected program memory segment ("
+ "`) is not writable - cannot insert software breakpoint" + Services::StringService::formatKey(this->selectedProgramSegmentDescriptor.key)
+ ") is not writable - cannot insert software breakpoint"
}; };
} }
@@ -225,8 +226,9 @@ namespace Targets::RiscV::Wch
) { ) {
if (!this->selectedProgramSegmentDescriptor.programmingModeAccess.writeable) { if (!this->selectedProgramSegmentDescriptor.programmingModeAccess.writeable) {
throw Exceptions::Exception{ throw Exceptions::Exception{
"The selected program memory segment (`" + this->selectedProgramSegmentDescriptor.key "The selected program memory segment ("
+ "`) is not writable - cannot remove software breakpoint" + Services::StringService::formatKey(this->selectedProgramSegmentDescriptor.key)
+ ") is not writable - cannot remove software breakpoint"
}; };
} }
@@ -267,8 +269,9 @@ namespace Targets::RiscV::Wch
throw Exceptions::Exception{ throw Exceptions::Exception{
"Read access range (0x" + StringService::toHex(addressRange.startAddress) + " -> 0x" "Read access range (0x" + StringService::toHex(addressRange.startAddress) + " -> 0x"
+ StringService::toHex(addressRange.endAddress) + ", " + std::to_string(addressRange.size()) + StringService::toHex(addressRange.endAddress) + ", " + std::to_string(addressRange.size())
+ " bytes) exceeds the boundary of the selected program segment `" + aliasedSegment.key + " bytes) exceeds the boundary of the selected program segment "
+ "` (0x" + StringService::toHex(aliasedSegment.addressRange.startAddress) + " -> 0x" + StringService::formatKey(aliasedSegment.key) + " (0x"
+ StringService::toHex(aliasedSegment.addressRange.startAddress) + " -> 0x"
+ StringService::toHex(aliasedSegment.addressRange.endAddress) + ", " + StringService::toHex(aliasedSegment.addressRange.endAddress) + ", "
+ std::to_string(aliasedSegment.addressRange.size()) + " bytes)" + std::to_string(aliasedSegment.addressRange.size()) + " bytes)"
}; };
@@ -308,7 +311,8 @@ namespace Targets::RiscV::Wch
&& (!this->programmingMode || !aliasedSegment.programmingModeAccess.writeable) && (!this->programmingMode || !aliasedSegment.programmingModeAccess.writeable)
) { ) {
throw Exceptions::Exception{ throw Exceptions::Exception{
"The selected program memory segment (`" + aliasedSegment.key + "`) is not writable" "The selected program memory segment (" + StringService::formatKey(aliasedSegment.key)
+ ") is not writable"
}; };
} }
@@ -323,8 +327,9 @@ namespace Targets::RiscV::Wch
throw Exceptions::Exception{ throw Exceptions::Exception{
"Write access range (0x" + StringService::toHex(addressRange.startAddress) + " -> 0x" "Write access range (0x" + StringService::toHex(addressRange.startAddress) + " -> 0x"
+ StringService::toHex(addressRange.endAddress) + ", " + std::to_string(addressRange.size()) + StringService::toHex(addressRange.endAddress) + ", " + std::to_string(addressRange.size())
+ " bytes) exceeds the boundary of the selected program segment `" + aliasedSegment.key + " bytes) exceeds the boundary of the selected program segment "
+ "` (0x" + StringService::toHex(aliasedSegment.addressRange.startAddress) + " -> 0x" + StringService::formatKey(aliasedSegment.key) + " (0x"
+ StringService::toHex(aliasedSegment.addressRange.startAddress) + " -> 0x"
+ StringService::toHex(aliasedSegment.addressRange.endAddress) + ", " + StringService::toHex(aliasedSegment.addressRange.endAddress) + ", "
+ std::to_string(aliasedSegment.addressRange.size()) + " bytes)" + std::to_string(aliasedSegment.addressRange.size()) + " bytes)"
}; };
@@ -350,7 +355,10 @@ namespace Targets::RiscV::Wch
return this->eraseMainFlashSegment(); return this->eraseMainFlashSegment();
} }
Logger::debug("Ignoring erase operation on `" + memorySegmentDescriptor.key + "` segment - not supported"); Logger::debug(
"Ignoring erase operation on " + Services::StringService::formatKey(memorySegmentDescriptor.key)
+ " segment - not supported"
);
} }
TargetMemoryAddress WchRiscV::getProgramCounter() { TargetMemoryAddress WchRiscV::getProgramCounter() {
@@ -385,8 +393,8 @@ namespace Targets::RiscV::Wch
*/ */
const auto deAliasedAddress = this->deAliasMappedAddress(programCounter, actualAliasedSegment); const auto deAliasedAddress = this->deAliasMappedAddress(programCounter, actualAliasedSegment);
Logger::warning( Logger::warning(
"The mapped program memory segment is currently aliasing the `" + actualAliasedSegment.key "The mapped program memory segment is currently aliasing the " + actualAliasedSegment.key
+ "` segment - the program counter (0x" + StringService::toHex(programCounter) + " segment - the program counter (0x" + StringService::toHex(programCounter)
+ ") has been de-aliased to 0x" + StringService::toHex(deAliasedAddress) + ") has been de-aliased to 0x" + StringService::toHex(deAliasedAddress)
); );
return deAliasedAddress; return deAliasedAddress;
@@ -518,11 +526,11 @@ namespace Targets::RiscV::Wch
output += leftPadding + "mon " + StringService::applyTerminalColor("program_mode", CMD_COLOR) + " " output += leftPadding + "mon " + StringService::applyTerminalColor("program_mode", CMD_COLOR) + " "
+ StringService::applyTerminalColor("boot", PARAM_COLOR) + "\n"; + StringService::applyTerminalColor("boot", PARAM_COLOR) + "\n";
output += leftPadding + " To switch to boot mode, where the mapped program memory segment aliases the boot" output += leftPadding + " To switch to boot mode, where the mapped program memory segment aliases the boot"
" segment (`" + this->bootProgramSegmentDescriptor.key + "`).\n\n"; " segment (" + StringService::formatKey(this->bootProgramSegmentDescriptor.key) + ").\n\n";
output += leftPadding + "mon " + StringService::applyTerminalColor("program_mode", CMD_COLOR) + " " output += leftPadding + "mon " + StringService::applyTerminalColor("program_mode", CMD_COLOR) + " "
+ StringService::applyTerminalColor("user", PARAM_COLOR) + "\n"; + StringService::applyTerminalColor("user", PARAM_COLOR) + "\n";
output += leftPadding + " To switch to user mode, where the mapped program memory segment aliases the main" output += leftPadding + " To switch to user mode, where the mapped program memory segment aliases the main"
" program segment (`" + this->mainProgramSegmentDescriptor.key + "`).\n"; " program segment (" + StringService::formatKey(this->mainProgramSegmentDescriptor.key) + ").\n";
return output; return output;
} }
@@ -546,25 +554,15 @@ namespace Targets::RiscV::Wch
actualAliasedSegment == this->bootProgramSegmentDescriptor ? "boot mode" : "user mode", actualAliasedSegment == this->bootProgramSegmentDescriptor ? "boot mode" : "user mode",
StringService::TerminalColor::DARK_YELLOW StringService::TerminalColor::DARK_YELLOW
) + "\"\n"; ) + "\"\n";
response.output += "Aliased memory segment key: `" response.output += "Aliased memory segment key: "
+ StringService::applyTerminalColor( + StringService::formatKey(actualAliasedSegment.key) + "\n";
actualAliasedSegment.key, response.output += "Mapped address -> aliased address: 0x" + StringService::asciiToUpper(
StringService::TerminalColor::DARK_YELLOW StringService::toHex(this->mappedSegmentDescriptor.addressRange.startAddress)
) + "`\n"; ) + " -> 0x" + StringService::asciiToUpper(
response.output += "Mapped address -> aliased address: " + StringService::applyTerminalColor( StringService::toHex(actualAliasedSegment.addressRange.startAddress)
"0x" + StringService::asciiToUpper(
StringService::toHex(this->mappedSegmentDescriptor.addressRange.startAddress)
),
StringService::TerminalColor::BLUE
) + " -> " + StringService::applyTerminalColor(
"0x" + StringService::asciiToUpper(
StringService::toHex(actualAliasedSegment.addressRange.startAddress)
),
StringService::TerminalColor::BLUE
) + "\n"; ) + "\n";
response.output += "Program counter: " + StringService::applyTerminalColor( response.output += "Program counter: 0x" + StringService::asciiToUpper(
"0x" + StringService::asciiToUpper(StringService::toHex(this->getProgramCounter())), StringService::toHex(this->getProgramCounter())
StringService::TerminalColor::BLUE
) + "\n"; ) + "\n";
return response; return response;
@@ -580,9 +578,8 @@ namespace Targets::RiscV::Wch
EventManager::triggerEvent(std::make_shared<Events::TargetReset>()); EventManager::triggerEvent(std::make_shared<Events::TargetReset>());
response.output += "Boot mode has been enabled\n"; response.output += "Boot mode has been enabled\n";
response.output += "Program counter: " + StringService::applyTerminalColor( response.output += "Program counter: 0x" + StringService::asciiToUpper(
"0x" + StringService::asciiToUpper(StringService::toHex(this->getProgramCounter())), StringService::toHex(this->getProgramCounter())
StringService::TerminalColor::BLUE
) + "\n"; ) + "\n";
return response; return response;
@@ -598,9 +595,8 @@ namespace Targets::RiscV::Wch
EventManager::triggerEvent(std::make_shared<Events::TargetReset>()); EventManager::triggerEvent(std::make_shared<Events::TargetReset>());
response.output += "User mode has been enabled\n"; response.output += "User mode has been enabled\n";
response.output += "Program counter: " + StringService::applyTerminalColor( response.output += "Program counter: 0x" + StringService::asciiToUpper(
"0x" + StringService::asciiToUpper(StringService::toHex(this->getProgramCounter())), StringService::toHex(this->getProgramCounter())
StringService::TerminalColor::BLUE
) + "\n"; ) + "\n";
return response; return response;
@@ -687,7 +683,7 @@ namespace Targets::RiscV::Wch
probeAddress probeAddress
) ? this->mainProgramSegmentDescriptor : this->bootProgramSegmentDescriptor; ) ? this->mainProgramSegmentDescriptor : this->bootProgramSegmentDescriptor;
Logger::debug("Aliased program memory segment: `" + segment.key + "`"); Logger::debug("Aliased program memory segment: " + Services::StringService::formatKey(segment.key));
return segment; return segment;
} }
@@ -702,7 +698,8 @@ namespace Targets::RiscV::Wch
Logger::debug( Logger::debug(
"De-aliased mapped program memory address 0x" + StringService::toHex(address) + " to 0x" "De-aliased mapped program memory address 0x" + StringService::toHex(address) + " to 0x"
+ StringService::toHex(deAliasedAddress) + " (segment: `" + aliasedSegmentDescriptor.key + "`)" + StringService::toHex(deAliasedAddress) + " (segment: "
+ StringService::formatKey(aliasedSegmentDescriptor.key) + ")"
); );
return deAliasedAddress; return deAliasedAddress;

View File

@@ -65,8 +65,8 @@ namespace Targets
const auto segment = this->tryGetMemorySegmentDescriptor(key); const auto segment = this->tryGetMemorySegmentDescriptor(key);
if (!segment.has_value()) { if (!segment.has_value()) {
throw Exceptions::InternalFatalErrorException{ throw Exceptions::InternalFatalErrorException{
"Failed to get memory segment descriptor \"" + key + "\" from address space \"" + this->key "Failed to get memory segment descriptor `" + key + "` from address space `" + this->key
+ "\" - segment not found" + "` - segment not found"
}; };
} }

View File

@@ -99,8 +99,8 @@ namespace Targets
} }
throw Exceptions::InternalFatalErrorException{ throw Exceptions::InternalFatalErrorException{
"Failed to get address space descriptor from target descriptor - descriptor containing memory segment \"" "Failed to get address space descriptor from target descriptor - descriptor containing memory segment `"
+ memorySegmentKey + "\" not found" + memorySegmentKey + "` not found"
}; };
} }

View File

@@ -52,7 +52,7 @@ namespace Targets
if (!descriptor.has_value()) { if (!descriptor.has_value()) {
throw Exceptions::InternalFatalErrorException{ throw Exceptions::InternalFatalErrorException{
"Failed to get register group descriptor \"" + std::string{key} "Failed to get register group descriptor \"" + std::string{key}
+ "\" from peripheral \"" + this->key + "\" - register group descriptor not found" + "\" from peripheral `" + this->key + "` - register group descriptor not found"
}; };
} }