From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-lj1-f180.google.com (mail-lj1-f180.google.com [209.85.208.180]) by sourceware.org (Postfix) with ESMTPS id 343FB3891B5D for ; Mon, 13 Nov 2023 15:05:02 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 343FB3891B5D Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=palves.net Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 343FB3891B5D Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=209.85.208.180 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1699887905; cv=none; b=xWSwi+EzL2UE4bckhFAS5H1aQ6Hk1wFT4gOM6+gg9+xYLz3SMkQW5PgO10UCgZe0pxpE2D2NAz2P4eR+rMmP9J+w9qmpEiud/hRHnP36y/d9Oy8MG2dBMOudtOpVw9WlFmgpaBJyK6H2JdvolLN+ky7IyrxFRn7fghEuSE08+14= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1699887905; c=relaxed/simple; bh=T3qDFyzMLgs0xZzrUwRD8o+lAnmdqBtEyENg9AEXlsY=; h=From:To:Subject:Date:Message-Id:MIME-Version; b=iOeYFyEUes1Y6j6UK3RVzN5J3YlTEeEYjcPFoUqMhufbIpwBDLVVBMNOMmMQMnyCLs7uqd1jbKWGIUpUBf/l3miMQm+tG+h1kkmu+BItZagon/OywP0tjONPuf+UR+pzBxbc0g7r0aWpvXRktCioHIpWifHzndsvqQhC4R1zy6w= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-lj1-f180.google.com with SMTP id 38308e7fff4ca-2c504a5e1deso62169641fa.2 for ; Mon, 13 Nov 2023 07:05:02 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1699887900; x=1700492700; 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:message-id:reply-to; bh=hI0x43/4ElE54qo6EiUqSJM8464NHmpp1fnM3eVvXSU=; b=sW6Pjt6g3okal7NQpfSL24ItH+SrBBzeOM6wwisQE6oBN2/vQ8UrFK7I7GoF9R2dea wozWqCbBhK7bBi+K+cmu8GFB3JYxNEwv0iQ3++gmvB4k61ZE2RT40PkUzlcD5IE0irYB 9X8SM7zz3IoErepIQPV+jpaJ98gi1Zjy3/dOrkuKnff2j4i+dc6chfVgp5xOHs1S9HD8 mPqfKDEefeA7Mhts6gW9Yc+bL/PJI6bw28k+UEyBLTcJOOn2hnAYQVt1vuD4QeonRetQ 7vMzrjv/WqJi6aaiTUgkH8AxAj5lNL1Swt5P1Vy29XujTXK/eU4XNUp/agBnaiuQWUQg Hiew== X-Gm-Message-State: AOJu0YxVF5hx5bnZIHMIZW6GKQyjhKlCHImIipouGcPXdqlmEIf2N0BP syufjGvadBd5aElIBdFracAu5ViQMVE= X-Google-Smtp-Source: AGHT+IFB9QQGuiw3aNYr5wmo3JvFGMk1uDwgn9ZYNUM8OBUXGIHhyWzmuq3e5JKcf4TYNOKaJijrSA== X-Received: by 2002:a05:651c:154c:b0:2c5:1f5f:ed96 with SMTP id y12-20020a05651c154c00b002c51f5fed96mr6244103ljp.3.1699887900372; Mon, 13 Nov 2023 07:05:00 -0800 (PST) Received: from localhost ([2001:8a0:f91e:1a00:8060:1e54:fb28:9635]) by smtp.gmail.com with UTF8SMTPSA id t3-20020a1c7703000000b004083729fc14sm13981872wmi.20.2023.11.13.07.04.59 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 13 Nov 2023 07:04:59 -0800 (PST) From: Pedro Alves To: gdb-patches@sourceware.org Cc: Andrew Burgess Subject: [FYI/pushed v4 04/25] Support clone events in the remote protocol Date: Mon, 13 Nov 2023 15:04:06 +0000 Message-Id: <20231113150427.477431-5-pedro@palves.net> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231113150427.477431-1-pedro@palves.net> References: <20231113150427.477431-1-pedro@palves.net> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-9.7 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 List-Id: The previous patch taught GDB about a new TARGET_WAITKIND_THREAD_CLONED event kind, and made the Linux target report clone events. A following patch will teach Linux GDBserver to do the same thing. But before we get there, we need to teach the remote protocol about TARGET_WAITKIND_THREAD_CLONED. That's what this patch does. Clone is very similar to vfork and fork, and the new stop reply is likewise handled similarly. The stub reports "T05clone:...". GDBserver core is taught to handle TARGET_WAITKIND_THREAD_CLONED and forward it to GDB in this patch, but no backend actually emits it yet. That will be done in a following patch. Documentation for this new remote protocol feature is included in a documentation patch later in the series. Reviewed-By: Andrew Burgess Change-Id: If271f20320d864f074d8ac0d531cc1a323da847f Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=19675 Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=27830 --- gdb/remote.c | 98 ++++++++++++++++++++++++++------------- gdbserver/remote-utils.cc | 26 +++++++++-- gdbserver/server.cc | 3 +- 3 files changed, 89 insertions(+), 38 deletions(-) diff --git a/gdb/remote.c b/gdb/remote.c index 69271048da2..8c7979ce6a6 100644 --- a/gdb/remote.c +++ b/gdb/remote.c @@ -1019,6 +1019,7 @@ class remote_target : public process_stratum_target const struct btrace_config *btrace_conf (const struct btrace_target_info *) override; bool augmented_libraries_svr4_read () override; void follow_fork (inferior *, ptid_t, target_waitkind, bool, bool) override; + void follow_clone (ptid_t child_ptid) override; void follow_exec (inferior *, ptid_t, const char *) override; int insert_fork_catchpoint (int) override; int remove_fork_catchpoint (int) override; @@ -1112,7 +1113,7 @@ class remote_target : public process_stratum_target void remote_btrace_maybe_reopen (); - void remove_new_fork_children (threads_listing_context *context); + void remove_new_children (threads_listing_context *context); void kill_new_fork_children (inferior *inf); void discard_pending_stop_replies (struct inferior *inf); int stop_reply_queue_length (); @@ -2792,10 +2793,8 @@ remote_target::remote_add_thread (ptid_t ptid, bool running, bool executing, else thread = add_thread (this, ptid); - /* 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 (); - + if (executing) + get_remote_thread_info (thread)->set_resumed (); set_executing (this, ptid, executing); set_running (this, ptid, running); @@ -4194,10 +4193,11 @@ remote_target::update_thread_list () } } - /* Remove any unreported fork child threads from CONTEXT so - that we don't interfere with follow fork, which is where - creation of such threads is handled. */ - remove_new_fork_children (&context); + /* Remove any unreported fork/vfork/clone child threads from + CONTEXT so that we don't interfere with follow + fork/vfork/clone, which is where creation of such threads is + handled. */ + remove_new_children (&context); /* And now add threads we don't know about yet to our list. */ for (thread_item &item : context.items) @@ -5161,6 +5161,8 @@ remote_target::start_remote_1 (int from_tty, int extended_p) } else switch_to_thread (this->find_thread (curr_thread)); + + get_remote_thread_info (inferior_thread ())->set_resumed (); } /* init_wait_for_inferior should be called before get_offsets in order @@ -6120,16 +6122,25 @@ is_fork_status (target_waitkind kind) || kind == TARGET_WAITKIND_VFORKED); } -/* Return THREAD's pending status if it is a pending fork parent, else - return nullptr. */ +/* Return a reference to the field where a pending child status, if + there's one, is recorded. If there's no child event pending, the + returned waitstatus has TARGET_WAITKIND_IGNORE kind. */ + +static const target_waitstatus & +thread_pending_status (struct thread_info *thread) +{ + return (thread->has_pending_waitstatus () + ? thread->pending_waitstatus () + : thread->pending_follow); +} + +/* Return THREAD's pending status if it is a pending fork/vfork (but + not clone) parent, else return nullptr. */ static const target_waitstatus * thread_pending_fork_status (struct thread_info *thread) { - const target_waitstatus &ws - = (thread->has_pending_waitstatus () - ? thread->pending_waitstatus () - : thread->pending_follow); + const target_waitstatus &ws = thread_pending_status (thread); if (!is_fork_status (ws.kind ())) return nullptr; @@ -6137,6 +6148,20 @@ thread_pending_fork_status (struct thread_info *thread) return &ws; } +/* Return THREAD's pending status if is is a pending fork/vfork/clone + event, else return nullptr. */ + +static const target_waitstatus * +thread_pending_child_status (thread_info *thread) +{ + const target_waitstatus &ws = thread_pending_status (thread); + + if (!is_new_child_status (ws.kind ())) + return nullptr; + + return &ws; +} + /* Detach the specified process. */ void @@ -6327,6 +6352,12 @@ remote_target::follow_fork (inferior *child_inf, ptid_t child_ptid, } } +void +remote_target::follow_clone (ptid_t child_ptid) +{ + remote_add_thread (child_ptid, false, false, false); +} + /* Target follow-exec function for remote targets. Save EXECD_PATHNAME in the program space of the new inferior. */ @@ -7054,10 +7085,10 @@ remote_target::commit_resumed () if (priv->get_resume_state () == resume_state::RESUMED_PENDING_VCONT) any_pending_vcont_resume = true; - /* If a thread is the parent of an unfollowed fork, then we - can't do a global wildcard, as that would resume the fork - child. */ - if (thread_pending_fork_status (tp) != nullptr) + /* If a thread is the parent of an unfollowed fork/vfork/clone, + then we can't do a global wildcard, as that would resume the + pending child. */ + if (thread_pending_child_status (tp) != nullptr) may_global_wildcard_vcont = false; } @@ -7517,22 +7548,22 @@ const notif_client notif_client_stop = REMOTE_NOTIF_STOP, }; -/* If CONTEXT contains any fork child threads that have not been - reported yet, remove them from the CONTEXT list. If such a - thread exists it is because we are stopped at a fork catchpoint - and have not yet called follow_fork, which will set up the - host-side data structures for the new process. */ +/* If CONTEXT contains any fork/vfork/clone child threads that have + not been reported yet, remove them from the CONTEXT list. If such + a thread exists it is because we are stopped at a fork/vfork/clone + catchpoint and have not yet called follow_fork/follow_clone, which + will set up the host-side data structures for the new child. */ void -remote_target::remove_new_fork_children (threads_listing_context *context) +remote_target::remove_new_children (threads_listing_context *context) { const notif_client *notif = ¬if_client_stop; - /* For any threads stopped at a fork event, remove the corresponding - fork child threads from the CONTEXT list. */ + /* For any threads stopped at a (v)fork/clone event, remove the + corresponding child threads from the CONTEXT list. */ for (thread_info *thread : all_non_exited_threads (this)) { - const target_waitstatus *ws = thread_pending_fork_status (thread); + const target_waitstatus *ws = thread_pending_child_status (thread); if (ws == nullptr) continue; @@ -7540,13 +7571,12 @@ remote_target::remove_new_fork_children (threads_listing_context *context) context->remove_thread (ws->child_ptid ()); } - /* Check for any pending fork events (not reported or processed yet) - in process PID and remove those fork child threads from the - CONTEXT list as well. */ + /* Check for any pending (v)fork/clone events (not reported or + processed yet) in process PID and remove those child threads from + the CONTEXT list as well. */ remote_notif_get_pending_events (notif); for (auto &event : get_remote_state ()->stop_reply_queue) - if (event->ws.kind () == TARGET_WAITKIND_FORKED - || event->ws.kind () == TARGET_WAITKIND_VFORKED) + if (is_new_child_status (event->ws.kind ())) context->remove_thread (event->ws.child_ptid ()); else if (event->ws.kind () == TARGET_WAITKIND_THREAD_EXITED) context->remove_thread (event->ptid); @@ -7877,6 +7907,8 @@ Packet: '%s'\n"), event->ws.set_forked (read_ptid (++p1, &p)); else if (strprefix (p, p1, "vfork")) event->ws.set_vforked (read_ptid (++p1, &p)); + else if (strprefix (p, p1, "clone")) + event->ws.set_thread_cloned (read_ptid (++p1, &p)); else if (strprefix (p, p1, "vforkdone")) { event->ws.set_vfork_done (); diff --git a/gdbserver/remote-utils.cc b/gdbserver/remote-utils.cc index fb5c38d4522..49dcd986c1c 100644 --- a/gdbserver/remote-utils.cc +++ b/gdbserver/remote-utils.cc @@ -1063,6 +1063,7 @@ prepare_resume_reply (char *buf, ptid_t ptid, const target_waitstatus &status) case TARGET_WAITKIND_FORKED: case TARGET_WAITKIND_VFORKED: case TARGET_WAITKIND_VFORK_DONE: + case TARGET_WAITKIND_THREAD_CLONED: case TARGET_WAITKIND_EXECD: case TARGET_WAITKIND_THREAD_CREATED: case TARGET_WAITKIND_SYSCALL_ENTRY: @@ -1071,13 +1072,30 @@ prepare_resume_reply (char *buf, ptid_t ptid, const target_waitstatus &status) struct regcache *regcache; char *buf_start = buf; - if ((status.kind () == TARGET_WAITKIND_FORKED && cs.report_fork_events) + if ((status.kind () == TARGET_WAITKIND_FORKED + && cs.report_fork_events) || (status.kind () == TARGET_WAITKIND_VFORKED - && cs.report_vfork_events)) + && cs.report_vfork_events) + || status.kind () == TARGET_WAITKIND_THREAD_CLONED) { enum gdb_signal signal = GDB_SIGNAL_TRAP; - const char *event = (status.kind () == TARGET_WAITKIND_FORKED - ? "fork" : "vfork"); + + auto kind_remote_str = [] (target_waitkind kind) + { + switch (kind) + { + case TARGET_WAITKIND_FORKED: + return "fork"; + case TARGET_WAITKIND_VFORKED: + return "vfork"; + case TARGET_WAITKIND_THREAD_CLONED: + return "clone"; + default: + gdb_assert_not_reached ("unhandled kind"); + } + }; + + const char *event = kind_remote_str (status.kind ()); sprintf (buf, "T%02x%s:", signal, event); buf += strlen (buf); diff --git a/gdbserver/server.cc b/gdbserver/server.cc index 5f2032c37c1..38f084bb035 100644 --- a/gdbserver/server.cc +++ b/gdbserver/server.cc @@ -241,7 +241,8 @@ in_queued_stop_replies_ptid (struct notif_event *event, ptid_t filter_ptid) /* Don't resume fork children that GDB does not know about yet. */ if ((vstop_event->status.kind () == TARGET_WAITKIND_FORKED - || vstop_event->status.kind () == TARGET_WAITKIND_VFORKED) + || vstop_event->status.kind () == TARGET_WAITKIND_VFORKED + || vstop_event->status.kind () == TARGET_WAITKIND_THREAD_CLONED) && vstop_event->status.child_ptid ().matches (filter_ptid)) return true; -- 2.34.1