linux-trace-devel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "Tzvetomir Stoyanov (VMware)" <tz.stoyanov@gmail.com>
To: rostedt@goodmis.org
Cc: linux-trace-devel@vger.kernel.org
Subject: [PATCH 4/5] libtracefs: New APIs for getting existing trace instance
Date: Fri, 15 Jan 2021 07:04:09 +0200	[thread overview]
Message-ID: <20210115050410.1194011-5-tz.stoyanov@gmail.com> (raw)
In-Reply-To: <20210115050410.1194011-1-tz.stoyanov@gmail.com>

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_get(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(+)

diff --git a/include/tracefs.h b/include/tracefs.h
index 1f10a00..20aa995 100644
--- a/include/tracefs.h
+++ b/include/tracefs.h
@@ -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_get(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);
diff --git a/src/tracefs-instance.c b/src/tracefs-instance.c
index 468da1b..112e237 100644
--- a/src/tracefs-instance.c
+++ b/src/tracefs-instance.c
@@ -151,6 +151,49 @@ error:
 	return NULL;
 }
 
+/**
+ * tracefs_instance_get - 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_get(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;
-- 
2.29.2


  parent reply	other threads:[~2021-01-15  5:05 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-01-15  5:04 [PATCH 0/5] New libtracefs APIs for trace options and trace dir Tzvetomir Stoyanov (VMware)
2021-01-15  5:04 ` [PATCH 1/5] libtracefs: New APIs for trace options Tzvetomir Stoyanov (VMware)
2021-01-19 21:12   ` Steven Rostedt
2021-01-15  5:04 ` [PATCH 2/5] libtracefs: Unit tests for tracing options APIs Tzvetomir Stoyanov (VMware)
2021-01-15  5:04 ` [PATCH 3/5] libtracefs: Add information about top tracing directory in instance structure Tzvetomir Stoyanov (VMware)
2021-01-15  5:04 ` Tzvetomir Stoyanov (VMware) [this message]
2021-01-19 22:18   ` [PATCH 4/5] libtracefs: New APIs for getting existing trace instance Steven Rostedt
2021-01-15  5:04 ` [PATCH 5/5] libtracefs: Unit tests for working with non default tracing dir Tzvetomir Stoyanov (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=20210115050410.1194011-5-tz.stoyanov@gmail.com \
    --to=tz.stoyanov@gmail.com \
    --cc=linux-trace-devel@vger.kernel.org \
    --cc=rostedt@goodmis.org \
    /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).