Linux Perf Users
 help / color / mirror / Atom feed
From: Jens Remus <jremus@linux.ibm.com>
To: Ian Rogers <irogers@google.com>,
	Arnaldo Carvalho de Melo <acme@kernel.org>,
	Namhyung Kim <namhyung@kernel.org>,
	Peter Zijlstra <peterz@infradead.org>,
	Ingo Molnar <mingo@redhat.com>
Cc: Jens Remus <jremus@linux.ibm.com>,
	Jan Polensky <japo@linux.ibm.com>,
	Sumanth Korikkar <sumanthk@linux.ibm.com>,
	Heiko Carstens <hca@linux.ibm.com>,
	Vasily Gorbik <gor@linux.ibm.com>,
	Alexander Gordeev <agordeev@linux.ibm.com>,
	linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: [PATCH 1/5] perf python: Support inlined frames in callchains
Date: Fri,  4 Dec 2026 17:24:28 +0100	[thread overview]
Message-ID: <20261204162426.4171717-8-jremus@linux.ibm.com> (raw)
In-Reply-To: <20261204162426.4171717-7-jremus@linux.ibm.com>

The perf.session binding forces symbol_conf.inline_name to false, so
callchains never contain inlined frames, unlike perf script, which
resolves them by default and offers --no-inline to disable it.

Add an inline keyword argument to perf.session.  When not given, it
defaults to the PERF_SYMBOL_INLINE environment variable, which perf
script sets from --inline/--no-inline when running a Python script,
like it already does for --vmlinux, --kallsyms, --symfs and --itrace.
Otherwise it defaults to false, as before.

Add the inlined flag to perf.callchain_node, so that scripts can tell
inlined frames from the frame of the function they are inlined into.

This enables perf script -s <script-file> using a Python script to
generate output identical to perf script.

Note that Python scripts run by perf script now resolve inlined frames
by default, as perf script does.  Use --no-inline to disable it.

Signed-off-by: Jens Remus <jremus@linux.ibm.com>
---
 .../perf/Documentation/perf-script-python.txt |  9 +++--
 tools/perf/builtin-script.c                   |  1 +
 tools/perf/python/perf.pyi                    |  7 +++-
 tools/perf/util/python.c                      | 38 +++++++++++++++++--
 4 files changed, 46 insertions(+), 9 deletions(-)

