Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(libsinsp): make proc.p* (proc.pname...) behave like proc.a*[1] (proc.aname...) #2230

Merged
merged 6 commits into from
Jan 13, 2025
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cmake/modules/CompilerFlags.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ if(NOT MSVC)
if(BUILD_WARNINGS_AS_ERRORS)
set(CMAKE_COMPILE_WARNING_AS_ERROR ON)
set(CMAKE_SUPPRESSED_WARNINGS
"-Wno-unused-parameter -Wno-sign-compare -Wno-implicit-fallthrough -Wno-format-truncation"
"-Wno-unused-parameter -Wno-sign-compare -Wno-implicit-fallthrough -Wno-format-truncation -Wno-deprecated-declarations"
)
if(CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
# Clang needs these for suppressing these warnings: - C++20 array designators used with
Expand Down
251 changes: 56 additions & 195 deletions userspace/libsinsp/sinsp_filtercheck_thread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@
PF_NA,
"proc.pname",
"Parent Name",
"The proc.name truncated after 16 characters) of the process generating the event."},
"The proc.name (truncated after 16 characters) of the parent process."},
{PT_CHARBUF,
EPF_ARG_ALLOWED | EPF_NO_RHS | EPF_NO_TRANSFORMER,
PF_NA,
Expand Down Expand Up @@ -153,8 +153,7 @@
PF_NA,
"proc.pcmdline",
"Parent Command Line",
"The proc.cmdline (full command line (proc.name + proc.args)) of the parent of the "
"process generating the event."},
"The proc.cmdline (full command line (proc.name + proc.args)) of the parent process."},
{PT_CHARBUF,
EPF_ARG_ALLOWED | EPF_NO_RHS | EPF_NO_TRANSFORMER,
PF_NA,
Expand Down Expand Up @@ -1264,89 +1263,48 @@
return NULL;
}
}
case TYPE_PPID:
if(tinfo->is_main_thread()) {
if(!should_extract_xid(tinfo->m_ptid)) {
return NULL;
}
RETURN_EXTRACT_VAR(tinfo->m_ptid);
} else {
sinsp_threadinfo* mt = tinfo->get_main_thread();
case TYPE_PPID: {
sinsp_threadinfo* mt = tinfo->get_ancestor_process();
if(!mt) {
return NULL;
}

if(mt != NULL) {
if(!should_extract_xid(mt->m_ptid)) {
return NULL;
}
RETURN_EXTRACT_VAR(mt->m_ptid);
} else {
return NULL;
}
if(!should_extract_xid(mt->m_pid)) {
return NULL;
}
RETURN_EXTRACT_VAR(mt->m_pid);
}
case TYPE_PNAME: {
sinsp_threadinfo* ptinfo = m_inspector->get_thread_ref(tinfo->m_ptid, false, true).get();

if(ptinfo != NULL) {
m_tstr = ptinfo->get_comm();
RETURN_EXTRACT_STRING(m_tstr);
} else {
sinsp_threadinfo* ptinfo = tinfo->get_ancestor_process();
if(!ptinfo) {
return NULL;
}

m_tstr = ptinfo->get_comm();
RETURN_EXTRACT_STRING(m_tstr);
}
case TYPE_PCMDLINE: {
sinsp_threadinfo* ptinfo = m_inspector->get_thread_ref(tinfo->m_ptid, false, true).get();

if(ptinfo != NULL) {
sinsp_threadinfo::populate_cmdline(m_tstr, ptinfo);
RETURN_EXTRACT_STRING(m_tstr);
} else {
sinsp_threadinfo* ptinfo = tinfo->get_ancestor_process();
if(!ptinfo) {
return NULL;
}

sinsp_threadinfo::populate_cmdline(m_tstr, ptinfo);
RETURN_EXTRACT_STRING(m_tstr);
}
case TYPE_ACMDLINE: {
sinsp_threadinfo* mt = NULL;

if(tinfo->is_main_thread()) {
mt = tinfo;
} else {
mt = tinfo->get_main_thread();

if(mt == NULL) {
return NULL;
}
sinsp_threadinfo* mt = tinfo->get_ancestor_process(m_argid);
if(!mt) {
return NULL;
}

for(int32_t j = 0; j < m_argid; j++) {
mt = mt->get_parent_thread();

if(mt == NULL) {
return NULL;
}
}
sinsp_threadinfo::populate_cmdline(m_tstr, mt);
RETURN_EXTRACT_STRING(m_tstr);
}
case TYPE_APID: {
sinsp_threadinfo* mt = NULL;

if(tinfo->is_main_thread()) {
mt = tinfo;
} else {
mt = tinfo->get_main_thread();

if(mt == NULL) {
return NULL;
}
}

//
// Search for a specific ancestors
//
for(int32_t j = 0; j < m_argid; j++) {
mt = mt->get_parent_thread();

if(mt == NULL) {
return NULL;
}
sinsp_threadinfo* mt = tinfo->get_ancestor_process(m_argid);
if(!mt) {
return NULL;
}

if(!should_extract_xid(mt->m_pid)) {
Expand All @@ -1355,92 +1313,45 @@
RETURN_EXTRACT_VAR(mt->m_pid);
}
case TYPE_ANAME: {
sinsp_threadinfo* mt = NULL;

if(tinfo->is_main_thread()) {
mt = tinfo;
} else {
mt = tinfo->get_main_thread();

if(mt == NULL) {
return NULL;
}
}

for(int32_t j = 0; j < m_argid; j++) {
mt = mt->get_parent_thread();

if(mt == NULL) {
return NULL;
}
sinsp_threadinfo* mt = tinfo->get_ancestor_process(m_argid);
if(!mt) {
return NULL;
}

m_tstr = mt->get_comm();
RETURN_EXTRACT_STRING(m_tstr);
}
case TYPE_PEXE: {
sinsp_threadinfo* ptinfo = m_inspector->get_thread_ref(tinfo->m_ptid, false, true).get();

if(ptinfo != NULL) {
m_tstr = ptinfo->get_exe();
RETURN_EXTRACT_STRING(m_tstr);
} else {
sinsp_threadinfo* ptinfo = tinfo->get_ancestor_process();
if(!ptinfo) {
return NULL;
}

m_tstr = ptinfo->get_exe();
RETURN_EXTRACT_STRING(m_tstr);
}
case TYPE_AEXE: {
sinsp_threadinfo* mt = NULL;

if(tinfo->is_main_thread()) {
mt = tinfo;
} else {
mt = tinfo->get_main_thread();

if(mt == NULL) {
return NULL;
}
}

for(int32_t j = 0; j < m_argid; j++) {
mt = mt->get_parent_thread();

if(mt == NULL) {
return NULL;
}
sinsp_threadinfo* mt = tinfo->get_ancestor_process(m_argid);
if(!mt) {
return NULL;
}

m_tstr = mt->get_exe();
RETURN_EXTRACT_STRING(m_tstr);
}
case TYPE_PEXEPATH: {
sinsp_threadinfo* ptinfo = m_inspector->get_thread_ref(tinfo->m_ptid, false, true).get();

if(ptinfo != NULL) {
m_tstr = ptinfo->get_exepath();
RETURN_EXTRACT_STRING(m_tstr);
} else {
sinsp_threadinfo* ptinfo = tinfo->get_ancestor_process();
if(!ptinfo) {
return NULL;
}

m_tstr = ptinfo->get_exepath();
RETURN_EXTRACT_STRING(m_tstr);
}
case TYPE_AEXEPATH: {
sinsp_threadinfo* mt = NULL;

if(tinfo->is_main_thread()) {
mt = tinfo;
} else {
mt = tinfo->get_main_thread();

if(mt == NULL) {
return NULL;
}
}

for(int32_t j = 0; j < m_argid; j++) {
mt = mt->get_parent_thread();

if(mt == NULL) {
return NULL;
}
sinsp_threadinfo* mt = tinfo->get_ancestor_process(m_argid);
if(!mt) {
return NULL;
}

m_tstr = mt->get_exepath();
Expand Down Expand Up @@ -1478,26 +1389,26 @@

RETURN_EXTRACT_PTR(res);
}
case TYPE_DURATION:
case TYPE_DURATION: {

Check warning on line 1392 in userspace/libsinsp/sinsp_filtercheck_thread.cpp

View check run for this annotation

Codecov / codecov/patch

userspace/libsinsp/sinsp_filtercheck_thread.cpp#L1392

Added line #L1392 was not covered by tests
if(tinfo->m_clone_ts != 0) {
m_val.s64 = evt->get_ts() - tinfo->m_clone_ts;
ASSERT(m_val.s64 > 0);
RETURN_EXTRACT_VAR(m_val.s64);
} else {
return NULL;
}
}
case TYPE_PPID_DURATION: {
sinsp_threadinfo* ptinfo = m_inspector->get_thread_ref(tinfo->m_ptid, false, true).get();

if(ptinfo != NULL) {
if(ptinfo->m_clone_ts != 0) {
m_val.s64 = evt->get_ts() - ptinfo->m_clone_ts;
ASSERT(m_val.s64 > 0);
RETURN_EXTRACT_VAR(m_val.s64);
}
} else {
sinsp_threadinfo* ptinfo = tinfo->get_ancestor_process();

Check warning on line 1402 in userspace/libsinsp/sinsp_filtercheck_thread.cpp

View check run for this annotation

Codecov / codecov/patch

userspace/libsinsp/sinsp_filtercheck_thread.cpp#L1402

Added line #L1402 was not covered by tests
if(!ptinfo) {
return NULL;
}

if(ptinfo->m_clone_ts != 0) {
m_val.s64 = evt->get_ts() - ptinfo->m_clone_ts;
ASSERT(m_val.s64 > 0);
RETURN_EXTRACT_VAR(m_val.s64);

Check warning on line 1410 in userspace/libsinsp/sinsp_filtercheck_thread.cpp

View check run for this annotation

Codecov / codecov/patch

userspace/libsinsp/sinsp_filtercheck_thread.cpp#L1408-L1410

Added lines #L1408 - L1410 were not covered by tests
}
}
case TYPE_FDOPENCOUNT:
m_val.u64 = tinfo->get_fd_opencount();
Expand Down Expand Up @@ -1596,56 +1507,6 @@

m_val.u64 = tinfo->m_vpid;
RETURN_EXTRACT_VAR(m_val.u64);
/*
case TYPE_PROC_CPU:
{
uint16_t etype = evt->get_type();

if(etype == PPME_PROCINFO_E)
{
double thval;
uint64_t tcpu;

sinsp_evt_param* parinfo = evt->get_param(0);
tcpu = *(uint64_t*)parinfo->m_val;

parinfo = evt->get_param(1);
tcpu += *(uint64_t*)parinfo->m_val;

if(tinfo->m_last_t_tot_cpu != 0)
{
uint64_t deltaval = tcpu - tinfo->m_last_t_tot_cpu;
thval = (double)deltaval;// / (ONE_SECOND_IN_NS / 100);
if(thval > 100)
{
thval = 100;
}
}
else
{
thval = 0;
}

tinfo->m_last_t_tot_cpu = tcpu;

uint64_t ets = evt->get_ts();
sinsp_threadinfo* mt = tinfo->get_main_thread();

if(ets != mt->m_last_mt_cpu_ts)
{
mt->m_last_mt_tot_cpu = 0;
mt->m_last_mt_cpu_ts = ets;
}

mt->m_last_mt_tot_cpu += thval;
m_val.d = mt->m_last_mt_tot_cpu;

RETURN_EXTRACT_VAR(m_val.d);
}

return NULL;
}
*/
case TYPE_THREAD_CPU: {
return extract_thread_cpu(evt, len, tinfo, true, true);
}
Expand Down
5 changes: 0 additions & 5 deletions userspace/libsinsp/test/events_proc.ut.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -719,23 +719,19 @@ TEST_F(sinsp_with_test_input, spawn_process) {
// check that the name is updated
ASSERT_EQ(get_field_as_string(evt, "proc.name"), "test-exe");
ASSERT_EQ(get_field_as_string(evt, "proc.aname[0]"), "test-exe");
ASSERT_EQ(get_field_as_string(evt, "proc.aname"), "test-exe");
FedeDP marked this conversation as resolved.
Show resolved Hide resolved

// check that the pid is updated
ASSERT_EQ(get_field_as_string(evt, "proc.pid"), "20");
ASSERT_EQ(get_field_as_string(evt, "proc.vpid"), "20");
ASSERT_EQ(get_field_as_string(evt, "proc.apid[0]"), "20");
ASSERT_EQ(get_field_as_string(evt, "proc.apid"), "20");

// check that the exe is updated (first arg given in this test setup is same as full exepath)
ASSERT_EQ(get_field_as_string(evt, "proc.exe"), "/bin/test-exe");
ASSERT_EQ(get_field_as_string(evt, "proc.aexe[0]"), "/bin/test-exe");
ASSERT_EQ(get_field_as_string(evt, "proc.aexe"), "/bin/test-exe");

// check that the exepath is updated
ASSERT_EQ(get_field_as_string(evt, "proc.exepath"), "/bin/test-exe");
ASSERT_EQ(get_field_as_string(evt, "proc.aexepath[0]"), "/bin/test-exe");
ASSERT_EQ(get_field_as_string(evt, "proc.aexepath"), "/bin/test-exe");

// check session leader (sid) related fields
ASSERT_EQ(get_field_as_string(evt, "proc.sid"), "0");
Expand Down Expand Up @@ -774,7 +770,6 @@ TEST_F(sinsp_with_test_input, spawn_process) {
ASSERT_EQ(get_field_as_string(evt, "proc.pcmdline"), "init");
ASSERT_EQ(get_field_as_string(evt, "proc.acmdline[0]"),
"test-exe -c 'echo aGVsbG8K | base64 -d'");
ASSERT_EQ(get_field_as_string(evt, "proc.acmdline"), "test-exe -c 'echo aGVsbG8K | base64 -d'");
ASSERT_EQ(get_field_as_string(evt, "proc.acmdline[1]"), "init");
ASSERT_FALSE(field_has_value(evt, "proc.acmdline[2]"));

Expand Down
Loading
Loading