Skip to content

Commit 42f91b1

Browse files
committed
initial and incomplete support for filtering in the get_thread_table lua api call
1 parent 2794711 commit 42f91b1

File tree

7 files changed

+128
-22
lines changed

7 files changed

+128
-22
lines changed

userspace/libsinsp/chisel.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -750,7 +750,7 @@ void sinsp_chisel::set_args(string args)
750750
throw sinsp_exception("corrupted parameters for chisel " + m_filename);
751751
}
752752

753-
m_argvals.push_back(args.substr(token_begin, j));
753+
m_argvals.push_back(args.substr(token_begin, j - quote_correction - token_begin));
754754
}
755755

756756
//

userspace/libsinsp/chisel.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,10 @@ class SINSP_PUBLIC sinsp_chisel
114114
void on_capture_start();
115115
void on_capture_end();
116116
bool get_nextrun_args(OUT string* args);
117+
chisel_desc* get_lua_script_info()
118+
{
119+
return &m_lua_script_info;
120+
}
117121

118122
private:
119123
bool openfile(string filename, OUT ifstream* is);

userspace/libsinsp/chisel_api.cpp

Lines changed: 87 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -577,7 +577,13 @@ int lua_cbacks::get_thread_table(lua_State *ls)
577577
threadinfo_map_iterator_t it;
578578
unordered_map<int64_t, sinsp_fdinfo_t>::iterator fdit;
579579
uint32_t j;
580+
sinsp_filter* filter = NULL;
581+
sinsp_evt tevt;
582+
scap_evt tscapevt;
580583

584+
//
585+
// Get the chisel state
586+
//
581587
lua_getglobal(ls, "sichisel");
582588

583589
sinsp_chisel* ch = (sinsp_chisel*)lua_touserdata(ls, -1);
@@ -587,6 +593,37 @@ int lua_cbacks::get_thread_table(lua_State *ls)
587593
ASSERT(ch->m_lua_cinfo);
588594
ASSERT(ch->m_inspector);
589595

596+
//
597+
// If the caller specified a filter, compile it
598+
//
599+
if(lua_isstring(ls, 1))
600+
{
601+
string filterstr = lua_tostring(ls, 1);
602+
lua_pop(ls, 1);
603+
604+
try
605+
{
606+
filter = new sinsp_filter(ch->m_inspector, filterstr);
607+
}
608+
catch(sinsp_exception& e)
609+
{
610+
string err = "invalid filter argument for get_thread_table in chisel " + ch->m_filename + ": " + e.what();
611+
fprintf(stderr, "%s\n", err.c_str());
612+
throw sinsp_exception("chisel error");
613+
}
614+
615+
tscapevt.ts = 0;
616+
tscapevt.type = PPME_SYSCALL_READ_X;
617+
tscapevt.len = 0;
618+
619+
tevt.m_inspector = ch->m_inspector;
620+
tevt.m_info = &(g_infotables.m_event_info[PPME_SYSCALL_READ_X]);
621+
tevt.m_pevt = NULL;
622+
tevt.m_cpuid = 0;
623+
tevt.m_evtnum = 0;
624+
tevt.m_pevt = &tscapevt;
625+
}
626+
590627
threadinfo_map_t* threadtable = ch->m_inspector->m_thread_manager->get_threads();
591628

592629
ASSERT(threadtable != NULL);
@@ -595,6 +632,39 @@ int lua_cbacks::get_thread_table(lua_State *ls)
595632

