Skip to content

Commit

Permalink
cleanup(libsinsp): add concat_attribute_thread_hierarchy helper
Browse files Browse the repository at this point in the history
Signed-off-by: Melissa Kilby <[email protected]>
  • Loading branch information
incertum committed Mar 7, 2024
1 parent f19ca75 commit 6b99e18
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 34 deletions.
37 changes: 3 additions & 34 deletions userspace/libsinsp/sinsp_filtercheck_thread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::string>(mt, m_argid, [](sinsp_threadinfo* t) { return t->get_comm(); });
RETURN_EXTRACT_STRING(m_tstr);
}
case TYPE_PEXE:
Expand Down Expand Up @@ -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<std::string>(mt, m_argid, [](sinsp_threadinfo* t) { return t->get_exe(); });
RETURN_EXTRACT_STRING(m_tstr);
}
case TYPE_PEXEPATH:
Expand Down Expand Up @@ -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<std::string>(mt, m_argid, [](sinsp_threadinfo* t) { return t->get_exepath(); });
RETURN_EXTRACT_STRING(m_tstr);
}
case TYPE_LOGINSHELLID:
Expand Down
22 changes: 22 additions & 0 deletions userspace/libsinsp/sinsp_filtercheck_thread.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ limitations under the License.

#pragma once

#include <functional>
#include <libsinsp/sinsp_filtercheck.h>
#include <libsinsp/state/dynamic_struct.h>

Expand Down Expand Up @@ -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<typename T>
std::string concat_attribute_thread_hierarchy(sinsp_threadinfo* mt, int32_t m_argid, const std::function<T(sinsp_threadinfo*)>& get_attribute_func)
{

// nullptr check of mt is done within each filtercheck prior to calling this function
static_assert(std::is_convertible<T, std::string>::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;
Expand Down

0 comments on commit 6b99e18

Please sign in to comment.