Skip to content

Commit 0f6abff

Browse files
author
Mark Stemm
authored
Add proc.pcmdline. (#721)
Add proc.pcmdline, which returns the commandline of the parent process. This is useful for some cases like detecting ansible environments when you want to see the parent command line (in this case, ansible's use of python) to tell the difference between python and python-run-by-ansible.
1 parent 8b2198f commit 0f6abff

File tree

2 files changed

+65
-43
lines changed

2 files changed

+65
-43
lines changed

userspace/libsinsp/filterchecks.cpp

Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1302,6 +1302,7 @@ const filtercheck_field_info sinsp_filter_check_thread_fields[] =
13021302
{PT_UINT32, EPF_NONE, PF_DEC, "proc.nchilds", "the number of child threads that the process generating the event currently has. This excludes the main process thread."},
13031303
{PT_INT64, EPF_NONE, PF_ID, "proc.ppid", "the pid of the parent of the process generating the event."},
13041304
{PT_CHARBUF, EPF_NONE, PF_NA, "proc.pname", "the name (excluding the path) of the parent of the process generating the event."},
1305+
{PT_CHARBUF, EPF_NONE, PF_NA, "proc.pcmdline", "the full command line (proc.name + proc.args) of the parent of the process generating the event."},
13051306
{PT_INT64, EPF_NONE, PF_ID, "proc.apid", "the pid of one of the process ancestors. E.g. proc.apid[1] returns the parent pid, proc.apid[2] returns the grandparent pid, and so on. proc.apid[0] is the pid of the current process. proc.apid without arguments can be used in filters only and matches any of the process ancestors, e.g. proc.apid=1234."},
13061307
{PT_CHARBUF, EPF_NONE, PF_NA, "proc.aname", "the name (excluding the path) of one of the process ancestors. E.g. proc.aname[1] returns the parent name, proc.aname[2] returns the grandparent name, and so on. proc.aname[0] is the name of the current process. proc.aname without arguments can be used in filters only and matches any of the process ancestors, e.g. proc.aname=bash."},
13071308
{PT_INT64, EPF_NONE, PF_ID, "proc.loginshellid", "the pid of the oldest shell among the ancestors of the current process, if there is one. This field can be used to separate different user sessions, and is useful in conjunction with chisels like spy_user."},
@@ -1567,6 +1568,23 @@ uint8_t* sinsp_filter_check_thread::extract_thread_cpu(sinsp_evt *evt, sinsp_thr
15671568
return NULL;
15681569
}
15691570

1571+
static void populate_cmdline(string &cmdline, sinsp_threadinfo *tinfo)
1572+
{
1573+
cmdline = tinfo->get_comm() + " ";
1574+
1575+
uint32_t j;
1576+
uint32_t nargs = (uint32_t)tinfo->m_args.size();
1577+
1578+
for(j = 0; j < nargs; j++)
1579+
{
1580+
cmdline += tinfo->m_args[j];
1581+
if(j < nargs -1)
1582+
{
1583+
cmdline += ' ';
1584+
}
1585+
}
1586+
}
1587+
15701588
uint8_t* sinsp_filter_check_thread::extract(sinsp_evt *evt, OUT uint32_t* len, bool sanitize_strings)
15711589
{
15721590
sinsp_threadinfo* tinfo = evt->get_thread_info();
@@ -1674,20 +1692,7 @@ uint8_t* sinsp_filter_check_thread::extract(sinsp_evt *evt, OUT uint32_t* len, b
16741692
}
16751693
case TYPE_CMDLINE:
16761694
{
1677-
m_tstr = tinfo->get_comm() + " ";
1678-
1679-
uint32_t j;
1680-
uint32_t nargs = (uint32_t)tinfo->m_args.size();
1681-
1682-
for(j = 0; j < nargs; j++)
1683-
{
1684-
m_tstr += tinfo->m_args[j];
1685-
if(j < nargs -1)
1686-
{
1687-
m_tstr += ' ';
1688-
}
1689-
}
1690-
1695+
populate_cmdline(m_tstr, tinfo);
16911696
*len = m_tstr.size();
16921697
return (uint8_t*)m_tstr.c_str();
16931698
}
@@ -1802,6 +1807,22 @@ uint8_t* sinsp_filter_check_thread::extract(sinsp_evt *evt, OUT uint32_t* len, b
18021807
return NULL;
18031808
}
18041809
}
1810+
case TYPE_PCMDLINE:
1811+
{
1812+
sinsp_threadinfo* ptinfo =
1813+
m_inspector->get_thread(tinfo->m_ptid, false, true);
1814+
1815+
if(ptinfo != NULL)
1816+
{
1817+
populate_cmdline(m_tstr, ptinfo);
1818+
*len = m_tstr.size();
1819+
return (uint8_t*)m_tstr.c_str();
1820+
}
1821+
else
1822+
{
1823+
return NULL;
1824+
}
1825+
}
18051826
case TYPE_APID:
18061827
{
18071828
sinsp_threadinfo* mt = NULL;

userspace/libsinsp/filterchecks.h

Lines changed: 30 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -340,35 +340,36 @@ class sinsp_filter_check_thread : public sinsp_filter_check
340340
TYPE_NCHILDS = 9,
341341
TYPE_PPID = 10,
342342
TYPE_PNAME = 11,
343-
TYPE_APID = 12,
344-
TYPE_ANAME = 13,
345-
TYPE_LOGINSHELLID = 14,
346-
TYPE_DURATION = 15,
347-
TYPE_FDOPENCOUNT = 16,
348-
TYPE_FDLIMIT = 17,
349-
TYPE_FDUSAGE = 18,
350-
TYPE_VMSIZE = 19,
351-
TYPE_VMRSS = 20,
352-
TYPE_VMSWAP = 21,
353-
TYPE_PFMAJOR = 22,
354-
TYPE_PFMINOR = 23,
355-
TYPE_TID = 24,
356-
TYPE_ISMAINTHREAD = 25,
357-
TYPE_EXECTIME = 26,
358-
TYPE_TOTEXECTIME = 27,
359-
TYPE_CGROUPS = 28,
360-
TYPE_CGROUP = 29,
361-
TYPE_VTID = 30,
362-
TYPE_VPID = 31,
363-
TYPE_THREAD_CPU = 32,
364-
TYPE_THREAD_CPU_USER = 33,
365-
TYPE_THREAD_CPU_SYSTEM = 34,
366-
TYPE_THREAD_VMSIZE = 35,
367-
TYPE_THREAD_VMRSS = 36,
368-
TYPE_THREAD_VMSIZE_B = 37,
369-
TYPE_THREAD_VMRSS_B = 38,
370-
TYPE_SID = 39,
371-
TYPE_SNAME = 40,
343+
TYPE_PCMDLINE = 12,
344+
TYPE_APID = 13,
345+
TYPE_ANAME = 14,
346+
TYPE_LOGINSHELLID = 15,
347+
TYPE_DURATION = 16,
348+
TYPE_FDOPENCOUNT = 17,
349+
TYPE_FDLIMIT = 18,
350+
TYPE_FDUSAGE = 19,
351+
TYPE_VMSIZE = 20,
352+
TYPE_VMRSS = 21,
353+
TYPE_VMSWAP = 22,
354+
TYPE_PFMAJOR = 23,
355+
TYPE_PFMINOR = 24,
356+
TYPE_TID = 25,
357+
TYPE_ISMAINTHREAD = 26,
358+
TYPE_EXECTIME = 27,
359+
TYPE_TOTEXECTIME = 28,
360+
TYPE_CGROUPS = 29,
361+
TYPE_CGROUP = 30,
362+
TYPE_VTID = 31,
363+
TYPE_VPID = 32,
364+
TYPE_THREAD_CPU = 33,
365+
TYPE_THREAD_CPU_USER = 34,
366+
TYPE_THREAD_CPU_SYSTEM = 35,
367+
TYPE_THREAD_VMSIZE = 36,
368+
TYPE_THREAD_VMRSS = 37,
369+
TYPE_THREAD_VMSIZE_B = 38,
370+
TYPE_THREAD_VMRSS_B = 39,
371+
TYPE_SID = 40,
372+
TYPE_SNAME = 41,
372373
};
373374

374375
sinsp_filter_check_thread();

0 commit comments

Comments
 (0)