Skip to content

Commit

Permalink
libtracecmd: Use cpu_data[cpu]->cpus and not ->max_cpu
Browse files Browse the repository at this point in the history
The handle->cpu_data[cpu]->max_cpu is the largest number CPU that is
recorded in the trace, where as cpu_data[cpu]->cpus is the number of
handle->cpu_data[] entries.

The iterator loops mistakenly used the max_cpu field instead of the cpus
field and this can cause errors if the two are not the same. This is the
case if one of the CPUs contained no data, it will not have a cpu_data[]
entry and will be skipped.

Link: https://lore.kernel.org/linux-trace-devel/[email protected]

Fixes: 2cb6cc2 ("tracecmd library: Add tracecmd_iterate_events()")
Signed-off-by: Steven Rostedt (Google) <[email protected]>
  • Loading branch information
rostedt committed Jan 11, 2024
1 parent 56d07ae commit a49c838
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions lib/trace-cmd/trace-input.c
Original file line number Diff line number Diff line change
Expand Up @@ -2822,11 +2822,11 @@ int tracecmd_iterate_events(struct tracecmd_input *handle,
return -1;
}

records = calloc(handle->max_cpu, sizeof(*records));
records = calloc(handle->cpus, sizeof(*records));
if (!records)
return -1;

for (cpu = 0; cpu < handle->max_cpu; cpu++) {
for (cpu = 0; cpu < handle->cpus; cpu++) {
if (cpus && !CPU_ISSET_S(cpu, cpu_size, cpus))
continue;

Expand All @@ -2835,7 +2835,7 @@ int tracecmd_iterate_events(struct tracecmd_input *handle,

do {
next_cpu = -1;
for (cpu = 0; cpu < handle->max_cpu; cpu++) {
for (cpu = 0; cpu < handle->cpus; cpu++) {
record = records[cpu];
if (!record)
continue;
Expand All @@ -2858,7 +2858,7 @@ int tracecmd_iterate_events(struct tracecmd_input *handle,
} while (next_cpu >= 0 && ret == 0);

/* Need to unlock and free the records */
for (cpu = 0; cpu < handle->max_cpu; cpu++) {
for (cpu = 0; cpu < handle->cpus; cpu++) {
int offset;

if (!records[cpu])
Expand Down Expand Up @@ -3028,7 +3028,7 @@ int tracecmd_iterate_events_reverse(struct tracecmd_input *handle,
struct tep_record **records;
struct tep_record *record;
int next_cpu;
int max_cpus = handle->max_cpu;
int max_cpus = handle->cpus;
int cpu;
int ret = 0;

Expand Down Expand Up @@ -3122,7 +3122,7 @@ int tracecmd_iterate_events_multi(struct tracecmd_input **handles,

for (i = 0; i < nr_handles; i++) {
handle = handles[i];
cpus += handle->max_cpu;
cpus += handle->cpus;
}

records = calloc(cpus, sizeof(*records));
Expand All @@ -3132,7 +3132,7 @@ int tracecmd_iterate_events_multi(struct tracecmd_input **handles,
for (i = 0; i < nr_handles; i++) {
handle = handles[i];
handle->start_cpu = all_cpus;
for (cpu = 0; cpu < handle->max_cpu; cpu++) {
for (cpu = 0; cpu < handle->cpus; cpu++) {
records[all_cpus + cpu].record = tracecmd_peek_data(handle, cpu);
records[all_cpus + cpu].handle = handle;
}
Expand Down

0 comments on commit a49c838

Please sign in to comment.