aboutsummaryrefslogtreecommitdiffstats
path: root/tools/perf/util/scripting-engines/trace-event-python.c
diff options
context:
space:
mode:
authorJaroslav Škarvada <jskarvad@redhat.com>2018-01-19 15:56:41 -0500
committerArnaldo Carvalho de Melo <acme@redhat.com>2018-02-19 10:28:23 -0500
commit66dfdff03d196e51322c6a85c0d8db8bb2bdd655 (patch)
treeebc6c1b8a68051967e31010c4398aa6513f765a4 /tools/perf/util/scripting-engines/trace-event-python.c
parentd2ed5d2bdc5cd30b44dc52c44c63f08c0a31b845 (diff)
perf tools: Add Python 3 support
Added Python 3 support while keeping Python 2.7 compatibility. Committer notes: This doesn't make it to auto detect python 3, one has to explicitely ask it to build with python 3 devel files, here are the instructions provided by Jaroslav: --- $ cp -a tools/perf tools/python3-perf $ make V=1 prefix=/usr -C tools/perf PYTHON=/usr/bin/python2 all $ make V=1 prefix=/usr -C tools/python3-perf PYTHON=/usr/bin/python3 all $ make V=1 prefix=/usr -C tools/python3-perf PYTHON=/usr/bin/python3 DESTDIR=%{buildroot} install-python_ext $ make V=1 prefix=/usr -C tools/perf PYTHON=/usr/bin/python2 DESTDIR=%{buildroot} install-python_ext --- We need to make this automatic, just like the existing tests for checking if the python2 devel files are in place, allowing the build with python3 if available, fallbacking to python2 and then just disabling it if none are available. So, using the PYTHON variable to build it using O= we get: Before this patch: $ rpm -q python3 python3-devel python3-3.6.4-7.fc27.x86_64 python3-devel-3.6.4-7.fc27.x86_64 $ rm -rf /tmp/build/perf/ ; mkdir -p /tmp/build/perf ; make O=/tmp/build/perf PYTHON=/usr/bin/python3 -C tools/perf install-bin make: Entering directory '/home/acme/git/linux/tools/perf' <SNIP> Makefile.config:670: Python 3 is not yet supported; please set Makefile.config:671: PYTHON and/or PYTHON_CONFIG appropriately. Makefile.config:672: If you also have Python 2 installed, then Makefile.config:673: try something like: Makefile.config:674: Makefile.config:675: make PYTHON=python2 Makefile.config:676: Makefile.config:677: Otherwise, disable Python support entirely: Makefile.config:678: Makefile.config:679: make NO_LIBPYTHON=1 Makefile.config:680: Makefile.config:681: *** . Stop. make[1]: *** [Makefile.perf:212: sub-make] Error 2 make: *** [Makefile:110: install-bin] Error 2 make: Leaving directory '/home/acme/git/linux/tools/perf' $ After: $ make O=/tmp/build/perf PYTHON=python3 -C tools/perf install-bin $ ldd ~/bin/perf | grep python libpython3.6m.so.1.0 => /lib64/libpython3.6m.so.1.0 (0x00007f58a31e8000) $ rpm -qf /lib64/libpython3.6m.so.1.0 python3-libs-3.6.4-7.fc27.x86_64 $ Now verify that when using the binding the right ELF file is loaded, using perf trace: $ perf trace -e open* perf test python 0.051 ( 0.016 ms): perf/3927 openat(dfd: CWD, filename: /etc/ld.so.cache, flags: CLOEXEC ) = 3 <SNIP> 18: 'import perf' in python : 8.849 ( 0.013 ms): sh/3929 openat(dfd: CWD, filename: /etc/ld.so.cache, flags: CLOEXEC ) = 3 <SNIP> 25.572 ( 0.008 ms): python3/3931 openat(dfd: CWD, filename: /tmp/build/perf/python/perf.cpython-36m-x86_64-linux-gnu.so, flags: CLOEXEC) = 3 <SNIP> Ok <SNIP> $ And using tools/perf/python/twatch.py, to show PERF_RECORD_ metaevents: $ python3 tools/perf/python/twatch.py cpu: 3, pid: 16060, tid: 16060 { type: fork, pid: 5207, ppid: 16060, tid: 5207, ptid: 16060, time: 10798513015459} cpu: 3, pid: 16060, tid: 16060 { type: fork, pid: 5208, ppid: 16060, tid: 5208, ptid: 16060, time: 10798513562503} cpu: 0, pid: 5208, tid: 5208 { type: comm, pid: 5208, tid: 5208, comm: grep } cpu: 2, pid: 5207, tid: 5207 { type: comm, pid: 5207, tid: 5207, comm: ps } cpu: 2, pid: 5207, tid: 5207 { type: exit, pid: 5207, ppid: 5207, tid: 5207, ptid: 5207, time: 10798551337484} cpu: 3, pid: 5208, tid: 5208 { type: exit, pid: 5208, ppid: 5208, tid: 5208, ptid: 5208, time: 10798551292153} cpu: 3, pid: 601, tid: 601 { type: fork, pid: 5209, ppid: 601, tid: 5209, ptid: 601, time: 10801779977324} ^CTraceback (most recent call last): File "tools/perf/python/twatch.py", line 68, in <module> main() File "tools/perf/python/twatch.py", line 40, in main evlist.poll(timeout = -1) KeyboardInterrupt $ # ps ax|grep twatch 5197 pts/8 S+ 0:00 python3 tools/perf/python/twatch.py # ls -la /proc/5197/smaps -r--r--r--. 1 acme acme 0 Feb 19 13:14 /proc/5197/smaps # grep python /proc/5197/smaps 558111307000-558111309000 r-xp 00000000 fd:00 3151710 /usr/bin/python3.6 558111508000-558111509000 r--p 00001000 fd:00 3151710 /usr/bin/python3.6 558111509000-55811150a000 rw-p 00002000 fd:00 3151710 /usr/bin/python3.6 7ffad6fc1000-7ffad7008000 r-xp 00000000 00:2d 220196 /tmp/build/perf/python/perf.cpython-36m-x86_64-linux-gnu.so 7ffad7008000-7ffad7207000 ---p 00047000 00:2d 220196 /tmp/build/perf/python/perf.cpython-36m-x86_64-linux-gnu.so 7ffad7207000-7ffad7208000 r--p 00046000 00:2d 220196 /tmp/build/perf/python/perf.cpython-36m-x86_64-linux-gnu.so 7ffad7208000-7ffad7215000 rw-p 00047000 00:2d 220196 /tmp/build/perf/python/perf.cpython-36m-x86_64-linux-gnu.so 7ffadea77000-7ffaded3d000 r-xp 00000000 fd:00 3151795 /usr/lib64/libpython3.6m.so.1.0 7ffaded3d000-7ffadef3c000 ---p 002c6000 fd:00 3151795 /usr/lib64/libpython3.6m.so.1.0 7ffadef3c000-7ffadef42000 r--p 002c5000 fd:00 3151795 /usr/lib64/libpython3.6m.so.1.0 7ffadef42000-7ffadefa5000 rw-p 002cb000 fd:00 3151795 /usr/lib64/libpython3.6m.so.1.0 # And with this patch, but building normally, without specifying the PYTHON=python3 part, which will make it use python2 if its devel files are available, like in this test: $ make O=/tmp/build/perf -C tools/perf install-bin $ ldd ~/bin/perf | grep python libpython2.7.so.1.0 => /lib64/libpython2.7.so.1.0 (0x00007f6a44410000) $ ldd /tmp/build/perf/python_ext_build/lib/perf.so | grep python libpython2.7.so.1.0 => /lib64/libpython2.7.so.1.0 (0x00007fed28a2c000) $ [acme@jouet perf]$ tools/perf/python/twatch.py cpu: 0, pid: 2817, tid: 2817 { type: fork, pid: 2817, ppid: 2817, tid: 8910, ptid: 2817, time: 11126454335306} cpu: 0, pid: 2817, tid: 2817 { type: comm, pid: 2817, tid: 8910, comm: worker } $ ps ax | grep twatch.py 8909 pts/8 S+ 0:00 /usr/bin/python tools/perf/python/twatch.py $ grep python /proc/8909/smaps 5579de658000-5579de659000 r-xp 00000000 fd:00 3156044 /usr/bin/python2.7 5579de858000-5579de859000 r--p 00000000 fd:00 3156044 /usr/bin/python2.7 5579de859000-5579de85a000 rw-p 00001000 fd:00 3156044 /usr/bin/python2.7 7f0de01f7000-7f0de023e000 r-xp 00000000 00:2d 230695 /tmp/build/perf/python/perf.so 7f0de023e000-7f0de043d000 ---p 00047000 00:2d 230695 /tmp/build/perf/python/perf.so 7f0de043d000-7f0de043e000 r--p 00046000 00:2d 230695 /tmp/build/perf/python/perf.so 7f0de043e000-7f0de044b000 rw-p 00047000 00:2d 230695 /tmp/build/perf/python/perf.so 7f0de6f0f000-7f0de6f13000 r-xp 00000000 fd:00 134975 /usr/lib64/python2.7/lib-dynload/_localemodule.so 7f0de6f13000-7f0de7113000 ---p 00004000 fd:00 134975 /usr/lib64/python2.7/lib-dynload/_localemodule.so 7f0de7113000-7f0de7114000 r--p 00004000 fd:00 134975 /usr/lib64/python2.7/lib-dynload/_localemodule.so 7f0de7114000-7f0de7115000 rw-p 00005000 fd:00 134975 /usr/lib64/python2.7/lib-dynload/_localemodule.so 7f0de7e73000-7f0de8052000 r-xp 00000000 fd:00 3173292 /usr/lib64/libpython2.7.so.1.0 7f0de8052000-7f0de8251000 ---p 001df000 fd:00 3173292 /usr/lib64/libpython2.7.so.1.0 7f0de8251000-7f0de8255000 r--p 001de000 fd:00 3173292 /usr/lib64/libpython2.7.so.1.0 7f0de8255000-7f0de8291000 rw-p 001e2000 fd:00 3173292 /usr/lib64/libpython2.7.so.1.0 $ Signed-off-by: Jaroslav Škarvada <jskarvad@redhat.com> Tested-by: Arnaldo Carvalho de Melo <acme@redhat.com> Cc: Adrian Hunter <adrian.hunter@intel.com> Cc: David Ahern <dsahern@gmail.com> Cc: Jiri Olsa <jolsa@kernel.org> Cc: Namhyung Kim <namhyung@kernel.org> Cc: Wang Nan <wangnan0@huawei.com> LPU-Reference: 20180119205641.24242-1-jskarvad@redhat.com Link: https://lkml.kernel.org/n/tip-8d7dt9kqp83vsz25hagug8fu@git.kernel.org [ Removed explicit check for python version, allowing it to really build with python3 ] Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
Diffstat (limited to 'tools/perf/util/scripting-engines/trace-event-python.c')
-rw-r--r--tools/perf/util/scripting-engines/trace-event-python.c147
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
52PyMODINIT_FUNC initperf_trace_context(void); 66PyMODINIT_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
81PyMODINIT_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)
668static int tuple_set_u64(PyObject *t, unsigned int pos, u64 val) 698static 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
678static int tuple_set_s32(PyObject *t, unsigned int pos, s32 val) 708static 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
683static int tuple_set_string(PyObject *t, unsigned int pos, const char *s) 713static 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
688static int python_export_evsel(struct db_export *dbe, struct perf_evsel *evsel) 718static 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
1246static void _free_command_line(const char **command_line, int num)
1247{
1248 free(command_line);
1249}
1250#else
1251static 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 */
1218static int python_start_script(const char *script, int argc, const char **argv) 1264static 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;
1268error: 1329error:
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}