All of lore.kernel.org
 help / color / mirror / Atom feed
From: Steven Rostedt <rostedt@goodmis.org>
To: linux-trace-devel@vger.kernel.org
Subject: [PATCH 3/9 v2] trace-cmd: Move the output state updates into the functions that change the state
Date: Fri, 05 Mar 2021 17:52:26 -0500	[thread overview]
Message-ID: <20210305225947.694840219@goodmis.org> (raw)
In-Reply-To: 20210305225223.794554327@goodmis.org

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

It makes more sense to have the functions that change the state of the
descriptor to change the value that stores the state. This makes it more
robust in case these functions are called by something other than
create_file_fd(). That way the state changes with the update, and this
removes the dependency on create_file_fd with the state changes.

Link: https://lore.kernel.org/linux-trace-devel/20210301143857.244340263@goodmis.org

Signed-off-by: Steven Rostedt (VMware) <rostedt@goodmis.org>
---
 lib/trace-cmd/trace-output.c | 97 ++++++++++++++++++++++++------------
 1 file changed, 65 insertions(+), 32 deletions(-)

diff --git a/lib/trace-cmd/trace-output.c b/lib/trace-cmd/trace-output.c
index b05e0032a92c..3e59bf7fdf5d 100644
--- a/lib/trace-cmd/trace-output.c
+++ b/lib/trace-cmd/trace-output.c
@@ -297,6 +297,33 @@ int tracecmd_ftrace_enable(int set)
 	return ret;
 }
 
+static int check_out_state(struct tracecmd_output *handle, int new_state)
+{
+	if (!handle)
+		return -1;
+
+	switch (new_state) {
+	case TRACECMD_FILE_HEADERS:
+	case TRACECMD_FILE_FTRACE_EVENTS:
+	case TRACECMD_FILE_ALL_EVENTS:
+	case TRACECMD_FILE_KALLSYMS:
+	case TRACECMD_FILE_PRINTK:
+	case TRACECMD_FILE_CMD_LINES:
+	case TRACECMD_FILE_CPU_COUNT:
+	case TRACECMD_FILE_OPTIONS:
+		if (handle->file_state == (new_state - 1))
+			return 0;
+		break;
+	case TRACECMD_FILE_CPU_LATENCY:
+	case TRACECMD_FILE_CPU_FLYRECORD:
+		if (handle->file_state == TRACECMD_FILE_OPTIONS)
+			return 0;
+		break;
+	}
+
+	return -1;
+}
+
 static int read_header_files(struct tracecmd_output *handle)
 {
 	tsize_t size, check_size, endian8;
@@ -305,6 +332,12 @@ static int read_header_files(struct tracecmd_output *handle)
 	int fd;
 	int ret;
 
+	if (check_out_state(handle, TRACECMD_FILE_HEADERS) < 0) {
+		warning("Cannot read header files, unexpected state 0x%X",
+			handle->file_state);
+		return -1;
+	}
+
 	path = get_tracing_file(handle, "events/header_page");
 	if (!path)
 		return -1;
@@ -373,6 +406,9 @@ static int read_header_files(struct tracecmd_output *handle)
 		return -1;
 	}
 	put_tracing_file(path);
+
+	handle->file_state = TRACECMD_FILE_HEADERS;
+
 	return 0;
 
  out_close:
@@ -609,12 +645,20 @@ static int read_ftrace_files(struct tracecmd_output *handle)
 	struct tracecmd_event_list list = { .glob = "ftrace/*" };
 	int ret;
 
+	if (check_out_state(handle, TRACECMD_FILE_FTRACE_EVENTS) < 0) {
+		warning("Cannot read ftrace files, unexpected state 0x%X",
+			handle->file_state);
+		return -1;
+	}
+
 	create_event_list_item(handle, &systems, &list);
 
 	ret = copy_event_system(handle, systems);
 
 	free_list_events(systems);
 
+	handle->file_state = TRACECMD_FILE_FTRACE_EVENTS;
+
 	return ret;
 }
 
