diff mbox series

[v2,35/87] trace-cmd library: Initialize internal sections database on file read

Message ID 20210729050959.12263-36-tz.stoyanov@gmail.com (mailing list archive)
State Superseded
Headers show
Series Trace file version 7 | expand

Commit Message

Tzvetomir Stoyanov (VMware) July 29, 2021, 5:09 a.m. UTC
Add sections in internal database on file reading and parsing.
In trace file version 7, sections are initialized when parsing
corresponding trace options. In version 6 files, sections are retrieved
on file reading, as there they are in a fixed position file.

Signed-off-by: Tzvetomir Stoyanov (VMware) <tz.stoyanov@gmail.com>
---
 lib/trace-cmd/trace-input.c | 55 +++++++++++++++++++++++++++----------
 1 file changed, 40 insertions(+), 15 deletions(-)

Comments

Steven Rostedt Aug. 19, 2021, 5:57 p.m. UTC | #1
On Thu, 29 Jul 2021 08:09:07 +0300
"Tzvetomir Stoyanov (VMware)" <tz.stoyanov@gmail.com> wrote:

>  
> +	if (handle->file_version < 7)
> +		add_section(handle, TRACECMD_OPTION_FTRACE_EVENTS, 0, 0,
> +			    lseek64(handle->fd, 0, SEEK_CUR));
> +

I really hate these "magic number 7"s.

Perhaps we should add flags, like "SECTIONED" with a bit more
meaning.

	if (handle->flags & HANDLE_FL_SECTIONED)
		section_add(...);

-- Steve
diff mbox series

Patch

