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 <tzanussi@gmail.com> 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/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" 25 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) 33 34 PyMODINIT_FUNC initperf_trace_context(void); 35 #else 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) \ 41 PyUnicode_AsUTF8(arg) 42 43 PyMODINIT_FUNC PyInit_perf_trace_context(void); 44 #endif 45 46 static struct scripting_context *get_args(PyObject *args, const char *name, PyObject **arg2) 47 { 48 int cnt = 1 + !!arg2; 49 PyObject *context; 50 51 if (!PyArg_UnpackTuple(args, name, 1, cnt, &context, arg2)) 52 return NULL; 53 54 return _PyCapsule_GetPointer(context, NULL); 55 } 56 57 static struct scripting_context *get_scripting_context(PyObject *args) 58 { 59 return get_args(args, "context", NULL); 60 } 61 62 #ifdef HAVE_LIBTRACEEVENT 63 static PyObject *perf_trace_context_common_pc(PyObject *obj, PyObject *args) 64 { 65 struct scripting_context *c = get_scripting_context(args); 66 67 if (!c) 68 return NULL; 69 70 return Py_BuildValue("i", common_pc(c)); 71 } 72 73 static PyObject *perf_trace_context_common_flags(PyObject *obj, 74 PyObject *args) 75 { 76 struct scripting_context *c = get_scripting_context(args); 77 78 if (!c) 79 return NULL; 80 81 return Py_BuildValue("i", common_flags(c)); 82 } 83 84 static PyObject *perf_trace_context_common_lock_depth(PyObject *obj, 85 PyObject *args) 86 { 87 struct scripting_context *c = get_scripting_context(args); 88 89 if (!c) 90 return NULL; 91 92 return Py_BuildValue("i", common_lock_depth(c)); 93 } 94 #endif 95 96 static PyObject *perf_sample_insn(PyObject *obj, PyObject *args) 97 { 98 struct scripting_context *c = get_scripting_context(args); 99 100 if (!c) 101 return NULL; 102 103 if (c->sample->ip && !c->sample->insn_len && 104 c->al->thread->maps && c->al->thread->maps->machine) 105 script_fetch_insn(c->sample, c->al->thread, c->al->thread->maps->machine); 106 107 if (!c->sample->insn_len) 108 Py_RETURN_NONE; /* N.B. This is a return statement */ 109 110 return _PyBytes_FromStringAndSize(c->sample->insn, c->sample->insn_len); 111 } 112 113 static PyObject *perf_set_itrace_options(PyObject *obj, PyObject *args) 114 { 115 struct scripting_context *c; 116 const char *itrace_options; 117 int retval = -1; 118 PyObject *str; 119 120 c = get_args(args, "itrace_options", &str); 121 if (!c) 122 return NULL; 123 124 if (!c->session || !c->session->itrace_synth_opts) 125 goto out; 126 127 if (c->session->itrace_synth_opts->set) { 128 retval = 1; 129 goto out; 130 } 131 132 itrace_options = _PyUnicode_AsUTF8(str); 133 134 retval = itrace_do_parse_synth_opts(c->session->itrace_synth_opts, itrace_options, 0); 135 out: 136 return Py_BuildValue("i", retval); 137 } 138 139 static PyObject *perf_sample_src(PyObject *obj, PyObject *args, bool get_srccode) 140 { 141 struct scripting_context *c = get_scripting_context(args); 142 unsigned int line = 0; 143 char *srcfile = NULL; 144 char *srccode = NULL; 145 PyObject *result; 146 struct map *map; 147 int len = 0; 148 u64 addr; 149 150 if (!c) 151 return NULL; 152 153 map = c->al->map; 154 addr = c->al->addr; 155 156 if (map && map->dso) 157 srcfile = get_srcline_split(map->dso, map__rip_2objdump(map, addr), &line); 158 159 if (get_srccode) { 160 if (srcfile) 161 srccode = find_sourceline(srcfile, line, &len); 162 result = Py_BuildValue("(sIs#)", srcfile, line, srccode, (Py_ssize_t)len); 163 } else { 164 result = Py_BuildValue("(sI)", srcfile, line); 165 } 166 167 free(srcfile); 168 169 return result; 170 } 171 172 static PyObject *perf_sample_srcline(PyObject *obj, PyObject *args) 173 { 174 return perf_sample_src(obj, args, false); 175 } 176 177 static PyObject *perf_sample_srccode(PyObject *obj, PyObject *args) 178 { 179 return perf_sample_src(obj, args, true); 180 } 181 182 static PyMethodDef ContextMethods[] = { 183 #ifdef HAVE_LIBTRACEEVENT 184 { "common_pc", perf_trace_context_common_pc, METH_VARARGS, 185 "Get the common preempt count event field value."}, 186 { "common_flags", perf_trace_context_common_flags, METH_VARARGS, 187 "Get the common flags event field value."}, 188 { "common_lock_depth", perf_trace_context_common_lock_depth, 189 METH_VARARGS, "Get the common lock depth event field value."}, 190 #endif 191 { "perf_sample_insn", perf_sample_insn, 192 METH_VARARGS, "Get the machine code instruction."}, 193 { "perf_set_itrace_options", perf_set_itrace_options, 194 METH_VARARGS, "Set --itrace options."}, 195 { "perf_sample_srcline", perf_sample_srcline, 196 METH_VARARGS, "Get source file name and line number."}, 197 { "perf_sample_srccode", perf_sample_srccode, 198 METH_VARARGS, "Get source file name, line number and line."}, 199 { NULL, NULL, 0, NULL} 200 }; 201 202 #if PY_MAJOR_VERSION < 3 203 PyMODINIT_FUNC initperf_trace_context(void) 204 { 205 (void) Py_InitModule("perf_trace_context", ContextMethods); 206 } 207 #else 208 PyMODINIT_FUNC PyInit_perf_trace_context(void) 209 { 210 static struct PyModuleDef moduledef = { 211 PyModuleDef_HEAD_INIT, 212 "perf_trace_context", /* m_name */ 213 "", /* m_doc */ 214 -1, /* m_size */ 215 ContextMethods, /* m_methods */ 216 NULL, /* m_reload */ 217 NULL, /* m_traverse */ 218 NULL, /* m_clear */ 219 NULL, /* m_free */ 220 }; 221 PyObject *mod; 222 223 mod = PyModule_Create(&moduledef); 224 /* Add perf_script_context to the module so it can be imported */ 225 PyObject_SetAttrString(mod, "perf_script_context", Py_None); 226 227 return mod; 228 } 229 #endif 230