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