[v3,2/6] Share parts of gdb/terminal.h with gdbserver

Message ID 20170208032257.15443-3-sergiodj@redhat.com
State New, archived
Headers

Commit Message

Sergio Durigan Junior Feb. 8, 2017, 3:22 a.m. UTC
  As part of the bigger work of sharing fork_inferior with gdbserver,
some parts of gdb/terminal.h also needed to be moved to a common
place.  These parts are:

- The code responsible for determining some terminal-based define's
  based on available features;

- job control;

- terminal-related functions needed by fork_inferior;

gdb/ChangeLog:
yyyy-mm-dd  Sergio Durigan Junior  <sergiodj@redhat.com>

	* Makefile.in (HFILES_NO_SRCDIR): Add "common/common-terminal.h".
	* common/common-terminal.h: New file, with parts of "terminal.h".
	* terminal.h: Move terminal-related defines to
	"common/common-terminal.h".  Include "common/common-terminal.h".
	(new_tty_prefork): Move to "common/common-terminal.h".
	(new_tty): Likewise.
	(new_tty_postfork): Likewise.
	(job_control): Likewise.
	(create_tty_session): Likewise.
	(gdb_setpgid): Likewise.
	* utils.c: Include "terminal.h".

gdb/gdbserver/ChangeLog:
yyyy-mm-dd  Sergio Durigan Junior  <sergiodj@redhat.com>

	* terminal.c: New file.
---
 gdb/Makefile.in              |   1 +
 gdb/common/common-terminal.h | 125 +++++++++++++++++++++++++++++++++++++++++++
 gdb/gdbserver/terminal.c     |  88 ++++++++++++++++++++++++++++++
 gdb/terminal.h               |  73 +------------------------
 gdb/utils.c                  |   1 +
 5 files changed, 216 insertions(+), 72 deletions(-)
 create mode 100644 gdb/common/common-terminal.h
 create mode 100644 gdb/gdbserver/terminal.c
  

Comments

Pedro Alves Feb. 15, 2017, 3:54 p.m. UTC | #1
On 02/08/2017 03:22 AM, Sergio Durigan Junior wrote:
> As part of the bigger work of sharing fork_inferior with gdbserver,
> some parts of gdb/terminal.h also needed to be moved to a common
> place.  These parts are:
> 
> - The code responsible for determining some terminal-based define's
>   based on available features;
> 
> - job control;
> 
> - terminal-related functions needed by fork_inferior;
> 


> diff --git a/gdb/common/common-terminal.h b/gdb/common/common-terminal.h
> new file mode 100644
> index 0000000..956bfcc
> --- /dev/null
> +++ b/gdb/common/common-terminal.h
> @@ -0,0 +1,125 @@


> +#ifndef COMMON_TERMINAL_H
> +#define COMMON_TERMINAL_H
> +
> +/* If we're using autoconf, it will define HAVE_TERMIOS_H,
> +   HAVE_TERMIO_H and HAVE_SGTTY_H for us.  One day we can rewrite
> +   ser-unix.c and inflow.c to inspect those names instead of
> +   HAVE_TERMIOS, HAVE_TERMIO and the implicit HAVE_SGTTY (when neither
> +   HAVE_TERMIOS or HAVE_TERMIO is set).  Until then, make sure that
> +   nothing has already defined the one of the names, and do the right
> +   thing.  */

We try to keep common/ self-contained wrt autoconf -- i.e.,
corresponding autoconf checks should be added to common/common.m4.

> +
> +#if !defined (HAVE_TERMIOS) && !defined(HAVE_TERMIO) && !defined(HAVE_SGTTY)
> +#if defined(HAVE_TERMIOS_H)
> +#define HAVE_TERMIOS


> +/* Do we have job control?  Can be assumed to always be the same
> +   within a given run of GDB.  Use in gdb/inflow.c and
> +   common/common-inflow.c.  */
> +extern int job_control;
> +

