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