Added new APIs for opening trace_marker file of given instance: tracefs_marker_init() tracefs_marker_write() tracefs_marker_vprint() tracefs_marker_print() tracefs_marker_close() Signed-off-by: Tzvetomir Stoyanov (VMware) <tz.stoyanov@xxxxxxxxx> --- Apply on top of "libtracefs: Update filtering functions" https://lore.kernel.org/linux-trace-devel/20210407202126.1870994-1-rostedt@xxxxxxxxxxx/ v2 changes: - Added set of new APIs, instead of the previously proposed API for just opening the file. include/tracefs-local.h | 1 + include/tracefs.h | 9 +++ src/Makefile | 1 + src/tracefs-instance.c | 3 + src/tracefs-marker.c | 129 ++++++++++++++++++++++++++++++++++++++++ 5 files changed, 143 insertions(+) create mode 100644 src/tracefs-marker.c diff --git a/include/tracefs-local.h b/include/tracefs-local.h index 6865611..6657b6f 100644 --- a/include/tracefs-local.h +++ b/include/tracefs-local.h @@ -21,6 +21,7 @@ struct tracefs_instance { int flags; int ftrace_filter_fd; int ftrace_notrace_fd; + int ftrace_marker_fd; }; extern pthread_mutex_t toplevel_lock; diff --git a/include/tracefs.h b/include/tracefs.h index 70b7ebe..9adcf07 100644 --- a/include/tracefs.h +++ b/include/tracefs.h @@ -63,6 +63,15 @@ static inline int tracefs_trace_on_get_fd(struct tracefs_instance *instance) return tracefs_instance_file_open(instance, "tracing_on", O_RDWR); } +/* trace marker */ +int tracefs_marker_init(struct tracefs_instance *instance); +int tracefs_marker_print(struct tracefs_instance *instance, + const char *fmt, ...); +int tracefs_marker_vprint(struct tracefs_instance *instance, + const char *fmt, va_list ap); +int tracefs_marker_write(struct tracefs_instance *instance, void *data, int len); +void tracefs_marker_close(struct tracefs_instance *instance); + /* events */ void tracefs_list_free(char **list); char **tracefs_event_systems(const char *tracing_dir); diff --git a/src/Makefile b/src/Makefile index dabdbb4..b4cff07 100644 --- a/src/Makefile +++ b/src/Makefile @@ -7,6 +7,7 @@ OBJS += tracefs-utils.o OBJS += tracefs-instance.o OBJS += tracefs-events.o OBJS += tracefs-tools.o +OBJS += tracefs-marker.o OBJS := $(OBJS:%.o=$(bdir)/%.o) DEPS := $(OBJS:$(bdir)/%.o=$(bdir)/.%.d) diff --git a/src/tracefs-instance.c b/src/tracefs-instance.c index 599c3a7..90e35b4 100644 --- a/src/tracefs-instance.c +++ b/src/tracefs-instance.c @@ -49,6 +49,7 @@ static struct tracefs_instance *instance_alloc(const char *trace_dir, const char instance->ftrace_filter_fd = -1; instance->ftrace_notrace_fd = -1; + instance->ftrace_marker_fd = -1; return instance; @@ -74,6 +75,8 @@ void tracefs_instance_free(struct tracefs_instance *instance) free(instance->trace_dir); free(instance->name); pthread_mutex_destroy(&instance->lock); + if (instance->ftrace_marker_fd >= 0) + close(instance->ftrace_marker_fd); free(instance); } diff --git a/src/tracefs-marker.c b/src/tracefs-marker.c new file mode 100644 index 0000000..92d55f8 --- /dev/null +++ b/src/tracefs-marker.c @@ -0,0 +1,129 @@ +// SPDX-License-Identifier: LGPL-2.1 +/* + * Copyright (C) 2021, VMware, Tzvetomir Stoyanov <tz.stoyanov@xxxxxxxxx> + * + */ + +#include <stdlib.h> +#include <unistd.h> +#include <stdio.h> + +#include "tracefs.h" +#include "tracefs-local.h" + +/* File descriptor for Top level trace_marker */ +static int ftrace_marker_fd = -1; + +/** + * tracefs_marker_init - Open trace_marker file of selected instance for writing + * @instance: ftrace instance, can be NULL for top tracing instance. + * + * Returns 0 if the trace_marker is opened successfully, or -1 in case of an error + */ +int tracefs_marker_init(struct tracefs_instance *instance) +{ + int *fd = instance ? &instance->ftrace_marker_fd : &ftrace_marker_fd; + + if (*fd >= 0) + return 0; + *fd = tracefs_instance_file_open(instance, "trace_marker", O_WRONLY); + + return *fd < 0 ? -1 : 0; +} + +/** + * tracefs_marker_write - Write binary data in the trace marker + * @instance: ftrace instance, can be NULL for top tracing instance. + * @data: binary data, that is going to be written in the trace marker + * @len: length of the @data + * + * If the trace marker file of the desired instance is not open already, + * this API will open it for writing. The file will stay open until + * tracefs_marker_close() is called. + * + * Returns 0 if the data is written correctly, or -1 in case of an error + */ +int tracefs_marker_write(struct tracefs_instance *instance, void *data, int len) +{ + int *fd = instance ? &instance->ftrace_marker_fd : &ftrace_marker_fd; + int ret; + + if (!data || len < 1) + return -1; + if (*fd < 0) { + ret = tracefs_marker_init(instance); + if (ret < 0) + return ret; + } + + ret = write(*fd, data, len); + + return ret == len ? 0 : -1; +} + +/** + * tracefs_marker_vprint - Write a formatted string in the trace marker + * @instance: ftrace instance, can be NULL for top tracing instance. + * @fmt: pritnf formatted string + * @ap: list of arguments for the formatted string + * + * If the trace marker file of the desired instance is not open already, + * this API will open it for writing. The file will stay open until + * tracefs_marker_close() is called. + * + * Returns 0 if the string is written correctly, or -1 in case of an error + */ +int tracefs_marker_vprint(struct tracefs_instance *instance, + const char *fmt, va_list ap) +{ + char *str = NULL; + int ret; + + ret = vasprintf(&str, fmt, ap); + if (ret < 0) + return ret; + ret = tracefs_marker_write(instance, str, strlen(str) + 1); + free(str); + + return ret; +} + +/** + * tracefs_marker_print - Write a formatted string in the trace marker + * @instance: ftrace instance, can be NULL for top tracing instance. + * @fmt: pritnf formatted string with variable arguments ... + * + * If the trace marker file of the desired instance is not open already, + * this API will open it for writing. The file will stay open until + * tracefs_marker_close() is called. + * + * Returns 0 if the string is written correctly, or -1 in case of an error + */ +int tracefs_marker_print(struct tracefs_instance *instance, + const char *fmt, ...) +{ + va_list ap; + int ret; + + va_start(ap, fmt); + ret = tracefs_marker_vprint(instance, fmt, ap); + va_end(ap); + + return ret; +} + +/** + * tracefs_marker_close - Close trace_marker file of selected instance + * @instance: ftrace instance, can be NULL for top tracing instance. + * + * Closes the file, previously opened with any of the other tracefs_marker_ APIs + */ +void tracefs_marker_close(struct tracefs_instance *instance) +{ + int *fd = instance ? &instance->ftrace_marker_fd : &ftrace_marker_fd; + + if (*fd < 0) + return; + close(*fd); + *fd = -1; +} -- 2.30.2