]>
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 | ||
fd20e811 | 24 | #include <inttypes.h> |
7e4b21b8 TZ |
25 | #include <stdio.h> |
26 | #include <stdlib.h> | |
27 | #include <string.h> | |
df919b40 | 28 | #include <stdbool.h> |
7e4b21b8 | 29 | #include <errno.h> |
adf5bcf3 | 30 | #include <linux/bitmap.h> |
6c346643 | 31 | #include <linux/compiler.h> |
bd48c63e | 32 | #include <linux/time64.h> |
7e4b21b8 TZ |
33 | |
34 | #include "../../perf.h" | |
84f5d36f | 35 | #include "../debug.h" |
8f651eae | 36 | #include "../callchain.h" |
fcf65bf1 | 37 | #include "../evsel.h" |
7e4b21b8 | 38 | #include "../util.h" |
743eb868 ACM |
39 | #include "../event.h" |
40 | #include "../thread.h" | |
df919b40 AH |
41 | #include "../comm.h" |
42 | #include "../machine.h" | |
43 | #include "../db-export.h" | |
6a70307d | 44 | #include "../thread-stack.h" |
7e4b21b8 | 45 | #include "../trace-event.h" |
451db126 | 46 | #include "../call-path.h" |
aef90263 JO |
47 | #include "thread_map.h" |
48 | #include "cpumap.h" | |
fea01392 | 49 | #include "print_binary.h" |
aef90263 | 50 | #include "stat.h" |
48a1f565 | 51 | #include "mem-events.h" |
7e4b21b8 | 52 | |
66dfdff0 JŠ |
53 | #if PY_MAJOR_VERSION < 3 |
54 | #define _PyUnicode_FromString(arg) \ | |
55 | PyString_FromString(arg) | |
56 | #define _PyUnicode_FromStringAndSize(arg1, arg2) \ | |
57 | PyString_FromStringAndSize((arg1), (arg2)) | |
58 | #define _PyBytes_FromStringAndSize(arg1, arg2) \ | |
59 | PyString_FromStringAndSize((arg1), (arg2)) | |
60 | #define _PyLong_FromLong(arg) \ | |
61 | PyInt_FromLong(arg) | |
62 | #define _PyLong_AsLong(arg) \ | |
63 | PyInt_AsLong(arg) | |
64 | #define _PyCapsule_New(arg1, arg2, arg3) \ | |
65 | PyCObject_FromVoidPtr((arg1), (arg2)) | |
66 | ||
7e4b21b8 | 67 | PyMODINIT_FUNC initperf_trace_context(void); |
66dfdff0 JŠ |
68 | #else |
69 | #define _PyUnicode_FromString(arg) \ | |
70 | PyUnicode_FromString(arg) | |
71 | #define _PyUnicode_FromStringAndSize(arg1, arg2) \ | |
72 | PyUnicode_FromStringAndSize((arg1), (arg2)) | |
73 | #define _PyBytes_FromStringAndSize(arg1, arg2) \ | |
74 | PyBytes_FromStringAndSize((arg1), (arg2)) | |
75 | #define _PyLong_FromLong(arg) \ | |
76 | PyLong_FromLong(arg) | |
77 | #define _PyLong_AsLong(arg) \ | |
78 | PyLong_AsLong(arg) | |
79 | #define _PyCapsule_New(arg1, arg2, arg3) \ | |
80 | PyCapsule_New((arg1), (arg2), (arg3)) | |
81 | ||
82 | PyMODINIT_FUNC PyInit_perf_trace_context(void); | |
83 | #endif | |
7e4b21b8 | 84 | |
609a7404 | 85 | #define TRACE_EVENT_TYPE_MAX \ |
7e4b21b8 TZ |
86 | ((1 << (sizeof(unsigned short) * 8)) - 1) |
87 | ||
609a7404 | 88 | static DECLARE_BITMAP(events_defined, TRACE_EVENT_TYPE_MAX); |
7e4b21b8 TZ |
89 | |
90 | #define MAX_FIELDS 64 | |
91 | #define N_COMMON_FIELDS 7 | |
92 | ||
93 | extern struct scripting_context *scripting_context; | |
94 | ||
95 | static char *cur_field_name; | |
96 | static int zero_flag_atom; | |
97 | ||
98 | static PyObject *main_module, *main_dict; | |
99 | ||
df919b40 AH |
100 | struct tables { |
101 | struct db_export dbe; | |
102 | PyObject *evsel_handler; | |
103 | PyObject *machine_handler; | |
104 | PyObject *thread_handler; | |
105 | PyObject *comm_handler; | |
106 | PyObject *comm_thread_handler; | |
107 | PyObject *dso_handler; | |
108 | PyObject *symbol_handler; | |
c29414f5 | 109 | PyObject *branch_type_handler; |
df919b40 | 110 | PyObject *sample_handler; |
6a70307d AH |
111 | PyObject *call_path_handler; |
112 | PyObject *call_return_handler; | |
df919b40 AH |
113 | bool db_export_mode; |
114 | }; | |
115 | ||
116 | static struct tables tables_global; | |
117 | ||
6c346643 | 118 | static void handler_call_die(const char *handler_name) __noreturn; |
7e4b21b8 TZ |
119 | static void handler_call_die(const char *handler_name) |
120 | { | |
121 | PyErr_Print(); | |
122 | Py_FatalError("problem in Python trace event handler"); | |
05f832e3 JS |
123 | // Py_FatalError does not return |
124 | // but we have to make the compiler happy | |
125 | abort(); | |
7e4b21b8 TZ |
126 | } |
127 | ||
c0268e8d JS |
128 | /* |
129 | * Insert val into into the dictionary and decrement the reference counter. | |
48000a1a | 130 | * This is necessary for dictionaries since PyDict_SetItemString() does not |
c0268e8d JS |
131 | * steal a reference, as opposed to PyTuple_SetItem(). |
132 | */ | |
133 | static void pydict_set_item_string_decref(PyObject *dict, const char *key, PyObject *val) | |
134 | { | |
135 | PyDict_SetItemString(dict, key, val); | |
136 | Py_DECREF(val); | |
137 | } | |
138 | ||
a5563edf AH |
139 | static PyObject *get_handler(const char *handler_name) |
140 | { | |
141 | PyObject *handler; | |
142 | ||
143 | handler = PyDict_GetItemString(main_dict, handler_name); | |
144 | if (handler && !PyCallable_Check(handler)) | |
145 | return NULL; | |
146 | return handler; | |
147 | } | |
148 | ||
f38d2816 AK |
149 | static int get_argument_count(PyObject *handler) |
150 | { | |
151 | int arg_count = 0; | |
152 | ||
153 | /* | |
154 | * The attribute for the code object is func_code in Python 2, | |
155 | * whereas it is __code__ in Python 3.0+. | |
156 | */ | |
157 | PyObject *code_obj = PyObject_GetAttrString(handler, | |
158 | "func_code"); | |
159 | if (PyErr_Occurred()) { | |
160 | PyErr_Clear(); | |
161 | code_obj = PyObject_GetAttrString(handler, | |
162 | "__code__"); | |
163 | } | |
164 | PyErr_Clear(); | |
165 | if (code_obj) { | |
166 | PyObject *arg_count_obj = PyObject_GetAttrString(code_obj, | |
167 | "co_argcount"); | |
168 | if (arg_count_obj) { | |
66dfdff0 | 169 | arg_count = (int) _PyLong_AsLong(arg_count_obj); |
f38d2816 AK |
170 | Py_DECREF(arg_count_obj); |
171 | } | |
172 | Py_DECREF(code_obj); | |
173 | } | |
174 | return arg_count; | |
175 | } | |
176 | ||
a5563edf AH |
177 | static void call_object(PyObject *handler, PyObject *args, const char *die_msg) |
178 | { | |
179 | PyObject *retval; | |
180 | ||
181 | retval = PyObject_CallObject(handler, args); | |
182 | if (retval == NULL) | |
183 | handler_call_die(die_msg); | |
184 | Py_DECREF(retval); | |
185 | } | |
186 | ||
187 | static void try_call_object(const char *handler_name, PyObject *args) | |
188 | { | |
189 | PyObject *handler; | |
190 | ||
191 | handler = get_handler(handler_name); | |
192 | if (handler) | |
193 | call_object(handler, args, handler_name); | |
194 | } | |
195 | ||
7e4b21b8 TZ |
196 | static void define_value(enum print_arg_type field_type, |
197 | const char *ev_name, | |
198 | const char *field_name, | |
199 | const char *field_value, | |
200 | const char *field_str) | |
201 | { | |
202 | const char *handler_name = "define_flag_value"; | |
a5563edf | 203 | PyObject *t; |
7e4b21b8 TZ |
204 | unsigned long long value; |
205 | unsigned n = 0; | |
206 | ||
207 | if (field_type == PRINT_SYMBOL) | |
208 | handler_name = "define_symbolic_value"; | |
209 | ||
44ad9cd8 | 210 | t = PyTuple_New(4); |
7e4b21b8 TZ |
211 | if (!t) |
212 | Py_FatalError("couldn't create Python tuple"); | |
213 | ||
214 | value = eval_flag(field_value); | |
215 | ||
66dfdff0 JŠ |
216 | PyTuple_SetItem(t, n++, _PyUnicode_FromString(ev_name)); |
217 | PyTuple_SetItem(t, n++, _PyUnicode_FromString(field_name)); | |
218 | PyTuple_SetItem(t, n++, _PyLong_FromLong(value)); | |
219 | PyTuple_SetItem(t, n++, _PyUnicode_FromString(field_str)); | |
7e4b21b8 | 220 | |
a5563edf | 221 | try_call_object(handler_name, t); |
7e4b21b8 TZ |
222 | |
223 | Py_DECREF(t); | |
224 | } | |
225 | ||
226 | static void define_values(enum print_arg_type field_type, | |
227 | struct print_flag_sym *field, | |
228 | const char *ev_name, | |
229 | const char *field_name) | |
230 | { | |
231 | define_value(field_type, ev_name, field_name, field->value, | |
232 | field->str); | |
233 | ||
234 | if (field->next) | |
235 | define_values(field_type, field->next, ev_name, field_name); | |
236 | } | |
237 | ||
238 | static void define_field(enum print_arg_type field_type, | |
239 | const char *ev_name, | |
240 | const char *field_name, | |
241 | const char *delim) | |
242 | { | |
243 | const char *handler_name = "define_flag_field"; | |
a5563edf | 244 | PyObject *t; |
7e4b21b8 TZ |
245 | unsigned n = 0; |
246 | ||
247 | if (field_type == PRINT_SYMBOL) | |
248 | handler_name = "define_symbolic_field"; | |
249 | ||
44ad9cd8 TZ |
250 | if (field_type == PRINT_FLAGS) |
251 | t = PyTuple_New(3); | |
252 | else | |
253 | t = PyTuple_New(2); | |
7e4b21b8 TZ |
254 | if (!t) |
255 | Py_FatalError("couldn't create Python tuple"); | |
256 | ||
66dfdff0 JŠ |
257 | PyTuple_SetItem(t, n++, _PyUnicode_FromString(ev_name)); |
258 | PyTuple_SetItem(t, n++, _PyUnicode_FromString(field_name)); | |
7e4b21b8 | 259 | if (field_type == PRINT_FLAGS) |
66dfdff0 | 260 | PyTuple_SetItem(t, n++, _PyUnicode_FromString(delim)); |
7e4b21b8 | 261 | |
a5563edf | 262 | try_call_object(handler_name, t); |
7e4b21b8 TZ |
263 | |
264 | Py_DECREF(t); | |
265 | } | |
266 | ||
aaf045f7 | 267 | static void define_event_symbols(struct event_format *event, |
7e4b21b8 TZ |
268 | const char *ev_name, |
269 | struct print_arg *args) | |
270 | { | |
8579aca3 TS |
271 | if (args == NULL) |
272 | return; | |
273 | ||
7e4b21b8 TZ |
274 | switch (args->type) { |
275 | case PRINT_NULL: | |
276 | break; | |
277 | case PRINT_ATOM: | |
278 | define_value(PRINT_FLAGS, ev_name, cur_field_name, "0", | |
279 | args->atom.atom); | |
280 | zero_flag_atom = 0; | |
281 | break; | |
282 | case PRINT_FIELD: | |
f5385650 | 283 | free(cur_field_name); |
7e4b21b8 TZ |
284 | cur_field_name = strdup(args->field.name); |
285 | break; | |
286 | case PRINT_FLAGS: | |
287 | define_event_symbols(event, ev_name, args->flags.field); | |
288 | define_field(PRINT_FLAGS, ev_name, cur_field_name, | |
289 | args->flags.delim); | |
290 | define_values(PRINT_FLAGS, args->flags.flags, ev_name, | |
291 | cur_field_name); | |
292 | break; | |
293 | case PRINT_SYMBOL: | |
294 | define_event_symbols(event, ev_name, args->symbol.field); | |
295 | define_field(PRINT_SYMBOL, ev_name, cur_field_name, NULL); | |
296 | define_values(PRINT_SYMBOL, args->symbol.symbols, ev_name, | |
297 | cur_field_name); | |
298 | break; | |
e080e6f1 | 299 | case PRINT_HEX: |
0fe05591 | 300 | case PRINT_HEX_STR: |
e080e6f1 NK |
301 | define_event_symbols(event, ev_name, args->hex.field); |
302 | define_event_symbols(event, ev_name, args->hex.size); | |
303 | break; | |
b839e1e8 JM |
304 | case PRINT_INT_ARRAY: |
305 | define_event_symbols(event, ev_name, args->int_array.field); | |
306 | define_event_symbols(event, ev_name, args->int_array.count); | |
307 | define_event_symbols(event, ev_name, args->int_array.el_size); | |
308 | break; | |
7e4b21b8 TZ |
309 | case PRINT_STRING: |
310 | break; | |
311 | case PRINT_TYPE: | |
312 | define_event_symbols(event, ev_name, args->typecast.item); | |
313 | break; | |
314 | case PRINT_OP: | |
315 | if (strcmp(args->op.op, ":") == 0) | |
316 | zero_flag_atom = 1; | |
317 | define_event_symbols(event, ev_name, args->op.left); | |
318 | define_event_symbols(event, ev_name, args->op.right); | |
319 | break; | |
320 | default: | |
aaf045f7 SR |
321 | /* gcc warns for these? */ |
322 | case PRINT_BSTRING: | |
323 | case PRINT_DYNAMIC_ARRAY: | |
76055940 | 324 | case PRINT_DYNAMIC_ARRAY_LEN: |
aaf045f7 | 325 | case PRINT_FUNC: |
473a778a | 326 | case PRINT_BITMASK: |
7e4b21b8 TZ |
327 | /* we should warn... */ |
328 | return; | |
329 | } | |
330 | ||
331 | if (args->next) | |
332 | define_event_symbols(event, ev_name, args->next); | |
333 | } | |
334 | ||
33058b94 SAS |
335 | static PyObject *get_field_numeric_entry(struct event_format *event, |
336 | struct format_field *field, void *data) | |
337 | { | |
8ac631cd | 338 | bool is_array = field->flags & FIELD_IS_ARRAY; |
39f54862 | 339 | PyObject *obj = NULL, *list = NULL; |
33058b94 | 340 | unsigned long long val; |
8ac631cd | 341 | unsigned int item_size, n_items, i; |
33058b94 | 342 | |
8ac631cd SAS |
343 | if (is_array) { |
344 | list = PyList_New(field->arraylen); | |
345 | item_size = field->size / field->arraylen; | |
346 | n_items = field->arraylen; | |
33058b94 | 347 | } else { |
8ac631cd SAS |
348 | item_size = field->size; |
349 | n_items = 1; | |
33058b94 | 350 | } |
8ac631cd SAS |
351 | |
352 | for (i = 0; i < n_items; i++) { | |
353 | ||
354 | val = read_size(event, data + field->offset + i * item_size, | |
355 | item_size); | |
356 | if (field->flags & FIELD_IS_SIGNED) { | |
357 | if ((long long)val >= LONG_MIN && | |
358 | (long long)val <= LONG_MAX) | |
66dfdff0 | 359 | obj = _PyLong_FromLong(val); |
8ac631cd SAS |
360 | else |
361 | obj = PyLong_FromLongLong(val); | |
362 | } else { | |
363 | if (val <= LONG_MAX) | |
66dfdff0 | 364 | obj = _PyLong_FromLong(val); |
8ac631cd SAS |
365 | else |
366 | obj = PyLong_FromUnsignedLongLong(val); | |
367 | } | |
368 | if (is_array) | |
369 | PyList_SET_ITEM(list, i, obj); | |
370 | } | |
371 | if (is_array) | |
372 | obj = list; | |
33058b94 SAS |
373 | return obj; |
374 | } | |
375 | ||
5f9e0f31 JY |
376 | static const char *get_dsoname(struct map *map) |
377 | { | |
378 | const char *dsoname = "[unknown]"; | |
379 | ||
380 | if (map && map->dso) { | |
381 | if (symbol_conf.show_kernel_path && map->dso->long_name) | |
382 | dsoname = map->dso->long_name; | |
383 | else | |
384 | dsoname = map->dso->name; | |
385 | } | |
386 | ||
387 | return dsoname; | |
388 | } | |
0f5f5bcd JS |
389 | |
390 | static PyObject *python_process_callchain(struct perf_sample *sample, | |
391 | struct perf_evsel *evsel, | |
392 | struct addr_location *al) | |
393 | { | |
394 | PyObject *pylist; | |
395 | ||
396 | pylist = PyList_New(0); | |
397 | if (!pylist) | |
398 | Py_FatalError("couldn't create Python list"); | |
399 | ||
400 | if (!symbol_conf.use_callchain || !sample->callchain) | |
401 | goto exit; | |
402 | ||
91d7b2de | 403 | if (thread__resolve_callchain(al->thread, &callchain_cursor, evsel, |
cc8b7c2b | 404 | sample, NULL, NULL, |
44cbe729 | 405 | scripting_max_stack) != 0) { |
0f5f5bcd JS |
406 | pr_err("Failed to resolve callchain. Skipping\n"); |
407 | goto exit; | |
408 | } | |
409 | callchain_cursor_commit(&callchain_cursor); | |
410 | ||
411 | ||
412 | while (1) { | |
413 | PyObject *pyelem; | |
414 | struct callchain_cursor_node *node; | |
415 | node = callchain_cursor_current(&callchain_cursor); | |
416 | if (!node) | |
417 | break; | |
418 | ||
419 | pyelem = PyDict_New(); | |
420 | if (!pyelem) | |
421 | Py_FatalError("couldn't create Python dictionary"); | |
422 | ||
423 | ||
424 | pydict_set_item_string_decref(pyelem, "ip", | |
425 | PyLong_FromUnsignedLongLong(node->ip)); | |
426 | ||
427 | if (node->sym) { | |
428 | PyObject *pysym = PyDict_New(); | |
429 | if (!pysym) | |
430 | Py_FatalError("couldn't create Python dictionary"); | |
431 | pydict_set_item_string_decref(pysym, "start", | |
432 | PyLong_FromUnsignedLongLong(node->sym->start)); | |
433 | pydict_set_item_string_decref(pysym, "end", | |
434 | PyLong_FromUnsignedLongLong(node->sym->end)); | |
435 | pydict_set_item_string_decref(pysym, "binding", | |
66dfdff0 | 436 | _PyLong_FromLong(node->sym->binding)); |
0f5f5bcd | 437 | pydict_set_item_string_decref(pysym, "name", |
66dfdff0 | 438 | _PyUnicode_FromStringAndSize(node->sym->name, |
0f5f5bcd JS |
439 | node->sym->namelen)); |
440 | pydict_set_item_string_decref(pyelem, "sym", pysym); | |
441 | } | |
442 | ||
443 | if (node->map) { | |
5f9e0f31 JY |
444 | const char *dsoname = get_dsoname(node->map); |
445 | ||
0f5f5bcd | 446 | pydict_set_item_string_decref(pyelem, "dso", |
66dfdff0 | 447 | _PyUnicode_FromString(dsoname)); |
0f5f5bcd JS |
448 | } |
449 | ||
450 | callchain_cursor_advance(&callchain_cursor); | |
451 | PyList_Append(pylist, pyelem); | |
452 | Py_DECREF(pyelem); | |
453 | } | |
454 | ||
455 | exit: | |
456 | return pylist; | |
457 | } | |
458 | ||
48a1f565 JY |
459 | static PyObject *python_process_brstack(struct perf_sample *sample, |
460 | struct thread *thread) | |
461 | { | |
462 | struct branch_stack *br = sample->branch_stack; | |
463 | PyObject *pylist; | |
464 | u64 i; | |
465 | ||
466 | pylist = PyList_New(0); | |
467 | if (!pylist) | |
468 | Py_FatalError("couldn't create Python list"); | |
469 | ||
470 | if (!(br && br->nr)) | |
471 | goto exit; | |
472 | ||
473 | for (i = 0; i < br->nr; i++) { | |
474 | PyObject *pyelem; | |
475 | struct addr_location al; | |
476 | const char *dsoname; | |
477 | ||
478 | pyelem = PyDict_New(); | |
479 | if (!pyelem) | |
480 | Py_FatalError("couldn't create Python dictionary"); | |
481 | ||
482 | pydict_set_item_string_decref(pyelem, "from", | |
483 | PyLong_FromUnsignedLongLong(br->entries[i].from)); | |
484 | pydict_set_item_string_decref(pyelem, "to", | |
485 | PyLong_FromUnsignedLongLong(br->entries[i].to)); | |
486 | pydict_set_item_string_decref(pyelem, "mispred", | |
487 | PyBool_FromLong(br->entries[i].flags.mispred)); | |
488 | pydict_set_item_string_decref(pyelem, "predicted", | |
489 | PyBool_FromLong(br->entries[i].flags.predicted)); | |
490 | pydict_set_item_string_decref(pyelem, "in_tx", | |
491 | PyBool_FromLong(br->entries[i].flags.in_tx)); | |
492 | pydict_set_item_string_decref(pyelem, "abort", | |
493 | PyBool_FromLong(br->entries[i].flags.abort)); | |
494 | pydict_set_item_string_decref(pyelem, "cycles", | |
495 | PyLong_FromUnsignedLongLong(br->entries[i].flags.cycles)); | |
496 | ||
497 | thread__find_map(thread, sample->cpumode, | |
498 | br->entries[i].from, &al); | |
499 | dsoname = get_dsoname(al.map); | |
500 | pydict_set_item_string_decref(pyelem, "from_dsoname", | |
501 | _PyUnicode_FromString(dsoname)); | |
502 | ||
503 | thread__find_map(thread, sample->cpumode, | |
504 | br->entries[i].to, &al); | |
505 | dsoname = get_dsoname(al.map); | |
506 | pydict_set_item_string_decref(pyelem, "to_dsoname", | |
507 | _PyUnicode_FromString(dsoname)); | |
508 | ||
509 | PyList_Append(pylist, pyelem); | |
510 | Py_DECREF(pyelem); | |
511 | } | |
512 | ||
513 | exit: | |
514 | return pylist; | |
515 | } | |
516 | ||
517 | static unsigned long get_offset(struct symbol *sym, struct addr_location *al) | |
518 | { | |
519 | unsigned long offset; | |
520 | ||
521 | if (al->addr < sym->end) | |
522 | offset = al->addr - sym->start; | |
523 | else | |
524 | offset = al->addr - al->map->start - sym->start; | |
525 | ||
526 | return offset; | |
527 | } | |
528 | ||
529 | static int get_symoff(struct symbol *sym, struct addr_location *al, | |
530 | bool print_off, char *bf, int size) | |
531 | { | |
532 | unsigned long offset; | |
533 | ||
534 | if (!sym || !sym->name[0]) | |
535 | return scnprintf(bf, size, "%s", "[unknown]"); | |
536 | ||
537 | if (!print_off) | |
538 | return scnprintf(bf, size, "%s", sym->name); | |
539 | ||
540 | offset = get_offset(sym, al); | |
541 | ||
542 | return scnprintf(bf, size, "%s+0x%x", sym->name, offset); | |
543 | } | |
544 | ||
545 | static int get_br_mspred(struct branch_flags *flags, char *bf, int size) | |
546 | { | |
547 | if (!flags->mispred && !flags->predicted) | |
548 | return scnprintf(bf, size, "%s", "-"); | |
549 | ||
550 | if (flags->mispred) | |
551 | return scnprintf(bf, size, "%s", "M"); | |
552 | ||
553 | return scnprintf(bf, size, "%s", "P"); | |
554 | } | |
555 | ||
556 | static PyObject *python_process_brstacksym(struct perf_sample *sample, | |
557 | struct thread *thread) | |
558 | { | |
559 | struct branch_stack *br = sample->branch_stack; | |
560 | PyObject *pylist; | |
561 | u64 i; | |
562 | char bf[512]; | |
563 | struct addr_location al; | |
564 | ||
565 | pylist = PyList_New(0); | |
566 | if (!pylist) | |
567 | Py_FatalError("couldn't create Python list"); | |
568 | ||
569 | if (!(br && br->nr)) | |
570 | goto exit; | |
571 | ||
572 | for (i = 0; i < br->nr; i++) { | |
573 | PyObject *pyelem; | |
574 | ||
575 | pyelem = PyDict_New(); | |
576 | if (!pyelem) | |
577 | Py_FatalError("couldn't create Python dictionary"); | |
578 | ||
579 | thread__find_symbol(thread, sample->cpumode, | |
580 | br->entries[i].from, &al); | |
581 | get_symoff(al.sym, &al, true, bf, sizeof(bf)); | |
582 | pydict_set_item_string_decref(pyelem, "from", | |
583 | _PyUnicode_FromString(bf)); | |
584 | ||
585 | thread__find_symbol(thread, sample->cpumode, | |
586 | br->entries[i].to, &al); | |
587 | get_symoff(al.sym, &al, true, bf, sizeof(bf)); | |
588 | pydict_set_item_string_decref(pyelem, "to", | |
589 | _PyUnicode_FromString(bf)); | |
590 | ||
591 | get_br_mspred(&br->entries[i].flags, bf, sizeof(bf)); | |
592 | pydict_set_item_string_decref(pyelem, "pred", | |
593 | _PyUnicode_FromString(bf)); | |
594 | ||
595 | if (br->entries[i].flags.in_tx) { | |
596 | pydict_set_item_string_decref(pyelem, "in_tx", | |
597 | _PyUnicode_FromString("X")); | |
598 | } else { | |
599 | pydict_set_item_string_decref(pyelem, "in_tx", | |
600 | _PyUnicode_FromString("-")); | |
601 | } | |
602 | ||
603 | if (br->entries[i].flags.abort) { | |
604 | pydict_set_item_string_decref(pyelem, "abort", | |
605 | _PyUnicode_FromString("A")); | |
606 | } else { | |
607 | pydict_set_item_string_decref(pyelem, "abort", | |
608 | _PyUnicode_FromString("-")); | |
609 | } | |
610 | ||
611 | PyList_Append(pylist, pyelem); | |
612 | Py_DECREF(pyelem); | |
613 | } | |
614 | ||
615 | exit: | |
616 | return pylist; | |
617 | } | |
618 | ||
74ec14f3 AK |
619 | static PyObject *get_sample_value_as_tuple(struct sample_read_value *value) |
620 | { | |
621 | PyObject *t; | |
622 | ||
623 | t = PyTuple_New(2); | |
624 | if (!t) | |
625 | Py_FatalError("couldn't create Python tuple"); | |
626 | PyTuple_SetItem(t, 0, PyLong_FromUnsignedLongLong(value->id)); | |
627 | PyTuple_SetItem(t, 1, PyLong_FromUnsignedLongLong(value->value)); | |
628 | return t; | |
629 | } | |
630 | ||
631 | static void set_sample_read_in_dict(PyObject *dict_sample, | |
632 | struct perf_sample *sample, | |
633 | struct perf_evsel *evsel) | |
634 | { | |
635 | u64 read_format = evsel->attr.read_format; | |
636 | PyObject *values; | |
637 | unsigned int i; | |
638 | ||
639 | if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) { | |
640 | pydict_set_item_string_decref(dict_sample, "time_enabled", | |
641 | PyLong_FromUnsignedLongLong(sample->read.time_enabled)); | |
642 | } | |
643 | ||
644 | if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) { | |
645 | pydict_set_item_string_decref(dict_sample, "time_running", | |
646 | PyLong_FromUnsignedLongLong(sample->read.time_running)); | |
647 | } | |
648 | ||
649 | if (read_format & PERF_FORMAT_GROUP) | |
650 | values = PyList_New(sample->read.group.nr); | |
651 | else | |
652 | values = PyList_New(1); | |
653 | ||
654 | if (!values) | |
655 | Py_FatalError("couldn't create Python list"); | |
656 | ||
657 | if (read_format & PERF_FORMAT_GROUP) { | |
658 | for (i = 0; i < sample->read.group.nr; i++) { | |
659 | PyObject *t = get_sample_value_as_tuple(&sample->read.group.values[i]); | |
660 | PyList_SET_ITEM(values, i, t); | |
661 | } | |
662 | } else { | |
663 | PyObject *t = get_sample_value_as_tuple(&sample->read.one); | |
664 | PyList_SET_ITEM(values, 0, t); | |
665 | } | |
666 | pydict_set_item_string_decref(dict_sample, "values", values); | |
667 | } | |
668 | ||
48a1f565 JY |
669 | static void set_sample_datasrc_in_dict(PyObject *dict, |
670 | struct perf_sample *sample) | |
671 | { | |
672 | struct mem_info mi = { .data_src.val = sample->data_src }; | |
673 | char decode[100]; | |
674 | ||
675 | pydict_set_item_string_decref(dict, "datasrc", | |
676 | PyLong_FromUnsignedLongLong(sample->data_src)); | |
677 | ||
678 | perf_script__meminfo_scnprintf(decode, 100, &mi); | |
679 | ||
680 | pydict_set_item_string_decref(dict, "datasrc_decode", | |
681 | _PyUnicode_FromString(decode)); | |
682 | } | |
683 | ||
684 | static int regs_map(struct regs_dump *regs, uint64_t mask, char *bf, int size) | |
685 | { | |
686 | unsigned int i = 0, r; | |
687 | int printed = 0; | |
688 | ||
689 | bf[0] = 0; | |
690 | ||
691 | for_each_set_bit(r, (unsigned long *) &mask, sizeof(mask) * 8) { | |
692 | u64 val = regs->regs[i++]; | |
693 | ||
694 | printed += scnprintf(bf + printed, size - printed, | |
695 | "%5s:0x%" PRIx64 " ", | |
696 | perf_reg_name(r), val); | |
697 | } | |
698 | ||
699 | return printed; | |
700 | } | |
701 | ||
702 | static void set_regs_in_dict(PyObject *dict, | |
703 | struct perf_sample *sample, | |
704 | struct perf_evsel *evsel) | |
705 | { | |
706 | struct perf_event_attr *attr = &evsel->attr; | |
707 | char bf[512]; | |
708 | ||
709 | regs_map(&sample->intr_regs, attr->sample_regs_intr, bf, sizeof(bf)); | |
710 | ||
711 | pydict_set_item_string_decref(dict, "iregs", | |
712 | _PyUnicode_FromString(bf)); | |
713 | ||
714 | regs_map(&sample->user_regs, attr->sample_regs_user, bf, sizeof(bf)); | |
715 | ||
716 | pydict_set_item_string_decref(dict, "uregs", | |
717 | _PyUnicode_FromString(bf)); | |
718 | } | |
719 | ||
892e76b2 AK |
720 | static PyObject *get_perf_sample_dict(struct perf_sample *sample, |
721 | struct perf_evsel *evsel, | |
722 | struct addr_location *al, | |
723 | PyObject *callchain) | |
724 | { | |
48a1f565 | 725 | PyObject *dict, *dict_sample, *brstack, *brstacksym; |
892e76b2 AK |
726 | |
727 | dict = PyDict_New(); | |
728 | if (!dict) | |
729 | Py_FatalError("couldn't create Python dictionary"); | |
730 | ||
731 | dict_sample = PyDict_New(); | |
732 | if (!dict_sample) | |
733 | Py_FatalError("couldn't create Python dictionary"); | |
734 | ||
66dfdff0 JŠ |
735 | pydict_set_item_string_decref(dict, "ev_name", _PyUnicode_FromString(perf_evsel__name(evsel))); |
736 | pydict_set_item_string_decref(dict, "attr", _PyUnicode_FromStringAndSize( | |
892e76b2 AK |
737 | (const char *)&evsel->attr, sizeof(evsel->attr))); |
738 | ||
739 | pydict_set_item_string_decref(dict_sample, "pid", | |
66dfdff0 | 740 | _PyLong_FromLong(sample->pid)); |
892e76b2 | 741 | pydict_set_item_string_decref(dict_sample, "tid", |
66dfdff0 | 742 | _PyLong_FromLong(sample->tid)); |
892e76b2 | 743 | pydict_set_item_string_decref(dict_sample, "cpu", |
66dfdff0 | 744 | _PyLong_FromLong(sample->cpu)); |
892e76b2 AK |
745 | pydict_set_item_string_decref(dict_sample, "ip", |
746 | PyLong_FromUnsignedLongLong(sample->ip)); | |
747 | pydict_set_item_string_decref(dict_sample, "time", | |
748 | PyLong_FromUnsignedLongLong(sample->time)); | |
749 | pydict_set_item_string_decref(dict_sample, "period", | |
750 | PyLong_FromUnsignedLongLong(sample->period)); | |
41013f0c KL |
751 | pydict_set_item_string_decref(dict_sample, "phys_addr", |
752 | PyLong_FromUnsignedLongLong(sample->phys_addr)); | |
943f32a0 LY |
753 | pydict_set_item_string_decref(dict_sample, "addr", |
754 | PyLong_FromUnsignedLongLong(sample->addr)); | |
74ec14f3 | 755 | set_sample_read_in_dict(dict_sample, sample, evsel); |
48a1f565 JY |
756 | pydict_set_item_string_decref(dict_sample, "weight", |
757 | PyLong_FromUnsignedLongLong(sample->weight)); | |
758 | pydict_set_item_string_decref(dict_sample, "transaction", | |
759 | PyLong_FromUnsignedLongLong(sample->transaction)); | |
760 | set_sample_datasrc_in_dict(dict_sample, sample); | |
892e76b2 AK |
761 | pydict_set_item_string_decref(dict, "sample", dict_sample); |
762 | ||
66dfdff0 | 763 | pydict_set_item_string_decref(dict, "raw_buf", _PyBytes_FromStringAndSize( |
892e76b2 AK |
764 | (const char *)sample->raw_data, sample->raw_size)); |
765 | pydict_set_item_string_decref(dict, "comm", | |
66dfdff0 | 766 | _PyUnicode_FromString(thread__comm_str(al->thread))); |
892e76b2 AK |
767 | if (al->map) { |
768 | pydict_set_item_string_decref(dict, "dso", | |
66dfdff0 | 769 | _PyUnicode_FromString(al->map->dso->name)); |
892e76b2 AK |
770 | } |
771 | if (al->sym) { | |
772 | pydict_set_item_string_decref(dict, "symbol", | |
66dfdff0 | 773 | _PyUnicode_FromString(al->sym->name)); |
892e76b2 AK |
774 | } |
775 | ||
776 | pydict_set_item_string_decref(dict, "callchain", callchain); | |
777 | ||
48a1f565 JY |
778 | brstack = python_process_brstack(sample, al->thread); |
779 | pydict_set_item_string_decref(dict, "brstack", brstack); | |
780 | ||
781 | brstacksym = python_process_brstacksym(sample, al->thread); | |
782 | pydict_set_item_string_decref(dict, "brstacksym", brstacksym); | |
783 | ||
784 | set_regs_in_dict(dict, sample, evsel); | |
785 | ||
892e76b2 AK |
786 | return dict; |
787 | } | |
788 | ||
b7fff6b5 ACM |
789 | static void python_process_tracepoint(struct perf_sample *sample, |
790 | struct perf_evsel *evsel, | |
b7fff6b5 | 791 | struct addr_location *al) |
7e4b21b8 | 792 | { |
adf5bcf3 | 793 | struct event_format *event = evsel->tp_format; |
39f54862 | 794 | PyObject *handler, *context, *t, *obj = NULL, *callchain; |
f38d2816 | 795 | PyObject *dict = NULL, *all_entries_dict = NULL; |
7e4b21b8 TZ |
796 | static char handler_name[256]; |
797 | struct format_field *field; | |
7e4b21b8 | 798 | unsigned long s, ns; |
7e4b21b8 | 799 | unsigned n = 0; |
7e4b21b8 | 800 | int pid; |
be6d842a DA |
801 | int cpu = sample->cpu; |
802 | void *data = sample->raw_data; | |
803 | unsigned long long nsecs = sample->time; | |
f9d5d549 | 804 | const char *comm = thread__comm_str(al->thread); |
e9f9a9ca | 805 | const char *default_handler_name = "trace_unhandled"; |
7e4b21b8 | 806 | |
62665dff ACM |
807 | if (!event) { |
808 | snprintf(handler_name, sizeof(handler_name), | |
809 | "ug! no event found for type %" PRIu64, (u64)evsel->attr.config); | |
810 | Py_FatalError(handler_name); | |
811 | } | |
7e4b21b8 | 812 | |
97822433 | 813 | pid = raw_field_value(event, "common_pid", data); |
7e4b21b8 TZ |
814 | |
815 | sprintf(handler_name, "%s__%s", event->system, event->name); | |
816 | ||
adf5bcf3 JO |
817 | if (!test_and_set_bit(event->id, events_defined)) |
818 | define_event_symbols(event, handler_name, event->print_fmt.args); | |
819 | ||
a5563edf | 820 | handler = get_handler(handler_name); |
c0251485 | 821 | if (!handler) { |
e9f9a9ca AK |
822 | handler = get_handler(default_handler_name); |
823 | if (!handler) | |
824 | return; | |
c0251485 PT |
825 | dict = PyDict_New(); |
826 | if (!dict) | |
827 | Py_FatalError("couldn't create Python dict"); | |
828 | } | |
e9f9a9ca AK |
829 | |
830 | t = PyTuple_New(MAX_FIELDS); | |
831 | if (!t) | |
832 | Py_FatalError("couldn't create Python tuple"); | |
833 | ||
834 | ||
bd48c63e ACM |
835 | s = nsecs / NSEC_PER_SEC; |
836 | ns = nsecs - s * NSEC_PER_SEC; | |
7e4b21b8 TZ |
837 | |
838 | scripting_context->event_data = data; | |
2de9533d | 839 | scripting_context->pevent = evsel->tp_format->pevent; |
7e4b21b8 | 840 | |
66dfdff0 | 841 | context = _PyCapsule_New(scripting_context, NULL, NULL); |
7e4b21b8 | 842 | |
66dfdff0 | 843 | PyTuple_SetItem(t, n++, _PyUnicode_FromString(handler_name)); |
fb7d0b3c | 844 | PyTuple_SetItem(t, n++, context); |
7e4b21b8 | 845 | |
0f5f5bcd JS |
846 | /* ip unwinding */ |
847 | callchain = python_process_callchain(sample, evsel, al); | |
f38d2816 AK |
848 | /* Need an additional reference for the perf_sample dict */ |
849 | Py_INCREF(callchain); | |
0f5f5bcd | 850 | |
e9f9a9ca | 851 | if (!dict) { |
66dfdff0 JŠ |
852 | PyTuple_SetItem(t, n++, _PyLong_FromLong(cpu)); |
853 | PyTuple_SetItem(t, n++, _PyLong_FromLong(s)); | |
854 | PyTuple_SetItem(t, n++, _PyLong_FromLong(ns)); | |
855 | PyTuple_SetItem(t, n++, _PyLong_FromLong(pid)); | |
856 | PyTuple_SetItem(t, n++, _PyUnicode_FromString(comm)); | |
0f5f5bcd | 857 | PyTuple_SetItem(t, n++, callchain); |
c0251485 | 858 | } else { |
66dfdff0 JŠ |
859 | pydict_set_item_string_decref(dict, "common_cpu", _PyLong_FromLong(cpu)); |
860 | pydict_set_item_string_decref(dict, "common_s", _PyLong_FromLong(s)); | |
861 | pydict_set_item_string_decref(dict, "common_ns", _PyLong_FromLong(ns)); | |
862 | pydict_set_item_string_decref(dict, "common_pid", _PyLong_FromLong(pid)); | |
863 | pydict_set_item_string_decref(dict, "common_comm", _PyUnicode_FromString(comm)); | |
0f5f5bcd | 864 | pydict_set_item_string_decref(dict, "common_callchain", callchain); |
c0251485 | 865 | } |
7e4b21b8 | 866 | for (field = event->format.fields; field; field = field->next) { |
249de6e0 JO |
867 | unsigned int offset, len; |
868 | unsigned long long val; | |
869 | ||
870 | if (field->flags & FIELD_IS_ARRAY) { | |
871 | offset = field->offset; | |
872 | len = field->size; | |
7e4b21b8 | 873 | if (field->flags & FIELD_IS_DYNAMIC) { |
249de6e0 JO |
874 | val = pevent_read_number(scripting_context->pevent, |
875 | data + offset, len); | |
876 | offset = val; | |
877 | len = offset >> 16; | |
7e4b21b8 | 878 | offset &= 0xffff; |
249de6e0 JO |
879 | } |
880 | if (field->flags & FIELD_IS_STRING && | |
881 | is_printable_array(data + offset, len)) { | |
66dfdff0 | 882 | obj = _PyUnicode_FromString((char *) data + offset); |
249de6e0 JO |
883 | } else { |
884 | obj = PyByteArray_FromStringAndSize((const char *) data + offset, len); | |
885 | field->flags &= ~FIELD_IS_STRING; | |
886 | } | |
7e4b21b8 | 887 | } else { /* FIELD_IS_NUMERIC */ |
33058b94 | 888 | obj = get_field_numeric_entry(event, field, data); |
7e4b21b8 | 889 | } |
e9f9a9ca | 890 | if (!dict) |
c0251485 PT |
891 | PyTuple_SetItem(t, n++, obj); |
892 | else | |
c0268e8d | 893 | pydict_set_item_string_decref(dict, field->name, obj); |
c0251485 | 894 | |
7e4b21b8 | 895 | } |
0f5f5bcd | 896 | |
e9f9a9ca | 897 | if (dict) |
c0251485 | 898 | PyTuple_SetItem(t, n++, dict); |
7e4b21b8 | 899 | |
f38d2816 AK |
900 | if (get_argument_count(handler) == (int) n + 1) { |
901 | all_entries_dict = get_perf_sample_dict(sample, evsel, al, | |
902 | callchain); | |
903 | PyTuple_SetItem(t, n++, all_entries_dict); | |
904 | } else { | |
905 | Py_DECREF(callchain); | |
906 | } | |
907 | ||
7e4b21b8 TZ |
908 | if (_PyTuple_Resize(&t, n) == -1) |
909 | Py_FatalError("error resizing Python tuple"); | |
910 | ||
e9f9a9ca | 911 | if (!dict) { |
a5563edf | 912 | call_object(handler, t, handler_name); |
7e4b21b8 | 913 | } else { |
e9f9a9ca | 914 | call_object(handler, t, default_handler_name); |
c0251485 | 915 | Py_DECREF(dict); |
7e4b21b8 TZ |
916 | } |
917 | ||
f38d2816 | 918 | Py_XDECREF(all_entries_dict); |
7e4b21b8 TZ |
919 | Py_DECREF(t); |
920 | } | |
921 | ||
df919b40 AH |
922 | static PyObject *tuple_new(unsigned int sz) |
923 | { | |
924 | PyObject *t; | |
925 | ||
926 | t = PyTuple_New(sz); | |
927 | if (!t) | |
928 | Py_FatalError("couldn't create Python tuple"); | |
929 | return t; | |
930 | } | |
931 | ||
932 | static int tuple_set_u64(PyObject *t, unsigned int pos, u64 val) | |
933 | { | |
934 | #if BITS_PER_LONG == 64 | |
66dfdff0 | 935 | return PyTuple_SetItem(t, pos, _PyLong_FromLong(val)); |
df919b40 AH |
936 | #endif |
937 | #if BITS_PER_LONG == 32 | |
938 | return PyTuple_SetItem(t, pos, PyLong_FromLongLong(val)); | |
939 | #endif | |
940 | } | |
941 | ||
942 | static int tuple_set_s32(PyObject *t, unsigned int pos, s32 val) | |
943 | { | |
66dfdff0 | 944 | return PyTuple_SetItem(t, pos, _PyLong_FromLong(val)); |
df919b40 AH |
945 | } |
946 | ||
947 | static int tuple_set_string(PyObject *t, unsigned int pos, const char *s) | |
948 | { | |
66dfdff0 | 949 | return PyTuple_SetItem(t, pos, _PyUnicode_FromString(s)); |
df919b40 AH |
950 | } |
951 | ||
952 | static int python_export_evsel(struct db_export *dbe, struct perf_evsel *evsel) | |
953 | { | |
954 | struct tables *tables = container_of(dbe, struct tables, dbe); | |
955 | PyObject *t; | |
956 | ||
957 | t = tuple_new(2); | |
958 | ||
959 | tuple_set_u64(t, 0, evsel->db_id); | |
960 | tuple_set_string(t, 1, perf_evsel__name(evsel)); | |
961 | ||
962 | call_object(tables->evsel_handler, t, "evsel_table"); | |
963 | ||
964 | Py_DECREF(t); | |
965 | ||
966 | return 0; | |
967 | } | |
968 | ||
969 | static int python_export_machine(struct db_export *dbe, | |
970 | struct machine *machine) | |
971 | { | |
972 | struct tables *tables = container_of(dbe, struct tables, dbe); | |
973 | PyObject *t; | |
974 | ||
975 | t = tuple_new(3); | |
976 | ||
977 | tuple_set_u64(t, 0, machine->db_id); | |
978 | tuple_set_s32(t, 1, machine->pid); | |
979 | tuple_set_string(t, 2, machine->root_dir ? machine->root_dir : ""); | |
980 | ||
981 | call_object(tables->machine_handler, t, "machine_table"); | |
982 | ||
983 | Py_DECREF(t); | |
984 | ||
985 | return 0; | |
986 | } | |
987 | ||
988 | static int python_export_thread(struct db_export *dbe, struct thread *thread, | |
989 | u64 main_thread_db_id, struct machine *machine) | |
990 | { | |
991 | struct tables *tables = container_of(dbe, struct tables, dbe); | |
992 | PyObject *t; | |
993 | ||
994 | t = tuple_new(5); | |
995 | ||
996 | tuple_set_u64(t, 0, thread->db_id); | |
997 | tuple_set_u64(t, 1, machine->db_id); | |
998 | tuple_set_u64(t, 2, main_thread_db_id); | |
999 | tuple_set_s32(t, 3, thread->pid_); | |
1000 | tuple_set_s32(t, 4, thread->tid); | |
1001 | ||
1002 | call_object(tables->thread_handler, t, "thread_table"); | |
1003 | ||
1004 | Py_DECREF(t); | |
1005 | ||
1006 | return 0; | |
1007 | } | |
1008 | ||
1009 | static int python_export_comm(struct db_export *dbe, struct comm *comm) | |
1010 | { | |
1011 | struct tables *tables = container_of(dbe, struct tables, dbe); | |
1012 | PyObject *t; | |
1013 | ||
1014 | t = tuple_new(2); | |
1015 | ||
1016 | tuple_set_u64(t, 0, comm->db_id); | |
1017 | tuple_set_string(t, 1, comm__str(comm)); | |
1018 | ||
1019 | call_object(tables->comm_handler, t, "comm_table"); | |
1020 | ||
1021 | Py_DECREF(t); | |
1022 | ||
1023 | return 0; | |
1024 | } | |
1025 | ||
1026 | static int python_export_comm_thread(struct db_export *dbe, u64 db_id, | |
1027 | struct comm *comm, struct thread *thread) | |
1028 | { | |
1029 | struct tables *tables = container_of(dbe, struct tables, dbe); | |
1030 | PyObject *t; | |
1031 | ||
1032 | t = tuple_new(3); | |
1033 | ||
1034 | tuple_set_u64(t, 0, db_id); | |
1035 | tuple_set_u64(t, 1, comm->db_id); | |
1036 | tuple_set_u64(t, 2, thread->db_id); | |
1037 | ||
1038 | call_object(tables->comm_thread_handler, t, "comm_thread_table"); | |
1039 | ||
1040 | Py_DECREF(t); | |
1041 | ||
1042 | return 0; | |
1043 | } | |
1044 | ||
1045 | static int python_export_dso(struct db_export *dbe, struct dso *dso, | |
1046 | struct machine *machine) | |
1047 | { | |
1048 | struct tables *tables = container_of(dbe, struct tables, dbe); | |
b5d8bbe8 | 1049 | char sbuild_id[SBUILD_ID_SIZE]; |
df919b40 AH |
1050 | PyObject *t; |
1051 | ||
1052 | build_id__sprintf(dso->build_id, sizeof(dso->build_id), sbuild_id); | |
1053 | ||
1054 | t = tuple_new(5); | |
1055 | ||
1056 | tuple_set_u64(t, 0, dso->db_id); | |
1057 | tuple_set_u64(t, 1, machine->db_id); | |
1058 | tuple_set_string(t, 2, dso->short_name); | |
1059 | tuple_set_string(t, 3, dso->long_name); | |
1060 | tuple_set_string(t, 4, sbuild_id); | |
1061 | ||
1062 | call_object(tables->dso_handler, t, "dso_table"); | |
1063 | ||
1064 | Py_DECREF(t); | |
1065 | ||
1066 | return 0; | |
1067 | } | |
1068 | ||
1069 | static int python_export_symbol(struct db_export *dbe, struct symbol *sym, | |
1070 | struct dso *dso) | |
1071 | { | |
1072 | struct tables *tables = container_of(dbe, struct tables, dbe); | |
1073 | u64 *sym_db_id = symbol__priv(sym); | |
1074 | PyObject *t; | |
1075 | ||
1076 | t = tuple_new(6); | |
1077 | ||
1078 | tuple_set_u64(t, 0, *sym_db_id); | |
1079 | tuple_set_u64(t, 1, dso->db_id); | |
1080 | tuple_set_u64(t, 2, sym->start); | |
1081 | tuple_set_u64(t, 3, sym->end); | |
1082 | tuple_set_s32(t, 4, sym->binding); | |
1083 | tuple_set_string(t, 5, sym->name); | |
1084 | ||
1085 | call_object(tables->symbol_handler, t, "symbol_table"); | |
1086 | ||
1087 | Py_DECREF(t); | |
1088 | ||
1089 | return 0; | |
1090 | } | |
1091 | ||
c29414f5 AH |
1092 | static int python_export_branch_type(struct db_export *dbe, u32 branch_type, |
1093 | const char *name) | |
1094 | { | |
1095 | struct tables *tables = container_of(dbe, struct tables, dbe); | |
1096 | PyObject *t; | |
1097 | ||
1098 | t = tuple_new(2); | |
1099 | ||
1100 | tuple_set_s32(t, 0, branch_type); | |
1101 | tuple_set_string(t, 1, name); | |
1102 | ||
1103 | call_object(tables->branch_type_handler, t, "branch_type_table"); | |
1104 | ||
1105 | Py_DECREF(t); | |
1106 | ||
1107 | return 0; | |
1108 | } | |
1109 | ||
df919b40 AH |
1110 | static int python_export_sample(struct db_export *dbe, |
1111 | struct export_sample *es) | |
1112 | { | |
1113 | struct tables *tables = container_of(dbe, struct tables, dbe); | |
1114 | PyObject *t; | |
1115 | ||
2c15f5eb | 1116 | t = tuple_new(22); |
df919b40 AH |
1117 | |
1118 | tuple_set_u64(t, 0, es->db_id); | |
1119 | tuple_set_u64(t, 1, es->evsel->db_id); | |
1120 | tuple_set_u64(t, 2, es->al->machine->db_id); | |
b83e868d | 1121 | tuple_set_u64(t, 3, es->al->thread->db_id); |
df919b40 AH |
1122 | tuple_set_u64(t, 4, es->comm_db_id); |
1123 | tuple_set_u64(t, 5, es->dso_db_id); | |
1124 | tuple_set_u64(t, 6, es->sym_db_id); | |
1125 | tuple_set_u64(t, 7, es->offset); | |
1126 | tuple_set_u64(t, 8, es->sample->ip); | |
1127 | tuple_set_u64(t, 9, es->sample->time); | |
1128 | tuple_set_s32(t, 10, es->sample->cpu); | |
1129 | tuple_set_u64(t, 11, es->addr_dso_db_id); | |
1130 | tuple_set_u64(t, 12, es->addr_sym_db_id); | |
1131 | tuple_set_u64(t, 13, es->addr_offset); | |
1132 | tuple_set_u64(t, 14, es->sample->addr); | |
1133 | tuple_set_u64(t, 15, es->sample->period); | |
1134 | tuple_set_u64(t, 16, es->sample->weight); | |
1135 | tuple_set_u64(t, 17, es->sample->transaction); | |
1136 | tuple_set_u64(t, 18, es->sample->data_src); | |
c29414f5 AH |
1137 | tuple_set_s32(t, 19, es->sample->flags & PERF_BRANCH_MASK); |
1138 | tuple_set_s32(t, 20, !!(es->sample->flags & PERF_IP_FLAG_IN_TX)); | |
2c15f5eb | 1139 | tuple_set_u64(t, 21, es->call_path_id); |
df919b40 AH |
1140 | |
1141 | call_object(tables->sample_handler, t, "sample_table"); | |
1142 | ||
1143 | Py_DECREF(t); | |
1144 | ||
1145 | return 0; | |
1146 | } | |
1147 | ||
6a70307d AH |
1148 | static int python_export_call_path(struct db_export *dbe, struct call_path *cp) |
1149 | { | |
1150 | struct tables *tables = container_of(dbe, struct tables, dbe); | |
1151 | PyObject *t; | |
1152 | u64 parent_db_id, sym_db_id; | |
1153 | ||
1154 | parent_db_id = cp->parent ? cp->parent->db_id : 0; | |
1155 | sym_db_id = cp->sym ? *(u64 *)symbol__priv(cp->sym) : 0; | |
1156 | ||
1157 | t = tuple_new(4); | |
1158 | ||
1159 | tuple_set_u64(t, 0, cp->db_id); | |
1160 | tuple_set_u64(t, 1, parent_db_id); | |
1161 | tuple_set_u64(t, 2, sym_db_id); | |
1162 | tuple_set_u64(t, 3, cp->ip); | |
1163 | ||
1164 | call_object(tables->call_path_handler, t, "call_path_table"); | |
1165 | ||
1166 | Py_DECREF(t); | |
1167 | ||
1168 | return 0; | |
1169 | } | |
1170 | ||
1171 | static int python_export_call_return(struct db_export *dbe, | |
1172 | struct call_return *cr) | |
1173 | { | |
1174 | struct tables *tables = container_of(dbe, struct tables, dbe); | |
1175 | u64 comm_db_id = cr->comm ? cr->comm->db_id : 0; | |
1176 | PyObject *t; | |
1177 | ||
1178 | t = tuple_new(11); | |
1179 | ||
1180 | tuple_set_u64(t, 0, cr->db_id); | |
1181 | tuple_set_u64(t, 1, cr->thread->db_id); | |
1182 | tuple_set_u64(t, 2, comm_db_id); | |
1183 | tuple_set_u64(t, 3, cr->cp->db_id); | |
1184 | tuple_set_u64(t, 4, cr->call_time); | |
1185 | tuple_set_u64(t, 5, cr->return_time); | |
1186 | tuple_set_u64(t, 6, cr->branch_count); | |
1187 | tuple_set_u64(t, 7, cr->call_ref); | |
1188 | tuple_set_u64(t, 8, cr->return_ref); | |
1189 | tuple_set_u64(t, 9, cr->cp->parent->db_id); | |
1190 | tuple_set_s32(t, 10, cr->flags); | |
1191 | ||
1192 | call_object(tables->call_return_handler, t, "call_return_table"); | |
1193 | ||
1194 | Py_DECREF(t); | |
1195 | ||
1196 | return 0; | |
1197 | } | |
1198 | ||
1199 | static int python_process_call_return(struct call_return *cr, void *data) | |
1200 | { | |
1201 | struct db_export *dbe = data; | |
1202 | ||
1203 | return db_export__call_return(dbe, cr); | |
1204 | } | |
1205 | ||
b7fff6b5 | 1206 | static void python_process_general_event(struct perf_sample *sample, |
6a6daec2 | 1207 | struct perf_evsel *evsel, |
87b6a3ad | 1208 | struct addr_location *al) |
6a6daec2 | 1209 | { |
892e76b2 | 1210 | PyObject *handler, *t, *dict, *callchain; |
6a6daec2 FT |
1211 | static char handler_name[64]; |
1212 | unsigned n = 0; | |
6a6daec2 | 1213 | |
e9f9a9ca AK |
1214 | snprintf(handler_name, sizeof(handler_name), "%s", "process_event"); |
1215 | ||
1216 | handler = get_handler(handler_name); | |
1217 | if (!handler) | |
1218 | return; | |
1219 | ||
fd6b858a FT |
1220 | /* |
1221 | * Use the MAX_FIELDS to make the function expandable, though | |
87b6a3ad | 1222 | * currently there is only one item for the tuple. |
fd6b858a | 1223 | */ |
6a6daec2 FT |
1224 | t = PyTuple_New(MAX_FIELDS); |
1225 | if (!t) | |
1226 | Py_FatalError("couldn't create Python tuple"); | |
1227 | ||
0f5f5bcd JS |
1228 | /* ip unwinding */ |
1229 | callchain = python_process_callchain(sample, evsel, al); | |
892e76b2 | 1230 | dict = get_perf_sample_dict(sample, evsel, al, callchain); |
0f5f5bcd | 1231 | |
fd6b858a | 1232 | PyTuple_SetItem(t, n++, dict); |
6a6daec2 FT |
1233 | if (_PyTuple_Resize(&t, n) == -1) |
1234 | Py_FatalError("error resizing Python tuple"); | |
1235 | ||
a5563edf | 1236 | call_object(handler, t, handler_name); |
e9f9a9ca | 1237 | |
fd6b858a | 1238 | Py_DECREF(dict); |
6a6daec2 FT |
1239 | Py_DECREF(t); |
1240 | } | |
1241 | ||
df919b40 | 1242 | static void python_process_event(union perf_event *event, |
6a6daec2 FT |
1243 | struct perf_sample *sample, |
1244 | struct perf_evsel *evsel, | |
73994dc1 | 1245 | struct addr_location *al) |
6a6daec2 | 1246 | { |
df919b40 AH |
1247 | struct tables *tables = &tables_global; |
1248 | ||
6a6daec2 FT |
1249 | switch (evsel->attr.type) { |
1250 | case PERF_TYPE_TRACEPOINT: | |
f9d5d549 | 1251 | python_process_tracepoint(sample, evsel, al); |
6a6daec2 FT |
1252 | break; |
1253 | /* Reserve for future process_hw/sw/raw APIs */ | |
1254 | default: | |
df919b40 | 1255 | if (tables->db_export_mode) |
7327259d | 1256 | db_export__sample(&tables->dbe, event, sample, evsel, al); |
df919b40 | 1257 | else |
f9d5d549 | 1258 | python_process_general_event(sample, evsel, al); |
6a6daec2 FT |
1259 | } |
1260 | } | |
1261 | ||
aef90263 JO |
1262 | static void get_handler_name(char *str, size_t size, |
1263 | struct perf_evsel *evsel) | |
1264 | { | |
1265 | char *p = str; | |
1266 | ||
1267 | scnprintf(str, size, "stat__%s", perf_evsel__name(evsel)); | |
1268 | ||
1269 | while ((p = strchr(p, ':'))) { | |
1270 | *p = '_'; | |
1271 | p++; | |
1272 | } | |
1273 | } | |
1274 | ||
1275 | static void | |
1276 | process_stat(struct perf_evsel *counter, int cpu, int thread, u64 tstamp, | |
1277 | struct perf_counts_values *count) | |
1278 | { | |
1279 | PyObject *handler, *t; | |
1280 | static char handler_name[256]; | |
1281 | int n = 0; | |
1282 | ||
1283 | t = PyTuple_New(MAX_FIELDS); | |
1284 | if (!t) | |
1285 | Py_FatalError("couldn't create Python tuple"); | |
1286 | ||
1287 | get_handler_name(handler_name, sizeof(handler_name), | |
1288 | counter); | |
1289 | ||
1290 | handler = get_handler(handler_name); | |
1291 | if (!handler) { | |
1292 | pr_debug("can't find python handler %s\n", handler_name); | |
1293 | return; | |
1294 | } | |
1295 | ||
66dfdff0 JŠ |
1296 | PyTuple_SetItem(t, n++, _PyLong_FromLong(cpu)); |
1297 | PyTuple_SetItem(t, n++, _PyLong_FromLong(thread)); | |
aef90263 JO |
1298 | |
1299 | tuple_set_u64(t, n++, tstamp); | |
1300 | tuple_set_u64(t, n++, count->val); | |
1301 | tuple_set_u64(t, n++, count->ena); | |
1302 | tuple_set_u64(t, n++, count->run); | |
1303 | ||
1304 | if (_PyTuple_Resize(&t, n) == -1) | |
1305 | Py_FatalError("error resizing Python tuple"); | |
1306 | ||
1307 | call_object(handler, t, handler_name); | |
1308 | ||
1309 | Py_DECREF(t); | |
1310 | } | |
1311 | ||
1312 | static void python_process_stat(struct perf_stat_config *config, | |
1313 | struct perf_evsel *counter, u64 tstamp) | |
1314 | { | |
1315 | struct thread_map *threads = counter->threads; | |
1316 | struct cpu_map *cpus = counter->cpus; | |
1317 | int cpu, thread; | |
1318 | ||
1319 | if (config->aggr_mode == AGGR_GLOBAL) { | |
1320 | process_stat(counter, -1, -1, tstamp, | |
1321 | &counter->counts->aggr); | |
1322 | return; | |
1323 | } | |
1324 | ||
1325 | for (thread = 0; thread < threads->nr; thread++) { | |
1326 | for (cpu = 0; cpu < cpus->nr; cpu++) { | |
1327 | process_stat(counter, cpus->map[cpu], | |
1328 | thread_map__pid(threads, thread), tstamp, | |
1329 | perf_counts(counter->counts, cpu, thread)); | |
1330 | } | |
1331 | } | |
1332 | } | |
1333 | ||
1334 | static void python_process_stat_interval(u64 tstamp) | |
1335 | { | |
1336 | PyObject *handler, *t; | |
1337 | static const char handler_name[] = "stat__interval"; | |
1338 | int n = 0; | |
1339 | ||
1340 | t = PyTuple_New(MAX_FIELDS); | |
1341 | if (!t) | |
1342 | Py_FatalError("couldn't create Python tuple"); | |
1343 | ||
1344 | handler = get_handler(handler_name); | |
1345 | if (!handler) { | |
1346 | pr_debug("can't find python handler %s\n", handler_name); | |
1347 | return; | |
1348 | } | |
1349 | ||
1350 | tuple_set_u64(t, n++, tstamp); | |
1351 | ||
1352 | if (_PyTuple_Resize(&t, n) == -1) | |
1353 | Py_FatalError("error resizing Python tuple"); | |
1354 | ||
1355 | call_object(handler, t, handler_name); | |
1356 | ||
1357 | Py_DECREF(t); | |
1358 | } | |
1359 | ||
7e4b21b8 TZ |
1360 | static int run_start_sub(void) |
1361 | { | |
7e4b21b8 TZ |
1362 | main_module = PyImport_AddModule("__main__"); |
1363 | if (main_module == NULL) | |
1364 | return -1; | |
1365 | Py_INCREF(main_module); | |
1366 | ||
1367 | main_dict = PyModule_GetDict(main_module); | |
a5563edf | 1368 | if (main_dict == NULL) |
7e4b21b8 | 1369 | goto error; |
7e4b21b8 TZ |
1370 | Py_INCREF(main_dict); |
1371 | ||
a5563edf | 1372 | try_call_object("trace_begin", NULL); |
7e4b21b8 | 1373 | |
a5563edf | 1374 | return 0; |
7e4b21b8 | 1375 | |
7e4b21b8 TZ |
1376 | error: |
1377 | Py_XDECREF(main_dict); | |
1378 | Py_XDECREF(main_module); | |
a5563edf | 1379 | return -1; |
7e4b21b8 TZ |
1380 | } |
1381 | ||
df919b40 AH |
1382 | #define SET_TABLE_HANDLER_(name, handler_name, table_name) do { \ |
1383 | tables->handler_name = get_handler(#table_name); \ | |
1384 | if (tables->handler_name) \ | |
1385 | tables->dbe.export_ ## name = python_export_ ## name; \ | |
1386 | } while (0) | |
1387 | ||
1388 | #define SET_TABLE_HANDLER(name) \ | |
1389 | SET_TABLE_HANDLER_(name, name ## _handler, name ## _table) | |
1390 | ||
1391 | static void set_table_handlers(struct tables *tables) | |
1392 | { | |
1393 | const char *perf_db_export_mode = "perf_db_export_mode"; | |
6a70307d | 1394 | const char *perf_db_export_calls = "perf_db_export_calls"; |
2c15f5eb CP |
1395 | const char *perf_db_export_callchains = "perf_db_export_callchains"; |
1396 | PyObject *db_export_mode, *db_export_calls, *db_export_callchains; | |
6a70307d | 1397 | bool export_calls = false; |
2c15f5eb | 1398 | bool export_callchains = false; |
df919b40 AH |
1399 | int ret; |
1400 | ||
1401 | memset(tables, 0, sizeof(struct tables)); | |
1402 | if (db_export__init(&tables->dbe)) | |
1403 | Py_FatalError("failed to initialize export"); | |
1404 | ||
1405 | db_export_mode = PyDict_GetItemString(main_dict, perf_db_export_mode); | |
1406 | if (!db_export_mode) | |
1407 | return; | |
1408 | ||
1409 | ret = PyObject_IsTrue(db_export_mode); | |
1410 | if (ret == -1) | |
1411 | handler_call_die(perf_db_export_mode); | |
1412 | if (!ret) | |
1413 | return; | |
1414 | ||
2c15f5eb | 1415 | /* handle export calls */ |
6a70307d AH |
1416 | tables->dbe.crp = NULL; |
1417 | db_export_calls = PyDict_GetItemString(main_dict, perf_db_export_calls); | |
1418 | if (db_export_calls) { | |
1419 | ret = PyObject_IsTrue(db_export_calls); | |
1420 | if (ret == -1) | |
1421 | handler_call_die(perf_db_export_calls); | |
1422 | export_calls = !!ret; | |
1423 | } | |
1424 | ||
1425 | if (export_calls) { | |
1426 | tables->dbe.crp = | |
1427 | call_return_processor__new(python_process_call_return, | |
1428 | &tables->dbe); | |
1429 | if (!tables->dbe.crp) | |
1430 | Py_FatalError("failed to create calls processor"); | |
1431 | } | |
1432 | ||
2c15f5eb CP |
1433 | /* handle export callchains */ |
1434 | tables->dbe.cpr = NULL; | |
1435 | db_export_callchains = PyDict_GetItemString(main_dict, | |
1436 | perf_db_export_callchains); | |
1437 | if (db_export_callchains) { | |
1438 | ret = PyObject_IsTrue(db_export_callchains); | |
1439 | if (ret == -1) | |
1440 | handler_call_die(perf_db_export_callchains); | |
1441 | export_callchains = !!ret; | |
1442 | } | |
1443 | ||
1444 | if (export_callchains) { | |
1445 | /* | |
1446 | * Attempt to use the call path root from the call return | |
1447 | * processor, if the call return processor is in use. Otherwise, | |
1448 | * we allocate a new call path root. This prevents exporting | |
1449 | * duplicate call path ids when both are in use simultaniously. | |
1450 | */ | |
1451 | if (tables->dbe.crp) | |
1452 | tables->dbe.cpr = tables->dbe.crp->cpr; | |
1453 | else | |
1454 | tables->dbe.cpr = call_path_root__new(); | |
1455 | ||
1456 | if (!tables->dbe.cpr) | |
aff63340 | 1457 | Py_FatalError("failed to create call path root"); |
2c15f5eb CP |
1458 | } |
1459 | ||
df919b40 AH |
1460 | tables->db_export_mode = true; |
1461 | /* | |
1462 | * Reserve per symbol space for symbol->db_id via symbol__priv() | |
1463 | */ | |
1464 | symbol_conf.priv_size = sizeof(u64); | |
1465 | ||
1466 | SET_TABLE_HANDLER(evsel); | |
1467 | SET_TABLE_HANDLER(machine); | |
1468 | SET_TABLE_HANDLER(thread); | |
1469 | SET_TABLE_HANDLER(comm); | |
1470 | SET_TABLE_HANDLER(comm_thread); | |
1471 | SET_TABLE_HANDLER(dso); | |
1472 | SET_TABLE_HANDLER(symbol); | |
c29414f5 | 1473 | SET_TABLE_HANDLER(branch_type); |
df919b40 | 1474 | SET_TABLE_HANDLER(sample); |
6a70307d AH |
1475 | SET_TABLE_HANDLER(call_path); |
1476 | SET_TABLE_HANDLER(call_return); | |
df919b40 AH |
1477 | } |
1478 | ||
66dfdff0 JŠ |
1479 | #if PY_MAJOR_VERSION < 3 |
1480 | static void _free_command_line(const char **command_line, int num) | |
1481 | { | |
1482 | free(command_line); | |
1483 | } | |
1484 | #else | |
1485 | static void _free_command_line(wchar_t **command_line, int num) | |
1486 | { | |
1487 | int i; | |
1488 | for (i = 0; i < num; i++) | |
1489 | PyMem_RawFree(command_line[i]); | |
1490 | free(command_line); | |
1491 | } | |
1492 | #endif | |
1493 | ||
1494 | ||
7e4b21b8 TZ |
1495 | /* |
1496 | * Start trace script | |
1497 | */ | |
1498 | static int python_start_script(const char *script, int argc, const char **argv) | |
1499 | { | |
df919b40 | 1500 | struct tables *tables = &tables_global; |
66dfdff0 | 1501 | #if PY_MAJOR_VERSION < 3 |
7e4b21b8 | 1502 | const char **command_line; |
66dfdff0 JŠ |
1503 | #else |
1504 | wchar_t **command_line; | |
1505 | #endif | |
7e4b21b8 TZ |
1506 | char buf[PATH_MAX]; |
1507 | int i, err = 0; | |
1508 | FILE *fp; | |
1509 | ||
66dfdff0 | 1510 | #if PY_MAJOR_VERSION < 3 |
7e4b21b8 TZ |
1511 | command_line = malloc((argc + 1) * sizeof(const char *)); |
1512 | command_line[0] = script; | |
1513 | for (i = 1; i < argc + 1; i++) | |
1514 | command_line[i] = argv[i - 1]; | |
66dfdff0 JŠ |
1515 | #else |
1516 | command_line = malloc((argc + 1) * sizeof(wchar_t *)); | |
1517 | command_line[0] = Py_DecodeLocale(script, NULL); | |
1518 | for (i = 1; i < argc + 1; i++) | |
1519 | command_line[i] = Py_DecodeLocale(argv[i - 1], NULL); | |
1520 | #endif | |
7e4b21b8 TZ |
1521 | |
1522 | Py_Initialize(); | |
1523 | ||
66dfdff0 | 1524 | #if PY_MAJOR_VERSION < 3 |
7e4b21b8 | 1525 | initperf_trace_context(); |
7e4b21b8 | 1526 | PySys_SetArgv(argc + 1, (char **)command_line); |
66dfdff0 JŠ |
1527 | #else |
1528 | PyInit_perf_trace_context(); | |
1529 | PySys_SetArgv(argc + 1, command_line); | |
1530 | #endif | |
7e4b21b8 TZ |
1531 | |
1532 | fp = fopen(script, "r"); | |
1533 | if (!fp) { | |
1534 | sprintf(buf, "Can't open python script \"%s\"", script); | |
1535 | perror(buf); | |
1536 | err = -1; | |
1537 | goto error; | |
1538 | } | |
1539 | ||
1540 | err = PyRun_SimpleFile(fp, script); | |
1541 | if (err) { | |
1542 | fprintf(stderr, "Error running python script %s\n", script); | |
1543 | goto error; | |
1544 | } | |
1545 | ||
1546 | err = run_start_sub(); | |
1547 | if (err) { | |
1548 | fprintf(stderr, "Error starting python script %s\n", script); | |
1549 | goto error; | |
1550 | } | |
1551 | ||
df919b40 AH |
1552 | set_table_handlers(tables); |
1553 | ||
c29414f5 AH |
1554 | if (tables->db_export_mode) { |
1555 | err = db_export__branch_types(&tables->dbe); | |
1556 | if (err) | |
1557 | goto error; | |
1558 | } | |
1559 | ||
66dfdff0 | 1560 | _free_command_line(command_line, argc + 1); |
979ac257 | 1561 | |
7e4b21b8 TZ |
1562 | return err; |
1563 | error: | |
1564 | Py_Finalize(); | |
66dfdff0 | 1565 | _free_command_line(command_line, argc + 1); |
7e4b21b8 TZ |
1566 | |
1567 | return err; | |
1568 | } | |
1569 | ||
d445dd2a AH |
1570 | static int python_flush_script(void) |
1571 | { | |
758008b2 AH |
1572 | struct tables *tables = &tables_global; |
1573 | ||
1574 | return db_export__flush(&tables->dbe); | |
d445dd2a AH |
1575 | } |
1576 | ||
7e4b21b8 TZ |
1577 | /* |
1578 | * Stop trace script | |
1579 | */ | |
1580 | static int python_stop_script(void) | |
1581 | { | |
df919b40 AH |
1582 | struct tables *tables = &tables_global; |
1583 | ||
a5563edf | 1584 | try_call_object("trace_end", NULL); |
7e4b21b8 | 1585 | |
df919b40 AH |
1586 | db_export__exit(&tables->dbe); |
1587 | ||
7e4b21b8 TZ |
1588 | Py_XDECREF(main_dict); |
1589 | Py_XDECREF(main_module); | |
1590 | Py_Finalize(); | |
1591 | ||
a5563edf | 1592 | return 0; |
7e4b21b8 TZ |
1593 | } |
1594 | ||
da378962 | 1595 | static int python_generate_script(struct pevent *pevent, const char *outfile) |
7e4b21b8 | 1596 | { |
aaf045f7 | 1597 | struct event_format *event = NULL; |
7e4b21b8 TZ |
1598 | struct format_field *f; |
1599 | char fname[PATH_MAX]; | |
1600 | int not_first, count; | |
1601 | FILE *ofp; | |
1602 | ||
1603 | sprintf(fname, "%s.py", outfile); | |
1604 | ofp = fopen(fname, "w"); | |
1605 | if (ofp == NULL) { | |
1606 | fprintf(stderr, "couldn't open %s\n", fname); | |
1607 | return -1; | |
1608 | } | |
133dc4c3 IM |
1609 | fprintf(ofp, "# perf script event handlers, " |
1610 | "generated by perf script -g python\n"); | |
7e4b21b8 TZ |
1611 | |
1612 | fprintf(ofp, "# Licensed under the terms of the GNU GPL" | |
1613 | " License version 2\n\n"); | |
1614 | ||
1615 | fprintf(ofp, "# The common_* event handler fields are the most useful " | |
1616 | "fields common to\n"); | |
1617 | ||
1618 | fprintf(ofp, "# all events. They don't necessarily correspond to " | |
1619 | "the 'common_*' fields\n"); | |
1620 | ||
1621 | fprintf(ofp, "# in the format files. Those fields not available as " | |
1622 | "handler params can\n"); | |
1623 | ||
1624 | fprintf(ofp, "# be retrieved using Python functions of the form " | |
1625 | "common_*(context).\n"); | |
1626 | ||
c76132dc | 1627 | fprintf(ofp, "# See the perf-script-python Documentation for the list " |
7e4b21b8 TZ |
1628 | "of available functions.\n\n"); |
1629 | ||
877cc639 | 1630 | fprintf(ofp, "from __future__ import print_function\n\n"); |
7e4b21b8 TZ |
1631 | fprintf(ofp, "import os\n"); |
1632 | fprintf(ofp, "import sys\n\n"); | |
1633 | ||
1634 | fprintf(ofp, "sys.path.append(os.environ['PERF_EXEC_PATH'] + \\\n"); | |
1635 | fprintf(ofp, "\t'/scripts/python/Perf-Trace-Util/lib/Perf/Trace')\n"); | |
1636 | fprintf(ofp, "\nfrom perf_trace_context import *\n"); | |
1637 | fprintf(ofp, "from Core import *\n\n\n"); | |
1638 | ||
1639 | fprintf(ofp, "def trace_begin():\n"); | |
877cc639 | 1640 | fprintf(ofp, "\tprint(\"in trace_begin\")\n\n"); |
7e4b21b8 TZ |
1641 | |
1642 | fprintf(ofp, "def trace_end():\n"); | |
877cc639 | 1643 | fprintf(ofp, "\tprint(\"in trace_end\")\n\n"); |
7e4b21b8 | 1644 | |
da378962 | 1645 | while ((event = trace_find_next_event(pevent, event))) { |
7e4b21b8 TZ |
1646 | fprintf(ofp, "def %s__%s(", event->system, event->name); |
1647 | fprintf(ofp, "event_name, "); | |
1648 | fprintf(ofp, "context, "); | |
1649 | fprintf(ofp, "common_cpu,\n"); | |
1650 | fprintf(ofp, "\tcommon_secs, "); | |
1651 | fprintf(ofp, "common_nsecs, "); | |
1652 | fprintf(ofp, "common_pid, "); | |
1653 | fprintf(ofp, "common_comm,\n\t"); | |
0f5f5bcd | 1654 | fprintf(ofp, "common_callchain, "); |
7e4b21b8 TZ |
1655 | |
1656 | not_first = 0; | |
1657 | count = 0; | |
1658 | ||
1659 | for (f = event->format.fields; f; f = f->next) { | |
1660 | if (not_first++) | |
1661 | fprintf(ofp, ", "); | |
1662 | if (++count % 5 == 0) | |
1663 | fprintf(ofp, "\n\t"); | |
1664 | ||
1665 | fprintf(ofp, "%s", f->name); | |
1666 | } | |
a6418605 AK |
1667 | if (not_first++) |
1668 | fprintf(ofp, ", "); | |
1669 | if (++count % 5 == 0) | |
1670 | fprintf(ofp, "\n\t\t"); | |
1671 | fprintf(ofp, "perf_sample_dict"); | |
1672 | ||
7e4b21b8 TZ |
1673 | fprintf(ofp, "):\n"); |
1674 | ||
1675 | fprintf(ofp, "\t\tprint_header(event_name, common_cpu, " | |
1676 | "common_secs, common_nsecs,\n\t\t\t" | |
1677 | "common_pid, common_comm)\n\n"); | |
1678 | ||
877cc639 | 1679 | fprintf(ofp, "\t\tprint(\""); |
7e4b21b8 TZ |
1680 | |
1681 | not_first = 0; | |
1682 | count = 0; | |
1683 | ||
1684 | for (f = event->format.fields; f; f = f->next) { | |
1685 | if (not_first++) | |
1686 | fprintf(ofp, ", "); | |
1687 | if (count && count % 3 == 0) { | |
1688 | fprintf(ofp, "\" \\\n\t\t\""); | |
1689 | } | |
1690 | count++; | |
1691 | ||
1692 | fprintf(ofp, "%s=", f->name); | |
1693 | if (f->flags & FIELD_IS_STRING || | |
1694 | f->flags & FIELD_IS_FLAG || | |
e646fe73 | 1695 | f->flags & FIELD_IS_ARRAY || |
7e4b21b8 TZ |
1696 | f->flags & FIELD_IS_SYMBOLIC) |
1697 | fprintf(ofp, "%%s"); | |
1698 | else if (f->flags & FIELD_IS_SIGNED) | |
1699 | fprintf(ofp, "%%d"); | |
1700 | else | |
1701 | fprintf(ofp, "%%u"); | |
1702 | } | |
1703 | ||
0f5f5bcd | 1704 | fprintf(ofp, "\" %% \\\n\t\t("); |
7e4b21b8 TZ |
1705 | |
1706 | not_first = 0; | |
1707 | count = 0; | |
1708 | ||
1709 | for (f = event->format.fields; f; f = f->next) { | |
1710 | if (not_first++) | |
1711 | fprintf(ofp, ", "); | |
1712 | ||
1713 | if (++count % 5 == 0) | |
1714 | fprintf(ofp, "\n\t\t"); | |
1715 | ||
1716 | if (f->flags & FIELD_IS_FLAG) { | |
1717 | if ((count - 1) % 5 != 0) { | |
1718 | fprintf(ofp, "\n\t\t"); | |
1719 | count = 4; | |
1720 | } | |
1721 | fprintf(ofp, "flag_str(\""); | |
1722 | fprintf(ofp, "%s__%s\", ", event->system, | |
1723 | event->name); | |
1724 | fprintf(ofp, "\"%s\", %s)", f->name, | |
1725 | f->name); | |
1726 | } else if (f->flags & FIELD_IS_SYMBOLIC) { | |
1727 | if ((count - 1) % 5 != 0) { | |
1728 | fprintf(ofp, "\n\t\t"); | |
1729 | count = 4; | |
1730 | } | |
1731 | fprintf(ofp, "symbol_str(\""); | |
1732 | fprintf(ofp, "%s__%s\", ", event->system, | |
1733 | event->name); | |
1734 | fprintf(ofp, "\"%s\", %s)", f->name, | |
1735 | f->name); | |
1736 | } else | |
1737 | fprintf(ofp, "%s", f->name); | |
1738 | } | |
1739 | ||
877cc639 | 1740 | fprintf(ofp, "))\n\n"); |
0f5f5bcd | 1741 | |
877cc639 JC |
1742 | fprintf(ofp, "\t\tprint('Sample: {'+" |
1743 | "get_dict_as_string(perf_sample_dict['sample'], ', ')+'}')\n\n"); | |
a6418605 | 1744 | |
0f5f5bcd JS |
1745 | fprintf(ofp, "\t\tfor node in common_callchain:"); |
1746 | fprintf(ofp, "\n\t\t\tif 'sym' in node:"); | |
877cc639 | 1747 | fprintf(ofp, "\n\t\t\t\tprint(\"\\t[%%x] %%s\" %% (node['ip'], node['sym']['name']))"); |
0f5f5bcd | 1748 | fprintf(ofp, "\n\t\t\telse:"); |
877cc639 JC |
1749 | fprintf(ofp, "\n\t\t\t\tprint(\"\t[%%x]\" %% (node['ip']))\n\n"); |
1750 | fprintf(ofp, "\t\tprint()\n\n"); | |
0f5f5bcd | 1751 | |
7e4b21b8 TZ |
1752 | } |
1753 | ||
1754 | fprintf(ofp, "def trace_unhandled(event_name, context, " | |
a6418605 | 1755 | "event_fields_dict, perf_sample_dict):\n"); |
7e4b21b8 | 1756 | |
877cc639 JC |
1757 | fprintf(ofp, "\t\tprint(get_dict_as_string(event_fields_dict))\n"); |
1758 | fprintf(ofp, "\t\tprint('Sample: {'+" | |
1759 | "get_dict_as_string(perf_sample_dict['sample'], ', ')+'}')\n\n"); | |
7e4b21b8 TZ |
1760 | |
1761 | fprintf(ofp, "def print_header(" | |
1762 | "event_name, cpu, secs, nsecs, pid, comm):\n" | |
877cc639 JC |
1763 | "\tprint(\"%%-20s %%5u %%05u.%%09u %%8u %%-20s \" %% \\\n\t" |
1764 | "(event_name, cpu, secs, nsecs, pid, comm), end=\"\")\n\n"); | |
a6418605 AK |
1765 | |
1766 | fprintf(ofp, "def get_dict_as_string(a_dict, delimiter=' '):\n" | |
1767 | "\treturn delimiter.join" | |
1768 | "(['%%s=%%s'%%(k,str(v))for k,v in sorted(a_dict.items())])\n"); | |
7e4b21b8 TZ |
1769 | |
1770 | fclose(ofp); | |
1771 | ||
1772 | fprintf(stderr, "generated Python script: %s\n", fname); | |
1773 | ||
1774 | return 0; | |
1775 | } | |
1776 | ||
1777 | struct scripting_ops python_scripting_ops = { | |
aef90263 JO |
1778 | .name = "Python", |
1779 | .start_script = python_start_script, | |
1780 | .flush_script = python_flush_script, | |
1781 | .stop_script = python_stop_script, | |
1782 | .process_event = python_process_event, | |
1783 | .process_stat = python_process_stat, | |
1784 | .process_stat_interval = python_process_stat_interval, | |
1785 | .generate_script = python_generate_script, | |
7e4b21b8 | 1786 | }; |