blob: f863e96fb7bc611e87ca2cc85955a5c15024e5f5 [file] [log] [blame]
Tom Zanussi7e4b21b2010-01-27 02:27:57 -06001/*
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
Arnaldo Carvalho de Melofd20e812017-04-17 15:23:08 -030024#include <inttypes.h>
Tom Zanussi7e4b21b2010-01-27 02:27:57 -060025#include <stdio.h>
26#include <stdlib.h>
27#include <string.h>
Adrian Hunterdf919b42014-10-23 13:45:14 +030028#include <stdbool.h>
Tom Zanussi7e4b21b2010-01-27 02:27:57 -060029#include <errno.h>
Jiri Olsaadf5bcf2014-10-26 23:44:05 +010030#include <linux/bitmap.h>
Arnaldo Carvalho de Melo6c346642017-06-16 11:39:15 -030031#include <linux/compiler.h>
Arnaldo Carvalho de Melobd48c632016-08-05 15:40:30 -030032#include <linux/time64.h>
Tom Zanussi7e4b21b2010-01-27 02:27:57 -060033
34#include "../../perf.h"
Jiri Olsa84f5d362014-07-14 23:46:48 +020035#include "../debug.h"
Arnaldo Carvalho de Melo8f651ea2014-10-09 16:12:24 -030036#include "../callchain.h"
Arnaldo Carvalho de Melofcf65bf2012-08-07 09:58:03 -030037#include "../evsel.h"
Tom Zanussi7e4b21b2010-01-27 02:27:57 -060038#include "../util.h"
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -020039#include "../event.h"
40#include "../thread.h"
Adrian Hunterdf919b42014-10-23 13:45:14 +030041#include "../comm.h"
42#include "../machine.h"
43#include "../db-export.h"
Adrian Hunter6a703072014-10-30 16:09:47 +020044#include "../thread-stack.h"
Tom Zanussi7e4b21b2010-01-27 02:27:57 -060045#include "../trace-event.h"
Chris Phlipot451db122016-04-28 01:19:07 -070046#include "../call-path.h"
Jiri Olsaaef90262016-01-05 22:09:11 +010047#include "thread_map.h"
48#include "cpumap.h"
Arnaldo Carvalho de Melofea01392017-04-17 16:23:22 -030049#include "print_binary.h"
Jiri Olsaaef90262016-01-05 22:09:11 +010050#include "stat.h"
Tom Zanussi7e4b21b2010-01-27 02:27:57 -060051
Jaroslav Škarvada66dfdff2018-01-19 21:56:41 +010052#if PY_MAJOR_VERSION < 3
53#define _PyUnicode_FromString(arg) \
54 PyString_FromString(arg)
55#define _PyUnicode_FromStringAndSize(arg1, arg2) \
56 PyString_FromStringAndSize((arg1), (arg2))
57#define _PyBytes_FromStringAndSize(arg1, arg2) \
58 PyString_FromStringAndSize((arg1), (arg2))
59#define _PyLong_FromLong(arg) \
60 PyInt_FromLong(arg)
61#define _PyLong_AsLong(arg) \
62 PyInt_AsLong(arg)
63#define _PyCapsule_New(arg1, arg2, arg3) \
64 PyCObject_FromVoidPtr((arg1), (arg2))
65
Tom Zanussi7e4b21b2010-01-27 02:27:57 -060066PyMODINIT_FUNC initperf_trace_context(void);
Jaroslav Škarvada66dfdff2018-01-19 21:56:41 +010067#else
68#define _PyUnicode_FromString(arg) \
69 PyUnicode_FromString(arg)
70#define _PyUnicode_FromStringAndSize(arg1, arg2) \
71 PyUnicode_FromStringAndSize((arg1), (arg2))
72#define _PyBytes_FromStringAndSize(arg1, arg2) \
73 PyBytes_FromStringAndSize((arg1), (arg2))
74#define _PyLong_FromLong(arg) \
75 PyLong_FromLong(arg)
76#define _PyLong_AsLong(arg) \
77 PyLong_AsLong(arg)
78#define _PyCapsule_New(arg1, arg2, arg3) \
79 PyCapsule_New((arg1), (arg2), (arg3))
80
81PyMODINIT_FUNC PyInit_perf_trace_context(void);
82#endif
Tom Zanussi7e4b21b2010-01-27 02:27:57 -060083
Steven Rostedt (Red Hat)609a7402015-05-13 13:44:36 -040084#define TRACE_EVENT_TYPE_MAX \
Tom Zanussi7e4b21b2010-01-27 02:27:57 -060085 ((1 << (sizeof(unsigned short) * 8)) - 1)
86
Steven Rostedt (Red Hat)609a7402015-05-13 13:44:36 -040087static DECLARE_BITMAP(events_defined, TRACE_EVENT_TYPE_MAX);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -060088
89#define MAX_FIELDS 64
90#define N_COMMON_FIELDS 7
91
92extern struct scripting_context *scripting_context;
93
94static char *cur_field_name;
95static int zero_flag_atom;
96
97static PyObject *main_module, *main_dict;
98
Adrian Hunterdf919b42014-10-23 13:45:14 +030099struct tables {
100 struct db_export dbe;
101 PyObject *evsel_handler;
102 PyObject *machine_handler;
103 PyObject *thread_handler;
104 PyObject *comm_handler;
105 PyObject *comm_thread_handler;
106 PyObject *dso_handler;
107 PyObject *symbol_handler;
Adrian Hunterc29414f2014-10-30 16:09:44 +0200108 PyObject *branch_type_handler;
Adrian Hunterdf919b42014-10-23 13:45:14 +0300109 PyObject *sample_handler;
Adrian Hunter6a703072014-10-30 16:09:47 +0200110 PyObject *call_path_handler;
111 PyObject *call_return_handler;
Adrian Hunterdf919b42014-10-23 13:45:14 +0300112 bool db_export_mode;
113};
114
115static struct tables tables_global;
116
Arnaldo Carvalho de Melo6c346642017-06-16 11:39:15 -0300117static void handler_call_die(const char *handler_name) __noreturn;
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600118static void handler_call_die(const char *handler_name)
119{
120 PyErr_Print();
121 Py_FatalError("problem in Python trace event handler");
Joseph Schuchart05f832e2014-07-09 16:16:31 +0200122 // Py_FatalError does not return
123 // but we have to make the compiler happy
124 abort();
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600125}
126
Joseph Schuchartc0268e82013-10-24 10:10:51 -0300127/*
128 * Insert val into into the dictionary and decrement the reference counter.
Arnaldo Carvalho de Melo48000a12014-12-17 17:24:45 -0300129 * This is necessary for dictionaries since PyDict_SetItemString() does not
Joseph Schuchartc0268e82013-10-24 10:10:51 -0300130 * steal a reference, as opposed to PyTuple_SetItem().
131 */
132static void pydict_set_item_string_decref(PyObject *dict, const char *key, PyObject *val)
133{
134 PyDict_SetItemString(dict, key, val);
135 Py_DECREF(val);
136}
137
Adrian Huntera5563ed2014-07-31 09:01:01 +0300138static PyObject *get_handler(const char *handler_name)
139{
140 PyObject *handler;
141
142 handler = PyDict_GetItemString(main_dict, handler_name);
143 if (handler && !PyCallable_Check(handler))
144 return NULL;
145 return handler;
146}
147
Arun Kalyanasundaramf38d2812017-07-21 15:04:21 -0700148static int get_argument_count(PyObject *handler)
149{
150 int arg_count = 0;
151
152 /*
153 * The attribute for the code object is func_code in Python 2,
154 * whereas it is __code__ in Python 3.0+.
155 */
156 PyObject *code_obj = PyObject_GetAttrString(handler,
157 "func_code");
158 if (PyErr_Occurred()) {
159 PyErr_Clear();
160 code_obj = PyObject_GetAttrString(handler,
161 "__code__");
162 }
163 PyErr_Clear();
164 if (code_obj) {
165 PyObject *arg_count_obj = PyObject_GetAttrString(code_obj,
166 "co_argcount");
167 if (arg_count_obj) {
Jaroslav Škarvada66dfdff2018-01-19 21:56:41 +0100168 arg_count = (int) _PyLong_AsLong(arg_count_obj);
Arun Kalyanasundaramf38d2812017-07-21 15:04:21 -0700169 Py_DECREF(arg_count_obj);
170 }
171 Py_DECREF(code_obj);
172 }
173 return arg_count;
174}
175
Adrian Huntera5563ed2014-07-31 09:01:01 +0300176static void call_object(PyObject *handler, PyObject *args, const char *die_msg)
177{
178 PyObject *retval;
179
180 retval = PyObject_CallObject(handler, args);
181 if (retval == NULL)
182 handler_call_die(die_msg);
183 Py_DECREF(retval);
184}
185
186static void try_call_object(const char *handler_name, PyObject *args)
187{
188 PyObject *handler;
189
190 handler = get_handler(handler_name);
191 if (handler)
192 call_object(handler, args, handler_name);
193}
194
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600195static void define_value(enum print_arg_type field_type,
196 const char *ev_name,
197 const char *field_name,
198 const char *field_value,
199 const char *field_str)
200{
201 const char *handler_name = "define_flag_value";
Adrian Huntera5563ed2014-07-31 09:01:01 +0300202 PyObject *t;
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600203 unsigned long long value;
204 unsigned n = 0;
205
206 if (field_type == PRINT_SYMBOL)
207 handler_name = "define_symbolic_value";
208
Tom Zanussi44ad9cd2010-02-22 01:12:59 -0600209 t = PyTuple_New(4);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600210 if (!t)
211 Py_FatalError("couldn't create Python tuple");
212
213 value = eval_flag(field_value);
214
Jaroslav Škarvada66dfdff2018-01-19 21:56:41 +0100215 PyTuple_SetItem(t, n++, _PyUnicode_FromString(ev_name));
216 PyTuple_SetItem(t, n++, _PyUnicode_FromString(field_name));
217 PyTuple_SetItem(t, n++, _PyLong_FromLong(value));
218 PyTuple_SetItem(t, n++, _PyUnicode_FromString(field_str));
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600219
Adrian Huntera5563ed2014-07-31 09:01:01 +0300220 try_call_object(handler_name, t);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600221
222 Py_DECREF(t);
223}
224
225static void define_values(enum print_arg_type field_type,
226 struct print_flag_sym *field,
227 const char *ev_name,
228 const char *field_name)
229{
230 define_value(field_type, ev_name, field_name, field->value,
231 field->str);
232
233 if (field->next)
234 define_values(field_type, field->next, ev_name, field_name);
235}
236
237static void define_field(enum print_arg_type field_type,
238 const char *ev_name,
239 const char *field_name,
240 const char *delim)
241{
242 const char *handler_name = "define_flag_field";
Adrian Huntera5563ed2014-07-31 09:01:01 +0300243 PyObject *t;
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600244 unsigned n = 0;
245
246 if (field_type == PRINT_SYMBOL)
247 handler_name = "define_symbolic_field";
248
Tom Zanussi44ad9cd2010-02-22 01:12:59 -0600249 if (field_type == PRINT_FLAGS)
250 t = PyTuple_New(3);
251 else
252 t = PyTuple_New(2);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600253 if (!t)
254 Py_FatalError("couldn't create Python tuple");
255
Jaroslav Škarvada66dfdff2018-01-19 21:56:41 +0100256 PyTuple_SetItem(t, n++, _PyUnicode_FromString(ev_name));
257 PyTuple_SetItem(t, n++, _PyUnicode_FromString(field_name));
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600258 if (field_type == PRINT_FLAGS)
Jaroslav Škarvada66dfdff2018-01-19 21:56:41 +0100259 PyTuple_SetItem(t, n++, _PyUnicode_FromString(delim));
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600260
Adrian Huntera5563ed2014-07-31 09:01:01 +0300261 try_call_object(handler_name, t);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600262
263 Py_DECREF(t);
264}
265
Steven Rostedtaaf045f2012-04-06 00:47:56 +0200266static void define_event_symbols(struct event_format *event,
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600267 const char *ev_name,
268 struct print_arg *args)
269{
Taeung Song8579aca2016-02-26 00:12:59 +0900270 if (args == NULL)
271 return;
272
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600273 switch (args->type) {
274 case PRINT_NULL:
275 break;
276 case PRINT_ATOM:
277 define_value(PRINT_FLAGS, ev_name, cur_field_name, "0",
278 args->atom.atom);
279 zero_flag_atom = 0;
280 break;
281 case PRINT_FIELD:
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -0300282 free(cur_field_name);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600283 cur_field_name = strdup(args->field.name);
284 break;
285 case PRINT_FLAGS:
286 define_event_symbols(event, ev_name, args->flags.field);
287 define_field(PRINT_FLAGS, ev_name, cur_field_name,
288 args->flags.delim);
289 define_values(PRINT_FLAGS, args->flags.flags, ev_name,
290 cur_field_name);
291 break;
292 case PRINT_SYMBOL:
293 define_event_symbols(event, ev_name, args->symbol.field);
294 define_field(PRINT_SYMBOL, ev_name, cur_field_name, NULL);
295 define_values(PRINT_SYMBOL, args->symbol.symbols, ev_name,
296 cur_field_name);
297 break;
Namhyung Kime080e6f2012-06-27 09:41:41 +0900298 case PRINT_HEX:
Daniel Borkmann0fe05592017-01-25 02:28:17 +0100299 case PRINT_HEX_STR:
Namhyung Kime080e6f2012-06-27 09:41:41 +0900300 define_event_symbols(event, ev_name, args->hex.field);
301 define_event_symbols(event, ev_name, args->hex.size);
302 break;
Javi Merinob839e1e82015-03-24 11:07:19 +0000303 case PRINT_INT_ARRAY:
304 define_event_symbols(event, ev_name, args->int_array.field);
305 define_event_symbols(event, ev_name, args->int_array.count);
306 define_event_symbols(event, ev_name, args->int_array.el_size);
307 break;
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600308 case PRINT_STRING:
309 break;
310 case PRINT_TYPE:
311 define_event_symbols(event, ev_name, args->typecast.item);
312 break;
313 case PRINT_OP:
314 if (strcmp(args->op.op, ":") == 0)
315 zero_flag_atom = 1;
316 define_event_symbols(event, ev_name, args->op.left);
317 define_event_symbols(event, ev_name, args->op.right);
318 break;
319 default:
Steven Rostedtaaf045f2012-04-06 00:47:56 +0200320 /* gcc warns for these? */
321 case PRINT_BSTRING:
322 case PRINT_DYNAMIC_ARRAY:
He Kuang76055942015-08-29 04:22:05 +0000323 case PRINT_DYNAMIC_ARRAY_LEN:
Steven Rostedtaaf045f2012-04-06 00:47:56 +0200324 case PRINT_FUNC:
Steven Rostedt (Red Hat)473a7782014-06-02 23:20:16 -0400325 case PRINT_BITMASK:
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600326 /* we should warn... */
327 return;
328 }
329
330 if (args->next)
331 define_event_symbols(event, ev_name, args->next);
332}
333
Sebastian Andrzej Siewior33058b92014-05-27 18:14:33 +0200334static PyObject *get_field_numeric_entry(struct event_format *event,
335 struct format_field *field, void *data)
336{
Sebastian Andrzej Siewior8ac631c2014-05-27 18:14:34 +0200337 bool is_array = field->flags & FIELD_IS_ARRAY;
Arnaldo Carvalho de Melo39f54862016-07-12 11:05:26 -0300338 PyObject *obj = NULL, *list = NULL;
Sebastian Andrzej Siewior33058b92014-05-27 18:14:33 +0200339 unsigned long long val;
Sebastian Andrzej Siewior8ac631c2014-05-27 18:14:34 +0200340 unsigned int item_size, n_items, i;
Sebastian Andrzej Siewior33058b92014-05-27 18:14:33 +0200341
Sebastian Andrzej Siewior8ac631c2014-05-27 18:14:34 +0200342 if (is_array) {
343 list = PyList_New(field->arraylen);
344 item_size = field->size / field->arraylen;
345 n_items = field->arraylen;
Sebastian Andrzej Siewior33058b92014-05-27 18:14:33 +0200346 } else {
Sebastian Andrzej Siewior8ac631c2014-05-27 18:14:34 +0200347 item_size = field->size;
348 n_items = 1;
Sebastian Andrzej Siewior33058b92014-05-27 18:14:33 +0200349 }
Sebastian Andrzej Siewior8ac631c2014-05-27 18:14:34 +0200350
351 for (i = 0; i < n_items; i++) {
352
353 val = read_size(event, data + field->offset + i * item_size,
354 item_size);
355 if (field->flags & FIELD_IS_SIGNED) {
356 if ((long long)val >= LONG_MIN &&
357 (long long)val <= LONG_MAX)
Jaroslav Škarvada66dfdff2018-01-19 21:56:41 +0100358 obj = _PyLong_FromLong(val);
Sebastian Andrzej Siewior8ac631c2014-05-27 18:14:34 +0200359 else
360 obj = PyLong_FromLongLong(val);
361 } else {
362 if (val <= LONG_MAX)
Jaroslav Škarvada66dfdff2018-01-19 21:56:41 +0100363 obj = _PyLong_FromLong(val);
Sebastian Andrzej Siewior8ac631c2014-05-27 18:14:34 +0200364 else
365 obj = PyLong_FromUnsignedLongLong(val);
366 }
367 if (is_array)
368 PyList_SET_ITEM(list, i, obj);
369 }
370 if (is_array)
371 obj = list;
Sebastian Andrzej Siewior33058b92014-05-27 18:14:33 +0200372 return obj;
373}
374
Jin Yao5f9e0f32018-06-01 17:01:01 +0800375static const char *get_dsoname(struct map *map)
376{
377 const char *dsoname = "[unknown]";
378
379 if (map && map->dso) {
380 if (symbol_conf.show_kernel_path && map->dso->long_name)
381 dsoname = map->dso->long_name;
382 else
383 dsoname = map->dso->name;
384 }
385
386 return dsoname;
387}
Joseph Schuchart0f5f5bc2014-07-10 13:50:51 +0200388
389static PyObject *python_process_callchain(struct perf_sample *sample,
390 struct perf_evsel *evsel,
391 struct addr_location *al)
392{
393 PyObject *pylist;
394
395 pylist = PyList_New(0);
396 if (!pylist)
397 Py_FatalError("couldn't create Python list");
398
399 if (!symbol_conf.use_callchain || !sample->callchain)
400 goto exit;
401
Arnaldo Carvalho de Melo91d7b2d2016-04-14 14:48:07 -0300402 if (thread__resolve_callchain(al->thread, &callchain_cursor, evsel,
Arnaldo Carvalho de Melocc8b7c22014-10-23 15:26:17 -0300403 sample, NULL, NULL,
Adrian Hunter44cbe722015-09-25 16:15:50 +0300404 scripting_max_stack) != 0) {
Joseph Schuchart0f5f5bc2014-07-10 13:50:51 +0200405 pr_err("Failed to resolve callchain. Skipping\n");
406 goto exit;
407 }
408 callchain_cursor_commit(&callchain_cursor);
409
410
411 while (1) {
412 PyObject *pyelem;
413 struct callchain_cursor_node *node;
414 node = callchain_cursor_current(&callchain_cursor);
415 if (!node)
416 break;
417
418 pyelem = PyDict_New();
419 if (!pyelem)
420 Py_FatalError("couldn't create Python dictionary");
421
422
423 pydict_set_item_string_decref(pyelem, "ip",
424 PyLong_FromUnsignedLongLong(node->ip));
425
426 if (node->sym) {
427 PyObject *pysym = PyDict_New();
428 if (!pysym)
429 Py_FatalError("couldn't create Python dictionary");
430 pydict_set_item_string_decref(pysym, "start",
431 PyLong_FromUnsignedLongLong(node->sym->start));
432 pydict_set_item_string_decref(pysym, "end",
433 PyLong_FromUnsignedLongLong(node->sym->end));
434 pydict_set_item_string_decref(pysym, "binding",
Jaroslav Škarvada66dfdff2018-01-19 21:56:41 +0100435 _PyLong_FromLong(node->sym->binding));
Joseph Schuchart0f5f5bc2014-07-10 13:50:51 +0200436 pydict_set_item_string_decref(pysym, "name",
Jaroslav Škarvada66dfdff2018-01-19 21:56:41 +0100437 _PyUnicode_FromStringAndSize(node->sym->name,
Joseph Schuchart0f5f5bc2014-07-10 13:50:51 +0200438 node->sym->namelen));
439 pydict_set_item_string_decref(pyelem, "sym", pysym);
440 }
441
442 if (node->map) {
Jin Yao5f9e0f32018-06-01 17:01:01 +0800443 const char *dsoname = get_dsoname(node->map);
444
Joseph Schuchart0f5f5bc2014-07-10 13:50:51 +0200445 pydict_set_item_string_decref(pyelem, "dso",
Jaroslav Škarvada66dfdff2018-01-19 21:56:41 +0100446 _PyUnicode_FromString(dsoname));
Joseph Schuchart0f5f5bc2014-07-10 13:50:51 +0200447 }
448
449 callchain_cursor_advance(&callchain_cursor);
450 PyList_Append(pylist, pyelem);
451 Py_DECREF(pyelem);
452 }
453
454exit:
455 return pylist;
456}
457
Arun Kalyanasundaram74ec14f2017-07-21 15:04:20 -0700458static PyObject *get_sample_value_as_tuple(struct sample_read_value *value)
459{
460 PyObject *t;
461
462 t = PyTuple_New(2);
463 if (!t)
464 Py_FatalError("couldn't create Python tuple");
465 PyTuple_SetItem(t, 0, PyLong_FromUnsignedLongLong(value->id));
466 PyTuple_SetItem(t, 1, PyLong_FromUnsignedLongLong(value->value));
467 return t;
468}
469
470static void set_sample_read_in_dict(PyObject *dict_sample,
471 struct perf_sample *sample,
472 struct perf_evsel *evsel)
473{
474 u64 read_format = evsel->attr.read_format;
475 PyObject *values;
476 unsigned int i;
477
478 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
479 pydict_set_item_string_decref(dict_sample, "time_enabled",
480 PyLong_FromUnsignedLongLong(sample->read.time_enabled));
481 }
482
483 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
484 pydict_set_item_string_decref(dict_sample, "time_running",
485 PyLong_FromUnsignedLongLong(sample->read.time_running));
486 }
487
488 if (read_format & PERF_FORMAT_GROUP)
489 values = PyList_New(sample->read.group.nr);
490 else
491 values = PyList_New(1);
492
493 if (!values)
494 Py_FatalError("couldn't create Python list");
495
496 if (read_format & PERF_FORMAT_GROUP) {
497 for (i = 0; i < sample->read.group.nr; i++) {
498 PyObject *t = get_sample_value_as_tuple(&sample->read.group.values[i]);
499 PyList_SET_ITEM(values, i, t);
500 }
501 } else {
502 PyObject *t = get_sample_value_as_tuple(&sample->read.one);
503 PyList_SET_ITEM(values, 0, t);
504 }
505 pydict_set_item_string_decref(dict_sample, "values", values);
506}
507
Arun Kalyanasundaram892e76b2017-07-21 15:04:19 -0700508static PyObject *get_perf_sample_dict(struct perf_sample *sample,
509 struct perf_evsel *evsel,
510 struct addr_location *al,
511 PyObject *callchain)
512{
513 PyObject *dict, *dict_sample;
514
515 dict = PyDict_New();
516 if (!dict)
517 Py_FatalError("couldn't create Python dictionary");
518
519 dict_sample = PyDict_New();
520 if (!dict_sample)
521 Py_FatalError("couldn't create Python dictionary");
522
Jaroslav Škarvada66dfdff2018-01-19 21:56:41 +0100523 pydict_set_item_string_decref(dict, "ev_name", _PyUnicode_FromString(perf_evsel__name(evsel)));
524 pydict_set_item_string_decref(dict, "attr", _PyUnicode_FromStringAndSize(
Arun Kalyanasundaram892e76b2017-07-21 15:04:19 -0700525 (const char *)&evsel->attr, sizeof(evsel->attr)));
526
527 pydict_set_item_string_decref(dict_sample, "pid",
Jaroslav Škarvada66dfdff2018-01-19 21:56:41 +0100528 _PyLong_FromLong(sample->pid));
Arun Kalyanasundaram892e76b2017-07-21 15:04:19 -0700529 pydict_set_item_string_decref(dict_sample, "tid",
Jaroslav Škarvada66dfdff2018-01-19 21:56:41 +0100530 _PyLong_FromLong(sample->tid));
Arun Kalyanasundaram892e76b2017-07-21 15:04:19 -0700531 pydict_set_item_string_decref(dict_sample, "cpu",
Jaroslav Škarvada66dfdff2018-01-19 21:56:41 +0100532 _PyLong_FromLong(sample->cpu));
Arun Kalyanasundaram892e76b2017-07-21 15:04:19 -0700533 pydict_set_item_string_decref(dict_sample, "ip",
534 PyLong_FromUnsignedLongLong(sample->ip));
535 pydict_set_item_string_decref(dict_sample, "time",
536 PyLong_FromUnsignedLongLong(sample->time));
537 pydict_set_item_string_decref(dict_sample, "period",
538 PyLong_FromUnsignedLongLong(sample->period));
Kan Liang41013f02018-01-04 12:59:55 -0800539 pydict_set_item_string_decref(dict_sample, "phys_addr",
540 PyLong_FromUnsignedLongLong(sample->phys_addr));
Leo Yan943f32a2018-05-28 16:45:01 +0800541 pydict_set_item_string_decref(dict_sample, "addr",
542 PyLong_FromUnsignedLongLong(sample->addr));
Arun Kalyanasundaram74ec14f2017-07-21 15:04:20 -0700543 set_sample_read_in_dict(dict_sample, sample, evsel);
Arun Kalyanasundaram892e76b2017-07-21 15:04:19 -0700544 pydict_set_item_string_decref(dict, "sample", dict_sample);
545
Jaroslav Škarvada66dfdff2018-01-19 21:56:41 +0100546 pydict_set_item_string_decref(dict, "raw_buf", _PyBytes_FromStringAndSize(
Arun Kalyanasundaram892e76b2017-07-21 15:04:19 -0700547 (const char *)sample->raw_data, sample->raw_size));
548 pydict_set_item_string_decref(dict, "comm",
Jaroslav Škarvada66dfdff2018-01-19 21:56:41 +0100549 _PyUnicode_FromString(thread__comm_str(al->thread)));
Arun Kalyanasundaram892e76b2017-07-21 15:04:19 -0700550 if (al->map) {
551 pydict_set_item_string_decref(dict, "dso",
Jaroslav Škarvada66dfdff2018-01-19 21:56:41 +0100552 _PyUnicode_FromString(al->map->dso->name));
Arun Kalyanasundaram892e76b2017-07-21 15:04:19 -0700553 }
554 if (al->sym) {
555 pydict_set_item_string_decref(dict, "symbol",
Jaroslav Škarvada66dfdff2018-01-19 21:56:41 +0100556 _PyUnicode_FromString(al->sym->name));
Arun Kalyanasundaram892e76b2017-07-21 15:04:19 -0700557 }
558
559 pydict_set_item_string_decref(dict, "callchain", callchain);
560
561 return dict;
562}
563
Arnaldo Carvalho de Melob7fff6b52013-12-19 16:34:52 -0300564static void python_process_tracepoint(struct perf_sample *sample,
565 struct perf_evsel *evsel,
Arnaldo Carvalho de Melob7fff6b52013-12-19 16:34:52 -0300566 struct addr_location *al)
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600567{
Jiri Olsaadf5bcf2014-10-26 23:44:05 +0100568 struct event_format *event = evsel->tp_format;
Arnaldo Carvalho de Melo39f54862016-07-12 11:05:26 -0300569 PyObject *handler, *context, *t, *obj = NULL, *callchain;
Arun Kalyanasundaramf38d2812017-07-21 15:04:21 -0700570 PyObject *dict = NULL, *all_entries_dict = NULL;
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600571 static char handler_name[256];
572 struct format_field *field;
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600573 unsigned long s, ns;
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600574 unsigned n = 0;
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600575 int pid;
David Ahernbe6d8422011-03-09 22:23:23 -0700576 int cpu = sample->cpu;
577 void *data = sample->raw_data;
578 unsigned long long nsecs = sample->time;
Arnaldo Carvalho de Melof9d5d542015-04-01 13:29:25 -0300579 const char *comm = thread__comm_str(al->thread);
Arun Kalyanasundarame9f9a9c2017-07-21 15:04:18 -0700580 const char *default_handler_name = "trace_unhandled";
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600581
Arnaldo Carvalho de Melo62665df2016-05-10 12:33:52 -0300582 if (!event) {
583 snprintf(handler_name, sizeof(handler_name),
584 "ug! no event found for type %" PRIu64, (u64)evsel->attr.config);
585 Py_FatalError(handler_name);
586 }
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600587
Arnaldo Carvalho de Melo97822432012-08-07 23:50:21 -0300588 pid = raw_field_value(event, "common_pid", data);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600589
590 sprintf(handler_name, "%s__%s", event->system, event->name);
591
Jiri Olsaadf5bcf2014-10-26 23:44:05 +0100592 if (!test_and_set_bit(event->id, events_defined))
593 define_event_symbols(event, handler_name, event->print_fmt.args);
594
Adrian Huntera5563ed2014-07-31 09:01:01 +0300595 handler = get_handler(handler_name);
Pierre Tardyc0251482010-05-31 23:12:09 +0200596 if (!handler) {
Arun Kalyanasundarame9f9a9c2017-07-21 15:04:18 -0700597 handler = get_handler(default_handler_name);
598 if (!handler)
599 return;
Pierre Tardyc0251482010-05-31 23:12:09 +0200600 dict = PyDict_New();
601 if (!dict)
602 Py_FatalError("couldn't create Python dict");
603 }
Arun Kalyanasundarame9f9a9c2017-07-21 15:04:18 -0700604
605 t = PyTuple_New(MAX_FIELDS);
606 if (!t)
607 Py_FatalError("couldn't create Python tuple");
608
609
Arnaldo Carvalho de Melobd48c632016-08-05 15:40:30 -0300610 s = nsecs / NSEC_PER_SEC;
611 ns = nsecs - s * NSEC_PER_SEC;
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600612
613 scripting_context->event_data = data;
Tom Zanussi2de95332013-01-18 13:51:27 -0600614 scripting_context->pevent = evsel->tp_format->pevent;
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600615
Jaroslav Škarvada66dfdff2018-01-19 21:56:41 +0100616 context = _PyCapsule_New(scripting_context, NULL, NULL);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600617
Jaroslav Škarvada66dfdff2018-01-19 21:56:41 +0100618 PyTuple_SetItem(t, n++, _PyUnicode_FromString(handler_name));
Kyle McMartinfb7d0b32011-01-24 11:13:04 -0500619 PyTuple_SetItem(t, n++, context);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600620
Joseph Schuchart0f5f5bc2014-07-10 13:50:51 +0200621 /* ip unwinding */
622 callchain = python_process_callchain(sample, evsel, al);
Arun Kalyanasundaramf38d2812017-07-21 15:04:21 -0700623 /* Need an additional reference for the perf_sample dict */
624 Py_INCREF(callchain);
Joseph Schuchart0f5f5bc2014-07-10 13:50:51 +0200625
Arun Kalyanasundarame9f9a9c2017-07-21 15:04:18 -0700626 if (!dict) {
Jaroslav Škarvada66dfdff2018-01-19 21:56:41 +0100627 PyTuple_SetItem(t, n++, _PyLong_FromLong(cpu));
628 PyTuple_SetItem(t, n++, _PyLong_FromLong(s));
629 PyTuple_SetItem(t, n++, _PyLong_FromLong(ns));
630 PyTuple_SetItem(t, n++, _PyLong_FromLong(pid));
631 PyTuple_SetItem(t, n++, _PyUnicode_FromString(comm));
Joseph Schuchart0f5f5bc2014-07-10 13:50:51 +0200632 PyTuple_SetItem(t, n++, callchain);
Pierre Tardyc0251482010-05-31 23:12:09 +0200633 } else {
Jaroslav Škarvada66dfdff2018-01-19 21:56:41 +0100634 pydict_set_item_string_decref(dict, "common_cpu", _PyLong_FromLong(cpu));
635 pydict_set_item_string_decref(dict, "common_s", _PyLong_FromLong(s));
636 pydict_set_item_string_decref(dict, "common_ns", _PyLong_FromLong(ns));
637 pydict_set_item_string_decref(dict, "common_pid", _PyLong_FromLong(pid));
638 pydict_set_item_string_decref(dict, "common_comm", _PyUnicode_FromString(comm));
Joseph Schuchart0f5f5bc2014-07-10 13:50:51 +0200639 pydict_set_item_string_decref(dict, "common_callchain", callchain);
Pierre Tardyc0251482010-05-31 23:12:09 +0200640 }
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600641 for (field = event->format.fields; field; field = field->next) {
Jiri Olsa249de6e2016-07-16 18:11:18 +0200642 unsigned int offset, len;
643 unsigned long long val;
644
645 if (field->flags & FIELD_IS_ARRAY) {
646 offset = field->offset;
647 len = field->size;
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600648 if (field->flags & FIELD_IS_DYNAMIC) {
Jiri Olsa249de6e2016-07-16 18:11:18 +0200649 val = pevent_read_number(scripting_context->pevent,
650 data + offset, len);
651 offset = val;
652 len = offset >> 16;
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600653 offset &= 0xffff;
Jiri Olsa249de6e2016-07-16 18:11:18 +0200654 }
655 if (field->flags & FIELD_IS_STRING &&
656 is_printable_array(data + offset, len)) {
Jaroslav Škarvada66dfdff2018-01-19 21:56:41 +0100657 obj = _PyUnicode_FromString((char *) data + offset);
Jiri Olsa249de6e2016-07-16 18:11:18 +0200658 } else {
659 obj = PyByteArray_FromStringAndSize((const char *) data + offset, len);
660 field->flags &= ~FIELD_IS_STRING;
661 }
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600662 } else { /* FIELD_IS_NUMERIC */
Sebastian Andrzej Siewior33058b92014-05-27 18:14:33 +0200663 obj = get_field_numeric_entry(event, field, data);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600664 }
Arun Kalyanasundarame9f9a9c2017-07-21 15:04:18 -0700665 if (!dict)
Pierre Tardyc0251482010-05-31 23:12:09 +0200666 PyTuple_SetItem(t, n++, obj);
667 else
Joseph Schuchartc0268e82013-10-24 10:10:51 -0300668 pydict_set_item_string_decref(dict, field->name, obj);
Pierre Tardyc0251482010-05-31 23:12:09 +0200669
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600670 }
Joseph Schuchart0f5f5bc2014-07-10 13:50:51 +0200671
Arun Kalyanasundarame9f9a9c2017-07-21 15:04:18 -0700672 if (dict)
Pierre Tardyc0251482010-05-31 23:12:09 +0200673 PyTuple_SetItem(t, n++, dict);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600674
Arun Kalyanasundaramf38d2812017-07-21 15:04:21 -0700675 if (get_argument_count(handler) == (int) n + 1) {
676 all_entries_dict = get_perf_sample_dict(sample, evsel, al,
677 callchain);
678 PyTuple_SetItem(t, n++, all_entries_dict);
679 } else {
680 Py_DECREF(callchain);
681 }
682
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600683 if (_PyTuple_Resize(&t, n) == -1)
684 Py_FatalError("error resizing Python tuple");
685
Arun Kalyanasundarame9f9a9c2017-07-21 15:04:18 -0700686 if (!dict) {
Adrian Huntera5563ed2014-07-31 09:01:01 +0300687 call_object(handler, t, handler_name);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600688 } else {
Arun Kalyanasundarame9f9a9c2017-07-21 15:04:18 -0700689 call_object(handler, t, default_handler_name);
Pierre Tardyc0251482010-05-31 23:12:09 +0200690 Py_DECREF(dict);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600691 }
692
Arun Kalyanasundaramf38d2812017-07-21 15:04:21 -0700693 Py_XDECREF(all_entries_dict);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600694 Py_DECREF(t);
695}
696
Adrian Hunterdf919b42014-10-23 13:45:14 +0300697static PyObject *tuple_new(unsigned int sz)
698{
699 PyObject *t;
700
701 t = PyTuple_New(sz);
702 if (!t)
703 Py_FatalError("couldn't create Python tuple");
704 return t;
705}
706
707static int tuple_set_u64(PyObject *t, unsigned int pos, u64 val)
708{
709#if BITS_PER_LONG == 64
Jaroslav Škarvada66dfdff2018-01-19 21:56:41 +0100710 return PyTuple_SetItem(t, pos, _PyLong_FromLong(val));
Adrian Hunterdf919b42014-10-23 13:45:14 +0300711#endif
712#if BITS_PER_LONG == 32
713 return PyTuple_SetItem(t, pos, PyLong_FromLongLong(val));
714#endif
715}
716
717static int tuple_set_s32(PyObject *t, unsigned int pos, s32 val)
718{
Jaroslav Škarvada66dfdff2018-01-19 21:56:41 +0100719 return PyTuple_SetItem(t, pos, _PyLong_FromLong(val));
Adrian Hunterdf919b42014-10-23 13:45:14 +0300720}
721
722static int tuple_set_string(PyObject *t, unsigned int pos, const char *s)
723{
Jaroslav Škarvada66dfdff2018-01-19 21:56:41 +0100724 return PyTuple_SetItem(t, pos, _PyUnicode_FromString(s));
Adrian Hunterdf919b42014-10-23 13:45:14 +0300725}
726
727static int python_export_evsel(struct db_export *dbe, struct perf_evsel *evsel)
728{
729 struct tables *tables = container_of(dbe, struct tables, dbe);
730 PyObject *t;
731
732 t = tuple_new(2);
733
734 tuple_set_u64(t, 0, evsel->db_id);
735 tuple_set_string(t, 1, perf_evsel__name(evsel));
736
737 call_object(tables->evsel_handler, t, "evsel_table");
738
739 Py_DECREF(t);
740
741 return 0;
742}
743
744static int python_export_machine(struct db_export *dbe,
745 struct machine *machine)
746{
747 struct tables *tables = container_of(dbe, struct tables, dbe);
748 PyObject *t;
749
750 t = tuple_new(3);
751
752 tuple_set_u64(t, 0, machine->db_id);
753 tuple_set_s32(t, 1, machine->pid);
754 tuple_set_string(t, 2, machine->root_dir ? machine->root_dir : "");
755
756 call_object(tables->machine_handler, t, "machine_table");
757
758 Py_DECREF(t);
759
760 return 0;
761}
762
763static int python_export_thread(struct db_export *dbe, struct thread *thread,
764 u64 main_thread_db_id, struct machine *machine)
765{
766 struct tables *tables = container_of(dbe, struct tables, dbe);
767 PyObject *t;
768
769 t = tuple_new(5);
770
771 tuple_set_u64(t, 0, thread->db_id);
772 tuple_set_u64(t, 1, machine->db_id);
773 tuple_set_u64(t, 2, main_thread_db_id);
774 tuple_set_s32(t, 3, thread->pid_);
775 tuple_set_s32(t, 4, thread->tid);
776
777 call_object(tables->thread_handler, t, "thread_table");
778
779 Py_DECREF(t);
780
781 return 0;
782}
783
784static int python_export_comm(struct db_export *dbe, struct comm *comm)
785{
786 struct tables *tables = container_of(dbe, struct tables, dbe);
787 PyObject *t;
788
789 t = tuple_new(2);
790
791 tuple_set_u64(t, 0, comm->db_id);
792 tuple_set_string(t, 1, comm__str(comm));
793
794 call_object(tables->comm_handler, t, "comm_table");
795
796 Py_DECREF(t);
797
798 return 0;
799}
800
801static int python_export_comm_thread(struct db_export *dbe, u64 db_id,
802 struct comm *comm, struct thread *thread)
803{
804 struct tables *tables = container_of(dbe, struct tables, dbe);
805 PyObject *t;
806
807 t = tuple_new(3);
808
809 tuple_set_u64(t, 0, db_id);
810 tuple_set_u64(t, 1, comm->db_id);
811 tuple_set_u64(t, 2, thread->db_id);
812
813 call_object(tables->comm_thread_handler, t, "comm_thread_table");
814
815 Py_DECREF(t);
816
817 return 0;
818}
819
820static int python_export_dso(struct db_export *dbe, struct dso *dso,
821 struct machine *machine)
822{
823 struct tables *tables = container_of(dbe, struct tables, dbe);
Masami Hiramatsub5d8bbe2016-05-11 22:51:59 +0900824 char sbuild_id[SBUILD_ID_SIZE];
Adrian Hunterdf919b42014-10-23 13:45:14 +0300825 PyObject *t;
826
827 build_id__sprintf(dso->build_id, sizeof(dso->build_id), sbuild_id);
828
829 t = tuple_new(5);
830
831 tuple_set_u64(t, 0, dso->db_id);
832 tuple_set_u64(t, 1, machine->db_id);
833 tuple_set_string(t, 2, dso->short_name);
834 tuple_set_string(t, 3, dso->long_name);
835 tuple_set_string(t, 4, sbuild_id);
836
837 call_object(tables->dso_handler, t, "dso_table");
838
839 Py_DECREF(t);
840
841 return 0;
842}
843
844static int python_export_symbol(struct db_export *dbe, struct symbol *sym,
845 struct dso *dso)
846{
847 struct tables *tables = container_of(dbe, struct tables, dbe);
848 u64 *sym_db_id = symbol__priv(sym);
849 PyObject *t;
850
851 t = tuple_new(6);
852
853 tuple_set_u64(t, 0, *sym_db_id);
854 tuple_set_u64(t, 1, dso->db_id);
855 tuple_set_u64(t, 2, sym->start);
856 tuple_set_u64(t, 3, sym->end);
857 tuple_set_s32(t, 4, sym->binding);
858 tuple_set_string(t, 5, sym->name);
859
860 call_object(tables->symbol_handler, t, "symbol_table");
861
862 Py_DECREF(t);
863
864 return 0;
865}
866
Adrian Hunterc29414f2014-10-30 16:09:44 +0200867static int python_export_branch_type(struct db_export *dbe, u32 branch_type,
868 const char *name)
869{
870 struct tables *tables = container_of(dbe, struct tables, dbe);
871 PyObject *t;
872
873 t = tuple_new(2);
874
875 tuple_set_s32(t, 0, branch_type);
876 tuple_set_string(t, 1, name);
877
878 call_object(tables->branch_type_handler, t, "branch_type_table");
879
880 Py_DECREF(t);
881
882 return 0;
883}
884
Adrian Hunterdf919b42014-10-23 13:45:14 +0300885static int python_export_sample(struct db_export *dbe,
886 struct export_sample *es)
887{
888 struct tables *tables = container_of(dbe, struct tables, dbe);
889 PyObject *t;
890
Chris Phlipot2c15f5e2016-04-28 01:19:10 -0700891 t = tuple_new(22);
Adrian Hunterdf919b42014-10-23 13:45:14 +0300892
893 tuple_set_u64(t, 0, es->db_id);
894 tuple_set_u64(t, 1, es->evsel->db_id);
895 tuple_set_u64(t, 2, es->al->machine->db_id);
Arnaldo Carvalho de Melob83e8682015-04-02 11:16:05 -0300896 tuple_set_u64(t, 3, es->al->thread->db_id);
Adrian Hunterdf919b42014-10-23 13:45:14 +0300897 tuple_set_u64(t, 4, es->comm_db_id);
898 tuple_set_u64(t, 5, es->dso_db_id);
899 tuple_set_u64(t, 6, es->sym_db_id);
900 tuple_set_u64(t, 7, es->offset);
901 tuple_set_u64(t, 8, es->sample->ip);
902 tuple_set_u64(t, 9, es->sample->time);
903 tuple_set_s32(t, 10, es->sample->cpu);
904 tuple_set_u64(t, 11, es->addr_dso_db_id);
905 tuple_set_u64(t, 12, es->addr_sym_db_id);
906 tuple_set_u64(t, 13, es->addr_offset);
907 tuple_set_u64(t, 14, es->sample->addr);
908 tuple_set_u64(t, 15, es->sample->period);
909 tuple_set_u64(t, 16, es->sample->weight);
910 tuple_set_u64(t, 17, es->sample->transaction);
911 tuple_set_u64(t, 18, es->sample->data_src);
Adrian Hunterc29414f2014-10-30 16:09:44 +0200912 tuple_set_s32(t, 19, es->sample->flags & PERF_BRANCH_MASK);
913 tuple_set_s32(t, 20, !!(es->sample->flags & PERF_IP_FLAG_IN_TX));
Chris Phlipot2c15f5e2016-04-28 01:19:10 -0700914 tuple_set_u64(t, 21, es->call_path_id);
Adrian Hunterdf919b42014-10-23 13:45:14 +0300915
916 call_object(tables->sample_handler, t, "sample_table");
917
918 Py_DECREF(t);
919
920 return 0;
921}
922
Adrian Hunter6a703072014-10-30 16:09:47 +0200923static int python_export_call_path(struct db_export *dbe, struct call_path *cp)
924{
925 struct tables *tables = container_of(dbe, struct tables, dbe);
926 PyObject *t;
927 u64 parent_db_id, sym_db_id;
928
929 parent_db_id = cp->parent ? cp->parent->db_id : 0;
930 sym_db_id = cp->sym ? *(u64 *)symbol__priv(cp->sym) : 0;
931
932 t = tuple_new(4);
933
934 tuple_set_u64(t, 0, cp->db_id);
935 tuple_set_u64(t, 1, parent_db_id);
936 tuple_set_u64(t, 2, sym_db_id);
937 tuple_set_u64(t, 3, cp->ip);
938
939 call_object(tables->call_path_handler, t, "call_path_table");
940
941 Py_DECREF(t);
942
943 return 0;
944}
945
946static int python_export_call_return(struct db_export *dbe,
947 struct call_return *cr)
948{
949 struct tables *tables = container_of(dbe, struct tables, dbe);
950 u64 comm_db_id = cr->comm ? cr->comm->db_id : 0;
951 PyObject *t;
952
953 t = tuple_new(11);
954
955 tuple_set_u64(t, 0, cr->db_id);
956 tuple_set_u64(t, 1, cr->thread->db_id);
957 tuple_set_u64(t, 2, comm_db_id);
958 tuple_set_u64(t, 3, cr->cp->db_id);
959 tuple_set_u64(t, 4, cr->call_time);
960 tuple_set_u64(t, 5, cr->return_time);
961 tuple_set_u64(t, 6, cr->branch_count);
962 tuple_set_u64(t, 7, cr->call_ref);
963 tuple_set_u64(t, 8, cr->return_ref);
964 tuple_set_u64(t, 9, cr->cp->parent->db_id);
965 tuple_set_s32(t, 10, cr->flags);
966
967 call_object(tables->call_return_handler, t, "call_return_table");
968
969 Py_DECREF(t);
970
971 return 0;
972}
973
974static int python_process_call_return(struct call_return *cr, void *data)
975{
976 struct db_export *dbe = data;
977
978 return db_export__call_return(dbe, cr);
979}
980
Arnaldo Carvalho de Melob7fff6b52013-12-19 16:34:52 -0300981static void python_process_general_event(struct perf_sample *sample,
Feng Tang6a6daec2012-08-08 17:57:51 +0800982 struct perf_evsel *evsel,
Feng Tang87b6a3a2012-08-09 13:46:13 +0800983 struct addr_location *al)
Feng Tang6a6daec2012-08-08 17:57:51 +0800984{
Arun Kalyanasundaram892e76b2017-07-21 15:04:19 -0700985 PyObject *handler, *t, *dict, *callchain;
Feng Tang6a6daec2012-08-08 17:57:51 +0800986 static char handler_name[64];
987 unsigned n = 0;
Feng Tang6a6daec2012-08-08 17:57:51 +0800988
Arun Kalyanasundarame9f9a9c2017-07-21 15:04:18 -0700989 snprintf(handler_name, sizeof(handler_name), "%s", "process_event");
990
991 handler = get_handler(handler_name);
992 if (!handler)
993 return;
994
Feng Tangfd6b8582012-08-08 17:57:53 +0800995 /*
996 * Use the MAX_FIELDS to make the function expandable, though
Feng Tang87b6a3a2012-08-09 13:46:13 +0800997 * currently there is only one item for the tuple.
Feng Tangfd6b8582012-08-08 17:57:53 +0800998 */
Feng Tang6a6daec2012-08-08 17:57:51 +0800999 t = PyTuple_New(MAX_FIELDS);
1000 if (!t)
1001 Py_FatalError("couldn't create Python tuple");
1002
Joseph Schuchart0f5f5bc2014-07-10 13:50:51 +02001003 /* ip unwinding */
1004 callchain = python_process_callchain(sample, evsel, al);
Arun Kalyanasundaram892e76b2017-07-21 15:04:19 -07001005 dict = get_perf_sample_dict(sample, evsel, al, callchain);
Joseph Schuchart0f5f5bc2014-07-10 13:50:51 +02001006
Feng Tangfd6b8582012-08-08 17:57:53 +08001007 PyTuple_SetItem(t, n++, dict);
Feng Tang6a6daec2012-08-08 17:57:51 +08001008 if (_PyTuple_Resize(&t, n) == -1)
1009 Py_FatalError("error resizing Python tuple");
1010
Adrian Huntera5563ed2014-07-31 09:01:01 +03001011 call_object(handler, t, handler_name);
Arun Kalyanasundarame9f9a9c2017-07-21 15:04:18 -07001012
Feng Tangfd6b8582012-08-08 17:57:53 +08001013 Py_DECREF(dict);
Feng Tang6a6daec2012-08-08 17:57:51 +08001014 Py_DECREF(t);
1015}
1016
Adrian Hunterdf919b42014-10-23 13:45:14 +03001017static void python_process_event(union perf_event *event,
Feng Tang6a6daec2012-08-08 17:57:51 +08001018 struct perf_sample *sample,
1019 struct perf_evsel *evsel,
Feng Tang73994dc2012-08-08 17:57:52 +08001020 struct addr_location *al)
Feng Tang6a6daec2012-08-08 17:57:51 +08001021{
Adrian Hunterdf919b42014-10-23 13:45:14 +03001022 struct tables *tables = &tables_global;
1023
Feng Tang6a6daec2012-08-08 17:57:51 +08001024 switch (evsel->attr.type) {
1025 case PERF_TYPE_TRACEPOINT:
Arnaldo Carvalho de Melof9d5d542015-04-01 13:29:25 -03001026 python_process_tracepoint(sample, evsel, al);
Feng Tang6a6daec2012-08-08 17:57:51 +08001027 break;
1028 /* Reserve for future process_hw/sw/raw APIs */
1029 default:
Adrian Hunterdf919b42014-10-23 13:45:14 +03001030 if (tables->db_export_mode)
Arnaldo Carvalho de Melo73272592015-04-02 11:08:30 -03001031 db_export__sample(&tables->dbe, event, sample, evsel, al);
Adrian Hunterdf919b42014-10-23 13:45:14 +03001032 else
Arnaldo Carvalho de Melof9d5d542015-04-01 13:29:25 -03001033 python_process_general_event(sample, evsel, al);
Feng Tang6a6daec2012-08-08 17:57:51 +08001034 }
1035}
1036
Jiri Olsaaef90262016-01-05 22:09:11 +01001037static void get_handler_name(char *str, size_t size,
1038 struct perf_evsel *evsel)
1039{
1040 char *p = str;
1041
1042 scnprintf(str, size, "stat__%s", perf_evsel__name(evsel));
1043
1044 while ((p = strchr(p, ':'))) {
1045 *p = '_';
1046 p++;
1047 }
1048}
1049
1050static void
1051process_stat(struct perf_evsel *counter, int cpu, int thread, u64 tstamp,
1052 struct perf_counts_values *count)
1053{
1054 PyObject *handler, *t;
1055 static char handler_name[256];
1056 int n = 0;
1057
1058 t = PyTuple_New(MAX_FIELDS);
1059 if (!t)
1060 Py_FatalError("couldn't create Python tuple");
1061
1062 get_handler_name(handler_name, sizeof(handler_name),
1063 counter);
1064
1065 handler = get_handler(handler_name);
1066 if (!handler) {
1067 pr_debug("can't find python handler %s\n", handler_name);
1068 return;
1069 }
1070
Jaroslav Škarvada66dfdff2018-01-19 21:56:41 +01001071 PyTuple_SetItem(t, n++, _PyLong_FromLong(cpu));
1072 PyTuple_SetItem(t, n++, _PyLong_FromLong(thread));
Jiri Olsaaef90262016-01-05 22:09:11 +01001073
1074 tuple_set_u64(t, n++, tstamp);
1075 tuple_set_u64(t, n++, count->val);
1076 tuple_set_u64(t, n++, count->ena);
1077 tuple_set_u64(t, n++, count->run);
1078
1079 if (_PyTuple_Resize(&t, n) == -1)
1080 Py_FatalError("error resizing Python tuple");
1081
1082 call_object(handler, t, handler_name);
1083
1084 Py_DECREF(t);
1085}
1086
1087static void python_process_stat(struct perf_stat_config *config,
1088 struct perf_evsel *counter, u64 tstamp)
1089{
1090 struct thread_map *threads = counter->threads;
1091 struct cpu_map *cpus = counter->cpus;
1092 int cpu, thread;
1093
1094 if (config->aggr_mode == AGGR_GLOBAL) {
1095 process_stat(counter, -1, -1, tstamp,
1096 &counter->counts->aggr);
1097 return;
1098 }
1099
1100 for (thread = 0; thread < threads->nr; thread++) {
1101 for (cpu = 0; cpu < cpus->nr; cpu++) {
1102 process_stat(counter, cpus->map[cpu],
1103 thread_map__pid(threads, thread), tstamp,
1104 perf_counts(counter->counts, cpu, thread));
1105 }
1106 }
1107}
1108
1109static void python_process_stat_interval(u64 tstamp)
1110{
1111 PyObject *handler, *t;
1112 static const char handler_name[] = "stat__interval";
1113 int n = 0;
1114
1115 t = PyTuple_New(MAX_FIELDS);
1116 if (!t)
1117 Py_FatalError("couldn't create Python tuple");
1118
1119 handler = get_handler(handler_name);
1120 if (!handler) {
1121 pr_debug("can't find python handler %s\n", handler_name);
1122 return;
1123 }
1124
1125 tuple_set_u64(t, n++, tstamp);
1126
1127 if (_PyTuple_Resize(&t, n) == -1)
1128 Py_FatalError("error resizing Python tuple");
1129
1130 call_object(handler, t, handler_name);
1131
1132 Py_DECREF(t);
1133}
1134
Tom Zanussi7e4b21b2010-01-27 02:27:57 -06001135static int run_start_sub(void)
1136{
Tom Zanussi7e4b21b2010-01-27 02:27:57 -06001137 main_module = PyImport_AddModule("__main__");
1138 if (main_module == NULL)
1139 return -1;
1140 Py_INCREF(main_module);
1141
1142 main_dict = PyModule_GetDict(main_module);
Adrian Huntera5563ed2014-07-31 09:01:01 +03001143 if (main_dict == NULL)
Tom Zanussi7e4b21b2010-01-27 02:27:57 -06001144 goto error;
Tom Zanussi7e4b21b2010-01-27 02:27:57 -06001145 Py_INCREF(main_dict);
1146
Adrian Huntera5563ed2014-07-31 09:01:01 +03001147 try_call_object("trace_begin", NULL);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -06001148
Adrian Huntera5563ed2014-07-31 09:01:01 +03001149 return 0;
Tom Zanussi7e4b21b2010-01-27 02:27:57 -06001150
Tom Zanussi7e4b21b2010-01-27 02:27:57 -06001151error:
1152 Py_XDECREF(main_dict);
1153 Py_XDECREF(main_module);
Adrian Huntera5563ed2014-07-31 09:01:01 +03001154 return -1;
Tom Zanussi7e4b21b2010-01-27 02:27:57 -06001155}
1156
Adrian Hunterdf919b42014-10-23 13:45:14 +03001157#define SET_TABLE_HANDLER_(name, handler_name, table_name) do { \
1158 tables->handler_name = get_handler(#table_name); \
1159 if (tables->handler_name) \
1160 tables->dbe.export_ ## name = python_export_ ## name; \
1161} while (0)
1162
1163#define SET_TABLE_HANDLER(name) \
1164 SET_TABLE_HANDLER_(name, name ## _handler, name ## _table)
1165
1166static void set_table_handlers(struct tables *tables)
1167{
1168 const char *perf_db_export_mode = "perf_db_export_mode";
Adrian Hunter6a703072014-10-30 16:09:47 +02001169 const char *perf_db_export_calls = "perf_db_export_calls";
Chris Phlipot2c15f5e2016-04-28 01:19:10 -07001170 const char *perf_db_export_callchains = "perf_db_export_callchains";
1171 PyObject *db_export_mode, *db_export_calls, *db_export_callchains;
Adrian Hunter6a703072014-10-30 16:09:47 +02001172 bool export_calls = false;
Chris Phlipot2c15f5e2016-04-28 01:19:10 -07001173 bool export_callchains = false;
Adrian Hunterdf919b42014-10-23 13:45:14 +03001174 int ret;
1175
1176 memset(tables, 0, sizeof(struct tables));
1177 if (db_export__init(&tables->dbe))
1178 Py_FatalError("failed to initialize export");
1179
1180 db_export_mode = PyDict_GetItemString(main_dict, perf_db_export_mode);
1181 if (!db_export_mode)
1182 return;
1183
1184 ret = PyObject_IsTrue(db_export_mode);
1185 if (ret == -1)
1186 handler_call_die(perf_db_export_mode);
1187 if (!ret)
1188 return;
1189
Chris Phlipot2c15f5e2016-04-28 01:19:10 -07001190 /* handle export calls */
Adrian Hunter6a703072014-10-30 16:09:47 +02001191 tables->dbe.crp = NULL;
1192 db_export_calls = PyDict_GetItemString(main_dict, perf_db_export_calls);
1193 if (db_export_calls) {
1194 ret = PyObject_IsTrue(db_export_calls);
1195 if (ret == -1)
1196 handler_call_die(perf_db_export_calls);
1197 export_calls = !!ret;
1198 }
1199
1200 if (export_calls) {
1201 tables->dbe.crp =
1202 call_return_processor__new(python_process_call_return,
1203 &tables->dbe);
1204 if (!tables->dbe.crp)
1205 Py_FatalError("failed to create calls processor");
1206 }
1207
Chris Phlipot2c15f5e2016-04-28 01:19:10 -07001208 /* handle export callchains */
1209 tables->dbe.cpr = NULL;
1210 db_export_callchains = PyDict_GetItemString(main_dict,
1211 perf_db_export_callchains);
1212 if (db_export_callchains) {
1213 ret = PyObject_IsTrue(db_export_callchains);
1214 if (ret == -1)
1215 handler_call_die(perf_db_export_callchains);
1216 export_callchains = !!ret;
1217 }
1218
1219 if (export_callchains) {
1220 /*
1221 * Attempt to use the call path root from the call return
1222 * processor, if the call return processor is in use. Otherwise,
1223 * we allocate a new call path root. This prevents exporting
1224 * duplicate call path ids when both are in use simultaniously.
1225 */
1226 if (tables->dbe.crp)
1227 tables->dbe.cpr = tables->dbe.crp->cpr;
1228 else
1229 tables->dbe.cpr = call_path_root__new();
1230
1231 if (!tables->dbe.cpr)
Chris Phlipotaff63342016-05-07 02:17:00 -07001232 Py_FatalError("failed to create call path root");
Chris Phlipot2c15f5e2016-04-28 01:19:10 -07001233 }
1234
Adrian Hunterdf919b42014-10-23 13:45:14 +03001235 tables->db_export_mode = true;
1236 /*
1237 * Reserve per symbol space for symbol->db_id via symbol__priv()
1238 */
1239 symbol_conf.priv_size = sizeof(u64);
1240
1241 SET_TABLE_HANDLER(evsel);
1242 SET_TABLE_HANDLER(machine);
1243 SET_TABLE_HANDLER(thread);
1244 SET_TABLE_HANDLER(comm);
1245 SET_TABLE_HANDLER(comm_thread);
1246 SET_TABLE_HANDLER(dso);
1247 SET_TABLE_HANDLER(symbol);
Adrian Hunterc29414f2014-10-30 16:09:44 +02001248 SET_TABLE_HANDLER(branch_type);
Adrian Hunterdf919b42014-10-23 13:45:14 +03001249 SET_TABLE_HANDLER(sample);
Adrian Hunter6a703072014-10-30 16:09:47 +02001250 SET_TABLE_HANDLER(call_path);
1251 SET_TABLE_HANDLER(call_return);
Adrian Hunterdf919b42014-10-23 13:45:14 +03001252}
1253
Jaroslav Škarvada66dfdff2018-01-19 21:56:41 +01001254#if PY_MAJOR_VERSION < 3
1255static void _free_command_line(const char **command_line, int num)
1256{
1257 free(command_line);
1258}
1259#else
1260static void _free_command_line(wchar_t **command_line, int num)
1261{
1262 int i;
1263 for (i = 0; i < num; i++)
1264 PyMem_RawFree(command_line[i]);
1265 free(command_line);
1266}
1267#endif
1268
1269
Tom Zanussi7e4b21b2010-01-27 02:27:57 -06001270/*
1271 * Start trace script
1272 */
1273static int python_start_script(const char *script, int argc, const char **argv)
1274{
Adrian Hunterdf919b42014-10-23 13:45:14 +03001275 struct tables *tables = &tables_global;
Jaroslav Škarvada66dfdff2018-01-19 21:56:41 +01001276#if PY_MAJOR_VERSION < 3
Tom Zanussi7e4b21b2010-01-27 02:27:57 -06001277 const char **command_line;
Jaroslav Škarvada66dfdff2018-01-19 21:56:41 +01001278#else
1279 wchar_t **command_line;
1280#endif
Tom Zanussi7e4b21b2010-01-27 02:27:57 -06001281 char buf[PATH_MAX];
1282 int i, err = 0;
1283 FILE *fp;
1284
Jaroslav Škarvada66dfdff2018-01-19 21:56:41 +01001285#if PY_MAJOR_VERSION < 3
Tom Zanussi7e4b21b2010-01-27 02:27:57 -06001286 command_line = malloc((argc + 1) * sizeof(const char *));
1287 command_line[0] = script;
1288 for (i = 1; i < argc + 1; i++)
1289 command_line[i] = argv[i - 1];
Jaroslav Škarvada66dfdff2018-01-19 21:56:41 +01001290#else
1291 command_line = malloc((argc + 1) * sizeof(wchar_t *));
1292 command_line[0] = Py_DecodeLocale(script, NULL);
1293 for (i = 1; i < argc + 1; i++)
1294 command_line[i] = Py_DecodeLocale(argv[i - 1], NULL);
1295#endif
Tom Zanussi7e4b21b2010-01-27 02:27:57 -06001296
1297 Py_Initialize();
1298
Jaroslav Škarvada66dfdff2018-01-19 21:56:41 +01001299#if PY_MAJOR_VERSION < 3
Tom Zanussi7e4b21b2010-01-27 02:27:57 -06001300 initperf_trace_context();
Tom Zanussi7e4b21b2010-01-27 02:27:57 -06001301 PySys_SetArgv(argc + 1, (char **)command_line);
Jaroslav Škarvada66dfdff2018-01-19 21:56:41 +01001302#else
1303 PyInit_perf_trace_context();
1304 PySys_SetArgv(argc + 1, command_line);
1305#endif
Tom Zanussi7e4b21b2010-01-27 02:27:57 -06001306
1307 fp = fopen(script, "r");
1308 if (!fp) {
1309 sprintf(buf, "Can't open python script \"%s\"", script);
1310 perror(buf);
1311 err = -1;
1312 goto error;
1313 }
1314
1315 err = PyRun_SimpleFile(fp, script);
1316 if (err) {
1317 fprintf(stderr, "Error running python script %s\n", script);
1318 goto error;
1319 }
1320
1321 err = run_start_sub();
1322 if (err) {
1323 fprintf(stderr, "Error starting python script %s\n", script);
1324 goto error;
1325 }
1326
Adrian Hunterdf919b42014-10-23 13:45:14 +03001327 set_table_handlers(tables);
1328
Adrian Hunterc29414f2014-10-30 16:09:44 +02001329 if (tables->db_export_mode) {
1330 err = db_export__branch_types(&tables->dbe);
1331 if (err)
1332 goto error;
1333 }
1334
Jaroslav Škarvada66dfdff2018-01-19 21:56:41 +01001335 _free_command_line(command_line, argc + 1);
Colin Ian King979ac252016-03-01 23:46:20 +00001336
Tom Zanussi7e4b21b2010-01-27 02:27:57 -06001337 return err;
1338error:
1339 Py_Finalize();
Jaroslav Škarvada66dfdff2018-01-19 21:56:41 +01001340 _free_command_line(command_line, argc + 1);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -06001341
1342 return err;
1343}
1344
Adrian Hunterd445dd22014-08-15 22:08:37 +03001345static int python_flush_script(void)
1346{
Adrian Hunter758008b2014-10-30 16:09:48 +02001347 struct tables *tables = &tables_global;
1348
1349 return db_export__flush(&tables->dbe);
Adrian Hunterd445dd22014-08-15 22:08:37 +03001350}
1351
Tom Zanussi7e4b21b2010-01-27 02:27:57 -06001352/*
1353 * Stop trace script
1354 */
1355static int python_stop_script(void)
1356{
Adrian Hunterdf919b42014-10-23 13:45:14 +03001357 struct tables *tables = &tables_global;
1358
Adrian Huntera5563ed2014-07-31 09:01:01 +03001359 try_call_object("trace_end", NULL);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -06001360
Adrian Hunterdf919b42014-10-23 13:45:14 +03001361 db_export__exit(&tables->dbe);
1362
Tom Zanussi7e4b21b2010-01-27 02:27:57 -06001363 Py_XDECREF(main_dict);
1364 Py_XDECREF(main_module);
1365 Py_Finalize();
1366
Adrian Huntera5563ed2014-07-31 09:01:01 +03001367 return 0;
Tom Zanussi7e4b21b2010-01-27 02:27:57 -06001368}
1369
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03001370static int python_generate_script(struct pevent *pevent, const char *outfile)
Tom Zanussi7e4b21b2010-01-27 02:27:57 -06001371{
Steven Rostedtaaf045f2012-04-06 00:47:56 +02001372 struct event_format *event = NULL;
Tom Zanussi7e4b21b2010-01-27 02:27:57 -06001373 struct format_field *f;
1374 char fname[PATH_MAX];
1375 int not_first, count;
1376 FILE *ofp;
1377
1378 sprintf(fname, "%s.py", outfile);
1379 ofp = fopen(fname, "w");
1380 if (ofp == NULL) {
1381 fprintf(stderr, "couldn't open %s\n", fname);
1382 return -1;
1383 }
Ingo Molnar133dc4c2010-11-16 18:45:39 +01001384 fprintf(ofp, "# perf script event handlers, "
1385 "generated by perf script -g python\n");
Tom Zanussi7e4b21b2010-01-27 02:27:57 -06001386
1387 fprintf(ofp, "# Licensed under the terms of the GNU GPL"
1388 " License version 2\n\n");
1389
1390 fprintf(ofp, "# The common_* event handler fields are the most useful "
1391 "fields common to\n");
1392
1393 fprintf(ofp, "# all events. They don't necessarily correspond to "
1394 "the 'common_*' fields\n");
1395
1396 fprintf(ofp, "# in the format files. Those fields not available as "
1397 "handler params can\n");
1398
1399 fprintf(ofp, "# be retrieved using Python functions of the form "
1400 "common_*(context).\n");
1401
SeongJae Parkc76132d2017-05-30 20:18:23 +09001402 fprintf(ofp, "# See the perf-script-python Documentation for the list "
Tom Zanussi7e4b21b2010-01-27 02:27:57 -06001403 "of available functions.\n\n");
1404
1405 fprintf(ofp, "import os\n");
1406 fprintf(ofp, "import sys\n\n");
1407
1408 fprintf(ofp, "sys.path.append(os.environ['PERF_EXEC_PATH'] + \\\n");
1409 fprintf(ofp, "\t'/scripts/python/Perf-Trace-Util/lib/Perf/Trace')\n");
1410 fprintf(ofp, "\nfrom perf_trace_context import *\n");
1411 fprintf(ofp, "from Core import *\n\n\n");
1412
1413 fprintf(ofp, "def trace_begin():\n");
1414 fprintf(ofp, "\tprint \"in trace_begin\"\n\n");
1415
1416 fprintf(ofp, "def trace_end():\n");
1417 fprintf(ofp, "\tprint \"in trace_end\"\n\n");
1418
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03001419 while ((event = trace_find_next_event(pevent, event))) {
Tom Zanussi7e4b21b2010-01-27 02:27:57 -06001420 fprintf(ofp, "def %s__%s(", event->system, event->name);
1421 fprintf(ofp, "event_name, ");
1422 fprintf(ofp, "context, ");
1423 fprintf(ofp, "common_cpu,\n");
1424 fprintf(ofp, "\tcommon_secs, ");
1425 fprintf(ofp, "common_nsecs, ");
1426 fprintf(ofp, "common_pid, ");
1427 fprintf(ofp, "common_comm,\n\t");
Joseph Schuchart0f5f5bc2014-07-10 13:50:51 +02001428 fprintf(ofp, "common_callchain, ");
Tom Zanussi7e4b21b2010-01-27 02:27:57 -06001429
1430 not_first = 0;
1431 count = 0;
1432
1433 for (f = event->format.fields; f; f = f->next) {
1434 if (not_first++)
1435 fprintf(ofp, ", ");
1436 if (++count % 5 == 0)
1437 fprintf(ofp, "\n\t");
1438
1439 fprintf(ofp, "%s", f->name);
1440 }
Arun Kalyanasundarama6418602017-07-21 15:04:22 -07001441 if (not_first++)
1442 fprintf(ofp, ", ");
1443 if (++count % 5 == 0)
1444 fprintf(ofp, "\n\t\t");
1445 fprintf(ofp, "perf_sample_dict");
1446
Tom Zanussi7e4b21b2010-01-27 02:27:57 -06001447 fprintf(ofp, "):\n");
1448
1449 fprintf(ofp, "\t\tprint_header(event_name, common_cpu, "
1450 "common_secs, common_nsecs,\n\t\t\t"
1451 "common_pid, common_comm)\n\n");
1452
1453 fprintf(ofp, "\t\tprint \"");
1454
1455 not_first = 0;
1456 count = 0;
1457
1458 for (f = event->format.fields; f; f = f->next) {
1459 if (not_first++)
1460 fprintf(ofp, ", ");
1461 if (count && count % 3 == 0) {
1462 fprintf(ofp, "\" \\\n\t\t\"");
1463 }
1464 count++;
1465
1466 fprintf(ofp, "%s=", f->name);
1467 if (f->flags & FIELD_IS_STRING ||
1468 f->flags & FIELD_IS_FLAG ||
Namhyung Kime646fe72014-05-29 13:44:55 +09001469 f->flags & FIELD_IS_ARRAY ||
Tom Zanussi7e4b21b2010-01-27 02:27:57 -06001470 f->flags & FIELD_IS_SYMBOLIC)
1471 fprintf(ofp, "%%s");
1472 else if (f->flags & FIELD_IS_SIGNED)
1473 fprintf(ofp, "%%d");
1474 else
1475 fprintf(ofp, "%%u");
1476 }
1477
Joseph Schuchart0f5f5bc2014-07-10 13:50:51 +02001478 fprintf(ofp, "\" %% \\\n\t\t(");
Tom Zanussi7e4b21b2010-01-27 02:27:57 -06001479
1480 not_first = 0;
1481 count = 0;
1482
1483 for (f = event->format.fields; f; f = f->next) {
1484 if (not_first++)
1485 fprintf(ofp, ", ");
1486
1487 if (++count % 5 == 0)
1488 fprintf(ofp, "\n\t\t");
1489
1490 if (f->flags & FIELD_IS_FLAG) {
1491 if ((count - 1) % 5 != 0) {
1492 fprintf(ofp, "\n\t\t");
1493 count = 4;
1494 }
1495 fprintf(ofp, "flag_str(\"");
1496 fprintf(ofp, "%s__%s\", ", event->system,
1497 event->name);
1498 fprintf(ofp, "\"%s\", %s)", f->name,
1499 f->name);
1500 } else if (f->flags & FIELD_IS_SYMBOLIC) {
1501 if ((count - 1) % 5 != 0) {
1502 fprintf(ofp, "\n\t\t");
1503 count = 4;
1504 }
1505 fprintf(ofp, "symbol_str(\"");
1506 fprintf(ofp, "%s__%s\", ", event->system,
1507 event->name);
1508 fprintf(ofp, "\"%s\", %s)", f->name,
1509 f->name);
1510 } else
1511 fprintf(ofp, "%s", f->name);
1512 }
1513
Joseph Schuchart0f5f5bc2014-07-10 13:50:51 +02001514 fprintf(ofp, ")\n\n");
1515
Arun Kalyanasundarama6418602017-07-21 15:04:22 -07001516 fprintf(ofp, "\t\tprint 'Sample: {'+"
1517 "get_dict_as_string(perf_sample_dict['sample'], ', ')+'}'\n\n");
1518
Joseph Schuchart0f5f5bc2014-07-10 13:50:51 +02001519 fprintf(ofp, "\t\tfor node in common_callchain:");
1520 fprintf(ofp, "\n\t\t\tif 'sym' in node:");
1521 fprintf(ofp, "\n\t\t\t\tprint \"\\t[%%x] %%s\" %% (node['ip'], node['sym']['name'])");
1522 fprintf(ofp, "\n\t\t\telse:");
1523 fprintf(ofp, "\n\t\t\t\tprint \"\t[%%x]\" %% (node['ip'])\n\n");
1524 fprintf(ofp, "\t\tprint \"\\n\"\n\n");
1525
Tom Zanussi7e4b21b2010-01-27 02:27:57 -06001526 }
1527
1528 fprintf(ofp, "def trace_unhandled(event_name, context, "
Arun Kalyanasundarama6418602017-07-21 15:04:22 -07001529 "event_fields_dict, perf_sample_dict):\n");
Tom Zanussi7e4b21b2010-01-27 02:27:57 -06001530
Arun Kalyanasundarama6418602017-07-21 15:04:22 -07001531 fprintf(ofp, "\t\tprint get_dict_as_string(event_fields_dict)\n");
1532 fprintf(ofp, "\t\tprint 'Sample: {'+"
1533 "get_dict_as_string(perf_sample_dict['sample'], ', ')+'}'\n\n");
Tom Zanussi7e4b21b2010-01-27 02:27:57 -06001534
1535 fprintf(ofp, "def print_header("
1536 "event_name, cpu, secs, nsecs, pid, comm):\n"
1537 "\tprint \"%%-20s %%5u %%05u.%%09u %%8u %%-20s \" %% \\\n\t"
Arun Kalyanasundarama6418602017-07-21 15:04:22 -07001538 "(event_name, cpu, secs, nsecs, pid, comm),\n\n");
1539
1540 fprintf(ofp, "def get_dict_as_string(a_dict, delimiter=' '):\n"
1541 "\treturn delimiter.join"
1542 "(['%%s=%%s'%%(k,str(v))for k,v in sorted(a_dict.items())])\n");
Tom Zanussi7e4b21b2010-01-27 02:27:57 -06001543
1544 fclose(ofp);
1545
1546 fprintf(stderr, "generated Python script: %s\n", fname);
1547
1548 return 0;
1549}
1550
1551struct scripting_ops python_scripting_ops = {
Jiri Olsaaef90262016-01-05 22:09:11 +01001552 .name = "Python",
1553 .start_script = python_start_script,
1554 .flush_script = python_flush_script,
1555 .stop_script = python_stop_script,
1556 .process_event = python_process_event,
1557 .process_stat = python_process_stat,
1558 .process_stat_interval = python_process_stat_interval,
1559 .generate_script = python_generate_script,
Tom Zanussi7e4b21b2010-01-27 02:27:57 -06001560};