Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

perf scripting python: Add perf_sample_srcline() and perf_sample_srccode()

Add perf_sample_srcline() and perf_sample_srccode() to the
perf_trace_context module so that a script can get the srcline or srccode
information.

Signed-off-by: Adrian Hunter <adrian.hunter@intel.com>
Cc: Andi Kleen <ak@linux.intel.com>
Cc: Jiri Olsa <jolsa@redhat.com>
Link: https://lore.kernel.org/r/20210530192308.7382-11-adrian.hunter@intel.com
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>

authored by

Adrian Hunter and committed by
Arnaldo Carvalho de Melo
e79457a5 7d00540d

+56
+56
tools/perf/scripts/python/Perf-Trace-Util/Context.c
··· 5 5 * Copyright (C) 2010 Tom Zanussi <tzanussi@gmail.com> 6 6 */ 7 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 + 8 14 #include <Python.h> 9 15 #include "../../../util/trace-event.h" 10 16 #include "../../../util/event.h" 11 17 #include "../../../util/symbol.h" 12 18 #include "../../../util/thread.h" 19 + #include "../../../util/map.h" 13 20 #include "../../../util/maps.h" 14 21 #include "../../../util/auxtrace.h" 15 22 #include "../../../util/session.h" 23 + #include "../../../util/srcline.h" 24 + #include "../../../util/srccode.h" 16 25 17 26 #if PY_MAJOR_VERSION < 3 18 27 #define _PyCapsule_GetPointer(arg1, arg2) \ ··· 134 125 return Py_BuildValue("i", retval); 135 126 } 136 127 128 + static PyObject *perf_sample_src(PyObject *obj, PyObject *args, bool get_srccode) 129 + { 130 + struct scripting_context *c = get_scripting_context(args); 131 + unsigned int line = 0; 132 + char *srcfile = NULL; 133 + char *srccode = NULL; 134 + PyObject *result; 135 + struct map *map; 136 + int len = 0; 137 + u64 addr; 138 + 139 + if (!c) 140 + return NULL; 141 + 142 + map = c->al->map; 143 + addr = c->al->addr; 144 + 145 + if (map && map->dso) 146 + srcfile = get_srcline_split(map->dso, map__rip_2objdump(map, addr), &line); 147 + 148 + if (get_srccode) { 149 + if (srcfile) 150 + srccode = find_sourceline(srcfile, line, &len); 151 + result = Py_BuildValue("(sIs#)", srcfile, line, srccode, (Py_ssize_t)len); 152 + } else { 153 + result = Py_BuildValue("(sI)", srcfile, line); 154 + } 155 + 156 + free(srcfile); 157 + 158 + return result; 159 + } 160 + 161 + static PyObject *perf_sample_srcline(PyObject *obj, PyObject *args) 162 + { 163 + return perf_sample_src(obj, args, false); 164 + } 165 + 166 + static PyObject *perf_sample_srccode(PyObject *obj, PyObject *args) 167 + { 168 + return perf_sample_src(obj, args, true); 169 + } 170 + 137 171 static PyMethodDef ContextMethods[] = { 138 172 { "common_pc", perf_trace_context_common_pc, METH_VARARGS, 139 173 "Get the common preempt count event field value."}, ··· 188 136 METH_VARARGS, "Get the machine code instruction."}, 189 137 { "perf_set_itrace_options", perf_set_itrace_options, 190 138 METH_VARARGS, "Set --itrace options."}, 139 + { "perf_sample_srcline", perf_sample_srcline, 140 + METH_VARARGS, "Get source file name and line number."}, 141 + { "perf_sample_srccode", perf_sample_srccode, 142 + METH_VARARGS, "Get source file name, line number and line."}, 191 143 { NULL, NULL, 0, NULL} 192 144 }; 193 145