1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Context.c. Python interfaces for perf script.
4 *
5 * Copyright (C) 2010 Tom Zanussi <[email protected]>
6 */
7
8 /*
9 * Use Py_ssize_t for '#' formats to avoid DeprecationWarning: PY_SSIZE_T_CLEAN
10 * will be required for '#' formats.
11 */
12 #define PY_SSIZE_T_CLEAN
13
14 #include <Python.h>
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"
26
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) \
32 PyUnicode_AsUTF8(arg)
33
34 PyMODINIT_FUNC PyInit_perf_trace_context(void);
35
get_args(PyObject * args,const char * name,PyObject ** arg2)36 static struct scripting_context *get_args(PyObject *args, const char *name, PyObject **arg2)
37 {
38 int cnt = 1 + !!arg2;
39 PyObject *context;
40
41 if (!PyArg_UnpackTuple(args, name, 1, cnt, &context, arg2))
42 return NULL;
43
44 return _PyCapsule_GetPointer(context, NULL);
45 }
46
get_scripting_context(PyObject * args)47 static struct scripting_context *get_scripting_context(PyObject *args)
48 {
49 return get_args(args, "context", NULL);
50 }
51
52 #ifdef HAVE_LIBTRACEEVENT
perf_trace_context_common_pc(PyObject * obj,PyObject * args)53 static PyObject *perf_trace_context_common_pc(PyObject *obj, PyObject *args)
54 {
55 struct scripting_context *c = get_scripting_context(args);
56
57 if (!c)
58 return NULL;
59
60 return Py_BuildValue("i", common_pc(c));
61 }
62
perf_trace_context_common_flags(PyObject * obj,PyObject * args)63 static PyObject *perf_trace_context_common_flags(PyObject *obj,
64 PyObject *args)
65 {
66 struct scripting_context *c = get_scripting_context(args);
67
68 if (!c)
69 return NULL;
70
71 return Py_BuildValue("i", common_flags(c));
72 }
73
perf_trace_context_common_lock_depth(PyObject * obj,PyObject * args)74 static PyObject *perf_trace_context_common_lock_depth(PyObject *obj,
75 PyObject *args)
76 {
77 struct scripting_context *c = get_scripting_context(args);
78
79 if (!c)
80 return NULL;
81
82 return Py_BuildValue("i", common_lock_depth(c));
83 }
84 #endif
85
perf_sample_insn(PyObject * obj,PyObject * args)86 static PyObject *perf_sample_insn(PyObject *obj, PyObject *args)
87 {
88 struct scripting_context *c = get_scripting_context(args);
89
90 if (!c)
91 return NULL;
92
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));
95
96 script_fetch_insn(c->sample, c->al->thread, machine, /*native_arch=*/true);
97 }
98 if (!c->sample->insn_len)
99 Py_RETURN_NONE; /* N.B. This is a return statement */
100
101 return _PyBytes_FromStringAndSize(c->sample->insn, c->sample->insn_len);
102 }
103
perf_set_itrace_options(PyObject * obj,PyObject * args)104 static PyObject *perf_set_itrace_options(PyObject *obj, PyObject *args)
105 {
106 struct scripting_context *c;
107 const char *itrace_options;
108 int retval = -1;
109 PyObject *str;
110
111 c = get_args(args, "itrace_options", &str);
112 if (!c)
113 return NULL;
114
115 if (!c->session || !c->session->itrace_synth_opts)
116 goto out;
117
118 if (c->session->itrace_synth_opts->set) {
119 retval = 1;
120 goto out;
121 }
122
123 itrace_options = _PyUnicode_AsUTF8(str);
124
125 retval = itrace_do_parse_synth_opts(c->session->itrace_synth_opts, itrace_options, 0);
126 out:
127 return Py_BuildValue("i", retval);
128 }
129
perf_sample_src(PyObject * obj,PyObject * args,bool get_srccode)130 static PyObject *perf_sample_src(PyObject *obj, PyObject *args, bool get_srccode)
131 {
132 struct scripting_context *c = get_scripting_context(args);
133 unsigned int line = 0;
134 char *srcfile = NULL;
135 char *srccode = NULL;
136 PyObject *result;
137 struct map *map;
138 struct dso *dso;
139 int len = 0;
140 u64 addr;
141
142 if (!c)
143 return NULL;
144
145 map = c->al->map;
146 addr = c->al->addr;
147 dso = map ? map__dso(map) : NULL;
148
149 if (dso)
150 srcfile = get_srcline_split(dso, map__rip_2objdump(map, addr), &line);
151
152 if (get_srccode) {
153 if (srcfile)
154 srccode = find_sourceline(srcfile, line, &len);
155 result = Py_BuildValue("(sIs#)", srcfile, line, srccode, (Py_ssize_t)len);
156 } else {
157 result = Py_BuildValue("(sI)", srcfile, line);
158 }
159
160 free(srcfile);
161
162 return result;
163 }
164
perf_sample_srcline(PyObject * obj,PyObject * args)165 static PyObject *perf_sample_srcline(PyObject *obj, PyObject *args)
166 {
167 return perf_sample_src(obj, args, false);
168 }
169
perf_sample_srccode(PyObject * obj,PyObject * args)170 static PyObject *perf_sample_srccode(PyObject *obj, PyObject *args)
171 {
172 return perf_sample_src(obj, args, true);
173 }
174
__perf_config_get(PyObject * obj,PyObject * args)175 static PyObject *__perf_config_get(PyObject *obj, PyObject *args)
176 {
177 const char *config_name;
178
179 if (!PyArg_ParseTuple(args, "s", &config_name))
180 return NULL;
181 return Py_BuildValue("s", perf_config_get(config_name));
182 }
183
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."},
192 #endif
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}
203 };
204
PyInit_perf_trace_context(void)205 PyMODINIT_FUNC PyInit_perf_trace_context(void)
206 {
207 static struct PyModuleDef moduledef = {
208 PyModuleDef_HEAD_INIT,
209 "perf_trace_context", /* m_name */
210 "", /* m_doc */
211 -1, /* m_size */
212 ContextMethods, /* m_methods */
213 NULL, /* m_reload */
214 NULL, /* m_traverse */
215 NULL, /* m_clear */
216 NULL, /* m_free */
217 };
218 PyObject *mod;
219
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);
223
224 return mod;
225 }
226