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/trace-event.h"
16 #include "../../../util/event.h"
17 #include "../../../util/symbol.h"
18 #include "../../../util/thread.h"
19 #include "../../../util/map.h"
20 #include "../../../util/maps.h"
21 #include "../../../util/auxtrace.h"
22 #include "../../../util/session.h"
23 #include "../../../util/srcline.h"
24 #include "../../../util/srccode.h"
26 #if PY_MAJOR_VERSION < 3
27 #define _PyCapsule_GetPointer(arg1, arg2) \
28 PyCObject_AsVoidPtr(arg1)
29 #define _PyBytes_FromStringAndSize(arg1, arg2) \
30 PyString_FromStringAndSize((arg1), (arg2))
31 #define _PyUnicode_AsUTF8(arg) \
32 PyString_AsString(arg)
34 PyMODINIT_FUNC initperf_trace_context(void);
36 #define _PyCapsule_GetPointer(arg1, arg2) \
37 PyCapsule_GetPointer((arg1), (arg2))
38 #define _PyBytes_FromStringAndSize(arg1, arg2) \
39 PyBytes_FromStringAndSize((arg1), (arg2))
40 #define _PyUnicode_AsUTF8(arg) \
43 PyMODINIT_FUNC PyInit_perf_trace_context(void);
46 static struct scripting_context *get_args(PyObject *args, const char *name, PyObject **arg2)
51 if (!PyArg_UnpackTuple(args, name, 1, cnt, &context, arg2))
54 return _PyCapsule_GetPointer(context, NULL);
57 static struct scripting_context *get_scripting_context(PyObject *args)
59 return get_args(args, "context", NULL);
62 #ifdef HAVE_LIBTRACEEVENT
63 static PyObject *perf_trace_context_common_pc(PyObject *obj, PyObject *args)
65 struct scripting_context *c = get_scripting_context(args);
70 return Py_BuildValue("i", common_pc(c));
73 static PyObject *perf_trace_context_common_flags(PyObject *obj,
76 struct scripting_context *c = get_scripting_context(args);
81 return Py_BuildValue("i", common_flags(c));
84 static PyObject *perf_trace_context_common_lock_depth(PyObject *obj,
87 struct scripting_context *c = get_scripting_context(args);
92 return Py_BuildValue("i", common_lock_depth(c));
96 static PyObject *perf_sample_insn(PyObject *obj, PyObject *args)
98 struct scripting_context *c = get_scripting_context(args);
103 if (c->sample->ip && !c->sample->insn_len && thread__maps(c->al->thread)) {
104 struct machine *machine = maps__machine(thread__maps(c->al->thread));
106 script_fetch_insn(c->sample, c->al->thread, machine);
108 if (!c->sample->insn_len)
109 Py_RETURN_NONE; /* N.B. This is a return statement */
111 return _PyBytes_FromStringAndSize(c->sample->insn, c->sample->insn_len);
114 static PyObject *perf_set_itrace_options(PyObject *obj, PyObject *args)
116 struct scripting_context *c;
117 const char *itrace_options;
121 c = get_args(args, "itrace_options", &str);
125 if (!c->session || !c->session->itrace_synth_opts)
128 if (c->session->itrace_synth_opts->set) {
133 itrace_options = _PyUnicode_AsUTF8(str);
135 retval = itrace_do_parse_synth_opts(c->session->itrace_synth_opts, itrace_options, 0);
137 return Py_BuildValue("i", retval);
140 static PyObject *perf_sample_src(PyObject *obj, PyObject *args, bool get_srccode)
142 struct scripting_context *c = get_scripting_context(args);
143 unsigned int line = 0;
144 char *srcfile = NULL;
145 char *srccode = NULL;
157 dso = map ? map__dso(map) : NULL;
160 srcfile = get_srcline_split(dso, map__rip_2objdump(map, addr), &line);
164 srccode = find_sourceline(srcfile, line, &len);
165 result = Py_BuildValue("(sIs#)", srcfile, line, srccode, (Py_ssize_t)len);
167 result = Py_BuildValue("(sI)", srcfile, line);
175 static PyObject *perf_sample_srcline(PyObject *obj, PyObject *args)
177 return perf_sample_src(obj, args, false);
180 static PyObject *perf_sample_srccode(PyObject *obj, PyObject *args)
182 return perf_sample_src(obj, args, true);
185 static PyMethodDef ContextMethods[] = {
186 #ifdef HAVE_LIBTRACEEVENT
187 { "common_pc", perf_trace_context_common_pc, METH_VARARGS,
188 "Get the common preempt count event field value."},
189 { "common_flags", perf_trace_context_common_flags, METH_VARARGS,
190 "Get the common flags event field value."},
191 { "common_lock_depth", perf_trace_context_common_lock_depth,
192 METH_VARARGS, "Get the common lock depth event field value."},
194 { "perf_sample_insn", perf_sample_insn,
195 METH_VARARGS, "Get the machine code instruction."},
196 { "perf_set_itrace_options", perf_set_itrace_options,
197 METH_VARARGS, "Set --itrace options."},
198 { "perf_sample_srcline", perf_sample_srcline,
199 METH_VARARGS, "Get source file name and line number."},
200 { "perf_sample_srccode", perf_sample_srccode,
201 METH_VARARGS, "Get source file name, line number and line."},
202 { NULL, NULL, 0, NULL}
205 #if PY_MAJOR_VERSION < 3
206 PyMODINIT_FUNC initperf_trace_context(void)
208 (void) Py_InitModule("perf_trace_context", ContextMethods);
211 PyMODINIT_FUNC PyInit_perf_trace_context(void)
213 static struct PyModuleDef moduledef = {
214 PyModuleDef_HEAD_INIT,
215 "perf_trace_context", /* m_name */
218 ContextMethods, /* m_methods */
220 NULL, /* m_traverse */
226 mod = PyModule_Create(&moduledef);
227 /* Add perf_script_context to the module so it can be imported */
228 PyObject_SetAttrString(mod, "perf_script_context", Py_None);