blob: 581e0efd6356839567a9ae5303a3fa6581b8ee3b [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>
Arnaldo Carvalho de Melobd48c632016-08-05 15:40:30 -030030#include <linux/time64.h>
Tom Zanussi7e4b21b2010-01-27 02:27:57 -060031
32#include "../../perf.h"
Jiri Olsa84f5d362014-07-14 23:46:48 +020033#include "../debug.h"
Arnaldo Carvalho de Melo8f651ea2014-10-09 16:12:24 -030034#include "../callchain.h"
Arnaldo Carvalho de Melofcf65bf2012-08-07 09:58:03 -030035#include "../evsel.h"
Tom Zanussi7e4b21b2010-01-27 02:27:57 -060036#include "../util.h"
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -020037#include "../event.h"
38#include "../thread.h"
Adrian Hunterdf919b42014-10-23 13:45:14 +030039#include "../comm.h"
40#include "../machine.h"
41#include "../db-export.h"
Adrian Hunter6a703072014-10-30 16:09:47 +020042#include "../thread-stack.h"
Tom Zanussi7e4b21b2010-01-27 02:27:57 -060043#include "../trace-event.h"
Joseph Schuchart0f5f5bc2014-07-10 13:50:51 +020044#include "../machine.h"
Chris Phlipot451db122016-04-28 01:19:07 -070045#include "../call-path.h"
Jiri Olsaaef90262016-01-05 22:09:11 +010046#include "thread_map.h"
47#include "cpumap.h"
48#include "stat.h"
Tom Zanussi7e4b21b2010-01-27 02:27:57 -060049
50PyMODINIT_FUNC initperf_trace_context(void);
51
Steven Rostedt (Red Hat)609a7402015-05-13 13:44:36 -040052#define TRACE_EVENT_TYPE_MAX \
Tom Zanussi7e4b21b2010-01-27 02:27:57 -060053 ((1 << (sizeof(unsigned short) * 8)) - 1)
54
Steven Rostedt (Red Hat)609a7402015-05-13 13:44:36 -040055static DECLARE_BITMAP(events_defined, TRACE_EVENT_TYPE_MAX);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -060056
57#define MAX_FIELDS 64
58#define N_COMMON_FIELDS 7
59
60extern struct scripting_context *scripting_context;
61
62static char *cur_field_name;
63static int zero_flag_atom;
64
65static PyObject *main_module, *main_dict;
66
Adrian Hunterdf919b42014-10-23 13:45:14 +030067struct tables {
68 struct db_export dbe;
69 PyObject *evsel_handler;
70 PyObject *machine_handler;
71 PyObject *thread_handler;
72 PyObject *comm_handler;
73 PyObject *comm_thread_handler;
74 PyObject *dso_handler;
75 PyObject *symbol_handler;
Adrian Hunterc29414f2014-10-30 16:09:44 +020076 PyObject *branch_type_handler;
Adrian Hunterdf919b42014-10-23 13:45:14 +030077 PyObject *sample_handler;
Adrian Hunter6a703072014-10-30 16:09:47 +020078 PyObject *call_path_handler;
79 PyObject *call_return_handler;
Adrian Hunterdf919b42014-10-23 13:45:14 +030080 bool db_export_mode;
81};
82
83static struct tables tables_global;
84
Joseph Schuchart05f832e2014-07-09 16:16:31 +020085static void handler_call_die(const char *handler_name) NORETURN;
Tom Zanussi7e4b21b2010-01-27 02:27:57 -060086static void handler_call_die(const char *handler_name)
87{
88 PyErr_Print();
89 Py_FatalError("problem in Python trace event handler");
Joseph Schuchart05f832e2014-07-09 16:16:31 +020090 // Py_FatalError does not return
91 // but we have to make the compiler happy
92 abort();
Tom Zanussi7e4b21b2010-01-27 02:27:57 -060093}
94
Joseph Schuchartc0268e82013-10-24 10:10:51 -030095/*
96 * Insert val into into the dictionary and decrement the reference counter.
Arnaldo Carvalho de Melo48000a12014-12-17 17:24:45 -030097 * This is necessary for dictionaries since PyDict_SetItemString() does not
Joseph Schuchartc0268e82013-10-24 10:10:51 -030098 * steal a reference, as opposed to PyTuple_SetItem().
99 */
100static void pydict_set_item_string_decref(PyObject *dict, const char *key, PyObject *val)
101{
102 PyDict_SetItemString(dict, key, val);
103 Py_DECREF(val);
104}
105
Adrian Huntera5563ed2014-07-31 09:01:01 +0300106static PyObject *get_handler(const char *handler_name)
107{
108 PyObject *handler;
109
110 handler = PyDict_GetItemString(main_dict, handler_name);
111 if (handler && !PyCallable_Check(handler))
112 return NULL;
113 return handler;
114}
115
116static void call_object(PyObject *handler, PyObject *args, const char *die_msg)
117{
118 PyObject *retval;
119
120 retval = PyObject_CallObject(handler, args);
121 if (retval == NULL)
122 handler_call_die(die_msg);
123 Py_DECREF(retval);
124}
125
126static void try_call_object(const char *handler_name, PyObject *args)
127{
128 PyObject *handler;
129
130 handler = get_handler(handler_name);
131 if (handler)
132 call_object(handler, args, handler_name);
133}
134
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600135static void define_value(enum print_arg_type field_type,
136 const char *ev_name,
137 const char *field_name,
138 const char *field_value,
139 const char *field_str)
140{
141 const char *handler_name = "define_flag_value";
Adrian Huntera5563ed2014-07-31 09:01:01 +0300142 PyObject *t;
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600143 unsigned long long value;
144 unsigned n = 0;
145
146 if (field_type == PRINT_SYMBOL)
147 handler_name = "define_symbolic_value";
148
Tom Zanussi44ad9cd2010-02-22 01:12:59 -0600149 t = PyTuple_New(4);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600150 if (!t)
151 Py_FatalError("couldn't create Python tuple");
152
153 value = eval_flag(field_value);
154
155 PyTuple_SetItem(t, n++, PyString_FromString(ev_name));
156 PyTuple_SetItem(t, n++, PyString_FromString(field_name));
157 PyTuple_SetItem(t, n++, PyInt_FromLong(value));
158 PyTuple_SetItem(t, n++, PyString_FromString(field_str));
159
Adrian Huntera5563ed2014-07-31 09:01:01 +0300160 try_call_object(handler_name, t);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600161
162 Py_DECREF(t);
163}
164
165static void define_values(enum print_arg_type field_type,
166 struct print_flag_sym *field,
167 const char *ev_name,
168 const char *field_name)
169{
170 define_value(field_type, ev_name, field_name, field->value,
171 field->str);
172
173 if (field->next)
174 define_values(field_type, field->next, ev_name, field_name);
175}
176
177static void define_field(enum print_arg_type field_type,
178 const char *ev_name,
179 const char *field_name,
180 const char *delim)
181{
182 const char *handler_name = "define_flag_field";
Adrian Huntera5563ed2014-07-31 09:01:01 +0300183 PyObject *t;
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600184 unsigned n = 0;
185
186 if (field_type == PRINT_SYMBOL)
187 handler_name = "define_symbolic_field";
188
Tom Zanussi44ad9cd2010-02-22 01:12:59 -0600189 if (field_type == PRINT_FLAGS)
190 t = PyTuple_New(3);
191 else
192 t = PyTuple_New(2);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600193 if (!t)
194 Py_FatalError("couldn't create Python tuple");
195
196 PyTuple_SetItem(t, n++, PyString_FromString(ev_name));
197 PyTuple_SetItem(t, n++, PyString_FromString(field_name));
198 if (field_type == PRINT_FLAGS)
199 PyTuple_SetItem(t, n++, PyString_FromString(delim));
200
Adrian Huntera5563ed2014-07-31 09:01:01 +0300201 try_call_object(handler_name, t);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600202
203 Py_DECREF(t);
204}
205
Steven Rostedtaaf045f2012-04-06 00:47:56 +0200206static void define_event_symbols(struct event_format *event,
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600207 const char *ev_name,
208 struct print_arg *args)
209{
Taeung Song8579aca2016-02-26 00:12:59 +0900210 if (args == NULL)
211 return;
212
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600213 switch (args->type) {
214 case PRINT_NULL:
215 break;
216 case PRINT_ATOM:
217 define_value(PRINT_FLAGS, ev_name, cur_field_name, "0",
218 args->atom.atom);
219 zero_flag_atom = 0;
220 break;
221 case PRINT_FIELD:
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -0300222 free(cur_field_name);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600223 cur_field_name = strdup(args->field.name);
224 break;
225 case PRINT_FLAGS:
226 define_event_symbols(event, ev_name, args->flags.field);
227 define_field(PRINT_FLAGS, ev_name, cur_field_name,
228 args->flags.delim);
229 define_values(PRINT_FLAGS, args->flags.flags, ev_name,
230 cur_field_name);
231 break;
232 case PRINT_SYMBOL:
233 define_event_symbols(event, ev_name, args->symbol.field);
234 define_field(PRINT_SYMBOL, ev_name, cur_field_name, NULL);
235 define_values(PRINT_SYMBOL, args->symbol.symbols, ev_name,
236 cur_field_name);
237 break;
Namhyung Kime080e6f2012-06-27 09:41:41 +0900238 case PRINT_HEX:
Daniel Borkmann0fe05592017-01-25 02:28:17 +0100239 case PRINT_HEX_STR:
Namhyung Kime080e6f2012-06-27 09:41:41 +0900240 define_event_symbols(event, ev_name, args->hex.field);
241 define_event_symbols(event, ev_name, args->hex.size);
242 break;
Javi Merinob839e1e82015-03-24 11:07:19 +0000243 case PRINT_INT_ARRAY:
244 define_event_symbols(event, ev_name, args->int_array.field);
245 define_event_symbols(event, ev_name, args->int_array.count);
246 define_event_symbols(event, ev_name, args->int_array.el_size);
247 break;
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600248 case PRINT_STRING:
249 break;
250 case PRINT_TYPE:
251 define_event_symbols(event, ev_name, args->typecast.item);
252 break;
253 case PRINT_OP:
254 if (strcmp(args->op.op, ":") == 0)
255 zero_flag_atom = 1;
256 define_event_symbols(event, ev_name, args->op.left);
257 define_event_symbols(event, ev_name, args->op.right);
258 break;
259 default:
Steven Rostedtaaf045f2012-04-06 00:47:56 +0200260 /* gcc warns for these? */
261 case PRINT_BSTRING:
262 case PRINT_DYNAMIC_ARRAY:
He Kuang76055942015-08-29 04:22:05 +0000263 case PRINT_DYNAMIC_ARRAY_LEN:
Steven Rostedtaaf045f2012-04-06 00:47:56 +0200264 case PRINT_FUNC:
Steven Rostedt (Red Hat)473a7782014-06-02 23:20:16 -0400265 case PRINT_BITMASK:
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600266 /* we should warn... */
267 return;
268 }
269
270 if (args->next)
271 define_event_symbols(event, ev_name, args->next);
272}
273
Sebastian Andrzej Siewior33058b92014-05-27 18:14:33 +0200274static PyObject *get_field_numeric_entry(struct event_format *event,
275 struct format_field *field, void *data)
276{
Sebastian Andrzej Siewior8ac631c2014-05-27 18:14:34 +0200277 bool is_array = field->flags & FIELD_IS_ARRAY;
Arnaldo Carvalho de Melo39f54862016-07-12 11:05:26 -0300278 PyObject *obj = NULL, *list = NULL;
Sebastian Andrzej Siewior33058b92014-05-27 18:14:33 +0200279 unsigned long long val;
Sebastian Andrzej Siewior8ac631c2014-05-27 18:14:34 +0200280 unsigned int item_size, n_items, i;
Sebastian Andrzej Siewior33058b92014-05-27 18:14:33 +0200281
Sebastian Andrzej Siewior8ac631c2014-05-27 18:14:34 +0200282 if (is_array) {
283 list = PyList_New(field->arraylen);
284 item_size = field->size / field->arraylen;
285 n_items = field->arraylen;
Sebastian Andrzej Siewior33058b92014-05-27 18:14:33 +0200286 } else {
Sebastian Andrzej Siewior8ac631c2014-05-27 18:14:34 +0200287 item_size = field->size;
288 n_items = 1;
Sebastian Andrzej Siewior33058b92014-05-27 18:14:33 +0200289 }
Sebastian Andrzej Siewior8ac631c2014-05-27 18:14:34 +0200290
291 for (i = 0; i < n_items; i++) {
292
293 val = read_size(event, data + field->offset + i * item_size,
294 item_size);
295 if (field->flags & FIELD_IS_SIGNED) {
296 if ((long long)val >= LONG_MIN &&
297 (long long)val <= LONG_MAX)
298 obj = PyInt_FromLong(val);
299 else
300 obj = PyLong_FromLongLong(val);
301 } else {
302 if (val <= LONG_MAX)
303 obj = PyInt_FromLong(val);
304 else
305 obj = PyLong_FromUnsignedLongLong(val);
306 }
307 if (is_array)
308 PyList_SET_ITEM(list, i, obj);
309 }
310 if (is_array)
311 obj = list;
Sebastian Andrzej Siewior33058b92014-05-27 18:14:33 +0200312 return obj;
313}
314
Joseph Schuchart0f5f5bc2014-07-10 13:50:51 +0200315
316static PyObject *python_process_callchain(struct perf_sample *sample,
317 struct perf_evsel *evsel,
318 struct addr_location *al)
319{
320 PyObject *pylist;
321
322 pylist = PyList_New(0);
323 if (!pylist)
324 Py_FatalError("couldn't create Python list");
325
326 if (!symbol_conf.use_callchain || !sample->callchain)
327 goto exit;
328
Arnaldo Carvalho de Melo91d7b2d2016-04-14 14:48:07 -0300329 if (thread__resolve_callchain(al->thread, &callchain_cursor, evsel,
Arnaldo Carvalho de Melocc8b7c22014-10-23 15:26:17 -0300330 sample, NULL, NULL,
Adrian Hunter44cbe722015-09-25 16:15:50 +0300331 scripting_max_stack) != 0) {
Joseph Schuchart0f5f5bc2014-07-10 13:50:51 +0200332 pr_err("Failed to resolve callchain. Skipping\n");
333 goto exit;
334 }
335 callchain_cursor_commit(&callchain_cursor);
336
337
338 while (1) {
339 PyObject *pyelem;
340 struct callchain_cursor_node *node;
341 node = callchain_cursor_current(&callchain_cursor);
342 if (!node)
343 break;
344
345 pyelem = PyDict_New();
346 if (!pyelem)
347 Py_FatalError("couldn't create Python dictionary");
348
349
350 pydict_set_item_string_decref(pyelem, "ip",
351 PyLong_FromUnsignedLongLong(node->ip));
352
353 if (node->sym) {
354 PyObject *pysym = PyDict_New();
355 if (!pysym)
356 Py_FatalError("couldn't create Python dictionary");
357 pydict_set_item_string_decref(pysym, "start",
358 PyLong_FromUnsignedLongLong(node->sym->start));
359 pydict_set_item_string_decref(pysym, "end",
360 PyLong_FromUnsignedLongLong(node->sym->end));
361 pydict_set_item_string_decref(pysym, "binding",
362 PyInt_FromLong(node->sym->binding));
363 pydict_set_item_string_decref(pysym, "name",
364 PyString_FromStringAndSize(node->sym->name,
365 node->sym->namelen));
366 pydict_set_item_string_decref(pyelem, "sym", pysym);
367 }
368
369 if (node->map) {
370 struct map *map = node->map;
371 const char *dsoname = "[unknown]";
372 if (map && map->dso && (map->dso->name || map->dso->long_name)) {
373 if (symbol_conf.show_kernel_path && map->dso->long_name)
374 dsoname = map->dso->long_name;
375 else if (map->dso->name)
376 dsoname = map->dso->name;
377 }
378 pydict_set_item_string_decref(pyelem, "dso",
379 PyString_FromString(dsoname));
380 }
381
382 callchain_cursor_advance(&callchain_cursor);
383 PyList_Append(pylist, pyelem);
384 Py_DECREF(pyelem);
385 }
386
387exit:
388 return pylist;
389}
390
Arnaldo Carvalho de Melob7fff6b52013-12-19 16:34:52 -0300391static void python_process_tracepoint(struct perf_sample *sample,
392 struct perf_evsel *evsel,
Arnaldo Carvalho de Melob7fff6b52013-12-19 16:34:52 -0300393 struct addr_location *al)
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600394{
Jiri Olsaadf5bcf2014-10-26 23:44:05 +0100395 struct event_format *event = evsel->tp_format;
Arnaldo Carvalho de Melo39f54862016-07-12 11:05:26 -0300396 PyObject *handler, *context, *t, *obj = NULL, *callchain;
Joseph Schuchart0f5f5bc2014-07-10 13:50:51 +0200397 PyObject *dict = NULL;
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600398 static char handler_name[256];
399 struct format_field *field;
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600400 unsigned long s, ns;
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600401 unsigned n = 0;
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600402 int pid;
David Ahernbe6d8422011-03-09 22:23:23 -0700403 int cpu = sample->cpu;
404 void *data = sample->raw_data;
405 unsigned long long nsecs = sample->time;
Arnaldo Carvalho de Melof9d5d542015-04-01 13:29:25 -0300406 const char *comm = thread__comm_str(al->thread);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600407
408 t = PyTuple_New(MAX_FIELDS);
409 if (!t)
410 Py_FatalError("couldn't create Python tuple");
411
Arnaldo Carvalho de Melo62665df2016-05-10 12:33:52 -0300412 if (!event) {
413 snprintf(handler_name, sizeof(handler_name),
414 "ug! no event found for type %" PRIu64, (u64)evsel->attr.config);
415 Py_FatalError(handler_name);
416 }
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600417
Arnaldo Carvalho de Melo97822432012-08-07 23:50:21 -0300418 pid = raw_field_value(event, "common_pid", data);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600419
420 sprintf(handler_name, "%s__%s", event->system, event->name);
421
Jiri Olsaadf5bcf2014-10-26 23:44:05 +0100422 if (!test_and_set_bit(event->id, events_defined))
423 define_event_symbols(event, handler_name, event->print_fmt.args);
424
Adrian Huntera5563ed2014-07-31 09:01:01 +0300425 handler = get_handler(handler_name);
Pierre Tardyc0251482010-05-31 23:12:09 +0200426 if (!handler) {
427 dict = PyDict_New();
428 if (!dict)
429 Py_FatalError("couldn't create Python dict");
430 }
Arnaldo Carvalho de Melobd48c632016-08-05 15:40:30 -0300431 s = nsecs / NSEC_PER_SEC;
432 ns = nsecs - s * NSEC_PER_SEC;
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600433
434 scripting_context->event_data = data;
Tom Zanussi2de95332013-01-18 13:51:27 -0600435 scripting_context->pevent = evsel->tp_format->pevent;
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600436
437 context = PyCObject_FromVoidPtr(scripting_context, NULL);
438
439 PyTuple_SetItem(t, n++, PyString_FromString(handler_name));
Kyle McMartinfb7d0b32011-01-24 11:13:04 -0500440 PyTuple_SetItem(t, n++, context);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600441
Joseph Schuchart0f5f5bc2014-07-10 13:50:51 +0200442 /* ip unwinding */
443 callchain = python_process_callchain(sample, evsel, al);
444
Pierre Tardyc0251482010-05-31 23:12:09 +0200445 if (handler) {
446 PyTuple_SetItem(t, n++, PyInt_FromLong(cpu));
447 PyTuple_SetItem(t, n++, PyInt_FromLong(s));
448 PyTuple_SetItem(t, n++, PyInt_FromLong(ns));
449 PyTuple_SetItem(t, n++, PyInt_FromLong(pid));
450 PyTuple_SetItem(t, n++, PyString_FromString(comm));
Joseph Schuchart0f5f5bc2014-07-10 13:50:51 +0200451 PyTuple_SetItem(t, n++, callchain);
Pierre Tardyc0251482010-05-31 23:12:09 +0200452 } else {
Joseph Schuchartc0268e82013-10-24 10:10:51 -0300453 pydict_set_item_string_decref(dict, "common_cpu", PyInt_FromLong(cpu));
454 pydict_set_item_string_decref(dict, "common_s", PyInt_FromLong(s));
455 pydict_set_item_string_decref(dict, "common_ns", PyInt_FromLong(ns));
456 pydict_set_item_string_decref(dict, "common_pid", PyInt_FromLong(pid));
457 pydict_set_item_string_decref(dict, "common_comm", PyString_FromString(comm));
Joseph Schuchart0f5f5bc2014-07-10 13:50:51 +0200458 pydict_set_item_string_decref(dict, "common_callchain", callchain);
Pierre Tardyc0251482010-05-31 23:12:09 +0200459 }
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600460 for (field = event->format.fields; field; field = field->next) {
Jiri Olsa249de6e2016-07-16 18:11:18 +0200461 unsigned int offset, len;
462 unsigned long long val;
463
464 if (field->flags & FIELD_IS_ARRAY) {
465 offset = field->offset;
466 len = field->size;
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600467 if (field->flags & FIELD_IS_DYNAMIC) {
Jiri Olsa249de6e2016-07-16 18:11:18 +0200468 val = pevent_read_number(scripting_context->pevent,
469 data + offset, len);
470 offset = val;
471 len = offset >> 16;
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600472 offset &= 0xffff;
Jiri Olsa249de6e2016-07-16 18:11:18 +0200473 }
474 if (field->flags & FIELD_IS_STRING &&
475 is_printable_array(data + offset, len)) {
476 obj = PyString_FromString((char *) data + offset);
477 } else {
478 obj = PyByteArray_FromStringAndSize((const char *) data + offset, len);
479 field->flags &= ~FIELD_IS_STRING;
480 }
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600481 } else { /* FIELD_IS_NUMERIC */
Sebastian Andrzej Siewior33058b92014-05-27 18:14:33 +0200482 obj = get_field_numeric_entry(event, field, data);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600483 }
Pierre Tardyc0251482010-05-31 23:12:09 +0200484 if (handler)
485 PyTuple_SetItem(t, n++, obj);
486 else
Joseph Schuchartc0268e82013-10-24 10:10:51 -0300487 pydict_set_item_string_decref(dict, field->name, obj);
Pierre Tardyc0251482010-05-31 23:12:09 +0200488
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600489 }
Joseph Schuchart0f5f5bc2014-07-10 13:50:51 +0200490
Pierre Tardyc0251482010-05-31 23:12:09 +0200491 if (!handler)
492 PyTuple_SetItem(t, n++, dict);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600493
494 if (_PyTuple_Resize(&t, n) == -1)
495 Py_FatalError("error resizing Python tuple");
496
Pierre Tardyc0251482010-05-31 23:12:09 +0200497 if (handler) {
Adrian Huntera5563ed2014-07-31 09:01:01 +0300498 call_object(handler, t, handler_name);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600499 } else {
Adrian Huntera5563ed2014-07-31 09:01:01 +0300500 try_call_object("trace_unhandled", t);
Pierre Tardyc0251482010-05-31 23:12:09 +0200501 Py_DECREF(dict);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600502 }
503
504 Py_DECREF(t);
505}
506
Adrian Hunterdf919b42014-10-23 13:45:14 +0300507static PyObject *tuple_new(unsigned int sz)
508{
509 PyObject *t;
510
511 t = PyTuple_New(sz);
512 if (!t)
513 Py_FatalError("couldn't create Python tuple");
514 return t;
515}
516
517static int tuple_set_u64(PyObject *t, unsigned int pos, u64 val)
518{
519#if BITS_PER_LONG == 64
520 return PyTuple_SetItem(t, pos, PyInt_FromLong(val));
521#endif
522#if BITS_PER_LONG == 32
523 return PyTuple_SetItem(t, pos, PyLong_FromLongLong(val));
524#endif
525}
526
527static int tuple_set_s32(PyObject *t, unsigned int pos, s32 val)
528{
529 return PyTuple_SetItem(t, pos, PyInt_FromLong(val));
530}
531
532static int tuple_set_string(PyObject *t, unsigned int pos, const char *s)
533{
534 return PyTuple_SetItem(t, pos, PyString_FromString(s));
535}
536
537static int python_export_evsel(struct db_export *dbe, struct perf_evsel *evsel)
538{
539 struct tables *tables = container_of(dbe, struct tables, dbe);
540 PyObject *t;
541
542 t = tuple_new(2);
543
544 tuple_set_u64(t, 0, evsel->db_id);
545 tuple_set_string(t, 1, perf_evsel__name(evsel));
546
547 call_object(tables->evsel_handler, t, "evsel_table");
548
549 Py_DECREF(t);
550
551 return 0;
552}
553
554static int python_export_machine(struct db_export *dbe,
555 struct machine *machine)
556{
557 struct tables *tables = container_of(dbe, struct tables, dbe);
558 PyObject *t;
559
560 t = tuple_new(3);
561
562 tuple_set_u64(t, 0, machine->db_id);
563 tuple_set_s32(t, 1, machine->pid);
564 tuple_set_string(t, 2, machine->root_dir ? machine->root_dir : "");
565
566 call_object(tables->machine_handler, t, "machine_table");
567
568 Py_DECREF(t);
569
570 return 0;
571}
572
573static int python_export_thread(struct db_export *dbe, struct thread *thread,
574 u64 main_thread_db_id, struct machine *machine)
575{
576 struct tables *tables = container_of(dbe, struct tables, dbe);
577 PyObject *t;
578
579 t = tuple_new(5);
580
581 tuple_set_u64(t, 0, thread->db_id);
582 tuple_set_u64(t, 1, machine->db_id);
583 tuple_set_u64(t, 2, main_thread_db_id);
584 tuple_set_s32(t, 3, thread->pid_);
585 tuple_set_s32(t, 4, thread->tid);
586
587 call_object(tables->thread_handler, t, "thread_table");
588
589 Py_DECREF(t);
590
591 return 0;
592}
593
594static int python_export_comm(struct db_export *dbe, struct comm *comm)
595{
596 struct tables *tables = container_of(dbe, struct tables, dbe);
597 PyObject *t;
598
599 t = tuple_new(2);
600
601 tuple_set_u64(t, 0, comm->db_id);
602 tuple_set_string(t, 1, comm__str(comm));
603
604 call_object(tables->comm_handler, t, "comm_table");
605
606 Py_DECREF(t);
607
608 return 0;
609}
610
611static int python_export_comm_thread(struct db_export *dbe, u64 db_id,
612 struct comm *comm, struct thread *thread)
613{
614 struct tables *tables = container_of(dbe, struct tables, dbe);
615 PyObject *t;
616
617 t = tuple_new(3);
618
619 tuple_set_u64(t, 0, db_id);
620 tuple_set_u64(t, 1, comm->db_id);
621 tuple_set_u64(t, 2, thread->db_id);
622
623 call_object(tables->comm_thread_handler, t, "comm_thread_table");
624
625 Py_DECREF(t);
626
627 return 0;
628}
629
630static int python_export_dso(struct db_export *dbe, struct dso *dso,
631 struct machine *machine)
632{
633 struct tables *tables = container_of(dbe, struct tables, dbe);
Masami Hiramatsub5d8bbe2016-05-11 22:51:59 +0900634 char sbuild_id[SBUILD_ID_SIZE];
Adrian Hunterdf919b42014-10-23 13:45:14 +0300635 PyObject *t;
636
637 build_id__sprintf(dso->build_id, sizeof(dso->build_id), sbuild_id);
638
639 t = tuple_new(5);
640
641 tuple_set_u64(t, 0, dso->db_id);
642 tuple_set_u64(t, 1, machine->db_id);
643 tuple_set_string(t, 2, dso->short_name);
644 tuple_set_string(t, 3, dso->long_name);
645 tuple_set_string(t, 4, sbuild_id);
646
647 call_object(tables->dso_handler, t, "dso_table");
648
649 Py_DECREF(t);
650
651 return 0;
652}
653
654static int python_export_symbol(struct db_export *dbe, struct symbol *sym,
655 struct dso *dso)
656{
657 struct tables *tables = container_of(dbe, struct tables, dbe);
658 u64 *sym_db_id = symbol__priv(sym);
659 PyObject *t;
660
661 t = tuple_new(6);
662
663 tuple_set_u64(t, 0, *sym_db_id);
664 tuple_set_u64(t, 1, dso->db_id);
665 tuple_set_u64(t, 2, sym->start);
666 tuple_set_u64(t, 3, sym->end);
667 tuple_set_s32(t, 4, sym->binding);
668 tuple_set_string(t, 5, sym->name);
669
670 call_object(tables->symbol_handler, t, "symbol_table");
671
672 Py_DECREF(t);
673
674 return 0;
675}
676
Adrian Hunterc29414f2014-10-30 16:09:44 +0200677static int python_export_branch_type(struct db_export *dbe, u32 branch_type,
678 const char *name)
679{
680 struct tables *tables = container_of(dbe, struct tables, dbe);
681 PyObject *t;
682
683 t = tuple_new(2);
684
685 tuple_set_s32(t, 0, branch_type);
686 tuple_set_string(t, 1, name);
687
688 call_object(tables->branch_type_handler, t, "branch_type_table");
689
690 Py_DECREF(t);
691
692 return 0;
693}
694
Adrian Hunterdf919b42014-10-23 13:45:14 +0300695static int python_export_sample(struct db_export *dbe,
696 struct export_sample *es)
697{
698 struct tables *tables = container_of(dbe, struct tables, dbe);
699 PyObject *t;
700
Chris Phlipot2c15f5e2016-04-28 01:19:10 -0700701 t = tuple_new(22);
Adrian Hunterdf919b42014-10-23 13:45:14 +0300702
703 tuple_set_u64(t, 0, es->db_id);
704 tuple_set_u64(t, 1, es->evsel->db_id);
705 tuple_set_u64(t, 2, es->al->machine->db_id);
Arnaldo Carvalho de Melob83e8682015-04-02 11:16:05 -0300706 tuple_set_u64(t, 3, es->al->thread->db_id);
Adrian Hunterdf919b42014-10-23 13:45:14 +0300707 tuple_set_u64(t, 4, es->comm_db_id);
708 tuple_set_u64(t, 5, es->dso_db_id);
709 tuple_set_u64(t, 6, es->sym_db_id);
710 tuple_set_u64(t, 7, es->offset);
711 tuple_set_u64(t, 8, es->sample->ip);
712 tuple_set_u64(t, 9, es->sample->time);
713 tuple_set_s32(t, 10, es->sample->cpu);
714 tuple_set_u64(t, 11, es->addr_dso_db_id);
715 tuple_set_u64(t, 12, es->addr_sym_db_id);
716 tuple_set_u64(t, 13, es->addr_offset);
717 tuple_set_u64(t, 14, es->sample->addr);
718 tuple_set_u64(t, 15, es->sample->period);
719 tuple_set_u64(t, 16, es->sample->weight);
720 tuple_set_u64(t, 17, es->sample->transaction);
721 tuple_set_u64(t, 18, es->sample->data_src);
Adrian Hunterc29414f2014-10-30 16:09:44 +0200722 tuple_set_s32(t, 19, es->sample->flags & PERF_BRANCH_MASK);
723 tuple_set_s32(t, 20, !!(es->sample->flags & PERF_IP_FLAG_IN_TX));
Chris Phlipot2c15f5e2016-04-28 01:19:10 -0700724 tuple_set_u64(t, 21, es->call_path_id);
Adrian Hunterdf919b42014-10-23 13:45:14 +0300725
726 call_object(tables->sample_handler, t, "sample_table");
727
728 Py_DECREF(t);
729
730 return 0;
731}
732
Adrian Hunter6a703072014-10-30 16:09:47 +0200733static int python_export_call_path(struct db_export *dbe, struct call_path *cp)
734{
735 struct tables *tables = container_of(dbe, struct tables, dbe);
736 PyObject *t;
737 u64 parent_db_id, sym_db_id;
738
739 parent_db_id = cp->parent ? cp->parent->db_id : 0;
740 sym_db_id = cp->sym ? *(u64 *)symbol__priv(cp->sym) : 0;
741
742 t = tuple_new(4);
743
744 tuple_set_u64(t, 0, cp->db_id);
745 tuple_set_u64(t, 1, parent_db_id);
746 tuple_set_u64(t, 2, sym_db_id);
747 tuple_set_u64(t, 3, cp->ip);
748
749 call_object(tables->call_path_handler, t, "call_path_table");
750
751 Py_DECREF(t);
752
753 return 0;
754}
755
756static int python_export_call_return(struct db_export *dbe,
757 struct call_return *cr)
758{
759 struct tables *tables = container_of(dbe, struct tables, dbe);
760 u64 comm_db_id = cr->comm ? cr->comm->db_id : 0;
761 PyObject *t;
762
763 t = tuple_new(11);
764
765 tuple_set_u64(t, 0, cr->db_id);
766 tuple_set_u64(t, 1, cr->thread->db_id);
767 tuple_set_u64(t, 2, comm_db_id);
768 tuple_set_u64(t, 3, cr->cp->db_id);
769 tuple_set_u64(t, 4, cr->call_time);
770 tuple_set_u64(t, 5, cr->return_time);
771 tuple_set_u64(t, 6, cr->branch_count);
772 tuple_set_u64(t, 7, cr->call_ref);
773 tuple_set_u64(t, 8, cr->return_ref);
774 tuple_set_u64(t, 9, cr->cp->parent->db_id);
775 tuple_set_s32(t, 10, cr->flags);
776
777 call_object(tables->call_return_handler, t, "call_return_table");
778
779 Py_DECREF(t);
780
781 return 0;
782}
783
784static int python_process_call_return(struct call_return *cr, void *data)
785{
786 struct db_export *dbe = data;
787
788 return db_export__call_return(dbe, cr);
789}
790
Arnaldo Carvalho de Melob7fff6b52013-12-19 16:34:52 -0300791static void python_process_general_event(struct perf_sample *sample,
Feng Tang6a6daec2012-08-08 17:57:51 +0800792 struct perf_evsel *evsel,
Feng Tang87b6a3a2012-08-09 13:46:13 +0800793 struct addr_location *al)
Feng Tang6a6daec2012-08-08 17:57:51 +0800794{
Adrian Huntera5563ed2014-07-31 09:01:01 +0300795 PyObject *handler, *t, *dict, *callchain, *dict_sample;
Feng Tang6a6daec2012-08-08 17:57:51 +0800796 static char handler_name[64];
797 unsigned n = 0;
Feng Tang6a6daec2012-08-08 17:57:51 +0800798
Feng Tangfd6b8582012-08-08 17:57:53 +0800799 /*
800 * Use the MAX_FIELDS to make the function expandable, though
Feng Tang87b6a3a2012-08-09 13:46:13 +0800801 * currently there is only one item for the tuple.
Feng Tangfd6b8582012-08-08 17:57:53 +0800802 */
Feng Tang6a6daec2012-08-08 17:57:51 +0800803 t = PyTuple_New(MAX_FIELDS);
804 if (!t)
805 Py_FatalError("couldn't create Python tuple");
806
Feng Tangfd6b8582012-08-08 17:57:53 +0800807 dict = PyDict_New();
808 if (!dict)
809 Py_FatalError("couldn't create Python dictionary");
810
Joseph Schuchart57608cf2014-07-10 13:50:56 +0200811 dict_sample = PyDict_New();
812 if (!dict_sample)
813 Py_FatalError("couldn't create Python dictionary");
814
Feng Tang6a6daec2012-08-08 17:57:51 +0800815 snprintf(handler_name, sizeof(handler_name), "%s", "process_event");
816
Adrian Huntera5563ed2014-07-31 09:01:01 +0300817 handler = get_handler(handler_name);
818 if (!handler)
Feng Tang6a6daec2012-08-08 17:57:51 +0800819 goto exit;
Feng Tang6a6daec2012-08-08 17:57:51 +0800820
Joseph Schuchartc0268e82013-10-24 10:10:51 -0300821 pydict_set_item_string_decref(dict, "ev_name", PyString_FromString(perf_evsel__name(evsel)));
822 pydict_set_item_string_decref(dict, "attr", PyString_FromStringAndSize(
Feng Tangfd6b8582012-08-08 17:57:53 +0800823 (const char *)&evsel->attr, sizeof(evsel->attr)));
Joseph Schuchart57608cf2014-07-10 13:50:56 +0200824
825 pydict_set_item_string_decref(dict_sample, "pid",
826 PyInt_FromLong(sample->pid));
827 pydict_set_item_string_decref(dict_sample, "tid",
828 PyInt_FromLong(sample->tid));
829 pydict_set_item_string_decref(dict_sample, "cpu",
830 PyInt_FromLong(sample->cpu));
831 pydict_set_item_string_decref(dict_sample, "ip",
832 PyLong_FromUnsignedLongLong(sample->ip));
833 pydict_set_item_string_decref(dict_sample, "time",
834 PyLong_FromUnsignedLongLong(sample->time));
835 pydict_set_item_string_decref(dict_sample, "period",
836 PyLong_FromUnsignedLongLong(sample->period));
837 pydict_set_item_string_decref(dict, "sample", dict_sample);
838
Joseph Schuchartc0268e82013-10-24 10:10:51 -0300839 pydict_set_item_string_decref(dict, "raw_buf", PyString_FromStringAndSize(
Feng Tangfd6b8582012-08-08 17:57:53 +0800840 (const char *)sample->raw_data, sample->raw_size));
Joseph Schuchartc0268e82013-10-24 10:10:51 -0300841 pydict_set_item_string_decref(dict, "comm",
Arnaldo Carvalho de Melof9d5d542015-04-01 13:29:25 -0300842 PyString_FromString(thread__comm_str(al->thread)));
Feng Tangfd6b8582012-08-08 17:57:53 +0800843 if (al->map) {
Joseph Schuchartc0268e82013-10-24 10:10:51 -0300844 pydict_set_item_string_decref(dict, "dso",
Feng Tangfd6b8582012-08-08 17:57:53 +0800845 PyString_FromString(al->map->dso->name));
846 }
847 if (al->sym) {
Joseph Schuchartc0268e82013-10-24 10:10:51 -0300848 pydict_set_item_string_decref(dict, "symbol",
Feng Tangfd6b8582012-08-08 17:57:53 +0800849 PyString_FromString(al->sym->name));
850 }
Feng Tang6a6daec2012-08-08 17:57:51 +0800851
Joseph Schuchart0f5f5bc2014-07-10 13:50:51 +0200852 /* ip unwinding */
853 callchain = python_process_callchain(sample, evsel, al);
854 pydict_set_item_string_decref(dict, "callchain", callchain);
855
Feng Tangfd6b8582012-08-08 17:57:53 +0800856 PyTuple_SetItem(t, n++, dict);
Feng Tang6a6daec2012-08-08 17:57:51 +0800857 if (_PyTuple_Resize(&t, n) == -1)
858 Py_FatalError("error resizing Python tuple");
859
Adrian Huntera5563ed2014-07-31 09:01:01 +0300860 call_object(handler, t, handler_name);
Feng Tang6a6daec2012-08-08 17:57:51 +0800861exit:
Feng Tangfd6b8582012-08-08 17:57:53 +0800862 Py_DECREF(dict);
Feng Tang6a6daec2012-08-08 17:57:51 +0800863 Py_DECREF(t);
864}
865
Adrian Hunterdf919b42014-10-23 13:45:14 +0300866static void python_process_event(union perf_event *event,
Feng Tang6a6daec2012-08-08 17:57:51 +0800867 struct perf_sample *sample,
868 struct perf_evsel *evsel,
Feng Tang73994dc2012-08-08 17:57:52 +0800869 struct addr_location *al)
Feng Tang6a6daec2012-08-08 17:57:51 +0800870{
Adrian Hunterdf919b42014-10-23 13:45:14 +0300871 struct tables *tables = &tables_global;
872
Feng Tang6a6daec2012-08-08 17:57:51 +0800873 switch (evsel->attr.type) {
874 case PERF_TYPE_TRACEPOINT:
Arnaldo Carvalho de Melof9d5d542015-04-01 13:29:25 -0300875 python_process_tracepoint(sample, evsel, al);
Feng Tang6a6daec2012-08-08 17:57:51 +0800876 break;
877 /* Reserve for future process_hw/sw/raw APIs */
878 default:
Adrian Hunterdf919b42014-10-23 13:45:14 +0300879 if (tables->db_export_mode)
Arnaldo Carvalho de Melo73272592015-04-02 11:08:30 -0300880 db_export__sample(&tables->dbe, event, sample, evsel, al);
Adrian Hunterdf919b42014-10-23 13:45:14 +0300881 else
Arnaldo Carvalho de Melof9d5d542015-04-01 13:29:25 -0300882 python_process_general_event(sample, evsel, al);
Feng Tang6a6daec2012-08-08 17:57:51 +0800883 }
884}
885
Jiri Olsaaef90262016-01-05 22:09:11 +0100886static void get_handler_name(char *str, size_t size,
887 struct perf_evsel *evsel)
888{
889 char *p = str;
890
891 scnprintf(str, size, "stat__%s", perf_evsel__name(evsel));
892
893 while ((p = strchr(p, ':'))) {
894 *p = '_';
895 p++;
896 }
897}
898
899static void
900process_stat(struct perf_evsel *counter, int cpu, int thread, u64 tstamp,
901 struct perf_counts_values *count)
902{
903 PyObject *handler, *t;
904 static char handler_name[256];
905 int n = 0;
906
907 t = PyTuple_New(MAX_FIELDS);
908 if (!t)
909 Py_FatalError("couldn't create Python tuple");
910
911 get_handler_name(handler_name, sizeof(handler_name),
912 counter);
913
914 handler = get_handler(handler_name);
915 if (!handler) {
916 pr_debug("can't find python handler %s\n", handler_name);
917 return;
918 }
919
920 PyTuple_SetItem(t, n++, PyInt_FromLong(cpu));
921 PyTuple_SetItem(t, n++, PyInt_FromLong(thread));
922
923 tuple_set_u64(t, n++, tstamp);
924 tuple_set_u64(t, n++, count->val);
925 tuple_set_u64(t, n++, count->ena);
926 tuple_set_u64(t, n++, count->run);
927
928 if (_PyTuple_Resize(&t, n) == -1)
929 Py_FatalError("error resizing Python tuple");
930
931 call_object(handler, t, handler_name);
932
933 Py_DECREF(t);
934}
935
936static void python_process_stat(struct perf_stat_config *config,
937 struct perf_evsel *counter, u64 tstamp)
938{
939 struct thread_map *threads = counter->threads;
940 struct cpu_map *cpus = counter->cpus;
941 int cpu, thread;
942
943 if (config->aggr_mode == AGGR_GLOBAL) {
944 process_stat(counter, -1, -1, tstamp,
945 &counter->counts->aggr);
946 return;
947 }
948
949 for (thread = 0; thread < threads->nr; thread++) {
950 for (cpu = 0; cpu < cpus->nr; cpu++) {
951 process_stat(counter, cpus->map[cpu],
952 thread_map__pid(threads, thread), tstamp,
953 perf_counts(counter->counts, cpu, thread));
954 }
955 }
956}
957
958static void python_process_stat_interval(u64 tstamp)
959{
960 PyObject *handler, *t;
961 static const char handler_name[] = "stat__interval";
962 int n = 0;
963
964 t = PyTuple_New(MAX_FIELDS);
965 if (!t)
966 Py_FatalError("couldn't create Python tuple");
967
968 handler = get_handler(handler_name);
969 if (!handler) {
970 pr_debug("can't find python handler %s\n", handler_name);
971 return;
972 }
973
974 tuple_set_u64(t, n++, tstamp);
975
976 if (_PyTuple_Resize(&t, n) == -1)
977 Py_FatalError("error resizing Python tuple");
978
979 call_object(handler, t, handler_name);
980
981 Py_DECREF(t);
982}
983
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600984static int run_start_sub(void)
985{
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600986 main_module = PyImport_AddModule("__main__");
987 if (main_module == NULL)
988 return -1;
989 Py_INCREF(main_module);
990
991 main_dict = PyModule_GetDict(main_module);
Adrian Huntera5563ed2014-07-31 09:01:01 +0300992 if (main_dict == NULL)
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600993 goto error;
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600994 Py_INCREF(main_dict);
995
Adrian Huntera5563ed2014-07-31 09:01:01 +0300996 try_call_object("trace_begin", NULL);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600997
Adrian Huntera5563ed2014-07-31 09:01:01 +0300998 return 0;
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600999
Tom Zanussi7e4b21b2010-01-27 02:27:57 -06001000error:
1001 Py_XDECREF(main_dict);
1002 Py_XDECREF(main_module);
Adrian Huntera5563ed2014-07-31 09:01:01 +03001003 return -1;
Tom Zanussi7e4b21b2010-01-27 02:27:57 -06001004}
1005
Adrian Hunterdf919b42014-10-23 13:45:14 +03001006#define SET_TABLE_HANDLER_(name, handler_name, table_name) do { \
1007 tables->handler_name = get_handler(#table_name); \
1008 if (tables->handler_name) \
1009 tables->dbe.export_ ## name = python_export_ ## name; \
1010} while (0)
1011
1012#define SET_TABLE_HANDLER(name) \
1013 SET_TABLE_HANDLER_(name, name ## _handler, name ## _table)
1014
1015static void set_table_handlers(struct tables *tables)
1016{
1017 const char *perf_db_export_mode = "perf_db_export_mode";
Adrian Hunter6a703072014-10-30 16:09:47 +02001018 const char *perf_db_export_calls = "perf_db_export_calls";
Chris Phlipot2c15f5e2016-04-28 01:19:10 -07001019 const char *perf_db_export_callchains = "perf_db_export_callchains";
1020 PyObject *db_export_mode, *db_export_calls, *db_export_callchains;
Adrian Hunter6a703072014-10-30 16:09:47 +02001021 bool export_calls = false;
Chris Phlipot2c15f5e2016-04-28 01:19:10 -07001022 bool export_callchains = false;
Adrian Hunterdf919b42014-10-23 13:45:14 +03001023 int ret;
1024
1025 memset(tables, 0, sizeof(struct tables));
1026 if (db_export__init(&tables->dbe))
1027 Py_FatalError("failed to initialize export");
1028
1029 db_export_mode = PyDict_GetItemString(main_dict, perf_db_export_mode);
1030 if (!db_export_mode)
1031 return;
1032
1033 ret = PyObject_IsTrue(db_export_mode);
1034 if (ret == -1)
1035 handler_call_die(perf_db_export_mode);
1036 if (!ret)
1037 return;
1038
Chris Phlipot2c15f5e2016-04-28 01:19:10 -07001039 /* handle export calls */
Adrian Hunter6a703072014-10-30 16:09:47 +02001040 tables->dbe.crp = NULL;
1041 db_export_calls = PyDict_GetItemString(main_dict, perf_db_export_calls);
1042 if (db_export_calls) {
1043 ret = PyObject_IsTrue(db_export_calls);
1044 if (ret == -1)
1045 handler_call_die(perf_db_export_calls);
1046 export_calls = !!ret;
1047 }
1048
1049 if (export_calls) {
1050 tables->dbe.crp =
1051 call_return_processor__new(python_process_call_return,
1052 &tables->dbe);
1053 if (!tables->dbe.crp)
1054 Py_FatalError("failed to create calls processor");
1055 }
1056
Chris Phlipot2c15f5e2016-04-28 01:19:10 -07001057 /* handle export callchains */
1058 tables->dbe.cpr = NULL;
1059 db_export_callchains = PyDict_GetItemString(main_dict,
1060 perf_db_export_callchains);
1061 if (db_export_callchains) {
1062 ret = PyObject_IsTrue(db_export_callchains);
1063 if (ret == -1)
1064 handler_call_die(perf_db_export_callchains);
1065 export_callchains = !!ret;
1066 }
1067
1068 if (export_callchains) {
1069 /*
1070 * Attempt to use the call path root from the call return
1071 * processor, if the call return processor is in use. Otherwise,
1072 * we allocate a new call path root. This prevents exporting
1073 * duplicate call path ids when both are in use simultaniously.
1074 */
1075 if (tables->dbe.crp)
1076 tables->dbe.cpr = tables->dbe.crp->cpr;
1077 else
1078 tables->dbe.cpr = call_path_root__new();
1079
1080 if (!tables->dbe.cpr)
Chris Phlipotaff63342016-05-07 02:17:00 -07001081 Py_FatalError("failed to create call path root");
Chris Phlipot2c15f5e2016-04-28 01:19:10 -07001082 }
1083
Adrian Hunterdf919b42014-10-23 13:45:14 +03001084 tables->db_export_mode = true;
1085 /*
1086 * Reserve per symbol space for symbol->db_id via symbol__priv()
1087 */
1088 symbol_conf.priv_size = sizeof(u64);
1089
1090 SET_TABLE_HANDLER(evsel);
1091 SET_TABLE_HANDLER(machine);
1092 SET_TABLE_HANDLER(thread);
1093 SET_TABLE_HANDLER(comm);
1094 SET_TABLE_HANDLER(comm_thread);
1095 SET_TABLE_HANDLER(dso);
1096 SET_TABLE_HANDLER(symbol);
Adrian Hunterc29414f2014-10-30 16:09:44 +02001097 SET_TABLE_HANDLER(branch_type);
Adrian Hunterdf919b42014-10-23 13:45:14 +03001098 SET_TABLE_HANDLER(sample);
Adrian Hunter6a703072014-10-30 16:09:47 +02001099 SET_TABLE_HANDLER(call_path);
1100 SET_TABLE_HANDLER(call_return);
Adrian Hunterdf919b42014-10-23 13:45:14 +03001101}
1102
Tom Zanussi7e4b21b2010-01-27 02:27:57 -06001103/*
1104 * Start trace script
1105 */
1106static int python_start_script(const char *script, int argc, const char **argv)
1107{
Adrian Hunterdf919b42014-10-23 13:45:14 +03001108 struct tables *tables = &tables_global;
Tom Zanussi7e4b21b2010-01-27 02:27:57 -06001109 const char **command_line;
1110 char buf[PATH_MAX];
1111 int i, err = 0;
1112 FILE *fp;
1113
1114 command_line = malloc((argc + 1) * sizeof(const char *));
1115 command_line[0] = script;
1116 for (i = 1; i < argc + 1; i++)
1117 command_line[i] = argv[i - 1];
1118
1119 Py_Initialize();
1120
1121 initperf_trace_context();
1122
1123 PySys_SetArgv(argc + 1, (char **)command_line);
1124
1125 fp = fopen(script, "r");
1126 if (!fp) {
1127 sprintf(buf, "Can't open python script \"%s\"", script);
1128 perror(buf);
1129 err = -1;
1130 goto error;
1131 }
1132
1133 err = PyRun_SimpleFile(fp, script);
1134 if (err) {
1135 fprintf(stderr, "Error running python script %s\n", script);
1136 goto error;
1137 }
1138
1139 err = run_start_sub();
1140 if (err) {
1141 fprintf(stderr, "Error starting python script %s\n", script);
1142 goto error;
1143 }
1144
Adrian Hunterdf919b42014-10-23 13:45:14 +03001145 set_table_handlers(tables);
1146
Adrian Hunterc29414f2014-10-30 16:09:44 +02001147 if (tables->db_export_mode) {
1148 err = db_export__branch_types(&tables->dbe);
1149 if (err)
1150 goto error;
1151 }
1152
Colin Ian King979ac252016-03-01 23:46:20 +00001153 free(command_line);
1154
Tom Zanussi7e4b21b2010-01-27 02:27:57 -06001155 return err;
1156error:
1157 Py_Finalize();
1158 free(command_line);
1159
1160 return err;
1161}
1162
Adrian Hunterd445dd22014-08-15 22:08:37 +03001163static int python_flush_script(void)
1164{
Adrian Hunter758008b2014-10-30 16:09:48 +02001165 struct tables *tables = &tables_global;
1166
1167 return db_export__flush(&tables->dbe);
Adrian Hunterd445dd22014-08-15 22:08:37 +03001168}
1169
Tom Zanussi7e4b21b2010-01-27 02:27:57 -06001170/*
1171 * Stop trace script
1172 */
1173static int python_stop_script(void)
1174{
Adrian Hunterdf919b42014-10-23 13:45:14 +03001175 struct tables *tables = &tables_global;
1176
Adrian Huntera5563ed2014-07-31 09:01:01 +03001177 try_call_object("trace_end", NULL);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -06001178
Adrian Hunterdf919b42014-10-23 13:45:14 +03001179 db_export__exit(&tables->dbe);
1180
Tom Zanussi7e4b21b2010-01-27 02:27:57 -06001181 Py_XDECREF(main_dict);
1182 Py_XDECREF(main_module);
1183 Py_Finalize();
1184
Adrian Huntera5563ed2014-07-31 09:01:01 +03001185 return 0;
Tom Zanussi7e4b21b2010-01-27 02:27:57 -06001186}
1187
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03001188static int python_generate_script(struct pevent *pevent, const char *outfile)
Tom Zanussi7e4b21b2010-01-27 02:27:57 -06001189{
Steven Rostedtaaf045f2012-04-06 00:47:56 +02001190 struct event_format *event = NULL;
Tom Zanussi7e4b21b2010-01-27 02:27:57 -06001191 struct format_field *f;
1192 char fname[PATH_MAX];
1193 int not_first, count;
1194 FILE *ofp;
1195
1196 sprintf(fname, "%s.py", outfile);
1197 ofp = fopen(fname, "w");
1198 if (ofp == NULL) {
1199 fprintf(stderr, "couldn't open %s\n", fname);
1200 return -1;
1201 }
Ingo Molnar133dc4c2010-11-16 18:45:39 +01001202 fprintf(ofp, "# perf script event handlers, "
1203 "generated by perf script -g python\n");
Tom Zanussi7e4b21b2010-01-27 02:27:57 -06001204
1205 fprintf(ofp, "# Licensed under the terms of the GNU GPL"
1206 " License version 2\n\n");
1207
1208 fprintf(ofp, "# The common_* event handler fields are the most useful "
1209 "fields common to\n");
1210
1211 fprintf(ofp, "# all events. They don't necessarily correspond to "
1212 "the 'common_*' fields\n");
1213
1214 fprintf(ofp, "# in the format files. Those fields not available as "
1215 "handler params can\n");
1216
1217 fprintf(ofp, "# be retrieved using Python functions of the form "
1218 "common_*(context).\n");
1219
1220 fprintf(ofp, "# See the perf-trace-python Documentation for the list "
1221 "of available functions.\n\n");
1222
1223 fprintf(ofp, "import os\n");
1224 fprintf(ofp, "import sys\n\n");
1225
1226 fprintf(ofp, "sys.path.append(os.environ['PERF_EXEC_PATH'] + \\\n");
1227 fprintf(ofp, "\t'/scripts/python/Perf-Trace-Util/lib/Perf/Trace')\n");
1228 fprintf(ofp, "\nfrom perf_trace_context import *\n");
1229 fprintf(ofp, "from Core import *\n\n\n");
1230
1231 fprintf(ofp, "def trace_begin():\n");
1232 fprintf(ofp, "\tprint \"in trace_begin\"\n\n");
1233
1234 fprintf(ofp, "def trace_end():\n");
1235 fprintf(ofp, "\tprint \"in trace_end\"\n\n");
1236
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03001237 while ((event = trace_find_next_event(pevent, event))) {
Tom Zanussi7e4b21b2010-01-27 02:27:57 -06001238 fprintf(ofp, "def %s__%s(", event->system, event->name);
1239 fprintf(ofp, "event_name, ");
1240 fprintf(ofp, "context, ");
1241 fprintf(ofp, "common_cpu,\n");
1242 fprintf(ofp, "\tcommon_secs, ");
1243 fprintf(ofp, "common_nsecs, ");
1244 fprintf(ofp, "common_pid, ");
1245 fprintf(ofp, "common_comm,\n\t");
Joseph Schuchart0f5f5bc2014-07-10 13:50:51 +02001246 fprintf(ofp, "common_callchain, ");
Tom Zanussi7e4b21b2010-01-27 02:27:57 -06001247
1248 not_first = 0;
1249 count = 0;
1250
1251 for (f = event->format.fields; f; f = f->next) {
1252 if (not_first++)
1253 fprintf(ofp, ", ");
1254 if (++count % 5 == 0)
1255 fprintf(ofp, "\n\t");
1256
1257 fprintf(ofp, "%s", f->name);
1258 }
1259 fprintf(ofp, "):\n");
1260
1261 fprintf(ofp, "\t\tprint_header(event_name, common_cpu, "
1262 "common_secs, common_nsecs,\n\t\t\t"
1263 "common_pid, common_comm)\n\n");
1264
1265 fprintf(ofp, "\t\tprint \"");
1266
1267 not_first = 0;
1268 count = 0;
1269
1270 for (f = event->format.fields; f; f = f->next) {
1271 if (not_first++)
1272 fprintf(ofp, ", ");
1273 if (count && count % 3 == 0) {
1274 fprintf(ofp, "\" \\\n\t\t\"");
1275 }
1276 count++;
1277
1278 fprintf(ofp, "%s=", f->name);
1279 if (f->flags & FIELD_IS_STRING ||
1280 f->flags & FIELD_IS_FLAG ||
Namhyung Kime646fe72014-05-29 13:44:55 +09001281 f->flags & FIELD_IS_ARRAY ||
Tom Zanussi7e4b21b2010-01-27 02:27:57 -06001282 f->flags & FIELD_IS_SYMBOLIC)
1283 fprintf(ofp, "%%s");
1284 else if (f->flags & FIELD_IS_SIGNED)
1285 fprintf(ofp, "%%d");
1286 else
1287 fprintf(ofp, "%%u");
1288 }
1289
Joseph Schuchart0f5f5bc2014-07-10 13:50:51 +02001290 fprintf(ofp, "\" %% \\\n\t\t(");
Tom Zanussi7e4b21b2010-01-27 02:27:57 -06001291
1292 not_first = 0;
1293 count = 0;
1294
1295 for (f = event->format.fields; f; f = f->next) {
1296 if (not_first++)
1297 fprintf(ofp, ", ");
1298
1299 if (++count % 5 == 0)
1300 fprintf(ofp, "\n\t\t");
1301
1302 if (f->flags & FIELD_IS_FLAG) {
1303 if ((count - 1) % 5 != 0) {
1304 fprintf(ofp, "\n\t\t");
1305 count = 4;
1306 }
1307 fprintf(ofp, "flag_str(\"");
1308 fprintf(ofp, "%s__%s\", ", event->system,
1309 event->name);
1310 fprintf(ofp, "\"%s\", %s)", f->name,
1311 f->name);
1312 } else if (f->flags & FIELD_IS_SYMBOLIC) {
1313 if ((count - 1) % 5 != 0) {
1314 fprintf(ofp, "\n\t\t");
1315 count = 4;
1316 }
1317 fprintf(ofp, "symbol_str(\"");
1318 fprintf(ofp, "%s__%s\", ", event->system,
1319 event->name);
1320 fprintf(ofp, "\"%s\", %s)", f->name,
1321 f->name);
1322 } else
1323 fprintf(ofp, "%s", f->name);
1324 }
1325
Joseph Schuchart0f5f5bc2014-07-10 13:50:51 +02001326 fprintf(ofp, ")\n\n");
1327
1328 fprintf(ofp, "\t\tfor node in common_callchain:");
1329 fprintf(ofp, "\n\t\t\tif 'sym' in node:");
1330 fprintf(ofp, "\n\t\t\t\tprint \"\\t[%%x] %%s\" %% (node['ip'], node['sym']['name'])");
1331 fprintf(ofp, "\n\t\t\telse:");
1332 fprintf(ofp, "\n\t\t\t\tprint \"\t[%%x]\" %% (node['ip'])\n\n");
1333 fprintf(ofp, "\t\tprint \"\\n\"\n\n");
1334
Tom Zanussi7e4b21b2010-01-27 02:27:57 -06001335 }
1336
1337 fprintf(ofp, "def trace_unhandled(event_name, context, "
Pierre Tardyc0251482010-05-31 23:12:09 +02001338 "event_fields_dict):\n");
Tom Zanussi7e4b21b2010-01-27 02:27:57 -06001339
Pierre Tardyc0251482010-05-31 23:12:09 +02001340 fprintf(ofp, "\t\tprint ' '.join(['%%s=%%s'%%(k,str(v))"
1341 "for k,v in sorted(event_fields_dict.items())])\n\n");
Tom Zanussi7e4b21b2010-01-27 02:27:57 -06001342
1343 fprintf(ofp, "def print_header("
1344 "event_name, cpu, secs, nsecs, pid, comm):\n"
1345 "\tprint \"%%-20s %%5u %%05u.%%09u %%8u %%-20s \" %% \\\n\t"
1346 "(event_name, cpu, secs, nsecs, pid, comm),\n");
1347
1348 fclose(ofp);
1349
1350 fprintf(stderr, "generated Python script: %s\n", fname);
1351
1352 return 0;
1353}
1354
1355struct scripting_ops python_scripting_ops = {
Jiri Olsaaef90262016-01-05 22:09:11 +01001356 .name = "Python",
1357 .start_script = python_start_script,
1358 .flush_script = python_flush_script,
1359 .stop_script = python_stop_script,
1360 .process_event = python_process_event,
1361 .process_stat = python_process_stat,
1362 .process_stat_interval = python_process_stat_interval,
1363 .generate_script = python_generate_script,
Tom Zanussi7e4b21b2010-01-27 02:27:57 -06001364};