From patchwork Sun Oct 2 16:53:04 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Burgess X-Patchwork-Id: 58276 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id CE58B385C412 for ; Sun, 2 Oct 2022 16:53:44 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org CE58B385C412 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1664729624; bh=wiRf73rNFiSR8bW+bKdIB+PezK04bet2ZjZg9zgcCXs=; h=To:Subject:Date:In-Reply-To:References:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=Ry2kQWSHTeCvPFEHR/hTyECF02S1uVE1YFcoMPcTBhOwHjJdhQkRsRK3qtx3BxTXF mVwq6atYKXeZDy/XqMq7ypv0z/ILjKMYlNxzEcZCjAPmqiVuEh2g9troVhokUHuyp/ JjEArQIlJ/kZwXUHTLenovFPaH528+g/vimcjTmM= X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id 350463857379 for ; Sun, 2 Oct 2022 16:53:18 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 350463857379 Received: from mail-wm1-f70.google.com (mail-wm1-f70.google.com [209.85.128.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-594--YogTC8PP-Whf9vMFrDbuA-1; Sun, 02 Oct 2022 12:53:14 -0400 X-MC-Unique: -YogTC8PP-Whf9vMFrDbuA-1 Received: by mail-wm1-f70.google.com with SMTP id n32-20020a05600c3ba000b003b5054c71faso7802985wms.9 for ; Sun, 02 Oct 2022 09:53:14 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date; bh=wiRf73rNFiSR8bW+bKdIB+PezK04bet2ZjZg9zgcCXs=; b=2EnI/Fj8bPSZmHrKEw6+Noyaes03vjU594uiz0/ws2iUUXOqC55tJWDPTXTImNKnpE DPz8CJ7Fjsv5Qx/S06MGrQLKBEUyysqafkOCKva6UUAc/ZLJNbS5U8IUgfxNVaUoWPoZ px9AW2bd21SaLuHRUbL45SY8F0lVMaHrW5S86C6JXLStbLbigTJZaGgbAPozMbWEHJU2 k7O4GSFsIH5h45I9nT1dGVHqHXpKm+ToctNQrMfyZNp6bLO5d9LA4U3nrfGF/+mUWni0 Yd9sbTDsN/rzyD8DDTA4gbZ/CRPwf/aLBCAkc9Xq3qRFjUn+knp372A5aiojhjF/bPWq fRyQ== X-Gm-Message-State: ACrzQf2bwXyP+su5QlWzRSA9qqDjEbTSFit4b1bg/lxptCg9LZvvhpCD 4d36Fp8IpFouY6OCreuzmaFASKs4rTtOrBD8ktjRC1iNvm3RHLcOS0kLBY0HFThCM4TVSXcddyu yAWV3aMTEejFhIzdZ2y9/TKBsGaTjfNCrq6fIF7MpEUXHKZJiu6brONlv8fzcdMLOBmP8Qjx7+w == X-Received: by 2002:a5d:6e8e:0:b0:21d:ea5:710f with SMTP id k14-20020a5d6e8e000000b0021d0ea5710fmr10983544wrz.48.1664729592989; Sun, 02 Oct 2022 09:53:12 -0700 (PDT) X-Google-Smtp-Source: AMsMyM4KFqRjbxalbJBhTmqDqeJbreC1rCAaP6uLJ+ZlLproJH1NuS+ASmwv7xK1C8fgw+PtPdN0wg== X-Received: by 2002:a5d:6e8e:0:b0:21d:ea5:710f with SMTP id k14-20020a5d6e8e000000b0021d0ea5710fmr10983534wrz.48.1664729592714; Sun, 02 Oct 2022 09:53:12 -0700 (PDT) Received: from localhost (52.72.115.87.dyn.plus.net. [87.115.72.52]) by smtp.gmail.com with ESMTPSA id bp22-20020a5d5a96000000b00228cd9f6349sm705561wrb.106.2022.10.02.09.53.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 02 Oct 2022 09:53:12 -0700 (PDT) To: gdb-patches@sourceware.org Subject: [PATCH 2/3] gdb/python: break more dependencies between gdbpy_initialize_* functions Date: Sun, 2 Oct 2022 17:53:04 +0100 Message-Id: <1e89f7f71a8170a1529697d1bfc9d971d89978f9.1664729134.git.aburgess@redhat.com> X-Mailer: git-send-email 2.25.4 In-Reply-To: References: MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-10.3 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_BARRACUDACENTRAL, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Andrew Burgess via Gdb-patches From: Andrew Burgess Reply-To: Andrew Burgess Errors-To: gdb-patches-bounces+patchwork=sourceware.org@sourceware.org Sender: "Gdb-patches" In a later commit in this series I will propose removing all of the explicit gdbpy_initialize_* calls from python.c and replace these calls with a more generic mechanism. One of the side effects of this generic mechanism is that the order in which the various Python sub-systems within GDB are initialized is no longer guaranteed. On the whole I don't think this matters, most of the sub-systems are independent of each other, though testing did reveal a few places where we did have dependencies, though I don't think those dependencies were explicitly documented in comment anywhere. This commit is similar to the previous one, and fixes the second dependency issue that I found. In this case the finish_breakpoint_object_type uses the breakpoint_object_type as its tp_base, this means that breakpoint_object_type must have been initialized with a call to PyType_Ready before finish_breakpoint_object_type can be initialized. Previously we depended on the ordering of calls to gdbpy_initialize_breakpoints and gdbpy_initialize_finishbreakpoints in python.c. After this commit a new function gdbpy_breakpoint_init_breakpoint_type exists, this function ensures that breakpoint_object_type has been initialized, and can be called from any gdbpy_initialize_* function. I feel that this change makes the dependency explicit, which I think is a good thing. There should be no user visible changes after this commit. --- gdb/python/py-breakpoint.c | 23 +++++++++++++++++++++-- gdb/python/py-finishbreakpoint.c | 3 +++ gdb/python/python-internal.h | 12 ++++++++++++ 3 files changed, 36 insertions(+), 2 deletions(-) diff --git a/gdb/python/py-breakpoint.c b/gdb/python/py-breakpoint.c index dd4519a1b05..7a757432948 100644 --- a/gdb/python/py-breakpoint.c +++ b/gdb/python/py-breakpoint.c @@ -989,6 +989,26 @@ build_bp_list (struct breakpoint *b, PyObject *list) return PyList_Append (list, bp) == 0; } +/* See python-internal.h. */ + +bool +gdbpy_breakpoint_init_breakpoint_type () +{ + if (breakpoint_object_type.tp_new == nullptr) + { + breakpoint_object_type.tp_new = PyType_GenericNew; + if (PyType_Ready (&breakpoint_object_type) < 0) + { + /* Reset tp_new back to nullptr so future calls to this function + will try calling PyType_Ready again. */ + breakpoint_object_type.tp_new = nullptr; + return false; + } + } + + return true; +} + /* Static function to return a tuple holding all breakpoints. */ PyObject * @@ -1216,8 +1236,7 @@ gdbpy_initialize_breakpoints (void) { int i; - breakpoint_object_type.tp_new = PyType_GenericNew; - if (PyType_Ready (&breakpoint_object_type) < 0) + if (!gdbpy_breakpoint_init_breakpoint_type ()) return -1; if (gdb_pymodule_addobject (gdb_module, "Breakpoint", diff --git a/gdb/python/py-finishbreakpoint.c b/gdb/python/py-finishbreakpoint.c index c4b043f5bfe..67637f16a39 100644 --- a/gdb/python/py-finishbreakpoint.c +++ b/gdb/python/py-finishbreakpoint.c @@ -408,6 +408,9 @@ bpfinishpy_handle_exit (struct inferior *inf) int gdbpy_initialize_finishbreakpoints (void) { + if (!gdbpy_breakpoint_init_breakpoint_type ()) + return -1; + if (PyType_Ready (&finish_breakpoint_object_type) < 0) return -1; diff --git a/gdb/python/python-internal.h b/gdb/python/python-internal.h index d624b23fdc5..a4f54e78268 100644 --- a/gdb/python/python-internal.h +++ b/gdb/python/python-internal.h @@ -290,6 +290,18 @@ extern PyTypeObject frame_object_type extern PyTypeObject thread_object_type CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("thread_object"); +/* Ensure that breakpoint_object_type is initialized and return true. If + breakpoint_object_type can't be initialized then set a suitable Python + error and return false. + + This function needs to be called from any gdbpy_initialize_* function + that wants to reference breakpoint_object_type. After all the + gdbpy_initialize_* functions have been called then breakpoint_object_type + is guaranteed to have been initialized, and this function does not need + calling before referencing breakpoint_object_type. */ + +extern bool gdbpy_breakpoint_init_breakpoint_type (); + struct gdbpy_breakpoint_object { PyObject_HEAD