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.133.124]) by sourceware.org (Postfix) with ESMTPS id 747CC385829D for ; Fri, 19 Jan 2024 11:57:10 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 747CC385829D 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 747CC385829D Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1705665433; cv=none; b=WUO7eWWgnQiM5S8ZQ552J3mESKmS4GKHobOBnZD1u+wkmzJ3A6ary6DN7+gWr/HrcjPgLE5JfDU5Ne+eWBT8hv5h5xJRRNlKjDIT5IbtrLpyJC9dASVqvQop09hQXZrHzxAlijGPdLP2wMU0FGcphSEBi2RMQzY/7nUtkUfCA6g= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1705665433; c=relaxed/simple; bh=CiehepRVhnpxNcP4oDIzaL7TcG89feNRk0o8YlJrQfM=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=H+IgXOKNbmDvBF67wANMob8Y29/4SJWOkdMj9xNTQVha5rdA71UQ6eAZ+/ckizbsMj07WLy3cFI9raUFWkU4VCg7rkJefddvOW84ADVfD9pSSwlADQ7xYOxFdwJpmeqyXPn6ZLEM7Gtg0zzjGBFdDo233GG9sylbV5xEixBb8F0= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1705665430; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=79vBgW7MHr8XKeufcF9A0DzWA6ykSxIf0lcRXEaKIfM=; b=WVfBuuMH7MA9HXbsobG8XXdpJkhX1jNvM9aJkqAdBIE5BHU+2NY4fjo7SQETNnsy+aIxw0 k9hrsCrB9FEBwyYpUI9+Hh3popbmqJX4WeuK1eaUyD1r9A7AIR9sZHxU3sMymMJJg7Lre7 EcCPRrUo5nTz+UmdNmtOaCzSsj5khm8= 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-652-8kKDOzu8PPOY4E1g3-S9Ng-1; Fri, 19 Jan 2024 06:57:08 -0500 X-MC-Unique: 8kKDOzu8PPOY4E1g3-S9Ng-1 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.rdu2.redhat.com [10.11.54.2]) (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 B05FF2815E25 for ; Fri, 19 Jan 2024 11:57:08 +0000 (UTC) Received: from fedora.redhat.com (unknown [10.45.242.18]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 352AF4011C41 for ; Fri, 19 Jan 2024 11:57:08 +0000 (UTC) From: =?UTF-8?q?Alexandra=20H=C3=A1jkov=C3=A1?= To: gdb-patches@sourceware.org Subject: [PATCH v2 4/6] gdbserver/linux-low.cc: Connect the inferior to the terminal Date: Fri, 19 Jan 2024 12:56:28 +0100 Message-ID: <20240119115659.491195-6-ahajkova@redhat.com> In-Reply-To: <20240119115659.491195-1-ahajkova@redhat.com> References: <20240119115659.491195-1-ahajkova@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.2 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=-11.8 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_H3,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 dc3d2629fdb..2e91f00da24 100644 --- a/gdbserver/linux-low.cc +++ b/gdbserver/linux-low.cc @@ -954,6 +954,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"); @@ -961,10 +963,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.43.0