^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) * trace-event-python. Feed trace events to an embedded Python interpreter.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * Copyright (C) 2010 Tom Zanussi <tzanussi@gmail.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * This program is free software; you can redistribute it and/or modify
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * it under the terms of the GNU General Public License as published by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) * the Free Software Foundation; either version 2 of the License, or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) * (at your option) any later version.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) * This program is distributed in the hope that it will be useful,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) * but WITHOUT ANY WARRANTY; without even the implied warranty of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) * GNU General Public License for more details.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) * You should have received a copy of the GNU General Public License
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) * along with this program; if not, write to the Free Software
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #include <Python.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #include <inttypes.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) #include <stdio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) #include <stdlib.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) #include <string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) #include <stdbool.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) #include <errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) #include <linux/bitmap.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) #include <linux/compiler.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) #include <linux/time64.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) #include "../build-id.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) #include "../counts.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) #include "../debug.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) #include "../dso.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) #include "../callchain.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) #include "../evsel.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) #include "../event.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) #include "../thread.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) #include "../comm.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) #include "../machine.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) #include "../db-export.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) #include "../thread-stack.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) #include "../trace-event.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) #include "../call-path.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) #include "map.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) #include "symbol.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) #include "thread_map.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) #include "print_binary.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) #include "stat.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) #include "mem-events.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) #if PY_MAJOR_VERSION < 3
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) #define _PyUnicode_FromString(arg) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) PyString_FromString(arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) #define _PyUnicode_FromStringAndSize(arg1, arg2) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) PyString_FromStringAndSize((arg1), (arg2))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) #define _PyBytes_FromStringAndSize(arg1, arg2) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) PyString_FromStringAndSize((arg1), (arg2))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) #define _PyLong_FromLong(arg) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) PyInt_FromLong(arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) #define _PyLong_AsLong(arg) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) PyInt_AsLong(arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) #define _PyCapsule_New(arg1, arg2, arg3) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) PyCObject_FromVoidPtr((arg1), (arg2))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) PyMODINIT_FUNC initperf_trace_context(void);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) #define _PyUnicode_FromString(arg) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) PyUnicode_FromString(arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) #define _PyUnicode_FromStringAndSize(arg1, arg2) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) PyUnicode_FromStringAndSize((arg1), (arg2))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) #define _PyBytes_FromStringAndSize(arg1, arg2) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) PyBytes_FromStringAndSize((arg1), (arg2))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) #define _PyLong_FromLong(arg) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) PyLong_FromLong(arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) #define _PyLong_AsLong(arg) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) PyLong_AsLong(arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) #define _PyCapsule_New(arg1, arg2, arg3) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) PyCapsule_New((arg1), (arg2), (arg3))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) PyMODINIT_FUNC PyInit_perf_trace_context(void);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) #define TRACE_EVENT_TYPE_MAX \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) ((1 << (sizeof(unsigned short) * 8)) - 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) static DECLARE_BITMAP(events_defined, TRACE_EVENT_TYPE_MAX);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) #define MAX_FIELDS 64
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) #define N_COMMON_FIELDS 7
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) extern struct scripting_context *scripting_context;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) static char *cur_field_name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) static int zero_flag_atom;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) static PyObject *main_module, *main_dict;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) struct tables {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) struct db_export dbe;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) PyObject *evsel_handler;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) PyObject *machine_handler;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) PyObject *thread_handler;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) PyObject *comm_handler;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) PyObject *comm_thread_handler;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) PyObject *dso_handler;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) PyObject *symbol_handler;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) PyObject *branch_type_handler;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) PyObject *sample_handler;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) PyObject *call_path_handler;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) PyObject *call_return_handler;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) PyObject *synth_handler;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) PyObject *context_switch_handler;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) bool db_export_mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) static struct tables tables_global;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) static void handler_call_die(const char *handler_name) __noreturn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) static void handler_call_die(const char *handler_name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) PyErr_Print();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) Py_FatalError("problem in Python trace event handler");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) // Py_FatalError does not return
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) // but we have to make the compiler happy
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) abort();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) * Insert val into into the dictionary and decrement the reference counter.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) * This is necessary for dictionaries since PyDict_SetItemString() does not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) * steal a reference, as opposed to PyTuple_SetItem().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) static void pydict_set_item_string_decref(PyObject *dict, const char *key, PyObject *val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) PyDict_SetItemString(dict, key, val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) Py_DECREF(val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) static PyObject *get_handler(const char *handler_name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) PyObject *handler;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) handler = PyDict_GetItemString(main_dict, handler_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) if (handler && !PyCallable_Check(handler))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) return handler;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) static int get_argument_count(PyObject *handler)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) int arg_count = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) * The attribute for the code object is func_code in Python 2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) * whereas it is __code__ in Python 3.0+.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) PyObject *code_obj = PyObject_GetAttrString(handler,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) "func_code");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) if (PyErr_Occurred()) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) PyErr_Clear();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) code_obj = PyObject_GetAttrString(handler,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) "__code__");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) PyErr_Clear();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) if (code_obj) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) PyObject *arg_count_obj = PyObject_GetAttrString(code_obj,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) "co_argcount");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) if (arg_count_obj) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) arg_count = (int) _PyLong_AsLong(arg_count_obj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) Py_DECREF(arg_count_obj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) Py_DECREF(code_obj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) return arg_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) static void call_object(PyObject *handler, PyObject *args, const char *die_msg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) PyObject *retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) retval = PyObject_CallObject(handler, args);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) if (retval == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) handler_call_die(die_msg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) Py_DECREF(retval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) static void try_call_object(const char *handler_name, PyObject *args)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) PyObject *handler;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) handler = get_handler(handler_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) if (handler)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) call_object(handler, args, handler_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) static void define_value(enum tep_print_arg_type field_type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) const char *ev_name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) const char *field_name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) const char *field_value,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) const char *field_str)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) const char *handler_name = "define_flag_value";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) PyObject *t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) unsigned long long value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) unsigned n = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) if (field_type == TEP_PRINT_SYMBOL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) handler_name = "define_symbolic_value";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) t = PyTuple_New(4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) if (!t)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) Py_FatalError("couldn't create Python tuple");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) value = eval_flag(field_value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) PyTuple_SetItem(t, n++, _PyUnicode_FromString(ev_name));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) PyTuple_SetItem(t, n++, _PyUnicode_FromString(field_name));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) PyTuple_SetItem(t, n++, _PyLong_FromLong(value));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) PyTuple_SetItem(t, n++, _PyUnicode_FromString(field_str));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) try_call_object(handler_name, t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) Py_DECREF(t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) static void define_values(enum tep_print_arg_type field_type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) struct tep_print_flag_sym *field,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) const char *ev_name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) const char *field_name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) define_value(field_type, ev_name, field_name, field->value,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) field->str);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) if (field->next)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) define_values(field_type, field->next, ev_name, field_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) static void define_field(enum tep_print_arg_type field_type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) const char *ev_name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) const char *field_name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) const char *delim)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) const char *handler_name = "define_flag_field";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) PyObject *t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) unsigned n = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) if (field_type == TEP_PRINT_SYMBOL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) handler_name = "define_symbolic_field";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) if (field_type == TEP_PRINT_FLAGS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) t = PyTuple_New(3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) t = PyTuple_New(2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) if (!t)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) Py_FatalError("couldn't create Python tuple");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) PyTuple_SetItem(t, n++, _PyUnicode_FromString(ev_name));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) PyTuple_SetItem(t, n++, _PyUnicode_FromString(field_name));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) if (field_type == TEP_PRINT_FLAGS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) PyTuple_SetItem(t, n++, _PyUnicode_FromString(delim));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) try_call_object(handler_name, t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) Py_DECREF(t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) static void define_event_symbols(struct tep_event *event,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) const char *ev_name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) struct tep_print_arg *args)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) if (args == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) switch (args->type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) case TEP_PRINT_NULL:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) case TEP_PRINT_ATOM:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) define_value(TEP_PRINT_FLAGS, ev_name, cur_field_name, "0",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) args->atom.atom);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) zero_flag_atom = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) case TEP_PRINT_FIELD:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) free(cur_field_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) cur_field_name = strdup(args->field.name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) case TEP_PRINT_FLAGS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) define_event_symbols(event, ev_name, args->flags.field);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) define_field(TEP_PRINT_FLAGS, ev_name, cur_field_name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) args->flags.delim);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) define_values(TEP_PRINT_FLAGS, args->flags.flags, ev_name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) cur_field_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) case TEP_PRINT_SYMBOL:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) define_event_symbols(event, ev_name, args->symbol.field);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) define_field(TEP_PRINT_SYMBOL, ev_name, cur_field_name, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) define_values(TEP_PRINT_SYMBOL, args->symbol.symbols, ev_name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) cur_field_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) case TEP_PRINT_HEX:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) case TEP_PRINT_HEX_STR:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) define_event_symbols(event, ev_name, args->hex.field);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) define_event_symbols(event, ev_name, args->hex.size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) case TEP_PRINT_INT_ARRAY:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) define_event_symbols(event, ev_name, args->int_array.field);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) define_event_symbols(event, ev_name, args->int_array.count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) define_event_symbols(event, ev_name, args->int_array.el_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) case TEP_PRINT_STRING:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) case TEP_PRINT_TYPE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) define_event_symbols(event, ev_name, args->typecast.item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) case TEP_PRINT_OP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) if (strcmp(args->op.op, ":") == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) zero_flag_atom = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) define_event_symbols(event, ev_name, args->op.left);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) define_event_symbols(event, ev_name, args->op.right);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) /* gcc warns for these? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) case TEP_PRINT_BSTRING:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) case TEP_PRINT_DYNAMIC_ARRAY:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) case TEP_PRINT_DYNAMIC_ARRAY_LEN:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) case TEP_PRINT_FUNC:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) case TEP_PRINT_BITMASK:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) /* we should warn... */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) if (args->next)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) define_event_symbols(event, ev_name, args->next);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) static PyObject *get_field_numeric_entry(struct tep_event *event,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) struct tep_format_field *field, void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) bool is_array = field->flags & TEP_FIELD_IS_ARRAY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) PyObject *obj = NULL, *list = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) unsigned long long val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) unsigned int item_size, n_items, i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) if (is_array) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) list = PyList_New(field->arraylen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) item_size = field->size / field->arraylen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) n_items = field->arraylen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) item_size = field->size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) n_items = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) for (i = 0; i < n_items; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) val = read_size(event, data + field->offset + i * item_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) item_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) if (field->flags & TEP_FIELD_IS_SIGNED) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) if ((long long)val >= LONG_MIN &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) (long long)val <= LONG_MAX)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) obj = _PyLong_FromLong(val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) obj = PyLong_FromLongLong(val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) if (val <= LONG_MAX)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) obj = _PyLong_FromLong(val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) obj = PyLong_FromUnsignedLongLong(val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) if (is_array)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) PyList_SET_ITEM(list, i, obj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) if (is_array)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) obj = list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) return obj;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) static const char *get_dsoname(struct map *map)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) const char *dsoname = "[unknown]";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) if (map && map->dso) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) if (symbol_conf.show_kernel_path && map->dso->long_name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) dsoname = map->dso->long_name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) dsoname = map->dso->name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) return dsoname;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) static PyObject *python_process_callchain(struct perf_sample *sample,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) struct evsel *evsel,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) struct addr_location *al)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) PyObject *pylist;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) pylist = PyList_New(0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) if (!pylist)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) Py_FatalError("couldn't create Python list");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) if (!symbol_conf.use_callchain || !sample->callchain)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) goto exit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) if (thread__resolve_callchain(al->thread, &callchain_cursor, evsel,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) sample, NULL, NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) scripting_max_stack) != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) pr_err("Failed to resolve callchain. Skipping\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) goto exit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) callchain_cursor_commit(&callchain_cursor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) while (1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) PyObject *pyelem;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) struct callchain_cursor_node *node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) node = callchain_cursor_current(&callchain_cursor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) if (!node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) pyelem = PyDict_New();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) if (!pyelem)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) Py_FatalError("couldn't create Python dictionary");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) pydict_set_item_string_decref(pyelem, "ip",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) PyLong_FromUnsignedLongLong(node->ip));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) if (node->ms.sym) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) PyObject *pysym = PyDict_New();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) if (!pysym)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) Py_FatalError("couldn't create Python dictionary");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) pydict_set_item_string_decref(pysym, "start",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) PyLong_FromUnsignedLongLong(node->ms.sym->start));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) pydict_set_item_string_decref(pysym, "end",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) PyLong_FromUnsignedLongLong(node->ms.sym->end));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) pydict_set_item_string_decref(pysym, "binding",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) _PyLong_FromLong(node->ms.sym->binding));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) pydict_set_item_string_decref(pysym, "name",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) _PyUnicode_FromStringAndSize(node->ms.sym->name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) node->ms.sym->namelen));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) pydict_set_item_string_decref(pyelem, "sym", pysym);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) if (node->ms.map) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) const char *dsoname = get_dsoname(node->ms.map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) pydict_set_item_string_decref(pyelem, "dso",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) _PyUnicode_FromString(dsoname));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) callchain_cursor_advance(&callchain_cursor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) PyList_Append(pylist, pyelem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) Py_DECREF(pyelem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) exit:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) return pylist;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) static PyObject *python_process_brstack(struct perf_sample *sample,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) struct thread *thread)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) struct branch_stack *br = sample->branch_stack;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) struct branch_entry *entries = perf_sample__branch_entries(sample);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) PyObject *pylist;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) u64 i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) pylist = PyList_New(0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) if (!pylist)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) Py_FatalError("couldn't create Python list");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) if (!(br && br->nr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) goto exit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) for (i = 0; i < br->nr; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) PyObject *pyelem;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) struct addr_location al;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) const char *dsoname;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) pyelem = PyDict_New();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) if (!pyelem)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) Py_FatalError("couldn't create Python dictionary");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) pydict_set_item_string_decref(pyelem, "from",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) PyLong_FromUnsignedLongLong(entries[i].from));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) pydict_set_item_string_decref(pyelem, "to",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) PyLong_FromUnsignedLongLong(entries[i].to));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) pydict_set_item_string_decref(pyelem, "mispred",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) PyBool_FromLong(entries[i].flags.mispred));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) pydict_set_item_string_decref(pyelem, "predicted",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) PyBool_FromLong(entries[i].flags.predicted));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) pydict_set_item_string_decref(pyelem, "in_tx",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) PyBool_FromLong(entries[i].flags.in_tx));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) pydict_set_item_string_decref(pyelem, "abort",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) PyBool_FromLong(entries[i].flags.abort));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) pydict_set_item_string_decref(pyelem, "cycles",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) PyLong_FromUnsignedLongLong(entries[i].flags.cycles));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) thread__find_map_fb(thread, sample->cpumode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) entries[i].from, &al);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) dsoname = get_dsoname(al.map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) pydict_set_item_string_decref(pyelem, "from_dsoname",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) _PyUnicode_FromString(dsoname));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) thread__find_map_fb(thread, sample->cpumode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) entries[i].to, &al);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) dsoname = get_dsoname(al.map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) pydict_set_item_string_decref(pyelem, "to_dsoname",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) _PyUnicode_FromString(dsoname));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) PyList_Append(pylist, pyelem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) Py_DECREF(pyelem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) exit:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) return pylist;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) static unsigned long get_offset(struct symbol *sym, struct addr_location *al)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) unsigned long offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) if (al->addr < sym->end)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) offset = al->addr - sym->start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) offset = al->addr - al->map->start - sym->start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) return offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) static int get_symoff(struct symbol *sym, struct addr_location *al,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) bool print_off, char *bf, int size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) unsigned long offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) if (!sym || !sym->name[0])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) return scnprintf(bf, size, "%s", "[unknown]");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) if (!print_off)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) return scnprintf(bf, size, "%s", sym->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) offset = get_offset(sym, al);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) return scnprintf(bf, size, "%s+0x%x", sym->name, offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) static int get_br_mspred(struct branch_flags *flags, char *bf, int size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) if (!flags->mispred && !flags->predicted)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) return scnprintf(bf, size, "%s", "-");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) if (flags->mispred)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) return scnprintf(bf, size, "%s", "M");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) return scnprintf(bf, size, "%s", "P");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) static PyObject *python_process_brstacksym(struct perf_sample *sample,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) struct thread *thread)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) struct branch_stack *br = sample->branch_stack;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) struct branch_entry *entries = perf_sample__branch_entries(sample);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) PyObject *pylist;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) u64 i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) char bf[512];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) struct addr_location al;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) pylist = PyList_New(0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) if (!pylist)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) Py_FatalError("couldn't create Python list");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) if (!(br && br->nr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) goto exit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) for (i = 0; i < br->nr; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) PyObject *pyelem;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) pyelem = PyDict_New();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) if (!pyelem)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) Py_FatalError("couldn't create Python dictionary");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) thread__find_symbol_fb(thread, sample->cpumode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) entries[i].from, &al);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) get_symoff(al.sym, &al, true, bf, sizeof(bf));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) pydict_set_item_string_decref(pyelem, "from",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) _PyUnicode_FromString(bf));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) thread__find_symbol_fb(thread, sample->cpumode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) entries[i].to, &al);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) get_symoff(al.sym, &al, true, bf, sizeof(bf));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) pydict_set_item_string_decref(pyelem, "to",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) _PyUnicode_FromString(bf));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) get_br_mspred(&entries[i].flags, bf, sizeof(bf));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) pydict_set_item_string_decref(pyelem, "pred",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) _PyUnicode_FromString(bf));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) if (entries[i].flags.in_tx) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) pydict_set_item_string_decref(pyelem, "in_tx",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) _PyUnicode_FromString("X"));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) pydict_set_item_string_decref(pyelem, "in_tx",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) _PyUnicode_FromString("-"));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) if (entries[i].flags.abort) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) pydict_set_item_string_decref(pyelem, "abort",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) _PyUnicode_FromString("A"));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) pydict_set_item_string_decref(pyelem, "abort",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) _PyUnicode_FromString("-"));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) PyList_Append(pylist, pyelem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) Py_DECREF(pyelem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) exit:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) return pylist;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) static PyObject *get_sample_value_as_tuple(struct sample_read_value *value)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) PyObject *t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) t = PyTuple_New(2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) if (!t)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) Py_FatalError("couldn't create Python tuple");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) PyTuple_SetItem(t, 0, PyLong_FromUnsignedLongLong(value->id));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) PyTuple_SetItem(t, 1, PyLong_FromUnsignedLongLong(value->value));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) return t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) static void set_sample_read_in_dict(PyObject *dict_sample,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) struct perf_sample *sample,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) struct evsel *evsel)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) u64 read_format = evsel->core.attr.read_format;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) PyObject *values;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) unsigned int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) pydict_set_item_string_decref(dict_sample, "time_enabled",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) PyLong_FromUnsignedLongLong(sample->read.time_enabled));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) pydict_set_item_string_decref(dict_sample, "time_running",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) PyLong_FromUnsignedLongLong(sample->read.time_running));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) if (read_format & PERF_FORMAT_GROUP)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) values = PyList_New(sample->read.group.nr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) values = PyList_New(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) if (!values)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) Py_FatalError("couldn't create Python list");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) if (read_format & PERF_FORMAT_GROUP) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) for (i = 0; i < sample->read.group.nr; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) PyObject *t = get_sample_value_as_tuple(&sample->read.group.values[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666) PyList_SET_ITEM(values, i, t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669) PyObject *t = get_sample_value_as_tuple(&sample->read.one);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) PyList_SET_ITEM(values, 0, t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672) pydict_set_item_string_decref(dict_sample, "values", values);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) static void set_sample_datasrc_in_dict(PyObject *dict,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676) struct perf_sample *sample)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678) struct mem_info mi = { .data_src.val = sample->data_src };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679) char decode[100];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) pydict_set_item_string_decref(dict, "datasrc",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682) PyLong_FromUnsignedLongLong(sample->data_src));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684) perf_script__meminfo_scnprintf(decode, 100, &mi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686) pydict_set_item_string_decref(dict, "datasrc_decode",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687) _PyUnicode_FromString(decode));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690) static int regs_map(struct regs_dump *regs, uint64_t mask, char *bf, int size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692) unsigned int i = 0, r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693) int printed = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695) bf[0] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697) if (!regs || !regs->regs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700) for_each_set_bit(r, (unsigned long *) &mask, sizeof(mask) * 8) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701) u64 val = regs->regs[i++];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703) printed += scnprintf(bf + printed, size - printed,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704) "%5s:0x%" PRIx64 " ",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705) perf_reg_name(r), val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708) return printed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711) static void set_regs_in_dict(PyObject *dict,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712) struct perf_sample *sample,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713) struct evsel *evsel)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715) struct perf_event_attr *attr = &evsel->core.attr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716) char bf[512];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718) regs_map(&sample->intr_regs, attr->sample_regs_intr, bf, sizeof(bf));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720) pydict_set_item_string_decref(dict, "iregs",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721) _PyUnicode_FromString(bf));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723) regs_map(&sample->user_regs, attr->sample_regs_user, bf, sizeof(bf));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 725) pydict_set_item_string_decref(dict, "uregs",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 726) _PyUnicode_FromString(bf));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 727) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729) static PyObject *get_perf_sample_dict(struct perf_sample *sample,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 730) struct evsel *evsel,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 731) struct addr_location *al,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 732) PyObject *callchain)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 733) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 734) PyObject *dict, *dict_sample, *brstack, *brstacksym;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 735)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 736) dict = PyDict_New();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 737) if (!dict)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 738) Py_FatalError("couldn't create Python dictionary");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 739)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 740) dict_sample = PyDict_New();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 741) if (!dict_sample)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 742) Py_FatalError("couldn't create Python dictionary");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 743)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 744) pydict_set_item_string_decref(dict, "ev_name", _PyUnicode_FromString(evsel__name(evsel)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 745) pydict_set_item_string_decref(dict, "attr", _PyBytes_FromStringAndSize((const char *)&evsel->core.attr, sizeof(evsel->core.attr)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 746)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 747) pydict_set_item_string_decref(dict_sample, "pid",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 748) _PyLong_FromLong(sample->pid));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 749) pydict_set_item_string_decref(dict_sample, "tid",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 750) _PyLong_FromLong(sample->tid));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 751) pydict_set_item_string_decref(dict_sample, "cpu",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 752) _PyLong_FromLong(sample->cpu));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 753) pydict_set_item_string_decref(dict_sample, "ip",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 754) PyLong_FromUnsignedLongLong(sample->ip));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 755) pydict_set_item_string_decref(dict_sample, "time",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 756) PyLong_FromUnsignedLongLong(sample->time));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 757) pydict_set_item_string_decref(dict_sample, "period",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 758) PyLong_FromUnsignedLongLong(sample->period));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 759) pydict_set_item_string_decref(dict_sample, "phys_addr",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 760) PyLong_FromUnsignedLongLong(sample->phys_addr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 761) pydict_set_item_string_decref(dict_sample, "addr",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 762) PyLong_FromUnsignedLongLong(sample->addr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 763) set_sample_read_in_dict(dict_sample, sample, evsel);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 764) pydict_set_item_string_decref(dict_sample, "weight",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 765) PyLong_FromUnsignedLongLong(sample->weight));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 766) pydict_set_item_string_decref(dict_sample, "transaction",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 767) PyLong_FromUnsignedLongLong(sample->transaction));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 768) set_sample_datasrc_in_dict(dict_sample, sample);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 769) pydict_set_item_string_decref(dict, "sample", dict_sample);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 770)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 771) pydict_set_item_string_decref(dict, "raw_buf", _PyBytes_FromStringAndSize(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 772) (const char *)sample->raw_data, sample->raw_size));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 773) pydict_set_item_string_decref(dict, "comm",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 774) _PyUnicode_FromString(thread__comm_str(al->thread)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 775) if (al->map) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 776) pydict_set_item_string_decref(dict, "dso",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 777) _PyUnicode_FromString(al->map->dso->name));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 778) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 779) if (al->sym) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 780) pydict_set_item_string_decref(dict, "symbol",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 781) _PyUnicode_FromString(al->sym->name));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 782) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 783)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 784) pydict_set_item_string_decref(dict, "callchain", callchain);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 785)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 786) brstack = python_process_brstack(sample, al->thread);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 787) pydict_set_item_string_decref(dict, "brstack", brstack);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 788)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 789) brstacksym = python_process_brstacksym(sample, al->thread);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 790) pydict_set_item_string_decref(dict, "brstacksym", brstacksym);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 791)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 792) set_regs_in_dict(dict, sample, evsel);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 793)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 794) return dict;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 795) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 796)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 797) static void python_process_tracepoint(struct perf_sample *sample,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 798) struct evsel *evsel,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 799) struct addr_location *al)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 800) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 801) struct tep_event *event = evsel->tp_format;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 802) PyObject *handler, *context, *t, *obj = NULL, *callchain;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 803) PyObject *dict = NULL, *all_entries_dict = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 804) static char handler_name[256];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 805) struct tep_format_field *field;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 806) unsigned long s, ns;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 807) unsigned n = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 808) int pid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 809) int cpu = sample->cpu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 810) void *data = sample->raw_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 811) unsigned long long nsecs = sample->time;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 812) const char *comm = thread__comm_str(al->thread);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 813) const char *default_handler_name = "trace_unhandled";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 814)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 815) if (!event) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 816) snprintf(handler_name, sizeof(handler_name),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 817) "ug! no event found for type %" PRIu64, (u64)evsel->core.attr.config);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 818) Py_FatalError(handler_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 819) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 820)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 821) pid = raw_field_value(event, "common_pid", data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 822)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 823) sprintf(handler_name, "%s__%s", event->system, event->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 824)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 825) if (!test_and_set_bit(event->id, events_defined))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 826) define_event_symbols(event, handler_name, event->print_fmt.args);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 827)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 828) handler = get_handler(handler_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 829) if (!handler) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 830) handler = get_handler(default_handler_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 831) if (!handler)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 832) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 833) dict = PyDict_New();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 834) if (!dict)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 835) Py_FatalError("couldn't create Python dict");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 836) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 837)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 838) t = PyTuple_New(MAX_FIELDS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 839) if (!t)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 840) Py_FatalError("couldn't create Python tuple");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 841)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 842)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 843) s = nsecs / NSEC_PER_SEC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 844) ns = nsecs - s * NSEC_PER_SEC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 845)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 846) scripting_context->event_data = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 847) scripting_context->pevent = evsel->tp_format->tep;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 848)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 849) context = _PyCapsule_New(scripting_context, NULL, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 850)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 851) PyTuple_SetItem(t, n++, _PyUnicode_FromString(handler_name));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 852) PyTuple_SetItem(t, n++, context);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 853)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 854) /* ip unwinding */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 855) callchain = python_process_callchain(sample, evsel, al);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 856) /* Need an additional reference for the perf_sample dict */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 857) Py_INCREF(callchain);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 858)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 859) if (!dict) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 860) PyTuple_SetItem(t, n++, _PyLong_FromLong(cpu));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 861) PyTuple_SetItem(t, n++, _PyLong_FromLong(s));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 862) PyTuple_SetItem(t, n++, _PyLong_FromLong(ns));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 863) PyTuple_SetItem(t, n++, _PyLong_FromLong(pid));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 864) PyTuple_SetItem(t, n++, _PyUnicode_FromString(comm));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 865) PyTuple_SetItem(t, n++, callchain);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 866) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 867) pydict_set_item_string_decref(dict, "common_cpu", _PyLong_FromLong(cpu));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 868) pydict_set_item_string_decref(dict, "common_s", _PyLong_FromLong(s));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 869) pydict_set_item_string_decref(dict, "common_ns", _PyLong_FromLong(ns));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 870) pydict_set_item_string_decref(dict, "common_pid", _PyLong_FromLong(pid));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 871) pydict_set_item_string_decref(dict, "common_comm", _PyUnicode_FromString(comm));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 872) pydict_set_item_string_decref(dict, "common_callchain", callchain);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 873) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 874) for (field = event->format.fields; field; field = field->next) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 875) unsigned int offset, len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 876) unsigned long long val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 877)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 878) if (field->flags & TEP_FIELD_IS_ARRAY) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 879) offset = field->offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 880) len = field->size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 881) if (field->flags & TEP_FIELD_IS_DYNAMIC) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 882) val = tep_read_number(scripting_context->pevent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 883) data + offset, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 884) offset = val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 885) len = offset >> 16;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 886) offset &= 0xffff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 887) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 888) if (field->flags & TEP_FIELD_IS_STRING &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 889) is_printable_array(data + offset, len)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 890) obj = _PyUnicode_FromString((char *) data + offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 891) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 892) obj = PyByteArray_FromStringAndSize((const char *) data + offset, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 893) field->flags &= ~TEP_FIELD_IS_STRING;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 894) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 895) } else { /* FIELD_IS_NUMERIC */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 896) obj = get_field_numeric_entry(event, field, data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 897) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 898) if (!dict)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 899) PyTuple_SetItem(t, n++, obj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 900) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 901) pydict_set_item_string_decref(dict, field->name, obj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 902)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 903) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 904)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 905) if (dict)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 906) PyTuple_SetItem(t, n++, dict);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 907)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 908) if (get_argument_count(handler) == (int) n + 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 909) all_entries_dict = get_perf_sample_dict(sample, evsel, al,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 910) callchain);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 911) PyTuple_SetItem(t, n++, all_entries_dict);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 912) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 913) Py_DECREF(callchain);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 914) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 915)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 916) if (_PyTuple_Resize(&t, n) == -1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 917) Py_FatalError("error resizing Python tuple");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 918)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 919) if (!dict)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 920) call_object(handler, t, handler_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 921) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 922) call_object(handler, t, default_handler_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 923)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 924) Py_DECREF(t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 925) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 926)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 927) static PyObject *tuple_new(unsigned int sz)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 928) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 929) PyObject *t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 930)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 931) t = PyTuple_New(sz);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 932) if (!t)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 933) Py_FatalError("couldn't create Python tuple");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 934) return t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 935) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 936)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 937) static int tuple_set_s64(PyObject *t, unsigned int pos, s64 val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 938) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 939) #if BITS_PER_LONG == 64
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 940) return PyTuple_SetItem(t, pos, _PyLong_FromLong(val));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 941) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 942) #if BITS_PER_LONG == 32
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 943) return PyTuple_SetItem(t, pos, PyLong_FromLongLong(val));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 944) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 945) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 946)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 947) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 948) * Databases support only signed 64-bit numbers, so even though we are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 949) * exporting a u64, it must be as s64.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 950) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 951) #define tuple_set_d64 tuple_set_s64
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 952)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 953) static int tuple_set_u64(PyObject *t, unsigned int pos, u64 val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 954) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 955) #if BITS_PER_LONG == 64
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 956) return PyTuple_SetItem(t, pos, PyLong_FromUnsignedLong(val));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 957) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 958) #if BITS_PER_LONG == 32
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 959) return PyTuple_SetItem(t, pos, PyLong_FromUnsignedLongLong(val));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 960) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 961) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 962)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 963) static int tuple_set_s32(PyObject *t, unsigned int pos, s32 val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 964) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 965) return PyTuple_SetItem(t, pos, _PyLong_FromLong(val));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 966) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 967)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 968) static int tuple_set_string(PyObject *t, unsigned int pos, const char *s)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 969) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 970) return PyTuple_SetItem(t, pos, _PyUnicode_FromString(s));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 971) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 972)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 973) static int tuple_set_bytes(PyObject *t, unsigned int pos, void *bytes,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 974) unsigned int sz)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 975) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 976) return PyTuple_SetItem(t, pos, _PyBytes_FromStringAndSize(bytes, sz));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 977) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 978)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 979) static int python_export_evsel(struct db_export *dbe, struct evsel *evsel)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 980) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 981) struct tables *tables = container_of(dbe, struct tables, dbe);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 982) PyObject *t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 983)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 984) t = tuple_new(2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 985)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 986) tuple_set_d64(t, 0, evsel->db_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 987) tuple_set_string(t, 1, evsel__name(evsel));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 988)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 989) call_object(tables->evsel_handler, t, "evsel_table");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 990)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 991) Py_DECREF(t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 992)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 993) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 994) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 995)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 996) static int python_export_machine(struct db_export *dbe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 997) struct machine *machine)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 998) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 999) struct tables *tables = container_of(dbe, struct tables, dbe);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000) PyObject *t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002) t = tuple_new(3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004) tuple_set_d64(t, 0, machine->db_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005) tuple_set_s32(t, 1, machine->pid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006) tuple_set_string(t, 2, machine->root_dir ? machine->root_dir : "");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008) call_object(tables->machine_handler, t, "machine_table");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010) Py_DECREF(t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015) static int python_export_thread(struct db_export *dbe, struct thread *thread,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1016) u64 main_thread_db_id, struct machine *machine)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1017) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1018) struct tables *tables = container_of(dbe, struct tables, dbe);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1019) PyObject *t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1020)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1021) t = tuple_new(5);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1022)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1023) tuple_set_d64(t, 0, thread->db_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1024) tuple_set_d64(t, 1, machine->db_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1025) tuple_set_d64(t, 2, main_thread_db_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1026) tuple_set_s32(t, 3, thread->pid_);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1027) tuple_set_s32(t, 4, thread->tid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1028)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1029) call_object(tables->thread_handler, t, "thread_table");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1030)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1031) Py_DECREF(t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1032)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1033) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1034) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1035)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1036) static int python_export_comm(struct db_export *dbe, struct comm *comm,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1037) struct thread *thread)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1038) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1039) struct tables *tables = container_of(dbe, struct tables, dbe);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1040) PyObject *t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1041)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1042) t = tuple_new(5);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1043)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1044) tuple_set_d64(t, 0, comm->db_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1045) tuple_set_string(t, 1, comm__str(comm));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1046) tuple_set_d64(t, 2, thread->db_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1047) tuple_set_d64(t, 3, comm->start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1048) tuple_set_s32(t, 4, comm->exec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1049)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1050) call_object(tables->comm_handler, t, "comm_table");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1051)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1052) Py_DECREF(t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1053)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1054) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1055) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1056)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1057) static int python_export_comm_thread(struct db_export *dbe, u64 db_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1058) struct comm *comm, struct thread *thread)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1059) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1060) struct tables *tables = container_of(dbe, struct tables, dbe);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1061) PyObject *t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1062)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1063) t = tuple_new(3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1064)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1065) tuple_set_d64(t, 0, db_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1066) tuple_set_d64(t, 1, comm->db_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1067) tuple_set_d64(t, 2, thread->db_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1068)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1069) call_object(tables->comm_thread_handler, t, "comm_thread_table");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1070)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1071) Py_DECREF(t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1072)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1073) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1074) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1075)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1076) static int python_export_dso(struct db_export *dbe, struct dso *dso,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1077) struct machine *machine)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1078) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1079) struct tables *tables = container_of(dbe, struct tables, dbe);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1080) char sbuild_id[SBUILD_ID_SIZE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1081) PyObject *t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1082)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1083) build_id__sprintf(&dso->bid, sbuild_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1084)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1085) t = tuple_new(5);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1086)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1087) tuple_set_d64(t, 0, dso->db_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1088) tuple_set_d64(t, 1, machine->db_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1089) tuple_set_string(t, 2, dso->short_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1090) tuple_set_string(t, 3, dso->long_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1091) tuple_set_string(t, 4, sbuild_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1092)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1093) call_object(tables->dso_handler, t, "dso_table");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1094)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1095) Py_DECREF(t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1096)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1097) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1098) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1099)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1100) static int python_export_symbol(struct db_export *dbe, struct symbol *sym,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1101) struct dso *dso)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1102) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1103) struct tables *tables = container_of(dbe, struct tables, dbe);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1104) u64 *sym_db_id = symbol__priv(sym);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1105) PyObject *t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1106)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1107) t = tuple_new(6);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1108)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1109) tuple_set_d64(t, 0, *sym_db_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1110) tuple_set_d64(t, 1, dso->db_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1111) tuple_set_d64(t, 2, sym->start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1112) tuple_set_d64(t, 3, sym->end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1113) tuple_set_s32(t, 4, sym->binding);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1114) tuple_set_string(t, 5, sym->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1115)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1116) call_object(tables->symbol_handler, t, "symbol_table");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1117)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1118) Py_DECREF(t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1119)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1120) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1121) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1122)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1123) static int python_export_branch_type(struct db_export *dbe, u32 branch_type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1124) const char *name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1125) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1126) struct tables *tables = container_of(dbe, struct tables, dbe);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1127) PyObject *t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1128)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1129) t = tuple_new(2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1130)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1131) tuple_set_s32(t, 0, branch_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1132) tuple_set_string(t, 1, name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1133)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1134) call_object(tables->branch_type_handler, t, "branch_type_table");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1135)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1136) Py_DECREF(t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1137)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1138) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1139) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1140)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1141) static void python_export_sample_table(struct db_export *dbe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1142) struct export_sample *es)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1143) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1144) struct tables *tables = container_of(dbe, struct tables, dbe);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1145) PyObject *t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1146)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1147) t = tuple_new(24);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1148)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1149) tuple_set_d64(t, 0, es->db_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1150) tuple_set_d64(t, 1, es->evsel->db_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1151) tuple_set_d64(t, 2, es->al->maps->machine->db_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1152) tuple_set_d64(t, 3, es->al->thread->db_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1153) tuple_set_d64(t, 4, es->comm_db_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1154) tuple_set_d64(t, 5, es->dso_db_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1155) tuple_set_d64(t, 6, es->sym_db_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1156) tuple_set_d64(t, 7, es->offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1157) tuple_set_d64(t, 8, es->sample->ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1158) tuple_set_d64(t, 9, es->sample->time);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1159) tuple_set_s32(t, 10, es->sample->cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1160) tuple_set_d64(t, 11, es->addr_dso_db_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1161) tuple_set_d64(t, 12, es->addr_sym_db_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1162) tuple_set_d64(t, 13, es->addr_offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1163) tuple_set_d64(t, 14, es->sample->addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1164) tuple_set_d64(t, 15, es->sample->period);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1165) tuple_set_d64(t, 16, es->sample->weight);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1166) tuple_set_d64(t, 17, es->sample->transaction);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1167) tuple_set_d64(t, 18, es->sample->data_src);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1168) tuple_set_s32(t, 19, es->sample->flags & PERF_BRANCH_MASK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1169) tuple_set_s32(t, 20, !!(es->sample->flags & PERF_IP_FLAG_IN_TX));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1170) tuple_set_d64(t, 21, es->call_path_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1171) tuple_set_d64(t, 22, es->sample->insn_cnt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1172) tuple_set_d64(t, 23, es->sample->cyc_cnt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1173)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1174) call_object(tables->sample_handler, t, "sample_table");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1175)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1176) Py_DECREF(t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1177) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1178)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1179) static void python_export_synth(struct db_export *dbe, struct export_sample *es)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1180) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1181) struct tables *tables = container_of(dbe, struct tables, dbe);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1182) PyObject *t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1183)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1184) t = tuple_new(3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1185)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1186) tuple_set_d64(t, 0, es->db_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1187) tuple_set_d64(t, 1, es->evsel->core.attr.config);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1188) tuple_set_bytes(t, 2, es->sample->raw_data, es->sample->raw_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1189)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1190) call_object(tables->synth_handler, t, "synth_data");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1191)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1192) Py_DECREF(t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1193) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1194)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1195) static int python_export_sample(struct db_export *dbe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1196) struct export_sample *es)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1197) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1198) struct tables *tables = container_of(dbe, struct tables, dbe);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1199)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1200) python_export_sample_table(dbe, es);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1201)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1202) if (es->evsel->core.attr.type == PERF_TYPE_SYNTH && tables->synth_handler)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1203) python_export_synth(dbe, es);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1204)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1205) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1206) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1207)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1208) static int python_export_call_path(struct db_export *dbe, struct call_path *cp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1209) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1210) struct tables *tables = container_of(dbe, struct tables, dbe);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1211) PyObject *t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1212) u64 parent_db_id, sym_db_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1213)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1214) parent_db_id = cp->parent ? cp->parent->db_id : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1215) sym_db_id = cp->sym ? *(u64 *)symbol__priv(cp->sym) : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1216)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1217) t = tuple_new(4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1218)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1219) tuple_set_d64(t, 0, cp->db_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1220) tuple_set_d64(t, 1, parent_db_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1221) tuple_set_d64(t, 2, sym_db_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1222) tuple_set_d64(t, 3, cp->ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1223)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1224) call_object(tables->call_path_handler, t, "call_path_table");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1225)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1226) Py_DECREF(t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1227)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1228) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1229) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1230)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1231) static int python_export_call_return(struct db_export *dbe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1232) struct call_return *cr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1233) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1234) struct tables *tables = container_of(dbe, struct tables, dbe);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1235) u64 comm_db_id = cr->comm ? cr->comm->db_id : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1236) PyObject *t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1237)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1238) t = tuple_new(14);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1239)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1240) tuple_set_d64(t, 0, cr->db_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1241) tuple_set_d64(t, 1, cr->thread->db_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1242) tuple_set_d64(t, 2, comm_db_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1243) tuple_set_d64(t, 3, cr->cp->db_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1244) tuple_set_d64(t, 4, cr->call_time);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1245) tuple_set_d64(t, 5, cr->return_time);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1246) tuple_set_d64(t, 6, cr->branch_count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1247) tuple_set_d64(t, 7, cr->call_ref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1248) tuple_set_d64(t, 8, cr->return_ref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1249) tuple_set_d64(t, 9, cr->cp->parent->db_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1250) tuple_set_s32(t, 10, cr->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1251) tuple_set_d64(t, 11, cr->parent_db_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1252) tuple_set_d64(t, 12, cr->insn_count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1253) tuple_set_d64(t, 13, cr->cyc_count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1254)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1255) call_object(tables->call_return_handler, t, "call_return_table");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1256)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1257) Py_DECREF(t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1258)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1259) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1260) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1261)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1262) static int python_export_context_switch(struct db_export *dbe, u64 db_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1263) struct machine *machine,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1264) struct perf_sample *sample,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1265) u64 th_out_id, u64 comm_out_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1266) u64 th_in_id, u64 comm_in_id, int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1267) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1268) struct tables *tables = container_of(dbe, struct tables, dbe);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1269) PyObject *t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1270)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1271) t = tuple_new(9);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1272)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1273) tuple_set_d64(t, 0, db_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1274) tuple_set_d64(t, 1, machine->db_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1275) tuple_set_d64(t, 2, sample->time);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1276) tuple_set_s32(t, 3, sample->cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1277) tuple_set_d64(t, 4, th_out_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1278) tuple_set_d64(t, 5, comm_out_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1279) tuple_set_d64(t, 6, th_in_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1280) tuple_set_d64(t, 7, comm_in_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1281) tuple_set_s32(t, 8, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1282)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1283) call_object(tables->context_switch_handler, t, "context_switch");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1284)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1285) Py_DECREF(t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1286)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1287) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1288) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1289)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1290) static int python_process_call_return(struct call_return *cr, u64 *parent_db_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1291) void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1292) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1293) struct db_export *dbe = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1294)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1295) return db_export__call_return(dbe, cr, parent_db_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1296) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1297)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1298) static void python_process_general_event(struct perf_sample *sample,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1299) struct evsel *evsel,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1300) struct addr_location *al)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1301) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1302) PyObject *handler, *t, *dict, *callchain;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1303) static char handler_name[64];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1304) unsigned n = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1305)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1306) snprintf(handler_name, sizeof(handler_name), "%s", "process_event");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1307)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1308) handler = get_handler(handler_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1309) if (!handler)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1310) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1311)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1312) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1313) * Use the MAX_FIELDS to make the function expandable, though
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1314) * currently there is only one item for the tuple.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1315) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1316) t = PyTuple_New(MAX_FIELDS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1317) if (!t)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1318) Py_FatalError("couldn't create Python tuple");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1319)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1320) /* ip unwinding */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1321) callchain = python_process_callchain(sample, evsel, al);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1322) dict = get_perf_sample_dict(sample, evsel, al, callchain);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1323)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1324) PyTuple_SetItem(t, n++, dict);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1325) if (_PyTuple_Resize(&t, n) == -1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1326) Py_FatalError("error resizing Python tuple");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1327)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1328) call_object(handler, t, handler_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1329)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1330) Py_DECREF(t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1331) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1332)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1333) static void python_process_event(union perf_event *event,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1334) struct perf_sample *sample,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1335) struct evsel *evsel,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1336) struct addr_location *al)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1337) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1338) struct tables *tables = &tables_global;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1339)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1340) switch (evsel->core.attr.type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1341) case PERF_TYPE_TRACEPOINT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1342) python_process_tracepoint(sample, evsel, al);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1343) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1344) /* Reserve for future process_hw/sw/raw APIs */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1345) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1346) if (tables->db_export_mode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1347) db_export__sample(&tables->dbe, event, sample, evsel, al);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1348) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1349) python_process_general_event(sample, evsel, al);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1350) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1351) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1352)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1353) static void python_process_switch(union perf_event *event,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1354) struct perf_sample *sample,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1355) struct machine *machine)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1356) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1357) struct tables *tables = &tables_global;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1358)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1359) if (tables->db_export_mode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1360) db_export__switch(&tables->dbe, event, sample, machine);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1361) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1362)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1363) static void get_handler_name(char *str, size_t size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1364) struct evsel *evsel)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1365) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1366) char *p = str;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1367)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1368) scnprintf(str, size, "stat__%s", evsel__name(evsel));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1369)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1370) while ((p = strchr(p, ':'))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1371) *p = '_';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1372) p++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1373) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1374) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1375)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1376) static void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1377) process_stat(struct evsel *counter, int cpu, int thread, u64 tstamp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1378) struct perf_counts_values *count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1379) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1380) PyObject *handler, *t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1381) static char handler_name[256];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1382) int n = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1383)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1384) t = PyTuple_New(MAX_FIELDS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1385) if (!t)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1386) Py_FatalError("couldn't create Python tuple");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1387)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1388) get_handler_name(handler_name, sizeof(handler_name),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1389) counter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1390)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1391) handler = get_handler(handler_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1392) if (!handler) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1393) pr_debug("can't find python handler %s\n", handler_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1394) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1395) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1396)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1397) PyTuple_SetItem(t, n++, _PyLong_FromLong(cpu));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1398) PyTuple_SetItem(t, n++, _PyLong_FromLong(thread));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1399)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1400) tuple_set_u64(t, n++, tstamp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1401) tuple_set_u64(t, n++, count->val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1402) tuple_set_u64(t, n++, count->ena);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1403) tuple_set_u64(t, n++, count->run);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1404)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1405) if (_PyTuple_Resize(&t, n) == -1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1406) Py_FatalError("error resizing Python tuple");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1407)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1408) call_object(handler, t, handler_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1409)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1410) Py_DECREF(t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1411) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1412)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1413) static void python_process_stat(struct perf_stat_config *config,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1414) struct evsel *counter, u64 tstamp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1415) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1416) struct perf_thread_map *threads = counter->core.threads;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1417) struct perf_cpu_map *cpus = counter->core.cpus;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1418) int cpu, thread;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1419)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1420) if (config->aggr_mode == AGGR_GLOBAL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1421) process_stat(counter, -1, -1, tstamp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1422) &counter->counts->aggr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1423) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1424) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1425)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1426) for (thread = 0; thread < threads->nr; thread++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1427) for (cpu = 0; cpu < cpus->nr; cpu++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1428) process_stat(counter, cpus->map[cpu],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1429) perf_thread_map__pid(threads, thread), tstamp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1430) perf_counts(counter->counts, cpu, thread));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1431) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1432) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1433) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1434)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1435) static void python_process_stat_interval(u64 tstamp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1436) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1437) PyObject *handler, *t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1438) static const char handler_name[] = "stat__interval";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1439) int n = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1440)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1441) t = PyTuple_New(MAX_FIELDS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1442) if (!t)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1443) Py_FatalError("couldn't create Python tuple");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1444)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1445) handler = get_handler(handler_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1446) if (!handler) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1447) pr_debug("can't find python handler %s\n", handler_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1448) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1449) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1450)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1451) tuple_set_u64(t, n++, tstamp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1452)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1453) if (_PyTuple_Resize(&t, n) == -1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1454) Py_FatalError("error resizing Python tuple");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1455)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1456) call_object(handler, t, handler_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1457)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1458) Py_DECREF(t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1459) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1460)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1461) static int run_start_sub(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1462) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1463) main_module = PyImport_AddModule("__main__");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1464) if (main_module == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1465) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1466) Py_INCREF(main_module);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1467)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1468) main_dict = PyModule_GetDict(main_module);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1469) if (main_dict == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1470) goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1471) Py_INCREF(main_dict);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1472)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1473) try_call_object("trace_begin", NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1474)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1475) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1476)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1477) error:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1478) Py_XDECREF(main_dict);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1479) Py_XDECREF(main_module);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1480) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1481) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1482)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1483) #define SET_TABLE_HANDLER_(name, handler_name, table_name) do { \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1484) tables->handler_name = get_handler(#table_name); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1485) if (tables->handler_name) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1486) tables->dbe.export_ ## name = python_export_ ## name; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1487) } while (0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1488)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1489) #define SET_TABLE_HANDLER(name) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1490) SET_TABLE_HANDLER_(name, name ## _handler, name ## _table)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1491)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1492) static void set_table_handlers(struct tables *tables)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1493) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1494) const char *perf_db_export_mode = "perf_db_export_mode";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1495) const char *perf_db_export_calls = "perf_db_export_calls";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1496) const char *perf_db_export_callchains = "perf_db_export_callchains";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1497) PyObject *db_export_mode, *db_export_calls, *db_export_callchains;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1498) bool export_calls = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1499) bool export_callchains = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1500) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1501)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1502) memset(tables, 0, sizeof(struct tables));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1503) if (db_export__init(&tables->dbe))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1504) Py_FatalError("failed to initialize export");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1505)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1506) db_export_mode = PyDict_GetItemString(main_dict, perf_db_export_mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1507) if (!db_export_mode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1508) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1509)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1510) ret = PyObject_IsTrue(db_export_mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1511) if (ret == -1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1512) handler_call_die(perf_db_export_mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1513) if (!ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1514) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1515)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1516) /* handle export calls */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1517) tables->dbe.crp = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1518) db_export_calls = PyDict_GetItemString(main_dict, perf_db_export_calls);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1519) if (db_export_calls) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1520) ret = PyObject_IsTrue(db_export_calls);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1521) if (ret == -1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1522) handler_call_die(perf_db_export_calls);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1523) export_calls = !!ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1524) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1525)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1526) if (export_calls) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1527) tables->dbe.crp =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1528) call_return_processor__new(python_process_call_return,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1529) &tables->dbe);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1530) if (!tables->dbe.crp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1531) Py_FatalError("failed to create calls processor");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1532) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1533)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1534) /* handle export callchains */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1535) tables->dbe.cpr = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1536) db_export_callchains = PyDict_GetItemString(main_dict,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1537) perf_db_export_callchains);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1538) if (db_export_callchains) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1539) ret = PyObject_IsTrue(db_export_callchains);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1540) if (ret == -1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1541) handler_call_die(perf_db_export_callchains);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1542) export_callchains = !!ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1543) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1544)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1545) if (export_callchains) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1546) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1547) * Attempt to use the call path root from the call return
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1548) * processor, if the call return processor is in use. Otherwise,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1549) * we allocate a new call path root. This prevents exporting
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1550) * duplicate call path ids when both are in use simultaniously.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1551) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1552) if (tables->dbe.crp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1553) tables->dbe.cpr = tables->dbe.crp->cpr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1554) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1555) tables->dbe.cpr = call_path_root__new();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1556)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1557) if (!tables->dbe.cpr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1558) Py_FatalError("failed to create call path root");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1559) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1560)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1561) tables->db_export_mode = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1562) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1563) * Reserve per symbol space for symbol->db_id via symbol__priv()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1564) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1565) symbol_conf.priv_size = sizeof(u64);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1566)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1567) SET_TABLE_HANDLER(evsel);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1568) SET_TABLE_HANDLER(machine);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1569) SET_TABLE_HANDLER(thread);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1570) SET_TABLE_HANDLER(comm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1571) SET_TABLE_HANDLER(comm_thread);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1572) SET_TABLE_HANDLER(dso);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1573) SET_TABLE_HANDLER(symbol);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1574) SET_TABLE_HANDLER(branch_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1575) SET_TABLE_HANDLER(sample);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1576) SET_TABLE_HANDLER(call_path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1577) SET_TABLE_HANDLER(call_return);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1578) SET_TABLE_HANDLER(context_switch);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1579)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1580) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1581) * Synthesized events are samples but with architecture-specific data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1582) * stored in sample->raw_data. They are exported via
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1583) * python_export_sample() and consequently do not need a separate export
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1584) * callback.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1585) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1586) tables->synth_handler = get_handler("synth_data");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1587) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1588)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1589) #if PY_MAJOR_VERSION < 3
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1590) static void _free_command_line(const char **command_line, int num)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1591) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1592) free(command_line);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1593) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1594) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1595) static void _free_command_line(wchar_t **command_line, int num)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1596) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1597) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1598) for (i = 0; i < num; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1599) PyMem_RawFree(command_line[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1600) free(command_line);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1601) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1602) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1603)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1604)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1605) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1606) * Start trace script
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1607) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1608) static int python_start_script(const char *script, int argc, const char **argv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1609) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1610) struct tables *tables = &tables_global;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1611) #if PY_MAJOR_VERSION < 3
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1612) const char **command_line;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1613) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1614) wchar_t **command_line;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1615) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1616) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1617) * Use a non-const name variable to cope with python 2.6's
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1618) * PyImport_AppendInittab prototype
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1619) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1620) char buf[PATH_MAX], name[19] = "perf_trace_context";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1621) int i, err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1622) FILE *fp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1623)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1624) #if PY_MAJOR_VERSION < 3
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1625) command_line = malloc((argc + 1) * sizeof(const char *));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1626) command_line[0] = script;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1627) for (i = 1; i < argc + 1; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1628) command_line[i] = argv[i - 1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1629) PyImport_AppendInittab(name, initperf_trace_context);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1630) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1631) command_line = malloc((argc + 1) * sizeof(wchar_t *));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1632) command_line[0] = Py_DecodeLocale(script, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1633) for (i = 1; i < argc + 1; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1634) command_line[i] = Py_DecodeLocale(argv[i - 1], NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1635) PyImport_AppendInittab(name, PyInit_perf_trace_context);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1636) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1637) Py_Initialize();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1638)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1639) #if PY_MAJOR_VERSION < 3
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1640) PySys_SetArgv(argc + 1, (char **)command_line);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1641) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1642) PySys_SetArgv(argc + 1, command_line);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1643) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1644)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1645) fp = fopen(script, "r");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1646) if (!fp) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1647) sprintf(buf, "Can't open python script \"%s\"", script);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1648) perror(buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1649) err = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1650) goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1651) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1652)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1653) err = PyRun_SimpleFile(fp, script);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1654) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1655) fprintf(stderr, "Error running python script %s\n", script);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1656) goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1657) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1658)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1659) err = run_start_sub();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1660) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1661) fprintf(stderr, "Error starting python script %s\n", script);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1662) goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1663) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1664)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1665) set_table_handlers(tables);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1666)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1667) if (tables->db_export_mode) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1668) err = db_export__branch_types(&tables->dbe);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1669) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1670) goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1671) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1672)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1673) _free_command_line(command_line, argc + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1674)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1675) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1676) error:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1677) Py_Finalize();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1678) _free_command_line(command_line, argc + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1679)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1680) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1681) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1682)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1683) static int python_flush_script(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1684) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1685) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1686) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1687)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1688) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1689) * Stop trace script
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1690) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1691) static int python_stop_script(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1692) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1693) struct tables *tables = &tables_global;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1694)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1695) try_call_object("trace_end", NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1696)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1697) db_export__exit(&tables->dbe);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1698)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1699) Py_XDECREF(main_dict);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1700) Py_XDECREF(main_module);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1701) Py_Finalize();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1702)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1703) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1704) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1705)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1706) static int python_generate_script(struct tep_handle *pevent, const char *outfile)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1707) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1708) int i, not_first, count, nr_events;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1709) struct tep_event **all_events;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1710) struct tep_event *event = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1711) struct tep_format_field *f;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1712) char fname[PATH_MAX];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1713) FILE *ofp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1714)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1715) sprintf(fname, "%s.py", outfile);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1716) ofp = fopen(fname, "w");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1717) if (ofp == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1718) fprintf(stderr, "couldn't open %s\n", fname);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1719) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1720) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1721) fprintf(ofp, "# perf script event handlers, "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1722) "generated by perf script -g python\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1723)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1724) fprintf(ofp, "# Licensed under the terms of the GNU GPL"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1725) " License version 2\n\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1726)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1727) fprintf(ofp, "# The common_* event handler fields are the most useful "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1728) "fields common to\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1729)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1730) fprintf(ofp, "# all events. They don't necessarily correspond to "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1731) "the 'common_*' fields\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1732)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1733) fprintf(ofp, "# in the format files. Those fields not available as "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1734) "handler params can\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1735)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1736) fprintf(ofp, "# be retrieved using Python functions of the form "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1737) "common_*(context).\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1738)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1739) fprintf(ofp, "# See the perf-script-python Documentation for the list "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1740) "of available functions.\n\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1741)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1742) fprintf(ofp, "from __future__ import print_function\n\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1743) fprintf(ofp, "import os\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1744) fprintf(ofp, "import sys\n\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1745)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1746) fprintf(ofp, "sys.path.append(os.environ['PERF_EXEC_PATH'] + \\\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1747) fprintf(ofp, "\t'/scripts/python/Perf-Trace-Util/lib/Perf/Trace')\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1748) fprintf(ofp, "\nfrom perf_trace_context import *\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1749) fprintf(ofp, "from Core import *\n\n\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1750)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1751) fprintf(ofp, "def trace_begin():\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1752) fprintf(ofp, "\tprint(\"in trace_begin\")\n\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1753)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1754) fprintf(ofp, "def trace_end():\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1755) fprintf(ofp, "\tprint(\"in trace_end\")\n\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1756)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1757) nr_events = tep_get_events_count(pevent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1758) all_events = tep_list_events(pevent, TEP_EVENT_SORT_ID);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1759)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1760) for (i = 0; all_events && i < nr_events; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1761) event = all_events[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1762) fprintf(ofp, "def %s__%s(", event->system, event->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1763) fprintf(ofp, "event_name, ");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1764) fprintf(ofp, "context, ");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1765) fprintf(ofp, "common_cpu,\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1766) fprintf(ofp, "\tcommon_secs, ");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1767) fprintf(ofp, "common_nsecs, ");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1768) fprintf(ofp, "common_pid, ");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1769) fprintf(ofp, "common_comm,\n\t");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1770) fprintf(ofp, "common_callchain, ");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1771)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1772) not_first = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1773) count = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1774)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1775) for (f = event->format.fields; f; f = f->next) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1776) if (not_first++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1777) fprintf(ofp, ", ");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1778) if (++count % 5 == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1779) fprintf(ofp, "\n\t");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1780)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1781) fprintf(ofp, "%s", f->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1782) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1783) if (not_first++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1784) fprintf(ofp, ", ");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1785) if (++count % 5 == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1786) fprintf(ofp, "\n\t\t");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1787) fprintf(ofp, "perf_sample_dict");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1788)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1789) fprintf(ofp, "):\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1790)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1791) fprintf(ofp, "\t\tprint_header(event_name, common_cpu, "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1792) "common_secs, common_nsecs,\n\t\t\t"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1793) "common_pid, common_comm)\n\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1794)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1795) fprintf(ofp, "\t\tprint(\"");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1796)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1797) not_first = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1798) count = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1799)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1800) for (f = event->format.fields; f; f = f->next) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1801) if (not_first++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1802) fprintf(ofp, ", ");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1803) if (count && count % 3 == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1804) fprintf(ofp, "\" \\\n\t\t\"");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1805) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1806) count++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1807)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1808) fprintf(ofp, "%s=", f->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1809) if (f->flags & TEP_FIELD_IS_STRING ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1810) f->flags & TEP_FIELD_IS_FLAG ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1811) f->flags & TEP_FIELD_IS_ARRAY ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1812) f->flags & TEP_FIELD_IS_SYMBOLIC)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1813) fprintf(ofp, "%%s");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1814) else if (f->flags & TEP_FIELD_IS_SIGNED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1815) fprintf(ofp, "%%d");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1816) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1817) fprintf(ofp, "%%u");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1818) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1819)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1820) fprintf(ofp, "\" %% \\\n\t\t(");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1821)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1822) not_first = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1823) count = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1824)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1825) for (f = event->format.fields; f; f = f->next) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1826) if (not_first++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1827) fprintf(ofp, ", ");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1828)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1829) if (++count % 5 == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1830) fprintf(ofp, "\n\t\t");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1831)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1832) if (f->flags & TEP_FIELD_IS_FLAG) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1833) if ((count - 1) % 5 != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1834) fprintf(ofp, "\n\t\t");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1835) count = 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1836) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1837) fprintf(ofp, "flag_str(\"");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1838) fprintf(ofp, "%s__%s\", ", event->system,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1839) event->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1840) fprintf(ofp, "\"%s\", %s)", f->name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1841) f->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1842) } else if (f->flags & TEP_FIELD_IS_SYMBOLIC) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1843) if ((count - 1) % 5 != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1844) fprintf(ofp, "\n\t\t");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1845) count = 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1846) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1847) fprintf(ofp, "symbol_str(\"");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1848) fprintf(ofp, "%s__%s\", ", event->system,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1849) event->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1850) fprintf(ofp, "\"%s\", %s)", f->name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1851) f->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1852) } else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1853) fprintf(ofp, "%s", f->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1854) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1855)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1856) fprintf(ofp, "))\n\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1857)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1858) fprintf(ofp, "\t\tprint('Sample: {'+"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1859) "get_dict_as_string(perf_sample_dict['sample'], ', ')+'}')\n\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1860)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1861) fprintf(ofp, "\t\tfor node in common_callchain:");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1862) fprintf(ofp, "\n\t\t\tif 'sym' in node:");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1863) fprintf(ofp, "\n\t\t\t\tprint(\"\\t[%%x] %%s\" %% (node['ip'], node['sym']['name']))");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1864) fprintf(ofp, "\n\t\t\telse:");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1865) fprintf(ofp, "\n\t\t\t\tprint(\"\t[%%x]\" %% (node['ip']))\n\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1866) fprintf(ofp, "\t\tprint()\n\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1867)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1868) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1869)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1870) fprintf(ofp, "def trace_unhandled(event_name, context, "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1871) "event_fields_dict, perf_sample_dict):\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1872)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1873) fprintf(ofp, "\t\tprint(get_dict_as_string(event_fields_dict))\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1874) fprintf(ofp, "\t\tprint('Sample: {'+"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1875) "get_dict_as_string(perf_sample_dict['sample'], ', ')+'}')\n\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1876)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1877) fprintf(ofp, "def print_header("
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1878) "event_name, cpu, secs, nsecs, pid, comm):\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1879) "\tprint(\"%%-20s %%5u %%05u.%%09u %%8u %%-20s \" %% \\\n\t"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1880) "(event_name, cpu, secs, nsecs, pid, comm), end=\"\")\n\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1881)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1882) fprintf(ofp, "def get_dict_as_string(a_dict, delimiter=' '):\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1883) "\treturn delimiter.join"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1884) "(['%%s=%%s'%%(k,str(v))for k,v in sorted(a_dict.items())])\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1885)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1886) fclose(ofp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1887)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1888) fprintf(stderr, "generated Python script: %s\n", fname);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1889)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1890) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1891) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1892)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1893) struct scripting_ops python_scripting_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1894) .name = "Python",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1895) .start_script = python_start_script,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1896) .flush_script = python_flush_script,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1897) .stop_script = python_stop_script,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1898) .process_event = python_process_event,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1899) .process_switch = python_process_switch,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1900) .process_stat = python_process_stat,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1901) .process_stat_interval = python_process_stat_interval,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1902) .generate_script = python_generate_script,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1903) };