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