blob: 8a8f4829d3e2932dec979856a201f8271c105554 [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"
Joseph Schuchart0f5f5bc2014-07-10 13:50:51 +020046#include "../machine.h"
Chris Phlipot451db122016-04-28 01:19:07 -070047#include "../call-path.h"
Jiri Olsaaef90262016-01-05 22:09:11 +010048#include "thread_map.h"
49#include "cpumap.h"
Arnaldo Carvalho de Melofea01392017-04-17 16:23:22 -030050#include "print_binary.h"
Jiri Olsaaef90262016-01-05 22:09:11 +010051#include "stat.h"
Tom Zanussi7e4b21b2010-01-27 02:27:57 -060052
53PyMODINIT_FUNC initperf_trace_context(void);
54
Steven Rostedt (Red Hat)609a7402015-05-13 13:44:36 -040055#define TRACE_EVENT_TYPE_MAX \
Tom Zanussi7e4b21b2010-01-27 02:27:57 -060056 ((1 << (sizeof(unsigned short) * 8)) - 1)
57
Steven Rostedt (Red Hat)609a7402015-05-13 13:44:36 -040058static DECLARE_BITMAP(events_defined, TRACE_EVENT_TYPE_MAX);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -060059
60#define MAX_FIELDS 64
61#define N_COMMON_FIELDS 7
62
63extern struct scripting_context *scripting_context;
64
65static char *cur_field_name;
66static int zero_flag_atom;
67
68static PyObject *main_module, *main_dict;
69
Adrian Hunterdf919b42014-10-23 13:45:14 +030070struct tables {
71 struct db_export dbe;
72 PyObject *evsel_handler;
73 PyObject *machine_handler;
74 PyObject *thread_handler;
75 PyObject *comm_handler;
76 PyObject *comm_thread_handler;
77 PyObject *dso_handler;
78 PyObject *symbol_handler;
Adrian Hunterc29414f2014-10-30 16:09:44 +020079 PyObject *branch_type_handler;
Adrian Hunterdf919b42014-10-23 13:45:14 +030080 PyObject *sample_handler;
Adrian Hunter6a703072014-10-30 16:09:47 +020081 PyObject *call_path_handler;
82 PyObject *call_return_handler;
Adrian Hunterdf919b42014-10-23 13:45:14 +030083 bool db_export_mode;
84};
85
86static struct tables tables_global;
87
Arnaldo Carvalho de Melo6c346642017-06-16 11:39:15 -030088static void handler_call_die(const char *handler_name) __noreturn;
Tom Zanussi7e4b21b2010-01-27 02:27:57 -060089static void handler_call_die(const char *handler_name)
90{
91 PyErr_Print();
92 Py_FatalError("problem in Python trace event handler");
Joseph Schuchart05f832e2014-07-09 16:16:31 +020093 // Py_FatalError does not return
94 // but we have to make the compiler happy
95 abort();
Tom Zanussi7e4b21b2010-01-27 02:27:57 -060096}
97
Joseph Schuchartc0268e82013-10-24 10:10:51 -030098/*
99 * Insert val into into the dictionary and decrement the reference counter.
Arnaldo Carvalho de Melo48000a12014-12-17 17:24:45 -0300100 * This is necessary for dictionaries since PyDict_SetItemString() does not
Joseph Schuchartc0268e82013-10-24 10:10:51 -0300101 * steal a reference, as opposed to PyTuple_SetItem().
102 */
103static void pydict_set_item_string_decref(PyObject *dict, const char *key, PyObject *val)
104{
105 PyDict_SetItemString(dict, key, val);
106 Py_DECREF(val);
107}
108
Adrian Huntera5563ed2014-07-31 09:01:01 +0300109static PyObject *get_handler(const char *handler_name)
110{
111 PyObject *handler;
112
113 handler = PyDict_GetItemString(main_dict, handler_name);
114 if (handler && !PyCallable_Check(handler))
115 return NULL;
116 return handler;
117}
118
119static void call_object(PyObject *handler, PyObject *args, const char *die_msg)
120{
121 PyObject *retval;
122
123 retval = PyObject_CallObject(handler, args);
124 if (retval == NULL)
125 handler_call_die(die_msg);
126 Py_DECREF(retval);
127}
128
129static void try_call_object(const char *handler_name, PyObject *args)
130{
131 PyObject *handler;
132
133 handler = get_handler(handler_name);
134 if (handler)
135 call_object(handler, args, handler_name);
136}
137
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600138static void define_value(enum print_arg_type field_type,
139 const char *ev_name,
140 const char *field_name,
141 const char *field_value,
142 const char *field_str)
143{
144 const char *handler_name = "define_flag_value";
Adrian Huntera5563ed2014-07-31 09:01:01 +0300145 PyObject *t;
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600146 unsigned long long value;
147 unsigned n = 0;
148
149 if (field_type == PRINT_SYMBOL)
150 handler_name = "define_symbolic_value";
151
Tom Zanussi44ad9cd2010-02-22 01:12:59 -0600152 t = PyTuple_New(4);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600153 if (!t)
154 Py_FatalError("couldn't create Python tuple");
155
156 value = eval_flag(field_value);
157
158 PyTuple_SetItem(t, n++, PyString_FromString(ev_name));
159 PyTuple_SetItem(t, n++, PyString_FromString(field_name));
160 PyTuple_SetItem(t, n++, PyInt_FromLong(value));
161 PyTuple_SetItem(t, n++, PyString_FromString(field_str));
162
Adrian Huntera5563ed2014-07-31 09:01:01 +0300163 try_call_object(handler_name, t);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600164
165 Py_DECREF(t);
166}
167
168static void define_values(enum print_arg_type field_type,
169 struct print_flag_sym *field,
170 const char *ev_name,
171 const char *field_name)
172{
173 define_value(field_type, ev_name, field_name, field->value,
174 field->str);
175
176 if (field->next)
177 define_values(field_type, field->next, ev_name, field_name);
178}
179
180static void define_field(enum print_arg_type field_type,
181 const char *ev_name,
182 const char *field_name,
183 const char *delim)
184{
185 const char *handler_name = "define_flag_field";
Adrian Huntera5563ed2014-07-31 09:01:01 +0300186 PyObject *t;
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600187 unsigned n = 0;
188
189 if (field_type == PRINT_SYMBOL)
190 handler_name = "define_symbolic_field";
191
Tom Zanussi44ad9cd2010-02-22 01:12:59 -0600192 if (field_type == PRINT_FLAGS)
193 t = PyTuple_New(3);
194 else
195 t = PyTuple_New(2);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600196 if (!t)
197 Py_FatalError("couldn't create Python tuple");
198
199 PyTuple_SetItem(t, n++, PyString_FromString(ev_name));
200 PyTuple_SetItem(t, n++, PyString_FromString(field_name));
201 if (field_type == PRINT_FLAGS)
202 PyTuple_SetItem(t, n++, PyString_FromString(delim));
203
Adrian Huntera5563ed2014-07-31 09:01:01 +0300204 try_call_object(handler_name, t);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600205
206 Py_DECREF(t);
207}
208
Steven Rostedtaaf045f2012-04-06 00:47:56 +0200209static void define_event_symbols(struct event_format *event,
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600210 const char *ev_name,
211 struct print_arg *args)
212{
Taeung Song8579aca2016-02-26 00:12:59 +0900213 if (args == NULL)
214 return;
215
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600216 switch (args->type) {
217 case PRINT_NULL:
218 break;
219 case PRINT_ATOM:
220 define_value(PRINT_FLAGS, ev_name, cur_field_name, "0",
221 args->atom.atom);
222 zero_flag_atom = 0;
223 break;
224 case PRINT_FIELD:
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -0300225 free(cur_field_name);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600226 cur_field_name = strdup(args->field.name);
227 break;
228 case PRINT_FLAGS:
229 define_event_symbols(event, ev_name, args->flags.field);
230 define_field(PRINT_FLAGS, ev_name, cur_field_name,
231 args->flags.delim);
232 define_values(PRINT_FLAGS, args->flags.flags, ev_name,
233 cur_field_name);
234 break;
235 case PRINT_SYMBOL:
236 define_event_symbols(event, ev_name, args->symbol.field);
237 define_field(PRINT_SYMBOL, ev_name, cur_field_name, NULL);
238 define_values(PRINT_SYMBOL, args->symbol.symbols, ev_name,
239 cur_field_name);
240 break;
Namhyung Kime080e6f2012-06-27 09:41:41 +0900241 case PRINT_HEX:
Daniel Borkmann0fe05592017-01-25 02:28:17 +0100242 case PRINT_HEX_STR:
Namhyung Kime080e6f2012-06-27 09:41:41 +0900243 define_event_symbols(event, ev_name, args->hex.field);
244 define_event_symbols(event, ev_name, args->hex.size);
245 break;
Javi Merinob839e1e82015-03-24 11:07:19 +0000246 case PRINT_INT_ARRAY:
247 define_event_symbols(event, ev_name, args->int_array.field);
248 define_event_symbols(event, ev_name, args->int_array.count);
249 define_event_symbols(event, ev_name, args->int_array.el_size);
250 break;
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600251 case PRINT_STRING:
252 break;
253 case PRINT_TYPE:
254 define_event_symbols(event, ev_name, args->typecast.item);
255 break;
256 case PRINT_OP:
257 if (strcmp(args->op.op, ":") == 0)
258 zero_flag_atom = 1;
259 define_event_symbols(event, ev_name, args->op.left);
260 define_event_symbols(event, ev_name, args->op.right);
261 break;
262 default:
Steven Rostedtaaf045f2012-04-06 00:47:56 +0200263 /* gcc warns for these? */
264 case PRINT_BSTRING:
265 case PRINT_DYNAMIC_ARRAY:
He Kuang76055942015-08-29 04:22:05 +0000266 case PRINT_DYNAMIC_ARRAY_LEN:
Steven Rostedtaaf045f2012-04-06 00:47:56 +0200267 case PRINT_FUNC:
Steven Rostedt (Red Hat)473a7782014-06-02 23:20:16 -0400268 case PRINT_BITMASK:
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600269 /* we should warn... */
270 return;
271 }
272
273 if (args->next)
274 define_event_symbols(event, ev_name, args->next);
275}
276
Sebastian Andrzej Siewior33058b92014-05-27 18:14:33 +0200277static PyObject *get_field_numeric_entry(struct event_format *event,
278 struct format_field *field, void *data)
279{
Sebastian Andrzej Siewior8ac631c2014-05-27 18:14:34 +0200280 bool is_array = field->flags & FIELD_IS_ARRAY;
Arnaldo Carvalho de Melo39f54862016-07-12 11:05:26 -0300281 PyObject *obj = NULL, *list = NULL;
Sebastian Andrzej Siewior33058b92014-05-27 18:14:33 +0200282 unsigned long long val;
Sebastian Andrzej Siewior8ac631c2014-05-27 18:14:34 +0200283 unsigned int item_size, n_items, i;
Sebastian Andrzej Siewior33058b92014-05-27 18:14:33 +0200284
Sebastian Andrzej Siewior8ac631c2014-05-27 18:14:34 +0200285 if (is_array) {
286 list = PyList_New(field->arraylen);
287 item_size = field->size / field->arraylen;
288 n_items = field->arraylen;
Sebastian Andrzej Siewior33058b92014-05-27 18:14:33 +0200289 } else {
Sebastian Andrzej Siewior8ac631c2014-05-27 18:14:34 +0200290 item_size = field->size;
291 n_items = 1;
Sebastian Andrzej Siewior33058b92014-05-27 18:14:33 +0200292 }
Sebastian Andrzej Siewior8ac631c2014-05-27 18:14:34 +0200293
294 for (i = 0; i < n_items; i++) {
295
296 val = read_size(event, data + field->offset + i * item_size,
297 item_size);
298 if (field->flags & FIELD_IS_SIGNED) {
299 if ((long long)val >= LONG_MIN &&
300 (long long)val <= LONG_MAX)
301 obj = PyInt_FromLong(val);
302 else
303 obj = PyLong_FromLongLong(val);
304 } else {
305 if (val <= LONG_MAX)
306 obj = PyInt_FromLong(val);
307 else
308 obj = PyLong_FromUnsignedLongLong(val);
309 }
310 if (is_array)
311 PyList_SET_ITEM(list, i, obj);
312 }
313 if (is_array)
314 obj = list;
Sebastian Andrzej Siewior33058b92014-05-27 18:14:33 +0200315 return obj;
316}
317
Joseph Schuchart0f5f5bc2014-07-10 13:50:51 +0200318
319static PyObject *python_process_callchain(struct perf_sample *sample,
320 struct perf_evsel *evsel,
321 struct addr_location *al)
322{
323 PyObject *pylist;
324
325 pylist = PyList_New(0);
326 if (!pylist)
327 Py_FatalError("couldn't create Python list");
328
329 if (!symbol_conf.use_callchain || !sample->callchain)
330 goto exit;
331
Arnaldo Carvalho de Melo91d7b2d2016-04-14 14:48:07 -0300332 if (thread__resolve_callchain(al->thread, &callchain_cursor, evsel,
Arnaldo Carvalho de Melocc8b7c22014-10-23 15:26:17 -0300333 sample, NULL, NULL,
Adrian Hunter44cbe722015-09-25 16:15:50 +0300334 scripting_max_stack) != 0) {
Joseph Schuchart0f5f5bc2014-07-10 13:50:51 +0200335 pr_err("Failed to resolve callchain. Skipping\n");
336 goto exit;
337 }
338 callchain_cursor_commit(&callchain_cursor);
339
340
341 while (1) {
342 PyObject *pyelem;
343 struct callchain_cursor_node *node;
344 node = callchain_cursor_current(&callchain_cursor);
345 if (!node)
346 break;
347
348 pyelem = PyDict_New();
349 if (!pyelem)
350 Py_FatalError("couldn't create Python dictionary");
351
352
353 pydict_set_item_string_decref(pyelem, "ip",
354 PyLong_FromUnsignedLongLong(node->ip));
355
356 if (node->sym) {
357 PyObject *pysym = PyDict_New();
358 if (!pysym)
359 Py_FatalError("couldn't create Python dictionary");
360 pydict_set_item_string_decref(pysym, "start",
361 PyLong_FromUnsignedLongLong(node->sym->start));
362 pydict_set_item_string_decref(pysym, "end",
363 PyLong_FromUnsignedLongLong(node->sym->end));
364 pydict_set_item_string_decref(pysym, "binding",
365 PyInt_FromLong(node->sym->binding));
366 pydict_set_item_string_decref(pysym, "name",
367 PyString_FromStringAndSize(node->sym->name,
368 node->sym->namelen));
369 pydict_set_item_string_decref(pyelem, "sym", pysym);
370 }
371
372 if (node->map) {
373 struct map *map = node->map;
374 const char *dsoname = "[unknown]";
Arnaldo Carvalho de Melo8bd8c652017-02-15 21:31:40 -0300375 if (map && map->dso) {
Joseph Schuchart0f5f5bc2014-07-10 13:50:51 +0200376 if (symbol_conf.show_kernel_path && map->dso->long_name)
377 dsoname = map->dso->long_name;
Arnaldo Carvalho de Melo8bd8c652017-02-15 21:31:40 -0300378 else
Joseph Schuchart0f5f5bc2014-07-10 13:50:51 +0200379 dsoname = map->dso->name;
380 }
381 pydict_set_item_string_decref(pyelem, "dso",
382 PyString_FromString(dsoname));
383 }
384
385 callchain_cursor_advance(&callchain_cursor);
386 PyList_Append(pylist, pyelem);
387 Py_DECREF(pyelem);
388 }
389
390exit:
391 return pylist;
392}
393
Arnaldo Carvalho de Melob7fff6b52013-12-19 16:34:52 -0300394static void python_process_tracepoint(struct perf_sample *sample,
395 struct perf_evsel *evsel,
Arnaldo Carvalho de Melob7fff6b52013-12-19 16:34:52 -0300396 struct addr_location *al)
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600397{
Jiri Olsaadf5bcf2014-10-26 23:44:05 +0100398 struct event_format *event = evsel->tp_format;
Arnaldo Carvalho de Melo39f54862016-07-12 11:05:26 -0300399 PyObject *handler, *context, *t, *obj = NULL, *callchain;
Joseph Schuchart0f5f5bc2014-07-10 13:50:51 +0200400 PyObject *dict = NULL;
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600401 static char handler_name[256];
402 struct format_field *field;
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600403 unsigned long s, ns;
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600404 unsigned n = 0;
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600405 int pid;
David Ahernbe6d8422011-03-09 22:23:23 -0700406 int cpu = sample->cpu;
407 void *data = sample->raw_data;
408 unsigned long long nsecs = sample->time;
Arnaldo Carvalho de Melof9d5d542015-04-01 13:29:25 -0300409 const char *comm = thread__comm_str(al->thread);
Arun Kalyanasundarame9f9a9c2017-07-21 15:04:18 -0700410 const char *default_handler_name = "trace_unhandled";
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600411
Arnaldo Carvalho de Melo62665df2016-05-10 12:33:52 -0300412 if (!event) {
413 snprintf(handler_name, sizeof(handler_name),
414 "ug! no event found for type %" PRIu64, (u64)evsel->attr.config);
415 Py_FatalError(handler_name);
416 }
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600417
Arnaldo Carvalho de Melo97822432012-08-07 23:50:21 -0300418 pid = raw_field_value(event, "common_pid", data);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600419
420 sprintf(handler_name, "%s__%s", event->system, event->name);
421
Jiri Olsaadf5bcf2014-10-26 23:44:05 +0100422 if (!test_and_set_bit(event->id, events_defined))
423 define_event_symbols(event, handler_name, event->print_fmt.args);
424
Adrian Huntera5563ed2014-07-31 09:01:01 +0300425 handler = get_handler(handler_name);
Pierre Tardyc0251482010-05-31 23:12:09 +0200426 if (!handler) {
Arun Kalyanasundarame9f9a9c2017-07-21 15:04:18 -0700427 handler = get_handler(default_handler_name);
428 if (!handler)
429 return;
Pierre Tardyc0251482010-05-31 23:12:09 +0200430 dict = PyDict_New();
431 if (!dict)
432 Py_FatalError("couldn't create Python dict");
433 }
Arun Kalyanasundarame9f9a9c2017-07-21 15:04:18 -0700434
435 t = PyTuple_New(MAX_FIELDS);
436 if (!t)
437 Py_FatalError("couldn't create Python tuple");
438
439
Arnaldo Carvalho de Melobd48c632016-08-05 15:40:30 -0300440 s = nsecs / NSEC_PER_SEC;
441 ns = nsecs - s * NSEC_PER_SEC;
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600442
443 scripting_context->event_data = data;
Tom Zanussi2de95332013-01-18 13:51:27 -0600444 scripting_context->pevent = evsel->tp_format->pevent;
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600445
446 context = PyCObject_FromVoidPtr(scripting_context, NULL);
447
448 PyTuple_SetItem(t, n++, PyString_FromString(handler_name));
Kyle McMartinfb7d0b32011-01-24 11:13:04 -0500449 PyTuple_SetItem(t, n++, context);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600450
Joseph Schuchart0f5f5bc2014-07-10 13:50:51 +0200451 /* ip unwinding */
452 callchain = python_process_callchain(sample, evsel, al);
453
Arun Kalyanasundarame9f9a9c2017-07-21 15:04:18 -0700454 if (!dict) {
Pierre Tardyc0251482010-05-31 23:12:09 +0200455 PyTuple_SetItem(t, n++, PyInt_FromLong(cpu));
456 PyTuple_SetItem(t, n++, PyInt_FromLong(s));
457 PyTuple_SetItem(t, n++, PyInt_FromLong(ns));
458 PyTuple_SetItem(t, n++, PyInt_FromLong(pid));
459 PyTuple_SetItem(t, n++, PyString_FromString(comm));
Joseph Schuchart0f5f5bc2014-07-10 13:50:51 +0200460 PyTuple_SetItem(t, n++, callchain);
Pierre Tardyc0251482010-05-31 23:12:09 +0200461 } else {
Joseph Schuchartc0268e82013-10-24 10:10:51 -0300462 pydict_set_item_string_decref(dict, "common_cpu", PyInt_FromLong(cpu));
463 pydict_set_item_string_decref(dict, "common_s", PyInt_FromLong(s));
464 pydict_set_item_string_decref(dict, "common_ns", PyInt_FromLong(ns));
465 pydict_set_item_string_decref(dict, "common_pid", PyInt_FromLong(pid));
466 pydict_set_item_string_decref(dict, "common_comm", PyString_FromString(comm));
Joseph Schuchart0f5f5bc2014-07-10 13:50:51 +0200467 pydict_set_item_string_decref(dict, "common_callchain", callchain);
Pierre Tardyc0251482010-05-31 23:12:09 +0200468 }
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600469 for (field = event->format.fields; field; field = field->next) {
Jiri Olsa249de6e2016-07-16 18:11:18 +0200470 unsigned int offset, len;
471 unsigned long long val;
472
473 if (field->flags & FIELD_IS_ARRAY) {
474 offset = field->offset;
475 len = field->size;
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600476 if (field->flags & FIELD_IS_DYNAMIC) {
Jiri Olsa249de6e2016-07-16 18:11:18 +0200477 val = pevent_read_number(scripting_context->pevent,
478 data + offset, len);
479 offset = val;
480 len = offset >> 16;
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600481 offset &= 0xffff;
Jiri Olsa249de6e2016-07-16 18:11:18 +0200482 }
483 if (field->flags & FIELD_IS_STRING &&
484 is_printable_array(data + offset, len)) {
485 obj = PyString_FromString((char *) data + offset);
486 } else {
487 obj = PyByteArray_FromStringAndSize((const char *) data + offset, len);
488 field->flags &= ~FIELD_IS_STRING;
489 }
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600490 } else { /* FIELD_IS_NUMERIC */
Sebastian Andrzej Siewior33058b92014-05-27 18:14:33 +0200491 obj = get_field_numeric_entry(event, field, data);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600492 }
Arun Kalyanasundarame9f9a9c2017-07-21 15:04:18 -0700493 if (!dict)
Pierre Tardyc0251482010-05-31 23:12:09 +0200494 PyTuple_SetItem(t, n++, obj);
495 else
Joseph Schuchartc0268e82013-10-24 10:10:51 -0300496 pydict_set_item_string_decref(dict, field->name, obj);
Pierre Tardyc0251482010-05-31 23:12:09 +0200497
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600498 }
Joseph Schuchart0f5f5bc2014-07-10 13:50:51 +0200499
Arun Kalyanasundarame9f9a9c2017-07-21 15:04:18 -0700500 if (dict)
Pierre Tardyc0251482010-05-31 23:12:09 +0200501 PyTuple_SetItem(t, n++, dict);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600502
503 if (_PyTuple_Resize(&t, n) == -1)
504 Py_FatalError("error resizing Python tuple");
505
Arun Kalyanasundarame9f9a9c2017-07-21 15:04:18 -0700506 if (!dict) {
Adrian Huntera5563ed2014-07-31 09:01:01 +0300507 call_object(handler, t, handler_name);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600508 } else {
Arun Kalyanasundarame9f9a9c2017-07-21 15:04:18 -0700509 call_object(handler, t, default_handler_name);
Pierre Tardyc0251482010-05-31 23:12:09 +0200510 Py_DECREF(dict);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600511 }
512
513 Py_DECREF(t);
514}
515
Adrian Hunterdf919b42014-10-23 13:45:14 +0300516static PyObject *tuple_new(unsigned int sz)
517{
518 PyObject *t;
519
520 t = PyTuple_New(sz);
521 if (!t)
522 Py_FatalError("couldn't create Python tuple");
523 return t;
524}
525
526static int tuple_set_u64(PyObject *t, unsigned int pos, u64 val)
527{
528#if BITS_PER_LONG == 64
529 return PyTuple_SetItem(t, pos, PyInt_FromLong(val));
530#endif
531#if BITS_PER_LONG == 32
532 return PyTuple_SetItem(t, pos, PyLong_FromLongLong(val));
533#endif
534}
535
536static int tuple_set_s32(PyObject *t, unsigned int pos, s32 val)
537{
538 return PyTuple_SetItem(t, pos, PyInt_FromLong(val));
539}
540
541static int tuple_set_string(PyObject *t, unsigned int pos, const char *s)
542{
543 return PyTuple_SetItem(t, pos, PyString_FromString(s));
544}
545
546static int python_export_evsel(struct db_export *dbe, struct perf_evsel *evsel)
547{
548 struct tables *tables = container_of(dbe, struct tables, dbe);
549 PyObject *t;
550
551 t = tuple_new(2);
552
553 tuple_set_u64(t, 0, evsel->db_id);
554 tuple_set_string(t, 1, perf_evsel__name(evsel));
555
556 call_object(tables->evsel_handler, t, "evsel_table");
557
558 Py_DECREF(t);
559
560 return 0;
561}
562
563static int python_export_machine(struct db_export *dbe,
564 struct machine *machine)
565{
566 struct tables *tables = container_of(dbe, struct tables, dbe);
567 PyObject *t;
568
569 t = tuple_new(3);
570
571 tuple_set_u64(t, 0, machine->db_id);
572 tuple_set_s32(t, 1, machine->pid);
573 tuple_set_string(t, 2, machine->root_dir ? machine->root_dir : "");
574
575 call_object(tables->machine_handler, t, "machine_table");
576
577 Py_DECREF(t);
578
579 return 0;
580}
581
582static int python_export_thread(struct db_export *dbe, struct thread *thread,
583 u64 main_thread_db_id, struct machine *machine)
584{
585 struct tables *tables = container_of(dbe, struct tables, dbe);
586 PyObject *t;
587
588 t = tuple_new(5);
589
590 tuple_set_u64(t, 0, thread->db_id);
591 tuple_set_u64(t, 1, machine->db_id);
592 tuple_set_u64(t, 2, main_thread_db_id);
593 tuple_set_s32(t, 3, thread->pid_);
594 tuple_set_s32(t, 4, thread->tid);
595
596 call_object(tables->thread_handler, t, "thread_table");
597
598 Py_DECREF(t);
599
600 return 0;
601}
602
603static int python_export_comm(struct db_export *dbe, struct comm *comm)
604{
605 struct tables *tables = container_of(dbe, struct tables, dbe);
606 PyObject *t;
607
608 t = tuple_new(2);
609
610 tuple_set_u64(t, 0, comm->db_id);
611 tuple_set_string(t, 1, comm__str(comm));
612
613 call_object(tables->comm_handler, t, "comm_table");
614
615 Py_DECREF(t);
616
617 return 0;
618}
619
620static int python_export_comm_thread(struct db_export *dbe, u64 db_id,
621 struct comm *comm, struct thread *thread)
622{
623 struct tables *tables = container_of(dbe, struct tables, dbe);
624 PyObject *t;
625
626 t = tuple_new(3);
627
628 tuple_set_u64(t, 0, db_id);
629 tuple_set_u64(t, 1, comm->db_id);
630 tuple_set_u64(t, 2, thread->db_id);
631
632 call_object(tables->comm_thread_handler, t, "comm_thread_table");
633
634 Py_DECREF(t);
635
636 return 0;
637}
638
639static int python_export_dso(struct db_export *dbe, struct dso *dso,
640 struct machine *machine)
641{
642 struct tables *tables = container_of(dbe, struct tables, dbe);
Masami Hiramatsub5d8bbe2016-05-11 22:51:59 +0900643 char sbuild_id[SBUILD_ID_SIZE];
Adrian Hunterdf919b42014-10-23 13:45:14 +0300644 PyObject *t;
645
646 build_id__sprintf(dso->build_id, sizeof(dso->build_id), sbuild_id);
647
648 t = tuple_new(5);
649
650 tuple_set_u64(t, 0, dso->db_id);
651 tuple_set_u64(t, 1, machine->db_id);
652 tuple_set_string(t, 2, dso->short_name);
653 tuple_set_string(t, 3, dso->long_name);
654 tuple_set_string(t, 4, sbuild_id);
655
656 call_object(tables->dso_handler, t, "dso_table");
657
658 Py_DECREF(t);
659
660 return 0;
661}
662
663static int python_export_symbol(struct db_export *dbe, struct symbol *sym,
664 struct dso *dso)
665{
666 struct tables *tables = container_of(dbe, struct tables, dbe);
667 u64 *sym_db_id = symbol__priv(sym);
668 PyObject *t;
669
670 t = tuple_new(6);
671
672 tuple_set_u64(t, 0, *sym_db_id);
673 tuple_set_u64(t, 1, dso->db_id);
674 tuple_set_u64(t, 2, sym->start);
675 tuple_set_u64(t, 3, sym->end);
676 tuple_set_s32(t, 4, sym->binding);
677 tuple_set_string(t, 5, sym->name);
678
679 call_object(tables->symbol_handler, t, "symbol_table");
680
681 Py_DECREF(t);
682
683 return 0;
684}
685
Adrian Hunterc29414f2014-10-30 16:09:44 +0200686static int python_export_branch_type(struct db_export *dbe, u32 branch_type,
687 const char *name)
688{
689 struct tables *tables = container_of(dbe, struct tables, dbe);
690 PyObject *t;
691
692 t = tuple_new(2);
693
694 tuple_set_s32(t, 0, branch_type);
695 tuple_set_string(t, 1, name);
696
697 call_object(tables->branch_type_handler, t, "branch_type_table");
698
699 Py_DECREF(t);
700
701 return 0;
702}
703
Adrian Hunterdf919b42014-10-23 13:45:14 +0300704static int python_export_sample(struct db_export *dbe,
705 struct export_sample *es)
706{
707 struct tables *tables = container_of(dbe, struct tables, dbe);
708 PyObject *t;
709
Chris Phlipot2c15f5e2016-04-28 01:19:10 -0700710 t = tuple_new(22);
Adrian Hunterdf919b42014-10-23 13:45:14 +0300711
712 tuple_set_u64(t, 0, es->db_id);
713 tuple_set_u64(t, 1, es->evsel->db_id);
714 tuple_set_u64(t, 2, es->al->machine->db_id);
Arnaldo Carvalho de Melob83e8682015-04-02 11:16:05 -0300715 tuple_set_u64(t, 3, es->al->thread->db_id);
Adrian Hunterdf919b42014-10-23 13:45:14 +0300716 tuple_set_u64(t, 4, es->comm_db_id);
717 tuple_set_u64(t, 5, es->dso_db_id);
718 tuple_set_u64(t, 6, es->sym_db_id);
719 tuple_set_u64(t, 7, es->offset);
720 tuple_set_u64(t, 8, es->sample->ip);
721 tuple_set_u64(t, 9, es->sample->time);
722 tuple_set_s32(t, 10, es->sample->cpu);
723 tuple_set_u64(t, 11, es->addr_dso_db_id);
724 tuple_set_u64(t, 12, es->addr_sym_db_id);
725 tuple_set_u64(t, 13, es->addr_offset);
726 tuple_set_u64(t, 14, es->sample->addr);
727 tuple_set_u64(t, 15, es->sample->period);
728 tuple_set_u64(t, 16, es->sample->weight);
729 tuple_set_u64(t, 17, es->sample->transaction);
730 tuple_set_u64(t, 18, es->sample->data_src);
Adrian Hunterc29414f2014-10-30 16:09:44 +0200731 tuple_set_s32(t, 19, es->sample->flags & PERF_BRANCH_MASK);
732 tuple_set_s32(t, 20, !!(es->sample->flags & PERF_IP_FLAG_IN_TX));
Chris Phlipot2c15f5e2016-04-28 01:19:10 -0700733 tuple_set_u64(t, 21, es->call_path_id);
Adrian Hunterdf919b42014-10-23 13:45:14 +0300734
735 call_object(tables->sample_handler, t, "sample_table");
736
737 Py_DECREF(t);
738
739 return 0;
740}
741
Adrian Hunter6a703072014-10-30 16:09:47 +0200742static int python_export_call_path(struct db_export *dbe, struct call_path *cp)
743{
744 struct tables *tables = container_of(dbe, struct tables, dbe);
745 PyObject *t;
746 u64 parent_db_id, sym_db_id;
747
748 parent_db_id = cp->parent ? cp->parent->db_id : 0;
749 sym_db_id = cp->sym ? *(u64 *)symbol__priv(cp->sym) : 0;
750
751 t = tuple_new(4);
752
753 tuple_set_u64(t, 0, cp->db_id);
754 tuple_set_u64(t, 1, parent_db_id);
755 tuple_set_u64(t, 2, sym_db_id);
756 tuple_set_u64(t, 3, cp->ip);
757
758 call_object(tables->call_path_handler, t, "call_path_table");
759
760 Py_DECREF(t);
761
762 return 0;
763}
764
765static int python_export_call_return(struct db_export *dbe,
766 struct call_return *cr)
767{
768 struct tables *tables = container_of(dbe, struct tables, dbe);
769 u64 comm_db_id = cr->comm ? cr->comm->db_id : 0;
770 PyObject *t;
771
772 t = tuple_new(11);
773
774 tuple_set_u64(t, 0, cr->db_id);
775 tuple_set_u64(t, 1, cr->thread->db_id);
776 tuple_set_u64(t, 2, comm_db_id);
777 tuple_set_u64(t, 3, cr->cp->db_id);
778 tuple_set_u64(t, 4, cr->call_time);
779 tuple_set_u64(t, 5, cr->return_time);
780 tuple_set_u64(t, 6, cr->branch_count);
781 tuple_set_u64(t, 7, cr->call_ref);
782 tuple_set_u64(t, 8, cr->return_ref);
783 tuple_set_u64(t, 9, cr->cp->parent->db_id);
784 tuple_set_s32(t, 10, cr->flags);
785
786 call_object(tables->call_return_handler, t, "call_return_table");
787
788 Py_DECREF(t);
789
790 return 0;
791}
792
793static int python_process_call_return(struct call_return *cr, void *data)
794{
795 struct db_export *dbe = data;
796
797 return db_export__call_return(dbe, cr);
798}
799
Arnaldo Carvalho de Melob7fff6b52013-12-19 16:34:52 -0300800static void python_process_general_event(struct perf_sample *sample,
Feng Tang6a6daec2012-08-08 17:57:51 +0800801 struct perf_evsel *evsel,
Feng Tang87b6a3a2012-08-09 13:46:13 +0800802 struct addr_location *al)
Feng Tang6a6daec2012-08-08 17:57:51 +0800803{
Adrian Huntera5563ed2014-07-31 09:01:01 +0300804 PyObject *handler, *t, *dict, *callchain, *dict_sample;
Feng Tang6a6daec2012-08-08 17:57:51 +0800805 static char handler_name[64];
806 unsigned n = 0;
Feng Tang6a6daec2012-08-08 17:57:51 +0800807
Arun Kalyanasundarame9f9a9c2017-07-21 15:04:18 -0700808 snprintf(handler_name, sizeof(handler_name), "%s", "process_event");
809
810 handler = get_handler(handler_name);
811 if (!handler)
812 return;
813
Feng Tangfd6b8582012-08-08 17:57:53 +0800814 /*
815 * Use the MAX_FIELDS to make the function expandable, though
Feng Tang87b6a3a2012-08-09 13:46:13 +0800816 * currently there is only one item for the tuple.
Feng Tangfd6b8582012-08-08 17:57:53 +0800817 */
Feng Tang6a6daec2012-08-08 17:57:51 +0800818 t = PyTuple_New(MAX_FIELDS);
819 if (!t)
820 Py_FatalError("couldn't create Python tuple");
821
Feng Tangfd6b8582012-08-08 17:57:53 +0800822 dict = PyDict_New();
823 if (!dict)
824 Py_FatalError("couldn't create Python dictionary");
825
Joseph Schuchart57608cf2014-07-10 13:50:56 +0200826 dict_sample = PyDict_New();
827 if (!dict_sample)
828 Py_FatalError("couldn't create Python dictionary");
829
Joseph Schuchartc0268e82013-10-24 10:10:51 -0300830 pydict_set_item_string_decref(dict, "ev_name", PyString_FromString(perf_evsel__name(evsel)));
831 pydict_set_item_string_decref(dict, "attr", PyString_FromStringAndSize(
Feng Tangfd6b8582012-08-08 17:57:53 +0800832 (const char *)&evsel->attr, sizeof(evsel->attr)));
Joseph Schuchart57608cf2014-07-10 13:50:56 +0200833
834 pydict_set_item_string_decref(dict_sample, "pid",
835 PyInt_FromLong(sample->pid));
836 pydict_set_item_string_decref(dict_sample, "tid",
837 PyInt_FromLong(sample->tid));
838 pydict_set_item_string_decref(dict_sample, "cpu",
839 PyInt_FromLong(sample->cpu));
840 pydict_set_item_string_decref(dict_sample, "ip",
841 PyLong_FromUnsignedLongLong(sample->ip));
842 pydict_set_item_string_decref(dict_sample, "time",
843 PyLong_FromUnsignedLongLong(sample->time));
844 pydict_set_item_string_decref(dict_sample, "period",
845 PyLong_FromUnsignedLongLong(sample->period));
846 pydict_set_item_string_decref(dict, "sample", dict_sample);
847
Joseph Schuchartc0268e82013-10-24 10:10:51 -0300848 pydict_set_item_string_decref(dict, "raw_buf", PyString_FromStringAndSize(
Feng Tangfd6b8582012-08-08 17:57:53 +0800849 (const char *)sample->raw_data, sample->raw_size));
Joseph Schuchartc0268e82013-10-24 10:10:51 -0300850 pydict_set_item_string_decref(dict, "comm",
Arnaldo Carvalho de Melof9d5d542015-04-01 13:29:25 -0300851 PyString_FromString(thread__comm_str(al->thread)));
Feng Tangfd6b8582012-08-08 17:57:53 +0800852 if (al->map) {
Joseph Schuchartc0268e82013-10-24 10:10:51 -0300853 pydict_set_item_string_decref(dict, "dso",
Feng Tangfd6b8582012-08-08 17:57:53 +0800854 PyString_FromString(al->map->dso->name));
855 }
856 if (al->sym) {
Joseph Schuchartc0268e82013-10-24 10:10:51 -0300857 pydict_set_item_string_decref(dict, "symbol",
Feng Tangfd6b8582012-08-08 17:57:53 +0800858 PyString_FromString(al->sym->name));
859 }
Feng Tang6a6daec2012-08-08 17:57:51 +0800860
Joseph Schuchart0f5f5bc2014-07-10 13:50:51 +0200861 /* ip unwinding */
862 callchain = python_process_callchain(sample, evsel, al);
863 pydict_set_item_string_decref(dict, "callchain", callchain);
864
Feng Tangfd6b8582012-08-08 17:57:53 +0800865 PyTuple_SetItem(t, n++, dict);
Feng Tang6a6daec2012-08-08 17:57:51 +0800866 if (_PyTuple_Resize(&t, n) == -1)
867 Py_FatalError("error resizing Python tuple");
868
Adrian Huntera5563ed2014-07-31 09:01:01 +0300869 call_object(handler, t, handler_name);
Arun Kalyanasundarame9f9a9c2017-07-21 15:04:18 -0700870
Feng Tangfd6b8582012-08-08 17:57:53 +0800871 Py_DECREF(dict);
Feng Tang6a6daec2012-08-08 17:57:51 +0800872 Py_DECREF(t);
873}
874
Adrian Hunterdf919b42014-10-23 13:45:14 +0300875static void python_process_event(union perf_event *event,
Feng Tang6a6daec2012-08-08 17:57:51 +0800876 struct perf_sample *sample,
877 struct perf_evsel *evsel,
Feng Tang73994dc2012-08-08 17:57:52 +0800878 struct addr_location *al)
Feng Tang6a6daec2012-08-08 17:57:51 +0800879{
Adrian Hunterdf919b42014-10-23 13:45:14 +0300880 struct tables *tables = &tables_global;
881
Feng Tang6a6daec2012-08-08 17:57:51 +0800882 switch (evsel->attr.type) {
883 case PERF_TYPE_TRACEPOINT:
Arnaldo Carvalho de Melof9d5d542015-04-01 13:29:25 -0300884 python_process_tracepoint(sample, evsel, al);
Feng Tang6a6daec2012-08-08 17:57:51 +0800885 break;
886 /* Reserve for future process_hw/sw/raw APIs */
887 default:
Adrian Hunterdf919b42014-10-23 13:45:14 +0300888 if (tables->db_export_mode)
Arnaldo Carvalho de Melo73272592015-04-02 11:08:30 -0300889 db_export__sample(&tables->dbe, event, sample, evsel, al);
Adrian Hunterdf919b42014-10-23 13:45:14 +0300890 else
Arnaldo Carvalho de Melof9d5d542015-04-01 13:29:25 -0300891 python_process_general_event(sample, evsel, al);
Feng Tang6a6daec2012-08-08 17:57:51 +0800892 }
893}
894
Jiri Olsaaef90262016-01-05 22:09:11 +0100895static void get_handler_name(char *str, size_t size,
896 struct perf_evsel *evsel)
897{
898 char *p = str;
899
900 scnprintf(str, size, "stat__%s", perf_evsel__name(evsel));
901
902 while ((p = strchr(p, ':'))) {
903 *p = '_';
904 p++;
905 }
906}
907
908static void
909process_stat(struct perf_evsel *counter, int cpu, int thread, u64 tstamp,
910 struct perf_counts_values *count)
911{
912 PyObject *handler, *t;
913 static char handler_name[256];
914 int n = 0;
915
916 t = PyTuple_New(MAX_FIELDS);
917 if (!t)
918 Py_FatalError("couldn't create Python tuple");
919
920 get_handler_name(handler_name, sizeof(handler_name),
921 counter);
922
923 handler = get_handler(handler_name);
924 if (!handler) {
925 pr_debug("can't find python handler %s\n", handler_name);
926 return;
927 }
928
929 PyTuple_SetItem(t, n++, PyInt_FromLong(cpu));
930 PyTuple_SetItem(t, n++, PyInt_FromLong(thread));
931
932 tuple_set_u64(t, n++, tstamp);
933 tuple_set_u64(t, n++, count->val);
934 tuple_set_u64(t, n++, count->ena);
935 tuple_set_u64(t, n++, count->run);
936
937 if (_PyTuple_Resize(&t, n) == -1)
938 Py_FatalError("error resizing Python tuple");
939
940 call_object(handler, t, handler_name);
941
942 Py_DECREF(t);
943}
944
945static void python_process_stat(struct perf_stat_config *config,
946 struct perf_evsel *counter, u64 tstamp)
947{
948 struct thread_map *threads = counter->threads;
949 struct cpu_map *cpus = counter->cpus;
950 int cpu, thread;
951
952 if (config->aggr_mode == AGGR_GLOBAL) {
953 process_stat(counter, -1, -1, tstamp,
954 &counter->counts->aggr);
955 return;
956 }
957
958 for (thread = 0; thread < threads->nr; thread++) {
959 for (cpu = 0; cpu < cpus->nr; cpu++) {
960 process_stat(counter, cpus->map[cpu],
961 thread_map__pid(threads, thread), tstamp,
962 perf_counts(counter->counts, cpu, thread));
963 }
964 }
965}
966
967static void python_process_stat_interval(u64 tstamp)
968{
969 PyObject *handler, *t;
970 static const char handler_name[] = "stat__interval";
971 int n = 0;
972
973 t = PyTuple_New(MAX_FIELDS);
974 if (!t)
975 Py_FatalError("couldn't create Python tuple");
976
977 handler = get_handler(handler_name);
978 if (!handler) {
979 pr_debug("can't find python handler %s\n", handler_name);
980 return;
981 }
982
983 tuple_set_u64(t, n++, tstamp);
984
985 if (_PyTuple_Resize(&t, n) == -1)
986 Py_FatalError("error resizing Python tuple");
987
988 call_object(handler, t, handler_name);
989
990 Py_DECREF(t);
991}
992
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600993static int run_start_sub(void)
994{
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600995 main_module = PyImport_AddModule("__main__");
996 if (main_module == NULL)
997 return -1;
998 Py_INCREF(main_module);
999
1000 main_dict = PyModule_GetDict(main_module);
Adrian Huntera5563ed2014-07-31 09:01:01 +03001001 if (main_dict == NULL)
Tom Zanussi7e4b21b2010-01-27 02:27:57 -06001002 goto error;
Tom Zanussi7e4b21b2010-01-27 02:27:57 -06001003 Py_INCREF(main_dict);
1004
Adrian Huntera5563ed2014-07-31 09:01:01 +03001005 try_call_object("trace_begin", NULL);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -06001006
Adrian Huntera5563ed2014-07-31 09:01:01 +03001007 return 0;
Tom Zanussi7e4b21b2010-01-27 02:27:57 -06001008
Tom Zanussi7e4b21b2010-01-27 02:27:57 -06001009error:
1010 Py_XDECREF(main_dict);
1011 Py_XDECREF(main_module);
Adrian Huntera5563ed2014-07-31 09:01:01 +03001012 return -1;
Tom Zanussi7e4b21b2010-01-27 02:27:57 -06001013}
1014
Adrian Hunterdf919b42014-10-23 13:45:14 +03001015#define SET_TABLE_HANDLER_(name, handler_name, table_name) do { \
1016 tables->handler_name = get_handler(#table_name); \
1017 if (tables->handler_name) \
1018 tables->dbe.export_ ## name = python_export_ ## name; \
1019} while (0)
1020
1021#define SET_TABLE_HANDLER(name) \
1022 SET_TABLE_HANDLER_(name, name ## _handler, name ## _table)
1023
1024static void set_table_handlers(struct tables *tables)
1025{
1026 const char *perf_db_export_mode = "perf_db_export_mode";
Adrian Hunter6a703072014-10-30 16:09:47 +02001027 const char *perf_db_export_calls = "perf_db_export_calls";
Chris Phlipot2c15f5e2016-04-28 01:19:10 -07001028 const char *perf_db_export_callchains = "perf_db_export_callchains";
1029 PyObject *db_export_mode, *db_export_calls, *db_export_callchains;
Adrian Hunter6a703072014-10-30 16:09:47 +02001030 bool export_calls = false;
Chris Phlipot2c15f5e2016-04-28 01:19:10 -07001031 bool export_callchains = false;
Adrian Hunterdf919b42014-10-23 13:45:14 +03001032 int ret;
1033
1034 memset(tables, 0, sizeof(struct tables));
1035 if (db_export__init(&tables->dbe))
1036 Py_FatalError("failed to initialize export");
1037
1038 db_export_mode = PyDict_GetItemString(main_dict, perf_db_export_mode);
1039 if (!db_export_mode)
1040 return;
1041
1042 ret = PyObject_IsTrue(db_export_mode);
1043 if (ret == -1)
1044 handler_call_die(perf_db_export_mode);
1045 if (!ret)
1046 return;
1047
Chris Phlipot2c15f5e2016-04-28 01:19:10 -07001048 /* handle export calls */
Adrian Hunter6a703072014-10-30 16:09:47 +02001049 tables->dbe.crp = NULL;
1050 db_export_calls = PyDict_GetItemString(main_dict, perf_db_export_calls);
1051 if (db_export_calls) {
1052 ret = PyObject_IsTrue(db_export_calls);
1053 if (ret == -1)
1054 handler_call_die(perf_db_export_calls);
1055 export_calls = !!ret;
1056 }
1057
1058 if (export_calls) {
1059 tables->dbe.crp =
1060 call_return_processor__new(python_process_call_return,
1061 &tables->dbe);
1062 if (!tables->dbe.crp)
1063 Py_FatalError("failed to create calls processor");
1064 }
1065
Chris Phlipot2c15f5e2016-04-28 01:19:10 -07001066 /* handle export callchains */
1067 tables->dbe.cpr = NULL;
1068 db_export_callchains = PyDict_GetItemString(main_dict,
1069 perf_db_export_callchains);
1070 if (db_export_callchains) {
1071 ret = PyObject_IsTrue(db_export_callchains);
1072 if (ret == -1)
1073 handler_call_die(perf_db_export_callchains);
1074 export_callchains = !!ret;
1075 }
1076
1077 if (export_callchains) {
1078 /*
1079 * Attempt to use the call path root from the call return
1080 * processor, if the call return processor is in use. Otherwise,
1081 * we allocate a new call path root. This prevents exporting
1082 * duplicate call path ids when both are in use simultaniously.
1083 */
1084 if (tables->dbe.crp)
1085 tables->dbe.cpr = tables->dbe.crp->cpr;
1086 else
1087 tables->dbe.cpr = call_path_root__new();
1088
1089 if (!tables->dbe.cpr)
Chris Phlipotaff63342016-05-07 02:17:00 -07001090 Py_FatalError("failed to create call path root");
Chris Phlipot2c15f5e2016-04-28 01:19:10 -07001091 }
1092
Adrian Hunterdf919b42014-10-23 13:45:14 +03001093 tables->db_export_mode = true;
1094 /*
1095 * Reserve per symbol space for symbol->db_id via symbol__priv()
1096 */
1097 symbol_conf.priv_size = sizeof(u64);
1098
1099 SET_TABLE_HANDLER(evsel);
1100 SET_TABLE_HANDLER(machine);
1101 SET_TABLE_HANDLER(thread);
1102 SET_TABLE_HANDLER(comm);
1103 SET_TABLE_HANDLER(comm_thread);
1104 SET_TABLE_HANDLER(dso);
1105 SET_TABLE_HANDLER(symbol);
Adrian Hunterc29414f2014-10-30 16:09:44 +02001106 SET_TABLE_HANDLER(branch_type);
Adrian Hunterdf919b42014-10-23 13:45:14 +03001107 SET_TABLE_HANDLER(sample);
Adrian Hunter6a703072014-10-30 16:09:47 +02001108 SET_TABLE_HANDLER(call_path);
1109 SET_TABLE_HANDLER(call_return);
Adrian Hunterdf919b42014-10-23 13:45:14 +03001110}
1111
Tom Zanussi7e4b21b2010-01-27 02:27:57 -06001112/*
1113 * Start trace script
1114 */
1115static int python_start_script(const char *script, int argc, const char **argv)
1116{
Adrian Hunterdf919b42014-10-23 13:45:14 +03001117 struct tables *tables = &tables_global;
Tom Zanussi7e4b21b2010-01-27 02:27:57 -06001118 const char **command_line;
1119 char buf[PATH_MAX];
1120 int i, err = 0;
1121 FILE *fp;
1122
1123 command_line = malloc((argc + 1) * sizeof(const char *));
1124 command_line[0] = script;
1125 for (i = 1; i < argc + 1; i++)
1126 command_line[i] = argv[i - 1];
1127
1128 Py_Initialize();
1129
1130 initperf_trace_context();
1131
1132 PySys_SetArgv(argc + 1, (char **)command_line);
1133
1134 fp = fopen(script, "r");
1135 if (!fp) {
1136 sprintf(buf, "Can't open python script \"%s\"", script);
1137 perror(buf);
1138 err = -1;
1139 goto error;
1140 }
1141
1142 err = PyRun_SimpleFile(fp, script);
1143 if (err) {
1144 fprintf(stderr, "Error running python script %s\n", script);
1145 goto error;
1146 }
1147
1148 err = run_start_sub();
1149 if (err) {
1150 fprintf(stderr, "Error starting python script %s\n", script);
1151 goto error;
1152 }
1153
Adrian Hunterdf919b42014-10-23 13:45:14 +03001154 set_table_handlers(tables);
1155
Adrian Hunterc29414f2014-10-30 16:09:44 +02001156 if (tables->db_export_mode) {
1157 err = db_export__branch_types(&tables->dbe);
1158 if (err)
1159 goto error;
1160 }
1161
Colin Ian King979ac252016-03-01 23:46:20 +00001162 free(command_line);
1163
Tom Zanussi7e4b21b2010-01-27 02:27:57 -06001164 return err;
1165error:
1166 Py_Finalize();
1167 free(command_line);
1168
1169 return err;
1170}
1171
Adrian Hunterd445dd22014-08-15 22:08:37 +03001172static int python_flush_script(void)
1173{
Adrian Hunter758008b2014-10-30 16:09:48 +02001174 struct tables *tables = &tables_global;
1175
1176 return db_export__flush(&tables->dbe);
Adrian Hunterd445dd22014-08-15 22:08:37 +03001177}
1178
Tom Zanussi7e4b21b2010-01-27 02:27:57 -06001179/*
1180 * Stop trace script
1181 */
1182static int python_stop_script(void)
1183{
Adrian Hunterdf919b42014-10-23 13:45:14 +03001184 struct tables *tables = &tables_global;
1185
Adrian Huntera5563ed2014-07-31 09:01:01 +03001186 try_call_object("trace_end", NULL);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -06001187
Adrian Hunterdf919b42014-10-23 13:45:14 +03001188 db_export__exit(&tables->dbe);
1189
Tom Zanussi7e4b21b2010-01-27 02:27:57 -06001190 Py_XDECREF(main_dict);
1191 Py_XDECREF(main_module);
1192 Py_Finalize();
1193
Adrian Huntera5563ed2014-07-31 09:01:01 +03001194 return 0;
Tom Zanussi7e4b21b2010-01-27 02:27:57 -06001195}
1196
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03001197static int python_generate_script(struct pevent *pevent, const char *outfile)
Tom Zanussi7e4b21b2010-01-27 02:27:57 -06001198{
Steven Rostedtaaf045f2012-04-06 00:47:56 +02001199 struct event_format *event = NULL;
Tom Zanussi7e4b21b2010-01-27 02:27:57 -06001200 struct format_field *f;
1201 char fname[PATH_MAX];
1202 int not_first, count;
1203 FILE *ofp;
1204
1205 sprintf(fname, "%s.py", outfile);
1206 ofp = fopen(fname, "w");
1207 if (ofp == NULL) {
1208 fprintf(stderr, "couldn't open %s\n", fname);
1209 return -1;
1210 }
Ingo Molnar133dc4c2010-11-16 18:45:39 +01001211 fprintf(ofp, "# perf script event handlers, "
1212 "generated by perf script -g python\n");
Tom Zanussi7e4b21b2010-01-27 02:27:57 -06001213
1214 fprintf(ofp, "# Licensed under the terms of the GNU GPL"
1215 " License version 2\n\n");
1216
1217 fprintf(ofp, "# The common_* event handler fields are the most useful "
1218 "fields common to\n");
1219
1220 fprintf(ofp, "# all events. They don't necessarily correspond to "
1221 "the 'common_*' fields\n");
1222
1223 fprintf(ofp, "# in the format files. Those fields not available as "
1224 "handler params can\n");
1225
1226 fprintf(ofp, "# be retrieved using Python functions of the form "
1227 "common_*(context).\n");
1228
SeongJae Parkc76132d2017-05-30 20:18:23 +09001229 fprintf(ofp, "# See the perf-script-python Documentation for the list "
Tom Zanussi7e4b21b2010-01-27 02:27:57 -06001230 "of available functions.\n\n");
1231
1232 fprintf(ofp, "import os\n");
1233 fprintf(ofp, "import sys\n\n");
1234
1235 fprintf(ofp, "sys.path.append(os.environ['PERF_EXEC_PATH'] + \\\n");
1236 fprintf(ofp, "\t'/scripts/python/Perf-Trace-Util/lib/Perf/Trace')\n");
1237 fprintf(ofp, "\nfrom perf_trace_context import *\n");
1238 fprintf(ofp, "from Core import *\n\n\n");
1239
1240 fprintf(ofp, "def trace_begin():\n");
1241 fprintf(ofp, "\tprint \"in trace_begin\"\n\n");
1242
1243 fprintf(ofp, "def trace_end():\n");
1244 fprintf(ofp, "\tprint \"in trace_end\"\n\n");
1245
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03001246 while ((event = trace_find_next_event(pevent, event))) {
Tom Zanussi7e4b21b2010-01-27 02:27:57 -06001247 fprintf(ofp, "def %s__%s(", event->system, event->name);
1248 fprintf(ofp, "event_name, ");
1249 fprintf(ofp, "context, ");
1250 fprintf(ofp, "common_cpu,\n");
1251 fprintf(ofp, "\tcommon_secs, ");
1252 fprintf(ofp, "common_nsecs, ");
1253 fprintf(ofp, "common_pid, ");
1254 fprintf(ofp, "common_comm,\n\t");
Joseph Schuchart0f5f5bc2014-07-10 13:50:51 +02001255 fprintf(ofp, "common_callchain, ");
Tom Zanussi7e4b21b2010-01-27 02:27:57 -06001256
1257 not_first = 0;
1258 count = 0;
1259
1260 for (f = event->format.fields; f; f = f->next) {
1261 if (not_first++)
1262 fprintf(ofp, ", ");
1263 if (++count % 5 == 0)
1264 fprintf(ofp, "\n\t");
1265
1266 fprintf(ofp, "%s", f->name);
1267 }
1268 fprintf(ofp, "):\n");
1269
1270 fprintf(ofp, "\t\tprint_header(event_name, common_cpu, "
1271 "common_secs, common_nsecs,\n\t\t\t"
1272 "common_pid, common_comm)\n\n");
1273
1274 fprintf(ofp, "\t\tprint \"");
1275
1276 not_first = 0;
1277 count = 0;
1278
1279 for (f = event->format.fields; f; f = f->next) {
1280 if (not_first++)
1281 fprintf(ofp, ", ");
1282 if (count && count % 3 == 0) {
1283 fprintf(ofp, "\" \\\n\t\t\"");
1284 }
1285 count++;
1286
1287 fprintf(ofp, "%s=", f->name);
1288 if (f->flags & FIELD_IS_STRING ||
1289 f->flags & FIELD_IS_FLAG ||
Namhyung Kime646fe72014-05-29 13:44:55 +09001290 f->flags & FIELD_IS_ARRAY ||
Tom Zanussi7e4b21b2010-01-27 02:27:57 -06001291 f->flags & FIELD_IS_SYMBOLIC)
1292 fprintf(ofp, "%%s");
1293 else if (f->flags & FIELD_IS_SIGNED)
1294 fprintf(ofp, "%%d");
1295 else
1296 fprintf(ofp, "%%u");
1297 }
1298
Joseph Schuchart0f5f5bc2014-07-10 13:50:51 +02001299 fprintf(ofp, "\" %% \\\n\t\t(");
Tom Zanussi7e4b21b2010-01-27 02:27:57 -06001300
1301 not_first = 0;
1302 count = 0;
1303
1304 for (f = event->format.fields; f; f = f->next) {
1305 if (not_first++)
1306 fprintf(ofp, ", ");
1307
1308 if (++count % 5 == 0)
1309 fprintf(ofp, "\n\t\t");
1310
1311 if (f->flags & FIELD_IS_FLAG) {
1312 if ((count - 1) % 5 != 0) {
1313 fprintf(ofp, "\n\t\t");
1314 count = 4;
1315 }
1316 fprintf(ofp, "flag_str(\"");
1317 fprintf(ofp, "%s__%s\", ", event->system,
1318 event->name);
1319 fprintf(ofp, "\"%s\", %s)", f->name,
1320 f->name);
1321 } else if (f->flags & FIELD_IS_SYMBOLIC) {
1322 if ((count - 1) % 5 != 0) {
1323 fprintf(ofp, "\n\t\t");
1324 count = 4;
1325 }
1326 fprintf(ofp, "symbol_str(\"");
1327 fprintf(ofp, "%s__%s\", ", event->system,
1328 event->name);
1329 fprintf(ofp, "\"%s\", %s)", f->name,
1330 f->name);
1331 } else
1332 fprintf(ofp, "%s", f->name);
1333 }
1334
Joseph Schuchart0f5f5bc2014-07-10 13:50:51 +02001335 fprintf(ofp, ")\n\n");
1336
1337 fprintf(ofp, "\t\tfor node in common_callchain:");
1338 fprintf(ofp, "\n\t\t\tif 'sym' in node:");
1339 fprintf(ofp, "\n\t\t\t\tprint \"\\t[%%x] %%s\" %% (node['ip'], node['sym']['name'])");
1340 fprintf(ofp, "\n\t\t\telse:");
1341 fprintf(ofp, "\n\t\t\t\tprint \"\t[%%x]\" %% (node['ip'])\n\n");
1342 fprintf(ofp, "\t\tprint \"\\n\"\n\n");
1343
Tom Zanussi7e4b21b2010-01-27 02:27:57 -06001344 }
1345
1346 fprintf(ofp, "def trace_unhandled(event_name, context, "
Pierre Tardyc0251482010-05-31 23:12:09 +02001347 "event_fields_dict):\n");
Tom Zanussi7e4b21b2010-01-27 02:27:57 -06001348
Pierre Tardyc0251482010-05-31 23:12:09 +02001349 fprintf(ofp, "\t\tprint ' '.join(['%%s=%%s'%%(k,str(v))"
1350 "for k,v in sorted(event_fields_dict.items())])\n\n");
Tom Zanussi7e4b21b2010-01-27 02:27:57 -06001351
1352 fprintf(ofp, "def print_header("
1353 "event_name, cpu, secs, nsecs, pid, comm):\n"
1354 "\tprint \"%%-20s %%5u %%05u.%%09u %%8u %%-20s \" %% \\\n\t"
1355 "(event_name, cpu, secs, nsecs, pid, comm),\n");
1356
1357 fclose(ofp);
1358
1359 fprintf(stderr, "generated Python script: %s\n", fname);
1360
1361 return 0;
1362}
1363
1364struct scripting_ops python_scripting_ops = {
Jiri Olsaaef90262016-01-05 22:09:11 +01001365 .name = "Python",
1366 .start_script = python_start_script,
1367 .flush_script = python_flush_script,
1368 .stop_script = python_stop_script,
1369 .process_event = python_process_event,
1370 .process_stat = python_process_stat,
1371 .process_stat_interval = python_process_stat_interval,
1372 .generate_script = python_generate_script,
Tom Zanussi7e4b21b2010-01-27 02:27:57 -06001373};