blob: c1848b543f27259aa249bf06d59f93390aa3c939 [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
52PyMODINIT_FUNC initperf_trace_context(void);
53
Steven Rostedt (Red Hat)609a7402015-05-13 13:44:36 -040054#define TRACE_EVENT_TYPE_MAX \
Tom Zanussi7e4b21b2010-01-27 02:27:57 -060055 ((1 << (sizeof(unsigned short) * 8)) - 1)
56
Steven Rostedt (Red Hat)609a7402015-05-13 13:44:36 -040057static DECLARE_BITMAP(events_defined, TRACE_EVENT_TYPE_MAX);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -060058
59#define MAX_FIELDS 64
60#define N_COMMON_FIELDS 7
61
62extern struct scripting_context *scripting_context;
63
64static char *cur_field_name;
65static int zero_flag_atom;
66
67static PyObject *main_module, *main_dict;
68
Adrian Hunterdf919b42014-10-23 13:45:14 +030069struct tables {
70 struct db_export dbe;
71 PyObject *evsel_handler;
72 PyObject *machine_handler;
73 PyObject *thread_handler;
74 PyObject *comm_handler;
75 PyObject *comm_thread_handler;
76 PyObject *dso_handler;
77 PyObject *symbol_handler;
Adrian Hunterc29414f2014-10-30 16:09:44 +020078 PyObject *branch_type_handler;
Adrian Hunterdf919b42014-10-23 13:45:14 +030079 PyObject *sample_handler;
Adrian Hunter6a703072014-10-30 16:09:47 +020080 PyObject *call_path_handler;
81 PyObject *call_return_handler;
Adrian Hunterdf919b42014-10-23 13:45:14 +030082 bool db_export_mode;
83};
84
85static struct tables tables_global;
86
Arnaldo Carvalho de Melo6c346642017-06-16 11:39:15 -030087static void handler_call_die(const char *handler_name) __noreturn;
Tom Zanussi7e4b21b2010-01-27 02:27:57 -060088static void handler_call_die(const char *handler_name)
89{
90 PyErr_Print();
91 Py_FatalError("problem in Python trace event handler");
Joseph Schuchart05f832e2014-07-09 16:16:31 +020092 // Py_FatalError does not return
93 // but we have to make the compiler happy
94 abort();
Tom Zanussi7e4b21b2010-01-27 02:27:57 -060095}
96
Joseph Schuchartc0268e82013-10-24 10:10:51 -030097/*
98 * Insert val into into the dictionary and decrement the reference counter.
Arnaldo Carvalho de Melo48000a12014-12-17 17:24:45 -030099 * This is necessary for dictionaries since PyDict_SetItemString() does not
Joseph Schuchartc0268e82013-10-24 10:10:51 -0300100 * steal a reference, as opposed to PyTuple_SetItem().
101 */
102static void pydict_set_item_string_decref(PyObject *dict, const char *key, PyObject *val)
103{
104 PyDict_SetItemString(dict, key, val);
105 Py_DECREF(val);
106}
107
Adrian Huntera5563ed2014-07-31 09:01:01 +0300108static PyObject *get_handler(const char *handler_name)
109{
110 PyObject *handler;
111
112 handler = PyDict_GetItemString(main_dict, handler_name);
113 if (handler && !PyCallable_Check(handler))
114 return NULL;
115 return handler;
116}
117
Arun Kalyanasundaramf38d2812017-07-21 15:04:21 -0700118static int get_argument_count(PyObject *handler)
119{
120 int arg_count = 0;
121
122 /*
123 * The attribute for the code object is func_code in Python 2,
124 * whereas it is __code__ in Python 3.0+.
125 */
126 PyObject *code_obj = PyObject_GetAttrString(handler,
127 "func_code");
128 if (PyErr_Occurred()) {
129 PyErr_Clear();
130 code_obj = PyObject_GetAttrString(handler,
131 "__code__");
132 }
133 PyErr_Clear();
134 if (code_obj) {
135 PyObject *arg_count_obj = PyObject_GetAttrString(code_obj,
136 "co_argcount");
137 if (arg_count_obj) {
138 arg_count = (int) PyInt_AsLong(arg_count_obj);
139 Py_DECREF(arg_count_obj);
140 }
141 Py_DECREF(code_obj);
142 }
143 return arg_count;
144}
145
Adrian Huntera5563ed2014-07-31 09:01:01 +0300146static void call_object(PyObject *handler, PyObject *args, const char *die_msg)
147{
148 PyObject *retval;
149
150 retval = PyObject_CallObject(handler, args);
151 if (retval == NULL)
152 handler_call_die(die_msg);
153 Py_DECREF(retval);
154}
155
156static void try_call_object(const char *handler_name, PyObject *args)
157{
158 PyObject *handler;
159
160 handler = get_handler(handler_name);
161 if (handler)
162 call_object(handler, args, handler_name);
163}
164
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600165static void define_value(enum print_arg_type field_type,
166 const char *ev_name,
167 const char *field_name,
168 const char *field_value,
169 const char *field_str)
170{
171 const char *handler_name = "define_flag_value";
Adrian Huntera5563ed2014-07-31 09:01:01 +0300172 PyObject *t;
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600173 unsigned long long value;
174 unsigned n = 0;
175
176 if (field_type == PRINT_SYMBOL)
177 handler_name = "define_symbolic_value";
178
Tom Zanussi44ad9cd2010-02-22 01:12:59 -0600179 t = PyTuple_New(4);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600180 if (!t)
181 Py_FatalError("couldn't create Python tuple");
182
183 value = eval_flag(field_value);
184
185 PyTuple_SetItem(t, n++, PyString_FromString(ev_name));
186 PyTuple_SetItem(t, n++, PyString_FromString(field_name));
187 PyTuple_SetItem(t, n++, PyInt_FromLong(value));
188 PyTuple_SetItem(t, n++, PyString_FromString(field_str));
189
Adrian Huntera5563ed2014-07-31 09:01:01 +0300190 try_call_object(handler_name, t);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600191
192 Py_DECREF(t);
193}
194
195static void define_values(enum print_arg_type field_type,
196 struct print_flag_sym *field,
197 const char *ev_name,
198 const char *field_name)
199{
200 define_value(field_type, ev_name, field_name, field->value,
201 field->str);
202
203 if (field->next)
204 define_values(field_type, field->next, ev_name, field_name);
205}
206
207static void define_field(enum print_arg_type field_type,
208 const char *ev_name,
209 const char *field_name,
210 const char *delim)
211{
212 const char *handler_name = "define_flag_field";
Adrian Huntera5563ed2014-07-31 09:01:01 +0300213 PyObject *t;
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600214 unsigned n = 0;
215
216 if (field_type == PRINT_SYMBOL)
217 handler_name = "define_symbolic_field";
218
Tom Zanussi44ad9cd2010-02-22 01:12:59 -0600219 if (field_type == PRINT_FLAGS)
220 t = PyTuple_New(3);
221 else
222 t = PyTuple_New(2);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600223 if (!t)
224 Py_FatalError("couldn't create Python tuple");
225
226 PyTuple_SetItem(t, n++, PyString_FromString(ev_name));
227 PyTuple_SetItem(t, n++, PyString_FromString(field_name));
228 if (field_type == PRINT_FLAGS)
229 PyTuple_SetItem(t, n++, PyString_FromString(delim));
230
Adrian Huntera5563ed2014-07-31 09:01:01 +0300231 try_call_object(handler_name, t);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600232
233 Py_DECREF(t);
234}
235
Steven Rostedtaaf045f2012-04-06 00:47:56 +0200236static void define_event_symbols(struct event_format *event,
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600237 const char *ev_name,
238 struct print_arg *args)
239{
Taeung Song8579aca2016-02-26 00:12:59 +0900240 if (args == NULL)
241 return;
242
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600243 switch (args->type) {
244 case PRINT_NULL:
245 break;
246 case PRINT_ATOM:
247 define_value(PRINT_FLAGS, ev_name, cur_field_name, "0",
248 args->atom.atom);
249 zero_flag_atom = 0;
250 break;
251 case PRINT_FIELD:
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -0300252 free(cur_field_name);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600253 cur_field_name = strdup(args->field.name);
254 break;
255 case PRINT_FLAGS:
256 define_event_symbols(event, ev_name, args->flags.field);
257 define_field(PRINT_FLAGS, ev_name, cur_field_name,
258 args->flags.delim);
259 define_values(PRINT_FLAGS, args->flags.flags, ev_name,
260 cur_field_name);
261 break;
262 case PRINT_SYMBOL:
263 define_event_symbols(event, ev_name, args->symbol.field);
264 define_field(PRINT_SYMBOL, ev_name, cur_field_name, NULL);
265 define_values(PRINT_SYMBOL, args->symbol.symbols, ev_name,
266 cur_field_name);
267 break;
Namhyung Kime080e6f2012-06-27 09:41:41 +0900268 case PRINT_HEX:
Daniel Borkmann0fe05592017-01-25 02:28:17 +0100269 case PRINT_HEX_STR:
Namhyung Kime080e6f2012-06-27 09:41:41 +0900270 define_event_symbols(event, ev_name, args->hex.field);
271 define_event_symbols(event, ev_name, args->hex.size);
272 break;
Javi Merinob839e1e82015-03-24 11:07:19 +0000273 case PRINT_INT_ARRAY:
274 define_event_symbols(event, ev_name, args->int_array.field);
275 define_event_symbols(event, ev_name, args->int_array.count);
276 define_event_symbols(event, ev_name, args->int_array.el_size);
277 break;
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600278 case PRINT_STRING:
279 break;
280 case PRINT_TYPE:
281 define_event_symbols(event, ev_name, args->typecast.item);
282 break;
283 case PRINT_OP:
284 if (strcmp(args->op.op, ":") == 0)
285 zero_flag_atom = 1;
286 define_event_symbols(event, ev_name, args->op.left);
287 define_event_symbols(event, ev_name, args->op.right);
288 break;
289 default:
Steven Rostedtaaf045f2012-04-06 00:47:56 +0200290 /* gcc warns for these? */
291 case PRINT_BSTRING:
292 case PRINT_DYNAMIC_ARRAY:
He Kuang76055942015-08-29 04:22:05 +0000293 case PRINT_DYNAMIC_ARRAY_LEN:
Steven Rostedtaaf045f2012-04-06 00:47:56 +0200294 case PRINT_FUNC:
Steven Rostedt (Red Hat)473a7782014-06-02 23:20:16 -0400295 case PRINT_BITMASK:
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600296 /* we should warn... */
297 return;
298 }
299
300 if (args->next)
301 define_event_symbols(event, ev_name, args->next);
302}
303
Sebastian Andrzej Siewior33058b92014-05-27 18:14:33 +0200304static PyObject *get_field_numeric_entry(struct event_format *event,
305 struct format_field *field, void *data)
306{
Sebastian Andrzej Siewior8ac631c2014-05-27 18:14:34 +0200307 bool is_array = field->flags & FIELD_IS_ARRAY;
Arnaldo Carvalho de Melo39f54862016-07-12 11:05:26 -0300308 PyObject *obj = NULL, *list = NULL;
Sebastian Andrzej Siewior33058b92014-05-27 18:14:33 +0200309 unsigned long long val;
Sebastian Andrzej Siewior8ac631c2014-05-27 18:14:34 +0200310 unsigned int item_size, n_items, i;
Sebastian Andrzej Siewior33058b92014-05-27 18:14:33 +0200311
Sebastian Andrzej Siewior8ac631c2014-05-27 18:14:34 +0200312 if (is_array) {
313 list = PyList_New(field->arraylen);
314 item_size = field->size / field->arraylen;
315 n_items = field->arraylen;
Sebastian Andrzej Siewior33058b92014-05-27 18:14:33 +0200316 } else {
Sebastian Andrzej Siewior8ac631c2014-05-27 18:14:34 +0200317 item_size = field->size;
318 n_items = 1;
Sebastian Andrzej Siewior33058b92014-05-27 18:14:33 +0200319 }
Sebastian Andrzej Siewior8ac631c2014-05-27 18:14:34 +0200320
321 for (i = 0; i < n_items; i++) {
322
323 val = read_size(event, data + field->offset + i * item_size,
324 item_size);
325 if (field->flags & FIELD_IS_SIGNED) {
326 if ((long long)val >= LONG_MIN &&
327 (long long)val <= LONG_MAX)
328 obj = PyInt_FromLong(val);
329 else
330 obj = PyLong_FromLongLong(val);
331 } else {
332 if (val <= LONG_MAX)
333 obj = PyInt_FromLong(val);
334 else
335 obj = PyLong_FromUnsignedLongLong(val);
336 }
337 if (is_array)
338 PyList_SET_ITEM(list, i, obj);
339 }
340 if (is_array)
341 obj = list;
Sebastian Andrzej Siewior33058b92014-05-27 18:14:33 +0200342 return obj;
343}
344
Joseph Schuchart0f5f5bc2014-07-10 13:50:51 +0200345
346static PyObject *python_process_callchain(struct perf_sample *sample,
347 struct perf_evsel *evsel,
348 struct addr_location *al)
349{
350 PyObject *pylist;
351
352 pylist = PyList_New(0);
353 if (!pylist)
354 Py_FatalError("couldn't create Python list");
355
356 if (!symbol_conf.use_callchain || !sample->callchain)
357 goto exit;
358
Arnaldo Carvalho de Melo91d7b2d2016-04-14 14:48:07 -0300359 if (thread__resolve_callchain(al->thread, &callchain_cursor, evsel,
Arnaldo Carvalho de Melocc8b7c22014-10-23 15:26:17 -0300360 sample, NULL, NULL,
Adrian Hunter44cbe722015-09-25 16:15:50 +0300361 scripting_max_stack) != 0) {
Joseph Schuchart0f5f5bc2014-07-10 13:50:51 +0200362 pr_err("Failed to resolve callchain. Skipping\n");
363 goto exit;
364 }
365 callchain_cursor_commit(&callchain_cursor);
366
367
368 while (1) {
369 PyObject *pyelem;
370 struct callchain_cursor_node *node;
371 node = callchain_cursor_current(&callchain_cursor);
372 if (!node)
373 break;
374
375 pyelem = PyDict_New();
376 if (!pyelem)
377 Py_FatalError("couldn't create Python dictionary");
378
379
380 pydict_set_item_string_decref(pyelem, "ip",
381 PyLong_FromUnsignedLongLong(node->ip));
382
383 if (node->sym) {
384 PyObject *pysym = PyDict_New();
385 if (!pysym)
386 Py_FatalError("couldn't create Python dictionary");
387 pydict_set_item_string_decref(pysym, "start",
388 PyLong_FromUnsignedLongLong(node->sym->start));
389 pydict_set_item_string_decref(pysym, "end",
390 PyLong_FromUnsignedLongLong(node->sym->end));
391 pydict_set_item_string_decref(pysym, "binding",
392 PyInt_FromLong(node->sym->binding));
393 pydict_set_item_string_decref(pysym, "name",
394 PyString_FromStringAndSize(node->sym->name,
395 node->sym->namelen));
396 pydict_set_item_string_decref(pyelem, "sym", pysym);
397 }
398
399 if (node->map) {
400 struct map *map = node->map;
401 const char *dsoname = "[unknown]";
Arnaldo Carvalho de Melo8bd8c652017-02-15 21:31:40 -0300402 if (map && map->dso) {
Joseph Schuchart0f5f5bc2014-07-10 13:50:51 +0200403 if (symbol_conf.show_kernel_path && map->dso->long_name)
404 dsoname = map->dso->long_name;
Arnaldo Carvalho de Melo8bd8c652017-02-15 21:31:40 -0300405 else
Joseph Schuchart0f5f5bc2014-07-10 13:50:51 +0200406 dsoname = map->dso->name;
407 }
408 pydict_set_item_string_decref(pyelem, "dso",
409 PyString_FromString(dsoname));
410 }
411
412 callchain_cursor_advance(&callchain_cursor);
413 PyList_Append(pylist, pyelem);
414 Py_DECREF(pyelem);
415 }
416
417exit:
418 return pylist;
419}
420
Arun Kalyanasundaram74ec14f2017-07-21 15:04:20 -0700421static PyObject *get_sample_value_as_tuple(struct sample_read_value *value)
422{
423 PyObject *t;
424
425 t = PyTuple_New(2);
426 if (!t)
427 Py_FatalError("couldn't create Python tuple");
428 PyTuple_SetItem(t, 0, PyLong_FromUnsignedLongLong(value->id));
429 PyTuple_SetItem(t, 1, PyLong_FromUnsignedLongLong(value->value));
430 return t;
431}
432
433static void set_sample_read_in_dict(PyObject *dict_sample,
434 struct perf_sample *sample,
435 struct perf_evsel *evsel)
436{
437 u64 read_format = evsel->attr.read_format;
438 PyObject *values;
439 unsigned int i;
440
441 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
442 pydict_set_item_string_decref(dict_sample, "time_enabled",
443 PyLong_FromUnsignedLongLong(sample->read.time_enabled));
444 }
445
446 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
447 pydict_set_item_string_decref(dict_sample, "time_running",
448 PyLong_FromUnsignedLongLong(sample->read.time_running));
449 }
450
451 if (read_format & PERF_FORMAT_GROUP)
452 values = PyList_New(sample->read.group.nr);
453 else
454 values = PyList_New(1);
455
456 if (!values)
457 Py_FatalError("couldn't create Python list");
458
459 if (read_format & PERF_FORMAT_GROUP) {
460 for (i = 0; i < sample->read.group.nr; i++) {
461 PyObject *t = get_sample_value_as_tuple(&sample->read.group.values[i]);
462 PyList_SET_ITEM(values, i, t);
463 }
464 } else {
465 PyObject *t = get_sample_value_as_tuple(&sample->read.one);
466 PyList_SET_ITEM(values, 0, t);
467 }
468 pydict_set_item_string_decref(dict_sample, "values", values);
469}
470
Arun Kalyanasundaram892e76b2017-07-21 15:04:19 -0700471static PyObject *get_perf_sample_dict(struct perf_sample *sample,
472 struct perf_evsel *evsel,
473 struct addr_location *al,
474 PyObject *callchain)
475{
476 PyObject *dict, *dict_sample;
477
478 dict = PyDict_New();
479 if (!dict)
480 Py_FatalError("couldn't create Python dictionary");
481
482 dict_sample = PyDict_New();
483 if (!dict_sample)
484 Py_FatalError("couldn't create Python dictionary");
485
486 pydict_set_item_string_decref(dict, "ev_name", PyString_FromString(perf_evsel__name(evsel)));
487 pydict_set_item_string_decref(dict, "attr", PyString_FromStringAndSize(
488 (const char *)&evsel->attr, sizeof(evsel->attr)));
489
490 pydict_set_item_string_decref(dict_sample, "pid",
491 PyInt_FromLong(sample->pid));
492 pydict_set_item_string_decref(dict_sample, "tid",
493 PyInt_FromLong(sample->tid));
494 pydict_set_item_string_decref(dict_sample, "cpu",
495 PyInt_FromLong(sample->cpu));
496 pydict_set_item_string_decref(dict_sample, "ip",
497 PyLong_FromUnsignedLongLong(sample->ip));
498 pydict_set_item_string_decref(dict_sample, "time",
499 PyLong_FromUnsignedLongLong(sample->time));
500 pydict_set_item_string_decref(dict_sample, "period",
501 PyLong_FromUnsignedLongLong(sample->period));
Arun Kalyanasundaram74ec14f2017-07-21 15:04:20 -0700502 set_sample_read_in_dict(dict_sample, sample, evsel);
Arun Kalyanasundaram892e76b2017-07-21 15:04:19 -0700503 pydict_set_item_string_decref(dict, "sample", dict_sample);
504
505 pydict_set_item_string_decref(dict, "raw_buf", PyString_FromStringAndSize(
506 (const char *)sample->raw_data, sample->raw_size));
507 pydict_set_item_string_decref(dict, "comm",
508 PyString_FromString(thread__comm_str(al->thread)));
509 if (al->map) {
510 pydict_set_item_string_decref(dict, "dso",
511 PyString_FromString(al->map->dso->name));
512 }
513 if (al->sym) {
514 pydict_set_item_string_decref(dict, "symbol",
515 PyString_FromString(al->sym->name));
516 }
517
518 pydict_set_item_string_decref(dict, "callchain", callchain);
519
520 return dict;
521}
522
Arnaldo Carvalho de Melob7fff6b52013-12-19 16:34:52 -0300523static void python_process_tracepoint(struct perf_sample *sample,
524 struct perf_evsel *evsel,
Arnaldo Carvalho de Melob7fff6b52013-12-19 16:34:52 -0300525 struct addr_location *al)
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600526{
Jiri Olsaadf5bcf2014-10-26 23:44:05 +0100527 struct event_format *event = evsel->tp_format;
Arnaldo Carvalho de Melo39f54862016-07-12 11:05:26 -0300528 PyObject *handler, *context, *t, *obj = NULL, *callchain;
Arun Kalyanasundaramf38d2812017-07-21 15:04:21 -0700529 PyObject *dict = NULL, *all_entries_dict = NULL;
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600530 static char handler_name[256];
531 struct format_field *field;
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600532 unsigned long s, ns;
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600533 unsigned n = 0;
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600534 int pid;
David Ahernbe6d8422011-03-09 22:23:23 -0700535 int cpu = sample->cpu;
536 void *data = sample->raw_data;
537 unsigned long long nsecs = sample->time;
Arnaldo Carvalho de Melof9d5d542015-04-01 13:29:25 -0300538 const char *comm = thread__comm_str(al->thread);
Arun Kalyanasundarame9f9a9c2017-07-21 15:04:18 -0700539 const char *default_handler_name = "trace_unhandled";
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600540
Arnaldo Carvalho de Melo62665df2016-05-10 12:33:52 -0300541 if (!event) {
542 snprintf(handler_name, sizeof(handler_name),
543 "ug! no event found for type %" PRIu64, (u64)evsel->attr.config);
544 Py_FatalError(handler_name);
545 }
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600546
Arnaldo Carvalho de Melo97822432012-08-07 23:50:21 -0300547 pid = raw_field_value(event, "common_pid", data);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600548
549 sprintf(handler_name, "%s__%s", event->system, event->name);
550
Jiri Olsaadf5bcf2014-10-26 23:44:05 +0100551 if (!test_and_set_bit(event->id, events_defined))
552 define_event_symbols(event, handler_name, event->print_fmt.args);
553
Adrian Huntera5563ed2014-07-31 09:01:01 +0300554 handler = get_handler(handler_name);
Pierre Tardyc0251482010-05-31 23:12:09 +0200555 if (!handler) {
Arun Kalyanasundarame9f9a9c2017-07-21 15:04:18 -0700556 handler = get_handler(default_handler_name);
557 if (!handler)
558 return;
Pierre Tardyc0251482010-05-31 23:12:09 +0200559 dict = PyDict_New();
560 if (!dict)
561 Py_FatalError("couldn't create Python dict");
562 }
Arun Kalyanasundarame9f9a9c2017-07-21 15:04:18 -0700563
564 t = PyTuple_New(MAX_FIELDS);
565 if (!t)
566 Py_FatalError("couldn't create Python tuple");
567
568
Arnaldo Carvalho de Melobd48c632016-08-05 15:40:30 -0300569 s = nsecs / NSEC_PER_SEC;
570 ns = nsecs - s * NSEC_PER_SEC;
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600571
572 scripting_context->event_data = data;
Tom Zanussi2de95332013-01-18 13:51:27 -0600573 scripting_context->pevent = evsel->tp_format->pevent;
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600574
575 context = PyCObject_FromVoidPtr(scripting_context, NULL);
576
577 PyTuple_SetItem(t, n++, PyString_FromString(handler_name));
Kyle McMartinfb7d0b32011-01-24 11:13:04 -0500578 PyTuple_SetItem(t, n++, context);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600579
Joseph Schuchart0f5f5bc2014-07-10 13:50:51 +0200580 /* ip unwinding */
581 callchain = python_process_callchain(sample, evsel, al);
Arun Kalyanasundaramf38d2812017-07-21 15:04:21 -0700582 /* Need an additional reference for the perf_sample dict */
583 Py_INCREF(callchain);
Joseph Schuchart0f5f5bc2014-07-10 13:50:51 +0200584
Arun Kalyanasundarame9f9a9c2017-07-21 15:04:18 -0700585 if (!dict) {
Pierre Tardyc0251482010-05-31 23:12:09 +0200586 PyTuple_SetItem(t, n++, PyInt_FromLong(cpu));
587 PyTuple_SetItem(t, n++, PyInt_FromLong(s));
588 PyTuple_SetItem(t, n++, PyInt_FromLong(ns));
589 PyTuple_SetItem(t, n++, PyInt_FromLong(pid));
590 PyTuple_SetItem(t, n++, PyString_FromString(comm));
Joseph Schuchart0f5f5bc2014-07-10 13:50:51 +0200591 PyTuple_SetItem(t, n++, callchain);
Pierre Tardyc0251482010-05-31 23:12:09 +0200592 } else {
Joseph Schuchartc0268e82013-10-24 10:10:51 -0300593 pydict_set_item_string_decref(dict, "common_cpu", PyInt_FromLong(cpu));
594 pydict_set_item_string_decref(dict, "common_s", PyInt_FromLong(s));
595 pydict_set_item_string_decref(dict, "common_ns", PyInt_FromLong(ns));
596 pydict_set_item_string_decref(dict, "common_pid", PyInt_FromLong(pid));
597 pydict_set_item_string_decref(dict, "common_comm", PyString_FromString(comm));
Joseph Schuchart0f5f5bc2014-07-10 13:50:51 +0200598 pydict_set_item_string_decref(dict, "common_callchain", callchain);
Pierre Tardyc0251482010-05-31 23:12:09 +0200599 }
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600600 for (field = event->format.fields; field; field = field->next) {
Jiri Olsa249de6e2016-07-16 18:11:18 +0200601 unsigned int offset, len;
602 unsigned long long val;
603
604 if (field->flags & FIELD_IS_ARRAY) {
605 offset = field->offset;
606 len = field->size;
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600607 if (field->flags & FIELD_IS_DYNAMIC) {
Jiri Olsa249de6e2016-07-16 18:11:18 +0200608 val = pevent_read_number(scripting_context->pevent,
609 data + offset, len);
610 offset = val;
611 len = offset >> 16;
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600612 offset &= 0xffff;
Jiri Olsa249de6e2016-07-16 18:11:18 +0200613 }
614 if (field->flags & FIELD_IS_STRING &&
615 is_printable_array(data + offset, len)) {
616 obj = PyString_FromString((char *) data + offset);
617 } else {
618 obj = PyByteArray_FromStringAndSize((const char *) data + offset, len);
619 field->flags &= ~FIELD_IS_STRING;
620 }
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600621 } else { /* FIELD_IS_NUMERIC */
Sebastian Andrzej Siewior33058b92014-05-27 18:14:33 +0200622 obj = get_field_numeric_entry(event, field, data);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600623 }
Arun Kalyanasundarame9f9a9c2017-07-21 15:04:18 -0700624 if (!dict)
Pierre Tardyc0251482010-05-31 23:12:09 +0200625 PyTuple_SetItem(t, n++, obj);
626 else
Joseph Schuchartc0268e82013-10-24 10:10:51 -0300627 pydict_set_item_string_decref(dict, field->name, obj);
Pierre Tardyc0251482010-05-31 23:12:09 +0200628
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600629 }
Joseph Schuchart0f5f5bc2014-07-10 13:50:51 +0200630
Arun Kalyanasundarame9f9a9c2017-07-21 15:04:18 -0700631 if (dict)
Pierre Tardyc0251482010-05-31 23:12:09 +0200632 PyTuple_SetItem(t, n++, dict);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600633
Arun Kalyanasundaramf38d2812017-07-21 15:04:21 -0700634 if (get_argument_count(handler) == (int) n + 1) {
635 all_entries_dict = get_perf_sample_dict(sample, evsel, al,
636 callchain);
637 PyTuple_SetItem(t, n++, all_entries_dict);
638 } else {
639 Py_DECREF(callchain);
640 }
641
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600642 if (_PyTuple_Resize(&t, n) == -1)
643 Py_FatalError("error resizing Python tuple");
644
Arun Kalyanasundarame9f9a9c2017-07-21 15:04:18 -0700645 if (!dict) {
Adrian Huntera5563ed2014-07-31 09:01:01 +0300646 call_object(handler, t, handler_name);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600647 } else {
Arun Kalyanasundarame9f9a9c2017-07-21 15:04:18 -0700648 call_object(handler, t, default_handler_name);
Pierre Tardyc0251482010-05-31 23:12:09 +0200649 Py_DECREF(dict);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600650 }
651
Arun Kalyanasundaramf38d2812017-07-21 15:04:21 -0700652 Py_XDECREF(all_entries_dict);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600653 Py_DECREF(t);
654}
655
Adrian Hunterdf919b42014-10-23 13:45:14 +0300656static PyObject *tuple_new(unsigned int sz)
657{
658 PyObject *t;
659
660 t = PyTuple_New(sz);
661 if (!t)
662 Py_FatalError("couldn't create Python tuple");
663 return t;
664}
665
666static int tuple_set_u64(PyObject *t, unsigned int pos, u64 val)
667{
668#if BITS_PER_LONG == 64
669 return PyTuple_SetItem(t, pos, PyInt_FromLong(val));
670#endif
671#if BITS_PER_LONG == 32
672 return PyTuple_SetItem(t, pos, PyLong_FromLongLong(val));
673#endif
674}
675
676static int tuple_set_s32(PyObject *t, unsigned int pos, s32 val)
677{
678 return PyTuple_SetItem(t, pos, PyInt_FromLong(val));
679}
680
681static int tuple_set_string(PyObject *t, unsigned int pos, const char *s)
682{
683 return PyTuple_SetItem(t, pos, PyString_FromString(s));
684}
685
686static int python_export_evsel(struct db_export *dbe, struct perf_evsel *evsel)
687{
688 struct tables *tables = container_of(dbe, struct tables, dbe);
689 PyObject *t;
690
691 t = tuple_new(2);
692
693 tuple_set_u64(t, 0, evsel->db_id);
694 tuple_set_string(t, 1, perf_evsel__name(evsel));
695
696 call_object(tables->evsel_handler, t, "evsel_table");
697
698 Py_DECREF(t);
699
700 return 0;
701}
702
703static int python_export_machine(struct db_export *dbe,
704 struct machine *machine)
705{
706 struct tables *tables = container_of(dbe, struct tables, dbe);
707 PyObject *t;
708
709 t = tuple_new(3);
710
711 tuple_set_u64(t, 0, machine->db_id);
712 tuple_set_s32(t, 1, machine->pid);
713 tuple_set_string(t, 2, machine->root_dir ? machine->root_dir : "");
714
715 call_object(tables->machine_handler, t, "machine_table");
716
717 Py_DECREF(t);
718
719 return 0;
720}
721
722static int python_export_thread(struct db_export *dbe, struct thread *thread,
723 u64 main_thread_db_id, struct machine *machine)
724{
725 struct tables *tables = container_of(dbe, struct tables, dbe);
726 PyObject *t;
727
728 t = tuple_new(5);
729
730 tuple_set_u64(t, 0, thread->db_id);
731 tuple_set_u64(t, 1, machine->db_id);
732 tuple_set_u64(t, 2, main_thread_db_id);
733 tuple_set_s32(t, 3, thread->pid_);
734 tuple_set_s32(t, 4, thread->tid);
735
736 call_object(tables->thread_handler, t, "thread_table");
737
738 Py_DECREF(t);
739
740 return 0;
741}
742
743static int python_export_comm(struct db_export *dbe, struct comm *comm)
744{
745 struct tables *tables = container_of(dbe, struct tables, dbe);
746 PyObject *t;
747
748 t = tuple_new(2);
749
750 tuple_set_u64(t, 0, comm->db_id);
751 tuple_set_string(t, 1, comm__str(comm));
752
753 call_object(tables->comm_handler, t, "comm_table");
754
755 Py_DECREF(t);
756
757 return 0;
758}
759
760static int python_export_comm_thread(struct db_export *dbe, u64 db_id,
761 struct comm *comm, struct thread *thread)
762{
763 struct tables *tables = container_of(dbe, struct tables, dbe);
764 PyObject *t;
765
766 t = tuple_new(3);
767
768 tuple_set_u64(t, 0, db_id);
769 tuple_set_u64(t, 1, comm->db_id);
770 tuple_set_u64(t, 2, thread->db_id);
771
772 call_object(tables->comm_thread_handler, t, "comm_thread_table");
773
774 Py_DECREF(t);
775
776 return 0;
777}
778
779static int python_export_dso(struct db_export *dbe, struct dso *dso,
780 struct machine *machine)
781{
782 struct tables *tables = container_of(dbe, struct tables, dbe);
Masami Hiramatsub5d8bbe2016-05-11 22:51:59 +0900783 char sbuild_id[SBUILD_ID_SIZE];
Adrian Hunterdf919b42014-10-23 13:45:14 +0300784 PyObject *t;
785
786 build_id__sprintf(dso->build_id, sizeof(dso->build_id), sbuild_id);
787
788 t = tuple_new(5);
789
790 tuple_set_u64(t, 0, dso->db_id);
791 tuple_set_u64(t, 1, machine->db_id);
792 tuple_set_string(t, 2, dso->short_name);
793 tuple_set_string(t, 3, dso->long_name);
794 tuple_set_string(t, 4, sbuild_id);
795
796 call_object(tables->dso_handler, t, "dso_table");
797
798 Py_DECREF(t);
799
800 return 0;
801}
802
803static int python_export_symbol(struct db_export *dbe, struct symbol *sym,
804 struct dso *dso)
805{
806 struct tables *tables = container_of(dbe, struct tables, dbe);
807 u64 *sym_db_id = symbol__priv(sym);
808 PyObject *t;
809
810 t = tuple_new(6);
811
812 tuple_set_u64(t, 0, *sym_db_id);
813 tuple_set_u64(t, 1, dso->db_id);
814 tuple_set_u64(t, 2, sym->start);
815 tuple_set_u64(t, 3, sym->end);
816 tuple_set_s32(t, 4, sym->binding);
817 tuple_set_string(t, 5, sym->name);
818
819 call_object(tables->symbol_handler, t, "symbol_table");
820
821 Py_DECREF(t);
822
823 return 0;
824}
825
Adrian Hunterc29414f2014-10-30 16:09:44 +0200826static int python_export_branch_type(struct db_export *dbe, u32 branch_type,
827 const char *name)
828{
829 struct tables *tables = container_of(dbe, struct tables, dbe);
830 PyObject *t;
831
832 t = tuple_new(2);
833
834 tuple_set_s32(t, 0, branch_type);
835 tuple_set_string(t, 1, name);
836
837 call_object(tables->branch_type_handler, t, "branch_type_table");
838
839 Py_DECREF(t);
840
841 return 0;
842}
843
Adrian Hunterdf919b42014-10-23 13:45:14 +0300844static int python_export_sample(struct db_export *dbe,
845 struct export_sample *es)
846{
847 struct tables *tables = container_of(dbe, struct tables, dbe);
848 PyObject *t;
849
Chris Phlipot2c15f5e2016-04-28 01:19:10 -0700850 t = tuple_new(22);
Adrian Hunterdf919b42014-10-23 13:45:14 +0300851
852 tuple_set_u64(t, 0, es->db_id);
853 tuple_set_u64(t, 1, es->evsel->db_id);
854 tuple_set_u64(t, 2, es->al->machine->db_id);
Arnaldo Carvalho de Melob83e8682015-04-02 11:16:05 -0300855 tuple_set_u64(t, 3, es->al->thread->db_id);
Adrian Hunterdf919b42014-10-23 13:45:14 +0300856 tuple_set_u64(t, 4, es->comm_db_id);
857 tuple_set_u64(t, 5, es->dso_db_id);
858 tuple_set_u64(t, 6, es->sym_db_id);
859 tuple_set_u64(t, 7, es->offset);
860 tuple_set_u64(t, 8, es->sample->ip);
861 tuple_set_u64(t, 9, es->sample->time);
862 tuple_set_s32(t, 10, es->sample->cpu);
863 tuple_set_u64(t, 11, es->addr_dso_db_id);
864 tuple_set_u64(t, 12, es->addr_sym_db_id);
865 tuple_set_u64(t, 13, es->addr_offset);
866 tuple_set_u64(t, 14, es->sample->addr);
867 tuple_set_u64(t, 15, es->sample->period);
868 tuple_set_u64(t, 16, es->sample->weight);
869 tuple_set_u64(t, 17, es->sample->transaction);
870 tuple_set_u64(t, 18, es->sample->data_src);
Adrian Hunterc29414f2014-10-30 16:09:44 +0200871 tuple_set_s32(t, 19, es->sample->flags & PERF_BRANCH_MASK);
872 tuple_set_s32(t, 20, !!(es->sample->flags & PERF_IP_FLAG_IN_TX));
Chris Phlipot2c15f5e2016-04-28 01:19:10 -0700873 tuple_set_u64(t, 21, es->call_path_id);
Adrian Hunterdf919b42014-10-23 13:45:14 +0300874
875 call_object(tables->sample_handler, t, "sample_table");
876
877 Py_DECREF(t);
878
879 return 0;
880}
881
Adrian Hunter6a703072014-10-30 16:09:47 +0200882static int python_export_call_path(struct db_export *dbe, struct call_path *cp)
883{
884 struct tables *tables = container_of(dbe, struct tables, dbe);
885 PyObject *t;
886 u64 parent_db_id, sym_db_id;
887
888 parent_db_id = cp->parent ? cp->parent->db_id : 0;
889 sym_db_id = cp->sym ? *(u64 *)symbol__priv(cp->sym) : 0;
890
891 t = tuple_new(4);
892
893 tuple_set_u64(t, 0, cp->db_id);
894 tuple_set_u64(t, 1, parent_db_id);
895 tuple_set_u64(t, 2, sym_db_id);
896 tuple_set_u64(t, 3, cp->ip);
897
898 call_object(tables->call_path_handler, t, "call_path_table");
899
900 Py_DECREF(t);
901
902 return 0;
903}
904
905static int python_export_call_return(struct db_export *dbe,
906 struct call_return *cr)
907{
908 struct tables *tables = container_of(dbe, struct tables, dbe);
909 u64 comm_db_id = cr->comm ? cr->comm->db_id : 0;
910 PyObject *t;
911
912 t = tuple_new(11);
913
914 tuple_set_u64(t, 0, cr->db_id);
915 tuple_set_u64(t, 1, cr->thread->db_id);
916 tuple_set_u64(t, 2, comm_db_id);
917 tuple_set_u64(t, 3, cr->cp->db_id);
918 tuple_set_u64(t, 4, cr->call_time);
919 tuple_set_u64(t, 5, cr->return_time);
920 tuple_set_u64(t, 6, cr->branch_count);
921 tuple_set_u64(t, 7, cr->call_ref);
922 tuple_set_u64(t, 8, cr->return_ref);
923 tuple_set_u64(t, 9, cr->cp->parent->db_id);
924 tuple_set_s32(t, 10, cr->flags);
925
926 call_object(tables->call_return_handler, t, "call_return_table");
927
928 Py_DECREF(t);
929
930 return 0;
931}
932
933static int python_process_call_return(struct call_return *cr, void *data)
934{
935 struct db_export *dbe = data;
936
937 return db_export__call_return(dbe, cr);
938}
939
Arnaldo Carvalho de Melob7fff6b52013-12-19 16:34:52 -0300940static void python_process_general_event(struct perf_sample *sample,
Feng Tang6a6daec2012-08-08 17:57:51 +0800941 struct perf_evsel *evsel,
Feng Tang87b6a3a2012-08-09 13:46:13 +0800942 struct addr_location *al)
Feng Tang6a6daec2012-08-08 17:57:51 +0800943{
Arun Kalyanasundaram892e76b2017-07-21 15:04:19 -0700944 PyObject *handler, *t, *dict, *callchain;
Feng Tang6a6daec2012-08-08 17:57:51 +0800945 static char handler_name[64];
946 unsigned n = 0;
Feng Tang6a6daec2012-08-08 17:57:51 +0800947
Arun Kalyanasundarame9f9a9c2017-07-21 15:04:18 -0700948 snprintf(handler_name, sizeof(handler_name), "%s", "process_event");
949
950 handler = get_handler(handler_name);
951 if (!handler)
952 return;
953
Feng Tangfd6b8582012-08-08 17:57:53 +0800954 /*
955 * Use the MAX_FIELDS to make the function expandable, though
Feng Tang87b6a3a2012-08-09 13:46:13 +0800956 * currently there is only one item for the tuple.
Feng Tangfd6b8582012-08-08 17:57:53 +0800957 */
Feng Tang6a6daec2012-08-08 17:57:51 +0800958 t = PyTuple_New(MAX_FIELDS);
959 if (!t)
960 Py_FatalError("couldn't create Python tuple");
961
Joseph Schuchart0f5f5bc2014-07-10 13:50:51 +0200962 /* ip unwinding */
963 callchain = python_process_callchain(sample, evsel, al);
Arun Kalyanasundaram892e76b2017-07-21 15:04:19 -0700964 dict = get_perf_sample_dict(sample, evsel, al, callchain);
Joseph Schuchart0f5f5bc2014-07-10 13:50:51 +0200965
Feng Tangfd6b8582012-08-08 17:57:53 +0800966 PyTuple_SetItem(t, n++, dict);
Feng Tang6a6daec2012-08-08 17:57:51 +0800967 if (_PyTuple_Resize(&t, n) == -1)
968 Py_FatalError("error resizing Python tuple");
969
Adrian Huntera5563ed2014-07-31 09:01:01 +0300970 call_object(handler, t, handler_name);
Arun Kalyanasundarame9f9a9c2017-07-21 15:04:18 -0700971
Feng Tangfd6b8582012-08-08 17:57:53 +0800972 Py_DECREF(dict);
Feng Tang6a6daec2012-08-08 17:57:51 +0800973 Py_DECREF(t);
974}
975
Adrian Hunterdf919b42014-10-23 13:45:14 +0300976static void python_process_event(union perf_event *event,
Feng Tang6a6daec2012-08-08 17:57:51 +0800977 struct perf_sample *sample,
978 struct perf_evsel *evsel,
Feng Tang73994dc2012-08-08 17:57:52 +0800979 struct addr_location *al)
Feng Tang6a6daec2012-08-08 17:57:51 +0800980{
Adrian Hunterdf919b42014-10-23 13:45:14 +0300981 struct tables *tables = &tables_global;
982
Feng Tang6a6daec2012-08-08 17:57:51 +0800983 switch (evsel->attr.type) {
984 case PERF_TYPE_TRACEPOINT:
Arnaldo Carvalho de Melof9d5d542015-04-01 13:29:25 -0300985 python_process_tracepoint(sample, evsel, al);
Feng Tang6a6daec2012-08-08 17:57:51 +0800986 break;
987 /* Reserve for future process_hw/sw/raw APIs */
988 default:
Adrian Hunterdf919b42014-10-23 13:45:14 +0300989 if (tables->db_export_mode)
Arnaldo Carvalho de Melo73272592015-04-02 11:08:30 -0300990 db_export__sample(&tables->dbe, event, sample, evsel, al);
Adrian Hunterdf919b42014-10-23 13:45:14 +0300991 else
Arnaldo Carvalho de Melof9d5d542015-04-01 13:29:25 -0300992 python_process_general_event(sample, evsel, al);
Feng Tang6a6daec2012-08-08 17:57:51 +0800993 }
994}
995
Jiri Olsaaef90262016-01-05 22:09:11 +0100996static void get_handler_name(char *str, size_t size,
997 struct perf_evsel *evsel)
998{
999 char *p = str;
1000
1001 scnprintf(str, size, "stat__%s", perf_evsel__name(evsel));
1002
1003 while ((p = strchr(p, ':'))) {
1004 *p = '_';
1005 p++;
1006 }
1007}
1008
1009static void
1010process_stat(struct perf_evsel *counter, int cpu, int thread, u64 tstamp,
1011 struct perf_counts_values *count)
1012{
1013 PyObject *handler, *t;
1014 static char handler_name[256];
1015 int n = 0;
1016
1017 t = PyTuple_New(MAX_FIELDS);
1018 if (!t)
1019 Py_FatalError("couldn't create Python tuple");
1020
1021 get_handler_name(handler_name, sizeof(handler_name),
1022 counter);
1023
1024 handler = get_handler(handler_name);
1025 if (!handler) {
1026 pr_debug("can't find python handler %s\n", handler_name);
1027 return;
1028 }
1029
1030 PyTuple_SetItem(t, n++, PyInt_FromLong(cpu));
1031 PyTuple_SetItem(t, n++, PyInt_FromLong(thread));
1032
1033 tuple_set_u64(t, n++, tstamp);
1034 tuple_set_u64(t, n++, count->val);
1035 tuple_set_u64(t, n++, count->ena);
1036 tuple_set_u64(t, n++, count->run);
1037
1038 if (_PyTuple_Resize(&t, n) == -1)
1039 Py_FatalError("error resizing Python tuple");
1040
1041 call_object(handler, t, handler_name);
1042
1043 Py_DECREF(t);
1044}
1045
1046static void python_process_stat(struct perf_stat_config *config,
1047 struct perf_evsel *counter, u64 tstamp)
1048{
1049 struct thread_map *threads = counter->threads;
1050 struct cpu_map *cpus = counter->cpus;
1051 int cpu, thread;
1052
1053 if (config->aggr_mode == AGGR_GLOBAL) {
1054 process_stat(counter, -1, -1, tstamp,
1055 &counter->counts->aggr);
1056 return;
1057 }
1058
1059 for (thread = 0; thread < threads->nr; thread++) {
1060 for (cpu = 0; cpu < cpus->nr; cpu++) {
1061 process_stat(counter, cpus->map[cpu],
1062 thread_map__pid(threads, thread), tstamp,
1063 perf_counts(counter->counts, cpu, thread));
1064 }
1065 }
1066}
1067
1068static void python_process_stat_interval(u64 tstamp)
1069{
1070 PyObject *handler, *t;
1071 static const char handler_name[] = "stat__interval";
1072 int n = 0;
1073
1074 t = PyTuple_New(MAX_FIELDS);
1075 if (!t)
1076 Py_FatalError("couldn't create Python tuple");
1077
1078 handler = get_handler(handler_name);
1079 if (!handler) {
1080 pr_debug("can't find python handler %s\n", handler_name);
1081 return;
1082 }
1083
1084 tuple_set_u64(t, n++, tstamp);
1085
1086 if (_PyTuple_Resize(&t, n) == -1)
1087 Py_FatalError("error resizing Python tuple");
1088
1089 call_object(handler, t, handler_name);
1090
1091 Py_DECREF(t);
1092}
1093
Tom Zanussi7e4b21b2010-01-27 02:27:57 -06001094static int run_start_sub(void)
1095{
Tom Zanussi7e4b21b2010-01-27 02:27:57 -06001096 main_module = PyImport_AddModule("__main__");
1097 if (main_module == NULL)
1098 return -1;
1099 Py_INCREF(main_module);
1100
1101 main_dict = PyModule_GetDict(main_module);
Adrian Huntera5563ed2014-07-31 09:01:01 +03001102 if (main_dict == NULL)
Tom Zanussi7e4b21b2010-01-27 02:27:57 -06001103 goto error;
Tom Zanussi7e4b21b2010-01-27 02:27:57 -06001104 Py_INCREF(main_dict);
1105
Adrian Huntera5563ed2014-07-31 09:01:01 +03001106 try_call_object("trace_begin", NULL);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -06001107
Adrian Huntera5563ed2014-07-31 09:01:01 +03001108 return 0;
Tom Zanussi7e4b21b2010-01-27 02:27:57 -06001109
Tom Zanussi7e4b21b2010-01-27 02:27:57 -06001110error:
1111 Py_XDECREF(main_dict);
1112 Py_XDECREF(main_module);
Adrian Huntera5563ed2014-07-31 09:01:01 +03001113 return -1;
Tom Zanussi7e4b21b2010-01-27 02:27:57 -06001114}
1115
Adrian Hunterdf919b42014-10-23 13:45:14 +03001116#define SET_TABLE_HANDLER_(name, handler_name, table_name) do { \
1117 tables->handler_name = get_handler(#table_name); \
1118 if (tables->handler_name) \
1119 tables->dbe.export_ ## name = python_export_ ## name; \
1120} while (0)
1121
1122#define SET_TABLE_HANDLER(name) \
1123 SET_TABLE_HANDLER_(name, name ## _handler, name ## _table)
1124
1125static void set_table_handlers(struct tables *tables)
1126{
1127 const char *perf_db_export_mode = "perf_db_export_mode";
Adrian Hunter6a703072014-10-30 16:09:47 +02001128 const char *perf_db_export_calls = "perf_db_export_calls";
Chris Phlipot2c15f5e2016-04-28 01:19:10 -07001129 const char *perf_db_export_callchains = "perf_db_export_callchains";
1130 PyObject *db_export_mode, *db_export_calls, *db_export_callchains;
Adrian Hunter6a703072014-10-30 16:09:47 +02001131 bool export_calls = false;
Chris Phlipot2c15f5e2016-04-28 01:19:10 -07001132 bool export_callchains = false;
Adrian Hunterdf919b42014-10-23 13:45:14 +03001133 int ret;
1134
1135 memset(tables, 0, sizeof(struct tables));
1136 if (db_export__init(&tables->dbe))
1137 Py_FatalError("failed to initialize export");
1138
1139 db_export_mode = PyDict_GetItemString(main_dict, perf_db_export_mode);
1140 if (!db_export_mode)
1141 return;
1142
1143 ret = PyObject_IsTrue(db_export_mode);
1144 if (ret == -1)
1145 handler_call_die(perf_db_export_mode);
1146 if (!ret)
1147 return;
1148
Chris Phlipot2c15f5e2016-04-28 01:19:10 -07001149 /* handle export calls */
Adrian Hunter6a703072014-10-30 16:09:47 +02001150 tables->dbe.crp = NULL;
1151 db_export_calls = PyDict_GetItemString(main_dict, perf_db_export_calls);
1152 if (db_export_calls) {
1153 ret = PyObject_IsTrue(db_export_calls);
1154 if (ret == -1)
1155 handler_call_die(perf_db_export_calls);
1156 export_calls = !!ret;
1157 }
1158
1159 if (export_calls) {
1160 tables->dbe.crp =
1161 call_return_processor__new(python_process_call_return,
1162 &tables->dbe);
1163 if (!tables->dbe.crp)
1164 Py_FatalError("failed to create calls processor");
1165 }
1166
Chris Phlipot2c15f5e2016-04-28 01:19:10 -07001167 /* handle export callchains */
1168 tables->dbe.cpr = NULL;
1169 db_export_callchains = PyDict_GetItemString(main_dict,
1170 perf_db_export_callchains);
1171 if (db_export_callchains) {
1172 ret = PyObject_IsTrue(db_export_callchains);
1173 if (ret == -1)
1174 handler_call_die(perf_db_export_callchains);
1175 export_callchains = !!ret;
1176 }
1177
1178 if (export_callchains) {
1179 /*
1180 * Attempt to use the call path root from the call return
1181 * processor, if the call return processor is in use. Otherwise,
1182 * we allocate a new call path root. This prevents exporting
1183 * duplicate call path ids when both are in use simultaniously.
1184 */
1185 if (tables->dbe.crp)
1186 tables->dbe.cpr = tables->dbe.crp->cpr;
1187 else
1188 tables->dbe.cpr = call_path_root__new();
1189
1190 if (!tables->dbe.cpr)
Chris Phlipotaff63342016-05-07 02:17:00 -07001191 Py_FatalError("failed to create call path root");
Chris Phlipot2c15f5e2016-04-28 01:19:10 -07001192 }
1193
Adrian Hunterdf919b42014-10-23 13:45:14 +03001194 tables->db_export_mode = true;
1195 /*
1196 * Reserve per symbol space for symbol->db_id via symbol__priv()
1197 */
1198 symbol_conf.priv_size = sizeof(u64);
1199
1200 SET_TABLE_HANDLER(evsel);
1201 SET_TABLE_HANDLER(machine);
1202 SET_TABLE_HANDLER(thread);
1203 SET_TABLE_HANDLER(comm);
1204 SET_TABLE_HANDLER(comm_thread);
1205 SET_TABLE_HANDLER(dso);
1206 SET_TABLE_HANDLER(symbol);
Adrian Hunterc29414f2014-10-30 16:09:44 +02001207 SET_TABLE_HANDLER(branch_type);
Adrian Hunterdf919b42014-10-23 13:45:14 +03001208 SET_TABLE_HANDLER(sample);
Adrian Hunter6a703072014-10-30 16:09:47 +02001209 SET_TABLE_HANDLER(call_path);
1210 SET_TABLE_HANDLER(call_return);
Adrian Hunterdf919b42014-10-23 13:45:14 +03001211}
1212
Tom Zanussi7e4b21b2010-01-27 02:27:57 -06001213/*
1214 * Start trace script
1215 */
1216static int python_start_script(const char *script, int argc, const char **argv)
1217{
Adrian Hunterdf919b42014-10-23 13:45:14 +03001218 struct tables *tables = &tables_global;
Tom Zanussi7e4b21b2010-01-27 02:27:57 -06001219 const char **command_line;
1220 char buf[PATH_MAX];
1221 int i, err = 0;
1222 FILE *fp;
1223
1224 command_line = malloc((argc + 1) * sizeof(const char *));
1225 command_line[0] = script;
1226 for (i = 1; i < argc + 1; i++)
1227 command_line[i] = argv[i - 1];
1228
1229 Py_Initialize();
1230
1231 initperf_trace_context();
1232
1233 PySys_SetArgv(argc + 1, (char **)command_line);
1234
1235 fp = fopen(script, "r");
1236 if (!fp) {
1237 sprintf(buf, "Can't open python script \"%s\"", script);
1238 perror(buf);
1239 err = -1;
1240 goto error;
1241 }
1242
1243 err = PyRun_SimpleFile(fp, script);
1244 if (err) {
1245 fprintf(stderr, "Error running python script %s\n", script);
1246 goto error;
1247 }
1248
1249 err = run_start_sub();
1250 if (err) {
1251 fprintf(stderr, "Error starting python script %s\n", script);
1252 goto error;
1253 }
1254
Adrian Hunterdf919b42014-10-23 13:45:14 +03001255 set_table_handlers(tables);
1256
Adrian Hunterc29414f2014-10-30 16:09:44 +02001257 if (tables->db_export_mode) {
1258 err = db_export__branch_types(&tables->dbe);
1259 if (err)
1260 goto error;
1261 }
1262
Colin Ian King979ac252016-03-01 23:46:20 +00001263 free(command_line);
1264
Tom Zanussi7e4b21b2010-01-27 02:27:57 -06001265 return err;
1266error:
1267 Py_Finalize();
1268 free(command_line);
1269
1270 return err;
1271}
1272
Adrian Hunterd445dd22014-08-15 22:08:37 +03001273static int python_flush_script(void)
1274{
Adrian Hunter758008b2014-10-30 16:09:48 +02001275 struct tables *tables = &tables_global;
1276
1277 return db_export__flush(&tables->dbe);
Adrian Hunterd445dd22014-08-15 22:08:37 +03001278}
1279
Tom Zanussi7e4b21b2010-01-27 02:27:57 -06001280/*
1281 * Stop trace script
1282 */
1283static int python_stop_script(void)
1284{
Adrian Hunterdf919b42014-10-23 13:45:14 +03001285 struct tables *tables = &tables_global;
1286
Adrian Huntera5563ed2014-07-31 09:01:01 +03001287 try_call_object("trace_end", NULL);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -06001288
Adrian Hunterdf919b42014-10-23 13:45:14 +03001289 db_export__exit(&tables->dbe);
1290
Tom Zanussi7e4b21b2010-01-27 02:27:57 -06001291 Py_XDECREF(main_dict);
1292 Py_XDECREF(main_module);
1293 Py_Finalize();
1294
Adrian Huntera5563ed2014-07-31 09:01:01 +03001295 return 0;
Tom Zanussi7e4b21b2010-01-27 02:27:57 -06001296}
1297
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03001298static int python_generate_script(struct pevent *pevent, const char *outfile)
Tom Zanussi7e4b21b2010-01-27 02:27:57 -06001299{
Steven Rostedtaaf045f2012-04-06 00:47:56 +02001300 struct event_format *event = NULL;
Tom Zanussi7e4b21b2010-01-27 02:27:57 -06001301 struct format_field *f;
1302 char fname[PATH_MAX];
1303 int not_first, count;
1304 FILE *ofp;
1305
1306 sprintf(fname, "%s.py", outfile);
1307 ofp = fopen(fname, "w");
1308 if (ofp == NULL) {
1309 fprintf(stderr, "couldn't open %s\n", fname);
1310 return -1;
1311 }
Ingo Molnar133dc4c2010-11-16 18:45:39 +01001312 fprintf(ofp, "# perf script event handlers, "
1313 "generated by perf script -g python\n");
Tom Zanussi7e4b21b2010-01-27 02:27:57 -06001314
1315 fprintf(ofp, "# Licensed under the terms of the GNU GPL"
1316 " License version 2\n\n");
1317
1318 fprintf(ofp, "# The common_* event handler fields are the most useful "
1319 "fields common to\n");
1320
1321 fprintf(ofp, "# all events. They don't necessarily correspond to "
1322 "the 'common_*' fields\n");
1323
1324 fprintf(ofp, "# in the format files. Those fields not available as "
1325 "handler params can\n");
1326
1327 fprintf(ofp, "# be retrieved using Python functions of the form "
1328 "common_*(context).\n");
1329
SeongJae Parkc76132d2017-05-30 20:18:23 +09001330 fprintf(ofp, "# See the perf-script-python Documentation for the list "
Tom Zanussi7e4b21b2010-01-27 02:27:57 -06001331 "of available functions.\n\n");
1332
1333 fprintf(ofp, "import os\n");
1334 fprintf(ofp, "import sys\n\n");
1335
1336 fprintf(ofp, "sys.path.append(os.environ['PERF_EXEC_PATH'] + \\\n");
1337 fprintf(ofp, "\t'/scripts/python/Perf-Trace-Util/lib/Perf/Trace')\n");
1338 fprintf(ofp, "\nfrom perf_trace_context import *\n");
1339 fprintf(ofp, "from Core import *\n\n\n");
1340
1341 fprintf(ofp, "def trace_begin():\n");
1342 fprintf(ofp, "\tprint \"in trace_begin\"\n\n");
1343
1344 fprintf(ofp, "def trace_end():\n");
1345 fprintf(ofp, "\tprint \"in trace_end\"\n\n");
1346
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03001347 while ((event = trace_find_next_event(pevent, event))) {
Tom Zanussi7e4b21b2010-01-27 02:27:57 -06001348 fprintf(ofp, "def %s__%s(", event->system, event->name);
1349 fprintf(ofp, "event_name, ");
1350 fprintf(ofp, "context, ");
1351 fprintf(ofp, "common_cpu,\n");
1352 fprintf(ofp, "\tcommon_secs, ");
1353 fprintf(ofp, "common_nsecs, ");
1354 fprintf(ofp, "common_pid, ");
1355 fprintf(ofp, "common_comm,\n\t");
Joseph Schuchart0f5f5bc2014-07-10 13:50:51 +02001356 fprintf(ofp, "common_callchain, ");
Tom Zanussi7e4b21b2010-01-27 02:27:57 -06001357
1358 not_first = 0;
1359 count = 0;
1360
1361 for (f = event->format.fields; f; f = f->next) {
1362 if (not_first++)
1363 fprintf(ofp, ", ");
1364 if (++count % 5 == 0)
1365 fprintf(ofp, "\n\t");
1366
1367 fprintf(ofp, "%s", f->name);
1368 }
Arun Kalyanasundarama6418602017-07-21 15:04:22 -07001369 if (not_first++)
1370 fprintf(ofp, ", ");
1371 if (++count % 5 == 0)
1372 fprintf(ofp, "\n\t\t");
1373 fprintf(ofp, "perf_sample_dict");
1374
Tom Zanussi7e4b21b2010-01-27 02:27:57 -06001375 fprintf(ofp, "):\n");
1376
1377 fprintf(ofp, "\t\tprint_header(event_name, common_cpu, "
1378 "common_secs, common_nsecs,\n\t\t\t"
1379 "common_pid, common_comm)\n\n");
1380
1381 fprintf(ofp, "\t\tprint \"");
1382
1383 not_first = 0;
1384 count = 0;
1385
1386 for (f = event->format.fields; f; f = f->next) {
1387 if (not_first++)
1388 fprintf(ofp, ", ");
1389 if (count && count % 3 == 0) {
1390 fprintf(ofp, "\" \\\n\t\t\"");
1391 }
1392 count++;
1393
1394 fprintf(ofp, "%s=", f->name);
1395 if (f->flags & FIELD_IS_STRING ||
1396 f->flags & FIELD_IS_FLAG ||
Namhyung Kime646fe72014-05-29 13:44:55 +09001397 f->flags & FIELD_IS_ARRAY ||
Tom Zanussi7e4b21b2010-01-27 02:27:57 -06001398 f->flags & FIELD_IS_SYMBOLIC)
1399 fprintf(ofp, "%%s");
1400 else if (f->flags & FIELD_IS_SIGNED)
1401 fprintf(ofp, "%%d");
1402 else
1403 fprintf(ofp, "%%u");
1404 }
1405
Joseph Schuchart0f5f5bc2014-07-10 13:50:51 +02001406 fprintf(ofp, "\" %% \\\n\t\t(");
Tom Zanussi7e4b21b2010-01-27 02:27:57 -06001407
1408 not_first = 0;
1409 count = 0;
1410
1411 for (f = event->format.fields; f; f = f->next) {
1412 if (not_first++)
1413 fprintf(ofp, ", ");
1414
1415 if (++count % 5 == 0)
1416 fprintf(ofp, "\n\t\t");
1417
1418 if (f->flags & FIELD_IS_FLAG) {
1419 if ((count - 1) % 5 != 0) {
1420 fprintf(ofp, "\n\t\t");
1421 count = 4;
1422 }
1423 fprintf(ofp, "flag_str(\"");
1424 fprintf(ofp, "%s__%s\", ", event->system,
1425 event->name);
1426 fprintf(ofp, "\"%s\", %s)", f->name,
1427 f->name);
1428 } else if (f->flags & FIELD_IS_SYMBOLIC) {
1429 if ((count - 1) % 5 != 0) {
1430 fprintf(ofp, "\n\t\t");
1431 count = 4;
1432 }
1433 fprintf(ofp, "symbol_str(\"");
1434 fprintf(ofp, "%s__%s\", ", event->system,
1435 event->name);
1436 fprintf(ofp, "\"%s\", %s)", f->name,
1437 f->name);
1438 } else
1439 fprintf(ofp, "%s", f->name);
1440 }
1441
Joseph Schuchart0f5f5bc2014-07-10 13:50:51 +02001442 fprintf(ofp, ")\n\n");
1443
Arun Kalyanasundarama6418602017-07-21 15:04:22 -07001444 fprintf(ofp, "\t\tprint 'Sample: {'+"
1445 "get_dict_as_string(perf_sample_dict['sample'], ', ')+'}'\n\n");
1446
Joseph Schuchart0f5f5bc2014-07-10 13:50:51 +02001447 fprintf(ofp, "\t\tfor node in common_callchain:");
1448 fprintf(ofp, "\n\t\t\tif 'sym' in node:");
1449 fprintf(ofp, "\n\t\t\t\tprint \"\\t[%%x] %%s\" %% (node['ip'], node['sym']['name'])");
1450 fprintf(ofp, "\n\t\t\telse:");
1451 fprintf(ofp, "\n\t\t\t\tprint \"\t[%%x]\" %% (node['ip'])\n\n");
1452 fprintf(ofp, "\t\tprint \"\\n\"\n\n");
1453
Tom Zanussi7e4b21b2010-01-27 02:27:57 -06001454 }
1455
1456 fprintf(ofp, "def trace_unhandled(event_name, context, "
Arun Kalyanasundarama6418602017-07-21 15:04:22 -07001457 "event_fields_dict, perf_sample_dict):\n");
Tom Zanussi7e4b21b2010-01-27 02:27:57 -06001458
Arun Kalyanasundarama6418602017-07-21 15:04:22 -07001459 fprintf(ofp, "\t\tprint get_dict_as_string(event_fields_dict)\n");
1460 fprintf(ofp, "\t\tprint 'Sample: {'+"
1461 "get_dict_as_string(perf_sample_dict['sample'], ', ')+'}'\n\n");
Tom Zanussi7e4b21b2010-01-27 02:27:57 -06001462
1463 fprintf(ofp, "def print_header("
1464 "event_name, cpu, secs, nsecs, pid, comm):\n"
1465 "\tprint \"%%-20s %%5u %%05u.%%09u %%8u %%-20s \" %% \\\n\t"
Arun Kalyanasundarama6418602017-07-21 15:04:22 -07001466 "(event_name, cpu, secs, nsecs, pid, comm),\n\n");
1467
1468 fprintf(ofp, "def get_dict_as_string(a_dict, delimiter=' '):\n"
1469 "\treturn delimiter.join"
1470 "(['%%s=%%s'%%(k,str(v))for k,v in sorted(a_dict.items())])\n");
Tom Zanussi7e4b21b2010-01-27 02:27:57 -06001471
1472 fclose(ofp);
1473
1474 fprintf(stderr, "generated Python script: %s\n", fname);
1475
1476 return 0;
1477}
1478
1479struct scripting_ops python_scripting_ops = {
Jiri Olsaaef90262016-01-05 22:09:11 +01001480 .name = "Python",
1481 .start_script = python_start_script,
1482 .flush_script = python_flush_script,
1483 .stop_script = python_stop_script,
1484 .process_event = python_process_event,
1485 .process_stat = python_process_stat,
1486 .process_stat_interval = python_process_stat_interval,
1487 .generate_script = python_generate_script,
Tom Zanussi7e4b21b2010-01-27 02:27:57 -06001488};