From cc81b69a8955f3140e3a8bff6846e6d212b51caa Mon Sep 17 00:00:00 2001 From: salsal97 Date: Tue, 23 Aug 2022 23:55:58 +0000 Subject: [PATCH] added strace exclude filter feature --- tests/Makefile | 2 + tests/strace/.gitignore | 7 +++ tests/strace/Makefile | 50 +++++++++++++++++ tests/strace/strace.c | 25 +++++++++ tests/strace/test1.expected.txt | 2 + tests/strace/test2.expected.txt | 2 + tools/myst/host/strace.c | 96 +++++++++++++++++++++++---------- 7 files changed, 157 insertions(+), 27 deletions(-) create mode 100644 tests/strace/.gitignore create mode 100644 tests/strace/Makefile create mode 100644 tests/strace/strace.c create mode 100644 tests/strace/test1.expected.txt create mode 100644 tests/strace/test2.expected.txt diff --git a/tests/Makefile b/tests/Makefile index 0a0eac641f..128241efef 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -135,6 +135,8 @@ DIRS += glibc endif DIRS += appenv +DIRS += strace + .PHONY: $(DIRS) diff --git a/tests/strace/.gitignore b/tests/strace/.gitignore new file mode 100644 index 0000000000..f34d65968e --- /dev/null +++ b/tests/strace/.gitignore @@ -0,0 +1,7 @@ +1 +2 +3 +test1.txt +test2.txt +test3.txt +test4.txt \ No newline at end of file diff --git a/tests/strace/Makefile b/tests/strace/Makefile new file mode 100644 index 0000000000..4318c1bc7f --- /dev/null +++ b/tests/strace/Makefile @@ -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 diff --git a/tests/strace/strace.c b/tests/strace/strace.c new file mode 100644 index 0000000000..fbf3693d88 --- /dev/null +++ b/tests/strace/strace.c @@ -0,0 +1,25 @@ +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT License. + +#include +#include +#include +#include +#include +#include + +void foofoo() +{ +} + +int main(int argc, const char* argv[]) +{ + char buf[PATH_MAX]; + + foofoo(); + + getcwd(buf, sizeof(buf)); + assert(strcmp(buf, "/") == 0); + + return 0; +} diff --git a/tests/strace/test1.expected.txt b/tests/strace/test1.expected.txt new file mode 100644 index 0000000000..d0f0e887e0 --- /dev/null +++ b/tests/strace/test1.expected.txt @@ -0,0 +1,2 @@ +Unknown syscall SYS_invali specified in --strace-filter or --strace-exclude-filter +Aborted (core dumped) diff --git a/tests/strace/test2.expected.txt b/tests/strace/test2.expected.txt new file mode 100644 index 0000000000..8127ab8a1b --- /dev/null +++ b/tests/strace/test2.expected.txt @@ -0,0 +1,2 @@ +Cannot specify both --strace-filter and --strace-exclude-filter +Aborted (core dumped) diff --git a/tools/myst/host/strace.c b/tools/myst/host/strace.c index a6c962ddd9..3a0407374f 100644 --- a/tools/myst/host/strace.c +++ b/tools/myst/host/strace.c @@ -8,6 +8,49 @@ #include #include +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, @@ -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) { @@ -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)