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> |
Adrian Hunter | df919b4 | 2014-10-23 13:45:14 +0300 | [diff] [blame] | 27 | #include <stdbool.h> |
Tom Zanussi | 7e4b21b | 2010-01-27 02:27:57 -0600 | [diff] [blame] | 28 | #include <errno.h> |
Jiri Olsa | adf5bcf | 2014-10-26 23:44:05 +0100 | [diff] [blame] | 29 | #include <linux/bitmap.h> |
Tom Zanussi | 7e4b21b | 2010-01-27 02:27:57 -0600 | [diff] [blame] | 30 | |
| 31 | #include "../../perf.h" |
Jiri Olsa | 84f5d36 | 2014-07-14 23:46:48 +0200 | [diff] [blame] | 32 | #include "../debug.h" |
Arnaldo Carvalho de Melo | 8f651ea | 2014-10-09 16:12:24 -0300 | [diff] [blame] | 33 | #include "../callchain.h" |
Arnaldo Carvalho de Melo | fcf65bf | 2012-08-07 09:58:03 -0300 | [diff] [blame] | 34 | #include "../evsel.h" |
Tom Zanussi | 7e4b21b | 2010-01-27 02:27:57 -0600 | [diff] [blame] | 35 | #include "../util.h" |
Arnaldo Carvalho de Melo | 743eb86 | 2011-11-28 07:56:39 -0200 | [diff] [blame] | 36 | #include "../event.h" |
| 37 | #include "../thread.h" |
Adrian Hunter | df919b4 | 2014-10-23 13:45:14 +0300 | [diff] [blame] | 38 | #include "../comm.h" |
| 39 | #include "../machine.h" |
| 40 | #include "../db-export.h" |
Adrian Hunter | 6a70307 | 2014-10-30 16:09:47 +0200 | [diff] [blame] | 41 | #include "../thread-stack.h" |
Tom Zanussi | 7e4b21b | 2010-01-27 02:27:57 -0600 | [diff] [blame] | 42 | #include "../trace-event.h" |
Joseph Schuchart | 0f5f5bc | 2014-07-10 13:50:51 +0200 | [diff] [blame] | 43 | #include "../machine.h" |
Tom Zanussi | 7e4b21b | 2010-01-27 02:27:57 -0600 | [diff] [blame] | 44 | |
| 45 | PyMODINIT_FUNC initperf_trace_context(void); |
| 46 | |
| 47 | #define FTRACE_MAX_EVENT \ |
| 48 | ((1 << (sizeof(unsigned short) * 8)) - 1) |
| 49 | |
Jiri Olsa | adf5bcf | 2014-10-26 23:44:05 +0100 | [diff] [blame] | 50 | static DECLARE_BITMAP(events_defined, FTRACE_MAX_EVENT); |
Tom Zanussi | 7e4b21b | 2010-01-27 02:27:57 -0600 | [diff] [blame] | 51 | |
| 52 | #define MAX_FIELDS 64 |
| 53 | #define N_COMMON_FIELDS 7 |
| 54 | |
| 55 | extern struct scripting_context *scripting_context; |
| 56 | |
| 57 | static char *cur_field_name; |
| 58 | static int zero_flag_atom; |
| 59 | |
| 60 | static PyObject *main_module, *main_dict; |
| 61 | |
Adrian Hunter | df919b4 | 2014-10-23 13:45:14 +0300 | [diff] [blame] | 62 | struct tables { |
| 63 | struct db_export dbe; |
| 64 | PyObject *evsel_handler; |
| 65 | PyObject *machine_handler; |
| 66 | PyObject *thread_handler; |
| 67 | PyObject *comm_handler; |
| 68 | PyObject *comm_thread_handler; |
| 69 | PyObject *dso_handler; |
| 70 | PyObject *symbol_handler; |
Adrian Hunter | c29414f | 2014-10-30 16:09:44 +0200 | [diff] [blame] | 71 | PyObject *branch_type_handler; |
Adrian Hunter | df919b4 | 2014-10-23 13:45:14 +0300 | [diff] [blame] | 72 | PyObject *sample_handler; |
Adrian Hunter | 6a70307 | 2014-10-30 16:09:47 +0200 | [diff] [blame] | 73 | PyObject *call_path_handler; |
| 74 | PyObject *call_return_handler; |
Adrian Hunter | df919b4 | 2014-10-23 13:45:14 +0300 | [diff] [blame] | 75 | bool db_export_mode; |
| 76 | }; |
| 77 | |
| 78 | static struct tables tables_global; |
| 79 | |
Joseph Schuchart | 05f832e | 2014-07-09 16:16:31 +0200 | [diff] [blame] | 80 | static void handler_call_die(const char *handler_name) NORETURN; |
Tom Zanussi | 7e4b21b | 2010-01-27 02:27:57 -0600 | [diff] [blame] | 81 | static void handler_call_die(const char *handler_name) |
| 82 | { |
| 83 | PyErr_Print(); |
| 84 | Py_FatalError("problem in Python trace event handler"); |
Joseph Schuchart | 05f832e | 2014-07-09 16:16:31 +0200 | [diff] [blame] | 85 | // Py_FatalError does not return |
| 86 | // but we have to make the compiler happy |
| 87 | abort(); |
Tom Zanussi | 7e4b21b | 2010-01-27 02:27:57 -0600 | [diff] [blame] | 88 | } |
| 89 | |
Joseph Schuchart | c0268e8 | 2013-10-24 10:10:51 -0300 | [diff] [blame] | 90 | /* |
| 91 | * Insert val into into the dictionary and decrement the reference counter. |
Arnaldo Carvalho de Melo | 48000a1 | 2014-12-17 17:24:45 -0300 | [diff] [blame] | 92 | * This is necessary for dictionaries since PyDict_SetItemString() does not |
Joseph Schuchart | c0268e8 | 2013-10-24 10:10:51 -0300 | [diff] [blame] | 93 | * steal a reference, as opposed to PyTuple_SetItem(). |
| 94 | */ |
| 95 | static void pydict_set_item_string_decref(PyObject *dict, const char *key, PyObject *val) |
| 96 | { |
| 97 | PyDict_SetItemString(dict, key, val); |
| 98 | Py_DECREF(val); |
| 99 | } |
| 100 | |
Adrian Hunter | a5563ed | 2014-07-31 09:01:01 +0300 | [diff] [blame] | 101 | static PyObject *get_handler(const char *handler_name) |
| 102 | { |
| 103 | PyObject *handler; |
| 104 | |
| 105 | handler = PyDict_GetItemString(main_dict, handler_name); |
| 106 | if (handler && !PyCallable_Check(handler)) |
| 107 | return NULL; |
| 108 | return handler; |
| 109 | } |
| 110 | |
| 111 | static void call_object(PyObject *handler, PyObject *args, const char *die_msg) |
| 112 | { |
| 113 | PyObject *retval; |
| 114 | |
| 115 | retval = PyObject_CallObject(handler, args); |
| 116 | if (retval == NULL) |
| 117 | handler_call_die(die_msg); |
| 118 | Py_DECREF(retval); |
| 119 | } |
| 120 | |
| 121 | static void try_call_object(const char *handler_name, PyObject *args) |
| 122 | { |
| 123 | PyObject *handler; |
| 124 | |
| 125 | handler = get_handler(handler_name); |
| 126 | if (handler) |
| 127 | call_object(handler, args, handler_name); |
| 128 | } |
| 129 | |
Tom Zanussi | 7e4b21b | 2010-01-27 02:27:57 -0600 | [diff] [blame] | 130 | static void define_value(enum print_arg_type field_type, |
| 131 | const char *ev_name, |
| 132 | const char *field_name, |
| 133 | const char *field_value, |
| 134 | const char *field_str) |
| 135 | { |
| 136 | const char *handler_name = "define_flag_value"; |
Adrian Hunter | a5563ed | 2014-07-31 09:01:01 +0300 | [diff] [blame] | 137 | PyObject *t; |
Tom Zanussi | 7e4b21b | 2010-01-27 02:27:57 -0600 | [diff] [blame] | 138 | unsigned long long value; |
| 139 | unsigned n = 0; |
| 140 | |
| 141 | if (field_type == PRINT_SYMBOL) |
| 142 | handler_name = "define_symbolic_value"; |
| 143 | |
Tom Zanussi | 44ad9cd | 2010-02-22 01:12:59 -0600 | [diff] [blame] | 144 | t = PyTuple_New(4); |
Tom Zanussi | 7e4b21b | 2010-01-27 02:27:57 -0600 | [diff] [blame] | 145 | if (!t) |
| 146 | Py_FatalError("couldn't create Python tuple"); |
| 147 | |
| 148 | value = eval_flag(field_value); |
| 149 | |
| 150 | PyTuple_SetItem(t, n++, PyString_FromString(ev_name)); |
| 151 | PyTuple_SetItem(t, n++, PyString_FromString(field_name)); |
| 152 | PyTuple_SetItem(t, n++, PyInt_FromLong(value)); |
| 153 | PyTuple_SetItem(t, n++, PyString_FromString(field_str)); |
| 154 | |
Adrian Hunter | a5563ed | 2014-07-31 09:01:01 +0300 | [diff] [blame] | 155 | try_call_object(handler_name, t); |
Tom Zanussi | 7e4b21b | 2010-01-27 02:27:57 -0600 | [diff] [blame] | 156 | |
| 157 | Py_DECREF(t); |
| 158 | } |
| 159 | |
| 160 | static void define_values(enum print_arg_type field_type, |
| 161 | struct print_flag_sym *field, |
| 162 | const char *ev_name, |
| 163 | const char *field_name) |
| 164 | { |
| 165 | define_value(field_type, ev_name, field_name, field->value, |
| 166 | field->str); |
| 167 | |
| 168 | if (field->next) |
| 169 | define_values(field_type, field->next, ev_name, field_name); |
| 170 | } |
| 171 | |
| 172 | static void define_field(enum print_arg_type field_type, |
| 173 | const char *ev_name, |
| 174 | const char *field_name, |
| 175 | const char *delim) |
| 176 | { |
| 177 | const char *handler_name = "define_flag_field"; |
Adrian Hunter | a5563ed | 2014-07-31 09:01:01 +0300 | [diff] [blame] | 178 | PyObject *t; |
Tom Zanussi | 7e4b21b | 2010-01-27 02:27:57 -0600 | [diff] [blame] | 179 | unsigned n = 0; |
| 180 | |
| 181 | if (field_type == PRINT_SYMBOL) |
| 182 | handler_name = "define_symbolic_field"; |
| 183 | |
Tom Zanussi | 44ad9cd | 2010-02-22 01:12:59 -0600 | [diff] [blame] | 184 | if (field_type == PRINT_FLAGS) |
| 185 | t = PyTuple_New(3); |
| 186 | else |
| 187 | t = PyTuple_New(2); |
Tom Zanussi | 7e4b21b | 2010-01-27 02:27:57 -0600 | [diff] [blame] | 188 | if (!t) |
| 189 | Py_FatalError("couldn't create Python tuple"); |
| 190 | |
| 191 | PyTuple_SetItem(t, n++, PyString_FromString(ev_name)); |
| 192 | PyTuple_SetItem(t, n++, PyString_FromString(field_name)); |
| 193 | if (field_type == PRINT_FLAGS) |
| 194 | PyTuple_SetItem(t, n++, PyString_FromString(delim)); |
| 195 | |
Adrian Hunter | a5563ed | 2014-07-31 09:01:01 +0300 | [diff] [blame] | 196 | try_call_object(handler_name, t); |
Tom Zanussi | 7e4b21b | 2010-01-27 02:27:57 -0600 | [diff] [blame] | 197 | |
| 198 | Py_DECREF(t); |
| 199 | } |
| 200 | |
Steven Rostedt | aaf045f | 2012-04-06 00:47:56 +0200 | [diff] [blame] | 201 | static void define_event_symbols(struct event_format *event, |
Tom Zanussi | 7e4b21b | 2010-01-27 02:27:57 -0600 | [diff] [blame] | 202 | const char *ev_name, |
| 203 | struct print_arg *args) |
| 204 | { |
| 205 | switch (args->type) { |
| 206 | case PRINT_NULL: |
| 207 | break; |
| 208 | case PRINT_ATOM: |
| 209 | define_value(PRINT_FLAGS, ev_name, cur_field_name, "0", |
| 210 | args->atom.atom); |
| 211 | zero_flag_atom = 0; |
| 212 | break; |
| 213 | case PRINT_FIELD: |
Arnaldo Carvalho de Melo | f538565 | 2013-12-26 15:54:57 -0300 | [diff] [blame] | 214 | free(cur_field_name); |
Tom Zanussi | 7e4b21b | 2010-01-27 02:27:57 -0600 | [diff] [blame] | 215 | cur_field_name = strdup(args->field.name); |
| 216 | break; |
| 217 | case PRINT_FLAGS: |
| 218 | define_event_symbols(event, ev_name, args->flags.field); |
| 219 | define_field(PRINT_FLAGS, ev_name, cur_field_name, |
| 220 | args->flags.delim); |
| 221 | define_values(PRINT_FLAGS, args->flags.flags, ev_name, |
| 222 | cur_field_name); |
| 223 | break; |
| 224 | case PRINT_SYMBOL: |
| 225 | define_event_symbols(event, ev_name, args->symbol.field); |
| 226 | define_field(PRINT_SYMBOL, ev_name, cur_field_name, NULL); |
| 227 | define_values(PRINT_SYMBOL, args->symbol.symbols, ev_name, |
| 228 | cur_field_name); |
| 229 | break; |
Namhyung Kim | e080e6f | 2012-06-27 09:41:41 +0900 | [diff] [blame] | 230 | case PRINT_HEX: |
| 231 | define_event_symbols(event, ev_name, args->hex.field); |
| 232 | define_event_symbols(event, ev_name, args->hex.size); |
| 233 | break; |
Javi Merino | b839e1e8 | 2015-03-24 11:07:19 +0000 | [diff] [blame^] | 234 | case PRINT_INT_ARRAY: |
| 235 | define_event_symbols(event, ev_name, args->int_array.field); |
| 236 | define_event_symbols(event, ev_name, args->int_array.count); |
| 237 | define_event_symbols(event, ev_name, args->int_array.el_size); |
| 238 | break; |
Tom Zanussi | 7e4b21b | 2010-01-27 02:27:57 -0600 | [diff] [blame] | 239 | case PRINT_STRING: |
| 240 | break; |
| 241 | case PRINT_TYPE: |
| 242 | define_event_symbols(event, ev_name, args->typecast.item); |
| 243 | break; |
| 244 | case PRINT_OP: |
| 245 | if (strcmp(args->op.op, ":") == 0) |
| 246 | zero_flag_atom = 1; |
| 247 | define_event_symbols(event, ev_name, args->op.left); |
| 248 | define_event_symbols(event, ev_name, args->op.right); |
| 249 | break; |
| 250 | default: |
Steven Rostedt | aaf045f | 2012-04-06 00:47:56 +0200 | [diff] [blame] | 251 | /* gcc warns for these? */ |
| 252 | case PRINT_BSTRING: |
| 253 | case PRINT_DYNAMIC_ARRAY: |
| 254 | case PRINT_FUNC: |
Steven Rostedt (Red Hat) | 473a778 | 2014-06-02 23:20:16 -0400 | [diff] [blame] | 255 | case PRINT_BITMASK: |
Tom Zanussi | 7e4b21b | 2010-01-27 02:27:57 -0600 | [diff] [blame] | 256 | /* we should warn... */ |
| 257 | return; |
| 258 | } |
| 259 | |
| 260 | if (args->next) |
| 261 | define_event_symbols(event, ev_name, args->next); |
| 262 | } |
| 263 | |
Sebastian Andrzej Siewior | 33058b9 | 2014-05-27 18:14:33 +0200 | [diff] [blame] | 264 | static PyObject *get_field_numeric_entry(struct event_format *event, |
| 265 | struct format_field *field, void *data) |
| 266 | { |
Sebastian Andrzej Siewior | 8ac631c | 2014-05-27 18:14:34 +0200 | [diff] [blame] | 267 | bool is_array = field->flags & FIELD_IS_ARRAY; |
| 268 | PyObject *obj, *list = NULL; |
Sebastian Andrzej Siewior | 33058b9 | 2014-05-27 18:14:33 +0200 | [diff] [blame] | 269 | unsigned long long val; |
Sebastian Andrzej Siewior | 8ac631c | 2014-05-27 18:14:34 +0200 | [diff] [blame] | 270 | unsigned int item_size, n_items, i; |
Sebastian Andrzej Siewior | 33058b9 | 2014-05-27 18:14:33 +0200 | [diff] [blame] | 271 | |
Sebastian Andrzej Siewior | 8ac631c | 2014-05-27 18:14:34 +0200 | [diff] [blame] | 272 | if (is_array) { |
| 273 | list = PyList_New(field->arraylen); |
| 274 | item_size = field->size / field->arraylen; |
| 275 | n_items = field->arraylen; |
Sebastian Andrzej Siewior | 33058b9 | 2014-05-27 18:14:33 +0200 | [diff] [blame] | 276 | } else { |
Sebastian Andrzej Siewior | 8ac631c | 2014-05-27 18:14:34 +0200 | [diff] [blame] | 277 | item_size = field->size; |
| 278 | n_items = 1; |
Sebastian Andrzej Siewior | 33058b9 | 2014-05-27 18:14:33 +0200 | [diff] [blame] | 279 | } |
Sebastian Andrzej Siewior | 8ac631c | 2014-05-27 18:14:34 +0200 | [diff] [blame] | 280 | |
| 281 | for (i = 0; i < n_items; i++) { |
| 282 | |
| 283 | val = read_size(event, data + field->offset + i * item_size, |
| 284 | item_size); |
| 285 | if (field->flags & FIELD_IS_SIGNED) { |
| 286 | if ((long long)val >= LONG_MIN && |
| 287 | (long long)val <= LONG_MAX) |
| 288 | obj = PyInt_FromLong(val); |
| 289 | else |
| 290 | obj = PyLong_FromLongLong(val); |
| 291 | } else { |
| 292 | if (val <= LONG_MAX) |
| 293 | obj = PyInt_FromLong(val); |
| 294 | else |
| 295 | obj = PyLong_FromUnsignedLongLong(val); |
| 296 | } |
| 297 | if (is_array) |
| 298 | PyList_SET_ITEM(list, i, obj); |
| 299 | } |
| 300 | if (is_array) |
| 301 | obj = list; |
Sebastian Andrzej Siewior | 33058b9 | 2014-05-27 18:14:33 +0200 | [diff] [blame] | 302 | return obj; |
| 303 | } |
| 304 | |
Joseph Schuchart | 0f5f5bc | 2014-07-10 13:50:51 +0200 | [diff] [blame] | 305 | |
| 306 | static PyObject *python_process_callchain(struct perf_sample *sample, |
| 307 | struct perf_evsel *evsel, |
| 308 | struct addr_location *al) |
| 309 | { |
| 310 | PyObject *pylist; |
| 311 | |
| 312 | pylist = PyList_New(0); |
| 313 | if (!pylist) |
| 314 | Py_FatalError("couldn't create Python list"); |
| 315 | |
| 316 | if (!symbol_conf.use_callchain || !sample->callchain) |
| 317 | goto exit; |
| 318 | |
Arnaldo Carvalho de Melo | cc8b7c2 | 2014-10-23 15:26:17 -0300 | [diff] [blame] | 319 | if (thread__resolve_callchain(al->thread, evsel, |
| 320 | sample, NULL, NULL, |
| 321 | PERF_MAX_STACK_DEPTH) != 0) { |
Joseph Schuchart | 0f5f5bc | 2014-07-10 13:50:51 +0200 | [diff] [blame] | 322 | pr_err("Failed to resolve callchain. Skipping\n"); |
| 323 | goto exit; |
| 324 | } |
| 325 | callchain_cursor_commit(&callchain_cursor); |
| 326 | |
| 327 | |
| 328 | while (1) { |
| 329 | PyObject *pyelem; |
| 330 | struct callchain_cursor_node *node; |
| 331 | node = callchain_cursor_current(&callchain_cursor); |
| 332 | if (!node) |
| 333 | break; |
| 334 | |
| 335 | pyelem = PyDict_New(); |
| 336 | if (!pyelem) |
| 337 | Py_FatalError("couldn't create Python dictionary"); |
| 338 | |
| 339 | |
| 340 | pydict_set_item_string_decref(pyelem, "ip", |
| 341 | PyLong_FromUnsignedLongLong(node->ip)); |
| 342 | |
| 343 | if (node->sym) { |
| 344 | PyObject *pysym = PyDict_New(); |
| 345 | if (!pysym) |
| 346 | Py_FatalError("couldn't create Python dictionary"); |
| 347 | pydict_set_item_string_decref(pysym, "start", |
| 348 | PyLong_FromUnsignedLongLong(node->sym->start)); |
| 349 | pydict_set_item_string_decref(pysym, "end", |
| 350 | PyLong_FromUnsignedLongLong(node->sym->end)); |
| 351 | pydict_set_item_string_decref(pysym, "binding", |
| 352 | PyInt_FromLong(node->sym->binding)); |
| 353 | pydict_set_item_string_decref(pysym, "name", |
| 354 | PyString_FromStringAndSize(node->sym->name, |
| 355 | node->sym->namelen)); |
| 356 | pydict_set_item_string_decref(pyelem, "sym", pysym); |
| 357 | } |
| 358 | |
| 359 | if (node->map) { |
| 360 | struct map *map = node->map; |
| 361 | const char *dsoname = "[unknown]"; |
| 362 | if (map && map->dso && (map->dso->name || map->dso->long_name)) { |
| 363 | if (symbol_conf.show_kernel_path && map->dso->long_name) |
| 364 | dsoname = map->dso->long_name; |
| 365 | else if (map->dso->name) |
| 366 | dsoname = map->dso->name; |
| 367 | } |
| 368 | pydict_set_item_string_decref(pyelem, "dso", |
| 369 | PyString_FromString(dsoname)); |
| 370 | } |
| 371 | |
| 372 | callchain_cursor_advance(&callchain_cursor); |
| 373 | PyList_Append(pylist, pyelem); |
| 374 | Py_DECREF(pyelem); |
| 375 | } |
| 376 | |
| 377 | exit: |
| 378 | return pylist; |
| 379 | } |
| 380 | |
| 381 | |
Arnaldo Carvalho de Melo | b7fff6b5 | 2013-12-19 16:34:52 -0300 | [diff] [blame] | 382 | static void python_process_tracepoint(struct perf_sample *sample, |
| 383 | struct perf_evsel *evsel, |
| 384 | struct thread *thread, |
| 385 | struct addr_location *al) |
Tom Zanussi | 7e4b21b | 2010-01-27 02:27:57 -0600 | [diff] [blame] | 386 | { |
Jiri Olsa | adf5bcf | 2014-10-26 23:44:05 +0100 | [diff] [blame] | 387 | struct event_format *event = evsel->tp_format; |
Adrian Hunter | a5563ed | 2014-07-31 09:01:01 +0300 | [diff] [blame] | 388 | PyObject *handler, *context, *t, *obj, *callchain; |
Joseph Schuchart | 0f5f5bc | 2014-07-10 13:50:51 +0200 | [diff] [blame] | 389 | PyObject *dict = NULL; |
Tom Zanussi | 7e4b21b | 2010-01-27 02:27:57 -0600 | [diff] [blame] | 390 | static char handler_name[256]; |
| 391 | struct format_field *field; |
Tom Zanussi | 7e4b21b | 2010-01-27 02:27:57 -0600 | [diff] [blame] | 392 | unsigned long s, ns; |
Tom Zanussi | 7e4b21b | 2010-01-27 02:27:57 -0600 | [diff] [blame] | 393 | unsigned n = 0; |
Tom Zanussi | 7e4b21b | 2010-01-27 02:27:57 -0600 | [diff] [blame] | 394 | int pid; |
David Ahern | be6d842 | 2011-03-09 22:23:23 -0700 | [diff] [blame] | 395 | int cpu = sample->cpu; |
| 396 | void *data = sample->raw_data; |
| 397 | unsigned long long nsecs = sample->time; |
Frederic Weisbecker | b9c5143 | 2013-09-11 14:46:56 +0200 | [diff] [blame] | 398 | const char *comm = thread__comm_str(thread); |
Tom Zanussi | 7e4b21b | 2010-01-27 02:27:57 -0600 | [diff] [blame] | 399 | |
| 400 | t = PyTuple_New(MAX_FIELDS); |
| 401 | if (!t) |
| 402 | Py_FatalError("couldn't create Python tuple"); |
| 403 | |
Tom Zanussi | 7e4b21b | 2010-01-27 02:27:57 -0600 | [diff] [blame] | 404 | if (!event) |
Arnaldo Carvalho de Melo | fcf65bf | 2012-08-07 09:58:03 -0300 | [diff] [blame] | 405 | die("ug! no event found for type %d", (int)evsel->attr.config); |
Tom Zanussi | 7e4b21b | 2010-01-27 02:27:57 -0600 | [diff] [blame] | 406 | |
Arnaldo Carvalho de Melo | 9782243 | 2012-08-07 23:50:21 -0300 | [diff] [blame] | 407 | pid = raw_field_value(event, "common_pid", data); |
Tom Zanussi | 7e4b21b | 2010-01-27 02:27:57 -0600 | [diff] [blame] | 408 | |
| 409 | sprintf(handler_name, "%s__%s", event->system, event->name); |
| 410 | |
Jiri Olsa | adf5bcf | 2014-10-26 23:44:05 +0100 | [diff] [blame] | 411 | if (!test_and_set_bit(event->id, events_defined)) |
| 412 | define_event_symbols(event, handler_name, event->print_fmt.args); |
| 413 | |
Adrian Hunter | a5563ed | 2014-07-31 09:01:01 +0300 | [diff] [blame] | 414 | handler = get_handler(handler_name); |
Pierre Tardy | c025148 | 2010-05-31 23:12:09 +0200 | [diff] [blame] | 415 | if (!handler) { |
| 416 | dict = PyDict_New(); |
| 417 | if (!dict) |
| 418 | Py_FatalError("couldn't create Python dict"); |
| 419 | } |
Tom Zanussi | 7e4b21b | 2010-01-27 02:27:57 -0600 | [diff] [blame] | 420 | s = nsecs / NSECS_PER_SEC; |
| 421 | ns = nsecs - s * NSECS_PER_SEC; |
| 422 | |
| 423 | scripting_context->event_data = data; |
Tom Zanussi | 2de9533 | 2013-01-18 13:51:27 -0600 | [diff] [blame] | 424 | scripting_context->pevent = evsel->tp_format->pevent; |
Tom Zanussi | 7e4b21b | 2010-01-27 02:27:57 -0600 | [diff] [blame] | 425 | |
| 426 | context = PyCObject_FromVoidPtr(scripting_context, NULL); |
| 427 | |
| 428 | PyTuple_SetItem(t, n++, PyString_FromString(handler_name)); |
Kyle McMartin | fb7d0b3 | 2011-01-24 11:13:04 -0500 | [diff] [blame] | 429 | PyTuple_SetItem(t, n++, context); |
Tom Zanussi | 7e4b21b | 2010-01-27 02:27:57 -0600 | [diff] [blame] | 430 | |
Joseph Schuchart | 0f5f5bc | 2014-07-10 13:50:51 +0200 | [diff] [blame] | 431 | /* ip unwinding */ |
| 432 | callchain = python_process_callchain(sample, evsel, al); |
| 433 | |
Pierre Tardy | c025148 | 2010-05-31 23:12:09 +0200 | [diff] [blame] | 434 | if (handler) { |
| 435 | PyTuple_SetItem(t, n++, PyInt_FromLong(cpu)); |
| 436 | PyTuple_SetItem(t, n++, PyInt_FromLong(s)); |
| 437 | PyTuple_SetItem(t, n++, PyInt_FromLong(ns)); |
| 438 | PyTuple_SetItem(t, n++, PyInt_FromLong(pid)); |
| 439 | PyTuple_SetItem(t, n++, PyString_FromString(comm)); |
Joseph Schuchart | 0f5f5bc | 2014-07-10 13:50:51 +0200 | [diff] [blame] | 440 | PyTuple_SetItem(t, n++, callchain); |
Pierre Tardy | c025148 | 2010-05-31 23:12:09 +0200 | [diff] [blame] | 441 | } else { |
Joseph Schuchart | c0268e8 | 2013-10-24 10:10:51 -0300 | [diff] [blame] | 442 | pydict_set_item_string_decref(dict, "common_cpu", PyInt_FromLong(cpu)); |
| 443 | pydict_set_item_string_decref(dict, "common_s", PyInt_FromLong(s)); |
| 444 | pydict_set_item_string_decref(dict, "common_ns", PyInt_FromLong(ns)); |
| 445 | pydict_set_item_string_decref(dict, "common_pid", PyInt_FromLong(pid)); |
| 446 | pydict_set_item_string_decref(dict, "common_comm", PyString_FromString(comm)); |
Joseph Schuchart | 0f5f5bc | 2014-07-10 13:50:51 +0200 | [diff] [blame] | 447 | pydict_set_item_string_decref(dict, "common_callchain", callchain); |
Pierre Tardy | c025148 | 2010-05-31 23:12:09 +0200 | [diff] [blame] | 448 | } |
Tom Zanussi | 7e4b21b | 2010-01-27 02:27:57 -0600 | [diff] [blame] | 449 | for (field = event->format.fields; field; field = field->next) { |
| 450 | if (field->flags & FIELD_IS_STRING) { |
| 451 | int offset; |
| 452 | if (field->flags & FIELD_IS_DYNAMIC) { |
| 453 | offset = *(int *)(data + field->offset); |
| 454 | offset &= 0xffff; |
| 455 | } else |
| 456 | offset = field->offset; |
Tom Zanussi | b1dcc03 | 2010-04-01 23:58:25 -0500 | [diff] [blame] | 457 | obj = PyString_FromString((char *)data + offset); |
Tom Zanussi | 7e4b21b | 2010-01-27 02:27:57 -0600 | [diff] [blame] | 458 | } else { /* FIELD_IS_NUMERIC */ |
Sebastian Andrzej Siewior | 33058b9 | 2014-05-27 18:14:33 +0200 | [diff] [blame] | 459 | obj = get_field_numeric_entry(event, field, data); |
Tom Zanussi | 7e4b21b | 2010-01-27 02:27:57 -0600 | [diff] [blame] | 460 | } |
Pierre Tardy | c025148 | 2010-05-31 23:12:09 +0200 | [diff] [blame] | 461 | if (handler) |
| 462 | PyTuple_SetItem(t, n++, obj); |
| 463 | else |
Joseph Schuchart | c0268e8 | 2013-10-24 10:10:51 -0300 | [diff] [blame] | 464 | pydict_set_item_string_decref(dict, field->name, obj); |
Pierre Tardy | c025148 | 2010-05-31 23:12:09 +0200 | [diff] [blame] | 465 | |
Tom Zanussi | 7e4b21b | 2010-01-27 02:27:57 -0600 | [diff] [blame] | 466 | } |
Joseph Schuchart | 0f5f5bc | 2014-07-10 13:50:51 +0200 | [diff] [blame] | 467 | |
Pierre Tardy | c025148 | 2010-05-31 23:12:09 +0200 | [diff] [blame] | 468 | if (!handler) |
| 469 | PyTuple_SetItem(t, n++, dict); |
Tom Zanussi | 7e4b21b | 2010-01-27 02:27:57 -0600 | [diff] [blame] | 470 | |
| 471 | if (_PyTuple_Resize(&t, n) == -1) |
| 472 | Py_FatalError("error resizing Python tuple"); |
| 473 | |
Pierre Tardy | c025148 | 2010-05-31 23:12:09 +0200 | [diff] [blame] | 474 | if (handler) { |
Adrian Hunter | a5563ed | 2014-07-31 09:01:01 +0300 | [diff] [blame] | 475 | call_object(handler, t, handler_name); |
Tom Zanussi | 7e4b21b | 2010-01-27 02:27:57 -0600 | [diff] [blame] | 476 | } else { |
Adrian Hunter | a5563ed | 2014-07-31 09:01:01 +0300 | [diff] [blame] | 477 | try_call_object("trace_unhandled", t); |
Pierre Tardy | c025148 | 2010-05-31 23:12:09 +0200 | [diff] [blame] | 478 | Py_DECREF(dict); |
Tom Zanussi | 7e4b21b | 2010-01-27 02:27:57 -0600 | [diff] [blame] | 479 | } |
| 480 | |
| 481 | Py_DECREF(t); |
| 482 | } |
| 483 | |
Adrian Hunter | df919b4 | 2014-10-23 13:45:14 +0300 | [diff] [blame] | 484 | static PyObject *tuple_new(unsigned int sz) |
| 485 | { |
| 486 | PyObject *t; |
| 487 | |
| 488 | t = PyTuple_New(sz); |
| 489 | if (!t) |
| 490 | Py_FatalError("couldn't create Python tuple"); |
| 491 | return t; |
| 492 | } |
| 493 | |
| 494 | static int tuple_set_u64(PyObject *t, unsigned int pos, u64 val) |
| 495 | { |
| 496 | #if BITS_PER_LONG == 64 |
| 497 | return PyTuple_SetItem(t, pos, PyInt_FromLong(val)); |
| 498 | #endif |
| 499 | #if BITS_PER_LONG == 32 |
| 500 | return PyTuple_SetItem(t, pos, PyLong_FromLongLong(val)); |
| 501 | #endif |
| 502 | } |
| 503 | |
| 504 | static int tuple_set_s32(PyObject *t, unsigned int pos, s32 val) |
| 505 | { |
| 506 | return PyTuple_SetItem(t, pos, PyInt_FromLong(val)); |
| 507 | } |
| 508 | |
| 509 | static int tuple_set_string(PyObject *t, unsigned int pos, const char *s) |
| 510 | { |
| 511 | return PyTuple_SetItem(t, pos, PyString_FromString(s)); |
| 512 | } |
| 513 | |
| 514 | static int python_export_evsel(struct db_export *dbe, struct perf_evsel *evsel) |
| 515 | { |
| 516 | struct tables *tables = container_of(dbe, struct tables, dbe); |
| 517 | PyObject *t; |
| 518 | |
| 519 | t = tuple_new(2); |
| 520 | |
| 521 | tuple_set_u64(t, 0, evsel->db_id); |
| 522 | tuple_set_string(t, 1, perf_evsel__name(evsel)); |
| 523 | |
| 524 | call_object(tables->evsel_handler, t, "evsel_table"); |
| 525 | |
| 526 | Py_DECREF(t); |
| 527 | |
| 528 | return 0; |
| 529 | } |
| 530 | |
| 531 | static int python_export_machine(struct db_export *dbe, |
| 532 | struct machine *machine) |
| 533 | { |
| 534 | struct tables *tables = container_of(dbe, struct tables, dbe); |
| 535 | PyObject *t; |
| 536 | |
| 537 | t = tuple_new(3); |
| 538 | |
| 539 | tuple_set_u64(t, 0, machine->db_id); |
| 540 | tuple_set_s32(t, 1, machine->pid); |
| 541 | tuple_set_string(t, 2, machine->root_dir ? machine->root_dir : ""); |
| 542 | |
| 543 | call_object(tables->machine_handler, t, "machine_table"); |
| 544 | |
| 545 | Py_DECREF(t); |
| 546 | |
| 547 | return 0; |
| 548 | } |
| 549 | |
| 550 | static int python_export_thread(struct db_export *dbe, struct thread *thread, |
| 551 | u64 main_thread_db_id, struct machine *machine) |
| 552 | { |
| 553 | struct tables *tables = container_of(dbe, struct tables, dbe); |
| 554 | PyObject *t; |
| 555 | |
| 556 | t = tuple_new(5); |
| 557 | |
| 558 | tuple_set_u64(t, 0, thread->db_id); |
| 559 | tuple_set_u64(t, 1, machine->db_id); |
| 560 | tuple_set_u64(t, 2, main_thread_db_id); |
| 561 | tuple_set_s32(t, 3, thread->pid_); |
| 562 | tuple_set_s32(t, 4, thread->tid); |
| 563 | |
| 564 | call_object(tables->thread_handler, t, "thread_table"); |
| 565 | |
| 566 | Py_DECREF(t); |
| 567 | |
| 568 | return 0; |
| 569 | } |
| 570 | |
| 571 | static int python_export_comm(struct db_export *dbe, struct comm *comm) |
| 572 | { |
| 573 | struct tables *tables = container_of(dbe, struct tables, dbe); |
| 574 | PyObject *t; |
| 575 | |
| 576 | t = tuple_new(2); |
| 577 | |
| 578 | tuple_set_u64(t, 0, comm->db_id); |
| 579 | tuple_set_string(t, 1, comm__str(comm)); |
| 580 | |
| 581 | call_object(tables->comm_handler, t, "comm_table"); |
| 582 | |
| 583 | Py_DECREF(t); |
| 584 | |
| 585 | return 0; |
| 586 | } |
| 587 | |
| 588 | static int python_export_comm_thread(struct db_export *dbe, u64 db_id, |
| 589 | struct comm *comm, struct thread *thread) |
| 590 | { |
| 591 | struct tables *tables = container_of(dbe, struct tables, dbe); |
| 592 | PyObject *t; |
| 593 | |
| 594 | t = tuple_new(3); |
| 595 | |
| 596 | tuple_set_u64(t, 0, db_id); |
| 597 | tuple_set_u64(t, 1, comm->db_id); |
| 598 | tuple_set_u64(t, 2, thread->db_id); |
| 599 | |
| 600 | call_object(tables->comm_thread_handler, t, "comm_thread_table"); |
| 601 | |
| 602 | Py_DECREF(t); |
| 603 | |
| 604 | return 0; |
| 605 | } |
| 606 | |
| 607 | static int python_export_dso(struct db_export *dbe, struct dso *dso, |
| 608 | struct machine *machine) |
| 609 | { |
| 610 | struct tables *tables = container_of(dbe, struct tables, dbe); |
| 611 | char sbuild_id[BUILD_ID_SIZE * 2 + 1]; |
| 612 | PyObject *t; |
| 613 | |
| 614 | build_id__sprintf(dso->build_id, sizeof(dso->build_id), sbuild_id); |
| 615 | |
| 616 | t = tuple_new(5); |
| 617 | |
| 618 | tuple_set_u64(t, 0, dso->db_id); |
| 619 | tuple_set_u64(t, 1, machine->db_id); |
| 620 | tuple_set_string(t, 2, dso->short_name); |
| 621 | tuple_set_string(t, 3, dso->long_name); |
| 622 | tuple_set_string(t, 4, sbuild_id); |
| 623 | |
| 624 | call_object(tables->dso_handler, t, "dso_table"); |
| 625 | |
| 626 | Py_DECREF(t); |
| 627 | |
| 628 | return 0; |
| 629 | } |
| 630 | |
| 631 | static int python_export_symbol(struct db_export *dbe, struct symbol *sym, |
| 632 | struct dso *dso) |
| 633 | { |
| 634 | struct tables *tables = container_of(dbe, struct tables, dbe); |
| 635 | u64 *sym_db_id = symbol__priv(sym); |
| 636 | PyObject *t; |
| 637 | |
| 638 | t = tuple_new(6); |
| 639 | |
| 640 | tuple_set_u64(t, 0, *sym_db_id); |
| 641 | tuple_set_u64(t, 1, dso->db_id); |
| 642 | tuple_set_u64(t, 2, sym->start); |
| 643 | tuple_set_u64(t, 3, sym->end); |
| 644 | tuple_set_s32(t, 4, sym->binding); |
| 645 | tuple_set_string(t, 5, sym->name); |
| 646 | |
| 647 | call_object(tables->symbol_handler, t, "symbol_table"); |
| 648 | |
| 649 | Py_DECREF(t); |
| 650 | |
| 651 | return 0; |
| 652 | } |
| 653 | |
Adrian Hunter | c29414f | 2014-10-30 16:09:44 +0200 | [diff] [blame] | 654 | static int python_export_branch_type(struct db_export *dbe, u32 branch_type, |
| 655 | const char *name) |
| 656 | { |
| 657 | struct tables *tables = container_of(dbe, struct tables, dbe); |
| 658 | PyObject *t; |
| 659 | |
| 660 | t = tuple_new(2); |
| 661 | |
| 662 | tuple_set_s32(t, 0, branch_type); |
| 663 | tuple_set_string(t, 1, name); |
| 664 | |
| 665 | call_object(tables->branch_type_handler, t, "branch_type_table"); |
| 666 | |
| 667 | Py_DECREF(t); |
| 668 | |
| 669 | return 0; |
| 670 | } |
| 671 | |
Adrian Hunter | df919b4 | 2014-10-23 13:45:14 +0300 | [diff] [blame] | 672 | static int python_export_sample(struct db_export *dbe, |
| 673 | struct export_sample *es) |
| 674 | { |
| 675 | struct tables *tables = container_of(dbe, struct tables, dbe); |
| 676 | PyObject *t; |
| 677 | |
Adrian Hunter | c29414f | 2014-10-30 16:09:44 +0200 | [diff] [blame] | 678 | t = tuple_new(21); |
Adrian Hunter | df919b4 | 2014-10-23 13:45:14 +0300 | [diff] [blame] | 679 | |
| 680 | tuple_set_u64(t, 0, es->db_id); |
| 681 | tuple_set_u64(t, 1, es->evsel->db_id); |
| 682 | tuple_set_u64(t, 2, es->al->machine->db_id); |
| 683 | tuple_set_u64(t, 3, es->thread->db_id); |
| 684 | tuple_set_u64(t, 4, es->comm_db_id); |
| 685 | tuple_set_u64(t, 5, es->dso_db_id); |
| 686 | tuple_set_u64(t, 6, es->sym_db_id); |
| 687 | tuple_set_u64(t, 7, es->offset); |
| 688 | tuple_set_u64(t, 8, es->sample->ip); |
| 689 | tuple_set_u64(t, 9, es->sample->time); |
| 690 | tuple_set_s32(t, 10, es->sample->cpu); |
| 691 | tuple_set_u64(t, 11, es->addr_dso_db_id); |
| 692 | tuple_set_u64(t, 12, es->addr_sym_db_id); |
| 693 | tuple_set_u64(t, 13, es->addr_offset); |
| 694 | tuple_set_u64(t, 14, es->sample->addr); |
| 695 | tuple_set_u64(t, 15, es->sample->period); |
| 696 | tuple_set_u64(t, 16, es->sample->weight); |
| 697 | tuple_set_u64(t, 17, es->sample->transaction); |
| 698 | tuple_set_u64(t, 18, es->sample->data_src); |
Adrian Hunter | c29414f | 2014-10-30 16:09:44 +0200 | [diff] [blame] | 699 | tuple_set_s32(t, 19, es->sample->flags & PERF_BRANCH_MASK); |
| 700 | tuple_set_s32(t, 20, !!(es->sample->flags & PERF_IP_FLAG_IN_TX)); |
Adrian Hunter | df919b4 | 2014-10-23 13:45:14 +0300 | [diff] [blame] | 701 | |
| 702 | call_object(tables->sample_handler, t, "sample_table"); |
| 703 | |
| 704 | Py_DECREF(t); |
| 705 | |
| 706 | return 0; |
| 707 | } |
| 708 | |
Adrian Hunter | 6a70307 | 2014-10-30 16:09:47 +0200 | [diff] [blame] | 709 | static int python_export_call_path(struct db_export *dbe, struct call_path *cp) |
| 710 | { |
| 711 | struct tables *tables = container_of(dbe, struct tables, dbe); |
| 712 | PyObject *t; |
| 713 | u64 parent_db_id, sym_db_id; |
| 714 | |
| 715 | parent_db_id = cp->parent ? cp->parent->db_id : 0; |
| 716 | sym_db_id = cp->sym ? *(u64 *)symbol__priv(cp->sym) : 0; |
| 717 | |
| 718 | t = tuple_new(4); |
| 719 | |
| 720 | tuple_set_u64(t, 0, cp->db_id); |
| 721 | tuple_set_u64(t, 1, parent_db_id); |
| 722 | tuple_set_u64(t, 2, sym_db_id); |
| 723 | tuple_set_u64(t, 3, cp->ip); |
| 724 | |
| 725 | call_object(tables->call_path_handler, t, "call_path_table"); |
| 726 | |
| 727 | Py_DECREF(t); |
| 728 | |
| 729 | return 0; |
| 730 | } |
| 731 | |
| 732 | static int python_export_call_return(struct db_export *dbe, |
| 733 | struct call_return *cr) |
| 734 | { |
| 735 | struct tables *tables = container_of(dbe, struct tables, dbe); |
| 736 | u64 comm_db_id = cr->comm ? cr->comm->db_id : 0; |
| 737 | PyObject *t; |
| 738 | |
| 739 | t = tuple_new(11); |
| 740 | |
| 741 | tuple_set_u64(t, 0, cr->db_id); |
| 742 | tuple_set_u64(t, 1, cr->thread->db_id); |
| 743 | tuple_set_u64(t, 2, comm_db_id); |
| 744 | tuple_set_u64(t, 3, cr->cp->db_id); |
| 745 | tuple_set_u64(t, 4, cr->call_time); |
| 746 | tuple_set_u64(t, 5, cr->return_time); |
| 747 | tuple_set_u64(t, 6, cr->branch_count); |
| 748 | tuple_set_u64(t, 7, cr->call_ref); |
| 749 | tuple_set_u64(t, 8, cr->return_ref); |
| 750 | tuple_set_u64(t, 9, cr->cp->parent->db_id); |
| 751 | tuple_set_s32(t, 10, cr->flags); |
| 752 | |
| 753 | call_object(tables->call_return_handler, t, "call_return_table"); |
| 754 | |
| 755 | Py_DECREF(t); |
| 756 | |
| 757 | return 0; |
| 758 | } |
| 759 | |
| 760 | static int python_process_call_return(struct call_return *cr, void *data) |
| 761 | { |
| 762 | struct db_export *dbe = data; |
| 763 | |
| 764 | return db_export__call_return(dbe, cr); |
| 765 | } |
| 766 | |
Arnaldo Carvalho de Melo | b7fff6b5 | 2013-12-19 16:34:52 -0300 | [diff] [blame] | 767 | static void python_process_general_event(struct perf_sample *sample, |
Feng Tang | 6a6daec | 2012-08-08 17:57:51 +0800 | [diff] [blame] | 768 | struct perf_evsel *evsel, |
David Ahern | 2eaa1b4 | 2013-07-18 16:06:15 -0600 | [diff] [blame] | 769 | struct thread *thread, |
Feng Tang | 87b6a3a | 2012-08-09 13:46:13 +0800 | [diff] [blame] | 770 | struct addr_location *al) |
Feng Tang | 6a6daec | 2012-08-08 17:57:51 +0800 | [diff] [blame] | 771 | { |
Adrian Hunter | a5563ed | 2014-07-31 09:01:01 +0300 | [diff] [blame] | 772 | PyObject *handler, *t, *dict, *callchain, *dict_sample; |
Feng Tang | 6a6daec | 2012-08-08 17:57:51 +0800 | [diff] [blame] | 773 | static char handler_name[64]; |
| 774 | unsigned n = 0; |
Feng Tang | 6a6daec | 2012-08-08 17:57:51 +0800 | [diff] [blame] | 775 | |
Feng Tang | fd6b858 | 2012-08-08 17:57:53 +0800 | [diff] [blame] | 776 | /* |
| 777 | * Use the MAX_FIELDS to make the function expandable, though |
Feng Tang | 87b6a3a | 2012-08-09 13:46:13 +0800 | [diff] [blame] | 778 | * currently there is only one item for the tuple. |
Feng Tang | fd6b858 | 2012-08-08 17:57:53 +0800 | [diff] [blame] | 779 | */ |
Feng Tang | 6a6daec | 2012-08-08 17:57:51 +0800 | [diff] [blame] | 780 | t = PyTuple_New(MAX_FIELDS); |
| 781 | if (!t) |
| 782 | Py_FatalError("couldn't create Python tuple"); |
| 783 | |
Feng Tang | fd6b858 | 2012-08-08 17:57:53 +0800 | [diff] [blame] | 784 | dict = PyDict_New(); |
| 785 | if (!dict) |
| 786 | Py_FatalError("couldn't create Python dictionary"); |
| 787 | |
Joseph Schuchart | 57608cf | 2014-07-10 13:50:56 +0200 | [diff] [blame] | 788 | dict_sample = PyDict_New(); |
| 789 | if (!dict_sample) |
| 790 | Py_FatalError("couldn't create Python dictionary"); |
| 791 | |
Feng Tang | 6a6daec | 2012-08-08 17:57:51 +0800 | [diff] [blame] | 792 | snprintf(handler_name, sizeof(handler_name), "%s", "process_event"); |
| 793 | |
Adrian Hunter | a5563ed | 2014-07-31 09:01:01 +0300 | [diff] [blame] | 794 | handler = get_handler(handler_name); |
| 795 | if (!handler) |
Feng Tang | 6a6daec | 2012-08-08 17:57:51 +0800 | [diff] [blame] | 796 | goto exit; |
Feng Tang | 6a6daec | 2012-08-08 17:57:51 +0800 | [diff] [blame] | 797 | |
Joseph Schuchart | c0268e8 | 2013-10-24 10:10:51 -0300 | [diff] [blame] | 798 | pydict_set_item_string_decref(dict, "ev_name", PyString_FromString(perf_evsel__name(evsel))); |
| 799 | pydict_set_item_string_decref(dict, "attr", PyString_FromStringAndSize( |
Feng Tang | fd6b858 | 2012-08-08 17:57:53 +0800 | [diff] [blame] | 800 | (const char *)&evsel->attr, sizeof(evsel->attr))); |
Joseph Schuchart | 57608cf | 2014-07-10 13:50:56 +0200 | [diff] [blame] | 801 | |
| 802 | pydict_set_item_string_decref(dict_sample, "pid", |
| 803 | PyInt_FromLong(sample->pid)); |
| 804 | pydict_set_item_string_decref(dict_sample, "tid", |
| 805 | PyInt_FromLong(sample->tid)); |
| 806 | pydict_set_item_string_decref(dict_sample, "cpu", |
| 807 | PyInt_FromLong(sample->cpu)); |
| 808 | pydict_set_item_string_decref(dict_sample, "ip", |
| 809 | PyLong_FromUnsignedLongLong(sample->ip)); |
| 810 | pydict_set_item_string_decref(dict_sample, "time", |
| 811 | PyLong_FromUnsignedLongLong(sample->time)); |
| 812 | pydict_set_item_string_decref(dict_sample, "period", |
| 813 | PyLong_FromUnsignedLongLong(sample->period)); |
| 814 | pydict_set_item_string_decref(dict, "sample", dict_sample); |
| 815 | |
Joseph Schuchart | c0268e8 | 2013-10-24 10:10:51 -0300 | [diff] [blame] | 816 | pydict_set_item_string_decref(dict, "raw_buf", PyString_FromStringAndSize( |
Feng Tang | fd6b858 | 2012-08-08 17:57:53 +0800 | [diff] [blame] | 817 | (const char *)sample->raw_data, sample->raw_size)); |
Joseph Schuchart | c0268e8 | 2013-10-24 10:10:51 -0300 | [diff] [blame] | 818 | pydict_set_item_string_decref(dict, "comm", |
Frederic Weisbecker | b9c5143 | 2013-09-11 14:46:56 +0200 | [diff] [blame] | 819 | PyString_FromString(thread__comm_str(thread))); |
Feng Tang | fd6b858 | 2012-08-08 17:57:53 +0800 | [diff] [blame] | 820 | if (al->map) { |
Joseph Schuchart | c0268e8 | 2013-10-24 10:10:51 -0300 | [diff] [blame] | 821 | pydict_set_item_string_decref(dict, "dso", |
Feng Tang | fd6b858 | 2012-08-08 17:57:53 +0800 | [diff] [blame] | 822 | PyString_FromString(al->map->dso->name)); |
| 823 | } |
| 824 | if (al->sym) { |
Joseph Schuchart | c0268e8 | 2013-10-24 10:10:51 -0300 | [diff] [blame] | 825 | pydict_set_item_string_decref(dict, "symbol", |
Feng Tang | fd6b858 | 2012-08-08 17:57:53 +0800 | [diff] [blame] | 826 | PyString_FromString(al->sym->name)); |
| 827 | } |
Feng Tang | 6a6daec | 2012-08-08 17:57:51 +0800 | [diff] [blame] | 828 | |
Joseph Schuchart | 0f5f5bc | 2014-07-10 13:50:51 +0200 | [diff] [blame] | 829 | /* ip unwinding */ |
| 830 | callchain = python_process_callchain(sample, evsel, al); |
| 831 | pydict_set_item_string_decref(dict, "callchain", callchain); |
| 832 | |
Feng Tang | fd6b858 | 2012-08-08 17:57:53 +0800 | [diff] [blame] | 833 | PyTuple_SetItem(t, n++, dict); |
Feng Tang | 6a6daec | 2012-08-08 17:57:51 +0800 | [diff] [blame] | 834 | if (_PyTuple_Resize(&t, n) == -1) |
| 835 | Py_FatalError("error resizing Python tuple"); |
| 836 | |
Adrian Hunter | a5563ed | 2014-07-31 09:01:01 +0300 | [diff] [blame] | 837 | call_object(handler, t, handler_name); |
Feng Tang | 6a6daec | 2012-08-08 17:57:51 +0800 | [diff] [blame] | 838 | exit: |
Feng Tang | fd6b858 | 2012-08-08 17:57:53 +0800 | [diff] [blame] | 839 | Py_DECREF(dict); |
Feng Tang | 6a6daec | 2012-08-08 17:57:51 +0800 | [diff] [blame] | 840 | Py_DECREF(t); |
| 841 | } |
| 842 | |
Adrian Hunter | df919b4 | 2014-10-23 13:45:14 +0300 | [diff] [blame] | 843 | static void python_process_event(union perf_event *event, |
Feng Tang | 6a6daec | 2012-08-08 17:57:51 +0800 | [diff] [blame] | 844 | struct perf_sample *sample, |
| 845 | struct perf_evsel *evsel, |
David Ahern | 2eaa1b4 | 2013-07-18 16:06:15 -0600 | [diff] [blame] | 846 | struct thread *thread, |
Feng Tang | 73994dc | 2012-08-08 17:57:52 +0800 | [diff] [blame] | 847 | struct addr_location *al) |
Feng Tang | 6a6daec | 2012-08-08 17:57:51 +0800 | [diff] [blame] | 848 | { |
Adrian Hunter | df919b4 | 2014-10-23 13:45:14 +0300 | [diff] [blame] | 849 | struct tables *tables = &tables_global; |
| 850 | |
Feng Tang | 6a6daec | 2012-08-08 17:57:51 +0800 | [diff] [blame] | 851 | switch (evsel->attr.type) { |
| 852 | case PERF_TYPE_TRACEPOINT: |
Arnaldo Carvalho de Melo | b7fff6b5 | 2013-12-19 16:34:52 -0300 | [diff] [blame] | 853 | python_process_tracepoint(sample, evsel, thread, al); |
Feng Tang | 6a6daec | 2012-08-08 17:57:51 +0800 | [diff] [blame] | 854 | break; |
| 855 | /* Reserve for future process_hw/sw/raw APIs */ |
| 856 | default: |
Adrian Hunter | df919b4 | 2014-10-23 13:45:14 +0300 | [diff] [blame] | 857 | if (tables->db_export_mode) |
| 858 | db_export__sample(&tables->dbe, event, sample, evsel, |
| 859 | thread, al); |
| 860 | else |
| 861 | python_process_general_event(sample, evsel, thread, al); |
Feng Tang | 6a6daec | 2012-08-08 17:57:51 +0800 | [diff] [blame] | 862 | } |
| 863 | } |
| 864 | |
Tom Zanussi | 7e4b21b | 2010-01-27 02:27:57 -0600 | [diff] [blame] | 865 | static int run_start_sub(void) |
| 866 | { |
Tom Zanussi | 7e4b21b | 2010-01-27 02:27:57 -0600 | [diff] [blame] | 867 | main_module = PyImport_AddModule("__main__"); |
| 868 | if (main_module == NULL) |
| 869 | return -1; |
| 870 | Py_INCREF(main_module); |
| 871 | |
| 872 | main_dict = PyModule_GetDict(main_module); |
Adrian Hunter | a5563ed | 2014-07-31 09:01:01 +0300 | [diff] [blame] | 873 | if (main_dict == NULL) |
Tom Zanussi | 7e4b21b | 2010-01-27 02:27:57 -0600 | [diff] [blame] | 874 | goto error; |
Tom Zanussi | 7e4b21b | 2010-01-27 02:27:57 -0600 | [diff] [blame] | 875 | Py_INCREF(main_dict); |
| 876 | |
Adrian Hunter | a5563ed | 2014-07-31 09:01:01 +0300 | [diff] [blame] | 877 | try_call_object("trace_begin", NULL); |
Tom Zanussi | 7e4b21b | 2010-01-27 02:27:57 -0600 | [diff] [blame] | 878 | |
Adrian Hunter | a5563ed | 2014-07-31 09:01:01 +0300 | [diff] [blame] | 879 | return 0; |
Tom Zanussi | 7e4b21b | 2010-01-27 02:27:57 -0600 | [diff] [blame] | 880 | |
Tom Zanussi | 7e4b21b | 2010-01-27 02:27:57 -0600 | [diff] [blame] | 881 | error: |
| 882 | Py_XDECREF(main_dict); |
| 883 | Py_XDECREF(main_module); |
Adrian Hunter | a5563ed | 2014-07-31 09:01:01 +0300 | [diff] [blame] | 884 | return -1; |
Tom Zanussi | 7e4b21b | 2010-01-27 02:27:57 -0600 | [diff] [blame] | 885 | } |
| 886 | |
Adrian Hunter | df919b4 | 2014-10-23 13:45:14 +0300 | [diff] [blame] | 887 | #define SET_TABLE_HANDLER_(name, handler_name, table_name) do { \ |
| 888 | tables->handler_name = get_handler(#table_name); \ |
| 889 | if (tables->handler_name) \ |
| 890 | tables->dbe.export_ ## name = python_export_ ## name; \ |
| 891 | } while (0) |
| 892 | |
| 893 | #define SET_TABLE_HANDLER(name) \ |
| 894 | SET_TABLE_HANDLER_(name, name ## _handler, name ## _table) |
| 895 | |
| 896 | static void set_table_handlers(struct tables *tables) |
| 897 | { |
| 898 | const char *perf_db_export_mode = "perf_db_export_mode"; |
Adrian Hunter | 6a70307 | 2014-10-30 16:09:47 +0200 | [diff] [blame] | 899 | const char *perf_db_export_calls = "perf_db_export_calls"; |
| 900 | PyObject *db_export_mode, *db_export_calls; |
| 901 | bool export_calls = false; |
Adrian Hunter | df919b4 | 2014-10-23 13:45:14 +0300 | [diff] [blame] | 902 | int ret; |
| 903 | |
| 904 | memset(tables, 0, sizeof(struct tables)); |
| 905 | if (db_export__init(&tables->dbe)) |
| 906 | Py_FatalError("failed to initialize export"); |
| 907 | |
| 908 | db_export_mode = PyDict_GetItemString(main_dict, perf_db_export_mode); |
| 909 | if (!db_export_mode) |
| 910 | return; |
| 911 | |
| 912 | ret = PyObject_IsTrue(db_export_mode); |
| 913 | if (ret == -1) |
| 914 | handler_call_die(perf_db_export_mode); |
| 915 | if (!ret) |
| 916 | return; |
| 917 | |
Adrian Hunter | 6a70307 | 2014-10-30 16:09:47 +0200 | [diff] [blame] | 918 | tables->dbe.crp = NULL; |
| 919 | db_export_calls = PyDict_GetItemString(main_dict, perf_db_export_calls); |
| 920 | if (db_export_calls) { |
| 921 | ret = PyObject_IsTrue(db_export_calls); |
| 922 | if (ret == -1) |
| 923 | handler_call_die(perf_db_export_calls); |
| 924 | export_calls = !!ret; |
| 925 | } |
| 926 | |
| 927 | if (export_calls) { |
| 928 | tables->dbe.crp = |
| 929 | call_return_processor__new(python_process_call_return, |
| 930 | &tables->dbe); |
| 931 | if (!tables->dbe.crp) |
| 932 | Py_FatalError("failed to create calls processor"); |
| 933 | } |
| 934 | |
Adrian Hunter | df919b4 | 2014-10-23 13:45:14 +0300 | [diff] [blame] | 935 | tables->db_export_mode = true; |
| 936 | /* |
| 937 | * Reserve per symbol space for symbol->db_id via symbol__priv() |
| 938 | */ |
| 939 | symbol_conf.priv_size = sizeof(u64); |
| 940 | |
| 941 | SET_TABLE_HANDLER(evsel); |
| 942 | SET_TABLE_HANDLER(machine); |
| 943 | SET_TABLE_HANDLER(thread); |
| 944 | SET_TABLE_HANDLER(comm); |
| 945 | SET_TABLE_HANDLER(comm_thread); |
| 946 | SET_TABLE_HANDLER(dso); |
| 947 | SET_TABLE_HANDLER(symbol); |
Adrian Hunter | c29414f | 2014-10-30 16:09:44 +0200 | [diff] [blame] | 948 | SET_TABLE_HANDLER(branch_type); |
Adrian Hunter | df919b4 | 2014-10-23 13:45:14 +0300 | [diff] [blame] | 949 | SET_TABLE_HANDLER(sample); |
Adrian Hunter | 6a70307 | 2014-10-30 16:09:47 +0200 | [diff] [blame] | 950 | SET_TABLE_HANDLER(call_path); |
| 951 | SET_TABLE_HANDLER(call_return); |
Adrian Hunter | df919b4 | 2014-10-23 13:45:14 +0300 | [diff] [blame] | 952 | } |
| 953 | |
Tom Zanussi | 7e4b21b | 2010-01-27 02:27:57 -0600 | [diff] [blame] | 954 | /* |
| 955 | * Start trace script |
| 956 | */ |
| 957 | static int python_start_script(const char *script, int argc, const char **argv) |
| 958 | { |
Adrian Hunter | df919b4 | 2014-10-23 13:45:14 +0300 | [diff] [blame] | 959 | struct tables *tables = &tables_global; |
Tom Zanussi | 7e4b21b | 2010-01-27 02:27:57 -0600 | [diff] [blame] | 960 | const char **command_line; |
| 961 | char buf[PATH_MAX]; |
| 962 | int i, err = 0; |
| 963 | FILE *fp; |
| 964 | |
| 965 | command_line = malloc((argc + 1) * sizeof(const char *)); |
| 966 | command_line[0] = script; |
| 967 | for (i = 1; i < argc + 1; i++) |
| 968 | command_line[i] = argv[i - 1]; |
| 969 | |
| 970 | Py_Initialize(); |
| 971 | |
| 972 | initperf_trace_context(); |
| 973 | |
| 974 | PySys_SetArgv(argc + 1, (char **)command_line); |
| 975 | |
| 976 | fp = fopen(script, "r"); |
| 977 | if (!fp) { |
| 978 | sprintf(buf, "Can't open python script \"%s\"", script); |
| 979 | perror(buf); |
| 980 | err = -1; |
| 981 | goto error; |
| 982 | } |
| 983 | |
| 984 | err = PyRun_SimpleFile(fp, script); |
| 985 | if (err) { |
| 986 | fprintf(stderr, "Error running python script %s\n", script); |
| 987 | goto error; |
| 988 | } |
| 989 | |
| 990 | err = run_start_sub(); |
| 991 | if (err) { |
| 992 | fprintf(stderr, "Error starting python script %s\n", script); |
| 993 | goto error; |
| 994 | } |
| 995 | |
| 996 | free(command_line); |
Tom Zanussi | 7e4b21b | 2010-01-27 02:27:57 -0600 | [diff] [blame] | 997 | |
Adrian Hunter | df919b4 | 2014-10-23 13:45:14 +0300 | [diff] [blame] | 998 | set_table_handlers(tables); |
| 999 | |
Adrian Hunter | c29414f | 2014-10-30 16:09:44 +0200 | [diff] [blame] | 1000 | if (tables->db_export_mode) { |
| 1001 | err = db_export__branch_types(&tables->dbe); |
| 1002 | if (err) |
| 1003 | goto error; |
| 1004 | } |
| 1005 | |
Tom Zanussi | 7e4b21b | 2010-01-27 02:27:57 -0600 | [diff] [blame] | 1006 | return err; |
| 1007 | error: |
| 1008 | Py_Finalize(); |
| 1009 | free(command_line); |
| 1010 | |
| 1011 | return err; |
| 1012 | } |
| 1013 | |
Adrian Hunter | d445dd2 | 2014-08-15 22:08:37 +0300 | [diff] [blame] | 1014 | static int python_flush_script(void) |
| 1015 | { |
Adrian Hunter | 758008b | 2014-10-30 16:09:48 +0200 | [diff] [blame] | 1016 | struct tables *tables = &tables_global; |
| 1017 | |
| 1018 | return db_export__flush(&tables->dbe); |
Adrian Hunter | d445dd2 | 2014-08-15 22:08:37 +0300 | [diff] [blame] | 1019 | } |
| 1020 | |
Tom Zanussi | 7e4b21b | 2010-01-27 02:27:57 -0600 | [diff] [blame] | 1021 | /* |
| 1022 | * Stop trace script |
| 1023 | */ |
| 1024 | static int python_stop_script(void) |
| 1025 | { |
Adrian Hunter | df919b4 | 2014-10-23 13:45:14 +0300 | [diff] [blame] | 1026 | struct tables *tables = &tables_global; |
| 1027 | |
Adrian Hunter | a5563ed | 2014-07-31 09:01:01 +0300 | [diff] [blame] | 1028 | try_call_object("trace_end", NULL); |
Tom Zanussi | 7e4b21b | 2010-01-27 02:27:57 -0600 | [diff] [blame] | 1029 | |
Adrian Hunter | df919b4 | 2014-10-23 13:45:14 +0300 | [diff] [blame] | 1030 | db_export__exit(&tables->dbe); |
| 1031 | |
Tom Zanussi | 7e4b21b | 2010-01-27 02:27:57 -0600 | [diff] [blame] | 1032 | Py_XDECREF(main_dict); |
| 1033 | Py_XDECREF(main_module); |
| 1034 | Py_Finalize(); |
| 1035 | |
Adrian Hunter | a5563ed | 2014-07-31 09:01:01 +0300 | [diff] [blame] | 1036 | return 0; |
Tom Zanussi | 7e4b21b | 2010-01-27 02:27:57 -0600 | [diff] [blame] | 1037 | } |
| 1038 | |
Arnaldo Carvalho de Melo | da37896 | 2012-06-27 13:08:42 -0300 | [diff] [blame] | 1039 | static int python_generate_script(struct pevent *pevent, const char *outfile) |
Tom Zanussi | 7e4b21b | 2010-01-27 02:27:57 -0600 | [diff] [blame] | 1040 | { |
Steven Rostedt | aaf045f | 2012-04-06 00:47:56 +0200 | [diff] [blame] | 1041 | struct event_format *event = NULL; |
Tom Zanussi | 7e4b21b | 2010-01-27 02:27:57 -0600 | [diff] [blame] | 1042 | struct format_field *f; |
| 1043 | char fname[PATH_MAX]; |
| 1044 | int not_first, count; |
| 1045 | FILE *ofp; |
| 1046 | |
| 1047 | sprintf(fname, "%s.py", outfile); |
| 1048 | ofp = fopen(fname, "w"); |
| 1049 | if (ofp == NULL) { |
| 1050 | fprintf(stderr, "couldn't open %s\n", fname); |
| 1051 | return -1; |
| 1052 | } |
Ingo Molnar | 133dc4c | 2010-11-16 18:45:39 +0100 | [diff] [blame] | 1053 | fprintf(ofp, "# perf script event handlers, " |
| 1054 | "generated by perf script -g python\n"); |
Tom Zanussi | 7e4b21b | 2010-01-27 02:27:57 -0600 | [diff] [blame] | 1055 | |
| 1056 | fprintf(ofp, "# Licensed under the terms of the GNU GPL" |
| 1057 | " License version 2\n\n"); |
| 1058 | |
| 1059 | fprintf(ofp, "# The common_* event handler fields are the most useful " |
| 1060 | "fields common to\n"); |
| 1061 | |
| 1062 | fprintf(ofp, "# all events. They don't necessarily correspond to " |
| 1063 | "the 'common_*' fields\n"); |
| 1064 | |
| 1065 | fprintf(ofp, "# in the format files. Those fields not available as " |
| 1066 | "handler params can\n"); |
| 1067 | |
| 1068 | fprintf(ofp, "# be retrieved using Python functions of the form " |
| 1069 | "common_*(context).\n"); |
| 1070 | |
| 1071 | fprintf(ofp, "# See the perf-trace-python Documentation for the list " |
| 1072 | "of available functions.\n\n"); |
| 1073 | |
| 1074 | fprintf(ofp, "import os\n"); |
| 1075 | fprintf(ofp, "import sys\n\n"); |
| 1076 | |
| 1077 | fprintf(ofp, "sys.path.append(os.environ['PERF_EXEC_PATH'] + \\\n"); |
| 1078 | fprintf(ofp, "\t'/scripts/python/Perf-Trace-Util/lib/Perf/Trace')\n"); |
| 1079 | fprintf(ofp, "\nfrom perf_trace_context import *\n"); |
| 1080 | fprintf(ofp, "from Core import *\n\n\n"); |
| 1081 | |
| 1082 | fprintf(ofp, "def trace_begin():\n"); |
| 1083 | fprintf(ofp, "\tprint \"in trace_begin\"\n\n"); |
| 1084 | |
| 1085 | fprintf(ofp, "def trace_end():\n"); |
| 1086 | fprintf(ofp, "\tprint \"in trace_end\"\n\n"); |
| 1087 | |
Arnaldo Carvalho de Melo | da37896 | 2012-06-27 13:08:42 -0300 | [diff] [blame] | 1088 | while ((event = trace_find_next_event(pevent, event))) { |
Tom Zanussi | 7e4b21b | 2010-01-27 02:27:57 -0600 | [diff] [blame] | 1089 | fprintf(ofp, "def %s__%s(", event->system, event->name); |
| 1090 | fprintf(ofp, "event_name, "); |
| 1091 | fprintf(ofp, "context, "); |
| 1092 | fprintf(ofp, "common_cpu,\n"); |
| 1093 | fprintf(ofp, "\tcommon_secs, "); |
| 1094 | fprintf(ofp, "common_nsecs, "); |
| 1095 | fprintf(ofp, "common_pid, "); |
| 1096 | fprintf(ofp, "common_comm,\n\t"); |
Joseph Schuchart | 0f5f5bc | 2014-07-10 13:50:51 +0200 | [diff] [blame] | 1097 | fprintf(ofp, "common_callchain, "); |
Tom Zanussi | 7e4b21b | 2010-01-27 02:27:57 -0600 | [diff] [blame] | 1098 | |
| 1099 | not_first = 0; |
| 1100 | count = 0; |
| 1101 | |
| 1102 | for (f = event->format.fields; f; f = f->next) { |
| 1103 | if (not_first++) |
| 1104 | fprintf(ofp, ", "); |
| 1105 | if (++count % 5 == 0) |
| 1106 | fprintf(ofp, "\n\t"); |
| 1107 | |
| 1108 | fprintf(ofp, "%s", f->name); |
| 1109 | } |
| 1110 | fprintf(ofp, "):\n"); |
| 1111 | |
| 1112 | fprintf(ofp, "\t\tprint_header(event_name, common_cpu, " |
| 1113 | "common_secs, common_nsecs,\n\t\t\t" |
| 1114 | "common_pid, common_comm)\n\n"); |
| 1115 | |
| 1116 | fprintf(ofp, "\t\tprint \""); |
| 1117 | |
| 1118 | not_first = 0; |
| 1119 | count = 0; |
| 1120 | |
| 1121 | for (f = event->format.fields; f; f = f->next) { |
| 1122 | if (not_first++) |
| 1123 | fprintf(ofp, ", "); |
| 1124 | if (count && count % 3 == 0) { |
| 1125 | fprintf(ofp, "\" \\\n\t\t\""); |
| 1126 | } |
| 1127 | count++; |
| 1128 | |
| 1129 | fprintf(ofp, "%s=", f->name); |
| 1130 | if (f->flags & FIELD_IS_STRING || |
| 1131 | f->flags & FIELD_IS_FLAG || |
Namhyung Kim | e646fe7 | 2014-05-29 13:44:55 +0900 | [diff] [blame] | 1132 | f->flags & FIELD_IS_ARRAY || |
Tom Zanussi | 7e4b21b | 2010-01-27 02:27:57 -0600 | [diff] [blame] | 1133 | f->flags & FIELD_IS_SYMBOLIC) |
| 1134 | fprintf(ofp, "%%s"); |
| 1135 | else if (f->flags & FIELD_IS_SIGNED) |
| 1136 | fprintf(ofp, "%%d"); |
| 1137 | else |
| 1138 | fprintf(ofp, "%%u"); |
| 1139 | } |
| 1140 | |
Joseph Schuchart | 0f5f5bc | 2014-07-10 13:50:51 +0200 | [diff] [blame] | 1141 | fprintf(ofp, "\" %% \\\n\t\t("); |
Tom Zanussi | 7e4b21b | 2010-01-27 02:27:57 -0600 | [diff] [blame] | 1142 | |
| 1143 | not_first = 0; |
| 1144 | count = 0; |
| 1145 | |
| 1146 | for (f = event->format.fields; f; f = f->next) { |
| 1147 | if (not_first++) |
| 1148 | fprintf(ofp, ", "); |
| 1149 | |
| 1150 | if (++count % 5 == 0) |
| 1151 | fprintf(ofp, "\n\t\t"); |
| 1152 | |
| 1153 | if (f->flags & FIELD_IS_FLAG) { |
| 1154 | if ((count - 1) % 5 != 0) { |
| 1155 | fprintf(ofp, "\n\t\t"); |
| 1156 | count = 4; |
| 1157 | } |
| 1158 | fprintf(ofp, "flag_str(\""); |
| 1159 | fprintf(ofp, "%s__%s\", ", event->system, |
| 1160 | event->name); |
| 1161 | fprintf(ofp, "\"%s\", %s)", f->name, |
| 1162 | f->name); |
| 1163 | } else if (f->flags & FIELD_IS_SYMBOLIC) { |
| 1164 | if ((count - 1) % 5 != 0) { |
| 1165 | fprintf(ofp, "\n\t\t"); |
| 1166 | count = 4; |
| 1167 | } |
| 1168 | fprintf(ofp, "symbol_str(\""); |
| 1169 | fprintf(ofp, "%s__%s\", ", event->system, |
| 1170 | event->name); |
| 1171 | fprintf(ofp, "\"%s\", %s)", f->name, |
| 1172 | f->name); |
| 1173 | } else |
| 1174 | fprintf(ofp, "%s", f->name); |
| 1175 | } |
| 1176 | |
Joseph Schuchart | 0f5f5bc | 2014-07-10 13:50:51 +0200 | [diff] [blame] | 1177 | fprintf(ofp, ")\n\n"); |
| 1178 | |
| 1179 | fprintf(ofp, "\t\tfor node in common_callchain:"); |
| 1180 | fprintf(ofp, "\n\t\t\tif 'sym' in node:"); |
| 1181 | fprintf(ofp, "\n\t\t\t\tprint \"\\t[%%x] %%s\" %% (node['ip'], node['sym']['name'])"); |
| 1182 | fprintf(ofp, "\n\t\t\telse:"); |
| 1183 | fprintf(ofp, "\n\t\t\t\tprint \"\t[%%x]\" %% (node['ip'])\n\n"); |
| 1184 | fprintf(ofp, "\t\tprint \"\\n\"\n\n"); |
| 1185 | |
Tom Zanussi | 7e4b21b | 2010-01-27 02:27:57 -0600 | [diff] [blame] | 1186 | } |
| 1187 | |
| 1188 | fprintf(ofp, "def trace_unhandled(event_name, context, " |
Pierre Tardy | c025148 | 2010-05-31 23:12:09 +0200 | [diff] [blame] | 1189 | "event_fields_dict):\n"); |
Tom Zanussi | 7e4b21b | 2010-01-27 02:27:57 -0600 | [diff] [blame] | 1190 | |
Pierre Tardy | c025148 | 2010-05-31 23:12:09 +0200 | [diff] [blame] | 1191 | fprintf(ofp, "\t\tprint ' '.join(['%%s=%%s'%%(k,str(v))" |
| 1192 | "for k,v in sorted(event_fields_dict.items())])\n\n"); |
Tom Zanussi | 7e4b21b | 2010-01-27 02:27:57 -0600 | [diff] [blame] | 1193 | |
| 1194 | fprintf(ofp, "def print_header(" |
| 1195 | "event_name, cpu, secs, nsecs, pid, comm):\n" |
| 1196 | "\tprint \"%%-20s %%5u %%05u.%%09u %%8u %%-20s \" %% \\\n\t" |
| 1197 | "(event_name, cpu, secs, nsecs, pid, comm),\n"); |
| 1198 | |
| 1199 | fclose(ofp); |
| 1200 | |
| 1201 | fprintf(stderr, "generated Python script: %s\n", fname); |
| 1202 | |
| 1203 | return 0; |
| 1204 | } |
| 1205 | |
| 1206 | struct scripting_ops python_scripting_ops = { |
| 1207 | .name = "Python", |
| 1208 | .start_script = python_start_script, |
Adrian Hunter | d445dd2 | 2014-08-15 22:08:37 +0300 | [diff] [blame] | 1209 | .flush_script = python_flush_script, |
Tom Zanussi | 7e4b21b | 2010-01-27 02:27:57 -0600 | [diff] [blame] | 1210 | .stop_script = python_stop_script, |
| 1211 | .process_event = python_process_event, |
| 1212 | .generate_script = python_generate_script, |
| 1213 | }; |