Skip to contentSkip to conversation

trace-cmd fixes and clean-ups

PATCH Superseded Tzvetomir Stoyanov (VMware) 1 participant 7 messages Oct 8, 2021

v4 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

v4 cover trace-cmd fixes and clean-ups

Various trace-cmd application and library fixes, clean-ups and internal refactoring, needed for trace file version 7 and compression changes.

v4 changes:
- Removed from the set the patches that were already merged upstream.
- Rebased on top of the latest master.
- Removed these 2 patches from the set, as they will be a separate patch sets:
"trace-cmd library: Refactor APIs for creating output handler" "trace-cmd library: Refactor the logic for writing trace data in the file"
- Addressed Steven's comments.
v3 changes:
- Fixed issues of split and convert commands with some corner cases.
v2 changes:
- More cleanups, forgotten in the first version.

Tzvetomir Stoyanov (VMware) (6):
trace-cmd library: Do not use local variables when reading CPU stat
option
trace-cmd library: Track maximum CPUs count in input handler trace-cmd library: Fix possible memory leak in read_event_files() trace-cmd library: Fix possible memory leak in read_ftrace_files() trace-cmd library: Set the correct file state when reading file with
no kallsyms
trace-cmd library: Set the correct file state when reading file with
no ftrace printk data

 lib/trace-cmd/trace-input.c | 92 ++++++++++++++++++++-----------------
 1 file changed, 51 insertions(+), 41 deletions(-)
Signature · 1 line
2.31.1
#1 lore raw

PATCH v4 1/6 trace-cmd library: Do not use local variables when reading CPU stat option

Using a local variable to read all CPUSTAT options assumes that all of them are in a single option section. While this is true for the current trace file version format, this assumption limits the design of a more flexible format with multiple options sections. Use input handler context instead of the local variable.

---
 lib/trace-cmd/trace-input.c | 14 +++++++-------
 1 file changed, 7 insertions(+), 7 deletions(-)
lib/trace-cmd/trace-input.c +7 -7
index ec4c6e55..b4af783b 100644
@@ -132,6 +132,7 @@ struct tracecmd_input {
 	bool			read_page;
 	bool			use_pipe;
 	int			file_version;
+	unsigned int		cpustats_size;
 	struct cpu_data 	*cpu_data;
 	long long		ts_offset;
 	struct tsc2nsec		tsc_calc;
@@ -2658,7 +2659,6 @@ static int handle_options(struct tracecmd_input *handle)
 	unsigned short option;
 	unsigned int size;
 	char *cpustats = NULL;
-	unsigned int cpustats_size = 0;
 	struct input_buffer_instance *buffer;
 	struct hook_list *hook;
 	char *buf;
@@ -2736,12 +2736,14 @@ static int handle_options(struct tracecmd_input *handle)
 			break;
 		case TRACECMD_OPTION_CPUSTAT:
 			buf[size-1] = '\n';
-			cpustats = realloc(cpustats, cpustats_size + size + 1);
+			cpustats = realloc(handle->cpustats,
+					   handle->cpustats_size + size + 1);
 			if (!cpustats)
 				return -ENOMEM;
-			memcpy(cpustats + cpustats_size, buf, size);
-			cpustats_size += size;
-			cpustats[cpustats_size] = 0;
+			memcpy(cpustats + handle->cpustats_size, buf, size);
+			handle->cpustats_size += size;
+			cpustats[handle->cpustats_size] = 0;
+			handle->cpustats = cpustats;
 			break;
 		case TRACECMD_OPTION_BUFFER:
 			/* A buffer instance is saved at the end of the file */
@@ -2811,8 +2813,6 @@ static int handle_options(struct tracecmd_input *handle)
 
 	}
 
-	handle->cpustats = cpustats;
-
 	return 0;
 }
 
Signature · 1 line
2.31.1
#2 lore raw

PATCH v4 2/6 trace-cmd library: Track maximum CPUs count in input handler

In reply to Tzvetomir Stoyanov (VMware)

This clean up is needed for the design of the next trace file version, where only CPUs with trace data could be stored in the file. Each trace instance may have its own CPU count, depending on collected traces. As the main input handler is used by the top trace instance, the CPU count there is for the top trace instance and may differ with cpu counts of the other instances. Added a new "max_cpu" member of the input handler, that tracks the maximum CPU count of all instances, recorded in the file.

