Skip to contentSkip to conversation

libtracefs: Add more methods for writing to files

RFC PATCH In review Yordan Karadzhov (VMware) 2 participants 2 messages Apr 12, 2021

lore
Share & tools
lore
#0 lore raw

RFC PATCH 1/1 libtracefs: Add more methods for writing to files

For the moment the library provides only one method for writing to tracefs files, that is:

int tracefs_instance_file_write()

This method first truncates the size of the file to 0 (clearing it), then writes new content to this file. Essentially this method overwrites (clear + write). In this patch we add two additional methids:

int tracefs_instance_file_append()

which writes without clearing and

int tracefs_instance_file_clear()

which clears without writing anything. Those two new APIs have various use-cases. For example one can use the two methods when adding/clearing kprobes.

---
 include/tracefs.h      |  4 +++
 src/tracefs-instance.c | 65 ++++++++++++++++++++++++++++++++----------
 2 files changed, 54 insertions(+), 15 deletions(-)
include/tracefs.h +4 -0
index 55ee867..551c37c 100644
@@ -33,6 +33,10 @@ tracefs_instance_get_file(struct tracefs_instance *instance, const char *file);
 char *tracefs_instance_get_dir(struct tracefs_instance *instance);
 int tracefs_instance_file_write(struct tracefs_instance *instance,
 				const char *file, const char *str);
+int tracefs_instance_file_append(struct tracefs_instance *instance,
+				 const char *file, const char *str);
+int tracefs_instance_file_clear(struct tracefs_instance *instance,
+				const char *file);
 char *tracefs_instance_file_read(struct tracefs_instance *instance,
 				 const char *file, int *psize);
 int tracefs_instance_file_read_number(struct tracefs_instance *instance,
src/tracefs-instance.c +50 -15

90 lines not on this page · raw message

Signature · 1 line
2.25.1
#1 lore raw
Quoted 9 lines · On Mon, 12 Apr 2021 18:20:06 +0300 "Yordan Karadzhov (VMware)"…
src/tracefs-instance.c +2 -3
@@ -364,19 +384,34 @@ static int write_file(const char *file, const char *str)
 int tracefs_instance_file_write(struct tracefs_instance *instance,
 				 const char *file, const char *str)
 {
-	struct stat st;
-	char *path;
-	int ret;
+	return instance_file_write(instance, file, str, O_WRONLY | O_TRUNC);
+}

src/tracefs-instance.c static int write_file(const char *file, const char *str) · line 391

Quoted 10 lines
src/tracefs-instance.c +2 -7
 
-	path = tracefs_instance_get_file(instance, file);
-	if (!path)
-		return -1;
-	ret = stat(path, &st);
-	if (ret == 0)
-		ret = write_file(path, str);
-	tracefs_put_tracing_file(path);
+/**
+ * tracefs_instance_file_write - Append to a trace file of specific instance.

I think you meant "tracefs_instance_file_append - "

Quoted 5 lines
src/tracefs-instance.c +5 -0
+ * @instance: ftrace instance, can be NULL for the top instance
+ * @file: name of the file
+ * @str: nul terminated string, that will be appended to the file.
+ *
+ * Returns the number of appended bytes, or -1 in case of an error.

src/tracefs-instance.c static int write_file(const char *file, const char *str) · line 405

Quoted 10 lines
src/tracefs-instance.c +8 -1
+ */
+int tracefs_instance_file_append(struct tracefs_instance *instance,
+				 const char *file, const char *str)
+{
+	return instance_file_write(instance, file, str, O_WRONLY);
+}
 
-	return ret;
+/**
+ * tracefs_instance_file_write - Clear a trace file of specific instance.

And "tracefs_instance_file_clear - "

Review static int write_file(const char *file, const char *str) · line 407

Quoted 2 lines
(quoted diff) +2 -0
+ * @instance: ftrace instance, can be NULL for the top instance
+ * @file: name of the file

"of the file to clear"

More comments should be in the body.

"Used to clear the file. Note, it only opens with O_TRUNC and closes the file. If the file has content that does not get cleared in this way, this will not have any effect. For example, set_ftrace_filter can have probes that are not cleared by O_TRUNC:

echo "schedule:stacktrace" > set_ftrace_filter

This function will not clear the above "set_ftrace_filter" after that command."

src/tracefs-instance.c static int write_file(const char *file, const char *str) · line 417

Quoted 10 lines
src/tracefs-instance.c +7 -0
+ *
+ * Returns 0 on success, or -1 in case of an error.
+ */
+int tracefs_instance_file_clear(struct tracefs_instance *instance,
+				const char *file)
+{
+	return instance_file_write(instance, file, NULL, O_WRONLY | O_TRUNC);
 }
 
 /**

Other than that, it looks good.

-- Steve