1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Context.c. Python interfaces for perf script.
9 * Use Py_ssize_t for '#' formats to avoid DeprecationWarning: PY_SSIZE_T_CLEAN
10 * will be required for '#' formats.
12 #define PY_SSIZE_T_CLEAN
15 #include "../../../util/config.h"
16 #include "../../../util/trace-event.h"
17 #include "../../../util/event.h"
18 #include "../../../util/symbol.h"
19 #include "../../../util/thread.h"
20 #include "../../../util/map.h"
21 #include "../../../util/maps.h"
22 #include "../../../util/auxtrace.h"
23 #include "../../../util/session.h"
24 #include "../../../util/srcline.h"
25 #include "../../../util/srccode.h"
27 #define _PyCapsule_GetPointer(arg1, arg2) \
28 PyCapsule_GetPointer((arg1), (arg2))
29 #define _PyBytes_FromStringAndSize(arg1, arg2) \
30 PyBytes_FromStringAndSize((arg1), (arg2))
31 #define _PyUnicode_AsUTF8(arg) \
34 PyMODINIT_FUNC PyInit_perf_trace_context(void);
36 static struct scripting_context *get_args(PyObject *args, const char *name, PyObject **arg2)
41 if (!PyArg_UnpackTuple(args, name, 1, cnt, &context, arg2))
44 return _PyCapsule_GetPointer(context, NULL);
47 static struct scripting_context *get_scripting_context(PyObject *args)
49 return get_args(args, "context", NULL);
52 #ifdef HAVE_LIBTRACEEVENT
53 static PyObject *perf_trace_context_common_pc(PyObject *obj, PyObject *args)
55 struct scripting_context *c = get_scripting_context(args);
60 return Py_BuildValue("i", common_pc(c));
63 static PyObject *perf_trace_context_common_flags(PyObject *obj,
66 struct scripting_context *c = get_scripting_context(args);
71 return Py_BuildValue("i", common_flags(c));
74 static PyObject *perf_trace_context_common_lock_depth(PyObject *obj,
77 struct scripting_context *c = get_scripting_context(args);
82 return Py_BuildValue("i", common_lock_depth(c));
86 static PyObject *perf_sample_insn(PyObject *obj, PyObject *args)
88 struct scripting_context *c = get_scripting_context(args);
93 if (c->sample->ip && !c->sample->insn_len && thread__maps(c->al->thread)) {
94 struct machine *machine = maps__machine(thread__maps(c->al->thread));
96 script_fetch_insn(c->sample, c->al->thread, machine, /*native_arch=*/true);
98 if (!c->sample->insn_len)
99 Py_RETURN_NONE; /* N.B. This is a return statement */
101 return _PyBytes_FromStringAndSize(c->sample->insn, c->sample->insn_len);
104 static PyObject *perf_set_itrace_options(PyObject *obj, PyObject *args)
106 struct scripting_context *c;
107 const char *itrace_options;
111 c = get_args(args, "itrace_options", &str);
115 if (!c->session || !c->session->itrace_synth_opts)
118 if (c->session->itrace_synth_opts->set) {
123 itrace_options = _PyUnicode_AsUTF8(str);
125 retval = itrace_do_parse_synth_opts(c->session->itrace_synth_opts, itrace_options, 0);
127 return Py_BuildValue("i", retval);
130 static PyObject *perf_sample_src(PyObject *obj, PyObject *args, bool get_srccode)
132 struct scripting_context *c = get_scripting_context(args);
133 unsigned int line = 0;
134 char *srcfile = NULL;
135 char *srccode = NULL;
147 dso = map ? map__dso(map) : NULL;
150 srcfile = get_srcline_split(dso, map__rip_2objdump(map, addr), &line);
154 srccode = find_sourceline(srcfile, line, &len);
155 result = Py_BuildValue("(sIs#)", srcfile, line, srccode, (Py_ssize_t)len);
157 result = Py_BuildValue("(sI)", srcfile, line);
165 static PyObject *perf_sample_srcline(PyObject *obj, PyObject *args)
167 return perf_sample_src(obj, args, false);
170 static PyObject *perf_sample_srccode(PyObject *obj, PyObject *args)
172 return perf_sample_src(obj, args, true);
175 static PyObject *__perf_config_get(PyObject *obj, PyObject *args)
177 const char *config_name;
179 if (!PyArg_ParseTuple(args, "s", &config_name))
181 return Py_BuildValue("s", perf_config_get(config_name));
184 static PyMethodDef ContextMethods[] = {
185 #ifdef HAVE_LIBTRACEEVENT
186 { "common_pc", perf_trace_context_common_pc, METH_VARARGS,
187 "Get the common preempt count event field value."},
188 { "common_flags", perf_trace_context_common_flags, METH_VARARGS,
189 "Get the common flags event field value."},
190 { "common_lock_depth", perf_trace_context_common_lock_depth,
191 METH_VARARGS, "Get the common lock depth event field value."},
193 { "perf_sample_insn", perf_sample_insn,
194 METH_VARARGS, "Get the machine code instruction."},
195 { "perf_set_itrace_options", perf_set_itrace_options,
196 METH_VARARGS, "Set --itrace options."},
197 { "perf_sample_srcline", perf_sample_srcline,
198 METH_VARARGS, "Get source file name and line number."},
199 { "perf_sample_srccode", perf_sample_srccode,
200 METH_VARARGS, "Get source file name, line number and line."},
201 { "perf_config_get", __perf_config_get, METH_VARARGS, "Get perf config entry"},
202 { NULL, NULL, 0, NULL}
205 PyMODINIT_FUNC PyInit_perf_trace_context(void)
207 static struct PyModuleDef moduledef = {
208 PyModuleDef_HEAD_INIT,
209 "perf_trace_context", /* m_name */
212 ContextMethods, /* m_methods */
214 NULL, /* m_traverse */
220 mod = PyModule_Create(&moduledef);
221 /* Add perf_script_context to the module so it can be imported */
222 PyObject_SetAttrString(mod, "perf_script_context", Py_None);