blob: 7bd6da80533eaaa1e549eb7808a4f6b0b0075859 [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
24#include <stdio.h>
25#include <stdlib.h>
26#include <string.h>
Adrian Hunterdf919b42014-10-23 13:45:14 +030027#include <stdbool.h>
Tom Zanussi7e4b21b2010-01-27 02:27:57 -060028#include <errno.h>
Jiri Olsaadf5bcf2014-10-26 23:44:05 +010029#include <linux/bitmap.h>
Tom Zanussi7e4b21b2010-01-27 02:27:57 -060030
31#include "../../perf.h"
Jiri Olsa84f5d362014-07-14 23:46:48 +020032#include "../debug.h"
Arnaldo Carvalho de Melo8f651ea2014-10-09 16:12:24 -030033#include "../callchain.h"
Arnaldo Carvalho de Melofcf65bf2012-08-07 09:58:03 -030034#include "../evsel.h"
Tom Zanussi7e4b21b2010-01-27 02:27:57 -060035#include "../util.h"
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -020036#include "../event.h"
37#include "../thread.h"
Adrian Hunterdf919b42014-10-23 13:45:14 +030038#include "../comm.h"
39#include "../machine.h"
40#include "../db-export.h"
Adrian Hunter6a703072014-10-30 16:09:47 +020041#include "../thread-stack.h"
Tom Zanussi7e4b21b2010-01-27 02:27:57 -060042#include "../trace-event.h"
Joseph Schuchart0f5f5bc2014-07-10 13:50:51 +020043#include "../machine.h"
Chris Phlipot451db122016-04-28 01:19:07 -070044#include "../call-path.h"
Jiri Olsaaef90262016-01-05 22:09:11 +010045#include "thread_map.h"
46#include "cpumap.h"
47#include "stat.h"
Tom Zanussi7e4b21b2010-01-27 02:27:57 -060048
49PyMODINIT_FUNC initperf_trace_context(void);
50
Steven Rostedt (Red Hat)609a7402015-05-13 13:44:36 -040051#define TRACE_EVENT_TYPE_MAX \
Tom Zanussi7e4b21b2010-01-27 02:27:57 -060052 ((1 << (sizeof(unsigned short) * 8)) - 1)
53
Steven Rostedt (Red Hat)609a7402015-05-13 13:44:36 -040054static DECLARE_BITMAP(events_defined, TRACE_EVENT_TYPE_MAX);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -060055
56#define MAX_FIELDS 64
57#define N_COMMON_FIELDS 7
58
59extern struct scripting_context *scripting_context;
60
61static char *cur_field_name;
62static int zero_flag_atom;
63
64static PyObject *main_module, *main_dict;
65
Adrian Hunterdf919b42014-10-23 13:45:14 +030066struct tables {
67 struct db_export dbe;
68 PyObject *evsel_handler;
69 PyObject *machine_handler;
70 PyObject *thread_handler;
71 PyObject *comm_handler;
72 PyObject *comm_thread_handler;
73 PyObject *dso_handler;
74 PyObject *symbol_handler;
Adrian Hunterc29414f2014-10-30 16:09:44 +020075 PyObject *branch_type_handler;
Adrian Hunterdf919b42014-10-23 13:45:14 +030076 PyObject *sample_handler;
Adrian Hunter6a703072014-10-30 16:09:47 +020077 PyObject *call_path_handler;
78 PyObject *call_return_handler;
Adrian Hunterdf919b42014-10-23 13:45:14 +030079 bool db_export_mode;
80};
81
82static struct tables tables_global;
83
Joseph Schuchart05f832e2014-07-09 16:16:31 +020084static void handler_call_die(const char *handler_name) NORETURN;
Tom Zanussi7e4b21b2010-01-27 02:27:57 -060085static void handler_call_die(const char *handler_name)
86{
87 PyErr_Print();
88 Py_FatalError("problem in Python trace event handler");
Joseph Schuchart05f832e2014-07-09 16:16:31 +020089 // Py_FatalError does not return
90 // but we have to make the compiler happy
91 abort();
Tom Zanussi7e4b21b2010-01-27 02:27:57 -060092}
93
Joseph Schuchartc0268e82013-10-24 10:10:51 -030094/*
95 * Insert val into into the dictionary and decrement the reference counter.
Arnaldo Carvalho de Melo48000a12014-12-17 17:24:45 -030096 * This is necessary for dictionaries since PyDict_SetItemString() does not
Joseph Schuchartc0268e82013-10-24 10:10:51 -030097 * steal a reference, as opposed to PyTuple_SetItem().
98 */
99static void pydict_set_item_string_decref(PyObject *dict, const char *key, PyObject *val)
100{
101 PyDict_SetItemString(dict, key, val);
102 Py_DECREF(val);
103}
104
Adrian Huntera5563ed2014-07-31 09:01:01 +0300105static PyObject *get_handler(const char *handler_name)
106{
107 PyObject *handler;
108
109 handler = PyDict_GetItemString(main_dict, handler_name);
110 if (handler && !PyCallable_Check(handler))
111 return NULL;
112 return handler;
113}
114
115static void call_object(PyObject *handler, PyObject *args, const char *die_msg)
116{
117 PyObject *retval;
118
119 retval = PyObject_CallObject(handler, args);
120 if (retval == NULL)
121 handler_call_die(die_msg);
122 Py_DECREF(retval);
123}
124
125static void try_call_object(const char *handler_name, PyObject *args)
126{
127 PyObject *handler;
128
129 handler = get_handler(handler_name);
130 if (handler)
131 call_object(handler, args, handler_name);
132}
133
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600134static void define_value(enum print_arg_type field_type,
135 const char *ev_name,
136 const char *field_name,
137 const char *field_value,
138 const char *field_str)
139{
140 const char *handler_name = "define_flag_value";
Adrian Huntera5563ed2014-07-31 09:01:01 +0300141 PyObject *t;
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600142 unsigned long long value;
143 unsigned n = 0;
144
145 if (field_type == PRINT_SYMBOL)
146 handler_name = "define_symbolic_value";
147
Tom Zanussi44ad9cd2010-02-22 01:12:59 -0600148 t = PyTuple_New(4);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600149 if (!t)
150 Py_FatalError("couldn't create Python tuple");
151
152 value = eval_flag(field_value);
153
154 PyTuple_SetItem(t, n++, PyString_FromString(ev_name));
155 PyTuple_SetItem(t, n++, PyString_FromString(field_name));
156 PyTuple_SetItem(t, n++, PyInt_FromLong(value));
157 PyTuple_SetItem(t, n++, PyString_FromString(field_str));
158
Adrian Huntera5563ed2014-07-31 09:01:01 +0300159 try_call_object(handler_name, t);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600160
161 Py_DECREF(t);
162}
163
164static void define_values(enum print_arg_type field_type,
165 struct print_flag_sym *field,
166 const char *ev_name,
167 const char *field_name)
168{
169 define_value(field_type, ev_name, field_name, field->value,
170 field->str);
171
172 if (field->next)
173 define_values(field_type, field->next, ev_name, field_name);
174}
175
176static void define_field(enum print_arg_type field_type,
177 const char *ev_name,
178 const char *field_name,
179 const char *delim)
180{
181 const char *handler_name = "define_flag_field";
Adrian Huntera5563ed2014-07-31 09:01:01 +0300182 PyObject *t;
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600183 unsigned n = 0;
184
185 if (field_type == PRINT_SYMBOL)
186 handler_name = "define_symbolic_field";
187
Tom Zanussi44ad9cd2010-02-22 01:12:59 -0600188 if (field_type == PRINT_FLAGS)
189 t = PyTuple_New(3);
190 else
191 t = PyTuple_New(2);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600192 if (!t)
193 Py_FatalError("couldn't create Python tuple");
194
195 PyTuple_SetItem(t, n++, PyString_FromString(ev_name));
196 PyTuple_SetItem(t, n++, PyString_FromString(field_name));
197 if (field_type == PRINT_FLAGS)
198 PyTuple_SetItem(t, n++, PyString_FromString(delim));
199
Adrian Huntera5563ed2014-07-31 09:01:01 +0300200 try_call_object(handler_name, t);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600201
202 Py_DECREF(t);
203}
204
Steven Rostedtaaf045f2012-04-06 00:47:56 +0200205static void define_event_symbols(struct event_format *event,
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600206 const char *ev_name,
207 struct print_arg *args)
208{
Taeung Song8579aca2016-02-26 00:12:59 +0900209 if (args == NULL)
210 return;
211
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600212 switch (args->type) {
213 case PRINT_NULL:
214 break;
215 case PRINT_ATOM:
216 define_value(PRINT_FLAGS, ev_name, cur_field_name, "0",
217 args->atom.atom);
218 zero_flag_atom = 0;
219 break;
220 case PRINT_FIELD:
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -0300221 free(cur_field_name);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600222 cur_field_name = strdup(args->field.name);
223 break;
224 case PRINT_FLAGS:
225 define_event_symbols(event, ev_name, args->flags.field);
226 define_field(PRINT_FLAGS, ev_name, cur_field_name,
227 args->flags.delim);
228 define_values(PRINT_FLAGS, args->flags.flags, ev_name,
229 cur_field_name);
230 break;
231 case PRINT_SYMBOL:
232 define_event_symbols(event, ev_name, args->symbol.field);
233 define_field(PRINT_SYMBOL, ev_name, cur_field_name, NULL);
234 define_values(PRINT_SYMBOL, args->symbol.symbols, ev_name,
235 cur_field_name);
236 break;
Namhyung Kime080e6f2012-06-27 09:41:41 +0900237 case PRINT_HEX:
238 define_event_symbols(event, ev_name, args->hex.field);
239 define_event_symbols(event, ev_name, args->hex.size);
240 break;
Javi Merinob839e1e82015-03-24 11:07:19 +0000241 case PRINT_INT_ARRAY:
242 define_event_symbols(event, ev_name, args->int_array.field);
243 define_event_symbols(event, ev_name, args->int_array.count);
244 define_event_symbols(event, ev_name, args->int_array.el_size);
245 break;
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600246 case PRINT_STRING:
247 break;
248 case PRINT_TYPE:
249 define_event_symbols(event, ev_name, args->typecast.item);
250 break;
251 case PRINT_OP:
252 if (strcmp(args->op.op, ":") == 0)
253 zero_flag_atom = 1;
254 define_event_symbols(event, ev_name, args->op.left);
255 define_event_symbols(event, ev_name, args->op.right);
256 break;
257 default:
Steven Rostedtaaf045f2012-04-06 00:47:56 +0200258 /* gcc warns for these? */
259 case PRINT_BSTRING:
260 case PRINT_DYNAMIC_ARRAY:
He Kuang76055942015-08-29 04:22:05 +0000261 case PRINT_DYNAMIC_ARRAY_LEN:
Steven Rostedtaaf045f2012-04-06 00:47:56 +0200262 case PRINT_FUNC:
Steven Rostedt (Red Hat)473a7782014-06-02 23:20:16 -0400263 case PRINT_BITMASK:
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600264 /* we should warn... */
265 return;
266 }
267
268 if (args->next)
269 define_event_symbols(event, ev_name, args->next);
270}
271
Sebastian Andrzej Siewior33058b92014-05-27 18:14:33 +0200272static PyObject *get_field_numeric_entry(struct event_format *event,
273 struct format_field *field, void *data)
274{
Sebastian Andrzej Siewior8ac631c2014-05-27 18:14:34 +0200275 bool is_array = field->flags & FIELD_IS_ARRAY;
Arnaldo Carvalho de Melo39f54862016-07-12 11:05:26 -0300276 PyObject *obj = NULL, *list = NULL;
Sebastian Andrzej Siewior33058b92014-05-27 18:14:33 +0200277 unsigned long long val;
Sebastian Andrzej Siewior8ac631c2014-05-27 18:14:34 +0200278 unsigned int item_size, n_items, i;
Sebastian Andrzej Siewior33058b92014-05-27 18:14:33 +0200279
Sebastian Andrzej Siewior8ac631c2014-05-27 18:14:34 +0200280 if (is_array) {
281 list = PyList_New(field->arraylen);
282 item_size = field->size / field->arraylen;
283 n_items = field->arraylen;
Sebastian Andrzej Siewior33058b92014-05-27 18:14:33 +0200284 } else {
Sebastian Andrzej Siewior8ac631c2014-05-27 18:14:34 +0200285 item_size = field->size;
286 n_items = 1;
Sebastian Andrzej Siewior33058b92014-05-27 18:14:33 +0200287 }
Sebastian Andrzej Siewior8ac631c2014-05-27 18:14:34 +0200288
289 for (i = 0; i < n_items; i++) {
290
291 val = read_size(event, data + field->offset + i * item_size,
292 item_size);
293 if (field->flags & FIELD_IS_SIGNED) {
294 if ((long long)val >= LONG_MIN &&
295 (long long)val <= LONG_MAX)
296 obj = PyInt_FromLong(val);
297 else
298 obj = PyLong_FromLongLong(val);
299 } else {
300 if (val <= LONG_MAX)
301 obj = PyInt_FromLong(val);
302 else
303 obj = PyLong_FromUnsignedLongLong(val);
304 }
305 if (is_array)
306 PyList_SET_ITEM(list, i, obj);
307 }
308 if (is_array)
309 obj = list;
Sebastian Andrzej Siewior33058b92014-05-27 18:14:33 +0200310 return obj;
311}
312
Joseph Schuchart0f5f5bc2014-07-10 13:50:51 +0200313
314static PyObject *python_process_callchain(struct perf_sample *sample,
315 struct perf_evsel *evsel,
316 struct addr_location *al)
317{
318 PyObject *pylist;
319
320 pylist = PyList_New(0);
321 if (!pylist)
322 Py_FatalError("couldn't create Python list");
323
324 if (!symbol_conf.use_callchain || !sample->callchain)
325 goto exit;
326
Arnaldo Carvalho de Melo91d7b2d2016-04-14 14:48:07 -0300327 if (thread__resolve_callchain(al->thread, &callchain_cursor, evsel,
Arnaldo Carvalho de Melocc8b7c22014-10-23 15:26:17 -0300328 sample, NULL, NULL,
Adrian Hunter44cbe722015-09-25 16:15:50 +0300329 scripting_max_stack) != 0) {
Joseph Schuchart0f5f5bc2014-07-10 13:50:51 +0200330 pr_err("Failed to resolve callchain. Skipping\n");
331 goto exit;
332 }
333 callchain_cursor_commit(&callchain_cursor);
334
335
336 while (1) {
337 PyObject *pyelem;
338 struct callchain_cursor_node *node;
339 node = callchain_cursor_current(&callchain_cursor);
340 if (!node)
341 break;
342
343 pyelem = PyDict_New();
344 if (!pyelem)
345 Py_FatalError("couldn't create Python dictionary");
346
347
348 pydict_set_item_string_decref(pyelem, "ip",
349 PyLong_FromUnsignedLongLong(node->ip));
350
351 if (node->sym) {
352 PyObject *pysym = PyDict_New();
353 if (!pysym)
354 Py_FatalError("couldn't create Python dictionary");
355 pydict_set_item_string_decref(pysym, "start",
356 PyLong_FromUnsignedLongLong(node->sym->start));
357 pydict_set_item_string_decref(pysym, "end",
358 PyLong_FromUnsignedLongLong(node->sym->end));
359 pydict_set_item_string_decref(pysym, "binding",
360 PyInt_FromLong(node->sym->binding));
361 pydict_set_item_string_decref(pysym, "name",
362 PyString_FromStringAndSize(node->sym->name,
363 node->sym->namelen));
364 pydict_set_item_string_decref(pyelem, "sym", pysym);
365 }
366
367 if (node->map) {
368 struct map *map = node->map;
369 const char *dsoname = "[unknown]";
370 if (map && map->dso && (map->dso->name || map->dso->long_name)) {
371 if (symbol_conf.show_kernel_path && map->dso->long_name)
372 dsoname = map->dso->long_name;
373 else if (map->dso->name)
374 dsoname = map->dso->name;
375 }
376 pydict_set_item_string_decref(pyelem, "dso",
377 PyString_FromString(dsoname));
378 }
379
380 callchain_cursor_advance(&callchain_cursor);
381 PyList_Append(pylist, pyelem);
382 Py_DECREF(pyelem);
383 }
384
385exit:
386 return pylist;
387}
388
Jiri Olsa249de6e2016-07-16 18:11:18 +0200389static int is_printable_array(char *p, unsigned int len)
390{
391 unsigned int i;
392
393 if (!p || !len || p[len - 1] != 0)
394 return 0;
395
396 len--;
397
398 for (i = 0; i < len; i++) {
399 if (!isprint(p[i]) && !isspace(p[i]))
400 return 0;
401 }
402 return 1;
403}
Joseph Schuchart0f5f5bc2014-07-10 13:50:51 +0200404
Arnaldo Carvalho de Melob7fff6b52013-12-19 16:34:52 -0300405static void python_process_tracepoint(struct perf_sample *sample,
406 struct perf_evsel *evsel,
Arnaldo Carvalho de Melob7fff6b52013-12-19 16:34:52 -0300407 struct addr_location *al)
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600408{
Jiri Olsaadf5bcf2014-10-26 23:44:05 +0100409 struct event_format *event = evsel->tp_format;
Arnaldo Carvalho de Melo39f54862016-07-12 11:05:26 -0300410 PyObject *handler, *context, *t, *obj = NULL, *callchain;
Joseph Schuchart0f5f5bc2014-07-10 13:50:51 +0200411 PyObject *dict = NULL;
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600412 static char handler_name[256];
413 struct format_field *field;
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600414 unsigned long s, ns;
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600415 unsigned n = 0;
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600416 int pid;
David Ahernbe6d8422011-03-09 22:23:23 -0700417 int cpu = sample->cpu;
418 void *data = sample->raw_data;
419 unsigned long long nsecs = sample->time;
Arnaldo Carvalho de Melof9d5d542015-04-01 13:29:25 -0300420 const char *comm = thread__comm_str(al->thread);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600421
422 t = PyTuple_New(MAX_FIELDS);
423 if (!t)
424 Py_FatalError("couldn't create Python tuple");
425
Arnaldo Carvalho de Melo62665df2016-05-10 12:33:52 -0300426 if (!event) {
427 snprintf(handler_name, sizeof(handler_name),
428 "ug! no event found for type %" PRIu64, (u64)evsel->attr.config);
429 Py_FatalError(handler_name);
430 }
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600431
Arnaldo Carvalho de Melo97822432012-08-07 23:50:21 -0300432 pid = raw_field_value(event, "common_pid", data);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600433
434 sprintf(handler_name, "%s__%s", event->system, event->name);
435
Jiri Olsaadf5bcf2014-10-26 23:44:05 +0100436 if (!test_and_set_bit(event->id, events_defined))
437 define_event_symbols(event, handler_name, event->print_fmt.args);
438
Adrian Huntera5563ed2014-07-31 09:01:01 +0300439 handler = get_handler(handler_name);
Pierre Tardyc0251482010-05-31 23:12:09 +0200440 if (!handler) {
441 dict = PyDict_New();
442 if (!dict)
443 Py_FatalError("couldn't create Python dict");
444 }
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600445 s = nsecs / NSECS_PER_SEC;
446 ns = nsecs - s * NSECS_PER_SEC;
447
448 scripting_context->event_data = data;
Tom Zanussi2de95332013-01-18 13:51:27 -0600449 scripting_context->pevent = evsel->tp_format->pevent;
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600450
451 context = PyCObject_FromVoidPtr(scripting_context, NULL);
452
453 PyTuple_SetItem(t, n++, PyString_FromString(handler_name));
Kyle McMartinfb7d0b32011-01-24 11:13:04 -0500454 PyTuple_SetItem(t, n++, context);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600455
Joseph Schuchart0f5f5bc2014-07-10 13:50:51 +0200456 /* ip unwinding */
457 callchain = python_process_callchain(sample, evsel, al);
458
Pierre Tardyc0251482010-05-31 23:12:09 +0200459 if (handler) {
460 PyTuple_SetItem(t, n++, PyInt_FromLong(cpu));
461 PyTuple_SetItem(t, n++, PyInt_FromLong(s));
462 PyTuple_SetItem(t, n++, PyInt_FromLong(ns));
463 PyTuple_SetItem(t, n++, PyInt_FromLong(pid));
464 PyTuple_SetItem(t, n++, PyString_FromString(comm));
Joseph Schuchart0f5f5bc2014-07-10 13:50:51 +0200465 PyTuple_SetItem(t, n++, callchain);
Pierre Tardyc0251482010-05-31 23:12:09 +0200466 } else {
Joseph Schuchartc0268e82013-10-24 10:10:51 -0300467 pydict_set_item_string_decref(dict, "common_cpu", PyInt_FromLong(cpu));
468 pydict_set_item_string_decref(dict, "common_s", PyInt_FromLong(s));
469 pydict_set_item_string_decref(dict, "common_ns", PyInt_FromLong(ns));
470 pydict_set_item_string_decref(dict, "common_pid", PyInt_FromLong(pid));
471 pydict_set_item_string_decref(dict, "common_comm", PyString_FromString(comm));
Joseph Schuchart0f5f5bc2014-07-10 13:50:51 +0200472 pydict_set_item_string_decref(dict, "common_callchain", callchain);
Pierre Tardyc0251482010-05-31 23:12:09 +0200473 }
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600474 for (field = event->format.fields; field; field = field->next) {
Jiri Olsa249de6e2016-07-16 18:11:18 +0200475 unsigned int offset, len;
476 unsigned long long val;
477
478 if (field->flags & FIELD_IS_ARRAY) {
479 offset = field->offset;
480 len = field->size;
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600481 if (field->flags & FIELD_IS_DYNAMIC) {
Jiri Olsa249de6e2016-07-16 18:11:18 +0200482 val = pevent_read_number(scripting_context->pevent,
483 data + offset, len);
484 offset = val;
485 len = offset >> 16;
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600486 offset &= 0xffff;
Jiri Olsa249de6e2016-07-16 18:11:18 +0200487 }
488 if (field->flags & FIELD_IS_STRING &&
489 is_printable_array(data + offset, len)) {
490 obj = PyString_FromString((char *) data + offset);
491 } else {
492 obj = PyByteArray_FromStringAndSize((const char *) data + offset, len);
493 field->flags &= ~FIELD_IS_STRING;
494 }
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600495 } else { /* FIELD_IS_NUMERIC */
Sebastian Andrzej Siewior33058b92014-05-27 18:14:33 +0200496 obj = get_field_numeric_entry(event, field, data);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600497 }
Pierre Tardyc0251482010-05-31 23:12:09 +0200498 if (handler)
499 PyTuple_SetItem(t, n++, obj);
500 else
Joseph Schuchartc0268e82013-10-24 10:10:51 -0300501 pydict_set_item_string_decref(dict, field->name, obj);
Pierre Tardyc0251482010-05-31 23:12:09 +0200502
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600503 }
Joseph Schuchart0f5f5bc2014-07-10 13:50:51 +0200504
Pierre Tardyc0251482010-05-31 23:12:09 +0200505 if (!handler)
506 PyTuple_SetItem(t, n++, dict);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600507
508 if (_PyTuple_Resize(&t, n) == -1)
509 Py_FatalError("error resizing Python tuple");
510
Pierre Tardyc0251482010-05-31 23:12:09 +0200511 if (handler) {
Adrian Huntera5563ed2014-07-31 09:01:01 +0300512 call_object(handler, t, handler_name);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600513 } else {
Adrian Huntera5563ed2014-07-31 09:01:01 +0300514 try_call_object("trace_unhandled", t);
Pierre Tardyc0251482010-05-31 23:12:09 +0200515 Py_DECREF(dict);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600516 }
517
518 Py_DECREF(t);
519}
520
Adrian Hunterdf919b42014-10-23 13:45:14 +0300521static PyObject *tuple_new(unsigned int sz)
522{
523 PyObject *t;
524
525 t = PyTuple_New(sz);
526 if (!t)
527 Py_FatalError("couldn't create Python tuple");
528 return t;
529}
530
531static int tuple_set_u64(PyObject *t, unsigned int pos, u64 val)
532{
533#if BITS_PER_LONG == 64
534 return PyTuple_SetItem(t, pos, PyInt_FromLong(val));
535#endif
536#if BITS_PER_LONG == 32
537 return PyTuple_SetItem(t, pos, PyLong_FromLongLong(val));
538#endif
539}
540
541static int tuple_set_s32(PyObject *t, unsigned int pos, s32 val)
542{
543 return PyTuple_SetItem(t, pos, PyInt_FromLong(val));
544}
545
546static int tuple_set_string(PyObject *t, unsigned int pos, const char *s)
547{
548 return PyTuple_SetItem(t, pos, PyString_FromString(s));
549}
550
551static int python_export_evsel(struct db_export *dbe, struct perf_evsel *evsel)
552{
553 struct tables *tables = container_of(dbe, struct tables, dbe);
554 PyObject *t;
555
556 t = tuple_new(2);
557
558 tuple_set_u64(t, 0, evsel->db_id);
559 tuple_set_string(t, 1, perf_evsel__name(evsel));
560
561 call_object(tables->evsel_handler, t, "evsel_table");
562
563 Py_DECREF(t);
564
565 return 0;
566}
567
568static int python_export_machine(struct db_export *dbe,
569 struct machine *machine)
570{
571 struct tables *tables = container_of(dbe, struct tables, dbe);
572 PyObject *t;
573
574 t = tuple_new(3);
575
576 tuple_set_u64(t, 0, machine->db_id);
577 tuple_set_s32(t, 1, machine->pid);
578 tuple_set_string(t, 2, machine->root_dir ? machine->root_dir : "");
579
580 call_object(tables->machine_handler, t, "machine_table");
581
582 Py_DECREF(t);
583
584 return 0;
585}
586
587static int python_export_thread(struct db_export *dbe, struct thread *thread,
588 u64 main_thread_db_id, struct machine *machine)
589{
590 struct tables *tables = container_of(dbe, struct tables, dbe);
591 PyObject *t;
592
593 t = tuple_new(5);
594
595 tuple_set_u64(t, 0, thread->db_id);
596 tuple_set_u64(t, 1, machine->db_id);
597 tuple_set_u64(t, 2, main_thread_db_id);
598 tuple_set_s32(t, 3, thread->pid_);
599 tuple_set_s32(t, 4, thread->tid);
600
601 call_object(tables->thread_handler, t, "thread_table");
602
603 Py_DECREF(t);
604
605 return 0;
606}
607
608static int python_export_comm(struct db_export *dbe, struct comm *comm)
609{
610 struct tables *tables = container_of(dbe, struct tables, dbe);
611 PyObject *t;
612
613 t = tuple_new(2);
614
615 tuple_set_u64(t, 0, comm->db_id);
616 tuple_set_string(t, 1, comm__str(comm));
617
618 call_object(tables->comm_handler, t, "comm_table");
619
620 Py_DECREF(t);
621
622 return 0;
623}
624
625static int python_export_comm_thread(struct db_export *dbe, u64 db_id,
626 struct comm *comm, struct thread *thread)
627{
628 struct tables *tables = container_of(dbe, struct tables, dbe);
629 PyObject *t;
630
631 t = tuple_new(3);
632
633 tuple_set_u64(t, 0, db_id);
634 tuple_set_u64(t, 1, comm->db_id);
635 tuple_set_u64(t, 2, thread->db_id);
636
637 call_object(tables->comm_thread_handler, t, "comm_thread_table");
638
639 Py_DECREF(t);
640
641 return 0;
642}
643
644static int python_export_dso(struct db_export *dbe, struct dso *dso,
645 struct machine *machine)
646{
647 struct tables *tables = container_of(dbe, struct tables, dbe);
Masami Hiramatsub5d8bbe2016-05-11 22:51:59 +0900648 char sbuild_id[SBUILD_ID_SIZE];
Adrian Hunterdf919b42014-10-23 13:45:14 +0300649 PyObject *t;
650
651 build_id__sprintf(dso->build_id, sizeof(dso->build_id), sbuild_id);
652
653 t = tuple_new(5);
654
655 tuple_set_u64(t, 0, dso->db_id);
656 tuple_set_u64(t, 1, machine->db_id);
657 tuple_set_string(t, 2, dso->short_name);
658 tuple_set_string(t, 3, dso->long_name);
659 tuple_set_string(t, 4, sbuild_id);
660
661 call_object(tables->dso_handler, t, "dso_table");
662
663 Py_DECREF(t);
664
665 return 0;
666}
667
668static int python_export_symbol(struct db_export *dbe, struct symbol *sym,
669 struct dso *dso)
670{
671 struct tables *tables = container_of(dbe, struct tables, dbe);
672 u64 *sym_db_id = symbol__priv(sym);
673 PyObject *t;
674
675 t = tuple_new(6);
676
677 tuple_set_u64(t, 0, *sym_db_id);
678 tuple_set_u64(t, 1, dso->db_id);
679 tuple_set_u64(t, 2, sym->start);
680 tuple_set_u64(t, 3, sym->end);
681 tuple_set_s32(t, 4, sym->binding);
682 tuple_set_string(t, 5, sym->name);
683
684 call_object(tables->symbol_handler, t, "symbol_table");
685
686 Py_DECREF(t);
687
688 return 0;
689}
690
Adrian Hunterc29414f2014-10-30 16:09:44 +0200691static int python_export_branch_type(struct db_export *dbe, u32 branch_type,
692 const char *name)
693{
694 struct tables *tables = container_of(dbe, struct tables, dbe);
695 PyObject *t;
696
697 t = tuple_new(2);
698
699 tuple_set_s32(t, 0, branch_type);
700 tuple_set_string(t, 1, name);
701
702 call_object(tables->branch_type_handler, t, "branch_type_table");
703
704 Py_DECREF(t);
705
706 return 0;
707}
708
Adrian Hunterdf919b42014-10-23 13:45:14 +0300709static int python_export_sample(struct db_export *dbe,
710 struct export_sample *es)
711{
712 struct tables *tables = container_of(dbe, struct tables, dbe);
713 PyObject *t;
714
Chris Phlipot2c15f5e2016-04-28 01:19:10 -0700715 t = tuple_new(22);
Adrian Hunterdf919b42014-10-23 13:45:14 +0300716
717 tuple_set_u64(t, 0, es->db_id);
718 tuple_set_u64(t, 1, es->evsel->db_id);
719 tuple_set_u64(t, 2, es->al->machine->db_id);
Arnaldo Carvalho de Melob83e8682015-04-02 11:16:05 -0300720 tuple_set_u64(t, 3, es->al->thread->db_id);
Adrian Hunterdf919b42014-10-23 13:45:14 +0300721 tuple_set_u64(t, 4, es->comm_db_id);
722 tuple_set_u64(t, 5, es->dso_db_id);
723 tuple_set_u64(t, 6, es->sym_db_id);
724 tuple_set_u64(t, 7, es->offset);
725 tuple_set_u64(t, 8, es->sample->ip);
726 tuple_set_u64(t, 9, es->sample->time);
727 tuple_set_s32(t, 10, es->sample->cpu);
728 tuple_set_u64(t, 11, es->addr_dso_db_id);
729 tuple_set_u64(t, 12, es->addr_sym_db_id);
730 tuple_set_u64(t, 13, es->addr_offset);
731 tuple_set_u64(t, 14, es->sample->addr);
732 tuple_set_u64(t, 15, es->sample->period);
733 tuple_set_u64(t, 16, es->sample->weight);
734 tuple_set_u64(t, 17, es->sample->transaction);
735 tuple_set_u64(t, 18, es->sample->data_src);
Adrian Hunterc29414f2014-10-30 16:09:44 +0200736 tuple_set_s32(t, 19, es->sample->flags & PERF_BRANCH_MASK);
737 tuple_set_s32(t, 20, !!(es->sample->flags & PERF_IP_FLAG_IN_TX));
Chris Phlipot2c15f5e2016-04-28 01:19:10 -0700738 tuple_set_u64(t, 21, es->call_path_id);
Adrian Hunterdf919b42014-10-23 13:45:14 +0300739
740 call_object(tables->sample_handler, t, "sample_table");
741
742 Py_DECREF(t);
743
744 return 0;
745}
746
Adrian Hunter6a703072014-10-30 16:09:47 +0200747static int python_export_call_path(struct db_export *dbe, struct call_path *cp)
748{
749 struct tables *tables = container_of(dbe, struct tables, dbe);
750 PyObject *t;
751 u64 parent_db_id, sym_db_id;
752
753 parent_db_id = cp->parent ? cp->parent->db_id : 0;
754 sym_db_id = cp->sym ? *(u64 *)symbol__priv(cp->sym) : 0;
755
756 t = tuple_new(4);
757
758 tuple_set_u64(t, 0, cp->db_id);
759 tuple_set_u64(t, 1, parent_db_id);
760 tuple_set_u64(t, 2, sym_db_id);
761 tuple_set_u64(t, 3, cp->ip);
762
763 call_object(tables->call_path_handler, t, "call_path_table");
764
765 Py_DECREF(t);
766
767 return 0;
768}
769
770static int python_export_call_return(struct db_export *dbe,
771 struct call_return *cr)
772{
773 struct tables *tables = container_of(dbe, struct tables, dbe);
774 u64 comm_db_id = cr->comm ? cr->comm->db_id : 0;
775 PyObject *t;
776
777 t = tuple_new(11);
778
779 tuple_set_u64(t, 0, cr->db_id);
780 tuple_set_u64(t, 1, cr->thread->db_id);
781 tuple_set_u64(t, 2, comm_db_id);
782 tuple_set_u64(t, 3, cr->cp->db_id);
783 tuple_set_u64(t, 4, cr->call_time);
784 tuple_set_u64(t, 5, cr->return_time);
785 tuple_set_u64(t, 6, cr->branch_count);
786 tuple_set_u64(t, 7, cr->call_ref);
787 tuple_set_u64(t, 8, cr->return_ref);
788 tuple_set_u64(t, 9, cr->cp->parent->db_id);
789 tuple_set_s32(t, 10, cr->flags);
790
791 call_object(tables->call_return_handler, t, "call_return_table");
792
793 Py_DECREF(t);
794
795 return 0;
796}
797
798static int python_process_call_return(struct call_return *cr, void *data)
799{
800 struct db_export *dbe = data;
801
802 return db_export__call_return(dbe, cr);
803}
804
Arnaldo Carvalho de Melob7fff6b52013-12-19 16:34:52 -0300805static void python_process_general_event(struct perf_sample *sample,
Feng Tang6a6daec2012-08-08 17:57:51 +0800806 struct perf_evsel *evsel,
Feng Tang87b6a3a2012-08-09 13:46:13 +0800807 struct addr_location *al)
Feng Tang6a6daec2012-08-08 17:57:51 +0800808{
Adrian Huntera5563ed2014-07-31 09:01:01 +0300809 PyObject *handler, *t, *dict, *callchain, *dict_sample;
Feng Tang6a6daec2012-08-08 17:57:51 +0800810 static char handler_name[64];
811 unsigned n = 0;
Feng Tang6a6daec2012-08-08 17:57:51 +0800812
Feng Tangfd6b8582012-08-08 17:57:53 +0800813 /*
814 * Use the MAX_FIELDS to make the function expandable, though
Feng Tang87b6a3a2012-08-09 13:46:13 +0800815 * currently there is only one item for the tuple.
Feng Tangfd6b8582012-08-08 17:57:53 +0800816 */
Feng Tang6a6daec2012-08-08 17:57:51 +0800817 t = PyTuple_New(MAX_FIELDS);
818 if (!t)
819 Py_FatalError("couldn't create Python tuple");
820
Feng Tangfd6b8582012-08-08 17:57:53 +0800821 dict = PyDict_New();
822 if (!dict)
823 Py_FatalError("couldn't create Python dictionary");
824
Joseph Schuchart57608cf2014-07-10 13:50:56 +0200825 dict_sample = PyDict_New();
826 if (!dict_sample)
827 Py_FatalError("couldn't create Python dictionary");
828
Feng Tang6a6daec2012-08-08 17:57:51 +0800829 snprintf(handler_name, sizeof(handler_name), "%s", "process_event");
830
Adrian Huntera5563ed2014-07-31 09:01:01 +0300831 handler = get_handler(handler_name);
832 if (!handler)
Feng Tang6a6daec2012-08-08 17:57:51 +0800833 goto exit;
Feng Tang6a6daec2012-08-08 17:57:51 +0800834
Joseph Schuchartc0268e82013-10-24 10:10:51 -0300835 pydict_set_item_string_decref(dict, "ev_name", PyString_FromString(perf_evsel__name(evsel)));
836 pydict_set_item_string_decref(dict, "attr", PyString_FromStringAndSize(
Feng Tangfd6b8582012-08-08 17:57:53 +0800837 (const char *)&evsel->attr, sizeof(evsel->attr)));
Joseph Schuchart57608cf2014-07-10 13:50:56 +0200838
839 pydict_set_item_string_decref(dict_sample, "pid",
840 PyInt_FromLong(sample->pid));
841 pydict_set_item_string_decref(dict_sample, "tid",
842 PyInt_FromLong(sample->tid));
843 pydict_set_item_string_decref(dict_sample, "cpu",
844 PyInt_FromLong(sample->cpu));
845 pydict_set_item_string_decref(dict_sample, "ip",
846 PyLong_FromUnsignedLongLong(sample->ip));
847 pydict_set_item_string_decref(dict_sample, "time",
848 PyLong_FromUnsignedLongLong(sample->time));
849 pydict_set_item_string_decref(dict_sample, "period",
850 PyLong_FromUnsignedLongLong(sample->period));
851 pydict_set_item_string_decref(dict, "sample", dict_sample);
852
Joseph Schuchartc0268e82013-10-24 10:10:51 -0300853 pydict_set_item_string_decref(dict, "raw_buf", PyString_FromStringAndSize(
Feng Tangfd6b8582012-08-08 17:57:53 +0800854 (const char *)sample->raw_data, sample->raw_size));
Joseph Schuchartc0268e82013-10-24 10:10:51 -0300855 pydict_set_item_string_decref(dict, "comm",
Arnaldo Carvalho de Melof9d5d542015-04-01 13:29:25 -0300856 PyString_FromString(thread__comm_str(al->thread)));
Feng Tangfd6b8582012-08-08 17:57:53 +0800857 if (al->map) {
Joseph Schuchartc0268e82013-10-24 10:10:51 -0300858 pydict_set_item_string_decref(dict, "dso",
Feng Tangfd6b8582012-08-08 17:57:53 +0800859 PyString_FromString(al->map->dso->name));
860 }
861 if (al->sym) {
Joseph Schuchartc0268e82013-10-24 10:10:51 -0300862 pydict_set_item_string_decref(dict, "symbol",
Feng Tangfd6b8582012-08-08 17:57:53 +0800863 PyString_FromString(al->sym->name));
864 }
Feng Tang6a6daec2012-08-08 17:57:51 +0800865
Joseph Schuchart0f5f5bc2014-07-10 13:50:51 +0200866 /* ip unwinding */
867 callchain = python_process_callchain(sample, evsel, al);
868 pydict_set_item_string_decref(dict, "callchain", callchain);
869
Feng Tangfd6b8582012-08-08 17:57:53 +0800870 PyTuple_SetItem(t, n++, dict);
Feng Tang6a6daec2012-08-08 17:57:51 +0800871 if (_PyTuple_Resize(&t, n) == -1)
872 Py_FatalError("error resizing Python tuple");
873
Adrian Huntera5563ed2014-07-31 09:01:01 +0300874 call_object(handler, t, handler_name);
Feng Tang6a6daec2012-08-08 17:57:51 +0800875exit:
Feng Tangfd6b8582012-08-08 17:57:53 +0800876 Py_DECREF(dict);
Feng Tang6a6daec2012-08-08 17:57:51 +0800877 Py_DECREF(t);
878}
879
Adrian Hunterdf919b42014-10-23 13:45:14 +0300880static void python_process_event(union perf_event *event,
Feng Tang6a6daec2012-08-08 17:57:51 +0800881 struct perf_sample *sample,
882 struct perf_evsel *evsel,
Feng Tang73994dc2012-08-08 17:57:52 +0800883 struct addr_location *al)
Feng Tang6a6daec2012-08-08 17:57:51 +0800884{
Adrian Hunterdf919b42014-10-23 13:45:14 +0300885 struct tables *tables = &tables_global;
886
Feng Tang6a6daec2012-08-08 17:57:51 +0800887 switch (evsel->attr.type) {
888 case PERF_TYPE_TRACEPOINT:
Arnaldo Carvalho de Melof9d5d542015-04-01 13:29:25 -0300889 python_process_tracepoint(sample, evsel, al);
Feng Tang6a6daec2012-08-08 17:57:51 +0800890 break;
891 /* Reserve for future process_hw/sw/raw APIs */
892 default:
Adrian Hunterdf919b42014-10-23 13:45:14 +0300893 if (tables->db_export_mode)
Arnaldo Carvalho de Melo73272592015-04-02 11:08:30 -0300894 db_export__sample(&tables->dbe, event, sample, evsel, al);
Adrian Hunterdf919b42014-10-23 13:45:14 +0300895 else
Arnaldo Carvalho de Melof9d5d542015-04-01 13:29:25 -0300896 python_process_general_event(sample, evsel, al);
Feng Tang6a6daec2012-08-08 17:57:51 +0800897 }
898}
899
Jiri Olsaaef90262016-01-05 22:09:11 +0100900static void get_handler_name(char *str, size_t size,
901 struct perf_evsel *evsel)
902{
903 char *p = str;
904
905 scnprintf(str, size, "stat__%s", perf_evsel__name(evsel));
906
907 while ((p = strchr(p, ':'))) {
908 *p = '_';
909 p++;
910 }
911}
912
913static void
914process_stat(struct perf_evsel *counter, int cpu, int thread, u64 tstamp,
915 struct perf_counts_values *count)
916{
917 PyObject *handler, *t;
918 static char handler_name[256];
919 int n = 0;
920
921 t = PyTuple_New(MAX_FIELDS);
922 if (!t)
923 Py_FatalError("couldn't create Python tuple");
924
925 get_handler_name(handler_name, sizeof(handler_name),
926 counter);
927
928 handler = get_handler(handler_name);
929 if (!handler) {
930 pr_debug("can't find python handler %s\n", handler_name);
931 return;
932 }
933
934 PyTuple_SetItem(t, n++, PyInt_FromLong(cpu));
935 PyTuple_SetItem(t, n++, PyInt_FromLong(thread));
936
937 tuple_set_u64(t, n++, tstamp);
938 tuple_set_u64(t, n++, count->val);
939 tuple_set_u64(t, n++, count->ena);
940 tuple_set_u64(t, n++, count->run);
941
942 if (_PyTuple_Resize(&t, n) == -1)
943 Py_FatalError("error resizing Python tuple");
944
945 call_object(handler, t, handler_name);
946
947 Py_DECREF(t);
948}
949
950static void python_process_stat(struct perf_stat_config *config,
951 struct perf_evsel *counter, u64 tstamp)
952{
953 struct thread_map *threads = counter->threads;
954 struct cpu_map *cpus = counter->cpus;
955 int cpu, thread;
956
957 if (config->aggr_mode == AGGR_GLOBAL) {
958 process_stat(counter, -1, -1, tstamp,
959 &counter->counts->aggr);
960 return;
961 }
962
963 for (thread = 0; thread < threads->nr; thread++) {
964 for (cpu = 0; cpu < cpus->nr; cpu++) {
965 process_stat(counter, cpus->map[cpu],
966 thread_map__pid(threads, thread), tstamp,
967 perf_counts(counter->counts, cpu, thread));
968 }
969 }
970}
971
972static void python_process_stat_interval(u64 tstamp)
973{
974 PyObject *handler, *t;
975 static const char handler_name[] = "stat__interval";
976 int n = 0;
977
978 t = PyTuple_New(MAX_FIELDS);
979 if (!t)
980 Py_FatalError("couldn't create Python tuple");
981
982 handler = get_handler(handler_name);
983 if (!handler) {
984 pr_debug("can't find python handler %s\n", handler_name);
985 return;
986 }
987
988 tuple_set_u64(t, n++, tstamp);
989
990 if (_PyTuple_Resize(&t, n) == -1)
991 Py_FatalError("error resizing Python tuple");
992
993 call_object(handler, t, handler_name);
994
995 Py_DECREF(t);
996}
997
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600998static int run_start_sub(void)
999{
Tom Zanussi7e4b21b2010-01-27 02:27:57 -06001000 main_module = PyImport_AddModule("__main__");
1001 if (main_module == NULL)
1002 return -1;
1003 Py_INCREF(main_module);
1004
1005 main_dict = PyModule_GetDict(main_module);
Adrian Huntera5563ed2014-07-31 09:01:01 +03001006 if (main_dict == NULL)
Tom Zanussi7e4b21b2010-01-27 02:27:57 -06001007 goto error;
Tom Zanussi7e4b21b2010-01-27 02:27:57 -06001008 Py_INCREF(main_dict);
1009
Adrian Huntera5563ed2014-07-31 09:01:01 +03001010 try_call_object("trace_begin", NULL);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -06001011
Adrian Huntera5563ed2014-07-31 09:01:01 +03001012 return 0;
Tom Zanussi7e4b21b2010-01-27 02:27:57 -06001013
Tom Zanussi7e4b21b2010-01-27 02:27:57 -06001014error:
1015 Py_XDECREF(main_dict);
1016 Py_XDECREF(main_module);
Adrian Huntera5563ed2014-07-31 09:01:01 +03001017 return -1;
Tom Zanussi7e4b21b2010-01-27 02:27:57 -06001018}
1019
Adrian Hunterdf919b42014-10-23 13:45:14 +03001020#define SET_TABLE_HANDLER_(name, handler_name, table_name) do { \
1021 tables->handler_name = get_handler(#table_name); \
1022 if (tables->handler_name) \
1023 tables->dbe.export_ ## name = python_export_ ## name; \
1024} while (0)
1025
1026#define SET_TABLE_HANDLER(name) \
1027 SET_TABLE_HANDLER_(name, name ## _handler, name ## _table)
1028
1029static void set_table_handlers(struct tables *tables)
1030{
1031 const char *perf_db_export_mode = "perf_db_export_mode";
Adrian Hunter6a703072014-10-30 16:09:47 +02001032 const char *perf_db_export_calls = "perf_db_export_calls";
Chris Phlipot2c15f5e2016-04-28 01:19:10 -07001033 const char *perf_db_export_callchains = "perf_db_export_callchains";
1034 PyObject *db_export_mode, *db_export_calls, *db_export_callchains;
Adrian Hunter6a703072014-10-30 16:09:47 +02001035 bool export_calls = false;
Chris Phlipot2c15f5e2016-04-28 01:19:10 -07001036 bool export_callchains = false;
Adrian Hunterdf919b42014-10-23 13:45:14 +03001037 int ret;
1038
1039 memset(tables, 0, sizeof(struct tables));
1040 if (db_export__init(&tables->dbe))
1041 Py_FatalError("failed to initialize export");
1042
1043 db_export_mode = PyDict_GetItemString(main_dict, perf_db_export_mode);
1044 if (!db_export_mode)
1045 return;
1046
1047 ret = PyObject_IsTrue(db_export_mode);
1048 if (ret == -1)
1049 handler_call_die(perf_db_export_mode);
1050 if (!ret)
1051 return;
1052
Chris Phlipot2c15f5e2016-04-28 01:19:10 -07001053 /* handle export calls */
Adrian Hunter6a703072014-10-30 16:09:47 +02001054 tables->dbe.crp = NULL;
1055 db_export_calls = PyDict_GetItemString(main_dict, perf_db_export_calls);
1056 if (db_export_calls) {
1057 ret = PyObject_IsTrue(db_export_calls);
1058 if (ret == -1)
1059 handler_call_die(perf_db_export_calls);
1060 export_calls = !!ret;
1061 }
1062
1063 if (export_calls) {
1064 tables->dbe.crp =
1065 call_return_processor__new(python_process_call_return,
1066 &tables->dbe);
1067 if (!tables->dbe.crp)
1068 Py_FatalError("failed to create calls processor");
1069 }
1070
Chris Phlipot2c15f5e2016-04-28 01:19:10 -07001071 /* handle export callchains */
1072 tables->dbe.cpr = NULL;
1073 db_export_callchains = PyDict_GetItemString(main_dict,
1074 perf_db_export_callchains);
1075 if (db_export_callchains) {
1076 ret = PyObject_IsTrue(db_export_callchains);
1077 if (ret == -1)
1078 handler_call_die(perf_db_export_callchains);
1079 export_callchains = !!ret;
1080 }
1081
1082 if (export_callchains) {
1083 /*
1084 * Attempt to use the call path root from the call return
1085 * processor, if the call return processor is in use. Otherwise,
1086 * we allocate a new call path root. This prevents exporting
1087 * duplicate call path ids when both are in use simultaniously.
1088 */
1089 if (tables->dbe.crp)
1090 tables->dbe.cpr = tables->dbe.crp->cpr;
1091 else
1092 tables->dbe.cpr = call_path_root__new();
1093
1094 if (!tables->dbe.cpr)
Chris Phlipotaff63342016-05-07 02:17:00 -07001095 Py_FatalError("failed to create call path root");
Chris Phlipot2c15f5e2016-04-28 01:19:10 -07001096 }
1097
Adrian Hunterdf919b42014-10-23 13:45:14 +03001098 tables->db_export_mode = true;
1099 /*
1100 * Reserve per symbol space for symbol->db_id via symbol__priv()
1101 */
1102 symbol_conf.priv_size = sizeof(u64);
1103
1104 SET_TABLE_HANDLER(evsel);
1105 SET_TABLE_HANDLER(machine);
1106 SET_TABLE_HANDLER(thread);
1107 SET_TABLE_HANDLER(comm);
1108 SET_TABLE_HANDLER(comm_thread);
1109 SET_TABLE_HANDLER(dso);
1110 SET_TABLE_HANDLER(symbol);
Adrian Hunterc29414f2014-10-30 16:09:44 +02001111 SET_TABLE_HANDLER(branch_type);
Adrian Hunterdf919b42014-10-23 13:45:14 +03001112 SET_TABLE_HANDLER(sample);
Adrian Hunter6a703072014-10-30 16:09:47 +02001113 SET_TABLE_HANDLER(call_path);
1114 SET_TABLE_HANDLER(call_return);
Adrian Hunterdf919b42014-10-23 13:45:14 +03001115}
1116
Tom Zanussi7e4b21b2010-01-27 02:27:57 -06001117/*
1118 * Start trace script
1119 */
1120static int python_start_script(const char *script, int argc, const char **argv)
1121{
Adrian Hunterdf919b42014-10-23 13:45:14 +03001122 struct tables *tables = &tables_global;
Tom Zanussi7e4b21b2010-01-27 02:27:57 -06001123 const char **command_line;
1124 char buf[PATH_MAX];
1125 int i, err = 0;
1126 FILE *fp;
1127
1128 command_line = malloc((argc + 1) * sizeof(const char *));
1129 command_line[0] = script;
1130 for (i = 1; i < argc + 1; i++)
1131 command_line[i] = argv[i - 1];
1132
1133 Py_Initialize();
1134
1135 initperf_trace_context();
1136
1137 PySys_SetArgv(argc + 1, (char **)command_line);
1138
1139 fp = fopen(script, "r");
1140 if (!fp) {
1141 sprintf(buf, "Can't open python script \"%s\"", script);
1142 perror(buf);
1143 err = -1;
1144 goto error;
1145 }
1146
1147 err = PyRun_SimpleFile(fp, script);
1148 if (err) {
1149 fprintf(stderr, "Error running python script %s\n", script);
1150 goto error;
1151 }
1152
1153 err = run_start_sub();
1154 if (err) {
1155 fprintf(stderr, "Error starting python script %s\n", script);
1156 goto error;
1157 }
1158
Adrian Hunterdf919b42014-10-23 13:45:14 +03001159 set_table_handlers(tables);
1160
Adrian Hunterc29414f2014-10-30 16:09:44 +02001161 if (tables->db_export_mode) {
1162 err = db_export__branch_types(&tables->dbe);
1163 if (err)
1164 goto error;
1165 }
1166
Colin Ian King979ac252016-03-01 23:46:20 +00001167 free(command_line);
1168
Tom Zanussi7e4b21b2010-01-27 02:27:57 -06001169 return err;
1170error:
1171 Py_Finalize();
1172 free(command_line);
1173
1174 return err;
1175}
1176
Adrian Hunterd445dd22014-08-15 22:08:37 +03001177static int python_flush_script(void)
1178{
Adrian Hunter758008b2014-10-30 16:09:48 +02001179 struct tables *tables = &tables_global;
1180
1181 return db_export__flush(&tables->dbe);
Adrian Hunterd445dd22014-08-15 22:08:37 +03001182}
1183
Tom Zanussi7e4b21b2010-01-27 02:27:57 -06001184/*
1185 * Stop trace script
1186 */
1187static int python_stop_script(void)
1188{
Adrian Hunterdf919b42014-10-23 13:45:14 +03001189 struct tables *tables = &tables_global;
1190
Adrian Huntera5563ed2014-07-31 09:01:01 +03001191 try_call_object("trace_end", NULL);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -06001192
Adrian Hunterdf919b42014-10-23 13:45:14 +03001193 db_export__exit(&tables->dbe);
1194
Tom Zanussi7e4b21b2010-01-27 02:27:57 -06001195 Py_XDECREF(main_dict);
1196 Py_XDECREF(main_module);
1197 Py_Finalize();
1198
Adrian Huntera5563ed2014-07-31 09:01:01 +03001199 return 0;
Tom Zanussi7e4b21b2010-01-27 02:27:57 -06001200}
1201
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03001202static int python_generate_script(struct pevent *pevent, const char *outfile)
Tom Zanussi7e4b21b2010-01-27 02:27:57 -06001203{
Steven Rostedtaaf045f2012-04-06 00:47:56 +02001204 struct event_format *event = NULL;
Tom Zanussi7e4b21b2010-01-27 02:27:57 -06001205 struct format_field *f;
1206 char fname[PATH_MAX];
1207 int not_first, count;
1208 FILE *ofp;
1209
1210 sprintf(fname, "%s.py", outfile);
1211 ofp = fopen(fname, "w");
1212 if (ofp == NULL) {
1213 fprintf(stderr, "couldn't open %s\n", fname);
1214 return -1;
1215 }
Ingo Molnar133dc4c2010-11-16 18:45:39 +01001216 fprintf(ofp, "# perf script event handlers, "
1217 "generated by perf script -g python\n");
Tom Zanussi7e4b21b2010-01-27 02:27:57 -06001218
1219 fprintf(ofp, "# Licensed under the terms of the GNU GPL"
1220 " License version 2\n\n");
1221
1222 fprintf(ofp, "# The common_* event handler fields are the most useful "
1223 "fields common to\n");
1224
1225 fprintf(ofp, "# all events. They don't necessarily correspond to "
1226 "the 'common_*' fields\n");
1227
1228 fprintf(ofp, "# in the format files. Those fields not available as "
1229 "handler params can\n");
1230
1231 fprintf(ofp, "# be retrieved using Python functions of the form "
1232 "common_*(context).\n");
1233
1234 fprintf(ofp, "# See the perf-trace-python Documentation for the list "
1235 "of available functions.\n\n");
1236
1237 fprintf(ofp, "import os\n");
1238 fprintf(ofp, "import sys\n\n");
1239
1240 fprintf(ofp, "sys.path.append(os.environ['PERF_EXEC_PATH'] + \\\n");
1241 fprintf(ofp, "\t'/scripts/python/Perf-Trace-Util/lib/Perf/Trace')\n");
1242 fprintf(ofp, "\nfrom perf_trace_context import *\n");
1243 fprintf(ofp, "from Core import *\n\n\n");
1244
1245 fprintf(ofp, "def trace_begin():\n");
1246 fprintf(ofp, "\tprint \"in trace_begin\"\n\n");
1247
1248 fprintf(ofp, "def trace_end():\n");
1249 fprintf(ofp, "\tprint \"in trace_end\"\n\n");
1250
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03001251 while ((event = trace_find_next_event(pevent, event))) {
Tom Zanussi7e4b21b2010-01-27 02:27:57 -06001252 fprintf(ofp, "def %s__%s(", event->system, event->name);
1253 fprintf(ofp, "event_name, ");
1254 fprintf(ofp, "context, ");
1255 fprintf(ofp, "common_cpu,\n");
1256 fprintf(ofp, "\tcommon_secs, ");
1257 fprintf(ofp, "common_nsecs, ");
1258 fprintf(ofp, "common_pid, ");
1259 fprintf(ofp, "common_comm,\n\t");
Joseph Schuchart0f5f5bc2014-07-10 13:50:51 +02001260 fprintf(ofp, "common_callchain, ");
Tom Zanussi7e4b21b2010-01-27 02:27:57 -06001261
1262 not_first = 0;
1263 count = 0;
1264
1265 for (f = event->format.fields; f; f = f->next) {
1266 if (not_first++)
1267 fprintf(ofp, ", ");
1268 if (++count % 5 == 0)
1269 fprintf(ofp, "\n\t");
1270
1271 fprintf(ofp, "%s", f->name);
1272 }
1273 fprintf(ofp, "):\n");
1274
1275 fprintf(ofp, "\t\tprint_header(event_name, common_cpu, "
1276 "common_secs, common_nsecs,\n\t\t\t"
1277 "common_pid, common_comm)\n\n");
1278
1279 fprintf(ofp, "\t\tprint \"");
1280
1281 not_first = 0;
1282 count = 0;
1283
1284 for (f = event->format.fields; f; f = f->next) {
1285 if (not_first++)
1286 fprintf(ofp, ", ");
1287 if (count && count % 3 == 0) {
1288 fprintf(ofp, "\" \\\n\t\t\"");
1289 }
1290 count++;
1291
1292 fprintf(ofp, "%s=", f->name);
1293 if (f->flags & FIELD_IS_STRING ||
1294 f->flags & FIELD_IS_FLAG ||
Namhyung Kime646fe72014-05-29 13:44:55 +09001295 f->flags & FIELD_IS_ARRAY ||
Tom Zanussi7e4b21b2010-01-27 02:27:57 -06001296 f->flags & FIELD_IS_SYMBOLIC)
1297 fprintf(ofp, "%%s");
1298 else if (f->flags & FIELD_IS_SIGNED)
1299 fprintf(ofp, "%%d");
1300 else
1301 fprintf(ofp, "%%u");
1302 }
1303
Joseph Schuchart0f5f5bc2014-07-10 13:50:51 +02001304 fprintf(ofp, "\" %% \\\n\t\t(");
Tom Zanussi7e4b21b2010-01-27 02:27:57 -06001305
1306 not_first = 0;
1307 count = 0;
1308
1309 for (f = event->format.fields; f; f = f->next) {
1310 if (not_first++)
1311 fprintf(ofp, ", ");
1312
1313 if (++count % 5 == 0)
1314 fprintf(ofp, "\n\t\t");
1315
1316 if (f->flags & FIELD_IS_FLAG) {
1317 if ((count - 1) % 5 != 0) {
1318 fprintf(ofp, "\n\t\t");
1319 count = 4;
1320 }
1321 fprintf(ofp, "flag_str(\"");
1322 fprintf(ofp, "%s__%s\", ", event->system,
1323 event->name);
1324 fprintf(ofp, "\"%s\", %s)", f->name,
1325 f->name);
1326 } else if (f->flags & FIELD_IS_SYMBOLIC) {
1327 if ((count - 1) % 5 != 0) {
1328 fprintf(ofp, "\n\t\t");
1329 count = 4;
1330 }
1331 fprintf(ofp, "symbol_str(\"");
1332 fprintf(ofp, "%s__%s\", ", event->system,
1333 event->name);
1334 fprintf(ofp, "\"%s\", %s)", f->name,
1335 f->name);
1336 } else
1337 fprintf(ofp, "%s", f->name);
1338 }
1339
Joseph Schuchart0f5f5bc2014-07-10 13:50:51 +02001340 fprintf(ofp, ")\n\n");
1341
1342 fprintf(ofp, "\t\tfor node in common_callchain:");
1343 fprintf(ofp, "\n\t\t\tif 'sym' in node:");
1344 fprintf(ofp, "\n\t\t\t\tprint \"\\t[%%x] %%s\" %% (node['ip'], node['sym']['name'])");
1345 fprintf(ofp, "\n\t\t\telse:");
1346 fprintf(ofp, "\n\t\t\t\tprint \"\t[%%x]\" %% (node['ip'])\n\n");
1347 fprintf(ofp, "\t\tprint \"\\n\"\n\n");
1348
Tom Zanussi7e4b21b2010-01-27 02:27:57 -06001349 }
1350
1351 fprintf(ofp, "def trace_unhandled(event_name, context, "
Pierre Tardyc0251482010-05-31 23:12:09 +02001352 "event_fields_dict):\n");
Tom Zanussi7e4b21b2010-01-27 02:27:57 -06001353
Pierre Tardyc0251482010-05-31 23:12:09 +02001354 fprintf(ofp, "\t\tprint ' '.join(['%%s=%%s'%%(k,str(v))"
1355 "for k,v in sorted(event_fields_dict.items())])\n\n");
Tom Zanussi7e4b21b2010-01-27 02:27:57 -06001356
1357 fprintf(ofp, "def print_header("
1358 "event_name, cpu, secs, nsecs, pid, comm):\n"
1359 "\tprint \"%%-20s %%5u %%05u.%%09u %%8u %%-20s \" %% \\\n\t"
1360 "(event_name, cpu, secs, nsecs, pid, comm),\n");
1361
1362 fclose(ofp);
1363
1364 fprintf(stderr, "generated Python script: %s\n", fname);
1365
1366 return 0;
1367}
1368
1369struct scripting_ops python_scripting_ops = {
Jiri Olsaaef90262016-01-05 22:09:11 +01001370 .name = "Python",
1371 .start_script = python_start_script,
1372 .flush_script = python_flush_script,
1373 .stop_script = python_stop_script,
1374 .process_event = python_process_event,
1375 .process_stat = python_process_stat,
1376 .process_stat_interval = python_process_stat_interval,
1377 .generate_script = python_generate_script,
Tom Zanussi7e4b21b2010-01-27 02:27:57 -06001378};