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