@@ -642,6 +686,11 @@ static int read_event_files(struct tracecmd_output *handle,
 	int endian4;
 	int ret;
 
+	if (check_out_state(handle, TRACECMD_FILE_ALL_EVENTS) < 0) {
+		warning("Cannot read event files, unexpected state 0x%X",
+			handle->file_state);
+		return -1;
+	}
 	/*
 	 * If any of the list is the special keyword "all" then
 	 * just do all files.
@@ -674,6 +723,7 @@ static int read_event_files(struct tracecmd_output *handle,
 		ret = copy_event_system(handle, slist);
 	}
 
+	handle->file_state = TRACECMD_FILE_ALL_EVENTS;
  out_free:
 	free_list_events(systems);
 
@@ -728,6 +778,12 @@ static int read_proc_kallsyms(struct tracecmd_output *handle,
 	struct stat st;
 	int ret;
 
+	if (check_out_state(handle, TRACECMD_FILE_KALLSYMS) < 0) {
+		warning("Cannot read kallsyms, unexpected state 0x%X",
+			handle->file_state);
+		return -1;
+	}
+
 	if (kallsyms)
 		path = kallsyms;
 
@@ -755,6 +811,8 @@ static int read_proc_kallsyms(struct tracecmd_output *handle,
 	}
 	set_proc_kptr_restrict(1);
 
+	handle->file_state = TRACECMD_FILE_KALLSYMS;
+
 	return 0;
 }
 
@@ -765,6 +823,12 @@ static int read_ftrace_printk(struct tracecmd_output *handle)
 	char *path;
 	int ret;
 
+	if (check_out_state(handle, TRACECMD_FILE_PRINTK) < 0) {
+		warning("Cannot read printk, unexpected state 0x%X",
+			handle->file_state);
+		return -1;
+	}
+
 	path = get_tracing_file(handle, "printk_formats");
 	if (!path)
 		return -1;
@@ -790,6 +854,7 @@ static int read_ftrace_printk(struct tracecmd_output *handle)
 	}
 
  out:
+	handle->file_state = TRACECMD_FILE_PRINTK;
 	put_tracing_file(path);
 	return 0;
  fail:
@@ -836,33 +901,6 @@ out_free:
 	return ret;
 }
 
-static int check_out_state(struct tracecmd_output *handle, int new_state)
-{
-	if (!handle)
-		return -1;
-
-	switch (new_state) {
-	case TRACECMD_FILE_HEADERS:
-	case TRACECMD_FILE_FTRACE_EVENTS:
-	case TRACECMD_FILE_ALL_EVENTS:
-	case TRACECMD_FILE_KALLSYMS:
-	case TRACECMD_FILE_PRINTK:
-	case TRACECMD_FILE_CMD_LINES:
-	case TRACECMD_FILE_CPU_COUNT:
-	case TRACECMD_FILE_OPTIONS:
-		if (handle->file_state == (new_state - 1))
-			return 0;
-		break;
-	case TRACECMD_FILE_CPU_LATENCY:
-	case TRACECMD_FILE_CPU_FLYRECORD:
-		if (handle->file_state == TRACECMD_FILE_OPTIONS)
-			return 0;
-		break;
-	}
-
-	return -1;
-}
-
 static struct tracecmd_output *
 create_file_fd(int fd, struct tracecmd_input *ihandle,
 	       const char *tracing_dir,
@@ -939,23 +977,18 @@ create_file_fd(int fd, struct tracecmd_input *ihandle,
 
 	if (read_header_files(handle))
 		goto out_free;
-	handle->file_state = TRACECMD_FILE_HEADERS;
 
 	if (read_ftrace_files(handle))
 		goto out_free;
-	handle->file_state = TRACECMD_FILE_FTRACE_EVENTS;
 
 	if (read_event_files(handle, list))
 		goto out_free;
-	handle->file_state = TRACECMD_FILE_ALL_EVENTS;
 
 	if (read_proc_kallsyms(handle, kallsyms))
 		goto out_free;
-	handle->file_state = TRACECMD_FILE_KALLSYMS;
 
 	if (read_ftrace_printk(handle))
 		goto out_free;
-	handle->file_state = TRACECMD_FILE_PRINTK;
 
 	return handle;
 
-- 
2.30.0



  parent reply	other threads:[~2021-03-05 23:00 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-03-05 22:52 [PATCH 0/9 v2] trace-cmd: Fixes for trace-cmd restore Steven Rostedt
2021-03-05 22:52 ` [PATCH 1/9 v2] trace-cmd restore: Fix to add saved cmdlines after calling tracecmd_create_init_file_override() Steven Rostedt
2021-03-05 22:52 ` [PATCH 2/9 v2] trace-cmd: Move tracecmd_write_cmdlines() out of tracecmd_append_cpu_data() Steven Rostedt
2021-03-05 22:52 ` Steven Rostedt [this message]
2021-03-05 22:52 ` [PATCH 4/9 v2] trace-cmd: Move the input state updates into the functions that change the state Steven Rostedt
2021-03-05 22:52 ` [PATCH 5/9 v2] trace-cmd input: Validate the input handle when copying from it Steven Rostedt
2021-03-05 22:52 ` [PATCH 6/9 v2] trace-cmd: Have tracecmd_read_headers() specify the state to read up to Steven Rostedt
2021-03-05 22:52 ` [PATCH 7/9 v2] trace-cmd: Have tracecmd_get_file_state() return the enum Steven Rostedt
2021-03-05 22:52 ` [PATCH 8/9 v2] trace-cmd input: Add validation updates to the copy of a handle Steven Rostedt
2021-03-05 22:52 ` [PATCH 9/9 v2] trace-cmd: Have tracecmd_copy_headers() have a start and stop state Steven Rostedt

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=20210305225947.694840219@goodmis.org \
    --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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.