xref: /linux/tools/perf/util/scripting-engines/trace-event-python.c (revision be3de80dc2e671d9ee15e69fe9cd84d2b71e2225)
1 /*
2  * trace-event-python.  Feed trace events to an embedded Python interpreter.
3  *
4  * Copyright (C) 2010 Tom Zanussi <tzanussi@gmail.com>
5  *
6  *  This program is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation; either version 2 of the License, or
9  *  (at your option) any later version.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, write to the Free Software
18  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  */
21 
22 #include <Python.h>
23 
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <ctype.h>
28 #include <errno.h>
29 
30 #include "../../perf.h"
31 #include "../util.h"
32 #include "../event.h"
33 #include "../thread.h"
34 #include "../trace-event.h"
35 
36 PyMODINIT_FUNC initperf_trace_context(void);
37 
38 #define FTRACE_MAX_EVENT				\
39 	((1 << (sizeof(unsigned short) * 8)) - 1)
40 
41 struct event *events[FTRACE_MAX_EVENT];
42 
43 #define MAX_FIELDS	64
44 #define N_COMMON_FIELDS	7
45 
46 extern struct scripting_context *scripting_context;
47 
48 static char *cur_field_name;
49 static int zero_flag_atom;
50 
51 static PyObject *main_module, *main_dict;
52 
53 static void handler_call_die(const char *handler_name)
54 {
55 	PyErr_Print();
56 	Py_FatalError("problem in Python trace event handler");
57 }
58 
59 static void define_value(enum print_arg_type field_type,
60 			 const char *ev_name,
61 			 const char *field_name,
62 			 const char *field_value,
63 			 const char *field_str)
64 {
65 	const char *handler_name = "define_flag_value";
66 	PyObject *handler, *t, *retval;
67 	unsigned long long value;
68 	unsigned n = 0;
69 
70 	if (field_type == PRINT_SYMBOL)
71 		handler_name = "define_symbolic_value";
72 
73 	t = PyTuple_New(4);
74 	if (!t)
75 		Py_FatalError("couldn't create Python tuple");
76 
77 	value = eval_flag(field_value);
78 
79 	PyTuple_SetItem(t, n++, PyString_FromString(ev_name));
80 	PyTuple_SetItem(t, n++, PyString_FromString(field_name));
81 	PyTuple_SetItem(t, n++, PyInt_FromLong(value));
82 	PyTuple_SetItem(t, n++, PyString_FromString(field_str));
83 
84 	handler = PyDict_GetItemString(main_dict, handler_name);
85 	if (handler && PyCallable_Check(handler)) {
86 		retval = PyObject_CallObject(handler, t);
87 		if (retval == NULL)
88 			handler_call_die(handler_name);
89 	}
90 
91 	Py_DECREF(t);
92 }
93 
94 static void define_values(enum print_arg_type field_type,
95 			  struct print_flag_sym *field,
96 			  const char *ev_name,
97 			  const char *field_name)
98 {
99 	define_value(field_type, ev_name, field_name, field->value,
100 		     field->str);
101 
102 	if (field->next)
103 		define_values(field_type, field->next, ev_name, field_name);
104 }
105 
106 static void define_field(enum print_arg_type field_type,
107 			 const char *ev_name,
108 			 const char *field_name,
109 			 const char *delim)
110 {
111 	const char *handler_name = "define_flag_field";
112 	PyObject *handler, *t, *retval;
113 	unsigned n = 0;
114 
115 	if (field_type == PRINT_SYMBOL)
116 		handler_name = "define_symbolic_field";
117 
118 	if (field_type == PRINT_FLAGS)
119 		t = PyTuple_New(3);
120 	else
121 		t = PyTuple_New(2);
122 	if (!t)
123 		Py_FatalError("couldn't create Python tuple");
124 
125 	PyTuple_SetItem(t, n++, PyString_FromString(ev_name));
126 	PyTuple_SetItem(t, n++, PyString_FromString(field_name));
127 	if (field_type == PRINT_FLAGS)
128 		PyTuple_SetItem(t, n++, PyString_FromString(delim));
129 
130 	handler = PyDict_GetItemString(main_dict, handler_name);
131 	if (handler && PyCallable_Check(handler)) {
132 		retval = PyObject_CallObject(handler, t);
133 		if (retval == NULL)
134 			handler_call_die(handler_name);
135 	}
136 
137 	Py_DECREF(t);
138 }
139 
140 static void define_event_symbols(struct event *event,
141 				 const char *ev_name,
142 				 struct print_arg *args)
143 {
144 	switch (args->type) {
145 	case PRINT_NULL:
146 		break;
147 	case PRINT_ATOM:
148 		define_value(PRINT_FLAGS, ev_name, cur_field_name, "0",
149 			     args->atom.atom);
150 		zero_flag_atom = 0;
151 		break;
152 	case PRINT_FIELD:
153 		if (cur_field_name)
154 			free(cur_field_name);
155 		cur_field_name = strdup(args->field.name);
156 		break;
157 	case PRINT_FLAGS:
158 		define_event_symbols(event, ev_name, args->flags.field);
159 		define_field(PRINT_FLAGS, ev_name, cur_field_name,
160 			     args->flags.delim);
161 		define_values(PRINT_FLAGS, args->flags.flags, ev_name,
162 			      cur_field_name);
163 		break;
164 	case PRINT_SYMBOL:
165 		define_event_symbols(event, ev_name, args->symbol.field);
166 		define_field(PRINT_SYMBOL, ev_name, cur_field_name, NULL);
167 		define_values(PRINT_SYMBOL, args->symbol.symbols, ev_name,
168 			      cur_field_name);
169 		break;
170 	case PRINT_STRING:
171 		break;
172 	case PRINT_TYPE:
173 		define_event_symbols(event, ev_name, args->typecast.item);
174 		break;
175 	case PRINT_OP:
176 		if (strcmp(args->op.op, ":") == 0)
177 			zero_flag_atom = 1;
178 		define_event_symbols(event, ev_name, args->op.left);
179 		define_event_symbols(event, ev_name, args->op.right);
180 		break;
181 	default:
182 		/* we should warn... */
183 		return;
184 	}
185 
186 	if (args->next)
187 		define_event_symbols(event, ev_name, args->next);
188 }
189 
190 static inline struct event *find_cache_event(int type)
191 {
192 	static char ev_name[256];
193 	struct event *event;
194 
195 	if (events[type])
196 		return events[type];
197 
198 	events[type] = event = trace_find_event(type);
199 	if (!event)
200 		return NULL;
201 
202 	sprintf(ev_name, "%s__%s", event->system, event->name);
203 
204 	define_event_symbols(event, ev_name, event->print_fmt.args);
205 
206 	return event;
207 }
208 
209 static void python_process_event(union perf_event *pevent __unused,
210 				 struct perf_sample *sample,
211 				 struct perf_evsel *evsel __unused,
212 				 struct machine *machine __unused,
213 				 struct thread *thread)
214 {
215 	PyObject *handler, *retval, *context, *t, *obj, *dict = NULL;
216 	static char handler_name[256];
217 	struct format_field *field;
218 	unsigned long long val;
219 	unsigned long s, ns;
220 	struct event *event;
221 	unsigned n = 0;
222 	int type;
223 	int pid;
224 	int cpu = sample->cpu;
225 	void *data = sample->raw_data;
226 	unsigned long long nsecs = sample->time;
227 	char *comm = thread->comm;
228 
229 	t = PyTuple_New(MAX_FIELDS);
230 	if (!t)
231 		Py_FatalError("couldn't create Python tuple");
232 
233 	type = trace_parse_common_type(data);
234 
235 	event = find_cache_event(type);
236 	if (!event)
237 		die("ug! no event found for type %d", type);
238 
239 	pid = trace_parse_common_pid(data);
240 
241 	sprintf(handler_name, "%s__%s", event->system, event->name);
242 
243 	handler = PyDict_GetItemString(main_dict, handler_name);
244 	if (handler && !PyCallable_Check(handler))
245 		handler = NULL;
246 	if (!handler) {
247 		dict = PyDict_New();
248 		if (!dict)
249 			Py_FatalError("couldn't create Python dict");
250 	}
251 	s = nsecs / NSECS_PER_SEC;
252 	ns = nsecs - s * NSECS_PER_SEC;
253 
254 	scripting_context->event_data = data;
255 
256 	context = PyCObject_FromVoidPtr(scripting_context, NULL);
257 
258 	PyTuple_SetItem(t, n++, PyString_FromString(handler_name));
259 	PyTuple_SetItem(t, n++, context);
260 
261 	if (handler) {
262 		PyTuple_SetItem(t, n++, PyInt_FromLong(cpu));
263 		PyTuple_SetItem(t, n++, PyInt_FromLong(s));
264 		PyTuple_SetItem(t, n++, PyInt_FromLong(ns));
265 		PyTuple_SetItem(t, n++, PyInt_FromLong(pid));
266 		PyTuple_SetItem(t, n++, PyString_FromString(comm));
267 	} else {
268 		PyDict_SetItemString(dict, "common_cpu", PyInt_FromLong(cpu));
269 		PyDict_SetItemString(dict, "common_s", PyInt_FromLong(s));
270 		PyDict_SetItemString(dict, "common_ns", PyInt_FromLong(ns));
271 		PyDict_SetItemString(dict, "common_pid", PyInt_FromLong(pid));
272 		PyDict_SetItemString(dict, "common_comm", PyString_FromString(comm));
273 	}
274 	for (field = event->format.fields; field; field = field->next) {
275 		if (field->flags & FIELD_IS_STRING) {
276 			int offset;
277 			if (field->flags & FIELD_IS_DYNAMIC) {
278 				offset = *(int *)(data + field->offset);
279 				offset &= 0xffff;
280 			} else
281 				offset = field->offset;
282 			obj = PyString_FromString((char *)data + offset);
283 		} else { /* FIELD_IS_NUMERIC */
284 			val = read_size(data + field->offset, field->size);
285 			if (field->flags & FIELD_IS_SIGNED) {
286 				if ((long long)val >= LONG_MIN &&
287 				    (long long)val <= LONG_MAX)
288 					obj = PyInt_FromLong(val);
289 				else
290 					obj = PyLong_FromLongLong(val);
291 			} else {
292 				if (val <= LONG_MAX)
293 					obj = PyInt_FromLong(val);
294 				else
295 					obj = PyLong_FromUnsignedLongLong(val);
296 			}
297 		}
298 		if (handler)
299 			PyTuple_SetItem(t, n++, obj);
300 		else
301 			PyDict_SetItemString(dict, field->name, obj);
302 
303 	}
304 	if (!handler)
305 		PyTuple_SetItem(t, n++, dict);
306 
307 	if (_PyTuple_Resize(&t, n) == -1)
308 		Py_FatalError("error resizing Python tuple");
309 
310 	if (handler) {
311 		retval = PyObject_CallObject(handler, t);
312 		if (retval == NULL)
313 			handler_call_die(handler_name);
314 	} else {
315 		handler = PyDict_GetItemString(main_dict, "trace_unhandled");
316 		if (handler && PyCallable_Check(handler)) {
317 
318 			retval = PyObject_CallObject(handler, t);
319 			if (retval == NULL)
320 				handler_call_die("trace_unhandled");
321 		}
322 		Py_DECREF(dict);
323 	}
324 
325 	Py_DECREF(t);
326 }
327 
328 static int run_start_sub(void)
329 {
330 	PyObject *handler, *retval;
331 	int err = 0;
332 
333 	main_module = PyImport_AddModule("__main__");
334 	if (main_module == NULL)
335 		return -1;
336 	Py_INCREF(main_module);
337 
338 	main_dict = PyModule_GetDict(main_module);
339 	if (main_dict == NULL) {
340 		err = -1;
341 		goto error;
342 	}
343 	Py_INCREF(main_dict);
344 
345 	handler = PyDict_GetItemString(main_dict, "trace_begin");
346 	if (handler == NULL || !PyCallable_Check(handler))
347 		goto out;
348 
349 	retval = PyObject_CallObject(handler, NULL);
350 	if (retval == NULL)
351 		handler_call_die("trace_begin");
352 
353 	Py_DECREF(retval);
354 	return err;
355 error:
356 	Py_XDECREF(main_dict);
357 	Py_XDECREF(main_module);
358 out:
359 	return err;
360 }
361 
362 /*
363  * Start trace script
364  */
365 static int python_start_script(const char *script, int argc, const char **argv)
366 {
367 	const char **command_line;
368 	char buf[PATH_MAX];
369 	int i, err = 0;
370 	FILE *fp;
371 
372 	command_line = malloc((argc + 1) * sizeof(const char *));
373 	command_line[0] = script;
374 	for (i = 1; i < argc + 1; i++)
375 		command_line[i] = argv[i - 1];
376 
377 	Py_Initialize();
378 
379 	initperf_trace_context();
380 
381 	PySys_SetArgv(argc + 1, (char **)command_line);
382 
383 	fp = fopen(script, "r");
384 	if (!fp) {
385 		sprintf(buf, "Can't open python script \"%s\"", script);
386 		perror(buf);
387 		err = -1;
388 		goto error;
389 	}
390 
391 	err = PyRun_SimpleFile(fp, script);
392 	if (err) {
393 		fprintf(stderr, "Error running python script %s\n", script);
394 		goto error;
395 	}
396 
397 	err = run_start_sub();
398 	if (err) {
399 		fprintf(stderr, "Error starting python script %s\n", script);
400 		goto error;
401 	}
402 
403 	free(command_line);
404 
405 	return err;
406 error:
407 	Py_Finalize();
408 	free(command_line);
409 
410 	return err;
411 }
412 
413 /*
414  * Stop trace script
415  */
416 static int python_stop_script(void)
417 {
418 	PyObject *handler, *retval;
419 	int err = 0;
420 
421 	handler = PyDict_GetItemString(main_dict, "trace_end");
422 	if (handler == NULL || !PyCallable_Check(handler))
423 		goto out;
424 
425 	retval = PyObject_CallObject(handler, NULL);
426 	if (retval == NULL)
427 		handler_call_die("trace_end");
428 	else
429 		Py_DECREF(retval);
430 out:
431 	Py_XDECREF(main_dict);
432 	Py_XDECREF(main_module);
433 	Py_Finalize();
434 
435 	return err;
436 }
437 
438 static int python_generate_script(const char *outfile)
439 {
440 	struct event *event = NULL;
441 	struct format_field *f;
442 	char fname[PATH_MAX];
443 	int not_first, count;
444 	FILE *ofp;
445 
446 	sprintf(fname, "%s.py", outfile);
447 	ofp = fopen(fname, "w");
448 	if (ofp == NULL) {
449 		fprintf(stderr, "couldn't open %s\n", fname);
450 		return -1;
451 	}
452 	fprintf(ofp, "# perf script event handlers, "
453 		"generated by perf script -g python\n");
454 
455 	fprintf(ofp, "# Licensed under the terms of the GNU GPL"
456 		" License version 2\n\n");
457 
458 	fprintf(ofp, "# The common_* event handler fields are the most useful "
459 		"fields common to\n");
460 
461 	fprintf(ofp, "# all events.  They don't necessarily correspond to "
462 		"the 'common_*' fields\n");
463 
464 	fprintf(ofp, "# in the format files.  Those fields not available as "
465 		"handler params can\n");
466 
467 	fprintf(ofp, "# be retrieved using Python functions of the form "
468 		"common_*(context).\n");
469 
470 	fprintf(ofp, "# See the perf-trace-python Documentation for the list "
471 		"of available functions.\n\n");
472 
473 	fprintf(ofp, "import os\n");
474 	fprintf(ofp, "import sys\n\n");
475 
476 	fprintf(ofp, "sys.path.append(os.environ['PERF_EXEC_PATH'] + \\\n");
477 	fprintf(ofp, "\t'/scripts/python/Perf-Trace-Util/lib/Perf/Trace')\n");
478 	fprintf(ofp, "\nfrom perf_trace_context import *\n");
479 	fprintf(ofp, "from Core import *\n\n\n");
480 
481 	fprintf(ofp, "def trace_begin():\n");
482 	fprintf(ofp, "\tprint \"in trace_begin\"\n\n");
483 
484 	fprintf(ofp, "def trace_end():\n");
485 	fprintf(ofp, "\tprint \"in trace_end\"\n\n");
486 
487 	while ((event = trace_find_next_event(event))) {
488 		fprintf(ofp, "def %s__%s(", event->system, event->name);
489 		fprintf(ofp, "event_name, ");
490 		fprintf(ofp, "context, ");
491 		fprintf(ofp, "common_cpu,\n");
492 		fprintf(ofp, "\tcommon_secs, ");
493 		fprintf(ofp, "common_nsecs, ");
494 		fprintf(ofp, "common_pid, ");
495 		fprintf(ofp, "common_comm,\n\t");
496 
497 		not_first = 0;
498 		count = 0;
499 
500 		for (f = event->format.fields; f; f = f->next) {
501 			if (not_first++)
502 				fprintf(ofp, ", ");
503 			if (++count % 5 == 0)
504 				fprintf(ofp, "\n\t");
505 
506 			fprintf(ofp, "%s", f->name);
507 		}
508 		fprintf(ofp, "):\n");
509 
510 		fprintf(ofp, "\t\tprint_header(event_name, common_cpu, "
511 			"common_secs, common_nsecs,\n\t\t\t"
512 			"common_pid, common_comm)\n\n");
513 
514 		fprintf(ofp, "\t\tprint \"");
515 
516 		not_first = 0;
517 		count = 0;
518 
519 		for (f = event->format.fields; f; f = f->next) {
520 			if (not_first++)
521 				fprintf(ofp, ", ");
522 			if (count && count % 3 == 0) {
523 				fprintf(ofp, "\" \\\n\t\t\"");
524 			}
525 			count++;
526 
527 			fprintf(ofp, "%s=", f->name);
528 			if (f->flags & FIELD_IS_STRING ||
529 			    f->flags & FIELD_IS_FLAG ||
530 			    f->flags & FIELD_IS_SYMBOLIC)
531 				fprintf(ofp, "%%s");
532 			else if (f->flags & FIELD_IS_SIGNED)
533 				fprintf(ofp, "%%d");
534 			else
535 				fprintf(ofp, "%%u");
536 		}
537 
538 		fprintf(ofp, "\\n\" %% \\\n\t\t(");
539 
540 		not_first = 0;
541 		count = 0;
542 
543 		for (f = event->format.fields; f; f = f->next) {
544 			if (not_first++)
545 				fprintf(ofp, ", ");
546 
547 			if (++count % 5 == 0)
548 				fprintf(ofp, "\n\t\t");
549 
550 			if (f->flags & FIELD_IS_FLAG) {
551 				if ((count - 1) % 5 != 0) {
552 					fprintf(ofp, "\n\t\t");
553 					count = 4;
554 				}
555 				fprintf(ofp, "flag_str(\"");
556 				fprintf(ofp, "%s__%s\", ", event->system,
557 					event->name);
558 				fprintf(ofp, "\"%s\", %s)", f->name,
559 					f->name);
560 			} else if (f->flags & FIELD_IS_SYMBOLIC) {
561 				if ((count - 1) % 5 != 0) {
562 					fprintf(ofp, "\n\t\t");
563 					count = 4;
564 				}
565 				fprintf(ofp, "symbol_str(\"");
566 				fprintf(ofp, "%s__%s\", ", event->system,
567 					event->name);
568 				fprintf(ofp, "\"%s\", %s)", f->name,
569 					f->name);
570 			} else
571 				fprintf(ofp, "%s", f->name);
572 		}
573 
574 		fprintf(ofp, "),\n\n");
575 	}
576 
577 	fprintf(ofp, "def trace_unhandled(event_name, context, "
578 		"event_fields_dict):\n");
579 
580 	fprintf(ofp, "\t\tprint ' '.join(['%%s=%%s'%%(k,str(v))"
581 		"for k,v in sorted(event_fields_dict.items())])\n\n");
582 
583 	fprintf(ofp, "def print_header("
584 		"event_name, cpu, secs, nsecs, pid, comm):\n"
585 		"\tprint \"%%-20s %%5u %%05u.%%09u %%8u %%-20s \" %% \\\n\t"
586 		"(event_name, cpu, secs, nsecs, pid, comm),\n");
587 
588 	fclose(ofp);
589 
590 	fprintf(stderr, "generated Python script: %s\n", fname);
591 
592 	return 0;
593 }
594 
595 struct scripting_ops python_scripting_ops = {
596 	.name = "Python",
597 	.start_script = python_start_script,
598 	.stop_script = python_stop_script,
599 	.process_event = python_process_event,
600 	.generate_script = python_generate_script,
601 };
602