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 "../evsel.h" 31 #include "../util.h" 32 #include "../event.h" 33 #include "../thread.h" 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 41 struct event_format *events[FTRACE_MAX_EVENT]; 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 73 t = PyTuple_New(4); 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 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 118 if (field_type == PRINT_FLAGS) 119 t = PyTuple_New(3); 120 else 121 t = PyTuple_New(2); 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 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 140 static void define_event_symbols(struct event_format *event, 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; 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; 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: 186 /* gcc warns for these? */ 187 case PRINT_BSTRING: 188 case PRINT_DYNAMIC_ARRAY: 189 case PRINT_FUNC: 190 /* we should warn... */ 191 return; 192 } 193 194 if (args->next) 195 define_event_symbols(event, ev_name, args->next); 196 } 197 198 static inline struct event_format *find_cache_event(struct perf_evsel *evsel) 199 { 200 static char ev_name[256]; 201 struct event_format *event; 202 int type = evsel->attr.config; 203 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 */ 209 if (events[type]) 210 return events[type]; 211 212 events[type] = event = evsel->tp_format; 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 223 static void python_process_tracepoint(union perf_event *perf_event 224 __maybe_unused, 225 struct perf_sample *sample, 226 struct perf_evsel *evsel, 227 struct machine *machine __maybe_unused, 228 struct thread *thread, 229 struct addr_location *al) 230 { 231 PyObject *handler, *retval, *context, *t, *obj, *dict = NULL; 232 static char handler_name[256]; 233 struct format_field *field; 234 unsigned long long val; 235 unsigned long s, ns; 236 struct event_format *event; 237 unsigned n = 0; 238 int pid; 239 int cpu = sample->cpu; 240 void *data = sample->raw_data; 241 unsigned long long nsecs = sample->time; 242 char *comm = thread->comm; 243 244 t = PyTuple_New(MAX_FIELDS); 245 if (!t) 246 Py_FatalError("couldn't create Python tuple"); 247 248 event = find_cache_event(evsel); 249 if (!event) 250 die("ug! no event found for type %d", (int)evsel->attr.config); 251 252 pid = raw_field_value(event, "common_pid", data); 253 254 sprintf(handler_name, "%s__%s", event->system, event->name); 255 256 handler = PyDict_GetItemString(main_dict, handler_name); 257 if (handler && !PyCallable_Check(handler)) 258 handler = NULL; 259 if (!handler) { 260 dict = PyDict_New(); 261 if (!dict) 262 Py_FatalError("couldn't create Python dict"); 263 } 264 s = nsecs / NSECS_PER_SEC; 265 ns = nsecs - s * NSECS_PER_SEC; 266 267 scripting_context->event_data = data; 268 scripting_context->pevent = evsel->tp_format->pevent; 269 270 context = PyCObject_FromVoidPtr(scripting_context, NULL); 271 272 PyTuple_SetItem(t, n++, PyString_FromString(handler_name)); 273 PyTuple_SetItem(t, n++, context); 274 275 if (handler) { 276 PyTuple_SetItem(t, n++, PyInt_FromLong(cpu)); 277 PyTuple_SetItem(t, n++, PyInt_FromLong(s)); 278 PyTuple_SetItem(t, n++, PyInt_FromLong(ns)); 279 PyTuple_SetItem(t, n++, PyInt_FromLong(pid)); 280 PyTuple_SetItem(t, n++, PyString_FromString(comm)); 281 } else { 282 PyDict_SetItemString(dict, "common_cpu", PyInt_FromLong(cpu)); 283 PyDict_SetItemString(dict, "common_s", PyInt_FromLong(s)); 284 PyDict_SetItemString(dict, "common_ns", PyInt_FromLong(ns)); 285 PyDict_SetItemString(dict, "common_pid", PyInt_FromLong(pid)); 286 PyDict_SetItemString(dict, "common_comm", PyString_FromString(comm)); 287 } 288 for (field = event->format.fields; field; field = field->next) { 289 if (field->flags & FIELD_IS_STRING) { 290 int offset; 291 if (field->flags & FIELD_IS_DYNAMIC) { 292 offset = *(int *)(data + field->offset); 293 offset &= 0xffff; 294 } else 295 offset = field->offset; 296 obj = PyString_FromString((char *)data + offset); 297 } else { /* FIELD_IS_NUMERIC */ 298 val = read_size(event, data + field->offset, 299 field->size); 300 if (field->flags & FIELD_IS_SIGNED) { 301 if ((long long)val >= LONG_MIN && 302 (long long)val <= LONG_MAX) 303 obj = PyInt_FromLong(val); 304 else 305 obj = PyLong_FromLongLong(val); 306 } else { 307 if (val <= LONG_MAX) 308 obj = PyInt_FromLong(val); 309 else 310 obj = PyLong_FromUnsignedLongLong(val); 311 } 312 } 313 if (handler) 314 PyTuple_SetItem(t, n++, obj); 315 else 316 PyDict_SetItemString(dict, field->name, obj); 317 318 } 319 if (!handler) 320 PyTuple_SetItem(t, n++, dict); 321 322 if (_PyTuple_Resize(&t, n) == -1) 323 Py_FatalError("error resizing Python tuple"); 324 325 if (handler) { 326 retval = PyObject_CallObject(handler, t); 327 if (retval == NULL) 328 handler_call_die(handler_name); 329 } else { 330 handler = PyDict_GetItemString(main_dict, "trace_unhandled"); 331 if (handler && PyCallable_Check(handler)) { 332 333 retval = PyObject_CallObject(handler, t); 334 if (retval == NULL) 335 handler_call_die("trace_unhandled"); 336 } 337 Py_DECREF(dict); 338 } 339 340 Py_DECREF(t); 341 } 342 343 static void python_process_general_event(union perf_event *perf_event 344 __maybe_unused, 345 struct perf_sample *sample, 346 struct perf_evsel *evsel, 347 struct machine *machine __maybe_unused, 348 struct thread *thread, 349 struct addr_location *al) 350 { 351 PyObject *handler, *retval, *t, *dict; 352 static char handler_name[64]; 353 unsigned n = 0; 354 355 /* 356 * Use the MAX_FIELDS to make the function expandable, though 357 * currently there is only one item for the tuple. 358 */ 359 t = PyTuple_New(MAX_FIELDS); 360 if (!t) 361 Py_FatalError("couldn't create Python tuple"); 362 363 dict = PyDict_New(); 364 if (!dict) 365 Py_FatalError("couldn't create Python dictionary"); 366 367 snprintf(handler_name, sizeof(handler_name), "%s", "process_event"); 368 369 handler = PyDict_GetItemString(main_dict, handler_name); 370 if (!handler || !PyCallable_Check(handler)) 371 goto exit; 372 373 PyDict_SetItemString(dict, "ev_name", PyString_FromString(perf_evsel__name(evsel))); 374 PyDict_SetItemString(dict, "attr", PyString_FromStringAndSize( 375 (const char *)&evsel->attr, sizeof(evsel->attr))); 376 PyDict_SetItemString(dict, "sample", PyString_FromStringAndSize( 377 (const char *)sample, sizeof(*sample))); 378 PyDict_SetItemString(dict, "raw_buf", PyString_FromStringAndSize( 379 (const char *)sample->raw_data, sample->raw_size)); 380 PyDict_SetItemString(dict, "comm", 381 PyString_FromString(thread->comm)); 382 if (al->map) { 383 PyDict_SetItemString(dict, "dso", 384 PyString_FromString(al->map->dso->name)); 385 } 386 if (al->sym) { 387 PyDict_SetItemString(dict, "symbol", 388 PyString_FromString(al->sym->name)); 389 } 390 391 PyTuple_SetItem(t, n++, dict); 392 if (_PyTuple_Resize(&t, n) == -1) 393 Py_FatalError("error resizing Python tuple"); 394 395 retval = PyObject_CallObject(handler, t); 396 if (retval == NULL) 397 handler_call_die(handler_name); 398 exit: 399 Py_DECREF(dict); 400 Py_DECREF(t); 401 } 402 403 static void python_process_event(union perf_event *perf_event, 404 struct perf_sample *sample, 405 struct perf_evsel *evsel, 406 struct machine *machine, 407 struct thread *thread, 408 struct addr_location *al) 409 { 410 switch (evsel->attr.type) { 411 case PERF_TYPE_TRACEPOINT: 412 python_process_tracepoint(perf_event, sample, evsel, 413 machine, thread, al); 414 break; 415 /* Reserve for future process_hw/sw/raw APIs */ 416 default: 417 python_process_general_event(perf_event, sample, evsel, 418 machine, thread, al); 419 } 420 } 421 422 static int run_start_sub(void) 423 { 424 PyObject *handler, *retval; 425 int err = 0; 426 427 main_module = PyImport_AddModule("__main__"); 428 if (main_module == NULL) 429 return -1; 430 Py_INCREF(main_module); 431 432 main_dict = PyModule_GetDict(main_module); 433 if (main_dict == NULL) { 434 err = -1; 435 goto error; 436 } 437 Py_INCREF(main_dict); 438 439 handler = PyDict_GetItemString(main_dict, "trace_begin"); 440 if (handler == NULL || !PyCallable_Check(handler)) 441 goto out; 442 443 retval = PyObject_CallObject(handler, NULL); 444 if (retval == NULL) 445 handler_call_die("trace_begin"); 446 447 Py_DECREF(retval); 448 return err; 449 error: 450 Py_XDECREF(main_dict); 451 Py_XDECREF(main_module); 452 out: 453 return err; 454 } 455 456 /* 457 * Start trace script 458 */ 459 static int python_start_script(const char *script, int argc, const char **argv) 460 { 461 const char **command_line; 462 char buf[PATH_MAX]; 463 int i, err = 0; 464 FILE *fp; 465 466 command_line = malloc((argc + 1) * sizeof(const char *)); 467 command_line[0] = script; 468 for (i = 1; i < argc + 1; i++) 469 command_line[i] = argv[i - 1]; 470 471 Py_Initialize(); 472 473 initperf_trace_context(); 474 475 PySys_SetArgv(argc + 1, (char **)command_line); 476 477 fp = fopen(script, "r"); 478 if (!fp) { 479 sprintf(buf, "Can't open python script \"%s\"", script); 480 perror(buf); 481 err = -1; 482 goto error; 483 } 484 485 err = PyRun_SimpleFile(fp, script); 486 if (err) { 487 fprintf(stderr, "Error running python script %s\n", script); 488 goto error; 489 } 490 491 err = run_start_sub(); 492 if (err) { 493 fprintf(stderr, "Error starting python script %s\n", script); 494 goto error; 495 } 496 497 free(command_line); 498 499 return err; 500 error: 501 Py_Finalize(); 502 free(command_line); 503 504 return err; 505 } 506 507 /* 508 * Stop trace script 509 */ 510 static int python_stop_script(void) 511 { 512 PyObject *handler, *retval; 513 int err = 0; 514 515 handler = PyDict_GetItemString(main_dict, "trace_end"); 516 if (handler == NULL || !PyCallable_Check(handler)) 517 goto out; 518 519 retval = PyObject_CallObject(handler, NULL); 520 if (retval == NULL) 521 handler_call_die("trace_end"); 522 else 523 Py_DECREF(retval); 524 out: 525 Py_XDECREF(main_dict); 526 Py_XDECREF(main_module); 527 Py_Finalize(); 528 529 return err; 530 } 531 532 static int python_generate_script(struct pevent *pevent, const char *outfile) 533 { 534 struct event_format *event = NULL; 535 struct format_field *f; 536 char fname[PATH_MAX]; 537 int not_first, count; 538 FILE *ofp; 539 540 sprintf(fname, "%s.py", outfile); 541 ofp = fopen(fname, "w"); 542 if (ofp == NULL) { 543 fprintf(stderr, "couldn't open %s\n", fname); 544 return -1; 545 } 546 fprintf(ofp, "# perf script event handlers, " 547 "generated by perf script -g python\n"); 548 549 fprintf(ofp, "# Licensed under the terms of the GNU GPL" 550 " License version 2\n\n"); 551 552 fprintf(ofp, "# The common_* event handler fields are the most useful " 553 "fields common to\n"); 554 555 fprintf(ofp, "# all events. They don't necessarily correspond to " 556 "the 'common_*' fields\n"); 557 558 fprintf(ofp, "# in the format files. Those fields not available as " 559 "handler params can\n"); 560 561 fprintf(ofp, "# be retrieved using Python functions of the form " 562 "common_*(context).\n"); 563 564 fprintf(ofp, "# See the perf-trace-python Documentation for the list " 565 "of available functions.\n\n"); 566 567 fprintf(ofp, "import os\n"); 568 fprintf(ofp, "import sys\n\n"); 569 570 fprintf(ofp, "sys.path.append(os.environ['PERF_EXEC_PATH'] + \\\n"); 571 fprintf(ofp, "\t'/scripts/python/Perf-Trace-Util/lib/Perf/Trace')\n"); 572 fprintf(ofp, "\nfrom perf_trace_context import *\n"); 573 fprintf(ofp, "from Core import *\n\n\n"); 574 575 fprintf(ofp, "def trace_begin():\n"); 576 fprintf(ofp, "\tprint \"in trace_begin\"\n\n"); 577 578 fprintf(ofp, "def trace_end():\n"); 579 fprintf(ofp, "\tprint \"in trace_end\"\n\n"); 580 581 while ((event = trace_find_next_event(pevent, event))) { 582 fprintf(ofp, "def %s__%s(", event->system, event->name); 583 fprintf(ofp, "event_name, "); 584 fprintf(ofp, "context, "); 585 fprintf(ofp, "common_cpu,\n"); 586 fprintf(ofp, "\tcommon_secs, "); 587 fprintf(ofp, "common_nsecs, "); 588 fprintf(ofp, "common_pid, "); 589 fprintf(ofp, "common_comm,\n\t"); 590 591 not_first = 0; 592 count = 0; 593 594 for (f = event->format.fields; f; f = f->next) { 595 if (not_first++) 596 fprintf(ofp, ", "); 597 if (++count % 5 == 0) 598 fprintf(ofp, "\n\t"); 599 600 fprintf(ofp, "%s", f->name); 601 } 602 fprintf(ofp, "):\n"); 603 604 fprintf(ofp, "\t\tprint_header(event_name, common_cpu, " 605 "common_secs, common_nsecs,\n\t\t\t" 606 "common_pid, common_comm)\n\n"); 607 608 fprintf(ofp, "\t\tprint \""); 609 610 not_first = 0; 611 count = 0; 612 613 for (f = event->format.fields; f; f = f->next) { 614 if (not_first++) 615 fprintf(ofp, ", "); 616 if (count && count % 3 == 0) { 617 fprintf(ofp, "\" \\\n\t\t\""); 618 } 619 count++; 620 621 fprintf(ofp, "%s=", f->name); 622 if (f->flags & FIELD_IS_STRING || 623 f->flags & FIELD_IS_FLAG || 624 f->flags & FIELD_IS_SYMBOLIC) 625 fprintf(ofp, "%%s"); 626 else if (f->flags & FIELD_IS_SIGNED) 627 fprintf(ofp, "%%d"); 628 else 629 fprintf(ofp, "%%u"); 630 } 631 632 fprintf(ofp, "\\n\" %% \\\n\t\t("); 633 634 not_first = 0; 635 count = 0; 636 637 for (f = event->format.fields; f; f = f->next) { 638 if (not_first++) 639 fprintf(ofp, ", "); 640 641 if (++count % 5 == 0) 642 fprintf(ofp, "\n\t\t"); 643 644 if (f->flags & FIELD_IS_FLAG) { 645 if ((count - 1) % 5 != 0) { 646 fprintf(ofp, "\n\t\t"); 647 count = 4; 648 } 649 fprintf(ofp, "flag_str(\""); 650 fprintf(ofp, "%s__%s\", ", event->system, 651 event->name); 652 fprintf(ofp, "\"%s\", %s)", f->name, 653 f->name); 654 } else if (f->flags & FIELD_IS_SYMBOLIC) { 655 if ((count - 1) % 5 != 0) { 656 fprintf(ofp, "\n\t\t"); 657 count = 4; 658 } 659 fprintf(ofp, "symbol_str(\""); 660 fprintf(ofp, "%s__%s\", ", event->system, 661 event->name); 662 fprintf(ofp, "\"%s\", %s)", f->name, 663 f->name); 664 } else 665 fprintf(ofp, "%s", f->name); 666 } 667 668 fprintf(ofp, "),\n\n"); 669 } 670 671 fprintf(ofp, "def trace_unhandled(event_name, context, " 672 "event_fields_dict):\n"); 673 674 fprintf(ofp, "\t\tprint ' '.join(['%%s=%%s'%%(k,str(v))" 675 "for k,v in sorted(event_fields_dict.items())])\n\n"); 676 677 fprintf(ofp, "def print_header(" 678 "event_name, cpu, secs, nsecs, pid, comm):\n" 679 "\tprint \"%%-20s %%5u %%05u.%%09u %%8u %%-20s \" %% \\\n\t" 680 "(event_name, cpu, secs, nsecs, pid, comm),\n"); 681 682 fclose(ofp); 683 684 fprintf(stderr, "generated Python script: %s\n", fname); 685 686 return 0; 687 } 688 689 struct scripting_ops python_scripting_ops = { 690 .name = "Python", 691 .start_script = python_start_script, 692 .stop_script = python_stop_script, 693 .process_event = python_process_event, 694 .generate_script = python_generate_script, 695 }; 696