diff --git a/src/tracing/esp32_trace/BUILD.gn b/src/tracing/esp32_trace/BUILD.gn index d7580a3e68ac2a..c9e2767ed2c587 100644 --- a/src/tracing/esp32_trace/BUILD.gn +++ b/src/tracing/esp32_trace/BUILD.gn @@ -24,4 +24,6 @@ source_set("esp32_trace") { public = [ "include/matter/tracing/macros_impl.h" ] sources = [ "include/matter/tracing/macros_impl.cpp" ] public_configs = [ ":tracing" ] + public_deps = ["${chip_root}/src/crypto", + "${chip_root}/src/lib/core",] } diff --git a/src/tracing/esp32_trace/include/matter/tracing/macros_impl.cpp b/src/tracing/esp32_trace/include/matter/tracing/macros_impl.cpp index 7046dd9cc8d7d9..05cc97fdeb0d74 100644 --- a/src/tracing/esp32_trace/include/matter/tracing/macros_impl.cpp +++ b/src/tracing/esp32_trace/include/matter/tracing/macros_impl.cpp @@ -17,8 +17,18 @@ #include "macros_impl.h" #include #include + namespace Insights { +bool ESP32Backend::isBlacklistInitialized = false; +uint64_t ESP32Backend::blacklistHashes[blacklistSize] = { 0 }; +const char * ESP32Backend::blackListStrings[] = { + "PacketParser", + "MinMdnsResolver", + "SessionManager", + "DeviceCommissioner", +}; + #define LOG_HEAP_INFO(label, group, entry_exit) \ do \ { \ @@ -28,17 +38,47 @@ namespace Insights { heap_caps_get_free_size(MALLOC_CAP_8BIT)); \ } while (0) +void ESP32Backend::InitializeBlacklistHashes() +{ + for (int i = 0; i < blacklistSize; i++) + { + if (blackListStrings[i] == NULL) + break; + size_t data_length = strlen(blackListStrings[i]); + uint8_t sha1_hash[20]; + CHIP_ERROR error = chip::Crypto::Hash_SHA1((const uint8_t *) blackListStrings[i], data_length, sha1_hash); + uint64_t truncated_hash = 0; + for (int j = 0; j < 8; j++) + { + truncated_hash <<= 8; + truncated_hash |= sha1_hash[j]; + } + blacklistHashes[i] = truncated_hash; + } +} + ESP32Backend::ESP32Backend(const char * str, ...) { va_list args; va_start(args, str); mlabel = str; mgroup = va_arg(args, const char *); - LOG_HEAP_INFO(mlabel, mgroup, "Entry"); + if (!isBlacklistInitialized) + { + InitializeBlacklistHashes(); + isBlacklistInitialized = true; + } + if (!isBlacklistedString(mgroup)) + { + LOG_HEAP_INFO(mlabel, mgroup, "Entry"); + } } ESP32Backend::~ESP32Backend() { - LOG_HEAP_INFO(mlabel, mgroup, "Exit"); + if (!isBlacklistedString(mgroup)) + { + LOG_HEAP_INFO(mlabel, mgroup, "Exit"); + } } } // namespace Insights diff --git a/src/tracing/esp32_trace/include/matter/tracing/macros_impl.h b/src/tracing/esp32_trace/include/matter/tracing/macros_impl.h index 1ab529313c3192..37a5611c4618d8 100644 --- a/src/tracing/esp32_trace/include/matter/tracing/macros_impl.h +++ b/src/tracing/esp32_trace/include/matter/tracing/macros_impl.h @@ -21,6 +21,9 @@ #error "Tracing macros seem to be double defined" #endif +#include +#include + namespace Insights { class ESP32Backend { @@ -28,10 +31,90 @@ class ESP32Backend ESP32Backend(const char * str, ...); ~ESP32Backend(); + static void AddStringToBlacklist(const char * aLabel) + { + bool duplicate = false; + int i = 0; + for (i = 0; i < blacklistSize; i++) + { + if (blackListStrings[i] == NULL) + { + break; + } + if (strcmp(aLabel, blackListStrings[i]) == 0) + { + duplicate = true; + break; + } + } + if (i < blacklistSize && !duplicate) + { + blackListStrings[i] = aLabel; + } + } + static void RemoveStringFromBlacklist(const char * aLabel) + { + bool found = false; + int i = 0; + for (i = 0; i < blacklistSize; i++) + { + if (blackListStrings[i] == NULL) + { + break; + } + if (strcmp(aLabel, blackListStrings[i]) == 0) + { + found = true; + break; + } + } + if (found) + { + for (int j = i; j < blacklistSize - 1; j++) + { + blackListStrings[j] = blackListStrings[j + 1]; + } + blackListStrings[blacklistSize - 1] = NULL; + } + } + + static const char * GetString(int aIndex) { return blackListStrings[aIndex]; } + private: const char * mlabel; const char * mgroup; + static constexpr int blacklistSize = 10; + static uint64_t blacklistHashes[blacklistSize]; + static void InitializeBlacklistHashes(); + static const char * blackListStrings[blacklistSize]; + static bool isBlacklistInitialized; + bool isBlacklistedString(const char * input) + { + uint8_t sha1_hash[20]; + size_t data_length = strlen(input); + CHIP_ERROR error = chip::Crypto::Hash_SHA1((const uint8_t *) input, data_length, sha1_hash); + if (error != CHIP_NO_ERROR) + { + return false; + } + uint64_t truncated_hash = 0; + for (int j = 0; j < 8; j++) + { + truncated_hash <<= 8; + truncated_hash |= sha1_hash[j]; + } + + for (int i = 0; i < blacklistSize; i++) + { + if (truncated_hash == blacklistHashes[i]) + { + return true; + } + } + return false; + } }; + } // namespace Insights #define MATTER_TRACE_SCOPE(...) \