| 
									
										
										
										
											2010-01-27 02:27:57 -06:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * trace-event-python.  Feed trace events to an embedded Python interpreter. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Copyright (C) 2010 Tom Zanussi <tzanussi@gmail.com> | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  *  This program is free software; you can redistribute it and/or modify | 
					
						
							|  |  |  |  *  it under the terms of the GNU General Public License as published by | 
					
						
							|  |  |  |  *  the Free Software Foundation; either version 2 of the License, or | 
					
						
							|  |  |  |  *  (at your option) any later version. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  *  This program is distributed in the hope that it will be useful, | 
					
						
							|  |  |  |  *  but WITHOUT ANY WARRANTY; without even the implied warranty of | 
					
						
							|  |  |  |  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
					
						
							|  |  |  |  *  GNU General Public License for more details. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  *  You should have received a copy of the GNU General Public License | 
					
						
							|  |  |  |  *  along with this program; if not, write to the Free Software | 
					
						
							|  |  |  |  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <Python.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <stdio.h>
 | 
					
						
							|  |  |  | #include <stdlib.h>
 | 
					
						
							|  |  |  | #include <string.h>
 | 
					
						
							|  |  |  | #include <errno.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "../../perf.h"
 | 
					
						
							| 
									
										
										
										
											2014-07-14 23:46:48 +02:00
										 |  |  | #include "../debug.h"
 | 
					
						
							| 
									
										
										
										
											2014-10-09 16:12:24 -03:00
										 |  |  | #include "../callchain.h"
 | 
					
						
							| 
									
										
										
										
											2012-08-07 09:58:03 -03:00
										 |  |  | #include "../evsel.h"
 | 
					
						
							| 
									
										
										
										
											2010-01-27 02:27:57 -06:00
										 |  |  | #include "../util.h"
 | 
					
						
							| 
									
										
										
										
											2011-11-28 07:56:39 -02:00
										 |  |  | #include "../event.h"
 | 
					
						
							|  |  |  | #include "../thread.h"
 | 
					
						
							| 
									
										
										
										
											2010-01-27 02:27:57 -06:00
										 |  |  | #include "../trace-event.h"
 | 
					
						
							| 
									
										
										
										
											2014-07-10 13:50:51 +02:00
										 |  |  | #include "../machine.h"
 | 
					
						
							| 
									
										
										
										
											2010-01-27 02:27:57 -06:00
										 |  |  | 
 | 
					
						
							|  |  |  | PyMODINIT_FUNC initperf_trace_context(void); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #define FTRACE_MAX_EVENT				\
 | 
					
						
							|  |  |  | 	((1 << (sizeof(unsigned short) * 8)) - 1) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-04-06 00:47:56 +02:00
										 |  |  | struct event_format *events[FTRACE_MAX_EVENT]; | 
					
						
							| 
									
										
										
										
											2010-01-27 02:27:57 -06:00
										 |  |  | 
 | 
					
						
							|  |  |  | #define MAX_FIELDS	64
 | 
					
						
							|  |  |  | #define N_COMMON_FIELDS	7
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | extern struct scripting_context *scripting_context; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static char *cur_field_name; | 
					
						
							|  |  |  | static int zero_flag_atom; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static PyObject *main_module, *main_dict; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-07-09 16:16:31 +02:00
										 |  |  | static void handler_call_die(const char *handler_name) NORETURN; | 
					
						
							| 
									
										
										
										
											2010-01-27 02:27:57 -06:00
										 |  |  | static void handler_call_die(const char *handler_name) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	PyErr_Print(); | 
					
						
							|  |  |  | 	Py_FatalError("problem in Python trace event handler"); | 
					
						
							| 
									
										
										
										
											2014-07-09 16:16:31 +02:00
										 |  |  | 	// Py_FatalError does not return
 | 
					
						
							|  |  |  | 	// but we have to make the compiler happy
 | 
					
						
							|  |  |  | 	abort(); | 
					
						
							| 
									
										
										
										
											2010-01-27 02:27:57 -06:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-24 10:10:51 -03:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * Insert val into into the dictionary and decrement the reference counter. | 
					
						
							|  |  |  |  * This is necessary for dictionaries since PyDict_SetItemString() does not  | 
					
						
							|  |  |  |  * steal a reference, as opposed to PyTuple_SetItem(). | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | static void pydict_set_item_string_decref(PyObject *dict, const char *key, PyObject *val) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	PyDict_SetItemString(dict, key, val); | 
					
						
							|  |  |  | 	Py_DECREF(val); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-07-31 09:01:01 +03:00
										 |  |  | static PyObject *get_handler(const char *handler_name) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	PyObject *handler; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	handler = PyDict_GetItemString(main_dict, handler_name); | 
					
						
							|  |  |  | 	if (handler && !PyCallable_Check(handler)) | 
					
						
							|  |  |  | 		return NULL; | 
					
						
							|  |  |  | 	return handler; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void call_object(PyObject *handler, PyObject *args, const char *die_msg) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	PyObject *retval; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	retval = PyObject_CallObject(handler, args); | 
					
						
							|  |  |  | 	if (retval == NULL) | 
					
						
							|  |  |  | 		handler_call_die(die_msg); | 
					
						
							|  |  |  | 	Py_DECREF(retval); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void try_call_object(const char *handler_name, PyObject *args) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	PyObject *handler; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	handler = get_handler(handler_name); | 
					
						
							|  |  |  | 	if (handler) | 
					
						
							|  |  |  | 		call_object(handler, args, handler_name); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-01-27 02:27:57 -06:00
										 |  |  | static void define_value(enum print_arg_type field_type, | 
					
						
							|  |  |  | 			 const char *ev_name, | 
					
						
							|  |  |  | 			 const char *field_name, | 
					
						
							|  |  |  | 			 const char *field_value, | 
					
						
							|  |  |  | 			 const char *field_str) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	const char *handler_name = "define_flag_value"; | 
					
						
							| 
									
										
										
										
											2014-07-31 09:01:01 +03:00
										 |  |  | 	PyObject *t; | 
					
						
							| 
									
										
										
										
											2010-01-27 02:27:57 -06:00
										 |  |  | 	unsigned long long value; | 
					
						
							|  |  |  | 	unsigned n = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (field_type == PRINT_SYMBOL) | 
					
						
							|  |  |  | 		handler_name = "define_symbolic_value"; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-02-22 01:12:59 -06:00
										 |  |  | 	t = PyTuple_New(4); | 
					
						
							| 
									
										
										
										
											2010-01-27 02:27:57 -06:00
										 |  |  | 	if (!t) | 
					
						
							|  |  |  | 		Py_FatalError("couldn't create Python tuple"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	value = eval_flag(field_value); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	PyTuple_SetItem(t, n++, PyString_FromString(ev_name)); | 
					
						
							|  |  |  | 	PyTuple_SetItem(t, n++, PyString_FromString(field_name)); | 
					
						
							|  |  |  | 	PyTuple_SetItem(t, n++, PyInt_FromLong(value)); | 
					
						
							|  |  |  | 	PyTuple_SetItem(t, n++, PyString_FromString(field_str)); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-07-31 09:01:01 +03:00
										 |  |  | 	try_call_object(handler_name, t); | 
					
						
							| 
									
										
										
										
											2010-01-27 02:27:57 -06:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	Py_DECREF(t); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void define_values(enum print_arg_type field_type, | 
					
						
							|  |  |  | 			  struct print_flag_sym *field, | 
					
						
							|  |  |  | 			  const char *ev_name, | 
					
						
							|  |  |  | 			  const char *field_name) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	define_value(field_type, ev_name, field_name, field->value, | 
					
						
							|  |  |  | 		     field->str); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (field->next) | 
					
						
							|  |  |  | 		define_values(field_type, field->next, ev_name, field_name); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void define_field(enum print_arg_type field_type, | 
					
						
							|  |  |  | 			 const char *ev_name, | 
					
						
							|  |  |  | 			 const char *field_name, | 
					
						
							|  |  |  | 			 const char *delim) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	const char *handler_name = "define_flag_field"; | 
					
						
							| 
									
										
										
										
											2014-07-31 09:01:01 +03:00
										 |  |  | 	PyObject *t; | 
					
						
							| 
									
										
										
										
											2010-01-27 02:27:57 -06:00
										 |  |  | 	unsigned n = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (field_type == PRINT_SYMBOL) | 
					
						
							|  |  |  | 		handler_name = "define_symbolic_field"; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-02-22 01:12:59 -06:00
										 |  |  | 	if (field_type == PRINT_FLAGS) | 
					
						
							|  |  |  | 		t = PyTuple_New(3); | 
					
						
							|  |  |  | 	else | 
					
						
							|  |  |  | 		t = PyTuple_New(2); | 
					
						
							| 
									
										
										
										
											2010-01-27 02:27:57 -06:00
										 |  |  | 	if (!t) | 
					
						
							|  |  |  | 		Py_FatalError("couldn't create Python tuple"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	PyTuple_SetItem(t, n++, PyString_FromString(ev_name)); | 
					
						
							|  |  |  | 	PyTuple_SetItem(t, n++, PyString_FromString(field_name)); | 
					
						
							|  |  |  | 	if (field_type == PRINT_FLAGS) | 
					
						
							|  |  |  | 		PyTuple_SetItem(t, n++, PyString_FromString(delim)); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-07-31 09:01:01 +03:00
										 |  |  | 	try_call_object(handler_name, t); | 
					
						
							| 
									
										
										
										
											2010-01-27 02:27:57 -06:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	Py_DECREF(t); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-04-06 00:47:56 +02:00
										 |  |  | static void define_event_symbols(struct event_format *event, | 
					
						
							| 
									
										
										
										
											2010-01-27 02:27:57 -06:00
										 |  |  | 				 const char *ev_name, | 
					
						
							|  |  |  | 				 struct print_arg *args) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	switch (args->type) { | 
					
						
							|  |  |  | 	case PRINT_NULL: | 
					
						
							|  |  |  | 		break; | 
					
						
							|  |  |  | 	case PRINT_ATOM: | 
					
						
							|  |  |  | 		define_value(PRINT_FLAGS, ev_name, cur_field_name, "0", | 
					
						
							|  |  |  | 			     args->atom.atom); | 
					
						
							|  |  |  | 		zero_flag_atom = 0; | 
					
						
							|  |  |  | 		break; | 
					
						
							|  |  |  | 	case PRINT_FIELD: | 
					
						
							| 
									
										
										
										
											2013-12-26 15:54:57 -03:00
										 |  |  | 		free(cur_field_name); | 
					
						
							| 
									
										
										
										
											2010-01-27 02:27:57 -06:00
										 |  |  | 		cur_field_name = strdup(args->field.name); | 
					
						
							|  |  |  | 		break; | 
					
						
							|  |  |  | 	case PRINT_FLAGS: | 
					
						
							|  |  |  | 		define_event_symbols(event, ev_name, args->flags.field); | 
					
						
							|  |  |  | 		define_field(PRINT_FLAGS, ev_name, cur_field_name, | 
					
						
							|  |  |  | 			     args->flags.delim); | 
					
						
							|  |  |  | 		define_values(PRINT_FLAGS, args->flags.flags, ev_name, | 
					
						
							|  |  |  | 			      cur_field_name); | 
					
						
							|  |  |  | 		break; | 
					
						
							|  |  |  | 	case PRINT_SYMBOL: | 
					
						
							|  |  |  | 		define_event_symbols(event, ev_name, args->symbol.field); | 
					
						
							|  |  |  | 		define_field(PRINT_SYMBOL, ev_name, cur_field_name, NULL); | 
					
						
							|  |  |  | 		define_values(PRINT_SYMBOL, args->symbol.symbols, ev_name, | 
					
						
							|  |  |  | 			      cur_field_name); | 
					
						
							|  |  |  | 		break; | 
					
						
							| 
									
										
										
										
											2012-06-27 09:41:41 +09:00
										 |  |  | 	case PRINT_HEX: | 
					
						
							|  |  |  | 		define_event_symbols(event, ev_name, args->hex.field); | 
					
						
							|  |  |  | 		define_event_symbols(event, ev_name, args->hex.size); | 
					
						
							|  |  |  | 		break; | 
					
						
							| 
									
										
										
										
											2010-01-27 02:27:57 -06:00
										 |  |  | 	case PRINT_STRING: | 
					
						
							|  |  |  | 		break; | 
					
						
							|  |  |  | 	case PRINT_TYPE: | 
					
						
							|  |  |  | 		define_event_symbols(event, ev_name, args->typecast.item); | 
					
						
							|  |  |  | 		break; | 
					
						
							|  |  |  | 	case PRINT_OP: | 
					
						
							|  |  |  | 		if (strcmp(args->op.op, ":") == 0) | 
					
						
							|  |  |  | 			zero_flag_atom = 1; | 
					
						
							|  |  |  | 		define_event_symbols(event, ev_name, args->op.left); | 
					
						
							|  |  |  | 		define_event_symbols(event, ev_name, args->op.right); | 
					
						
							|  |  |  | 		break; | 
					
						
							|  |  |  | 	default: | 
					
						
							| 
									
										
										
										
											2012-04-06 00:47:56 +02:00
										 |  |  | 		/* gcc warns for these? */ | 
					
						
							|  |  |  | 	case PRINT_BSTRING: | 
					
						
							|  |  |  | 	case PRINT_DYNAMIC_ARRAY: | 
					
						
							|  |  |  | 	case PRINT_FUNC: | 
					
						
							| 
									
										
										
										
											2014-06-02 23:20:16 -04:00
										 |  |  | 	case PRINT_BITMASK: | 
					
						
							| 
									
										
										
										
											2010-01-27 02:27:57 -06:00
										 |  |  | 		/* we should warn... */ | 
					
						
							|  |  |  | 		return; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (args->next) | 
					
						
							|  |  |  | 		define_event_symbols(event, ev_name, args->next); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-08-07 09:58:03 -03:00
										 |  |  | static inline struct event_format *find_cache_event(struct perf_evsel *evsel) | 
					
						
							| 
									
										
										
										
											2010-01-27 02:27:57 -06:00
										 |  |  | { | 
					
						
							|  |  |  | 	static char ev_name[256]; | 
					
						
							| 
									
										
										
										
											2012-04-06 00:47:56 +02:00
										 |  |  | 	struct event_format *event; | 
					
						
							| 
									
										
										
										
											2012-08-07 09:58:03 -03:00
										 |  |  | 	int type = evsel->attr.config; | 
					
						
							| 
									
										
										
										
											2010-01-27 02:27:57 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-08-07 09:58:03 -03:00
										 |  |  | 	/*
 | 
					
						
							|  |  |  |  	 * XXX: Do we really need to cache this since now we have evsel->tp_format | 
					
						
							|  |  |  |  	 * cached already? Need to re-read this "cache" routine that as well calls | 
					
						
							|  |  |  |  	 * define_event_symbols() :-\ | 
					
						
							|  |  |  |  	 */ | 
					
						
							| 
									
										
										
										
											2010-01-27 02:27:57 -06:00
										 |  |  | 	if (events[type]) | 
					
						
							|  |  |  | 		return events[type]; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-08-07 09:58:03 -03:00
										 |  |  | 	events[type] = event = evsel->tp_format; | 
					
						
							| 
									
										
										
										
											2010-01-27 02:27:57 -06:00
										 |  |  | 	if (!event) | 
					
						
							|  |  |  | 		return NULL; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	sprintf(ev_name, "%s__%s", event->system, event->name); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	define_event_symbols(event, ev_name, event->print_fmt.args); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return event; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-05-27 18:14:33 +02:00
										 |  |  | static PyObject *get_field_numeric_entry(struct event_format *event, | 
					
						
							|  |  |  | 		struct format_field *field, void *data) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2014-05-27 18:14:34 +02:00
										 |  |  | 	bool is_array = field->flags & FIELD_IS_ARRAY; | 
					
						
							|  |  |  | 	PyObject *obj, *list = NULL; | 
					
						
							| 
									
										
										
										
											2014-05-27 18:14:33 +02:00
										 |  |  | 	unsigned long long val; | 
					
						
							| 
									
										
										
										
											2014-05-27 18:14:34 +02:00
										 |  |  | 	unsigned int item_size, n_items, i; | 
					
						
							| 
									
										
										
										
											2014-05-27 18:14:33 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-05-27 18:14:34 +02:00
										 |  |  | 	if (is_array) { | 
					
						
							|  |  |  | 		list = PyList_New(field->arraylen); | 
					
						
							|  |  |  | 		item_size = field->size / field->arraylen; | 
					
						
							|  |  |  | 		n_items = field->arraylen; | 
					
						
							| 
									
										
										
										
											2014-05-27 18:14:33 +02:00
										 |  |  | 	} else { | 
					
						
							| 
									
										
										
										
											2014-05-27 18:14:34 +02:00
										 |  |  | 		item_size = field->size; | 
					
						
							|  |  |  | 		n_items = 1; | 
					
						
							| 
									
										
										
										
											2014-05-27 18:14:33 +02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-05-27 18:14:34 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	for (i = 0; i < n_items; i++) { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		val = read_size(event, data + field->offset + i * item_size, | 
					
						
							|  |  |  | 				item_size); | 
					
						
							|  |  |  | 		if (field->flags & FIELD_IS_SIGNED) { | 
					
						
							|  |  |  | 			if ((long long)val >= LONG_MIN && | 
					
						
							|  |  |  | 					(long long)val <= LONG_MAX) | 
					
						
							|  |  |  | 				obj = PyInt_FromLong(val); | 
					
						
							|  |  |  | 			else | 
					
						
							|  |  |  | 				obj = PyLong_FromLongLong(val); | 
					
						
							|  |  |  | 		} else { | 
					
						
							|  |  |  | 			if (val <= LONG_MAX) | 
					
						
							|  |  |  | 				obj = PyInt_FromLong(val); | 
					
						
							|  |  |  | 			else | 
					
						
							|  |  |  | 				obj = PyLong_FromUnsignedLongLong(val); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		if (is_array) | 
					
						
							|  |  |  | 			PyList_SET_ITEM(list, i, obj); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if (is_array) | 
					
						
							|  |  |  | 		obj = list; | 
					
						
							| 
									
										
										
										
											2014-05-27 18:14:33 +02:00
										 |  |  | 	return obj; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-07-10 13:50:51 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | static PyObject *python_process_callchain(struct perf_sample *sample, | 
					
						
							|  |  |  | 					 struct perf_evsel *evsel, | 
					
						
							|  |  |  | 					 struct addr_location *al) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	PyObject *pylist; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	pylist = PyList_New(0); | 
					
						
							|  |  |  | 	if (!pylist) | 
					
						
							|  |  |  | 		Py_FatalError("couldn't create Python list"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (!symbol_conf.use_callchain || !sample->callchain) | 
					
						
							|  |  |  | 		goto exit; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (machine__resolve_callchain(al->machine, evsel, al->thread, | 
					
						
							|  |  |  | 					   sample, NULL, NULL, | 
					
						
							|  |  |  | 					   PERF_MAX_STACK_DEPTH) != 0) { | 
					
						
							|  |  |  | 		pr_err("Failed to resolve callchain. Skipping\n"); | 
					
						
							|  |  |  | 		goto exit; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	callchain_cursor_commit(&callchain_cursor); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	while (1) { | 
					
						
							|  |  |  | 		PyObject *pyelem; | 
					
						
							|  |  |  | 		struct callchain_cursor_node *node; | 
					
						
							|  |  |  | 		node = callchain_cursor_current(&callchain_cursor); | 
					
						
							|  |  |  | 		if (!node) | 
					
						
							|  |  |  | 			break; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		pyelem = PyDict_New(); | 
					
						
							|  |  |  | 		if (!pyelem) | 
					
						
							|  |  |  | 			Py_FatalError("couldn't create Python dictionary"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		pydict_set_item_string_decref(pyelem, "ip", | 
					
						
							|  |  |  | 				PyLong_FromUnsignedLongLong(node->ip)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if (node->sym) { | 
					
						
							|  |  |  | 			PyObject *pysym  = PyDict_New(); | 
					
						
							|  |  |  | 			if (!pysym) | 
					
						
							|  |  |  | 				Py_FatalError("couldn't create Python dictionary"); | 
					
						
							|  |  |  | 			pydict_set_item_string_decref(pysym, "start", | 
					
						
							|  |  |  | 					PyLong_FromUnsignedLongLong(node->sym->start)); | 
					
						
							|  |  |  | 			pydict_set_item_string_decref(pysym, "end", | 
					
						
							|  |  |  | 					PyLong_FromUnsignedLongLong(node->sym->end)); | 
					
						
							|  |  |  | 			pydict_set_item_string_decref(pysym, "binding", | 
					
						
							|  |  |  | 					PyInt_FromLong(node->sym->binding)); | 
					
						
							|  |  |  | 			pydict_set_item_string_decref(pysym, "name", | 
					
						
							|  |  |  | 					PyString_FromStringAndSize(node->sym->name, | 
					
						
							|  |  |  | 							node->sym->namelen)); | 
					
						
							|  |  |  | 			pydict_set_item_string_decref(pyelem, "sym", pysym); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if (node->map) { | 
					
						
							|  |  |  | 			struct map *map = node->map; | 
					
						
							|  |  |  | 			const char *dsoname = "[unknown]"; | 
					
						
							|  |  |  | 			if (map && map->dso && (map->dso->name || map->dso->long_name)) { | 
					
						
							|  |  |  | 				if (symbol_conf.show_kernel_path && map->dso->long_name) | 
					
						
							|  |  |  | 					dsoname = map->dso->long_name; | 
					
						
							|  |  |  | 				else if (map->dso->name) | 
					
						
							|  |  |  | 					dsoname = map->dso->name; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			pydict_set_item_string_decref(pyelem, "dso", | 
					
						
							|  |  |  | 					PyString_FromString(dsoname)); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		callchain_cursor_advance(&callchain_cursor); | 
					
						
							|  |  |  | 		PyList_Append(pylist, pyelem); | 
					
						
							|  |  |  | 		Py_DECREF(pyelem); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | exit: | 
					
						
							|  |  |  | 	return pylist; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-12-19 16:34:52 -03:00
										 |  |  | static void python_process_tracepoint(struct perf_sample *sample, | 
					
						
							|  |  |  | 				      struct perf_evsel *evsel, | 
					
						
							|  |  |  | 				      struct thread *thread, | 
					
						
							|  |  |  | 				      struct addr_location *al) | 
					
						
							| 
									
										
										
										
											2010-01-27 02:27:57 -06:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2014-07-31 09:01:01 +03:00
										 |  |  | 	PyObject *handler, *context, *t, *obj, *callchain; | 
					
						
							| 
									
										
										
										
											2014-07-10 13:50:51 +02:00
										 |  |  | 	PyObject *dict = NULL; | 
					
						
							| 
									
										
										
										
											2010-01-27 02:27:57 -06:00
										 |  |  | 	static char handler_name[256]; | 
					
						
							|  |  |  | 	struct format_field *field; | 
					
						
							|  |  |  | 	unsigned long s, ns; | 
					
						
							| 
									
										
										
										
											2012-04-06 00:47:56 +02:00
										 |  |  | 	struct event_format *event; | 
					
						
							| 
									
										
										
										
											2010-01-27 02:27:57 -06:00
										 |  |  | 	unsigned n = 0; | 
					
						
							|  |  |  | 	int pid; | 
					
						
							| 
									
										
										
										
											2011-03-09 22:23:23 -07:00
										 |  |  | 	int cpu = sample->cpu; | 
					
						
							|  |  |  | 	void *data = sample->raw_data; | 
					
						
							|  |  |  | 	unsigned long long nsecs = sample->time; | 
					
						
							| 
									
										
										
										
											2013-09-11 14:46:56 +02:00
										 |  |  | 	const char *comm = thread__comm_str(thread); | 
					
						
							| 
									
										
										
										
											2010-01-27 02:27:57 -06:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	t = PyTuple_New(MAX_FIELDS); | 
					
						
							|  |  |  | 	if (!t) | 
					
						
							|  |  |  | 		Py_FatalError("couldn't create Python tuple"); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-08-07 09:58:03 -03:00
										 |  |  | 	event = find_cache_event(evsel); | 
					
						
							| 
									
										
										
										
											2010-01-27 02:27:57 -06:00
										 |  |  | 	if (!event) | 
					
						
							| 
									
										
										
										
											2012-08-07 09:58:03 -03:00
										 |  |  | 		die("ug! no event found for type %d", (int)evsel->attr.config); | 
					
						
							| 
									
										
										
										
											2010-01-27 02:27:57 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-08-07 23:50:21 -03:00
										 |  |  | 	pid = raw_field_value(event, "common_pid", data); | 
					
						
							| 
									
										
										
										
											2010-01-27 02:27:57 -06:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	sprintf(handler_name, "%s__%s", event->system, event->name); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-07-31 09:01:01 +03:00
										 |  |  | 	handler = get_handler(handler_name); | 
					
						
							| 
									
										
										
										
											2010-05-31 23:12:09 +02:00
										 |  |  | 	if (!handler) { | 
					
						
							|  |  |  | 		dict = PyDict_New(); | 
					
						
							|  |  |  | 		if (!dict) | 
					
						
							|  |  |  | 			Py_FatalError("couldn't create Python dict"); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2010-01-27 02:27:57 -06:00
										 |  |  | 	s = nsecs / NSECS_PER_SEC; | 
					
						
							|  |  |  | 	ns = nsecs - s * NSECS_PER_SEC; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	scripting_context->event_data = data; | 
					
						
							| 
									
										
										
										
											2013-01-18 13:51:27 -06:00
										 |  |  | 	scripting_context->pevent = evsel->tp_format->pevent; | 
					
						
							| 
									
										
										
										
											2010-01-27 02:27:57 -06:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	context = PyCObject_FromVoidPtr(scripting_context, NULL); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	PyTuple_SetItem(t, n++, PyString_FromString(handler_name)); | 
					
						
							| 
									
										
										
										
											2011-01-24 11:13:04 -05:00
										 |  |  | 	PyTuple_SetItem(t, n++, context); | 
					
						
							| 
									
										
										
										
											2010-01-27 02:27:57 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-07-10 13:50:51 +02:00
										 |  |  | 	/* ip unwinding */ | 
					
						
							|  |  |  | 	callchain = python_process_callchain(sample, evsel, al); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-05-31 23:12:09 +02:00
										 |  |  | 	if (handler) { | 
					
						
							|  |  |  | 		PyTuple_SetItem(t, n++, PyInt_FromLong(cpu)); | 
					
						
							|  |  |  | 		PyTuple_SetItem(t, n++, PyInt_FromLong(s)); | 
					
						
							|  |  |  | 		PyTuple_SetItem(t, n++, PyInt_FromLong(ns)); | 
					
						
							|  |  |  | 		PyTuple_SetItem(t, n++, PyInt_FromLong(pid)); | 
					
						
							|  |  |  | 		PyTuple_SetItem(t, n++, PyString_FromString(comm)); | 
					
						
							| 
									
										
										
										
											2014-07-10 13:50:51 +02:00
										 |  |  | 		PyTuple_SetItem(t, n++, callchain); | 
					
						
							| 
									
										
										
										
											2010-05-31 23:12:09 +02:00
										 |  |  | 	} else { | 
					
						
							| 
									
										
										
										
											2013-10-24 10:10:51 -03:00
										 |  |  | 		pydict_set_item_string_decref(dict, "common_cpu", PyInt_FromLong(cpu)); | 
					
						
							|  |  |  | 		pydict_set_item_string_decref(dict, "common_s", PyInt_FromLong(s)); | 
					
						
							|  |  |  | 		pydict_set_item_string_decref(dict, "common_ns", PyInt_FromLong(ns)); | 
					
						
							|  |  |  | 		pydict_set_item_string_decref(dict, "common_pid", PyInt_FromLong(pid)); | 
					
						
							|  |  |  | 		pydict_set_item_string_decref(dict, "common_comm", PyString_FromString(comm)); | 
					
						
							| 
									
										
										
										
											2014-07-10 13:50:51 +02:00
										 |  |  | 		pydict_set_item_string_decref(dict, "common_callchain", callchain); | 
					
						
							| 
									
										
										
										
											2010-05-31 23:12:09 +02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2010-01-27 02:27:57 -06:00
										 |  |  | 	for (field = event->format.fields; field; field = field->next) { | 
					
						
							|  |  |  | 		if (field->flags & FIELD_IS_STRING) { | 
					
						
							|  |  |  | 			int offset; | 
					
						
							|  |  |  | 			if (field->flags & FIELD_IS_DYNAMIC) { | 
					
						
							|  |  |  | 				offset = *(int *)(data + field->offset); | 
					
						
							|  |  |  | 				offset &= 0xffff; | 
					
						
							|  |  |  | 			} else | 
					
						
							|  |  |  | 				offset = field->offset; | 
					
						
							| 
									
										
										
										
											2010-04-01 23:58:25 -05:00
										 |  |  | 			obj = PyString_FromString((char *)data + offset); | 
					
						
							| 
									
										
										
										
											2010-01-27 02:27:57 -06:00
										 |  |  | 		} else { /* FIELD_IS_NUMERIC */ | 
					
						
							| 
									
										
										
										
											2014-05-27 18:14:33 +02:00
										 |  |  | 			obj = get_field_numeric_entry(event, field, data); | 
					
						
							| 
									
										
										
										
											2010-01-27 02:27:57 -06:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2010-05-31 23:12:09 +02:00
										 |  |  | 		if (handler) | 
					
						
							|  |  |  | 			PyTuple_SetItem(t, n++, obj); | 
					
						
							|  |  |  | 		else | 
					
						
							| 
									
										
										
										
											2013-10-24 10:10:51 -03:00
										 |  |  | 			pydict_set_item_string_decref(dict, field->name, obj); | 
					
						
							| 
									
										
										
										
											2010-05-31 23:12:09 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-01-27 02:27:57 -06:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-07-10 13:50:51 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-05-31 23:12:09 +02:00
										 |  |  | 	if (!handler) | 
					
						
							|  |  |  | 		PyTuple_SetItem(t, n++, dict); | 
					
						
							| 
									
										
										
										
											2010-01-27 02:27:57 -06:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if (_PyTuple_Resize(&t, n) == -1) | 
					
						
							|  |  |  | 		Py_FatalError("error resizing Python tuple"); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-05-31 23:12:09 +02:00
										 |  |  | 	if (handler) { | 
					
						
							| 
									
										
										
										
											2014-07-31 09:01:01 +03:00
										 |  |  | 		call_object(handler, t, handler_name); | 
					
						
							| 
									
										
										
										
											2010-01-27 02:27:57 -06:00
										 |  |  | 	} else { | 
					
						
							| 
									
										
										
										
											2014-07-31 09:01:01 +03:00
										 |  |  | 		try_call_object("trace_unhandled", t); | 
					
						
							| 
									
										
										
										
											2010-05-31 23:12:09 +02:00
										 |  |  | 		Py_DECREF(dict); | 
					
						
							| 
									
										
										
										
											2010-01-27 02:27:57 -06:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	Py_DECREF(t); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-12-19 16:34:52 -03:00
										 |  |  | static void python_process_general_event(struct perf_sample *sample, | 
					
						
							| 
									
										
										
										
											2012-08-08 17:57:51 +08:00
										 |  |  | 					 struct perf_evsel *evsel, | 
					
						
							| 
									
										
										
										
											2013-07-18 16:06:15 -06:00
										 |  |  | 					 struct thread *thread, | 
					
						
							| 
									
										
										
										
											2012-08-09 13:46:13 +08:00
										 |  |  | 					 struct addr_location *al) | 
					
						
							| 
									
										
										
										
											2012-08-08 17:57:51 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2014-07-31 09:01:01 +03:00
										 |  |  | 	PyObject *handler, *t, *dict, *callchain, *dict_sample; | 
					
						
							| 
									
										
										
										
											2012-08-08 17:57:51 +08:00
										 |  |  | 	static char handler_name[64]; | 
					
						
							|  |  |  | 	unsigned n = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-08-08 17:57:53 +08:00
										 |  |  | 	/*
 | 
					
						
							|  |  |  | 	 * Use the MAX_FIELDS to make the function expandable, though | 
					
						
							| 
									
										
										
										
											2012-08-09 13:46:13 +08:00
										 |  |  | 	 * currently there is only one item for the tuple. | 
					
						
							| 
									
										
										
										
											2012-08-08 17:57:53 +08:00
										 |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2012-08-08 17:57:51 +08:00
										 |  |  | 	t = PyTuple_New(MAX_FIELDS); | 
					
						
							|  |  |  | 	if (!t) | 
					
						
							|  |  |  | 		Py_FatalError("couldn't create Python tuple"); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-08-08 17:57:53 +08:00
										 |  |  | 	dict = PyDict_New(); | 
					
						
							|  |  |  | 	if (!dict) | 
					
						
							|  |  |  | 		Py_FatalError("couldn't create Python dictionary"); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-07-10 13:50:56 +02:00
										 |  |  | 	dict_sample = PyDict_New(); | 
					
						
							|  |  |  | 	if (!dict_sample) | 
					
						
							|  |  |  | 		Py_FatalError("couldn't create Python dictionary"); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-08-08 17:57:51 +08:00
										 |  |  | 	snprintf(handler_name, sizeof(handler_name), "%s", "process_event"); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-07-31 09:01:01 +03:00
										 |  |  | 	handler = get_handler(handler_name); | 
					
						
							|  |  |  | 	if (!handler) | 
					
						
							| 
									
										
										
										
											2012-08-08 17:57:51 +08:00
										 |  |  | 		goto exit; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-24 10:10:51 -03:00
										 |  |  | 	pydict_set_item_string_decref(dict, "ev_name", PyString_FromString(perf_evsel__name(evsel))); | 
					
						
							|  |  |  | 	pydict_set_item_string_decref(dict, "attr", PyString_FromStringAndSize( | 
					
						
							| 
									
										
										
										
											2012-08-08 17:57:53 +08:00
										 |  |  | 			(const char *)&evsel->attr, sizeof(evsel->attr))); | 
					
						
							| 
									
										
										
										
											2014-07-10 13:50:56 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	pydict_set_item_string_decref(dict_sample, "pid", | 
					
						
							|  |  |  | 			PyInt_FromLong(sample->pid)); | 
					
						
							|  |  |  | 	pydict_set_item_string_decref(dict_sample, "tid", | 
					
						
							|  |  |  | 			PyInt_FromLong(sample->tid)); | 
					
						
							|  |  |  | 	pydict_set_item_string_decref(dict_sample, "cpu", | 
					
						
							|  |  |  | 			PyInt_FromLong(sample->cpu)); | 
					
						
							|  |  |  | 	pydict_set_item_string_decref(dict_sample, "ip", | 
					
						
							|  |  |  | 			PyLong_FromUnsignedLongLong(sample->ip)); | 
					
						
							|  |  |  | 	pydict_set_item_string_decref(dict_sample, "time", | 
					
						
							|  |  |  | 			PyLong_FromUnsignedLongLong(sample->time)); | 
					
						
							|  |  |  | 	pydict_set_item_string_decref(dict_sample, "period", | 
					
						
							|  |  |  | 			PyLong_FromUnsignedLongLong(sample->period)); | 
					
						
							|  |  |  | 	pydict_set_item_string_decref(dict, "sample", dict_sample); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-24 10:10:51 -03:00
										 |  |  | 	pydict_set_item_string_decref(dict, "raw_buf", PyString_FromStringAndSize( | 
					
						
							| 
									
										
										
										
											2012-08-08 17:57:53 +08:00
										 |  |  | 			(const char *)sample->raw_data, sample->raw_size)); | 
					
						
							| 
									
										
										
										
											2013-10-24 10:10:51 -03:00
										 |  |  | 	pydict_set_item_string_decref(dict, "comm", | 
					
						
							| 
									
										
										
										
											2013-09-11 14:46:56 +02:00
										 |  |  | 			PyString_FromString(thread__comm_str(thread))); | 
					
						
							| 
									
										
										
										
											2012-08-08 17:57:53 +08:00
										 |  |  | 	if (al->map) { | 
					
						
							| 
									
										
										
										
											2013-10-24 10:10:51 -03:00
										 |  |  | 		pydict_set_item_string_decref(dict, "dso", | 
					
						
							| 
									
										
										
										
											2012-08-08 17:57:53 +08:00
										 |  |  | 			PyString_FromString(al->map->dso->name)); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if (al->sym) { | 
					
						
							| 
									
										
										
										
											2013-10-24 10:10:51 -03:00
										 |  |  | 		pydict_set_item_string_decref(dict, "symbol", | 
					
						
							| 
									
										
										
										
											2012-08-08 17:57:53 +08:00
										 |  |  | 			PyString_FromString(al->sym->name)); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2012-08-08 17:57:51 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-07-10 13:50:51 +02:00
										 |  |  | 	/* ip unwinding */ | 
					
						
							|  |  |  | 	callchain = python_process_callchain(sample, evsel, al); | 
					
						
							|  |  |  | 	pydict_set_item_string_decref(dict, "callchain", callchain); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-08-08 17:57:53 +08:00
										 |  |  | 	PyTuple_SetItem(t, n++, dict); | 
					
						
							| 
									
										
										
										
											2012-08-08 17:57:51 +08:00
										 |  |  | 	if (_PyTuple_Resize(&t, n) == -1) | 
					
						
							|  |  |  | 		Py_FatalError("error resizing Python tuple"); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-07-31 09:01:01 +03:00
										 |  |  | 	call_object(handler, t, handler_name); | 
					
						
							| 
									
										
										
										
											2012-08-08 17:57:51 +08:00
										 |  |  | exit: | 
					
						
							| 
									
										
										
										
											2012-08-08 17:57:53 +08:00
										 |  |  | 	Py_DECREF(dict); | 
					
						
							| 
									
										
										
										
											2012-08-08 17:57:51 +08:00
										 |  |  | 	Py_DECREF(t); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-12-19 16:34:52 -03:00
										 |  |  | static void python_process_event(union perf_event *event __maybe_unused, | 
					
						
							| 
									
										
										
										
											2012-08-08 17:57:51 +08:00
										 |  |  | 				 struct perf_sample *sample, | 
					
						
							|  |  |  | 				 struct perf_evsel *evsel, | 
					
						
							| 
									
										
										
										
											2013-07-18 16:06:15 -06:00
										 |  |  | 				 struct thread *thread, | 
					
						
							| 
									
										
										
										
											2012-08-08 17:57:52 +08:00
										 |  |  | 				 struct addr_location *al) | 
					
						
							| 
									
										
										
										
											2012-08-08 17:57:51 +08:00
										 |  |  | { | 
					
						
							|  |  |  | 	switch (evsel->attr.type) { | 
					
						
							|  |  |  | 	case PERF_TYPE_TRACEPOINT: | 
					
						
							| 
									
										
										
										
											2013-12-19 16:34:52 -03:00
										 |  |  | 		python_process_tracepoint(sample, evsel, thread, al); | 
					
						
							| 
									
										
										
										
											2012-08-08 17:57:51 +08:00
										 |  |  | 		break; | 
					
						
							|  |  |  | 	/* Reserve for future process_hw/sw/raw APIs */ | 
					
						
							|  |  |  | 	default: | 
					
						
							| 
									
										
										
										
											2013-12-19 16:34:52 -03:00
										 |  |  | 		python_process_general_event(sample, evsel, thread, al); | 
					
						
							| 
									
										
										
										
											2012-08-08 17:57:51 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-01-27 02:27:57 -06:00
										 |  |  | static int run_start_sub(void) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	main_module = PyImport_AddModule("__main__"); | 
					
						
							|  |  |  | 	if (main_module == NULL) | 
					
						
							|  |  |  | 		return -1; | 
					
						
							|  |  |  | 	Py_INCREF(main_module); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	main_dict = PyModule_GetDict(main_module); | 
					
						
							| 
									
										
										
										
											2014-07-31 09:01:01 +03:00
										 |  |  | 	if (main_dict == NULL) | 
					
						
							| 
									
										
										
										
											2010-01-27 02:27:57 -06:00
										 |  |  | 		goto error; | 
					
						
							|  |  |  | 	Py_INCREF(main_dict); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-07-31 09:01:01 +03:00
										 |  |  | 	try_call_object("trace_begin", NULL); | 
					
						
							| 
									
										
										
										
											2010-01-27 02:27:57 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-07-31 09:01:01 +03:00
										 |  |  | 	return 0; | 
					
						
							| 
									
										
										
										
											2010-01-27 02:27:57 -06:00
										 |  |  | 
 | 
					
						
							|  |  |  | error: | 
					
						
							|  |  |  | 	Py_XDECREF(main_dict); | 
					
						
							|  |  |  | 	Py_XDECREF(main_module); | 
					
						
							| 
									
										
										
										
											2014-07-31 09:01:01 +03:00
										 |  |  | 	return -1; | 
					
						
							| 
									
										
										
										
											2010-01-27 02:27:57 -06:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /*
 | 
					
						
							|  |  |  |  * Start trace script | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | static int python_start_script(const char *script, int argc, const char **argv) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	const char **command_line; | 
					
						
							|  |  |  | 	char buf[PATH_MAX]; | 
					
						
							|  |  |  | 	int i, err = 0; | 
					
						
							|  |  |  | 	FILE *fp; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	command_line = malloc((argc + 1) * sizeof(const char *)); | 
					
						
							|  |  |  | 	command_line[0] = script; | 
					
						
							|  |  |  | 	for (i = 1; i < argc + 1; i++) | 
					
						
							|  |  |  | 		command_line[i] = argv[i - 1]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	Py_Initialize(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	initperf_trace_context(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	PySys_SetArgv(argc + 1, (char **)command_line); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	fp = fopen(script, "r"); | 
					
						
							|  |  |  | 	if (!fp) { | 
					
						
							|  |  |  | 		sprintf(buf, "Can't open python script \"%s\"", script); | 
					
						
							|  |  |  | 		perror(buf); | 
					
						
							|  |  |  | 		err = -1; | 
					
						
							|  |  |  | 		goto error; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	err = PyRun_SimpleFile(fp, script); | 
					
						
							|  |  |  | 	if (err) { | 
					
						
							|  |  |  | 		fprintf(stderr, "Error running python script %s\n", script); | 
					
						
							|  |  |  | 		goto error; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	err = run_start_sub(); | 
					
						
							|  |  |  | 	if (err) { | 
					
						
							|  |  |  | 		fprintf(stderr, "Error starting python script %s\n", script); | 
					
						
							|  |  |  | 		goto error; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	free(command_line); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return err; | 
					
						
							|  |  |  | error: | 
					
						
							|  |  |  | 	Py_Finalize(); | 
					
						
							|  |  |  | 	free(command_line); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return err; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-08-15 22:08:37 +03:00
										 |  |  | static int python_flush_script(void) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-01-27 02:27:57 -06:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * Stop trace script | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | static int python_stop_script(void) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2014-07-31 09:01:01 +03:00
										 |  |  | 	try_call_object("trace_end", NULL); | 
					
						
							| 
									
										
										
										
											2010-01-27 02:27:57 -06:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	Py_XDECREF(main_dict); | 
					
						
							|  |  |  | 	Py_XDECREF(main_module); | 
					
						
							|  |  |  | 	Py_Finalize(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-07-31 09:01:01 +03:00
										 |  |  | 	return 0; | 
					
						
							| 
									
										
										
										
											2010-01-27 02:27:57 -06:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-06-27 13:08:42 -03:00
										 |  |  | static int python_generate_script(struct pevent *pevent, const char *outfile) | 
					
						
							| 
									
										
										
										
											2010-01-27 02:27:57 -06:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2012-04-06 00:47:56 +02:00
										 |  |  | 	struct event_format *event = NULL; | 
					
						
							| 
									
										
										
										
											2010-01-27 02:27:57 -06:00
										 |  |  | 	struct format_field *f; | 
					
						
							|  |  |  | 	char fname[PATH_MAX]; | 
					
						
							|  |  |  | 	int not_first, count; | 
					
						
							|  |  |  | 	FILE *ofp; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	sprintf(fname, "%s.py", outfile); | 
					
						
							|  |  |  | 	ofp = fopen(fname, "w"); | 
					
						
							|  |  |  | 	if (ofp == NULL) { | 
					
						
							|  |  |  | 		fprintf(stderr, "couldn't open %s\n", fname); | 
					
						
							|  |  |  | 		return -1; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2010-11-16 18:45:39 +01:00
										 |  |  | 	fprintf(ofp, "# perf script event handlers, " | 
					
						
							|  |  |  | 		"generated by perf script -g python\n"); | 
					
						
							| 
									
										
										
										
											2010-01-27 02:27:57 -06:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	fprintf(ofp, "# Licensed under the terms of the GNU GPL" | 
					
						
							|  |  |  | 		" License version 2\n\n"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	fprintf(ofp, "# The common_* event handler fields are the most useful " | 
					
						
							|  |  |  | 		"fields common to\n"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	fprintf(ofp, "# all events.  They don't necessarily correspond to " | 
					
						
							|  |  |  | 		"the 'common_*' fields\n"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	fprintf(ofp, "# in the format files.  Those fields not available as " | 
					
						
							|  |  |  | 		"handler params can\n"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	fprintf(ofp, "# be retrieved using Python functions of the form " | 
					
						
							|  |  |  | 		"common_*(context).\n"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	fprintf(ofp, "# See the perf-trace-python Documentation for the list " | 
					
						
							|  |  |  | 		"of available functions.\n\n"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	fprintf(ofp, "import os\n"); | 
					
						
							|  |  |  | 	fprintf(ofp, "import sys\n\n"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	fprintf(ofp, "sys.path.append(os.environ['PERF_EXEC_PATH'] + \\\n"); | 
					
						
							|  |  |  | 	fprintf(ofp, "\t'/scripts/python/Perf-Trace-Util/lib/Perf/Trace')\n"); | 
					
						
							|  |  |  | 	fprintf(ofp, "\nfrom perf_trace_context import *\n"); | 
					
						
							|  |  |  | 	fprintf(ofp, "from Core import *\n\n\n"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	fprintf(ofp, "def trace_begin():\n"); | 
					
						
							|  |  |  | 	fprintf(ofp, "\tprint \"in trace_begin\"\n\n"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	fprintf(ofp, "def trace_end():\n"); | 
					
						
							|  |  |  | 	fprintf(ofp, "\tprint \"in trace_end\"\n\n"); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-06-27 13:08:42 -03:00
										 |  |  | 	while ((event = trace_find_next_event(pevent, event))) { | 
					
						
							| 
									
										
										
										
											2010-01-27 02:27:57 -06:00
										 |  |  | 		fprintf(ofp, "def %s__%s(", event->system, event->name); | 
					
						
							|  |  |  | 		fprintf(ofp, "event_name, "); | 
					
						
							|  |  |  | 		fprintf(ofp, "context, "); | 
					
						
							|  |  |  | 		fprintf(ofp, "common_cpu,\n"); | 
					
						
							|  |  |  | 		fprintf(ofp, "\tcommon_secs, "); | 
					
						
							|  |  |  | 		fprintf(ofp, "common_nsecs, "); | 
					
						
							|  |  |  | 		fprintf(ofp, "common_pid, "); | 
					
						
							|  |  |  | 		fprintf(ofp, "common_comm,\n\t"); | 
					
						
							| 
									
										
										
										
											2014-07-10 13:50:51 +02:00
										 |  |  | 		fprintf(ofp, "common_callchain, "); | 
					
						
							| 
									
										
										
										
											2010-01-27 02:27:57 -06:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		not_first = 0; | 
					
						
							|  |  |  | 		count = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		for (f = event->format.fields; f; f = f->next) { | 
					
						
							|  |  |  | 			if (not_first++) | 
					
						
							|  |  |  | 				fprintf(ofp, ", "); | 
					
						
							|  |  |  | 			if (++count % 5 == 0) | 
					
						
							|  |  |  | 				fprintf(ofp, "\n\t"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			fprintf(ofp, "%s", f->name); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		fprintf(ofp, "):\n"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		fprintf(ofp, "\t\tprint_header(event_name, common_cpu, " | 
					
						
							|  |  |  | 			"common_secs, common_nsecs,\n\t\t\t" | 
					
						
							|  |  |  | 			"common_pid, common_comm)\n\n"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		fprintf(ofp, "\t\tprint \""); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		not_first = 0; | 
					
						
							|  |  |  | 		count = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		for (f = event->format.fields; f; f = f->next) { | 
					
						
							|  |  |  | 			if (not_first++) | 
					
						
							|  |  |  | 				fprintf(ofp, ", "); | 
					
						
							|  |  |  | 			if (count && count % 3 == 0) { | 
					
						
							|  |  |  | 				fprintf(ofp, "\" \\\n\t\t\""); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			count++; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			fprintf(ofp, "%s=", f->name); | 
					
						
							|  |  |  | 			if (f->flags & FIELD_IS_STRING || | 
					
						
							|  |  |  | 			    f->flags & FIELD_IS_FLAG || | 
					
						
							| 
									
										
										
										
											2014-05-29 13:44:55 +09:00
										 |  |  | 			    f->flags & FIELD_IS_ARRAY || | 
					
						
							| 
									
										
										
										
											2010-01-27 02:27:57 -06:00
										 |  |  | 			    f->flags & FIELD_IS_SYMBOLIC) | 
					
						
							|  |  |  | 				fprintf(ofp, "%%s"); | 
					
						
							|  |  |  | 			else if (f->flags & FIELD_IS_SIGNED) | 
					
						
							|  |  |  | 				fprintf(ofp, "%%d"); | 
					
						
							|  |  |  | 			else | 
					
						
							|  |  |  | 				fprintf(ofp, "%%u"); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-07-10 13:50:51 +02:00
										 |  |  | 		fprintf(ofp, "\" %% \\\n\t\t("); | 
					
						
							| 
									
										
										
										
											2010-01-27 02:27:57 -06:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		not_first = 0; | 
					
						
							|  |  |  | 		count = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		for (f = event->format.fields; f; f = f->next) { | 
					
						
							|  |  |  | 			if (not_first++) | 
					
						
							|  |  |  | 				fprintf(ofp, ", "); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			if (++count % 5 == 0) | 
					
						
							|  |  |  | 				fprintf(ofp, "\n\t\t"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			if (f->flags & FIELD_IS_FLAG) { | 
					
						
							|  |  |  | 				if ((count - 1) % 5 != 0) { | 
					
						
							|  |  |  | 					fprintf(ofp, "\n\t\t"); | 
					
						
							|  |  |  | 					count = 4; | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 				fprintf(ofp, "flag_str(\""); | 
					
						
							|  |  |  | 				fprintf(ofp, "%s__%s\", ", event->system, | 
					
						
							|  |  |  | 					event->name); | 
					
						
							|  |  |  | 				fprintf(ofp, "\"%s\", %s)", f->name, | 
					
						
							|  |  |  | 					f->name); | 
					
						
							|  |  |  | 			} else if (f->flags & FIELD_IS_SYMBOLIC) { | 
					
						
							|  |  |  | 				if ((count - 1) % 5 != 0) { | 
					
						
							|  |  |  | 					fprintf(ofp, "\n\t\t"); | 
					
						
							|  |  |  | 					count = 4; | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 				fprintf(ofp, "symbol_str(\""); | 
					
						
							|  |  |  | 				fprintf(ofp, "%s__%s\", ", event->system, | 
					
						
							|  |  |  | 					event->name); | 
					
						
							|  |  |  | 				fprintf(ofp, "\"%s\", %s)", f->name, | 
					
						
							|  |  |  | 					f->name); | 
					
						
							|  |  |  | 			} else | 
					
						
							|  |  |  | 				fprintf(ofp, "%s", f->name); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-07-10 13:50:51 +02:00
										 |  |  | 		fprintf(ofp, ")\n\n"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		fprintf(ofp, "\t\tfor node in common_callchain:"); | 
					
						
							|  |  |  | 		fprintf(ofp, "\n\t\t\tif 'sym' in node:"); | 
					
						
							|  |  |  | 		fprintf(ofp, "\n\t\t\t\tprint \"\\t[%%x] %%s\" %% (node['ip'], node['sym']['name'])"); | 
					
						
							|  |  |  | 		fprintf(ofp, "\n\t\t\telse:"); | 
					
						
							|  |  |  | 		fprintf(ofp, "\n\t\t\t\tprint \"\t[%%x]\" %% (node['ip'])\n\n"); | 
					
						
							|  |  |  | 		fprintf(ofp, "\t\tprint \"\\n\"\n\n"); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-01-27 02:27:57 -06:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	fprintf(ofp, "def trace_unhandled(event_name, context, " | 
					
						
							| 
									
										
										
										
											2010-05-31 23:12:09 +02:00
										 |  |  | 		"event_fields_dict):\n"); | 
					
						
							| 
									
										
										
										
											2010-01-27 02:27:57 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-05-31 23:12:09 +02:00
										 |  |  | 	fprintf(ofp, "\t\tprint ' '.join(['%%s=%%s'%%(k,str(v))" | 
					
						
							|  |  |  | 		"for k,v in sorted(event_fields_dict.items())])\n\n"); | 
					
						
							| 
									
										
										
										
											2010-01-27 02:27:57 -06:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	fprintf(ofp, "def print_header(" | 
					
						
							|  |  |  | 		"event_name, cpu, secs, nsecs, pid, comm):\n" | 
					
						
							|  |  |  | 		"\tprint \"%%-20s %%5u %%05u.%%09u %%8u %%-20s \" %% \\\n\t" | 
					
						
							|  |  |  | 		"(event_name, cpu, secs, nsecs, pid, comm),\n"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	fclose(ofp); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	fprintf(stderr, "generated Python script: %s\n", fname); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | struct scripting_ops python_scripting_ops = { | 
					
						
							|  |  |  | 	.name = "Python", | 
					
						
							|  |  |  | 	.start_script = python_start_script, | 
					
						
							| 
									
										
										
										
											2014-08-15 22:08:37 +03:00
										 |  |  | 	.flush_script = python_flush_script, | 
					
						
							| 
									
										
										
										
											2010-01-27 02:27:57 -06:00
										 |  |  | 	.stop_script = python_stop_script, | 
					
						
							|  |  |  | 	.process_event = python_process_event, | 
					
						
							|  |  |  | 	.generate_script = python_generate_script, | 
					
						
							|  |  |  | }; |