[PATCH v1 16/58] perf python: Add syscall name/id to convert syscall number and name

From: Ian Rogers

Date: Sun Apr 19 2026 - 20:03:42 EST


Use perf's syscalltbl support to convert syscall number to name
assuming the number is for the host machine. This avoids python
libaudit support as tools/perf/scripts/python/syscall-counts.py
requires.

Signed-off-by: Ian Rogers <irogers@xxxxxxxxxx>
---
tools/perf/util/python.c | 44 ++++++++++++++++++++++++++++++++++++++++
1 file changed, 44 insertions(+)

diff --git a/tools/perf/util/python.c b/tools/perf/util/python.c
index b68668c267d8..84a186532353 100644
--- a/tools/perf/util/python.c
+++ b/tools/perf/util/python.c
@@ -13,6 +13,7 @@
#include "counts.h"
#include "data.h"
#include "debug.h"
+#include "dwarf-regs.h"
#include "event.h"
#include "evlist.h"
#include "evsel.h"
@@ -25,6 +26,7 @@
#include "session.h"
#include "strbuf.h"
#include "symbol.h"
+#include "syscalltbl.h"
#include "thread.h"
#include "thread_map.h"
#include "tool.h"
@@ -2581,6 +2583,36 @@ static int pyrf_session__setup_types(void)
return PyType_Ready(&pyrf_session__type);
}

+static PyObject *pyrf__syscall_name(PyObject *self, PyObject *args)
+{
+ const char *name;
+ int id;
+
+ if (!PyArg_ParseTuple(args, "i", &id))
+ return NULL;
+
+ name = syscalltbl__name(EM_HOST, id);
+ if (!name)
+ Py_RETURN_NONE;
+ return PyUnicode_FromString(name);
+}
+
+static PyObject *pyrf__syscall_id(PyObject *self, PyObject *args)
+{
+ const char *name;
+ int id;
+
+ if (!PyArg_ParseTuple(args, "s", &name))
+ return NULL;
+
+ id = syscalltbl__id(EM_HOST, name);
+ if (id < 0) {
+ PyErr_Format(PyExc_TypeError, "Failed to find syscall %s", name);
+ return NULL;
+ }
+ return PyLong_FromLong(id);
+}
+
static PyMethodDef perf__methods[] = {
{
.ml_name = "metrics",
@@ -2614,6 +2646,18 @@ static PyMethodDef perf__methods[] = {
.ml_flags = METH_NOARGS,
.ml_doc = PyDoc_STR("Returns a sequence of pmus.")
},
+ {
+ .ml_name = "syscall_name",
+ .ml_meth = (PyCFunction) pyrf__syscall_name,
+ .ml_flags = METH_VARARGS,
+ .ml_doc = PyDoc_STR("Turns a syscall number to a string.")
+ },
+ {
+ .ml_name = "syscall_id",
+ .ml_meth = (PyCFunction) pyrf__syscall_id,
+ .ml_flags = METH_VARARGS,
+ .ml_doc = PyDoc_STR("Turns a syscall name number to a number.")
+ },
{ .ml_name = NULL, }
};

--
2.54.0.rc1.513.gad8abe7a5a-goog