Skip to content

Commit

Permalink
cleanup(libsinsp): ppm_sc_modifies_state flag for sinsp-example
Browse files Browse the repository at this point in the history
Signed-off-by: Melissa Kilby <melissa.kilby.oss@gmail.com>
  • Loading branch information
incertum authored and poiana committed Apr 11, 2023
1 parent 28b9c9a commit 6c2876a
Showing 1 changed file with 29 additions and 8 deletions.
37 changes: 29 additions & 8 deletions userspace/libsinsp/examples/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ std::function<void(sinsp& inspector)> dump;
static bool g_interrupted = false;
static const uint8_t g_backoff_timeout_secs = 2;
static bool g_all_threads = false;
static bool ppm_sc_modifies_state = false;
static bool json_dump_init_success = false;
string engine_string = KMOD_ENGINE; /* Default for backward compatibility. */
string filter_string = "";
Expand Down Expand Up @@ -113,12 +114,13 @@ void parse_CLI_options(sinsp& inspector, int argc, char** argv)
{"output-fields-json", required_argument, 0, 'o'},
{"exclude-users", no_argument, 0, 'E'},
{"num-events", required_argument, 0, 'n'},
{"ppm-sc-modifies-state", no_argument, 0, 'z'},
{0, 0, 0, 0}};

int op;
int long_index = 0;
while((op = getopt_long(argc, argv,
"hf:jab:mks:d:o:En:",
"hf:jab:mks:d:o:Enz",
long_options, &long_index)) != -1)
{
switch(op)
Expand Down Expand Up @@ -163,6 +165,9 @@ void parse_CLI_options(sinsp& inspector, int argc, char** argv)
case 'n':
max_events = std::atol(optarg);
break;
case 'z':
ppm_sc_modifies_state = true;
break;
default:
break;
}
Expand All @@ -181,13 +186,29 @@ libsinsp::events::set<ppm_sc_code> extract_filter_sc_codes(sinsp& inspector)
return {};
}

void open_engine(sinsp& inspector)
void open_engine(sinsp& inspector, libsinsp::events::set<ppm_sc_code> events_sc_codes)
{
std::cout << "-- Try to open: '" + engine_string + "' engine." << std::endl;
libsinsp::events::set<ppm_sc_code> ppm_sc;

/* Select sc codes for active tracing in the kernel.
* Include all ppm sc codes from filter AST.
* Provide more e2e testing options.
* Demonstrate ppm sc API usage.
*/
if (ppm_sc_modifies_state)
{
ppm_sc = libsinsp::events::sinsp_state_sc_set().merge(events_sc_codes);
if (!ppm_sc.empty())
{
auto events_sc_names = libsinsp::events::sc_set_to_sc_names(ppm_sc);
printf("-- Activated ppm sc names in kernel: %s\n", concat_set_in_order(events_sc_names).c_str());
}
}

if(!engine_string.compare(KMOD_ENGINE))
{
inspector.open_kmod(buffer_bytes_dim);
inspector.open_kmod(buffer_bytes_dim, ppm_sc);
}
else if(!engine_string.compare(BPF_ENGINE))
{
Expand All @@ -200,7 +221,7 @@ void open_engine(sinsp& inspector)
{
std::cerr << bpf_path << std::endl;
}
inspector.open_bpf(bpf_path.c_str(), buffer_bytes_dim);
inspector.open_bpf(bpf_path.c_str(), buffer_bytes_dim, ppm_sc);
}
else if(!engine_string.compare(SAVEFILE_ENGINE))
{
Expand All @@ -213,7 +234,7 @@ void open_engine(sinsp& inspector)
}
else if(!engine_string.compare(MODERN_BPF_ENGINE))
{
inspector.open_modern_bpf(buffer_bytes_dim, DEFAULT_CPU_FOR_EACH_BUFFER, true);
inspector.open_modern_bpf(buffer_bytes_dim, DEFAULT_CPU_FOR_EACH_BUFFER, true, ppm_sc);
}
else
{
Expand Down Expand Up @@ -303,8 +324,6 @@ int main(int argc, char** argv)
signal(SIGINT, sigint_handler);
signal(SIGTERM, sigint_handler);

open_engine(inspector);

if(!filter_string.empty())
{
try
Expand All @@ -321,9 +340,11 @@ int main(int argc, char** argv)
if(!events_sc_codes.empty())
{
auto events_sc_names = libsinsp::events::sc_set_to_sc_names(events_sc_codes);
printf("-- Filter event types sc codes names: %s\n", concat_set_in_order(events_sc_names).c_str());
printf("-- Filter AST ppm sc names: %s\n", concat_set_in_order(events_sc_names).c_str());
}

open_engine(inspector, events_sc_codes);

std::cout << "-- Start capture" << std::endl;

inspector.start_capture();
Expand Down

0 comments on commit 6c2876a

Please sign in to comment.