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