[PATCH v2 22/25] Make main_ui be heap allocated

Pedro Alves
2016-03-21T15:21:54Z

This is preparation for being able to create more than one UI object.

The change to gdb_main to stop using catch_errors is necessary because
catch_errors references current_uiout, which expands to
current_ui->m_current_ui, which would crash because current_ui is not
initialized yet at that point. It didn't trigger earlier in the
series because before this patch, main_ui/current_ui always start out
non-NULL.
---
gdb/event-top.c | 7 +++----
gdb/main.c | 27 ++++++++++++---------------
gdb/top.c | 37 +++++++++++++++++++++++++++++++++++++
gdb/top.h | 5 +++++
4 files changed, 57 insertions(+), 19 deletions(-)

diff --git a/gdb/event-top.c b/gdb/event-top.c
index 436edf7..69f6be7 100644
--- a/gdb/event-top.c
+++ b/gdb/event-top.c
@@ -362,11 +362,10 @@ top_level_prompt (void)
return xstrdup (prompt);
}

-static struct ui main_ui_;

-struct ui *main_ui = &main_ui_;
-struct ui *current_ui = &main_ui_;
-struct ui *ui_list = &main_ui_;
+struct ui *main_ui;
+struct ui *current_ui;
+struct ui *ui_list;

void
restore_ui_cleanup (void *data)
diff --git a/gdb/main.c b/gdb/main.c
index e107f60..95578e4 100644
--- a/gdb/main.c
+++ b/gdb/main.c
@@ -441,7 +441,6 @@ DEF_VEC_O (cmdarg_s);
static int
captured_main (void *data)
{
- struct ui *ui = current_ui;
struct captured_main_args *context = (struct captured_main_args *) data;
int argc = context->argc;
char **argv = context->argv;
@@ -513,26 +512,15 @@ captured_main (void *data)
clear_quit_flag ();
saved_command_line = (char *) xstrdup ("");

- ui->instream = stdin;
- ui->outstream = stdout;
- ui->errstream = stderr;
-
- ui->input_fd = fileno (stdin);
-
- ui->prompt_state = PROMPT_NEEDED;
-
#ifdef __MINGW32__
/* Ensure stderr is unbuffered. A Cygwin pty or pipe is implemented
as a Windows pipe, and Windows buffers on pipes. */
setvbuf (stderr, NULL, _IONBF, BUFSIZ);
#endif

- gdb_stdout = stdio_fileopen (stdout);
- gdb_stderr = stderr_fileopen (stderr);
+ main_ui = new_ui (stdin, stdout, stderr);
+ current_ui = main_ui;

- gdb_stdlog = gdb_stderr; /* for moment */
- gdb_stdtarg = gdb_stderr; /* for moment */
- gdb_stdin = stdio_fileopen (stdin);
gdb_stdtargerr = gdb_stderr; /* for moment */
gdb_stdtargin = gdb_stdin; /* for moment */

@@ -1173,7 +1161,16 @@ captured_main (void *data)
int
gdb_main (struct captured_main_args *args)
{
- catch_errors (captured_main, args, "", RETURN_MASK_ALL);
+ TRY
+ {
+ captured_main (args);
+ }
+ CATCH (ex, RETURN_MASK_ALL)
+ {
+ exception_print (gdb_stderr, ex);
+ }
+ END_CATCH
+
/* The only way to end up here is by an error (normal exit is
handled by quit_force()), hence always return an error status. */
return 1;
diff --git a/gdb/top.c b/gdb/top.c
index 2941dbd..12064bc 100644
--- a/gdb/top.c
+++ b/gdb/top.c
@@ -245,6 +245,43 @@ void (*deprecated_call_command_hook) (struct cmd_list_element * c,

void (*deprecated_context_hook) (int id);

+static int highest_ui_num;
+
+struct ui *
+new_ui (FILE *instream, FILE *outstream, FILE *errstream)
+{
+ struct ui *ui;
+
+ ui = XCNEW (struct ui);
+
+ ui->num = ++highest_ui_num;
+ ui->instream = instream;
+ ui->outstream = outstream;
+ ui->errstream = errstream;
+
+ ui->input_fd = fileno (ui->instream);
+
+ ui->m_gdb_stdin = stdio_fileopen (ui->instream);
+ ui->m_gdb_stdout = stdio_fileopen (ui->outstream);
+ ui->m_gdb_stderr = stderr_fileopen (ui->errstream);
+ ui->m_gdb_stdlog = ui->m_gdb_stderr;
+
+ ui->prompt_state = PROMPT_NEEDED;
+
+ if (ui_list == NULL)
+ ui_list = ui;
+ else
+ {
+ struct ui *last;
+
+ for (last = ui_list; last->next != NULL; last = last->next)
+ ;
+ last->next = ui;
+ }
+
+ return ui;
+}
+
/* Handler for SIGHUP. */

#ifdef SIGHUP
diff --git a/gdb/top.h b/gdb/top.h
index 29deb3f..df76204 100644
--- a/gdb/top.h
+++ b/gdb/top.h
@@ -56,6 +56,9 @@ struct ui
{
struct ui *next;

+ /* Convenient handle (UI number). Unique across all UIs. */
+ int num;
+
/* The UI's command line buffer. This is to used to accumulate
input until we have a whole command line. */
struct buffer line_buffer;
@@ -157,6 +160,8 @@ extern void switch_thru_all_uis_next (struct switch_thru_all_uis *state);
#define ALL_UIS(UI) \
for (UI = ui_list; UI; UI = UI->next) \

+extern struct ui *new_ui (FILE *instream, FILE *outstream, FILE *errstream);
+
extern void restore_ui_cleanup (void *data);

/* From top.c. */
--
2.5.0