diff --git a/lib/trace-cmd/trace-input.c b/lib/trace-cmd/trace-input.c
index e8eefb5c..f5e13bbc 100644
--- a/lib/trace-cmd/trace-input.c
+++ b/lib/trace-cmd/trace-input.c
@@ -165,7 +165,6 @@  struct tracecmd_input {
 	struct file_section	*sections;
 	size_t			header_files_start;
 	size_t			ftrace_files_start;
-	size_t			event_files_start;
 	size_t			options_start;
 	size_t			total_file_size;
 
@@ -485,6 +484,10 @@  static int read_header_files(struct tracecmd_input *handle)
 	if (CHECK_READ_STATE(handle, TRACECMD_FILE_HEADERS))
 		return 0;
 
+	if (handle->file_version < 7)
+		add_section(handle, TRACECMD_OPTION_HEADER_INFO, 0, 0,
+			    lseek64(handle->fd, 0, SEEK_CUR));
+
 	if (do_read_check(handle, buf, 12))
 		return -1;
 
@@ -528,9 +531,6 @@  static int read_header_files(struct tracecmd_input *handle)
 
 	free(header);
 
-	handle->ftrace_files_start =
-		lseek64(handle->fd, 0, SEEK_CUR);
-
 	handle->file_state = TRACECMD_FILE_HEADERS;
 
 	return 0;
@@ -691,6 +691,10 @@  static int read_ftrace_files(struct tracecmd_input *handle, const char *regex)
 	if (CHECK_READ_STATE(handle, TRACECMD_FILE_FTRACE_EVENTS))
 		return 0;
 
+	if (handle->file_version < 7)
+		add_section(handle, TRACECMD_OPTION_FTRACE_EVENTS, 0, 0,
+			    lseek64(handle->fd, 0, SEEK_CUR));
+
 	if (regex) {
 		sreg = &spreg;
 		ereg = &epreg;
@@ -729,9 +733,6 @@  static int read_ftrace_files(struct tracecmd_input *handle, const char *regex)
 			return -1;
 	}
 
-	handle->event_files_start =
-		lseek64(handle->fd, 0, SEEK_CUR);
-
 	if (sreg) {
 		regfree(sreg);
 		regfree(ereg);
@@ -762,6 +763,10 @@  static int read_event_files(struct tracecmd_input *handle, const char *regex)
 	if (CHECK_READ_STATE(handle, TRACECMD_FILE_ALL_EVENTS))
 		return 0;
 
+	if (handle->file_version < 7)
+		add_section(handle, TRACECMD_OPTION_EVENT_FORMATS, 0, 0,
+			    lseek64(handle->fd, 0, SEEK_CUR));
+
 	if (regex) {
 		sreg = &spreg;
 		ereg = &epreg;
@@ -847,6 +852,11 @@  static int read_proc_kallsyms(struct tracecmd_input *handle)
 	if (CHECK_READ_STATE(handle, TRACECMD_FILE_KALLSYMS))
 		return 0;
 
+	if (handle->file_version < 7)
+		add_section(handle, TRACECMD_OPTION_KALLSYMS, 0, 0,
+			    lseek64(handle->fd, 0, SEEK_CUR));
+
+
 	if (read4(handle, &size) < 0)
 		return -1;
 	if (!size)
@@ -878,6 +888,11 @@  static int read_ftrace_printk(struct tracecmd_input *handle)
 	if (CHECK_READ_STATE(handle, TRACECMD_FILE_PRINTK))
 		return 0;
 
+	if (handle->file_version < 7)
+		add_section(handle, TRACECMD_OPTION_PRINTK, 0, 0,
+			    lseek64(handle->fd, 0, SEEK_CUR));
+
+
 	if (read4(handle, &size) < 0)
 		return -1;
 	if (!size)
@@ -2909,6 +2924,16 @@  static int handle_options(struct tracecmd_input *handle)
 			handle->tsc_calc.offset = tep_read_number(handle->pevent,
 								  buf + 8, 8);
 			break;
+		case TRACECMD_OPTION_HEADER_INFO:
+		case TRACECMD_OPTION_FTRACE_EVENTS:
+		case TRACECMD_OPTION_EVENT_FORMATS:
+		case TRACECMD_OPTION_KALLSYMS:
+		case TRACECMD_OPTION_PRINTK:
+		case TRACECMD_OPTION_CMDLINES:
+			if (size < 8)
+				break;
+			add_section(handle, option, -1, tep_read_number(handle->pevent, buf, 8), 0);
+			break;
 		default:
 			tracecmd_warning("unknown option %d", option);
 			break;
@@ -3095,6 +3120,10 @@  static int read_and_parse_cmdlines(struct tracecmd_input *handle)
 	if (CHECK_READ_STATE(handle, TRACECMD_FILE_CMD_LINES))
 		return 0;
 
+	if (handle->file_version < 7)
+		add_section(handle, TRACECMD_OPTION_CMDLINES, 0, 0,
+			    lseek64(handle->fd, 0, SEEK_CUR));
+
 	if (read_data_and_size(handle, &cmdlines, &size) < 0)
 		return -1;
 	cmdlines[size] = 0;
@@ -3384,6 +3413,7 @@  struct tracecmd_input *tracecmd_alloc_fd(int fd, int flags)
 	struct tracecmd_input *handle;
 	char test[] = TRACECMD_MAGIC;
 	unsigned int page_size;
+	size_t offset;
 	char *version;
 	char buf[BUFSIZ];
 	unsigned long ver;
@@ -3444,14 +3474,9 @@  struct tracecmd_input *tracecmd_alloc_fd(int fd, int flags)
 	read4(handle, &page_size);
 	handle->page_size = page_size;
 
-	handle->header_files_start =
-		lseek64(handle->fd, 0, SEEK_CUR);
-
-	handle->total_file_size =
-		lseek64(handle->fd, 0, SEEK_END);
-
-	handle->header_files_start =
-		lseek64(handle->fd, handle->header_files_start, SEEK_SET);
+	offset = lseek64(handle->fd, 0, SEEK_CUR);
+	handle->total_file_size = lseek64(handle->fd, 0, SEEK_END);
+	lseek64(handle->fd, offset, SEEK_SET);
 
 	handle->file_state = TRACECMD_FILE_INIT;