1 /*
2  * trace-event-python.  Feed trace events to an embedded Python interpreter.
3  *
4  * Copyright (C) 2010 Tom Zanussi <[email protected]>
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 <inttypes.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <stdbool.h>
29 #include <errno.h>
30 #include <linux/bitmap.h>
31 #include <linux/time64.h>
32 
33 #include "../../perf.h"
34 #include "../debug.h"
35 #include "../callchain.h"
36 #include "../evsel.h"
37 #include "../util.h"
38 #include "../event.h"
39 #include "../thread.h"
40 #include "../comm.h"
41 #include "../machine.h"
42 #include "../db-export.h"
43 #include "../thread-stack.h"
44 #include "../trace-event.h"
45 #include "../machine.h"
46 #include "../call-path.h"
47 #include "thread_map.h"
48 #include "cpumap.h"
49 #include "stat.h"
50 
51 PyMODINIT_FUNC initperf_trace_context(void);
52 
53 #define TRACE_EVENT_TYPE_MAX				\
54 	((1 << (sizeof(unsigned short) * 8)) - 1)
55 
56 static DECLARE_BITMAP(events_defined, TRACE_EVENT_TYPE_MAX);
57 
58 #define MAX_FIELDS	64
59 #define N_COMMON_FIELDS	7
60 
61 extern struct scripting_context *scripting_context;
62 
63 static char *cur_field_name;
64 static int zero_flag_atom;
65 
66 static PyObject *main_module, *main_dict;
67 
68 struct tables {
69 	struct db_export	dbe;
70 	PyObject		*evsel_handler;
71 	PyObject		*machine_handler;
72 	PyObject		*thread_handler;
73 	PyObject		*comm_handler;
74 	PyObject		*comm_thread_handler;
75 	PyObject		*dso_handler;
76 	PyObject		*symbol_handler;
77 	PyObject		*branch_type_handler;
78 	PyObject		*sample_handler;
79 	PyObject		*call_path_handler;
80 	PyObject		*call_return_handler;
81 	bool			db_export_mode;
82 };
83 
84 static struct tables tables_global;
85 
86 static void handler_call_die(const char *handler_name) NORETURN;
87 static void handler_call_die(const char *handler_name)
88 {
89 	PyErr_Print();
90 	Py_FatalError("problem in Python trace event handler");
91 	// Py_FatalError does not return
92 	// but we have to make the compiler happy
93 	abort();
94 }
95 
96 /*
97  * Insert val into into the dictionary and decrement the reference counter.
98  * This is necessary for dictionaries since PyDict_SetItemString() does not
99  * steal a reference, as opposed to PyTuple_SetItem().
100  */
101 static void pydict_set_item_string_decref(PyObject *dict, const char *key, PyObject *val)
102 {
103 	PyDict_SetItemString(dict, key, val);
104 	Py_DECREF(val);
105 }
106 
107 static PyObject *get_handler(const char *handler_name)
108 {
109 	PyObject *handler;
110 
111 	handler = PyDict_GetItemString(main_dict, handler_name);
112 	if (handler && !PyCallable_Check(handler))
113 		return NULL;
114 	return handler;
115 }
116 
117 static void call_object(PyObject *handler, PyObject *args, const char *die_msg)
118 {
119 	PyObject *retval;
120 
121 	retval = PyObject_CallObject(handler, args);
122 	if (retval == NULL)
123 		handler_call_die(die_msg);
124 	Py_DECREF(retval);
125 }
126 
127 static void try_call_object(const char *handler_name, PyObject *args)
128 {
129 	PyObject *handler;
130 
131 	handler = get_handler(handler_name);
132 	if (handler)
133 		call_object(handler, args, handler_name);
134 }
135 
136 static void define_value(enum print_arg_type field_type,
137 			 const char *ev_name,
138 			 const char *field_name,
139 			 const char *field_value,
140 			 const char *field_str)
141 {
142 	const char *handler_name = "define_flag_value";
143 	PyObject *t;
144 	unsigned long long value;
145 	unsigned n = 0;
146 
147 	if (field_type == PRINT_SYMBOL)
148 		handler_name = "define_symbolic_value";
149 
150 	t = PyTuple_New(4);
151 	if (!t)
152 		Py_FatalError("couldn't create Python tuple");
153 
154 	value = eval_flag(field_value);
155 
156 	PyTuple_SetItem(t, n++, PyString_FromString(ev_name));
157 	PyTuple_SetItem(t, n++, PyString_FromString(field_name));
158 	PyTuple_SetItem(t, n++, PyInt_FromLong(value));
159 	PyTuple_SetItem(t, n++, PyString_FromString(field_str));
160 
161 	try_call_object(handler_name, t);
162 
163 	Py_DECREF(t);
164 }
165 
166 static void define_values(enum print_arg_type field_type,
167 			  struct print_flag_sym *field,
168 			  const char *ev_name,
169 			  const char *field_name)
170 {
171 	define_value(field_type, ev_name, field_name, field->value,
172 		     field->str);
173 
174 	if (field->next)
175 		define_values(field_type, field->next, ev_name, field_name);
176 }
177 
178 static void define_field(enum print_arg_type field_type,
179 			 const char *ev_name,
180 			 const char *field_name,
181 			 const char *delim)
182 {
183 	const char *handler_name = "define_flag_field";
184 	PyObject *t;
185 	unsigned n = 0;
186 
187 	if (field_type == PRINT_SYMBOL)
188 		handler_name = "define_symbolic_field";
189 
190 	if (field_type == PRINT_FLAGS)
191 		t = PyTuple_New(3);
192 	else
193 		t = PyTuple_New(2);
194 	if (!t)
195 		Py_FatalError("couldn't create Python tuple");
196 
197 	PyTuple_SetItem(t, n++, PyString_FromString(ev_name));
198 	PyTuple_SetItem(t, n++, PyString_FromString(field_name));
199 	if (field_type == PRINT_FLAGS)
200 		PyTuple_SetItem(t, n++, PyString_FromString(delim));
201 
202 	try_call_object(handler_name, t);
203 
204 	Py_DECREF(t);
205 }
206 
207 static void define_event_symbols(struct event_format *event,
208 				 const char *ev_name,
209 				 struct print_arg *args)
210 {
211 	if (args == NULL)
212 		return;
213 
214 	switch (args->type) {
215 	case PRINT_NULL:
216 		break;
217 	case PRINT_ATOM:
218 		define_value(PRINT_FLAGS, ev_name, cur_field_name, "0",
219 			     args->atom.atom);
220 		zero_flag_atom = 0;
221 		break;
222 	case PRINT_FIELD:
223 		free(cur_field_name);
224 		cur_field_name = strdup(args->field.name);
225 		break;
226 	case PRINT_FLAGS:
227 		define_event_symbols(event, ev_name, args->flags.field);
228 		define_field(PRINT_FLAGS, ev_name, cur_field_name,
229 			     args->flags.delim);
230 		define_values(PRINT_FLAGS, args->flags.flags, ev_name,
231 			      cur_field_name);
232 		break;
233 	case PRINT_SYMBOL:
234 		define_event_symbols(event, ev_name, args->symbol.field);
235 		define_field(PRINT_SYMBOL, ev_name, cur_field_name, NULL);
236 		define_values(PRINT_SYMBOL, args->symbol.symbols, ev_name,
237 			      cur_field_name);
238 		break;
239 	case PRINT_HEX:
240 	case PRINT_HEX_STR:
241 		define_event_symbols(event, ev_name, args->hex.field);
242 		define_event_symbols(event, ev_name, args->hex.size);
243 		break;
244 	case PRINT_INT_ARRAY:
245 		define_event_symbols(event, ev_name, args->int_array.field);
246 		define_event_symbols(event, ev_name, args->int_array.count);
247 		define_event_symbols(event, ev_name, args->int_array.el_size);
248 		break;
249 	case PRINT_STRING:
250 		break;
251 	case PRINT_TYPE:
252 		define_event_symbols(event, ev_name, args->typecast.item);
253 		break;
254 	case PRINT_OP:
255 		if (strcmp(args->op.op, ":") == 0)
256 			zero_flag_atom = 1;
257 		define_event_symbols(event, ev_name, args->op.left);
258 		define_event_symbols(event, ev_name, args->op.right);
259 		break;
260 	default:
261 		/* gcc warns for these? */
262 	case PRINT_BSTRING:
263 	case PRINT_DYNAMIC_ARRAY:
264 	case PRINT_DYNAMIC_ARRAY_LEN:
265 	case PRINT_FUNC:
266 	case PRINT_BITMASK:
267 		/* we should warn... */
268 		return;
269 	}
270 
271 	if (args->next)
272 		define_event_symbols(event, ev_name, args->next);
273 }
274 
275 static PyObject *get_field_numeric_entry(struct event_format *event,
276 		struct format_field *field, void *data)
277 {
278 	bool is_array = field->flags & FIELD_IS_ARRAY;
279 	PyObject *obj = NULL, *list = NULL;
280 	unsigned long long val;
281 	unsigned int item_size, n_items, i;
282 
283 	if (is_array) {
284 		list = PyList_New(field->arraylen);
285 		item_size = field->size / field->arraylen;
286 		n_items = field->arraylen;
287 	} else {
288 		item_size = field->size;
289 		n_items = 1;
290 	}
291 
292 	for (i = 0; i < n_items; i++) {
293 
294 		val = read_size(event, data + field->offset + i * item_size,
295 				item_size);
296 		if (field->flags & FIELD_IS_SIGNED) {
297 			if ((long long)val >= LONG_MIN &&
298 					(long long)val <= LONG_MAX)
299 				obj = PyInt_FromLong(val);
300 			else
301 				obj = PyLong_FromLongLong(val);
302 		} else {
303 			if (val <= LONG_MAX)
304 				obj = PyInt_FromLong(val);
305 			else
306 				obj = PyLong_FromUnsignedLongLong(val);
307 		}
308 		if (is_array)
309 			PyList_SET_ITEM(list, i, obj);
310 	}
311 	if (is_array)
312 		obj = list;
313 	return obj;
314 }
315 
316 
317 static PyObject *python_process_callchain(struct perf_sample *sample,
318 					 struct perf_evsel *evsel,
319 					 struct addr_location *al)
320 {
321 	PyObject *pylist;
322 
323 	pylist = PyList_New(0);
324 	if (!pylist)
325 		Py_FatalError("couldn't create Python list");
326 
327 	if (!symbol_conf.use_callchain || !sample->callchain)
328 		goto exit;
329 
330 	if (thread__resolve_callchain(al->thread, &callchain_cursor, evsel,
331 				      sample, NULL, NULL,
332 				      scripting_max_stack) != 0) {
333 		pr_err("Failed to resolve callchain. Skipping\n");
334 		goto exit;
335 	}
336 	callchain_cursor_commit(&callchain_cursor);
337 
338 
339 	while (1) {
340 		PyObject *pyelem;
341 		struct callchain_cursor_node *node;
342 		node = callchain_cursor_current(&callchain_cursor);
343 		if (!node)
344 			break;
345 
346 		pyelem = PyDict_New();
347 		if (!pyelem)
348 			Py_FatalError("couldn't create Python dictionary");
349 
350 
351 		pydict_set_item_string_decref(pyelem, "ip",
352 				PyLong_FromUnsignedLongLong(node->ip));
353 
354 		if (node->sym) {
355 			PyObject *pysym  = PyDict_New();
356 			if (!pysym)
357 				Py_FatalError("couldn't create Python dictionary");
358 			pydict_set_item_string_decref(pysym, "start",
359 					PyLong_FromUnsignedLongLong(node->sym->start));
360 			pydict_set_item_string_decref(pysym, "end",
361 					PyLong_FromUnsignedLongLong(node->sym->end));
362 			pydict_set_item_string_decref(pysym, "binding",
363 					PyInt_FromLong(node->sym->binding));
364 			pydict_set_item_string_decref(pysym, "name",
365 					PyString_FromStringAndSize(node->sym->name,
366 							node->sym->namelen));
367 			pydict_set_item_string_decref(pyelem, "sym", pysym);
368 		}
369 
370 		if (node->map) {
371 			struct map *map = node->map;
372 			const char *dsoname = "[unknown]";
373 			if (map && map->dso) {
374 				if (symbol_conf.show_kernel_path && map->dso->long_name)
375 					dsoname = map->dso->long_name;
376 				else
377 					dsoname = map->dso->name;
378 			}
379 			pydict_set_item_string_decref(pyelem, "dso",
380 					PyString_FromString(dsoname));
381 		}
382 
383 		callchain_cursor_advance(&callchain_cursor);
384 		PyList_Append(pylist, pyelem);
385 		Py_DECREF(pyelem);
386 	}
387 
388 exit:
389 	return pylist;
390 }
391 
392 static void python_process_tracepoint(struct perf_sample *sample,
393 				      struct perf_evsel *evsel,
394 				      struct addr_location *al)
395 {
396 	struct event_format *event = evsel->tp_format;
397 	PyObject *handler, *context, *t, *obj = NULL, *callchain;
398 	PyObject *dict = NULL;
399 	static char handler_name[256];
400 	struct format_field *field;
401 	unsigned long s, ns;
402 	unsigned n = 0;
403 	int pid;
404 	int cpu = sample->cpu;
405 	void *data = sample->raw_data;
406 	unsigned long long nsecs = sample->time;
407 	const char *comm = thread__comm_str(al->thread);
408 
409 	t = PyTuple_New(MAX_FIELDS);
410 	if (!t)
411 		Py_FatalError("couldn't create Python tuple");
412 
413 	if (!event) {
414 		snprintf(handler_name, sizeof(handler_name),
415 			 "ug! no event found for type %" PRIu64, (u64)evsel->attr.config);
416 		Py_FatalError(handler_name);
417 	}
418 
419 	pid = raw_field_value(event, "common_pid", data);
420 
421 	sprintf(handler_name, "%s__%s", event->system, event->name);
422 
423 	if (!test_and_set_bit(event->id, events_defined))
424 		define_event_symbols(event, handler_name, event->print_fmt.args);
425 
426 	handler = get_handler(handler_name);
427 	if (!handler) {
428 		dict = PyDict_New();
429 		if (!dict)
430 			Py_FatalError("couldn't create Python dict");
431 	}
432 	s = nsecs / NSEC_PER_SEC;
433 	ns = nsecs - s * NSEC_PER_SEC;
434 
435 	scripting_context->event_data = data;
436 	scripting_context->pevent = evsel->tp_format->pevent;
437 
438 	context = PyCObject_FromVoidPtr(scripting_context, NULL);
439 
440 	PyTuple_SetItem(t, n++, PyString_FromString(handler_name));
441 	PyTuple_SetItem(t, n++, context);
442 
443 	/* ip unwinding */
444 	callchain = python_process_callchain(sample, evsel, al);
445 
446 	if (handler) {
447 		PyTuple_SetItem(t, n++, PyInt_FromLong(cpu));
448 		PyTuple_SetItem(t, n++, PyInt_FromLong(s));
449 		PyTuple_SetItem(t, n++, PyInt_FromLong(ns));
450 		PyTuple_SetItem(t, n++, PyInt_FromLong(pid));
451 		PyTuple_SetItem(t, n++, PyString_FromString(comm));
452 		PyTuple_SetItem(t, n++, callchain);
453 	} else {
454 		pydict_set_item_string_decref(dict, "common_cpu", PyInt_FromLong(cpu));
455 		pydict_set_item_string_decref(dict, "common_s", PyInt_FromLong(s));
456 		pydict_set_item_string_decref(dict, "common_ns", PyInt_FromLong(ns));
457 		pydict_set_item_string_decref(dict, "common_pid", PyInt_FromLong(pid));
458 		pydict_set_item_string_decref(dict, "common_comm", PyString_FromString(comm));
459 		pydict_set_item_string_decref(dict, "common_callchain", callchain);
460 	}
461 	for (field = event->format.fields; field; field = field->next) {
462 		unsigned int offset, len;
463 		unsigned long long val;
464 
465 		if (field->flags & FIELD_IS_ARRAY) {
466 			offset = field->offset;
467 			len    = field->size;
468 			if (field->flags & FIELD_IS_DYNAMIC) {
469 				val     = pevent_read_number(scripting_context->pevent,
470 							     data + offset, len);
471 				offset  = val;
472 				len     = offset >> 16;
473 				offset &= 0xffff;
474 			}
475 			if (field->flags & FIELD_IS_STRING &&
476 			    is_printable_array(data + offset, len)) {
477 				obj = PyString_FromString((char *) data + offset);
478 			} else {
479 				obj = PyByteArray_FromStringAndSize((const char *) data + offset, len);
480 				field->flags &= ~FIELD_IS_STRING;
481 			}
482 		} else { /* FIELD_IS_NUMERIC */
483 			obj = get_field_numeric_entry(event, field, data);
484 		}
485 		if (handler)
486 			PyTuple_SetItem(t, n++, obj);
487 		else
488 			pydict_set_item_string_decref(dict, field->name, obj);
489 
490 	}
491 
492 	if (!handler)
493 		PyTuple_SetItem(t, n++, dict);
494 
495 	if (_PyTuple_Resize(&t, n) == -1)
496 		Py_FatalError("error resizing Python tuple");
497 
498 	if (handler) {
499 		call_object(handler, t, handler_name);
500 	} else {
501 		try_call_object("trace_unhandled", t);
502 		Py_DECREF(dict);
503 	}
504 
505 	Py_DECREF(t);
506 }
507 
508 static PyObject *tuple_new(unsigned int sz)
509 {
510 	PyObject *t;
511 
512 	t = PyTuple_New(sz);
513 	if (!t)
514 		Py_FatalError("couldn't create Python tuple");
515 	return t;
516 }
517 
518 static int tuple_set_u64(PyObject *t, unsigned int pos, u64 val)
519 {
520 #if BITS_PER_LONG == 64
521 	return PyTuple_SetItem(t, pos, PyInt_FromLong(val));
522 #endif
523 #if BITS_PER_LONG == 32
524 	return PyTuple_SetItem(t, pos, PyLong_FromLongLong(val));
525 #endif
526 }
527 
528 static int tuple_set_s32(PyObject *t, unsigned int pos, s32 val)
529 {
530 	return PyTuple_SetItem(t, pos, PyInt_FromLong(val));
531 }
532 
533 static int tuple_set_string(PyObject *t, unsigned int pos, const char *s)
534 {
535 	return PyTuple_SetItem(t, pos, PyString_FromString(s));
536 }
537 
538 static int python_export_evsel(struct db_export *dbe, struct perf_evsel *evsel)
539 {
540 	struct tables *tables = container_of(dbe, struct tables, dbe);
541 	PyObject *t;
542 
543 	t = tuple_new(2);
544 
545 	tuple_set_u64(t, 0, evsel->db_id);
546 	tuple_set_string(t, 1, perf_evsel__name(evsel));
547 
548 	call_object(tables->evsel_handler, t, "evsel_table");
549 
550 	Py_DECREF(t);
551 
552 	return 0;
553 }
554 
555 static int python_export_machine(struct db_export *dbe,
556 				 struct machine *machine)
557 {
558 	struct tables *tables = container_of(dbe, struct tables, dbe);
559 	PyObject *t;
560 
561 	t = tuple_new(3);
562 
563 	tuple_set_u64(t, 0, machine->db_id);
564 	tuple_set_s32(t, 1, machine->pid);
565 	tuple_set_string(t, 2, machine->root_dir ? machine->root_dir : "");
566 
567 	call_object(tables->machine_handler, t, "machine_table");
568 
569 	Py_DECREF(t);
570 
571 	return 0;
572 }
573 
574 static int python_export_thread(struct db_export *dbe, struct thread *thread,
575 				u64 main_thread_db_id, struct machine *machine)
576 {
577 	struct tables *tables = container_of(dbe, struct tables, dbe);
578 	PyObject *t;
579 
580 	t = tuple_new(5);
581 
582 	tuple_set_u64(t, 0, thread->db_id);
583 	tuple_set_u64(t, 1, machine->db_id);
584 	tuple_set_u64(t, 2, main_thread_db_id);
585 	tuple_set_s32(t, 3, thread->pid_);
586 	tuple_set_s32(t, 4, thread->tid);
587 
588 	call_object(tables->thread_handler, t, "thread_table");
589 
590 	Py_DECREF(t);
591 
592 	return 0;
593 }
594 
595 static int python_export_comm(struct db_export *dbe, struct comm *comm)
596 {
597 	struct tables *tables = container_of(dbe, struct tables, dbe);
598 	PyObject *t;
599 
600 	t = tuple_new(2);
601 
602 	tuple_set_u64(t, 0, comm->db_id);
603 	tuple_set_string(t, 1, comm__str(comm));
604 
605 	call_object(tables->comm_handler, t, "comm_table");
606 
607 	Py_DECREF(t);
608 
609 	return 0;
610 }
611 
612 static int python_export_comm_thread(struct db_export *dbe, u64 db_id,
613 				     struct comm *comm, struct thread *thread)
614 {
615 	struct tables *tables = container_of(dbe, struct tables, dbe);
616 	PyObject *t;
617 
618 	t = tuple_new(3);
619 
620 	tuple_set_u64(t, 0, db_id);
621 	tuple_set_u64(t, 1, comm->db_id);
622 	tuple_set_u64(t, 2, thread->db_id);
623 
624 	call_object(tables->comm_thread_handler, t, "comm_thread_table");
625 
626 	Py_DECREF(t);
627 
628 	return 0;
629 }
630 
631 static int python_export_dso(struct db_export *dbe, struct dso *dso,
632 			     struct machine *machine)
633 {
634 	struct tables *tables = container_of(dbe, struct tables, dbe);
635 	char sbuild_id[SBUILD_ID_SIZE];
636 	PyObject *t;
637 
638 	build_id__sprintf(dso->build_id, sizeof(dso->build_id), sbuild_id);
639 
640 	t = tuple_new(5);
641 
642 	tuple_set_u64(t, 0, dso->db_id);
643 	tuple_set_u64(t, 1, machine->db_id);
644 	tuple_set_string(t, 2, dso->short_name);
645 	tuple_set_string(t, 3, dso->long_name);
646 	tuple_set_string(t, 4, sbuild_id);
647 
648 	call_object(tables->dso_handler, t, "dso_table");
649 
650 	Py_DECREF(t);
651 
652 	return 0;
653 }
654 
655 static int python_export_symbol(struct db_export *dbe, struct symbol *sym,
656 				struct dso *dso)
657 {
658 	struct tables *tables = container_of(dbe, struct tables, dbe);
659 	u64 *sym_db_id = symbol__priv(sym);
660 	PyObject *t;
661 
662 	t = tuple_new(6);
663 
664 	tuple_set_u64(t, 0, *sym_db_id);
665 	tuple_set_u64(t, 1, dso->db_id);
666 	tuple_set_u64(t, 2, sym->start);
667 	tuple_set_u64(t, 3, sym->end);
668 	tuple_set_s32(t, 4, sym->binding);
669 	tuple_set_string(t, 5, sym->name);
670 
671 	call_object(tables->symbol_handler, t, "symbol_table");
672 
673 	Py_DECREF(t);
674 
675 	return 0;
676 }
677 
678 static int python_export_branch_type(struct db_export *dbe, u32 branch_type,
679 				     const char *name)
680 {
681 	struct tables *tables = container_of(dbe, struct tables, dbe);
682 	PyObject *t;
683 
684 	t = tuple_new(2);
685 
686 	tuple_set_s32(t, 0, branch_type);
687 	tuple_set_string(t, 1, name);
688 
689 	call_object(tables->branch_type_handler, t, "branch_type_table");
690 
691 	Py_DECREF(t);
692 
693 	return 0;
694 }
695 
696 static int python_export_sample(struct db_export *dbe,
697 				struct export_sample *es)
698 {
699 	struct tables *tables = container_of(dbe, struct tables, dbe);
700 	PyObject *t;
701 
702 	t = tuple_new(22);
703 
704 	tuple_set_u64(t, 0, es->db_id);
705 	tuple_set_u64(t, 1, es->evsel->db_id);
706 	tuple_set_u64(t, 2, es->al->machine->db_id);
707 	tuple_set_u64(t, 3, es->al->thread->db_id);
708 	tuple_set_u64(t, 4, es->comm_db_id);
709 	tuple_set_u64(t, 5, es->dso_db_id);
710 	tuple_set_u64(t, 6, es->sym_db_id);
711 	tuple_set_u64(t, 7, es->offset);
712 	tuple_set_u64(t, 8, es->sample->ip);
713 	tuple_set_u64(t, 9, es->sample->time);
714 	tuple_set_s32(t, 10, es->sample->cpu);
715 	tuple_set_u64(t, 11, es->addr_dso_db_id);
716 	tuple_set_u64(t, 12, es->addr_sym_db_id);
717 	tuple_set_u64(t, 13, es->addr_offset);
718 	tuple_set_u64(t, 14, es->sample->addr);
719 	tuple_set_u64(t, 15, es->sample->period);
720 	tuple_set_u64(t, 16, es->sample->weight);
721 	tuple_set_u64(t, 17, es->sample->transaction);
722 	tuple_set_u64(t, 18, es->sample->data_src);
723 	tuple_set_s32(t, 19, es->sample->flags & PERF_BRANCH_MASK);
724 	tuple_set_s32(t, 20, !!(es->sample->flags & PERF_IP_FLAG_IN_TX));
725 	tuple_set_u64(t, 21, es->call_path_id);
726 
727 	call_object(tables->sample_handler, t, "sample_table");
728 
729 	Py_DECREF(t);
730 
731 	return 0;
732 }
733 
734 static int python_export_call_path(struct db_export *dbe, struct call_path *cp)
735 {
736 	struct tables *tables = container_of(dbe, struct tables, dbe);
737 	PyObject *t;
738 	u64 parent_db_id, sym_db_id;
739 
740 	parent_db_id = cp->parent ? cp->parent->db_id : 0;
741 	sym_db_id = cp->sym ? *(u64 *)symbol__priv(cp->sym) : 0;
742 
743 	t = tuple_new(4);
744 
745 	tuple_set_u64(t, 0, cp->db_id);
746 	tuple_set_u64(t, 1, parent_db_id);
747 	tuple_set_u64(t, 2, sym_db_id);
748 	tuple_set_u64(t, 3, cp->ip);
749 
750 	call_object(tables->call_path_handler, t, "call_path_table");
751 
752 	Py_DECREF(t);
753 
754 	return 0;
755 }
756 
757 static int python_export_call_return(struct db_export *dbe,
758 				     struct call_return *cr)
759 {
760 	struct tables *tables = container_of(dbe, struct tables, dbe);
761 	u64 comm_db_id = cr->comm ? cr->comm->db_id : 0;
762 	PyObject *t;
763 
764 	t = tuple_new(11);
765 
766 	tuple_set_u64(t, 0, cr->db_id);
767 	tuple_set_u64(t, 1, cr->thread->db_id);
768 	tuple_set_u64(t, 2, comm_db_id);
769 	tuple_set_u64(t, 3, cr->cp->db_id);
770 	tuple_set_u64(t, 4, cr->call_time);
771 	tuple_set_u64(t, 5, cr->return_time);
772 	tuple_set_u64(t, 6, cr->branch_count);
773 	tuple_set_u64(t, 7, cr->call_ref);
774 	tuple_set_u64(t, 8, cr->return_ref);
775 	tuple_set_u64(t, 9, cr->cp->parent->db_id);
776 	tuple_set_s32(t, 10, cr->flags);
777 
778 	call_object(tables->call_return_handler, t, "call_return_table");
779 
780 	Py_DECREF(t);
781 
782 	return 0;
783 }
784 
785 static int python_process_call_return(struct call_return *cr, void *data)
786 {
787 	struct db_export *dbe = data;
788 
789 	return db_export__call_return(dbe, cr);
790 }
791 
792 static void python_process_general_event(struct perf_sample *sample,
793 					 struct perf_evsel *evsel,
794 					 struct addr_location *al)
795 {
796 	PyObject *handler, *t, *dict, *callchain, *dict_sample;
797 	static char handler_name[64];
798 	unsigned n = 0;
799 
800 	/*
801 	 * Use the MAX_FIELDS to make the function expandable, though
802 	 * currently there is only one item for the tuple.
803 	 */
804 	t = PyTuple_New(MAX_FIELDS);
805 	if (!t)
806 		Py_FatalError("couldn't create Python tuple");
807 
808 	dict = PyDict_New();
809 	if (!dict)
810 		Py_FatalError("couldn't create Python dictionary");
811 
812 	dict_sample = PyDict_New();
813 	if (!dict_sample)
814 		Py_FatalError("couldn't create Python dictionary");
815 
816 	snprintf(handler_name, sizeof(handler_name), "%s", "process_event");
817 
818 	handler = get_handler(handler_name);
819 	if (!handler)
820 		goto exit;
821 
822 	pydict_set_item_string_decref(dict, "ev_name", PyString_FromString(perf_evsel__name(evsel)));
823 	pydict_set_item_string_decref(dict, "attr", PyString_FromStringAndSize(
824 			(const char *)&evsel->attr, sizeof(evsel->attr)));
825 
826 	pydict_set_item_string_decref(dict_sample, "pid",
827 			PyInt_FromLong(sample->pid));
828 	pydict_set_item_string_decref(dict_sample, "tid",
829 			PyInt_FromLong(sample->tid));
830 	pydict_set_item_string_decref(dict_sample, "cpu",
831 			PyInt_FromLong(sample->cpu));
832 	pydict_set_item_string_decref(dict_sample, "ip",
833 			PyLong_FromUnsignedLongLong(sample->ip));
834 	pydict_set_item_string_decref(dict_sample, "time",
835 			PyLong_FromUnsignedLongLong(sample->time));
836 	pydict_set_item_string_decref(dict_sample, "period",
837 			PyLong_FromUnsignedLongLong(sample->period));
838 	pydict_set_item_string_decref(dict, "sample", dict_sample);
839 
840 	pydict_set_item_string_decref(dict, "raw_buf", PyString_FromStringAndSize(
841 			(const char *)sample->raw_data, sample->raw_size));
842 	pydict_set_item_string_decref(dict, "comm",
843 			PyString_FromString(thread__comm_str(al->thread)));
844 	if (al->map) {
845 		pydict_set_item_string_decref(dict, "dso",
846 			PyString_FromString(al->map->dso->name));
847 	}
848 	if (al->sym) {
849 		pydict_set_item_string_decref(dict, "symbol",
850 			PyString_FromString(al->sym->name));
851 	}
852 
853 	/* ip unwinding */
854 	callchain = python_process_callchain(sample, evsel, al);
855 	pydict_set_item_string_decref(dict, "callchain", callchain);
856 
857 	PyTuple_SetItem(t, n++, dict);
858 	if (_PyTuple_Resize(&t, n) == -1)
859 		Py_FatalError("error resizing Python tuple");
860 
861 	call_object(handler, t, handler_name);
862 exit:
863 	Py_DECREF(dict);
864 	Py_DECREF(t);
865 }
866 
867 static void python_process_event(union perf_event *event,
868 				 struct perf_sample *sample,
869 				 struct perf_evsel *evsel,
870 				 struct addr_location *al)
871 {
872 	struct tables *tables = &tables_global;
873 
874 	switch (evsel->attr.type) {
875 	case PERF_TYPE_TRACEPOINT:
876 		python_process_tracepoint(sample, evsel, al);
877 		break;
878 	/* Reserve for future process_hw/sw/raw APIs */
879 	default:
880 		if (tables->db_export_mode)
881 			db_export__sample(&tables->dbe, event, sample, evsel, al);
882 		else
883 			python_process_general_event(sample, evsel, al);
884 	}
885 }
886 
887 static void get_handler_name(char *str, size_t size,
888 			     struct perf_evsel *evsel)
889 {
890 	char *p = str;
891 
892 	scnprintf(str, size, "stat__%s", perf_evsel__name(evsel));
893 
894 	while ((p = strchr(p, ':'))) {
895 		*p = '_';
896 		p++;
897 	}
898 }
899 
900 static void
901 process_stat(struct perf_evsel *counter, int cpu, int thread, u64 tstamp,
902 	     struct perf_counts_values *count)
903 {
904 	PyObject *handler, *t;
905 	static char handler_name[256];
906 	int n = 0;
907 
908 	t = PyTuple_New(MAX_FIELDS);
909 	if (!t)
910 		Py_FatalError("couldn't create Python tuple");
911 
912 	get_handler_name(handler_name, sizeof(handler_name),
913 			 counter);
914 
915 	handler = get_handler(handler_name);
916 	if (!handler) {
917 		pr_debug("can't find python handler %s\n", handler_name);
918 		return;
919 	}
920 
921 	PyTuple_SetItem(t, n++, PyInt_FromLong(cpu));
922 	PyTuple_SetItem(t, n++, PyInt_FromLong(thread));
923 
924 	tuple_set_u64(t, n++, tstamp);
925 	tuple_set_u64(t, n++, count->val);
926 	tuple_set_u64(t, n++, count->ena);
927 	tuple_set_u64(t, n++, count->run);
928 
929 	if (_PyTuple_Resize(&t, n) == -1)
930 		Py_FatalError("error resizing Python tuple");
931 
932 	call_object(handler, t, handler_name);
933 
934 	Py_DECREF(t);
935 }
936 
937 static void python_process_stat(struct perf_stat_config *config,
938 				struct perf_evsel *counter, u64 tstamp)
939 {
940 	struct thread_map *threads = counter->threads;
941 	struct cpu_map *cpus = counter->cpus;
942 	int cpu, thread;
943 
944 	if (config->aggr_mode == AGGR_GLOBAL) {
945 		process_stat(counter, -1, -1, tstamp,
946 			     &counter->counts->aggr);
947 		return;
948 	}
949 
950 	for (thread = 0; thread < threads->nr; thread++) {
951 		for (cpu = 0; cpu < cpus->nr; cpu++) {
952 			process_stat(counter, cpus->map[cpu],
953 				     thread_map__pid(threads, thread), tstamp,
954 				     perf_counts(counter->counts, cpu, thread));
955 		}
956 	}
957 }
958 
959 static void python_process_stat_interval(u64 tstamp)
960 {
961 	PyObject *handler, *t;
962 	static const char handler_name[] = "stat__interval";
963 	int n = 0;
964 
965 	t = PyTuple_New(MAX_FIELDS);
966 	if (!t)
967 		Py_FatalError("couldn't create Python tuple");
968 
969 	handler = get_handler(handler_name);
970 	if (!handler) {
971 		pr_debug("can't find python handler %s\n", handler_name);
972 		return;
973 	}
974 
975 	tuple_set_u64(t, n++, tstamp);
976 
977 	if (_PyTuple_Resize(&t, n) == -1)
978 		Py_FatalError("error resizing Python tuple");
979 
980 	call_object(handler, t, handler_name);
981 
982 	Py_DECREF(t);
983 }
984 
985 static int run_start_sub(void)
986 {
987 	main_module = PyImport_AddModule("__main__");
988 	if (main_module == NULL)
989 		return -1;
990 	Py_INCREF(main_module);
991 
992 	main_dict = PyModule_GetDict(main_module);
993 	if (main_dict == NULL)
994 		goto error;
995 	Py_INCREF(main_dict);
996 
997 	try_call_object("trace_begin", NULL);
998 
999 	return 0;
1000 
1001 error:
1002 	Py_XDECREF(main_dict);
1003 	Py_XDECREF(main_module);
1004 	return -1;
1005 }
1006 
1007 #define SET_TABLE_HANDLER_(name, handler_name, table_name) do {		\
1008 	tables->handler_name = get_handler(#table_name);		\
1009 	if (tables->handler_name)					\
1010 		tables->dbe.export_ ## name = python_export_ ## name;	\
1011 } while (0)
1012 
1013 #define SET_TABLE_HANDLER(name) \
1014 	SET_TABLE_HANDLER_(name, name ## _handler, name ## _table)
1015 
1016 static void set_table_handlers(struct tables *tables)
1017 {
1018 	const char *perf_db_export_mode = "perf_db_export_mode";
1019 	const char *perf_db_export_calls = "perf_db_export_calls";
1020 	const char *perf_db_export_callchains = "perf_db_export_callchains";
1021 	PyObject *db_export_mode, *db_export_calls, *db_export_callchains;
1022 	bool export_calls = false;
1023 	bool export_callchains = false;
1024 	int ret;
1025 
1026 	memset(tables, 0, sizeof(struct tables));
1027 	if (db_export__init(&tables->dbe))
1028 		Py_FatalError("failed to initialize export");
1029 
1030 	db_export_mode = PyDict_GetItemString(main_dict, perf_db_export_mode);
1031 	if (!db_export_mode)
1032 		return;
1033 
1034 	ret = PyObject_IsTrue(db_export_mode);
1035 	if (ret == -1)
1036 		handler_call_die(perf_db_export_mode);
1037 	if (!ret)
1038 		return;
1039 
1040 	/* handle export calls */
1041 	tables->dbe.crp = NULL;
1042 	db_export_calls = PyDict_GetItemString(main_dict, perf_db_export_calls);
1043 	if (db_export_calls) {
1044 		ret = PyObject_IsTrue(db_export_calls);
1045 		if (ret == -1)
1046 			handler_call_die(perf_db_export_calls);
1047 		export_calls = !!ret;
1048 	}
1049 
1050 	if (export_calls) {
1051 		tables->dbe.crp =
1052 			call_return_processor__new(python_process_call_return,
1053 						   &tables->dbe);
1054 		if (!tables->dbe.crp)
1055 			Py_FatalError("failed to create calls processor");
1056 	}
1057 
1058 	/* handle export callchains */
1059 	tables->dbe.cpr = NULL;
1060 	db_export_callchains = PyDict_GetItemString(main_dict,
1061 						    perf_db_export_callchains);
1062 	if (db_export_callchains) {
1063 		ret = PyObject_IsTrue(db_export_callchains);
1064 		if (ret == -1)
1065 			handler_call_die(perf_db_export_callchains);
1066 		export_callchains = !!ret;
1067 	}
1068 
1069 	if (export_callchains) {
1070 		/*
1071 		 * Attempt to use the call path root from the call return
1072 		 * processor, if the call return processor is in use. Otherwise,
1073 		 * we allocate a new call path root. This prevents exporting
1074 		 * duplicate call path ids when both are in use simultaniously.
1075 		 */
1076 		if (tables->dbe.crp)
1077 			tables->dbe.cpr = tables->dbe.crp->cpr;
1078 		else
1079 			tables->dbe.cpr = call_path_root__new();
1080 
1081 		if (!tables->dbe.cpr)
1082 			Py_FatalError("failed to create call path root");
1083 	}
1084 
1085 	tables->db_export_mode = true;
1086 	/*
1087 	 * Reserve per symbol space for symbol->db_id via symbol__priv()
1088 	 */
1089 	symbol_conf.priv_size = sizeof(u64);
1090 
1091 	SET_TABLE_HANDLER(evsel);
1092 	SET_TABLE_HANDLER(machine);
1093 	SET_TABLE_HANDLER(thread);
1094 	SET_TABLE_HANDLER(comm);
1095 	SET_TABLE_HANDLER(comm_thread);
1096 	SET_TABLE_HANDLER(dso);
1097 	SET_TABLE_HANDLER(symbol);
1098 	SET_TABLE_HANDLER(branch_type);
1099 	SET_TABLE_HANDLER(sample);
1100 	SET_TABLE_HANDLER(call_path);
1101 	SET_TABLE_HANDLER(call_return);
1102 }
1103 
1104 /*
1105  * Start trace script
1106  */
1107 static int python_start_script(const char *script, int argc, const char **argv)
1108 {
1109 	struct tables *tables = &tables_global;
1110 	const char **command_line;
1111 	char buf[PATH_MAX];
1112 	int i, err = 0;
1113 	FILE *fp;
1114 
1115 	command_line = malloc((argc + 1) * sizeof(const char *));
1116 	command_line[0] = script;
1117 	for (i = 1; i < argc + 1; i++)
1118 		command_line[i] = argv[i - 1];
1119 
1120 	Py_Initialize();
1121 
1122 	initperf_trace_context();
1123 
1124 	PySys_SetArgv(argc + 1, (char **)command_line);
1125 
1126 	fp = fopen(script, "r");
1127 	if (!fp) {
1128 		sprintf(buf, "Can't open python script \"%s\"", script);
1129 		perror(buf);
1130 		err = -1;
1131 		goto error;
1132 	}
1133 
1134 	err = PyRun_SimpleFile(fp, script);
1135 	if (err) {
1136 		fprintf(stderr, "Error running python script %s\n", script);
1137 		goto error;
1138 	}
1139 
1140 	err = run_start_sub();
1141 	if (err) {
1142 		fprintf(stderr, "Error starting python script %s\n", script);
1143 		goto error;
1144 	}
1145 
1146 	set_table_handlers(tables);
1147 
1148 	if (tables->db_export_mode) {
1149 		err = db_export__branch_types(&tables->dbe);
1150 		if (err)
1151 			goto error;
1152 	}
1153 
1154 	free(command_line);
1155 
1156 	return err;
1157 error:
1158 	Py_Finalize();
1159 	free(command_line);
1160 
1161 	return err;
1162 }
1163 
1164 static int python_flush_script(void)
1165 {
1166 	struct tables *tables = &tables_global;
1167 
1168 	return db_export__flush(&tables->dbe);
1169 }
1170 
1171 /*
1172  * Stop trace script
1173  */
1174 static int python_stop_script(void)
1175 {
1176 	struct tables *tables = &tables_global;
1177 
1178 	try_call_object("trace_end", NULL);
1179 
1180 	db_export__exit(&tables->dbe);
1181 
1182 	Py_XDECREF(main_dict);
1183 	Py_XDECREF(main_module);
1184 	Py_Finalize();
1185 
1186 	return 0;
1187 }
1188 
1189 static int python_generate_script(struct pevent *pevent, const char *outfile)
1190 {
1191 	struct event_format *event = NULL;
1192 	struct format_field *f;
1193 	char fname[PATH_MAX];
1194 	int not_first, count;
1195 	FILE *ofp;
1196 
1197 	sprintf(fname, "%s.py", outfile);
1198 	ofp = fopen(fname, "w");
1199 	if (ofp == NULL) {
1200 		fprintf(stderr, "couldn't open %s\n", fname);
1201 		return -1;
1202 	}
1203 	fprintf(ofp, "# perf script event handlers, "
1204 		"generated by perf script -g python\n");
1205 
1206 	fprintf(ofp, "# Licensed under the terms of the GNU GPL"
1207 		" License version 2\n\n");
1208 
1209 	fprintf(ofp, "# The common_* event handler fields are the most useful "
1210 		"fields common to\n");
1211 
1212 	fprintf(ofp, "# all events.  They don't necessarily correspond to "
1213 		"the 'common_*' fields\n");
1214 
1215 	fprintf(ofp, "# in the format files.  Those fields not available as "
1216 		"handler params can\n");
1217 
1218 	fprintf(ofp, "# be retrieved using Python functions of the form "
1219 		"common_*(context).\n");
1220 
1221 	fprintf(ofp, "# See the perf-trace-python Documentation for the list "
1222 		"of available functions.\n\n");
1223 
1224 	fprintf(ofp, "import os\n");
1225 	fprintf(ofp, "import sys\n\n");
1226 
1227 	fprintf(ofp, "sys.path.append(os.environ['PERF_EXEC_PATH'] + \\\n");
1228 	fprintf(ofp, "\t'/scripts/python/Perf-Trace-Util/lib/Perf/Trace')\n");
1229 	fprintf(ofp, "\nfrom perf_trace_context import *\n");
1230 	fprintf(ofp, "from Core import *\n\n\n");
1231 
1232 	fprintf(ofp, "def trace_begin():\n");
1233 	fprintf(ofp, "\tprint \"in trace_begin\"\n\n");
1234 
1235 	fprintf(ofp, "def trace_end():\n");
1236 	fprintf(ofp, "\tprint \"in trace_end\"\n\n");
1237 
1238 	while ((event = trace_find_next_event(pevent, event))) {
1239 		fprintf(ofp, "def %s__%s(", event->system, event->name);
1240 		fprintf(ofp, "event_name, ");
1241 		fprintf(ofp, "context, ");
1242 		fprintf(ofp, "common_cpu,\n");
1243 		fprintf(ofp, "\tcommon_secs, ");
1244 		fprintf(ofp, "common_nsecs, ");
1245 		fprintf(ofp, "common_pid, ");
1246 		fprintf(ofp, "common_comm,\n\t");
1247 		fprintf(ofp, "common_callchain, ");
1248 
1249 		not_first = 0;
1250 		count = 0;
1251 
1252 		for (f = event->format.fields; f; f = f->next) {
1253 			if (not_first++)
1254 				fprintf(ofp, ", ");
1255 			if (++count % 5 == 0)
1256 				fprintf(ofp, "\n\t");
1257 
1258 			fprintf(ofp, "%s", f->name);
1259 		}
1260 		fprintf(ofp, "):\n");
1261 
1262 		fprintf(ofp, "\t\tprint_header(event_name, common_cpu, "
1263 			"common_secs, common_nsecs,\n\t\t\t"
1264 			"common_pid, common_comm)\n\n");
1265 
1266 		fprintf(ofp, "\t\tprint \"");
1267 
1268 		not_first = 0;
1269 		count = 0;
1270 
1271 		for (f = event->format.fields; f; f = f->next) {
1272 			if (not_first++)
1273 				fprintf(ofp, ", ");
1274 			if (count && count % 3 == 0) {
1275 				fprintf(ofp, "\" \\\n\t\t\"");
1276 			}
1277 			count++;
1278 
1279 			fprintf(ofp, "%s=", f->name);
1280 			if (f->flags & FIELD_IS_STRING ||
1281 			    f->flags & FIELD_IS_FLAG ||
1282 			    f->flags & FIELD_IS_ARRAY ||
1283 			    f->flags & FIELD_IS_SYMBOLIC)
1284 				fprintf(ofp, "%%s");
1285 			else if (f->flags & FIELD_IS_SIGNED)
1286 				fprintf(ofp, "%%d");
1287 			else
1288 				fprintf(ofp, "%%u");
1289 		}
1290 
1291 		fprintf(ofp, "\" %% \\\n\t\t(");
1292 
1293 		not_first = 0;
1294 		count = 0;
1295 
1296 		for (f = event->format.fields; f; f = f->next) {
1297 			if (not_first++)
1298 				fprintf(ofp, ", ");
1299 
1300 			if (++count % 5 == 0)
1301 				fprintf(ofp, "\n\t\t");
1302 
1303 			if (f->flags & FIELD_IS_FLAG) {
1304 				if ((count - 1) % 5 != 0) {
1305 					fprintf(ofp, "\n\t\t");
1306 					count = 4;
1307 				}
1308 				fprintf(ofp, "flag_str(\"");
1309 				fprintf(ofp, "%s__%s\", ", event->system,
1310 					event->name);
1311 				fprintf(ofp, "\"%s\", %s)", f->name,
1312 					f->name);
1313 			} else if (f->flags & FIELD_IS_SYMBOLIC) {
1314 				if ((count - 1) % 5 != 0) {
1315 					fprintf(ofp, "\n\t\t");
1316 					count = 4;
1317 				}
1318 				fprintf(ofp, "symbol_str(\"");
1319 				fprintf(ofp, "%s__%s\", ", event->system,
1320 					event->name);
1321 				fprintf(ofp, "\"%s\", %s)", f->name,
1322 					f->name);
1323 			} else
1324 				fprintf(ofp, "%s", f->name);
1325 		}
1326 
1327 		fprintf(ofp, ")\n\n");
1328 
1329 		fprintf(ofp, "\t\tfor node in common_callchain:");
1330 		fprintf(ofp, "\n\t\t\tif 'sym' in node:");
1331 		fprintf(ofp, "\n\t\t\t\tprint \"\\t[%%x] %%s\" %% (node['ip'], node['sym']['name'])");
1332 		fprintf(ofp, "\n\t\t\telse:");
1333 		fprintf(ofp, "\n\t\t\t\tprint \"\t[%%x]\" %% (node['ip'])\n\n");
1334 		fprintf(ofp, "\t\tprint \"\\n\"\n\n");
1335 
1336 	}
1337 
1338 	fprintf(ofp, "def trace_unhandled(event_name, context, "
1339 		"event_fields_dict):\n");
1340 
1341 	fprintf(ofp, "\t\tprint ' '.join(['%%s=%%s'%%(k,str(v))"
1342 		"for k,v in sorted(event_fields_dict.items())])\n\n");
1343 
1344 	fprintf(ofp, "def print_header("
1345 		"event_name, cpu, secs, nsecs, pid, comm):\n"
1346 		"\tprint \"%%-20s %%5u %%05u.%%09u %%8u %%-20s \" %% \\\n\t"
1347 		"(event_name, cpu, secs, nsecs, pid, comm),\n");
1348 
1349 	fclose(ofp);
1350 
1351 	fprintf(stderr, "generated Python script: %s\n", fname);
1352 
1353 	return 0;
1354 }
1355 
1356 struct scripting_ops python_scripting_ops = {
1357 	.name			= "Python",
1358 	.start_script		= python_start_script,
1359 	.flush_script		= python_flush_script,
1360 	.stop_script		= python_stop_script,
1361 	.process_event		= python_process_event,
1362 	.process_stat		= python_process_stat,
1363 	.process_stat_interval	= python_process_stat_interval,
1364 	.generate_script	= python_generate_script,
1365 };
1366