xref: /linux/tools/perf/scripts/python/Perf-Trace-Util/Context.c (revision 6337bd0c91f66527741e61ecb73b9cff0d7f48f8)
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 #include <Python.h>
9 #include "../../../util/trace-event.h"
10 
11 #if PY_MAJOR_VERSION < 3
12 #define _PyCapsule_GetPointer(arg1, arg2) \
13   PyCObject_AsVoidPtr(arg1)
14 
15 PyMODINIT_FUNC initperf_trace_context(void);
16 #else
17 #define _PyCapsule_GetPointer(arg1, arg2) \
18   PyCapsule_GetPointer((arg1), (arg2))
19 
20 PyMODINIT_FUNC PyInit_perf_trace_context(void);
21 #endif
22 
23 static struct scripting_context *get_scripting_context(PyObject *args)
24 {
25 	PyObject *context;
26 
27 	if (!PyArg_ParseTuple(args, "O", &context))
28 		return NULL;
29 
30 	return _PyCapsule_GetPointer(context, NULL);
31 }
32 
33 static PyObject *perf_trace_context_common_pc(PyObject *obj, PyObject *args)
34 {
35 	struct scripting_context *c = get_scripting_context(args);
36 
37 	if (!c)
38 		return NULL;
39 
40 	return Py_BuildValue("i", common_pc(c));
41 }
42 
43 static PyObject *perf_trace_context_common_flags(PyObject *obj,
44 						 PyObject *args)
45 {
46 	struct scripting_context *c = get_scripting_context(args);
47 
48 	if (!c)
49 		return NULL;
50 
51 	return Py_BuildValue("i", common_flags(c));
52 }
53 
54 static PyObject *perf_trace_context_common_lock_depth(PyObject *obj,
55 						      PyObject *args)
56 {
57 	struct scripting_context *c = get_scripting_context(args);
58 
59 	if (!c)
60 		return NULL;
61 
62 	return Py_BuildValue("i", common_lock_depth(c));
63 }
64 
65 static PyMethodDef ContextMethods[] = {
66 	{ "common_pc", perf_trace_context_common_pc, METH_VARARGS,
67 	  "Get the common preempt count event field value."},
68 	{ "common_flags", perf_trace_context_common_flags, METH_VARARGS,
69 	  "Get the common flags event field value."},
70 	{ "common_lock_depth", perf_trace_context_common_lock_depth,
71 	  METH_VARARGS,	"Get the common lock depth event field value."},
72 	{ NULL, NULL, 0, NULL}
73 };
74 
75 #if PY_MAJOR_VERSION < 3
76 PyMODINIT_FUNC initperf_trace_context(void)
77 {
78 	(void) Py_InitModule("perf_trace_context", ContextMethods);
79 }
80 #else
81 PyMODINIT_FUNC PyInit_perf_trace_context(void)
82 {
83 	static struct PyModuleDef moduledef = {
84 		PyModuleDef_HEAD_INIT,
85 		"perf_trace_context",	/* m_name */
86 		"",			/* m_doc */
87 		-1,			/* m_size */
88 		ContextMethods,		/* m_methods */
89 		NULL,			/* m_reload */
90 		NULL,			/* m_traverse */
91 		NULL,			/* m_clear */
92 		NULL,			/* m_free */
93 	};
94 	return PyModule_Create(&moduledef);
95 }
96 #endif
97