* [PATCH v5 1/3] trace-cmd: Add new tracefs APIs for checking if a ftrace file / directory exists
2020-04-14 15:56 [PATCH v5 0/3] Extend coverage of "trace-cmd reset" command Tzvetomir Stoyanov (VMware)
@ 2020-04-14 15:56 ` Tzvetomir Stoyanov (VMware)
2020-04-14 15:56 ` [PATCH v5 2/3] trace-cmd: Unit tests for new libtracefs APIs Tzvetomir Stoyanov (VMware)
2020-04-14 15:56 ` [PATCH v5 3/3] trace-cmd: Clear error log on reset subcommand Tzvetomir Stoyanov (VMware)
2 siblings, 0 replies; 4+ messages in thread
From: Tzvetomir Stoyanov (VMware) @ 2020-04-14 15:56 UTC (permalink / raw)
To: rostedt; +Cc: linux-trace-devel
Some ftrace files and directories are optional, depending on specific
kernel configuration or version. It is a good practice to check if
the file / directory exist, before trying to access it.
There are a lot of places in trace-cmd implementation with such checks.
The new libtracefs APIs can be used for this, they are ftarce instance aware:
bool tracefs_file_exist(struct tracefs_instance *instance, char *name);
bool tracefs_dir_exist(struct tracefs_instance *instance, char *name);
Signed-off-by: Tzvetomir Stoyanov (VMware) <tz.stoyanov@gmail.com>
---
include/tracefs/tracefs.h | 3 +++
lib/tracefs/tracefs-instance.c | 46 +++++++++++++++++++++++++++++++++-
2 files changed, 48 insertions(+), 1 deletion(-)
diff --git a/include/tracefs/tracefs.h b/include/tracefs/tracefs.h
index 85690b66..bc8bebcb 100644
--- a/include/tracefs/tracefs.h
+++ b/include/tracefs/tracefs.h
@@ -33,6 +33,9 @@ int tracefs_instance_file_write(struct tracefs_instance *instance,
char *tracefs_instance_file_read(struct tracefs_instance *instance,
char *file, int *psize);
+bool tracefs_file_exist(struct tracefs_instance *instance, char *name);
+bool tracefs_dir_exist(struct tracefs_instance *instance, char *name);
+
/* events */
void tracefs_list_free(char **list);
char **tracefs_event_systems(const char *tracing_dir);
diff --git a/lib/tracefs/tracefs-instance.c b/lib/tracefs/tracefs-instance.c
index b96ab61c..a9ab64bd 100644
--- a/lib/tracefs/tracefs-instance.c
+++ b/lib/tracefs/tracefs-instance.c
@@ -13,7 +13,7 @@
#include <errno.h>
#include <sys/stat.h>
#include <fcntl.h>
-
+#include <linux/limits.h>
#include "tracefs.h"
#include "tracefs-local.h"
@@ -247,3 +247,47 @@ char *tracefs_instance_file_read(struct tracefs_instance *instance,
return buf;
}
+
+static bool check_file_exist(struct tracefs_instance *instance,
+ char *name, bool dir)
+{
+ char file[PATH_MAX];
+ struct stat st;
+ char *path;
+ int ret;
+
+ path = tracefs_instance_get_dir(instance);
+ snprintf(file, PATH_MAX, "%s/%s", path, name);
+ tracefs_put_tracing_file(path);
+ ret = stat(file, &st);
+ if (ret < 0)
+ return false;
+
+ return !dir == !S_ISDIR(st.st_mode);
+}
+
+/**
+ * tracefs_file_exist - Check if a file with given name exists in given instance
+ * @instance: ftrace instance, can be NULL for the top instance
+ * @name: name of the file
+ *
+ * Returns true if the file exists, false otherwise
+ *
+ * If a directory with the given name exists, false is returned.
+ */
+bool tracefs_file_exist(struct tracefs_instance *instance, char *name)
+{
+ return check_file_exist(instance, name, false);
+}
+
+/**
+ * tracefs_dir_exist - Check if a directory with given name exists in given instance
+ * @instance: ftrace instance, can be NULL for the top instance
+ * @name: name of the directory
+ *
+ * Returns true if the directory exists, false otherwise
+ */
+bool tracefs_dir_exist(struct tracefs_instance *instance, char *name)
+{
+ return check_file_exist(instance, name, true);
+}
--
2.25.1
^ permalink raw reply related [flat|nested] 4+ messages in thread
* [PATCH v5 2/3] trace-cmd: Unit tests for new libtracefs APIs
2020-04-14 15:56 [PATCH v5 0/3] Extend coverage of "trace-cmd reset" command Tzvetomir Stoyanov (VMware)
2020-04-14 15:56 ` [PATCH v5 1/3] trace-cmd: Add new tracefs APIs for checking if a ftrace file / directory exists Tzvetomir Stoyanov (VMware)
@ 2020-04-14 15:56 ` Tzvetomir Stoyanov (VMware)
2020-04-14 15:56 ` [PATCH v5 3/3] trace-cmd: Clear error log on reset subcommand Tzvetomir Stoyanov (VMware)
2 siblings, 0 replies; 4+ messages in thread
From: Tzvetomir Stoyanov (VMware) @ 2020-04-14 15:56 UTC (permalink / raw)
To: rostedt; +Cc: linux-trace-devel
Unit tests for tracefs APIs, checking for ftrace file / directory existence:
bool tracefs_file_exist(struct tracefs_instance *instance, char *name);
bool tracefs_dir_exist(struct tracefs_instance *instance, char *name);
Signed-off-by: Tzvetomir Stoyanov (VMware) <tz.stoyanov@gmail.com>
---
utest/tracefs-utest.c | 16 ++++++++++++++++
1 file changed, 16 insertions(+)
diff --git a/utest/tracefs-utest.c b/utest/tracefs-utest.c
index 2473e893..3f57ecad 100644
--- a/utest/tracefs-utest.c
+++ b/utest/tracefs-utest.c
@@ -175,6 +175,7 @@ static void test_instance_file_read(struct tracefs_instance *inst, char *fname)
#define ALL_TRACERS "available_tracers"
#define CUR_TRACER "current_tracer"
+#define PER_CPU "per_cpu"
static void test_instance_file(void)
{
struct tracefs_instance *instance = NULL;
@@ -251,6 +252,21 @@ static void test_instance_file(void)
tracefs_put_tracing_file(inst_file);
free(fname);
+ CU_TEST(tracefs_file_exist(NULL, (char *)name) == false);
+ CU_TEST(tracefs_dir_exist(NULL, (char *)name) == false);
+ CU_TEST(tracefs_file_exist(instance, (char *)name) == false);
+ CU_TEST(tracefs_dir_exist(instance, (char *)name) == false);
+
+ CU_TEST(tracefs_file_exist(NULL, CUR_TRACER) == true);
+ CU_TEST(tracefs_dir_exist(NULL, CUR_TRACER) == false);
+ CU_TEST(tracefs_file_exist(instance, CUR_TRACER) == true);
+ CU_TEST(tracefs_dir_exist(instance, CUR_TRACER) == false);
+
+ CU_TEST(tracefs_file_exist(NULL, PER_CPU) == false);
+ CU_TEST(tracefs_dir_exist(NULL, PER_CPU) == true);
+ CU_TEST(tracefs_file_exist(instance, PER_CPU) == false);
+ CU_TEST(tracefs_dir_exist(instance, PER_CPU) == true);
+
CU_TEST(tracefs_instance_destroy(NULL) != 0);
CU_TEST(tracefs_instance_destroy(instance) == 0);
CU_TEST(tracefs_instance_destroy(instance) != 0);
--
2.25.1
^ permalink raw reply related [flat|nested] 4+ messages in thread
* [PATCH v5 3/3] trace-cmd: Clear error log on reset subcommand
2020-04-14 15:56 [PATCH v5 0/3] Extend coverage of "trace-cmd reset" command Tzvetomir Stoyanov (VMware)
2020-04-14 15:56 ` [PATCH v5 1/3] trace-cmd: Add new tracefs APIs for checking if a ftrace file / directory exists Tzvetomir Stoyanov (VMware)
2020-04-14 15:56 ` [PATCH v5 2/3] trace-cmd: Unit tests for new libtracefs APIs Tzvetomir Stoyanov (VMware)
@ 2020-04-14 15:56 ` Tzvetomir Stoyanov (VMware)
2 siblings, 0 replies; 4+ messages in thread
From: Tzvetomir Stoyanov (VMware) @ 2020-04-14 15:56 UTC (permalink / raw)
To: rostedt; +Cc: linux-trace-devel
The "trace-cmd reset" command is supposed to set the ftrace state to default.
However, the ftrace error logs are not reseted.
A logic is added to delete the content of "error_log" file when "trace-cmd reset" is executed.
Signed-off-by: Tzvetomir Stoyanov (VMware) <tz.stoyanov@gmail.com>
---
tracecmd/trace-record.c | 23 +++++++++++++++++++++++
1 file changed, 23 insertions(+)
diff --git a/tracecmd/trace-record.c b/tracecmd/trace-record.c
index 1b3f403f..349a5596 100644
--- a/tracecmd/trace-record.c
+++ b/tracecmd/trace-record.c
@@ -4820,6 +4820,28 @@ static void clear_triggers(void)
clear_instance_triggers(instance);
}
+static void clear_instance_error_log(struct buffer_instance *instance)
+{
+ char *file;
+
+ if (!tracefs_file_exist(instance->tracefs, "error_log"))
+ return;
+
+ file = tracefs_instance_get_file(instance->tracefs, "error_log");
+ if (!file)
+ return;
+ write_file(file, " ");
+ tracefs_put_tracing_file(file);
+}
+
+static void clear_error_log(void)
+{
+ struct buffer_instance *instance;
+
+ for_all_instances(instance)
+ clear_instance_error_log(instance);
+}
+
static void clear_all_synth_events(void)
{
char sevent[BUFSIZ];
@@ -5564,6 +5586,7 @@ void trace_reset(int argc, char **argv)
clear_filters();
clear_triggers();
clear_all_synth_events();
+ clear_error_log();
/* set clock to "local" */
reset_clock();
reset_event_pid();
--
2.25.1
^ permalink raw reply related [flat|nested] 4+ messages in thread