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