From 1e7d1c77fa384c0016da66781a5a10a3fd61f85a Mon Sep 17 00:00:00 2001 From: Mark Salyzyn Date: Fri, 16 Mar 2018 08:57:20 -0700 Subject: [PATCH] bootstat: alias underline to space in bit error rate handling When we are matching existing known boot reasons, we should try with compliant underlines first, then again with underlines replaced with spaces. Replace references to Ber with BitError for maintenance clarity. Replace helper functions with C++11 lambdas. Test: boot_reason_test.sh Bug: 63736262 Change-Id: I91b865013513526a55a85523080c7127f198968c --- bootstat/bootstat.cpp | 40 +++++++++++++++++++++++----------------- 1 file changed, 23 insertions(+), 17 deletions(-) diff --git a/bootstat/bootstat.cpp b/bootstat/bootstat.cpp index 74ba188c7..4ba430ce3 100644 --- a/bootstat/bootstat.cpp +++ b/bootstat/bootstat.cpp @@ -468,7 +468,7 @@ class pstoreConsole { // If bit error match to needle, correct it. // Return true if any corrections were discovered and applied. -bool correctForBer(std::string& reason, const std::string& needle) { +bool correctForBitError(std::string& reason, const std::string& needle) { bool corrected = false; if (reason.length() < needle.length()) return corrected; const pstoreConsole console(reason); @@ -486,6 +486,20 @@ bool correctForBer(std::string& reason, const std::string& needle) { return corrected; } +// If bit error match to needle, correct it. +// Return true if any corrections were discovered and applied. +// Try again if we can replace underline with spaces. +bool correctForBitErrorOrUnderline(std::string& reason, const std::string& needle) { + bool corrected = correctForBitError(reason, needle); + std::string _needle(needle); + std::transform(_needle.begin(), _needle.end(), _needle.begin(), + [](char c) { return (c == '_') ? ' ' : c; }); + if (needle != _needle) { + corrected |= correctForBitError(reason, _needle); + } + return corrected; +} + bool addKernelPanicSubReason(const pstoreConsole& console, std::string& ret) { // Check for kernel panic types to refine information if ((console.rfind("SysRq : Trigger a crash") != std::string::npos) || @@ -510,22 +524,14 @@ bool addKernelPanicSubReason(const std::string& content, std::string& ret) { return addKernelPanicSubReason(pstoreConsole(content), ret); } -// std::transform Helper callback functions: // Converts a string value representing the reason the system booted to a // string complying with Android system standard reason. -char tounderline(char c) { - return ::isblank(c) ? '_' : c; -} - -char toprintable(char c) { - return ::isprint(c) ? c : '?'; -} - -// Cleanup boot_reason regarding acceptable character set void transformReason(std::string& reason) { std::transform(reason.begin(), reason.end(), reason.begin(), ::tolower); - std::transform(reason.begin(), reason.end(), reason.begin(), tounderline); - std::transform(reason.begin(), reason.end(), reason.begin(), toprintable); + std::transform(reason.begin(), reason.end(), reason.begin(), + [](char c) { return ::isblank(c) ? '_' : c; }); + std::transform(reason.begin(), reason.end(), reason.begin(), + [](char c) { return ::isprint(c) ? c : '?'; }); } const char system_reboot_reason_property[] = "sys.boot.reason"; @@ -632,14 +638,14 @@ std::string BootReasonStrToReason(const std::string& boot_reason) { std::string subReason(content.substr(pos, max_reason_length)); // Correct against any known strings that Bit Error Match for (const auto& s : knownReasons) { - correctForBer(subReason, s); + correctForBitErrorOrUnderline(subReason, s); } for (const auto& m : kBootReasonMap) { if (m.first.length() <= strlen("cold")) continue; // too short? - if (correctForBer(subReason, m.first + "'")) continue; + if (correctForBitErrorOrUnderline(subReason, m.first + "'")) continue; if (m.first.length() <= strlen("reboot,cold")) continue; // short? if (!android::base::StartsWith(m.first, "reboot,")) continue; - correctForBer(subReason, m.first.substr(strlen("reboot,")) + "'"); + correctForBitErrorOrUnderline(subReason, m.first.substr(strlen("reboot,")) + "'"); } for (pos = 0; pos < subReason.length(); ++pos) { char c = subReason[pos]; @@ -687,7 +693,7 @@ std::string BootReasonStrToReason(const std::string& boot_reason) { if (pos != std::string::npos) { digits = content.substr(pos + strlen(battery), strlen("100 ")); // correct common errors - correctForBer(digits, "100 "); + correctForBitError(digits, "100 "); if (digits[0] == '!') digits[0] = '1'; if (digits[1] == '!') digits[1] = '1'; }