[PATCH 4/5] perf python: Honor perf script --demangle and --demangle-kernel

From: Jens Remus

Date: Fri Dec 04 2026 - 11:25:07 EST


The perf.session binding always runs with the default symbol_conf
demangle settings, so --no-demangle and --demangle-kernel of perf
script have no effect on the symbol names of a Python script run by
perf script.

Add demangle and demangle_kernel keyword arguments to perf.session.
When not given, they default to the PERF_SYMBOL_DEMANGLE and
PERF_SYMBOL_DEMANGLE_KERNEL environment variables, which perf script
sets from --[no-]demangle and --[no-]demangle-kernel when running a
Python script. Otherwise they default to true and false, as before.

Signed-off-by: Jens Remus <jremus@xxxxxxxxxxxxx>
---
.../perf/Documentation/perf-script-python.txt | 5 +++--
tools/perf/builtin-script.c | 3 +++
tools/perf/python/perf.pyi | 10 ++++++++-
tools/perf/util/python.c | 21 ++++++++++++++++---
4 files changed, 33 insertions(+), 6 deletions(-)

diff --git a/tools/perf/Documentation/perf-script-python.txt b/tools/perf/Documentation/perf-script-python.txt
index b4652a67b5bc..e2f05f5d526c 100644
--- a/tools/perf/Documentation/perf-script-python.txt
+++ b/tools/perf/Documentation/perf-script-python.txt
@@ -19,7 +19,8 @@ trace data recorded by `perf record`.
With the removal of embedded Python interpreter from `perf`, scripts
are now run as standalone Python programs that import the `perf`
module to access trace data. Symbol configuration options (`--vmlinux`,
-`--kallsyms`, `--symfs`, `--inline`/`--no-inline` and `--show-kernel-path`)
+`--kallsyms`, `--symfs`, `--inline`/`--no-inline`, `--show-kernel-path`,
+`--demangle`/`--no-demangle` and `--demangle-kernel`)
and `--itrace` options passed to `perf script` are forwarded via
`PERF_SYMBOL_*` and `PERF_ITRACE` environment variables to `perf.session`,
and can also be passed directly to `perf.session`.
@@ -119,7 +120,7 @@ perf.session

Manages a trace session.

-- `__init__(data, sample=None, stat=None, context_switch=None, call_return=None, itrace=None, vmlinux=None, kallsyms=None, symfs=None, inline=False, show_kernel_path=True)`: Creates a new session. `data` is a `perf.data` object. `sample` is a callback function called for each sample event. `vmlinux`, `kallsyms`, and `symfs` configure symbol resolution paths. `inline` resolves inlined functions in callchains, which are then flagged by `callchain_node.inlined`. `show_kernel_path` reports the kernel DSO by its vmlinux path rather than `[kernel.kallsyms]`. When run by `perf script`, the defaults of `inline` and `show_kernel_path` are the values of `--inline`/`--no-inline` and `--show-kernel-path`.
+- `__init__(data, sample=None, stat=None, context_switch=None, call_return=None, itrace=None, vmlinux=None, kallsyms=None, symfs=None, inline=False, show_kernel_path=True, demangle=True, demangle_kernel=False)`: Creates a new session. `data` is a `perf.data` object. `sample` is a callback function called for each sample event. `vmlinux`, `kallsyms`, and `symfs` configure symbol resolution paths. `inline` resolves inlined functions in callchains, which are then flagged by `callchain_node.inlined`. `show_kernel_path` reports the kernel DSO by its vmlinux path rather than `[kernel.kallsyms]`. `demangle` and `demangle_kernel` demangle user space and kernel symbol names. When run by `perf script`, the defaults of `inline`, `show_kernel_path`, `demangle` and `demangle_kernel` are the values of `--inline`/`--no-inline`, `--show-kernel-path`, `--demangle`/`--no-demangle` and `--demangle-kernel`.
- `process_events()`: Reads the trace data and calls the sample callback for each event.
- `find_thread(pid, tid=-1)`: Returns the thread associated with a PID/TID.
- `e_machine`: ELF machine architecture ID (`EM_*`) of the session.
diff --git a/tools/perf/builtin-script.c b/tools/perf/builtin-script.c
index ccf7e3fe8934..0936cf711e8c 100644
--- a/tools/perf/builtin-script.c
+++ b/tools/perf/builtin-script.c
@@ -4540,6 +4540,9 @@ int cmd_script(int argc, const char **argv)
setenv("PERF_SYMBOL_INLINE", symbol_conf.inline_name ? "1" : "0", 1);
setenv("PERF_SYMBOL_SHOW_KERNEL_PATH",
symbol_conf.show_kernel_path ? "1" : "0", 1);
+ setenv("PERF_SYMBOL_DEMANGLE", symbol_conf.demangle ? "1" : "0", 1);
+ setenv("PERF_SYMBOL_DEMANGLE_KERNEL",
+ symbol_conf.demangle_kernel ? "1" : "0", 1);

