Linux Audio

Check our new training course

Loading...
v3.5.6
 
 1/*
 2 * Context.c.  Python interfaces for perf script.
 3 *
 4 * Copyright (C) 2010 Tom Zanussi <tzanussi@gmail.com>
 5 *
 6 *  This program is free software; you can redistribute it and/or modify
 7 *  it under the terms of the GNU General Public License as published by
 8 *  the Free Software Foundation; either version 2 of the License, or
 9 *  (at your option) any later version.
10 *
11 *  This program is distributed in the hope that it will be useful,
12 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 *  GNU General Public License for more details.
15 *
16 *  You should have received a copy of the GNU General Public License
17 *  along with this program; if not, write to the Free Software
18 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19 *
20 */
21
 
 
 
 
 
 
22#include <Python.h>
23#include "../../../perf.h"
24#include "../../../util/trace-event.h"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25
26PyMODINIT_FUNC initperf_trace_context(void);
 
 
 
 
 
 
 
27
28static PyObject *perf_trace_context_common_pc(PyObject *self, PyObject *args)
 
 
 
29{
30	static struct scripting_context *scripting_context;
31	PyObject *context;
32	int retval;
33
34	if (!PyArg_ParseTuple(args, "O", &context))
35		return NULL;
36
37	scripting_context = PyCObject_AsVoidPtr(context);
38	retval = common_pc(scripting_context);
39
40	return Py_BuildValue("i", retval);
 
 
41}
42
43static PyObject *perf_trace_context_common_flags(PyObject *self,
44						 PyObject *args)
45{
46	static struct scripting_context *scripting_context;
47	PyObject *context;
48	int retval;
49
50	if (!PyArg_ParseTuple(args, "O", &context))
51		return NULL;
52
53	scripting_context = PyCObject_AsVoidPtr(context);
54	retval = common_flags(scripting_context);
55
56	return Py_BuildValue("i", retval);
 
 
 
 
 
 
 
 
57}
58
59static PyObject *perf_trace_context_common_lock_depth(PyObject *self,
60						      PyObject *args)
61{
62	static struct scripting_context *scripting_context;
63	PyObject *context;
64	int retval;
 
 
 
 
 
 
 
 
 
65
66	if (!PyArg_ParseTuple(args, "O", &context))
67		return NULL;
68
69	scripting_context = PyCObject_AsVoidPtr(context);
70	retval = common_lock_depth(scripting_context);
71
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
72	return Py_BuildValue("i", retval);
73}
74
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
75static PyMethodDef ContextMethods[] = {
 
76	{ "common_pc", perf_trace_context_common_pc, METH_VARARGS,
77	  "Get the common preempt count event field value."},
78	{ "common_flags", perf_trace_context_common_flags, METH_VARARGS,
79	  "Get the common flags event field value."},
80	{ "common_lock_depth", perf_trace_context_common_lock_depth,
81	  METH_VARARGS,	"Get the common lock depth event field value."},
 
 
 
 
 
 
 
 
 
82	{ NULL, NULL, 0, NULL}
83};
84
 
