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