From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-wm1-f50.google.com (mail-wm1-f50.google.com [209.85.128.50]) by sourceware.org (Postfix) with ESMTPS id 51E21384144F for ; Mon, 20 Jun 2022 22:54:35 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 51E21384144F 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-f50.google.com with SMTP id c130-20020a1c3588000000b0039c6fd897b4so8428394wma.4 for ; Mon, 20 Jun 2022 15:54:35 -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=3EmIUq4OHl4f7Xmk4LvxTzvD2dQ22FhzrPOWlDHzUzQ=; b=F+DJh8MBB5QFvhiAWF4uC7Eb/pb6DrJVmEUzQjcwfYys4a7yoqosX9Rn2hEKO3q1eC o5LkUYBL1sy3ZnLF/cQT3hGPnKgU4tjPezDVdiG51iPgQJiwQ5Rm8brspRayi0MpX5s3 DnuCoqleRI/3vWjpwQRLVmLObKm/kGIkliobpk2hkWYkpXPjImHvGc6V+jg9Z/wVwhWI 9g54N2a+pbpQFUCIRXrGkgiacM/Qt+YBWwCbyZwNVNPl6xOrHKdz9VM8al8IjON8ebiB Ayb2A5F8Y7JI5kXunBpCzgajC5Gw3f53Uf3N42LfuYpdoFOdk0pHwXiyXiQAl9tv1KOz 6oqg== X-Gm-Message-State: AJIora9xdMuhdX8AO1c86tRLnyp1YZG3JxXK0nlb3XeyscmZ4dnKcb7j 9I03xfem6wPyAnL9HWZNjStx7Y+B6mI= X-Google-Smtp-Source: AGRyM1vuVclfsjzikwvp/KamzcsW4DZjA052yHuZ8GLz5t3id3MCPszvkyrENhlNKKWWComulmGQpQ== X-Received: by 2002:a05:600c:4ec7:b0:39c:84a7:3762 with SMTP id g7-20020a05600c4ec700b0039c84a73762mr26283103wmq.153.1655765673449; Mon, 20 Jun 2022 15:54:33 -0700 (PDT) Received: from localhost ([2001:8a0:f924:2600:209d:85e2:409e:8726]) by smtp.gmail.com with ESMTPSA id k24-20020a05600c1c9800b00397122e63b6sm16898006wms.29.2022.06.20.15.54.31 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 20 Jun 2022 15:54:32 -0700 (PDT) From: Pedro Alves To: gdb-patches@sourceware.org Subject: [PATCH 05/25] Support clone events in the remote protocol Date: Mon, 20 Jun 2022 23:53:59 +0100 Message-Id: <20220620225419.382221-6-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.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 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:37 -0000 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. 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 | 86 ++++++++++++++++++++++++++++++--------- gdbserver/remote-utils.cc | 26 ++++++++++-- gdbserver/server.cc | 3 +- 3 files changed, 90 insertions(+), 25 deletions(-) diff --git a/gdb/remote.c b/gdb/remote.c index ed834228829..c0bd075e9e5 100644 --- a/gdb/remote.c +++ b/gdb/remote.c @@ -672,6 +672,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; @@ -2581,9 +2582,10 @@ 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 (); + /* We start by assuming threads are resumed. That state then gets + updated when we process a matching stop reply. */ + if (executing) + get_remote_thread_info (thread)->set_resumed (); set_executing (this, ptid, executing); set_running (this, ptid, running); @@ -4944,6 +4946,8 @@ remote_target::start_remote_1 (int from_tty, int extended_p) } else switch_to_thread (find_thread_ptid (this, curr_thread)); + + get_remote_thread_info (inferior_thread ())->set_resumed (); } /* init_wait_for_inferior should be called before get_offsets in order @@ -5897,16 +5901,35 @@ 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. */ +/* Determine if WS represents an event with a new child - a fork, + vfork, or clone. */ + +static bool +is_new_child_status (target_waitkind kind) +{ + return (kind == TARGET_WAITKIND_FORKED + || kind == TARGET_WAITKIND_VFORKED + || kind == TARGET_WAITKIND_THREAD_CLONED); +} + +/* Return a reference to the field that records the THREAD's pending + child status, if there's one. */ + +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; @@ -5914,6 +5937,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 @@ -6079,6 +6116,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. */ @@ -6811,10 +6854,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; } @@ -7291,11 +7334,11 @@ remote_target::remove_new_fork_children (threads_listing_context *context) { struct 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; @@ -7303,13 +7346,14 @@ 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) + || event->ws.kind () == TARGET_WAITKIND_VFORKED + || event->ws.kind () == TARGET_WAITKIND_THREAD_CLONED) context->remove_thread (event->ws.child_ptid ()); else if (event->ws.kind () == TARGET_WAITKIND_THREAD_EXITED) context->remove_thread (event->ptid); @@ -7638,6 +7682,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 db9b2a66f3c..b6a5a17fcd3 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: @@ -1072,13 +1073,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 f9c02a9c6da..30a1ccbb367 100644 --- a/gdbserver/server.cc +++ b/gdbserver/server.cc @@ -236,7 +236,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.36.0