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