diff mbox series

[trace-cmd,v2,2/3] trace-cmd-report: Support global filters

Message ID 20230330185211.5604-3-krisman@suse.de (mailing list archive)
State Accepted
Commit 7a390c56493dd7199982112e588872a247fb9808
Headers show
Series trace-cmd filtering | expand

Commit Message

Gabriel Krisman Bertazi March 30, 2023, 6:52 p.m. UTC
When a filter is provided before an input file, consider it globally
applying to all traces:

i.e., the following will trace kernel_stack out of both traces instead
of only the first:

  $ trace-cmd report -v -F kernel_stack -i trace1.dat -i trace2.dat

Filter order matters due to the order of processing. Always process
global filters first, which are likely more generic.

Signed-off-by: Gabriel Krisman Bertazi <krisman@suse.de>
---
 tracecmd/trace-read.c | 42 ++++++++++++++++++++++++++----------------
 1 file changed, 26 insertions(+), 16 deletions(-)
diff mbox series

Patch

diff --git a/tracecmd/trace-read.c b/tracecmd/trace-read.c
index 6a872d7..f645c05 100644
--- a/tracecmd/trace-read.c
+++ b/tracecmd/trace-read.c
@@ -562,30 +562,40 @@  static void make_pid_filter(struct tracecmd_input *handle,
 	}
 }
 
+static int __process_filters(struct tracecmd_input *handle,
+			     struct filter_str *filters)
+{
+	struct tracecmd_filter *trace_filter;
+
+	for (; filters; filters = filters->next) {
+		trace_filter = tracecmd_filter_add(handle,
+						   filters->filter,
+						   filters->neg);
+		if (!trace_filter)
+			die("Failed to create event filter: %s",
+			    filters->filter);
+	}
+
+	return !!filters;
+}
+
 static void process_filters(struct handle_list *handles)
 {
 	struct input_files *input_file = handles->input_file ?: last_input_file;
-	struct tracecmd_filter *trace_filter;
-	struct filter_str *filter;
-	int filters = 0;
+	int added = 0;
 
 	make_pid_filter(handles->handle, input_file);
 
+	/*
+	 * Order of filter processing matters. Apply the global filters
+	 * before file-specific ones.
+	 */
+	added += __process_filters(handles->handle, filter_strings);
 	if (input_file)
-		filter = input_file->filter_str;
-	else
-		filter = filter_strings;
-
-	for (; filter; filter = filter->next) {
-		trace_filter = tracecmd_filter_add(handles->handle,
-						   filter->filter,
-						   filter->neg);
-		if (!trace_filter)
-			die("Failed to create event filter: %s", filter->filter);
+		added += __process_filters(handles->handle,
+					   input_file->filter_str);
 
-		filters++;
-	}
-	if (filters && test_filters_mode)
+	if (added && test_filters_mode)
 		exit(0);
 }