Tom Zanussi | 7e4b21b | 2010-01-27 02:27:57 -0600 | [diff] [blame] | 1 | /* |
| 2 | * trace-event-python. Feed trace events to an embedded Python interpreter. |
| 3 | * |
| 4 | * Copyright (C) 2010 Tom Zanussi <tzanussi@gmail.com> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 19 | * |
| 20 | */ |
| 21 | |
| 22 | #include <Python.h> |
| 23 | |
| 24 | #include <stdio.h> |
| 25 | #include <stdlib.h> |
| 26 | #include <string.h> |
Tom Zanussi | 7e4b21b | 2010-01-27 02:27:57 -0600 | [diff] [blame] | 27 | #include <errno.h> |
| 28 | |
| 29 | #include "../../perf.h" |
Arnaldo Carvalho de Melo | fcf65bf | 2012-08-07 09:58:03 -0300 | [diff] [blame] | 30 | #include "../evsel.h" |
Tom Zanussi | 7e4b21b | 2010-01-27 02:27:57 -0600 | [diff] [blame] | 31 | #include "../util.h" |
Arnaldo Carvalho de Melo | 743eb86 | 2011-11-28 07:56:39 -0200 | [diff] [blame] | 32 | #include "../event.h" |
| 33 | #include "../thread.h" |
Tom Zanussi | 7e4b21b | 2010-01-27 02:27:57 -0600 | [diff] [blame] | 34 | #include "../trace-event.h" |
| 35 | |
| 36 | PyMODINIT_FUNC initperf_trace_context(void); |
| 37 | |
| 38 | #define FTRACE_MAX_EVENT \ |
| 39 | ((1 << (sizeof(unsigned short) * 8)) - 1) |
| 40 | |
Steven Rostedt | aaf045f | 2012-04-06 00:47:56 +0200 | [diff] [blame] | 41 | struct event_format *events[FTRACE_MAX_EVENT]; |
Tom Zanussi | 7e4b21b | 2010-01-27 02:27:57 -0600 | [diff] [blame] | 42 | |
| 43 | #define MAX_FIELDS 64 |
| 44 | #define N_COMMON_FIELDS 7 |
| 45 | |
| 46 | extern struct scripting_context *scripting_context; |
| 47 | |
| 48 | static char *cur_field_name; |
| 49 | static int zero_flag_atom; |
| 50 | |
| 51 | static PyObject *main_module, *main_dict; |
| 52 | |
| 53 | static void handler_call_die(const char *handler_name) |
| 54 | { |
| 55 | PyErr_Print(); |
| 56 | Py_FatalError("problem in Python trace event handler"); |
| 57 | } |
| 58 | |
Joseph Schuchart | c0268e8 | 2013-10-24 10:10:51 -0300 | [diff] [blame] | 59 | /* |
| 60 | * Insert val into into the dictionary and decrement the reference counter. |
| 61 | * This is necessary for dictionaries since PyDict_SetItemString() does not |
| 62 | * steal a reference, as opposed to PyTuple_SetItem(). |
| 63 | */ |
| 64 | static void pydict_set_item_string_decref(PyObject *dict, const char *key, PyObject *val) |
| 65 | { |
| 66 | PyDict_SetItemString(dict, key, val); |
| 67 | Py_DECREF(val); |
| 68 | } |
| 69 | |
Tom Zanussi | 7e4b21b | 2010-01-27 02:27:57 -0600 | [diff] [blame] | 70 | static void define_value(enum print_arg_type field_type, |
| 71 | const char *ev_name, |
| 72 | const char *field_name, |
| 73 | const char *field_value, |
| 74 | const char *field_str) |
| 75 | { |
| 76 | const char *handler_name = "define_flag_value"; |
| 77 | PyObject *handler, *t, *retval; |
| 78 | unsigned long long value; |
| 79 | unsigned n = 0; |
| 80 | |
| 81 | if (field_type == PRINT_SYMBOL) |
| 82 | handler_name = "define_symbolic_value"; |
| 83 | |
Tom Zanussi | 44ad9cd | 2010-02-22 01:12:59 -0600 | [diff] [blame] | 84 | t = PyTuple_New(4); |
Tom Zanussi | 7e4b21b | 2010-01-27 02:27:57 -0600 | [diff] [blame] | 85 | if (!t) |
| 86 | Py_FatalError("couldn't create Python tuple"); |
| 87 | |
| 88 | value = eval_flag(field_value); |
| 89 | |
| 90 | PyTuple_SetItem(t, n++, PyString_FromString(ev_name)); |
| 91 | PyTuple_SetItem(t, n++, PyString_FromString(field_name)); |
| 92 | PyTuple_SetItem(t, n++, PyInt_FromLong(value)); |
| 93 | PyTuple_SetItem(t, n++, PyString_FromString(field_str)); |
| 94 | |
Tom Zanussi | 7e4b21b | 2010-01-27 02:27:57 -0600 | [diff] [blame] | 95 | handler = PyDict_GetItemString(main_dict, handler_name); |
| 96 | if (handler && PyCallable_Check(handler)) { |
| 97 | retval = PyObject_CallObject(handler, t); |
| 98 | if (retval == NULL) |
| 99 | handler_call_die(handler_name); |
| 100 | } |
| 101 | |
| 102 | Py_DECREF(t); |
| 103 | } |
| 104 | |
| 105 | static void define_values(enum print_arg_type field_type, |
| 106 | struct print_flag_sym *field, |
| 107 | const char *ev_name, |
| 108 | const char *field_name) |
| 109 | { |
| 110 | define_value(field_type, ev_name, field_name, field->value, |
| 111 | field->str); |
| 112 | |
| 113 | if (field->next) |
| 114 | define_values(field_type, field->next, ev_name, field_name); |
| 115 | } |
| 116 | |
| 117 | static void define_field(enum print_arg_type field_type, |
| 118 | const char *ev_name, |
| 119 | const char *field_name, |
| 120 | const char *delim) |
| 121 | { |
| 122 | const char *handler_name = "define_flag_field"; |
| 123 | PyObject *handler, *t, *retval; |
| 124 | unsigned n = 0; |
| 125 | |
| 126 | if (field_type == PRINT_SYMBOL) |
| 127 | handler_name = "define_symbolic_field"; |
| 128 | |
Tom Zanussi | 44ad9cd | 2010-02-22 01:12:59 -0600 | [diff] [blame] | 129 | if (field_type == PRINT_FLAGS) |
| 130 | t = PyTuple_New(3); |
| 131 | else |
| 132 | t = PyTuple_New(2); |
Tom Zanussi | 7e4b21b | 2010-01-27 02:27:57 -0600 | [diff] [blame] | 133 | if (!t) |
| 134 | Py_FatalError("couldn't create Python tuple"); |
| 135 | |
| 136 | PyTuple_SetItem(t, n++, PyString_FromString(ev_name)); |
| 137 | PyTuple_SetItem(t, n++, PyString_FromString(field_name)); |
| 138 | if (field_type == PRINT_FLAGS) |
| 139 | PyTuple_SetItem(t, n++, PyString_FromString(delim)); |
| 140 | |
Tom Zanussi | 7e4b21b | 2010-01-27 02:27:57 -0600 | [diff] [blame] | 141 | handler = PyDict_GetItemString(main_dict, handler_name); |
| 142 | if (handler && PyCallable_Check(handler)) { |
| 143 | retval = PyObject_CallObject(handler, t); |
| 144 | if (retval == NULL) |
| 145 | handler_call_die(handler_name); |
| 146 | } |
| 147 | |
| 148 | Py_DECREF(t); |
| 149 | } |
| 150 | |
Steven Rostedt | aaf045f | 2012-04-06 00:47:56 +0200 | [diff] [blame] | 151 | static void define_event_symbols(struct event_format *event, |
Tom Zanussi | 7e4b21b | 2010-01-27 02:27:57 -0600 | [diff] [blame] | 152 | const char *ev_name, |
| 153 | struct print_arg *args) |
| 154 | { |
| 155 | switch (args->type) { |
| 156 | case PRINT_NULL: |
| 157 | break; |
| 158 | case PRINT_ATOM: |
| 159 | define_value(PRINT_FLAGS, ev_name, cur_field_name, "0", |
| 160 | args->atom.atom); |
| 161 | zero_flag_atom = 0; |
| 162 | break; |
| 163 | case PRINT_FIELD: |
Arnaldo Carvalho de Melo | f538565 | 2013-12-26 15:54:57 -0300 | [diff] [blame] | 164 | free(cur_field_name); |
Tom Zanussi | 7e4b21b | 2010-01-27 02:27:57 -0600 | [diff] [blame] | 165 | cur_field_name = strdup(args->field.name); |
| 166 | break; |
| 167 | case PRINT_FLAGS: |
| 168 | define_event_symbols(event, ev_name, args->flags.field); |
| 169 | define_field(PRINT_FLAGS, ev_name, cur_field_name, |
| 170 | args->flags.delim); |
| 171 | define_values(PRINT_FLAGS, args->flags.flags, ev_name, |
| 172 | cur_field_name); |
| 173 | break; |
| 174 | case PRINT_SYMBOL: |
| 175 | define_event_symbols(event, ev_name, args->symbol.field); |
| 176 | define_field(PRINT_SYMBOL, ev_name, cur_field_name, NULL); |
| 177 | define_values(PRINT_SYMBOL, args->symbol.symbols, ev_name, |
| 178 | cur_field_name); |
| 179 | break; |
Namhyung Kim | e080e6f | 2012-06-27 09:41:41 +0900 | [diff] [blame] | 180 | case PRINT_HEX: |
| 181 | define_event_symbols(event, ev_name, args->hex.field); |
| 182 | define_event_symbols(event, ev_name, args->hex.size); |
| 183 | break; |
Tom Zanussi | 7e4b21b | 2010-01-27 02:27:57 -0600 | [diff] [blame] | 184 | case PRINT_STRING: |
| 185 | break; |
| 186 | case PRINT_TYPE: |
| 187 | define_event_symbols(event, ev_name, args->typecast.item); |
| 188 | break; |
| 189 | case PRINT_OP: |
| 190 | if (strcmp(args->op.op, ":") == 0) |
| 191 | zero_flag_atom = 1; |
| 192 | define_event_symbols(event, ev_name, args->op.left); |
| 193 | define_event_symbols(event, ev_name, args->op.right); |
| 194 | break; |
| 195 | default: |
Steven Rostedt | aaf045f | 2012-04-06 00:47:56 +0200 | [diff] [blame] | 196 | /* gcc warns for these? */ |
| 197 | case PRINT_BSTRING: |
| 198 | case PRINT_DYNAMIC_ARRAY: |
| 199 | case PRINT_FUNC: |
Steven Rostedt (Red Hat) | 473a778 | 2014-06-02 23:20:16 -0400 | [diff] [blame] | 200 | case PRINT_BITMASK: |
Tom Zanussi | 7e4b21b | 2010-01-27 02:27:57 -0600 | [diff] [blame] | 201 | /* we should warn... */ |
| 202 | return; |
| 203 | } |
| 204 | |
| 205 | if (args->next) |
| 206 | define_event_symbols(event, ev_name, args->next); |
| 207 | } |
| 208 | |
Arnaldo Carvalho de Melo | fcf65bf | 2012-08-07 09:58:03 -0300 | [diff] [blame] | 209 | static inline struct event_format *find_cache_event(struct perf_evsel *evsel) |
Tom Zanussi | 7e4b21b | 2010-01-27 02:27:57 -0600 | [diff] [blame] | 210 | { |
| 211 | static char ev_name[256]; |
Steven Rostedt | aaf045f | 2012-04-06 00:47:56 +0200 | [diff] [blame] | 212 | struct event_format *event; |
Arnaldo Carvalho de Melo | fcf65bf | 2012-08-07 09:58:03 -0300 | [diff] [blame] | 213 | int type = evsel->attr.config; |
Tom Zanussi | 7e4b21b | 2010-01-27 02:27:57 -0600 | [diff] [blame] | 214 | |
Arnaldo Carvalho de Melo | fcf65bf | 2012-08-07 09:58:03 -0300 | [diff] [blame] | 215 | /* |
| 216 | * XXX: Do we really need to cache this since now we have evsel->tp_format |
| 217 | * cached already? Need to re-read this "cache" routine that as well calls |
| 218 | * define_event_symbols() :-\ |
| 219 | */ |
Tom Zanussi | 7e4b21b | 2010-01-27 02:27:57 -0600 | [diff] [blame] | 220 | if (events[type]) |
| 221 | return events[type]; |
| 222 | |
Arnaldo Carvalho de Melo | fcf65bf | 2012-08-07 09:58:03 -0300 | [diff] [blame] | 223 | events[type] = event = evsel->tp_format; |
Tom Zanussi | 7e4b21b | 2010-01-27 02:27:57 -0600 | [diff] [blame] | 224 | if (!event) |
| 225 | return NULL; |
| 226 | |
| 227 | sprintf(ev_name, "%s__%s", event->system, event->name); |
| 228 | |
| 229 | define_event_symbols(event, ev_name, event->print_fmt.args); |
| 230 | |
| 231 | return event; |
| 232 | } |
| 233 | |
Sebastian Andrzej Siewior | 33058b9 | 2014-05-27 18:14:33 +0200 | [diff] [blame] | 234 | static PyObject *get_field_numeric_entry(struct event_format *event, |
| 235 | struct format_field *field, void *data) |
| 236 | { |
Sebastian Andrzej Siewior | 8ac631c | 2014-05-27 18:14:34 +0200 | [diff] [blame^] | 237 | bool is_array = field->flags & FIELD_IS_ARRAY; |
| 238 | PyObject *obj, *list = NULL; |
Sebastian Andrzej Siewior | 33058b9 | 2014-05-27 18:14:33 +0200 | [diff] [blame] | 239 | unsigned long long val; |
Sebastian Andrzej Siewior | 8ac631c | 2014-05-27 18:14:34 +0200 | [diff] [blame^] | 240 | unsigned int item_size, n_items, i; |
Sebastian Andrzej Siewior | 33058b9 | 2014-05-27 18:14:33 +0200 | [diff] [blame] | 241 | |
Sebastian Andrzej Siewior | 8ac631c | 2014-05-27 18:14:34 +0200 | [diff] [blame^] | 242 | if (is_array) { |
| 243 | list = PyList_New(field->arraylen); |
| 244 | item_size = field->size / field->arraylen; |
| 245 | n_items = field->arraylen; |
Sebastian Andrzej Siewior | 33058b9 | 2014-05-27 18:14:33 +0200 | [diff] [blame] | 246 | } else { |
Sebastian Andrzej Siewior | 8ac631c | 2014-05-27 18:14:34 +0200 | [diff] [blame^] | 247 | item_size = field->size; |
| 248 | n_items = 1; |
Sebastian Andrzej Siewior | 33058b9 | 2014-05-27 18:14:33 +0200 | [diff] [blame] | 249 | } |
Sebastian Andrzej Siewior | 8ac631c | 2014-05-27 18:14:34 +0200 | [diff] [blame^] | 250 | |
| 251 | for (i = 0; i < n_items; i++) { |
| 252 | |
| 253 | val = read_size(event, data + field->offset + i * item_size, |
| 254 | item_size); |
| 255 | if (field->flags & FIELD_IS_SIGNED) { |
| 256 | if ((long long)val >= LONG_MIN && |
| 257 | (long long)val <= LONG_MAX) |
| 258 | obj = PyInt_FromLong(val); |
| 259 | else |
| 260 | obj = PyLong_FromLongLong(val); |
| 261 | } else { |
| 262 | if (val <= LONG_MAX) |
| 263 | obj = PyInt_FromLong(val); |
| 264 | else |
| 265 | obj = PyLong_FromUnsignedLongLong(val); |
| 266 | } |
| 267 | if (is_array) |
| 268 | PyList_SET_ITEM(list, i, obj); |
| 269 | } |
| 270 | if (is_array) |
| 271 | obj = list; |
Sebastian Andrzej Siewior | 33058b9 | 2014-05-27 18:14:33 +0200 | [diff] [blame] | 272 | return obj; |
| 273 | } |
| 274 | |
Arnaldo Carvalho de Melo | b7fff6b5 | 2013-12-19 16:34:52 -0300 | [diff] [blame] | 275 | static void python_process_tracepoint(struct perf_sample *sample, |
| 276 | struct perf_evsel *evsel, |
| 277 | struct thread *thread, |
| 278 | struct addr_location *al) |
Tom Zanussi | 7e4b21b | 2010-01-27 02:27:57 -0600 | [diff] [blame] | 279 | { |
Pierre Tardy | c025148 | 2010-05-31 23:12:09 +0200 | [diff] [blame] | 280 | PyObject *handler, *retval, *context, *t, *obj, *dict = NULL; |
Tom Zanussi | 7e4b21b | 2010-01-27 02:27:57 -0600 | [diff] [blame] | 281 | static char handler_name[256]; |
| 282 | struct format_field *field; |
Tom Zanussi | 7e4b21b | 2010-01-27 02:27:57 -0600 | [diff] [blame] | 283 | unsigned long s, ns; |
Steven Rostedt | aaf045f | 2012-04-06 00:47:56 +0200 | [diff] [blame] | 284 | struct event_format *event; |
Tom Zanussi | 7e4b21b | 2010-01-27 02:27:57 -0600 | [diff] [blame] | 285 | unsigned n = 0; |
Tom Zanussi | 7e4b21b | 2010-01-27 02:27:57 -0600 | [diff] [blame] | 286 | int pid; |
David Ahern | be6d842 | 2011-03-09 22:23:23 -0700 | [diff] [blame] | 287 | int cpu = sample->cpu; |
| 288 | void *data = sample->raw_data; |
| 289 | unsigned long long nsecs = sample->time; |
Frederic Weisbecker | b9c5143 | 2013-09-11 14:46:56 +0200 | [diff] [blame] | 290 | const char *comm = thread__comm_str(thread); |
Tom Zanussi | 7e4b21b | 2010-01-27 02:27:57 -0600 | [diff] [blame] | 291 | |
| 292 | t = PyTuple_New(MAX_FIELDS); |
| 293 | if (!t) |
| 294 | Py_FatalError("couldn't create Python tuple"); |
| 295 | |
Arnaldo Carvalho de Melo | fcf65bf | 2012-08-07 09:58:03 -0300 | [diff] [blame] | 296 | event = find_cache_event(evsel); |
Tom Zanussi | 7e4b21b | 2010-01-27 02:27:57 -0600 | [diff] [blame] | 297 | if (!event) |
Arnaldo Carvalho de Melo | fcf65bf | 2012-08-07 09:58:03 -0300 | [diff] [blame] | 298 | die("ug! no event found for type %d", (int)evsel->attr.config); |
Tom Zanussi | 7e4b21b | 2010-01-27 02:27:57 -0600 | [diff] [blame] | 299 | |
Arnaldo Carvalho de Melo | 9782243 | 2012-08-07 23:50:21 -0300 | [diff] [blame] | 300 | pid = raw_field_value(event, "common_pid", data); |
Tom Zanussi | 7e4b21b | 2010-01-27 02:27:57 -0600 | [diff] [blame] | 301 | |
| 302 | sprintf(handler_name, "%s__%s", event->system, event->name); |
| 303 | |
Pierre Tardy | c025148 | 2010-05-31 23:12:09 +0200 | [diff] [blame] | 304 | handler = PyDict_GetItemString(main_dict, handler_name); |
| 305 | if (handler && !PyCallable_Check(handler)) |
| 306 | handler = NULL; |
| 307 | if (!handler) { |
| 308 | dict = PyDict_New(); |
| 309 | if (!dict) |
| 310 | Py_FatalError("couldn't create Python dict"); |
| 311 | } |
Tom Zanussi | 7e4b21b | 2010-01-27 02:27:57 -0600 | [diff] [blame] | 312 | s = nsecs / NSECS_PER_SEC; |
| 313 | ns = nsecs - s * NSECS_PER_SEC; |
| 314 | |
| 315 | scripting_context->event_data = data; |
Tom Zanussi | 2de9533 | 2013-01-18 13:51:27 -0600 | [diff] [blame] | 316 | scripting_context->pevent = evsel->tp_format->pevent; |
Tom Zanussi | 7e4b21b | 2010-01-27 02:27:57 -0600 | [diff] [blame] | 317 | |
| 318 | context = PyCObject_FromVoidPtr(scripting_context, NULL); |
| 319 | |
| 320 | PyTuple_SetItem(t, n++, PyString_FromString(handler_name)); |
Kyle McMartin | fb7d0b3 | 2011-01-24 11:13:04 -0500 | [diff] [blame] | 321 | PyTuple_SetItem(t, n++, context); |
Tom Zanussi | 7e4b21b | 2010-01-27 02:27:57 -0600 | [diff] [blame] | 322 | |
Pierre Tardy | c025148 | 2010-05-31 23:12:09 +0200 | [diff] [blame] | 323 | if (handler) { |
| 324 | PyTuple_SetItem(t, n++, PyInt_FromLong(cpu)); |
| 325 | PyTuple_SetItem(t, n++, PyInt_FromLong(s)); |
| 326 | PyTuple_SetItem(t, n++, PyInt_FromLong(ns)); |
| 327 | PyTuple_SetItem(t, n++, PyInt_FromLong(pid)); |
| 328 | PyTuple_SetItem(t, n++, PyString_FromString(comm)); |
| 329 | } else { |
Joseph Schuchart | c0268e8 | 2013-10-24 10:10:51 -0300 | [diff] [blame] | 330 | pydict_set_item_string_decref(dict, "common_cpu", PyInt_FromLong(cpu)); |
| 331 | pydict_set_item_string_decref(dict, "common_s", PyInt_FromLong(s)); |
| 332 | pydict_set_item_string_decref(dict, "common_ns", PyInt_FromLong(ns)); |
| 333 | pydict_set_item_string_decref(dict, "common_pid", PyInt_FromLong(pid)); |
| 334 | pydict_set_item_string_decref(dict, "common_comm", PyString_FromString(comm)); |
Pierre Tardy | c025148 | 2010-05-31 23:12:09 +0200 | [diff] [blame] | 335 | } |
Tom Zanussi | 7e4b21b | 2010-01-27 02:27:57 -0600 | [diff] [blame] | 336 | for (field = event->format.fields; field; field = field->next) { |
| 337 | if (field->flags & FIELD_IS_STRING) { |
| 338 | int offset; |
| 339 | if (field->flags & FIELD_IS_DYNAMIC) { |
| 340 | offset = *(int *)(data + field->offset); |
| 341 | offset &= 0xffff; |
| 342 | } else |
| 343 | offset = field->offset; |
Tom Zanussi | b1dcc03 | 2010-04-01 23:58:25 -0500 | [diff] [blame] | 344 | obj = PyString_FromString((char *)data + offset); |
Tom Zanussi | 7e4b21b | 2010-01-27 02:27:57 -0600 | [diff] [blame] | 345 | } else { /* FIELD_IS_NUMERIC */ |
Sebastian Andrzej Siewior | 33058b9 | 2014-05-27 18:14:33 +0200 | [diff] [blame] | 346 | obj = get_field_numeric_entry(event, field, data); |
Tom Zanussi | 7e4b21b | 2010-01-27 02:27:57 -0600 | [diff] [blame] | 347 | } |
Pierre Tardy | c025148 | 2010-05-31 23:12:09 +0200 | [diff] [blame] | 348 | if (handler) |
| 349 | PyTuple_SetItem(t, n++, obj); |
| 350 | else |
Joseph Schuchart | c0268e8 | 2013-10-24 10:10:51 -0300 | [diff] [blame] | 351 | pydict_set_item_string_decref(dict, field->name, obj); |
Pierre Tardy | c025148 | 2010-05-31 23:12:09 +0200 | [diff] [blame] | 352 | |
Tom Zanussi | 7e4b21b | 2010-01-27 02:27:57 -0600 | [diff] [blame] | 353 | } |
Pierre Tardy | c025148 | 2010-05-31 23:12:09 +0200 | [diff] [blame] | 354 | if (!handler) |
| 355 | PyTuple_SetItem(t, n++, dict); |
Tom Zanussi | 7e4b21b | 2010-01-27 02:27:57 -0600 | [diff] [blame] | 356 | |
| 357 | if (_PyTuple_Resize(&t, n) == -1) |
| 358 | Py_FatalError("error resizing Python tuple"); |
| 359 | |
Pierre Tardy | c025148 | 2010-05-31 23:12:09 +0200 | [diff] [blame] | 360 | if (handler) { |
Tom Zanussi | 7e4b21b | 2010-01-27 02:27:57 -0600 | [diff] [blame] | 361 | retval = PyObject_CallObject(handler, t); |
| 362 | if (retval == NULL) |
| 363 | handler_call_die(handler_name); |
| 364 | } else { |
| 365 | handler = PyDict_GetItemString(main_dict, "trace_unhandled"); |
| 366 | if (handler && PyCallable_Check(handler)) { |
Tom Zanussi | 7e4b21b | 2010-01-27 02:27:57 -0600 | [diff] [blame] | 367 | |
| 368 | retval = PyObject_CallObject(handler, t); |
| 369 | if (retval == NULL) |
| 370 | handler_call_die("trace_unhandled"); |
| 371 | } |
Pierre Tardy | c025148 | 2010-05-31 23:12:09 +0200 | [diff] [blame] | 372 | Py_DECREF(dict); |
Tom Zanussi | 7e4b21b | 2010-01-27 02:27:57 -0600 | [diff] [blame] | 373 | } |
| 374 | |
| 375 | Py_DECREF(t); |
| 376 | } |
| 377 | |
Arnaldo Carvalho de Melo | b7fff6b5 | 2013-12-19 16:34:52 -0300 | [diff] [blame] | 378 | static void python_process_general_event(struct perf_sample *sample, |
Feng Tang | 6a6daec | 2012-08-08 17:57:51 +0800 | [diff] [blame] | 379 | struct perf_evsel *evsel, |
David Ahern | 2eaa1b4 | 2013-07-18 16:06:15 -0600 | [diff] [blame] | 380 | struct thread *thread, |
Feng Tang | 87b6a3a | 2012-08-09 13:46:13 +0800 | [diff] [blame] | 381 | struct addr_location *al) |
Feng Tang | 6a6daec | 2012-08-08 17:57:51 +0800 | [diff] [blame] | 382 | { |
Feng Tang | fd6b858 | 2012-08-08 17:57:53 +0800 | [diff] [blame] | 383 | PyObject *handler, *retval, *t, *dict; |
Feng Tang | 6a6daec | 2012-08-08 17:57:51 +0800 | [diff] [blame] | 384 | static char handler_name[64]; |
| 385 | unsigned n = 0; |
Feng Tang | 6a6daec | 2012-08-08 17:57:51 +0800 | [diff] [blame] | 386 | |
Feng Tang | fd6b858 | 2012-08-08 17:57:53 +0800 | [diff] [blame] | 387 | /* |
| 388 | * Use the MAX_FIELDS to make the function expandable, though |
Feng Tang | 87b6a3a | 2012-08-09 13:46:13 +0800 | [diff] [blame] | 389 | * currently there is only one item for the tuple. |
Feng Tang | fd6b858 | 2012-08-08 17:57:53 +0800 | [diff] [blame] | 390 | */ |
Feng Tang | 6a6daec | 2012-08-08 17:57:51 +0800 | [diff] [blame] | 391 | t = PyTuple_New(MAX_FIELDS); |
| 392 | if (!t) |
| 393 | Py_FatalError("couldn't create Python tuple"); |
| 394 | |
Feng Tang | fd6b858 | 2012-08-08 17:57:53 +0800 | [diff] [blame] | 395 | dict = PyDict_New(); |
| 396 | if (!dict) |
| 397 | Py_FatalError("couldn't create Python dictionary"); |
| 398 | |
Feng Tang | 6a6daec | 2012-08-08 17:57:51 +0800 | [diff] [blame] | 399 | snprintf(handler_name, sizeof(handler_name), "%s", "process_event"); |
| 400 | |
| 401 | handler = PyDict_GetItemString(main_dict, handler_name); |
Feng Tang | 87b6a3a | 2012-08-09 13:46:13 +0800 | [diff] [blame] | 402 | if (!handler || !PyCallable_Check(handler)) |
Feng Tang | 6a6daec | 2012-08-08 17:57:51 +0800 | [diff] [blame] | 403 | goto exit; |
Feng Tang | 6a6daec | 2012-08-08 17:57:51 +0800 | [diff] [blame] | 404 | |
Joseph Schuchart | c0268e8 | 2013-10-24 10:10:51 -0300 | [diff] [blame] | 405 | pydict_set_item_string_decref(dict, "ev_name", PyString_FromString(perf_evsel__name(evsel))); |
| 406 | pydict_set_item_string_decref(dict, "attr", PyString_FromStringAndSize( |
Feng Tang | fd6b858 | 2012-08-08 17:57:53 +0800 | [diff] [blame] | 407 | (const char *)&evsel->attr, sizeof(evsel->attr))); |
Joseph Schuchart | c0268e8 | 2013-10-24 10:10:51 -0300 | [diff] [blame] | 408 | pydict_set_item_string_decref(dict, "sample", PyString_FromStringAndSize( |
Feng Tang | fd6b858 | 2012-08-08 17:57:53 +0800 | [diff] [blame] | 409 | (const char *)sample, sizeof(*sample))); |
Joseph Schuchart | c0268e8 | 2013-10-24 10:10:51 -0300 | [diff] [blame] | 410 | pydict_set_item_string_decref(dict, "raw_buf", PyString_FromStringAndSize( |
Feng Tang | fd6b858 | 2012-08-08 17:57:53 +0800 | [diff] [blame] | 411 | (const char *)sample->raw_data, sample->raw_size)); |
Joseph Schuchart | c0268e8 | 2013-10-24 10:10:51 -0300 | [diff] [blame] | 412 | pydict_set_item_string_decref(dict, "comm", |
Frederic Weisbecker | b9c5143 | 2013-09-11 14:46:56 +0200 | [diff] [blame] | 413 | PyString_FromString(thread__comm_str(thread))); |
Feng Tang | fd6b858 | 2012-08-08 17:57:53 +0800 | [diff] [blame] | 414 | if (al->map) { |
Joseph Schuchart | c0268e8 | 2013-10-24 10:10:51 -0300 | [diff] [blame] | 415 | pydict_set_item_string_decref(dict, "dso", |
Feng Tang | fd6b858 | 2012-08-08 17:57:53 +0800 | [diff] [blame] | 416 | PyString_FromString(al->map->dso->name)); |
| 417 | } |
| 418 | if (al->sym) { |
Joseph Schuchart | c0268e8 | 2013-10-24 10:10:51 -0300 | [diff] [blame] | 419 | pydict_set_item_string_decref(dict, "symbol", |
Feng Tang | fd6b858 | 2012-08-08 17:57:53 +0800 | [diff] [blame] | 420 | PyString_FromString(al->sym->name)); |
| 421 | } |
Feng Tang | 6a6daec | 2012-08-08 17:57:51 +0800 | [diff] [blame] | 422 | |
Feng Tang | fd6b858 | 2012-08-08 17:57:53 +0800 | [diff] [blame] | 423 | PyTuple_SetItem(t, n++, dict); |
Feng Tang | 6a6daec | 2012-08-08 17:57:51 +0800 | [diff] [blame] | 424 | if (_PyTuple_Resize(&t, n) == -1) |
| 425 | Py_FatalError("error resizing Python tuple"); |
| 426 | |
| 427 | retval = PyObject_CallObject(handler, t); |
| 428 | if (retval == NULL) |
| 429 | handler_call_die(handler_name); |
| 430 | exit: |
Feng Tang | fd6b858 | 2012-08-08 17:57:53 +0800 | [diff] [blame] | 431 | Py_DECREF(dict); |
Feng Tang | 6a6daec | 2012-08-08 17:57:51 +0800 | [diff] [blame] | 432 | Py_DECREF(t); |
| 433 | } |
| 434 | |
Arnaldo Carvalho de Melo | b7fff6b5 | 2013-12-19 16:34:52 -0300 | [diff] [blame] | 435 | static void python_process_event(union perf_event *event __maybe_unused, |
Feng Tang | 6a6daec | 2012-08-08 17:57:51 +0800 | [diff] [blame] | 436 | struct perf_sample *sample, |
| 437 | struct perf_evsel *evsel, |
David Ahern | 2eaa1b4 | 2013-07-18 16:06:15 -0600 | [diff] [blame] | 438 | struct thread *thread, |
Feng Tang | 73994dc | 2012-08-08 17:57:52 +0800 | [diff] [blame] | 439 | struct addr_location *al) |
Feng Tang | 6a6daec | 2012-08-08 17:57:51 +0800 | [diff] [blame] | 440 | { |
| 441 | switch (evsel->attr.type) { |
| 442 | case PERF_TYPE_TRACEPOINT: |
Arnaldo Carvalho de Melo | b7fff6b5 | 2013-12-19 16:34:52 -0300 | [diff] [blame] | 443 | python_process_tracepoint(sample, evsel, thread, al); |
Feng Tang | 6a6daec | 2012-08-08 17:57:51 +0800 | [diff] [blame] | 444 | break; |
| 445 | /* Reserve for future process_hw/sw/raw APIs */ |
| 446 | default: |
Arnaldo Carvalho de Melo | b7fff6b5 | 2013-12-19 16:34:52 -0300 | [diff] [blame] | 447 | python_process_general_event(sample, evsel, thread, al); |
Feng Tang | 6a6daec | 2012-08-08 17:57:51 +0800 | [diff] [blame] | 448 | } |
| 449 | } |
| 450 | |
Tom Zanussi | 7e4b21b | 2010-01-27 02:27:57 -0600 | [diff] [blame] | 451 | static int run_start_sub(void) |
| 452 | { |
| 453 | PyObject *handler, *retval; |
| 454 | int err = 0; |
| 455 | |
| 456 | main_module = PyImport_AddModule("__main__"); |
| 457 | if (main_module == NULL) |
| 458 | return -1; |
| 459 | Py_INCREF(main_module); |
| 460 | |
| 461 | main_dict = PyModule_GetDict(main_module); |
| 462 | if (main_dict == NULL) { |
| 463 | err = -1; |
| 464 | goto error; |
| 465 | } |
| 466 | Py_INCREF(main_dict); |
| 467 | |
| 468 | handler = PyDict_GetItemString(main_dict, "trace_begin"); |
| 469 | if (handler == NULL || !PyCallable_Check(handler)) |
| 470 | goto out; |
| 471 | |
| 472 | retval = PyObject_CallObject(handler, NULL); |
| 473 | if (retval == NULL) |
| 474 | handler_call_die("trace_begin"); |
| 475 | |
| 476 | Py_DECREF(retval); |
| 477 | return err; |
| 478 | error: |
| 479 | Py_XDECREF(main_dict); |
| 480 | Py_XDECREF(main_module); |
| 481 | out: |
| 482 | return err; |
| 483 | } |
| 484 | |
| 485 | /* |
| 486 | * Start trace script |
| 487 | */ |
| 488 | static int python_start_script(const char *script, int argc, const char **argv) |
| 489 | { |
| 490 | const char **command_line; |
| 491 | char buf[PATH_MAX]; |
| 492 | int i, err = 0; |
| 493 | FILE *fp; |
| 494 | |
| 495 | command_line = malloc((argc + 1) * sizeof(const char *)); |
| 496 | command_line[0] = script; |
| 497 | for (i = 1; i < argc + 1; i++) |
| 498 | command_line[i] = argv[i - 1]; |
| 499 | |
| 500 | Py_Initialize(); |
| 501 | |
| 502 | initperf_trace_context(); |
| 503 | |
| 504 | PySys_SetArgv(argc + 1, (char **)command_line); |
| 505 | |
| 506 | fp = fopen(script, "r"); |
| 507 | if (!fp) { |
| 508 | sprintf(buf, "Can't open python script \"%s\"", script); |
| 509 | perror(buf); |
| 510 | err = -1; |
| 511 | goto error; |
| 512 | } |
| 513 | |
| 514 | err = PyRun_SimpleFile(fp, script); |
| 515 | if (err) { |
| 516 | fprintf(stderr, "Error running python script %s\n", script); |
| 517 | goto error; |
| 518 | } |
| 519 | |
| 520 | err = run_start_sub(); |
| 521 | if (err) { |
| 522 | fprintf(stderr, "Error starting python script %s\n", script); |
| 523 | goto error; |
| 524 | } |
| 525 | |
| 526 | free(command_line); |
Tom Zanussi | 7e4b21b | 2010-01-27 02:27:57 -0600 | [diff] [blame] | 527 | |
| 528 | return err; |
| 529 | error: |
| 530 | Py_Finalize(); |
| 531 | free(command_line); |
| 532 | |
| 533 | return err; |
| 534 | } |
| 535 | |
| 536 | /* |
| 537 | * Stop trace script |
| 538 | */ |
| 539 | static int python_stop_script(void) |
| 540 | { |
| 541 | PyObject *handler, *retval; |
| 542 | int err = 0; |
| 543 | |
| 544 | handler = PyDict_GetItemString(main_dict, "trace_end"); |
| 545 | if (handler == NULL || !PyCallable_Check(handler)) |
| 546 | goto out; |
| 547 | |
| 548 | retval = PyObject_CallObject(handler, NULL); |
| 549 | if (retval == NULL) |
| 550 | handler_call_die("trace_end"); |
| 551 | else |
| 552 | Py_DECREF(retval); |
| 553 | out: |
| 554 | Py_XDECREF(main_dict); |
| 555 | Py_XDECREF(main_module); |
| 556 | Py_Finalize(); |
| 557 | |
Tom Zanussi | 7e4b21b | 2010-01-27 02:27:57 -0600 | [diff] [blame] | 558 | return err; |
| 559 | } |
| 560 | |
Arnaldo Carvalho de Melo | da37896 | 2012-06-27 13:08:42 -0300 | [diff] [blame] | 561 | static int python_generate_script(struct pevent *pevent, const char *outfile) |
Tom Zanussi | 7e4b21b | 2010-01-27 02:27:57 -0600 | [diff] [blame] | 562 | { |
Steven Rostedt | aaf045f | 2012-04-06 00:47:56 +0200 | [diff] [blame] | 563 | struct event_format *event = NULL; |
Tom Zanussi | 7e4b21b | 2010-01-27 02:27:57 -0600 | [diff] [blame] | 564 | struct format_field *f; |
| 565 | char fname[PATH_MAX]; |
| 566 | int not_first, count; |
| 567 | FILE *ofp; |
| 568 | |
| 569 | sprintf(fname, "%s.py", outfile); |
| 570 | ofp = fopen(fname, "w"); |
| 571 | if (ofp == NULL) { |
| 572 | fprintf(stderr, "couldn't open %s\n", fname); |
| 573 | return -1; |
| 574 | } |
Ingo Molnar | 133dc4c | 2010-11-16 18:45:39 +0100 | [diff] [blame] | 575 | fprintf(ofp, "# perf script event handlers, " |
| 576 | "generated by perf script -g python\n"); |
Tom Zanussi | 7e4b21b | 2010-01-27 02:27:57 -0600 | [diff] [blame] | 577 | |
| 578 | fprintf(ofp, "# Licensed under the terms of the GNU GPL" |
| 579 | " License version 2\n\n"); |
| 580 | |
| 581 | fprintf(ofp, "# The common_* event handler fields are the most useful " |
| 582 | "fields common to\n"); |
| 583 | |
| 584 | fprintf(ofp, "# all events. They don't necessarily correspond to " |
| 585 | "the 'common_*' fields\n"); |
| 586 | |
| 587 | fprintf(ofp, "# in the format files. Those fields not available as " |
| 588 | "handler params can\n"); |
| 589 | |
| 590 | fprintf(ofp, "# be retrieved using Python functions of the form " |
| 591 | "common_*(context).\n"); |
| 592 | |
| 593 | fprintf(ofp, "# See the perf-trace-python Documentation for the list " |
| 594 | "of available functions.\n\n"); |
| 595 | |
| 596 | fprintf(ofp, "import os\n"); |
| 597 | fprintf(ofp, "import sys\n\n"); |
| 598 | |
| 599 | fprintf(ofp, "sys.path.append(os.environ['PERF_EXEC_PATH'] + \\\n"); |
| 600 | fprintf(ofp, "\t'/scripts/python/Perf-Trace-Util/lib/Perf/Trace')\n"); |
| 601 | fprintf(ofp, "\nfrom perf_trace_context import *\n"); |
| 602 | fprintf(ofp, "from Core import *\n\n\n"); |
| 603 | |
| 604 | fprintf(ofp, "def trace_begin():\n"); |
| 605 | fprintf(ofp, "\tprint \"in trace_begin\"\n\n"); |
| 606 | |
| 607 | fprintf(ofp, "def trace_end():\n"); |
| 608 | fprintf(ofp, "\tprint \"in trace_end\"\n\n"); |
| 609 | |
Arnaldo Carvalho de Melo | da37896 | 2012-06-27 13:08:42 -0300 | [diff] [blame] | 610 | while ((event = trace_find_next_event(pevent, event))) { |
Tom Zanussi | 7e4b21b | 2010-01-27 02:27:57 -0600 | [diff] [blame] | 611 | fprintf(ofp, "def %s__%s(", event->system, event->name); |
| 612 | fprintf(ofp, "event_name, "); |
| 613 | fprintf(ofp, "context, "); |
| 614 | fprintf(ofp, "common_cpu,\n"); |
| 615 | fprintf(ofp, "\tcommon_secs, "); |
| 616 | fprintf(ofp, "common_nsecs, "); |
| 617 | fprintf(ofp, "common_pid, "); |
| 618 | fprintf(ofp, "common_comm,\n\t"); |
| 619 | |
| 620 | not_first = 0; |
| 621 | count = 0; |
| 622 | |
| 623 | for (f = event->format.fields; f; f = f->next) { |
| 624 | if (not_first++) |
| 625 | fprintf(ofp, ", "); |
| 626 | if (++count % 5 == 0) |
| 627 | fprintf(ofp, "\n\t"); |
| 628 | |
| 629 | fprintf(ofp, "%s", f->name); |
| 630 | } |
| 631 | fprintf(ofp, "):\n"); |
| 632 | |
| 633 | fprintf(ofp, "\t\tprint_header(event_name, common_cpu, " |
| 634 | "common_secs, common_nsecs,\n\t\t\t" |
| 635 | "common_pid, common_comm)\n\n"); |
| 636 | |
| 637 | fprintf(ofp, "\t\tprint \""); |
| 638 | |
| 639 | not_first = 0; |
| 640 | count = 0; |
| 641 | |
| 642 | for (f = event->format.fields; f; f = f->next) { |
| 643 | if (not_first++) |
| 644 | fprintf(ofp, ", "); |
| 645 | if (count && count % 3 == 0) { |
| 646 | fprintf(ofp, "\" \\\n\t\t\""); |
| 647 | } |
| 648 | count++; |
| 649 | |
| 650 | fprintf(ofp, "%s=", f->name); |
| 651 | if (f->flags & FIELD_IS_STRING || |
| 652 | f->flags & FIELD_IS_FLAG || |
Namhyung Kim | e646fe7 | 2014-05-29 13:44:55 +0900 | [diff] [blame] | 653 | f->flags & FIELD_IS_ARRAY || |
Tom Zanussi | 7e4b21b | 2010-01-27 02:27:57 -0600 | [diff] [blame] | 654 | f->flags & FIELD_IS_SYMBOLIC) |
| 655 | fprintf(ofp, "%%s"); |
| 656 | else if (f->flags & FIELD_IS_SIGNED) |
| 657 | fprintf(ofp, "%%d"); |
| 658 | else |
| 659 | fprintf(ofp, "%%u"); |
| 660 | } |
| 661 | |
| 662 | fprintf(ofp, "\\n\" %% \\\n\t\t("); |
| 663 | |
| 664 | not_first = 0; |
| 665 | count = 0; |
| 666 | |
| 667 | for (f = event->format.fields; f; f = f->next) { |
| 668 | if (not_first++) |
| 669 | fprintf(ofp, ", "); |
| 670 | |
| 671 | if (++count % 5 == 0) |
| 672 | fprintf(ofp, "\n\t\t"); |
| 673 | |
| 674 | if (f->flags & FIELD_IS_FLAG) { |
| 675 | if ((count - 1) % 5 != 0) { |
| 676 | fprintf(ofp, "\n\t\t"); |
| 677 | count = 4; |
| 678 | } |
| 679 | fprintf(ofp, "flag_str(\""); |
| 680 | fprintf(ofp, "%s__%s\", ", event->system, |
| 681 | event->name); |
| 682 | fprintf(ofp, "\"%s\", %s)", f->name, |
| 683 | f->name); |
| 684 | } else if (f->flags & FIELD_IS_SYMBOLIC) { |
| 685 | if ((count - 1) % 5 != 0) { |
| 686 | fprintf(ofp, "\n\t\t"); |
| 687 | count = 4; |
| 688 | } |
| 689 | fprintf(ofp, "symbol_str(\""); |
| 690 | fprintf(ofp, "%s__%s\", ", event->system, |
| 691 | event->name); |
| 692 | fprintf(ofp, "\"%s\", %s)", f->name, |
| 693 | f->name); |
| 694 | } else |
| 695 | fprintf(ofp, "%s", f->name); |
| 696 | } |
| 697 | |
| 698 | fprintf(ofp, "),\n\n"); |
| 699 | } |
| 700 | |
| 701 | fprintf(ofp, "def trace_unhandled(event_name, context, " |
Pierre Tardy | c025148 | 2010-05-31 23:12:09 +0200 | [diff] [blame] | 702 | "event_fields_dict):\n"); |
Tom Zanussi | 7e4b21b | 2010-01-27 02:27:57 -0600 | [diff] [blame] | 703 | |
Pierre Tardy | c025148 | 2010-05-31 23:12:09 +0200 | [diff] [blame] | 704 | fprintf(ofp, "\t\tprint ' '.join(['%%s=%%s'%%(k,str(v))" |
| 705 | "for k,v in sorted(event_fields_dict.items())])\n\n"); |
Tom Zanussi | 7e4b21b | 2010-01-27 02:27:57 -0600 | [diff] [blame] | 706 | |
| 707 | fprintf(ofp, "def print_header(" |
| 708 | "event_name, cpu, secs, nsecs, pid, comm):\n" |
| 709 | "\tprint \"%%-20s %%5u %%05u.%%09u %%8u %%-20s \" %% \\\n\t" |
| 710 | "(event_name, cpu, secs, nsecs, pid, comm),\n"); |
| 711 | |
| 712 | fclose(ofp); |
| 713 | |
| 714 | fprintf(stderr, "generated Python script: %s\n", fname); |
| 715 | |
| 716 | return 0; |
| 717 | } |
| 718 | |
| 719 | struct scripting_ops python_scripting_ops = { |
| 720 | .name = "Python", |
| 721 | .start_script = python_start_script, |
| 722 | .stop_script = python_stop_script, |
| 723 | .process_event = python_process_event, |
| 724 | .generate_script = python_generate_script, |
| 725 | }; |