diff --git a/tools/perf/Documentation/perf-script-python.txt b/tools/perf/Documentation/perf-script-python.txt
index cbf50b3e5f06..10b124267f89 100644
--- a/tools/perf/Documentation/perf-script-python.txt
+++ b/tools/perf/Documentation/perf-script-python.txt
@@ -19,9 +19,10 @@ 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`, and `--symfs`) 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`.
+`--kallsyms`, `--symfs` and `--inline`/`--no-inline`) 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`.
 Trace filtering command-line
 options (`-C`, `-c`, `--pid`, `--tid`, `--time`, `--dlfilter`, `--dlarg`) passed
 to `perf script` are not applied to standalone scripts.
@@ -118,7 +119,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)`: 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.
+- `__init__(data, sample=None, stat=None, context_switch=None, call_return=None, itrace=None, vmlinux=None, kallsyms=None, symfs=None, inline=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`. When run by `perf script`, the default of `inline` is the value of `--inline`/`--no-inline`.
 - `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 b6691ebb1b4e..2ab6b17f638e 100644
--- a/tools/perf/builtin-script.c
+++ b/tools/perf/builtin-script.c
@@ -4537,6 +4537,7 @@ int cmd_script(int argc, const char **argv)
 				setenv("PERF_SYMBOL_SYMFS", symbol_conf.symfs, 1);
 			if (script_itrace_opts)
 				setenv("PERF_ITRACE", script_itrace_opts, 1);
+			setenv("PERF_SYMBOL_INLINE", symbol_conf.inline_name ? "1" : "0", 1);
 
 			execvp(__argv[0], (char **)__argv);
 			{
diff --git a/tools/perf/python/perf.pyi b/tools/perf/python/perf.pyi
index 087da00728fd..4fcbaf4694c9 100644
--- a/tools/perf/python/perf.pyi
+++ b/tools/perf/python/perf.pyi
@@ -353,6 +353,7 @@ class callchain_node:
     symbol: str
     dso: str
     sym_offset: Optional[int]
+    inlined: bool
 
 class callchain:
     """Sequence of callchain frames."""
@@ -487,7 +488,8 @@ class session:
         itrace: Optional[str] = None,
         vmlinux: Optional[str] = None,
         kallsyms: Optional[str] = None,
-        symfs: Optional[str] = None
+        symfs: Optional[str] = None,
+        inline: bool = ...
     ) -> None:
         """Initialize a perf session.
 
@@ -495,6 +497,9 @@ class session:
             data: The perf data file to read.
             sample: Callback for sample events.
             stat: Callback for stat events.
+            inline: Resolve inlined functions in callchains. Defaults to the
+                PERF_SYMBOL_INLINE 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 60dbe2dad5eb..b5426dfaee01 100644
--- a/tools/perf/util/python.c
+++ b/tools/perf/util/python.c
@@ -994,11 +994,18 @@ static PyObject *pyrf_callchain_node__get_sym_offset(struct pyrf_callchain_node
 	return PyLong_FromUnsignedLongLong(addr - pnode->sym->start);
 }
 
+static PyObject *pyrf_callchain_node__get_inlined(struct pyrf_callchain_node *pnode,
+						  void *closure __maybe_unused)
+{
+	return PyBool_FromLong(pnode->sym && symbol__inlined(pnode->sym));
+}
+
 static PyGetSetDef pyrf_callchain_node__getset[] = {
 	{ .name = "ip",         .get = (getter)pyrf_callchain_node__get_ip, },
 	{ .name = "symbol",     .get = (getter)pyrf_callchain_node__get_symbol, },
 	{ .name = "dso",        .get = (getter)pyrf_callchain_node__get_dso, },
 	{ .name = "sym_offset", .get = (getter)pyrf_callchain_node__get_sym_offset, },
+	{ .name = "inlined",    .get = (getter)pyrf_callchain_node__get_inlined, },
 	{ .name = NULL, },
 };
 
@@ -4241,6 +4248,7 @@ struct pyrf_session {
 	char *vmlinux_name;
 	char *kallsyms_name;
 	char *symfs;
+	bool inline_name;
 };
 
 struct pyrf_call_return {
@@ -4958,6 +4966,16 @@ static PyObject *pyrf_session__find_thread(struct pyrf_session *psession, PyObje
 	return result;
 }
 
+/* Return @def when the environment variable is unset or empty. */
+static bool pyrf_getenv_bool(const char *name, bool def)
+{
+	const char *str = getenv(name);
+
+	if (!str || !str[0])
+		return def;
+	return strcmp(str, "0") != 0;
+}
+
 static PyObject *pyrf_session__new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
 {
 	struct pyrf_data *pdata;
@@ -4965,17 +4983,19 @@ static PyObject *pyrf_session__new(PyTypeObject *type, PyObject *args, PyObject
 	PyObject *call_return = NULL;
 	char *itrace_str = NULL;
 	char *vmlinux_str = NULL, *kallsyms_str = NULL, *symfs_str = NULL;
+	int inline_arg = -1;
 	static char *kwlist[] = { "data", "sample", "stat", "context_switch",
 				  "call_return", "itrace", "vmlinux", "kallsyms",
-				  "symfs", NULL };
+				  "symfs", "inline", NULL };
 	struct pyrf_session *psession;
 	struct perf_session *session;
 
-	if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O!|OOOOzzzz", kwlist,
+	if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O!|OOOOzzzzp", kwlist,
 					 &pyrf_data__type, &pdata,
 					 &sample, &stat, &context_switch,
 					 &call_return, &itrace_str,
-					 &vmlinux_str, &kallsyms_str, &symfs_str))
+					 &vmlinux_str, &kallsyms_str, &symfs_str,
+					 &inline_arg))
 		return NULL;
 
 	psession = PyObject_New(struct pyrf_session, type);
@@ -4997,6 +5017,7 @@ static PyObject *pyrf_session__new(PyTypeObject *type, PyObject *args, PyObject
 	psession->vmlinux_name = NULL;
 	psession->kallsyms_name = NULL;
 	psession->symfs = NULL;
+	psession->inline_name = false;
 	psession->pdata = NULL;
 	memset(&psession->itrace_opts, 0, sizeof(psession->itrace_opts));
 
@@ -5108,6 +5129,14 @@ static PyObject *pyrf_session__new(PyTypeObject *type, PyObject *args, PyObject
 		}
 	}
 
+	/*
+	 * The keyword arguments take precedence over the environment variables
+	 * perf script sets from its command line options, which take
+	 * precedence over the defaults.
+	 */
+	psession->inline_name = inline_arg >= 0 ? inline_arg :
+		pyrf_getenv_bool("PERF_SYMBOL_INLINE", psession->inline_name);
+
 	if (pyrf_session__active) {
 		PyErr_SetString(PyExc_RuntimeError,
 				"Another perf.session is currently active");
@@ -5119,7 +5148,7 @@ static PyObject *pyrf_session__new(PyTypeObject *type, PyObject *args, PyObject
 	symbol_conf.symfs = psession->symfs ?: "";
 	symbol_conf.use_callchain = true;
 	symbol_conf.show_kernel_path = true;
-	symbol_conf.inline_name = false;
+	symbol_conf.inline_name = psession->inline_name;
 
 	/* Reading the header of a pipe may block, let other python threads run. */
 	pyrf_session__active = psession;
@@ -5190,6 +5219,7 @@ static PyObject *pyrf_session__find_thread_events(struct pyrf_session *psession)
 	symbol_conf.vmlinux_name = psession->vmlinux_name;
 	symbol_conf.kallsyms_name = psession->kallsyms_name;
 	symbol_conf.symfs = psession->symfs ?: "";
+	symbol_conf.inline_name = psession->inline_name;
 
 	psession->callback_raised = false;
 	/* Callbacks that use python acquire the GIL, see pyrf_session_tool__sample(). */
-- 
2.53.0


  reply	other threads:[~2026-12-04 16:24 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-04 16:24 [PATCH 0/5] perf python: Honor more perf script options Jens Remus
2026-12-04 16:24 ` Jens Remus [this message]
2026-12-04 16:46   ` [PATCH 1/5] perf python: Support inlined frames in callchains sashiko-bot
2026-12-04 16:24 ` [PATCH 2/5] perf python: Export inlined symbol flag in DB export scripts Jens Remus
2026-12-04 16:59   ` sashiko-bot
2026-12-04 16:24 ` [PATCH 3/5] perf python: Honor perf script --show-kernel-path Jens Remus
2026-12-04 17:13   ` sashiko-bot
2026-12-04 16:24 ` [PATCH 4/5] perf python: Honor perf script --demangle and --demangle-kernel Jens Remus
2026-12-04 16:24 ` [PATCH 5/5] perf python: Honor perf script --max-stack Jens Remus
2026-12-04 17:35   ` sashiko-bot
2026-12-04 17:35 ` [PATCH 0/5] perf python: Honor more perf script options sashiko-bot

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261204162426.4171717-8-jremus@linux.ibm.com \
    --to=jremus@linux.ibm.com \
    --cc=acme@kernel.org \
    --cc=agordeev@linux.ibm.com \
    --cc=gor@linux.ibm.com \
    --cc=hca@linux.ibm.com \
    --cc=irogers@google.com \
    --cc=japo@linux.ibm.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-perf-users@vger.kernel.org \
    --cc=mingo@redhat.com \
    --cc=namhyung@kernel.org \
    --cc=peterz@infradead.org \
    --cc=sumanthk@linux.ibm.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox