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

sashiko-bot
2026-04-25T23:09:11Z

Sashiko AI review found 1 potential issue(s):
- [Medium] Unhandled exceptions during thread and comm resolution silently truncate event processing.
--

commit 51781e2de9d088f1f729d3ac01beec1a4a11ff21
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

[ ... ]

> @@ -0,0 +1,126 @@
> +#!/usr/bin/env python3

[ ... ]

> + def process_event(self, sample: perf.sample_event) -> None:
> + """Collect call stack for each sample."""

[ ... ]

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

What happens if the sample PID cannot be resolved by the perf session?
Will self.session.find_thread() raise a TypeError?

Also, if the thread is found but has no comm string, will .comm() return None,
causing the .replace(" ", "_") call to raise an AttributeError?

If these exceptions propagate unhandled to the C-level callback, it appears
the event processing loop is aborted. Because the script ignores the return
value of session.process_events(), could this silently truncate the flamegraph
data and exit with a success status?

Would it make sense to wrap these lookups in a try...except block catching
TypeError and AttributeError to allow a fallback to "Unknown" for the comm
string?

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