From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-wr1-f41.google.com (mail-wr1-f41.google.com [209.85.221.41]) by sourceware.org (Postfix) with ESMTPS id B298C384144F for ; Mon, 20 Jun 2022 22:54:56 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org B298C384144F Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=palves.net Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-wr1-f41.google.com with SMTP id n1so16260715wrg.12 for ; Mon, 20 Jun 2022 15:54:56 -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:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=1nG1+NRgJA8cy82TsKdILbnJdmdyH0+oFy7+VEzQ37w=; b=u0iicL8w+aDKXwgoJJI5xOmWLHE33A1WYEmqG5JCyvzfA1NFuZGpFc1taI6mIYEtKf 2DeGoAFQ+lcOpwv/ETBm/unRqkNBP4qyYxqUavXBfZYmQUNCcJ8AcJgQtQhSRl01dQYS gUaRdkTSV+5W5fJSo2KkIec08CPjyFuI6KvJO+qULfRo7WLtuVuWK99fiT5xODOWTH2e gKVpy3YNBwYtCrXK/Kxc22A88nASOdIg/ZW8w8DbBMxhroPyyqynt7iIG6C7d8kI0Ghf XkWrEDf65xfk1NAt2LRgYin9LL6l0XVe5/UQISAKkq8cuqB4JzZcvSP5jaMlcv0bT8fp +VKQ== X-Gm-Message-State: AJIora/zgN04mnSya/nCWh8L8DwNTs+LWJLUw6oo2cWwS1PwWIO+KAkG IBN0CI57CrGDu4l8YwcvMsVQ48XwwNE= X-Google-Smtp-Source: AGRyM1uJyb4MljgLik+1mAYMjf58MJvGJ4rttpQFTG8CdZsYDAbHH5GIKHb+QIwIDR+RH/fUpRIqIw== X-Received: by 2002:adf:d1c6:0:b0:218:51ae:8812 with SMTP id b6-20020adfd1c6000000b0021851ae8812mr24922006wrd.178.1655765695208; Mon, 20 Jun 2022 15:54:55 -0700 (PDT) Received: from localhost ([2001:8a0:f924:2600:209d:85e2:409e:8726]) by smtp.gmail.com with ESMTPSA id k24-20020a05600c1c9800b00397122e63b6sm16898532wms.29.2022.06.20.15.54.53 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 20 Jun 2022 15:54:54 -0700 (PDT) From: Pedro Alves To: gdb-patches@sourceware.org Subject: [PATCH 15/25] Implement GDB_TO_EXIT support for native Linux Date: Mon, 20 Jun 2022 23:54:09 +0100 Message-Id: <20220620225419.382221-16-pedro@palves.net> X-Mailer: git-send-email 2.36.0 In-Reply-To: <20220620225419.382221-1-pedro@palves.net> References: <20220620225419.382221-1-pedro@palves.net> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-9.8 required=5.0 tests=BAYES_00, FREEMAIL_FORGED_FROMDOMAIN, FREEMAIL_FROM, GIT_PATCH_0, HEADER_FROM_DIFFERENT_DOMAINS, KAM_DMARC_STATUS, 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.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-List-Received-Date: Mon, 20 Jun 2022 22:54:58 -0000 This implements support for the new GDB_TO_EXIT thread option for native Linux. Change-Id: Ia69fc0b9b96f9af7de7cefc1ddb1fba9bbb0bb90 Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=27338 --- gdb/linux-nat.c | 77 +++++++++++++++++++++++++++++++++++-------------- 1 file changed, 56 insertions(+), 21 deletions(-) diff --git a/gdb/linux-nat.c b/gdb/linux-nat.c index a446a755242..65d0bae588f 100644 --- a/gdb/linux-nat.c +++ b/gdb/linux-nat.c @@ -266,6 +266,27 @@ pending_status_str (lwp_info *lp) return status_to_str (lp->status); } +/* Return true if we should report exit events for THR. */ + +static bool +report_exit_events_for (thread_info *thr) +{ + return (report_thread_events + || (thr->thread_options () & GDB_TO_EXIT) != 0); +} + +/* Convenience overload for the above -- return true if we should + report exit events for LP. */ + +static bool +report_exit_events_for (lwp_info *lp) +{ + thread_info *thr = find_thread_ptid (linux_target, lp->ptid); + gdb_assert (thr != nullptr); + + return report_exit_events_for (thr); +} + /* LWP accessors. */ @@ -895,10 +916,11 @@ linux_nat_switch_fork (ptid_t new_ptid) registers_changed (); } -/* Handle the exit of a single thread LP. */ +/* Handle the exit of a single thread LP. If DEL_THREAD is true, + delete the thread_info associated to LP, if it exists. */ static void -exit_lwp (struct lwp_info *lp) +exit_lwp (struct lwp_info *lp, bool del_thread = true) { struct thread_info *th = find_thread_ptid (linux_target, lp->ptid); @@ -908,7 +930,8 @@ exit_lwp (struct lwp_info *lp) gdb_printf (_("[%s exited]\n"), target_pid_to_str (lp->ptid).c_str ()); - delete_thread (th); + if (del_thread) + delete_thread (th); } delete_lwp (lp->ptid); @@ -1909,7 +1932,7 @@ linux_nat_target::follow_clone (ptid_t child_ptid) { new_lp->status = 0; - if (report_thread_events) + if (report_exit_events_for (new_thr)) new_lp->waitstatus.set_thread_created (); } } @@ -2145,8 +2168,7 @@ wait_lwp (struct lwp_info *lp) /* Check if the thread has exited. */ if (WIFEXITED (status) || WIFSIGNALED (status)) { - if (report_thread_events - || lp->ptid.pid () == lp->ptid.lwp ()) + if (report_exit_events_for (lp) || is_leader (lp)) { linux_nat_debug_printf ("LWP %d exited.", lp->ptid.pid ()); @@ -2923,7 +2945,7 @@ linux_nat_filter_event (int lwpid, int status) /* Check if the thread has exited. */ if (WIFEXITED (status) || WIFSIGNALED (status)) { - if (!report_thread_events && !is_leader (lp)) + if (!report_exit_events_for (lp) && !is_leader (lp)) { linux_nat_debug_printf ("%s exited.", lp->ptid.to_string ().c_str ()); @@ -3133,10 +3155,11 @@ check_zombie_leaders (void) } } -/* Convenience function that is called when the kernel reports an exit - event. This decides whether to report the event to GDB as a - process exit event, a thread exit event, or to suppress the - event. */ +/* Convenience function that is called when we're about to return an + event to the core. If the event is an exit or signalled event, + then this decides whether to report it as process-wide event, as a + thread exit event, or to suppress it. All other event kinds are + passed through unmodified. */ static ptid_t filter_exit_event (struct lwp_info *event_child, @@ -3144,14 +3167,28 @@ filter_exit_event (struct lwp_info *event_child, { ptid_t ptid = event_child->ptid; + /* Note we must filter TARGET_WAITKIND_SIGNALLED as well, otherwise + if a non-leader thread exits with a signal, we'd report it to the + core which would interpret it as the whole-process exiting. + There is no TARGET_WAITKIND_THREAD_SIGNALLED event kind. */ + if (ourstatus->kind () != TARGET_WAITKIND_EXITED + && ourstatus->kind () != TARGET_WAITKIND_SIGNALLED) + return ptid; + if (!is_leader (event_child)) { - if (report_thread_events) - ourstatus->set_thread_exited (0); + if (report_exit_events_for (event_child)) + { + ourstatus->set_thread_exited (0); + /* Delete lwp, but not thread_info, infrun will need it to + process the event. */ + exit_lwp (event_child, false); + } else - ourstatus->set_ignore (); - - exit_lwp (event_child); + { + ourstatus->set_ignore (); + exit_lwp (event_child); + } } return ptid; @@ -3373,10 +3410,7 @@ linux_nat_wait_1 (ptid_t ptid, struct target_waitstatus *ourstatus, else lp->core = linux_common_core_of_thread (lp->ptid); - if (ourstatus->kind () == TARGET_WAITKIND_EXITED) - return filter_exit_event (lp, ourstatus); - - return lp->ptid; + return filter_exit_event (lp, ourstatus); } /* Resume LWPs that are currently stopped without any pending status @@ -4409,7 +4443,8 @@ linux_nat_target::thread_events (int enable) bool linux_nat_target::supports_set_thread_options (gdb_thread_options options) { - constexpr gdb_thread_options supported_options = GDB_TO_CLONE; + constexpr gdb_thread_options supported_options + = GDB_TO_CLONE | GDB_TO_EXIT; return ((options & supported_options) == options); } -- 2.36.0