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