---
 lib/trace-cmd/trace-input.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
lib/trace-cmd/trace-input.c +6 -1
index b4af783b..aaafe3a4 100644
@@ -125,6 +125,7 @@ struct tracecmd_input {
 	int			long_size;
 	int			page_size;
 	int			page_map_size;
+	int			max_cpu;
 	int			cpus;
 	int			ref;
 	int			nr_buffers;	/* buffer instances */
@@ -826,6 +827,7 @@ static int read_cpus(struct tracecmd_input *handle)
 		return -1;
 
 	handle->cpus = cpus;
+	handle->max_cpu = cpus;
 	tep_set_cpus(handle->pevent, handle->cpus);
 	handle->file_state = TRACECMD_FILE_CPU_COUNT;
 
@@ -2780,6 +2782,9 @@ static int handle_options(struct tracecmd_input *handle)
 		case TRACECMD_OPTION_CPUCOUNT:
 			cpus = *(int *)buf;
 			handle->cpus = tep_read_number(handle->pevent, &cpus, 4);
+			if (handle->cpus > handle->max_cpu)
+				handle->max_cpu = handle->cpus;
+			tep_set_cpus(handle->pevent, handle->cpus);
 			break;
 		case TRACECMD_OPTION_PROCMAPS:
 			if (buf[size-1] == '\0')
@@ -4054,7 +4059,7 @@ int tracecmd_page_size(struct tracecmd_input *handle)
  */
 int tracecmd_cpus(struct tracecmd_input *handle)
 {
-	return handle->cpus;
+	return handle->max_cpu;
 }
 
 /**
Signature · 1 line
2.31.1
#3 lore raw

PATCH v4 3/6 trace-cmd library: Fix possible memory leak in read_event_files()

In reply to Tzvetomir Stoyanov (VMware)

Some error paths in read_event_files() may lead to a memory leak. Improved the error handling of this internal function to avoid it.

---
 lib/trace-cmd/trace-input.c | 39 ++++++++++++++++++-------------------
 1 file changed, 19 insertions(+), 20 deletions(-)
lib/trace-cmd/trace-input.c +19 -20
index aaafe3a4..0a42ca4e 100644
@@ -644,7 +644,7 @@ static int read_ftrace_files(struct tracecmd_input *handle, const char *regex)
 static int read_event_files(struct tracecmd_input *handle, const char *regex)
 {
 	unsigned long long size;
-	char *system;
+	char *system = NULL;
 	regex_t spreg;
 	regex_t epreg;
 	regex_t *sreg = NULL;
@@ -669,13 +669,16 @@ static int read_event_files(struct tracecmd_input *handle, const char *regex)
 			return -1;
 	}
 
-	if (read4(handle, &systems) < 0)
-		return -1;
+	ret = read4(handle, &systems);
+	if (ret < 0)
+		goto out;
 
 	for (i = 0; i < systems; i++) {
 		system = read_string(handle);
-		if (!system)
-			return -1;
+		if (!system) {
+			ret = -1;
+			goto out;
+		}
 
 		sys_printed = 0;
 		print_all = 0;
@@ -702,39 +705,35 @@ static int read_event_files(struct tracecmd_input *handle, const char *regex)
 			}
 		}
 
-		if (read4(handle, &count) < 0)
-			goto failed;
+		ret = read4(handle, &count);
+		if (ret < 0)
+			goto out;
 
 		for (x=0; x < count; x++) {
-			if (read8(handle, &size) < 0)
-				goto failed;
+			ret = read8(handle, &size);
+			if (ret < 0)
+				goto out;
 
 			ret = read_event_file(handle, system, size,
 					      print_all, &sys_printed,
 					      reg);
 			if (ret < 0)
-				goto failed;
+				goto out;
 		}
 		free(system);
 	}
-
-	if (sreg) {
-		regfree(sreg);
-		regfree(ereg);
-	}
+	system = NULL;
 
 	handle->file_state = TRACECMD_FILE_ALL_EVENTS;
-
-	return 0;
-
- failed:
+	ret = 0;
+ out:
 	if (sreg) {
 		regfree(sreg);
 		regfree(ereg);
 	}
 
 	free(system);
-	return -1;
+	return ret;
 }
 
 static int read_proc_kallsyms(struct tracecmd_input *handle)
Signature · 1 line
2.31.1
#4 lore raw

PATCH v4 4/6 trace-cmd library: Fix possible memory leak in read_ftrace_files()

In reply to Tzvetomir Stoyanov (VMware)

Some error paths in read_ftrace_files() may lead to a memory leak. Improved the error handling of this internal function to avoid it.

---
 lib/trace-cmd/trace-input.c | 20 +++++++++++---------
 1 file changed, 11 insertions(+), 9 deletions(-)
lib/trace-cmd/trace-input.c +11 -9
index 0a42ca4e..43491c5b 100644
@@ -617,28 +617,30 @@ static int read_ftrace_files(struct tracecmd_input *handle, const char *regex)
 		}
 	}
 
-	if (read4(handle, &count) < 0)
-		return -1;
+	ret = read4(handle, &count);
+	if (ret < 0)
+		goto out;
 
 	for (i = 0; i < count; i++) {
-		if (read8(handle, &size) < 0)
-			return -1;
+		ret = read8(handle, &size);
+		if (ret < 0)
+			goto out;
 		ret = read_ftrace_file(handle, size, print_all, ereg);
 		if (ret < 0)
-			return -1;
+			goto out;
 	}
 
 	handle->event_files_start =
 		lseek64(handle->fd, 0, SEEK_CUR);
-
+	handle->file_state = TRACECMD_FILE_FTRACE_EVENTS;
+	ret = 0;
+out:
 	if (sreg) {
 		regfree(sreg);
 		regfree(ereg);
 	}
 
-	handle->file_state = TRACECMD_FILE_FTRACE_EVENTS;
-
-	return 0;
+	return ret;
 }
 
 static int read_event_files(struct tracecmd_input *handle, const char *regex)
Signature · 1 line
2.31.1
#5 lore raw

PATCH v4 5/6 trace-cmd library: Set the correct file state when reading file with no kallsyms

In reply to Tzvetomir Stoyanov (VMware)

The kallsyms section of the trace file can contain no data, this is valid use case. When such file is read and parsed by read_proc_kallsyms(), the file state should be set to TRACECMD_FILE_KALLSYMS in that case.

Renamed a local variable from pevent to tep, to be consistent with the libtraceevent prefix.

---
 lib/trace-cmd/trace-input.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
lib/trace-cmd/trace-input.c +5 -3
index 43491c5b..f3677ad3 100644
@@ -740,7 +740,7 @@ static int read_event_files(struct tracecmd_input *handle, const char *regex)
 
 static int read_proc_kallsyms(struct tracecmd_input *handle)
 {
-	struct tep_handle *pevent = handle->pevent;
+	struct tep_handle *tep = handle->pevent;
 	unsigned int size;
 	char *buf;
 
@@ -749,8 +749,10 @@ static int read_proc_kallsyms(struct tracecmd_input *handle)
 
 	if (read4(handle, &size) < 0)
 		return -1;
-	if (!size)
+	if (!size) {
+		handle->file_state = TRACECMD_FILE_KALLSYMS;
 		return 0; /* OK? */
+	}
 
 	buf = malloc(size+1);
 	if (!buf)
@@ -761,7 +763,7 @@ static int read_proc_kallsyms(struct tracecmd_input *handle)
 	}
 	buf[size] = 0;
 
-	tep_parse_kallsyms(pevent, buf);
+	tep_parse_kallsyms(tep, buf);
 
 	free(buf);
 
Signature · 1 line
2.31.1
#6 lore raw

PATCH v4 6/6 trace-cmd library: Set the correct file state when reading file with no ftrace printk data

In reply to Tzvetomir Stoyanov (VMware)

The ftrace printk section of the trace file can contain no data, this is valid use case. When such file is read and parsed by read_ftrace_printk, the file state should be set to TRACECMD_FILE_PRINTK in that case.

---
 lib/trace-cmd/trace-input.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
lib/trace-cmd/trace-input.c +3 -1
index f3677ad3..38c7a3b8 100644
@@ -782,8 +782,10 @@ static int read_ftrace_printk(struct tracecmd_input *handle)
 
 	if (read4(handle, &size) < 0)
 		return -1;
-	if (!size)
+	if (!size) {
+		handle->file_state = TRACECMD_FILE_PRINTK;
 		return 0; /* OK? */
+	}
 
 	buf = malloc(size + 1);
 	if (!buf)
Signature · 1 line
2.31.1