85PyMODINIT_FUNC initperf_trace_context(void)
86{
87	(void) Py_InitModule("perf_trace_context", ContextMethods);
88}
v6.9.4
  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
 34PyMODINIT_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
 43PyMODINIT_FUNC PyInit_perf_trace_context(void);
 44#endif
 45
 46static 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
 57static struct scripting_context *get_scripting_context(PyObject *args)
 58{
 59	return get_args(args, "context", NULL);
 60}
 61
 62#ifdef HAVE_LIBTRACEEVENT
 63static 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
 73static 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
 84static 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
 96static 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 && thread__maps(c->al->thread)) {
104		struct machine *machine =  maps__machine(thread__maps(c->al->thread));
105
106		script_fetch_insn(c->sample, c->al->thread, machine);
107	}
108	if (!c->sample->insn_len)
109		Py_RETURN_NONE; /* N.B. This is a return statement */
110
111	return _PyBytes_FromStringAndSize(c->sample->insn, c->sample->insn_len);
112}
113
114static PyObject *perf_set_itrace_options(PyObject *obj, PyObject *args)
115{
116	struct scripting_context *c;
117	const char *itrace_options;
118	int retval = -1;
119	PyObject *str;
120
121	c = get_args(args, "itrace_options", &str);
122	if (!c)
123		return NULL;
124
125	if (!c->session || !c->session->itrace_synth_opts)
126		goto out;
127
128	if (c->session->itrace_synth_opts->set) {
129		retval = 1;
130		goto out;
131	}
132
133	itrace_options = _PyUnicode_AsUTF8(str);
134
135	retval = itrace_do_parse_synth_opts(c->session->itrace_synth_opts, itrace_options, 0);
136out:
137	return Py_BuildValue("i", retval);
138}
139
140static PyObject *perf_sample_src(PyObject *obj, PyObject *args, bool get_srccode)
141{
142	struct scripting_context *c = get_scripting_context(args);
143	unsigned int line = 0;
144	char *srcfile = NULL;
145	char *srccode = NULL;
146	PyObject *result;
147	struct map *map;
148	struct dso *dso;
149	int len = 0;
150	u64 addr;
151
152	if (!c)
153		return NULL;
154
155	map = c->al->map;
156	addr = c->al->addr;
157	dso = map ? map__dso(map) : NULL;
158
159	if (dso)
160		srcfile = get_srcline_split(dso, map__rip_2objdump(map, addr), &line);
161
162	if (get_srccode) {
163		if (srcfile)
164			srccode = find_sourceline(srcfile, line, &len);
165		result = Py_BuildValue("(sIs#)", srcfile, line, srccode, (Py_ssize_t)len);
166	} else {
167		result = Py_BuildValue("(sI)", srcfile, line);
168	}
169
170	free(srcfile);
171
172	return result;
173}
174
175static PyObject *perf_sample_srcline(PyObject *obj, PyObject *args)
176{
177	return perf_sample_src(obj, args, false);
178}
179
180static PyObject *perf_sample_srccode(PyObject *obj, PyObject *args)
181{
182	return perf_sample_src(obj, args, true);
183}
184
185static PyMethodDef ContextMethods[] = {
186#ifdef HAVE_LIBTRACEEVENT
187	{ "common_pc", perf_trace_context_common_pc, METH_VARARGS,
188	  "Get the common preempt count event field value."},
189	{ "common_flags", perf_trace_context_common_flags, METH_VARARGS,
190	  "Get the common flags event field value."},
191	{ "common_lock_depth", perf_trace_context_common_lock_depth,
192	  METH_VARARGS,	"Get the common lock depth event field value."},
193#endif
194	{ "perf_sample_insn", perf_sample_insn,
195	  METH_VARARGS,	"Get the machine code instruction."},
196	{ "perf_set_itrace_options", perf_set_itrace_options,
197	  METH_VARARGS,	"Set --itrace options."},
198	{ "perf_sample_srcline", perf_sample_srcline,
199	  METH_VARARGS,	"Get source file name and line number."},
200	{ "perf_sample_srccode", perf_sample_srccode,
201	  METH_VARARGS,	"Get source file name, line number and line."},
202	{ NULL, NULL, 0, NULL}
203};
204
205#if PY_MAJOR_VERSION < 3
206PyMODINIT_FUNC initperf_trace_context(void)
207{
208	(void) Py_InitModule("perf_trace_context", ContextMethods);
209}
210#else
211PyMODINIT_FUNC PyInit_perf_trace_context(void)
212{
213	static struct PyModuleDef moduledef = {
214		PyModuleDef_HEAD_INIT,
215		"perf_trace_context",	/* m_name */
216		"",			/* m_doc */
217		-1,			/* m_size */
218		ContextMethods,		/* m_methods */
219		NULL,			/* m_reload */
220		NULL,			/* m_traverse */
221		NULL,			/* m_clear */
222		NULL,			/* m_free */
223	};
224	PyObject *mod;
225
226	mod = PyModule_Create(&moduledef);
227	/* Add perf_script_context to the module so it can be imported */
228	PyObject_SetAttrString(mod, "perf_script_context", Py_None);
229
230	return mod;
231}
232#endif