linux-trace-devel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Steven Rostedt <rostedt@goodmis.org>
To: "linux-trace-devel@vger.kernel.org"  <linux-trace-devel@vger.kernel.org>
Subject: [PATCH] libtracefs: Add tracefs_load_cmdlines() API
Date: Fri, 9 Apr 2021 21:40:00 -0400	[thread overview]
Message-ID: <20210409214000.5f9dc904@oasis.local.home> (raw)

From: "Steven Rostedt (VMware)" <rostedt@goodmis.org>

While writing a program using the libtracefs routines, I had to load the
tep handle before tracing to see what events were available before
starting the trace. Loading the mappings of pids to comms is best done
after the trace as the saved_cmdlines file gets filled by the trace. Since
the loading was already done at the beginning, I needed a way to load it
again when the trace was over.

Add tracefs_load_cmdlines() API to be able to accomplish this.

Signed-off-by: Steven Rostedt (VMware) <rostedt@goodmis.org>
---
 Documentation/libtracefs-events-tep.txt | 17 +++++++++++++++--
 include/tracefs.h                       |  2 ++
 src/tracefs-events.c                    | 19 +++++++++++++------
 3 files changed, 30 insertions(+), 8 deletions(-)

diff --git a/Documentation/libtracefs-events-tep.txt b/Documentation/libtracefs-events-tep.txt
index e0be02c..4c5cb21 100644
--- a/Documentation/libtracefs-events-tep.txt
+++ b/Documentation/libtracefs-events-tep.txt
@@ -3,7 +3,8 @@ libtracefs(3)
 
 NAME
 ----
-tracefs_local_events, tracefs_local_events_system, tracefs_fill_local_events -
+tracefs_local_events, tracefs_local_events_system, tracefs_fill_local_events,
+tracefs_load_cmdlines -
 Initialize a tep handler with trace events from the local system.
 
 SYNOPSIS
@@ -15,7 +16,7 @@ SYNOPSIS
 struct tep_handle pass:[*]*tracefs_local_events*(const char pass:[*]_tracing_dir_);
 struct tep_handle pass:[*]*tracefs_local_events_system*(const char pass:[*]_tracing_dir_, const char pass:[*] const pass:[*]_sys_names_);
 int *tracefs_fill_local_events*(const char pass:[*]_tracing_dir_, struct tep_handle pass:[*]_tep_, int pass:[*]_parsing_failures_);
-
+int *tracefs_load_cmdlines*(const char pass:[*]_tracing_dir_, struct tep_handle pass:[*]_tep_);
 --
 
 DESCRIPTION
@@ -46,6 +47,14 @@ a pointer to already allocated tep handler, that is going to be initialized.
 The _parsing_failures_ argument could be NULL or a pointer to an integer,
 where the number of failures while parsing the event files are returned.
 
+The above functions will also load the mappings between pids and the process
+command line names. In some cases the _tep_ handle is created with one
+of the above before tracing begins. As the mappings get updated during the
+trace, there may be a need to read the mappings again after the trace.
+The _tracefs_load_cmdlines()_ does just that. The _tracing_dir_ is
+the director of the mount point to load from, or NULL to use the
+mount point of the tracefs file system.
+
 RETURN VALUE
 ------------
 The _tracefs_local_events()_ and _tracefs_local_events_system()_ functions
@@ -55,6 +64,9 @@ case of an error. The returned _tep_ handler must be freed with _tep_free(3)_.
 The _tracefs_fill_local_events()_ function returns -1 in case of an error or
 0 otherwise.
 
+The _tracefs_load_cmdlines()_ function returns -1 in case of an error, or
+0 otherwise.
+
 EXAMPLE
 -------
 [source,c]
@@ -91,6 +103,7 @@ struct tep_event *tep;
 	if (tracefs_fill_local_events(NULL, tep, &parsing_failures) < 0) {
 		/* Failed to initialise tep handler with local events from top instance */
 	}
+	tracefs_load_cmdlines(NULL, tep);
 ...
 	tep_free(tep);
 --
diff --git a/include/tracefs.h b/include/tracefs.h
index 2dc29b3..55ee867 100644
--- a/include/tracefs.h
+++ b/include/tracefs.h
@@ -94,6 +94,8 @@ struct tep_handle *tracefs_local_events_system(const char *tracing_dir,
 int tracefs_fill_local_events(const char *tracing_dir,
 			       struct tep_handle *tep, int *parsing_failures);
 
+int tracefs_load_cmdlines(const char *tracing_dir, struct tep_handle *tep);
+
 char *tracefs_get_clock(struct tracefs_instance *instance);
 
 enum tracefs_option_id {
diff --git a/src/tracefs-events.c b/src/tracefs-events.c
index f37ada1..94573b3 100644
--- a/src/tracefs-events.c
+++ b/src/tracefs-events.c
@@ -610,8 +610,8 @@ static void load_kallsyms(struct tep_handle *tep)
 	free(buf);
 }
 
-static void load_saved_cmdlines(const char *tracing_dir,
-				struct tep_handle *tep)
+static int load_saved_cmdlines(const char *tracing_dir,
+			       struct tep_handle *tep, bool warn)
 {
 	char *path;
 	char *buf;
@@ -619,15 +619,17 @@ static void load_saved_cmdlines(const char *tracing_dir,
 
 	path = trace_append_file(tracing_dir, "saved_cmdlines");
 	if (!path)
-		return;
+		return -1;
 
 	ret = str_read_file(path, &buf, false);
 	free(path);
 	if (ret < 0)
-		return;
+		return -1;
 
-	tep_parse_saved_cmdlines(tep, buf);
+	ret = tep_parse_saved_cmdlines(tep, buf);
 	free(buf);
+
+	return ret;
 }
 
 static void load_printk_formats(const char *tracing_dir,
@@ -659,10 +661,15 @@ static void load_mappings(const char *tracing_dir,
 			  struct tep_handle *tep)
 {
 	load_kallsyms(tep);
-	load_saved_cmdlines(tracing_dir, tep);
+	load_saved_cmdlines(tracing_dir, tep, false);
 	load_printk_formats(tracing_dir, tep);
 }
 
+int tracefs_load_cmdlines(const char *tracing_dir, struct tep_handle *tep)
+{
+	return load_saved_cmdlines(tracing_dir, tep, true);
+}
+
 static int fill_local_events_system(const char *tracing_dir,
 				    struct tep_handle *tep,
 				    const char * const *sys_names,
-- 
2.29.2


                 reply	other threads:[~2021-04-10  1:40 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=20210409214000.5f9dc904@oasis.local.home \
    --to=rostedt@goodmis.org \
    --cc=linux-trace-devel@vger.kernel.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).