Re: [PATCH v6 45/59] perf stackcollapse: Port stackcollapse to use python module

sashiko-bot
2026-04-25T18:08:06Z

Sashiko AI review found 1 potential issue(s):
- [Medium] Unhandled TypeError in stackcollapse.py when resolving unknown threads causes the script to crash.
--

commit 702f12c8a01187fcead25d5d7f9e4ce41f1f01ae
Author: Ian Rogers <irogers@google.com>

perf stackcollapse: Port stackcollapse to use python module

This commit modernizes the legacy stackcollapse.py trace script by
refactoring it into a class-based architecture. It uses perf.session
for event processing and aggregates call stacks to produce output
suitable for flame graphs.

> diff --git a/tools/perf/python/stackcollapse.py b/tools/perf/python/stackcollapse.py
> new file mode 100755
> index 000000000000..996c73246ebc
> --- /dev/null
> +++ b/tools/perf/python/stackcollapse.py

[ ... ]

> + def process_event(self, sample: perf.sample_event) -> None:

[ ... ]

> + if self.args.include_comm:
> + if hasattr(self, 'session') and self.session:
> + comm = self.session.find_thread(sample.sample_pid).comm()

Will this crash the script if the thread cannot be found?

The Python extension function pyrf_session__find_thread() in
tools/perf/util/python.c raises a TypeError if the thread lookup fails:

tools/perf/util/python.c:pyrf_session__find_thread() {
...
thread = machine__findnew_thread(machine, -1, pid);
if (thread == NULL) {
PyErr_Format(PyExc_TypeError, "Fail to find thread for mac %d and pid %d",
machine->pid, pid);
return NULL;
}
...
}

If perf.data contains samples with unmapped or unresolved PIDs, won't
calling self.session.find_thread() without a try-except block result in an
unhandled TypeError and terminate the script?

> + else:
> + comm = "Unknown"
> + comm = comm.replace(" ", "_")

--
Sashiko AI review · https://sashiko.dev/#/patchset/20260425174858.3922152-1-irogers@google.com?part=45