Stop printing non-ASCII characters from GDB in debug logs

This commit is contained in:
Nav
2023-05-07 16:45:04 +01:00
parent adcca0079d
commit e2f202d5c9
3 changed files with 16 additions and 1 deletions

View File

@@ -13,6 +13,7 @@
#include "src/Exceptions/Exception.hpp"
#include "src/Logger/Logger.hpp"
#include "src/Services/StringService.hpp"
namespace Bloom::DebugServer::Gdb
{
@@ -127,7 +128,12 @@ namespace Bloom::DebugServer::Gdb
continue;
}
Logger::debug("Read GDB packet: " + std::string(rawPacket.begin(), rawPacket.end()));
Logger::debug(
"Read GDB packet: "
+ Services::StringService::replaceUnprintable(
std::string(rawPacket.begin(), rawPacket.end())
)
);
// Acknowledge receipt
this->write({'+'});

View File

@@ -29,6 +29,14 @@ namespace Bloom::Services
});
}
std::string StringService::replaceUnprintable(std::string str) {
std::transform(str.begin(), str.end(), str.begin(), [] (unsigned char character) {
return character < 32 || character > 126 ? '?' : character;
});
return str;
}
std::string StringService::toHex(unsigned char value) {
auto stream = std::stringstream();
stream << std::hex << std::setfill('0') << std::setw(2) << static_cast<unsigned int>(value);

View File

@@ -13,6 +13,7 @@ namespace Bloom::Services
static std::string asciiToUpper(std::string str);
static bool isAscii(const std::string& str);
static std::string replaceUnprintable(std::string str);
static std::string toHex(unsigned char value);
static std::string toHex(const std::vector<unsigned char>& data);