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 pydict_set_item_string_decref(dict_sample, "phys_addr", 503 PyLong_FromUnsignedLongLong(sample->phys_addr)); 504 set_sample_read_in_dict(dict_sample, sample, evsel); 505 pydict_set_item_string_decref(dict, "sample", dict_sample); 506 507 pydict_set_item_string_decref(dict, "raw_buf", PyString_FromStringAndSize( 508 (const char *)sample->raw_data, sample->raw_size)); 509 pydict_set_item_string_decref(dict, "comm", 510 PyString_FromString(thread__comm_str(al->thread))); 511 if (al->map) { 512 pydict_set_item_string_decref(dict, "dso", 513 PyString_FromString(al->map->dso->name)); 514 } 515 if (al->sym) { 516 pydict_set_item_string_decref(dict, "symbol", 517 PyString_FromString(al->sym->name)); 518 } 519 520 pydict_set_item_string_decref(dict, "callchain", callchain); 521 522 return dict; 523 } 524 525 static void python_process_tracepoint(struct perf_sample *sample, 526 struct perf_evsel *evsel, 527 struct addr_location *al) 528 { 529 struct event_format *event = evsel->tp_format; 530 PyObject *handler, *context, *t, *obj = NULL, *callchain; 531 PyObject *dict = NULL, *all_entries_dict = NULL; 532 static char handler_name[256]; 533 struct format_field *field; 534 unsigned long s, ns; 535 unsigned n = 0; 536 int pid; 537 int cpu = sample->cpu; 538 void *data = sample->raw_data; 539 unsigned long long nsecs = sample->time; 540 const char *comm = thread__comm_str(al->thread); 541 const char *default_handler_name = "trace_unhandled"; 542 543 if (!event) { 544 snprintf(handler_name, sizeof(handler_name), 545 "ug! no event found for type %" PRIu64, (u64)evsel->attr.config); 546 Py_FatalError(handler_name); 547 } 548 549 pid = raw_field_value(event, "common_pid", data); 550 551 sprintf(handler_name, "%s__%s", event->system, event->name); 552 553 if (!test_and_set_bit(event->id, events_defined)) 554 define_event_symbols(event, handler_name, event->print_fmt.args); 555 556 handler = get_handler(handler_name); 557 if (!handler) { 558 handler = get_handler(default_handler_name); 559 if (!handler) 560 return; 561 dict = PyDict_New(); 562 if (!dict) 563 Py_FatalError("couldn't create Python dict"); 564 } 565 566 t = PyTuple_New(MAX_FIELDS); 567 if (!t) 568 Py_FatalError("couldn't create Python tuple"); 569 570 571 s = nsecs / NSEC_PER_SEC; 572 ns = nsecs - s * NSEC_PER_SEC; 573 574 scripting_context->event_data = data; 575 scripting_context->pevent = evsel->tp_format->pevent; 576 577 context = PyCObject_FromVoidPtr(scripting_context, NULL); 578 579 PyTuple_SetItem(t, n++, PyString_FromString(handler_name)); 580 PyTuple_SetItem(t, n++, context); 581 582 /* ip unwinding */ 583 callchain = python_process_callchain(sample, evsel, al); 584 /* Need an additional reference for the perf_sample dict */ 585 Py_INCREF(callchain); 586 587 if (!dict) { 588 PyTuple_SetItem(t, n++, PyInt_FromLong(cpu)); 589 PyTuple_SetItem(t, n++, PyInt_FromLong(s)); 590 PyTuple_SetItem(t, n++, PyInt_FromLong(ns)); 591 PyTuple_SetItem(t, n++, PyInt_FromLong(pid)); 592 PyTuple_SetItem(t, n++, PyString_FromString(comm)); 593 PyTuple_SetItem(t, n++, callchain); 594 } else { 595 pydict_set_item_string_decref(dict, "common_cpu", PyInt_FromLong(cpu)); 596 pydict_set_item_string_decref(dict, "common_s", PyInt_FromLong(s)); 597 pydict_set_item_string_decref(dict, "common_ns", PyInt_FromLong(ns)); 598 pydict_set_item_string_decref(dict, "common_pid", PyInt_FromLong(pid)); 599 pydict_set_item_string_decref(dict, "common_comm", PyString_FromString(comm)); 600 pydict_set_item_string_decref(dict, "common_callchain", callchain); 601 } 602 for (field = event->format.fields; field; field = field->next) { 603 unsigned int offset, len; 604 unsigned long long val; 605 606 if (field->flags & FIELD_IS_ARRAY) { 607 offset = field->offset; 608 len = field->size; 609 if (field->flags & FIELD_IS_DYNAMIC) { 610 val = pevent_read_number(scripting_context->pevent, 611 data + offset, len); 612 offset = val; 613 len = offset >> 16; 614 offset &= 0xffff; 615 } 616 if (field->flags & FIELD_IS_STRING && 617 is_printable_array(data + offset, len)) { 618 obj = PyString_FromString((char *) data + offset); 619 } else { 620 obj = PyByteArray_FromStringAndSize((const char *) data + offset, len); 621 field->flags &= ~FIELD_IS_STRING; 622 } 623 } else { /* FIELD_IS_NUMERIC */ 624 obj = get_field_numeric_entry(event, field, data); 625 } 626 if (!dict) 627 PyTuple_SetItem(t, n++, obj); 628 else 629 pydict_set_item_string_decref(dict, field->name, obj); 630 631 } 632 633 if (dict) 634 PyTuple_SetItem(t, n++, dict); 635 636 if (get_argument_count(handler) == (int) n + 1) { 637 all_entries_dict = get_perf_sample_dict(sample, evsel, al, 638 callchain); 639 PyTuple_SetItem(t, n++, all_entries_dict); 640 } else { 641 Py_DECREF(callchain); 642 } 643 644 if (_PyTuple_Resize(&t, n) == -1) 645 Py_FatalError("error resizing Python tuple"); 646 647 if (!dict) { 648 call_object(handler, t, handler_name); 649 } else { 650 call_object(handler, t, default_handler_name); 651 Py_DECREF(dict); 652 } 653 654 Py_XDECREF(all_entries_dict); 655 Py_DECREF(t); 656 } 657 658 static PyObject *tuple_new(unsigned int sz) 659 { 660 PyObject *t; 661 662 t = PyTuple_New(sz); 663 if (!t) 664 Py_FatalError("couldn't create Python tuple"); 665 return t; 666 } 667 668 static int tuple_set_u64(PyObject *t, unsigned int pos, u64 val) 669 { 670 #if BITS_PER_LONG == 64 671 return PyTuple_SetItem(t, pos, PyInt_FromLong(val)); 672 #endif 673 #if BITS_PER_LONG == 32 674 return PyTuple_SetItem(t, pos, PyLong_FromLongLong(val)); 675 #endif 676 } 677 678 static int tuple_set_s32(PyObject *t, unsigned int pos, s32 val) 679 { 680 return PyTuple_SetItem(t, pos, PyInt_FromLong(val)); 681 } 682 683 static int tuple_set_string(PyObject *t, unsigned int pos, const char *s) 684 { 685 return PyTuple_SetItem(t, pos, PyString_FromString(s)); 686 } 687 688 static int python_export_evsel(struct db_export *dbe, struct perf_evsel *evsel) 689 { 690 struct tables *tables = container_of(dbe, struct tables, dbe); 691 PyObject *t; 692 693 t = tuple_new(2); 694 695 tuple_set_u64(t, 0, evsel->db_id); 696 tuple_set_string(t, 1, perf_evsel__name(evsel)); 697 698 call_object(tables->evsel_handler, t, "evsel_table"); 699 700 Py_DECREF(t); 701 702 return 0; 703 } 704 705 static int python_export_machine(struct db_export *dbe, 706 struct machine *machine) 707 { 708 struct tables *tables = container_of(dbe, struct tables, dbe); 709 PyObject *t; 710 711 t = tuple_new(3); 712 713 tuple_set_u64(t, 0, machine->db_id); 714 tuple_set_s32(t, 1, machine->pid); 715 tuple_set_string(t, 2, machine->root_dir ? machine->root_dir : ""); 716 717 call_object(tables->machine_handler, t, "machine_table"); 718 719 Py_DECREF(t); 720 721 return 0; 722 } 723 724 static int python_export_thread(struct db_export *dbe, struct thread *thread, 725 u64 main_thread_db_id, struct machine *machine) 726 { 727 struct tables *tables = container_of(dbe, struct tables, dbe); 728 PyObject *t; 729 730 t = tuple_new(5); 731 732 tuple_set_u64(t, 0, thread->db_id); 733 tuple_set_u64(t, 1, machine->db_id); 734 tuple_set_u64(t, 2, main_thread_db_id); 735 tuple_set_s32(t, 3, thread->pid_); 736 tuple_set_s32(t, 4, thread->tid); 737 738 call_object(tables->thread_handler, t, "thread_table"); 739 740 Py_DECREF(t); 741 742 return 0; 743 } 744 745 static int python_export_comm(struct db_export *dbe, struct comm *comm) 746 { 747 struct tables *tables = container_of(dbe, struct tables, dbe); 748 PyObject *t; 749 750 t = tuple_new(2); 751 752 tuple_set_u64(t, 0, comm->db_id); 753 tuple_set_string(t, 1, comm__str(comm)); 754 755 call_object(tables->comm_handler, t, "comm_table"); 756 757 Py_DECREF(t); 758 759 return 0; 760 } 761 762 static int python_export_comm_thread(struct db_export *dbe, u64 db_id, 763 struct comm *comm, struct thread *thread) 764 { 765 struct tables *tables = container_of(dbe, struct tables, dbe); 766 PyObject *t; 767 768 t = tuple_new(3); 769 770 tuple_set_u64(t, 0, db_id); 771 tuple_set_u64(t, 1, comm->db_id); 772 tuple_set_u64(t, 2, thread->db_id); 773 774 call_object(tables->comm_thread_handler, t, "comm_thread_table"); 775 776 Py_DECREF(t); 777 778 return 0; 779 } 780 781 static int python_export_dso(struct db_export *dbe, struct dso *dso, 782 struct machine *machine) 783 { 784 struct tables *tables = container_of(dbe, struct tables, dbe); 785 char sbuild_id[SBUILD_ID_SIZE]; 786 PyObject *t; 787 788 build_id__sprintf(dso->build_id, sizeof(dso->build_id), sbuild_id); 789 790 t = tuple_new(5); 791 792 tuple_set_u64(t, 0, dso->db_id); 793 tuple_set_u64(t, 1, machine->db_id); 794 tuple_set_string(t, 2, dso->short_name); 795 tuple_set_string(t, 3, dso->long_name); 796 tuple_set_string(t, 4, sbuild_id); 797 798 call_object(tables->dso_handler, t, "dso_table"); 799 800 Py_DECREF(t); 801 802 return 0; 803 } 804 805 static int python_export_symbol(struct db_export *dbe, struct symbol *sym, 806 struct dso *dso) 807 { 808 struct tables *tables = container_of(dbe, struct tables, dbe); 809 u64 *sym_db_id = symbol__priv(sym); 810 PyObject *t; 811 812 t = tuple_new(6); 813 814 tuple_set_u64(t, 0, *sym_db_id); 815 tuple_set_u64(t, 1, dso->db_id); 816 tuple_set_u64(t, 2, sym->start); 817 tuple_set_u64(t, 3, sym->end); 818 tuple_set_s32(t, 4, sym->binding); 819 tuple_set_string(t, 5, sym->name); 820 821 call_object(tables->symbol_handler, t, "symbol_table"); 822 823 Py_DECREF(t); 824 825 return 0; 826 } 827 828 static int python_export_branch_type(struct db_export *dbe, u32 branch_type, 829 const char *name) 830 { 831 struct tables *tables = container_of(dbe, struct tables, dbe); 832 PyObject *t; 833 834 t = tuple_new(2); 835 836 tuple_set_s32(t, 0, branch_type); 837 tuple_set_string(t, 1, name); 838 839 call_object(tables->branch_type_handler, t, "branch_type_table"); 840 841 Py_DECREF(t); 842 843 return 0; 844 } 845 846 static int python_export_sample(struct db_export *dbe, 847 struct export_sample *es) 848 { 849 struct tables *tables = container_of(dbe, struct tables, dbe); 850 PyObject *t; 851 852 t = tuple_new(22); 853 854 tuple_set_u64(t, 0, es->db_id); 855 tuple_set_u64(t, 1, es->evsel->db_id); 856 tuple_set_u64(t, 2, es->al->machine->db_id); 857 tuple_set_u64(t, 3, es->al->thread->db_id); 858 tuple_set_u64(t, 4, es->comm_db_id); 859 tuple_set_u64(t, 5, es->dso_db_id); 860 tuple_set_u64(t, 6, es->sym_db_id); 861 tuple_set_u64(t, 7, es->offset); 862 tuple_set_u64(t, 8, es->sample->ip); 863 tuple_set_u64(t, 9, es->sample->time); 864 tuple_set_s32(t, 10, es->sample->cpu); 865 tuple_set_u64(t, 11, es->addr_dso_db_id); 866 tuple_set_u64(t, 12, es->addr_sym_db_id); 867 tuple_set_u64(t, 13, es->addr_offset); 868 tuple_set_u64(t, 14, es->sample->addr); 869 tuple_set_u64(t, 15, es->sample->period); 870 tuple_set_u64(t, 16, es->sample->weight); 871 tuple_set_u64(t, 17, es->sample->transaction); 872 tuple_set_u64(t, 18, es->sample->data_src); 873 tuple_set_s32(t, 19, es->sample->flags & PERF_BRANCH_MASK); 874 tuple_set_s32(t, 20, !!(es->sample->flags & PERF_IP_FLAG_IN_TX)); 875 tuple_set_u64(t, 21, es->call_path_id); 876 877 call_object(tables->sample_handler, t, "sample_table"); 878 879 Py_DECREF(t); 880 881 return 0; 882 } 883 884 static int python_export_call_path(struct db_export *dbe, struct call_path *cp) 885 { 886 struct tables *tables = container_of(dbe, struct tables, dbe); 887 PyObject *t; 888 u64 parent_db_id, sym_db_id; 889 890 parent_db_id = cp->parent ? cp->parent->db_id : 0; 891 sym_db_id = cp->sym ? *(u64 *)symbol__priv(cp->sym) : 0; 892 893 t = tuple_new(4); 894 895 tuple_set_u64(t, 0, cp->db_id); 896 tuple_set_u64(t, 1, parent_db_id); 897 tuple_set_u64(t, 2, sym_db_id); 898 tuple_set_u64(t, 3, cp->ip); 899 900 call_object(tables->call_path_handler, t, "call_path_table"); 901 902 Py_DECREF(t); 903 904 return 0; 905 } 906 907 static int python_export_call_return(struct db_export *dbe, 908 struct call_return *cr) 909 { 910 struct tables *tables = container_of(dbe, struct tables, dbe); 911 u64 comm_db_id = cr->comm ? cr->comm->db_id : 0; 912 PyObject *t; 913 914 t = tuple_new(11); 915 916 tuple_set_u64(t, 0, cr->db_id); 917 tuple_set_u64(t, 1, cr->thread->db_id); 918 tuple_set_u64(t, 2, comm_db_id); 919 tuple_set_u64(t, 3, cr->cp->db_id); 920 tuple_set_u64(t, 4, cr->call_time); 921 tuple_set_u64(t, 5, cr->return_time); 922 tuple_set_u64(t, 6, cr->branch_count); 923 tuple_set_u64(t, 7, cr->call_ref); 924 tuple_set_u64(t, 8, cr->return_ref); 925 tuple_set_u64(t, 9, cr->cp->parent->db_id); 926 tuple_set_s32(t, 10, cr->flags); 927 928 call_object(tables->call_return_handler, t, "call_return_table"); 929 930 Py_DECREF(t); 931 932 return 0; 933 } 934 935 static int python_process_call_return(struct call_return *cr, void *data) 936 { 937 struct db_export *dbe = data; 938 939 return db_export__call_return(dbe, cr); 940 } 941 942 static void python_process_general_event(struct perf_sample *sample, 943 struct perf_evsel *evsel, 944 struct addr_location *al) 945 { 946 PyObject *handler, *t, *dict, *callchain; 947 static char handler_name[64]; 948 unsigned n = 0; 949 950 snprintf(handler_name, sizeof(handler_name), "%s", "process_event"); 951 952 handler = get_handler(handler_name); 953 if (!handler) 954 return; 955 956 /* 957 * Use the MAX_FIELDS to make the function expandable, though 958 * currently there is only one item for the tuple. 959 */ 960 t = PyTuple_New(MAX_FIELDS); 961 if (!t) 962 Py_FatalError("couldn't create Python tuple"); 963 964 /* ip unwinding */ 965 callchain = python_process_callchain(sample, evsel, al); 966 dict = get_perf_sample_dict(sample, evsel, al, callchain); 967 968 PyTuple_SetItem(t, n++, dict); 969 if (_PyTuple_Resize(&t, n) == -1) 970 Py_FatalError("error resizing Python tuple"); 971 972 call_object(handler, t, handler_name); 973 974 Py_DECREF(dict); 975 Py_DECREF(t); 976 } 977 978 static void python_process_event(union perf_event *event, 979 struct perf_sample *sample, 980 struct perf_evsel *evsel, 981 struct addr_location *al) 982 { 983 struct tables *tables = &tables_global; 984 985 switch (evsel->attr.type) { 986 case PERF_TYPE_TRACEPOINT: 987 python_process_tracepoint(sample, evsel, al); 988 break; 989 /* Reserve for future process_hw/sw/raw APIs */ 990 default: 991 if (tables->db_export_mode) 992 db_export__sample(&tables->dbe, event, sample, evsel, al); 993 else 994 python_process_general_event(sample, evsel, al); 995 } 996 } 997 998 static void get_handler_name(char *str, size_t size, 999 struct perf_evsel *evsel) 1000 { 1001 char *p = str; 1002 1003 scnprintf(str, size, "stat__%s", perf_evsel__name(evsel)); 1004 1005 while ((p = strchr(p, ':'))) { 1006 *p = '_'; 1007 p++; 1008 } 1009 } 1010 1011 static void 1012 process_stat(struct perf_evsel *counter, int cpu, int thread, u64 tstamp, 1013 struct perf_counts_values *count) 1014 { 1015 PyObject *handler, *t; 1016 static char handler_name[256]; 1017 int n = 0; 1018 1019 t = PyTuple_New(MAX_FIELDS); 1020 if (!t) 1021 Py_FatalError("couldn't create Python tuple"); 1022 1023 get_handler_name(handler_name, sizeof(handler_name), 1024 counter); 1025 1026 handler = get_handler(handler_name); 1027 if (!handler) { 1028 pr_debug("can't find python handler %s\n", handler_name); 1029 return; 1030 } 1031 1032 PyTuple_SetItem(t, n++, PyInt_FromLong(cpu)); 1033 PyTuple_SetItem(t, n++, PyInt_FromLong(thread)); 1034 1035 tuple_set_u64(t, n++, tstamp); 1036 tuple_set_u64(t, n++, count->val); 1037 tuple_set_u64(t, n++, count->ena); 1038 tuple_set_u64(t, n++, count->run); 1039 1040 if (_PyTuple_Resize(&t, n) == -1) 1041 Py_FatalError("error resizing Python tuple"); 1042 1043 call_object(handler, t, handler_name); 1044 1045 Py_DECREF(t); 1046 } 1047 1048 static void python_process_stat(struct perf_stat_config *config, 1049 struct perf_evsel *counter, u64 tstamp) 1050 { 1051 struct thread_map *threads = counter->threads; 1052 struct cpu_map *cpus = counter->cpus; 1053 int cpu, thread; 1054 1055 if (config->aggr_mode == AGGR_GLOBAL) { 1056 process_stat(counter, -1, -1, tstamp, 1057 &counter->counts->aggr); 1058 return; 1059 } 1060 1061 for (thread = 0; thread < threads->nr; thread++) { 1062 for (cpu = 0; cpu < cpus->nr; cpu++) { 1063 process_stat(counter, cpus->map[cpu], 1064 thread_map__pid(threads, thread), tstamp, 1065 perf_counts(counter->counts, cpu, thread)); 1066 } 1067 } 1068 } 1069 1070 static void python_process_stat_interval(u64 tstamp) 1071 { 1072 PyObject *handler, *t; 1073 static const char handler_name[] = "stat__interval"; 1074 int n = 0; 1075 1076 t = PyTuple_New(MAX_FIELDS); 1077 if (!t) 1078 Py_FatalError("couldn't create Python tuple"); 1079 1080 handler = get_handler(handler_name); 1081 if (!handler) { 1082 pr_debug("can't find python handler %s\n", handler_name); 1083 return; 1084 } 1085 1086 tuple_set_u64(t, n++, tstamp); 1087 1088 if (_PyTuple_Resize(&t, n) == -1) 1089 Py_FatalError("error resizing Python tuple"); 1090 1091 call_object(handler, t, handler_name); 1092 1093 Py_DECREF(t); 1094 } 1095 1096 static int run_start_sub(void) 1097 { 1098 main_module = PyImport_AddModule("__main__"); 1099 if (main_module == NULL) 1100 return -1; 1101 Py_INCREF(main_module); 1102 1103 main_dict = PyModule_GetDict(main_module); 1104 if (main_dict == NULL) 1105 goto error; 1106 Py_INCREF(main_dict); 1107 1108 try_call_object("trace_begin", NULL); 1109 1110 return 0; 1111 1112 error: 1113 Py_XDECREF(main_dict); 1114 Py_XDECREF(main_module); 1115 return -1; 1116 } 1117 1118 #define SET_TABLE_HANDLER_(name, handler_name, table_name) do { \ 1119 tables->handler_name = get_handler(#table_name); \ 1120 if (tables->handler_name) \ 1121 tables->dbe.export_ ## name = python_export_ ## name; \ 1122 } while (0) 1123 1124 #define SET_TABLE_HANDLER(name) \ 1125 SET_TABLE_HANDLER_(name, name ## _handler, name ## _table) 1126 1127 static void set_table_handlers(struct tables *tables) 1128 { 1129 const char *perf_db_export_mode = "perf_db_export_mode"; 1130 const char *perf_db_export_calls = "perf_db_export_calls"; 1131 const char *perf_db_export_callchains = "perf_db_export_callchains"; 1132 PyObject *db_export_mode, *db_export_calls, *db_export_callchains; 1133 bool export_calls = false; 1134 bool export_callchains = false; 1135 int ret; 1136 1137 memset(tables, 0, sizeof(struct tables)); 1138 if (db_export__init(&tables->dbe)) 1139 Py_FatalError("failed to initialize export"); 1140 1141 db_export_mode = PyDict_GetItemString(main_dict, perf_db_export_mode); 1142 if (!db_export_mode) 1143 return; 1144 1145 ret = PyObject_IsTrue(db_export_mode); 1146 if (ret == -1) 1147 handler_call_die(perf_db_export_mode); 1148 if (!ret) 1149 return; 1150 1151 /* handle export calls */ 1152 tables->dbe.crp = NULL; 1153 db_export_calls = PyDict_GetItemString(main_dict, perf_db_export_calls); 1154 if (db_export_calls) { 1155 ret = PyObject_IsTrue(db_export_calls); 1156 if (ret == -1) 1157 handler_call_die(perf_db_export_calls); 1158 export_calls = !!ret; 1159 } 1160 1161 if (export_calls) { 1162 tables->dbe.crp = 1163 call_return_processor__new(python_process_call_return, 1164 &tables->dbe); 1165 if (!tables->dbe.crp) 1166 Py_FatalError("failed to create calls processor"); 1167 } 1168 1169 /* handle export callchains */ 1170 tables->dbe.cpr = NULL; 1171 db_export_callchains = PyDict_GetItemString(main_dict, 1172 perf_db_export_callchains); 1173 if (db_export_callchains) { 1174 ret = PyObject_IsTrue(db_export_callchains); 1175 if (ret == -1) 1176 handler_call_die(perf_db_export_callchains); 1177 export_callchains = !!ret; 1178 } 1179 1180 if (export_callchains) { 1181 /* 1182 * Attempt to use the call path root from the call return 1183 * processor, if the call return processor is in use. Otherwise, 1184 * we allocate a new call path root. This prevents exporting 1185 * duplicate call path ids when both are in use simultaniously. 1186 */ 1187 if (tables->dbe.crp) 1188 tables->dbe.cpr = tables->dbe.crp->cpr; 1189 else 1190 tables->dbe.cpr = call_path_root__new(); 1191 1192 if (!tables->dbe.cpr) 1193 Py_FatalError("failed to create call path root"); 1194 } 1195 1196 tables->db_export_mode = true; 1197 /* 1198 * Reserve per symbol space for symbol->db_id via symbol__priv() 1199 */ 1200 symbol_conf.priv_size = sizeof(u64); 1201 1202 SET_TABLE_HANDLER(evsel); 1203 SET_TABLE_HANDLER(machine); 1204 SET_TABLE_HANDLER(thread); 1205 SET_TABLE_HANDLER(comm); 1206 SET_TABLE_HANDLER(comm_thread); 1207 SET_TABLE_HANDLER(dso); 1208 SET_TABLE_HANDLER(symbol); 1209 SET_TABLE_HANDLER(branch_type); 1210 SET_TABLE_HANDLER(sample); 1211 SET_TABLE_HANDLER(call_path); 1212 SET_TABLE_HANDLER(call_return); 1213 } 1214 1215 /* 1216 * Start trace script 1217 */ 1218 static int python_start_script(const char *script, int argc, const char **argv) 1219 { 1220 struct tables *tables = &tables_global; 1221 const char **command_line; 1222 char buf[PATH_MAX]; 1223 int i, err = 0; 1224 FILE *fp; 1225 1226 command_line = malloc((argc + 1) * sizeof(const char *)); 1227 command_line[0] = script; 1228 for (i = 1; i < argc + 1; i++) 1229 command_line[i] = argv[i - 1]; 1230 1231 Py_Initialize(); 1232 1233 initperf_trace_context(); 1234 1235 PySys_SetArgv(argc + 1, (char **)command_line); 1236 1237 fp = fopen(script, "r"); 1238 if (!fp) { 1239 sprintf(buf, "Can't open python script \"%s\"", script); 1240 perror(buf); 1241 err = -1; 1242 goto error; 1243 } 1244 1245 err = PyRun_SimpleFile(fp, script); 1246 if (err) { 1247 fprintf(stderr, "Error running python script %s\n", script); 1248 goto error; 1249 } 1250 1251 err = run_start_sub(); 1252 if (err) { 1253 fprintf(stderr, "Error starting python script %s\n", script); 1254 goto error; 1255 } 1256 1257 set_table_handlers(tables); 1258 1259 if (tables->db_export_mode) { 1260 err = db_export__branch_types(&tables->dbe); 1261 if (err) 1262 goto error; 1263 } 1264 1265 free(command_line); 1266 1267 return err; 1268 error: 1269 Py_Finalize(); 1270 free(command_line); 1271 1272 return err; 1273 } 1274 1275 static int python_flush_script(void) 1276 { 1277 struct tables *tables = &tables_global; 1278 1279 return db_export__flush(&tables->dbe); 1280 } 1281 1282 /* 1283 * Stop trace script 1284 */ 1285 static int python_stop_script(void) 1286 { 1287 struct tables *tables = &tables_global; 1288 1289 try_call_object("trace_end", NULL); 1290 1291 db_export__exit(&tables->dbe); 1292 1293 Py_XDECREF(main_dict); 1294 Py_XDECREF(main_module); 1295 Py_Finalize(); 1296 1297 return 0; 1298 } 1299 1300 static int python_generate_script(struct pevent *pevent, const char *outfile) 1301 { 1302 struct event_format *event = NULL; 1303 struct format_field *f; 1304 char fname[PATH_MAX]; 1305 int not_first, count; 1306 FILE *ofp; 1307 1308 sprintf(fname, "%s.py", outfile); 1309 ofp = fopen(fname, "w"); 1310 if (ofp == NULL) { 1311 fprintf(stderr, "couldn't open %s\n", fname); 1312 return -1; 1313 } 1314 fprintf(ofp, "# perf script event handlers, " 1315 "generated by perf script -g python\n"); 1316 1317 fprintf(ofp, "# Licensed under the terms of the GNU GPL" 1318 " License version 2\n\n"); 1319 1320 fprintf(ofp, "# The common_* event handler fields are the most useful " 1321 "fields common to\n"); 1322 1323 fprintf(ofp, "# all events. They don't necessarily correspond to " 1324 "the 'common_*' fields\n"); 1325 1326 fprintf(ofp, "# in the format files. Those fields not available as " 1327 "handler params can\n"); 1328 1329 fprintf(ofp, "# be retrieved using Python functions of the form " 1330 "common_*(context).\n"); 1331 1332 fprintf(ofp, "# See the perf-script-python Documentation for the list " 1333 "of available functions.\n\n"); 1334 1335 fprintf(ofp, "import os\n"); 1336 fprintf(ofp, "import sys\n\n"); 1337 1338 fprintf(ofp, "sys.path.append(os.environ['PERF_EXEC_PATH'] + \\\n"); 1339 fprintf(ofp, "\t'/scripts/python/Perf-Trace-Util/lib/Perf/Trace')\n"); 1340 fprintf(ofp, "\nfrom perf_trace_context import *\n"); 1341 fprintf(ofp, "from Core import *\n\n\n"); 1342 1343 fprintf(ofp, "def trace_begin():\n"); 1344 fprintf(ofp, "\tprint \"in trace_begin\"\n\n"); 1345 1346 fprintf(ofp, "def trace_end():\n"); 1347 fprintf(ofp, "\tprint \"in trace_end\"\n\n"); 1348 1349 while ((event = trace_find_next_event(pevent, event))) { 1350 fprintf(ofp, "def %s__%s(", event->system, event->name); 1351 fprintf(ofp, "event_name, "); 1352 fprintf(ofp, "context, "); 1353 fprintf(ofp, "common_cpu,\n"); 1354 fprintf(ofp, "\tcommon_secs, "); 1355 fprintf(ofp, "common_nsecs, "); 1356 fprintf(ofp, "common_pid, "); 1357 fprintf(ofp, "common_comm,\n\t"); 1358 fprintf(ofp, "common_callchain, "); 1359 1360 not_first = 0; 1361 count = 0; 1362 1363 for (f = event->format.fields; f; f = f->next) { 1364 if (not_first++) 1365 fprintf(ofp, ", "); 1366 if (++count % 5 == 0) 1367 fprintf(ofp, "\n\t"); 1368 1369 fprintf(ofp, "%s", f->name); 1370 } 1371 if (not_first++) 1372 fprintf(ofp, ", "); 1373 if (++count % 5 == 0) 1374 fprintf(ofp, "\n\t\t"); 1375 fprintf(ofp, "perf_sample_dict"); 1376 1377 fprintf(ofp, "):\n"); 1378 1379 fprintf(ofp, "\t\tprint_header(event_name, common_cpu, " 1380 "common_secs, common_nsecs,\n\t\t\t" 1381 "common_pid, common_comm)\n\n"); 1382 1383 fprintf(ofp, "\t\tprint \""); 1384 1385 not_first = 0; 1386 count = 0; 1387 1388 for (f = event->format.fields; f; f = f->next) { 1389 if (not_first++) 1390 fprintf(ofp, ", "); 1391 if (count && count % 3 == 0) { 1392 fprintf(ofp, "\" \\\n\t\t\""); 1393 } 1394 count++; 1395 1396 fprintf(ofp, "%s=", f->name); 1397 if (f->flags & FIELD_IS_STRING || 1398 f->flags & FIELD_IS_FLAG || 1399 f->flags & FIELD_IS_ARRAY || 1400 f->flags & FIELD_IS_SYMBOLIC) 1401 fprintf(ofp, "%%s"); 1402 else if (f->flags & FIELD_IS_SIGNED) 1403 fprintf(ofp, "%%d"); 1404 else 1405 fprintf(ofp, "%%u"); 1406 } 1407 1408 fprintf(ofp, "\" %% \\\n\t\t("); 1409 1410 not_first = 0; 1411 count = 0; 1412 1413 for (f = event->format.fields; f; f = f->next) { 1414 if (not_first++) 1415 fprintf(ofp, ", "); 1416 1417 if (++count % 5 == 0) 1418 fprintf(ofp, "\n\t\t"); 1419 1420 if (f->flags & FIELD_IS_FLAG) { 1421 if ((count - 1) % 5 != 0) { 1422 fprintf(ofp, "\n\t\t"); 1423 count = 4; 1424 } 1425 fprintf(ofp, "flag_str(\""); 1426 fprintf(ofp, "%s__%s\", ", event->system, 1427 event->name); 1428 fprintf(ofp, "\"%s\", %s)", f->name, 1429 f->name); 1430 } else if (f->flags & FIELD_IS_SYMBOLIC) { 1431 if ((count - 1) % 5 != 0) { 1432 fprintf(ofp, "\n\t\t"); 1433 count = 4; 1434 } 1435 fprintf(ofp, "symbol_str(\""); 1436 fprintf(ofp, "%s__%s\", ", event->system, 1437 event->name); 1438 fprintf(ofp, "\"%s\", %s)", f->name, 1439 f->name); 1440 } else 1441 fprintf(ofp, "%s", f->name); 1442 } 1443 1444 fprintf(ofp, ")\n\n"); 1445 1446 fprintf(ofp, "\t\tprint 'Sample: {'+" 1447 "get_dict_as_string(perf_sample_dict['sample'], ', ')+'}'\n\n"); 1448 1449 fprintf(ofp, "\t\tfor node in common_callchain:"); 1450 fprintf(ofp, "\n\t\t\tif 'sym' in node:"); 1451 fprintf(ofp, "\n\t\t\t\tprint \"\\t[%%x] %%s\" %% (node['ip'], node['sym']['name'])"); 1452 fprintf(ofp, "\n\t\t\telse:"); 1453 fprintf(ofp, "\n\t\t\t\tprint \"\t[%%x]\" %% (node['ip'])\n\n"); 1454 fprintf(ofp, "\t\tprint \"\\n\"\n\n"); 1455 1456 } 1457 1458 fprintf(ofp, "def trace_unhandled(event_name, context, " 1459 "event_fields_dict, perf_sample_dict):\n"); 1460 1461 fprintf(ofp, "\t\tprint get_dict_as_string(event_fields_dict)\n"); 1462 fprintf(ofp, "\t\tprint 'Sample: {'+" 1463 "get_dict_as_string(perf_sample_dict['sample'], ', ')+'}'\n\n"); 1464 1465 fprintf(ofp, "def print_header(" 1466 "event_name, cpu, secs, nsecs, pid, comm):\n" 1467 "\tprint \"%%-20s %%5u %%05u.%%09u %%8u %%-20s \" %% \\\n\t" 1468 "(event_name, cpu, secs, nsecs, pid, comm),\n\n"); 1469 1470 fprintf(ofp, "def get_dict_as_string(a_dict, delimiter=' '):\n" 1471 "\treturn delimiter.join" 1472 "(['%%s=%%s'%%(k,str(v))for k,v in sorted(a_dict.items())])\n"); 1473 1474 fclose(ofp); 1475 1476 fprintf(stderr, "generated Python script: %s\n", fname); 1477 1478 return 0; 1479 } 1480 1481 struct scripting_ops python_scripting_ops = { 1482 .name = "Python", 1483 .start_script = python_start_script, 1484 .flush_script = python_flush_script, 1485 .stop_script = python_stop_script, 1486 .process_event = python_process_event, 1487 .process_stat = python_process_stat, 1488 .process_stat_interval = python_process_stat_interval, 1489 .generate_script = python_generate_script, 1490 }; 1491