Skip to content

Commit

Permalink
Adding filtering support to insights traces.
Browse files Browse the repository at this point in the history
1.Provides add and remove apis to add or remove the traces as per requirement.
2.Filters the required traces and reports it to esp-insights.
  • Loading branch information
shripad621git committed Sep 18, 2023
1 parent 242a52b commit e6ab974
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 2 deletions.
2 changes: 2 additions & 0 deletions src/tracing/esp32_trace/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -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",]
}
44 changes: 42 additions & 2 deletions src/tracing/esp32_trace/include/matter/tracing/macros_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,18 @@
#include "macros_impl.h"
#include <esp_heap_caps.h>
#include <esp_insights.h>

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 \
{ \
Expand All @@ -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
83 changes: 83 additions & 0 deletions src/tracing/esp32_trace/include/matter/tracing/macros_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,100 @@
#error "Tracing macros seem to be double defined"
#endif

#include <crypto/CHIPCryptoPAL.h>
#include <lib/core/CHIPError.h>

namespace Insights {
class ESP32Backend
{
public:
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(...) \
Expand Down

0 comments on commit e6ab974

Please sign in to comment.