From patchwork Fri Nov 17 11:18:35 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexandra Petlanova Hajkova X-Patchwork-Id: 80118 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 2B34F3858426 for ; Fri, 17 Nov 2023 11:19:17 +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 392E03858D37 for ; Fri, 17 Nov 2023 11:18:47 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 392E03858D37 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 392E03858D37 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=1700219928; cv=none; b=qWZIQx5uxB4+Y4ZlEOwOUiuYcErZUr9KZKQ8PerJZmMb56VPMQe8ntcb+BtewrDt0cImGRpirITMz3Tx6KySAUiN1Ca+r5F+KYUOK8i4trTlriuRv3Em7AVVJ3Gu3FjCGls04is3F9XwmV4H8qlV83qbmzlQO2nGCyCly62Iz4M= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1700219928; c=relaxed/simple; bh=RcTHOJCgBOUwTTfL6zkbIE70k2Arbkm3Y+CJz5WETw0=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=wZxFjfoANcey+ePAG4ru+RtQ9AX8fm3ZgYgdVRCz6Tkmt3mf6+CMLkU9IHXBxTR8FggJy7HRFipX+k0u+9Mu66X9g+/aQESdUgR0bXpqAWq20t4Fj/vcTXNRkpTTH/Br/OTzktFWR9vmspSyBJfJR4wdcBvioNbbmg7vwN9mkPo= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1700219926; 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=rSaHTZPBHJuOkDb0G6wsHLD8YsJmXMl8KvMC1ULM7mE=; b=RfPRYWDHsj4AdsdTtWd7c1Xv7cBKzCp9PV6cU4nrMzO1n4b/9UsQyljkOlxNbFE+IQTH6O us/0uu1ysEXdwylkieYtdTA5PQeZlc2qG7lbBikSlP0X+pFn2tZLfmXtpqsdEg1I1bLWr6 F/BizLi4BD1kt4uvsNfVv4IjpU0G+iE= 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-608-E3m_ChBwPvyAR-LU0vXmuQ-1; Fri, 17 Nov 2023 06:18:45 -0500 X-MC-Unique: E3m_ChBwPvyAR-LU0vXmuQ-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 8122538116E0 for ; Fri, 17 Nov 2023 11:18:45 +0000 (UTC) Received: from fedora.redhat.com (unknown [10.45.242.6]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 6B7DC2026D4C; Fri, 17 Nov 2023 11:18:44 +0000 (UTC) From: =?utf-8?q?Alexandra_H=C3=A1jkov=C3=A1?= To: gdb-patches@sourceware.org Cc: ahajkova@redhat.com Subject: [PATCH 1/6] gdb.server/non-existing-program.exp: Use gdbserver_start. Date: Fri, 17 Nov 2023 12:18:35 +0100 Message-ID: <20231117111840.2040709-2-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 X-Spam-Status: No, score=-10.3 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 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 eea1eb1d911..99b98e1222c 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 30d94fd7eb6..aa22315ef92 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 Nov 17 11:18:36 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexandra Petlanova Hajkova X-Patchwork-Id: 80117 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 4929F3858408 for ; Fri, 17 Nov 2023 11:19:10 +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 1E3B53858C98 for ; Fri, 17 Nov 2023 11:18:49 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 1E3B53858C98 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 1E3B53858C98 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=1700219930; cv=none; b=rvri9CRgRJqvW0V4ii/YS17sdQf5Fks41iujy7CEHblE55e85/EO8fqcUaNxzwRhUUuMehuqWjXZToGVADLhTjAVeprz3XpeVshgAAZWW9TWNTHsro5rdWYAaIuJfUCNS/VLtzR3GGpGtU+bWhXwYSdOQMzp6ZOTEesUby7J2e0= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1700219930; c=relaxed/simple; bh=71cgKv7O20ar55M/3gjqbpd84bj9jncmvcnADr6J6lc=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=Rg0/fJeauCJA8Cc6V1u8Yf05g73C1b73lup2/RMii+Xx+78+SJqPzN6fO9UZENOXWNTF36iO2Rqpp5ifyxumjxWuseS3ZW/dnGngGY38iDcSC9u/yO7XBc3ED72LYqoLhjNv5vx2MzAKZHodDwwJwr8rXTLz4VIP7uNg3Ce2M7g= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1700219928; 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=1ettGmkh/krZKvHrwZs1j5rftxhwzQFQnf65eTMlyLc=; b=O7K5JDXs96MC8Ha3tcE9ZbVnep18gI8r3GBeRPAY/2IoQmJ4npAI0bPD/TNItmbua3h+4w Z0lQEr7f5KrHSheJRcwZma5ocNAS2HhYta1p/QooHrEhMf+Em3cBLFGXS2pr+ToMU2TCTQ aeNMassacLZTX/DznEMzqeJnax3hJGY= 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-37-UmRCOnFUPUyXqRpQZM_8eg-1; Fri, 17 Nov 2023 06:18:47 -0500 X-MC-Unique: UmRCOnFUPUyXqRpQZM_8eg-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 D9A2C1C04353 for ; Fri, 17 Nov 2023 11:18:46 +0000 (UTC) Received: from fedora.redhat.com (unknown [10.45.242.6]) by smtp.corp.redhat.com (Postfix) with ESMTPS id E464B2026D4C; Fri, 17 Nov 2023 11:18:45 +0000 (UTC) From: =?utf-8?q?Alexandra_H=C3=A1jkov=C3=A1?= To: gdb-patches@sourceware.org Cc: ahajkova@redhat.com Subject: [PATCH 2/6] gdb/ser-pipe.c: Duplicate the file descriptors Date: Fri, 17 Nov 2023 12:18:36 +0100 Message-ID: <20231117111840.2040709-3-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 X-Spam-Status: No, score=-10.3 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 he 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 | 25 +++++++++++++++++++++++++ gdb/serial.c | 4 ++++ gdb/serial.h | 4 ++++ 3 files changed, 33 insertions(+) diff --git a/gdb/ser-pipe.c b/gdb/ser-pipe.c index 47ccd33cece..84253cf2e2c 100644 --- a/gdb/ser-pipe.c +++ b/gdb/ser-pipe.c @@ -77,6 +77,16 @@ pipe_open (struct serial *scb, const char *name) return -1; } + /* 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); + + 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'' @@ -90,6 +100,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; return -1; } @@ -128,6 +144,10 @@ pipe_open (struct serial *scb, const char *name) close (err_pdes[1]); } + mark_fd_no_cloexec (saved_stdout); + mark_fd_no_cloexec (saved_stdin); + mark_fd_no_cloexec (saved_stderr); + close_most_fds (); const char *shellfile = get_shell (); @@ -139,6 +159,11 @@ pipe_open (struct serial *scb, const char *name) close (pdes[1]); if (err_pdes[1] != -1) close (err_pdes[1]); + + 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 8a8bab46ead..a2bbe9a972d 100644 --- a/gdb/serial.c +++ b/gdb/serial.c @@ -182,6 +182,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 3b861200302..113f7360a55 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 Nov 17 11:18:37 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexandra Petlanova Hajkova X-Patchwork-Id: 80119 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 9C0B23858438 for ; Fri, 17 Nov 2023 11:19: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.133.124]) by sourceware.org (Postfix) with ESMTPS id 01357385840A for ; Fri, 17 Nov 2023 11:18:49 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 01357385840A 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 01357385840A 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=1700219932; cv=none; b=YO5ZBW095PA1JBoonyAbFkDrsX2JvSJl/W4zN/ycs8RtqIMJVvb2CKSswDXjtoVqhVcXin4rh+/Dvxi+s/nzs2xeBSZu+eTMa4x6bkmK4cxTMw//yJZrxGqTKZhasvMQJBMiAqVcXq9M8GHjwGwH9DqxgmosMvHaNCse6EfW1ug= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1700219932; c=relaxed/simple; bh=YomGGVaBNmG8XtludhFc19PchU0Phvs0S9WbKd66xvU=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=iIHbJpxJmRQk2HESObe170qwOthG9s2DDeZjFFjIHiV7Yf++AgUXera1dXS1dUzu061PtNwQdqIJJjkYCbjqYCs7W6sWr3lLcUN/5HE2aS9VaaoR1tMl02Lbjwymahm4eLZDc10bz+cl7ZGl+h18a12zTbX8qyowuLNTJjfOcwQ= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1700219929; 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=R0F6CF18ktZFFXj+M49m5bE1CTkobiZh8V4mWgnOMsw=; b=fwqx5lij9IYe+Nwl+fTAucidOzj9B7LdZIK75Jj3vqELbmuF5tJKIogPB6w9YSajw1YIQV UhHqLx504V0bzU6BKBaVFyCX1gx+ZXdWjUIOtb8rRCTY1Ukm8HlkbE925JjowbwZgMpvqo Yf4hSzpzo0oZfyPqw6VPWWyWGob/BR0= 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-158-9Gt7VfM9Pt2q7fMI1wedXw-1; Fri, 17 Nov 2023 06:18:48 -0500 X-MC-Unique: 9Gt7VfM9Pt2q7fMI1wedXw-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 4AD18881E22 for ; Fri, 17 Nov 2023 11:18:48 +0000 (UTC) Received: from fedora.redhat.com (unknown [10.45.242.6]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 47AA32026D4C; Fri, 17 Nov 2023 11:18:47 +0000 (UTC) From: =?utf-8?q?Alexandra_H=C3=A1jkov=C3=A1?= To: gdb-patches@sourceware.org Cc: ahajkova@redhat.com Subject: [PATCH 3/6] Add new vDefaultInferiorFd packet Date: Fri, 17 Nov 2023 12:18:37 +0100 Message-ID: <20231117111840.2040709-4-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 X-Spam-Status: No, score=-10.1 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 | 32 +++++++++++ gdb/remote.c | 28 ++++++++++ gdbserver/server.cc | 132 +++++++++++++++++++++++++++++++++++++++++++- gdbserver/server.h | 12 ++++ 4 files changed, 201 insertions(+), 3 deletions(-) diff --git a/gdb/doc/gdb.texinfo b/gdb/doc/gdb.texinfo index e4c00143fd1..69f0d0499ef 100644 --- a/gdb/doc/gdb.texinfo +++ b/gdb/doc/gdb.texinfo @@ -23210,6 +23210,15 @@ 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 +@code{target remote | gdbserver - inferior}. +Inferior's STDIN is closed which means the inferior can't read any input. + +If GDBserver is run locally using stdio and supports the @xref{default inferior +file descriptors} feature, GDBserver will start inferior connected to the same +terminal as @value{GDBN} which enables the inferior to read from STDIN and +write to the STDOUT/ERR in the same fashion native target does. + @anchor{Attaching in Types of Remote Connections} @item Attaching @strong{With target remote mode:} The @value{GDBN} command @code{attach} is @@ -42934,6 +42943,29 @@ for success (@pxref{Stop Reply Packets}) @cindex @samp{vStopped} packet @xref{Notification Packets}. +@item vDefaultInferior;@var{fd0};@var{fd1};@var{fd2} +@item vDefaultInferiorFd +@anchor{default inferior file descriptors} +@cindex @samp{vDefaultInferiorFd} packet +@value{GDBN} would preserve the numbers of STDOUT/STDIN/STDERR file descriptors. +Preserved numbers of the file descriptors are then sent to GDBserver. +If GDBserver is run locally and will accept the numbers of the file +descriptors, it will start the inferior connected to the same STDIN/OUT/ERR +@value{GDBN} is connected to. This allows the inferior run under the local GDBserver +to read from the STDIN and write to STOUT/ERR. This makes a remote target (which is +run locally) to behave similarly to the native target. + +This packet is also available in extended mode (@pxref{extended mode}). +This feature does not support setting inferior tty. + +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 ce5addade6f..27d053c367c 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 (); @@ -5010,6 +5019,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 (); @@ -5717,6 +5738,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; @@ -5828,6 +5850,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 @@ -15984,6 +16010,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/server.cc b/gdbserver/server.cc index a8e23561dcb..e69e5c125c4 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; } +volatile 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. */ @@ -2274,6 +2308,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 @@ -2484,6 +2557,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. */ @@ -2613,6 +2688,17 @@ 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 ()) + 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 (); @@ -3316,6 +3402,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 ()) @@ -3807,7 +3906,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; @@ -4056,8 +4154,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 @@ -4107,6 +4224,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); @@ -4295,6 +4413,14 @@ 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. */ + 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 306d75d4e9b..7afdc9f6cd4 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 (); From patchwork Fri Nov 17 11:18:38 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexandra Petlanova Hajkova X-Patchwork-Id: 80120 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 062E73858C2D for ; Fri, 17 Nov 2023 11:19:34 +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 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_H=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 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 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 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"); From patchwork Fri Nov 17 11:18:39 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexandra Petlanova Hajkova X-Patchwork-Id: 80121 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 6410D38582A5 for ; Fri, 17 Nov 2023 11:19:51 +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 59DF6385843A for ; Fri, 17 Nov 2023 11:18:54 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 59DF6385843A 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 59DF6385843A 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=1700219935; cv=none; b=S7NaUC1so+5tKnK6TGibmqQoiznKdF6EsK5UYClN5sZ3zW8WikWHQ6oLx/tfZIcQnRImqZY5bVZn5AE9LNQZV/BHhbmGaH8BXP/O1AiBjy+lwtBg6sL2Kyz5/wlFIF6MCOhgZX1i2kq6sNr+SU3AEhLQ1qqqJLg4PC4OxSgS4ak= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1700219935; c=relaxed/simple; bh=zl6SbykT0f8V4USPdRb345DkZGEgKjDIQ0F2k7OuWHA=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=rrzif9xbl101XESkgm444p3nXHmp2iIVFnpwLZm3frS2ecklj7cHMPSeVDzXf40p+xmAi3CdAuLs6rKCKbvqVVHhBznYr2CabVMI2HwNEzIv8Xyo9tC/jfvxermTpV8KOGno+nIG183IJWgG4UCVX7/1NkYmxABhVgKa5THnVtg= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1700219934; 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=+blr+/wdIPf5QpJiVZT4tybQIN6EaOhjmxHqx2ll5cA=; b=NIZghobuTFfnO31UtyqzjE6ZHDpU/vQMzAAoUeJ3bEueji0iMrMAhAS7vBg2htCCuzsxTU iAdM3p5b8qmLLD37enJit8/LvwS2+LUsPwmtastWcmQH273wOJUo+t3dd+UkMVyyjYKAVU AcrMYHXeuvaLW9fTd/cbaDYBR2fxIcM= 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-479-8XGgJPP6PU-lv72pxHDN8Q-1; Fri, 17 Nov 2023 06:18:50 -0500 X-MC-Unique: 8XGgJPP6PU-lv72pxHDN8Q-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 B30A7185A780 for ; Fri, 17 Nov 2023 11:18:50 +0000 (UTC) Received: from fedora.redhat.com (unknown [10.45.242.6]) by smtp.corp.redhat.com (Postfix) with ESMTPS id BC5482026D4C; Fri, 17 Nov 2023 11:18:49 +0000 (UTC) From: =?utf-8?q?Alexandra_H=C3=A1jkov=C3=A1?= To: gdb-patches@sourceware.org Cc: ahajkova@redhat.com Subject: [PATCH 5/6] remote.c: Add terminal handling functions Date: Fri, 17 Nov 2023 12:18:39 +0100 Message-ID: <20231117111840.2040709-6-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 X-Spam-Status: No, score=-10.2 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 27d053c367c..68835a29244 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; @@ -1732,6 +1744,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 * @@ -5029,6 +5071,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 @@ -7558,11 +7606,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 Nov 17 11:18:40 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexandra Petlanova Hajkova X-Patchwork-Id: 80122 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 7C5573858299 for ; Fri, 17 Nov 2023 11:19: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.133.124]) by sourceware.org (Postfix) with ESMTPS id F10B0385841B for ; Fri, 17 Nov 2023 11:18:53 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org F10B0385841B 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 F10B0385841B 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=1700219936; cv=none; b=KfYnhuVTaXfpqXueBuVxhKKkKS/QV9FdTOB+tGOk8VW0/5UUxYshVil5cU7cuLd3wx5b4Djshha8ux6cSEKUy/XH+E6HbvLLovXXi3GfzywpXrB8ZHnhgtxcJP+mOigV1GGj6/JWuhghnJKs1pNPVvwhP2Ykn8GtZbgg91kKNMw= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1700219936; c=relaxed/simple; bh=9eAt2A5aJlGbZav+974a5VzMZiAhGXcn9LQSw3seQz8=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=LYirt5KvfC47TbS1PcPDThcb8FM7pziJpwMXe6vv6G3xLATzzhUetJKcdcBtdj2bzey9RLoKLoVZfFolsXuucSM40/swW7dXCJ15+bvz+BzYH34/UyOW7vBOnu0mKT0KIydlO1Pss25NrcVdz+Jmk+2uSQB/y2Tz8AvqXF+NTSc= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1700219933; 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=7v6LNSyAaKn+b1KWEYkyDK64oCfWeHTju54macLrf3A=; b=SS2BGgxWACJbsMeqJsO3CaI9aYbUQ9zVFGk4IvYA7uCLf5i0v+rux+ffAMwAVKz6n5Dk3O IZuZxeyAOWUxcylP7vmwEnyFc1F25kx7GzTlNucEW2DgJfjqftI0rMr2jFM3ZFNHNGeHHA VArlWbqaM9NyPtoB5vUFrfRPGKyd8u8= 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-622-TV9yJjovM-iuzVITdAImdg-1; Fri, 17 Nov 2023 06:18:52 -0500 X-MC-Unique: TV9yJjovM-iuzVITdAImdg-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 6818038116E3 for ; Fri, 17 Nov 2023 11:18:52 +0000 (UTC) Received: from fedora.redhat.com (unknown [10.45.242.6]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 41F882026D4C; Fri, 17 Nov 2023 11:18:51 +0000 (UTC) From: =?utf-8?q?Alexandra_H=C3=A1jkov=C3=A1?= To: gdb-patches@sourceware.org Cc: ahajkova@redhat.com Subject: [PATCH 6/6] Add defaultinf.exp test to the testsuite Date: Fri, 17 Nov 2023 12:18:40 +0100 Message-ID: <20231117111840.2040709-7-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 X-Spam-Status: No, score=-10.5 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_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 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" + } + }