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