From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id 30DEE3858D38 for ; Fri, 17 Nov 2023 11:18:51 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 30DEE3858D38 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 30DEE3858D38 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1700219932; cv=none; b=JWg6Y9U6kAM8eKe92YjXSqMCjjtz7p+kvnX6iOxhqwRNqrrNvvQGSURyKe8LdjulQufO3d5j5XXD006Nalx179gcVaz3HqAQQWqamwi1KZyQNzC6AOJZUu7adUZaQWi0tU9jueGm0EvbYQIXJE3hFbqkO32DC8qDl7Dr0rZj5Ks= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1700219932; c=relaxed/simple; bh=IGonPIiClb3FPuqkRx5MnMGnwtbww7xXBXzVZ0l/3IQ=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=egZHMWrvnvS5lo2M5u+XNQvf6KbLbIno32aV54b+oOec0A4q1K+R5Fd3JDtaC+Td9CVVQuWDTJLSXhhB6M+zPuqAKCC7+c7cXxHHy+qlfg/czkB4qvVZV9Oclnsk1QZpLaWaW9dQb4tjv3qFvcuYKqeR2HVB80BaEg1al33RW9s= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1700219930; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=6EeFFZoNFpNEeRsdklvmKTuYAYoGcLO99G1Pypf0+so=; b=QSqF6msumwhDJj/DFiPKyPxxvpCsN/zuC6kWF49SJ9XIVbiNXc6LTnl5DS8ssOYFhtvljR jhwJZ4EAquKD3A6ZIY9Jnmbm2ep8OSfDDCpfmVU2z0Y8YmxN0R7QQMCNJzX3gBl6KbGMYK cwnozK/uU5hyAg27D8C3Hi6iTvz10KM= Received: from mimecast-mx02.redhat.com (mx-ext.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-223-eqLjrPoBMHGC5wSMEWirPg-1; Fri, 17 Nov 2023 06:18:49 -0500 X-MC-Unique: eqLjrPoBMHGC5wSMEWirPg-1 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 652B51C04351 for ; Fri, 17 Nov 2023 11:18:49 +0000 (UTC) Received: from fedora.redhat.com (unknown [10.45.242.6]) by smtp.corp.redhat.com (Postfix) with ESMTPS id A2E802026D4C; Fri, 17 Nov 2023 11:18:48 +0000 (UTC) From: =?UTF-8?q?Alexandra=20H=C3=A1jkov=C3=A1?= To: gdb-patches@sourceware.org Cc: ahajkova@redhat.com Subject: [PATCH 4/6] gdbserver/linux-low.cc: Connect the inferior to the terminal Date: Fri, 17 Nov 2023 12:18:38 +0100 Message-ID: <20231117111840.2040709-5-ahajkova@redhat.com> In-Reply-To: <20231117111840.2040709-1-ahajkova@redhat.com> References: <20231117111840.2040709-1-ahajkova@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.4 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset="US-ASCII"; x-default=true X-Spam-Status: No, score=-10.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H4,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,SPF_NONE,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: If GDBserver is connected to GDB via stdio and it recieved file descriptors of the STDIN/OUT/ERR of the terminal GDB is connected to, GDBserver will start the inferior connected to them. --- gdbserver/linux-low.cc | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/gdbserver/linux-low.cc b/gdbserver/linux-low.cc index f9001e2fa17..04d72d22050 100644 --- a/gdbserver/linux-low.cc +++ b/gdbserver/linux-low.cc @@ -952,6 +952,8 @@ linux_process_target::low_new_thread (lwp_info *info) static void linux_ptrace_fun () { + client_state &cs = get_client_state (); + if (ptrace (PTRACE_TRACEME, 0, (PTRACE_TYPE_ARG3) 0, (PTRACE_TYPE_ARG4) 0) < 0) trace_start_error_with_name ("ptrace"); @@ -959,10 +961,38 @@ linux_ptrace_fun () if (setpgid (0, 0) < 0) trace_start_error_with_name ("setpgid"); + /* If GDBserver is connected to GDB via stdio and it recieved the file + descriptors for the terminal stdin/out/err from GDB, start the inferior + connected to them. */ + if (remote_connection_is_stdio () && cs.vDefaultInferiorFd_accepted) + { + struct stat buf{}; + + if ((fstat(cs.fds[0], &buf) == -1) + || (fstat(cs.fds[1], &buf) == -1) + || (fstat(cs.fds[2], &buf) == -1)) + { + write (2, "Fstat failed. Can't connect inferior to the terminal.\n", + sizeof ("Fstat failed. Can't connect inferior to the terminal.\n") - 1); + cs.vDefaultInferiorFd_accepted = false; + } + + /* Dupped file descriptors will be inherited by the inferior. */ + if (!cs.vDefaultInferiorFd_accepted + || (dup2 (cs.fds[0], 0) == -1) + || (dup2 (cs.fds[1], 1) == -1) + || (dup2 (cs.fds[2], 2) == -1)) + { + write (2, "Dup2 failed. Can't connect inferior to the terminal.\n", + sizeof ("Dup2 failed. Can't connect inferior to the terminal.\n") - 1); + cs.vDefaultInferiorFd_accepted = false; + } + } + /* If GDBserver is connected to gdb via stdio, redirect the inferior's stdout to stderr so that inferior i/o doesn't corrupt the connection. Also, redirect stdin to /dev/null. */ - if (remote_connection_is_stdio ()) + if (remote_connection_is_stdio () && !cs.vDefaultInferiorFd_accepted) { if (close (0) < 0) trace_start_error_with_name ("close"); -- 2.41.0