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