execvp(__argv[0], (char **)__argv);
{
diff --git a/tools/perf/python/perf.pyi b/tools/perf/python/perf.pyi
index ce75f8999fa7..c7f43deb84bf 100644
--- a/tools/perf/python/perf.pyi
+++ b/tools/perf/python/perf.pyi
@@ -490,7 +490,9 @@ class session:
kallsyms: Optional[str] = None,
symfs: Optional[str] = None,
inline: bool = ...,
- show_kernel_path: bool = ...
+ show_kernel_path: bool = ...,
+ demangle: bool = ...,
+ demangle_kernel: bool = ...
) -> None:
"""Initialize a perf session.

@@ -505,6 +507,12 @@ class session:
than [kernel.kallsyms]. Defaults to the
PERF_SYMBOL_SHOW_KERNEL_PATH environment variable set by
perf script, otherwise True.
+ demangle: Demangle user space symbol names. Defaults to the
+ PERF_SYMBOL_DEMANGLE environment variable set by perf script,
+ otherwise True.
+ demangle_kernel: Demangle kernel symbol names. Defaults to the
+ PERF_SYMBOL_DEMANGLE_KERNEL environment variable set by perf
+ script, otherwise False.
"""
...
e_machine: int
diff --git a/tools/perf/util/python.c b/tools/perf/util/python.c
index 18a8468c0eb2..fe468a0f0aa4 100644
--- a/tools/perf/util/python.c
+++ b/tools/perf/util/python.c
@@ -4250,6 +4250,8 @@ struct pyrf_session {
char *symfs;
bool inline_name;
bool show_kernel_path;
+ bool demangle;
+ bool demangle_kernel;
};

struct pyrf_call_return {
@@ -4985,18 +4987,21 @@ static PyObject *pyrf_session__new(PyTypeObject *type, PyObject *args, PyObject
char *itrace_str = NULL;
char *vmlinux_str = NULL, *kallsyms_str = NULL, *symfs_str = NULL;
int inline_arg = -1, show_kernel_path_arg = -1;
+ int demangle_arg = -1, demangle_kernel_arg = -1;
static char *kwlist[] = { "data", "sample", "stat", "context_switch",
"call_return", "itrace", "vmlinux", "kallsyms",
- "symfs", "inline", "show_kernel_path", NULL };
+ "symfs", "inline", "show_kernel_path",
+ "demangle", "demangle_kernel", NULL };
struct pyrf_session *psession;
struct perf_session *session;

- if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O!|OOOOzzzzpp", kwlist,
+ if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O!|OOOOzzzzpppp", kwlist,
&pyrf_data__type, &pdata,
&sample, &stat, &context_switch,
&call_return, &itrace_str,
&vmlinux_str, &kallsyms_str, &symfs_str,
- &inline_arg, &show_kernel_path_arg))
+ &inline_arg, &show_kernel_path_arg,
+ &demangle_arg, &demangle_kernel_arg))
return NULL;

psession = PyObject_New(struct pyrf_session, type);
@@ -5020,6 +5025,8 @@ static PyObject *pyrf_session__new(PyTypeObject *type, PyObject *args, PyObject
psession->symfs = NULL;
psession->inline_name = false;
psession->show_kernel_path = true;
+ psession->demangle = true;
+ psession->demangle_kernel = false;
psession->pdata = NULL;
memset(&psession->itrace_opts, 0, sizeof(psession->itrace_opts));

@@ -5140,6 +5147,10 @@ static PyObject *pyrf_session__new(PyTypeObject *type, PyObject *args, PyObject
pyrf_getenv_bool("PERF_SYMBOL_INLINE", psession->inline_name);
psession->show_kernel_path = show_kernel_path_arg >= 0 ? show_kernel_path_arg :
pyrf_getenv_bool("PERF_SYMBOL_SHOW_KERNEL_PATH", psession->show_kernel_path);
+ psession->demangle = demangle_arg >= 0 ? demangle_arg :
+ pyrf_getenv_bool("PERF_SYMBOL_DEMANGLE", psession->demangle);
+ psession->demangle_kernel = demangle_kernel_arg >= 0 ? demangle_kernel_arg :
+ pyrf_getenv_bool("PERF_SYMBOL_DEMANGLE_KERNEL", psession->demangle_kernel);

if (pyrf_session__active) {
PyErr_SetString(PyExc_RuntimeError,
@@ -5153,6 +5164,8 @@ static PyObject *pyrf_session__new(PyTypeObject *type, PyObject *args, PyObject
symbol_conf.use_callchain = true;
symbol_conf.inline_name = psession->inline_name;
symbol_conf.show_kernel_path = psession->show_kernel_path;
+ symbol_conf.demangle = psession->demangle;
+ symbol_conf.demangle_kernel = psession->demangle_kernel;

/* Reading the header of a pipe may block, let other python threads run. */
pyrf_session__active = psession;
@@ -5225,6 +5238,8 @@ static PyObject *pyrf_session__find_thread_events(struct pyrf_session *psession)
symbol_conf.symfs = psession->symfs ?: "";
symbol_conf.inline_name = psession->inline_name;
symbol_conf.show_kernel_path = psession->show_kernel_path;
+ symbol_conf.demangle = psession->demangle;
+ symbol_conf.demangle_kernel = psession->demangle_kernel;

psession->callback_raised = false;
/* Callbacks that use python acquire the GIL, see pyrf_session_tool__sample(). */
--
2.53.0