From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-io1-f42.google.com (mail-io1-f42.google.com [209.85.166.42]) by sourceware.org (Postfix) with ESMTPS id 5648E3858C83 for ; Tue, 19 Apr 2022 22:01:49 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 5648E3858C83 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=qyriad.me Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-io1-f42.google.com with SMTP id p62so9679iod.0 for ; Tue, 19 Apr 2022 15:01:49 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=M16AC41osQOPgwcHmBEf/7xIPJ/OJL8HvgYpMYTe0U0=; b=5QO+q4ZjKPGmZIOq53vOgjal+Z8+U5tBISWGrLrs4vMsy0Eqh03I+PX1ZoBu4V5d+G xcsg2qHp2aOo/TH9a7P1HEU/I5/psXx5DcjGOsj7Wn31gllIIaGk9bsA31M0Y3mRh5fh ldne9iTizGiq7s69jNDA9iNbBPy0AFZtuzEWpUBZKTAbM0gOy3YIfU7f2zoCeC+Ts/n9 eM9MR0JxZbc45UyfNxnI6AIrEPeD30CbbBuWHXyXM4KvZoxczLgDDXbFLHyoS2xioGgn VwTK+hTqhYD4VoO+9WRNXOGMeDQZRDyBFTH4j1KiAG18K544V1DTUJtECeffdQEpPMCh 06RA== X-Gm-Message-State: AOAM532yOXvfBS9rKngGO4PzbX6YI2PZ88ZTsBlcfHjc8ZqcvaDqQ5q6 /sQQnU99JAXIZohvXzPB1P8hspx3nO0= X-Google-Smtp-Source: ABdhPJzN2yvFMeo1dVG7IVjCQk6GdJpXfsDMMJYh0itJ2bUieu2GXm8N1Y9mI2bqRhEeFyeYd51Bwg== X-Received: by 2002:a5d:9da4:0:b0:655:a0a1:4166 with SMTP id ay36-20020a5d9da4000000b00655a0a14166mr1055428iob.116.1650405708063; Tue, 19 Apr 2022 15:01:48 -0700 (PDT) Received: from localhost.localdomain (c-67-190-109-48.hsd1.co.comcast.net. [67.190.109.48]) by smtp.gmail.com with ESMTPSA id r26-20020a5d96da000000b00654b214def9sm2410142iol.52.2022.04.19.15.01.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 19 Apr 2022 15:01:47 -0700 (PDT) From: Mikaela Szekely To: gdb-patches@sourceware.org Cc: Mikaela Szekely Subject: [PATCH][PR gdb/28874] gdb/remote: switch to added threads if there isn't one already Date: Tue, 19 Apr 2022 16:01:36 -0600 Message-Id: <20220419220135.1714665-1-mikaela.szekely@qyriad.me> X-Mailer: git-send-email 2.35.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-10.4 required=5.0 tests=BAYES_00, FREEMAIL_FORGED_FROMDOMAIN, FREEMAIL_FROM, GIT_PATCH_0, HEADER_FROM_DIFFERENT_DOMAINS, KAM_DMARC_STATUS, KAM_INFOUSMEBIZ, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) 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-List-Received-Date: Tue, 19 Apr 2022 22:01:52 -0000 Bug PR gdb/28874 reports a failed assertion when attaching to remote targets. This assertion is checked after a call to gdb::observers::new_thread.notify from set_running. The call to set_running in remote_target::remote_add_thread, however, appears to always be called before any call to switch_to_thread for remote targets that have set non-stop off. This commit proposes a new internal function has_inferior_thread in gdb/thread.c, which allows remote_target::remote_add_thread to check if the current thread has been set or not. And thus I have also adjusted the logic of ::remote_add_thread to call switch_to_thread upon thread creation if has_inferior_thread returns false. Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=28874 --- gdb/gdbthread.h | 3 +++ gdb/remote.c | 6 ++++++ gdb/thread.c | 7 +++++++ 3 files changed, 16 insertions(+) diff --git a/gdb/gdbthread.h b/gdb/gdbthread.h index 1a33eb61221..bf230410a12 100644 --- a/gdb/gdbthread.h +++ b/gdb/gdbthread.h @@ -875,6 +875,9 @@ class scoped_restore_current_thread enum language m_lang; }; +/* Returns true if there is a current inferior thread. */ +extern bool has_inferior_thread (void); + /* Returns a pointer into the thread_info corresponding to INFERIOR_PTID. INFERIOR_PTID *must* be in the thread list. */ extern struct thread_info* inferior_thread (void); diff --git a/gdb/remote.c b/gdb/remote.c index aa6a67a96e0..d35387650b3 100644 --- a/gdb/remote.c +++ b/gdb/remote.c @@ -2572,6 +2572,12 @@ remote_target::remote_add_thread (ptid_t ptid, bool running, bool executing, else thread = add_thread (this, ptid); + /* If there isn't already an active thread, then switch to the + thread we just added, so bare-metal targets don't assert the + current thread as null. */ + if (!has_inferior_thread()) + switch_to_thread(thread); + /* We start by assuming threads are resumed. That state then gets updated when we process a matching stop reply. */ get_remote_thread_info (thread)->set_resumed (); diff --git a/gdb/thread.c b/gdb/thread.c index 9d0693bd0b8..e7b418b1c85 100644 --- a/gdb/thread.c +++ b/gdb/thread.c @@ -79,6 +79,13 @@ is_current_thread (const thread_info *thr) return thr == current_thread_; } + +bool +has_inferior_thread (void) +{ + return current_thread_ != nullptr; +} + struct thread_info* inferior_thread (void) { -- 2.35.1