@@ -20,9 +20,12 @@ struct tracefs_instance;
void tracefs_instance_free(struct tracefs_instance *instance);
struct tracefs_instance *tracefs_instance_create(const char *name);
+struct tracefs_instance *tracefs_instance_alloc(const char *tracing_dir,
+ const char *name);
int tracefs_instance_destroy(struct tracefs_instance *instance);
bool tracefs_instance_is_new(struct tracefs_instance *instance);
const char *tracefs_instance_get_name(struct tracefs_instance *instance);
+const char *tracefs_instance_get_trace_dir(struct tracefs_instance *instance);
char *
tracefs_instance_get_file(struct tracefs_instance *instance, const char *file);
char *tracefs_instance_get_dir(struct tracefs_instance *instance);
@@ -151,6 +151,49 @@ error:
return NULL;
}
+/**
+ * tracefs_instance_alloc - Allocate an instance structure for existing trace instance
+ * @tracing_dir: full path to the system trace directory, where the new instance is
+ * if NULL, the default top tracing directory is used.
+ * @name: Name of the instance.
+ *
+ * Allocates and initializes a new instance structure. If the instance does not
+ * exist, do not create it and exit with error.
+ * Returns a pointer to a newly allocated instance, or NULL in case of an error
+ * or the requested instance does not exists.
+ * The returned instance must be freed by tracefs_instance_free().
+ */
+struct tracefs_instance *tracefs_instance_alloc(const char *tracing_dir,
+ const char *name)
+{
+ struct tracefs_instance *inst = NULL;
+ char file[PATH_MAX];
+ const char *tdir;
+ struct stat st;
+ int ret;
+
+ if (tracing_dir) {
+ ret = stat(tracing_dir, &st);
+ if (ret < 0 || !S_ISDIR(st.st_mode))
+ return NULL;
+ tdir = tracing_dir;
+
+ } else
+ tdir = tracefs_tracing_dir();
+ if (!tdir)
+ return NULL;
+
+ if (name) {
+ sprintf(file, "%s/instances/%s", tdir, name);
+ ret = stat(file, &st);
+ if (ret < 0 || !S_ISDIR(st.st_mode))
+ return NULL;
+ }
+ inst = instance_alloc(tdir, name);
+
+ return inst;
+}
+
/**
* tracefs_instance_destroy - Remove a ftrace instance
* @instance: Pointer to the instance to be removed
@@ -247,6 +290,20 @@ const char *tracefs_instance_get_name(struct tracefs_instance *instance)
return NULL;
}
+/**
+ * tracefs_instance_get_trace_dir - return the top trace directory, where the instance is confuigred
+ * @instance: ftrace instance
+ *
+ * Returns the top trace directory where the given @instance is configured.
+ * The returned string must *not* be freed.
+ */
+const char *tracefs_instance_get_trace_dir(struct tracefs_instance *instance)
+{
+ if (instance)
+ return instance->trace_dir;
+ return NULL;
+}
+
static int write_file(const char *file, const char *str)
{
int ret;
The new API allocates an instance structure for existing trace instance. It has an optional tracing_dir parameter, which allows to get instances from non default trace directory. If the instance with the given name does not exist, the API fails. If no name is given, the structure for the top instance in the given trace directory is allocated and returned. struct tracefs_instance *tracefs_instance_alloc(const char *tracing_dir, const char *name); This helper API is added also, to get the tracing directory where the instance is configured. In most cases this should be the default system trace directroy mount point. const char *tracefs_instance_get_trace_dir(struct tracefs_instance *instance); Signed-off-by: Tzvetomir Stoyanov (VMware) <tz.stoyanov@gmail.com> --- include/tracefs.h | 3 +++ src/tracefs-instance.c | 57 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+)