blob: 2ec5dfb5a45608ded774b62d9e30cb837443f1c5 [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"
Tom Zanussi7e4b21b2010-01-27 02:27:57 -060044
45PyMODINIT_FUNC initperf_trace_context(void);
46
47#define FTRACE_MAX_EVENT \
48 ((1 << (sizeof(unsigned short) * 8)) - 1)
49
Jiri Olsaadf5bcf2014-10-26 23:44:05 +010050static DECLARE_BITMAP(events_defined, FTRACE_MAX_EVENT);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -060051
52#define MAX_FIELDS 64
53#define N_COMMON_FIELDS 7
54
55extern struct scripting_context *scripting_context;
56
57static char *cur_field_name;
58static int zero_flag_atom;
59
60static PyObject *main_module, *main_dict;
61
Adrian Hunterdf919b42014-10-23 13:45:14 +030062struct tables {
63 struct db_export dbe;
64 PyObject *evsel_handler;
65 PyObject *machine_handler;
66 PyObject *thread_handler;
67 PyObject *comm_handler;
68 PyObject *comm_thread_handler;
69 PyObject *dso_handler;
70 PyObject *symbol_handler;
Adrian Hunterc29414f2014-10-30 16:09:44 +020071 PyObject *branch_type_handler;
Adrian Hunterdf919b42014-10-23 13:45:14 +030072 PyObject *sample_handler;
Adrian Hunter6a703072014-10-30 16:09:47 +020073 PyObject *call_path_handler;
74 PyObject *call_return_handler;
Adrian Hunterdf919b42014-10-23 13:45:14 +030075 bool db_export_mode;
76};
77
78static struct tables tables_global;
79
Joseph Schuchart05f832e2014-07-09 16:16:31 +020080static void handler_call_die(const char *handler_name) NORETURN;
Tom Zanussi7e4b21b2010-01-27 02:27:57 -060081static void handler_call_die(const char *handler_name)
82{
83 PyErr_Print();
84 Py_FatalError("problem in Python trace event handler");
Joseph Schuchart05f832e2014-07-09 16:16:31 +020085 // Py_FatalError does not return
86 // but we have to make the compiler happy
87 abort();
Tom Zanussi7e4b21b2010-01-27 02:27:57 -060088}
89
Joseph Schuchartc0268e82013-10-24 10:10:51 -030090/*
91 * Insert val into into the dictionary and decrement the reference counter.
Arnaldo Carvalho de Melo48000a12014-12-17 17:24:45 -030092 * This is necessary for dictionaries since PyDict_SetItemString() does not
Joseph Schuchartc0268e82013-10-24 10:10:51 -030093 * steal a reference, as opposed to PyTuple_SetItem().
94 */
95static void pydict_set_item_string_decref(PyObject *dict, const char *key, PyObject *val)
96{
97 PyDict_SetItemString(dict, key, val);
98 Py_DECREF(val);
99}
100
Adrian Huntera5563ed2014-07-31 09:01:01 +0300101static PyObject *get_handler(const char *handler_name)
102{
103 PyObject *handler;
104
105 handler = PyDict_GetItemString(main_dict, handler_name);
106 if (handler && !PyCallable_Check(handler))
107 return NULL;
108 return handler;
109}
110
111static void call_object(PyObject *handler, PyObject *args, const char *die_msg)
112{
113 PyObject *retval;
114
115 retval = PyObject_CallObject(handler, args);
116 if (retval == NULL)
117 handler_call_die(die_msg);
118 Py_DECREF(retval);
119}
120
121static void try_call_object(const char *handler_name, PyObject *args)
122{
123 PyObject *handler;
124
125 handler = get_handler(handler_name);
126 if (handler)
127 call_object(handler, args, handler_name);
128}
129
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600130static void define_value(enum print_arg_type field_type,
131 const char *ev_name,
132 const char *field_name,
133 const char *field_value,
134 const char *field_str)
135{
136 const char *handler_name = "define_flag_value";
Adrian Huntera5563ed2014-07-31 09:01:01 +0300137 PyObject *t;
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600138 unsigned long long value;
139 unsigned n = 0;
140
141 if (field_type == PRINT_SYMBOL)
142 handler_name = "define_symbolic_value";
143
Tom Zanussi44ad9cd2010-02-22 01:12:59 -0600144 t = PyTuple_New(4);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600145 if (!t)
146 Py_FatalError("couldn't create Python tuple");
147
148 value = eval_flag(field_value);
149
150 PyTuple_SetItem(t, n++, PyString_FromString(ev_name));
151 PyTuple_SetItem(t, n++, PyString_FromString(field_name));
152 PyTuple_SetItem(t, n++, PyInt_FromLong(value));
153 PyTuple_SetItem(t, n++, PyString_FromString(field_str));
154
Adrian Huntera5563ed2014-07-31 09:01:01 +0300155 try_call_object(handler_name, t);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600156
157 Py_DECREF(t);
158}
159
160static void define_values(enum print_arg_type field_type,
161 struct print_flag_sym *field,
162 const char *ev_name,
163 const char *field_name)
164{
165 define_value(field_type, ev_name, field_name, field->value,
166 field->str);
167
168 if (field->next)
169 define_values(field_type, field->next, ev_name, field_name);
170}
171
172static void define_field(enum print_arg_type field_type,
173 const char *ev_name,
174 const char *field_name,
175 const char *delim)
176{
177 const char *handler_name = "define_flag_field";
Adrian Huntera5563ed2014-07-31 09:01:01 +0300178 PyObject *t;
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600179 unsigned n = 0;
180
181 if (field_type == PRINT_SYMBOL)
182 handler_name = "define_symbolic_field";
183
Tom Zanussi44ad9cd2010-02-22 01:12:59 -0600184 if (field_type == PRINT_FLAGS)
185 t = PyTuple_New(3);
186 else
187 t = PyTuple_New(2);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600188 if (!t)
189 Py_FatalError("couldn't create Python tuple");
190
191 PyTuple_SetItem(t, n++, PyString_FromString(ev_name));
192 PyTuple_SetItem(t, n++, PyString_FromString(field_name));
193 if (field_type == PRINT_FLAGS)
194 PyTuple_SetItem(t, n++, PyString_FromString(delim));
195
Adrian Huntera5563ed2014-07-31 09:01:01 +0300196 try_call_object(handler_name, t);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600197
198 Py_DECREF(t);
199}
200
Steven Rostedtaaf045f2012-04-06 00:47:56 +0200201static void define_event_symbols(struct event_format *event,
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600202 const char *ev_name,
203 struct print_arg *args)
204{
205 switch (args->type) {
206 case PRINT_NULL:
207 break;
208 case PRINT_ATOM:
209 define_value(PRINT_FLAGS, ev_name, cur_field_name, "0",
210 args->atom.atom);
211 zero_flag_atom = 0;
212 break;
213 case PRINT_FIELD:
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -0300214 free(cur_field_name);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600215 cur_field_name = strdup(args->field.name);
216 break;
217 case PRINT_FLAGS:
218 define_event_symbols(event, ev_name, args->flags.field);
219 define_field(PRINT_FLAGS, ev_name, cur_field_name,
220 args->flags.delim);
221 define_values(PRINT_FLAGS, args->flags.flags, ev_name,
222 cur_field_name);
223 break;
224 case PRINT_SYMBOL:
225 define_event_symbols(event, ev_name, args->symbol.field);
226 define_field(PRINT_SYMBOL, ev_name, cur_field_name, NULL);
227 define_values(PRINT_SYMBOL, args->symbol.symbols, ev_name,
228 cur_field_name);
229 break;
Namhyung Kime080e6f2012-06-27 09:41:41 +0900230 case PRINT_HEX:
231 define_event_symbols(event, ev_name, args->hex.field);
232 define_event_symbols(event, ev_name, args->hex.size);
233 break;
Javi Merinob839e1e82015-03-24 11:07:19 +0000234 case PRINT_INT_ARRAY:
235 define_event_symbols(event, ev_name, args->int_array.field);
236 define_event_symbols(event, ev_name, args->int_array.count);
237 define_event_symbols(event, ev_name, args->int_array.el_size);
238 break;
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600239 case PRINT_STRING:
240 break;
241 case PRINT_TYPE:
242 define_event_symbols(event, ev_name, args->typecast.item);
243 break;
244 case PRINT_OP:
245 if (strcmp(args->op.op, ":") == 0)
246 zero_flag_atom = 1;
247 define_event_symbols(event, ev_name, args->op.left);
248 define_event_symbols(event, ev_name, args->op.right);
249 break;
250 default:
Steven Rostedtaaf045f2012-04-06 00:47:56 +0200251 /* gcc warns for these? */
252 case PRINT_BSTRING:
253 case PRINT_DYNAMIC_ARRAY:
254 case PRINT_FUNC:
Steven Rostedt (Red Hat)473a7782014-06-02 23:20:16 -0400255 case PRINT_BITMASK:
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600256 /* we should warn... */
257 return;
258 }
259
260 if (args->next)
261 define_event_symbols(event, ev_name, args->next);
262}
263
Sebastian Andrzej Siewior33058b92014-05-27 18:14:33 +0200264static PyObject *get_field_numeric_entry(struct event_format *event,
265 struct format_field *field, void *data)
266{
Sebastian Andrzej Siewior8ac631c2014-05-27 18:14:34 +0200267 bool is_array = field->flags & FIELD_IS_ARRAY;
268 PyObject *obj, *list = NULL;
Sebastian Andrzej Siewior33058b92014-05-27 18:14:33 +0200269 unsigned long long val;
Sebastian Andrzej Siewior8ac631c2014-05-27 18:14:34 +0200270 unsigned int item_size, n_items, i;
Sebastian Andrzej Siewior33058b92014-05-27 18:14:33 +0200271
Sebastian Andrzej Siewior8ac631c2014-05-27 18:14:34 +0200272 if (is_array) {
273 list = PyList_New(field->arraylen);
274 item_size = field->size / field->arraylen;
275 n_items = field->arraylen;
Sebastian Andrzej Siewior33058b92014-05-27 18:14:33 +0200276 } else {
Sebastian Andrzej Siewior8ac631c2014-05-27 18:14:34 +0200277 item_size = field->size;
278 n_items = 1;
Sebastian Andrzej Siewior33058b92014-05-27 18:14:33 +0200279 }
Sebastian Andrzej Siewior8ac631c2014-05-27 18:14:34 +0200280
281 for (i = 0; i < n_items; i++) {
282
283 val = read_size(event, data + field->offset + i * item_size,
284 item_size);
285 if (field->flags & FIELD_IS_SIGNED) {
286 if ((long long)val >= LONG_MIN &&
287 (long long)val <= LONG_MAX)
288 obj = PyInt_FromLong(val);
289 else
290 obj = PyLong_FromLongLong(val);
291 } else {
292 if (val <= LONG_MAX)
293 obj = PyInt_FromLong(val);
294 else
295 obj = PyLong_FromUnsignedLongLong(val);
296 }
297 if (is_array)
298 PyList_SET_ITEM(list, i, obj);
299 }
300 if (is_array)
301 obj = list;
Sebastian Andrzej Siewior33058b92014-05-27 18:14:33 +0200302 return obj;
303}
304
Joseph Schuchart0f5f5bc2014-07-10 13:50:51 +0200305
306static PyObject *python_process_callchain(struct perf_sample *sample,
307 struct perf_evsel *evsel,
308 struct addr_location *al)
309{
310 PyObject *pylist;
311
312 pylist = PyList_New(0);
313 if (!pylist)
314 Py_FatalError("couldn't create Python list");
315
316 if (!symbol_conf.use_callchain || !sample->callchain)
317 goto exit;
318
Arnaldo Carvalho de Melocc8b7c22014-10-23 15:26:17 -0300319 if (thread__resolve_callchain(al->thread, evsel,
320 sample, NULL, NULL,
321 PERF_MAX_STACK_DEPTH) != 0) {
Joseph Schuchart0f5f5bc2014-07-10 13:50:51 +0200322 pr_err("Failed to resolve callchain. Skipping\n");
323 goto exit;
324 }
325 callchain_cursor_commit(&callchain_cursor);
326
327
328 while (1) {
329 PyObject *pyelem;
330 struct callchain_cursor_node *node;
331 node = callchain_cursor_current(&callchain_cursor);
332 if (!node)
333 break;
334
335 pyelem = PyDict_New();
336 if (!pyelem)
337 Py_FatalError("couldn't create Python dictionary");
338
339
340 pydict_set_item_string_decref(pyelem, "ip",
341 PyLong_FromUnsignedLongLong(node->ip));
342
343 if (node->sym) {
344 PyObject *pysym = PyDict_New();
345 if (!pysym)
346 Py_FatalError("couldn't create Python dictionary");
347 pydict_set_item_string_decref(pysym, "start",
348 PyLong_FromUnsignedLongLong(node->sym->start));
349 pydict_set_item_string_decref(pysym, "end",
350 PyLong_FromUnsignedLongLong(node->sym->end));
351 pydict_set_item_string_decref(pysym, "binding",
352 PyInt_FromLong(node->sym->binding));
353 pydict_set_item_string_decref(pysym, "name",
354 PyString_FromStringAndSize(node->sym->name,
355 node->sym->namelen));
356 pydict_set_item_string_decref(pyelem, "sym", pysym);
357 }
358
359 if (node->map) {
360 struct map *map = node->map;
361 const char *dsoname = "[unknown]";
362 if (map && map->dso && (map->dso->name || map->dso->long_name)) {
363 if (symbol_conf.show_kernel_path && map->dso->long_name)
364 dsoname = map->dso->long_name;
365 else if (map->dso->name)
366 dsoname = map->dso->name;
367 }
368 pydict_set_item_string_decref(pyelem, "dso",
369 PyString_FromString(dsoname));
370 }
371
372 callchain_cursor_advance(&callchain_cursor);
373 PyList_Append(pylist, pyelem);
374 Py_DECREF(pyelem);
375 }
376
377exit:
378 return pylist;
379}
380
381
Arnaldo Carvalho de Melob7fff6b52013-12-19 16:34:52 -0300382static void python_process_tracepoint(struct perf_sample *sample,
383 struct perf_evsel *evsel,
384 struct thread *thread,
385 struct addr_location *al)
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600386{
Jiri Olsaadf5bcf2014-10-26 23:44:05 +0100387 struct event_format *event = evsel->tp_format;
Adrian Huntera5563ed2014-07-31 09:01:01 +0300388 PyObject *handler, *context, *t, *obj, *callchain;
Joseph Schuchart0f5f5bc2014-07-10 13:50:51 +0200389 PyObject *dict = NULL;
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600390 static char handler_name[256];
391 struct format_field *field;
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600392 unsigned long s, ns;
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600393 unsigned n = 0;
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600394 int pid;
David Ahernbe6d8422011-03-09 22:23:23 -0700395 int cpu = sample->cpu;
396 void *data = sample->raw_data;
397 unsigned long long nsecs = sample->time;
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +0200398 const char *comm = thread__comm_str(thread);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600399
400 t = PyTuple_New(MAX_FIELDS);
401 if (!t)
402 Py_FatalError("couldn't create Python tuple");
403
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600404 if (!event)
Arnaldo Carvalho de Melofcf65bf2012-08-07 09:58:03 -0300405 die("ug! no event found for type %d", (int)evsel->attr.config);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600406
Arnaldo Carvalho de Melo97822432012-08-07 23:50:21 -0300407 pid = raw_field_value(event, "common_pid", data);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600408
409 sprintf(handler_name, "%s__%s", event->system, event->name);
410
Jiri Olsaadf5bcf2014-10-26 23:44:05 +0100411 if (!test_and_set_bit(event->id, events_defined))
412 define_event_symbols(event, handler_name, event->print_fmt.args);
413
Adrian Huntera5563ed2014-07-31 09:01:01 +0300414 handler = get_handler(handler_name);
Pierre Tardyc0251482010-05-31 23:12:09 +0200415 if (!handler) {
416 dict = PyDict_New();
417 if (!dict)
418 Py_FatalError("couldn't create Python dict");
419 }
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600420 s = nsecs / NSECS_PER_SEC;
421 ns = nsecs - s * NSECS_PER_SEC;
422
423 scripting_context->event_data = data;
Tom Zanussi2de95332013-01-18 13:51:27 -0600424 scripting_context->pevent = evsel->tp_format->pevent;
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600425
426 context = PyCObject_FromVoidPtr(scripting_context, NULL);
427
428 PyTuple_SetItem(t, n++, PyString_FromString(handler_name));
Kyle McMartinfb7d0b32011-01-24 11:13:04 -0500429 PyTuple_SetItem(t, n++, context);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600430
Joseph Schuchart0f5f5bc2014-07-10 13:50:51 +0200431 /* ip unwinding */
432 callchain = python_process_callchain(sample, evsel, al);
433
Pierre Tardyc0251482010-05-31 23:12:09 +0200434 if (handler) {
435 PyTuple_SetItem(t, n++, PyInt_FromLong(cpu));
436 PyTuple_SetItem(t, n++, PyInt_FromLong(s));
437 PyTuple_SetItem(t, n++, PyInt_FromLong(ns));
438 PyTuple_SetItem(t, n++, PyInt_FromLong(pid));
439 PyTuple_SetItem(t, n++, PyString_FromString(comm));
Joseph Schuchart0f5f5bc2014-07-10 13:50:51 +0200440 PyTuple_SetItem(t, n++, callchain);
Pierre Tardyc0251482010-05-31 23:12:09 +0200441 } else {
Joseph Schuchartc0268e82013-10-24 10:10:51 -0300442 pydict_set_item_string_decref(dict, "common_cpu", PyInt_FromLong(cpu));
443 pydict_set_item_string_decref(dict, "common_s", PyInt_FromLong(s));
444 pydict_set_item_string_decref(dict, "common_ns", PyInt_FromLong(ns));
445 pydict_set_item_string_decref(dict, "common_pid", PyInt_FromLong(pid));
446 pydict_set_item_string_decref(dict, "common_comm", PyString_FromString(comm));
Joseph Schuchart0f5f5bc2014-07-10 13:50:51 +0200447 pydict_set_item_string_decref(dict, "common_callchain", callchain);
Pierre Tardyc0251482010-05-31 23:12:09 +0200448 }
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600449 for (field = event->format.fields; field; field = field->next) {
450 if (field->flags & FIELD_IS_STRING) {
451 int offset;
452 if (field->flags & FIELD_IS_DYNAMIC) {
453 offset = *(int *)(data + field->offset);
454 offset &= 0xffff;
455 } else
456 offset = field->offset;
Tom Zanussib1dcc032010-04-01 23:58:25 -0500457 obj = PyString_FromString((char *)data + offset);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600458 } else { /* FIELD_IS_NUMERIC */
Sebastian Andrzej Siewior33058b92014-05-27 18:14:33 +0200459 obj = get_field_numeric_entry(event, field, data);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600460 }
Pierre Tardyc0251482010-05-31 23:12:09 +0200461 if (handler)
462 PyTuple_SetItem(t, n++, obj);
463 else
Joseph Schuchartc0268e82013-10-24 10:10:51 -0300464 pydict_set_item_string_decref(dict, field->name, obj);
Pierre Tardyc0251482010-05-31 23:12:09 +0200465
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600466 }
Joseph Schuchart0f5f5bc2014-07-10 13:50:51 +0200467
Pierre Tardyc0251482010-05-31 23:12:09 +0200468 if (!handler)
469 PyTuple_SetItem(t, n++, dict);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600470
471 if (_PyTuple_Resize(&t, n) == -1)
472 Py_FatalError("error resizing Python tuple");
473
Pierre Tardyc0251482010-05-31 23:12:09 +0200474 if (handler) {
Adrian Huntera5563ed2014-07-31 09:01:01 +0300475 call_object(handler, t, handler_name);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600476 } else {
Adrian Huntera5563ed2014-07-31 09:01:01 +0300477 try_call_object("trace_unhandled", t);
Pierre Tardyc0251482010-05-31 23:12:09 +0200478 Py_DECREF(dict);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600479 }
480
481 Py_DECREF(t);
482}
483
Adrian Hunterdf919b42014-10-23 13:45:14 +0300484static PyObject *tuple_new(unsigned int sz)
485{
486 PyObject *t;
487
488 t = PyTuple_New(sz);
489 if (!t)
490 Py_FatalError("couldn't create Python tuple");
491 return t;
492}
493
494static int tuple_set_u64(PyObject *t, unsigned int pos, u64 val)
495{
496#if BITS_PER_LONG == 64
497 return PyTuple_SetItem(t, pos, PyInt_FromLong(val));
498#endif
499#if BITS_PER_LONG == 32
500 return PyTuple_SetItem(t, pos, PyLong_FromLongLong(val));
501#endif
502}
503
504static int tuple_set_s32(PyObject *t, unsigned int pos, s32 val)
505{
506 return PyTuple_SetItem(t, pos, PyInt_FromLong(val));
507}
508
509static int tuple_set_string(PyObject *t, unsigned int pos, const char *s)
510{
511 return PyTuple_SetItem(t, pos, PyString_FromString(s));
512}
513
514static int python_export_evsel(struct db_export *dbe, struct perf_evsel *evsel)
515{
516 struct tables *tables = container_of(dbe, struct tables, dbe);
517 PyObject *t;
518
519 t = tuple_new(2);
520
521 tuple_set_u64(t, 0, evsel->db_id);
522 tuple_set_string(t, 1, perf_evsel__name(evsel));
523
524 call_object(tables->evsel_handler, t, "evsel_table");
525
526 Py_DECREF(t);
527
528 return 0;
529}
530
531static int python_export_machine(struct db_export *dbe,
532 struct machine *machine)
533{
534 struct tables *tables = container_of(dbe, struct tables, dbe);
535 PyObject *t;
536
537 t = tuple_new(3);
538
539 tuple_set_u64(t, 0, machine->db_id);
540 tuple_set_s32(t, 1, machine->pid);
541 tuple_set_string(t, 2, machine->root_dir ? machine->root_dir : "");
542
543 call_object(tables->machine_handler, t, "machine_table");
544
545 Py_DECREF(t);
546
547 return 0;
548}
549
550static int python_export_thread(struct db_export *dbe, struct thread *thread,
551 u64 main_thread_db_id, struct machine *machine)
552{
553 struct tables *tables = container_of(dbe, struct tables, dbe);
554 PyObject *t;
555
556 t = tuple_new(5);
557
558 tuple_set_u64(t, 0, thread->db_id);
559 tuple_set_u64(t, 1, machine->db_id);
560 tuple_set_u64(t, 2, main_thread_db_id);
561 tuple_set_s32(t, 3, thread->pid_);
562 tuple_set_s32(t, 4, thread->tid);
563
564 call_object(tables->thread_handler, t, "thread_table");
565
566 Py_DECREF(t);
567
568 return 0;
569}
570
571static int python_export_comm(struct db_export *dbe, struct comm *comm)
572{
573 struct tables *tables = container_of(dbe, struct tables, dbe);
574 PyObject *t;
575
576 t = tuple_new(2);
577
578 tuple_set_u64(t, 0, comm->db_id);
579 tuple_set_string(t, 1, comm__str(comm));
580
581 call_object(tables->comm_handler, t, "comm_table");
582
583 Py_DECREF(t);
584
585 return 0;
586}
587
588static int python_export_comm_thread(struct db_export *dbe, u64 db_id,
589 struct comm *comm, struct thread *thread)
590{
591 struct tables *tables = container_of(dbe, struct tables, dbe);
592 PyObject *t;
593
594 t = tuple_new(3);
595
596 tuple_set_u64(t, 0, db_id);
597 tuple_set_u64(t, 1, comm->db_id);
598 tuple_set_u64(t, 2, thread->db_id);
599
600 call_object(tables->comm_thread_handler, t, "comm_thread_table");
601
602 Py_DECREF(t);
603
604 return 0;
605}
606
607static int python_export_dso(struct db_export *dbe, struct dso *dso,
608 struct machine *machine)
609{
610 struct tables *tables = container_of(dbe, struct tables, dbe);
611 char sbuild_id[BUILD_ID_SIZE * 2 + 1];
612 PyObject *t;
613
614 build_id__sprintf(dso->build_id, sizeof(dso->build_id), sbuild_id);
615
616 t = tuple_new(5);
617
618 tuple_set_u64(t, 0, dso->db_id);
619 tuple_set_u64(t, 1, machine->db_id);
620 tuple_set_string(t, 2, dso->short_name);
621 tuple_set_string(t, 3, dso->long_name);
622 tuple_set_string(t, 4, sbuild_id);
623
624 call_object(tables->dso_handler, t, "dso_table");
625
626 Py_DECREF(t);
627
628 return 0;
629}
630
631static int python_export_symbol(struct db_export *dbe, struct symbol *sym,
632 struct dso *dso)
633{
634 struct tables *tables = container_of(dbe, struct tables, dbe);
635 u64 *sym_db_id = symbol__priv(sym);
636 PyObject *t;
637
638 t = tuple_new(6);
639
640 tuple_set_u64(t, 0, *sym_db_id);
641 tuple_set_u64(t, 1, dso->db_id);
642 tuple_set_u64(t, 2, sym->start);
643 tuple_set_u64(t, 3, sym->end);
644 tuple_set_s32(t, 4, sym->binding);
645 tuple_set_string(t, 5, sym->name);
646
647 call_object(tables->symbol_handler, t, "symbol_table");
648
649 Py_DECREF(t);
650
651 return 0;
652}
653
Adrian Hunterc29414f2014-10-30 16:09:44 +0200654static int python_export_branch_type(struct db_export *dbe, u32 branch_type,
655 const char *name)
656{
657 struct tables *tables = container_of(dbe, struct tables, dbe);
658 PyObject *t;
659
660 t = tuple_new(2);
661
662 tuple_set_s32(t, 0, branch_type);
663 tuple_set_string(t, 1, name);
664
665 call_object(tables->branch_type_handler, t, "branch_type_table");
666
667 Py_DECREF(t);
668
669 return 0;
670}
671
Adrian Hunterdf919b42014-10-23 13:45:14 +0300672static int python_export_sample(struct db_export *dbe,
673 struct export_sample *es)
674{
675 struct tables *tables = container_of(dbe, struct tables, dbe);
676 PyObject *t;
677
Adrian Hunterc29414f2014-10-30 16:09:44 +0200678 t = tuple_new(21);
Adrian Hunterdf919b42014-10-23 13:45:14 +0300679
680 tuple_set_u64(t, 0, es->db_id);
681 tuple_set_u64(t, 1, es->evsel->db_id);
682 tuple_set_u64(t, 2, es->al->machine->db_id);
683 tuple_set_u64(t, 3, es->thread->db_id);
684 tuple_set_u64(t, 4, es->comm_db_id);
685 tuple_set_u64(t, 5, es->dso_db_id);
686 tuple_set_u64(t, 6, es->sym_db_id);
687 tuple_set_u64(t, 7, es->offset);
688 tuple_set_u64(t, 8, es->sample->ip);
689 tuple_set_u64(t, 9, es->sample->time);
690 tuple_set_s32(t, 10, es->sample->cpu);
691 tuple_set_u64(t, 11, es->addr_dso_db_id);
692 tuple_set_u64(t, 12, es->addr_sym_db_id);
693 tuple_set_u64(t, 13, es->addr_offset);
694 tuple_set_u64(t, 14, es->sample->addr);
695 tuple_set_u64(t, 15, es->sample->period);
696 tuple_set_u64(t, 16, es->sample->weight);
697 tuple_set_u64(t, 17, es->sample->transaction);
698 tuple_set_u64(t, 18, es->sample->data_src);
Adrian Hunterc29414f2014-10-30 16:09:44 +0200699 tuple_set_s32(t, 19, es->sample->flags & PERF_BRANCH_MASK);
700 tuple_set_s32(t, 20, !!(es->sample->flags & PERF_IP_FLAG_IN_TX));
Adrian Hunterdf919b42014-10-23 13:45:14 +0300701
702 call_object(tables->sample_handler, t, "sample_table");
703
704 Py_DECREF(t);
705
706 return 0;
707}
708
Adrian Hunter6a703072014-10-30 16:09:47 +0200709static int python_export_call_path(struct db_export *dbe, struct call_path *cp)
710{
711 struct tables *tables = container_of(dbe, struct tables, dbe);
712 PyObject *t;
713 u64 parent_db_id, sym_db_id;
714
715 parent_db_id = cp->parent ? cp->parent->db_id : 0;
716 sym_db_id = cp->sym ? *(u64 *)symbol__priv(cp->sym) : 0;
717
718 t = tuple_new(4);
719
720 tuple_set_u64(t, 0, cp->db_id);
721 tuple_set_u64(t, 1, parent_db_id);
722 tuple_set_u64(t, 2, sym_db_id);
723 tuple_set_u64(t, 3, cp->ip);
724
725 call_object(tables->call_path_handler, t, "call_path_table");
726
727 Py_DECREF(t);
728
729 return 0;
730}
731
732static int python_export_call_return(struct db_export *dbe,
733 struct call_return *cr)
734{
735 struct tables *tables = container_of(dbe, struct tables, dbe);
736 u64 comm_db_id = cr->comm ? cr->comm->db_id : 0;
737 PyObject *t;
738
739 t = tuple_new(11);
740
741 tuple_set_u64(t, 0, cr->db_id);
742 tuple_set_u64(t, 1, cr->thread->db_id);
743 tuple_set_u64(t, 2, comm_db_id);
744 tuple_set_u64(t, 3, cr->cp->db_id);
745 tuple_set_u64(t, 4, cr->call_time);
746 tuple_set_u64(t, 5, cr->return_time);
747 tuple_set_u64(t, 6, cr->branch_count);
748 tuple_set_u64(t, 7, cr->call_ref);
749 tuple_set_u64(t, 8, cr->return_ref);
750 tuple_set_u64(t, 9, cr->cp->parent->db_id);
751 tuple_set_s32(t, 10, cr->flags);
752
753 call_object(tables->call_return_handler, t, "call_return_table");
754
755 Py_DECREF(t);
756
757 return 0;
758}
759
760static int python_process_call_return(struct call_return *cr, void *data)
761{
762 struct db_export *dbe = data;
763
764 return db_export__call_return(dbe, cr);
765}
766
Arnaldo Carvalho de Melob7fff6b52013-12-19 16:34:52 -0300767static void python_process_general_event(struct perf_sample *sample,
Feng Tang6a6daec2012-08-08 17:57:51 +0800768 struct perf_evsel *evsel,
David Ahern2eaa1b42013-07-18 16:06:15 -0600769 struct thread *thread,
Feng Tang87b6a3a2012-08-09 13:46:13 +0800770 struct addr_location *al)
Feng Tang6a6daec2012-08-08 17:57:51 +0800771{
Adrian Huntera5563ed2014-07-31 09:01:01 +0300772 PyObject *handler, *t, *dict, *callchain, *dict_sample;
Feng Tang6a6daec2012-08-08 17:57:51 +0800773 static char handler_name[64];
774 unsigned n = 0;
Feng Tang6a6daec2012-08-08 17:57:51 +0800775
Feng Tangfd6b8582012-08-08 17:57:53 +0800776 /*
777 * Use the MAX_FIELDS to make the function expandable, though
Feng Tang87b6a3a2012-08-09 13:46:13 +0800778 * currently there is only one item for the tuple.
Feng Tangfd6b8582012-08-08 17:57:53 +0800779 */
Feng Tang6a6daec2012-08-08 17:57:51 +0800780 t = PyTuple_New(MAX_FIELDS);
781 if (!t)
782 Py_FatalError("couldn't create Python tuple");
783
Feng Tangfd6b8582012-08-08 17:57:53 +0800784 dict = PyDict_New();
785 if (!dict)
786 Py_FatalError("couldn't create Python dictionary");
787
Joseph Schuchart57608cf2014-07-10 13:50:56 +0200788 dict_sample = PyDict_New();
789 if (!dict_sample)
790 Py_FatalError("couldn't create Python dictionary");
791
Feng Tang6a6daec2012-08-08 17:57:51 +0800792 snprintf(handler_name, sizeof(handler_name), "%s", "process_event");
793
Adrian Huntera5563ed2014-07-31 09:01:01 +0300794 handler = get_handler(handler_name);
795 if (!handler)
Feng Tang6a6daec2012-08-08 17:57:51 +0800796 goto exit;
Feng Tang6a6daec2012-08-08 17:57:51 +0800797
Joseph Schuchartc0268e82013-10-24 10:10:51 -0300798 pydict_set_item_string_decref(dict, "ev_name", PyString_FromString(perf_evsel__name(evsel)));
799 pydict_set_item_string_decref(dict, "attr", PyString_FromStringAndSize(
Feng Tangfd6b8582012-08-08 17:57:53 +0800800 (const char *)&evsel->attr, sizeof(evsel->attr)));
Joseph Schuchart57608cf2014-07-10 13:50:56 +0200801
802 pydict_set_item_string_decref(dict_sample, "pid",
803 PyInt_FromLong(sample->pid));
804 pydict_set_item_string_decref(dict_sample, "tid",
805 PyInt_FromLong(sample->tid));
806 pydict_set_item_string_decref(dict_sample, "cpu",
807 PyInt_FromLong(sample->cpu));
808 pydict_set_item_string_decref(dict_sample, "ip",
809 PyLong_FromUnsignedLongLong(sample->ip));
810 pydict_set_item_string_decref(dict_sample, "time",
811 PyLong_FromUnsignedLongLong(sample->time));
812 pydict_set_item_string_decref(dict_sample, "period",
813 PyLong_FromUnsignedLongLong(sample->period));
814 pydict_set_item_string_decref(dict, "sample", dict_sample);
815
Joseph Schuchartc0268e82013-10-24 10:10:51 -0300816 pydict_set_item_string_decref(dict, "raw_buf", PyString_FromStringAndSize(
Feng Tangfd6b8582012-08-08 17:57:53 +0800817 (const char *)sample->raw_data, sample->raw_size));
Joseph Schuchartc0268e82013-10-24 10:10:51 -0300818 pydict_set_item_string_decref(dict, "comm",
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +0200819 PyString_FromString(thread__comm_str(thread)));
Feng Tangfd6b8582012-08-08 17:57:53 +0800820 if (al->map) {
Joseph Schuchartc0268e82013-10-24 10:10:51 -0300821 pydict_set_item_string_decref(dict, "dso",
Feng Tangfd6b8582012-08-08 17:57:53 +0800822 PyString_FromString(al->map->dso->name));
823 }
824 if (al->sym) {
Joseph Schuchartc0268e82013-10-24 10:10:51 -0300825 pydict_set_item_string_decref(dict, "symbol",
Feng Tangfd6b8582012-08-08 17:57:53 +0800826 PyString_FromString(al->sym->name));
827 }
Feng Tang6a6daec2012-08-08 17:57:51 +0800828
Joseph Schuchart0f5f5bc2014-07-10 13:50:51 +0200829 /* ip unwinding */
830 callchain = python_process_callchain(sample, evsel, al);
831 pydict_set_item_string_decref(dict, "callchain", callchain);
832
Feng Tangfd6b8582012-08-08 17:57:53 +0800833 PyTuple_SetItem(t, n++, dict);
Feng Tang6a6daec2012-08-08 17:57:51 +0800834 if (_PyTuple_Resize(&t, n) == -1)
835 Py_FatalError("error resizing Python tuple");
836
Adrian Huntera5563ed2014-07-31 09:01:01 +0300837 call_object(handler, t, handler_name);
Feng Tang6a6daec2012-08-08 17:57:51 +0800838exit:
Feng Tangfd6b8582012-08-08 17:57:53 +0800839 Py_DECREF(dict);
Feng Tang6a6daec2012-08-08 17:57:51 +0800840 Py_DECREF(t);
841}
842
Adrian Hunterdf919b42014-10-23 13:45:14 +0300843static void python_process_event(union perf_event *event,
Feng Tang6a6daec2012-08-08 17:57:51 +0800844 struct perf_sample *sample,
845 struct perf_evsel *evsel,
David Ahern2eaa1b42013-07-18 16:06:15 -0600846 struct thread *thread,
Feng Tang73994dc2012-08-08 17:57:52 +0800847 struct addr_location *al)
Feng Tang6a6daec2012-08-08 17:57:51 +0800848{
Adrian Hunterdf919b42014-10-23 13:45:14 +0300849 struct tables *tables = &tables_global;
850
Feng Tang6a6daec2012-08-08 17:57:51 +0800851 switch (evsel->attr.type) {
852 case PERF_TYPE_TRACEPOINT:
Arnaldo Carvalho de Melob7fff6b52013-12-19 16:34:52 -0300853 python_process_tracepoint(sample, evsel, thread, al);
Feng Tang6a6daec2012-08-08 17:57:51 +0800854 break;
855 /* Reserve for future process_hw/sw/raw APIs */
856 default:
Adrian Hunterdf919b42014-10-23 13:45:14 +0300857 if (tables->db_export_mode)
858 db_export__sample(&tables->dbe, event, sample, evsel,
859 thread, al);
860 else
861 python_process_general_event(sample, evsel, thread, al);
Feng Tang6a6daec2012-08-08 17:57:51 +0800862 }
863}
864
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600865static int run_start_sub(void)
866{
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600867 main_module = PyImport_AddModule("__main__");
868 if (main_module == NULL)
869 return -1;
870 Py_INCREF(main_module);
871
872 main_dict = PyModule_GetDict(main_module);
Adrian Huntera5563ed2014-07-31 09:01:01 +0300873 if (main_dict == NULL)
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600874 goto error;
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600875 Py_INCREF(main_dict);
876
Adrian Huntera5563ed2014-07-31 09:01:01 +0300877 try_call_object("trace_begin", NULL);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600878
Adrian Huntera5563ed2014-07-31 09:01:01 +0300879 return 0;
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600880
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600881error:
882 Py_XDECREF(main_dict);
883 Py_XDECREF(main_module);
Adrian Huntera5563ed2014-07-31 09:01:01 +0300884 return -1;
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600885}
886
Adrian Hunterdf919b42014-10-23 13:45:14 +0300887#define SET_TABLE_HANDLER_(name, handler_name, table_name) do { \
888 tables->handler_name = get_handler(#table_name); \
889 if (tables->handler_name) \
890 tables->dbe.export_ ## name = python_export_ ## name; \
891} while (0)
892
893#define SET_TABLE_HANDLER(name) \
894 SET_TABLE_HANDLER_(name, name ## _handler, name ## _table)
895
896static void set_table_handlers(struct tables *tables)
897{
898 const char *perf_db_export_mode = "perf_db_export_mode";
Adrian Hunter6a703072014-10-30 16:09:47 +0200899 const char *perf_db_export_calls = "perf_db_export_calls";
900 PyObject *db_export_mode, *db_export_calls;
901 bool export_calls = false;
Adrian Hunterdf919b42014-10-23 13:45:14 +0300902 int ret;
903
904 memset(tables, 0, sizeof(struct tables));
905 if (db_export__init(&tables->dbe))
906 Py_FatalError("failed to initialize export");
907
908 db_export_mode = PyDict_GetItemString(main_dict, perf_db_export_mode);
909 if (!db_export_mode)
910 return;
911
912 ret = PyObject_IsTrue(db_export_mode);
913 if (ret == -1)
914 handler_call_die(perf_db_export_mode);
915 if (!ret)
916 return;
917
Adrian Hunter6a703072014-10-30 16:09:47 +0200918 tables->dbe.crp = NULL;
919 db_export_calls = PyDict_GetItemString(main_dict, perf_db_export_calls);
920 if (db_export_calls) {
921 ret = PyObject_IsTrue(db_export_calls);
922 if (ret == -1)
923 handler_call_die(perf_db_export_calls);
924 export_calls = !!ret;
925 }
926
927 if (export_calls) {
928 tables->dbe.crp =
929 call_return_processor__new(python_process_call_return,
930 &tables->dbe);
931 if (!tables->dbe.crp)
932 Py_FatalError("failed to create calls processor");
933 }
934
Adrian Hunterdf919b42014-10-23 13:45:14 +0300935 tables->db_export_mode = true;
936 /*
937 * Reserve per symbol space for symbol->db_id via symbol__priv()
938 */
939 symbol_conf.priv_size = sizeof(u64);
940
941 SET_TABLE_HANDLER(evsel);
942 SET_TABLE_HANDLER(machine);
943 SET_TABLE_HANDLER(thread);
944 SET_TABLE_HANDLER(comm);
945 SET_TABLE_HANDLER(comm_thread);
946 SET_TABLE_HANDLER(dso);
947 SET_TABLE_HANDLER(symbol);
Adrian Hunterc29414f2014-10-30 16:09:44 +0200948 SET_TABLE_HANDLER(branch_type);
Adrian Hunterdf919b42014-10-23 13:45:14 +0300949 SET_TABLE_HANDLER(sample);
Adrian Hunter6a703072014-10-30 16:09:47 +0200950 SET_TABLE_HANDLER(call_path);
951 SET_TABLE_HANDLER(call_return);
Adrian Hunterdf919b42014-10-23 13:45:14 +0300952}
953
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600954/*
955 * Start trace script
956 */
957static int python_start_script(const char *script, int argc, const char **argv)
958{
Adrian Hunterdf919b42014-10-23 13:45:14 +0300959 struct tables *tables = &tables_global;
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600960 const char **command_line;
961 char buf[PATH_MAX];
962 int i, err = 0;
963 FILE *fp;
964
965 command_line = malloc((argc + 1) * sizeof(const char *));
966 command_line[0] = script;
967 for (i = 1; i < argc + 1; i++)
968 command_line[i] = argv[i - 1];
969
970 Py_Initialize();
971
972 initperf_trace_context();
973
974 PySys_SetArgv(argc + 1, (char **)command_line);
975
976 fp = fopen(script, "r");
977 if (!fp) {
978 sprintf(buf, "Can't open python script \"%s\"", script);
979 perror(buf);
980 err = -1;
981 goto error;
982 }
983
984 err = PyRun_SimpleFile(fp, script);
985 if (err) {
986 fprintf(stderr, "Error running python script %s\n", script);
987 goto error;
988 }
989
990 err = run_start_sub();
991 if (err) {
992 fprintf(stderr, "Error starting python script %s\n", script);
993 goto error;
994 }
995
996 free(command_line);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600997
Adrian Hunterdf919b42014-10-23 13:45:14 +0300998 set_table_handlers(tables);
999
Adrian Hunterc29414f2014-10-30 16:09:44 +02001000 if (tables->db_export_mode) {
1001 err = db_export__branch_types(&tables->dbe);
1002 if (err)
1003 goto error;
1004 }
1005
Tom Zanussi7e4b21b2010-01-27 02:27:57 -06001006 return err;
1007error:
1008 Py_Finalize();
1009 free(command_line);
1010
1011 return err;
1012}
1013
Adrian Hunterd445dd22014-08-15 22:08:37 +03001014static int python_flush_script(void)
1015{
Adrian Hunter758008b2014-10-30 16:09:48 +02001016 struct tables *tables = &tables_global;
1017
1018 return db_export__flush(&tables->dbe);
Adrian Hunterd445dd22014-08-15 22:08:37 +03001019}
1020
Tom Zanussi7e4b21b2010-01-27 02:27:57 -06001021/*
1022 * Stop trace script
1023 */
1024static int python_stop_script(void)
1025{
Adrian Hunterdf919b42014-10-23 13:45:14 +03001026 struct tables *tables = &tables_global;
1027
Adrian Huntera5563ed2014-07-31 09:01:01 +03001028 try_call_object("trace_end", NULL);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -06001029
Adrian Hunterdf919b42014-10-23 13:45:14 +03001030 db_export__exit(&tables->dbe);
1031
Tom Zanussi7e4b21b2010-01-27 02:27:57 -06001032 Py_XDECREF(main_dict);
1033 Py_XDECREF(main_module);
1034 Py_Finalize();
1035
Adrian Huntera5563ed2014-07-31 09:01:01 +03001036 return 0;
Tom Zanussi7e4b21b2010-01-27 02:27:57 -06001037}
1038
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03001039static int python_generate_script(struct pevent *pevent, const char *outfile)
Tom Zanussi7e4b21b2010-01-27 02:27:57 -06001040{
Steven Rostedtaaf045f2012-04-06 00:47:56 +02001041 struct event_format *event = NULL;
Tom Zanussi7e4b21b2010-01-27 02:27:57 -06001042 struct format_field *f;
1043 char fname[PATH_MAX];
1044 int not_first, count;
1045 FILE *ofp;
1046
1047 sprintf(fname, "%s.py", outfile);
1048 ofp = fopen(fname, "w");
1049 if (ofp == NULL) {
1050 fprintf(stderr, "couldn't open %s\n", fname);
1051 return -1;
1052 }
Ingo Molnar133dc4c2010-11-16 18:45:39 +01001053 fprintf(ofp, "# perf script event handlers, "
1054 "generated by perf script -g python\n");
Tom Zanussi7e4b21b2010-01-27 02:27:57 -06001055
1056 fprintf(ofp, "# Licensed under the terms of the GNU GPL"
1057 " License version 2\n\n");
1058
1059 fprintf(ofp, "# The common_* event handler fields are the most useful "
1060 "fields common to\n");
1061
1062 fprintf(ofp, "# all events. They don't necessarily correspond to "
1063 "the 'common_*' fields\n");
1064
1065 fprintf(ofp, "# in the format files. Those fields not available as "
1066 "handler params can\n");
1067
1068 fprintf(ofp, "# be retrieved using Python functions of the form "
1069 "common_*(context).\n");
1070
1071 fprintf(ofp, "# See the perf-trace-python Documentation for the list "
1072 "of available functions.\n\n");
1073
1074 fprintf(ofp, "import os\n");
1075 fprintf(ofp, "import sys\n\n");
1076
1077 fprintf(ofp, "sys.path.append(os.environ['PERF_EXEC_PATH'] + \\\n");
1078 fprintf(ofp, "\t'/scripts/python/Perf-Trace-Util/lib/Perf/Trace')\n");
1079 fprintf(ofp, "\nfrom perf_trace_context import *\n");
1080 fprintf(ofp, "from Core import *\n\n\n");
1081
1082 fprintf(ofp, "def trace_begin():\n");
1083 fprintf(ofp, "\tprint \"in trace_begin\"\n\n");
1084
1085 fprintf(ofp, "def trace_end():\n");
1086 fprintf(ofp, "\tprint \"in trace_end\"\n\n");
1087
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03001088 while ((event = trace_find_next_event(pevent, event))) {
Tom Zanussi7e4b21b2010-01-27 02:27:57 -06001089 fprintf(ofp, "def %s__%s(", event->system, event->name);
1090 fprintf(ofp, "event_name, ");
1091 fprintf(ofp, "context, ");
1092 fprintf(ofp, "common_cpu,\n");
1093 fprintf(ofp, "\tcommon_secs, ");
1094 fprintf(ofp, "common_nsecs, ");
1095 fprintf(ofp, "common_pid, ");
1096 fprintf(ofp, "common_comm,\n\t");
Joseph Schuchart0f5f5bc2014-07-10 13:50:51 +02001097 fprintf(ofp, "common_callchain, ");
Tom Zanussi7e4b21b2010-01-27 02:27:57 -06001098
1099 not_first = 0;
1100 count = 0;
1101
1102 for (f = event->format.fields; f; f = f->next) {
1103 if (not_first++)
1104 fprintf(ofp, ", ");
1105 if (++count % 5 == 0)
1106 fprintf(ofp, "\n\t");
1107
1108 fprintf(ofp, "%s", f->name);
1109 }
1110 fprintf(ofp, "):\n");
1111
1112 fprintf(ofp, "\t\tprint_header(event_name, common_cpu, "
1113 "common_secs, common_nsecs,\n\t\t\t"
1114 "common_pid, common_comm)\n\n");
1115
1116 fprintf(ofp, "\t\tprint \"");
1117
1118 not_first = 0;
1119 count = 0;
1120
1121 for (f = event->format.fields; f; f = f->next) {
1122 if (not_first++)
1123 fprintf(ofp, ", ");
1124 if (count && count % 3 == 0) {
1125 fprintf(ofp, "\" \\\n\t\t\"");
1126 }
1127 count++;
1128
1129 fprintf(ofp, "%s=", f->name);
1130 if (f->flags & FIELD_IS_STRING ||
1131 f->flags & FIELD_IS_FLAG ||
Namhyung Kime646fe72014-05-29 13:44:55 +09001132 f->flags & FIELD_IS_ARRAY ||
Tom Zanussi7e4b21b2010-01-27 02:27:57 -06001133 f->flags & FIELD_IS_SYMBOLIC)
1134 fprintf(ofp, "%%s");
1135 else if (f->flags & FIELD_IS_SIGNED)
1136 fprintf(ofp, "%%d");
1137 else
1138 fprintf(ofp, "%%u");
1139 }
1140
Joseph Schuchart0f5f5bc2014-07-10 13:50:51 +02001141 fprintf(ofp, "\" %% \\\n\t\t(");
Tom Zanussi7e4b21b2010-01-27 02:27:57 -06001142
1143 not_first = 0;
1144 count = 0;
1145
1146 for (f = event->format.fields; f; f = f->next) {
1147 if (not_first++)
1148 fprintf(ofp, ", ");
1149
1150 if (++count % 5 == 0)
1151 fprintf(ofp, "\n\t\t");
1152
1153 if (f->flags & FIELD_IS_FLAG) {
1154 if ((count - 1) % 5 != 0) {
1155 fprintf(ofp, "\n\t\t");
1156 count = 4;
1157 }
1158 fprintf(ofp, "flag_str(\"");
1159 fprintf(ofp, "%s__%s\", ", event->system,
1160 event->name);
1161 fprintf(ofp, "\"%s\", %s)", f->name,
1162 f->name);
1163 } else if (f->flags & FIELD_IS_SYMBOLIC) {
1164 if ((count - 1) % 5 != 0) {
1165 fprintf(ofp, "\n\t\t");
1166 count = 4;
1167 }
1168 fprintf(ofp, "symbol_str(\"");
1169 fprintf(ofp, "%s__%s\", ", event->system,
1170 event->name);
1171 fprintf(ofp, "\"%s\", %s)", f->name,
1172 f->name);
1173 } else
1174 fprintf(ofp, "%s", f->name);
1175 }
1176
Joseph Schuchart0f5f5bc2014-07-10 13:50:51 +02001177 fprintf(ofp, ")\n\n");
1178
1179 fprintf(ofp, "\t\tfor node in common_callchain:");
1180 fprintf(ofp, "\n\t\t\tif 'sym' in node:");
1181 fprintf(ofp, "\n\t\t\t\tprint \"\\t[%%x] %%s\" %% (node['ip'], node['sym']['name'])");
1182 fprintf(ofp, "\n\t\t\telse:");
1183 fprintf(ofp, "\n\t\t\t\tprint \"\t[%%x]\" %% (node['ip'])\n\n");
1184 fprintf(ofp, "\t\tprint \"\\n\"\n\n");
1185
Tom Zanussi7e4b21b2010-01-27 02:27:57 -06001186 }
1187
1188 fprintf(ofp, "def trace_unhandled(event_name, context, "
Pierre Tardyc0251482010-05-31 23:12:09 +02001189 "event_fields_dict):\n");
Tom Zanussi7e4b21b2010-01-27 02:27:57 -06001190
Pierre Tardyc0251482010-05-31 23:12:09 +02001191 fprintf(ofp, "\t\tprint ' '.join(['%%s=%%s'%%(k,str(v))"
1192 "for k,v in sorted(event_fields_dict.items())])\n\n");
Tom Zanussi7e4b21b2010-01-27 02:27:57 -06001193
1194 fprintf(ofp, "def print_header("
1195 "event_name, cpu, secs, nsecs, pid, comm):\n"
1196 "\tprint \"%%-20s %%5u %%05u.%%09u %%8u %%-20s \" %% \\\n\t"
1197 "(event_name, cpu, secs, nsecs, pid, comm),\n");
1198
1199 fclose(ofp);
1200
1201 fprintf(stderr, "generated Python script: %s\n", fname);
1202
1203 return 0;
1204}
1205
1206struct scripting_ops python_scripting_ops = {
1207 .name = "Python",
1208 .start_script = python_start_script,
Adrian Hunterd445dd22014-08-15 22:08:37 +03001209 .flush_script = python_flush_script,
Tom Zanussi7e4b21b2010-01-27 02:27:57 -06001210 .stop_script = python_stop_script,
1211 .process_event = python_process_event,
1212 .generate_script = python_generate_script,
1213};