blob: 496f21cadd97098751c22e054ec89311a29417f9 [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 Melo8f651ea2014-10-09 16:12:24 -030031#include "../callchain.h"
Arnaldo Carvalho de Melofcf65bf2012-08-07 09:58:03 -030032#include "../evsel.h"
Tom Zanussi7e4b21b2010-01-27 02:27:57 -060033#include "../util.h"
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -020034#include "../event.h"
35#include "../thread.h"
Tom Zanussi7e4b21b2010-01-27 02:27:57 -060036#include "../trace-event.h"
Joseph Schuchart0f5f5bc2014-07-10 13:50:51 +020037#include "../machine.h"
Tom Zanussi7e4b21b2010-01-27 02:27:57 -060038
39PyMODINIT_FUNC initperf_trace_context(void);
40
41#define FTRACE_MAX_EVENT \
42 ((1 << (sizeof(unsigned short) * 8)) - 1)
43
Steven Rostedtaaf045f2012-04-06 00:47:56 +020044struct event_format *events[FTRACE_MAX_EVENT];
Tom Zanussi7e4b21b2010-01-27 02:27:57 -060045
46#define MAX_FIELDS 64
47#define N_COMMON_FIELDS 7
48
49extern struct scripting_context *scripting_context;
50
51static char *cur_field_name;
52static int zero_flag_atom;
53
54static PyObject *main_module, *main_dict;
55
Joseph Schuchart05f832e2014-07-09 16:16:31 +020056static void handler_call_die(const char *handler_name) NORETURN;
Tom Zanussi7e4b21b2010-01-27 02:27:57 -060057static void handler_call_die(const char *handler_name)
58{
59 PyErr_Print();
60 Py_FatalError("problem in Python trace event handler");
Joseph Schuchart05f832e2014-07-09 16:16:31 +020061 // Py_FatalError does not return
62 // but we have to make the compiler happy
63 abort();
Tom Zanussi7e4b21b2010-01-27 02:27:57 -060064}
65
Joseph Schuchartc0268e82013-10-24 10:10:51 -030066/*
67 * Insert val into into the dictionary and decrement the reference counter.
68 * This is necessary for dictionaries since PyDict_SetItemString() does not
69 * steal a reference, as opposed to PyTuple_SetItem().
70 */
71static void pydict_set_item_string_decref(PyObject *dict, const char *key, PyObject *val)
72{
73 PyDict_SetItemString(dict, key, val);
74 Py_DECREF(val);
75}
76
Adrian Huntera5563ed2014-07-31 09:01:01 +030077static PyObject *get_handler(const char *handler_name)
78{
79 PyObject *handler;
80
81 handler = PyDict_GetItemString(main_dict, handler_name);
82 if (handler && !PyCallable_Check(handler))
83 return NULL;
84 return handler;
85}
86
87static void call_object(PyObject *handler, PyObject *args, const char *die_msg)
88{
89 PyObject *retval;
90
91 retval = PyObject_CallObject(handler, args);
92 if (retval == NULL)
93 handler_call_die(die_msg);
94 Py_DECREF(retval);
95}
96
97static void try_call_object(const char *handler_name, PyObject *args)
98{
99 PyObject *handler;
100
101 handler = get_handler(handler_name);
102 if (handler)
103 call_object(handler, args, handler_name);
104}
105
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600106static void define_value(enum print_arg_type field_type,
107 const char *ev_name,
108 const char *field_name,
109 const char *field_value,
110 const char *field_str)
111{
112 const char *handler_name = "define_flag_value";
Adrian Huntera5563ed2014-07-31 09:01:01 +0300113 PyObject *t;
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600114 unsigned long long value;
115 unsigned n = 0;
116
117 if (field_type == PRINT_SYMBOL)
118 handler_name = "define_symbolic_value";
119
Tom Zanussi44ad9cd2010-02-22 01:12:59 -0600120 t = PyTuple_New(4);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600121 if (!t)
122 Py_FatalError("couldn't create Python tuple");
123
124 value = eval_flag(field_value);
125
126 PyTuple_SetItem(t, n++, PyString_FromString(ev_name));
127 PyTuple_SetItem(t, n++, PyString_FromString(field_name));
128 PyTuple_SetItem(t, n++, PyInt_FromLong(value));
129 PyTuple_SetItem(t, n++, PyString_FromString(field_str));
130
Adrian Huntera5563ed2014-07-31 09:01:01 +0300131 try_call_object(handler_name, t);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600132
133 Py_DECREF(t);
134}
135
136static void define_values(enum print_arg_type field_type,
137 struct print_flag_sym *field,
138 const char *ev_name,
139 const char *field_name)
140{
141 define_value(field_type, ev_name, field_name, field->value,
142 field->str);
143
144 if (field->next)
145 define_values(field_type, field->next, ev_name, field_name);
146}
147
148static void define_field(enum print_arg_type field_type,
149 const char *ev_name,
150 const char *field_name,
151 const char *delim)
152{
153 const char *handler_name = "define_flag_field";
Adrian Huntera5563ed2014-07-31 09:01:01 +0300154 PyObject *t;
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600155 unsigned n = 0;
156
157 if (field_type == PRINT_SYMBOL)
158 handler_name = "define_symbolic_field";
159
Tom Zanussi44ad9cd2010-02-22 01:12:59 -0600160 if (field_type == PRINT_FLAGS)
161 t = PyTuple_New(3);
162 else
163 t = PyTuple_New(2);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600164 if (!t)
165 Py_FatalError("couldn't create Python tuple");
166
167 PyTuple_SetItem(t, n++, PyString_FromString(ev_name));
168 PyTuple_SetItem(t, n++, PyString_FromString(field_name));
169 if (field_type == PRINT_FLAGS)
170 PyTuple_SetItem(t, n++, PyString_FromString(delim));
171
Adrian Huntera5563ed2014-07-31 09:01:01 +0300172 try_call_object(handler_name, t);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600173
174 Py_DECREF(t);
175}
176
Steven Rostedtaaf045f2012-04-06 00:47:56 +0200177static void define_event_symbols(struct event_format *event,
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600178 const char *ev_name,
179 struct print_arg *args)
180{
181 switch (args->type) {
182 case PRINT_NULL:
183 break;
184 case PRINT_ATOM:
185 define_value(PRINT_FLAGS, ev_name, cur_field_name, "0",
186 args->atom.atom);
187 zero_flag_atom = 0;
188 break;
189 case PRINT_FIELD:
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -0300190 free(cur_field_name);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600191 cur_field_name = strdup(args->field.name);
192 break;
193 case PRINT_FLAGS:
194 define_event_symbols(event, ev_name, args->flags.field);
195 define_field(PRINT_FLAGS, ev_name, cur_field_name,
196 args->flags.delim);
197 define_values(PRINT_FLAGS, args->flags.flags, ev_name,
198 cur_field_name);
199 break;
200 case PRINT_SYMBOL:
201 define_event_symbols(event, ev_name, args->symbol.field);
202 define_field(PRINT_SYMBOL, ev_name, cur_field_name, NULL);
203 define_values(PRINT_SYMBOL, args->symbol.symbols, ev_name,
204 cur_field_name);
205 break;
Namhyung Kime080e6f2012-06-27 09:41:41 +0900206 case PRINT_HEX:
207 define_event_symbols(event, ev_name, args->hex.field);
208 define_event_symbols(event, ev_name, args->hex.size);
209 break;
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600210 case PRINT_STRING:
211 break;
212 case PRINT_TYPE:
213 define_event_symbols(event, ev_name, args->typecast.item);
214 break;
215 case PRINT_OP:
216 if (strcmp(args->op.op, ":") == 0)
217 zero_flag_atom = 1;
218 define_event_symbols(event, ev_name, args->op.left);
219 define_event_symbols(event, ev_name, args->op.right);
220 break;
221 default:
Steven Rostedtaaf045f2012-04-06 00:47:56 +0200222 /* gcc warns for these? */
223 case PRINT_BSTRING:
224 case PRINT_DYNAMIC_ARRAY:
225 case PRINT_FUNC:
Steven Rostedt (Red Hat)473a7782014-06-02 23:20:16 -0400226 case PRINT_BITMASK:
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600227 /* we should warn... */
228 return;
229 }
230
231 if (args->next)
232 define_event_symbols(event, ev_name, args->next);
233}
234
Arnaldo Carvalho de Melofcf65bf2012-08-07 09:58:03 -0300235static inline struct event_format *find_cache_event(struct perf_evsel *evsel)
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600236{
237 static char ev_name[256];
Steven Rostedtaaf045f2012-04-06 00:47:56 +0200238 struct event_format *event;
Arnaldo Carvalho de Melofcf65bf2012-08-07 09:58:03 -0300239 int type = evsel->attr.config;
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600240
Arnaldo Carvalho de Melofcf65bf2012-08-07 09:58:03 -0300241 /*
242 * XXX: Do we really need to cache this since now we have evsel->tp_format
243 * cached already? Need to re-read this "cache" routine that as well calls
244 * define_event_symbols() :-\
245 */
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600246 if (events[type])
247 return events[type];
248
Arnaldo Carvalho de Melofcf65bf2012-08-07 09:58:03 -0300249 events[type] = event = evsel->tp_format;
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600250 if (!event)
251 return NULL;
252
253 sprintf(ev_name, "%s__%s", event->system, event->name);
254
255 define_event_symbols(event, ev_name, event->print_fmt.args);
256
257 return event;
258}
259
Sebastian Andrzej Siewior33058b92014-05-27 18:14:33 +0200260static PyObject *get_field_numeric_entry(struct event_format *event,
261 struct format_field *field, void *data)
262{
Sebastian Andrzej Siewior8ac631c2014-05-27 18:14:34 +0200263 bool is_array = field->flags & FIELD_IS_ARRAY;
264 PyObject *obj, *list = NULL;
Sebastian Andrzej Siewior33058b92014-05-27 18:14:33 +0200265 unsigned long long val;
Sebastian Andrzej Siewior8ac631c2014-05-27 18:14:34 +0200266 unsigned int item_size, n_items, i;
Sebastian Andrzej Siewior33058b92014-05-27 18:14:33 +0200267
Sebastian Andrzej Siewior8ac631c2014-05-27 18:14:34 +0200268 if (is_array) {
269 list = PyList_New(field->arraylen);
270 item_size = field->size / field->arraylen;
271 n_items = field->arraylen;
Sebastian Andrzej Siewior33058b92014-05-27 18:14:33 +0200272 } else {
Sebastian Andrzej Siewior8ac631c2014-05-27 18:14:34 +0200273 item_size = field->size;
274 n_items = 1;
Sebastian Andrzej Siewior33058b92014-05-27 18:14:33 +0200275 }
Sebastian Andrzej Siewior8ac631c2014-05-27 18:14:34 +0200276
277 for (i = 0; i < n_items; i++) {
278
279 val = read_size(event, data + field->offset + i * item_size,
280 item_size);
281 if (field->flags & FIELD_IS_SIGNED) {
282 if ((long long)val >= LONG_MIN &&
283 (long long)val <= LONG_MAX)
284 obj = PyInt_FromLong(val);
285 else
286 obj = PyLong_FromLongLong(val);
287 } else {
288 if (val <= LONG_MAX)
289 obj = PyInt_FromLong(val);
290 else
291 obj = PyLong_FromUnsignedLongLong(val);
292 }
293 if (is_array)
294 PyList_SET_ITEM(list, i, obj);
295 }
296 if (is_array)
297 obj = list;
Sebastian Andrzej Siewior33058b92014-05-27 18:14:33 +0200298 return obj;
299}
300
Joseph Schuchart0f5f5bc2014-07-10 13:50:51 +0200301
302static PyObject *python_process_callchain(struct perf_sample *sample,
303 struct perf_evsel *evsel,
304 struct addr_location *al)
305{
306 PyObject *pylist;
307
308 pylist = PyList_New(0);
309 if (!pylist)
310 Py_FatalError("couldn't create Python list");
311
312 if (!symbol_conf.use_callchain || !sample->callchain)
313 goto exit;
314
315 if (machine__resolve_callchain(al->machine, evsel, al->thread,
316 sample, NULL, NULL,
317 PERF_MAX_STACK_DEPTH) != 0) {
318 pr_err("Failed to resolve callchain. Skipping\n");
319 goto exit;
320 }
321 callchain_cursor_commit(&callchain_cursor);
322
323
324 while (1) {
325 PyObject *pyelem;
326 struct callchain_cursor_node *node;
327 node = callchain_cursor_current(&callchain_cursor);
328 if (!node)
329 break;
330
331 pyelem = PyDict_New();
332 if (!pyelem)
333 Py_FatalError("couldn't create Python dictionary");
334
335
336 pydict_set_item_string_decref(pyelem, "ip",
337 PyLong_FromUnsignedLongLong(node->ip));
338
339 if (node->sym) {
340 PyObject *pysym = PyDict_New();
341 if (!pysym)
342 Py_FatalError("couldn't create Python dictionary");
343 pydict_set_item_string_decref(pysym, "start",
344 PyLong_FromUnsignedLongLong(node->sym->start));
345 pydict_set_item_string_decref(pysym, "end",
346 PyLong_FromUnsignedLongLong(node->sym->end));
347 pydict_set_item_string_decref(pysym, "binding",
348 PyInt_FromLong(node->sym->binding));
349 pydict_set_item_string_decref(pysym, "name",
350 PyString_FromStringAndSize(node->sym->name,
351 node->sym->namelen));
352 pydict_set_item_string_decref(pyelem, "sym", pysym);
353 }
354
355 if (node->map) {
356 struct map *map = node->map;
357 const char *dsoname = "[unknown]";
358 if (map && map->dso && (map->dso->name || map->dso->long_name)) {
359 if (symbol_conf.show_kernel_path && map->dso->long_name)
360 dsoname = map->dso->long_name;
361 else if (map->dso->name)
362 dsoname = map->dso->name;
363 }
364 pydict_set_item_string_decref(pyelem, "dso",
365 PyString_FromString(dsoname));
366 }
367
368 callchain_cursor_advance(&callchain_cursor);
369 PyList_Append(pylist, pyelem);
370 Py_DECREF(pyelem);
371 }
372
373exit:
374 return pylist;
375}
376
377
Arnaldo Carvalho de Melob7fff6b52013-12-19 16:34:52 -0300378static void python_process_tracepoint(struct perf_sample *sample,
379 struct perf_evsel *evsel,
380 struct thread *thread,
381 struct addr_location *al)
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600382{
Adrian Huntera5563ed2014-07-31 09:01:01 +0300383 PyObject *handler, *context, *t, *obj, *callchain;
Joseph Schuchart0f5f5bc2014-07-10 13:50:51 +0200384 PyObject *dict = NULL;
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600385 static char handler_name[256];
386 struct format_field *field;
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600387 unsigned long s, ns;
Steven Rostedtaaf045f2012-04-06 00:47:56 +0200388 struct event_format *event;
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600389 unsigned n = 0;
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600390 int pid;
David Ahernbe6d8422011-03-09 22:23:23 -0700391 int cpu = sample->cpu;
392 void *data = sample->raw_data;
393 unsigned long long nsecs = sample->time;
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +0200394 const char *comm = thread__comm_str(thread);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600395
396 t = PyTuple_New(MAX_FIELDS);
397 if (!t)
398 Py_FatalError("couldn't create Python tuple");
399
Arnaldo Carvalho de Melofcf65bf2012-08-07 09:58:03 -0300400 event = find_cache_event(evsel);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600401 if (!event)
Arnaldo Carvalho de Melofcf65bf2012-08-07 09:58:03 -0300402 die("ug! no event found for type %d", (int)evsel->attr.config);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600403
Arnaldo Carvalho de Melo97822432012-08-07 23:50:21 -0300404 pid = raw_field_value(event, "common_pid", data);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600405
406 sprintf(handler_name, "%s__%s", event->system, event->name);
407
Adrian Huntera5563ed2014-07-31 09:01:01 +0300408 handler = get_handler(handler_name);
Pierre Tardyc0251482010-05-31 23:12:09 +0200409 if (!handler) {
410 dict = PyDict_New();
411 if (!dict)
412 Py_FatalError("couldn't create Python dict");
413 }
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600414 s = nsecs / NSECS_PER_SEC;
415 ns = nsecs - s * NSECS_PER_SEC;
416
417 scripting_context->event_data = data;
Tom Zanussi2de95332013-01-18 13:51:27 -0600418 scripting_context->pevent = evsel->tp_format->pevent;
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600419
420 context = PyCObject_FromVoidPtr(scripting_context, NULL);
421
422 PyTuple_SetItem(t, n++, PyString_FromString(handler_name));
Kyle McMartinfb7d0b32011-01-24 11:13:04 -0500423 PyTuple_SetItem(t, n++, context);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600424
Joseph Schuchart0f5f5bc2014-07-10 13:50:51 +0200425 /* ip unwinding */
426 callchain = python_process_callchain(sample, evsel, al);
427
Pierre Tardyc0251482010-05-31 23:12:09 +0200428 if (handler) {
429 PyTuple_SetItem(t, n++, PyInt_FromLong(cpu));
430 PyTuple_SetItem(t, n++, PyInt_FromLong(s));
431 PyTuple_SetItem(t, n++, PyInt_FromLong(ns));
432 PyTuple_SetItem(t, n++, PyInt_FromLong(pid));
433 PyTuple_SetItem(t, n++, PyString_FromString(comm));
Joseph Schuchart0f5f5bc2014-07-10 13:50:51 +0200434 PyTuple_SetItem(t, n++, callchain);
Pierre Tardyc0251482010-05-31 23:12:09 +0200435 } else {
Joseph Schuchartc0268e82013-10-24 10:10:51 -0300436 pydict_set_item_string_decref(dict, "common_cpu", PyInt_FromLong(cpu));
437 pydict_set_item_string_decref(dict, "common_s", PyInt_FromLong(s));
438 pydict_set_item_string_decref(dict, "common_ns", PyInt_FromLong(ns));
439 pydict_set_item_string_decref(dict, "common_pid", PyInt_FromLong(pid));
440 pydict_set_item_string_decref(dict, "common_comm", PyString_FromString(comm));
Joseph Schuchart0f5f5bc2014-07-10 13:50:51 +0200441 pydict_set_item_string_decref(dict, "common_callchain", callchain);
Pierre Tardyc0251482010-05-31 23:12:09 +0200442 }
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600443 for (field = event->format.fields; field; field = field->next) {
444 if (field->flags & FIELD_IS_STRING) {
445 int offset;
446 if (field->flags & FIELD_IS_DYNAMIC) {
447 offset = *(int *)(data + field->offset);
448 offset &= 0xffff;
449 } else
450 offset = field->offset;
Tom Zanussib1dcc032010-04-01 23:58:25 -0500451 obj = PyString_FromString((char *)data + offset);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600452 } else { /* FIELD_IS_NUMERIC */
Sebastian Andrzej Siewior33058b92014-05-27 18:14:33 +0200453 obj = get_field_numeric_entry(event, field, data);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600454 }
Pierre Tardyc0251482010-05-31 23:12:09 +0200455 if (handler)
456 PyTuple_SetItem(t, n++, obj);
457 else
Joseph Schuchartc0268e82013-10-24 10:10:51 -0300458 pydict_set_item_string_decref(dict, field->name, obj);
Pierre Tardyc0251482010-05-31 23:12:09 +0200459
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600460 }
Joseph Schuchart0f5f5bc2014-07-10 13:50:51 +0200461
Pierre Tardyc0251482010-05-31 23:12:09 +0200462 if (!handler)
463 PyTuple_SetItem(t, n++, dict);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600464
465 if (_PyTuple_Resize(&t, n) == -1)
466 Py_FatalError("error resizing Python tuple");
467
Pierre Tardyc0251482010-05-31 23:12:09 +0200468 if (handler) {
Adrian Huntera5563ed2014-07-31 09:01:01 +0300469 call_object(handler, t, handler_name);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600470 } else {
Adrian Huntera5563ed2014-07-31 09:01:01 +0300471 try_call_object("trace_unhandled", t);
Pierre Tardyc0251482010-05-31 23:12:09 +0200472 Py_DECREF(dict);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600473 }
474
475 Py_DECREF(t);
476}
477
Arnaldo Carvalho de Melob7fff6b52013-12-19 16:34:52 -0300478static void python_process_general_event(struct perf_sample *sample,
Feng Tang6a6daec2012-08-08 17:57:51 +0800479 struct perf_evsel *evsel,
David Ahern2eaa1b42013-07-18 16:06:15 -0600480 struct thread *thread,
Feng Tang87b6a3a2012-08-09 13:46:13 +0800481 struct addr_location *al)
Feng Tang6a6daec2012-08-08 17:57:51 +0800482{
Adrian Huntera5563ed2014-07-31 09:01:01 +0300483 PyObject *handler, *t, *dict, *callchain, *dict_sample;
Feng Tang6a6daec2012-08-08 17:57:51 +0800484 static char handler_name[64];
485 unsigned n = 0;
Feng Tang6a6daec2012-08-08 17:57:51 +0800486
Feng Tangfd6b8582012-08-08 17:57:53 +0800487 /*
488 * Use the MAX_FIELDS to make the function expandable, though
Feng Tang87b6a3a2012-08-09 13:46:13 +0800489 * currently there is only one item for the tuple.
Feng Tangfd6b8582012-08-08 17:57:53 +0800490 */
Feng Tang6a6daec2012-08-08 17:57:51 +0800491 t = PyTuple_New(MAX_FIELDS);
492 if (!t)
493 Py_FatalError("couldn't create Python tuple");
494
Feng Tangfd6b8582012-08-08 17:57:53 +0800495 dict = PyDict_New();
496 if (!dict)
497 Py_FatalError("couldn't create Python dictionary");
498
Joseph Schuchart57608cf2014-07-10 13:50:56 +0200499 dict_sample = PyDict_New();
500 if (!dict_sample)
501 Py_FatalError("couldn't create Python dictionary");
502
Feng Tang6a6daec2012-08-08 17:57:51 +0800503 snprintf(handler_name, sizeof(handler_name), "%s", "process_event");
504
Adrian Huntera5563ed2014-07-31 09:01:01 +0300505 handler = get_handler(handler_name);
506 if (!handler)
Feng Tang6a6daec2012-08-08 17:57:51 +0800507 goto exit;
Feng Tang6a6daec2012-08-08 17:57:51 +0800508
Joseph Schuchartc0268e82013-10-24 10:10:51 -0300509 pydict_set_item_string_decref(dict, "ev_name", PyString_FromString(perf_evsel__name(evsel)));
510 pydict_set_item_string_decref(dict, "attr", PyString_FromStringAndSize(
Feng Tangfd6b8582012-08-08 17:57:53 +0800511 (const char *)&evsel->attr, sizeof(evsel->attr)));
Joseph Schuchart57608cf2014-07-10 13:50:56 +0200512
513 pydict_set_item_string_decref(dict_sample, "pid",
514 PyInt_FromLong(sample->pid));
515 pydict_set_item_string_decref(dict_sample, "tid",
516 PyInt_FromLong(sample->tid));
517 pydict_set_item_string_decref(dict_sample, "cpu",
518 PyInt_FromLong(sample->cpu));
519 pydict_set_item_string_decref(dict_sample, "ip",
520 PyLong_FromUnsignedLongLong(sample->ip));
521 pydict_set_item_string_decref(dict_sample, "time",
522 PyLong_FromUnsignedLongLong(sample->time));
523 pydict_set_item_string_decref(dict_sample, "period",
524 PyLong_FromUnsignedLongLong(sample->period));
525 pydict_set_item_string_decref(dict, "sample", dict_sample);
526
Joseph Schuchartc0268e82013-10-24 10:10:51 -0300527 pydict_set_item_string_decref(dict, "raw_buf", PyString_FromStringAndSize(
Feng Tangfd6b8582012-08-08 17:57:53 +0800528 (const char *)sample->raw_data, sample->raw_size));
Joseph Schuchartc0268e82013-10-24 10:10:51 -0300529 pydict_set_item_string_decref(dict, "comm",
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +0200530 PyString_FromString(thread__comm_str(thread)));
Feng Tangfd6b8582012-08-08 17:57:53 +0800531 if (al->map) {
Joseph Schuchartc0268e82013-10-24 10:10:51 -0300532 pydict_set_item_string_decref(dict, "dso",
Feng Tangfd6b8582012-08-08 17:57:53 +0800533 PyString_FromString(al->map->dso->name));
534 }
535 if (al->sym) {
Joseph Schuchartc0268e82013-10-24 10:10:51 -0300536 pydict_set_item_string_decref(dict, "symbol",
Feng Tangfd6b8582012-08-08 17:57:53 +0800537 PyString_FromString(al->sym->name));
538 }
Feng Tang6a6daec2012-08-08 17:57:51 +0800539
Joseph Schuchart0f5f5bc2014-07-10 13:50:51 +0200540 /* ip unwinding */
541 callchain = python_process_callchain(sample, evsel, al);
542 pydict_set_item_string_decref(dict, "callchain", callchain);
543
Feng Tangfd6b8582012-08-08 17:57:53 +0800544 PyTuple_SetItem(t, n++, dict);
Feng Tang6a6daec2012-08-08 17:57:51 +0800545 if (_PyTuple_Resize(&t, n) == -1)
546 Py_FatalError("error resizing Python tuple");
547
Adrian Huntera5563ed2014-07-31 09:01:01 +0300548 call_object(handler, t, handler_name);
Feng Tang6a6daec2012-08-08 17:57:51 +0800549exit:
Feng Tangfd6b8582012-08-08 17:57:53 +0800550 Py_DECREF(dict);
Feng Tang6a6daec2012-08-08 17:57:51 +0800551 Py_DECREF(t);
552}
553
Arnaldo Carvalho de Melob7fff6b52013-12-19 16:34:52 -0300554static void python_process_event(union perf_event *event __maybe_unused,
Feng Tang6a6daec2012-08-08 17:57:51 +0800555 struct perf_sample *sample,
556 struct perf_evsel *evsel,
David Ahern2eaa1b42013-07-18 16:06:15 -0600557 struct thread *thread,
Feng Tang73994dc2012-08-08 17:57:52 +0800558 struct addr_location *al)
Feng Tang6a6daec2012-08-08 17:57:51 +0800559{
560 switch (evsel->attr.type) {
561 case PERF_TYPE_TRACEPOINT:
Arnaldo Carvalho de Melob7fff6b52013-12-19 16:34:52 -0300562 python_process_tracepoint(sample, evsel, thread, al);
Feng Tang6a6daec2012-08-08 17:57:51 +0800563 break;
564 /* Reserve for future process_hw/sw/raw APIs */
565 default:
Arnaldo Carvalho de Melob7fff6b52013-12-19 16:34:52 -0300566 python_process_general_event(sample, evsel, thread, al);
Feng Tang6a6daec2012-08-08 17:57:51 +0800567 }
568}
569
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600570static int run_start_sub(void)
571{
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600572 main_module = PyImport_AddModule("__main__");
573 if (main_module == NULL)
574 return -1;
575 Py_INCREF(main_module);
576
577 main_dict = PyModule_GetDict(main_module);
Adrian Huntera5563ed2014-07-31 09:01:01 +0300578 if (main_dict == NULL)
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600579 goto error;
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600580 Py_INCREF(main_dict);
581
Adrian Huntera5563ed2014-07-31 09:01:01 +0300582 try_call_object("trace_begin", NULL);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600583
Adrian Huntera5563ed2014-07-31 09:01:01 +0300584 return 0;
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600585
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600586error:
587 Py_XDECREF(main_dict);
588 Py_XDECREF(main_module);
Adrian Huntera5563ed2014-07-31 09:01:01 +0300589 return -1;
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600590}
591
592/*
593 * Start trace script
594 */
595static int python_start_script(const char *script, int argc, const char **argv)
596{
597 const char **command_line;
598 char buf[PATH_MAX];
599 int i, err = 0;
600 FILE *fp;
601
602 command_line = malloc((argc + 1) * sizeof(const char *));
603 command_line[0] = script;
604 for (i = 1; i < argc + 1; i++)
605 command_line[i] = argv[i - 1];
606
607 Py_Initialize();
608
609 initperf_trace_context();
610
611 PySys_SetArgv(argc + 1, (char **)command_line);
612
613 fp = fopen(script, "r");
614 if (!fp) {
615 sprintf(buf, "Can't open python script \"%s\"", script);
616 perror(buf);
617 err = -1;
618 goto error;
619 }
620
621 err = PyRun_SimpleFile(fp, script);
622 if (err) {
623 fprintf(stderr, "Error running python script %s\n", script);
624 goto error;
625 }
626
627 err = run_start_sub();
628 if (err) {
629 fprintf(stderr, "Error starting python script %s\n", script);
630 goto error;
631 }
632
633 free(command_line);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600634
635 return err;
636error:
637 Py_Finalize();
638 free(command_line);
639
640 return err;
641}
642
Adrian Hunterd445dd22014-08-15 22:08:37 +0300643static int python_flush_script(void)
644{
645 return 0;
646}
647
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600648/*
649 * Stop trace script
650 */
651static int python_stop_script(void)
652{
Adrian Huntera5563ed2014-07-31 09:01:01 +0300653 try_call_object("trace_end", NULL);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600654
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600655 Py_XDECREF(main_dict);
656 Py_XDECREF(main_module);
657 Py_Finalize();
658
Adrian Huntera5563ed2014-07-31 09:01:01 +0300659 return 0;
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600660}
661
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -0300662static int python_generate_script(struct pevent *pevent, const char *outfile)
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600663{
Steven Rostedtaaf045f2012-04-06 00:47:56 +0200664 struct event_format *event = NULL;
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600665 struct format_field *f;
666 char fname[PATH_MAX];
667 int not_first, count;
668 FILE *ofp;
669
670 sprintf(fname, "%s.py", outfile);
671 ofp = fopen(fname, "w");
672 if (ofp == NULL) {
673 fprintf(stderr, "couldn't open %s\n", fname);
674 return -1;
675 }
Ingo Molnar133dc4c2010-11-16 18:45:39 +0100676 fprintf(ofp, "# perf script event handlers, "
677 "generated by perf script -g python\n");
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600678
679 fprintf(ofp, "# Licensed under the terms of the GNU GPL"
680 " License version 2\n\n");
681
682 fprintf(ofp, "# The common_* event handler fields are the most useful "
683 "fields common to\n");
684
685 fprintf(ofp, "# all events. They don't necessarily correspond to "
686 "the 'common_*' fields\n");
687
688 fprintf(ofp, "# in the format files. Those fields not available as "
689 "handler params can\n");
690
691 fprintf(ofp, "# be retrieved using Python functions of the form "
692 "common_*(context).\n");
693
694 fprintf(ofp, "# See the perf-trace-python Documentation for the list "
695 "of available functions.\n\n");
696
697 fprintf(ofp, "import os\n");
698 fprintf(ofp, "import sys\n\n");
699
700 fprintf(ofp, "sys.path.append(os.environ['PERF_EXEC_PATH'] + \\\n");
701 fprintf(ofp, "\t'/scripts/python/Perf-Trace-Util/lib/Perf/Trace')\n");
702 fprintf(ofp, "\nfrom perf_trace_context import *\n");
703 fprintf(ofp, "from Core import *\n\n\n");
704
705 fprintf(ofp, "def trace_begin():\n");
706 fprintf(ofp, "\tprint \"in trace_begin\"\n\n");
707
708 fprintf(ofp, "def trace_end():\n");
709 fprintf(ofp, "\tprint \"in trace_end\"\n\n");
710
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -0300711 while ((event = trace_find_next_event(pevent, event))) {
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600712 fprintf(ofp, "def %s__%s(", event->system, event->name);
713 fprintf(ofp, "event_name, ");
714 fprintf(ofp, "context, ");
715 fprintf(ofp, "common_cpu,\n");
716 fprintf(ofp, "\tcommon_secs, ");
717 fprintf(ofp, "common_nsecs, ");
718 fprintf(ofp, "common_pid, ");
719 fprintf(ofp, "common_comm,\n\t");
Joseph Schuchart0f5f5bc2014-07-10 13:50:51 +0200720 fprintf(ofp, "common_callchain, ");
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600721
722 not_first = 0;
723 count = 0;
724
725 for (f = event->format.fields; f; f = f->next) {
726 if (not_first++)
727 fprintf(ofp, ", ");
728 if (++count % 5 == 0)
729 fprintf(ofp, "\n\t");
730
731 fprintf(ofp, "%s", f->name);
732 }
733 fprintf(ofp, "):\n");
734
735 fprintf(ofp, "\t\tprint_header(event_name, common_cpu, "
736 "common_secs, common_nsecs,\n\t\t\t"
737 "common_pid, common_comm)\n\n");
738
739 fprintf(ofp, "\t\tprint \"");
740
741 not_first = 0;
742 count = 0;
743
744 for (f = event->format.fields; f; f = f->next) {
745 if (not_first++)
746 fprintf(ofp, ", ");
747 if (count && count % 3 == 0) {
748 fprintf(ofp, "\" \\\n\t\t\"");
749 }
750 count++;
751
752 fprintf(ofp, "%s=", f->name);
753 if (f->flags & FIELD_IS_STRING ||
754 f->flags & FIELD_IS_FLAG ||
Namhyung Kime646fe72014-05-29 13:44:55 +0900755 f->flags & FIELD_IS_ARRAY ||
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600756 f->flags & FIELD_IS_SYMBOLIC)
757 fprintf(ofp, "%%s");
758 else if (f->flags & FIELD_IS_SIGNED)
759 fprintf(ofp, "%%d");
760 else
761 fprintf(ofp, "%%u");
762 }
763
Joseph Schuchart0f5f5bc2014-07-10 13:50:51 +0200764 fprintf(ofp, "\" %% \\\n\t\t(");
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600765
766 not_first = 0;
767 count = 0;
768
769 for (f = event->format.fields; f; f = f->next) {
770 if (not_first++)
771 fprintf(ofp, ", ");
772
773 if (++count % 5 == 0)
774 fprintf(ofp, "\n\t\t");
775
776 if (f->flags & FIELD_IS_FLAG) {
777 if ((count - 1) % 5 != 0) {
778 fprintf(ofp, "\n\t\t");
779 count = 4;
780 }
781 fprintf(ofp, "flag_str(\"");
782 fprintf(ofp, "%s__%s\", ", event->system,
783 event->name);
784 fprintf(ofp, "\"%s\", %s)", f->name,
785 f->name);
786 } else if (f->flags & FIELD_IS_SYMBOLIC) {
787 if ((count - 1) % 5 != 0) {
788 fprintf(ofp, "\n\t\t");
789 count = 4;
790 }
791 fprintf(ofp, "symbol_str(\"");
792 fprintf(ofp, "%s__%s\", ", event->system,
793 event->name);
794 fprintf(ofp, "\"%s\", %s)", f->name,
795 f->name);
796 } else
797 fprintf(ofp, "%s", f->name);
798 }
799
Joseph Schuchart0f5f5bc2014-07-10 13:50:51 +0200800 fprintf(ofp, ")\n\n");
801
802 fprintf(ofp, "\t\tfor node in common_callchain:");
803 fprintf(ofp, "\n\t\t\tif 'sym' in node:");
804 fprintf(ofp, "\n\t\t\t\tprint \"\\t[%%x] %%s\" %% (node['ip'], node['sym']['name'])");
805 fprintf(ofp, "\n\t\t\telse:");
806 fprintf(ofp, "\n\t\t\t\tprint \"\t[%%x]\" %% (node['ip'])\n\n");
807 fprintf(ofp, "\t\tprint \"\\n\"\n\n");
808
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600809 }
810
811 fprintf(ofp, "def trace_unhandled(event_name, context, "
Pierre Tardyc0251482010-05-31 23:12:09 +0200812 "event_fields_dict):\n");
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600813
Pierre Tardyc0251482010-05-31 23:12:09 +0200814 fprintf(ofp, "\t\tprint ' '.join(['%%s=%%s'%%(k,str(v))"
815 "for k,v in sorted(event_fields_dict.items())])\n\n");
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600816
817 fprintf(ofp, "def print_header("
818 "event_name, cpu, secs, nsecs, pid, comm):\n"
819 "\tprint \"%%-20s %%5u %%05u.%%09u %%8u %%-20s \" %% \\\n\t"
820 "(event_name, cpu, secs, nsecs, pid, comm),\n");
821
822 fclose(ofp);
823
824 fprintf(stderr, "generated Python script: %s\n", fname);
825
826 return 0;
827}
828
829struct scripting_ops python_scripting_ops = {
830 .name = "Python",
831 .start_script = python_start_script,
Adrian Hunterd445dd22014-08-15 22:08:37 +0300832 .flush_script = python_flush_script,
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600833 .stop_script = python_stop_script,
834 .process_event = python_process_event,
835 .generate_script = python_generate_script,
836};