596633
for(it = threadtable->begin(); it != threadtable->end(); ++it)
597634
{
635+
if(it->second.m_comm == "colord")
636+
{
637+
int a = 0;
638+
}
639+
//
640+
// Check if there's at least an fd that matches the filter.
641+
// If not, skip this thread
642+
//
643+
sinsp_fdtable* fdtable = it->second.get_fd_table();
644+
645+
if(filter != NULL)
646+
{
647+
bool match = false;
648+
649+
for(fdit = fdtable->m_table.begin(); fdit != fdtable->m_table.end(); ++fdit)
650+
{
651+
tevt.m_tinfo = &(it->second);
652+
tevt.m_fdinfo = &(fdit->second);
653+
tscapevt.tid = it->first;
654+
655+
if(filter->run(&tevt))
656+
{
657+
match = true;
658+
break;
659+
}
660+
}
661+
662+
if(!match)
663+
{
664+
continue;
665+
}
666+
}
667+
598668
//
599669
// Set the thread properties
600670
//
@@ -702,10 +772,21 @@ int lua_cbacks::get_thread_table(lua_State *ls)
702772
// Create and populate the FD table
703773
//
704774
lua_pushstring(ls, "fdtable");
705-
sinsp_fdtable* fdtable = it->second.get_fd_table();
706775
lua_newtable(ls);
707776
for(fdit = fdtable->m_table.begin(); fdit != fdtable->m_table.end(); ++fdit)
708777
{
778+
tevt.m_tinfo = &(it->second);
779+
tevt.m_fdinfo = &(fdit->second);
780+
tscapevt.tid = it->first;
781+
782+
if(filter != NULL)
783+
{
784+
if(filter->run(&tevt) == false)
785+
{
786+
continue;
787+
}
788+
}
789+
709790
lua_newtable(ls);
710791
lua_pushliteral(ls, "name");
711792
lua_pushstring(ls, fdit->second.tostring_clean().c_str());
@@ -724,6 +805,11 @@ int lua_cbacks::get_thread_table(lua_State *ls)
724805
lua_rawseti(ls,-2, (uint32_t)it->first);
725806
}
726807

808+
if(filter)
809+
{
810+
delete filter;
811+
}
812+
727813
return 1;
728814
}
729815

userspace/libsinsp/event.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,7 @@ VISIBILITY_PRIVATE
312312
friend class sinsp_dumper;
313313
friend class sinsp_analyzer_fd_listener;
314314
friend class sinsp_analyzer_parsers;
315+
friend class lua_cbacks;
315316
};
316317

317318
/*@}*/

userspace/sysdig/chisels/lsof.lua

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,27 +16,35 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
1616
--]]
1717

1818
-- Chisel description
19-
description = "This chisel prints the open file descriptors for every process in the system, with an output that is very similar to the one of lsof";
19+
description = "This chisel prints the open file descriptirs for every process in the system, with an output that is very similar to the one of lsof";
2020
short_description = "List the open file descriptors.";
2121
category = "System State";
2222

2323
-- Argument list
24-
args = {}
24+
args =
25+
{
26+
{
27+
name = "filter",
28+
description = "a sysdig-like filter expression that allows restricting the FD list. E.g. 'proc.name=foo and fd.name contains /etc'.",
29+
argtype = "filter",
30+
optional = true
31+
}
32+
}
2533

2634
-- Imports and globals
2735
require "common"
2836
local dctable = {}
2937
local capturing = false
38+
local filter = nil
3039

3140
-- Argument notification callback
3241
function on_set_arg(name, val)
33-
if name == "dump_file_name" then
34-
do_dump = true
35-
dump_file_name = val
36-
return true
37-
end
42+
if name == "filter" then
43+
filter = val
44+
return true
45+
end
3846

39-
return false
47+
return false
4048
end
4149

4250
-- Initialization callback
@@ -59,7 +67,7 @@ function on_capture_end()
5967
return
6068
end
6169

62-
local ttable = sysdig.get_thread_table()
70+
local ttable = sysdig.get_thread_table(filter)
6371

6472
print(extend_string("COMMAND", 20) ..
6573
extend_string("PID", 8) ..

userspace/sysdig/chisels/spy_port.lua

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,12 @@ args =
2828
description = "the remote host IP port number",
2929
argtype = "int"
3030
},
31-
{
32-
name = "disable_color",
33-
description = "Set to 'disable_colors' if you want to disable color output",
34-
argtype = "string",
35-
optional = true
36-
},
31+
{
32+
name = "disable_color",
33+
description = "Set to 'disable_colors' if you want to disable color output",
34+
argtype = "string",
35+
optional = true
36+
},
3737
}
3838

3939
require "common"

userspace/sysdig/sysdig.cpp

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -346,14 +346,21 @@ static void parse_chisel_args(sinsp_chisel* ch, sinsp* inspector, int optind, in
346346
}
347347
}
348348

349-
try
349+
if(nargs == 1 && ch->get_lua_script_info()->m_args[0].m_type == "filter")
350350
{
351-
sinsp_filter df(inspector, testflt);
351+
ch->set_args(args);
352352
}
353-
catch(...)
353+
else
354354
{
355-
ch->set_args(args);
356-
(*n_filterargs)++;
355+
try
356+
{
357+
sinsp_filter df(inspector, testflt);
358+
}
359+
catch(...)
360+
{
361+
ch->set_args(args);
362+
(*n_filterargs)++;
363+
}
357364
}
358365
}
359366
}

0 commit comments

Comments
 (0)