aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSteven Rostedt (Google) <rostedt@goodmis.org>2022-11-15 15:53:54 -0500
committerSteven Rostedt (Google) <rostedt@goodmis.org>2022-11-15 16:05:45 -0500
commit1b0c600e2bd8152c703a0c3262050e51be520ad2 (patch)
tree60e881868aa7cddb118b493cc6b7b6e81a04da07
parent01926bce6c685ee1c06d7fb01a57ae6c8e8fc620 (diff)
downloadtrace-cmd-1b0c600e2bd8152c703a0c3262050e51be520ad2.tar.gz
trace-cmd utest: Separate out the grep and pipes
To make the grep and pipe functions in the unit test more flexible, separate them out and instead of passing in the arguments and using va_list directly, pass in a void pointer and use as structure to pass in the arguments. This will allow for extending the grep and pipe functions to use different type of arguments as well as functionality. Link: https://lore.kernel.org/linux-trace-devel/20221115205355.2580214-4-rostedt@goodmis.org Signed-off-by: Steven Rostedt (Google) <rostedt@goodmis.org>
-rw-r--r--utest/tracecmd-utest.c112
1 files changed, 95 insertions, 17 deletions
diff --git a/utest/tracecmd-utest.c b/utest/tracecmd-utest.c
index 2ec3f711..88d72177 100644
--- a/utest/tracecmd-utest.c
+++ b/utest/tracecmd-utest.c
@@ -10,6 +10,7 @@
#include <time.h>
#include <dirent.h>
#include <unistd.h>
+#include <ctype.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <sys/types.h>
@@ -111,9 +112,9 @@ static int run_trace(const char *cmd, ...)
return ret;
}
-static int pipe_it(int *ofd, int *efd, const char *cmd, va_list ap)
+static int pipe_it(int *ofd, int *efd, int (*func)(void *),
+ void *data)
{
- char **argv;
int obrass[2];
int ebrass[2];
pid_t pid;
@@ -130,9 +131,6 @@ static int pipe_it(int *ofd, int *efd, const char *cmd, va_list ap)
goto fail;
if (!pid) {
- argv = get_args(cmd, ap);
- if (!argv)
- exit(-1);
close(obrass[0]);
close(STDOUT_FILENO);
@@ -144,7 +142,7 @@ static int pipe_it(int *ofd, int *efd, const char *cmd, va_list ap)
if (dup2(obrass[1], STDERR_FILENO) < 0)
exit(-1);
- ret = execvp(tracecmd_exec, argv);
+ ret = func(data);
exit(ret);
}
@@ -165,26 +163,52 @@ static int pipe_it(int *ofd, int *efd, const char *cmd, va_list ap)
return -1;
}
-static int grep_it(const char *match, const char *cmd, ...)
+struct do_grep {
+ const char *cmd;
+ va_list *ap;
+};
+
+static int do_grep(void *data)
+{
+ struct do_grep *gdata = data;
+ char **argv;
+ int ret;
+
+ argv = get_args(gdata->cmd, *gdata->ap);
+ if (!argv)
+ exit(-1);
+
+ ret = execvp(tracecmd_exec, argv);
+ tracefs_list_free(argv);
+ return ret;
+}
+
+struct do_grep_it {
+ const char *match;
+ const char *cmd;
+ va_list *ap;
+};
+
+static int do_grep_it(void *data)
{
+ struct do_grep_it *dgdata = data;
+ struct do_grep gdata;
FILE *fp;
regex_t reg;
- va_list ap;
char *buf = NULL;
ssize_t n;
size_t l = 0;
- bool found = false;
int ofd;
int efd;
int pid;
int ret;
- if (regcomp(&reg, match, REG_ICASE|REG_NOSUB))
+ if (regcomp(&reg, dgdata->match, REG_ICASE|REG_NOSUB))
return -1;
- va_start(ap, cmd);
- pid = pipe_it(&ofd, &efd, cmd, ap);
- va_end(ap);
+ gdata.cmd = dgdata->cmd;
+ gdata.ap = dgdata->ap;
+ pid = pipe_it(&ofd, &efd, do_grep, &gdata);
if (pid < 0) {
regfree(&reg);
@@ -197,10 +221,65 @@ static int grep_it(const char *match, const char *cmd, ...)
do {
n = getline(&buf, &l, fp);
- if (show_output && n > 0)
- printf("%s", buf);
if (n > 0 && regexec(&reg, buf, 0, NULL, 0) == 0)
+ printf("%s", buf);
+ } while (n >= 0);
+
+ free(buf);
+ out:
+ ret = wait_for_exec(pid);
+ if (fp)
+ fclose(fp);
+ else
+ perror("fp");
+ close(ofd);
+ close(efd);
+ regfree(&reg);
+
+ return ret > 0 ? 0 : ret;
+}
+
+struct do_grep_match {
+ const char *match;
+ const char *cmd;
+ va_list *ap;
+};
+
+static int grep_match(const char *match, const char *cmd, ...)
+{
+ struct do_grep_it gdata;
+ FILE *fp;
+ va_list ap;
+ char *buf = NULL;
+ ssize_t n;
+ size_t l = 0;
+ bool found = false;
+ int ofd;
+ int efd;
+ int pid;
+ int ret;
+
+ va_start(ap, cmd);
+ gdata.match = match;
+ gdata.cmd = cmd;
+ gdata.ap = &ap;
+ pid = pipe_it(&ofd, &efd, do_grep_it, &gdata);
+ va_end(ap);
+
+ if (pid < 0)
+ return -1;
+
+ fp = fdopen(ofd, "r");
+ if (!fp)
+ goto out;
+
+ do {
+ n = getline(&buf, &l, fp);
+ if (n > 0) {
+ if (show_output)
+ printf("%s", buf);
found = true;
+ }
} while (n >= 0);
free(buf);
@@ -215,7 +294,6 @@ static int grep_it(const char *match, const char *cmd, ...)
close(ofd);
}
close(efd);
- regfree(&reg);
return found ? 0 : 1;
}
@@ -240,7 +318,7 @@ static void test_trace_convert6(void)
ret = run_trace("record", TRACECMD_OUT, "-e", "sched", "sleep", "1", NULL);
CU_TEST(ret == 0);
}
- ret = grep_it("[ \t]6[ \t]*\\[Version\\]", "dump", TRACECMD_IN2, NULL);
+ ret = grep_match("[ \t]6[ \t]*\\[Version\\]", "dump", TRACECMD_IN2, NULL);
CU_TEST(ret == 0);
}