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 | |
Steven Rostedt (Red Hat) | 609a740 | 2015-05-13 13:44:36 -0400 | [diff] [blame^] | 47 | #define TRACE_EVENT_TYPE_MAX \ |
Tom Zanussi | 7e4b21b | 2010-01-27 02:27:57 -0600 | [diff] [blame] | 48 | ((1 << (sizeof(unsigned short) * 8)) - 1) |
| 49 | |
Steven Rostedt (Red Hat) | 609a740 | 2015-05-13 13:44:36 -0400 | [diff] [blame^] | 50 | static DECLARE_BITMAP(events_defined, TRACE_EVENT_TYPE_MAX); |
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, |
Arnaldo Carvalho de Melo | b7fff6b5 | 2013-12-19 16:34:52 -0300 | [diff] [blame] | 384 | struct addr_location *al) |
Tom Zanussi | 7e4b21b | 2010-01-27 02:27:57 -0600 | [diff] [blame] | 385 | { |
Jiri Olsa | adf5bcf | 2014-10-26 23:44:05 +0100 | [diff] [blame] | 386 | struct event_format *event = evsel->tp_format; |
Adrian Hunter | a5563ed | 2014-07-31 09:01:01 +0300 | [diff] [blame] | 387 | PyObject *handler, *context, *t, *obj, *callchain; |
Joseph Schuchart | 0f5f5bc | 2014-07-10 13:50:51 +0200 | [diff] [blame] | 388 | PyObject *dict = NULL; |
Tom Zanussi | 7e4b21b | 2010-01-27 02:27:57 -0600 | [diff] [blame] | 389 | static char handler_name[256]; |
| 390 | struct format_field *field; |
Tom Zanussi | 7e4b21b | 2010-01-27 02:27:57 -0600 | [diff] [blame] | 391 | unsigned long s, ns; |
Tom Zanussi | 7e4b21b | 2010-01-27 02:27:57 -0600 | [diff] [blame] | 392 | unsigned n = 0; |
Tom Zanussi | 7e4b21b | 2010-01-27 02:27:57 -0600 | [diff] [blame] | 393 | int pid; |
David Ahern | be6d842 | 2011-03-09 22:23:23 -0700 | [diff] [blame] | 394 | int cpu = sample->cpu; |
| 395 | void *data = sample->raw_data; |
| 396 | unsigned long long nsecs = sample->time; |
Arnaldo Carvalho de Melo | f9d5d54 | 2015-04-01 13:29:25 -0300 | [diff] [blame] | 397 | const char *comm = thread__comm_str(al->thread); |
Tom Zanussi | 7e4b21b | 2010-01-27 02:27:57 -0600 | [diff] [blame] | 398 | |
| 399 | t = PyTuple_New(MAX_FIELDS); |
| 400 | if (!t) |
| 401 | Py_FatalError("couldn't create Python tuple"); |
| 402 | |
Tom Zanussi | 7e4b21b | 2010-01-27 02:27:57 -0600 | [diff] [blame] | 403 | if (!event) |
Arnaldo Carvalho de Melo | fcf65bf | 2012-08-07 09:58:03 -0300 | [diff] [blame] | 404 | die("ug! no event found for type %d", (int)evsel->attr.config); |
Tom Zanussi | 7e4b21b | 2010-01-27 02:27:57 -0600 | [diff] [blame] | 405 | |
Arnaldo Carvalho de Melo | 9782243 | 2012-08-07 23:50:21 -0300 | [diff] [blame] | 406 | pid = raw_field_value(event, "common_pid", data); |
Tom Zanussi | 7e4b21b | 2010-01-27 02:27:57 -0600 | [diff] [blame] | 407 | |
| 408 | sprintf(handler_name, "%s__%s", event->system, event->name); |
| 409 | |
Jiri Olsa | adf5bcf | 2014-10-26 23:44:05 +0100 | [diff] [blame] | 410 | if (!test_and_set_bit(event->id, events_defined)) |
| 411 | define_event_symbols(event, handler_name, event->print_fmt.args); |
| 412 | |
Adrian Hunter | a5563ed | 2014-07-31 09:01:01 +0300 | [diff] [blame] | 413 | handler = get_handler(handler_name); |
Pierre Tardy | c025148 | 2010-05-31 23:12:09 +0200 | [diff] [blame] | 414 | if (!handler) { |
| 415 | dict = PyDict_New(); |
| 416 | if (!dict) |
| 417 | Py_FatalError("couldn't create Python dict"); |
| 418 | } |
Tom Zanussi | 7e4b21b | 2010-01-27 02:27:57 -0600 | [diff] [blame] | 419 | s = nsecs / NSECS_PER_SEC; |
| 420 | ns = nsecs - s * NSECS_PER_SEC; |
| 421 | |
| 422 | scripting_context->event_data = data; |
Tom Zanussi | 2de9533 | 2013-01-18 13:51:27 -0600 | [diff] [blame] | 423 | scripting_context->pevent = evsel->tp_format->pevent; |
Tom Zanussi | 7e4b21b | 2010-01-27 02:27:57 -0600 | [diff] [blame] | 424 | |
| 425 | context = PyCObject_FromVoidPtr(scripting_context, NULL); |
| 426 | |
| 427 | PyTuple_SetItem(t, n++, PyString_FromString(handler_name)); |
Kyle McMartin | fb7d0b3 | 2011-01-24 11:13:04 -0500 | [diff] [blame] | 428 | PyTuple_SetItem(t, n++, context); |
Tom Zanussi | 7e4b21b | 2010-01-27 02:27:57 -0600 | [diff] [blame] | 429 | |
Joseph Schuchart | 0f5f5bc | 2014-07-10 13:50:51 +0200 | [diff] [blame] | 430 | /* ip unwinding */ |
| 431 | callchain = python_process_callchain(sample, evsel, al); |
| 432 | |
Pierre Tardy | c025148 | 2010-05-31 23:12:09 +0200 | [diff] [blame] | 433 | if (handler) { |
| 434 | PyTuple_SetItem(t, n++, PyInt_FromLong(cpu)); |
| 435 | PyTuple_SetItem(t, n++, PyInt_FromLong(s)); |
| 436 | PyTuple_SetItem(t, n++, PyInt_FromLong(ns)); |
| 437 | PyTuple_SetItem(t, n++, PyInt_FromLong(pid)); |
| 438 | PyTuple_SetItem(t, n++, PyString_FromString(comm)); |
Joseph Schuchart | 0f5f5bc | 2014-07-10 13:50:51 +0200 | [diff] [blame] | 439 | PyTuple_SetItem(t, n++, callchain); |
Pierre Tardy | c025148 | 2010-05-31 23:12:09 +0200 | [diff] [blame] | 440 | } else { |
Joseph Schuchart | c0268e8 | 2013-10-24 10:10:51 -0300 | [diff] [blame] | 441 | pydict_set_item_string_decref(dict, "common_cpu", PyInt_FromLong(cpu)); |
| 442 | pydict_set_item_string_decref(dict, "common_s", PyInt_FromLong(s)); |
| 443 | pydict_set_item_string_decref(dict, "common_ns", PyInt_FromLong(ns)); |
| 444 | pydict_set_item_string_decref(dict, "common_pid", PyInt_FromLong(pid)); |
| 445 | pydict_set_item_string_decref(dict, "common_comm", PyString_FromString(comm)); |
Joseph Schuchart | 0f5f5bc | 2014-07-10 13:50:51 +0200 | [diff] [blame] | 446 | pydict_set_item_string_decref(dict, "common_callchain", callchain); |
Pierre Tardy | c025148 | 2010-05-31 23:12:09 +0200 | [diff] [blame] | 447 | } |
Tom Zanussi | 7e4b21b | 2010-01-27 02:27:57 -0600 | [diff] [blame] | 448 | for (field = event->format.fields; field; field = field->next) { |
| 449 | if (field->flags & FIELD_IS_STRING) { |
| 450 | int offset; |
| 451 | if (field->flags & FIELD_IS_DYNAMIC) { |
| 452 | offset = *(int *)(data + field->offset); |
| 453 | offset &= 0xffff; |
| 454 | } else |
| 455 | offset = field->offset; |
Tom Zanussi | b1dcc03 | 2010-04-01 23:58:25 -0500 | [diff] [blame] | 456 | obj = PyString_FromString((char *)data + offset); |
Tom Zanussi | 7e4b21b | 2010-01-27 02:27:57 -0600 | [diff] [blame] | 457 | } else { /* FIELD_IS_NUMERIC */ |
Sebastian Andrzej Siewior | 33058b9 | 2014-05-27 18:14:33 +0200 | [diff] [blame] | 458 | obj = get_field_numeric_entry(event, field, data); |
Tom Zanussi | 7e4b21b | 2010-01-27 02:27:57 -0600 | [diff] [blame] | 459 | } |
Pierre Tardy | c025148 | 2010-05-31 23:12:09 +0200 | [diff] [blame] | 460 | if (handler) |
| 461 | PyTuple_SetItem(t, n++, obj); |
| 462 | else |
Joseph Schuchart | c0268e8 | 2013-10-24 10:10:51 -0300 | [diff] [blame] | 463 | pydict_set_item_string_decref(dict, field->name, obj); |
Pierre Tardy | c025148 | 2010-05-31 23:12:09 +0200 | [diff] [blame] | 464 | |
Tom Zanussi | 7e4b21b | 2010-01-27 02:27:57 -0600 | [diff] [blame] | 465 | } |
Joseph Schuchart | 0f5f5bc | 2014-07-10 13:50:51 +0200 | [diff] [blame] | 466 | |
Pierre Tardy | c025148 | 2010-05-31 23:12:09 +0200 | [diff] [blame] | 467 | if (!handler) |
| 468 | PyTuple_SetItem(t, n++, dict); |
Tom Zanussi | 7e4b21b | 2010-01-27 02:27:57 -0600 | [diff] [blame] | 469 | |
| 470 | if (_PyTuple_Resize(&t, n) == -1) |
| 471 | Py_FatalError("error resizing Python tuple"); |
| 472 | |
Pierre Tardy | c025148 | 2010-05-31 23:12:09 +0200 | [diff] [blame] | 473 | if (handler) { |
Adrian Hunter | a5563ed | 2014-07-31 09:01:01 +0300 | [diff] [blame] | 474 | call_object(handler, t, handler_name); |
Tom Zanussi | 7e4b21b | 2010-01-27 02:27:57 -0600 | [diff] [blame] | 475 | } else { |
Adrian Hunter | a5563ed | 2014-07-31 09:01:01 +0300 | [diff] [blame] | 476 | try_call_object("trace_unhandled", t); |
Pierre Tardy | c025148 | 2010-05-31 23:12:09 +0200 | [diff] [blame] | 477 | Py_DECREF(dict); |
Tom Zanussi | 7e4b21b | 2010-01-27 02:27:57 -0600 | [diff] [blame] | 478 | } |
| 479 | |
| 480 | Py_DECREF(t); |
| 481 | } |
| 482 | |
Adrian Hunter | df919b4 | 2014-10-23 13:45:14 +0300 | [diff] [blame] | 483 | static PyObject *tuple_new(unsigned int sz) |
| 484 | { |
| 485 | PyObject *t; |
| 486 | |
| 487 | t = PyTuple_New(sz); |
| 488 | if (!t) |
| 489 | Py_FatalError("couldn't create Python tuple"); |
| 490 | return t; |
| 491 | } |
| 492 | |
| 493 | static int tuple_set_u64(PyObject *t, unsigned int pos, u64 val) |
| 494 | { |
| 495 | #if BITS_PER_LONG == 64 |
| 496 | return PyTuple_SetItem(t, pos, PyInt_FromLong(val)); |
| 497 | #endif |
| 498 | #if BITS_PER_LONG == 32 |
| 499 | return PyTuple_SetItem(t, pos, PyLong_FromLongLong(val)); |
| 500 | #endif |
| 501 | } |
| 502 | |
| 503 | static int tuple_set_s32(PyObject *t, unsigned int pos, s32 val) |
| 504 | { |
| 505 | return PyTuple_SetItem(t, pos, PyInt_FromLong(val)); |
| 506 | } |
| 507 | |
| 508 | static int tuple_set_string(PyObject *t, unsigned int pos, const char *s) |
| 509 | { |
| 510 | return PyTuple_SetItem(t, pos, PyString_FromString(s)); |
| 511 | } |
| 512 | |
| 513 | static int python_export_evsel(struct db_export *dbe, struct perf_evsel *evsel) |
| 514 | { |
| 515 | struct tables *tables = container_of(dbe, struct tables, dbe); |
| 516 | PyObject *t; |
| 517 | |
| 518 | t = tuple_new(2); |
| 519 | |
| 520 | tuple_set_u64(t, 0, evsel->db_id); |
| 521 | tuple_set_string(t, 1, perf_evsel__name(evsel)); |
| 522 | |
| 523 | call_object(tables->evsel_handler, t, "evsel_table"); |
| 524 | |
| 525 | Py_DECREF(t); |
| 526 | |
| 527 | return 0; |
| 528 | } |
| 529 | |
| 530 | static int python_export_machine(struct db_export *dbe, |
| 531 | struct machine *machine) |
| 532 | { |
| 533 | struct tables *tables = container_of(dbe, struct tables, dbe); |
| 534 | PyObject *t; |
| 535 | |
| 536 | t = tuple_new(3); |
| 537 | |
| 538 | tuple_set_u64(t, 0, machine->db_id); |
| 539 | tuple_set_s32(t, 1, machine->pid); |
| 540 | tuple_set_string(t, 2, machine->root_dir ? machine->root_dir : ""); |
| 541 | |
| 542 | call_object(tables->machine_handler, t, "machine_table"); |
| 543 | |
| 544 | Py_DECREF(t); |
| 545 | |
| 546 | return 0; |
| 547 | } |
| 548 | |
| 549 | static int python_export_thread(struct db_export *dbe, struct thread *thread, |
| 550 | u64 main_thread_db_id, struct machine *machine) |
| 551 | { |
| 552 | struct tables *tables = container_of(dbe, struct tables, dbe); |
| 553 | PyObject *t; |
| 554 | |
| 555 | t = tuple_new(5); |
| 556 | |
| 557 | tuple_set_u64(t, 0, thread->db_id); |
| 558 | tuple_set_u64(t, 1, machine->db_id); |
| 559 | tuple_set_u64(t, 2, main_thread_db_id); |
| 560 | tuple_set_s32(t, 3, thread->pid_); |
| 561 | tuple_set_s32(t, 4, thread->tid); |
| 562 | |
| 563 | call_object(tables->thread_handler, t, "thread_table"); |
| 564 | |
| 565 | Py_DECREF(t); |
| 566 | |
| 567 | return 0; |
| 568 | } |
| 569 | |
| 570 | static int python_export_comm(struct db_export *dbe, struct comm *comm) |
| 571 | { |
| 572 | struct tables *tables = container_of(dbe, struct tables, dbe); |
| 573 | PyObject *t; |
| 574 | |
| 575 | t = tuple_new(2); |
| 576 | |
| 577 | tuple_set_u64(t, 0, comm->db_id); |
| 578 | tuple_set_string(t, 1, comm__str(comm)); |
| 579 | |
| 580 | call_object(tables->comm_handler, t, "comm_table"); |
| 581 | |
| 582 | Py_DECREF(t); |
| 583 | |
| 584 | return 0; |
| 585 | } |
| 586 | |
| 587 | static int python_export_comm_thread(struct db_export *dbe, u64 db_id, |
| 588 | struct comm *comm, struct thread *thread) |
| 589 | { |
| 590 | struct tables *tables = container_of(dbe, struct tables, dbe); |
| 591 | PyObject *t; |
| 592 | |
| 593 | t = tuple_new(3); |
| 594 | |
| 595 | tuple_set_u64(t, 0, db_id); |
| 596 | tuple_set_u64(t, 1, comm->db_id); |
| 597 | tuple_set_u64(t, 2, thread->db_id); |
| 598 | |
| 599 | call_object(tables->comm_thread_handler, t, "comm_thread_table"); |
| 600 | |
| 601 | Py_DECREF(t); |
| 602 | |
| 603 | return 0; |
| 604 | } |
| 605 | |
| 606 | static int python_export_dso(struct db_export *dbe, struct dso *dso, |
| 607 | struct machine *machine) |
| 608 | { |
| 609 | struct tables *tables = container_of(dbe, struct tables, dbe); |
| 610 | char sbuild_id[BUILD_ID_SIZE * 2 + 1]; |
| 611 | PyObject *t; |
| 612 | |
| 613 | build_id__sprintf(dso->build_id, sizeof(dso->build_id), sbuild_id); |
| 614 | |
| 615 | t = tuple_new(5); |
| 616 | |
| 617 | tuple_set_u64(t, 0, dso->db_id); |
| 618 | tuple_set_u64(t, 1, machine->db_id); |
| 619 | tuple_set_string(t, 2, dso->short_name); |
| 620 | tuple_set_string(t, 3, dso->long_name); |
| 621 | tuple_set_string(t, 4, sbuild_id); |
| 622 | |
| 623 | call_object(tables->dso_handler, t, "dso_table"); |
| 624 | |
| 625 | Py_DECREF(t); |
| 626 | |
| 627 | return 0; |
| 628 | } |
| 629 | |
| 630 | static int python_export_symbol(struct db_export *dbe, struct symbol *sym, |
| 631 | struct dso *dso) |
| 632 | { |
| 633 | struct tables *tables = container_of(dbe, struct tables, dbe); |
| 634 | u64 *sym_db_id = symbol__priv(sym); |
| 635 | PyObject *t; |
| 636 | |
| 637 | t = tuple_new(6); |
| 638 | |
| 639 | tuple_set_u64(t, 0, *sym_db_id); |
| 640 | tuple_set_u64(t, 1, dso->db_id); |
| 641 | tuple_set_u64(t, 2, sym->start); |
| 642 | tuple_set_u64(t, 3, sym->end); |
| 643 | tuple_set_s32(t, 4, sym->binding); |
| 644 | tuple_set_string(t, 5, sym->name); |
| 645 | |
| 646 | call_object(tables->symbol_handler, t, "symbol_table"); |
| 647 | |
| 648 | Py_DECREF(t); |
| 649 | |
| 650 | return 0; |
| 651 | } |
| 652 | |
Adrian Hunter | c29414f | 2014-10-30 16:09:44 +0200 | [diff] [blame] | 653 | static int python_export_branch_type(struct db_export *dbe, u32 branch_type, |
| 654 | const char *name) |
| 655 | { |
| 656 | struct tables *tables = container_of(dbe, struct tables, dbe); |
| 657 | PyObject *t; |
| 658 | |
| 659 | t = tuple_new(2); |
| 660 | |
| 661 | tuple_set_s32(t, 0, branch_type); |
| 662 | tuple_set_string(t, 1, name); |
| 663 | |
| 664 | call_object(tables->branch_type_handler, t, "branch_type_table"); |
| 665 | |
| 666 | Py_DECREF(t); |
| 667 | |
| 668 | return 0; |
| 669 | } |
| 670 | |
Adrian Hunter | df919b4 | 2014-10-23 13:45:14 +0300 | [diff] [blame] | 671 | static int python_export_sample(struct db_export *dbe, |
| 672 | struct export_sample *es) |
| 673 | { |
| 674 | struct tables *tables = container_of(dbe, struct tables, dbe); |
| 675 | PyObject *t; |
| 676 | |
Adrian Hunter | c29414f | 2014-10-30 16:09:44 +0200 | [diff] [blame] | 677 | t = tuple_new(21); |
Adrian Hunter | df919b4 | 2014-10-23 13:45:14 +0300 | [diff] [blame] | 678 | |
| 679 | tuple_set_u64(t, 0, es->db_id); |
| 680 | tuple_set_u64(t, 1, es->evsel->db_id); |
| 681 | tuple_set_u64(t, 2, es->al->machine->db_id); |
Arnaldo Carvalho de Melo | b83e868 | 2015-04-02 11:16:05 -0300 | [diff] [blame] | 682 | tuple_set_u64(t, 3, es->al->thread->db_id); |
Adrian Hunter | df919b4 | 2014-10-23 13:45:14 +0300 | [diff] [blame] | 683 | tuple_set_u64(t, 4, es->comm_db_id); |
| 684 | tuple_set_u64(t, 5, es->dso_db_id); |
| 685 | tuple_set_u64(t, 6, es->sym_db_id); |
| 686 | tuple_set_u64(t, 7, es->offset); |
| 687 | tuple_set_u64(t, 8, es->sample->ip); |
| 688 | tuple_set_u64(t, 9, es->sample->time); |
| 689 | tuple_set_s32(t, 10, es->sample->cpu); |
| 690 | tuple_set_u64(t, 11, es->addr_dso_db_id); |
| 691 | tuple_set_u64(t, 12, es->addr_sym_db_id); |
| 692 | tuple_set_u64(t, 13, es->addr_offset); |
| 693 | tuple_set_u64(t, 14, es->sample->addr); |
| 694 | tuple_set_u64(t, 15, es->sample->period); |
| 695 | tuple_set_u64(t, 16, es->sample->weight); |
| 696 | tuple_set_u64(t, 17, es->sample->transaction); |
| 697 | tuple_set_u64(t, 18, es->sample->data_src); |
Adrian Hunter | c29414f | 2014-10-30 16:09:44 +0200 | [diff] [blame] | 698 | tuple_set_s32(t, 19, es->sample->flags & PERF_BRANCH_MASK); |
| 699 | 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] | 700 | |
| 701 | call_object(tables->sample_handler, t, "sample_table"); |
| 702 | |
| 703 | Py_DECREF(t); |
| 704 | |
| 705 | return 0; |
| 706 | } |
| 707 | |
Adrian Hunter | 6a70307 | 2014-10-30 16:09:47 +0200 | [diff] [blame] | 708 | static int python_export_call_path(struct db_export *dbe, struct call_path *cp) |
| 709 | { |
| 710 | struct tables *tables = container_of(dbe, struct tables, dbe); |
| 711 | PyObject *t; |
| 712 | u64 parent_db_id, sym_db_id; |
| 713 | |
| 714 | parent_db_id = cp->parent ? cp->parent->db_id : 0; |
| 715 | sym_db_id = cp->sym ? *(u64 *)symbol__priv(cp->sym) : 0; |
| 716 | |
| 717 | t = tuple_new(4); |
| 718 | |
| 719 | tuple_set_u64(t, 0, cp->db_id); |
| 720 | tuple_set_u64(t, 1, parent_db_id); |
| 721 | tuple_set_u64(t, 2, sym_db_id); |
| 722 | tuple_set_u64(t, 3, cp->ip); |
| 723 | |
| 724 | call_object(tables->call_path_handler, t, "call_path_table"); |
| 725 | |
| 726 | Py_DECREF(t); |
| 727 | |
| 728 | return 0; |
| 729 | } |
| 730 | |
| 731 | static int python_export_call_return(struct db_export *dbe, |
| 732 | struct call_return *cr) |
| 733 | { |
| 734 | struct tables *tables = container_of(dbe, struct tables, dbe); |
| 735 | u64 comm_db_id = cr->comm ? cr->comm->db_id : 0; |
| 736 | PyObject *t; |
| 737 | |
| 738 | t = tuple_new(11); |
| 739 | |
| 740 | tuple_set_u64(t, 0, cr->db_id); |
| 741 | tuple_set_u64(t, 1, cr->thread->db_id); |
| 742 | tuple_set_u64(t, 2, comm_db_id); |
| 743 | tuple_set_u64(t, 3, cr->cp->db_id); |
| 744 | tuple_set_u64(t, 4, cr->call_time); |
| 745 | tuple_set_u64(t, 5, cr->return_time); |
| 746 | tuple_set_u64(t, 6, cr->branch_count); |
| 747 | tuple_set_u64(t, 7, cr->call_ref); |
| 748 | tuple_set_u64(t, 8, cr->return_ref); |
| 749 | tuple_set_u64(t, 9, cr->cp->parent->db_id); |
| 750 | tuple_set_s32(t, 10, cr->flags); |
| 751 | |
| 752 | call_object(tables->call_return_handler, t, "call_return_table"); |
| 753 | |
| 754 | Py_DECREF(t); |
| 755 | |
| 756 | return 0; |
| 757 | } |
| 758 | |
| 759 | static int python_process_call_return(struct call_return *cr, void *data) |
| 760 | { |
| 761 | struct db_export *dbe = data; |
| 762 | |
| 763 | return db_export__call_return(dbe, cr); |
| 764 | } |
| 765 | |
Arnaldo Carvalho de Melo | b7fff6b5 | 2013-12-19 16:34:52 -0300 | [diff] [blame] | 766 | static void python_process_general_event(struct perf_sample *sample, |
Feng Tang | 6a6daec | 2012-08-08 17:57:51 +0800 | [diff] [blame] | 767 | struct perf_evsel *evsel, |
Feng Tang | 87b6a3a | 2012-08-09 13:46:13 +0800 | [diff] [blame] | 768 | struct addr_location *al) |
Feng Tang | 6a6daec | 2012-08-08 17:57:51 +0800 | [diff] [blame] | 769 | { |
Adrian Hunter | a5563ed | 2014-07-31 09:01:01 +0300 | [diff] [blame] | 770 | PyObject *handler, *t, *dict, *callchain, *dict_sample; |
Feng Tang | 6a6daec | 2012-08-08 17:57:51 +0800 | [diff] [blame] | 771 | static char handler_name[64]; |
| 772 | unsigned n = 0; |
Feng Tang | 6a6daec | 2012-08-08 17:57:51 +0800 | [diff] [blame] | 773 | |
Feng Tang | fd6b858 | 2012-08-08 17:57:53 +0800 | [diff] [blame] | 774 | /* |
| 775 | * Use the MAX_FIELDS to make the function expandable, though |
Feng Tang | 87b6a3a | 2012-08-09 13:46:13 +0800 | [diff] [blame] | 776 | * currently there is only one item for the tuple. |
Feng Tang | fd6b858 | 2012-08-08 17:57:53 +0800 | [diff] [blame] | 777 | */ |
Feng Tang | 6a6daec | 2012-08-08 17:57:51 +0800 | [diff] [blame] | 778 | t = PyTuple_New(MAX_FIELDS); |
| 779 | if (!t) |
| 780 | Py_FatalError("couldn't create Python tuple"); |
| 781 | |
Feng Tang | fd6b858 | 2012-08-08 17:57:53 +0800 | [diff] [blame] | 782 | dict = PyDict_New(); |
| 783 | if (!dict) |
| 784 | Py_FatalError("couldn't create Python dictionary"); |
| 785 | |
Joseph Schuchart | 57608cf | 2014-07-10 13:50:56 +0200 | [diff] [blame] | 786 | dict_sample = PyDict_New(); |
| 787 | if (!dict_sample) |
| 788 | Py_FatalError("couldn't create Python dictionary"); |
| 789 | |
Feng Tang | 6a6daec | 2012-08-08 17:57:51 +0800 | [diff] [blame] | 790 | snprintf(handler_name, sizeof(handler_name), "%s", "process_event"); |
| 791 | |
Adrian Hunter | a5563ed | 2014-07-31 09:01:01 +0300 | [diff] [blame] | 792 | handler = get_handler(handler_name); |
| 793 | if (!handler) |
Feng Tang | 6a6daec | 2012-08-08 17:57:51 +0800 | [diff] [blame] | 794 | goto exit; |
Feng Tang | 6a6daec | 2012-08-08 17:57:51 +0800 | [diff] [blame] | 795 | |
Joseph Schuchart | c0268e8 | 2013-10-24 10:10:51 -0300 | [diff] [blame] | 796 | pydict_set_item_string_decref(dict, "ev_name", PyString_FromString(perf_evsel__name(evsel))); |
| 797 | pydict_set_item_string_decref(dict, "attr", PyString_FromStringAndSize( |
Feng Tang | fd6b858 | 2012-08-08 17:57:53 +0800 | [diff] [blame] | 798 | (const char *)&evsel->attr, sizeof(evsel->attr))); |
Joseph Schuchart | 57608cf | 2014-07-10 13:50:56 +0200 | [diff] [blame] | 799 | |
| 800 | pydict_set_item_string_decref(dict_sample, "pid", |
| 801 | PyInt_FromLong(sample->pid)); |
| 802 | pydict_set_item_string_decref(dict_sample, "tid", |
| 803 | PyInt_FromLong(sample->tid)); |
| 804 | pydict_set_item_string_decref(dict_sample, "cpu", |
| 805 | PyInt_FromLong(sample->cpu)); |
| 806 | pydict_set_item_string_decref(dict_sample, "ip", |
| 807 | PyLong_FromUnsignedLongLong(sample->ip)); |
| 808 | pydict_set_item_string_decref(dict_sample, "time", |
| 809 | PyLong_FromUnsignedLongLong(sample->time)); |
| 810 | pydict_set_item_string_decref(dict_sample, "period", |
| 811 | PyLong_FromUnsignedLongLong(sample->period)); |
| 812 | pydict_set_item_string_decref(dict, "sample", dict_sample); |
| 813 | |
Joseph Schuchart | c0268e8 | 2013-10-24 10:10:51 -0300 | [diff] [blame] | 814 | pydict_set_item_string_decref(dict, "raw_buf", PyString_FromStringAndSize( |
Feng Tang | fd6b858 | 2012-08-08 17:57:53 +0800 | [diff] [blame] | 815 | (const char *)sample->raw_data, sample->raw_size)); |
Joseph Schuchart | c0268e8 | 2013-10-24 10:10:51 -0300 | [diff] [blame] | 816 | pydict_set_item_string_decref(dict, "comm", |
Arnaldo Carvalho de Melo | f9d5d54 | 2015-04-01 13:29:25 -0300 | [diff] [blame] | 817 | PyString_FromString(thread__comm_str(al->thread))); |
Feng Tang | fd6b858 | 2012-08-08 17:57:53 +0800 | [diff] [blame] | 818 | if (al->map) { |
Joseph Schuchart | c0268e8 | 2013-10-24 10:10:51 -0300 | [diff] [blame] | 819 | pydict_set_item_string_decref(dict, "dso", |
Feng Tang | fd6b858 | 2012-08-08 17:57:53 +0800 | [diff] [blame] | 820 | PyString_FromString(al->map->dso->name)); |
| 821 | } |
| 822 | if (al->sym) { |
Joseph Schuchart | c0268e8 | 2013-10-24 10:10:51 -0300 | [diff] [blame] | 823 | pydict_set_item_string_decref(dict, "symbol", |
Feng Tang | fd6b858 | 2012-08-08 17:57:53 +0800 | [diff] [blame] | 824 | PyString_FromString(al->sym->name)); |
| 825 | } |
Feng Tang | 6a6daec | 2012-08-08 17:57:51 +0800 | [diff] [blame] | 826 | |
Joseph Schuchart | 0f5f5bc | 2014-07-10 13:50:51 +0200 | [diff] [blame] | 827 | /* ip unwinding */ |
| 828 | callchain = python_process_callchain(sample, evsel, al); |
| 829 | pydict_set_item_string_decref(dict, "callchain", callchain); |
| 830 | |
Feng Tang | fd6b858 | 2012-08-08 17:57:53 +0800 | [diff] [blame] | 831 | PyTuple_SetItem(t, n++, dict); |
Feng Tang | 6a6daec | 2012-08-08 17:57:51 +0800 | [diff] [blame] | 832 | if (_PyTuple_Resize(&t, n) == -1) |
| 833 | Py_FatalError("error resizing Python tuple"); |
| 834 | |
Adrian Hunter | a5563ed | 2014-07-31 09:01:01 +0300 | [diff] [blame] | 835 | call_object(handler, t, handler_name); |
Feng Tang | 6a6daec | 2012-08-08 17:57:51 +0800 | [diff] [blame] | 836 | exit: |
Feng Tang | fd6b858 | 2012-08-08 17:57:53 +0800 | [diff] [blame] | 837 | Py_DECREF(dict); |
Feng Tang | 6a6daec | 2012-08-08 17:57:51 +0800 | [diff] [blame] | 838 | Py_DECREF(t); |
| 839 | } |
| 840 | |
Adrian Hunter | df919b4 | 2014-10-23 13:45:14 +0300 | [diff] [blame] | 841 | static void python_process_event(union perf_event *event, |
Feng Tang | 6a6daec | 2012-08-08 17:57:51 +0800 | [diff] [blame] | 842 | struct perf_sample *sample, |
| 843 | struct perf_evsel *evsel, |
Feng Tang | 73994dc | 2012-08-08 17:57:52 +0800 | [diff] [blame] | 844 | struct addr_location *al) |
Feng Tang | 6a6daec | 2012-08-08 17:57:51 +0800 | [diff] [blame] | 845 | { |
Adrian Hunter | df919b4 | 2014-10-23 13:45:14 +0300 | [diff] [blame] | 846 | struct tables *tables = &tables_global; |
| 847 | |
Feng Tang | 6a6daec | 2012-08-08 17:57:51 +0800 | [diff] [blame] | 848 | switch (evsel->attr.type) { |
| 849 | case PERF_TYPE_TRACEPOINT: |
Arnaldo Carvalho de Melo | f9d5d54 | 2015-04-01 13:29:25 -0300 | [diff] [blame] | 850 | python_process_tracepoint(sample, evsel, al); |
Feng Tang | 6a6daec | 2012-08-08 17:57:51 +0800 | [diff] [blame] | 851 | break; |
| 852 | /* Reserve for future process_hw/sw/raw APIs */ |
| 853 | default: |
Adrian Hunter | df919b4 | 2014-10-23 13:45:14 +0300 | [diff] [blame] | 854 | if (tables->db_export_mode) |
Arnaldo Carvalho de Melo | 7327259 | 2015-04-02 11:08:30 -0300 | [diff] [blame] | 855 | db_export__sample(&tables->dbe, event, sample, evsel, al); |
Adrian Hunter | df919b4 | 2014-10-23 13:45:14 +0300 | [diff] [blame] | 856 | else |
Arnaldo Carvalho de Melo | f9d5d54 | 2015-04-01 13:29:25 -0300 | [diff] [blame] | 857 | python_process_general_event(sample, evsel, al); |
Feng Tang | 6a6daec | 2012-08-08 17:57:51 +0800 | [diff] [blame] | 858 | } |
| 859 | } |
| 860 | |
Tom Zanussi | 7e4b21b | 2010-01-27 02:27:57 -0600 | [diff] [blame] | 861 | static int run_start_sub(void) |
| 862 | { |
Tom Zanussi | 7e4b21b | 2010-01-27 02:27:57 -0600 | [diff] [blame] | 863 | main_module = PyImport_AddModule("__main__"); |
| 864 | if (main_module == NULL) |
| 865 | return -1; |
| 866 | Py_INCREF(main_module); |
| 867 | |
| 868 | main_dict = PyModule_GetDict(main_module); |
Adrian Hunter | a5563ed | 2014-07-31 09:01:01 +0300 | [diff] [blame] | 869 | if (main_dict == NULL) |
Tom Zanussi | 7e4b21b | 2010-01-27 02:27:57 -0600 | [diff] [blame] | 870 | goto error; |
Tom Zanussi | 7e4b21b | 2010-01-27 02:27:57 -0600 | [diff] [blame] | 871 | Py_INCREF(main_dict); |
| 872 | |
Adrian Hunter | a5563ed | 2014-07-31 09:01:01 +0300 | [diff] [blame] | 873 | try_call_object("trace_begin", NULL); |
Tom Zanussi | 7e4b21b | 2010-01-27 02:27:57 -0600 | [diff] [blame] | 874 | |
Adrian Hunter | a5563ed | 2014-07-31 09:01:01 +0300 | [diff] [blame] | 875 | return 0; |
Tom Zanussi | 7e4b21b | 2010-01-27 02:27:57 -0600 | [diff] [blame] | 876 | |
Tom Zanussi | 7e4b21b | 2010-01-27 02:27:57 -0600 | [diff] [blame] | 877 | error: |
| 878 | Py_XDECREF(main_dict); |
| 879 | Py_XDECREF(main_module); |
Adrian Hunter | a5563ed | 2014-07-31 09:01:01 +0300 | [diff] [blame] | 880 | return -1; |
Tom Zanussi | 7e4b21b | 2010-01-27 02:27:57 -0600 | [diff] [blame] | 881 | } |
| 882 | |
Adrian Hunter | df919b4 | 2014-10-23 13:45:14 +0300 | [diff] [blame] | 883 | #define SET_TABLE_HANDLER_(name, handler_name, table_name) do { \ |
| 884 | tables->handler_name = get_handler(#table_name); \ |
| 885 | if (tables->handler_name) \ |
| 886 | tables->dbe.export_ ## name = python_export_ ## name; \ |
| 887 | } while (0) |
| 888 | |
| 889 | #define SET_TABLE_HANDLER(name) \ |
| 890 | SET_TABLE_HANDLER_(name, name ## _handler, name ## _table) |
| 891 | |
| 892 | static void set_table_handlers(struct tables *tables) |
| 893 | { |
| 894 | const char *perf_db_export_mode = "perf_db_export_mode"; |
Adrian Hunter | 6a70307 | 2014-10-30 16:09:47 +0200 | [diff] [blame] | 895 | const char *perf_db_export_calls = "perf_db_export_calls"; |
| 896 | PyObject *db_export_mode, *db_export_calls; |
| 897 | bool export_calls = false; |
Adrian Hunter | df919b4 | 2014-10-23 13:45:14 +0300 | [diff] [blame] | 898 | int ret; |
| 899 | |
| 900 | memset(tables, 0, sizeof(struct tables)); |
| 901 | if (db_export__init(&tables->dbe)) |
| 902 | Py_FatalError("failed to initialize export"); |
| 903 | |
| 904 | db_export_mode = PyDict_GetItemString(main_dict, perf_db_export_mode); |
| 905 | if (!db_export_mode) |
| 906 | return; |
| 907 | |
| 908 | ret = PyObject_IsTrue(db_export_mode); |
| 909 | if (ret == -1) |
| 910 | handler_call_die(perf_db_export_mode); |
| 911 | if (!ret) |
| 912 | return; |
| 913 | |
Adrian Hunter | 6a70307 | 2014-10-30 16:09:47 +0200 | [diff] [blame] | 914 | tables->dbe.crp = NULL; |
| 915 | db_export_calls = PyDict_GetItemString(main_dict, perf_db_export_calls); |
| 916 | if (db_export_calls) { |
| 917 | ret = PyObject_IsTrue(db_export_calls); |
| 918 | if (ret == -1) |
| 919 | handler_call_die(perf_db_export_calls); |
| 920 | export_calls = !!ret; |
| 921 | } |
| 922 | |
| 923 | if (export_calls) { |
| 924 | tables->dbe.crp = |
| 925 | call_return_processor__new(python_process_call_return, |
| 926 | &tables->dbe); |
| 927 | if (!tables->dbe.crp) |
| 928 | Py_FatalError("failed to create calls processor"); |
| 929 | } |
| 930 | |
Adrian Hunter | df919b4 | 2014-10-23 13:45:14 +0300 | [diff] [blame] | 931 | tables->db_export_mode = true; |
| 932 | /* |
| 933 | * Reserve per symbol space for symbol->db_id via symbol__priv() |
| 934 | */ |
| 935 | symbol_conf.priv_size = sizeof(u64); |
| 936 | |
| 937 | SET_TABLE_HANDLER(evsel); |
| 938 | SET_TABLE_HANDLER(machine); |
| 939 | SET_TABLE_HANDLER(thread); |
| 940 | SET_TABLE_HANDLER(comm); |
| 941 | SET_TABLE_HANDLER(comm_thread); |
| 942 | SET_TABLE_HANDLER(dso); |
| 943 | SET_TABLE_HANDLER(symbol); |
Adrian Hunter | c29414f | 2014-10-30 16:09:44 +0200 | [diff] [blame] | 944 | SET_TABLE_HANDLER(branch_type); |
Adrian Hunter | df919b4 | 2014-10-23 13:45:14 +0300 | [diff] [blame] | 945 | SET_TABLE_HANDLER(sample); |
Adrian Hunter | 6a70307 | 2014-10-30 16:09:47 +0200 | [diff] [blame] | 946 | SET_TABLE_HANDLER(call_path); |
| 947 | SET_TABLE_HANDLER(call_return); |
Adrian Hunter | df919b4 | 2014-10-23 13:45:14 +0300 | [diff] [blame] | 948 | } |
| 949 | |
Tom Zanussi | 7e4b21b | 2010-01-27 02:27:57 -0600 | [diff] [blame] | 950 | /* |
| 951 | * Start trace script |
| 952 | */ |
| 953 | static int python_start_script(const char *script, int argc, const char **argv) |
| 954 | { |
Adrian Hunter | df919b4 | 2014-10-23 13:45:14 +0300 | [diff] [blame] | 955 | struct tables *tables = &tables_global; |
Tom Zanussi | 7e4b21b | 2010-01-27 02:27:57 -0600 | [diff] [blame] | 956 | const char **command_line; |
| 957 | char buf[PATH_MAX]; |
| 958 | int i, err = 0; |
| 959 | FILE *fp; |
| 960 | |
| 961 | command_line = malloc((argc + 1) * sizeof(const char *)); |
| 962 | command_line[0] = script; |
| 963 | for (i = 1; i < argc + 1; i++) |
| 964 | command_line[i] = argv[i - 1]; |
| 965 | |
| 966 | Py_Initialize(); |
| 967 | |
| 968 | initperf_trace_context(); |
| 969 | |
| 970 | PySys_SetArgv(argc + 1, (char **)command_line); |
| 971 | |
| 972 | fp = fopen(script, "r"); |
| 973 | if (!fp) { |
| 974 | sprintf(buf, "Can't open python script \"%s\"", script); |
| 975 | perror(buf); |
| 976 | err = -1; |
| 977 | goto error; |
| 978 | } |
| 979 | |
| 980 | err = PyRun_SimpleFile(fp, script); |
| 981 | if (err) { |
| 982 | fprintf(stderr, "Error running python script %s\n", script); |
| 983 | goto error; |
| 984 | } |
| 985 | |
| 986 | err = run_start_sub(); |
| 987 | if (err) { |
| 988 | fprintf(stderr, "Error starting python script %s\n", script); |
| 989 | goto error; |
| 990 | } |
| 991 | |
| 992 | free(command_line); |
Tom Zanussi | 7e4b21b | 2010-01-27 02:27:57 -0600 | [diff] [blame] | 993 | |
Adrian Hunter | df919b4 | 2014-10-23 13:45:14 +0300 | [diff] [blame] | 994 | set_table_handlers(tables); |
| 995 | |
Adrian Hunter | c29414f | 2014-10-30 16:09:44 +0200 | [diff] [blame] | 996 | if (tables->db_export_mode) { |
| 997 | err = db_export__branch_types(&tables->dbe); |
| 998 | if (err) |
| 999 | goto error; |
| 1000 | } |
| 1001 | |
Tom Zanussi | 7e4b21b | 2010-01-27 02:27:57 -0600 | [diff] [blame] | 1002 | return err; |
| 1003 | error: |
| 1004 | Py_Finalize(); |
| 1005 | free(command_line); |
| 1006 | |
| 1007 | return err; |
| 1008 | } |
| 1009 | |
Adrian Hunter | d445dd2 | 2014-08-15 22:08:37 +0300 | [diff] [blame] | 1010 | static int python_flush_script(void) |
| 1011 | { |
Adrian Hunter | 758008b | 2014-10-30 16:09:48 +0200 | [diff] [blame] | 1012 | struct tables *tables = &tables_global; |
| 1013 | |
| 1014 | return db_export__flush(&tables->dbe); |
Adrian Hunter | d445dd2 | 2014-08-15 22:08:37 +0300 | [diff] [blame] | 1015 | } |
| 1016 | |
Tom Zanussi | 7e4b21b | 2010-01-27 02:27:57 -0600 | [diff] [blame] | 1017 | /* |
| 1018 | * Stop trace script |
| 1019 | */ |
| 1020 | static int python_stop_script(void) |
| 1021 | { |
Adrian Hunter | df919b4 | 2014-10-23 13:45:14 +0300 | [diff] [blame] | 1022 | struct tables *tables = &tables_global; |
| 1023 | |
Adrian Hunter | a5563ed | 2014-07-31 09:01:01 +0300 | [diff] [blame] | 1024 | try_call_object("trace_end", NULL); |
Tom Zanussi | 7e4b21b | 2010-01-27 02:27:57 -0600 | [diff] [blame] | 1025 | |
Adrian Hunter | df919b4 | 2014-10-23 13:45:14 +0300 | [diff] [blame] | 1026 | db_export__exit(&tables->dbe); |
| 1027 | |
Tom Zanussi | 7e4b21b | 2010-01-27 02:27:57 -0600 | [diff] [blame] | 1028 | Py_XDECREF(main_dict); |
| 1029 | Py_XDECREF(main_module); |
| 1030 | Py_Finalize(); |
| 1031 | |
Adrian Hunter | a5563ed | 2014-07-31 09:01:01 +0300 | [diff] [blame] | 1032 | return 0; |
Tom Zanussi | 7e4b21b | 2010-01-27 02:27:57 -0600 | [diff] [blame] | 1033 | } |
| 1034 | |
Arnaldo Carvalho de Melo | da37896 | 2012-06-27 13:08:42 -0300 | [diff] [blame] | 1035 | static int python_generate_script(struct pevent *pevent, const char *outfile) |
Tom Zanussi | 7e4b21b | 2010-01-27 02:27:57 -0600 | [diff] [blame] | 1036 | { |
Steven Rostedt | aaf045f | 2012-04-06 00:47:56 +0200 | [diff] [blame] | 1037 | struct event_format *event = NULL; |
Tom Zanussi | 7e4b21b | 2010-01-27 02:27:57 -0600 | [diff] [blame] | 1038 | struct format_field *f; |
| 1039 | char fname[PATH_MAX]; |
| 1040 | int not_first, count; |
| 1041 | FILE *ofp; |
| 1042 | |
| 1043 | sprintf(fname, "%s.py", outfile); |
| 1044 | ofp = fopen(fname, "w"); |
| 1045 | if (ofp == NULL) { |
| 1046 | fprintf(stderr, "couldn't open %s\n", fname); |
| 1047 | return -1; |
| 1048 | } |
Ingo Molnar | 133dc4c | 2010-11-16 18:45:39 +0100 | [diff] [blame] | 1049 | fprintf(ofp, "# perf script event handlers, " |
| 1050 | "generated by perf script -g python\n"); |
Tom Zanussi | 7e4b21b | 2010-01-27 02:27:57 -0600 | [diff] [blame] | 1051 | |
| 1052 | fprintf(ofp, "# Licensed under the terms of the GNU GPL" |
| 1053 | " License version 2\n\n"); |
| 1054 | |
| 1055 | fprintf(ofp, "# The common_* event handler fields are the most useful " |
| 1056 | "fields common to\n"); |
| 1057 | |
| 1058 | fprintf(ofp, "# all events. They don't necessarily correspond to " |
| 1059 | "the 'common_*' fields\n"); |
| 1060 | |
| 1061 | fprintf(ofp, "# in the format files. Those fields not available as " |
| 1062 | "handler params can\n"); |
| 1063 | |
| 1064 | fprintf(ofp, "# be retrieved using Python functions of the form " |
| 1065 | "common_*(context).\n"); |
| 1066 | |
| 1067 | fprintf(ofp, "# See the perf-trace-python Documentation for the list " |
| 1068 | "of available functions.\n\n"); |
| 1069 | |
| 1070 | fprintf(ofp, "import os\n"); |
| 1071 | fprintf(ofp, "import sys\n\n"); |
| 1072 | |
| 1073 | fprintf(ofp, "sys.path.append(os.environ['PERF_EXEC_PATH'] + \\\n"); |
| 1074 | fprintf(ofp, "\t'/scripts/python/Perf-Trace-Util/lib/Perf/Trace')\n"); |
| 1075 | fprintf(ofp, "\nfrom perf_trace_context import *\n"); |
| 1076 | fprintf(ofp, "from Core import *\n\n\n"); |
| 1077 | |
| 1078 | fprintf(ofp, "def trace_begin():\n"); |
| 1079 | fprintf(ofp, "\tprint \"in trace_begin\"\n\n"); |
| 1080 | |
| 1081 | fprintf(ofp, "def trace_end():\n"); |
| 1082 | fprintf(ofp, "\tprint \"in trace_end\"\n\n"); |
| 1083 | |
Arnaldo Carvalho de Melo | da37896 | 2012-06-27 13:08:42 -0300 | [diff] [blame] | 1084 | while ((event = trace_find_next_event(pevent, event))) { |
Tom Zanussi | 7e4b21b | 2010-01-27 02:27:57 -0600 | [diff] [blame] | 1085 | fprintf(ofp, "def %s__%s(", event->system, event->name); |
| 1086 | fprintf(ofp, "event_name, "); |
| 1087 | fprintf(ofp, "context, "); |
| 1088 | fprintf(ofp, "common_cpu,\n"); |
| 1089 | fprintf(ofp, "\tcommon_secs, "); |
| 1090 | fprintf(ofp, "common_nsecs, "); |
| 1091 | fprintf(ofp, "common_pid, "); |
| 1092 | fprintf(ofp, "common_comm,\n\t"); |
Joseph Schuchart | 0f5f5bc | 2014-07-10 13:50:51 +0200 | [diff] [blame] | 1093 | fprintf(ofp, "common_callchain, "); |
Tom Zanussi | 7e4b21b | 2010-01-27 02:27:57 -0600 | [diff] [blame] | 1094 | |
| 1095 | not_first = 0; |
| 1096 | count = 0; |
| 1097 | |
| 1098 | for (f = event->format.fields; f; f = f->next) { |
| 1099 | if (not_first++) |
| 1100 | fprintf(ofp, ", "); |
| 1101 | if (++count % 5 == 0) |
| 1102 | fprintf(ofp, "\n\t"); |
| 1103 | |
| 1104 | fprintf(ofp, "%s", f->name); |
| 1105 | } |
| 1106 | fprintf(ofp, "):\n"); |
| 1107 | |
| 1108 | fprintf(ofp, "\t\tprint_header(event_name, common_cpu, " |
| 1109 | "common_secs, common_nsecs,\n\t\t\t" |
| 1110 | "common_pid, common_comm)\n\n"); |
| 1111 | |
| 1112 | fprintf(ofp, "\t\tprint \""); |
| 1113 | |
| 1114 | not_first = 0; |
| 1115 | count = 0; |
| 1116 | |
| 1117 | for (f = event->format.fields; f; f = f->next) { |
| 1118 | if (not_first++) |
| 1119 | fprintf(ofp, ", "); |
| 1120 | if (count && count % 3 == 0) { |
| 1121 | fprintf(ofp, "\" \\\n\t\t\""); |
| 1122 | } |
| 1123 | count++; |
| 1124 | |
| 1125 | fprintf(ofp, "%s=", f->name); |
| 1126 | if (f->flags & FIELD_IS_STRING || |
| 1127 | f->flags & FIELD_IS_FLAG || |
Namhyung Kim | e646fe7 | 2014-05-29 13:44:55 +0900 | [diff] [blame] | 1128 | f->flags & FIELD_IS_ARRAY || |
Tom Zanussi | 7e4b21b | 2010-01-27 02:27:57 -0600 | [diff] [blame] | 1129 | f->flags & FIELD_IS_SYMBOLIC) |
| 1130 | fprintf(ofp, "%%s"); |
| 1131 | else if (f->flags & FIELD_IS_SIGNED) |
| 1132 | fprintf(ofp, "%%d"); |
| 1133 | else |
| 1134 | fprintf(ofp, "%%u"); |
| 1135 | } |
| 1136 | |
Joseph Schuchart | 0f5f5bc | 2014-07-10 13:50:51 +0200 | [diff] [blame] | 1137 | fprintf(ofp, "\" %% \\\n\t\t("); |
Tom Zanussi | 7e4b21b | 2010-01-27 02:27:57 -0600 | [diff] [blame] | 1138 | |
| 1139 | not_first = 0; |
| 1140 | count = 0; |
| 1141 | |
| 1142 | for (f = event->format.fields; f; f = f->next) { |
| 1143 | if (not_first++) |
| 1144 | fprintf(ofp, ", "); |
| 1145 | |
| 1146 | if (++count % 5 == 0) |
| 1147 | fprintf(ofp, "\n\t\t"); |
| 1148 | |
| 1149 | if (f->flags & FIELD_IS_FLAG) { |
| 1150 | if ((count - 1) % 5 != 0) { |
| 1151 | fprintf(ofp, "\n\t\t"); |
| 1152 | count = 4; |
| 1153 | } |
| 1154 | fprintf(ofp, "flag_str(\""); |
| 1155 | fprintf(ofp, "%s__%s\", ", event->system, |
| 1156 | event->name); |
| 1157 | fprintf(ofp, "\"%s\", %s)", f->name, |
| 1158 | f->name); |
| 1159 | } else if (f->flags & FIELD_IS_SYMBOLIC) { |
| 1160 | if ((count - 1) % 5 != 0) { |
| 1161 | fprintf(ofp, "\n\t\t"); |
| 1162 | count = 4; |
| 1163 | } |
| 1164 | fprintf(ofp, "symbol_str(\""); |
| 1165 | fprintf(ofp, "%s__%s\", ", event->system, |
| 1166 | event->name); |
| 1167 | fprintf(ofp, "\"%s\", %s)", f->name, |
| 1168 | f->name); |
| 1169 | } else |
| 1170 | fprintf(ofp, "%s", f->name); |
| 1171 | } |
| 1172 | |
Joseph Schuchart | 0f5f5bc | 2014-07-10 13:50:51 +0200 | [diff] [blame] | 1173 | fprintf(ofp, ")\n\n"); |
| 1174 | |
| 1175 | fprintf(ofp, "\t\tfor node in common_callchain:"); |
| 1176 | fprintf(ofp, "\n\t\t\tif 'sym' in node:"); |
| 1177 | fprintf(ofp, "\n\t\t\t\tprint \"\\t[%%x] %%s\" %% (node['ip'], node['sym']['name'])"); |
| 1178 | fprintf(ofp, "\n\t\t\telse:"); |
| 1179 | fprintf(ofp, "\n\t\t\t\tprint \"\t[%%x]\" %% (node['ip'])\n\n"); |
| 1180 | fprintf(ofp, "\t\tprint \"\\n\"\n\n"); |
| 1181 | |
Tom Zanussi | 7e4b21b | 2010-01-27 02:27:57 -0600 | [diff] [blame] | 1182 | } |
| 1183 | |
| 1184 | fprintf(ofp, "def trace_unhandled(event_name, context, " |
Pierre Tardy | c025148 | 2010-05-31 23:12:09 +0200 | [diff] [blame] | 1185 | "event_fields_dict):\n"); |
Tom Zanussi | 7e4b21b | 2010-01-27 02:27:57 -0600 | [diff] [blame] | 1186 | |
Pierre Tardy | c025148 | 2010-05-31 23:12:09 +0200 | [diff] [blame] | 1187 | fprintf(ofp, "\t\tprint ' '.join(['%%s=%%s'%%(k,str(v))" |
| 1188 | "for k,v in sorted(event_fields_dict.items())])\n\n"); |
Tom Zanussi | 7e4b21b | 2010-01-27 02:27:57 -0600 | [diff] [blame] | 1189 | |
| 1190 | fprintf(ofp, "def print_header(" |
| 1191 | "event_name, cpu, secs, nsecs, pid, comm):\n" |
| 1192 | "\tprint \"%%-20s %%5u %%05u.%%09u %%8u %%-20s \" %% \\\n\t" |
| 1193 | "(event_name, cpu, secs, nsecs, pid, comm),\n"); |
| 1194 | |
| 1195 | fclose(ofp); |
| 1196 | |
| 1197 | fprintf(stderr, "generated Python script: %s\n", fname); |
| 1198 | |
| 1199 | return 0; |
| 1200 | } |
| 1201 | |
| 1202 | struct scripting_ops python_scripting_ops = { |
| 1203 | .name = "Python", |
| 1204 | .start_script = python_start_script, |
Adrian Hunter | d445dd2 | 2014-08-15 22:08:37 +0300 | [diff] [blame] | 1205 | .flush_script = python_flush_script, |
Tom Zanussi | 7e4b21b | 2010-01-27 02:27:57 -0600 | [diff] [blame] | 1206 | .stop_script = python_stop_script, |
| 1207 | .process_event = python_process_event, |
| 1208 | .generate_script = python_generate_script, |
| 1209 | }; |