blob: 53c20e7fd90037ce46dc1c53d20e99264d66c47a [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"
Arnaldo Carvalho de Melofcf65bf2012-08-07 09:58:03 -030030#include "../evsel.h"
Tom Zanussi7e4b21b2010-01-27 02:27:57 -060031#include "../util.h"
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -020032#include "../event.h"
33#include "../thread.h"
Tom Zanussi7e4b21b2010-01-27 02:27:57 -060034#include "../trace-event.h"
35
36PyMODINIT_FUNC initperf_trace_context(void);
37
38#define FTRACE_MAX_EVENT \
39 ((1 << (sizeof(unsigned short) * 8)) - 1)
40
Steven Rostedtaaf045f2012-04-06 00:47:56 +020041struct event_format *events[FTRACE_MAX_EVENT];
Tom Zanussi7e4b21b2010-01-27 02:27:57 -060042
43#define MAX_FIELDS 64
44#define N_COMMON_FIELDS 7
45
46extern struct scripting_context *scripting_context;
47
48static char *cur_field_name;
49static int zero_flag_atom;
50
51static PyObject *main_module, *main_dict;
52
53static void handler_call_die(const char *handler_name)
54{
55 PyErr_Print();
56 Py_FatalError("problem in Python trace event handler");
57}
58
Joseph Schuchartc0268e82013-10-24 10:10:51 -030059/*
60 * Insert val into into the dictionary and decrement the reference counter.
61 * This is necessary for dictionaries since PyDict_SetItemString() does not
62 * steal a reference, as opposed to PyTuple_SetItem().
63 */
64static void pydict_set_item_string_decref(PyObject *dict, const char *key, PyObject *val)
65{
66 PyDict_SetItemString(dict, key, val);
67 Py_DECREF(val);
68}
69
Tom Zanussi7e4b21b2010-01-27 02:27:57 -060070static void define_value(enum print_arg_type field_type,
71 const char *ev_name,
72 const char *field_name,
73 const char *field_value,
74 const char *field_str)
75{
76 const char *handler_name = "define_flag_value";
77 PyObject *handler, *t, *retval;
78 unsigned long long value;
79 unsigned n = 0;
80
81 if (field_type == PRINT_SYMBOL)
82 handler_name = "define_symbolic_value";
83
Tom Zanussi44ad9cd2010-02-22 01:12:59 -060084 t = PyTuple_New(4);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -060085 if (!t)
86 Py_FatalError("couldn't create Python tuple");
87
88 value = eval_flag(field_value);
89
90 PyTuple_SetItem(t, n++, PyString_FromString(ev_name));
91 PyTuple_SetItem(t, n++, PyString_FromString(field_name));
92 PyTuple_SetItem(t, n++, PyInt_FromLong(value));
93 PyTuple_SetItem(t, n++, PyString_FromString(field_str));
94
Tom Zanussi7e4b21b2010-01-27 02:27:57 -060095 handler = PyDict_GetItemString(main_dict, handler_name);
96 if (handler && PyCallable_Check(handler)) {
97 retval = PyObject_CallObject(handler, t);
98 if (retval == NULL)
99 handler_call_die(handler_name);
100 }
101
102 Py_DECREF(t);
103}
104
105static void define_values(enum print_arg_type field_type,
106 struct print_flag_sym *field,
107 const char *ev_name,
108 const char *field_name)
109{
110 define_value(field_type, ev_name, field_name, field->value,
111 field->str);
112
113 if (field->next)
114 define_values(field_type, field->next, ev_name, field_name);
115}
116
117static void define_field(enum print_arg_type field_type,
118 const char *ev_name,
119 const char *field_name,
120 const char *delim)
121{
122 const char *handler_name = "define_flag_field";
123 PyObject *handler, *t, *retval;
124 unsigned n = 0;
125
126 if (field_type == PRINT_SYMBOL)
127 handler_name = "define_symbolic_field";
128
Tom Zanussi44ad9cd2010-02-22 01:12:59 -0600129 if (field_type == PRINT_FLAGS)
130 t = PyTuple_New(3);
131 else
132 t = PyTuple_New(2);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600133 if (!t)
134 Py_FatalError("couldn't create Python tuple");
135
136 PyTuple_SetItem(t, n++, PyString_FromString(ev_name));
137 PyTuple_SetItem(t, n++, PyString_FromString(field_name));
138 if (field_type == PRINT_FLAGS)
139 PyTuple_SetItem(t, n++, PyString_FromString(delim));
140
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600141 handler = PyDict_GetItemString(main_dict, handler_name);
142 if (handler && PyCallable_Check(handler)) {
143 retval = PyObject_CallObject(handler, t);
144 if (retval == NULL)
145 handler_call_die(handler_name);
146 }
147
148 Py_DECREF(t);
149}
150
Steven Rostedtaaf045f2012-04-06 00:47:56 +0200151static void define_event_symbols(struct event_format *event,
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600152 const char *ev_name,
153 struct print_arg *args)
154{
155 switch (args->type) {
156 case PRINT_NULL:
157 break;
158 case PRINT_ATOM:
159 define_value(PRINT_FLAGS, ev_name, cur_field_name, "0",
160 args->atom.atom);
161 zero_flag_atom = 0;
162 break;
163 case PRINT_FIELD:
164 if (cur_field_name)
165 free(cur_field_name);
166 cur_field_name = strdup(args->field.name);
167 break;
168 case PRINT_FLAGS:
169 define_event_symbols(event, ev_name, args->flags.field);
170 define_field(PRINT_FLAGS, ev_name, cur_field_name,
171 args->flags.delim);
172 define_values(PRINT_FLAGS, args->flags.flags, ev_name,
173 cur_field_name);
174 break;
175 case PRINT_SYMBOL:
176 define_event_symbols(event, ev_name, args->symbol.field);
177 define_field(PRINT_SYMBOL, ev_name, cur_field_name, NULL);
178 define_values(PRINT_SYMBOL, args->symbol.symbols, ev_name,
179 cur_field_name);
180 break;
Namhyung Kime080e6f2012-06-27 09:41:41 +0900181 case PRINT_HEX:
182 define_event_symbols(event, ev_name, args->hex.field);
183 define_event_symbols(event, ev_name, args->hex.size);
184 break;
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600185 case PRINT_STRING:
186 break;
187 case PRINT_TYPE:
188 define_event_symbols(event, ev_name, args->typecast.item);
189 break;
190 case PRINT_OP:
191 if (strcmp(args->op.op, ":") == 0)
192 zero_flag_atom = 1;
193 define_event_symbols(event, ev_name, args->op.left);
194 define_event_symbols(event, ev_name, args->op.right);
195 break;
196 default:
Steven Rostedtaaf045f2012-04-06 00:47:56 +0200197 /* gcc warns for these? */
198 case PRINT_BSTRING:
199 case PRINT_DYNAMIC_ARRAY:
200 case PRINT_FUNC:
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600201 /* we should warn... */
202 return;
203 }
204
205 if (args->next)
206 define_event_symbols(event, ev_name, args->next);
207}
208
Arnaldo Carvalho de Melofcf65bf2012-08-07 09:58:03 -0300209static inline struct event_format *find_cache_event(struct perf_evsel *evsel)
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600210{
211 static char ev_name[256];
Steven Rostedtaaf045f2012-04-06 00:47:56 +0200212 struct event_format *event;
Arnaldo Carvalho de Melofcf65bf2012-08-07 09:58:03 -0300213 int type = evsel->attr.config;
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600214
Arnaldo Carvalho de Melofcf65bf2012-08-07 09:58:03 -0300215 /*
216 * XXX: Do we really need to cache this since now we have evsel->tp_format
217 * cached already? Need to re-read this "cache" routine that as well calls
218 * define_event_symbols() :-\
219 */
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600220 if (events[type])
221 return events[type];
222
Arnaldo Carvalho de Melofcf65bf2012-08-07 09:58:03 -0300223 events[type] = event = evsel->tp_format;
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600224 if (!event)
225 return NULL;
226
227 sprintf(ev_name, "%s__%s", event->system, event->name);
228
229 define_event_symbols(event, ev_name, event->print_fmt.args);
230
231 return event;
232}
233
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300234static void python_process_tracepoint(union perf_event *perf_event
235 __maybe_unused,
David Ahernbe6d8422011-03-09 22:23:23 -0700236 struct perf_sample *sample,
Arnaldo Carvalho de Melofcf65bf2012-08-07 09:58:03 -0300237 struct perf_evsel *evsel,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300238 struct machine *machine __maybe_unused,
David Ahern2eaa1b42013-07-18 16:06:15 -0600239 struct thread *thread,
Feng Tang73994dc2012-08-08 17:57:52 +0800240 struct addr_location *al)
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600241{
Pierre Tardyc0251482010-05-31 23:12:09 +0200242 PyObject *handler, *retval, *context, *t, *obj, *dict = NULL;
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600243 static char handler_name[256];
244 struct format_field *field;
245 unsigned long long val;
246 unsigned long s, ns;
Steven Rostedtaaf045f2012-04-06 00:47:56 +0200247 struct event_format *event;
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600248 unsigned n = 0;
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600249 int pid;
David Ahernbe6d8422011-03-09 22:23:23 -0700250 int cpu = sample->cpu;
251 void *data = sample->raw_data;
252 unsigned long long nsecs = sample->time;
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +0200253 const char *comm = thread__comm_str(thread);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600254
255 t = PyTuple_New(MAX_FIELDS);
256 if (!t)
257 Py_FatalError("couldn't create Python tuple");
258
Arnaldo Carvalho de Melofcf65bf2012-08-07 09:58:03 -0300259 event = find_cache_event(evsel);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600260 if (!event)
Arnaldo Carvalho de Melofcf65bf2012-08-07 09:58:03 -0300261 die("ug! no event found for type %d", (int)evsel->attr.config);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600262
Arnaldo Carvalho de Melo97822432012-08-07 23:50:21 -0300263 pid = raw_field_value(event, "common_pid", data);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600264
265 sprintf(handler_name, "%s__%s", event->system, event->name);
266
Pierre Tardyc0251482010-05-31 23:12:09 +0200267 handler = PyDict_GetItemString(main_dict, handler_name);
268 if (handler && !PyCallable_Check(handler))
269 handler = NULL;
270 if (!handler) {
271 dict = PyDict_New();
272 if (!dict)
273 Py_FatalError("couldn't create Python dict");
274 }
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600275 s = nsecs / NSECS_PER_SEC;
276 ns = nsecs - s * NSECS_PER_SEC;
277
278 scripting_context->event_data = data;
Tom Zanussi2de95332013-01-18 13:51:27 -0600279 scripting_context->pevent = evsel->tp_format->pevent;
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600280
281 context = PyCObject_FromVoidPtr(scripting_context, NULL);
282
283 PyTuple_SetItem(t, n++, PyString_FromString(handler_name));
Kyle McMartinfb7d0b32011-01-24 11:13:04 -0500284 PyTuple_SetItem(t, n++, context);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600285
Pierre Tardyc0251482010-05-31 23:12:09 +0200286 if (handler) {
287 PyTuple_SetItem(t, n++, PyInt_FromLong(cpu));
288 PyTuple_SetItem(t, n++, PyInt_FromLong(s));
289 PyTuple_SetItem(t, n++, PyInt_FromLong(ns));
290 PyTuple_SetItem(t, n++, PyInt_FromLong(pid));
291 PyTuple_SetItem(t, n++, PyString_FromString(comm));
292 } else {
Joseph Schuchartc0268e82013-10-24 10:10:51 -0300293 pydict_set_item_string_decref(dict, "common_cpu", PyInt_FromLong(cpu));
294 pydict_set_item_string_decref(dict, "common_s", PyInt_FromLong(s));
295 pydict_set_item_string_decref(dict, "common_ns", PyInt_FromLong(ns));
296 pydict_set_item_string_decref(dict, "common_pid", PyInt_FromLong(pid));
297 pydict_set_item_string_decref(dict, "common_comm", PyString_FromString(comm));
Pierre Tardyc0251482010-05-31 23:12:09 +0200298 }
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600299 for (field = event->format.fields; field; field = field->next) {
300 if (field->flags & FIELD_IS_STRING) {
301 int offset;
302 if (field->flags & FIELD_IS_DYNAMIC) {
303 offset = *(int *)(data + field->offset);
304 offset &= 0xffff;
305 } else
306 offset = field->offset;
Tom Zanussib1dcc032010-04-01 23:58:25 -0500307 obj = PyString_FromString((char *)data + offset);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600308 } else { /* FIELD_IS_NUMERIC */
Arnaldo Carvalho de Melo97822432012-08-07 23:50:21 -0300309 val = read_size(event, data + field->offset,
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -0300310 field->size);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600311 if (field->flags & FIELD_IS_SIGNED) {
Tom Zanussib1dcc032010-04-01 23:58:25 -0500312 if ((long long)val >= LONG_MIN &&
313 (long long)val <= LONG_MAX)
314 obj = PyInt_FromLong(val);
315 else
316 obj = PyLong_FromLongLong(val);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600317 } else {
Tom Zanussib1dcc032010-04-01 23:58:25 -0500318 if (val <= LONG_MAX)
319 obj = PyInt_FromLong(val);
320 else
321 obj = PyLong_FromUnsignedLongLong(val);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600322 }
323 }
Pierre Tardyc0251482010-05-31 23:12:09 +0200324 if (handler)
325 PyTuple_SetItem(t, n++, obj);
326 else
Joseph Schuchartc0268e82013-10-24 10:10:51 -0300327 pydict_set_item_string_decref(dict, field->name, obj);
Pierre Tardyc0251482010-05-31 23:12:09 +0200328
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600329 }
Pierre Tardyc0251482010-05-31 23:12:09 +0200330 if (!handler)
331 PyTuple_SetItem(t, n++, dict);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600332
333 if (_PyTuple_Resize(&t, n) == -1)
334 Py_FatalError("error resizing Python tuple");
335
Pierre Tardyc0251482010-05-31 23:12:09 +0200336 if (handler) {
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600337 retval = PyObject_CallObject(handler, t);
338 if (retval == NULL)
339 handler_call_die(handler_name);
340 } else {
341 handler = PyDict_GetItemString(main_dict, "trace_unhandled");
342 if (handler && PyCallable_Check(handler)) {
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600343
344 retval = PyObject_CallObject(handler, t);
345 if (retval == NULL)
346 handler_call_die("trace_unhandled");
347 }
Pierre Tardyc0251482010-05-31 23:12:09 +0200348 Py_DECREF(dict);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600349 }
350
351 Py_DECREF(t);
352}
353
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300354static void python_process_general_event(union perf_event *perf_event
355 __maybe_unused,
Feng Tang6a6daec2012-08-08 17:57:51 +0800356 struct perf_sample *sample,
357 struct perf_evsel *evsel,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300358 struct machine *machine __maybe_unused,
David Ahern2eaa1b42013-07-18 16:06:15 -0600359 struct thread *thread,
Feng Tang87b6a3a2012-08-09 13:46:13 +0800360 struct addr_location *al)
Feng Tang6a6daec2012-08-08 17:57:51 +0800361{
Feng Tangfd6b8582012-08-08 17:57:53 +0800362 PyObject *handler, *retval, *t, *dict;
Feng Tang6a6daec2012-08-08 17:57:51 +0800363 static char handler_name[64];
364 unsigned n = 0;
Feng Tang6a6daec2012-08-08 17:57:51 +0800365
Feng Tangfd6b8582012-08-08 17:57:53 +0800366 /*
367 * Use the MAX_FIELDS to make the function expandable, though
Feng Tang87b6a3a2012-08-09 13:46:13 +0800368 * currently there is only one item for the tuple.
Feng Tangfd6b8582012-08-08 17:57:53 +0800369 */
Feng Tang6a6daec2012-08-08 17:57:51 +0800370 t = PyTuple_New(MAX_FIELDS);
371 if (!t)
372 Py_FatalError("couldn't create Python tuple");
373
Feng Tangfd6b8582012-08-08 17:57:53 +0800374 dict = PyDict_New();
375 if (!dict)
376 Py_FatalError("couldn't create Python dictionary");
377
Feng Tang6a6daec2012-08-08 17:57:51 +0800378 snprintf(handler_name, sizeof(handler_name), "%s", "process_event");
379
380 handler = PyDict_GetItemString(main_dict, handler_name);
Feng Tang87b6a3a2012-08-09 13:46:13 +0800381 if (!handler || !PyCallable_Check(handler))
Feng Tang6a6daec2012-08-08 17:57:51 +0800382 goto exit;
Feng Tang6a6daec2012-08-08 17:57:51 +0800383
Joseph Schuchartc0268e82013-10-24 10:10:51 -0300384 pydict_set_item_string_decref(dict, "ev_name", PyString_FromString(perf_evsel__name(evsel)));
385 pydict_set_item_string_decref(dict, "attr", PyString_FromStringAndSize(
Feng Tangfd6b8582012-08-08 17:57:53 +0800386 (const char *)&evsel->attr, sizeof(evsel->attr)));
Joseph Schuchartc0268e82013-10-24 10:10:51 -0300387 pydict_set_item_string_decref(dict, "sample", PyString_FromStringAndSize(
Feng Tangfd6b8582012-08-08 17:57:53 +0800388 (const char *)sample, sizeof(*sample)));
Joseph Schuchartc0268e82013-10-24 10:10:51 -0300389 pydict_set_item_string_decref(dict, "raw_buf", PyString_FromStringAndSize(
Feng Tangfd6b8582012-08-08 17:57:53 +0800390 (const char *)sample->raw_data, sample->raw_size));
Joseph Schuchartc0268e82013-10-24 10:10:51 -0300391 pydict_set_item_string_decref(dict, "comm",
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +0200392 PyString_FromString(thread__comm_str(thread)));
Feng Tangfd6b8582012-08-08 17:57:53 +0800393 if (al->map) {
Joseph Schuchartc0268e82013-10-24 10:10:51 -0300394 pydict_set_item_string_decref(dict, "dso",
Feng Tangfd6b8582012-08-08 17:57:53 +0800395 PyString_FromString(al->map->dso->name));
396 }
397 if (al->sym) {
Joseph Schuchartc0268e82013-10-24 10:10:51 -0300398 pydict_set_item_string_decref(dict, "symbol",
Feng Tangfd6b8582012-08-08 17:57:53 +0800399 PyString_FromString(al->sym->name));
400 }
Feng Tang6a6daec2012-08-08 17:57:51 +0800401
Feng Tangfd6b8582012-08-08 17:57:53 +0800402 PyTuple_SetItem(t, n++, dict);
Feng Tang6a6daec2012-08-08 17:57:51 +0800403 if (_PyTuple_Resize(&t, n) == -1)
404 Py_FatalError("error resizing Python tuple");
405
406 retval = PyObject_CallObject(handler, t);
407 if (retval == NULL)
408 handler_call_die(handler_name);
409exit:
Feng Tangfd6b8582012-08-08 17:57:53 +0800410 Py_DECREF(dict);
Feng Tang6a6daec2012-08-08 17:57:51 +0800411 Py_DECREF(t);
412}
413
414static void python_process_event(union perf_event *perf_event,
415 struct perf_sample *sample,
416 struct perf_evsel *evsel,
417 struct machine *machine,
David Ahern2eaa1b42013-07-18 16:06:15 -0600418 struct thread *thread,
Feng Tang73994dc2012-08-08 17:57:52 +0800419 struct addr_location *al)
Feng Tang6a6daec2012-08-08 17:57:51 +0800420{
421 switch (evsel->attr.type) {
422 case PERF_TYPE_TRACEPOINT:
423 python_process_tracepoint(perf_event, sample, evsel,
David Ahern2eaa1b42013-07-18 16:06:15 -0600424 machine, thread, al);
Feng Tang6a6daec2012-08-08 17:57:51 +0800425 break;
426 /* Reserve for future process_hw/sw/raw APIs */
427 default:
428 python_process_general_event(perf_event, sample, evsel,
David Ahern2eaa1b42013-07-18 16:06:15 -0600429 machine, thread, al);
Feng Tang6a6daec2012-08-08 17:57:51 +0800430 }
431}
432
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600433static int run_start_sub(void)
434{
435 PyObject *handler, *retval;
436 int err = 0;
437
438 main_module = PyImport_AddModule("__main__");
439 if (main_module == NULL)
440 return -1;
441 Py_INCREF(main_module);
442
443 main_dict = PyModule_GetDict(main_module);
444 if (main_dict == NULL) {
445 err = -1;
446 goto error;
447 }
448 Py_INCREF(main_dict);
449
450 handler = PyDict_GetItemString(main_dict, "trace_begin");
451 if (handler == NULL || !PyCallable_Check(handler))
452 goto out;
453
454 retval = PyObject_CallObject(handler, NULL);
455 if (retval == NULL)
456 handler_call_die("trace_begin");
457
458 Py_DECREF(retval);
459 return err;
460error:
461 Py_XDECREF(main_dict);
462 Py_XDECREF(main_module);
463out:
464 return err;
465}
466
467/*
468 * Start trace script
469 */
470static int python_start_script(const char *script, int argc, const char **argv)
471{
472 const char **command_line;
473 char buf[PATH_MAX];
474 int i, err = 0;
475 FILE *fp;
476
477 command_line = malloc((argc + 1) * sizeof(const char *));
478 command_line[0] = script;
479 for (i = 1; i < argc + 1; i++)
480 command_line[i] = argv[i - 1];
481
482 Py_Initialize();
483
484 initperf_trace_context();
485
486 PySys_SetArgv(argc + 1, (char **)command_line);
487
488 fp = fopen(script, "r");
489 if (!fp) {
490 sprintf(buf, "Can't open python script \"%s\"", script);
491 perror(buf);
492 err = -1;
493 goto error;
494 }
495
496 err = PyRun_SimpleFile(fp, script);
497 if (err) {
498 fprintf(stderr, "Error running python script %s\n", script);
499 goto error;
500 }
501
502 err = run_start_sub();
503 if (err) {
504 fprintf(stderr, "Error starting python script %s\n", script);
505 goto error;
506 }
507
508 free(command_line);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600509
510 return err;
511error:
512 Py_Finalize();
513 free(command_line);
514
515 return err;
516}
517
518/*
519 * Stop trace script
520 */
521static int python_stop_script(void)
522{
523 PyObject *handler, *retval;
524 int err = 0;
525
526 handler = PyDict_GetItemString(main_dict, "trace_end");
527 if (handler == NULL || !PyCallable_Check(handler))
528 goto out;
529
530 retval = PyObject_CallObject(handler, NULL);
531 if (retval == NULL)
532 handler_call_die("trace_end");
533 else
534 Py_DECREF(retval);
535out:
536 Py_XDECREF(main_dict);
537 Py_XDECREF(main_module);
538 Py_Finalize();
539
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600540 return err;
541}
542
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -0300543static int python_generate_script(struct pevent *pevent, const char *outfile)
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600544{
Steven Rostedtaaf045f2012-04-06 00:47:56 +0200545 struct event_format *event = NULL;
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600546 struct format_field *f;
547 char fname[PATH_MAX];
548 int not_first, count;
549 FILE *ofp;
550
551 sprintf(fname, "%s.py", outfile);
552 ofp = fopen(fname, "w");
553 if (ofp == NULL) {
554 fprintf(stderr, "couldn't open %s\n", fname);
555 return -1;
556 }
Ingo Molnar133dc4c2010-11-16 18:45:39 +0100557 fprintf(ofp, "# perf script event handlers, "
558 "generated by perf script -g python\n");
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600559
560 fprintf(ofp, "# Licensed under the terms of the GNU GPL"
561 " License version 2\n\n");
562
563 fprintf(ofp, "# The common_* event handler fields are the most useful "
564 "fields common to\n");
565
566 fprintf(ofp, "# all events. They don't necessarily correspond to "
567 "the 'common_*' fields\n");
568
569 fprintf(ofp, "# in the format files. Those fields not available as "
570 "handler params can\n");
571
572 fprintf(ofp, "# be retrieved using Python functions of the form "
573 "common_*(context).\n");
574
575 fprintf(ofp, "# See the perf-trace-python Documentation for the list "
576 "of available functions.\n\n");
577
578 fprintf(ofp, "import os\n");
579 fprintf(ofp, "import sys\n\n");
580
581 fprintf(ofp, "sys.path.append(os.environ['PERF_EXEC_PATH'] + \\\n");
582 fprintf(ofp, "\t'/scripts/python/Perf-Trace-Util/lib/Perf/Trace')\n");
583 fprintf(ofp, "\nfrom perf_trace_context import *\n");
584 fprintf(ofp, "from Core import *\n\n\n");
585
586 fprintf(ofp, "def trace_begin():\n");
587 fprintf(ofp, "\tprint \"in trace_begin\"\n\n");
588
589 fprintf(ofp, "def trace_end():\n");
590 fprintf(ofp, "\tprint \"in trace_end\"\n\n");
591
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -0300592 while ((event = trace_find_next_event(pevent, event))) {
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600593 fprintf(ofp, "def %s__%s(", event->system, event->name);
594 fprintf(ofp, "event_name, ");
595 fprintf(ofp, "context, ");
596 fprintf(ofp, "common_cpu,\n");
597 fprintf(ofp, "\tcommon_secs, ");
598 fprintf(ofp, "common_nsecs, ");
599 fprintf(ofp, "common_pid, ");
600 fprintf(ofp, "common_comm,\n\t");
601
602 not_first = 0;
603 count = 0;
604
605 for (f = event->format.fields; f; f = f->next) {
606 if (not_first++)
607 fprintf(ofp, ", ");
608 if (++count % 5 == 0)
609 fprintf(ofp, "\n\t");
610
611 fprintf(ofp, "%s", f->name);
612 }
613 fprintf(ofp, "):\n");
614
615 fprintf(ofp, "\t\tprint_header(event_name, common_cpu, "
616 "common_secs, common_nsecs,\n\t\t\t"
617 "common_pid, common_comm)\n\n");
618
619 fprintf(ofp, "\t\tprint \"");
620
621 not_first = 0;
622 count = 0;
623
624 for (f = event->format.fields; f; f = f->next) {
625 if (not_first++)
626 fprintf(ofp, ", ");
627 if (count && count % 3 == 0) {
628 fprintf(ofp, "\" \\\n\t\t\"");
629 }
630 count++;
631
632 fprintf(ofp, "%s=", f->name);
633 if (f->flags & FIELD_IS_STRING ||
634 f->flags & FIELD_IS_FLAG ||
635 f->flags & FIELD_IS_SYMBOLIC)
636 fprintf(ofp, "%%s");
637 else if (f->flags & FIELD_IS_SIGNED)
638 fprintf(ofp, "%%d");
639 else
640 fprintf(ofp, "%%u");
641 }
642
643 fprintf(ofp, "\\n\" %% \\\n\t\t(");
644
645 not_first = 0;
646 count = 0;
647
648 for (f = event->format.fields; f; f = f->next) {
649 if (not_first++)
650 fprintf(ofp, ", ");
651
652 if (++count % 5 == 0)
653 fprintf(ofp, "\n\t\t");
654
655 if (f->flags & FIELD_IS_FLAG) {
656 if ((count - 1) % 5 != 0) {
657 fprintf(ofp, "\n\t\t");
658 count = 4;
659 }
660 fprintf(ofp, "flag_str(\"");
661 fprintf(ofp, "%s__%s\", ", event->system,
662 event->name);
663 fprintf(ofp, "\"%s\", %s)", f->name,
664 f->name);
665 } else if (f->flags & FIELD_IS_SYMBOLIC) {
666 if ((count - 1) % 5 != 0) {
667 fprintf(ofp, "\n\t\t");
668 count = 4;
669 }
670 fprintf(ofp, "symbol_str(\"");
671 fprintf(ofp, "%s__%s\", ", event->system,
672 event->name);
673 fprintf(ofp, "\"%s\", %s)", f->name,
674 f->name);
675 } else
676 fprintf(ofp, "%s", f->name);
677 }
678
679 fprintf(ofp, "),\n\n");
680 }
681
682 fprintf(ofp, "def trace_unhandled(event_name, context, "
Pierre Tardyc0251482010-05-31 23:12:09 +0200683 "event_fields_dict):\n");
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600684
Pierre Tardyc0251482010-05-31 23:12:09 +0200685 fprintf(ofp, "\t\tprint ' '.join(['%%s=%%s'%%(k,str(v))"
686 "for k,v in sorted(event_fields_dict.items())])\n\n");
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600687
688 fprintf(ofp, "def print_header("
689 "event_name, cpu, secs, nsecs, pid, comm):\n"
690 "\tprint \"%%-20s %%5u %%05u.%%09u %%8u %%-20s \" %% \\\n\t"
691 "(event_name, cpu, secs, nsecs, pid, comm),\n");
692
693 fclose(ofp);
694
695 fprintf(stderr, "generated Python script: %s\n", fname);
696
697 return 0;
698}
699
700struct scripting_ops python_scripting_ops = {
701 .name = "Python",
702 .start_script = python_start_script,
703 .stop_script = python_stop_script,
704 .process_event = python_process_event,
705 .generate_script = python_generate_script,
706};