From: "Steven Rostedt (Google)" <rostedt@xxxxxxxxxxx> Add a simple test that does a trace-cmd record of schedule events and then reads it with trace-cmd report. This also adds some infrastructure to make it easy to run trace-cmd commands. Signed-off-by: Steven Rostedt (Google) <rostedt@xxxxxxxxxxx> --- utest/trace-utest.c | 7 +++- utest/trace-utest.h | 3 ++ utest/tracecmd-utest.c | 76 +++++++++++++++++++++++++++++++++++++++++- 3 files changed, 84 insertions(+), 2 deletions(-) diff --git a/utest/trace-utest.c b/utest/trace-utest.c index 802765a77673..051a0285fcc5 100644 --- a/utest/trace-utest.c +++ b/utest/trace-utest.c @@ -14,6 +14,7 @@ #include "trace-utest.h" const char *argv0; +bool show_output; enum unit_tests { RUN_NONE = 0, @@ -41,10 +42,11 @@ int main(int argc, char **argv) for (;;) { int c; int index = 0; - const char *opts = "+hsr:"; + const char *opts = "+hsr:v"; static struct option long_options[] = { {"silent", no_argument, NULL, 's'}, {"run", required_argument, NULL, 'r'}, + {"verbose", no_argument, NULL, 'v'}, {"help", no_argument, NULL, 'h'}, {NULL, 0, NULL, 0} }; @@ -62,6 +64,9 @@ int main(int argc, char **argv) case 's': verbose = CU_BRM_SILENT; break; + case 'v': + show_output = true; + break; case 'h': default: print_help(argv); diff --git a/utest/trace-utest.h b/utest/trace-utest.h index a9e365f8dcb5..b57e6469f4db 100644 --- a/utest/trace-utest.h +++ b/utest/trace-utest.h @@ -6,7 +6,10 @@ #ifndef _TRACE_UTEST_H_ #define _TRACE_UTEST_H_ +#include <stdbool.h> + extern const char *argv0; +extern bool show_output; void test_tracecmd_lib(void); diff --git a/utest/tracecmd-utest.c b/utest/tracecmd-utest.c index d85d9a21d20b..5e17f91c1720 100644 --- a/utest/tracecmd-utest.c +++ b/utest/tracecmd-utest.c @@ -5,27 +5,101 @@ */ #include <stdio.h> #include <stdlib.h> -#include <sys/stat.h> #include <fcntl.h> #include <unistd.h> #include <time.h> #include <dirent.h> +#include <unistd.h> +#include <sys/stat.h> +#include <sys/wait.h> +#include <sys/types.h> #include <CUnit/CUnit.h> #include <CUnit/Basic.h> +#include <tracefs.h> + #include "trace-utest.h" static char tracecmd_exec[PATH_MAX]; #define TRACECMD_SUITE "trace-cmd" +#define TRACECMD_FILE "__trace_test__.dat" +#define TRACECMD_OUT "-o", TRACECMD_FILE +#define TRACECMD_IN "-i", TRACECMD_FILE + +static void silent_output(void) +{ + close(STDOUT_FILENO); + open("/dev/null", O_WRONLY); + close(STDERR_FILENO); + open("/dev/null", O_WRONLY); +} + +static int run_trace(const char *cmd, ...) +{ + const char *param; + va_list ap; + char **tmp; + char **argv; + int status; + int ret = -1; + pid_t pid; + + argv = tracefs_list_add(NULL, tracecmd_exec); + if (!argv) + return -1; + + tmp = tracefs_list_add(argv, cmd); + if (!tmp) + goto out; + argv = tmp; + + va_start(ap, cmd); + for (param = va_arg(ap, const char *); + param; param = va_arg(ap, const char *)) { + tmp = tracefs_list_add(argv, param); + if (!tmp) + goto out; + argv = tmp; + } + va_end(ap); + + pid = fork(); + if (pid < 0) + goto out; + if (!pid) { + if (!show_output) + silent_output(); + ret = execvp(tracecmd_exec, argv); + exit (ret); + } + + ret = waitpid(pid, &status, 0); + if (ret != pid) { + ret = -1; + goto out; + } + + ret = WEXIT_STATUS(status); + out: + tracefs_list_free(argv); + return ret; +} static void test_trace_record_report(void) { + int ret; + + ret = run_trace("record", TRACECMD_OUT, "-e", "sched", "sleep", "1", NULL); + CU_TEST(ret == 0); + ret = run_trace("report", TRACECMD_IN, NULL); + CU_TEST(ret == 0); } static int test_suite_destroy(void) { + unlink(TRACECMD_FILE); return 0; } -- 2.35.1