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