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.
Showing the changes to tools/perf/scripts/python (1 of 1 file). Show all
Unified · Split
| @@ -5,14 +5,23 @@ · 4 unmodified lines | ||
| 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) \ |
| @@ -125,6 +134,49 @@ out: · 106 unmodified lines | ||
| 125 | 134 | return Py_BuildValue("i", retval); |
| 126 | 135 | } |
| 127 | 136 | |
| 137 | +static PyObject *perf_sample_src(PyObject *obj, PyObject *args, bool get_srccode) | |
| 138 | +{ | |
| 139 | + struct scripting_context *c = get_scripting_context(args); | |
| 140 | + unsigned int line = 0; | |
| 141 | + char *srcfile = NULL; | |
| 142 | + char *srccode = NULL; | |
| 143 | + PyObject *result; | |
| 144 | + struct map *map; | |
| 145 | + int len = 0; | |
| 146 | + u64 addr; | |
| 147 | + | |
| 148 | + if (!c) | |
| 149 | + return NULL; | |
| 150 | + | |
| 151 | + map = c->al->map; | |
| 152 | + addr = c->al->addr; | |
| 153 | + | |
| 154 | + if (map && map->dso) | |
| 155 | + srcfile = get_srcline_split(map->dso, map__rip_2objdump(map, addr), &line); | |
| 156 | + | |
| 157 | + if (get_srccode) { | |
| 158 | + if (srcfile) | |
| 159 | + srccode = find_sourceline(srcfile, line, &len); | |
| 160 | + result = Py_BuildValue("(sIs#)", srcfile, line, srccode, (Py_ssize_t)len); | |
| 161 | + } else { | |
| 162 | + result = Py_BuildValue("(sI)", srcfile, line); | |
| 163 | + } | |
| 164 | + | |
| 165 | + free(srcfile); | |
| 166 | + | |
| 167 | + return result; | |
| 168 | +} | |
| 169 | + | |
| 170 | +static PyObject *perf_sample_srcline(PyObject *obj, PyObject *args) | |
| 171 | +{ | |
| 172 | + return perf_sample_src(obj, args, false); | |
| 173 | +} | |
| 174 | + | |
| 175 | +static PyObject *perf_sample_srccode(PyObject *obj, PyObject *args) | |
| 176 | +{ | |
| 177 | + return perf_sample_src(obj, args, true); | |
| 178 | +} | |
| 179 | + | |
| 128 | 180 | static PyMethodDef ContextMethods[] = { |
| 129 | 181 | { "common_pc", perf_trace_context_common_pc, METH_VARARGS, |
| 130 | 182 | "Get the common preempt count event field value."}, |
| @@ -136,6 +188,10 @@ static PyMethodDef ContextMethods[] = { · 5 unmodified lines | ||
| 136 | 188 | METH_VARARGS, "Get the machine code instruction."}, |
| 137 | 189 | { "perf_set_itrace_options", perf_set_itrace_options, |
| 138 | 190 | METH_VARARGS, "Set --itrace options."}, |
| 191 | + { "perf_sample_srcline", perf_sample_srcline, | |
| 192 | + METH_VARARGS, "Get source file name and line number."}, | |
| 193 | + { "perf_sample_srccode", perf_sample_srccode, | |
| 194 | + METH_VARARGS, "Get source file name, line number and line."}, | |
| 139 | 195 | { NULL, NULL, 0, NULL} |
| 140 | 196 | }; |
| 141 | 197 | |