xref: /netbsd-src/external/gpl3/gdb.old/dist/gdb/top.h (revision 8b657b0747480f8989760d71343d6dd33f8d4cf9)
1 /* Top level stuff for GDB, the GNU debugger.
2 
3    Copyright (C) 1986-2023 Free Software Foundation, Inc.
4 
5    This file is part of GDB.
6 
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3 of the License, or
10    (at your option) any later version.
11 
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16 
17    You should have received a copy of the GNU General Public License
18    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
19 
20 #ifndef TOP_H
21 #define TOP_H
22 
23 #include "gdbsupport/buffer.h"
24 #include "gdbsupport/event-loop.h"
25 #include "gdbsupport/next-iterator.h"
26 #include "value.h"
27 
28 /* Prompt state.  */
29 
30 enum prompt_state
31 {
32   /* The command line is blocked simulating synchronous execution.
33      This is used to implement the foreground execution commands
34      ('run', 'continue', etc.).  We won't display the prompt and
35      accept further commands until the execution is actually over.  */
36   PROMPT_BLOCKED,
37 
38   /* The command finished; display the prompt before returning back to
39      the top level.  */
40   PROMPT_NEEDED,
41 
42   /* We've displayed the prompt already, ready for input.  */
43   PROMPTED,
44 };
45 
46 /* All about a user interface instance.  Each user interface has its
47    own I/O files/streams, readline state, its own top level
48    interpreter (for the main UI, this is the interpreter specified
49    with -i on the command line) and secondary interpreters (for
50    interpreter-exec ...), etc.  There's always one UI associated with
51    stdin/stdout/stderr, but the user can create secondary UIs, for
52    example, to create a separate MI channel on its own stdio
53    streams.  */
54 
55 struct ui
56 {
57   /* Create a new UI.  */
58   ui (FILE *instream, FILE *outstream, FILE *errstream);
59   ~ui ();
60 
61   DISABLE_COPY_AND_ASSIGN (ui);
62 
63   /* Pointer to next in singly-linked list.  */
64   struct ui *next = nullptr;
65 
66   /* Convenient handle (UI number).  Unique across all UIs.  */
67   int num;
68 
69   /* The UI's command line buffer.  This is to used to accumulate
70      input until we have a whole command line.  */
71   std::string line_buffer;
72 
73   /* The callback used by the event loop whenever an event is detected
74      on the UI's input file descriptor.  This function incrementally
75      builds a buffer where it accumulates the line read up to the
76      point of invocation.  In the special case in which the character
77      read is newline, the function invokes the INPUT_HANDLER callback
78      (see below).  */
79   void (*call_readline) (gdb_client_data) = nullptr;
80 
81   /* The function to invoke when a complete line of input is ready for
82      processing.  */
83   void (*input_handler) (gdb::unique_xmalloc_ptr<char> &&) = nullptr;
84 
85   /* True if this UI is using the readline library for command
86      editing; false if using GDB's own simple readline emulation, with
87      no editing support.  */
88   int command_editing = 0;
89 
90   /* Each UI has its own independent set of interpreters.  */
91   struct ui_interp_info *interp_info = nullptr;
92 
93   /* True if the UI is in async mode, false if in sync mode.  If in
94      sync mode, a synchronous execution command (e.g, "next") does not
95      return until the command is finished.  If in async mode, then
96      running a synchronous command returns right after resuming the
97      target.  Waiting for the command's completion is later done on
98      the top event loop.  For the main UI, this starts out disabled,
99      until all the explicit command line arguments (e.g., `gdb -ex
100      "start" -ex "next"') are processed.  */
101   int async = 0;
102 
103   /* The number of nested readline secondary prompts that are
104      currently active.  */
105   int secondary_prompt_depth = 0;
106 
107   /* The UI's stdin.  Set to stdin for the main UI.  */
108   FILE *stdin_stream;
109 
110   /* stdio stream that command input is being read from.  Set to stdin
111      normally.  Set by source_command to the file we are sourcing.
112      Set to NULL if we are executing a user-defined command or
113      interacting via a GUI.  */
114   FILE *instream;
115   /* Standard output stream.  */
116   FILE *outstream;
117   /* Standard error stream.  */
118   FILE *errstream;
119 
120   /* The file descriptor for the input stream, so that we can register
121      it with the event loop.  This can be set to -1 to prevent this
122      registration.  */
123   int input_fd;
124 
125   /* Whether ISATTY returns true on input_fd.  Cached here because
126      quit_force needs to know this _after_ input_fd might be
127      closed.  */
128   bool m_input_interactive_p;
129 
130   /* See enum prompt_state's description.  */
131   enum prompt_state prompt_state = PROMPT_NEEDED;
132 
133   /* The fields below that start with "m_" are "private".  They're
134      meant to be accessed through wrapper macros that make them look
135      like globals.  */
136 
137   /* The ui_file streams.  */
138   /* Normal results */
139   struct ui_file *m_gdb_stdout;
140   /* Input stream */
141   struct ui_file *m_gdb_stdin;
142   /* Serious error notifications */
143   struct ui_file *m_gdb_stderr;
144   /* Log/debug/trace messages that should bypass normal stdout/stderr
145      filtering.  */
146   struct ui_file *m_gdb_stdlog;
147 
148   /* The current ui_out.  */
149   struct ui_out *m_current_uiout = nullptr;
150 
151   /* Register the UI's input file descriptor in the event loop.  */
152   void register_file_handler ();
153 
154   /* Unregister the UI's input file descriptor from the event loop.  */
155   void unregister_file_handler ();
156 
157   /* Return true if this UI's input fd is a tty.  */
158   bool input_interactive_p () const;
159 };
160 
161 /* The main UI.  This is the UI that is bound to stdin/stdout/stderr.
162    It always exists and is created automatically when GDB starts
163    up.  */
164 extern struct ui *main_ui;
165 
166 /* The current UI.  */
167 extern struct ui *current_ui;
168 
169 /* The list of all UIs.  */
170 extern struct ui *ui_list;
171 
172 /* State for SWITCH_THRU_ALL_UIS.  */
173 class switch_thru_all_uis
174 {
175 public:
176 
177   switch_thru_all_uis () : m_iter (ui_list), m_save_ui (&current_ui)
178   {
179     current_ui = ui_list;
180   }
181 
182   DISABLE_COPY_AND_ASSIGN (switch_thru_all_uis);
183 
184   /* If done iterating, return true; otherwise return false.  */
185   bool done () const
186   {
187     return m_iter == NULL;
188   }
189 
190   /* Move to the next UI, setting current_ui if iteration is not yet
191      complete.  */
192   void next ()
193   {
194     m_iter = m_iter->next;
195     if (m_iter != NULL)
196       current_ui = m_iter;
197   }
198 
199  private:
200 
201   /* Used to iterate through the UIs.  */
202   struct ui *m_iter;
203 
204   /* Save and restore current_ui.  */
205   scoped_restore_tmpl<struct ui *> m_save_ui;
206 };
207 
208   /* Traverse through all UI, and switch the current UI to the one
209      being iterated.  */
210 #define SWITCH_THRU_ALL_UIS()		\
211   for (switch_thru_all_uis stau_state; !stau_state.done (); stau_state.next ())
212 
213 using ui_range = next_range<ui>;
214 
215 /* An adapter that can be used to traverse over all UIs.  */
216 static inline
217 ui_range all_uis ()
218 {
219   return ui_range (ui_list);
220 }
221 
222 /* From top.c.  */
223 extern bool confirm;
224 extern int inhibit_gdbinit;
225 
226 /* Print the GDB version banner to STREAM.  If INTERACTIVE is false,
227    then information referring to commands (e.g., "show configuration")
228    is omitted; this mode is used for the --version command line
229    option.  If INTERACTIVE is true, then interactive commands are
230    mentioned.  */
231 extern void print_gdb_version (struct ui_file *stream, bool interactive);
232 
233 extern void print_gdb_configuration (struct ui_file *);
234 
235 extern void read_command_file (FILE *);
236 extern void init_history (void);
237 extern void command_loop (void);
238 extern int quit_confirm (void);
239 extern void quit_force (int *, int);
240 extern void quit_command (const char *, int);
241 extern void quit_cover (void);
242 extern void execute_command (const char *, int);
243 
244 /* If the interpreter is in sync mode (we're running a user command's
245    list, running command hooks or similars), and we just ran a
246    synchronous command that started the target, wait for that command
247    to end.  WAS_SYNC indicates whether sync_execution was set before
248    the command was run.  */
249 
250 extern void maybe_wait_sync_command_done (int was_sync);
251 
252 /* Wait for a synchronous execution command to end.  */
253 extern void wait_sync_command_done (void);
254 
255 extern void check_frame_language_change (void);
256 
257 /* Prepare for execution of a command.
258    Call this before every command, CLI or MI.
259    Returns a cleanup to be run after the command is completed.  */
260 extern scoped_value_mark prepare_execute_command (void);
261 
262 /* This function returns a pointer to the string that is used
263    by gdb for its command prompt.  */
264 extern const std::string &get_prompt ();
265 
266 /* This function returns a pointer to the string that is used
267    by gdb for its command prompt.  */
268 extern void set_prompt (const char *s);
269 
270 /* Return 1 if UI's current input handler is a secondary prompt, 0
271    otherwise.  */
272 
273 extern int gdb_in_secondary_prompt_p (struct ui *ui);
274 
275 /* Perform _initialize initialization.  */
276 extern void gdb_init ();
277 
278 /* For use by event-top.c.  */
279 /* Variables from top.c.  */
280 extern int source_line_number;
281 extern std::string source_file_name;
282 extern bool history_expansion_p;
283 extern bool server_command;
284 extern char *lim_at_start;
285 
286 extern void gdb_add_history (const char *);
287 
288 extern void show_commands (const char *args, int from_tty);
289 
290 extern void set_verbose (const char *, int, struct cmd_list_element *);
291 
292 extern const char *handle_line_of_input (std::string &cmd_line_buffer,
293 					 const char *rl, int repeat,
294 					 const char *annotation_suffix);
295 
296 /* Call at startup to see if the user has requested that gdb start up
297    quietly.  */
298 
299 extern bool check_quiet_mode ();
300 
301 #endif
302