Skip to content

Commit

Permalink
added strace exclude filter feature
Browse files Browse the repository at this point in the history
  • Loading branch information
salsal97 committed Aug 23, 2022
1 parent 85ece7c commit 5ab2767
Show file tree
Hide file tree
Showing 7 changed files with 157 additions and 27 deletions.
2 changes: 2 additions & 0 deletions tests/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,8 @@ endif

DIRS += appenv
DIRS += go
DIRS += strace


.PHONY: $(DIRS)

Expand Down
7 changes: 7 additions & 0 deletions tests/strace/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
1
2
3
test1.txt
test2.txt
test3.txt
test4.txt
50 changes: 50 additions & 0 deletions tests/strace/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
TOP=$(abspath ../..)
include $(TOP)/defs.mak

APPDIR = appdir
CFLAGS = -fPIC
LDFLAGS = -Wl,-rpath=$(MUSL_LIB)

all:
$(MAKE) myst
$(MAKE) rootfs

rootfs: strace.c
mkdir -p $(APPDIR)/bin
$(CC) $(CFLAGS) -o $(APPDIR)/bin/strace strace.c $(LDFLAGS)
$(MYST) mkcpio $(APPDIR) rootfs

OPTS =

ifdef PERF
OPTS += --perf
endif

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

tests: all test1 test2 test3 test4
echo " === passed test strace"

test1:
-$(MYST_EXEC) rootfs /bin/strace --strace-filter=SYS_invali $(OPTS) 2> test1.txt || $(RUNTEST) diff test1.txt test1.expected.txt
echo "=== test1 passed"

test2:
-$(MYST_EXEC) rootfs /bin/strace --strace-filter=SYS_mmap --strace-exclude-filter=SYS_ioctl $(OPTS) 2> test2.txt || $(RUNTEST) diff test2.txt test2.expected.txt
echo "=== test2 passed"

test3:
$(MYST_EXEC) rootfs /bin/strace --strace-filter=SYS_close $(OPTS) 2> test3.txt
cat test3.txt | grep "SYS_" > 1 && cat test3.txt | grep "SYS_close" > 2 && $(RUNTEST) diff 1 2
echo "=== test3 passed"

test4:
$(MYST_EXEC) rootfs /bin/strace --strace-exclude-filter=SYS_open $(OPTS) 2> test4.txt
cat test4.txt | grep -v "SYS_open" > 3 && $(RUNTEST) diff 3 test4.txt
echo "=== test4 passed"

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

clean:
rm -rf $(APPDIR) rootfs export ramfs 1 2 3 test1.txt text2.txt test3.txt test4.txt
25 changes: 25 additions & 0 deletions tests/strace/strace.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

#include <assert.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

void foofoo()
{
}

int main(int argc, const char* argv[])
{
char buf[PATH_MAX];

foofoo();

getcwd(buf, sizeof(buf));
assert(strcmp(buf, "/") == 0);

return 0;
}
2 changes: 2 additions & 0 deletions tests/strace/test1.expected.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Unknown syscall SYS_invali specified in --strace-filter or --strace-exclude-filter
Aborted (core dumped)
2 changes: 2 additions & 0 deletions tests/strace/test2.expected.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Cannot specify both --strace-filter and --strace-exclude-filter
Aborted (core dumped)
96 changes: 69 additions & 27 deletions tools/myst/host/strace.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,49 @@
#include <myst/strings.h>
#include <myst/syscall.h>

int myst_set_strace_filter(
int num_tokens,
char** tokens,
myst_strace_config_t* strace_config,
bool include)
{
for (size_t i = 0; i < MYST_MAX_SYSCALLS; ++i)
{
strace_config->trace[i] = !include;
}

for (size_t i = 0; i < num_tokens; ++i)
{
const char* name = tokens[i];
long num = myst_syscall_num(name);
if (num >= 0)
{
if (num < MYST_MAX_SYSCALLS)
strace_config->trace[num] = include;
else
{
fprintf(
stderr,
"Syscall %s exceeds trace array. Fix "
"myst_syscall_config_t\n",
name);
abort();
}
}
else
{
fprintf(
stderr,
"Unknown syscall %s specified in --strace-filter or "
"--strace-exclude-filter \n",
name);
abort();
}
}
strace_config->filter = 1;
return 0;
}

int myst_parse_strace_config(
int* argc,
const char** argv,
Expand All @@ -17,6 +60,7 @@ int myst_parse_strace_config(
const char* filter = NULL;
char** tokens = NULL;
size_t num_tokens = 0;
bool filter_flag = 0;

if (cli_getopt(argc, argv, "--strace-failing", NULL) == 0)
{
Expand All @@ -31,35 +75,33 @@ int myst_parse_strace_config(
fprintf(stderr, "Invalid strace-filter '%s' specified.\n", filter);
abort();
}
for (size_t i = 0; i < num_tokens; ++i)

ret = myst_set_strace_filter(num_tokens, tokens, strace_config, 1);
filter_flag = true;
}

if (cli_getopt(argc, argv, "--strace-exclude-filter", &filter) == 0 &&
filter)
{
if (filter_flag)
{
const char* name = tokens[i];
long num = myst_syscall_num(name);
if (num >= 0)
{
if (num < MYST_MAX_SYSCALLS)
strace_config->trace[num] = 1;
else
{
fprintf(
stderr,
"Syscall %s exceeds trace array. Fix "
"myst_syscall_config_t\n",
name);
abort();
}
}
else
{
fprintf(
stderr,
"Unknown syscall %s specified in --strace=filter\n",
name);
abort();
}
fprintf(
stderr,
"Cannot specify both --strace-filter and "
"--strace-exclude-filter\n");
abort();
}
strace_config->filter = 1;
ret = 0;

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

ret = myst_set_strace_filter(num_tokens, tokens, strace_config, 0);
}

if (tokens)
Expand Down

0 comments on commit 5ab2767

Please sign in to comment.