From patchwork Wed May 29 08:47:52 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Willgerodt, Felix" X-Patchwork-Id: 32885 Received: (qmail 11236 invoked by alias); 29 May 2019 08:48:25 -0000 Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sourceware.org Delivered-To: mailing list gdb-patches@sourceware.org Received: (qmail 10903 invoked by uid 89); 29 May 2019 08:48:18 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-25.2 required=5.0 tests=AWL, BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3 autolearn=ham version=3.3.1 spammy=H*r:LOCAL, gil, history X-HELO: mga01.intel.com Received: from mga01.intel.com (HELO mga01.intel.com) (192.55.52.88) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Wed, 29 May 2019 08:48:16 +0000 Received: from orsmga004.jf.intel.com ([10.7.209.38]) by fmsmga101.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 29 May 2019 01:48:14 -0700 Received: from irvmail001.ir.intel.com ([163.33.26.43]) by orsmga004.jf.intel.com with ESMTP; 29 May 2019 01:48:13 -0700 Received: from ulvlx001.iul.intel.com (ulvlx001.iul.intel.com [172.28.207.17]) by irvmail001.ir.intel.com (8.14.3/8.13.6/MailSET/Hub) with ESMTP id x4T8mCaE007674; Wed, 29 May 2019 09:48:12 +0100 Received: from ulvlx001.iul.intel.com (localhost [127.0.0.1]) by ulvlx001.iul.intel.com with ESMTP id x4T8mCDB030826; Wed, 29 May 2019 10:48:12 +0200 Received: (from fwillger@localhost) by ulvlx001.iul.intel.com with LOCAL id x4T8mC46030822; Wed, 29 May 2019 10:48:12 +0200 From: felix.willgerodt@intel.com To: gdb-patches@sourceware.org Cc: markus.t.metzger@intel.com, Felix Willgerodt Subject: [PATCH 09/10] btrace, python: Enable calling the ptwrite listener. Date: Wed, 29 May 2019 10:47:52 +0200 Message-Id: <1559119673-30516-10-git-send-email-felix.willgerodt@intel.com> In-Reply-To: <1559119673-30516-1-git-send-email-felix.willgerodt@intel.com> References: <1559119673-30516-1-git-send-email-felix.willgerodt@intel.com> X-IsSubscribed: yes From: Felix Willgerodt Adding a new function to btinfo that allows to call the python ptwrite listener from inside GDB. 2019-05-29 Felix Willgerodt gdb/ChangeLog: * btrace.h (btrace_thread_info): New member ptw_callback_fun. * python/py-record-btrace.c (recpy_call_listener): New function. (recpy_initialize_listener): Save recpy_call_listener in btinfo. --- gdb/btrace.h | 8 +++++ gdb/python/py-record-btrace.c | 63 +++++++++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+) diff --git a/gdb/btrace.h b/gdb/btrace.h index 7a80f5a44e1..7ad3cb65a28 100644 --- a/gdb/btrace.h +++ b/gdb/btrace.h @@ -354,6 +354,14 @@ struct btrace_thread_info stepping through the execution history. */ std::vector aux_data; + /* Function pointer to the ptwrite callback. Returns the string returned + by the ptwrite listener function or nullptr if no string is supposed to + be printed. */ + gdb::unique_xmalloc_ptr (*ptw_callback_fun) ( + const uint64_t *payload, + const uint64_t *ip, + const void *ptw_listener); + /* PyObject pointer to the ptwrite listener function. */ void *ptw_listener = nullptr; diff --git a/gdb/python/py-record-btrace.c b/gdb/python/py-record-btrace.c index c7ad47d64a0..42a0c29a348 100644 --- a/gdb/python/py-record-btrace.c +++ b/gdb/python/py-record-btrace.c @@ -772,6 +772,68 @@ recpy_bt_function_call_history (PyObject *self, void *closure) return btpy_list_new (tinfo, first, last, 1, &recpy_func_type); } +/* Calling the ptwrite listener. Returns a pointer to the string that will be + printed or nullptr if nothing should be printed. */ +gdb::unique_xmalloc_ptr +recpy_call_listener (const uint64_t *payload, const uint64_t *ip, + const void *ptw_listener) +{ + if ((PyObject *) ptw_listener == Py_None) + return nullptr; + else if ((PyObject *) ptw_listener == nullptr) + error (_("No valid ptwrite listener.")); + + /* As Python is started as a seperate thread, we need to + acquire the GIL to safely call the listener function. */ + PyGILState_STATE gstate = PyGILState_Ensure (); + + PyObject *py_ip = Py_None; + PyObject *py_payload = PyLong_FromUnsignedLongLong (*payload); + Py_INCREF (Py_None); + + if (ip != nullptr) + py_ip = PyLong_FromUnsignedLongLong (*ip); + + PyObject *py_result = PyObject_CallFunctionObjArgs ((PyObject *) ptw_listener, + py_payload, py_ip, NULL); + + if (PyErr_Occurred ()) + { + gdbpy_print_stack (); + gdb_Py_DECREF (py_ip); + gdb_Py_DECREF (py_payload); + gdb_Py_DECREF (py_result); + PyGILState_Release (gstate); + error (_("Error while executing Python code.")); + } + + gdb_Py_DECREF (py_ip); + gdb_Py_DECREF (py_payload); + + if (py_result == Py_None) + { + gdb_Py_DECREF (py_result); + PyGILState_Release (gstate); + return nullptr; + } + + gdb::unique_xmalloc_ptr resultstring = gdbpy_obj_to_string (py_result); + + if (PyErr_Occurred ()) + { + gdbpy_print_stack (); + gdb_Py_DECREF (py_result); + PyGILState_Release (gstate); + error (_("Error while executing Python code.")); + } + + if (py_result != nullptr) + gdb_Py_DECREF (py_result); + PyGILState_Release (gstate); + + return resultstring; +} + /* Helper function returning the current ptwrite listener. Returns nullptr in case of errors. */ @@ -807,6 +869,7 @@ recpy_initialize_listener (ptid_t inferior_ptid) gdb_assert (tinfo != nullptr); + tinfo->btrace.ptw_callback_fun = &recpy_call_listener; tinfo->btrace.ptw_listener = get_ptwrite_listener (); return (PyObject *) tinfo->btrace.ptw_listener;