Context.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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. * Use Py_ssize_t for '#' formats to avoid DeprecationWarning: PY_SSIZE_T_CLEAN
  9. * will be required for '#' formats.
  10. */
  11. #define PY_SSIZE_T_CLEAN
  12. #include <Python.h>
  13. #include "../../../util/trace-event.h"
  14. #include "../../../util/event.h"
  15. #include "../../../util/symbol.h"
  16. #include "../../../util/thread.h"
  17. #include "../../../util/map.h"
  18. #include "../../../util/maps.h"
  19. #include "../../../util/auxtrace.h"
  20. #include "../../../util/session.h"
  21. #include "../../../util/srcline.h"
  22. #include "../../../util/srccode.h"
  23. #if PY_MAJOR_VERSION < 3
  24. #define _PyCapsule_GetPointer(arg1, arg2) \
  25. PyCObject_AsVoidPtr(arg1)
  26. #define _PyBytes_FromStringAndSize(arg1, arg2) \
  27. PyString_FromStringAndSize((arg1), (arg2))
  28. #define _PyUnicode_AsUTF8(arg) \
  29. PyString_AsString(arg)
  30. PyMODINIT_FUNC initperf_trace_context(void);
  31. #else
  32. #define _PyCapsule_GetPointer(arg1, arg2) \
  33. PyCapsule_GetPointer((arg1), (arg2))
  34. #define _PyBytes_FromStringAndSize(arg1, arg2) \
  35. PyBytes_FromStringAndSize((arg1), (arg2))
  36. #define _PyUnicode_AsUTF8(arg) \
  37. PyUnicode_AsUTF8(arg)
  38. PyMODINIT_FUNC PyInit_perf_trace_context(void);
  39. #endif
  40. static struct scripting_context *get_args(PyObject *args, const char *name, PyObject **arg2)
  41. {
  42. int cnt = 1 + !!arg2;
  43. PyObject *context;
  44. if (!PyArg_UnpackTuple(args, name, 1, cnt, &context, arg2))
  45. return NULL;
  46. return _PyCapsule_GetPointer(context, NULL);
  47. }
  48. static struct scripting_context *get_scripting_context(PyObject *args)
  49. {
  50. return get_args(args, "context", NULL);
  51. }
  52. #ifdef HAVE_LIBTRACEEVENT
  53. static PyObject *perf_trace_context_common_pc(PyObject *obj, PyObject *args)
  54. {
  55. struct scripting_context *c = get_scripting_context(args);
  56. if (!c)
  57. return NULL;
  58. return Py_BuildValue("i", common_pc(c));
  59. }
  60. static PyObject *perf_trace_context_common_flags(PyObject *obj,
  61. PyObject *args)
  62. {
  63. struct scripting_context *c = get_scripting_context(args);
  64. if (!c)
  65. return NULL;
  66. return Py_BuildValue("i", common_flags(c));
  67. }
  68. static PyObject *perf_trace_context_common_lock_depth(PyObject *obj,
  69. PyObject *args)
  70. {
  71. struct scripting_context *c = get_scripting_context(args);
  72. if (!c)
  73. return NULL;
  74. return Py_BuildValue("i", common_lock_depth(c));
  75. }
  76. #endif
  77. static PyObject *perf_sample_insn(PyObject *obj, PyObject *args)
  78. {
  79. struct scripting_context *c = get_scripting_context(args);
  80. if (!c)
  81. return NULL;
  82. if (c->sample->ip && !c->sample->insn_len && thread__maps(c->al->thread)) {
  83. struct machine *machine = maps__machine(thread__maps(c->al->thread));
  84. script_fetch_insn(c->sample, c->al->thread, machine);
  85. }
  86. if (!c->sample->insn_len)
  87. Py_RETURN_NONE; /* N.B. This is a return statement */
  88. return _PyBytes_FromStringAndSize(c->sample->insn, c->sample->insn_len);
  89. }
  90. static PyObject *perf_set_itrace_options(PyObject *obj, PyObject *args)
  91. {
  92. struct scripting_context *c;
  93. const char *itrace_options;
  94. int retval = -1;
  95. PyObject *str;
  96. c = get_args(args, "itrace_options", &str);
  97. if (!c)
  98. return NULL;
  99. if (!c->session || !c->session->itrace_synth_opts)
  100. goto out;
  101. if (c->session->itrace_synth_opts->set) {
  102. retval = 1;
  103. goto out;
  104. }
  105. itrace_options = _PyUnicode_AsUTF8(str);
  106. retval = itrace_do_parse_synth_opts(c->session->itrace_synth_opts, itrace_options, 0);
  107. out:
  108. return Py_BuildValue("i", retval);
  109. }
  110. static PyObject *perf_sample_src(PyObject *obj, PyObject *args, bool get_srccode)
  111. {
  112. struct scripting_context *c = get_scripting_context(args);
  113. unsigned int line = 0;
  114. char *srcfile = NULL;
  115. char *srccode = NULL;
  116. PyObject *result;
  117. struct map *map;
  118. struct dso *dso;
  119. int len = 0;
  120. u64 addr;
  121. if (!c)
  122. return NULL;
  123. map = c->al->map;
  124. addr = c->al->addr;
  125. dso = map ? map__dso(map) : NULL;
  126. if (dso)
  127. srcfile = get_srcline_split(dso, map__rip_2objdump(map, addr), &line);
  128. if (get_srccode) {
  129. if (srcfile)
  130. srccode = find_sourceline(srcfile, line, &len);
  131. result = Py_BuildValue("(sIs#)", srcfile, line, srccode, (Py_ssize_t)len);
  132. } else {
  133. result = Py_BuildValue("(sI)", srcfile, line);
  134. }
  135. free(srcfile);
  136. return result;
  137. }
  138. static PyObject *perf_sample_srcline(PyObject *obj, PyObject *args)
  139. {
  140. return perf_sample_src(obj, args, false);
  141. }
  142. static PyObject *perf_sample_srccode(PyObject *obj, PyObject *args)
  143. {
  144. return perf_sample_src(obj, args, true);
  145. }
  146. static PyMethodDef ContextMethods[] = {
  147. #ifdef HAVE_LIBTRACEEVENT
  148. { "common_pc", perf_trace_context_common_pc, METH_VARARGS,
  149. "Get the common preempt count event field value."},
  150. { "common_flags", perf_trace_context_common_flags, METH_VARARGS,
  151. "Get the common flags event field value."},
  152. { "common_lock_depth", perf_trace_context_common_lock_depth,
  153. METH_VARARGS, "Get the common lock depth event field value."},
  154. #endif
  155. { "perf_sample_insn", perf_sample_insn,
  156. METH_VARARGS, "Get the machine code instruction."},
  157. { "perf_set_itrace_options", perf_set_itrace_options,
  158. METH_VARARGS, "Set --itrace options."},
  159. { "perf_sample_srcline", perf_sample_srcline,
  160. METH_VARARGS, "Get source file name and line number."},
  161. { "perf_sample_srccode", perf_sample_srccode,
  162. METH_VARARGS, "Get source file name, line number and line."},
  163. { NULL, NULL, 0, NULL}
  164. };
  165. #if PY_MAJOR_VERSION < 3
  166. PyMODINIT_FUNC initperf_trace_context(void)
  167. {
  168. (void) Py_InitModule("perf_trace_context", ContextMethods);
  169. }
  170. #else
  171. PyMODINIT_FUNC PyInit_perf_trace_context(void)
  172. {
  173. static struct PyModuleDef moduledef = {
  174. PyModuleDef_HEAD_INIT,
  175. "perf_trace_context", /* m_name */
  176. "", /* m_doc */
  177. -1, /* m_size */
  178. ContextMethods, /* m_methods */
  179. NULL, /* m_reload */
  180. NULL, /* m_traverse */
  181. NULL, /* m_clear */
  182. NULL, /* m_free */
  183. };
  184. PyObject *mod;
  185. mod = PyModule_Create(&moduledef);
  186. /* Add perf_script_context to the module so it can be imported */
  187. PyObject_SetAttrString(mod, "perf_script_context", Py_None);
  188. return mod;
  189. }
  190. #endif