Skip to content

Commit

Permalink
added strace pid filtering feature
Browse files Browse the repository at this point in the history
  • Loading branch information
salsal97 committed Sep 2, 2022
1 parent 8a86784 commit 7fd0c29
Show file tree
Hide file tree
Showing 9 changed files with 144 additions and 46 deletions.
5 changes: 5 additions & 0 deletions doc/user-getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,11 @@ Options:
from the output. Can be used in conjunction with any of the above filters
E.g: To filter by tid=101, specify -
--strace-filter-tid '101'
--strace-filter-pid 'pid1:pid2...'
-- Specify a set of process IDs to be traced, all others are excluded
from the output. Can be used in conjunction with any of the above filters
E.g: To filter by pid=101, specify -
--strace-filter-pid=101
```
You can also specify many other parameters in a configuration file, conventionally called `config.json`, you will also use this configuration file to package your application.
Expand Down
9 changes: 9 additions & 0 deletions include/myst/kernel.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,15 @@ typedef struct _myst_strace_config
*/
long tid_trace[MYST_MAX_IDS];

/* If the pid filter is enabled, this will store the number of pids to
* filter by */
int pid_filter_num;

/*
If pid filter is enabled, stores the list of pids to include.
*/
long pid_trace[MYST_MAX_IDS];

} myst_strace_config_t;

typedef struct myst_kernel_args
Expand Down
17 changes: 17 additions & 0 deletions kernel/syscall.c
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,23 @@ static bool _trace_syscall(long n)
// tid filter is specified, but no match in filter
return false;
}
if (__myst_kernel_args.strace_config.pid_filter_num > 0)
{
// Check if thread filtering is enabled & follow filter
myst_process_t* self = myst_process_self();
pid_t current_pid = self->pid;

for (int i = 0; i < __myst_kernel_args.strace_config.pid_filter_num;
i++)
{
if ((long)current_pid ==
__myst_kernel_args.strace_config.pid_trace[i])
return true;
}

// pid filter is specified, but no match in filter
return false;
}

// syscall filtering is enabled & syscall is to be traversed
return true;
Expand Down
6 changes: 6 additions & 0 deletions tests/strace/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,10 @@
14
15
16
17
18
19
20
21
22
test*.txt
2 changes: 2 additions & 0 deletions tests/strace/14test.expected.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Tid file is invalid. Must be a positive number.
Aborted (core dumped)
21 changes: 20 additions & 1 deletion tests/strace/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ endif

OPTS += --thread-stack-size=1048576

__TESTS=1 2 3 4 5 6 7 8 9 10 11 12 13
__TESTS=1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
.PHONY: $(__TESTS)

tests: $(__TESTS)
Expand Down Expand Up @@ -91,6 +91,25 @@ test13:
$(RUNTEST) test ! -s test13.txt || (cat test13.txt | grep -E '(tid=|SYS_)' > 15 && cat test13.txt | grep 'tid=101' | grep -v 'SYS_mmap' > 16 && $(RUNTEST) diff 15 16)
echo "=== test13 passed"

test14:
$(MYST_EXEC) rootfs /bin/strace --strace-filter-pid=file $(OPTS) 2> test14.txt || $(RUNTEST) diff test14.txt 10test.expected.txt
echo "=== test14 passed"

test15:
$(MYST_EXEC) rootfs /bin/strace --strace-filter-pid=101 $(OPTS) 2> test15.txt
$(RUNTEST) test ! -s test15.txt || (cat test15.txt | grep "pid=" > 17 && cat test15.txt | grep 'pid=101' > 18 && $(RUNTEST) diff 17 18)
echo "=== test15 passed"

test16:
$(MYST_EXEC) rootfs /bin/strace --strace-filter=SYS_mmap --strace-filter-pid 101 $(OPTS) 2> test16.txt
$(RUNTEST) test ! -s test16.txt || (cat test16.txt | grep -E '(pid=|SYS_)' > 19 && cat test16.txt | grep -E '(pid=101|SYS_mmap)' > 20 && $(RUNTEST) diff 19 20)
echo "=== test16 passed"

test17:
$(MYST_EXEC) rootfs /bin/strace --strace-exclude-filter=SYS_mmap --strace-filter-pid 101 $(OPTS) 2> test17.txt
$(RUNTEST) test ! -s test17.txt || (cat test17.txt | grep -E '(pid=|SYS_)' > 21 && cat test17.txt | grep 'pid=101' | grep -v 'SYS_mmap' > 22 && $(RUNTEST) diff 21 22)
echo "=== test17 passed"

myst:
$(MAKE) -C $(TOP)/tools/myst

Expand Down
8 changes: 5 additions & 3 deletions tools/myst/host/exec.c
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,11 @@ Options:\n\
from the output. Can be used in conjunction with any of the above filters \n\
E.g: To filter by tid=101, specify - \n\
--strace-filter-tid '101'\n\
--strace-filter-pid 'pid1:pid2...'\n\
-- Specify a set of process IDs to be traced, all others are excluded \n\
from the output. Can be used in conjunction with any of the above filters \n\
E.g: To filter by pid=101, specify - \n\
--strace-filter-pid=101\n\
\n"

int exec_action(int argc, const char* argv[], const char* envp[])
Expand Down Expand Up @@ -460,16 +465,13 @@ int exec_action(int argc, const char* argv[], const char* envp[])
cli_getopt(&argc, argv, "--strace", NULL) == 0)
{
options.strace_config.trace_syscalls = true;
// fprintf(stderr, "Failed to final options\n");
}

if (myst_strace_parse_config(&argc, argv, &options.strace_config) == 0)
{
options.strace_config.trace_syscalls = true;
// fprintf(stderr, "Failed to determine final options\n");
}

// fprintf(stderr, " to determine final options\n");
/* Get --trace option */
if (cli_getopt(&argc, argv, "--trace-errors", NULL) == 0 ||
cli_getopt(&argc, argv, "--etrace", NULL) == 0)
Expand Down
5 changes: 5 additions & 0 deletions tools/myst/host/exec_linux.c
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,11 @@ Options:\n\
from the output. Can be used in conjunction with any of the above filters \n\
E.g: To filter by tid=101, specify - \n\
--strace-filter-tid '101'\n\
--strace-filter-pid 'pid1:pid2...'\n\
-- Specify a set of process IDs to be traced, all others are excluded \n\
from the output. Can be used in conjunction with any of the above filters \n\
E.g: To filter by pid=101, specify - \n\
--strace-filter-pid=101\n\
\n\
"

Expand Down
117 changes: 75 additions & 42 deletions tools/myst/host/strace.c
Original file line number Diff line number Diff line change
Expand Up @@ -91,14 +91,73 @@ int myst_set_strace_filter(
return 0;
}

int myst_set_strace_id_filter(
const char* id_filter,
myst_strace_config_t* strace_config,
bool tid)
{
char** tokens = NULL;
size_t num_tokens = 0;
int ret = -1;

if (myst_strsplit(id_filter, ":", &tokens, &num_tokens) != 0)
{
fprintf(
stderr,
"Invalid strace-filter-tid or strace-filter-pid option '%s' "
"specified.\n",
id_filter);
abort();
}

for (size_t i = 0; i < num_tokens; ++i)
{
long id;
const char* _id = tokens[i];

ret = myst_str2long(_id, &id);

if (id <= 0)
{
fprintf(
stderr,
"tid or pid%s is invalid. Must be a positive number.\n",
_id);
abort();
}

if (tid)
strace_config->tid_trace[i] = id;
else
strace_config->pid_trace[i] = id;

if (i >= MYST_MAX_IDS)
{
fprintf(
stderr,
"Tid %s exceeds trace array. Fix "
"myst_syscall_config_t\n",
_id);
abort();
}
}

if (tid)
strace_config->tid_filter_num = num_tokens;
else
strace_config->pid_filter_num = num_tokens;

return ret;
}

int myst_strace_parse_config(
int* argc,
const char** argv,
myst_strace_config_t* strace_config)
{
int ret = -1;
const char* filter = NULL;
const char* tid_filter = NULL;
const char* id_filter = NULL;
char** tokens = NULL;
size_t num_tokens = 0;
bool filter_flag = 0;
Expand Down Expand Up @@ -145,48 +204,10 @@ int myst_strace_parse_config(
ret = myst_set_strace_filter(num_tokens, tokens, strace_config, 1);
}

if (cli_getopt(argc, argv, "--strace-filter-tid", &tid_filter) == 0 &&
tid_filter)
if (cli_getopt(argc, argv, "--strace-filter-tid", &id_filter) == 0 &&
id_filter)
{
if (myst_strsplit(tid_filter, ":", &tokens, &num_tokens) != 0)
{
fprintf(
stderr,
"Invalid strace-filter-tid option '%s' specified.\n",
tid_filter);
abort();
}

for (size_t i = 0; i < num_tokens; ++i)
{
long tid;
const char* _tid = tokens[i];

ret = myst_str2long(_tid, &tid);

if (tid <= 0)
{
fprintf(
stderr,
"Tid %s is invalid. Must be a positive number.\n",
_tid);
abort();
}

strace_config->tid_trace[i] = tid;

if (i >= MYST_MAX_IDS)
{
fprintf(
stderr,
"Tid %s exceeds trace array. Fix "
"myst_syscall_config_t\n",
_tid);
abort();
}
}

strace_config->tid_filter_num = num_tokens;
myst_set_strace_id_filter(id_filter, strace_config, true);
}
else
{
Expand All @@ -195,6 +216,18 @@ int myst_strace_parse_config(
ret = 0;
}

if (cli_getopt(argc, argv, "--strace-filter-pid", &id_filter) == 0 &&
id_filter)
{
ret = myst_set_strace_id_filter(id_filter, strace_config, false);
}
else
{
// tid filter is not enabled
strace_config->pid_filter_num = 0;
ret = 0;
}

if (tokens)
free(tokens);

Expand Down

0 comments on commit 7fd0c29

Please sign in to comment.