From 6b99e18335076e68b3678bf0c6d6d49e07b24de7 Mon Sep 17 00:00:00 2001 From: Melissa Kilby Date: Sat, 20 Jan 2024 01:26:49 +0000 Subject: [PATCH] cleanup(libsinsp): add concat_attribute_thread_hierarchy helper Signed-off-by: Melissa Kilby --- .../libsinsp/sinsp_filtercheck_thread.cpp | 37 ++----------------- userspace/libsinsp/sinsp_filtercheck_thread.h | 22 +++++++++++ 2 files changed, 25 insertions(+), 34 deletions(-) diff --git a/userspace/libsinsp/sinsp_filtercheck_thread.cpp b/userspace/libsinsp/sinsp_filtercheck_thread.cpp index ce73ef5942..10708b5cb3 100644 --- a/userspace/libsinsp/sinsp_filtercheck_thread.cpp +++ b/userspace/libsinsp/sinsp_filtercheck_thread.cpp @@ -1204,18 +1204,7 @@ uint8_t* sinsp_filter_check_thread::extract(sinsp_evt *evt, OUT uint32_t* len, b { return NULL; } - - m_tstr = mt->get_comm(); - for(int32_t j = 0; j < m_argid; j++) - { - mt = mt->get_parent_thread(); - - if(mt == NULL) - { - RETURN_EXTRACT_STRING(m_tstr); - } - m_tstr = mt->get_comm() + "->" + m_tstr; - } + m_tstr = concat_attribute_thread_hierarchy(mt, m_argid, [](sinsp_threadinfo* t) { return t->get_comm(); }); RETURN_EXTRACT_STRING(m_tstr); } case TYPE_PEXE: @@ -1264,17 +1253,7 @@ uint8_t* sinsp_filter_check_thread::extract(sinsp_evt *evt, OUT uint32_t* len, b return NULL; } - m_tstr = mt->get_exe(); - for(int32_t j = 0; j < m_argid; j++) - { - mt = mt->get_parent_thread(); - - if(mt == NULL) - { - RETURN_EXTRACT_STRING(m_tstr); - } - m_tstr = mt->get_exe() + "->" + m_tstr; - } + m_tstr = concat_attribute_thread_hierarchy(mt, m_argid, [](sinsp_threadinfo* t) { return t->get_exe(); }); RETURN_EXTRACT_STRING(m_tstr); } case TYPE_PEXEPATH: @@ -1323,17 +1302,7 @@ uint8_t* sinsp_filter_check_thread::extract(sinsp_evt *evt, OUT uint32_t* len, b return NULL; } - m_tstr = mt->get_exepath(); - for(int32_t j = 0; j < m_argid; j++) - { - mt = mt->get_parent_thread(); - - if(mt == NULL) - { - RETURN_EXTRACT_STRING(m_tstr); - } - m_tstr = mt->get_exepath() + "->" + m_tstr; - } + m_tstr = concat_attribute_thread_hierarchy(mt, m_argid, [](sinsp_threadinfo* t) { return t->get_exepath(); }); RETURN_EXTRACT_STRING(m_tstr); } case TYPE_LOGINSHELLID: diff --git a/userspace/libsinsp/sinsp_filtercheck_thread.h b/userspace/libsinsp/sinsp_filtercheck_thread.h index c6a2b6521a..41d426d22c 100644 --- a/userspace/libsinsp/sinsp_filtercheck_thread.h +++ b/userspace/libsinsp/sinsp_filtercheck_thread.h @@ -18,6 +18,7 @@ limitations under the License. #pragma once +#include #include #include @@ -135,6 +136,27 @@ class sinsp_filter_check_thread : public sinsp_filter_check bool compare_full_acmdline(sinsp_evt *evt); bool compare_full_aenv(sinsp_evt *evt); + template + std::string concat_attribute_thread_hierarchy(sinsp_threadinfo* mt, int32_t m_argid, const std::function& get_attribute_func) + { + + // nullptr check of mt is done within each filtercheck prior to calling this function + static_assert(std::is_convertible::value, "T must be convertible to std::string to concat parent lineage thread attributes"); + std::string result = get_attribute_func(mt); + + for (int32_t j = 0; j < m_argid; j++) + { + mt = mt->get_parent_thread(); + if(mt == NULL) + { + return result; + } + result = get_attribute_func(mt) + "->" + result; + } + + return result; + } + int32_t m_argid; std::string m_argname; uint32_t m_tbool;