Skip to contentSkip to conversation

libtracecmd: Fix continuing in tracecmd_iterate_events_reverse()

PATCH Superseded Steven Rostedt 2 participants 3 messages Nov 21, 2025 – Nov 24, 2025

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 libtracecmd: Fix continuing in tracecmd_iterate_events_reverse()

From: "Steven Rostedt (Google)" <[email protected]>

When a callback from tracecmd_iterate_events_reverse() returns non-zero it cause the function to exit out early. Calling tracecmd_iterate_events_reverse() again with cont=true, is supposed to restart where it left off. But because of the way the reverse traverses all the events on a page, by calling tracecmd_read_data() to find the next task, it does not pick the next task. It can pick the next task at the start of the page.

If the callback back causes the function to exit out early, record the page_offset of the last record read, and then set the cursor back to that event.

Fixes 56cbc522518a5 ("trace-cmd library: Add tracecmd_iterate_events_reverse() API")
---
 lib/trace-cmd/trace-input.c | 14 ++++++++++++++
 1 file changed, 14 insertions(+)
lib/trace-cmd/trace-input.c +14 -0
index afdbd2aa98b6..8dfeff458485 100644
@@ -3036,6 +3036,7 @@ int tracecmd_iterate_events_reverse(struct tracecmd_input *handle,
 				    void *callback_data, bool cont)
 {
 	unsigned long long last_timestamp = 0;
+	unsigned long long page_offset = 0;
 	struct tep_record **records;
 	struct tep_record *record;
 	int next_cpu;
@@ -3072,6 +3073,8 @@ int tracecmd_iterate_events_reverse(struct tracecmd_input *handle,
 			record = next_last_event(handle, records, next_cpu);;
 			ret = call_callbacks(handle, record, next_cpu,
 					     callback, callback_data);
+			if (ret)
+				page_offset = record->offset;
 			tracecmd_free_record(record);
 		}
 	} while (next_cpu >= 0 && ret == 0);
@@ -3079,6 +3082,17 @@ int tracecmd_iterate_events_reverse(struct tracecmd_input *handle,
 	free_last_events(handle, records, cpus, cpu_size, max_cpus);
 	free(records);
 
+	/*
+	 * If the callback exited out early, then set the cursor back
+	 * to the location of that record so that if this gets called
+	 * again with cont = true, it will continue where it left off.
+	 */
+	if (page_offset) {
+		/* Set the record to the previous record that was read */
+		record = tracecmd_read_at(handle, page_offset - 4, NULL);
+		tracecmd_free_record(record);
+	}
+
 	return ret;
 }
 
Signature · 1 line
2.51.0
#1 lore raw
Quoted 44 lines · Steven Rostedt

From: "Steven Rostedt (Google)" <[email protected]>

When a callback from tracecmd_iterate_events_reverse() returns non-zero it cause the function to exit out early. Calling tracecmd_iterate_events_reverse() again with cont=true, is supposed to restart where it left off. But because of the way the reverse traverses all the events on a page, by calling tracecmd_read_data() to find the next task, it does not pick the next task. It can pick the next task at the start of the page.

If the callback back causes the function to exit out early, record the page_offset of the last record read, and then set the cursor back to that event.

Fixes 56cbc522518a5 ("trace-cmd library: Add tracecmd_iterate_events_reverse() API")
---
 lib/trace-cmd/trace-input.c | 14 ++++++++++++++
 1 file changed, 14 insertions(+)
lib/trace-cmd/trace-input.c +3 -0
index afdbd2aa98b6..8dfeff458485 100644
@@ -3036,6 +3036,7 @@ int tracecmd_iterate_events_reverse(struct tracecmd_input *handle,
 				    void *callback_data, bool cont)
 {
 	unsigned long long last_timestamp = 0;
+	unsigned long long page_offset = 0;
 	struct tep_record **records;
 	struct tep_record *record;
 	int next_cpu;
@@ -3072,6 +3073,8 @@ int tracecmd_iterate_events_reverse(struct tracecmd_input *handle,
 			record = next_last_event(handle, records, next_cpu);;
 			ret = call_callbacks(handle, record, next_cpu,
 					     callback, callback_data);
+			if (ret)
+				page_offset = record->offset;
 			tracecmd_free_record(record);
 		}
 	} while (next_cpu >= 0 && ret == 0);
@@ -3079,6 +3082,17 @@ int tracecmd_iterate_events_reverse(struct tracecmd_input *handle,
 	free_last_events(handle, records, cpus, cpu_size, max_cpus);

lib/trace-cmd/trace-input.c int tracecmd_iterate_events_reverse(struct tracecmd_input *handle, · line 3092

Quoted 10 lines
lib/trace-cmd/trace-input.c +8 -0
 	free(records);
 
+	/*
+	 * If the callback exited out early, then set the cursor back
+	 * to the location of that record so that if this gets called
+	 * again with cont = true, it will continue where it left off.
+	 */
+	if (page_offset) {
+		/* Set the record to the previous record that was read */
+		record = tracecmd_read_at(handle, page_offset - 4, NULL);

Hi,

where does the magic "- 4" come from?

lib/trace-cmd/trace-input.c int tracecmd_iterate_events_reverse(struct tracecmd_input *handle, · line 3093

Quoted 1 line
lib/trace-cmd/trace-input.c +1 -0
+		tracecmd_free_record(record);

I still don't get why this fixes the issue. Does tracecmd_read_at has a side-effect to set the the internal cursor? If so, it IMHO would help to document this side effect in tracecmd_read_at().

Felix

Quoted 7 lines
lib/trace-cmd/trace-input.c +2 -0
+	}
+
 	return ret;
 }
 
Signature · 1 line
2.51.0
Signature · 4 lines
Siemens AG
Linux Expert Center
Friedrich-Ludwig-Bauer-Str. 3
85748 Garching, Germany
#2 lore raw
Quoted 11 lines · Felix

lib/trace-cmd/trace-input.c int tracecmd_iterate_events_reverse(struct tracecmd_input *handle, · line 3092

Quoted 8 lines
lib/trace-cmd/trace-input.c +8 -0
+	/*
+	 * If the callback exited out early, then set the cursor back
+	 * to the location of that record so that if this gets called
+	 * again with cont = true, it will continue where it left off.
+	 */
+	if (page_offset) {
+		/* Set the record to the previous record that was read */
+		record = tracecmd_read_at(handle, page_offset - 4, NULL);  

Hi,

Quoted 1 line

where does the magic "- 4" come from?

It's just to get to the previous record. - 1 would likely work, but as I know all events are aligned by a minimum of 4 bytes I used that. I probably should add a comment about that.

Quoted 6 lines

lib/trace-cmd/trace-input.c int tracecmd_iterate_events_reverse(struct tracecmd_input *handle, · line 3093

Quoted 2 lines
lib/trace-cmd/trace-input.c +1 -0
+		tracecmd_free_record(record);  

I still don't get why this fixes the issue. Does tracecmd_read_at has a side-effect to set the the internal cursor? If so, it IMHO would help to document this side effect in tracecmd_read_at().

Hmm, I guess I should. Yes, that's one of the things I consider "obvious", but "obvious" is seldom obvious ;-)

Pretty much all tracecmd_read* functions set the internal cursor unless stated otherwise.

-- Steve