From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-il1-x131.google.com (mail-il1-x131.google.com [IPv6:2607:f8b0:4864:20::131]) by sourceware.org (Postfix) with ESMTPS id 619F33947C1E for ; Mon, 5 Dec 2022 18:57:00 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 619F33947C1E Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=adacore.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=adacore.com Received: by mail-il1-x131.google.com with SMTP id o13so5498954ilc.7 for ; Mon, 05 Dec 2022 10:57:00 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=adacore.com; s=google; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=e76TkS87vnR35H5MuZakzS3F8w2RJrvmoQRCM0cOk1A=; b=OIDYxygIVDgFdMfSw6NeaDbiViasZFy+eW5KZbCqiaXTRFIeA4EuXpSUfw3xE6GBfc e14OSK1ROFU5aiDxWr95h9V1WrCcs5jQaL9oGVpYcwlgsOWrO/bpF+WNa9hAkGjVZ9S/ aACbJ14VIx6tc3TIi/QRn3nJMZdttXiioH844ZdPm7LuCchzhpHS3/naFBzZ5IFj7Vlv 61EbR3ju0EkVHrPgLwb4VhfDCkd2McfOtM1FOSTL7+T5hUXXoLWyyXSIthgE5JKNjXpn MGe53M+3LQFUbo2y/fFV7lMbZiivN3Ad6R9RXt+YYpqhrzTYF95uiOZdc+n4nePNRNLR F7pg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=e76TkS87vnR35H5MuZakzS3F8w2RJrvmoQRCM0cOk1A=; b=BF7oWK370p+anJKWe1bTdfNmIX/L8rw2XzgUpzzxBWhC3pUqgZRUMd6d95LuYeBcBe CsDwN2cOc47uigfl5cQM4h2eVQ60FnG/AVMmKYia3Q/A76MIlO5NbqErdK8xJA92ofDH KVDRFtJVF8idJYJM8ZMaB3YZOLHC5MNkq3epERAoFVHV0mqy2EJHuDjrjfo9wB/8apYd KYBDRCT+LA5VxMY+AbvxDaAp+rU2h8zHzlh2eooGuXgttzofHUWKKlmqqLEeLFzQm0hK B6qzmBkKzjIWAWXHDbTEo6zxd9GmU5EkMb+7XYoTltJ8iabl3maQbij5u9kPFrp2izqX YYHw== X-Gm-Message-State: ANoB5pmfX7X5dNvDQwiyW1/1s37Lh70SbL3tPYz2Fx49TqHyCKqtw+mv aBgOiosWiTHpFDDYcQM+efebE1YEWtJV2XR9 X-Google-Smtp-Source: AA0mqf4qWv7ToO5EaN4W3JFK/ssHwJd/8FJWtGy+U1Y3QdeU1n0VcMPPu8qzzEITjkdtYFrW2yJMGQ== X-Received: by 2002:a92:d144:0:b0:300:5d50:15d3 with SMTP id t4-20020a92d144000000b003005d5015d3mr27937984ilg.173.1670266619637; Mon, 05 Dec 2022 10:56:59 -0800 (PST) Received: from localhost.localdomain (97-122-76-186.hlrn.qwest.net. [97.122.76.186]) by smtp.gmail.com with ESMTPSA id z29-20020a027a5d000000b00389d02a032dsm2000459jad.172.2022.12.05.10.56.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 05 Dec 2022 10:56:59 -0800 (PST) From: Tom Tromey To: gdb-patches@sourceware.org Cc: Tom Tromey Subject: [PATCH 2/3] Refactor code to check for terminal sharing Date: Mon, 5 Dec 2022 11:56:50 -0700 Message-Id: <20221205185651.2704492-3-tromey@adacore.com> X-Mailer: git-send-email 2.34.3 In-Reply-To: <20221205185651.2704492-1-tromey@adacore.com> References: <20221205185651.2704492-1-tromey@adacore.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-11.1 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: This refactors the code to check for terminal sharing. is_gdb_terminal is exported, and sharing_input_terminal_1 is renamed, slightly refactored, and moved to posix-hdep.c. A new Windows-specific implementation of this function is added to mingw-hdep.c. One thing I'm not sure of here is how GetConsoleProcessList will work in all situations. MSDN has a warning about it: This API is not recommended and does not have a virtual terminal equivalent. [...] Applications remoting via cross-platform utilities and transports like SSH may not work as expected if using this API. However, I don't precisely know what this means. --- gdb/inferior.h | 11 +++++++++++ gdb/inflow.c | 27 +++------------------------ gdb/mingw-hdep.c | 31 ++++++++++++++++++++++++++++++- gdb/posix-hdep.c | 20 +++++++++++++++++++- 4 files changed, 63 insertions(+), 26 deletions(-) diff --git a/gdb/inferior.h b/gdb/inferior.h index 69525a2e053..547e8751d08 100644 --- a/gdb/inferior.h +++ b/gdb/inferior.h @@ -167,6 +167,17 @@ extern void default_print_float_info (struct gdbarch *gdbarch, frame_info_ptr frame, const char *args); +/* Try to determine whether TTY is GDB's input terminal. Returns + TRIBOOL_UNKNOWN if we can't tell. */ + +extern tribool is_gdb_terminal (const char *tty); + +/* Helper for sharing_input_terminal. Try to determine whether pid + PID is using the same TTY for input as GDB is. Returns + TRIBOOL_UNKNOWN if we can't tell. */ + +extern tribool sharing_input_terminal (int pid); + extern void child_terminal_info (struct target_ops *self, const char *, int); extern void child_terminal_ours (struct target_ops *self); diff --git a/gdb/inflow.c b/gdb/inflow.c index 5477624bcd5..17b1e8ce282 100644 --- a/gdb/inflow.c +++ b/gdb/inflow.c @@ -212,10 +212,9 @@ gdb_save_tty_state (void) } } -/* Try to determine whether TTY is GDB's input terminal. Returns - TRIBOOL_UNKNOWN if we can't tell. */ +/* See inferior.h. */ -static tribool +tribool is_gdb_terminal (const char *tty) { struct stat gdb_tty; @@ -236,26 +235,6 @@ is_gdb_terminal (const char *tty) : TRIBOOL_FALSE); } -/* Helper for sharing_input_terminal. Try to determine whether - inferior INF is using the same TTY for input as GDB is. Returns - TRIBOOL_UNKNOWN if we can't tell. */ - -static tribool -sharing_input_terminal_1 (inferior *inf) -{ - /* Using host-dependent code here is fine, because the - child_terminal_foo functions are meant to be used by child/native - targets. */ -#if defined (__linux__) || defined (__sun__) - char buf[100]; - - xsnprintf (buf, sizeof (buf), "/proc/%d/fd/0", inf->pid); - return is_gdb_terminal (buf); -#else - return TRIBOOL_UNKNOWN; -#endif -} - /* Return true if the inferior is using the same TTY for input as GDB is. If this is true, then we save/restore terminal flags/state. @@ -287,7 +266,7 @@ sharing_input_terminal (inferior *inf) { terminal_info *tinfo = get_inflow_inferior_data (inf); - tribool res = sharing_input_terminal_1 (inf); + tribool res = sharing_input_terminal (inf->pid); if (res == TRIBOOL_UNKNOWN) { diff --git a/gdb/mingw-hdep.c b/gdb/mingw-hdep.c index 651e795216d..9d0337cf4ef 100644 --- a/gdb/mingw-hdep.c +++ b/gdb/mingw-hdep.c @@ -21,8 +21,8 @@ #include "main.h" #include "serial.h" #include "gdbsupport/event-loop.h" - #include "gdbsupport/gdb_select.h" +#include "inferior.h" #include @@ -371,3 +371,32 @@ gdb_console_fputs (const char *linebuf, FILE *fstream) last_style = style; return 1; } + +/* See inferior.h. */ + +tribool +sharing_input_terminal (int pid) +{ + std::vector results (10); + DWORD len = 0; + while (true) + { + len = GetConsoleProcessList (results.data (), results.size ()); + /* Note that LEN == 0 is a failure, but we can treat it the same + as a "no". */ + if (len < results.size ()) + break; + + results.resize (len); + } + /* In case the vector was too big. */ + results.resize (len); + if (std::find (results.begin (), results.end (), pid) != results.end ()) + { + /* The pid is in the list sharing the console, so don't + interrupt the inferior -- it will get the signal itself. */ + return TRIBOOL_TRUE; + } + + return TRIBOOL_FALSE; +} diff --git a/gdb/posix-hdep.c b/gdb/posix-hdep.c index 3d44338d2ae..26211978d06 100644 --- a/gdb/posix-hdep.c +++ b/gdb/posix-hdep.c @@ -19,8 +19,8 @@ #include "defs.h" #include "gdbsupport/event-loop.h" - #include "gdbsupport/gdb_select.h" +#include "inferior.h" /* Wrapper for select. Nothing special needed on POSIX platforms. */ @@ -38,3 +38,21 @@ gdb_console_fputs (const char *buf, FILE *f) { return 0; } + +/* See inferior.h. */ + +tribool +sharing_input_terminal (int pid) +{ + /* Using host-dependent code here is fine, because the + child_terminal_foo functions are meant to be used by child/native + targets. */ +#if defined (__linux__) || defined (__sun__) + char buf[100]; + + xsnprintf (buf, sizeof (buf), "/proc/%d/fd/0", pid); + return is_gdb_terminal (buf); +#else + return TRIBOOL_UNKNOWN; +#endif +} -- 2.34.3