@@ -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
0 commit comments