Received: by 2002:ac0:bc90:0:0:0:0:0 with SMTP id a16csp5643845img; Wed, 27 Mar 2019 12:18:47 -0700 (PDT) X-Google-Smtp-Source: APXvYqxN99cBiEOk0auhGqGA0LFc9Cg5VwSaIJijUt8FZRPEr9SK5oJBF9sabWtKnFsbH91Wrwju X-Received: by 2002:aa7:8b93:: with SMTP id r19mr36206086pfd.163.1553714327161; Wed, 27 Mar 2019 12:18:47 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1553714327; cv=none; d=google.com; s=arc-20160816; b=atazbfcFCwshZGruuQnBnhGoqaOjtehfUM8Ixcr1glV7quWtWHfwSUJAmE3wAPbHG0 7rYXJeqxU5jf1B0xY11mOu3sI54nmiZ5/swOWEj246oJnxgOiZfORDozHhY4ecv1dz0Y S6jVkHYG4sJBr8y+zFxqAutGr5eRy0RJjR12nzmDiqCEyKAkGeDUqJTD5yb3xbyffVjz KKglcljki3+RKy+N4yUsUouLccl3NB5loglzPRUSk3DzXzIyt1p8iFlLiEobYeLabafX NXWjULCbR6HzuKza9v6FembtqJyYFxWne9FKHGoUqGO8lWcLAWeNKJe/aFSKUsdRak8x T/fQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=o36dxH7zhaFZ0J6f5PlX+K1HIo20sSVhUfYIboNvTZQ=; b=iibdSNySRq6nW77L1Ifee1gG0RrO7hQPP4uyg/Oo+VR93RI6D25frzDnV6J3IUl/gJ 7VWVXhsLqffGSOD2EhUc+1Gp3tKQDEwb70HFB0tQCfah1Qo4cD8Bga8v35D1TfmOyvKC HESlXxX/vhWkxs8ime1oTbuoO/54KxXthYbH2fIN3SOz/9P9xNpJG3ooT6XNlIhaTkN3 0hwHJM8jlYYt0Mb6LVp3qnTGvtjdG/ifqvLNRRmKIWwnpTyJHaZxUILNqY0DjQWnlqRM BbpvplRYyQuJKHoRjeKpkZhJOVAn0v23bxEtgmAxPZegerfh8yztIfcDKr+QmnD93jQL Caig== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=MZp70G9R; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id f2si1098606pgv.441.2019.03.27.12.18.32; Wed, 27 Mar 2019 12:18:47 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=MZp70G9R; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2388672AbfC0TQq (ORCPT + 99 others); Wed, 27 Mar 2019 15:16:46 -0400 Received: from mail.kernel.org ([198.145.29.99]:50800 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2388480AbfC0SJB (ORCPT ); Wed, 27 Mar 2019 14:09:01 -0400 Received: from sasha-vm.mshome.net (c-73-47-72-35.hsd1.nh.comcast.net [73.47.72.35]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id CF0E92063F; Wed, 27 Mar 2019 18:08:58 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1553710139; bh=3jg8RvITWTZKqtIe1RgCZUtLQSUkulKLHD+Wz0IFLmc=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=MZp70G9RaZbdptkbdvnCd7v9zIR3MYV1xTaI0EpPmDIncmJDKaCwuwNi/mpRSbCcX AkRm+y5XT0AIoFRYPdCVZUuQdsfu1tawbsSLebdpdsOMw0Pjs9kAXNjBeerVLnm3PR nGF2E+PE5u0lN37Qk1xmDcZ+q2PbbSBC1yGpkSP4= From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Tony Jones , =?UTF-8?q?Jaroslav=20=C5=A0karvada?= , Jonathan Corbet , Ravi Bangoria , Seeteena Thoufeek , Arnaldo Carvalho de Melo , Sasha Levin Subject: [PATCH AUTOSEL 5.0 214/262] perf script python: Add trace_context extension module to sys.modules Date: Wed, 27 Mar 2019 14:01:09 -0400 Message-Id: <20190327180158.10245-214-sashal@kernel.org> X-Mailer: git-send-email 2.19.1 In-Reply-To: <20190327180158.10245-1-sashal@kernel.org> References: <20190327180158.10245-1-sashal@kernel.org> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Patchwork-Hint: Ignore Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Tony Jones [ Upstream commit cc437642255224e4140fed1f3e3156fc8ad91903 ] In Python3, the result of PyModule_Create (called from scripts/python/Perf-Trace-Util/Context.c) is not automatically added to sys.modules. See: https://bugs.python.org/issue4592 Below is the observed behavior without the fix: # ldd /usr/bin/perf | grep -i python libpython3.6m.so.1.0 => /usr/lib64/libpython3.6m.so.1.0 (0x00007f8e1dfb2000) # perf record /bin/false [ perf record: Woken up 1 times to write data ] [ perf record: Captured and wrote 0.015 MB perf.data (17 samples) ] # perf script -g python | cat generated Python script: perf-script.py # perf script -s ./perf-script.py Traceback (most recent call last): File "./perf-script.py", line 18, in from perf_trace_context import * ModuleNotFoundError: No module named 'perf_trace_context' Error running python script ./perf-script.py # Committer notes: To build with python3 use: $ make -C tools/perf PYTHON=python3 Use a non-const variable to pass the 'name' arg to PyImport_AppendInittab(), as python2.6 has that as 'char *', which ends up trowing this in some environments: CC /tmp/build/perf/util/parse-branch-options.o util/scripting-engines/trace-event-python.c: In function 'python_start_script': util/scripting-engines/trace-event-python.c:1520:2: error: passing argument 1 of 'PyImport_AppendInittab' discards 'const' qualifier from pointer target type [-Werror] PyImport_AppendInittab("perf_trace_context", initfunc); ^ In file included from /usr/include/python2.6/Python.h:130:0, from util/scripting-engines/trace-event-python.c:22: /usr/include/python2.6/import.h:54:17: note: expected 'char *' but argument is of type 'const char *' PyAPI_FUNC(int) PyImport_AppendInittab(char *name, void (*initfunc)(void)); ^ cc1: all warnings being treated as errors Signed-off-by: Tony Jones Acked-by: Jiri Olsa Tested-by: Arnaldo Carvalho de Melo Cc: Jaroslav Škarvada Cc: Jonathan Corbet Cc: Ravi Bangoria Cc: Seeteena Thoufeek Fixes: 66dfdff03d19 ("perf tools: Add Python 3 support") Link: http://lkml.kernel.org/r/20190124005229.16146-2-tonyj@suse.de Signed-off-by: Arnaldo Carvalho de Melo Signed-off-by: Sasha Levin --- .../perf/util/scripting-engines/trace-event-python.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/tools/perf/util/scripting-engines/trace-event-python.c b/tools/perf/util/scripting-engines/trace-event-python.c index 2fe3cc43526f..7059d1be2d09 100644 --- a/tools/perf/util/scripting-engines/trace-event-python.c +++ b/tools/perf/util/scripting-engines/trace-event-python.c @@ -1493,34 +1493,40 @@ static void _free_command_line(wchar_t **command_line, int num) static int python_start_script(const char *script, int argc, const char **argv) { struct tables *tables = &tables_global; + PyMODINIT_FUNC (*initfunc)(void); #if PY_MAJOR_VERSION < 3 const char **command_line; #else wchar_t **command_line; #endif - char buf[PATH_MAX]; + /* + * Use a non-const name variable to cope with python 2.6's + * PyImport_AppendInittab prototype + */ + char buf[PATH_MAX], name[19] = "perf_trace_context"; int i, err = 0; FILE *fp; #if PY_MAJOR_VERSION < 3 + initfunc = initperf_trace_context; command_line = malloc((argc + 1) * sizeof(const char *)); command_line[0] = script; for (i = 1; i < argc + 1; i++) command_line[i] = argv[i - 1]; #else + initfunc = PyInit_perf_trace_context; command_line = malloc((argc + 1) * sizeof(wchar_t *)); command_line[0] = Py_DecodeLocale(script, NULL); for (i = 1; i < argc + 1; i++) command_line[i] = Py_DecodeLocale(argv[i - 1], NULL); #endif + PyImport_AppendInittab(name, initfunc); Py_Initialize(); #if PY_MAJOR_VERSION < 3 - initperf_trace_context(); PySys_SetArgv(argc + 1, (char **)command_line); #else - PyInit_perf_trace_context(); PySys_SetArgv(argc + 1, command_line); #endif -- 2.19.1