[PATCH 1/2] tools: perf: add perf_input_name() helper
From: Stephen Brennan
Date: Thu Oct 01 2026 - 14:28:20 EST
The perf scripting API does not expose the name of the input file being
processed. Some scripts, like flamegraph.py, assume that the "-i" option
will be passed through, but when perf actually processes "-i", it does
not keep it on the command line for the script. (This could break some
scripts which do not expect it.)
To allow scripts to know what file they're processing, add a new helper
perf_input_name(), which returns either the filename being processed, or
None for an unnamed pipe.
Signed-off-by: Stephen Brennan <stephen.s.brennan@xxxxxxxxxx>
---
tools/perf/Documentation/perf-script-python.txt | 1 +
.../scripts/python/Perf-Trace-Util/Context.c | 16 ++++++++++++++++
2 files changed, 17 insertions(+)
diff --git a/tools/perf/Documentation/perf-script-python.txt b/tools/perf/Documentation/perf-script-python.txt
index 27a1cac6fe763..3f127024f640d 100644
--- a/tools/perf/Documentation/perf-script-python.txt
+++ b/tools/perf/Documentation/perf-script-python.txt
@@ -622,6 +622,7 @@ as perf_trace_context.perf_script_context .
common_lock_depth(context) - returns common_lock_depth for the current event
perf_sample_insn(context) - returns the machine code instruction
perf_set_itrace_options(context, itrace_options) - set --itrace options if they have not been set already
+ perf_input_name(context) - returns the input file name (or None for an unnamed pipe)
perf_sample_srcline(context) - returns source_file_name, line_number
perf_sample_srccode(context) - returns source_file_name, line_number, source_line
perf_config_get(config_name) - returns the value of the named config item, or None if unset
diff --git a/tools/perf/scripts/python/Perf-Trace-Util/Context.c b/tools/perf/scripts/python/Perf-Trace-Util/Context.c
index c19f446109831..c50c2432c1f60 100644
--- a/tools/perf/scripts/python/Perf-Trace-Util/Context.c
+++ b/tools/perf/scripts/python/Perf-Trace-Util/Context.c
@@ -20,6 +20,7 @@
#include "../../../util/map.h"
#include "../../../util/maps.h"
#include "../../../util/auxtrace.h"
+#include "../../../util/data.h"
#include "../../../util/session.h"
#include "../../../util/srcline.h"
#include "../../../util/srccode.h"
@@ -181,6 +182,19 @@ static PyObject *__perf_config_get(PyObject *obj, PyObject *args)
return Py_BuildValue("s", perf_config_get(config_name));
}
+static PyObject *perf_input_name(PyObject *obj, PyObject *args)
+{
+ struct scripting_context *c = get_scripting_context(args);
+
+ if (!c)
+ return NULL;
+
+ if (!c->session || !c->session->data || !c->session->data->path)
+ Py_RETURN_NONE;
+
+ return PyUnicode_DecodeFSDefault(c->session->data->path);
+}
+
static PyMethodDef ContextMethods[] = {
#ifdef HAVE_LIBTRACEEVENT
{ "common_pc", perf_trace_context_common_pc, METH_VARARGS,
@@ -199,6 +213,8 @@ static PyMethodDef ContextMethods[] = {
{ "perf_sample_srccode", perf_sample_srccode,
METH_VARARGS, "Get source file name, line number and line."},
{ "perf_config_get", __perf_config_get, METH_VARARGS, "Get perf config entry"},
+ { "perf_input_name", perf_input_name,
+ METH_VARARGS, "Get the input file name, or None for a pipe"},
{ NULL, NULL, 0, NULL}
};
--
2.52.0