From patchwork Fri Jan 19 11:56:24 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexandra Petlanova Hajkova X-Patchwork-Id: 84427 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id E63EC38582BF for ; Fri, 19 Jan 2024 11:58:26 +0000 (GMT) X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org 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 CBE473858403 for ; Fri, 19 Jan 2024 11:57:08 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org CBE473858403 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 CBE473858403 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=1705665430; cv=none; b=tjPdJlE5OgVqnO8UYOndZZT84R+9hCr0sQaQP4DaemXzFXa6s4vzcGVHvKXX4ga9YiAMDA5R5oOLxwJfMQTdQ7R2x6cpHpa00jRDytSCl/DUSHoH7UJRcxvAZNxBIxjx1TXGyw0WGATRtHRfROLb8xVzFAO00Qev4ggs37+nxwE= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1705665430; c=relaxed/simple; bh=JqKRREI7rjwt0bPV+7oGY+/H35qDvhsuZo1GhE5CLgM=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=MzHSIXKbP3gDHHYlEIzXfcqUKjZrVDB/VlJU8k/88WwbnlCLuECKMzD7+2nzkZmlZ/0fdZJ49uCk/HbK+r92CkuPnvV5vBpwwOPcypJ5gpsI5ATspT7n4ua6u8V7C0MhO/SgszI0U/p2cMYGxRgbKklzgqrb/qsvTOgNXKSI8cA= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1705665428; 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=Zso/UkUkcJI09ZsJVpfvBo/O3RSq0ACuv8hc+pJ3md8=; b=EZKr3LEC+okJQXVX77gMOJ/2pv0IhOxowu2unl+hl/EEgnauEtxPqyyvGGcELRuq9pnFzk E8YmamcOqLhUJ9fX1GlH0R58+5Q1a4/QOyfmGj5RuvgkIwgHVjfScQTg5qek2jGA2qrjzv WDwqzgbdAgFnRSfI0g/I36YKsCJIx2o= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-271-0u2XwPQHO72S7mYQi_kMVQ-1; Fri, 19 Jan 2024 06:57:04 -0500 X-MC-Unique: 0u2XwPQHO72S7mYQi_kMVQ-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 0B62F881BC0 for ; Fri, 19 Jan 2024 11:57:04 +0000 (UTC) Received: from fedora.redhat.com (unknown [10.45.242.18]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 2BA0E40C95AD for ; Fri, 19 Jan 2024 11:57:03 +0000 (UTC) From: =?utf-8?q?Alexandra_H=C3=A1jkov=C3=A1?= To: gdb-patches@sourceware.org Subject: [PATCH v2 1/6] gdb.server/non-existing-program.exp: Use gdbserver_start. Date: Fri, 19 Jan 2024 12:56:24 +0100 Message-ID: <20240119115659.491195-2-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 X-Spam-Status: No, score=-11.6 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 X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gdb-patches-bounces+patchwork=sourceware.org@sourceware.org Also modify GDBserver_start to match more messages from the server and return them to the caller. This test tests if GDBserver exits cleanly when GDBserver is called using stdio with some program that does not exist. My series modifies the GDBserver in such a way, it defers starting the inferior till certain packets arrive when stdio is used. The result was, when GDBserver was called using stdio with some nonexistent file, it was waiting for those packets to come but since GDB was never attached, they would never do so and the test timed out. Running GDBserver using stdio without planning to attach there with GDB is not a realistic situation and what the user would normally do. The test was modifyied to start GDBserver with a port number instead of stdio by calling GDBserver_start proc which was modifyied to return a message from GDBserver. --- .../gdb.server/non-existing-program.exp | 54 +++++----------- gdb/testsuite/lib/gdbserver-support.exp | 62 +++++++++++++------ 2 files changed, 58 insertions(+), 58 deletions(-) diff --git a/gdb/testsuite/gdb.server/non-existing-program.exp b/gdb/testsuite/gdb.server/non-existing-program.exp index 2d08e500def..751a55822b6 100644 --- a/gdb/testsuite/gdb.server/non-existing-program.exp +++ b/gdb/testsuite/gdb.server/non-existing-program.exp @@ -30,44 +30,20 @@ if { $gdbserver == "" } { return } -# Fire off gdbserver. The port doesn't really matter, gdbserver tries -# to spawn the program before opening the connection. -set spawn_id [remote_spawn target "$gdbserver stdio non-existing-program"] - -set msg "gdbserver exits cleanly" -set saw_exiting 0 -expect { - # This is what we get on ptrace-based targets with - # startup-with-shell disabled (e.g., when the SHELL variable is - # unset). - -re "stdin/stdout redirected.*gdbserver: Cannot exec non-existing-program\r\ngdbserver: Error: No such file or directory\r\n\r\nDuring startup program exited with code 127\.\r\nExiting\r\n" { - set saw_exiting 1 - exp_continue - } - # Likewise, but with startup-with-shell enabled, which is the - # default behaviour. - -re "stdin/stdout redirected.*exec: non-existing-program: not found\r\nDuring startup program exited with code 127\.\r\nExiting\r\n" { - set saw_exiting 1 - exp_continue - } - # This is what we get on Windows. - -re "Error creating process\r\n\r\nExiting\r\n" { - set saw_exiting 1 - exp_continue - } - -re "A problem internal to GDBserver has been detected" { - fail "$msg (GDBserver internal error)" - wait - } - eof { - gdb_assert $saw_exiting $msg - wait +foreach_with_prefix start_with_shell { on off } { + if { $start_with_shell } { + set arg "--startup-with-shell" + } else { + set arg "--no-startup-with-shell" } - timeout { - fail "$msg (timeout)" - } -} -# expect defaults to spawn_id in many places. Avoid confusing any -# following code. -unset spawn_id + set msg "GDBserver exits cleanly" + set res [gdbserver_start $arg non-existent-file] + set status [lindex $res 2] + + # We expect GDBserver to fail with the message indicating executable + # does not exist. + gdb_assert { $status == "During startup program exited with code 127\.\r\nExiting\r\n" + || $status == "Error creating process\r\n\r\nExiting\r\n"} $msg + +} diff --git a/gdb/testsuite/lib/gdbserver-support.exp b/gdb/testsuite/lib/gdbserver-support.exp index 8bcf4fbbb01..92afd6a4642 100644 --- a/gdb/testsuite/lib/gdbserver-support.exp +++ b/gdb/testsuite/lib/gdbserver-support.exp @@ -244,7 +244,8 @@ proc gdbserver_default_get_comm_port { port } { # Start a gdbserver process with initial OPTIONS and trailing ARGUMENTS. # The port will be filled in between them automatically. # -# Returns the target protocol and socket to connect to. +# Returns the target protocol, socket to connect to and the status +# message from the gdbserver. proc gdbserver_start { options arguments } { global portnum @@ -363,33 +364,56 @@ proc gdbserver_start { options arguments } { # talk to the program using GDBserver's tty instead. global inferior_spawn_id set inferior_spawn_id $server_spawn_id + set msg 0 # Wait for the server to open its TCP socket, so that GDB can connect. expect { - -i $server_spawn_id - -timeout 120 - -notransfer - -re "Listening on" { } - -re "Can't (bind address|listen on socket): Address already in use\\.\r\n" { - verbose -log "Port $portnum is already in use." - if ![target_info exists gdb,socketport] { - # Bump the port number to avoid the conflict. - wait -i $expect_out(spawn_id) - incr portnum - continue - } - } - -re ".*: cannot resolve name: .*\r\n" { - error "gdbserver cannot resolve name." - } - timeout { + -i $server_spawn_id + -timeout 120 + -notransfer + -re "Listening on" { + set msg $expect_out(0,string) + } + -re "Can't (bind address|listen on socket): Address already in use\\.\r\n" { + verbose -log "Port $portnum is already in use." + set msg "Port is already in use" + if ![target_info exists gdb,socketport] { + # Bump the port number to avoid the conflict. + wait -i $expect_out(spawn_id) + incr portnum + continue + } + } + -re ".*: cannot resolve name: .*\r\n" { + error "gdbserver cannot resolve name." + } + # Likewise, but with startup-with-shell enabled, which is the + # default behaviour. + -re "During startup program exited with code 127\.\r\nExiting\r\n" { + set msg $expect_out(0,string) + exp_continue + } + -re "Can't bind address: Address already in use\.\r\nExiting\r\n" { + set msg $expect_out(0,string) + exp_continue + } + # This is what we get on Windows. + -re "Error creating process\r\n\r\nExiting\r\n" { + set msg $expect_out(0,string) + exp_continue + } + -re "A problem internal to GDBserver has been detected" { + set msg $expect_out(0,string) + wait + } + timeout { error "Timeout waiting for gdbserver response." } } break } - return [list $protocol [$get_remote_address $debughost $portnum]] + return [list $protocol [$get_remote_address $debughost $portnum] $msg] } # Start a gdbserver process running SERVER_EXEC, and connect GDB From patchwork Fri Jan 19 11:56:26 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexandra Petlanova Hajkova X-Patchwork-Id: 84426 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 33C6B3858008 for ; Fri, 19 Jan 2024 11:57:54 +0000 (GMT) X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org 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 B4A7A3858C74 for ; Fri, 19 Jan 2024 11:57:08 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org B4A7A3858C74 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 B4A7A3858C74 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=1705665430; cv=none; b=MMyLbcXxLSFoqWW6NVzeaMhA33K2UwVbXaNXQqc7qam3d3sjqfhjA/4ADYVl78oAsfbGewEfXqbBfdunG80FBjXXTdCag5jEqxQ43mNBMhR78ck6I0H3AVM7lBxlnvCC/lJkpa0wOZv7yrgidr39O/mSfYNSkl9WNJoPXsgoDUc= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1705665430; c=relaxed/simple; bh=R0ikvpl73RJk0tWjTJEJyPazsbp4xImmBys4m8gEIWU=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=HZ+f1crzYiicvswoMP2JvnuZrQeuZvZzIq41fxSS22G8Lzz6BSCy/nNO75ylBTQlaF989S1ieisFkRMLq8iFvtDjwBSx68/5RIUD0iTL0el1XXM73M+pKSjV4Z6livZLa2NNeq8ztegDw8JvgZw0cfTFH3gKWS7rwM5WB0YIyFE= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1705665428; 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=tvVM/e4EmuKcIK7aGvXZLvvHy7K4N8Y20LwPyBVlQfo=; b=Od623woLnIYj1hmcnuXHRqLc9DAybo28AI6t+t6qMOwACY2ycwxen8DxMJXzeRwKKNtQqI +kb+PK8VxuAXpcqY3TpVQMRio1Pgj7ByzYInt/MsKU6QLTtWHNcbrHMSV0BqU1D3GB0MaN cvWZP4KS9W4QKs22TgLXQpKR1l4OUl8= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-601-rPuYjfiHPoScNwRMV0Yv7Q-1; Fri, 19 Jan 2024 06:57:06 -0500 X-MC-Unique: rPuYjfiHPoScNwRMV0Yv7Q-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 B853983B82C for ; Fri, 19 Jan 2024 11:57:06 +0000 (UTC) Received: from fedora.redhat.com (unknown [10.45.242.18]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 1C0474011C41 for ; Fri, 19 Jan 2024 11:57:05 +0000 (UTC) From: =?utf-8?q?Alexandra_H=C3=A1jkov=C3=A1?= To: gdb-patches@sourceware.org Subject: [PATCH v2 2/6] gdb/ser-pipe.c: Duplicate the file descriptors Date: Fri, 19 Jan 2024 12:56:26 +0100 Message-ID: <20240119115659.491195-4-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 X-Spam-Status: No, score=-11.5 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 X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gdb-patches-bounces+patchwork=sourceware.org@sourceware.org Duplicate the numbers of STDOUT/STDIN/STDERR file descriptors GDB is connected to. Preserved numbers of the file descriptors could be then sent to the gdbserver. If gdbserver is run locally and will accept the numbers of the file descriptors, it can start the inferior connected to the same STDIN/OUT/ERR, GDB is connected to. --- gdb/ser-pipe.c | 40 ++++++++++++++++++++++++++++++++++++++++ gdb/serial.c | 4 ++++ gdb/serial.h | 4 ++++ 3 files changed, 48 insertions(+) diff --git a/gdb/ser-pipe.c b/gdb/ser-pipe.c index 842b656eb5a..849926c179b 100644 --- a/gdb/ser-pipe.c +++ b/gdb/ser-pipe.c @@ -77,6 +77,32 @@ pipe_open (struct serial *scb, const char *name) perror_with_name (_("could not open socket pair"), save); } + /* Preserve STDIN/STDOUT/STDERR so they won't be closed on + exec later, after we fork. */ + int saved_stdin = dup (STDIN_FILENO); + int saved_stdout = dup (STDOUT_FILENO); + int saved_stderr = dup (STDERR_FILENO); + if (saved_stdin == -1 || saved_stdout == -1 || saved_stderr == -1) + { + /* If any FD failed to dup() then we can't used the default-fd mechanism, + so close any files that succeeded. */ + if (saved_stdin == -1) + close (saved_stdin); + if (saved_stdout == -1) + close (saved_stdout); + if (saved_stderr == -1) + close (saved_stderr); + saved_stdin = saved_stdout = saved_stderr = -1; + } + + mark_fd_no_cloexec (saved_stdout); + mark_fd_no_cloexec (saved_stdin); + mark_fd_no_cloexec (saved_stderr); + + scb->fds[0] = saved_stdin; + scb->fds[1] = saved_stdout; + scb->fds[2] = saved_stderr; + /* Create the child process to run the command in. Note that the apparent call to vfork() below *might* actually be a call to fork() due to the fact that autoconf will ``#define vfork fork'' @@ -91,6 +117,12 @@ pipe_open (struct serial *scb, const char *name) close (pdes[1]); close (err_pdes[0]); close (err_pdes[1]); + close (saved_stdout); + close (saved_stdin); + close (saved_stderr); + scb->fds[0] = -1; + scb->fds[1] = -1; + scb->fds[2] = -1; perror_with_name (_("could not vfork"), save); } @@ -140,6 +172,14 @@ pipe_open (struct serial *scb, const char *name) close (pdes[1]); if (err_pdes[1] != -1) close (err_pdes[1]); + + unmark_fd_no_cloexec(saved_stdout); + unmark_fd_no_cloexec(saved_stdin); + unmark_fd_no_cloexec(saved_stderr); + close (saved_stdout); + close (saved_stdin); + close (saved_stderr); + /* :end chunk */ state = XNEW (struct pipe_state); state->pid = pid; diff --git a/gdb/serial.c b/gdb/serial.c index 734a580ed02..8fe7bcfe4b4 100644 --- a/gdb/serial.c +++ b/gdb/serial.c @@ -180,6 +180,10 @@ new_serial (const struct serial_ops *ops) scb->ops = ops; + scb->fds[0] = -1; + scb->fds[1] = -1; + scb->fds[2] = -1; + scb->bufp = scb->buf; scb->error_fd = -1; scb->refcnt = 1; diff --git a/gdb/serial.h b/gdb/serial.h index 69507e69295..bfec99ad7c6 100644 --- a/gdb/serial.h +++ b/gdb/serial.h @@ -254,6 +254,10 @@ struct serial int async_state; /* Async internal state. */ void *async_context; /* Async event thread's context */ serial_event_ftype *async_handler;/* Async event handler */ + + /* File descriptors for preserved STDIN/STDOUT/STDERR + to be sent to gdbserver when run locally. */ + int fds[3]; }; struct serial_ops From patchwork Fri Jan 19 11:56:27 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexandra Petlanova Hajkova X-Patchwork-Id: 84428 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 6157E3858C56 for ; Fri, 19 Jan 2024 11:58:39 +0000 (GMT) X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org 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 DBCE3385841A for ; Fri, 19 Jan 2024 11:57:09 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org DBCE3385841A 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 DBCE3385841A 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=PbDEljIwkoW/lhclDcYCtshKSdl/ewpg8hMDkFR/rs/1Lzy2gbg9RRhPLSCsyNV2KWHrph9R1CY6LGHElMFTIG+jdXZ9TuFDcQVPEFbAU5+rK04KhWveOqoUSAA2U2zqTNC+1Thhl65P99Dh8w6BwXJaM7Xy1YWFm07ZqC/BNO0= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1705665433; c=relaxed/simple; bh=4xLQFJg7PAHW5SrwqB3uDoXGS0tFgEvV6wGG7M5ZHqI=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=xAlCitziQ+/AmFlU6n2cgRn0Gd1wO4n6m0PQ44rtm/0nE6a1M5W6WHd3qtRSgzG0TB6ELmN5vd58bBXoRfV0gkRliGriFtaxbEcG6IJYlmHVW6sk92f77UvFwmDkXBFf4v3xiAEnLNopHR8vH7kgeacruzFTdafpj4Ygt0PLlJo= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1705665429; 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=sVdUbd8Xia2QaYaScTTF3hAxI/q1jOyJRYwOybYsx1Q=; b=QWFH3Fa+zllcqT4mMh0gqlE1miHHUPUm8zxFhmNBJZECQUbnMxgVqDEaGace+vfZl9d4c1 eE1YgFH4pt7wpeug0vEdS7XWT5gzdnBLRyumPbUejcqRO3p2+Ob/N8os3SfnMlD4KTY5IR DxL8PeAI5tBkSvAINCydOu9cJE947Qo= 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-172-D9uXeJnbMDSMDmliPRTVkw-1; Fri, 19 Jan 2024 06:57:08 -0500 X-MC-Unique: D9uXeJnbMDSMDmliPRTVkw-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 E2FF129AA385 for ; Fri, 19 Jan 2024 11:57:07 +0000 (UTC) Received: from fedora.redhat.com (unknown [10.45.242.18]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 17A4A4011C41 for ; Fri, 19 Jan 2024 11:57:06 +0000 (UTC) From: =?utf-8?q?Alexandra_H=C3=A1jkov=C3=A1?= To: gdb-patches@sourceware.org Subject: [PATCH v2 3/6] Add new vDefaultInferiorFd packet Date: Fri, 19 Jan 2024 12:56:27 +0100 Message-ID: <20240119115659.491195-5-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 X-Spam-Status: No, score=-11.9 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 X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gdb-patches-bounces+patchwork=sourceware.org@sourceware.org Add a new DefaultInferiorFd feature and the corresponding packet. This feature allows GDB to send, to GDBserver, the file descriptor numbers of the terminal to which GDB is connected. The inferior is then started connected to the same terminal as GDB. This allows the inferior run by local GDBserver to read from GDB's STDIN and write its output to GDB's STOUT/ERR the same way as native target. Reviewed-By: Eli Zaretskii --- gdb/doc/gdb.texinfo | 42 +++++++++++++ gdb/remote.c | 28 +++++++++ gdbserver/linux-low.cc | 6 ++ gdbserver/linux-low.h | 2 + gdbserver/server.cc | 135 ++++++++++++++++++++++++++++++++++++++++- gdbserver/server.h | 12 ++++ gdbserver/target.cc | 6 ++ gdbserver/target.h | 6 ++ 8 files changed, 234 insertions(+), 3 deletions(-) diff --git a/gdb/doc/gdb.texinfo b/gdb/doc/gdb.texinfo index e98c15242bc..8ea8d96f73c 100644 --- a/gdb/doc/gdb.texinfo +++ b/gdb/doc/gdb.texinfo @@ -23251,6 +23251,18 @@ If you specify the program to debug on the command line, then the resume using commands like @kbd{step} and @kbd{continue} as with @code{target remote} mode. +When GDBserver is run locally using stdio, for example, with +@w{@kbd{target remote | gdbserver - @var{inferior}}}, the inferior's +@code{stdin} is closed, which means the inferior can't read any +input. + +If GDBserver supports the default inferior file descriptors feature +(@pxref{default inferior file descriptors}), it will start the +inferior connected to the same terminal as @value{GDBN}, which +enables the inferior to read from @code{stdin} and write to +@code{stdout} and @code{stderr}, in the same way native targets do. + + @anchor{Attaching in Types of Remote Connections} @item Attaching @strong{With target remote mode:} The @value{GDBN} command @code{attach} is @@ -43114,6 +43126,36 @@ for success (@pxref{Stop Reply Packets}) @cindex @samp{vStopped} packet @xref{Notification Packets}. +@anchor{default inferior file descriptors} +@cindex @samp{vDefaultInferiorFd} packet +@item vDefaultInferiorFd;@var{fd0};@var{fd1};@var{fd2} +@itemx vDefaultInferiorFd +vDefaultInferiorFd packet contains the file descriptors of the same @code{stdin}/ +@code{stdout}/@code{stderr} @value{GDBN} is connected to. If GDBserver is run +locally, it will accept the packet and will use the file descriptors sent by @value{GDBN} +to start the inferior connected to the same @code{stdin}/@code{stdout}/@code{stderr} +@value{GDBN} is connected to. This allows the inferior run under the local GDBserver +to read from the @code{stdin} and write to @code{stdout}/@code{stderr}. +This makes a remote target (which is run locally) to behave similarly to the native target. + +When this feature it not used, inferior is started with its @code{stdin} closed and its +@code{stdout} is redirected to @code{stderr} @value{GDBN} is connected to. + +This packet is not probed by default; the remote stub must request it, +by supplying an appropriate @samp{qSupported} response (@pxref{qSupported}). + +This packet is also available in extended mode (@pxref{extended mode}). +This feature does not support @code{set inferior-tty} command. +At the time of writing this feature only works on Linux. + +Reply: +@table @samp +@item OK +for success +@item E.errtext +for an error +@end table + @item X @var{addr},@var{length}:@var{XX@dots{}} @anchor{X packet} @cindex @samp{X} packet diff --git a/gdb/remote.c b/gdb/remote.c index 72f14e28f54..d90e071f4fb 100644 --- a/gdb/remote.c +++ b/gdb/remote.c @@ -305,6 +305,10 @@ enum { packets and the tag violation stop replies. */ PACKET_memory_tagging_feature, + /* Support for connecting inferior to the same terminal as + GDB when running GDBserver locally. */ + PACKET_vDefaultInferiorFd, + PACKET_MAX }; @@ -726,6 +730,11 @@ struct remote_features bool remote_memory_tagging_p () const { return packet_support (PACKET_memory_tagging_feature) == PACKET_ENABLE; } + /* Returns true if connecting inferior to the same terminal as GDB is + supported, false otherwise. */ + bool remote_fd_switch_p () const + { return packet_support (PACKET_vDefaultInferiorFd) == PACKET_ENABLE; } + /* Reset all packets back to "unknown support". Called when opening a new connection to a remote target. */ void reset_all_packet_configs_support (); @@ -5016,6 +5025,18 @@ remote_target::start_remote_1 (int from_tty, int extended_p) which later probes to skip. */ remote_query_supported (); + if (m_features.packet_support (PACKET_vDefaultInferiorFd) != PACKET_DISABLE + && rs->remote_desc->fds[0] != -1 && rs->remote_desc->fds[1] != -1 + && rs->remote_desc->fds[2] != -1) + { + xsnprintf (rs->buf.data(), rs->buf.size (), "vDefaultInferiorFd;%d;%d;%d", + rs->remote_desc->fds[0], rs->remote_desc->fds[1], + rs->remote_desc->fds[2]); + + putpkt (rs->buf); + getpkt (&rs->buf, 0); + } + /* Check vCont support and set the remote state's vCont_action_support attribute. */ remote_vcont_probe (); @@ -5723,6 +5744,7 @@ static const struct protocol_feature remote_protocol_features[] = { { "no-resumed", PACKET_DISABLE, remote_supported_packet, PACKET_no_resumed }, { "memory-tagging", PACKET_DISABLE, remote_supported_packet, PACKET_memory_tagging_feature }, + { "vDefaultInferiorFd", PACKET_DISABLE, remote_supported_packet, PACKET_vDefaultInferiorFd }, }; static char *remote_support_xml; @@ -5834,6 +5856,10 @@ remote_target::remote_query_supported () != AUTO_BOOLEAN_FALSE) remote_query_supported_append (&q, "memory-tagging+"); + if (m_features.packet_set_cmd_state (PACKET_vDefaultInferiorFd) + != AUTO_BOOLEAN_FALSE) + remote_query_supported_append (&q, "vDefaultInferiorFd+"); + /* Keep this one last to work around a gdbserver <= 7.10 bug in the qSupported:xmlRegisters=i386 handling. */ if (remote_support_xml != NULL @@ -15990,6 +16016,8 @@ Show the maximum size of the address (in bits) in a memory packet."), NULL, add_packet_config_cmd (PACKET_memory_tagging_feature, "memory-tagging-feature", "memory-tagging-feature", 0); + add_packet_config_cmd (PACKET_vDefaultInferiorFd, "vDefaultInferiorFd", "vDefaultInferiorFd", 0); + /* Assert that we've registered "set remote foo-packet" commands for all packet configs. */ { diff --git a/gdbserver/linux-low.cc b/gdbserver/linux-low.cc index 444eebc6bbe..dc3d2629fdb 100644 --- a/gdbserver/linux-low.cc +++ b/gdbserver/linux-low.cc @@ -6199,6 +6199,12 @@ linux_process_target::low_supports_catch_syscall () return false; } +bool +linux_process_target::supports_default_fds () +{ + return true; +} + CORE_ADDR linux_process_target::read_pc (regcache *regcache) { diff --git a/gdbserver/linux-low.h b/gdbserver/linux-low.h index d34d2738238..dfbd079e223 100644 --- a/gdbserver/linux-low.h +++ b/gdbserver/linux-low.h @@ -322,6 +322,8 @@ class linux_process_target : public process_stratum_target bool supports_catch_syscall () override; + bool supports_default_fds () override; + /* Return the information to access registers. This has public visibility because proc-service uses it. */ virtual const regs_info *get_regs_info () = 0; diff --git a/gdbserver/server.cc b/gdbserver/server.cc index 74c7763d777..f9ce02a8594 100644 --- a/gdbserver/server.cc +++ b/gdbserver/server.cc @@ -81,6 +81,12 @@ static bool extended_protocol; static bool response_needed; static bool exit_requested; +/* To be able to connect the inferior to GDB's stdin/out/err, + when GDBserver is run locally, we need to wait with starting + the inferior to have time to recieve the vDefaultInferiorFd + packet. Set to true if need to postpone starting the inferior. */ +static bool deferred_inferior_startup = false; + /* --once: Exit after the first connection has closed. */ bool run_once; @@ -171,6 +177,34 @@ get_client_state () return cs; } +int multi_mode = 0; + +/* To be able to connect the inferior to GDB's stdin/out/err, + when GDBserver is run locally, we need to wait with starting + the inferior to have time to recieve the vDefaultInferiorFd + packet. + + Can only be called when deferred_inferior_startup is true. + Starts the inferior and throws an error if startup fails. + If startup is successful then deferred_inferior_startup is + reset to false. Throw an error on failure. */ +static void +do_deferred_startup () +{ + gdb_assert (deferred_inferior_startup); + client_state &cs = get_client_state (); + target_create_inferior (program_path.get (), program_args); + + if (current_thread != nullptr) + current_process ()->dlls_changed = false; + + if ((cs.last_status.kind () == TARGET_WAITKIND_EXITED + || cs.last_status.kind () == TARGET_WAITKIND_SIGNALLED) + && !multi_mode) + error ("No program to debug"); + + deferred_inferior_startup = false; +} /* Put a stop reply to the stop reply queue. */ @@ -2501,6 +2535,45 @@ supported_btrace_packets (char *buf) strcat (buf, ";qXfer:btrace-conf:read+"); } +/* Parse a vDefaultInferiorFd packet from pkt + and save the received file descriptors into + fds. Return true on success, false otherwise. + Set the file descriptors to -1 on failure. */ +static bool +parse_vdefaultinf (const char *pkt, int *fds) +{ + char *end; + int ret = true; + errno = 0; + + if (*pkt != '\0') + { + fds[0] = (int) strtoul (pkt, &end, 10); + if ((pkt == end) || (*end != ';')) + ret = false; + pkt = end + 1; + fds[1] = (int) strtoul (pkt, &end, 10); + if ((pkt == end) || (*end != ';')) + ret = false; + pkt = end + 1; + fds[2] = (int) strtoul (pkt, &end, 10); + if ((pkt == end) || (*end != '\0')) + ret = false; + if (errno != 0) + ret = false; + } + else + ret = false; + if (!ret) + { + fds[0] = -1; + fds[1] = -1; + fds[2] = -1; + } + + return ret; +} + /* Handle all of the extended 'q' packets. */ static void @@ -2711,6 +2784,8 @@ handle_query (char *own_buf, int packet_len, int *new_packet_len_p) if (target_supports_memory_tagging ()) cs.memory_tagging_feature = true; } + else if (feature == "vDefaultInferiorFd+") + cs.vDefaultInferiorFd_supported = 1; else { /* Move the unknown features all together. */ @@ -2840,6 +2915,18 @@ handle_query (char *own_buf, int packet_len, int *new_packet_len_p) if (target_supports_memory_tagging ()) strcat (own_buf, ";memory-tagging+"); + if (cs.vDefaultInferiorFd_supported) + { + /* If GDB didn't advertise vDefaultInferiorFd then we don't mention + vDefaultInferiorFd in the reply. If GDB did mention vDefaultInferiorFd + then we only claim support if we are started as an stdio target. */ + if (remote_connection_is_stdio () + && target_supports_default_fds ()) + strcat (own_buf, ";vDefaultInferiorFd+"); + else + strcat (own_buf, ";vDefaultInferiorFd-"); + } + /* Reinitialize components as needed for the new connection. */ hostio_handle_new_gdb_connection (); target_handle_new_gdb_connection (); @@ -3543,6 +3630,19 @@ handle_v_requests (char *own_buf, int packet_len, int *new_packet_len) return; } + if (startswith (own_buf, "vDefaultInferiorFd;")) + { + if (!parse_vdefaultinf (own_buf + 19, cs.fds)) + { + strcpy (own_buf, "E.invalid vDefaultInferiorFd packet"); + return; + } + + cs.vDefaultInferiorFd_accepted = true; + write_ok (own_buf); + return; + } + if (startswith (own_buf, "vKill;")) { if (!target_running ()) @@ -4038,7 +4138,6 @@ captured_main (int argc, char *argv[]) char *arg_end; const char *port = NULL; char **next_arg = &argv[1]; - volatile int multi_mode = 0; volatile int attach = 0; int was_running; bool selftest = false; @@ -4307,8 +4406,27 @@ captured_main (int argc, char *argv[]) for (i = 1; i < n; i++) program_args.push_back (xstrdup (next_arg[i])); - /* Wait till we are at first instruction in program. */ - target_create_inferior (program_path.get (), program_args); + if (remote_connection_is_stdio ()) + { + /* Debugger might want to set the default inferior + in/out/err file descriptors, in which case we need to + defer starting the inferior until this information + arrives. */ + deferred_inferior_startup = true; + multi_mode = 1; + + /* Only used when reporting the first stop to GDB. This + should be replaced when we start the inferior, but lets + be on the safe side and set this now just so we carry + around a sane state. */ + cs.last_status.set_exited (0); + cs.last_ptid = minus_one_ptid; + } + else + { + /* Wait till we are at first instruction in program. */ + target_create_inferior (program_path.get (), program_args); + } /* We are now (hopefully) stopped at the first instruction of the target process. This assumes that the target process was @@ -4358,6 +4476,7 @@ captured_main (int argc, char *argv[]) cs.hwbreak_feature = 0; cs.vCont_supported = 0; cs.memory_tagging_feature = false; + cs.vDefaultInferiorFd_supported = false; remote_open (port); @@ -4547,6 +4666,16 @@ process_serial_event (void) response_needed = true; char ch = cs.own_buf[0]; + if (deferred_inferior_startup) + { + /* Actually start the inferior if not a qSupported or + vDefaultInferiorFd packet. vDefaultInferiorFd is + only accepted after qSupported and before any other + request. */ + if (!startswith(cs.own_buf, "qSupported") + && !startswith(cs.own_buf, "vDefaultInferiorFd")) + do_deferred_startup (); + } switch (ch) { case 'q': diff --git a/gdbserver/server.h b/gdbserver/server.h index 0074818c6df..dacc5cb5d6f 100644 --- a/gdbserver/server.h +++ b/gdbserver/server.h @@ -192,6 +192,18 @@ struct client_state /* If true, memory tagging features are supported. */ bool memory_tagging_feature = false; + /* If true, connecting the inferior to the terminal's + stdin/out/err when running GDBserver locally is supported. */ + bool vDefaultInferiorFd_supported = false; + + /* If true, GDBserver accepted file descriptors sent by GDB + and connecting the inferior to the same terminal as GDB + when running GDBserver locally is then supported. */ + bool vDefaultInferiorFd_accepted = false; + + /* File descriptors pointing to terminal's stdin/out/err + sent by GDB to GDBserver. */ + int fds[3]; }; client_state &get_client_state (); diff --git a/gdbserver/target.cc b/gdbserver/target.cc index 23b699d33bb..f19edb67dc5 100644 --- a/gdbserver/target.cc +++ b/gdbserver/target.cc @@ -840,6 +840,12 @@ process_stratum_target::supports_catch_syscall () return false; } +bool +process_stratum_target::supports_default_fds () +{ + return false; +} + int process_stratum_target::get_ipa_tdesc_idx () { diff --git a/gdbserver/target.h b/gdbserver/target.h index 3643b9110da..01d2882a7e1 100644 --- a/gdbserver/target.h +++ b/gdbserver/target.h @@ -497,6 +497,9 @@ class process_stratum_target /* Return true if the target supports catch syscall. */ virtual bool supports_catch_syscall (); + /* Return true if the target supports vDefaultInferiorFd. */ + virtual bool supports_default_fds (); + /* Return tdesc index for IPA. */ virtual int get_ipa_tdesc_idx (); @@ -578,6 +581,9 @@ int kill_inferior (process_info *proc); #define target_supports_catch_syscall() \ the_target->supports_catch_syscall () +#define target_supports_default_fds() \ + the_target->supports_default_fds () + #define target_get_ipa_tdesc_idx() \ the_target->get_ipa_tdesc_idx () From patchwork Fri Jan 19 11:56:28 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexandra Petlanova Hajkova X-Patchwork-Id: 84429 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 5F3113858020 for ; Fri, 19 Jan 2024 11:59:23 +0000 (GMT) X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org 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_H=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 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 X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gdb-patches-bounces+patchwork=sourceware.org@sourceware.org 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"); From patchwork Fri Jan 19 11:56:29 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexandra Petlanova Hajkova X-Patchwork-Id: 84430 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id E80BA38582A1 for ; Fri, 19 Jan 2024 11:59:26 +0000 (GMT) X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org 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 E786E385841C for ; Fri, 19 Jan 2024 11:57:11 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org E786E385841C 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 E786E385841C 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=1705665433; cv=none; b=A/7tH8cy+YN6k+HkyqC8A3iAb8MfJPU2PW0BHnYrjeSXVvW3wLKjtgLBAlnEVAWV96ZOJQCuOvyYL5TsGgtpSzZHwLJ18dZL35Mi278JbXWt5X0wrO9OMFvWBSOuX1SPRdJU56Gmq3+7iyoS3yi/KAYj/paUq9RKXx1UR3+FqtU= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1705665433; c=relaxed/simple; bh=NB7knEpu9tHmfKXS9koJRXQA0g85OhP/d/ioRItYsMg=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=h95ZYPD/SYJdOeVJmLIaJnMIgmuWBFgraYr0Ioi9oQSwgTxcWRzWOFzpUepu7RFgXyXtELSQrtZGU7FpK+YSFoNwOuIEV+nwJIyA+BuW0RP5AzLA73iDVAYYvEPKT1KK39bZruad0BUj3ptg0gY4COXV2evVRRbfYNEYFvN5w9o= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1705665431; 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=GnkS2p36HdxI+bD5NJKvcoCTcPNEY4j5zgAqXKGvXoQ=; b=SbqDuWMkEaGwSIDhv/wuyLDfXunyS7WU3L0nMrvDKa+OSwU608maaXNrx67NwCN7QCGGjz T6hlOthBtUp2y0Dumz0WYjHC/S3OS0LMQf0F2o9U/z6F6XjPR16E0VYUxhVg59VZkXZuFs GcRoHAJIJZAfDFb0j1HVNDFdz8ZOu4Y= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-281-y0TKgKzNOlmK54MzNrGXng-1; Fri, 19 Jan 2024 06:57:09 -0500 X-MC-Unique: y0TKgKzNOlmK54MzNrGXng-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 8AA14881BC0 for ; Fri, 19 Jan 2024 11:57:09 +0000 (UTC) Received: from fedora.redhat.com (unknown [10.45.242.18]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 058A44011FE7 for ; Fri, 19 Jan 2024 11:57:08 +0000 (UTC) From: =?utf-8?q?Alexandra_H=C3=A1jkov=C3=A1?= To: gdb-patches@sourceware.org Subject: [PATCH v2 5/6] remote.c: Add terminal handling functions Date: Fri, 19 Jan 2024 12:56:29 +0100 Message-ID: <20240119115659.491195-7-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 X-Spam-Status: No, score=-11.6 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 X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gdb-patches-bounces+patchwork=sourceware.org@sourceware.org Re-use terminal handling functions from inf-child.c so GDBserver can call them. This helps local remote target to behave more like native target. --- gdb/remote.c | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/gdb/remote.c b/gdb/remote.c index d90e071f4fb..3406eaf43e7 100644 --- a/gdb/remote.c +++ b/gdb/remote.c @@ -607,6 +607,10 @@ class remote_state qSupported. */ gdb_thread_options supported_thread_options = 0; + /* gdbserver accepted the file descriptor numbers of the terminal + GDB is connected to. */ + bool fds_accepted = false; + private: /* Asynchronous signal handle registered as event loop source for when we have pending events ready to be passed to the core. */ @@ -908,10 +912,18 @@ class remote_target : public process_stratum_target int can_do_single_step () override; + bool supports_terminal_ours () override; + + void terminal_init () override; + void terminal_inferior () override; + void terminal_save_inferior() override; + void terminal_ours () override; + void terminal_ours_for_output() override; + bool supports_non_stop () override; bool supports_multi_process () override; @@ -1731,6 +1743,36 @@ remote_target::get_remote_state () return &m_remote_state; } +bool +remote_target::supports_terminal_ours () +{ + return true; +} + +void +remote_target::terminal_init () +{ + struct remote_state *rs = get_remote_state (); + if (rs->fds_accepted) + child_terminal_init (this); +} + +void +remote_target::terminal_save_inferior () +{ + struct remote_state *rs = get_remote_state (); + if (rs->fds_accepted) + child_terminal_save_inferior (this); +} + +void +remote_target::terminal_ours_for_output () +{ + struct remote_state *rs = get_remote_state (); + if (rs->fds_accepted) + child_terminal_ours_for_output (this); +} + /* Fetch the remote exec-file from the current program space. */ static const char * @@ -5035,6 +5077,12 @@ remote_target::start_remote_1 (int from_tty, int extended_p) putpkt (rs->buf); getpkt (&rs->buf, 0); + + if (m_features.packet_ok (rs->buf, PACKET_vDefaultInferiorFd) == PACKET_OK) + rs->fds_accepted = true; + else + error (_("Remote replied unexpectedly to 'vDefaultInferiorFd': %s"), + rs->buf.data ()); } /* Check vCont support and set the remote state's vCont_action_support @@ -7565,11 +7613,18 @@ remote_target::terminal_inferior () /* NOTE: At this point we could also register our selves as the recipient of all input. Any characters typed could then be passed on down to the target. */ + + struct remote_state *rs = get_remote_state (); + if (rs->fds_accepted) + child_terminal_inferior (this); } void remote_target::terminal_ours () { + struct remote_state *rs = get_remote_state (); + if (rs->fds_accepted) + child_terminal_ours (this); } static void From patchwork Fri Jan 19 11:56:30 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexandra Petlanova Hajkova X-Patchwork-Id: 84431 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 772C23858430 for ; Fri, 19 Jan 2024 11:59:32 +0000 (GMT) X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org 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 9C48C38582B2 for ; Fri, 19 Jan 2024 11:57:12 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 9C48C38582B2 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 9C48C38582B2 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=1705665434; cv=none; b=ML2wL7F0yEGtAdUo67eh3/TtDVzlju4tXfUiNJ0/fbayHWxSxBKPbchYoUeiCSn3PwQvy4G5eZKQFYEafo4Mq5eaQKF7KXmlYX5ASzt6sm06nKPlkKCkQJI+yx1hPfdwDjIfES30L1LjaOReqOhDSkSzNrFs+SmsEmjcwZ6EGxA= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1705665434; c=relaxed/simple; bh=IQDRjtelO/AWKuzaR+taQiKSp1Q0uhfygxOUk8JrlZM=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=MivWmvVP/qhWmFVRRzyPKoHz3TLs2lJ/qzRnCUtyLTUyYfby03QU+oqFnOGHbi2vVsRJSF0MLg33nUKUQ/SDxq3w0QBKgjZE4XNt6N/xvOXwTh/KtG6/I2i2TSgTYyH55l0ZDi1Pf8GDkoTSJCCW3Cgt68dVHL/qtzIDIE2Okok= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1705665431; 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=Nh8Ev/IZG8EGjCGsqACR3poa8l138F4FpuVBnEE17qo=; b=Fg6bzWGaN5gbFuHabRgzVfgAFkR8U1bHKksuOQsjJAZ83txtOZrPhFMV4LnJtFK1bm7o/l 3WgQyzmP5YtS/D4b3VrPZ3Bz4Vh6Mtw815PEvbWO/WMw12awDq3aQ1x2S0jHWyoJyElUBH ec/mE2mq9mERREiUO5n2KLPl5gzuf+M= 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-647-WXKs_OuuPHy0sSZ6mwReJg-1; Fri, 19 Jan 2024 06:57:10 -0500 X-MC-Unique: WXKs_OuuPHy0sSZ6mwReJg-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 5E5BF3C2AB0E for ; Fri, 19 Jan 2024 11:57:10 +0000 (UTC) Received: from fedora.redhat.com (unknown [10.45.242.18]) by smtp.corp.redhat.com (Postfix) with ESMTPS id D4B724011C41 for ; Fri, 19 Jan 2024 11:57:09 +0000 (UTC) From: =?utf-8?q?Alexandra_H=C3=A1jkov=C3=A1?= To: gdb-patches@sourceware.org Subject: [PATCH v2 6/6] Add defaultinf.exp test to the testsuite Date: Fri, 19 Jan 2024 12:56:30 +0100 Message-ID: <20240119115659.491195-8-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 X-Spam-Status: No, score=-11.9 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, 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 X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gdb-patches-bounces+patchwork=sourceware.org@sourceware.org Test the behaviour of the inferior when GDBserver is run locally and using stdio. The inferior should be able to write to STOUT and STDERR and read from STDIN. --- gdb/testsuite/gdb.server/defaultinf.c | 39 ++++++++++++++++ gdb/testsuite/gdb.server/defaultinf.exp | 59 +++++++++++++++++++++++++ 2 files changed, 98 insertions(+) create mode 100644 gdb/testsuite/gdb.server/defaultinf.c create mode 100644 gdb/testsuite/gdb.server/defaultinf.exp diff --git a/gdb/testsuite/gdb.server/defaultinf.c b/gdb/testsuite/gdb.server/defaultinf.c new file mode 100644 index 00000000000..8963c7dbabf --- /dev/null +++ b/gdb/testsuite/gdb.server/defaultinf.c @@ -0,0 +1,39 @@ +/* Copyright 2023 Free Software Foundation, Inc. + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . */ + +#include +#include + +int +main () +{ + printf ("Hello stdout!\n"); + fprintf (stderr, "Hello stderr!\n"); + + char *line = NULL; + size_t len = 0; + ssize_t nread; + + while ((nread = getline (&line, &len, stdin)) != -1) + { + if (line[nread - 1] == '\n') + line[nread - 1] = '\0'; + printf ("Got line len %zd: '%s'\n", nread, line); + } + + printf ("Got EOF\n"); + free (line); + return 0; +} diff --git a/gdb/testsuite/gdb.server/defaultinf.exp b/gdb/testsuite/gdb.server/defaultinf.exp new file mode 100644 index 00000000000..03163e150e0 --- /dev/null +++ b/gdb/testsuite/gdb.server/defaultinf.exp @@ -0,0 +1,59 @@ +# This testcase is part of GDB, the GNU debugger. + +# Copyright 2023 Free Software Foundation, Inc. + +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +# Test the behaviour of the inferior when GDBserver is run locally +# and using stdio. The inferior should be able to write to STOUT +# and STDERR and read from STDIN. + +require {!is_remote target} {!is_remote host} + +load_lib gdbserver-support.exp + +require allow_gdbserver_tests + +set gdbserver [find_gdbserver] +if { $gdbserver == "" } { + unsupported "could not find GDBserver" + return +} + +standard_testfile + +if {[build_executable "failed to prepare" $testfile $srcfile debug]} { + return -1 +} + +clean_restart ${testfile} + +gdb_test "target remote | ${::gdbserver} - [standard_output_file $testfile]" ".*" \ + "start gdbserver using pipe syntax" + + set input_msg "hello world" + set input_len [expr [string length ${input_msg}] + 1] + gdb_test_multiple "c" "Test STDOUT/ERR, STDIN" { + -re "Hello stdout!\r\nHello stderr!" { + send_gdb "${input_msg}\n" + exp_continue + } + -re "${input_msg}\r\nGot line len ${input_len}: '${input_msg}'" { + send_gdb "\004" + exp_continue + } + -re "Got EOF" { + pass "$gdb_test_name" + } + }