blob: 56ba07cce549ef5b5a79c2f2ae31e0799f245907 [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>
Tom Zanussi7e4b21b2010-01-27 02:27:57 -060027#include <errno.h>
28
29#include "../../perf.h"
Jiri Olsa84f5d362014-07-14 23:46:48 +020030#include "../debug.h"
Arnaldo Carvalho de Melofcf65bf2012-08-07 09:58:03 -030031#include "../evsel.h"
Tom Zanussi7e4b21b2010-01-27 02:27:57 -060032#include "../util.h"
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -020033#include "../event.h"
34#include "../thread.h"
Tom Zanussi7e4b21b2010-01-27 02:27:57 -060035#include "../trace-event.h"
Joseph Schuchart0f5f5bc2014-07-10 13:50:51 +020036#include "../machine.h"
Tom Zanussi7e4b21b2010-01-27 02:27:57 -060037
38PyMODINIT_FUNC initperf_trace_context(void);
39
40#define FTRACE_MAX_EVENT \
41 ((1 << (sizeof(unsigned short) * 8)) - 1)
42
Steven Rostedtaaf045f2012-04-06 00:47:56 +020043struct event_format *events[FTRACE_MAX_EVENT];
Tom Zanussi7e4b21b2010-01-27 02:27:57 -060044
45#define MAX_FIELDS 64
46#define N_COMMON_FIELDS 7
47
48extern struct scripting_context *scripting_context;
49
50static char *cur_field_name;
51static int zero_flag_atom;
52
53static PyObject *main_module, *main_dict;
54
Joseph Schuchart05f832e2014-07-09 16:16:31 +020055static void handler_call_die(const char *handler_name) NORETURN;
Tom Zanussi7e4b21b2010-01-27 02:27:57 -060056static void handler_call_die(const char *handler_name)
57{
58 PyErr_Print();
59 Py_FatalError("problem in Python trace event handler");
Joseph Schuchart05f832e2014-07-09 16:16:31 +020060 // Py_FatalError does not return
61 // but we have to make the compiler happy
62 abort();
Tom Zanussi7e4b21b2010-01-27 02:27:57 -060063}
64
Joseph Schuchartc0268e82013-10-24 10:10:51 -030065/*
66 * Insert val into into the dictionary and decrement the reference counter.
67 * This is necessary for dictionaries since PyDict_SetItemString() does not
68 * steal a reference, as opposed to PyTuple_SetItem().
69 */
70static void pydict_set_item_string_decref(PyObject *dict, const char *key, PyObject *val)
71{
72 PyDict_SetItemString(dict, key, val);
73 Py_DECREF(val);
74}
75
Adrian Huntera5563ed2014-07-31 09:01:01 +030076static PyObject *get_handler(const char *handler_name)
77{
78 PyObject *handler;
79
80 handler = PyDict_GetItemString(main_dict, handler_name);
81 if (handler && !PyCallable_Check(handler))
82 return NULL;
83 return handler;
84}
85
86static void call_object(PyObject *handler, PyObject *args, const char *die_msg)
87{
88 PyObject *retval;
89
90 retval = PyObject_CallObject(handler, args);
91 if (retval == NULL)
92 handler_call_die(die_msg);
93 Py_DECREF(retval);
94}
95
96static void try_call_object(const char *handler_name, PyObject *args)
97{
98 PyObject *handler;
99
100 handler = get_handler(handler_name);
101 if (handler)
102 call_object(handler, args, handler_name);
103}
104
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600105static void define_value(enum print_arg_type field_type,
106 const char *ev_name,
107 const char *field_name,
108 const char *field_value,
109 const char *field_str)
110{
111 const char *handler_name = "define_flag_value";
Adrian Huntera5563ed2014-07-31 09:01:01 +0300112 PyObject *t;
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600113 unsigned long long value;
114 unsigned n = 0;
115
116 if (field_type == PRINT_SYMBOL)
117 handler_name = "define_symbolic_value";
118
Tom Zanussi44ad9cd2010-02-22 01:12:59 -0600119 t = PyTuple_New(4);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600120 if (!t)
121 Py_FatalError("couldn't create Python tuple");
122
123 value = eval_flag(field_value);
124
125 PyTuple_SetItem(t, n++, PyString_FromString(ev_name));
126 PyTuple_SetItem(t, n++, PyString_FromString(field_name));
127 PyTuple_SetItem(t, n++, PyInt_FromLong(value));
128 PyTuple_SetItem(t, n++, PyString_FromString(field_str));
129
Adrian Huntera5563ed2014-07-31 09:01:01 +0300130 try_call_object(handler_name, t);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600131
132 Py_DECREF(t);
133}
134
135static void define_values(enum print_arg_type field_type,
136 struct print_flag_sym *field,
137 const char *ev_name,
138 const char *field_name)
139{
140 define_value(field_type, ev_name, field_name, field->value,
141 field->str);
142
143 if (field->next)
144 define_values(field_type, field->next, ev_name, field_name);
145}
146
147static void define_field(enum print_arg_type field_type,
148 const char *ev_name,
149 const char *field_name,
150 const char *delim)
151{
152 const char *handler_name = "define_flag_field";
Adrian Huntera5563ed2014-07-31 09:01:01 +0300153 PyObject *t;
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600154 unsigned n = 0;
155
156 if (field_type == PRINT_SYMBOL)
157 handler_name = "define_symbolic_field";
158
Tom Zanussi44ad9cd2010-02-22 01:12:59 -0600159 if (field_type == PRINT_FLAGS)
160 t = PyTuple_New(3);
161 else
162 t = PyTuple_New(2);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600163 if (!t)
164 Py_FatalError("couldn't create Python tuple");
165
166 PyTuple_SetItem(t, n++, PyString_FromString(ev_name));
167 PyTuple_SetItem(t, n++, PyString_FromString(field_name));
168 if (field_type == PRINT_FLAGS)
169 PyTuple_SetItem(t, n++, PyString_FromString(delim));
170
Adrian Huntera5563ed2014-07-31 09:01:01 +0300171 try_call_object(handler_name, t);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600172
173 Py_DECREF(t);
174}
175
Steven Rostedtaaf045f2012-04-06 00:47:56 +0200176static void define_event_symbols(struct event_format *event,
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600177 const char *ev_name,
178 struct print_arg *args)
179{
180 switch (args->type) {
181 case PRINT_NULL:
182 break;
183 case PRINT_ATOM:
184 define_value(PRINT_FLAGS, ev_name, cur_field_name, "0",
185 args->atom.atom);
186 zero_flag_atom = 0;
187 break;
188 case PRINT_FIELD:
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -0300189 free(cur_field_name);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600190 cur_field_name = strdup(args->field.name);
191 break;
192 case PRINT_FLAGS:
193 define_event_symbols(event, ev_name, args->flags.field);
194 define_field(PRINT_FLAGS, ev_name, cur_field_name,
195 args->flags.delim);
196 define_values(PRINT_FLAGS, args->flags.flags, ev_name,
197 cur_field_name);
198 break;
199 case PRINT_SYMBOL:
200 define_event_symbols(event, ev_name, args->symbol.field);
201 define_field(PRINT_SYMBOL, ev_name, cur_field_name, NULL);
202 define_values(PRINT_SYMBOL, args->symbol.symbols, ev_name,
203 cur_field_name);
204 break;
Namhyung Kime080e6f2012-06-27 09:41:41 +0900205 case PRINT_HEX:
206 define_event_symbols(event, ev_name, args->hex.field);
207 define_event_symbols(event, ev_name, args->hex.size);
208 break;
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600209 case PRINT_STRING:
210 break;
211 case PRINT_TYPE:
212 define_event_symbols(event, ev_name, args->typecast.item);
213 break;
214 case PRINT_OP:
215 if (strcmp(args->op.op, ":") == 0)
216 zero_flag_atom = 1;
217 define_event_symbols(event, ev_name, args->op.left);
218 define_event_symbols(event, ev_name, args->op.right);
219 break;
220 default:
Steven Rostedtaaf045f2012-04-06 00:47:56 +0200221 /* gcc warns for these? */
222 case PRINT_BSTRING:
223 case PRINT_DYNAMIC_ARRAY:
224 case PRINT_FUNC:
Steven Rostedt (Red Hat)473a7782014-06-02 23:20:16 -0400225 case PRINT_BITMASK:
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600226 /* we should warn... */
227 return;
228 }
229
230 if (args->next)
231 define_event_symbols(event, ev_name, args->next);
232}
233
Arnaldo Carvalho de Melofcf65bf2012-08-07 09:58:03 -0300234static inline struct event_format *find_cache_event(struct perf_evsel *evsel)
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600235{
236 static char ev_name[256];
Steven Rostedtaaf045f2012-04-06 00:47:56 +0200237 struct event_format *event;
Arnaldo Carvalho de Melofcf65bf2012-08-07 09:58:03 -0300238 int type = evsel->attr.config;
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600239
Arnaldo Carvalho de Melofcf65bf2012-08-07 09:58:03 -0300240 /*
241 * XXX: Do we really need to cache this since now we have evsel->tp_format
242 * cached already? Need to re-read this "cache" routine that as well calls
243 * define_event_symbols() :-\
244 */
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600245 if (events[type])
246 return events[type];
247
Arnaldo Carvalho de Melofcf65bf2012-08-07 09:58:03 -0300248 events[type] = event = evsel->tp_format;
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600249 if (!event)
250 return NULL;
251
252 sprintf(ev_name, "%s__%s", event->system, event->name);
253
254 define_event_symbols(event, ev_name, event->print_fmt.args);
255
256 return event;
257}
258
Sebastian Andrzej Siewior33058b92014-05-27 18:14:33 +0200259static PyObject *get_field_numeric_entry(struct event_format *event,
260 struct format_field *field, void *data)
261{
Sebastian Andrzej Siewior8ac631c2014-05-27 18:14:34 +0200262 bool is_array = field->flags & FIELD_IS_ARRAY;
263 PyObject *obj, *list = NULL;
Sebastian Andrzej Siewior33058b92014-05-27 18:14:33 +0200264 unsigned long long val;
Sebastian Andrzej Siewior8ac631c2014-05-27 18:14:34 +0200265 unsigned int item_size, n_items, i;
Sebastian Andrzej Siewior33058b92014-05-27 18:14:33 +0200266
Sebastian Andrzej Siewior8ac631c2014-05-27 18:14:34 +0200267 if (is_array) {
268 list = PyList_New(field->arraylen);
269 item_size = field->size / field->arraylen;
270 n_items = field->arraylen;
Sebastian Andrzej Siewior33058b92014-05-27 18:14:33 +0200271 } else {
Sebastian Andrzej Siewior8ac631c2014-05-27 18:14:34 +0200272 item_size = field->size;
273 n_items = 1;
Sebastian Andrzej Siewior33058b92014-05-27 18:14:33 +0200274 }
Sebastian Andrzej Siewior8ac631c2014-05-27 18:14:34 +0200275
276 for (i = 0; i < n_items; i++) {
277
278 val = read_size(event, data + field->offset + i * item_size,
279 item_size);
280 if (field->flags & FIELD_IS_SIGNED) {
281 if ((long long)val >= LONG_MIN &&
282 (long long)val <= LONG_MAX)
283 obj = PyInt_FromLong(val);
284 else
285 obj = PyLong_FromLongLong(val);
286 } else {
287 if (val <= LONG_MAX)
288 obj = PyInt_FromLong(val);
289 else
290 obj = PyLong_FromUnsignedLongLong(val);
291 }
292 if (is_array)
293 PyList_SET_ITEM(list, i, obj);
294 }
295 if (is_array)
296 obj = list;
Sebastian Andrzej Siewior33058b92014-05-27 18:14:33 +0200297 return obj;
298}
299
Joseph Schuchart0f5f5bc2014-07-10 13:50:51 +0200300
301static PyObject *python_process_callchain(struct perf_sample *sample,
302 struct perf_evsel *evsel,
303 struct addr_location *al)
304{
305 PyObject *pylist;
306
307 pylist = PyList_New(0);
308 if (!pylist)
309 Py_FatalError("couldn't create Python list");
310
311 if (!symbol_conf.use_callchain || !sample->callchain)
312 goto exit;
313
314 if (machine__resolve_callchain(al->machine, evsel, al->thread,
315 sample, NULL, NULL,
316 PERF_MAX_STACK_DEPTH) != 0) {
317 pr_err("Failed to resolve callchain. Skipping\n");
318 goto exit;
319 }
320 callchain_cursor_commit(&callchain_cursor);
321
322
323 while (1) {
324 PyObject *pyelem;
325 struct callchain_cursor_node *node;
326 node = callchain_cursor_current(&callchain_cursor);
327 if (!node)
328 break;
329
330 pyelem = PyDict_New();
331 if (!pyelem)
332 Py_FatalError("couldn't create Python dictionary");
333
334
335 pydict_set_item_string_decref(pyelem, "ip",
336 PyLong_FromUnsignedLongLong(node->ip));
337
338 if (node->sym) {
339 PyObject *pysym = PyDict_New();
340 if (!pysym)
341 Py_FatalError("couldn't create Python dictionary");
342 pydict_set_item_string_decref(pysym, "start",
343 PyLong_FromUnsignedLongLong(node->sym->start));
344 pydict_set_item_string_decref(pysym, "end",
345 PyLong_FromUnsignedLongLong(node->sym->end));
346 pydict_set_item_string_decref(pysym, "binding",
347 PyInt_FromLong(node->sym->binding));
348 pydict_set_item_string_decref(pysym, "name",
349 PyString_FromStringAndSize(node->sym->name,
350 node->sym->namelen));
351 pydict_set_item_string_decref(pyelem, "sym", pysym);
352 }
353
354 if (node->map) {
355 struct map *map = node->map;
356 const char *dsoname = "[unknown]";
357 if (map && map->dso && (map->dso->name || map->dso->long_name)) {
358 if (symbol_conf.show_kernel_path && map->dso->long_name)
359 dsoname = map->dso->long_name;
360 else if (map->dso->name)
361 dsoname = map->dso->name;
362 }
363 pydict_set_item_string_decref(pyelem, "dso",
364 PyString_FromString(dsoname));
365 }
366
367 callchain_cursor_advance(&callchain_cursor);
368 PyList_Append(pylist, pyelem);
369 Py_DECREF(pyelem);
370 }
371
372exit:
373 return pylist;
374}
375
376
Arnaldo Carvalho de Melob7fff6b52013-12-19 16:34:52 -0300377static void python_process_tracepoint(struct perf_sample *sample,
378 struct perf_evsel *evsel,
379 struct thread *thread,
380 struct addr_location *al)
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600381{
Adrian Huntera5563ed2014-07-31 09:01:01 +0300382 PyObject *handler, *context, *t, *obj, *callchain;
Joseph Schuchart0f5f5bc2014-07-10 13:50:51 +0200383 PyObject *dict = NULL;
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600384 static char handler_name[256];
385 struct format_field *field;
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600386 unsigned long s, ns;
Steven Rostedtaaf045f2012-04-06 00:47:56 +0200387 struct event_format *event;
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600388 unsigned n = 0;
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600389 int pid;
David Ahernbe6d8422011-03-09 22:23:23 -0700390 int cpu = sample->cpu;
391 void *data = sample->raw_data;
392 unsigned long long nsecs = sample->time;
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +0200393 const char *comm = thread__comm_str(thread);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600394
395 t = PyTuple_New(MAX_FIELDS);
396 if (!t)
397 Py_FatalError("couldn't create Python tuple");
398
Arnaldo Carvalho de Melofcf65bf2012-08-07 09:58:03 -0300399 event = find_cache_event(evsel);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600400 if (!event)
Arnaldo Carvalho de Melofcf65bf2012-08-07 09:58:03 -0300401 die("ug! no event found for type %d", (int)evsel->attr.config);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600402
Arnaldo Carvalho de Melo97822432012-08-07 23:50:21 -0300403 pid = raw_field_value(event, "common_pid", data);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600404
405 sprintf(handler_name, "%s__%s", event->system, event->name);
406
Adrian Huntera5563ed2014-07-31 09:01:01 +0300407 handler = get_handler(handler_name);
Pierre Tardyc0251482010-05-31 23:12:09 +0200408 if (!handler) {
409 dict = PyDict_New();
410 if (!dict)
411 Py_FatalError("couldn't create Python dict");
412 }
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600413 s = nsecs / NSECS_PER_SEC;
414 ns = nsecs - s * NSECS_PER_SEC;
415
416 scripting_context->event_data = data;
Tom Zanussi2de95332013-01-18 13:51:27 -0600417 scripting_context->pevent = evsel->tp_format->pevent;
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600418
419 context = PyCObject_FromVoidPtr(scripting_context, NULL);
420
421 PyTuple_SetItem(t, n++, PyString_FromString(handler_name));
Kyle McMartinfb7d0b32011-01-24 11:13:04 -0500422 PyTuple_SetItem(t, n++, context);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600423
Joseph Schuchart0f5f5bc2014-07-10 13:50:51 +0200424 /* ip unwinding */
425 callchain = python_process_callchain(sample, evsel, al);
426
Pierre Tardyc0251482010-05-31 23:12:09 +0200427 if (handler) {
428 PyTuple_SetItem(t, n++, PyInt_FromLong(cpu));
429 PyTuple_SetItem(t, n++, PyInt_FromLong(s));
430 PyTuple_SetItem(t, n++, PyInt_FromLong(ns));
431 PyTuple_SetItem(t, n++, PyInt_FromLong(pid));
432 PyTuple_SetItem(t, n++, PyString_FromString(comm));
Joseph Schuchart0f5f5bc2014-07-10 13:50:51 +0200433 PyTuple_SetItem(t, n++, callchain);
Pierre Tardyc0251482010-05-31 23:12:09 +0200434 } else {
Joseph Schuchartc0268e82013-10-24 10:10:51 -0300435 pydict_set_item_string_decref(dict, "common_cpu", PyInt_FromLong(cpu));
436 pydict_set_item_string_decref(dict, "common_s", PyInt_FromLong(s));
437 pydict_set_item_string_decref(dict, "common_ns", PyInt_FromLong(ns));
438 pydict_set_item_string_decref(dict, "common_pid", PyInt_FromLong(pid));
439 pydict_set_item_string_decref(dict, "common_comm", PyString_FromString(comm));
Joseph Schuchart0f5f5bc2014-07-10 13:50:51 +0200440 pydict_set_item_string_decref(dict, "common_callchain", callchain);
Pierre Tardyc0251482010-05-31 23:12:09 +0200441 }
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600442 for (field = event->format.fields; field; field = field->next) {
443 if (field->flags & FIELD_IS_STRING) {
444 int offset;
445 if (field->flags & FIELD_IS_DYNAMIC) {
446 offset = *(int *)(data + field->offset);
447 offset &= 0xffff;
448 } else
449 offset = field->offset;
Tom Zanussib1dcc032010-04-01 23:58:25 -0500450 obj = PyString_FromString((char *)data + offset);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600451 } else { /* FIELD_IS_NUMERIC */
Sebastian Andrzej Siewior33058b92014-05-27 18:14:33 +0200452 obj = get_field_numeric_entry(event, field, data);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600453 }
Pierre Tardyc0251482010-05-31 23:12:09 +0200454 if (handler)
455 PyTuple_SetItem(t, n++, obj);
456 else
Joseph Schuchartc0268e82013-10-24 10:10:51 -0300457 pydict_set_item_string_decref(dict, field->name, obj);
Pierre Tardyc0251482010-05-31 23:12:09 +0200458
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600459 }
Joseph Schuchart0f5f5bc2014-07-10 13:50:51 +0200460
Pierre Tardyc0251482010-05-31 23:12:09 +0200461 if (!handler)
462 PyTuple_SetItem(t, n++, dict);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600463
464 if (_PyTuple_Resize(&t, n) == -1)
465 Py_FatalError("error resizing Python tuple");
466
Pierre Tardyc0251482010-05-31 23:12:09 +0200467 if (handler) {
Adrian Huntera5563ed2014-07-31 09:01:01 +0300468 call_object(handler, t, handler_name);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600469 } else {
Adrian Huntera5563ed2014-07-31 09:01:01 +0300470 try_call_object("trace_unhandled", t);
Pierre Tardyc0251482010-05-31 23:12:09 +0200471 Py_DECREF(dict);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600472 }
473
474 Py_DECREF(t);
475}
476
Arnaldo Carvalho de Melob7fff6b52013-12-19 16:34:52 -0300477static void python_process_general_event(struct perf_sample *sample,
Feng Tang6a6daec2012-08-08 17:57:51 +0800478 struct perf_evsel *evsel,
David Ahern2eaa1b42013-07-18 16:06:15 -0600479 struct thread *thread,
Feng Tang87b6a3a2012-08-09 13:46:13 +0800480 struct addr_location *al)
Feng Tang6a6daec2012-08-08 17:57:51 +0800481{
Adrian Huntera5563ed2014-07-31 09:01:01 +0300482 PyObject *handler, *t, *dict, *callchain, *dict_sample;
Feng Tang6a6daec2012-08-08 17:57:51 +0800483 static char handler_name[64];
484 unsigned n = 0;
Feng Tang6a6daec2012-08-08 17:57:51 +0800485
Feng Tangfd6b8582012-08-08 17:57:53 +0800486 /*
487 * Use the MAX_FIELDS to make the function expandable, though
Feng Tang87b6a3a2012-08-09 13:46:13 +0800488 * currently there is only one item for the tuple.
Feng Tangfd6b8582012-08-08 17:57:53 +0800489 */
Feng Tang6a6daec2012-08-08 17:57:51 +0800490 t = PyTuple_New(MAX_FIELDS);
491 if (!t)
492 Py_FatalError("couldn't create Python tuple");
493
Feng Tangfd6b8582012-08-08 17:57:53 +0800494 dict = PyDict_New();
495 if (!dict)
496 Py_FatalError("couldn't create Python dictionary");
497
Joseph Schuchart57608cf2014-07-10 13:50:56 +0200498 dict_sample = PyDict_New();
499 if (!dict_sample)
500 Py_FatalError("couldn't create Python dictionary");
501
Feng Tang6a6daec2012-08-08 17:57:51 +0800502 snprintf(handler_name, sizeof(handler_name), "%s", "process_event");
503
Adrian Huntera5563ed2014-07-31 09:01:01 +0300504 handler = get_handler(handler_name);
505 if (!handler)
Feng Tang6a6daec2012-08-08 17:57:51 +0800506 goto exit;
Feng Tang6a6daec2012-08-08 17:57:51 +0800507
Joseph Schuchartc0268e82013-10-24 10:10:51 -0300508 pydict_set_item_string_decref(dict, "ev_name", PyString_FromString(perf_evsel__name(evsel)));
509 pydict_set_item_string_decref(dict, "attr", PyString_FromStringAndSize(
Feng Tangfd6b8582012-08-08 17:57:53 +0800510 (const char *)&evsel->attr, sizeof(evsel->attr)));
Joseph Schuchart57608cf2014-07-10 13:50:56 +0200511
512 pydict_set_item_string_decref(dict_sample, "pid",
513 PyInt_FromLong(sample->pid));
514 pydict_set_item_string_decref(dict_sample, "tid",
515 PyInt_FromLong(sample->tid));
516 pydict_set_item_string_decref(dict_sample, "cpu",
517 PyInt_FromLong(sample->cpu));
518 pydict_set_item_string_decref(dict_sample, "ip",
519 PyLong_FromUnsignedLongLong(sample->ip));
520 pydict_set_item_string_decref(dict_sample, "time",
521 PyLong_FromUnsignedLongLong(sample->time));
522 pydict_set_item_string_decref(dict_sample, "period",
523 PyLong_FromUnsignedLongLong(sample->period));
524 pydict_set_item_string_decref(dict, "sample", dict_sample);
525
Joseph Schuchartc0268e82013-10-24 10:10:51 -0300526 pydict_set_item_string_decref(dict, "raw_buf", PyString_FromStringAndSize(
Feng Tangfd6b8582012-08-08 17:57:53 +0800527 (const char *)sample->raw_data, sample->raw_size));
Joseph Schuchartc0268e82013-10-24 10:10:51 -0300528 pydict_set_item_string_decref(dict, "comm",
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +0200529 PyString_FromString(thread__comm_str(thread)));
Feng Tangfd6b8582012-08-08 17:57:53 +0800530 if (al->map) {
Joseph Schuchartc0268e82013-10-24 10:10:51 -0300531 pydict_set_item_string_decref(dict, "dso",
Feng Tangfd6b8582012-08-08 17:57:53 +0800532 PyString_FromString(al->map->dso->name));
533 }
534 if (al->sym) {
Joseph Schuchartc0268e82013-10-24 10:10:51 -0300535 pydict_set_item_string_decref(dict, "symbol",
Feng Tangfd6b8582012-08-08 17:57:53 +0800536 PyString_FromString(al->sym->name));
537 }
Feng Tang6a6daec2012-08-08 17:57:51 +0800538
Joseph Schuchart0f5f5bc2014-07-10 13:50:51 +0200539 /* ip unwinding */
540 callchain = python_process_callchain(sample, evsel, al);
541 pydict_set_item_string_decref(dict, "callchain", callchain);
542
Feng Tangfd6b8582012-08-08 17:57:53 +0800543 PyTuple_SetItem(t, n++, dict);
Feng Tang6a6daec2012-08-08 17:57:51 +0800544 if (_PyTuple_Resize(&t, n) == -1)
545 Py_FatalError("error resizing Python tuple");
546
Adrian Huntera5563ed2014-07-31 09:01:01 +0300547 call_object(handler, t, handler_name);
Feng Tang6a6daec2012-08-08 17:57:51 +0800548exit:
Feng Tangfd6b8582012-08-08 17:57:53 +0800549 Py_DECREF(dict);
Feng Tang6a6daec2012-08-08 17:57:51 +0800550 Py_DECREF(t);
551}
552
Arnaldo Carvalho de Melob7fff6b52013-12-19 16:34:52 -0300553static void python_process_event(union perf_event *event __maybe_unused,
Feng Tang6a6daec2012-08-08 17:57:51 +0800554 struct perf_sample *sample,
555 struct perf_evsel *evsel,
David Ahern2eaa1b42013-07-18 16:06:15 -0600556 struct thread *thread,
Feng Tang73994dc2012-08-08 17:57:52 +0800557 struct addr_location *al)
Feng Tang6a6daec2012-08-08 17:57:51 +0800558{
559 switch (evsel->attr.type) {
560 case PERF_TYPE_TRACEPOINT:
Arnaldo Carvalho de Melob7fff6b52013-12-19 16:34:52 -0300561 python_process_tracepoint(sample, evsel, thread, al);
Feng Tang6a6daec2012-08-08 17:57:51 +0800562 break;
563 /* Reserve for future process_hw/sw/raw APIs */
564 default:
Arnaldo Carvalho de Melob7fff6b52013-12-19 16:34:52 -0300565 python_process_general_event(sample, evsel, thread, al);
Feng Tang6a6daec2012-08-08 17:57:51 +0800566 }
567}
568
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600569static int run_start_sub(void)
570{
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600571 main_module = PyImport_AddModule("__main__");
572 if (main_module == NULL)
573 return -1;
574 Py_INCREF(main_module);
575
576 main_dict = PyModule_GetDict(main_module);
Adrian Huntera5563ed2014-07-31 09:01:01 +0300577 if (main_dict == NULL)
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600578 goto error;
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600579 Py_INCREF(main_dict);
580
Adrian Huntera5563ed2014-07-31 09:01:01 +0300581 try_call_object("trace_begin", NULL);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600582
Adrian Huntera5563ed2014-07-31 09:01:01 +0300583 return 0;
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600584
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600585error:
586 Py_XDECREF(main_dict);
587 Py_XDECREF(main_module);
Adrian Huntera5563ed2014-07-31 09:01:01 +0300588 return -1;
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600589}
590
591/*
592 * Start trace script
593 */
594static int python_start_script(const char *script, int argc, const char **argv)
595{
596 const char **command_line;
597 char buf[PATH_MAX];
598 int i, err = 0;
599 FILE *fp;
600
601 command_line = malloc((argc + 1) * sizeof(const char *));
602 command_line[0] = script;
603 for (i = 1; i < argc + 1; i++)
604 command_line[i] = argv[i - 1];
605
606 Py_Initialize();
607
608 initperf_trace_context();
609
610 PySys_SetArgv(argc + 1, (char **)command_line);
611
612 fp = fopen(script, "r");
613 if (!fp) {
614 sprintf(buf, "Can't open python script \"%s\"", script);
615 perror(buf);
616 err = -1;
617 goto error;
618 }
619
620 err = PyRun_SimpleFile(fp, script);
621 if (err) {
622 fprintf(stderr, "Error running python script %s\n", script);
623 goto error;
624 }
625
626 err = run_start_sub();
627 if (err) {
628 fprintf(stderr, "Error starting python script %s\n", script);
629 goto error;
630 }
631
632 free(command_line);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600633
634 return err;
635error:
636 Py_Finalize();
637 free(command_line);
638
639 return err;
640}
641
Adrian Hunterd445dd22014-08-15 22:08:37 +0300642static int python_flush_script(void)
643{
644 return 0;
645}
646
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600647/*
648 * Stop trace script
649 */
650static int python_stop_script(void)
651{
Adrian Huntera5563ed2014-07-31 09:01:01 +0300652 try_call_object("trace_end", NULL);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600653
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600654 Py_XDECREF(main_dict);
655 Py_XDECREF(main_module);
656 Py_Finalize();
657
Adrian Huntera5563ed2014-07-31 09:01:01 +0300658 return 0;
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600659}
660
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -0300661static int python_generate_script(struct pevent *pevent, const char *outfile)
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600662{
Steven Rostedtaaf045f2012-04-06 00:47:56 +0200663 struct event_format *event = NULL;
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600664 struct format_field *f;
665 char fname[PATH_MAX];
666 int not_first, count;
667 FILE *ofp;
668
669 sprintf(fname, "%s.py", outfile);
670 ofp = fopen(fname, "w");
671 if (ofp == NULL) {
672 fprintf(stderr, "couldn't open %s\n", fname);
673 return -1;
674 }
Ingo Molnar133dc4c2010-11-16 18:45:39 +0100675 fprintf(ofp, "# perf script event handlers, "
676 "generated by perf script -g python\n");
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600677
678 fprintf(ofp, "# Licensed under the terms of the GNU GPL"
679 " License version 2\n\n");
680
681 fprintf(ofp, "# The common_* event handler fields are the most useful "
682 "fields common to\n");
683
684 fprintf(ofp, "# all events. They don't necessarily correspond to "
685 "the 'common_*' fields\n");
686
687 fprintf(ofp, "# in the format files. Those fields not available as "
688 "handler params can\n");
689
690 fprintf(ofp, "# be retrieved using Python functions of the form "
691 "common_*(context).\n");
692
693 fprintf(ofp, "# See the perf-trace-python Documentation for the list "
694 "of available functions.\n\n");
695
696 fprintf(ofp, "import os\n");
697 fprintf(ofp, "import sys\n\n");
698
699 fprintf(ofp, "sys.path.append(os.environ['PERF_EXEC_PATH'] + \\\n");
700 fprintf(ofp, "\t'/scripts/python/Perf-Trace-Util/lib/Perf/Trace')\n");
701 fprintf(ofp, "\nfrom perf_trace_context import *\n");
702 fprintf(ofp, "from Core import *\n\n\n");
703
704 fprintf(ofp, "def trace_begin():\n");
705 fprintf(ofp, "\tprint \"in trace_begin\"\n\n");
706
707 fprintf(ofp, "def trace_end():\n");
708 fprintf(ofp, "\tprint \"in trace_end\"\n\n");
709
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -0300710 while ((event = trace_find_next_event(pevent, event))) {
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600711 fprintf(ofp, "def %s__%s(", event->system, event->name);
712 fprintf(ofp, "event_name, ");
713 fprintf(ofp, "context, ");
714 fprintf(ofp, "common_cpu,\n");
715 fprintf(ofp, "\tcommon_secs, ");
716 fprintf(ofp, "common_nsecs, ");
717 fprintf(ofp, "common_pid, ");
718 fprintf(ofp, "common_comm,\n\t");
Joseph Schuchart0f5f5bc2014-07-10 13:50:51 +0200719 fprintf(ofp, "common_callchain, ");
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600720
721 not_first = 0;
722 count = 0;
723
724 for (f = event->format.fields; f; f = f->next) {
725 if (not_first++)
726 fprintf(ofp, ", ");
727 if (++count % 5 == 0)
728 fprintf(ofp, "\n\t");
729
730 fprintf(ofp, "%s", f->name);
731 }
732 fprintf(ofp, "):\n");
733
734 fprintf(ofp, "\t\tprint_header(event_name, common_cpu, "
735 "common_secs, common_nsecs,\n\t\t\t"
736 "common_pid, common_comm)\n\n");
737
738 fprintf(ofp, "\t\tprint \"");
739
740 not_first = 0;
741 count = 0;
742
743 for (f = event->format.fields; f; f = f->next) {
744 if (not_first++)
745 fprintf(ofp, ", ");
746 if (count && count % 3 == 0) {
747 fprintf(ofp, "\" \\\n\t\t\"");
748 }
749 count++;
750
751 fprintf(ofp, "%s=", f->name);
752 if (f->flags & FIELD_IS_STRING ||
753 f->flags & FIELD_IS_FLAG ||
Namhyung Kime646fe72014-05-29 13:44:55 +0900754 f->flags & FIELD_IS_ARRAY ||
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600755 f->flags & FIELD_IS_SYMBOLIC)
756 fprintf(ofp, "%%s");
757 else if (f->flags & FIELD_IS_SIGNED)
758 fprintf(ofp, "%%d");
759 else
760 fprintf(ofp, "%%u");
761 }
762
Joseph Schuchart0f5f5bc2014-07-10 13:50:51 +0200763 fprintf(ofp, "\" %% \\\n\t\t(");
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600764
765 not_first = 0;
766 count = 0;
767
768 for (f = event->format.fields; f; f = f->next) {
769 if (not_first++)
770 fprintf(ofp, ", ");
771
772 if (++count % 5 == 0)
773 fprintf(ofp, "\n\t\t");
774
775 if (f->flags & FIELD_IS_FLAG) {
776 if ((count - 1) % 5 != 0) {
777 fprintf(ofp, "\n\t\t");
778 count = 4;
779 }
780 fprintf(ofp, "flag_str(\"");
781 fprintf(ofp, "%s__%s\", ", event->system,
782 event->name);
783 fprintf(ofp, "\"%s\", %s)", f->name,
784 f->name);
785 } else if (f->flags & FIELD_IS_SYMBOLIC) {
786 if ((count - 1) % 5 != 0) {
787 fprintf(ofp, "\n\t\t");
788 count = 4;
789 }
790 fprintf(ofp, "symbol_str(\"");
791 fprintf(ofp, "%s__%s\", ", event->system,
792 event->name);
793 fprintf(ofp, "\"%s\", %s)", f->name,
794 f->name);
795 } else
796 fprintf(ofp, "%s", f->name);
797 }
798
Joseph Schuchart0f5f5bc2014-07-10 13:50:51 +0200799 fprintf(ofp, ")\n\n");
800
801 fprintf(ofp, "\t\tfor node in common_callchain:");
802 fprintf(ofp, "\n\t\t\tif 'sym' in node:");
803 fprintf(ofp, "\n\t\t\t\tprint \"\\t[%%x] %%s\" %% (node['ip'], node['sym']['name'])");
804 fprintf(ofp, "\n\t\t\telse:");
805 fprintf(ofp, "\n\t\t\t\tprint \"\t[%%x]\" %% (node['ip'])\n\n");
806 fprintf(ofp, "\t\tprint \"\\n\"\n\n");
807
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600808 }
809
810 fprintf(ofp, "def trace_unhandled(event_name, context, "
Pierre Tardyc0251482010-05-31 23:12:09 +0200811 "event_fields_dict):\n");
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600812
Pierre Tardyc0251482010-05-31 23:12:09 +0200813 fprintf(ofp, "\t\tprint ' '.join(['%%s=%%s'%%(k,str(v))"
814 "for k,v in sorted(event_fields_dict.items())])\n\n");
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600815
816 fprintf(ofp, "def print_header("
817 "event_name, cpu, secs, nsecs, pid, comm):\n"
818 "\tprint \"%%-20s %%5u %%05u.%%09u %%8u %%-20s \" %% \\\n\t"
819 "(event_name, cpu, secs, nsecs, pid, comm),\n");
820
821 fclose(ofp);
822
823 fprintf(stderr, "generated Python script: %s\n", fname);
824
825 return 0;
826}
827
828struct scripting_ops python_scripting_ops = {
829 .name = "Python",
830 .start_script = python_start_script,
Adrian Hunterd445dd22014-08-15 22:08:37 +0300831 .flush_script = python_flush_script,
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600832 .stop_script = python_stop_script,
833 .process_event = python_process_event,
834 .generate_script = python_generate_script,
835};