1 /* 2 * trace-event-python. Feed trace events to an embedded Python interpreter. 3 * 4 * Copyright (C) 2010 Tom Zanussi <[email protected]> 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 <inttypes.h> 25 #include <stdio.h> 26 #include <stdlib.h> 27 #include <string.h> 28 #include <stdbool.h> 29 #include <errno.h> 30 #include <linux/bitmap.h> 31 #include <linux/compiler.h> 32 #include <linux/time64.h> 33 #ifdef HAVE_LIBTRACEEVENT 34 #include <traceevent/event-parse.h> 35 #endif 36 37 #include "../build-id.h" 38 #include "../counts.h" 39 #include "../debug.h" 40 #include "../dso.h" 41 #include "../callchain.h" 42 #include "../env.h" 43 #include "../evsel.h" 44 #include "../event.h" 45 #include "../thread.h" 46 #include "../comm.h" 47 #include "../machine.h" 48 #include "../db-export.h" 49 #include "../thread-stack.h" 50 #include "../trace-event.h" 51 #include "../call-path.h" 52 #include "map.h" 53 #include "symbol.h" 54 #include "thread_map.h" 55 #include "print_binary.h" 56 #include "stat.h" 57 #include "mem-events.h" 58 #include "util/perf_regs.h" 59 60 #if PY_MAJOR_VERSION < 3 61 #define _PyUnicode_FromString(arg) \ 62 PyString_FromString(arg) 63 #define _PyUnicode_FromStringAndSize(arg1, arg2) \ 64 PyString_FromStringAndSize((arg1), (arg2)) 65 #define _PyBytes_FromStringAndSize(arg1, arg2) \ 66 PyString_FromStringAndSize((arg1), (arg2)) 67 #define _PyLong_FromLong(arg) \ 68 PyInt_FromLong(arg) 69 #define _PyLong_AsLong(arg) \ 70 PyInt_AsLong(arg) 71 #define _PyCapsule_New(arg1, arg2, arg3) \ 72 PyCObject_FromVoidPtr((arg1), (arg2)) 73 74 PyMODINIT_FUNC initperf_trace_context(void); 75 #else 76 #define _PyUnicode_FromString(arg) \ 77 PyUnicode_FromString(arg) 78 #define _PyUnicode_FromStringAndSize(arg1, arg2) \ 79 PyUnicode_FromStringAndSize((arg1), (arg2)) 80 #define _PyBytes_FromStringAndSize(arg1, arg2) \ 81 PyBytes_FromStringAndSize((arg1), (arg2)) 82 #define _PyLong_FromLong(arg) \ 83 PyLong_FromLong(arg) 84 #define _PyLong_AsLong(arg) \ 85 PyLong_AsLong(arg) 86 #define _PyCapsule_New(arg1, arg2, arg3) \ 87 PyCapsule_New((arg1), (arg2), (arg3)) 88 89 PyMODINIT_FUNC PyInit_perf_trace_context(void); 90 #endif 91 92 #ifdef HAVE_LIBTRACEEVENT 93 #define TRACE_EVENT_TYPE_MAX \ 94 ((1 << (sizeof(unsigned short) * 8)) - 1) 95 96 #define N_COMMON_FIELDS 7 97 98 static char *cur_field_name; 99 static int zero_flag_atom; 100 #endif 101 102 #define MAX_FIELDS 64 103 104 extern struct scripting_context *scripting_context; 105 106 static PyObject *main_module, *main_dict; 107 108 struct tables { 109 struct db_export dbe; 110 PyObject *evsel_handler; 111 PyObject *machine_handler; 112 PyObject *thread_handler; 113 PyObject *comm_handler; 114 PyObject *comm_thread_handler; 115 PyObject *dso_handler; 116 PyObject *symbol_handler; 117 PyObject *branch_type_handler; 118 PyObject *sample_handler; 119 PyObject *call_path_handler; 120 PyObject *call_return_handler; 121 PyObject *synth_handler; 122 PyObject *context_switch_handler; 123 bool db_export_mode; 124 }; 125 126 static struct tables tables_global; 127 128 static void handler_call_die(const char *handler_name) __noreturn; 129 static void handler_call_die(const char *handler_name) 130 { 131 PyErr_Print(); 132 Py_FatalError("problem in Python trace event handler"); 133 // Py_FatalError does not return 134 // but we have to make the compiler happy 135 abort(); 136 } 137 138 /* 139 * Insert val into the dictionary and decrement the reference counter. 140 * This is necessary for dictionaries since PyDict_SetItemString() does not 141 * steal a reference, as opposed to PyTuple_SetItem(). 142 */ 143 static void pydict_set_item_string_decref(PyObject *dict, const char *key, PyObject *val) 144 { 145 PyDict_SetItemString(dict, key, val); 146 Py_DECREF(val); 147 } 148 149 static PyObject *get_handler(const char *handler_name) 150 { 151 PyObject *handler; 152 153 handler = PyDict_GetItemString(main_dict, handler_name); 154 if (handler && !PyCallable_Check(handler)) 155 return NULL; 156 return handler; 157 } 158 159 static void call_object(PyObject *handler, PyObject *args, const char *die_msg) 160 { 161 PyObject *retval; 162 163 retval = PyObject_CallObject(handler, args); 164 if (retval == NULL) 165 handler_call_die(die_msg); 166 Py_DECREF(retval); 167 } 168 169 static void try_call_object(const char *handler_name, PyObject *args) 170 { 171 PyObject *handler; 172 173 handler = get_handler(handler_name); 174 if (handler) 175 call_object(handler, args, handler_name); 176 } 177 178 #ifdef HAVE_LIBTRACEEVENT 179 static int get_argument_count(PyObject *handler) 180 { 181 int arg_count = 0; 182 183 /* 184 * The attribute for the code object is func_code in Python 2, 185 * whereas it is __code__ in Python 3.0+. 186 */ 187 PyObject *code_obj = PyObject_GetAttrString(handler, 188 "func_code"); 189 if (PyErr_Occurred()) { 190 PyErr_Clear(); 191 code_obj = PyObject_GetAttrString(handler, 192 "__code__"); 193 } 194 PyErr_Clear(); 195 if (code_obj) { 196 PyObject *arg_count_obj = PyObject_GetAttrString(code_obj, 197 "co_argcount"); 198 if (arg_count_obj) { 199 arg_count = (int) _PyLong_AsLong(arg_count_obj); 200 Py_DECREF(arg_count_obj); 201 } 202 Py_DECREF(code_obj); 203 } 204 return arg_count; 205 } 206 207 static void define_value(enum tep_print_arg_type field_type, 208 const char *ev_name, 209 const char *field_name, 210 const char *field_value, 211 const char *field_str) 212 { 213 const char *handler_name = "define_flag_value"; 214 PyObject *t; 215 unsigned long long value; 216 unsigned n = 0; 217 218 if (field_type == TEP_PRINT_SYMBOL) 219 handler_name = "define_symbolic_value"; 220 221 t = PyTuple_New(4); 222 if (!t) 223 Py_FatalError("couldn't create Python tuple"); 224 225 value = eval_flag(field_value); 226 227 PyTuple_SetItem(t, n++, _PyUnicode_FromString(ev_name)); 228 PyTuple_SetItem(t, n++, _PyUnicode_FromString(field_name)); 229 PyTuple_SetItem(t, n++, _PyLong_FromLong(value)); 230 PyTuple_SetItem(t, n++, _PyUnicode_FromString(field_str)); 231 232 try_call_object(handler_name, t); 233 234 Py_DECREF(t); 235 } 236 237 static void define_values(enum tep_print_arg_type field_type, 238 struct tep_print_flag_sym *field, 239 const char *ev_name, 240 const char *field_name) 241 { 242 define_value(field_type, ev_name, field_name, field->value, 243 field->str); 244 245 if (field->next) 246 define_values(field_type, field->next, ev_name, field_name); 247 } 248 249 static void define_field(enum tep_print_arg_type field_type, 250 const char *ev_name, 251 const char *field_name, 252 const char *delim) 253 { 254 const char *handler_name = "define_flag_field"; 255 PyObject *t; 256 unsigned n = 0; 257 258 if (field_type == TEP_PRINT_SYMBOL) 259 handler_name = "define_symbolic_field"; 260 261 if (field_type == TEP_PRINT_FLAGS) 262 t = PyTuple_New(3); 263 else 264 t = PyTuple_New(2); 265 if (!t) 266 Py_FatalError("couldn't create Python tuple"); 267 268 PyTuple_SetItem(t, n++, _PyUnicode_FromString(ev_name)); 269 PyTuple_SetItem(t, n++, _PyUnicode_FromString(field_name)); 270 if (field_type == TEP_PRINT_FLAGS) 271 PyTuple_SetItem(t, n++, _PyUnicode_FromString(delim)); 272 273 try_call_object(handler_name, t); 274 275 Py_DECREF(t); 276 } 277 278 static void define_event_symbols(struct tep_event *event, 279 const char *ev_name, 280 struct tep_print_arg *args) 281 { 282 if (args == NULL) 283 return; 284 285 switch (args->type) { 286 case TEP_PRINT_NULL: 287 break; 288 case TEP_PRINT_ATOM: 289 define_value(TEP_PRINT_FLAGS, ev_name, cur_field_name, "0", 290 args->atom.atom); 291 zero_flag_atom = 0; 292 break; 293 case TEP_PRINT_FIELD: 294 free(cur_field_name); 295 cur_field_name = strdup(args->field.name); 296 break; 297 case TEP_PRINT_FLAGS: 298 define_event_symbols(event, ev_name, args->flags.field); 299 define_field(TEP_PRINT_FLAGS, ev_name, cur_field_name, 300 args->flags.delim); 301 define_values(TEP_PRINT_FLAGS, args->flags.flags, ev_name, 302 cur_field_name); 303 break; 304 case TEP_PRINT_SYMBOL: 305 define_event_symbols(event, ev_name, args->symbol.field); 306 define_field(TEP_PRINT_SYMBOL, ev_name, cur_field_name, NULL); 307 define_values(TEP_PRINT_SYMBOL, args->symbol.symbols, ev_name, 308 cur_field_name); 309 break; 310 case TEP_PRINT_HEX: 311 case TEP_PRINT_HEX_STR: 312 define_event_symbols(event, ev_name, args->hex.field); 313 define_event_symbols(event, ev_name, args->hex.size); 314 break; 315 case TEP_PRINT_INT_ARRAY: 316 define_event_symbols(event, ev_name, args->int_array.field); 317 define_event_symbols(event, ev_name, args->int_array.count); 318 define_event_symbols(event, ev_name, args->int_array.el_size); 319 break; 320 case TEP_PRINT_STRING: 321 break; 322 case TEP_PRINT_TYPE: 323 define_event_symbols(event, ev_name, args->typecast.item); 324 break; 325 case TEP_PRINT_OP: 326 if (strcmp(args->op.op, ":") == 0) 327 zero_flag_atom = 1; 328 define_event_symbols(event, ev_name, args->op.left); 329 define_event_symbols(event, ev_name, args->op.right); 330 break; 331 default: 332 /* gcc warns for these? */ 333 case TEP_PRINT_BSTRING: 334 case TEP_PRINT_DYNAMIC_ARRAY: 335 case TEP_PRINT_DYNAMIC_ARRAY_LEN: 336 case TEP_PRINT_FUNC: 337 case TEP_PRINT_BITMASK: 338 /* we should warn... */ 339 return; 340 } 341 342 if (args->next) 343 define_event_symbols(event, ev_name, args->next); 344 } 345 346 static PyObject *get_field_numeric_entry(struct tep_event *event, 347 struct tep_format_field *field, void *data) 348 { 349 bool is_array = field->flags & TEP_FIELD_IS_ARRAY; 350 PyObject *obj = NULL, *list = NULL; 351 unsigned long long val; 352 unsigned int item_size, n_items, i; 353 354 if (is_array) { 355 list = PyList_New(field->arraylen); 356 item_size = field->size / field->arraylen; 357 n_items = field->arraylen; 358 } else { 359 item_size = field->size; 360 n_items = 1; 361 } 362 363 for (i = 0; i < n_items; i++) { 364 365 val = read_size(event, data + field->offset + i * item_size, 366 item_size); 367 if (field->flags & TEP_FIELD_IS_SIGNED) { 368 if ((long long)val >= LONG_MIN && 369 (long long)val <= LONG_MAX) 370 obj = _PyLong_FromLong(val); 371 else 372 obj = PyLong_FromLongLong(val); 373 } else { 374 if (val <= LONG_MAX) 375 obj = _PyLong_FromLong(val); 376 else 377 obj = PyLong_FromUnsignedLongLong(val); 378 } 379 if (is_array) 380 PyList_SET_ITEM(list, i, obj); 381 } 382 if (is_array) 383 obj = list; 384 return obj; 385 } 386 #endif 387 388 static const char *get_dsoname(struct map *map) 389 { 390 const char *dsoname = "[unknown]"; 391 struct dso *dso = map ? map__dso(map) : NULL; 392 393 if (dso) { 394 if (symbol_conf.show_kernel_path && dso->long_name) 395 dsoname = dso->long_name; 396 else 397 dsoname = dso->name; 398 } 399 400 return dsoname; 401 } 402 403 static unsigned long get_offset(struct symbol *sym, struct addr_location *al) 404 { 405 unsigned long offset; 406 407 if (al->addr < sym->end) 408 offset = al->addr - sym->start; 409 else 410 offset = al->addr - map__start(al->map) - sym->start; 411 412 return offset; 413 } 414 415 static PyObject *python_process_callchain(struct perf_sample *sample, 416 struct evsel *evsel, 417 struct addr_location *al) 418 { 419 PyObject *pylist; 420 421 pylist = PyList_New(0); 422 if (!pylist) 423 Py_FatalError("couldn't create Python list"); 424 425 if (!symbol_conf.use_callchain || !sample->callchain) 426 goto exit; 427 428 if (thread__resolve_callchain(al->thread, &callchain_cursor, evsel, 429 sample, NULL, NULL, 430 scripting_max_stack) != 0) { 431 pr_err("Failed to resolve callchain. Skipping\n"); 432 goto exit; 433 } 434 callchain_cursor_commit(&callchain_cursor); 435 436 437 while (1) { 438 PyObject *pyelem; 439 struct callchain_cursor_node *node; 440 node = callchain_cursor_current(&callchain_cursor); 441 if (!node) 442 break; 443 444 pyelem = PyDict_New(); 445 if (!pyelem) 446 Py_FatalError("couldn't create Python dictionary"); 447 448 449 pydict_set_item_string_decref(pyelem, "ip", 450 PyLong_FromUnsignedLongLong(node->ip)); 451 452 if (node->ms.sym) { 453 PyObject *pysym = PyDict_New(); 454 if (!pysym) 455 Py_FatalError("couldn't create Python dictionary"); 456 pydict_set_item_string_decref(pysym, "start", 457 PyLong_FromUnsignedLongLong(node->ms.sym->start)); 458 pydict_set_item_string_decref(pysym, "end", 459 PyLong_FromUnsignedLongLong(node->ms.sym->end)); 460 pydict_set_item_string_decref(pysym, "binding", 461 _PyLong_FromLong(node->ms.sym->binding)); 462 pydict_set_item_string_decref(pysym, "name", 463 _PyUnicode_FromStringAndSize(node->ms.sym->name, 464 node->ms.sym->namelen)); 465 pydict_set_item_string_decref(pyelem, "sym", pysym); 466 467 if (node->ms.map) { 468 struct map *map = node->ms.map; 469 struct addr_location node_al; 470 unsigned long offset; 471 472 addr_location__init(&node_al); 473 node_al.addr = map__map_ip(map, node->ip); 474 node_al.map = map__get(map); 475 offset = get_offset(node->ms.sym, &node_al); 476 addr_location__exit(&node_al); 477 478 pydict_set_item_string_decref( 479 pyelem, "sym_off", 480 PyLong_FromUnsignedLongLong(offset)); 481 } 482 if (node->srcline && strcmp(":0", node->srcline)) { 483 pydict_set_item_string_decref( 484 pyelem, "sym_srcline", 485 _PyUnicode_FromString(node->srcline)); 486 } 487 } 488 489 if (node->ms.map) { 490 const char *dsoname = get_dsoname(node->ms.map); 491 492 pydict_set_item_string_decref(pyelem, "dso", 493 _PyUnicode_FromString(dsoname)); 494 } 495 496 callchain_cursor_advance(&callchain_cursor); 497 PyList_Append(pylist, pyelem); 498 Py_DECREF(pyelem); 499 } 500 501 exit: 502 return pylist; 503 } 504 505 static PyObject *python_process_brstack(struct perf_sample *sample, 506 struct thread *thread) 507 { 508 struct branch_stack *br = sample->branch_stack; 509 struct branch_entry *entries = perf_sample__branch_entries(sample); 510 PyObject *pylist; 511 u64 i; 512 513 pylist = PyList_New(0); 514 if (!pylist) 515 Py_FatalError("couldn't create Python list"); 516 517 if (!(br && br->nr)) 518 goto exit; 519 520 for (i = 0; i < br->nr; i++) { 521 PyObject *pyelem; 522 struct addr_location al; 523 const char *dsoname; 524 525 pyelem = PyDict_New(); 526 if (!pyelem) 527 Py_FatalError("couldn't create Python dictionary"); 528 529 pydict_set_item_string_decref(pyelem, "from", 530 PyLong_FromUnsignedLongLong(entries[i].from)); 531 pydict_set_item_string_decref(pyelem, "to", 532 PyLong_FromUnsignedLongLong(entries[i].to)); 533 pydict_set_item_string_decref(pyelem, "mispred", 534 PyBool_FromLong(entries[i].flags.mispred)); 535 pydict_set_item_string_decref(pyelem, "predicted", 536 PyBool_FromLong(entries[i].flags.predicted)); 537 pydict_set_item_string_decref(pyelem, "in_tx", 538 PyBool_FromLong(entries[i].flags.in_tx)); 539 pydict_set_item_string_decref(pyelem, "abort", 540 PyBool_FromLong(entries[i].flags.abort)); 541 pydict_set_item_string_decref(pyelem, "cycles", 542 PyLong_FromUnsignedLongLong(entries[i].flags.cycles)); 543 544 addr_location__init(&al); 545 thread__find_map_fb(thread, sample->cpumode, 546 entries[i].from, &al); 547 dsoname = get_dsoname(al.map); 548 pydict_set_item_string_decref(pyelem, "from_dsoname", 549 _PyUnicode_FromString(dsoname)); 550 551 thread__find_map_fb(thread, sample->cpumode, 552 entries[i].to, &al); 553 dsoname = get_dsoname(al.map); 554 pydict_set_item_string_decref(pyelem, "to_dsoname", 555 _PyUnicode_FromString(dsoname)); 556 557 addr_location__exit(&al); 558 PyList_Append(pylist, pyelem); 559 Py_DECREF(pyelem); 560 } 561 562 exit: 563 return pylist; 564 } 565 566 static int get_symoff(struct symbol *sym, struct addr_location *al, 567 bool print_off, char *bf, int size) 568 { 569 unsigned long offset; 570 571 if (!sym || !sym->name[0]) 572 return scnprintf(bf, size, "%s", "[unknown]"); 573 574 if (!print_off) 575 return scnprintf(bf, size, "%s", sym->name); 576 577 offset = get_offset(sym, al); 578 579 return scnprintf(bf, size, "%s+0x%x", sym->name, offset); 580 } 581 582 static int get_br_mspred(struct branch_flags *flags, char *bf, int size) 583 { 584 if (!flags->mispred && !flags->predicted) 585 return scnprintf(bf, size, "%s", "-"); 586 587 if (flags->mispred) 588 return scnprintf(bf, size, "%s", "M"); 589 590 return scnprintf(bf, size, "%s", "P"); 591 } 592 593 static PyObject *python_process_brstacksym(struct perf_sample *sample, 594 struct thread *thread) 595 { 596 struct branch_stack *br = sample->branch_stack; 597 struct branch_entry *entries = perf_sample__branch_entries(sample); 598 PyObject *pylist; 599 u64 i; 600 char bf[512]; 601 602 pylist = PyList_New(0); 603 if (!pylist) 604 Py_FatalError("couldn't create Python list"); 605 606 if (!(br && br->nr)) 607 goto exit; 608 609 for (i = 0; i < br->nr; i++) { 610 PyObject *pyelem; 611 struct addr_location al; 612 613 addr_location__init(&al); 614 pyelem = PyDict_New(); 615 if (!pyelem) 616 Py_FatalError("couldn't create Python dictionary"); 617 618 thread__find_symbol_fb(thread, sample->cpumode, 619 entries[i].from, &al); 620 get_symoff(al.sym, &al, true, bf, sizeof(bf)); 621 pydict_set_item_string_decref(pyelem, "from", 622 _PyUnicode_FromString(bf)); 623 624 thread__find_symbol_fb(thread, sample->cpumode, 625 entries[i].to, &al); 626 get_symoff(al.sym, &al, true, bf, sizeof(bf)); 627 pydict_set_item_string_decref(pyelem, "to", 628 _PyUnicode_FromString(bf)); 629 630 get_br_mspred(&entries[i].flags, bf, sizeof(bf)); 631 pydict_set_item_string_decref(pyelem, "pred", 632 _PyUnicode_FromString(bf)); 633 634 if (entries[i].flags.in_tx) { 635 pydict_set_item_string_decref(pyelem, "in_tx", 636 _PyUnicode_FromString("X")); 637 } else { 638 pydict_set_item_string_decref(pyelem, "in_tx", 639 _PyUnicode_FromString("-")); 640 } 641 642 if (entries[i].flags.abort) { 643 pydict_set_item_string_decref(pyelem, "abort", 644 _PyUnicode_FromString("A")); 645 } else { 646 pydict_set_item_string_decref(pyelem, "abort", 647 _PyUnicode_FromString("-")); 648 } 649 650 PyList_Append(pylist, pyelem); 651 Py_DECREF(pyelem); 652 addr_location__exit(&al); 653 } 654 655 exit: 656 return pylist; 657 } 658 659 static PyObject *get_sample_value_as_tuple(struct sample_read_value *value, 660 u64 read_format) 661 { 662 PyObject *t; 663 664 t = PyTuple_New(3); 665 if (!t) 666 Py_FatalError("couldn't create Python tuple"); 667 PyTuple_SetItem(t, 0, PyLong_FromUnsignedLongLong(value->id)); 668 PyTuple_SetItem(t, 1, PyLong_FromUnsignedLongLong(value->value)); 669 if (read_format & PERF_FORMAT_LOST) 670 PyTuple_SetItem(t, 2, PyLong_FromUnsignedLongLong(value->lost)); 671 672 return t; 673 } 674 675 static void set_sample_read_in_dict(PyObject *dict_sample, 676 struct perf_sample *sample, 677 struct evsel *evsel) 678 { 679 u64 read_format = evsel->core.attr.read_format; 680 PyObject *values; 681 unsigned int i; 682 683 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) { 684 pydict_set_item_string_decref(dict_sample, "time_enabled", 685 PyLong_FromUnsignedLongLong(sample->read.time_enabled)); 686 } 687 688 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) { 689 pydict_set_item_string_decref(dict_sample, "time_running", 690 PyLong_FromUnsignedLongLong(sample->read.time_running)); 691 } 692 693 if (read_format & PERF_FORMAT_GROUP) 694 values = PyList_New(sample->read.group.nr); 695 else 696 values = PyList_New(1); 697 698 if (!values) 699 Py_FatalError("couldn't create Python list"); 700 701 if (read_format & PERF_FORMAT_GROUP) { 702 struct sample_read_value *v = sample->read.group.values; 703 704 i = 0; 705 sample_read_group__for_each(v, sample->read.group.nr, read_format) { 706 PyObject *t = get_sample_value_as_tuple(v, read_format); 707 PyList_SET_ITEM(values, i, t); 708 i++; 709 } 710 } else { 711 PyObject *t = get_sample_value_as_tuple(&sample->read.one, 712 read_format); 713 PyList_SET_ITEM(values, 0, t); 714 } 715 pydict_set_item_string_decref(dict_sample, "values", values); 716 } 717 718 static void set_sample_datasrc_in_dict(PyObject *dict, 719 struct perf_sample *sample) 720 { 721 struct mem_info mi = { .data_src.val = sample->data_src }; 722 char decode[100]; 723 724 pydict_set_item_string_decref(dict, "datasrc", 725 PyLong_FromUnsignedLongLong(sample->data_src)); 726 727 perf_script__meminfo_scnprintf(decode, 100, &mi); 728 729 pydict_set_item_string_decref(dict, "datasrc_decode", 730 _PyUnicode_FromString(decode)); 731 } 732 733 static void regs_map(struct regs_dump *regs, uint64_t mask, const char *arch, char *bf, int size) 734 { 735 unsigned int i = 0, r; 736 int printed = 0; 737 738 if (size <= 0) 739 return; 740 741 bf[0] = 0; 742 743 if (!regs || !regs->regs) 744 return; 745 746 for_each_set_bit(r, (unsigned long *) &mask, sizeof(mask) * 8) { 747 u64 val = regs->regs[i++]; 748 749 printed += scnprintf(bf + printed, size - printed, 750 "%5s:0x%" PRIx64 " ", 751 perf_reg_name(r, arch), val); 752 } 753 } 754 755 static void set_regs_in_dict(PyObject *dict, 756 struct perf_sample *sample, 757 struct evsel *evsel) 758 { 759 struct perf_event_attr *attr = &evsel->core.attr; 760 const char *arch = perf_env__arch(evsel__env(evsel)); 761 762 /* 763 * Here value 28 is a constant size which can be used to print 764 * one register value and its corresponds to: 765 * 16 chars is to specify 64 bit register in hexadecimal. 766 * 2 chars is for appending "0x" to the hexadecimal value and 767 * 10 chars is for register name. 768 */ 769 int size = __sw_hweight64(attr->sample_regs_intr) * 28; 770 char *bf = malloc(size); 771 772 regs_map(&sample->intr_regs, attr->sample_regs_intr, arch, bf, sizeof(bf)); 773 774 pydict_set_item_string_decref(dict, "iregs", 775 _PyUnicode_FromString(bf)); 776 777 regs_map(&sample->user_regs, attr->sample_regs_user, arch, bf, sizeof(bf)); 778 779 pydict_set_item_string_decref(dict, "uregs", 780 _PyUnicode_FromString(bf)); 781 free(bf); 782 } 783 784 static void set_sym_in_dict(PyObject *dict, struct addr_location *al, 785 const char *dso_field, const char *dso_bid_field, 786 const char *dso_map_start, const char *dso_map_end, 787 const char *sym_field, const char *symoff_field) 788 { 789 char sbuild_id[SBUILD_ID_SIZE]; 790 791 if (al->map) { 792 struct dso *dso = map__dso(al->map); 793 794 pydict_set_item_string_decref(dict, dso_field, _PyUnicode_FromString(dso->name)); 795 build_id__sprintf(&dso->bid, sbuild_id); 796 pydict_set_item_string_decref(dict, dso_bid_field, 797 _PyUnicode_FromString(sbuild_id)); 798 pydict_set_item_string_decref(dict, dso_map_start, 799 PyLong_FromUnsignedLong(map__start(al->map))); 800 pydict_set_item_string_decref(dict, dso_map_end, 801 PyLong_FromUnsignedLong(map__end(al->map))); 802 } 803 if (al->sym) { 804 pydict_set_item_string_decref(dict, sym_field, 805 _PyUnicode_FromString(al->sym->name)); 806 pydict_set_item_string_decref(dict, symoff_field, 807 PyLong_FromUnsignedLong(get_offset(al->sym, al))); 808 } 809 } 810 811 static void set_sample_flags(PyObject *dict, u32 flags) 812 { 813 const char *ch = PERF_IP_FLAG_CHARS; 814 char *p, str[33]; 815 816 for (p = str; *ch; ch++, flags >>= 1) { 817 if (flags & 1) 818 *p++ = *ch; 819 } 820 *p = 0; 821 pydict_set_item_string_decref(dict, "flags", _PyUnicode_FromString(str)); 822 } 823 824 static void python_process_sample_flags(struct perf_sample *sample, PyObject *dict_sample) 825 { 826 char flags_disp[SAMPLE_FLAGS_BUF_SIZE]; 827 828 set_sample_flags(dict_sample, sample->flags); 829 perf_sample__sprintf_flags(sample->flags, flags_disp, sizeof(flags_disp)); 830 pydict_set_item_string_decref(dict_sample, "flags_disp", 831 _PyUnicode_FromString(flags_disp)); 832 } 833 834 static PyObject *get_perf_sample_dict(struct perf_sample *sample, 835 struct evsel *evsel, 836 struct addr_location *al, 837 struct addr_location *addr_al, 838 PyObject *callchain) 839 { 840 PyObject *dict, *dict_sample, *brstack, *brstacksym; 841 842 dict = PyDict_New(); 843 if (!dict) 844 Py_FatalError("couldn't create Python dictionary"); 845 846 dict_sample = PyDict_New(); 847 if (!dict_sample) 848 Py_FatalError("couldn't create Python dictionary"); 849 850 pydict_set_item_string_decref(dict, "ev_name", _PyUnicode_FromString(evsel__name(evsel))); 851 pydict_set_item_string_decref(dict, "attr", _PyBytes_FromStringAndSize((const char *)&evsel->core.attr, sizeof(evsel->core.attr))); 852 853 pydict_set_item_string_decref(dict_sample, "pid", 854 _PyLong_FromLong(sample->pid)); 855 pydict_set_item_string_decref(dict_sample, "tid", 856 _PyLong_FromLong(sample->tid)); 857 pydict_set_item_string_decref(dict_sample, "cpu", 858 _PyLong_FromLong(sample->cpu)); 859 pydict_set_item_string_decref(dict_sample, "ip", 860 PyLong_FromUnsignedLongLong(sample->ip)); 861 pydict_set_item_string_decref(dict_sample, "time", 862 PyLong_FromUnsignedLongLong(sample->time)); 863 pydict_set_item_string_decref(dict_sample, "period", 864 PyLong_FromUnsignedLongLong(sample->period)); 865 pydict_set_item_string_decref(dict_sample, "phys_addr", 866 PyLong_FromUnsignedLongLong(sample->phys_addr)); 867 pydict_set_item_string_decref(dict_sample, "addr", 868 PyLong_FromUnsignedLongLong(sample->addr)); 869 set_sample_read_in_dict(dict_sample, sample, evsel); 870 pydict_set_item_string_decref(dict_sample, "weight", 871 PyLong_FromUnsignedLongLong(sample->weight)); 872 pydict_set_item_string_decref(dict_sample, "transaction", 873 PyLong_FromUnsignedLongLong(sample->transaction)); 874 set_sample_datasrc_in_dict(dict_sample, sample); 875 pydict_set_item_string_decref(dict, "sample", dict_sample); 876 877 pydict_set_item_string_decref(dict, "raw_buf", _PyBytes_FromStringAndSize( 878 (const char *)sample->raw_data, sample->raw_size)); 879 pydict_set_item_string_decref(dict, "comm", 880 _PyUnicode_FromString(thread__comm_str(al->thread))); 881 set_sym_in_dict(dict, al, "dso", "dso_bid", "dso_map_start", "dso_map_end", 882 "symbol", "symoff"); 883 884 pydict_set_item_string_decref(dict, "callchain", callchain); 885 886 brstack = python_process_brstack(sample, al->thread); 887 pydict_set_item_string_decref(dict, "brstack", brstack); 888 889 brstacksym = python_process_brstacksym(sample, al->thread); 890 pydict_set_item_string_decref(dict, "brstacksym", brstacksym); 891 892 if (sample->machine_pid) { 893 pydict_set_item_string_decref(dict_sample, "machine_pid", 894 _PyLong_FromLong(sample->machine_pid)); 895 pydict_set_item_string_decref(dict_sample, "vcpu", 896 _PyLong_FromLong(sample->vcpu)); 897 } 898 899 pydict_set_item_string_decref(dict_sample, "cpumode", 900 _PyLong_FromLong((unsigned long)sample->cpumode)); 901 902 if (addr_al) { 903 pydict_set_item_string_decref(dict_sample, "addr_correlates_sym", 904 PyBool_FromLong(1)); 905 set_sym_in_dict(dict_sample, addr_al, "addr_dso", "addr_dso_bid", 906 "addr_dso_map_start", "addr_dso_map_end", 907 "addr_symbol", "addr_symoff"); 908 } 909 910 if (sample->flags) 911 python_process_sample_flags(sample, dict_sample); 912 913 /* Instructions per cycle (IPC) */ 914 if (sample->insn_cnt && sample->cyc_cnt) { 915 pydict_set_item_string_decref(dict_sample, "insn_cnt", 916 PyLong_FromUnsignedLongLong(sample->insn_cnt)); 917 pydict_set_item_string_decref(dict_sample, "cyc_cnt", 918 PyLong_FromUnsignedLongLong(sample->cyc_cnt)); 919 } 920 921 set_regs_in_dict(dict, sample, evsel); 922 923 return dict; 924 } 925 926 #ifdef HAVE_LIBTRACEEVENT 927 static void python_process_tracepoint(struct perf_sample *sample, 928 struct evsel *evsel, 929 struct addr_location *al, 930 struct addr_location *addr_al) 931 { 932 struct tep_event *event = evsel->tp_format; 933 PyObject *handler, *context, *t, *obj = NULL, *callchain; 934 PyObject *dict = NULL, *all_entries_dict = NULL; 935 static char handler_name[256]; 936 struct tep_format_field *field; 937 unsigned long s, ns; 938 unsigned n = 0; 939 int pid; 940 int cpu = sample->cpu; 941 void *data = sample->raw_data; 942 unsigned long long nsecs = sample->time; 943 const char *comm = thread__comm_str(al->thread); 944 const char *default_handler_name = "trace_unhandled"; 945 DECLARE_BITMAP(events_defined, TRACE_EVENT_TYPE_MAX); 946 947 bitmap_zero(events_defined, TRACE_EVENT_TYPE_MAX); 948 949 if (!event) { 950 snprintf(handler_name, sizeof(handler_name), 951 "ug! no event found for type %" PRIu64, (u64)evsel->core.attr.config); 952 Py_FatalError(handler_name); 953 } 954 955 pid = raw_field_value(event, "common_pid", data); 956 957 sprintf(handler_name, "%s__%s", event->system, event->name); 958 959 if (!__test_and_set_bit(event->id, events_defined)) 960 define_event_symbols(event, handler_name, event->print_fmt.args); 961 962 handler = get_handler(handler_name); 963 if (!handler) { 964 handler = get_handler(default_handler_name); 965 if (!handler) 966 return; 967 dict = PyDict_New(); 968 if (!dict) 969 Py_FatalError("couldn't create Python dict"); 970 } 971 972 t = PyTuple_New(MAX_FIELDS); 973 if (!t) 974 Py_FatalError("couldn't create Python tuple"); 975 976 977 s = nsecs / NSEC_PER_SEC; 978 ns = nsecs - s * NSEC_PER_SEC; 979 980 context = _PyCapsule_New(scripting_context, NULL, NULL); 981 982 PyTuple_SetItem(t, n++, _PyUnicode_FromString(handler_name)); 983 PyTuple_SetItem(t, n++, context); 984 985 /* ip unwinding */ 986 callchain = python_process_callchain(sample, evsel, al); 987 /* Need an additional reference for the perf_sample dict */ 988 Py_INCREF(callchain); 989 990 if (!dict) { 991 PyTuple_SetItem(t, n++, _PyLong_FromLong(cpu)); 992 PyTuple_SetItem(t, n++, _PyLong_FromLong(s)); 993 PyTuple_SetItem(t, n++, _PyLong_FromLong(ns)); 994 PyTuple_SetItem(t, n++, _PyLong_FromLong(pid)); 995 PyTuple_SetItem(t, n++, _PyUnicode_FromString(comm)); 996 PyTuple_SetItem(t, n++, callchain); 997 } else { 998 pydict_set_item_string_decref(dict, "common_cpu", _PyLong_FromLong(cpu)); 999 pydict_set_item_string_decref(dict, "common_s", _PyLong_FromLong(s)); 1000 pydict_set_item_string_decref(dict, "common_ns", _PyLong_FromLong(ns)); 1001 pydict_set_item_string_decref(dict, "common_pid", _PyLong_FromLong(pid)); 1002 pydict_set_item_string_decref(dict, "common_comm", _PyUnicode_FromString(comm)); 1003 pydict_set_item_string_decref(dict, "common_callchain", callchain); 1004 } 1005 for (field = event->format.fields; field; field = field->next) { 1006 unsigned int offset, len; 1007 unsigned long long val; 1008 1009 if (field->flags & TEP_FIELD_IS_ARRAY) { 1010 offset = field->offset; 1011 len = field->size; 1012 if (field->flags & TEP_FIELD_IS_DYNAMIC) { 1013 val = tep_read_number(scripting_context->pevent, 1014 data + offset, len); 1015 offset = val; 1016 len = offset >> 16; 1017 offset &= 0xffff; 1018 if (tep_field_is_relative(field->flags)) 1019 offset += field->offset + field->size; 1020 } 1021 if (field->flags & TEP_FIELD_IS_STRING && 1022 is_printable_array(data + offset, len)) { 1023 obj = _PyUnicode_FromString((char *) data + offset); 1024 } else { 1025 obj = PyByteArray_FromStringAndSize((const char *) data + offset, len); 1026 field->flags &= ~TEP_FIELD_IS_STRING; 1027 } 1028 } else { /* FIELD_IS_NUMERIC */ 1029 obj = get_field_numeric_entry(event, field, data); 1030 } 1031 if (!dict) 1032 PyTuple_SetItem(t, n++, obj); 1033 else 1034 pydict_set_item_string_decref(dict, field->name, obj); 1035 1036 } 1037 1038 if (dict) 1039 PyTuple_SetItem(t, n++, dict); 1040 1041 if (get_argument_count(handler) == (int) n + 1) { 1042 all_entries_dict = get_perf_sample_dict(sample, evsel, al, addr_al, 1043 callchain); 1044 PyTuple_SetItem(t, n++, all_entries_dict); 1045 } else { 1046 Py_DECREF(callchain); 1047 } 1048 1049 if (_PyTuple_Resize(&t, n) == -1) 1050 Py_FatalError("error resizing Python tuple"); 1051 1052 if (!dict) 1053 call_object(handler, t, handler_name); 1054 else 1055 call_object(handler, t, default_handler_name); 1056 1057 Py_DECREF(t); 1058 } 1059 #else 1060 static void python_process_tracepoint(struct perf_sample *sample __maybe_unused, 1061 struct evsel *evsel __maybe_unused, 1062 struct addr_location *al __maybe_unused, 1063 struct addr_location *addr_al __maybe_unused) 1064 { 1065 fprintf(stderr, "Tracepoint events are not supported because " 1066 "perf is not linked with libtraceevent.\n"); 1067 } 1068 #endif 1069 1070 static PyObject *tuple_new(unsigned int sz) 1071 { 1072 PyObject *t; 1073 1074 t = PyTuple_New(sz); 1075 if (!t) 1076 Py_FatalError("couldn't create Python tuple"); 1077 return t; 1078 } 1079 1080 static int tuple_set_s64(PyObject *t, unsigned int pos, s64 val) 1081 { 1082 #if BITS_PER_LONG == 64 1083 return PyTuple_SetItem(t, pos, _PyLong_FromLong(val)); 1084 #endif 1085 #if BITS_PER_LONG == 32 1086 return PyTuple_SetItem(t, pos, PyLong_FromLongLong(val)); 1087 #endif 1088 } 1089 1090 /* 1091 * Databases support only signed 64-bit numbers, so even though we are 1092 * exporting a u64, it must be as s64. 1093 */ 1094 #define tuple_set_d64 tuple_set_s64 1095 1096 static int tuple_set_u64(PyObject *t, unsigned int pos, u64 val) 1097 { 1098 #if BITS_PER_LONG == 64 1099 return PyTuple_SetItem(t, pos, PyLong_FromUnsignedLong(val)); 1100 #endif 1101 #if BITS_PER_LONG == 32 1102 return PyTuple_SetItem(t, pos, PyLong_FromUnsignedLongLong(val)); 1103 #endif 1104 } 1105 1106 static int tuple_set_u32(PyObject *t, unsigned int pos, u32 val) 1107 { 1108 return PyTuple_SetItem(t, pos, PyLong_FromUnsignedLong(val)); 1109 } 1110 1111 static int tuple_set_s32(PyObject *t, unsigned int pos, s32 val) 1112 { 1113 return PyTuple_SetItem(t, pos, _PyLong_FromLong(val)); 1114 } 1115 1116 static int tuple_set_bool(PyObject *t, unsigned int pos, bool val) 1117 { 1118 return PyTuple_SetItem(t, pos, PyBool_FromLong(val)); 1119 } 1120 1121 static int tuple_set_string(PyObject *t, unsigned int pos, const char *s) 1122 { 1123 return PyTuple_SetItem(t, pos, _PyUnicode_FromString(s)); 1124 } 1125 1126 static int tuple_set_bytes(PyObject *t, unsigned int pos, void *bytes, 1127 unsigned int sz) 1128 { 1129 return PyTuple_SetItem(t, pos, _PyBytes_FromStringAndSize(bytes, sz)); 1130 } 1131 1132 static int python_export_evsel(struct db_export *dbe, struct evsel *evsel) 1133 { 1134 struct tables *tables = container_of(dbe, struct tables, dbe); 1135 PyObject *t; 1136 1137 t = tuple_new(2); 1138 1139 tuple_set_d64(t, 0, evsel->db_id); 1140 tuple_set_string(t, 1, evsel__name(evsel)); 1141 1142 call_object(tables->evsel_handler, t, "evsel_table"); 1143 1144 Py_DECREF(t); 1145 1146 return 0; 1147 } 1148 1149 static int python_export_machine(struct db_export *dbe, 1150 struct machine *machine) 1151 { 1152 struct tables *tables = container_of(dbe, struct tables, dbe); 1153 PyObject *t; 1154 1155 t = tuple_new(3); 1156 1157 tuple_set_d64(t, 0, machine->db_id); 1158 tuple_set_s32(t, 1, machine->pid); 1159 tuple_set_string(t, 2, machine->root_dir ? machine->root_dir : ""); 1160 1161 call_object(tables->machine_handler, t, "machine_table"); 1162 1163 Py_DECREF(t); 1164 1165 return 0; 1166 } 1167 1168 static int python_export_thread(struct db_export *dbe, struct thread *thread, 1169 u64 main_thread_db_id, struct machine *machine) 1170 { 1171 struct tables *tables = container_of(dbe, struct tables, dbe); 1172 PyObject *t; 1173 1174 t = tuple_new(5); 1175 1176 tuple_set_d64(t, 0, thread__db_id(thread)); 1177 tuple_set_d64(t, 1, machine->db_id); 1178 tuple_set_d64(t, 2, main_thread_db_id); 1179 tuple_set_s32(t, 3, thread__pid(thread)); 1180 tuple_set_s32(t, 4, thread__tid(thread)); 1181 1182 call_object(tables->thread_handler, t, "thread_table"); 1183 1184 Py_DECREF(t); 1185 1186 return 0; 1187 } 1188 1189 static int python_export_comm(struct db_export *dbe, struct comm *comm, 1190 struct thread *thread) 1191 { 1192 struct tables *tables = container_of(dbe, struct tables, dbe); 1193 PyObject *t; 1194 1195 t = tuple_new(5); 1196 1197 tuple_set_d64(t, 0, comm->db_id); 1198 tuple_set_string(t, 1, comm__str(comm)); 1199 tuple_set_d64(t, 2, thread__db_id(thread)); 1200 tuple_set_d64(t, 3, comm->start); 1201 tuple_set_s32(t, 4, comm->exec); 1202 1203 call_object(tables->comm_handler, t, "comm_table"); 1204 1205 Py_DECREF(t); 1206 1207 return 0; 1208 } 1209 1210 static int python_export_comm_thread(struct db_export *dbe, u64 db_id, 1211 struct comm *comm, struct thread *thread) 1212 { 1213 struct tables *tables = container_of(dbe, struct tables, dbe); 1214 PyObject *t; 1215 1216 t = tuple_new(3); 1217 1218 tuple_set_d64(t, 0, db_id); 1219 tuple_set_d64(t, 1, comm->db_id); 1220 tuple_set_d64(t, 2, thread__db_id(thread)); 1221 1222 call_object(tables->comm_thread_handler, t, "comm_thread_table"); 1223 1224 Py_DECREF(t); 1225 1226 return 0; 1227 } 1228 1229 static int python_export_dso(struct db_export *dbe, struct dso *dso, 1230 struct machine *machine) 1231 { 1232 struct tables *tables = container_of(dbe, struct tables, dbe); 1233 char sbuild_id[SBUILD_ID_SIZE]; 1234 PyObject *t; 1235 1236 build_id__sprintf(&dso->bid, sbuild_id); 1237 1238 t = tuple_new(5); 1239 1240 tuple_set_d64(t, 0, dso->db_id); 1241 tuple_set_d64(t, 1, machine->db_id); 1242 tuple_set_string(t, 2, dso->short_name); 1243 tuple_set_string(t, 3, dso->long_name); 1244 tuple_set_string(t, 4, sbuild_id); 1245 1246 call_object(tables->dso_handler, t, "dso_table"); 1247 1248 Py_DECREF(t); 1249 1250 return 0; 1251 } 1252 1253 static int python_export_symbol(struct db_export *dbe, struct symbol *sym, 1254 struct dso *dso) 1255 { 1256 struct tables *tables = container_of(dbe, struct tables, dbe); 1257 u64 *sym_db_id = symbol__priv(sym); 1258 PyObject *t; 1259 1260 t = tuple_new(6); 1261 1262 tuple_set_d64(t, 0, *sym_db_id); 1263 tuple_set_d64(t, 1, dso->db_id); 1264 tuple_set_d64(t, 2, sym->start); 1265 tuple_set_d64(t, 3, sym->end); 1266 tuple_set_s32(t, 4, sym->binding); 1267 tuple_set_string(t, 5, sym->name); 1268 1269 call_object(tables->symbol_handler, t, "symbol_table"); 1270 1271 Py_DECREF(t); 1272 1273 return 0; 1274 } 1275 1276 static int python_export_branch_type(struct db_export *dbe, u32 branch_type, 1277 const char *name) 1278 { 1279 struct tables *tables = container_of(dbe, struct tables, dbe); 1280 PyObject *t; 1281 1282 t = tuple_new(2); 1283 1284 tuple_set_s32(t, 0, branch_type); 1285 tuple_set_string(t, 1, name); 1286 1287 call_object(tables->branch_type_handler, t, "branch_type_table"); 1288 1289 Py_DECREF(t); 1290 1291 return 0; 1292 } 1293 1294 static void python_export_sample_table(struct db_export *dbe, 1295 struct export_sample *es) 1296 { 1297 struct tables *tables = container_of(dbe, struct tables, dbe); 1298 PyObject *t; 1299 1300 t = tuple_new(25); 1301 1302 tuple_set_d64(t, 0, es->db_id); 1303 tuple_set_d64(t, 1, es->evsel->db_id); 1304 tuple_set_d64(t, 2, maps__machine(es->al->maps)->db_id); 1305 tuple_set_d64(t, 3, thread__db_id(es->al->thread)); 1306 tuple_set_d64(t, 4, es->comm_db_id); 1307 tuple_set_d64(t, 5, es->dso_db_id); 1308 tuple_set_d64(t, 6, es->sym_db_id); 1309 tuple_set_d64(t, 7, es->offset); 1310 tuple_set_d64(t, 8, es->sample->ip); 1311 tuple_set_d64(t, 9, es->sample->time); 1312 tuple_set_s32(t, 10, es->sample->cpu); 1313 tuple_set_d64(t, 11, es->addr_dso_db_id); 1314 tuple_set_d64(t, 12, es->addr_sym_db_id); 1315 tuple_set_d64(t, 13, es->addr_offset); 1316 tuple_set_d64(t, 14, es->sample->addr); 1317 tuple_set_d64(t, 15, es->sample->period); 1318 tuple_set_d64(t, 16, es->sample->weight); 1319 tuple_set_d64(t, 17, es->sample->transaction); 1320 tuple_set_d64(t, 18, es->sample->data_src); 1321 tuple_set_s32(t, 19, es->sample->flags & PERF_BRANCH_MASK); 1322 tuple_set_s32(t, 20, !!(es->sample->flags & PERF_IP_FLAG_IN_TX)); 1323 tuple_set_d64(t, 21, es->call_path_id); 1324 tuple_set_d64(t, 22, es->sample->insn_cnt); 1325 tuple_set_d64(t, 23, es->sample->cyc_cnt); 1326 tuple_set_s32(t, 24, es->sample->flags); 1327 1328 call_object(tables->sample_handler, t, "sample_table"); 1329 1330 Py_DECREF(t); 1331 } 1332 1333 static void python_export_synth(struct db_export *dbe, struct export_sample *es) 1334 { 1335 struct tables *tables = container_of(dbe, struct tables, dbe); 1336 PyObject *t; 1337 1338 t = tuple_new(3); 1339 1340 tuple_set_d64(t, 0, es->db_id); 1341 tuple_set_d64(t, 1, es->evsel->core.attr.config); 1342 tuple_set_bytes(t, 2, es->sample->raw_data, es->sample->raw_size); 1343 1344 call_object(tables->synth_handler, t, "synth_data"); 1345 1346 Py_DECREF(t); 1347 } 1348 1349 static int python_export_sample(struct db_export *dbe, 1350 struct export_sample *es) 1351 { 1352 struct tables *tables = container_of(dbe, struct tables, dbe); 1353 1354 python_export_sample_table(dbe, es); 1355 1356 if (es->evsel->core.attr.type == PERF_TYPE_SYNTH && tables->synth_handler) 1357 python_export_synth(dbe, es); 1358 1359 return 0; 1360 } 1361 1362 static int python_export_call_path(struct db_export *dbe, struct call_path *cp) 1363 { 1364 struct tables *tables = container_of(dbe, struct tables, dbe); 1365 PyObject *t; 1366 u64 parent_db_id, sym_db_id; 1367 1368 parent_db_id = cp->parent ? cp->parent->db_id : 0; 1369 sym_db_id = cp->sym ? *(u64 *)symbol__priv(cp->sym) : 0; 1370 1371 t = tuple_new(4); 1372 1373 tuple_set_d64(t, 0, cp->db_id); 1374 tuple_set_d64(t, 1, parent_db_id); 1375 tuple_set_d64(t, 2, sym_db_id); 1376 tuple_set_d64(t, 3, cp->ip); 1377 1378 call_object(tables->call_path_handler, t, "call_path_table"); 1379 1380 Py_DECREF(t); 1381 1382 return 0; 1383 } 1384 1385 static int python_export_call_return(struct db_export *dbe, 1386 struct call_return *cr) 1387 { 1388 struct tables *tables = container_of(dbe, struct tables, dbe); 1389 u64 comm_db_id = cr->comm ? cr->comm->db_id : 0; 1390 PyObject *t; 1391 1392 t = tuple_new(14); 1393 1394 tuple_set_d64(t, 0, cr->db_id); 1395 tuple_set_d64(t, 1, thread__db_id(cr->thread)); 1396 tuple_set_d64(t, 2, comm_db_id); 1397 tuple_set_d64(t, 3, cr->cp->db_id); 1398 tuple_set_d64(t, 4, cr->call_time); 1399 tuple_set_d64(t, 5, cr->return_time); 1400 tuple_set_d64(t, 6, cr->branch_count); 1401 tuple_set_d64(t, 7, cr->call_ref); 1402 tuple_set_d64(t, 8, cr->return_ref); 1403 tuple_set_d64(t, 9, cr->cp->parent->db_id); 1404 tuple_set_s32(t, 10, cr->flags); 1405 tuple_set_d64(t, 11, cr->parent_db_id); 1406 tuple_set_d64(t, 12, cr->insn_count); 1407 tuple_set_d64(t, 13, cr->cyc_count); 1408 1409 call_object(tables->call_return_handler, t, "call_return_table"); 1410 1411 Py_DECREF(t); 1412 1413 return 0; 1414 } 1415 1416 static int python_export_context_switch(struct db_export *dbe, u64 db_id, 1417 struct machine *machine, 1418 struct perf_sample *sample, 1419 u64 th_out_id, u64 comm_out_id, 1420 u64 th_in_id, u64 comm_in_id, int flags) 1421 { 1422 struct tables *tables = container_of(dbe, struct tables, dbe); 1423 PyObject *t; 1424 1425 t = tuple_new(9); 1426 1427 tuple_set_d64(t, 0, db_id); 1428 tuple_set_d64(t, 1, machine->db_id); 1429 tuple_set_d64(t, 2, sample->time); 1430 tuple_set_s32(t, 3, sample->cpu); 1431 tuple_set_d64(t, 4, th_out_id); 1432 tuple_set_d64(t, 5, comm_out_id); 1433 tuple_set_d64(t, 6, th_in_id); 1434 tuple_set_d64(t, 7, comm_in_id); 1435 tuple_set_s32(t, 8, flags); 1436 1437 call_object(tables->context_switch_handler, t, "context_switch"); 1438 1439 Py_DECREF(t); 1440 1441 return 0; 1442 } 1443 1444 static int python_process_call_return(struct call_return *cr, u64 *parent_db_id, 1445 void *data) 1446 { 1447 struct db_export *dbe = data; 1448 1449 return db_export__call_return(dbe, cr, parent_db_id); 1450 } 1451 1452 static void python_process_general_event(struct perf_sample *sample, 1453 struct evsel *evsel, 1454 struct addr_location *al, 1455 struct addr_location *addr_al) 1456 { 1457 PyObject *handler, *t, *dict, *callchain; 1458 static char handler_name[64]; 1459 unsigned n = 0; 1460 1461 snprintf(handler_name, sizeof(handler_name), "%s", "process_event"); 1462 1463 handler = get_handler(handler_name); 1464 if (!handler) 1465 return; 1466 1467 /* 1468 * Use the MAX_FIELDS to make the function expandable, though 1469 * currently there is only one item for the tuple. 1470 */ 1471 t = PyTuple_New(MAX_FIELDS); 1472 if (!t) 1473 Py_FatalError("couldn't create Python tuple"); 1474 1475 /* ip unwinding */ 1476 callchain = python_process_callchain(sample, evsel, al); 1477 dict = get_perf_sample_dict(sample, evsel, al, addr_al, callchain); 1478 1479 PyTuple_SetItem(t, n++, dict); 1480 if (_PyTuple_Resize(&t, n) == -1) 1481 Py_FatalError("error resizing Python tuple"); 1482 1483 call_object(handler, t, handler_name); 1484 1485 Py_DECREF(t); 1486 } 1487 1488 static void python_process_event(union perf_event *event, 1489 struct perf_sample *sample, 1490 struct evsel *evsel, 1491 struct addr_location *al, 1492 struct addr_location *addr_al) 1493 { 1494 struct tables *tables = &tables_global; 1495 1496 scripting_context__update(scripting_context, event, sample, evsel, al, addr_al); 1497 1498 switch (evsel->core.attr.type) { 1499 case PERF_TYPE_TRACEPOINT: 1500 python_process_tracepoint(sample, evsel, al, addr_al); 1501 break; 1502 /* Reserve for future process_hw/sw/raw APIs */ 1503 default: 1504 if (tables->db_export_mode) 1505 db_export__sample(&tables->dbe, event, sample, evsel, al, addr_al); 1506 else 1507 python_process_general_event(sample, evsel, al, addr_al); 1508 } 1509 } 1510 1511 static void python_process_throttle(union perf_event *event, 1512 struct perf_sample *sample, 1513 struct machine *machine) 1514 { 1515 const char *handler_name; 1516 PyObject *handler, *t; 1517 1518 if (event->header.type == PERF_RECORD_THROTTLE) 1519 handler_name = "throttle"; 1520 else 1521 handler_name = "unthrottle"; 1522 handler = get_handler(handler_name); 1523 if (!handler) 1524 return; 1525 1526 t = tuple_new(6); 1527 if (!t) 1528 return; 1529 1530 tuple_set_u64(t, 0, event->throttle.time); 1531 tuple_set_u64(t, 1, event->throttle.id); 1532 tuple_set_u64(t, 2, event->throttle.stream_id); 1533 tuple_set_s32(t, 3, sample->cpu); 1534 tuple_set_s32(t, 4, sample->pid); 1535 tuple_set_s32(t, 5, sample->tid); 1536 1537 call_object(handler, t, handler_name); 1538 1539 Py_DECREF(t); 1540 } 1541 1542 static void python_do_process_switch(union perf_event *event, 1543 struct perf_sample *sample, 1544 struct machine *machine) 1545 { 1546 const char *handler_name = "context_switch"; 1547 bool out = event->header.misc & PERF_RECORD_MISC_SWITCH_OUT; 1548 bool out_preempt = out && (event->header.misc & PERF_RECORD_MISC_SWITCH_OUT_PREEMPT); 1549 pid_t np_pid = -1, np_tid = -1; 1550 PyObject *handler, *t; 1551 1552 handler = get_handler(handler_name); 1553 if (!handler) 1554 return; 1555 1556 if (event->header.type == PERF_RECORD_SWITCH_CPU_WIDE) { 1557 np_pid = event->context_switch.next_prev_pid; 1558 np_tid = event->context_switch.next_prev_tid; 1559 } 1560 1561 t = tuple_new(11); 1562 if (!t) 1563 return; 1564 1565 tuple_set_u64(t, 0, sample->time); 1566 tuple_set_s32(t, 1, sample->cpu); 1567 tuple_set_s32(t, 2, sample->pid); 1568 tuple_set_s32(t, 3, sample->tid); 1569 tuple_set_s32(t, 4, np_pid); 1570 tuple_set_s32(t, 5, np_tid); 1571 tuple_set_s32(t, 6, machine->pid); 1572 tuple_set_bool(t, 7, out); 1573 tuple_set_bool(t, 8, out_preempt); 1574 tuple_set_s32(t, 9, sample->machine_pid); 1575 tuple_set_s32(t, 10, sample->vcpu); 1576 1577 call_object(handler, t, handler_name); 1578 1579 Py_DECREF(t); 1580 } 1581 1582 static void python_process_switch(union perf_event *event, 1583 struct perf_sample *sample, 1584 struct machine *machine) 1585 { 1586 struct tables *tables = &tables_global; 1587 1588 if (tables->db_export_mode) 1589 db_export__switch(&tables->dbe, event, sample, machine); 1590 else 1591 python_do_process_switch(event, sample, machine); 1592 } 1593 1594 static void python_process_auxtrace_error(struct perf_session *session __maybe_unused, 1595 union perf_event *event) 1596 { 1597 struct perf_record_auxtrace_error *e = &event->auxtrace_error; 1598 u8 cpumode = e->header.misc & PERF_RECORD_MISC_CPUMODE_MASK; 1599 const char *handler_name = "auxtrace_error"; 1600 unsigned long long tm = e->time; 1601 const char *msg = e->msg; 1602 PyObject *handler, *t; 1603 1604 handler = get_handler(handler_name); 1605 if (!handler) 1606 return; 1607 1608 if (!e->fmt) { 1609 tm = 0; 1610 msg = (const char *)&e->time; 1611 } 1612 1613 t = tuple_new(11); 1614 1615 tuple_set_u32(t, 0, e->type); 1616 tuple_set_u32(t, 1, e->code); 1617 tuple_set_s32(t, 2, e->cpu); 1618 tuple_set_s32(t, 3, e->pid); 1619 tuple_set_s32(t, 4, e->tid); 1620 tuple_set_u64(t, 5, e->ip); 1621 tuple_set_u64(t, 6, tm); 1622 tuple_set_string(t, 7, msg); 1623 tuple_set_u32(t, 8, cpumode); 1624 tuple_set_s32(t, 9, e->machine_pid); 1625 tuple_set_s32(t, 10, e->vcpu); 1626 1627 call_object(handler, t, handler_name); 1628 1629 Py_DECREF(t); 1630 } 1631 1632 static void get_handler_name(char *str, size_t size, 1633 struct evsel *evsel) 1634 { 1635 char *p = str; 1636 1637 scnprintf(str, size, "stat__%s", evsel__name(evsel)); 1638 1639 while ((p = strchr(p, ':'))) { 1640 *p = '_'; 1641 p++; 1642 } 1643 } 1644 1645 static void 1646 process_stat(struct evsel *counter, struct perf_cpu cpu, int thread, u64 tstamp, 1647 struct perf_counts_values *count) 1648 { 1649 PyObject *handler, *t; 1650 static char handler_name[256]; 1651 int n = 0; 1652 1653 t = PyTuple_New(MAX_FIELDS); 1654 if (!t) 1655 Py_FatalError("couldn't create Python tuple"); 1656 1657 get_handler_name(handler_name, sizeof(handler_name), 1658 counter); 1659 1660 handler = get_handler(handler_name); 1661 if (!handler) { 1662 pr_debug("can't find python handler %s\n", handler_name); 1663 return; 1664 } 1665 1666 PyTuple_SetItem(t, n++, _PyLong_FromLong(cpu.cpu)); 1667 PyTuple_SetItem(t, n++, _PyLong_FromLong(thread)); 1668 1669 tuple_set_u64(t, n++, tstamp); 1670 tuple_set_u64(t, n++, count->val); 1671 tuple_set_u64(t, n++, count->ena); 1672 tuple_set_u64(t, n++, count->run); 1673 1674 if (_PyTuple_Resize(&t, n) == -1) 1675 Py_FatalError("error resizing Python tuple"); 1676 1677 call_object(handler, t, handler_name); 1678 1679 Py_DECREF(t); 1680 } 1681 1682 static void python_process_stat(struct perf_stat_config *config, 1683 struct evsel *counter, u64 tstamp) 1684 { 1685 struct perf_thread_map *threads = counter->core.threads; 1686 struct perf_cpu_map *cpus = counter->core.cpus; 1687 int cpu, thread; 1688 1689 for (thread = 0; thread < perf_thread_map__nr(threads); thread++) { 1690 for (cpu = 0; cpu < perf_cpu_map__nr(cpus); cpu++) { 1691 process_stat(counter, perf_cpu_map__cpu(cpus, cpu), 1692 perf_thread_map__pid(threads, thread), tstamp, 1693 perf_counts(counter->counts, cpu, thread)); 1694 } 1695 } 1696 } 1697 1698 static void python_process_stat_interval(u64 tstamp) 1699 { 1700 PyObject *handler, *t; 1701 static const char handler_name[] = "stat__interval"; 1702 int n = 0; 1703 1704 t = PyTuple_New(MAX_FIELDS); 1705 if (!t) 1706 Py_FatalError("couldn't create Python tuple"); 1707 1708 handler = get_handler(handler_name); 1709 if (!handler) { 1710 pr_debug("can't find python handler %s\n", handler_name); 1711 return; 1712 } 1713 1714 tuple_set_u64(t, n++, tstamp); 1715 1716 if (_PyTuple_Resize(&t, n) == -1) 1717 Py_FatalError("error resizing Python tuple"); 1718 1719 call_object(handler, t, handler_name); 1720 1721 Py_DECREF(t); 1722 } 1723 1724 static int perf_script_context_init(void) 1725 { 1726 PyObject *perf_script_context; 1727 PyObject *perf_trace_context; 1728 PyObject *dict; 1729 int ret; 1730 1731 perf_trace_context = PyImport_AddModule("perf_trace_context"); 1732 if (!perf_trace_context) 1733 return -1; 1734 dict = PyModule_GetDict(perf_trace_context); 1735 if (!dict) 1736 return -1; 1737 1738 perf_script_context = _PyCapsule_New(scripting_context, NULL, NULL); 1739 if (!perf_script_context) 1740 return -1; 1741 1742 ret = PyDict_SetItemString(dict, "perf_script_context", perf_script_context); 1743 if (!ret) 1744 ret = PyDict_SetItemString(main_dict, "perf_script_context", perf_script_context); 1745 Py_DECREF(perf_script_context); 1746 return ret; 1747 } 1748 1749 static int run_start_sub(void) 1750 { 1751 main_module = PyImport_AddModule("__main__"); 1752 if (main_module == NULL) 1753 return -1; 1754 Py_INCREF(main_module); 1755 1756 main_dict = PyModule_GetDict(main_module); 1757 if (main_dict == NULL) 1758 goto error; 1759 Py_INCREF(main_dict); 1760 1761 if (perf_script_context_init()) 1762 goto error; 1763 1764 try_call_object("trace_begin", NULL); 1765 1766 return 0; 1767 1768 error: 1769 Py_XDECREF(main_dict); 1770 Py_XDECREF(main_module); 1771 return -1; 1772 } 1773 1774 #define SET_TABLE_HANDLER_(name, handler_name, table_name) do { \ 1775 tables->handler_name = get_handler(#table_name); \ 1776 if (tables->handler_name) \ 1777 tables->dbe.export_ ## name = python_export_ ## name; \ 1778 } while (0) 1779 1780 #define SET_TABLE_HANDLER(name) \ 1781 SET_TABLE_HANDLER_(name, name ## _handler, name ## _table) 1782 1783 static void set_table_handlers(struct tables *tables) 1784 { 1785 const char *perf_db_export_mode = "perf_db_export_mode"; 1786 const char *perf_db_export_calls = "perf_db_export_calls"; 1787 const char *perf_db_export_callchains = "perf_db_export_callchains"; 1788 PyObject *db_export_mode, *db_export_calls, *db_export_callchains; 1789 bool export_calls = false; 1790 bool export_callchains = false; 1791 int ret; 1792 1793 memset(tables, 0, sizeof(struct tables)); 1794 if (db_export__init(&tables->dbe)) 1795 Py_FatalError("failed to initialize export"); 1796 1797 db_export_mode = PyDict_GetItemString(main_dict, perf_db_export_mode); 1798 if (!db_export_mode) 1799 return; 1800 1801 ret = PyObject_IsTrue(db_export_mode); 1802 if (ret == -1) 1803 handler_call_die(perf_db_export_mode); 1804 if (!ret) 1805 return; 1806 1807 /* handle export calls */ 1808 tables->dbe.crp = NULL; 1809 db_export_calls = PyDict_GetItemString(main_dict, perf_db_export_calls); 1810 if (db_export_calls) { 1811 ret = PyObject_IsTrue(db_export_calls); 1812 if (ret == -1) 1813 handler_call_die(perf_db_export_calls); 1814 export_calls = !!ret; 1815 } 1816 1817 if (export_calls) { 1818 tables->dbe.crp = 1819 call_return_processor__new(python_process_call_return, 1820 &tables->dbe); 1821 if (!tables->dbe.crp) 1822 Py_FatalError("failed to create calls processor"); 1823 } 1824 1825 /* handle export callchains */ 1826 tables->dbe.cpr = NULL; 1827 db_export_callchains = PyDict_GetItemString(main_dict, 1828 perf_db_export_callchains); 1829 if (db_export_callchains) { 1830 ret = PyObject_IsTrue(db_export_callchains); 1831 if (ret == -1) 1832 handler_call_die(perf_db_export_callchains); 1833 export_callchains = !!ret; 1834 } 1835 1836 if (export_callchains) { 1837 /* 1838 * Attempt to use the call path root from the call return 1839 * processor, if the call return processor is in use. Otherwise, 1840 * we allocate a new call path root. This prevents exporting 1841 * duplicate call path ids when both are in use simultaneously. 1842 */ 1843 if (tables->dbe.crp) 1844 tables->dbe.cpr = tables->dbe.crp->cpr; 1845 else 1846 tables->dbe.cpr = call_path_root__new(); 1847 1848 if (!tables->dbe.cpr) 1849 Py_FatalError("failed to create call path root"); 1850 } 1851 1852 tables->db_export_mode = true; 1853 /* 1854 * Reserve per symbol space for symbol->db_id via symbol__priv() 1855 */ 1856 symbol_conf.priv_size = sizeof(u64); 1857 1858 SET_TABLE_HANDLER(evsel); 1859 SET_TABLE_HANDLER(machine); 1860 SET_TABLE_HANDLER(thread); 1861 SET_TABLE_HANDLER(comm); 1862 SET_TABLE_HANDLER(comm_thread); 1863 SET_TABLE_HANDLER(dso); 1864 SET_TABLE_HANDLER(symbol); 1865 SET_TABLE_HANDLER(branch_type); 1866 SET_TABLE_HANDLER(sample); 1867 SET_TABLE_HANDLER(call_path); 1868 SET_TABLE_HANDLER(call_return); 1869 SET_TABLE_HANDLER(context_switch); 1870 1871 /* 1872 * Synthesized events are samples but with architecture-specific data 1873 * stored in sample->raw_data. They are exported via 1874 * python_export_sample() and consequently do not need a separate export 1875 * callback. 1876 */ 1877 tables->synth_handler = get_handler("synth_data"); 1878 } 1879 1880 #if PY_MAJOR_VERSION < 3 1881 static void _free_command_line(const char **command_line, int num) 1882 { 1883 free(command_line); 1884 } 1885 #else 1886 static void _free_command_line(wchar_t **command_line, int num) 1887 { 1888 int i; 1889 for (i = 0; i < num; i++) 1890 PyMem_RawFree(command_line[i]); 1891 free(command_line); 1892 } 1893 #endif 1894 1895 1896 /* 1897 * Start trace script 1898 */ 1899 static int python_start_script(const char *script, int argc, const char **argv, 1900 struct perf_session *session) 1901 { 1902 struct tables *tables = &tables_global; 1903 #if PY_MAJOR_VERSION < 3 1904 const char **command_line; 1905 #else 1906 wchar_t **command_line; 1907 #endif 1908 /* 1909 * Use a non-const name variable to cope with python 2.6's 1910 * PyImport_AppendInittab prototype 1911 */ 1912 char buf[PATH_MAX], name[19] = "perf_trace_context"; 1913 int i, err = 0; 1914 FILE *fp; 1915 1916 scripting_context->session = session; 1917 #if PY_MAJOR_VERSION < 3 1918 command_line = malloc((argc + 1) * sizeof(const char *)); 1919 command_line[0] = script; 1920 for (i = 1; i < argc + 1; i++) 1921 command_line[i] = argv[i - 1]; 1922 PyImport_AppendInittab(name, initperf_trace_context); 1923 #else 1924 command_line = malloc((argc + 1) * sizeof(wchar_t *)); 1925 command_line[0] = Py_DecodeLocale(script, NULL); 1926 for (i = 1; i < argc + 1; i++) 1927 command_line[i] = Py_DecodeLocale(argv[i - 1], NULL); 1928 PyImport_AppendInittab(name, PyInit_perf_trace_context); 1929 #endif 1930 Py_Initialize(); 1931 1932 #if PY_MAJOR_VERSION < 3 1933 PySys_SetArgv(argc + 1, (char **)command_line); 1934 #else 1935 PySys_SetArgv(argc + 1, command_line); 1936 #endif 1937 1938 fp = fopen(script, "r"); 1939 if (!fp) { 1940 sprintf(buf, "Can't open python script \"%s\"", script); 1941 perror(buf); 1942 err = -1; 1943 goto error; 1944 } 1945 1946 err = PyRun_SimpleFile(fp, script); 1947 if (err) { 1948 fprintf(stderr, "Error running python script %s\n", script); 1949 goto error; 1950 } 1951 1952 err = run_start_sub(); 1953 if (err) { 1954 fprintf(stderr, "Error starting python script %s\n", script); 1955 goto error; 1956 } 1957 1958 set_table_handlers(tables); 1959 1960 if (tables->db_export_mode) { 1961 err = db_export__branch_types(&tables->dbe); 1962 if (err) 1963 goto error; 1964 } 1965 1966 _free_command_line(command_line, argc + 1); 1967 1968 return err; 1969 error: 1970 Py_Finalize(); 1971 _free_command_line(command_line, argc + 1); 1972 1973 return err; 1974 } 1975 1976 static int python_flush_script(void) 1977 { 1978 return 0; 1979 } 1980 1981 /* 1982 * Stop trace script 1983 */ 1984 static int python_stop_script(void) 1985 { 1986 struct tables *tables = &tables_global; 1987 1988 try_call_object("trace_end", NULL); 1989 1990 db_export__exit(&tables->dbe); 1991 1992 Py_XDECREF(main_dict); 1993 Py_XDECREF(main_module); 1994 Py_Finalize(); 1995 1996 return 0; 1997 } 1998 1999 #ifdef HAVE_LIBTRACEEVENT 2000 static int python_generate_script(struct tep_handle *pevent, const char *outfile) 2001 { 2002 int i, not_first, count, nr_events; 2003 struct tep_event **all_events; 2004 struct tep_event *event = NULL; 2005 struct tep_format_field *f; 2006 char fname[PATH_MAX]; 2007 FILE *ofp; 2008 2009 sprintf(fname, "%s.py", outfile); 2010 ofp = fopen(fname, "w"); 2011 if (ofp == NULL) { 2012 fprintf(stderr, "couldn't open %s\n", fname); 2013 return -1; 2014 } 2015 fprintf(ofp, "# perf script event handlers, " 2016 "generated by perf script -g python\n"); 2017 2018 fprintf(ofp, "# Licensed under the terms of the GNU GPL" 2019 " License version 2\n\n"); 2020 2021 fprintf(ofp, "# The common_* event handler fields are the most useful " 2022 "fields common to\n"); 2023 2024 fprintf(ofp, "# all events. They don't necessarily correspond to " 2025 "the 'common_*' fields\n"); 2026 2027 fprintf(ofp, "# in the format files. Those fields not available as " 2028 "handler params can\n"); 2029 2030 fprintf(ofp, "# be retrieved using Python functions of the form " 2031 "common_*(context).\n"); 2032 2033 fprintf(ofp, "# See the perf-script-python Documentation for the list " 2034 "of available functions.\n\n"); 2035 2036 fprintf(ofp, "from __future__ import print_function\n\n"); 2037 fprintf(ofp, "import os\n"); 2038 fprintf(ofp, "import sys\n\n"); 2039 2040 fprintf(ofp, "sys.path.append(os.environ['PERF_EXEC_PATH'] + \\\n"); 2041 fprintf(ofp, "\t'/scripts/python/Perf-Trace-Util/lib/Perf/Trace')\n"); 2042 fprintf(ofp, "\nfrom perf_trace_context import *\n"); 2043 fprintf(ofp, "from Core import *\n\n\n"); 2044 2045 fprintf(ofp, "def trace_begin():\n"); 2046 fprintf(ofp, "\tprint(\"in trace_begin\")\n\n"); 2047 2048 fprintf(ofp, "def trace_end():\n"); 2049 fprintf(ofp, "\tprint(\"in trace_end\")\n\n"); 2050 2051 nr_events = tep_get_events_count(pevent); 2052 all_events = tep_list_events(pevent, TEP_EVENT_SORT_ID); 2053 2054 for (i = 0; all_events && i < nr_events; i++) { 2055 event = all_events[i]; 2056 fprintf(ofp, "def %s__%s(", event->system, event->name); 2057 fprintf(ofp, "event_name, "); 2058 fprintf(ofp, "context, "); 2059 fprintf(ofp, "common_cpu,\n"); 2060 fprintf(ofp, "\tcommon_secs, "); 2061 fprintf(ofp, "common_nsecs, "); 2062 fprintf(ofp, "common_pid, "); 2063 fprintf(ofp, "common_comm,\n\t"); 2064 fprintf(ofp, "common_callchain, "); 2065 2066 not_first = 0; 2067 count = 0; 2068 2069 for (f = event->format.fields; f; f = f->next) { 2070 if (not_first++) 2071 fprintf(ofp, ", "); 2072 if (++count % 5 == 0) 2073 fprintf(ofp, "\n\t"); 2074 2075 fprintf(ofp, "%s", f->name); 2076 } 2077 if (not_first++) 2078 fprintf(ofp, ", "); 2079 if (++count % 5 == 0) 2080 fprintf(ofp, "\n\t\t"); 2081 fprintf(ofp, "perf_sample_dict"); 2082 2083 fprintf(ofp, "):\n"); 2084 2085 fprintf(ofp, "\t\tprint_header(event_name, common_cpu, " 2086 "common_secs, common_nsecs,\n\t\t\t" 2087 "common_pid, common_comm)\n\n"); 2088 2089 fprintf(ofp, "\t\tprint(\""); 2090 2091 not_first = 0; 2092 count = 0; 2093 2094 for (f = event->format.fields; f; f = f->next) { 2095 if (not_first++) 2096 fprintf(ofp, ", "); 2097 if (count && count % 3 == 0) { 2098 fprintf(ofp, "\" \\\n\t\t\""); 2099 } 2100 count++; 2101 2102 fprintf(ofp, "%s=", f->name); 2103 if (f->flags & TEP_FIELD_IS_STRING || 2104 f->flags & TEP_FIELD_IS_FLAG || 2105 f->flags & TEP_FIELD_IS_ARRAY || 2106 f->flags & TEP_FIELD_IS_SYMBOLIC) 2107 fprintf(ofp, "%%s"); 2108 else if (f->flags & TEP_FIELD_IS_SIGNED) 2109 fprintf(ofp, "%%d"); 2110 else 2111 fprintf(ofp, "%%u"); 2112 } 2113 2114 fprintf(ofp, "\" %% \\\n\t\t("); 2115 2116 not_first = 0; 2117 count = 0; 2118 2119 for (f = event->format.fields; f; f = f->next) { 2120 if (not_first++) 2121 fprintf(ofp, ", "); 2122 2123 if (++count % 5 == 0) 2124 fprintf(ofp, "\n\t\t"); 2125 2126 if (f->flags & TEP_FIELD_IS_FLAG) { 2127 if ((count - 1) % 5 != 0) { 2128 fprintf(ofp, "\n\t\t"); 2129 count = 4; 2130 } 2131 fprintf(ofp, "flag_str(\""); 2132 fprintf(ofp, "%s__%s\", ", event->system, 2133 event->name); 2134 fprintf(ofp, "\"%s\", %s)", f->name, 2135 f->name); 2136 } else if (f->flags & TEP_FIELD_IS_SYMBOLIC) { 2137 if ((count - 1) % 5 != 0) { 2138 fprintf(ofp, "\n\t\t"); 2139 count = 4; 2140 } 2141 fprintf(ofp, "symbol_str(\""); 2142 fprintf(ofp, "%s__%s\", ", event->system, 2143 event->name); 2144 fprintf(ofp, "\"%s\", %s)", f->name, 2145 f->name); 2146 } else 2147 fprintf(ofp, "%s", f->name); 2148 } 2149 2150 fprintf(ofp, "))\n\n"); 2151 2152 fprintf(ofp, "\t\tprint('Sample: {'+" 2153 "get_dict_as_string(perf_sample_dict['sample'], ', ')+'}')\n\n"); 2154 2155 fprintf(ofp, "\t\tfor node in common_callchain:"); 2156 fprintf(ofp, "\n\t\t\tif 'sym' in node:"); 2157 fprintf(ofp, "\n\t\t\t\tprint(\"\t[%%x] %%s%%s%%s%%s\" %% ("); 2158 fprintf(ofp, "\n\t\t\t\t\tnode['ip'], node['sym']['name'],"); 2159 fprintf(ofp, "\n\t\t\t\t\t\"+0x{:x}\".format(node['sym_off']) if 'sym_off' in node else \"\","); 2160 fprintf(ofp, "\n\t\t\t\t\t\" ({})\".format(node['dso']) if 'dso' in node else \"\","); 2161 fprintf(ofp, "\n\t\t\t\t\t\" \" + node['sym_srcline'] if 'sym_srcline' in node else \"\"))"); 2162 fprintf(ofp, "\n\t\t\telse:"); 2163 fprintf(ofp, "\n\t\t\t\tprint(\"\t[%%x]\" %% (node['ip']))\n\n"); 2164 fprintf(ofp, "\t\tprint()\n\n"); 2165 2166 } 2167 2168 fprintf(ofp, "def trace_unhandled(event_name, context, " 2169 "event_fields_dict, perf_sample_dict):\n"); 2170 2171 fprintf(ofp, "\t\tprint(get_dict_as_string(event_fields_dict))\n"); 2172 fprintf(ofp, "\t\tprint('Sample: {'+" 2173 "get_dict_as_string(perf_sample_dict['sample'], ', ')+'}')\n\n"); 2174 2175 fprintf(ofp, "def print_header(" 2176 "event_name, cpu, secs, nsecs, pid, comm):\n" 2177 "\tprint(\"%%-20s %%5u %%05u.%%09u %%8u %%-20s \" %% \\\n\t" 2178 "(event_name, cpu, secs, nsecs, pid, comm), end=\"\")\n\n"); 2179 2180 fprintf(ofp, "def get_dict_as_string(a_dict, delimiter=' '):\n" 2181 "\treturn delimiter.join" 2182 "(['%%s=%%s'%%(k,str(v))for k,v in sorted(a_dict.items())])\n"); 2183 2184 fclose(ofp); 2185 2186 fprintf(stderr, "generated Python script: %s\n", fname); 2187 2188 return 0; 2189 } 2190 #else 2191 static int python_generate_script(struct tep_handle *pevent __maybe_unused, 2192 const char *outfile __maybe_unused) 2193 { 2194 fprintf(stderr, "Generating Python perf-script is not supported." 2195 " Install libtraceevent and rebuild perf to enable it.\n" 2196 "For example:\n # apt install libtraceevent-dev (ubuntu)" 2197 "\n # yum install libtraceevent-devel (Fedora)" 2198 "\n etc.\n"); 2199 return -1; 2200 } 2201 #endif 2202 2203 struct scripting_ops python_scripting_ops = { 2204 .name = "Python", 2205 .dirname = "python", 2206 .start_script = python_start_script, 2207 .flush_script = python_flush_script, 2208 .stop_script = python_stop_script, 2209 .process_event = python_process_event, 2210 .process_switch = python_process_switch, 2211 .process_auxtrace_error = python_process_auxtrace_error, 2212 .process_stat = python_process_stat, 2213 .process_stat_interval = python_process_stat_interval, 2214 .process_throttle = python_process_throttle, 2215 .generate_script = python_generate_script, 2216 }; 2217