From: "Yordan Karadzhov (VMware)" <y.karadz@gmail.com>
To: rostedt@goodmis.org
Cc: tz.stoyanov@gmail.com, linux-trace-devel@vger.kernel.org,
"Yordan Karadzhov (VMware)" <y.karadz@gmail.com>
Subject: [PATCH 1/3] libtracefs: Add more methods for writing to files
Date: Thu, 15 Apr 2021 15:13:09 +0300 [thread overview]
Message-ID: <20210415121311.59787-2-y.karadz@gmail.com> (raw)
In-Reply-To: <20210415121311.59787-1-y.karadz@gmail.com>
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.
Signed-off-by: Yordan Karadzhov (VMware) <y.karadz@gmail.com>
---
include/tracefs.h | 4 +++
src/tracefs-instance.c | 74 +++++++++++++++++++++++++++++++++---------
2 files changed, 63 insertions(+), 15 deletions(-)
diff --git a/include/tracefs.h b/include/tracefs.h
index 55ee867..551c37c 100644
--- a/include/tracefs.h
+++ b/include/tracefs.h
@@ -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,
diff --git a/src/tracefs-instance.c b/src/tracefs-instance.c
index b8ce36f..2aeb529 100644
--- a/src/tracefs-instance.c
+++ b/src/tracefs-instance.c
@@ -337,21 +337,41 @@ const char *tracefs_instance_get_trace_dir(struct tracefs_instance *instance)
return NULL;
}
-static int write_file(const char *file, const char *str)
+static int write_file(const char *file, const char *str, int flags)
{
- int ret;
+ int ret = 0;
int fd;
- fd = open(file, O_WRONLY | O_TRUNC);
+ fd = open(file, flags);
if (fd < 0) {
tracefs_warning("Failed to open '%s'", file);
return -1;
}
- ret = write(fd, str, strlen(str));
+
+ if (str)
+ ret = write(fd, str, strlen(str));
+
close(fd);
return ret;
}
+static int instance_file_write(struct tracefs_instance *instance,
+ const char *file, const char *str, int flags)
+{
+ struct stat st;
+ char *path;
+ int ret;
+
+ path = tracefs_instance_get_file(instance, file);
+ if (!path)
+ return -1;
+ ret = stat(path, &st);
+ if (ret == 0)
+ ret = write_file(path, str, flags);
+ tracefs_put_tracing_file(path);
+
+ return ret;
+}
/**
* tracefs_instance_file_write - Write in trace file of specific instance.
@@ -364,19 +384,43 @@ 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);
+}
- 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_append - Append to a trace file of specific instance.
+ * @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.
+ */
+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_clear - Clear a trace file of specific instance.
+ * 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.
+ * @instance: ftrace instance, can be NULL for the top instance.
+ * @file: name of the file to clear.
+ *
+ * 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);
}
/**
--
2.25.1
next prev parent reply other threads:[~2021-04-15 12:13 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-04-15 12:13 [PATCH 0/3] New methods for writing to file Yordan Karadzhov (VMware)
2021-04-15 12:13 ` Yordan Karadzhov (VMware) [this message]
2021-04-15 12:13 ` [PATCH 2/3] libtracefs: Add tests for the new methods for writing Yordan Karadzhov (VMware)
2021-04-15 12:13 ` [PATCH 3/3] libtracefs: Document the new methods for writing to file Yordan Karadzhov (VMware)
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20210415121311.59787-2-y.karadz@gmail.com \
--to=y.karadz@gmail.com \
--cc=linux-trace-devel@vger.kernel.org \
--cc=rostedt@goodmis.org \
--cc=tz.stoyanov@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).