...

> +/* Set the process group of the caller to its own pid, or do nothing
> +   if we lack job control.  */
> +extern int gdb_setpgid (void);
> +
> +/* Determine whether we have job control, and set variable JOB_CONTROL
> +   accordingly.  */
> +extern void have_job_control (void);

It had been clearer if these three were moved in the patch that
moves the corresponding .c code.  I was going to comment in that
patch that the "have_job_control" documentation should
mention that it must be called early, before "job_control"
is ever used.  And that that patch would have better been the one
that adds the have_job_control() calls to the appropriate
places.  With the split as is, this detail ends up easily missed.

> +
> +#endif /* ! COMMON_TERMINAL_H */
> diff --git a/gdb/gdbserver/terminal.c b/gdb/gdbserver/terminal.c
> new file mode 100644
> index 0000000..42ac651
> --- /dev/null
> +++ b/gdb/gdbserver/terminal.c
> @@ -0,0 +1,88 @@

> +/* See common/common-terminal.h.  */
> +
> +pid_t
> +create_tty_session (void)
> +{
> +  /* Placeholder needed by fork_inferior.  For now, this function is
> +     not needed nor useful to have on gdbserver.  When/If we properly
> +     handle terminal modes, we can revisit and implement the needed
> +     support.  */
> +  return (pid_t) 1;

Without looking at the caller, it would seem to me that -1 / 0 
would make more sense, and "1" kind of looks like a typo/bug.
Please add a comment mentioning why this returns 1.

> +}
> +
> +/* See common/common-inferior.h.  */
> +
> +void
> +set_inferior_io_terminal (const char *terminal_name)
> +{
> +  /* Placeholder needed by fork_inferior.  For now, this function is
> +     not needed nor useful to have on gdbserver.  When/If we properly
> +     handle terminal modes, we can revisit and implement the needed
> +     support.  */
> +}
> +
> +/* See common/common-inferior.h.  */
> +
> +const char *
> +get_inferior_io_terminal (void)
> +{
> +  /* Placeholder needed by fork_inferior.  For now, this function is
> +     not needed nor useful to have on gdbserver.  When/If we properly
> +     handle terminal modes, we can revisit and implement the needed
> +     support.  */

How about instead of repeating the same comment multiple times,
we add it once at the top:

  /* Placeholders needed by fork_inferior.  For now, these functions are
     not needed nor useful to have on gdbserver.  When/If we properly
     handle terminal modes, we can revisit and implement the needed
     support.  */

?

Thanks,
Pedro Alves
  
Sergio Durigan Junior Feb. 16, 2017, 9:37 p.m. UTC | #2
Hey Pedro,

Thanks for the review.  Comments below.

On Wednesday, February 15 2017, Pedro Alves wrote:

> On 02/08/2017 03:22 AM, Sergio Durigan Junior wrote:
>> As part of the bigger work of sharing fork_inferior with gdbserver,
>> some parts of gdb/terminal.h also needed to be moved to a common
>> place.  These parts are:
>> 
>> - The code responsible for determining some terminal-based define's
>>   based on available features;
>> 
>> - job control;
>> 
>> - terminal-related functions needed by fork_inferior;
>> 
>
>
>> diff --git a/gdb/common/common-terminal.h b/gdb/common/common-terminal.h
>> new file mode 100644
>> index 0000000..956bfcc
>> --- /dev/null
>> +++ b/gdb/common/common-terminal.h
>> @@ -0,0 +1,125 @@
>
>
>> +#ifndef COMMON_TERMINAL_H
>> +#define COMMON_TERMINAL_H
>> +
>> +/* If we're using autoconf, it will define HAVE_TERMIOS_H,
>> +   HAVE_TERMIO_H and HAVE_SGTTY_H for us.  One day we can rewrite
>> +   ser-unix.c and inflow.c to inspect those names instead of
>> +   HAVE_TERMIOS, HAVE_TERMIO and the implicit HAVE_SGTTY (when neither
>> +   HAVE_TERMIOS or HAVE_TERMIO is set).  Until then, make sure that
>> +   nothing has already defined the one of the names, and do the right
>> +   thing.  */
>
> We try to keep common/ self-contained wrt autoconf -- i.e.,
> corresponding autoconf checks should be added to common/common.m4.

Done.  Thanks for bringing this to my attention.

>> +
>> +#if !defined (HAVE_TERMIOS) && !defined(HAVE_TERMIO) && !defined(HAVE_SGTTY)
>> +#if defined(HAVE_TERMIOS_H)
>> +#define HAVE_TERMIOS
>
>
>> +/* Do we have job control?  Can be assumed to always be the same
>> +   within a given run of GDB.  Use in gdb/inflow.c and
>> +   common/common-inflow.c.  */
>> +extern int job_control;
>> +
>
> ...
>
>> +/* Set the process group of the caller to its own pid, or do nothing
>> +   if we lack job control.  */
>> +extern int gdb_setpgid (void);
>> +
>> +/* Determine whether we have job control, and set variable JOB_CONTROL
>> +   accordingly.  */
>> +extern void have_job_control (void);
>
> It had been clearer if these three were moved in the patch that
> moves the corresponding .c code.  I was going to comment in that
> patch that the "have_job_control" documentation should
> mention that it must be called early, before "job_control"
> is ever used.  And that that patch would have better been the one
> that adds the have_job_control() calls to the appropriate
> places.  With the split as is, this detail ends up easily missed.

Hm, OK.  I'll reorganize this part of the patch, then.  Sorry about
that.

>> +#endif /* ! COMMON_TERMINAL_H */
>> diff --git a/gdb/gdbserver/terminal.c b/gdb/gdbserver/terminal.c
>> new file mode 100644
>> index 0000000..42ac651
>> --- /dev/null
>> +++ b/gdb/gdbserver/terminal.c
>> @@ -0,0 +1,88 @@
>
>> +/* See common/common-terminal.h.  */
>> +
>> +pid_t
>> +create_tty_session (void)
>> +{
>> +  /* Placeholder needed by fork_inferior.  For now, this function is
>> +     not needed nor useful to have on gdbserver.  When/If we properly
>> +     handle terminal modes, we can revisit and implement the needed
>> +     support.  */
>> +  return (pid_t) 1;
>
> Without looking at the caller, it would seem to me that -1 / 0 
> would make more sense, and "1" kind of looks like a typo/bug.
> Please add a comment mentioning why this returns 1.

You're right, this should have been 0.  Fixed now.

>> +}
>> +
>> +/* See common/common-inferior.h.  */
>> +
>> +void
>> +set_inferior_io_terminal (const char *terminal_name)
>> +{
>> +  /* Placeholder needed by fork_inferior.  For now, this function is
>> +     not needed nor useful to have on gdbserver.  When/If we properly
>> +     handle terminal modes, we can revisit and implement the needed
>> +     support.  */
>> +}
>> +
>> +/* See common/common-inferior.h.  */
>> +
>> +const char *
>> +get_inferior_io_terminal (void)
>> +{
>> +  /* Placeholder needed by fork_inferior.  For now, this function is
>> +     not needed nor useful to have on gdbserver.  When/If we properly
>> +     handle terminal modes, we can revisit and implement the needed
>> +     support.  */
>
> How about instead of repeating the same comment multiple times,
> we add it once at the top:
>
>   /* Placeholders needed by fork_inferior.  For now, these functions are
>      not needed nor useful to have on gdbserver.  When/If we properly
>      handle terminal modes, we can revisit and implement the needed
>      support.  */
>
> ?

Yeah, I did that on the other file but forgot about this one.  Done,
thanks.

I'll wait until all of your comments are addressed before I send v4.

Cheers,
  

Patch

diff --git a/gdb/Makefile.in b/gdb/Makefile.in
index 74b4d79..6dc80ea 100644
--- a/gdb/Makefile.in
+++ b/gdb/Makefile.in
@@ -1472,6 +1472,7 @@  HFILES_NO_SRCDIR = \
 	common/common-regcache.h \
 	common/common-types.h \
 	common/common-utils.h \
+	common/common-terminal.h \
 	common/errors.h \
 	common/environ.h \
 	common/fileio.h \
diff --git a/gdb/common/common-terminal.h b/gdb/common/common-terminal.h
new file mode 100644
index 0000000..956bfcc
--- /dev/null
+++ b/gdb/common/common-terminal.h
@@ -0,0 +1,125 @@ 
+/* Common terminal interface definitions for GDB and gdbserver.
+   Copyright (C) 1986-2017 Free Software Foundation, Inc.
+
+   This file is part of GDB.
+
+   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 <http://www.gnu.org/licenses/>.  */
+
+#ifndef COMMON_TERMINAL_H
+#define COMMON_TERMINAL_H
+
+/* If we're using autoconf, it will define HAVE_TERMIOS_H,
+   HAVE_TERMIO_H and HAVE_SGTTY_H for us.  One day we can rewrite
+   ser-unix.c and inflow.c to inspect those names instead of
+   HAVE_TERMIOS, HAVE_TERMIO and the implicit HAVE_SGTTY (when neither
+   HAVE_TERMIOS or HAVE_TERMIO is set).  Until then, make sure that
+   nothing has already defined the one of the names, and do the right
+   thing.  */
+
+#if !defined (HAVE_TERMIOS) && !defined(HAVE_TERMIO) && !defined(HAVE_SGTTY)
+#if defined(HAVE_TERMIOS_H)
+#define HAVE_TERMIOS
+#else /* ! defined (HAVE_TERMIOS_H) */
+#if defined(HAVE_TERMIO_H)
+#define HAVE_TERMIO
+#else /* ! defined (HAVE_TERMIO_H) */
+#if defined(HAVE_SGTTY_H)
+#define HAVE_SGTTY
+#endif /* ! defined (HAVE_SGTTY_H) */
+#endif /* ! defined (HAVE_TERMIO_H) */
+#endif /* ! defined (HAVE_TERMIOS_H) */
+#endif /* !defined (HAVE_TERMIOS) && !defined (HAVE_TERMIO) &&
+	  !defined (HAVE_SGTTY) */
+
+#if defined(HAVE_TERMIOS)
+#include <termios.h>
+#endif
+
+#if !defined(_WIN32) && !defined (HAVE_TERMIOS)
+
+/* Define a common set of macros -- BSD based -- and redefine whatever
+   the system offers to make it look like that.  FIXME: serial.h and
+   ser-*.c deal with this in a much cleaner fashion; as soon as stuff
+   is converted to use them, can get rid of this crap.  */
+
+#ifdef HAVE_TERMIO
+
+#include <termio.h>
+
+#undef TIOCGETP
+#define TIOCGETP TCGETA
+#undef TIOCSETN
+#define TIOCSETN TCSETA
+#undef TIOCSETP
+#define TIOCSETP TCSETAF
+#define TERMINAL struct termio
+
+#else /* sgtty */
+
+#include <fcntl.h>
+#include <sgtty.h>
+#include <sys/ioctl.h>
+#define TERMINAL struct sgttyb
+
+#endif /* sgtty */
+#endif
+
+#include <sys/types.h>
+
+/* Do we have job control?  Can be assumed to always be the same
+   within a given run of GDB.  Use in gdb/inflow.c and
+   common/common-inflow.c.  */
+extern int job_control;
+
+extern void new_tty (void);
+
+/* NEW_TTY_PREFORK is called before forking a new child process,
+   so we can record the state of ttys in the child to be formed.
+   TTYNAME is null if we are to share the terminal with gdb;
+   or points to a string containing the name of the desired tty.
+
+   NEW_TTY is called in new child processes under Unix, which will
+   become debugger target processes.  This actually switches to
+   the terminal specified in the NEW_TTY_PREFORK call.
+   
+   This function is implemented as a placeholder on GDBserver.  */
+extern void new_tty_prefork (const char *ttyname);
+
+/* NEW_TTY_POSTFORK is called after forking a new child process, and
+   adding it to the inferior table, to store the TTYNAME being used by
+   the child, or null if it sharing the terminal with gdb.
+
+   This function is implemented as a placeholder on GDBserver.  */
+extern void new_tty_postfork (void);
+
+/* Create a new session if the inferior will run in a different tty.
+   A session is UNIX's way of grouping processes that share a controlling
+   terminal, so a new one is needed if the inferior terminal will be
+   different from GDB's.
+
+   Returns the session id of the new session, 0 if no session was created
+   or -1 if an error occurred.
+
+   This function is implemented as a placeholder on GDBserver.  */
+extern pid_t create_tty_session (void);
+
+/* Set the process group of the caller to its own pid, or do nothing
+   if we lack job control.  */
+extern int gdb_setpgid (void);
+
+/* Determine whether we have job control, and set variable JOB_CONTROL
+   accordingly.  */
+extern void have_job_control (void);
+
+#endif /* ! COMMON_TERMINAL_H */
diff --git a/gdb/gdbserver/terminal.c b/gdb/gdbserver/terminal.c
new file mode 100644
index 0000000..42ac651
--- /dev/null
+++ b/gdb/gdbserver/terminal.c
@@ -0,0 +1,88 @@ 
+/* Terminal interface definitions for the GDB remote server.
+   Copyright (C) 2016-2017 Free Software Foundation, Inc.
+
+   This file is part of GDB.
+
+   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 <http://www.gnu.org/licenses/>.  */
+
+#include "server.h"
+#include "common-terminal.h"
+
+/* See common/common-terminal.h.  */
+
+void
+new_tty (void)
+{
+  /* Placeholder needed by fork_inferior.  For now, this function is
+     not needed nor useful to have on gdbserver.  When/If we properly
+     handle terminal modes, we can revisit and implement the needed
+     support.  */
+}
+
+/* See common/common-terminal.h.  */
+
+void
+new_tty_prefork (const char *ttyname)
+{
+  /* Placeholder needed by fork_inferior.  For now, this function is
+     not needed nor useful to have on gdbserver.  When/If we properly
+     handle terminal modes, we can revisit and implement the needed
+     support.  */
+}
+
+/* See common/common-terminal.h.  */
+
+void
+new_tty_postfork (void)
+{
+  /* Placeholder needed by fork_inferior.  For now, this function is
+     not needed nor useful to have on gdbserver.  When/If we properly
+     handle terminal modes, we can revisit and implement the needed
+     support.  */
+}
+
+/* See common/common-terminal.h.  */
+
+pid_t
+create_tty_session (void)
+{
+  /* Placeholder needed by fork_inferior.  For now, this function is
+     not needed nor useful to have on gdbserver.  When/If we properly
+     handle terminal modes, we can revisit and implement the needed
+     support.  */
+  return (pid_t) 1;
+}
+
+/* See common/common-inferior.h.  */
+
+void
+set_inferior_io_terminal (const char *terminal_name)
+{
+  /* Placeholder needed by fork_inferior.  For now, this function is
+     not needed nor useful to have on gdbserver.  When/If we properly
+     handle terminal modes, we can revisit and implement the needed
+     support.  */
+}
+
+/* See common/common-inferior.h.  */
+
+const char *
+get_inferior_io_terminal (void)
+{
+  /* Placeholder needed by fork_inferior.  For now, this function is
+     not needed nor useful to have on gdbserver.  When/If we properly
+     handle terminal modes, we can revisit and implement the needed
+     support.  */
+  return NULL;
+}
diff --git a/gdb/terminal.h b/gdb/terminal.h
index d8691b2..8139319 100644
--- a/gdb/terminal.h
+++ b/gdb/terminal.h
@@ -19,83 +19,12 @@ 
 #if !defined (TERMINAL_H)
 #define TERMINAL_H 1
 
-
-/* If we're using autoconf, it will define HAVE_TERMIOS_H,
-   HAVE_TERMIO_H and HAVE_SGTTY_H for us.  One day we can rewrite
-   ser-unix.c and inflow.c to inspect those names instead of
-   HAVE_TERMIOS, HAVE_TERMIO and the implicit HAVE_SGTTY (when neither
-   HAVE_TERMIOS or HAVE_TERMIO is set).  Until then, make sure that
-   nothing has already defined the one of the names, and do the right
-   thing.  */
-
-#if !defined (HAVE_TERMIOS) && !defined(HAVE_TERMIO) && !defined(HAVE_SGTTY)
-#if defined(HAVE_TERMIOS_H)
-#define HAVE_TERMIOS
-#else /* ! defined (HAVE_TERMIOS_H) */
-#if defined(HAVE_TERMIO_H)
-#define HAVE_TERMIO
-#else /* ! defined (HAVE_TERMIO_H) */
-#if defined(HAVE_SGTTY_H)
-#define HAVE_SGTTY
-#endif /* ! defined (HAVE_SGTTY_H) */
-#endif /* ! defined (HAVE_TERMIO_H) */
-#endif /* ! defined (HAVE_TERMIOS_H) */
-#endif /* !defined (HAVE_TERMIOS) && !defined (HAVE_TERMIO) &&
-	  !defined (HAVE_SGTTY) */
-
-#if defined(HAVE_TERMIOS)
-#include <termios.h>
-#endif
-
-#if !defined(_WIN32) && !defined (HAVE_TERMIOS)
-
-/* Define a common set of macros -- BSD based -- and redefine whatever
-   the system offers to make it look like that.  FIXME: serial.h and
-   ser-*.c deal with this in a much cleaner fashion; as soon as stuff
-   is converted to use them, can get rid of this crap.  */
-
-#ifdef HAVE_TERMIO
-
-#include <termio.h>
-
-#undef TIOCGETP
-#define TIOCGETP TCGETA
-#undef TIOCSETN
-#define TIOCSETN TCSETA
-#undef TIOCSETP
-#define TIOCSETP TCSETAF
-#define TERMINAL struct termio
-
-#else /* sgtty */
-
-#include <fcntl.h>
-#include <sgtty.h>
-#include <sys/ioctl.h>
-#define TERMINAL struct sgttyb
-
-#endif /* sgtty */
-#endif
+#include "common-terminal.h"
 
 struct inferior;
 
-extern void new_tty_prefork (const char *);
-
-extern void new_tty (void);
-
-extern void new_tty_postfork (void);
-
 extern void copy_terminal_info (struct inferior *to, struct inferior *from);
 
-/* Do we have job control?  Can be assumed to always be the same within
-   a given run of GDB.  In inflow.c.  */
-extern int job_control;
-
-extern pid_t create_tty_session (void);
-
-/* Set the process group of the caller to its own pid, or do nothing if
-   we lack job control.  */
-extern int gdb_setpgid (void);
-
 /* Set up a serial structure describing standard input.  In inflow.c.  */
 extern void initialize_stdin_serial (void);
 
diff --git a/gdb/utils.c b/gdb/utils.c
index 3dc2f03..6cbaf07 100644
--- a/gdb/utils.c
+++ b/gdb/utils.c
@@ -53,6 +53,7 @@ 
 #include "top.h"
 #include "main.h"
 #include "solist.h"
+#include "terminal.h"
 
 #include "inferior.h"		/* for signed_pointer_to_address */