From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id D17E5FF885C for ; Sat, 25 Apr 2026 22:51:26 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Type:Cc:To:From: Subject:Message-ID:References:Mime-Version:In-Reply-To:Date:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=teyYQp0E3EyOIZgpUXSUO0oKReWVKi9UUmdHMeA+/YA=; b=lCexpABMqvLAb11KkIE/NFlikB xLi8XBiqKmwqgsOj9+C6epcTfU70MwuKxDOQFVfPrliYKpUpTWwGV9FHUXghuvoDq+SmT2g4vvWzW w2LU197lc9vmC1FZhybzdcfOpAzmu+Hty9ELVZPTJAZq5fHkn7yih9/EJD/tigX7Cz4H0cEWr9SBH fRchnf77jJRSJkUxjZIC/z01yHDKfAgRirsNjt5h93loNoRdde/PMi6jNAdzBMGwNsOsXMdpifKOt +JzMK6GyqUStItqwgZdWxj8RCw3iJI8hx0xKayDRXjzN8hj3mVx65k8CG7cdK2E0cUaOosLBjbU+y KkL3WcFg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98.2 #2 (Red Hat Linux)) id 1wGlqE-0000000Eyo7-3gui; Sat, 25 Apr 2026 22:51:14 +0000 Received: from mail-dl1-x1249.google.com ([2607:f8b0:4864:20::1249]) by bombadil.infradead.org with esmtps (Exim 4.98.2 #2 (Red Hat Linux)) id 1wGlpr-0000000EyNf-3iJK for linux-arm-kernel@lists.infradead.org; Sat, 25 Apr 2026 22:50:55 +0000 Received: by mail-dl1-x1249.google.com with SMTP id a92af1059eb24-1270dcd11c1so5627435c88.0 for ; Sat, 25 Apr 2026 15:50:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1777157450; x=1777762250; darn=lists.infradead.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=teyYQp0E3EyOIZgpUXSUO0oKReWVKi9UUmdHMeA+/YA=; b=aRkmM3CVoXsiJLauO946kSEk0wqp7PksCpCsQevtnlK384wUunYz/O0Z9md+AdmpNF eJBluKTubObBhMX864z+NF8+ru9R/fj73NiIfiVFNmD+5xsNFhBIoxESopPwk9BGoqke 4COVpleZL/Ddd8DT/fqS5mjoU1oBFy/vWT3p3sR0Ax6/BD/6GQJoL7w62o87yoSD+XFE ClOrwRtGw+Uw/biBkEq7gerBq+jOm4aqINPAnBJ4QZCTmDVmcS/+pBBKGA1craniTQ3g iSIyCj9tQZDPw0gzud/8A0NMtPurYRR2MdMWVDYrtAn+bWOYR+v7oktk2k6vSezCM7vk Mctg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1777157450; x=1777762250; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=teyYQp0E3EyOIZgpUXSUO0oKReWVKi9UUmdHMeA+/YA=; b=AQYX9CuWdR9Li3RB9yQH+6VHIy42zP0aUB8iwI/IJXeUJ2csL4TN3lp5FP7FbP9YQI O/vdEPICx8c2nSdRLyV2RTLG3lOHUsDPXdOSCr0tSwYeuh1boDzPkcx2w8VQzRnBmRPW 0uttluAAx3wmOHd/L2h4OO2f18rR2z9e8kYKSAba5iQ5/Hl8MEpubMnwN867gTm7Mn+x dMHsJXmtAMrFvVNPLm6HuMar/3nJoORNTGoW7+Z4Vg/AuF+JcEK5vT2anIg3oiLICxfJ hjpemrXj3Jr/UOWW4r0YMuhfvxYlO1G9gcv1U7EZz6syPd7y+pPa4U1R9rUKhAYBl5nj nyTA== X-Forwarded-Encrypted: i=1; AFNElJ+QloH1yfXGj/8caScaX/MkvUZr4OGoixwj0+tZuzb61OY7HVBcp4Mnv8ZO5Is7lewUlPGQzdS8NMHYjHkhgMKn@lists.infradead.org X-Gm-Message-State: AOJu0Yy0zB1exJblZ2V7yON9uq0MulCopw2f27LmaDdaNjGFeVH+Cdoc GNSGopNgz5vCt8xa8FbGQh/IXgCLU2pdnR0pdm62VNs6RWhp5NB/pb2gr0caCEGZj6SBFzhYrLv okX+y1ereWw== X-Received: from dlad28.prod.google.com ([2002:a05:701b:221c:b0:12a:7f44:d2e3]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a05:7022:6981:b0:12c:856:ddcc with SMTP id a92af1059eb24-12c73fa3c9amr19854524c88.27.1777157450294; Sat, 25 Apr 2026 15:50:50 -0700 (PDT) Date: Sat, 25 Apr 2026 15:49:10 -0700 In-Reply-To: <20260425224951.174663-1-irogers@google.com> Mime-Version: 1.0 References: <20260425174858.3922152-1-irogers@google.com> <20260425224951.174663-1-irogers@google.com> X-Mailer: git-send-email 2.54.0.545.g6539524ca2-goog Message-ID: <20260425224951.174663-19-irogers@google.com> Subject: [PATCH v7 18/59] perf python: Add callchain support From: Ian Rogers To: acme@kernel.org, adrian.hunter@intel.com, james.clark@linaro.org, leo.yan@linux.dev, namhyung@kernel.org, tmricht@linux.ibm.com Cc: alice.mei.rogers@gmail.com, dapeng1.mi@linux.intel.com, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, mingo@redhat.com, peterz@infradead.org, Ian Rogers Content-Type: text/plain; charset="UTF-8" X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20260425_155051_965863_42033986 X-CRM114-Status: GOOD ( 21.16 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Implement pyrf_callchain_node and pyrf_callchain types for lazy iteration over callchain frames. Add callchain property to sample_event. Assisted-by: Gemini:gemini-3.1-pro-preview Signed-off-by: Ian Rogers --- v2: 1. Eager Callchain Resolution: Moved the callchain resolution from deferred iteration to eager processing in pyrf_session_tool__sample() . This avoids risks of reading from unmapped memory or following dangling pointers to closed sessions. 2. Cached Callchain: Added a callchain field to struct pyrf_event to store the resolved object. 3. Simplified Access: pyrf_sample_event__get_callchain() now just returns the cached object if available. 4. Avoided Double Free: Handled lazy cleanups properly. v6: - Moved callchain resolution from `session_tool__sample` to `pyrf_event__new`. --- tools/perf/util/python.c | 241 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 240 insertions(+), 1 deletion(-) diff --git a/tools/perf/util/python.c b/tools/perf/util/python.c index 824cf58645e0..2953c4c8e142 100644 --- a/tools/perf/util/python.c +++ b/tools/perf/util/python.c @@ -66,6 +66,8 @@ struct pyrf_event { struct addr_location al; /** @al_resolved: True when machine__resolve been called. */ bool al_resolved; + /** @callchain: Resolved callchain, eagerly computed if requested. */ + PyObject *callchain; /** @event: The underlying perf_event that may be in a file or ring buffer. */ union perf_event event; }; @@ -103,6 +105,7 @@ static void pyrf_event__delete(struct pyrf_event *pevent) { if (pevent->al_resolved) addr_location__exit(&pevent->al); + Py_XDECREF(pevent->callchain); perf_sample__exit(&pevent->sample); Py_TYPE(pevent)->tp_free((PyObject *)pevent); } @@ -621,6 +624,181 @@ static PyObject *pyrf_sample_event__insn(PyObject *self, PyObject *args __maybe_ pevent->sample.insn_len); } +struct pyrf_callchain_node { + PyObject_HEAD + u64 ip; + struct map *map; + struct symbol *sym; +}; + +static void pyrf_callchain_node__delete(struct pyrf_callchain_node *pnode) +{ + map__put(pnode->map); + Py_TYPE(pnode)->tp_free((PyObject*)pnode); +} + +static PyObject *pyrf_callchain_node__get_ip(struct pyrf_callchain_node *pnode, + void *closure __maybe_unused) +{ + return PyLong_FromUnsignedLongLong(pnode->ip); +} + +static PyObject *pyrf_callchain_node__get_symbol(struct pyrf_callchain_node *pnode, + void *closure __maybe_unused) +{ + if (pnode->sym) + return PyUnicode_FromString(pnode->sym->name); + return PyUnicode_FromString("[unknown]"); +} + +static PyObject *pyrf_callchain_node__get_dso(struct pyrf_callchain_node *pnode, + void *closure __maybe_unused) +{ + const char *dsoname = "[unknown]"; + + if (pnode->map) { + struct dso *dso = map__dso(pnode->map); + if (dso) { + if (symbol_conf.show_kernel_path && dso__long_name(dso)) + dsoname = dso__long_name(dso); + else + dsoname = dso__name(dso); + } + } + return PyUnicode_FromString(dsoname); +} + +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 = NULL, }, +}; + +static PyTypeObject pyrf_callchain_node__type = { + PyVarObject_HEAD_INIT(NULL, 0) + .tp_name = "perf.callchain_node", + .tp_basicsize = sizeof(struct pyrf_callchain_node), + .tp_dealloc = (destructor)pyrf_callchain_node__delete, + .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, + .tp_doc = "perf callchain node object.", + .tp_getset = pyrf_callchain_node__getset, +}; + +struct pyrf_callchain_frame { + u64 ip; + struct map *map; + struct symbol *sym; +}; + +struct pyrf_callchain { + PyObject_HEAD + struct pyrf_event *pevent; + struct pyrf_callchain_frame *frames; + u64 nr_frames; + u64 pos; + bool resolved; +}; + +static void pyrf_callchain__delete(struct pyrf_callchain *pchain) +{ + Py_XDECREF(pchain->pevent); + if (pchain->frames) { + for (u64 i = 0; i < pchain->nr_frames; i++) + map__put(pchain->frames[i].map); + free(pchain->frames); + } + Py_TYPE(pchain)->tp_free((PyObject*)pchain); +} + +static PyObject *pyrf_callchain__next(struct pyrf_callchain *pchain) +{ + struct pyrf_callchain_node *pnode; + + if (!pchain->resolved) { + struct evsel *evsel = pchain->pevent->sample.evsel; + struct evlist *evlist = evsel->evlist; + struct perf_session *session = evlist ? evlist__session(evlist) : NULL; + struct addr_location al; + struct callchain_cursor *cursor; + struct callchain_cursor_node *node; + u64 i; + + if (!session || !pchain->pevent->sample.callchain) + return NULL; + + addr_location__init(&al); + if (machine__resolve(&session->machines.host, &al, &pchain->pevent->sample) < 0) { + addr_location__exit(&al); + return NULL; + } + + cursor = get_tls_callchain_cursor(); + if (thread__resolve_callchain(al.thread, cursor, evsel, + &pchain->pevent->sample, NULL, NULL, + PERF_MAX_STACK_DEPTH) != 0) { + addr_location__exit(&al); + return NULL; + } + callchain_cursor_commit(cursor); + + pchain->nr_frames = cursor->nr; + if (pchain->nr_frames > 0) { + pchain->frames = calloc(pchain->nr_frames, sizeof(*pchain->frames)); + if (!pchain->frames) { + addr_location__exit(&al); + return PyErr_NoMemory(); + } + + for (i = 0; i < pchain->nr_frames; i++) { + node = callchain_cursor_current(cursor); + pchain->frames[i].ip = node->ip; + pchain->frames[i].map = map__get(node->ms.map); + pchain->frames[i].sym = node->ms.sym; + callchain_cursor_advance(cursor); + } + } + pchain->resolved = true; + addr_location__exit(&al); + } + + if (pchain->pos >= pchain->nr_frames) + return NULL; + + pnode = PyObject_New(struct pyrf_callchain_node, &pyrf_callchain_node__type); + if (!pnode) + return NULL; + + pnode->ip = pchain->frames[pchain->pos].ip; + pnode->map = map__get(pchain->frames[pchain->pos].map); + pnode->sym = pchain->frames[pchain->pos].sym; + + pchain->pos++; + return (PyObject *)pnode; +} + +static PyTypeObject pyrf_callchain__type = { + PyVarObject_HEAD_INIT(NULL, 0) + .tp_name = "perf.callchain", + .tp_basicsize = sizeof(struct pyrf_callchain), + .tp_dealloc = (destructor)pyrf_callchain__delete, + .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, + .tp_doc = "perf callchain object.", + .tp_iter = PyObject_SelfIter, + .tp_iternext = (iternextfunc)pyrf_callchain__next, +}; + +static PyObject *pyrf_sample_event__get_callchain(PyObject *self, void *closure __maybe_unused) +{ + struct pyrf_event *pevent = (void *)self; + + if (!pevent->callchain) + Py_RETURN_NONE; + + Py_INCREF(pevent->callchain); + return pevent->callchain; +} + static PyObject* pyrf_sample_event__getattro(struct pyrf_event *pevent, PyObject *attr_name) { @@ -635,6 +813,12 @@ pyrf_sample_event__getattro(struct pyrf_event *pevent, PyObject *attr_name) } static PyGetSetDef pyrf_sample_event__getset[] = { + { + .name = "callchain", + .get = pyrf_sample_event__get_callchain, + .set = NULL, + .doc = "event callchain.", + }, { .name = "raw_buf", .get = (getter)pyrf_sample_event__get_raw_buf, @@ -803,6 +987,12 @@ static int pyrf_event__setup_types(void) err = PyType_Ready(&pyrf_context_switch_event__type); if (err < 0) goto out; + err = PyType_Ready(&pyrf_callchain_node__type); + if (err < 0) + goto out; + err = PyType_Ready(&pyrf_callchain__type); + if (err < 0) + goto out; out: return err; } @@ -822,10 +1012,12 @@ static PyTypeObject *pyrf_event__type[] = { }; static PyObject *pyrf_event__new(const union perf_event *event, struct evsel *evsel, - struct perf_session *session __maybe_unused) + struct perf_session *session) { struct pyrf_event *pevent; PyTypeObject *ptype; + struct perf_sample *sample; + struct machine *machine = session ? &session->machines.host : NULL; size_t size; int err; size_t min_size = sizeof(struct perf_event_header); @@ -883,6 +1075,7 @@ static PyObject *pyrf_event__new(const union perf_event *event, struct evsel *ev PyObject_Init((PyObject *)pevent, ptype); memcpy(&pevent->event, event, event->header.size); perf_sample__init(&pevent->sample, /*all=*/true); + pevent->callchain = NULL; pevent->al_resolved = false; addr_location__init(&pevent->al); @@ -896,6 +1089,49 @@ static PyObject *pyrf_event__new(const union perf_event *event, struct evsel *ev return PyErr_Format(PyExc_OSError, "perf: can't parse sample, err=%d", err); } + sample = &pevent->sample; + if (machine && sample->callchain) { + struct addr_location al; + struct callchain_cursor *cursor; + u64 i; + struct pyrf_callchain *pchain; + + addr_location__init(&al); + if (machine__resolve(machine, &al, sample) >= 0) { + cursor = get_tls_callchain_cursor(); + if (thread__resolve_callchain(al.thread, cursor, evsel, sample, + NULL, NULL, PERF_MAX_STACK_DEPTH) == 0) { + callchain_cursor_commit(cursor); + + pchain = PyObject_New(struct pyrf_callchain, &pyrf_callchain__type); + if (pchain) { + pchain->pevent = pevent; + Py_INCREF(pevent); + pchain->nr_frames = cursor->nr; + pchain->pos = 0; + pchain->resolved = true; + pchain->frames = calloc(pchain->nr_frames, + sizeof(*pchain->frames)); + if (pchain->frames) { + struct callchain_cursor_node *node; + + for (i = 0; i < pchain->nr_frames; i++) { + node = callchain_cursor_current(cursor); + pchain->frames[i].ip = node->ip; + pchain->frames[i].map = + map__get(node->ms.map); + pchain->frames[i].sym = node->ms.sym; + callchain_cursor_advance(cursor); + } + pevent->callchain = (PyObject *)pchain; + } else { + Py_DECREF(pchain); + } + } + } + addr_location__exit(&al); + } + } return (PyObject *)pevent; } @@ -2959,6 +3195,9 @@ static int pyrf_session__init(struct pyrf_session *psession, PyObject *args, PyO return -1; } + symbol_conf.use_callchain = true; + symbol_conf.show_kernel_path = true; + symbol_conf.inline_name = false; if (symbol__init(perf_session__env(psession->session)) < 0) { perf_session__delete(psession->session); psession->session = NULL; -- 2.54.0.545.g6539524ca2-goog