History

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.

Adrian Hunter committed 5 years ago

Commit e79457a1 parent 7d005401 file · +56 −0Browse files

Showing the changes to tools/perf/scripts/python (1 of 1 file). Show all

Unified · Split

tools/perf/scripts/python/Perf-Trace-Util/Context.c+56−0
@@ -5,14 +5,23 @@ · 4 unmodified lines
55 * Copyright (C) 2010 Tom Zanussi <tzanussi@gmail.com>
66 */
77
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+
814 #include <Python.h>
915 #include "../../../util/trace-event.h"
1016 #include "../../../util/event.h"
1117 #include "../../../util/symbol.h"
1218 #include "../../../util/thread.h"
19+#include "../../../util/map.h"
1320 #include "../../../util/maps.h"
1421 #include "../../../util/auxtrace.h"
1522 #include "../../../util/session.h"
23+#include "../../../util/srcline.h"
24+#include "../../../util/srccode.h"
1625
1726 #if PY_MAJOR_VERSION < 3
1827 #define _PyCapsule_GetPointer(arg1, arg2) \
@@ -125,6 +134,49 @@ out: · 106 unmodified lines
125134 return Py_BuildValue("i", retval);
126135 }
127136
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+
128180 static PyMethodDef ContextMethods[] = {
129181 { "common_pc", perf_trace_context_common_pc, METH_VARARGS,
130182 "Get the common preempt count event field value."},
@@ -136,6 +188,10 @@ static PyMethodDef ContextMethods[] = { · 5 unmodified lines
136188 METH_VARARGS, "Get the machine code instruction."},
137189 { "perf_set_itrace_options", perf_set_itrace_options,
138190 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."},
139195 { NULL, NULL, 0, NULL}
140196 };
141197