From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-wm1-f41.google.com (mail-wm1-f41.google.com [209.85.128.41]) by sourceware.org (Postfix) with ESMTPS id 676653892841 for ; Wed, 13 Jul 2022 22:25:14 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 676653892841 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-wm1-f41.google.com with SMTP id o8so7330728wms.2 for ; Wed, 13 Jul 2022 15:25:14 -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=oo7Lk0+nnOmFTfB1yNLiM8C6oByBiYumdHy/SSQjxBA=; b=rbVFfOLCSIO7TYST/LfoTNwDKil2nPCXOb5dLlt3uB9Y7lZ65sKh3WeksuP+q7Cf4q I6B7G3Ro+sv+ILHQSITrxkXItKAri5PLHIdx5Xv8uGTj6pK3N4xTjuHxaYaXe86C4IoS HgZS9YylzKVuOiYjldYF4V2If4eSm1Tlbcf7I+Dy8ekbT8/0ehoLVaE0dZVtrDSIBbzz 1R+/svTkgRuxR3FD0+pTuM0WWJmZG7egzl8SUnRjhg6jLjuG62BrVCUcXoHzsvhJ/lGB iE2+uZwGZXzy2AXla2jAMGGCVQjEiTPcBIDqwqkrL1QlKAh1kkZKQeNlZiaBtmAWyjTD StPw== X-Gm-Message-State: AJIora9x4FKSFoEbcpV/7SvzNNFc4rUOHkWD7PSNXdMX+5qh4S58pw8e Vi8KSiJHsfKGnwPJ7XFaYuOXN7C10to= X-Google-Smtp-Source: AGRyM1ub4hlUgZ+Argk0J91e/1wnE1KgmETTf7MdRJFfq6CNVlITDHHp/PdOhfphZM7u3ls4yY1qYA== X-Received: by 2002:a05:600c:154a:b0:3a1:70dd:9a12 with SMTP id f10-20020a05600c154a00b003a170dd9a12mr12250835wmg.70.1657751112521; Wed, 13 Jul 2022 15:25:12 -0700 (PDT) Received: from localhost ([2001:8a0:f924:2600:209d:85e2:409e:8726]) by smtp.gmail.com with ESMTPSA id j9-20020a05600c190900b0039db31f6372sm4035573wmq.2.2022.07.13.15.25.11 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 13 Jul 2022 15:25:11 -0700 (PDT) From: Pedro Alves To: gdb-patches@sourceware.org Subject: [PATCH v2 17/29] Implement GDB_TO_EXIT support for native Linux Date: Wed, 13 Jul 2022 23:24:21 +0100 Message-Id: <20220713222433.374898-18-pedro@palves.net> X-Mailer: git-send-email 2.36.0 In-Reply-To: <20220713222433.374898-1-pedro@palves.net> References: <20220713222433.374898-1-pedro@palves.net> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-9.9 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: Wed, 13 Jul 2022 22:25:16 -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 | 68 ++++++++++++++++++++++++++++++++++--------------- 1 file changed, 47 insertions(+), 21 deletions(-) diff --git a/gdb/linux-nat.c b/gdb/linux-nat.c index 4ddc54ff8bc..0d0cbf55521 100644 --- a/gdb/linux-nat.c +++ b/gdb/linux-nat.c @@ -266,6 +266,18 @@ pending_status_str (lwp_info *lp) return status_to_str (lp->status); } +/* 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_thread_events + || (thr->thread_options () & GDB_TO_EXIT) != 0); +} + /* LWP accessors. */ @@ -895,10 +907,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 +921,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); @@ -1900,7 +1914,7 @@ linux_nat_target::follow_clone (ptid_t child_ptid) { new_lp->status = 0; - if (report_thread_events) + if (report_exit_events_for (new_lp)) new_lp->waitstatus.set_thread_created (); } } @@ -2151,8 +2165,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 ()); @@ -2933,7 +2946,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 ()); @@ -3143,10 +3156,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, @@ -3154,14 +3168,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; @@ -3383,10 +3411,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 @@ -4419,7 +4444,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