From patchwork Thu Mar 21 04:46:49 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kevin Buettner X-Patchwork-Id: 31930 Received: (qmail 4771 invoked by alias); 21 Mar 2019 04:46:52 -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 4757 invoked by uid 89); 21 Mar 2019 04:46:52 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-16.0 required=5.0 tests=AWL, BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, SPF_HELO_PASS autolearn=ham version=3.3.1 spammy= X-HELO: mx1.redhat.com Received: from mx1.redhat.com (HELO mx1.redhat.com) (209.132.183.28) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Thu, 21 Mar 2019 04:46:50 +0000 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id CDD553688E for ; Thu, 21 Mar 2019 04:46:49 +0000 (UTC) Received: from f29-4.lan (ovpn-117-184.phx2.redhat.com [10.3.117.184]) by smtp.corp.redhat.com (Postfix) with ESMTPS id AB74D60141 for ; Thu, 21 Mar 2019 04:46:49 +0000 (UTC) Date: Wed, 20 Mar 2019 21:46:49 -0700 From: Kevin Buettner To: gdb-patches@sourceware.org Subject: [PATCH v3 3/6] Support buffer objects as handles in Inferior.thread_from_thread_handle() Message-ID: <20190320214649.5d5a2fd3@f29-4.lan> In-Reply-To: <20190320213250.718c4c19@f29-4.lan> References: <20190320213250.718c4c19@f29-4.lan> MIME-Version: 1.0 X-IsSubscribed: yes InferiorThread.handle() returns a python bytes object. We'd like to be able to pass the output of this function, a thread handle, to Inferior.thread_from_thread_handle(). Up to now, thread_from_thread_handle() expects to receive a gdb.Value input. This commit adds support to also allow a python buffer object to be passed as the handle. infpy_thread_from_thread_handle() calls find_thread_by_handle() which has the obvious functionality. It used to pass the thread handle via a struct value pointer. I've revised this interface to instead pass a gdb::array_view object. (Thanks to Tom Tromey for suggesting this data structure over an earlier version which passed a gdb_byte pointer and length.) gdb/ChangeLog: * gdbthread.h (find_thread_by_handle): Revise declaration. * thread.c (find_thread_by_handle): Likewise. Adjust implementation too. * python/py-inferior.c (infpy_thread_from_thread_handle): Add support for buffer objects as handles. --- gdb/gdbthread.h | 4 ++-- gdb/python/py-inferior.c | 25 ++++++++++++++++++++++--- gdb/thread.c | 10 +++++----- 3 files changed, 29 insertions(+), 10 deletions(-) diff --git a/gdb/gdbthread.h b/gdb/gdbthread.h index 37c7910635..d89cadbab9 100644 --- a/gdb/gdbthread.h +++ b/gdb/gdbthread.h @@ -486,8 +486,8 @@ extern struct thread_info *find_thread_ptid (inferior *inf, ptid_t ptid); struct thread_info *find_thread_global_id (int global_id); /* Find thread by thread library specific handle in inferior INF. */ -struct thread_info *find_thread_by_handle (struct value *thread_handle, - struct inferior *inf); +struct thread_info *find_thread_by_handle + (gdb::array_view handle, struct inferior *inf); /* Finds the first thread of the specified inferior. */ extern struct thread_info *first_thread_of_inferior (inferior *inf); diff --git a/gdb/python/py-inferior.c b/gdb/python/py-inferior.c index 72fbf6d90b..7129815b21 100644 --- a/gdb/python/py-inferior.c +++ b/gdb/python/py-inferior.c @@ -774,7 +774,25 @@ infpy_thread_from_thread_handle (PyObject *self, PyObject *args, PyObject *kw) if (! gdb_PyArg_ParseTupleAndKeywords (args, kw, "O", keywords, &handle_obj)) return NULL; - if (!gdbpy_is_value_object (handle_obj)) + const gdb_byte *bytes; + size_t bytes_len; + Py_buffer_up buffer_up; + Py_buffer py_buf; + + if (PyObject_CheckBuffer (handle_obj) + && PyObject_GetBuffer (handle_obj, &py_buf, PyBUF_SIMPLE) == 0) + { + buffer_up.reset (&py_buf); + bytes = (const gdb_byte *) py_buf.buf; + bytes_len = py_buf.len; + } + else if (gdbpy_is_value_object (handle_obj)) + { + struct value *val = value_object_to_value (handle_obj); + bytes = value_contents_all (val); + bytes_len = TYPE_LENGTH (value_type (val)); + } + else { PyErr_SetString (PyExc_TypeError, _("Argument 'handle_obj' must be a thread handle object.")); @@ -785,9 +803,10 @@ infpy_thread_from_thread_handle (PyObject *self, PyObject *args, PyObject *kw) TRY { struct thread_info *thread_info; - struct value *val = value_object_to_value (handle_obj); - thread_info = find_thread_by_handle (val, inf_obj->inferior); + thread_info = find_thread_by_handle + (gdb::array_view (bytes, bytes_len), + inf_obj->inferior); if (thread_info != NULL) return thread_to_thread_object (thread_info).release (); } diff --git a/gdb/thread.c b/gdb/thread.c index 0879549b8a..8f6b762add 100644 --- a/gdb/thread.c +++ b/gdb/thread.c @@ -541,12 +541,12 @@ find_thread_ptid (inferior *inf, ptid_t ptid) /* See gdbthread.h. */ struct thread_info * -find_thread_by_handle (struct value *thread_handle, struct inferior *inf) +find_thread_by_handle (gdb::array_view handle, + struct inferior *inf) { - return target_thread_handle_to_thread_info - (value_contents_all (thread_handle), - TYPE_LENGTH (value_type (thread_handle)), - inf); + return target_thread_handle_to_thread_info (handle.data (), + handle.size (), + inf); } /*