From 3653886be82acdbc4a08be713e8d7cb7f4a15739 Mon Sep 17 00:00:00 2001 From: shripad621git Date: Wed, 13 Sep 2023 11:28:24 +0530 Subject: [PATCH] Commit 1 --- examples/lighting-app/esp32/main/main.cpp | 1 + src/tracing/esp32_trace/BUILD.gn | 2 + .../include/matter/tracing/macros_impl.cpp | 48 +++++++++- .../include/matter/tracing/macros_impl.h | 92 +++++++++++++++++++ 4 files changed, 141 insertions(+), 2 deletions(-) diff --git a/examples/lighting-app/esp32/main/main.cpp b/examples/lighting-app/esp32/main/main.cpp index 79247258658dd8..54ba265fb7bf20 100644 --- a/examples/lighting-app/esp32/main/main.cpp +++ b/examples/lighting-app/esp32/main/main.cpp @@ -61,6 +61,7 @@ #if CONFIG_ENABLE_ESP_INSIGHTS_TRACE #include +#include #endif using namespace ::chip; 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..a85c665892b987 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 { +static bool isBlacklistInitialized = false; + +uint64_t Insights::ESP32Backend::blacklistHashes[BLACKLIST_SIZE] = { 0 }; +const char * ESP32Backend::blackListStrings[] = { + "PacketParser", + "MinMdnsResolver", + "SessionManager", + "DeviceCommissioner", +}; #define LOG_HEAP_INFO(label, group, entry_exit) \ do \ { \ @@ -28,17 +38,51 @@ namespace Insights { heap_caps_get_free_size(MALLOC_CAP_8BIT)); \ } while (0) +void ESP32Backend::InitializeBlacklistHashes() +{ + for (int i = 0; i < BLACKLIST_SIZE; 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; + if (error != CHIP_NO_ERROR) + { + printf("error"); + } + } +} + 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 (!isBlacklisted(mgroup)) + { + LOG_HEAP_INFO(mlabel, mgroup, "Entry"); + } } ESP32Backend::~ESP32Backend() { - LOG_HEAP_INFO(mlabel, mgroup, "Exit"); + if (!isBlacklisted(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..c6dd419ed0882b 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,18 @@ #error "Tracing macros seem to be double defined" #endif +/*static unsigned int hash(const char *str) { + unsigned int hash = 5381; + int c; + while ((c = *str++)) { + hash = ((hash << 5) + hash) + c; + } + return hash; +} +*/ +#include +#include + namespace Insights { class ESP32Backend { @@ -28,10 +40,90 @@ class ESP32Backend ESP32Backend(const char * str, ...); ~ESP32Backend(); + static void SetString(const char * aLabel) + { + bool duplicate = false; + int i = 0; + for (i = 0; i < BLACKLIST_SIZE; i++) + { + if (blackListStrings[i] == NULL) + { + break; + } + if (strcmp(aLabel, blackListStrings[i]) == 0) + { + duplicate = true; + break; + } + } + if (i < BLACKLIST_SIZE && !duplicate) + { + blackListStrings[i] = aLabel; + } + } + static void RemoveString(const char * aLabel) + { + bool found = false; + int i = 0; + for (i = 0; i < BLACKLIST_SIZE; i++) + { + if (blackListStrings[i] == NULL) + { + break; + } + if (strcmp(aLabel, blackListStrings[i]) == 0) + { + found = true; + break; + } + } + if (found) + { + for (int j = i; j < BLACKLIST_SIZE - 1; j++) + { + blackListStrings[j] = blackListStrings[j + 1]; + } + blackListStrings[BLACKLIST_SIZE - 1] = NULL; + } + } + + static const char * GetString(int aIndex) { return blackListStrings[aIndex]; } + private: const char * mlabel; const char * mgroup; + static constexpr int BLACKLIST_SIZE = 10; + static uint64_t blacklistHashes[BLACKLIST_SIZE]; + static void InitializeBlacklistHashes(); + static const char * blackListStrings[BLACKLIST_SIZE]; + + bool isBlacklisted(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 < BLACKLIST_SIZE; i++) + { + if (truncated_hash == blacklistHashes[i]) + { + return true; + } + } + return false; + } }; + } // namespace Insights #define MATTER_TRACE_SCOPE(...) \