diff options
Diffstat (limited to 'tools/perf/util/scripting-engines/trace-event-python.c')
-rw-r--r-- | tools/perf/util/scripting-engines/trace-event-python.c | 147 |
1 files changed, 104 insertions, 43 deletions
diff --git a/tools/perf/util/scripting-engines/trace-event-python.c b/tools/perf/util/scripting-engines/trace-event-python.c index ea070883c593..10dd5fce082b 100644 --- a/tools/perf/util/scripting-engines/trace-event-python.c +++ b/tools/perf/util/scripting-engines/trace-event-python.c | |||
@@ -49,7 +49,37 @@ | |||
49 | #include "print_binary.h" | 49 | #include "print_binary.h" |
50 | #include "stat.h" | 50 | #include "stat.h" |
51 | 51 | ||
52 | #if PY_MAJOR_VERSION < 3 | ||
53 | #define _PyUnicode_FromString(arg) \ | ||
54 | PyString_FromString(arg) | ||
55 | #define _PyUnicode_FromStringAndSize(arg1, arg2) \ | ||
56 | PyString_FromStringAndSize((arg1), (arg2)) | ||
57 | #define _PyBytes_FromStringAndSize(arg1, arg2) \ | ||
58 | PyString_FromStringAndSize((arg1), (arg2)) | ||
59 | #define _PyLong_FromLong(arg) \ | ||
60 | PyInt_FromLong(arg) | ||
61 | #define _PyLong_AsLong(arg) \ | ||
62 | PyInt_AsLong(arg) | ||
63 | #define _PyCapsule_New(arg1, arg2, arg3) \ | ||
64 | PyCObject_FromVoidPtr((arg1), (arg2)) | ||
65 | |||
52 | PyMODINIT_FUNC initperf_trace_context(void); | 66 | PyMODINIT_FUNC initperf_trace_context(void); |
67 | #else | ||
68 | #define _PyUnicode_FromString(arg) \ | ||
69 | PyUnicode_FromString(arg) | ||
70 | #define _PyUnicode_FromStringAndSize(arg1, arg2) \ | ||
71 | PyUnicode_FromStringAndSize((arg1), (arg2)) | ||
72 | #define _PyBytes_FromStringAndSize(arg1, arg2) \ | ||
73 | PyBytes_FromStringAndSize((arg1), (arg2)) | ||
74 | #define _PyLong_FromLong(arg) \ | ||
75 | PyLong_FromLong(arg) | ||
76 | #define _PyLong_AsLong(arg) \ | ||
77 | PyLong_AsLong(arg) | ||
78 | #define _PyCapsule_New(arg1, arg2, arg3) \ | ||
79 | PyCapsule_New((arg1), (arg2), (arg3)) | ||
80 | |||
81 | PyMODINIT_FUNC PyInit_perf_trace_context(void); | ||
82 | #endif | ||
53 | 83 | ||
54 | #define TRACE_EVENT_TYPE_MAX \ | 84 | #define TRACE_EVENT_TYPE_MAX \ |
55 | ((1 << (sizeof(unsigned short) * 8)) - 1) | 85 | ((1 << (sizeof(unsigned short) * 8)) - 1) |
@@ -135,7 +165,7 @@ static int get_argument_count(PyObject *handler) | |||
135 | PyObject *arg_count_obj = PyObject_GetAttrString(code_obj, | 165 | PyObject *arg_count_obj = PyObject_GetAttrString(code_obj, |
136 | "co_argcount"); | 166 | "co_argcount"); |
137 | if (arg_count_obj) { | 167 | if (arg_count_obj) { |
138 | arg_count = (int) PyInt_AsLong(arg_count_obj); | 168 | arg_count = (int) _PyLong_AsLong(arg_count_obj); |
139 | Py_DECREF(arg_count_obj); | 169 | Py_DECREF(arg_count_obj); |
140 | } | 170 | } |
141 | Py_DECREF(code_obj); | 171 | Py_DECREF(code_obj); |
@@ -182,10 +212,10 @@ static void define_value(enum print_arg_type field_type, | |||
182 | 212 | ||
183 | value = eval_flag(field_value); | 213 | value = eval_flag(field_value); |
184 | 214 | ||
185 | PyTuple_SetItem(t, n++, PyString_FromString(ev_name)); | 215 | PyTuple_SetItem(t, n++, _PyUnicode_FromString(ev_name)); |
186 | PyTuple_SetItem(t, n++, PyString_FromString(field_name)); | 216 | PyTuple_SetItem(t, n++, _PyUnicode_FromString(field_name)); |
187 | PyTuple_SetItem(t, n++, PyInt_FromLong(value)); | 217 | PyTuple_SetItem(t, n++, _PyLong_FromLong(value)); |
188 | PyTuple_SetItem(t, n++, PyString_FromString(field_str)); | 218 | PyTuple_SetItem(t, n++, _PyUnicode_FromString(field_str)); |
189 | 219 | ||
190 | try_call_object(handler_name, t); | 220 | try_call_object(handler_name, t); |
191 | 221 | ||
@@ -223,10 +253,10 @@ static void define_field(enum print_arg_type field_type, | |||
223 | if (!t) | 253 | if (!t) |
224 | Py_FatalError("couldn't create Python tuple"); | 254 | Py_FatalError("couldn't create Python tuple"); |
225 | 255 | ||
226 | PyTuple_SetItem(t, n++, PyString_FromString(ev_name)); | 256 | PyTuple_SetItem(t, n++, _PyUnicode_FromString(ev_name)); |
227 | PyTuple_SetItem(t, n++, PyString_FromString(field_name)); | 257 | PyTuple_SetItem(t, n++, _PyUnicode_FromString(field_name)); |
228 | if (field_type == PRINT_FLAGS) | 258 | if (field_type == PRINT_FLAGS) |
229 | PyTuple_SetItem(t, n++, PyString_FromString(delim)); | 259 | PyTuple_SetItem(t, n++, _PyUnicode_FromString(delim)); |
230 | 260 | ||
231 | try_call_object(handler_name, t); | 261 | try_call_object(handler_name, t); |
232 | 262 | ||
@@ -325,12 +355,12 @@ static PyObject *get_field_numeric_entry(struct event_format *event, | |||
325 | if (field->flags & FIELD_IS_SIGNED) { | 355 | if (field->flags & FIELD_IS_SIGNED) { |
326 | if ((long long)val >= LONG_MIN && | 356 | if ((long long)val >= LONG_MIN && |
327 | (long long)val <= LONG_MAX) | 357 | (long long)val <= LONG_MAX) |
328 | obj = PyInt_FromLong(val); | 358 | obj = _PyLong_FromLong(val); |
329 | else | 359 | else |
330 | obj = PyLong_FromLongLong(val); | 360 | obj = PyLong_FromLongLong(val); |
331 | } else { | 361 | } else { |
332 | if (val <= LONG_MAX) | 362 | if (val <= LONG_MAX) |
333 | obj = PyInt_FromLong(val); | 363 | obj = _PyLong_FromLong(val); |
334 | else | 364 | else |
335 | obj = PyLong_FromUnsignedLongLong(val); | 365 | obj = PyLong_FromUnsignedLongLong(val); |
336 | } | 366 | } |
@@ -389,9 +419,9 @@ static PyObject *python_process_callchain(struct perf_sample *sample, | |||
389 | pydict_set_item_string_decref(pysym, "end", | 419 | pydict_set_item_string_decref(pysym, "end", |
390 | PyLong_FromUnsignedLongLong(node->sym->end)); | 420 | PyLong_FromUnsignedLongLong(node->sym->end)); |
391 | pydict_set_item_string_decref(pysym, "binding", | 421 | pydict_set_item_string_decref(pysym, "binding", |
392 | PyInt_FromLong(node->sym->binding)); | 422 | _PyLong_FromLong(node->sym->binding)); |
393 | pydict_set_item_string_decref(pysym, "name", | 423 | pydict_set_item_string_decref(pysym, "name", |
394 | PyString_FromStringAndSize(node->sym->name, | 424 | _PyUnicode_FromStringAndSize(node->sym->name, |
395 | node->sym->namelen)); | 425 | node->sym->namelen)); |
396 | pydict_set_item_string_decref(pyelem, "sym", pysym); | 426 | pydict_set_item_string_decref(pyelem, "sym", pysym); |
397 | } | 427 | } |
@@ -406,7 +436,7 @@ static PyObject *python_process_callchain(struct perf_sample *sample, | |||
406 | dsoname = map->dso->name; | 436 | dsoname = map->dso->name; |
407 | } | 437 | } |
408 | pydict_set_item_string_decref(pyelem, "dso", | 438 | pydict_set_item_string_decref(pyelem, "dso", |
409 | PyString_FromString(dsoname)); | 439 | _PyUnicode_FromString(dsoname)); |
410 | } | 440 | } |
411 | 441 | ||
412 | callchain_cursor_advance(&callchain_cursor); | 442 | callchain_cursor_advance(&callchain_cursor); |
@@ -483,16 +513,16 @@ static PyObject *get_perf_sample_dict(struct perf_sample *sample, | |||
483 | if (!dict_sample) | 513 | if (!dict_sample) |
484 | Py_FatalError("couldn't create Python dictionary"); | 514 | Py_FatalError("couldn't create Python dictionary"); |
485 | 515 | ||
486 | pydict_set_item_string_decref(dict, "ev_name", PyString_FromString(perf_evsel__name(evsel))); | 516 | pydict_set_item_string_decref(dict, "ev_name", _PyUnicode_FromString(perf_evsel__name(evsel))); |
487 | pydict_set_item_string_decref(dict, "attr", PyString_FromStringAndSize( | 517 | pydict_set_item_string_decref(dict, "attr", _PyUnicode_FromStringAndSize( |
488 | (const char *)&evsel->attr, sizeof(evsel->attr))); | 518 | (const char *)&evsel->attr, sizeof(evsel->attr))); |
489 | 519 | ||
490 | pydict_set_item_string_decref(dict_sample, "pid", | 520 | pydict_set_item_string_decref(dict_sample, "pid", |
491 | PyInt_FromLong(sample->pid)); | 521 | _PyLong_FromLong(sample->pid)); |
492 | pydict_set_item_string_decref(dict_sample, "tid", | 522 | pydict_set_item_string_decref(dict_sample, "tid", |
493 | PyInt_FromLong(sample->tid)); | 523 | _PyLong_FromLong(sample->tid)); |
494 | pydict_set_item_string_decref(dict_sample, "cpu", | 524 | pydict_set_item_string_decref(dict_sample, "cpu", |
495 | PyInt_FromLong(sample->cpu)); | 525 | _PyLong_FromLong(sample->cpu)); |
496 | pydict_set_item_string_decref(dict_sample, "ip", | 526 | pydict_set_item_string_decref(dict_sample, "ip", |
497 | PyLong_FromUnsignedLongLong(sample->ip)); | 527 | PyLong_FromUnsignedLongLong(sample->ip)); |
498 | pydict_set_item_string_decref(dict_sample, "time", | 528 | pydict_set_item_string_decref(dict_sample, "time", |
@@ -504,17 +534,17 @@ static PyObject *get_perf_sample_dict(struct perf_sample *sample, | |||
504 | set_sample_read_in_dict(dict_sample, sample, evsel); | 534 | set_sample_read_in_dict(dict_sample, sample, evsel); |
505 | pydict_set_item_string_decref(dict, "sample", dict_sample); | 535 | pydict_set_item_string_decref(dict, "sample", dict_sample); |
506 | 536 | ||
507 | pydict_set_item_string_decref(dict, "raw_buf", PyString_FromStringAndSize( | 537 | pydict_set_item_string_decref(dict, "raw_buf", _PyBytes_FromStringAndSize( |
508 | (const char *)sample->raw_data, sample->raw_size)); | 538 | (const char *)sample->raw_data, sample->raw_size)); |
509 | pydict_set_item_string_decref(dict, "comm", | 539 | pydict_set_item_string_decref(dict, "comm", |
510 | PyString_FromString(thread__comm_str(al->thread))); | 540 | _PyUnicode_FromString(thread__comm_str(al->thread))); |
511 | if (al->map) { | 541 | if (al->map) { |
512 | pydict_set_item_string_decref(dict, "dso", | 542 | pydict_set_item_string_decref(dict, "dso", |
513 | PyString_FromString(al->map->dso->name)); | 543 | _PyUnicode_FromString(al->map->dso->name)); |
514 | } | 544 | } |
515 | if (al->sym) { | 545 | if (al->sym) { |
516 | pydict_set_item_string_decref(dict, "symbol", | 546 | pydict_set_item_string_decref(dict, "symbol", |
517 | PyString_FromString(al->sym->name)); | 547 | _PyUnicode_FromString(al->sym->name)); |
518 | } | 548 | } |
519 | 549 | ||
520 | pydict_set_item_string_decref(dict, "callchain", callchain); | 550 | pydict_set_item_string_decref(dict, "callchain", callchain); |
@@ -574,9 +604,9 @@ static void python_process_tracepoint(struct perf_sample *sample, | |||
574 | scripting_context->event_data = data; | 604 | scripting_context->event_data = data; |
575 | scripting_context->pevent = evsel->tp_format->pevent; | 605 | scripting_context->pevent = evsel->tp_format->pevent; |
576 | 606 | ||
577 | context = PyCObject_FromVoidPtr(scripting_context, NULL); | 607 | context = _PyCapsule_New(scripting_context, NULL, NULL); |
578 | 608 | ||
579 | PyTuple_SetItem(t, n++, PyString_FromString(handler_name)); | 609 | PyTuple_SetItem(t, n++, _PyUnicode_FromString(handler_name)); |
580 | PyTuple_SetItem(t, n++, context); | 610 | PyTuple_SetItem(t, n++, context); |
581 | 611 | ||
582 | /* ip unwinding */ | 612 | /* ip unwinding */ |
@@ -585,18 +615,18 @@ static void python_process_tracepoint(struct perf_sample *sample, | |||
585 | Py_INCREF(callchain); | 615 | Py_INCREF(callchain); |
586 | 616 | ||
587 | if (!dict) { | 617 | if (!dict) { |
588 | PyTuple_SetItem(t, n++, PyInt_FromLong(cpu)); | 618 | PyTuple_SetItem(t, n++, _PyLong_FromLong(cpu)); |
589 | PyTuple_SetItem(t, n++, PyInt_FromLong(s)); | 619 | PyTuple_SetItem(t, n++, _PyLong_FromLong(s)); |
590 | PyTuple_SetItem(t, n++, PyInt_FromLong(ns)); | 620 | PyTuple_SetItem(t, n++, _PyLong_FromLong(ns)); |
591 | PyTuple_SetItem(t, n++, PyInt_FromLong(pid)); | 621 | PyTuple_SetItem(t, n++, _PyLong_FromLong(pid)); |
592 | PyTuple_SetItem(t, n++, PyString_FromString(comm)); | 622 | PyTuple_SetItem(t, n++, _PyUnicode_FromString(comm)); |
593 | PyTuple_SetItem(t, n++, callchain); | 623 | PyTuple_SetItem(t, n++, callchain); |
594 | } else { | 624 | } else { |
595 | pydict_set_item_string_decref(dict, "common_cpu", PyInt_FromLong(cpu)); | 625 | pydict_set_item_string_decref(dict, "common_cpu", _PyLong_FromLong(cpu)); |
596 | pydict_set_item_string_decref(dict, "common_s", PyInt_FromLong(s)); | 626 | pydict_set_item_string_decref(dict, "common_s", _PyLong_FromLong(s)); |
597 | pydict_set_item_string_decref(dict, "common_ns", PyInt_FromLong(ns)); | 627 | pydict_set_item_string_decref(dict, "common_ns", _PyLong_FromLong(ns)); |
598 | pydict_set_item_string_decref(dict, "common_pid", PyInt_FromLong(pid)); | 628 | pydict_set_item_string_decref(dict, "common_pid", _PyLong_FromLong(pid)); |
599 | pydict_set_item_string_decref(dict, "common_comm", PyString_FromString(comm)); | 629 | pydict_set_item_string_decref(dict, "common_comm", _PyUnicode_FromString(comm)); |
600 | pydict_set_item_string_decref(dict, "common_callchain", callchain); | 630 | pydict_set_item_string_decref(dict, "common_callchain", callchain); |
601 | } | 631 | } |
602 | for (field = event->format.fields; field; field = field->next) { | 632 | for (field = event->format.fields; field; field = field->next) { |
@@ -615,7 +645,7 @@ static void python_process_tracepoint(struct perf_sample *sample, | |||
615 | } | 645 | } |
616 | if (field->flags & FIELD_IS_STRING && | 646 | if (field->flags & FIELD_IS_STRING && |
617 | is_printable_array(data + offset, len)) { | 647 | is_printable_array(data + offset, len)) { |
618 | obj = PyString_FromString((char *) data + offset); | 648 | obj = _PyUnicode_FromString((char *) data + offset); |
619 | } else { | 649 | } else { |
620 | obj = PyByteArray_FromStringAndSize((const char *) data + offset, len); | 650 | obj = PyByteArray_FromStringAndSize((const char *) data + offset, len); |
621 | field->flags &= ~FIELD_IS_STRING; | 651 | field->flags &= ~FIELD_IS_STRING; |
@@ -668,7 +698,7 @@ static PyObject *tuple_new(unsigned int sz) | |||
668 | static int tuple_set_u64(PyObject *t, unsigned int pos, u64 val) | 698 | static int tuple_set_u64(PyObject *t, unsigned int pos, u64 val) |
669 | { | 699 | { |
670 | #if BITS_PER_LONG == 64 | 700 | #if BITS_PER_LONG == 64 |
671 | return PyTuple_SetItem(t, pos, PyInt_FromLong(val)); | 701 | return PyTuple_SetItem(t, pos, _PyLong_FromLong(val)); |
672 | #endif | 702 | #endif |
673 | #if BITS_PER_LONG == 32 | 703 | #if BITS_PER_LONG == 32 |
674 | return PyTuple_SetItem(t, pos, PyLong_FromLongLong(val)); | 704 | return PyTuple_SetItem(t, pos, PyLong_FromLongLong(val)); |
@@ -677,12 +707,12 @@ static int tuple_set_u64(PyObject *t, unsigned int pos, u64 val) | |||
677 | 707 | ||
678 | static int tuple_set_s32(PyObject *t, unsigned int pos, s32 val) | 708 | static int tuple_set_s32(PyObject *t, unsigned int pos, s32 val) |
679 | { | 709 | { |
680 | return PyTuple_SetItem(t, pos, PyInt_FromLong(val)); | 710 | return PyTuple_SetItem(t, pos, _PyLong_FromLong(val)); |
681 | } | 711 | } |
682 | 712 | ||
683 | static int tuple_set_string(PyObject *t, unsigned int pos, const char *s) | 713 | static int tuple_set_string(PyObject *t, unsigned int pos, const char *s) |
684 | { | 714 | { |
685 | return PyTuple_SetItem(t, pos, PyString_FromString(s)); | 715 | return PyTuple_SetItem(t, pos, _PyUnicode_FromString(s)); |
686 | } | 716 | } |
687 | 717 | ||
688 | static int python_export_evsel(struct db_export *dbe, struct perf_evsel *evsel) | 718 | static int python_export_evsel(struct db_export *dbe, struct perf_evsel *evsel) |
@@ -1029,8 +1059,8 @@ process_stat(struct perf_evsel *counter, int cpu, int thread, u64 tstamp, | |||
1029 | return; | 1059 | return; |
1030 | } | 1060 | } |
1031 | 1061 | ||
1032 | PyTuple_SetItem(t, n++, PyInt_FromLong(cpu)); | 1062 | PyTuple_SetItem(t, n++, _PyLong_FromLong(cpu)); |
1033 | PyTuple_SetItem(t, n++, PyInt_FromLong(thread)); | 1063 | PyTuple_SetItem(t, n++, _PyLong_FromLong(thread)); |
1034 | 1064 | ||
1035 | tuple_set_u64(t, n++, tstamp); | 1065 | tuple_set_u64(t, n++, tstamp); |
1036 | tuple_set_u64(t, n++, count->val); | 1066 | tuple_set_u64(t, n++, count->val); |
@@ -1212,27 +1242,58 @@ static void set_table_handlers(struct tables *tables) | |||
1212 | SET_TABLE_HANDLER(call_return); | 1242 | SET_TABLE_HANDLER(call_return); |
1213 | } | 1243 | } |
1214 | 1244 | ||
1245 | #if PY_MAJOR_VERSION < 3 | ||
1246 | static void _free_command_line(const char **command_line, int num) | ||
1247 | { | ||
1248 | free(command_line); | ||
1249 | } | ||
1250 | #else | ||
1251 | static void _free_command_line(wchar_t **command_line, int num) | ||
1252 | { | ||
1253 | int i; | ||
1254 | for (i = 0; i < num; i++) | ||
1255 | PyMem_RawFree(command_line[i]); | ||
1256 | free(command_line); | ||
1257 | } | ||
1258 | #endif | ||
1259 | |||
1260 | |||
1215 | /* | 1261 | /* |
1216 | * Start trace script | 1262 | * Start trace script |
1217 | */ | 1263 | */ |
1218 | static int python_start_script(const char *script, int argc, const char **argv) | 1264 | static int python_start_script(const char *script, int argc, const char **argv) |
1219 | { | 1265 | { |
1220 | struct tables *tables = &tables_global; | 1266 | struct tables *tables = &tables_global; |
1267 | #if PY_MAJOR_VERSION < 3 | ||
1221 | const char **command_line; | 1268 | const char **command_line; |
1269 | #else | ||
1270 | wchar_t **command_line; | ||
1271 | #endif | ||
1222 | char buf[PATH_MAX]; | 1272 | char buf[PATH_MAX]; |
1223 | int i, err = 0; | 1273 | int i, err = 0; |
1224 | FILE *fp; | 1274 | FILE *fp; |
1225 | 1275 | ||
1276 | #if PY_MAJOR_VERSION < 3 | ||
1226 | command_line = malloc((argc + 1) * sizeof(const char *)); | 1277 | command_line = malloc((argc + 1) * sizeof(const char *)); |
1227 | command_line[0] = script; | 1278 | command_line[0] = script; |
1228 | for (i = 1; i < argc + 1; i++) | 1279 | for (i = 1; i < argc + 1; i++) |
1229 | command_line[i] = argv[i - 1]; | 1280 | command_line[i] = argv[i - 1]; |
1281 | #else | ||
1282 | command_line = malloc((argc + 1) * sizeof(wchar_t *)); | ||
1283 | command_line[0] = Py_DecodeLocale(script, NULL); | ||
1284 | for (i = 1; i < argc + 1; i++) | ||
1285 | command_line[i] = Py_DecodeLocale(argv[i - 1], NULL); | ||
1286 | #endif | ||
1230 | 1287 | ||
1231 | Py_Initialize(); | 1288 | Py_Initialize(); |
1232 | 1289 | ||
1290 | #if PY_MAJOR_VERSION < 3 | ||
1233 | initperf_trace_context(); | 1291 | initperf_trace_context(); |
1234 | |||
1235 | PySys_SetArgv(argc + 1, (char **)command_line); | 1292 | PySys_SetArgv(argc + 1, (char **)command_line); |
1293 | #else | ||
1294 | PyInit_perf_trace_context(); | ||
1295 | PySys_SetArgv(argc + 1, command_line); | ||
1296 | #endif | ||
1236 | 1297 | ||
1237 | fp = fopen(script, "r"); | 1298 | fp = fopen(script, "r"); |
1238 | if (!fp) { | 1299 | if (!fp) { |
@@ -1262,12 +1323,12 @@ static int python_start_script(const char *script, int argc, const char **argv) | |||
1262 | goto error; | 1323 | goto error; |
1263 | } | 1324 | } |
1264 | 1325 | ||
1265 | free(command_line); | 1326 | _free_command_line(command_line, argc + 1); |
1266 | 1327 | ||
1267 | return err; | 1328 | return err; |
1268 | error: | 1329 | error: |
1269 | Py_Finalize(); | 1330 | Py_Finalize(); |
1270 | free(command_line); | 1331 | _free_command_line(command_line, argc + 1); |
1271 | 1332 | ||
1272 | return err; | 1333 | return err; |
1273 | } | 1334 | } |