From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=148.163.158.5 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796401495; b=2BWIJVdhlD57SeIpfMjVywYcOJosaiuxQki8cMnFI0tUAf1O3VdQ0EA/HKPkGts7kzDJ VXzdwfnJIA51V5lmStYUyfKF7ets8E5isQxL2FkIFeo1fR92cLO4nbeqlBh8p/U23ZgIN G4/WZlNRfHGTLskA9Dzg03vwFQ24dOlmjUkqCZJ6Y6SGzEOA2jyB0C3ZCvyXc7AWdAFun iehm4GIWYq3dJQlWY9xdbEF86VZnH1re06oqtpWuqWRYKnhQeM/x+iuQkHTAyFogNzRNX oBK0BFd+mcpbTDCHLcc0C+CEe6WwiAWTW9bF15pfmSzFRzUEAu/ybO9Vil6HlsYHylQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796401495; h=DMARC-Filter:Received:DKIM-Signature:Received:Received:Received: Received:Received:Received:Received:From:To:Cc:Subject:Date: Message-ID:X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding:X-TM-AS-GCONF:X-Proofpoint-Reinject: X-Proofpoint-ORIG-GUID:X-Proofpoint-GUID: X-Proofpoint-Spam-Details-Enc:X-Proofpoint-Spam-Info: X-Authority-Analysis:X-Proofpoint-Virus-Version: X-Proofpoint-Spam-Details; bh=1BxXn+RUfMG0+UOetTpMRkN0ShdAuwAvVuQ/91RwiAo=; b=gYeigNTo6wuPdxgMBEyvuVS/jnHdESfSQObjovaZqv/cP2ePzVdY1bTdkBbnQPj53EbL VSjtcjQhCQ+0rvVbahK6yhshtbAOyXAUj5zlTYw9I6lh3JGxBLdfbDte076+cH07SU07E LKRyQRejp8auFvw9EoGSQBeimgy0+SONVpMOLGxbfR+5RAjs/DX/x8nQRI2R0vJXb17U4 porcRh4ttXAywdcL/wv9xEbhHhYKjhYt/qCyoU3p8BNwN84gfRcQtIAVkx9BBk4zvhzFA onKQtFV9hGz+wyo7MlJNMp0it9bE1E97lJtihtd68mQME/gEuBt6rSNN/U+qPkL8S0Q== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=linux.ibm.com; spf=pass smtp.mailfrom=linux.ibm.com; dkim=pass header.d=ibm.com header.i=@ibm.com header.a=rsa-sha256 header.s=pp1 header.b=kAu0ZeX2; arc=none smtp.remote-ip=148.163.158.5 Received: from mx0b-001b2d01.pphosted.com (mx0b-001b2d01.pphosted.com [148.163.158.5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 37C8C606A32D; Fri, 4 Dec 2026 16:24:54 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 37C8C606A32D Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.ibm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.ibm.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=ibm.com header.i=@ibm.com header.a=rsa-sha256 header.s=pp1 header.b=kAu0ZeX2 Received: from pps.filterd (m0356516.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.18.1.11/8.18.1.11) with ESMTP id 6B4EYaM02350309; Fri, 4 Dec 2026 16:24:44 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ibm.com; h=cc :content-transfer-encoding:date:from:in-reply-to:message-id :mime-version:references:subject:to; s=pp1; bh=1BxXn+RUfMG0+UOet TpMRkN0ShdAuwAvVuQ/91RwiAo=; b=kAu0ZeX2KLS5el2HY5u96JfmF5OKv95UX QL11HAYdTxHw4byl8MWypRFpp7W4YhJUXegS6zNG5jzfP3e2icFcj48MZOV5+UcE w944X22HKgIBvFu3LmDbRLwCT/+vW6dhOJa2sTSuY3GTWZQFdqyV7bEbpvGTWoOs Z/TMFkTAg82yWpLyxztEn/PeLUgZz6+CjMWIXHRO9cmjY/1kENpWhjhWq0W+Ed+m 6HwpaaWDYTd8ScNvBtRay1NJs9whVpFjMbmkN1dp0Lhea8BhloQrHLb7MPnhl/K0 mAzks6FgLxmq7rtcrRyL1MEfi3UYgviRgdgoxeQ/Y7Jkry103Wxlw== Received: from ppma13.dal12v.mail.ibm.com (dd.9e.1632.ip4.static.sl-reverse.com [50.22.158.221]) by mx0a-001b2d01.pphosted.com (PPS) with ESMTPS id 4jbqghjnhq-1 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Fri, 04 Dec 2026 16:24:43 +0000 (GMT) Received: from pps.filterd (ppma13.dal12v.mail.ibm.com [127.0.0.1]) by ppma13.dal12v.mail.ibm.com (8.18.1.11/8.18.1.11) with ESMTP id 6B4EYgZf1125538; Fri, 4 Dec 2026 16:24:42 GMT Received: from smtprelay06.wdc07v.mail.ibm.com ([172.16.1.73]) by ppma13.dal12v.mail.ibm.com (PPS) with ESMTPS id 4jbjn9u81q-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Fri, 04 Dec 2026 16:24:42 +0000 (GMT) Received: from smtpav04.dal12v.mail.ibm.com (smtpav04.dal12v.mail.ibm.com [10.241.53.103]) by smtprelay06.wdc07v.mail.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id 6B4GOf7432899628 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 4 Dec 2026 16:24:41 GMT Received: from smtpav04.dal12v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id DD37C58052; Fri, 4 Dec 2026 16:24:40 +0000 (GMT) Received: from smtpav04.dal12v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id D22325805A; Fri, 4 Dec 2026 16:24:38 +0000 (GMT) Received: from tuxmaker.lnxne.boe (unknown [9.87.85.9]) by smtpav04.dal12v.mail.ibm.com (Postfix) with ESMTP; Fri, 4 Dec 2026 16:24:38 +0000 (GMT) From: Jens Remus To: Ian Rogers , Arnaldo Carvalho de Melo , Namhyung Kim , Peter Zijlstra , Ingo Molnar Cc: Jens Remus , Jan Polensky , Sumanth Korikkar , Heiko Carstens , Vasily Gorbik , Alexander Gordeev , 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 Message-ID: <20261204162426.4171717-8-jremus@linux.ibm.com> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20261204162426.4171717-7-jremus@linux.ibm.com> References: <20261204162426.4171717-7-jremus@linux.ibm.com> X-Mailing-List: linux-perf-users@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-TM-AS-GCONF: 00 X-Proofpoint-Reinject: loops=2 maxloops=12 X-Proofpoint-ORIG-GUID: xK5OKV2gZzUy5GhF8YRta3HrH2_tydI6 X-Proofpoint-GUID: 6w96EoDRc_qnSLISdDchqj_axpYfN7u9 X-Proofpoint-Spam-Details-Enc: AW1haW4tMjYxMjA0MDA2NCBTYWx0ZWRfX86kUZpi9IbxQ UyYBRnJxQiqDMX7FLovOSAS3DXFwveCFFjA+kB5sNdOaPbt+aJ6JnSI+fbOguEI3DCFKZY3LJDZ L4IdnQsm9wfCqQwWzCsoqF+AyvBAmyhs6P48T3L67fRiUAS/NtUnzEzyGp/Hc5JXJTLV+RY0ds4 ViUOYDrCwmdBz8EPl0LOwhtRwOMXB5KMD3NmxkVpwCNlFkO010AyodgYvRH364+6HBRfqQOmke6 jF7XpLVvrxOLJ99WpmtGUsgGZVcW5n+uBuC7db3Q8Xl/NoeLltGlq5JfvxHTv99mNV0B/5M2vXh vusriUVEsNkVDZb5c+1aT43/IfQppY1fD7EWhe8Ti/yvCF+P5NB2KSYP3tsFRidaJd4HVG9nN2O gDMlrwOEXg2TfdcjdB5kiuDSKCo+aLoRrE2t9OrYger49xvrawZ1LzG3I68twgSa281oGsUYi3d MA4L/rzke13IZQVdWwg== X-Proofpoint-Spam-Info: AW1haW4tMjYxMjA0MDA2NCBTYWx0ZWRfX5/qEyodxrFGZ VK6t+nIX1qVgiYWrPJvFUT/hPLBg79q7GFvCGhM0YMFS8IqaZS/mbz3PIBOqkemJfPDEgrWgWo6 QzviRi3uimPC30pZmVK3jT7SmeA8YZQ= X-Authority-Analysis: v=2.4 cv=Uc7E26SN c=1 sm=1 tr=0 ts=6b12e94b cx=c_pps a=AfN7/Ok6k8XGzOShvHwTGQ==:117 a=AfN7/Ok6k8XGzOShvHwTGQ==:17 a=pUM-KoujPXoA:10 a=VkNPw1HP01LnGYTKEx00:22 a=RnoormkPH1_aCDwRdu11:22 a=Y2IxJ9c9Rs8Kov3niI8_:22 a=VnNF1IyMAAAA:8 a=TT01aAlpYz6tgXXpO2MA:9 a=L03L2QfmqWoA:10 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1176,Hydra:6.1.138,FMLib:17.12.100.49 definitions=2026-12-04_03,2026-12-03_04,2025-10-01_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 spamscore=0 adultscore=0 lowpriorityscore=0 clxscore=1015 priorityscore=1501 phishscore=0 malwarescore=0 suspectscore=0 impostorscore=0 bulkscore=0 classifier=typeunknown authscore=0 authtc= authcc= route=outbound adjust=0 reason=mlx scancount=1 engine=8.22.0-2611040001 definitions=main-2612040064 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 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 --- .../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