From: Tom Tromey <tromey@adacore.com>
To: gdb-patches@sourceware.org
Cc: Tom Tromey <tromey@adacore.com>
Subject: [PATCH 2/3] Refactor code to check for terminal sharing
Date: Mon, 5 Dec 2022 11:56:50 -0700 [thread overview]
Message-ID: <20221205185651.2704492-3-tromey@adacore.com> (raw)
In-Reply-To: <20221205185651.2704492-1-tromey@adacore.com>
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 <windows.h>
@@ -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<DWORD> 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
next prev parent reply other threads:[~2022-12-05 18:57 UTC|newest]
Thread overview: 18+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-12-05 18:56 [PATCH 0/3] Fix Windows C-c handling Tom Tromey
2022-12-05 18:56 ` [PATCH 1/3] Rename install_sigint_handler Tom Tromey
2022-12-05 18:56 ` Tom Tromey [this message]
2022-12-05 19:50 ` [PATCH 2/3] Refactor code to check for terminal sharing Eli Zaretskii
2022-12-12 17:27 ` Tom Tromey
2022-12-05 18:56 ` [PATCH 3/3] Fix control-c handling on Windows Tom Tromey
2022-12-07 17:13 ` Hannes Domani
2022-12-09 15:58 ` Tom Tromey
2022-12-09 16:19 ` Hannes Domani
2022-12-09 17:20 ` Tom Tromey
2022-12-09 18:13 ` Hannes Domani
2022-12-12 15:36 ` Tom Tromey
2022-12-12 17:05 ` Tom Tromey
2022-12-13 11:30 ` Hannes Domani
2022-12-13 19:51 ` Tom Tromey
2022-12-05 18:59 ` [PATCH 0/3] Fix Windows C-c handling Tom Tromey
2022-12-12 13:23 ` Jon Turney
2022-12-12 17:29 ` Tom Tromey
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20221205185651.2704492-3-tromey@adacore.com \
--to=tromey@adacore.com \
--cc=gdb-patches@sourceware.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).