blob: 118bc62850a8e599349dd0b6b8f1d2ff8333cde9 [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>
29
30#include "../../perf.h"
Jiri Olsa84f5d362014-07-14 23:46:48 +020031#include "../debug.h"
Arnaldo Carvalho de Melo8f651ea2014-10-09 16:12:24 -030032#include "../callchain.h"
Arnaldo Carvalho de Melofcf65bf2012-08-07 09:58:03 -030033#include "../evsel.h"
Tom Zanussi7e4b21b2010-01-27 02:27:57 -060034#include "../util.h"
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -020035#include "../event.h"
36#include "../thread.h"
Adrian Hunterdf919b42014-10-23 13:45:14 +030037#include "../comm.h"
38#include "../machine.h"
39#include "../db-export.h"
Adrian Hunter6a703072014-10-30 16:09:47 +020040#include "../thread-stack.h"
Tom Zanussi7e4b21b2010-01-27 02:27:57 -060041#include "../trace-event.h"
Joseph Schuchart0f5f5bc2014-07-10 13:50:51 +020042#include "../machine.h"
Tom Zanussi7e4b21b2010-01-27 02:27:57 -060043
44PyMODINIT_FUNC initperf_trace_context(void);
45
46#define FTRACE_MAX_EVENT \
47 ((1 << (sizeof(unsigned short) * 8)) - 1)
48
Steven Rostedtaaf045f2012-04-06 00:47:56 +020049struct event_format *events[FTRACE_MAX_EVENT];
Tom Zanussi7e4b21b2010-01-27 02:27:57 -060050
51#define MAX_FIELDS 64
52#define N_COMMON_FIELDS 7
53
54extern struct scripting_context *scripting_context;
55
56static char *cur_field_name;
57static int zero_flag_atom;
58
59static PyObject *main_module, *main_dict;
60
Adrian Hunterdf919b42014-10-23 13:45:14 +030061struct tables {
62 struct db_export dbe;
63 PyObject *evsel_handler;
64 PyObject *machine_handler;
65 PyObject *thread_handler;
66 PyObject *comm_handler;
67 PyObject *comm_thread_handler;
68 PyObject *dso_handler;
69 PyObject *symbol_handler;
Adrian Hunterc29414f2014-10-30 16:09:44 +020070 PyObject *branch_type_handler;
Adrian Hunterdf919b42014-10-23 13:45:14 +030071 PyObject *sample_handler;
Adrian Hunter6a703072014-10-30 16:09:47 +020072 PyObject *call_path_handler;
73 PyObject *call_return_handler;
Adrian Hunterdf919b42014-10-23 13:45:14 +030074 bool db_export_mode;
75};
76
77static struct tables tables_global;
78
Joseph Schuchart05f832e2014-07-09 16:16:31 +020079static void handler_call_die(const char *handler_name) NORETURN;
Tom Zanussi7e4b21b2010-01-27 02:27:57 -060080static void handler_call_die(const char *handler_name)
81{
82 PyErr_Print();
83 Py_FatalError("problem in Python trace event handler");
Joseph Schuchart05f832e2014-07-09 16:16:31 +020084 // Py_FatalError does not return
85 // but we have to make the compiler happy
86 abort();
Tom Zanussi7e4b21b2010-01-27 02:27:57 -060087}
88
Joseph Schuchartc0268e82013-10-24 10:10:51 -030089/*
90 * Insert val into into the dictionary and decrement the reference counter.
91 * This is necessary for dictionaries since PyDict_SetItemString() does not
92 * steal a reference, as opposed to PyTuple_SetItem().
93 */
94static void pydict_set_item_string_decref(PyObject *dict, const char *key, PyObject *val)
95{
96 PyDict_SetItemString(dict, key, val);
97 Py_DECREF(val);
98}
99
Adrian Huntera5563ed2014-07-31 09:01:01 +0300100static PyObject *get_handler(const char *handler_name)
101{
102 PyObject *handler;
103
104 handler = PyDict_GetItemString(main_dict, handler_name);
105 if (handler && !PyCallable_Check(handler))
106 return NULL;
107 return handler;
108}
109
110static void call_object(PyObject *handler, PyObject *args, const char *die_msg)
111{
112 PyObject *retval;
113
114 retval = PyObject_CallObject(handler, args);
115 if (retval == NULL)
116 handler_call_die(die_msg);
117 Py_DECREF(retval);
118}
119
120static void try_call_object(const char *handler_name, PyObject *args)
121{
122 PyObject *handler;
123
124 handler = get_handler(handler_name);
125 if (handler)
126 call_object(handler, args, handler_name);
127}
128
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600129static void define_value(enum print_arg_type field_type,
130 const char *ev_name,
131 const char *field_name,
132 const char *field_value,
133 const char *field_str)
134{
135 const char *handler_name = "define_flag_value";
Adrian Huntera5563ed2014-07-31 09:01:01 +0300136 PyObject *t;
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600137 unsigned long long value;
138 unsigned n = 0;
139
140 if (field_type == PRINT_SYMBOL)
141 handler_name = "define_symbolic_value";
142
Tom Zanussi44ad9cd2010-02-22 01:12:59 -0600143 t = PyTuple_New(4);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600144 if (!t)
145 Py_FatalError("couldn't create Python tuple");
146
147 value = eval_flag(field_value);
148
149 PyTuple_SetItem(t, n++, PyString_FromString(ev_name));
150 PyTuple_SetItem(t, n++, PyString_FromString(field_name));
151 PyTuple_SetItem(t, n++, PyInt_FromLong(value));
152 PyTuple_SetItem(t, n++, PyString_FromString(field_str));
153
Adrian Huntera5563ed2014-07-31 09:01:01 +0300154 try_call_object(handler_name, t);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600155
156 Py_DECREF(t);
157}
158
159static void define_values(enum print_arg_type field_type,
160 struct print_flag_sym *field,
161 const char *ev_name,
162 const char *field_name)
163{
164 define_value(field_type, ev_name, field_name, field->value,
165 field->str);
166
167 if (field->next)
168 define_values(field_type, field->next, ev_name, field_name);
169}
170
171static void define_field(enum print_arg_type field_type,
172 const char *ev_name,
173 const char *field_name,
174 const char *delim)
175{
176 const char *handler_name = "define_flag_field";
Adrian Huntera5563ed2014-07-31 09:01:01 +0300177 PyObject *t;
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600178 unsigned n = 0;
179
180 if (field_type == PRINT_SYMBOL)
181 handler_name = "define_symbolic_field";
182
Tom Zanussi44ad9cd2010-02-22 01:12:59 -0600183 if (field_type == PRINT_FLAGS)
184 t = PyTuple_New(3);
185 else
186 t = PyTuple_New(2);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600187 if (!t)
188 Py_FatalError("couldn't create Python tuple");
189
190 PyTuple_SetItem(t, n++, PyString_FromString(ev_name));
191 PyTuple_SetItem(t, n++, PyString_FromString(field_name));
192 if (field_type == PRINT_FLAGS)
193 PyTuple_SetItem(t, n++, PyString_FromString(delim));
194
Adrian Huntera5563ed2014-07-31 09:01:01 +0300195 try_call_object(handler_name, t);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600196
197 Py_DECREF(t);
198}
199
Steven Rostedtaaf045f2012-04-06 00:47:56 +0200200static void define_event_symbols(struct event_format *event,
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600201 const char *ev_name,
202 struct print_arg *args)
203{
204 switch (args->type) {
205 case PRINT_NULL:
206 break;
207 case PRINT_ATOM:
208 define_value(PRINT_FLAGS, ev_name, cur_field_name, "0",
209 args->atom.atom);
210 zero_flag_atom = 0;
211 break;
212 case PRINT_FIELD:
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -0300213 free(cur_field_name);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600214 cur_field_name = strdup(args->field.name);
215 break;
216 case PRINT_FLAGS:
217 define_event_symbols(event, ev_name, args->flags.field);
218 define_field(PRINT_FLAGS, ev_name, cur_field_name,
219 args->flags.delim);
220 define_values(PRINT_FLAGS, args->flags.flags, ev_name,
221 cur_field_name);
222 break;
223 case PRINT_SYMBOL:
224 define_event_symbols(event, ev_name, args->symbol.field);
225 define_field(PRINT_SYMBOL, ev_name, cur_field_name, NULL);
226 define_values(PRINT_SYMBOL, args->symbol.symbols, ev_name,
227 cur_field_name);
228 break;
Namhyung Kime080e6f2012-06-27 09:41:41 +0900229 case PRINT_HEX:
230 define_event_symbols(event, ev_name, args->hex.field);
231 define_event_symbols(event, ev_name, args->hex.size);
232 break;
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600233 case PRINT_STRING:
234 break;
235 case PRINT_TYPE:
236 define_event_symbols(event, ev_name, args->typecast.item);
237 break;
238 case PRINT_OP:
239 if (strcmp(args->op.op, ":") == 0)
240 zero_flag_atom = 1;
241 define_event_symbols(event, ev_name, args->op.left);
242 define_event_symbols(event, ev_name, args->op.right);
243 break;
244 default:
Steven Rostedtaaf045f2012-04-06 00:47:56 +0200245 /* gcc warns for these? */
246 case PRINT_BSTRING:
247 case PRINT_DYNAMIC_ARRAY:
248 case PRINT_FUNC:
Steven Rostedt (Red Hat)473a7782014-06-02 23:20:16 -0400249 case PRINT_BITMASK:
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600250 /* we should warn... */
251 return;
252 }
253
254 if (args->next)
255 define_event_symbols(event, ev_name, args->next);
256}
257
Arnaldo Carvalho de Melofcf65bf2012-08-07 09:58:03 -0300258static inline struct event_format *find_cache_event(struct perf_evsel *evsel)
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600259{
260 static char ev_name[256];
Steven Rostedtaaf045f2012-04-06 00:47:56 +0200261 struct event_format *event;
Arnaldo Carvalho de Melofcf65bf2012-08-07 09:58:03 -0300262 int type = evsel->attr.config;
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600263
Arnaldo Carvalho de Melofcf65bf2012-08-07 09:58:03 -0300264 /*
265 * XXX: Do we really need to cache this since now we have evsel->tp_format
266 * cached already? Need to re-read this "cache" routine that as well calls
267 * define_event_symbols() :-\
268 */
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600269 if (events[type])
270 return events[type];
271
Arnaldo Carvalho de Melofcf65bf2012-08-07 09:58:03 -0300272 events[type] = event = evsel->tp_format;
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600273 if (!event)
274 return NULL;
275
276 sprintf(ev_name, "%s__%s", event->system, event->name);
277
278 define_event_symbols(event, ev_name, event->print_fmt.args);
279
280 return event;
281}
282
Sebastian Andrzej Siewior33058b92014-05-27 18:14:33 +0200283static PyObject *get_field_numeric_entry(struct event_format *event,
284 struct format_field *field, void *data)
285{
Sebastian Andrzej Siewior8ac631c2014-05-27 18:14:34 +0200286 bool is_array = field->flags & FIELD_IS_ARRAY;
287 PyObject *obj, *list = NULL;
Sebastian Andrzej Siewior33058b92014-05-27 18:14:33 +0200288 unsigned long long val;
Sebastian Andrzej Siewior8ac631c2014-05-27 18:14:34 +0200289 unsigned int item_size, n_items, i;
Sebastian Andrzej Siewior33058b92014-05-27 18:14:33 +0200290
Sebastian Andrzej Siewior8ac631c2014-05-27 18:14:34 +0200291 if (is_array) {
292 list = PyList_New(field->arraylen);
293 item_size = field->size / field->arraylen;
294 n_items = field->arraylen;
Sebastian Andrzej Siewior33058b92014-05-27 18:14:33 +0200295 } else {
Sebastian Andrzej Siewior8ac631c2014-05-27 18:14:34 +0200296 item_size = field->size;
297 n_items = 1;
Sebastian Andrzej Siewior33058b92014-05-27 18:14:33 +0200298 }
Sebastian Andrzej Siewior8ac631c2014-05-27 18:14:34 +0200299
300 for (i = 0; i < n_items; i++) {
301
302 val = read_size(event, data + field->offset + i * item_size,
303 item_size);
304 if (field->flags & FIELD_IS_SIGNED) {
305 if ((long long)val >= LONG_MIN &&
306 (long long)val <= LONG_MAX)
307 obj = PyInt_FromLong(val);
308 else
309 obj = PyLong_FromLongLong(val);
310 } else {
311 if (val <= LONG_MAX)
312 obj = PyInt_FromLong(val);
313 else
314 obj = PyLong_FromUnsignedLongLong(val);
315 }
316 if (is_array)
317 PyList_SET_ITEM(list, i, obj);
318 }
319 if (is_array)
320 obj = list;
Sebastian Andrzej Siewior33058b92014-05-27 18:14:33 +0200321 return obj;
322}
323
Joseph Schuchart0f5f5bc2014-07-10 13:50:51 +0200324
325static PyObject *python_process_callchain(struct perf_sample *sample,
326 struct perf_evsel *evsel,
327 struct addr_location *al)
328{
329 PyObject *pylist;
330
331 pylist = PyList_New(0);
332 if (!pylist)
333 Py_FatalError("couldn't create Python list");
334
335 if (!symbol_conf.use_callchain || !sample->callchain)
336 goto exit;
337
Arnaldo Carvalho de Melocc8b7c22014-10-23 15:26:17 -0300338 if (thread__resolve_callchain(al->thread, evsel,
339 sample, NULL, NULL,
340 PERF_MAX_STACK_DEPTH) != 0) {
Joseph Schuchart0f5f5bc2014-07-10 13:50:51 +0200341 pr_err("Failed to resolve callchain. Skipping\n");
342 goto exit;
343 }
344 callchain_cursor_commit(&callchain_cursor);
345
346
347 while (1) {
348 PyObject *pyelem;
349 struct callchain_cursor_node *node;
350 node = callchain_cursor_current(&callchain_cursor);
351 if (!node)
352 break;
353
354 pyelem = PyDict_New();
355 if (!pyelem)
356 Py_FatalError("couldn't create Python dictionary");
357
358
359 pydict_set_item_string_decref(pyelem, "ip",
360 PyLong_FromUnsignedLongLong(node->ip));
361
362 if (node->sym) {
363 PyObject *pysym = PyDict_New();
364 if (!pysym)
365 Py_FatalError("couldn't create Python dictionary");
366 pydict_set_item_string_decref(pysym, "start",
367 PyLong_FromUnsignedLongLong(node->sym->start));
368 pydict_set_item_string_decref(pysym, "end",
369 PyLong_FromUnsignedLongLong(node->sym->end));
370 pydict_set_item_string_decref(pysym, "binding",
371 PyInt_FromLong(node->sym->binding));
372 pydict_set_item_string_decref(pysym, "name",
373 PyString_FromStringAndSize(node->sym->name,
374 node->sym->namelen));
375 pydict_set_item_string_decref(pyelem, "sym", pysym);
376 }
377
378 if (node->map) {
379 struct map *map = node->map;
380 const char *dsoname = "[unknown]";
381 if (map && map->dso && (map->dso->name || map->dso->long_name)) {
382 if (symbol_conf.show_kernel_path && map->dso->long_name)
383 dsoname = map->dso->long_name;
384 else if (map->dso->name)
385 dsoname = map->dso->name;
386 }
387 pydict_set_item_string_decref(pyelem, "dso",
388 PyString_FromString(dsoname));
389 }
390
391 callchain_cursor_advance(&callchain_cursor);
392 PyList_Append(pylist, pyelem);
393 Py_DECREF(pyelem);
394 }
395
396exit:
397 return pylist;
398}
399
400
Arnaldo Carvalho de Melob7fff6b52013-12-19 16:34:52 -0300401static void python_process_tracepoint(struct perf_sample *sample,
402 struct perf_evsel *evsel,
403 struct thread *thread,
404 struct addr_location *al)
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600405{
Adrian Huntera5563ed2014-07-31 09:01:01 +0300406 PyObject *handler, *context, *t, *obj, *callchain;
Joseph Schuchart0f5f5bc2014-07-10 13:50:51 +0200407 PyObject *dict = NULL;
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600408 static char handler_name[256];
409 struct format_field *field;
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600410 unsigned long s, ns;
Steven Rostedtaaf045f2012-04-06 00:47:56 +0200411 struct event_format *event;
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600412 unsigned n = 0;
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600413 int pid;
David Ahernbe6d8422011-03-09 22:23:23 -0700414 int cpu = sample->cpu;
415 void *data = sample->raw_data;
416 unsigned long long nsecs = sample->time;
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +0200417 const char *comm = thread__comm_str(thread);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600418
419 t = PyTuple_New(MAX_FIELDS);
420 if (!t)
421 Py_FatalError("couldn't create Python tuple");
422
Arnaldo Carvalho de Melofcf65bf2012-08-07 09:58:03 -0300423 event = find_cache_event(evsel);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600424 if (!event)
Arnaldo Carvalho de Melofcf65bf2012-08-07 09:58:03 -0300425 die("ug! no event found for type %d", (int)evsel->attr.config);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600426
Arnaldo Carvalho de Melo97822432012-08-07 23:50:21 -0300427 pid = raw_field_value(event, "common_pid", data);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600428
429 sprintf(handler_name, "%s__%s", event->system, event->name);
430
Adrian Huntera5563ed2014-07-31 09:01:01 +0300431 handler = get_handler(handler_name);
Pierre Tardyc0251482010-05-31 23:12:09 +0200432 if (!handler) {
433 dict = PyDict_New();
434 if (!dict)
435 Py_FatalError("couldn't create Python dict");
436 }
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600437 s = nsecs / NSECS_PER_SEC;
438 ns = nsecs - s * NSECS_PER_SEC;
439
440 scripting_context->event_data = data;
Tom Zanussi2de95332013-01-18 13:51:27 -0600441 scripting_context->pevent = evsel->tp_format->pevent;
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600442
443 context = PyCObject_FromVoidPtr(scripting_context, NULL);
444
445 PyTuple_SetItem(t, n++, PyString_FromString(handler_name));
Kyle McMartinfb7d0b32011-01-24 11:13:04 -0500446 PyTuple_SetItem(t, n++, context);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600447
Joseph Schuchart0f5f5bc2014-07-10 13:50:51 +0200448 /* ip unwinding */
449 callchain = python_process_callchain(sample, evsel, al);
450
Pierre Tardyc0251482010-05-31 23:12:09 +0200451 if (handler) {
452 PyTuple_SetItem(t, n++, PyInt_FromLong(cpu));
453 PyTuple_SetItem(t, n++, PyInt_FromLong(s));
454 PyTuple_SetItem(t, n++, PyInt_FromLong(ns));
455 PyTuple_SetItem(t, n++, PyInt_FromLong(pid));
456 PyTuple_SetItem(t, n++, PyString_FromString(comm));
Joseph Schuchart0f5f5bc2014-07-10 13:50:51 +0200457 PyTuple_SetItem(t, n++, callchain);
Pierre Tardyc0251482010-05-31 23:12:09 +0200458 } else {
Joseph Schuchartc0268e82013-10-24 10:10:51 -0300459 pydict_set_item_string_decref(dict, "common_cpu", PyInt_FromLong(cpu));
460 pydict_set_item_string_decref(dict, "common_s", PyInt_FromLong(s));
461 pydict_set_item_string_decref(dict, "common_ns", PyInt_FromLong(ns));
462 pydict_set_item_string_decref(dict, "common_pid", PyInt_FromLong(pid));
463 pydict_set_item_string_decref(dict, "common_comm", PyString_FromString(comm));
Joseph Schuchart0f5f5bc2014-07-10 13:50:51 +0200464 pydict_set_item_string_decref(dict, "common_callchain", callchain);
Pierre Tardyc0251482010-05-31 23:12:09 +0200465 }
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600466 for (field = event->format.fields; field; field = field->next) {
467 if (field->flags & FIELD_IS_STRING) {
468 int offset;
469 if (field->flags & FIELD_IS_DYNAMIC) {
470 offset = *(int *)(data + field->offset);
471 offset &= 0xffff;
472 } else
473 offset = field->offset;
Tom Zanussib1dcc032010-04-01 23:58:25 -0500474 obj = PyString_FromString((char *)data + offset);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600475 } else { /* FIELD_IS_NUMERIC */
Sebastian Andrzej Siewior33058b92014-05-27 18:14:33 +0200476 obj = get_field_numeric_entry(event, field, data);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600477 }
Pierre Tardyc0251482010-05-31 23:12:09 +0200478 if (handler)
479 PyTuple_SetItem(t, n++, obj);
480 else
Joseph Schuchartc0268e82013-10-24 10:10:51 -0300481 pydict_set_item_string_decref(dict, field->name, obj);
Pierre Tardyc0251482010-05-31 23:12:09 +0200482
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600483 }
Joseph Schuchart0f5f5bc2014-07-10 13:50:51 +0200484
Pierre Tardyc0251482010-05-31 23:12:09 +0200485 if (!handler)
486 PyTuple_SetItem(t, n++, dict);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600487
488 if (_PyTuple_Resize(&t, n) == -1)
489 Py_FatalError("error resizing Python tuple");
490
Pierre Tardyc0251482010-05-31 23:12:09 +0200491 if (handler) {
Adrian Huntera5563ed2014-07-31 09:01:01 +0300492 call_object(handler, t, handler_name);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600493 } else {
Adrian Huntera5563ed2014-07-31 09:01:01 +0300494 try_call_object("trace_unhandled", t);
Pierre Tardyc0251482010-05-31 23:12:09 +0200495 Py_DECREF(dict);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600496 }
497
498 Py_DECREF(t);
499}
500
Adrian Hunterdf919b42014-10-23 13:45:14 +0300501static PyObject *tuple_new(unsigned int sz)
502{
503 PyObject *t;
504
505 t = PyTuple_New(sz);
506 if (!t)
507 Py_FatalError("couldn't create Python tuple");
508 return t;
509}
510
511static int tuple_set_u64(PyObject *t, unsigned int pos, u64 val)
512{
513#if BITS_PER_LONG == 64
514 return PyTuple_SetItem(t, pos, PyInt_FromLong(val));
515#endif
516#if BITS_PER_LONG == 32
517 return PyTuple_SetItem(t, pos, PyLong_FromLongLong(val));
518#endif
519}
520
521static int tuple_set_s32(PyObject *t, unsigned int pos, s32 val)
522{
523 return PyTuple_SetItem(t, pos, PyInt_FromLong(val));
524}
525
526static int tuple_set_string(PyObject *t, unsigned int pos, const char *s)
527{
528 return PyTuple_SetItem(t, pos, PyString_FromString(s));
529}
530
531static int python_export_evsel(struct db_export *dbe, struct perf_evsel *evsel)
532{
533 struct tables *tables = container_of(dbe, struct tables, dbe);
534 PyObject *t;
535
536 t = tuple_new(2);
537
538 tuple_set_u64(t, 0, evsel->db_id);
539 tuple_set_string(t, 1, perf_evsel__name(evsel));
540
541 call_object(tables->evsel_handler, t, "evsel_table");
542
543 Py_DECREF(t);
544
545 return 0;
546}
547
548static int python_export_machine(struct db_export *dbe,
549 struct machine *machine)
550{
551 struct tables *tables = container_of(dbe, struct tables, dbe);
552 PyObject *t;
553
554 t = tuple_new(3);
555
556 tuple_set_u64(t, 0, machine->db_id);
557 tuple_set_s32(t, 1, machine->pid);
558 tuple_set_string(t, 2, machine->root_dir ? machine->root_dir : "");
559
560 call_object(tables->machine_handler, t, "machine_table");
561
562 Py_DECREF(t);
563
564 return 0;
565}
566
567static int python_export_thread(struct db_export *dbe, struct thread *thread,
568 u64 main_thread_db_id, struct machine *machine)
569{
570 struct tables *tables = container_of(dbe, struct tables, dbe);
571 PyObject *t;
572
573 t = tuple_new(5);
574
575 tuple_set_u64(t, 0, thread->db_id);
576 tuple_set_u64(t, 1, machine->db_id);
577 tuple_set_u64(t, 2, main_thread_db_id);
578 tuple_set_s32(t, 3, thread->pid_);
579 tuple_set_s32(t, 4, thread->tid);
580
581 call_object(tables->thread_handler, t, "thread_table");
582
583 Py_DECREF(t);
584
585 return 0;
586}
587
588static int python_export_comm(struct db_export *dbe, struct comm *comm)
589{
590 struct tables *tables = container_of(dbe, struct tables, dbe);
591 PyObject *t;
592
593 t = tuple_new(2);
594
595 tuple_set_u64(t, 0, comm->db_id);
596 tuple_set_string(t, 1, comm__str(comm));
597
598 call_object(tables->comm_handler, t, "comm_table");
599
600 Py_DECREF(t);
601
602 return 0;
603}
604
605static int python_export_comm_thread(struct db_export *dbe, u64 db_id,
606 struct comm *comm, struct thread *thread)
607{
608 struct tables *tables = container_of(dbe, struct tables, dbe);
609 PyObject *t;
610
611 t = tuple_new(3);
612
613 tuple_set_u64(t, 0, db_id);
614 tuple_set_u64(t, 1, comm->db_id);
615 tuple_set_u64(t, 2, thread->db_id);
616
617 call_object(tables->comm_thread_handler, t, "comm_thread_table");
618
619 Py_DECREF(t);
620
621 return 0;
622}
623
624static int python_export_dso(struct db_export *dbe, struct dso *dso,
625 struct machine *machine)
626{
627 struct tables *tables = container_of(dbe, struct tables, dbe);
628 char sbuild_id[BUILD_ID_SIZE * 2 + 1];
629 PyObject *t;
630
631 build_id__sprintf(dso->build_id, sizeof(dso->build_id), sbuild_id);
632
633 t = tuple_new(5);
634
635 tuple_set_u64(t, 0, dso->db_id);
636 tuple_set_u64(t, 1, machine->db_id);
637 tuple_set_string(t, 2, dso->short_name);
638 tuple_set_string(t, 3, dso->long_name);
639 tuple_set_string(t, 4, sbuild_id);
640
641 call_object(tables->dso_handler, t, "dso_table");
642
643 Py_DECREF(t);
644
645 return 0;
646}
647
648static int python_export_symbol(struct db_export *dbe, struct symbol *sym,
649 struct dso *dso)
650{
651 struct tables *tables = container_of(dbe, struct tables, dbe);
652 u64 *sym_db_id = symbol__priv(sym);
653 PyObject *t;
654
655 t = tuple_new(6);
656
657 tuple_set_u64(t, 0, *sym_db_id);
658 tuple_set_u64(t, 1, dso->db_id);
659 tuple_set_u64(t, 2, sym->start);
660 tuple_set_u64(t, 3, sym->end);
661 tuple_set_s32(t, 4, sym->binding);
662 tuple_set_string(t, 5, sym->name);
663
664 call_object(tables->symbol_handler, t, "symbol_table");
665
666 Py_DECREF(t);
667
668 return 0;
669}
670
Adrian Hunterc29414f2014-10-30 16:09:44 +0200671static int python_export_branch_type(struct db_export *dbe, u32 branch_type,
672 const char *name)
673{
674 struct tables *tables = container_of(dbe, struct tables, dbe);
675 PyObject *t;
676
677 t = tuple_new(2);
678
679 tuple_set_s32(t, 0, branch_type);
680 tuple_set_string(t, 1, name);
681
682 call_object(tables->branch_type_handler, t, "branch_type_table");
683
684 Py_DECREF(t);
685
686 return 0;
687}
688
Adrian Hunterdf919b42014-10-23 13:45:14 +0300689static int python_export_sample(struct db_export *dbe,
690 struct export_sample *es)
691{
692 struct tables *tables = container_of(dbe, struct tables, dbe);
693 PyObject *t;
694
Adrian Hunterc29414f2014-10-30 16:09:44 +0200695 t = tuple_new(21);
Adrian Hunterdf919b42014-10-23 13:45:14 +0300696
697 tuple_set_u64(t, 0, es->db_id);
698 tuple_set_u64(t, 1, es->evsel->db_id);
699 tuple_set_u64(t, 2, es->al->machine->db_id);
700 tuple_set_u64(t, 3, es->thread->db_id);
701 tuple_set_u64(t, 4, es->comm_db_id);
702 tuple_set_u64(t, 5, es->dso_db_id);
703 tuple_set_u64(t, 6, es->sym_db_id);
704 tuple_set_u64(t, 7, es->offset);
705 tuple_set_u64(t, 8, es->sample->ip);
706 tuple_set_u64(t, 9, es->sample->time);
707 tuple_set_s32(t, 10, es->sample->cpu);
708 tuple_set_u64(t, 11, es->addr_dso_db_id);
709 tuple_set_u64(t, 12, es->addr_sym_db_id);
710 tuple_set_u64(t, 13, es->addr_offset);
711 tuple_set_u64(t, 14, es->sample->addr);
712 tuple_set_u64(t, 15, es->sample->period);
713 tuple_set_u64(t, 16, es->sample->weight);
714 tuple_set_u64(t, 17, es->sample->transaction);
715 tuple_set_u64(t, 18, es->sample->data_src);
Adrian Hunterc29414f2014-10-30 16:09:44 +0200716 tuple_set_s32(t, 19, es->sample->flags & PERF_BRANCH_MASK);
717 tuple_set_s32(t, 20, !!(es->sample->flags & PERF_IP_FLAG_IN_TX));
Adrian Hunterdf919b42014-10-23 13:45:14 +0300718
719 call_object(tables->sample_handler, t, "sample_table");
720
721 Py_DECREF(t);
722
723 return 0;
724}
725
Adrian Hunter6a703072014-10-30 16:09:47 +0200726static int python_export_call_path(struct db_export *dbe, struct call_path *cp)
727{
728 struct tables *tables = container_of(dbe, struct tables, dbe);
729 PyObject *t;
730 u64 parent_db_id, sym_db_id;
731
732 parent_db_id = cp->parent ? cp->parent->db_id : 0;
733 sym_db_id = cp->sym ? *(u64 *)symbol__priv(cp->sym) : 0;
734
735 t = tuple_new(4);
736
737 tuple_set_u64(t, 0, cp->db_id);
738 tuple_set_u64(t, 1, parent_db_id);
739 tuple_set_u64(t, 2, sym_db_id);
740 tuple_set_u64(t, 3, cp->ip);
741
742 call_object(tables->call_path_handler, t, "call_path_table");
743
744 Py_DECREF(t);
745
746 return 0;
747}
748
749static int python_export_call_return(struct db_export *dbe,
750 struct call_return *cr)
751{
752 struct tables *tables = container_of(dbe, struct tables, dbe);
753 u64 comm_db_id = cr->comm ? cr->comm->db_id : 0;
754 PyObject *t;
755
756 t = tuple_new(11);
757
758 tuple_set_u64(t, 0, cr->db_id);
759 tuple_set_u64(t, 1, cr->thread->db_id);
760 tuple_set_u64(t, 2, comm_db_id);
761 tuple_set_u64(t, 3, cr->cp->db_id);
762 tuple_set_u64(t, 4, cr->call_time);
763 tuple_set_u64(t, 5, cr->return_time);
764 tuple_set_u64(t, 6, cr->branch_count);
765 tuple_set_u64(t, 7, cr->call_ref);
766 tuple_set_u64(t, 8, cr->return_ref);
767 tuple_set_u64(t, 9, cr->cp->parent->db_id);
768 tuple_set_s32(t, 10, cr->flags);
769
770 call_object(tables->call_return_handler, t, "call_return_table");
771
772 Py_DECREF(t);
773
774 return 0;
775}
776
777static int python_process_call_return(struct call_return *cr, void *data)
778{
779 struct db_export *dbe = data;
780
781 return db_export__call_return(dbe, cr);
782}
783
Arnaldo Carvalho de Melob7fff6b52013-12-19 16:34:52 -0300784static void python_process_general_event(struct perf_sample *sample,
Feng Tang6a6daec2012-08-08 17:57:51 +0800785 struct perf_evsel *evsel,
David Ahern2eaa1b42013-07-18 16:06:15 -0600786 struct thread *thread,
Feng Tang87b6a3a2012-08-09 13:46:13 +0800787 struct addr_location *al)
Feng Tang6a6daec2012-08-08 17:57:51 +0800788{
Adrian Huntera5563ed2014-07-31 09:01:01 +0300789 PyObject *handler, *t, *dict, *callchain, *dict_sample;
Feng Tang6a6daec2012-08-08 17:57:51 +0800790 static char handler_name[64];
791 unsigned n = 0;
Feng Tang6a6daec2012-08-08 17:57:51 +0800792
Feng Tangfd6b8582012-08-08 17:57:53 +0800793 /*
794 * Use the MAX_FIELDS to make the function expandable, though
Feng Tang87b6a3a2012-08-09 13:46:13 +0800795 * currently there is only one item for the tuple.
Feng Tangfd6b8582012-08-08 17:57:53 +0800796 */
Feng Tang6a6daec2012-08-08 17:57:51 +0800797 t = PyTuple_New(MAX_FIELDS);
798 if (!t)
799 Py_FatalError("couldn't create Python tuple");
800
Feng Tangfd6b8582012-08-08 17:57:53 +0800801 dict = PyDict_New();
802 if (!dict)
803 Py_FatalError("couldn't create Python dictionary");
804
Joseph Schuchart57608cf2014-07-10 13:50:56 +0200805 dict_sample = PyDict_New();
806 if (!dict_sample)
807 Py_FatalError("couldn't create Python dictionary");
808
Feng Tang6a6daec2012-08-08 17:57:51 +0800809 snprintf(handler_name, sizeof(handler_name), "%s", "process_event");
810
Adrian Huntera5563ed2014-07-31 09:01:01 +0300811 handler = get_handler(handler_name);
812 if (!handler)
Feng Tang6a6daec2012-08-08 17:57:51 +0800813 goto exit;
Feng Tang6a6daec2012-08-08 17:57:51 +0800814
Joseph Schuchartc0268e82013-10-24 10:10:51 -0300815 pydict_set_item_string_decref(dict, "ev_name", PyString_FromString(perf_evsel__name(evsel)));
816 pydict_set_item_string_decref(dict, "attr", PyString_FromStringAndSize(
Feng Tangfd6b8582012-08-08 17:57:53 +0800817 (const char *)&evsel->attr, sizeof(evsel->attr)));
Joseph Schuchart57608cf2014-07-10 13:50:56 +0200818
819 pydict_set_item_string_decref(dict_sample, "pid",
820 PyInt_FromLong(sample->pid));
821 pydict_set_item_string_decref(dict_sample, "tid",
822 PyInt_FromLong(sample->tid));
823 pydict_set_item_string_decref(dict_sample, "cpu",
824 PyInt_FromLong(sample->cpu));
825 pydict_set_item_string_decref(dict_sample, "ip",
826 PyLong_FromUnsignedLongLong(sample->ip));
827 pydict_set_item_string_decref(dict_sample, "time",
828 PyLong_FromUnsignedLongLong(sample->time));
829 pydict_set_item_string_decref(dict_sample, "period",
830 PyLong_FromUnsignedLongLong(sample->period));
831 pydict_set_item_string_decref(dict, "sample", dict_sample);
832
Joseph Schuchartc0268e82013-10-24 10:10:51 -0300833 pydict_set_item_string_decref(dict, "raw_buf", PyString_FromStringAndSize(
Feng Tangfd6b8582012-08-08 17:57:53 +0800834 (const char *)sample->raw_data, sample->raw_size));
Joseph Schuchartc0268e82013-10-24 10:10:51 -0300835 pydict_set_item_string_decref(dict, "comm",
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +0200836 PyString_FromString(thread__comm_str(thread)));
Feng Tangfd6b8582012-08-08 17:57:53 +0800837 if (al->map) {
Joseph Schuchartc0268e82013-10-24 10:10:51 -0300838 pydict_set_item_string_decref(dict, "dso",
Feng Tangfd6b8582012-08-08 17:57:53 +0800839 PyString_FromString(al->map->dso->name));
840 }
841 if (al->sym) {
Joseph Schuchartc0268e82013-10-24 10:10:51 -0300842 pydict_set_item_string_decref(dict, "symbol",
Feng Tangfd6b8582012-08-08 17:57:53 +0800843 PyString_FromString(al->sym->name));
844 }
Feng Tang6a6daec2012-08-08 17:57:51 +0800845
Joseph Schuchart0f5f5bc2014-07-10 13:50:51 +0200846 /* ip unwinding */
847 callchain = python_process_callchain(sample, evsel, al);
848 pydict_set_item_string_decref(dict, "callchain", callchain);
849
Feng Tangfd6b8582012-08-08 17:57:53 +0800850 PyTuple_SetItem(t, n++, dict);
Feng Tang6a6daec2012-08-08 17:57:51 +0800851 if (_PyTuple_Resize(&t, n) == -1)
852 Py_FatalError("error resizing Python tuple");
853
Adrian Huntera5563ed2014-07-31 09:01:01 +0300854 call_object(handler, t, handler_name);
Feng Tang6a6daec2012-08-08 17:57:51 +0800855exit:
Feng Tangfd6b8582012-08-08 17:57:53 +0800856 Py_DECREF(dict);
Feng Tang6a6daec2012-08-08 17:57:51 +0800857 Py_DECREF(t);
858}
859
Adrian Hunterdf919b42014-10-23 13:45:14 +0300860static void python_process_event(union perf_event *event,
Feng Tang6a6daec2012-08-08 17:57:51 +0800861 struct perf_sample *sample,
862 struct perf_evsel *evsel,
David Ahern2eaa1b42013-07-18 16:06:15 -0600863 struct thread *thread,
Feng Tang73994dc2012-08-08 17:57:52 +0800864 struct addr_location *al)
Feng Tang6a6daec2012-08-08 17:57:51 +0800865{
Adrian Hunterdf919b42014-10-23 13:45:14 +0300866 struct tables *tables = &tables_global;
867
Feng Tang6a6daec2012-08-08 17:57:51 +0800868 switch (evsel->attr.type) {
869 case PERF_TYPE_TRACEPOINT:
Arnaldo Carvalho de Melob7fff6b52013-12-19 16:34:52 -0300870 python_process_tracepoint(sample, evsel, thread, al);
Feng Tang6a6daec2012-08-08 17:57:51 +0800871 break;
872 /* Reserve for future process_hw/sw/raw APIs */
873 default:
Adrian Hunterdf919b42014-10-23 13:45:14 +0300874 if (tables->db_export_mode)
875 db_export__sample(&tables->dbe, event, sample, evsel,
876 thread, al);
877 else
878 python_process_general_event(sample, evsel, thread, al);
Feng Tang6a6daec2012-08-08 17:57:51 +0800879 }
880}
881
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600882static int run_start_sub(void)
883{
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600884 main_module = PyImport_AddModule("__main__");
885 if (main_module == NULL)
886 return -1;
887 Py_INCREF(main_module);
888
889 main_dict = PyModule_GetDict(main_module);
Adrian Huntera5563ed2014-07-31 09:01:01 +0300890 if (main_dict == NULL)
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600891 goto error;
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600892 Py_INCREF(main_dict);
893
Adrian Huntera5563ed2014-07-31 09:01:01 +0300894 try_call_object("trace_begin", NULL);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600895
Adrian Huntera5563ed2014-07-31 09:01:01 +0300896 return 0;
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600897
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600898error:
899 Py_XDECREF(main_dict);
900 Py_XDECREF(main_module);
Adrian Huntera5563ed2014-07-31 09:01:01 +0300901 return -1;
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600902}
903
Adrian Hunterdf919b42014-10-23 13:45:14 +0300904#define SET_TABLE_HANDLER_(name, handler_name, table_name) do { \
905 tables->handler_name = get_handler(#table_name); \
906 if (tables->handler_name) \
907 tables->dbe.export_ ## name = python_export_ ## name; \
908} while (0)
909
910#define SET_TABLE_HANDLER(name) \
911 SET_TABLE_HANDLER_(name, name ## _handler, name ## _table)
912
913static void set_table_handlers(struct tables *tables)
914{
915 const char *perf_db_export_mode = "perf_db_export_mode";
Adrian Hunter6a703072014-10-30 16:09:47 +0200916 const char *perf_db_export_calls = "perf_db_export_calls";
917 PyObject *db_export_mode, *db_export_calls;
918 bool export_calls = false;
Adrian Hunterdf919b42014-10-23 13:45:14 +0300919 int ret;
920
921 memset(tables, 0, sizeof(struct tables));
922 if (db_export__init(&tables->dbe))
923 Py_FatalError("failed to initialize export");
924
925 db_export_mode = PyDict_GetItemString(main_dict, perf_db_export_mode);
926 if (!db_export_mode)
927 return;
928
929 ret = PyObject_IsTrue(db_export_mode);
930 if (ret == -1)
931 handler_call_die(perf_db_export_mode);
932 if (!ret)
933 return;
934
Adrian Hunter6a703072014-10-30 16:09:47 +0200935 tables->dbe.crp = NULL;
936 db_export_calls = PyDict_GetItemString(main_dict, perf_db_export_calls);
937 if (db_export_calls) {
938 ret = PyObject_IsTrue(db_export_calls);
939 if (ret == -1)
940 handler_call_die(perf_db_export_calls);
941 export_calls = !!ret;
942 }
943
944 if (export_calls) {
945 tables->dbe.crp =
946 call_return_processor__new(python_process_call_return,
947 &tables->dbe);
948 if (!tables->dbe.crp)
949 Py_FatalError("failed to create calls processor");
950 }
951
Adrian Hunterdf919b42014-10-23 13:45:14 +0300952 tables->db_export_mode = true;
953 /*
954 * Reserve per symbol space for symbol->db_id via symbol__priv()
955 */
956 symbol_conf.priv_size = sizeof(u64);
957
958 SET_TABLE_HANDLER(evsel);
959 SET_TABLE_HANDLER(machine);
960 SET_TABLE_HANDLER(thread);
961 SET_TABLE_HANDLER(comm);
962 SET_TABLE_HANDLER(comm_thread);
963 SET_TABLE_HANDLER(dso);
964 SET_TABLE_HANDLER(symbol);
Adrian Hunterc29414f2014-10-30 16:09:44 +0200965 SET_TABLE_HANDLER(branch_type);
Adrian Hunterdf919b42014-10-23 13:45:14 +0300966 SET_TABLE_HANDLER(sample);
Adrian Hunter6a703072014-10-30 16:09:47 +0200967 SET_TABLE_HANDLER(call_path);
968 SET_TABLE_HANDLER(call_return);
Adrian Hunterdf919b42014-10-23 13:45:14 +0300969}
970
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600971/*
972 * Start trace script
973 */
974static int python_start_script(const char *script, int argc, const char **argv)
975{
Adrian Hunterdf919b42014-10-23 13:45:14 +0300976 struct tables *tables = &tables_global;
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600977 const char **command_line;
978 char buf[PATH_MAX];
979 int i, err = 0;
980 FILE *fp;
981
982 command_line = malloc((argc + 1) * sizeof(const char *));
983 command_line[0] = script;
984 for (i = 1; i < argc + 1; i++)
985 command_line[i] = argv[i - 1];
986
987 Py_Initialize();
988
989 initperf_trace_context();
990
991 PySys_SetArgv(argc + 1, (char **)command_line);
992
993 fp = fopen(script, "r");
994 if (!fp) {
995 sprintf(buf, "Can't open python script \"%s\"", script);
996 perror(buf);
997 err = -1;
998 goto error;
999 }
1000
1001 err = PyRun_SimpleFile(fp, script);
1002 if (err) {
1003 fprintf(stderr, "Error running python script %s\n", script);
1004 goto error;
1005 }
1006
1007 err = run_start_sub();
1008 if (err) {
1009 fprintf(stderr, "Error starting python script %s\n", script);
1010 goto error;
1011 }
1012
1013 free(command_line);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -06001014
Adrian Hunterdf919b42014-10-23 13:45:14 +03001015 set_table_handlers(tables);
1016
Adrian Hunterc29414f2014-10-30 16:09:44 +02001017 if (tables->db_export_mode) {
1018 err = db_export__branch_types(&tables->dbe);
1019 if (err)
1020 goto error;
1021 }
1022
Tom Zanussi7e4b21b2010-01-27 02:27:57 -06001023 return err;
1024error:
1025 Py_Finalize();
1026 free(command_line);
1027
1028 return err;
1029}
1030
Adrian Hunterd445dd22014-08-15 22:08:37 +03001031static int python_flush_script(void)
1032{
Adrian Hunter758008b2014-10-30 16:09:48 +02001033 struct tables *tables = &tables_global;
1034
1035 return db_export__flush(&tables->dbe);
Adrian Hunterd445dd22014-08-15 22:08:37 +03001036}
1037
Tom Zanussi7e4b21b2010-01-27 02:27:57 -06001038/*
1039 * Stop trace script
1040 */
1041static int python_stop_script(void)
1042{
Adrian Hunterdf919b42014-10-23 13:45:14 +03001043 struct tables *tables = &tables_global;
1044
Adrian Huntera5563ed2014-07-31 09:01:01 +03001045 try_call_object("trace_end", NULL);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -06001046
Adrian Hunterdf919b42014-10-23 13:45:14 +03001047 db_export__exit(&tables->dbe);
1048
Tom Zanussi7e4b21b2010-01-27 02:27:57 -06001049 Py_XDECREF(main_dict);
1050 Py_XDECREF(main_module);
1051 Py_Finalize();
1052
Adrian Huntera5563ed2014-07-31 09:01:01 +03001053 return 0;
Tom Zanussi7e4b21b2010-01-27 02:27:57 -06001054}
1055
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03001056static int python_generate_script(struct pevent *pevent, const char *outfile)
Tom Zanussi7e4b21b2010-01-27 02:27:57 -06001057{
Steven Rostedtaaf045f2012-04-06 00:47:56 +02001058 struct event_format *event = NULL;
Tom Zanussi7e4b21b2010-01-27 02:27:57 -06001059 struct format_field *f;
1060 char fname[PATH_MAX];
1061 int not_first, count;
1062 FILE *ofp;
1063
1064 sprintf(fname, "%s.py", outfile);
1065 ofp = fopen(fname, "w");
1066 if (ofp == NULL) {
1067 fprintf(stderr, "couldn't open %s\n", fname);
1068 return -1;
1069 }
Ingo Molnar133dc4c2010-11-16 18:45:39 +01001070 fprintf(ofp, "# perf script event handlers, "
1071 "generated by perf script -g python\n");
Tom Zanussi7e4b21b2010-01-27 02:27:57 -06001072
1073 fprintf(ofp, "# Licensed under the terms of the GNU GPL"
1074 " License version 2\n\n");
1075
1076 fprintf(ofp, "# The common_* event handler fields are the most useful "
1077 "fields common to\n");
1078
1079 fprintf(ofp, "# all events. They don't necessarily correspond to "
1080 "the 'common_*' fields\n");
1081
1082 fprintf(ofp, "# in the format files. Those fields not available as "
1083 "handler params can\n");
1084
1085 fprintf(ofp, "# be retrieved using Python functions of the form "
1086 "common_*(context).\n");
1087
1088 fprintf(ofp, "# See the perf-trace-python Documentation for the list "
1089 "of available functions.\n\n");
1090
1091 fprintf(ofp, "import os\n");
1092 fprintf(ofp, "import sys\n\n");
1093
1094 fprintf(ofp, "sys.path.append(os.environ['PERF_EXEC_PATH'] + \\\n");
1095 fprintf(ofp, "\t'/scripts/python/Perf-Trace-Util/lib/Perf/Trace')\n");
1096 fprintf(ofp, "\nfrom perf_trace_context import *\n");
1097 fprintf(ofp, "from Core import *\n\n\n");
1098
1099 fprintf(ofp, "def trace_begin():\n");
1100 fprintf(ofp, "\tprint \"in trace_begin\"\n\n");
1101
1102 fprintf(ofp, "def trace_end():\n");
1103 fprintf(ofp, "\tprint \"in trace_end\"\n\n");
1104
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03001105 while ((event = trace_find_next_event(pevent, event))) {
Tom Zanussi7e4b21b2010-01-27 02:27:57 -06001106 fprintf(ofp, "def %s__%s(", event->system, event->name);
1107 fprintf(ofp, "event_name, ");
1108 fprintf(ofp, "context, ");
1109 fprintf(ofp, "common_cpu,\n");
1110 fprintf(ofp, "\tcommon_secs, ");
1111 fprintf(ofp, "common_nsecs, ");
1112 fprintf(ofp, "common_pid, ");
1113 fprintf(ofp, "common_comm,\n\t");
Joseph Schuchart0f5f5bc2014-07-10 13:50:51 +02001114 fprintf(ofp, "common_callchain, ");
Tom Zanussi7e4b21b2010-01-27 02:27:57 -06001115
1116 not_first = 0;
1117 count = 0;
1118
1119 for (f = event->format.fields; f; f = f->next) {
1120 if (not_first++)
1121 fprintf(ofp, ", ");
1122 if (++count % 5 == 0)
1123 fprintf(ofp, "\n\t");
1124
1125 fprintf(ofp, "%s", f->name);
1126 }
1127 fprintf(ofp, "):\n");
1128
1129 fprintf(ofp, "\t\tprint_header(event_name, common_cpu, "
1130 "common_secs, common_nsecs,\n\t\t\t"
1131 "common_pid, common_comm)\n\n");
1132
1133 fprintf(ofp, "\t\tprint \"");
1134
1135 not_first = 0;
1136 count = 0;
1137
1138 for (f = event->format.fields; f; f = f->next) {
1139 if (not_first++)
1140 fprintf(ofp, ", ");
1141 if (count && count % 3 == 0) {
1142 fprintf(ofp, "\" \\\n\t\t\"");
1143 }
1144 count++;
1145
1146 fprintf(ofp, "%s=", f->name);
1147 if (f->flags & FIELD_IS_STRING ||
1148 f->flags & FIELD_IS_FLAG ||
Namhyung Kime646fe72014-05-29 13:44:55 +09001149 f->flags & FIELD_IS_ARRAY ||
Tom Zanussi7e4b21b2010-01-27 02:27:57 -06001150 f->flags & FIELD_IS_SYMBOLIC)
1151 fprintf(ofp, "%%s");
1152 else if (f->flags & FIELD_IS_SIGNED)
1153 fprintf(ofp, "%%d");
1154 else
1155 fprintf(ofp, "%%u");
1156 }
1157
Joseph Schuchart0f5f5bc2014-07-10 13:50:51 +02001158 fprintf(ofp, "\" %% \\\n\t\t(");
Tom Zanussi7e4b21b2010-01-27 02:27:57 -06001159
1160 not_first = 0;
1161 count = 0;
1162
1163 for (f = event->format.fields; f; f = f->next) {
1164 if (not_first++)
1165 fprintf(ofp, ", ");
1166
1167 if (++count % 5 == 0)
1168 fprintf(ofp, "\n\t\t");
1169
1170 if (f->flags & FIELD_IS_FLAG) {
1171 if ((count - 1) % 5 != 0) {
1172 fprintf(ofp, "\n\t\t");
1173 count = 4;
1174 }
1175 fprintf(ofp, "flag_str(\"");
1176 fprintf(ofp, "%s__%s\", ", event->system,
1177 event->name);
1178 fprintf(ofp, "\"%s\", %s)", f->name,
1179 f->name);
1180 } else if (f->flags & FIELD_IS_SYMBOLIC) {
1181 if ((count - 1) % 5 != 0) {
1182 fprintf(ofp, "\n\t\t");
1183 count = 4;
1184 }
1185 fprintf(ofp, "symbol_str(\"");
1186 fprintf(ofp, "%s__%s\", ", event->system,
1187 event->name);
1188 fprintf(ofp, "\"%s\", %s)", f->name,
1189 f->name);
1190 } else
1191 fprintf(ofp, "%s", f->name);
1192 }
1193
Joseph Schuchart0f5f5bc2014-07-10 13:50:51 +02001194 fprintf(ofp, ")\n\n");
1195
1196 fprintf(ofp, "\t\tfor node in common_callchain:");
1197 fprintf(ofp, "\n\t\t\tif 'sym' in node:");
1198 fprintf(ofp, "\n\t\t\t\tprint \"\\t[%%x] %%s\" %% (node['ip'], node['sym']['name'])");
1199 fprintf(ofp, "\n\t\t\telse:");
1200 fprintf(ofp, "\n\t\t\t\tprint \"\t[%%x]\" %% (node['ip'])\n\n");
1201 fprintf(ofp, "\t\tprint \"\\n\"\n\n");
1202
Tom Zanussi7e4b21b2010-01-27 02:27:57 -06001203 }
1204
1205 fprintf(ofp, "def trace_unhandled(event_name, context, "
Pierre Tardyc0251482010-05-31 23:12:09 +02001206 "event_fields_dict):\n");
Tom Zanussi7e4b21b2010-01-27 02:27:57 -06001207
Pierre Tardyc0251482010-05-31 23:12:09 +02001208 fprintf(ofp, "\t\tprint ' '.join(['%%s=%%s'%%(k,str(v))"
1209 "for k,v in sorted(event_fields_dict.items())])\n\n");
Tom Zanussi7e4b21b2010-01-27 02:27:57 -06001210
1211 fprintf(ofp, "def print_header("
1212 "event_name, cpu, secs, nsecs, pid, comm):\n"
1213 "\tprint \"%%-20s %%5u %%05u.%%09u %%8u %%-20s \" %% \\\n\t"
1214 "(event_name, cpu, secs, nsecs, pid, comm),\n");
1215
1216 fclose(ofp);
1217
1218 fprintf(stderr, "generated Python script: %s\n", fname);
1219
1220 return 0;
1221}
1222
1223struct scripting_ops python_scripting_ops = {
1224 .name = "Python",
1225 .start_script = python_start_script,
Adrian Hunterd445dd22014-08-15 22:08:37 +03001226 .flush_script = python_flush_script,
Tom Zanussi7e4b21b2010-01-27 02:27:57 -06001227 .stop_script = python_stop_script,
1228 .process_event = python_process_event,
1229 .generate_script = python_generate_script,
1230};