From patchwork Wed Jan 2 17:01:36 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tom Tromey X-Patchwork-Id: 30937 Received: (qmail 1876 invoked by alias); 2 Jan 2019 17:01:50 -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 1403 invoked by uid 89); 2 Jan 2019 17:01:50 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-26.9 required=5.0 tests=BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, RCVD_IN_DNSWL_NONE, SPF_HELO_PASS, TIME_LIMIT_EXCEEDED autolearn=unavailable version=3.3.2 spammy= X-HELO: gateway34.websitewelcome.com Received: from gateway34.websitewelcome.com (HELO gateway34.websitewelcome.com) (192.185.149.105) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Wed, 02 Jan 2019 17:01:40 +0000 Received: from cm13.websitewelcome.com (cm13.websitewelcome.com [100.42.49.6]) by gateway34.websitewelcome.com (Postfix) with ESMTP id D90572DA88D for ; Wed, 2 Jan 2019 11:01:38 -0600 (CST) Received: from box5379.bluehost.com ([162.241.216.53]) by cmsmtp with SMTP id ejtigrfEiYTGMejtigeJPi; Wed, 02 Jan 2019 11:01:38 -0600 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=tromey.com; s=default; h=Message-Id:Date:Subject:Cc:To:From:Sender:Reply-To:MIME-Version :Content-Type:Content-Transfer-Encoding:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: In-Reply-To:References:List-Id:List-Help:List-Unsubscribe:List-Subscribe: List-Post:List-Owner:List-Archive; bh=jBWTt5UZOypmCysOP7XXsThfltgpSliwg6iwSkg1lko=; b=fMHn86n/vumx5GSb8Ym4x5vEcm E7904isxT9pBHsg24GCfoEaTEEueKU0gist3TWextUu2yAInnuX+TE7+HOFd+ETU+aMHCqxUZ/HAa WXKcQ2m3rXgZFSr6Cux7bB5j6; Received: from [50.236.237.38] (port=44226 helo=bapiya.TCHFunNetwork.pvt) by box5379.bluehost.com with esmtpsa (TLSv1.2:ECDHE-RSA-AES256-GCM-SHA384:256) (Exim 4.91) (envelope-from ) id 1gejti-003vCG-Mk; Wed, 02 Jan 2019 11:01:38 -0600 From: Tom Tromey To: gdb-patches@sourceware.org Cc: Tom Tromey Subject: [PATCH] Change inferior_to_inferior_object to return a gdbpy_ref Date: Wed, 2 Jan 2019 10:01:36 -0700 Message-Id: <20190102170136.437-1-tom@tromey.com> Most callers of inferior_to_inferior_object already use a gdbpy_ref, so this changes inferior_to_inferior_object to return one. Doing this revealed that create_thread_object was not correctly handling the case where inferior_to_inferior_object failed, so this patch fixes this as well. gdb/ChangeLog 2019-01-02 Tom Tromey * python/python-internal.h (inferior_to_inferior_object): Change return type. * python/py-exitedevent.c (create_exited_event_object): Update. * python/py-inferior.c (inferior_to_inferior_object): Return gdbpy_ref. (python_new_inferior, python_inferior_deleted) (thread_to_thread_object, delete_thread_object) (build_inferior_list, gdbpy_selected_inferior): Update. * python/py-infthread.c (create_thread_object): Update. Also fail if inferior_to_inferior_object fails. --- gdb/ChangeLog | 13 +++++++++++++ gdb/python/py-exitedevent.c | 2 +- gdb/python/py-inferior.c | 24 +++++++++++------------- gdb/python/py-infthread.c | 6 +++++- gdb/python/python-internal.h | 2 +- 5 files changed, 31 insertions(+), 16 deletions(-) diff --git a/gdb/python/py-exitedevent.c b/gdb/python/py-exitedevent.c index 474aa250ce..5e9dabf15e 100644 --- a/gdb/python/py-exitedevent.c +++ b/gdb/python/py-exitedevent.c @@ -39,7 +39,7 @@ create_exited_event_object (const LONGEST *exit_code, struct inferior *inf) return NULL; } - gdbpy_ref inf_obj (inferior_to_inferior_object (inf)); + gdbpy_ref inf_obj = inferior_to_inferior_object (inf); if (inf_obj == NULL || evpy_add_attribute (exited_event.get (), "inferior", (PyObject *) inf_obj.get ()) < 0) diff --git a/gdb/python/py-inferior.c b/gdb/python/py-inferior.c index b1b9e947ae..63b13d46ec 100644 --- a/gdb/python/py-inferior.c +++ b/gdb/python/py-inferior.c @@ -208,7 +208,7 @@ python_new_objfile (struct objfile *objfile) return it and increment the reference count, otherwise, create it. Return NULL on failure. */ -inferior_object * +gdbpy_ref inferior_to_inferior_object (struct inferior *inferior) { inferior_object *inf_obj; @@ -218,7 +218,7 @@ inferior_to_inferior_object (struct inferior *inferior) { inf_obj = PyObject_New (inferior_object, &inferior_object_type); if (!inf_obj) - return NULL; + return NULL; inf_obj->inferior = inferior; inf_obj->threads = NULL; @@ -227,13 +227,11 @@ inferior_to_inferior_object (struct inferior *inferior) /* PyObject_New initializes the new object with a refcount of 1. This counts for the reference we are keeping in the inferior data. */ set_inferior_data (inferior, infpy_inf_data_key, inf_obj); - } /* We are returning a new reference. */ - Py_INCREF ((PyObject *)inf_obj); - - return inf_obj; + gdb_assert (inf_obj != nullptr); + return gdbpy_ref::new_reference (inf_obj); } /* Called when a new inferior is created. Notifies any Python event @@ -249,7 +247,7 @@ python_new_inferior (struct inferior *inf) if (evregpy_no_listeners_p (gdb_py_events.new_inferior)) return; - gdbpy_ref inf_obj (inferior_to_inferior_object (inf)); + gdbpy_ref inf_obj = inferior_to_inferior_object (inf); if (inf_obj == NULL) { gdbpy_print_stack (); @@ -277,7 +275,7 @@ python_inferior_deleted (struct inferior *inf) if (evregpy_no_listeners_p (gdb_py_events.inferior_deleted)) return; - gdbpy_ref inf_obj (inferior_to_inferior_object (inf)); + gdbpy_ref inf_obj = inferior_to_inferior_object (inf); if (inf_obj == NULL) { gdbpy_print_stack (); @@ -295,7 +293,7 @@ python_inferior_deleted (struct inferior *inf) gdbpy_ref<> thread_to_thread_object (thread_info *thr) { - gdbpy_ref inf_obj (inferior_to_inferior_object (thr->inf)); + gdbpy_ref inf_obj = inferior_to_inferior_object (thr->inf); if (inf_obj == NULL) return NULL; @@ -358,8 +356,7 @@ delete_thread_object (struct thread_info *tp, int ignore) gdbpy_enter enter_py (python_gdbarch, python_language); - gdbpy_ref inf_obj - ((inferior_object *) inferior_to_inferior_object (tp->inf)); + gdbpy_ref inf_obj = inferior_to_inferior_object (tp->inf); if (inf_obj == NULL) return; @@ -466,7 +463,7 @@ static int build_inferior_list (struct inferior *inf, void *arg) { PyObject *list = (PyObject *) arg; - gdbpy_ref inferior (inferior_to_inferior_object (inf)); + gdbpy_ref inferior = inferior_to_inferior_object (inf); if (inferior == NULL) return 0; @@ -925,7 +922,8 @@ py_free_inferior (struct inferior *inf, void *datum) PyObject * gdbpy_selected_inferior (PyObject *self, PyObject *args) { - return (PyObject *) inferior_to_inferior_object (current_inferior ()); + return ((PyObject *) + inferior_to_inferior_object (current_inferior ()).release ()); } int diff --git a/gdb/python/py-infthread.c b/gdb/python/py-infthread.c index e15e2810cd..bf90d08ae6 100644 --- a/gdb/python/py-infthread.c +++ b/gdb/python/py-infthread.c @@ -41,12 +41,16 @@ create_thread_object (struct thread_info *tp) { thread_object *thread_obj; + gdbpy_ref inf_obj = inferior_to_inferior_object (tp->inf); + if (inf_obj == NULL) + return NULL; + thread_obj = PyObject_New (thread_object, &thread_object_type); if (!thread_obj) return NULL; thread_obj->thread = tp; - thread_obj->inf_obj = (PyObject *) inferior_to_inferior_object (tp->inf); + thread_obj->inf_obj = (PyObject *) inf_obj.release (); return thread_obj; } diff --git a/gdb/python/python-internal.h b/gdb/python/python-internal.h index d8d345734d..51919b7b85 100644 --- a/gdb/python/python-internal.h +++ b/gdb/python/python-internal.h @@ -521,7 +521,7 @@ PyObject *gdbarch_to_arch_object (struct gdbarch *gdbarch); thread_object *create_thread_object (struct thread_info *tp); gdbpy_ref<> thread_to_thread_object (thread_info *thr);; -inferior_object *inferior_to_inferior_object (inferior *inf); +gdbpy_ref inferior_to_inferior_object (inferior *inf); const struct block *block_object_to_block (PyObject *obj); struct symbol *symbol_object_to_symbol (PyObject *obj);