53d951b8d637b2e940d3e47fb4affde249f236fa
[binutils-gdb.git] / gdb / interps.h
1 /* Manages interpreters for GDB, the GNU debugger.
2
3 Copyright (C) 2000-2023 Free Software Foundation, Inc.
4
5 Written by Jim Ingham <jingham@apple.com> of Apple Computer, Inc.
6
7 This file is part of GDB.
8
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
21
22 #ifndef INTERPS_H
23 #define INTERPS_H
24
25 #include "gdbsupport/intrusive_list.h"
26
27 struct bpstat;
28 struct ui_out;
29 struct interp;
30 struct ui;
31 class completion_tracker;
32
33 typedef struct interp *(*interp_factory_func) (const char *name);
34
35 /* Each interpreter kind (CLI, MI, etc.) registers itself with a call
36 to this function, passing along its name, and a pointer to a
37 function that creates a new instance of an interpreter with that
38 name.
39
40 The memory for NAME must have static storage duration. */
41 extern void interp_factory_register (const char *name,
42 interp_factory_func func);
43
44 extern void interp_exec (struct interp *interp, const char *command);
45
46 class interp : public intrusive_list_node<interp>
47 {
48 public:
49 explicit interp (const char *name);
50 virtual ~interp () = 0;
51
52 virtual void init (bool top_level)
53 {}
54
55 virtual void resume () = 0;
56 virtual void suspend () = 0;
57
58 virtual void exec (const char *command) = 0;
59
60 /* Returns the ui_out currently used to collect results for this
61 interpreter. It can be a formatter for stdout, as is the case
62 for the console & mi outputs, or it might be a result
63 formatter. */
64 virtual ui_out *interp_ui_out () = 0;
65
66 /* Provides a hook for interpreters to do any additional
67 setup/cleanup that they might need when logging is enabled or
68 disabled. */
69 virtual void set_logging (ui_file_up logfile, bool logging_redirect,
70 bool debug_redirect) = 0;
71
72 /* Called before starting an event loop, to give the interpreter a
73 chance to e.g., print a prompt. */
74 virtual void pre_command_loop ()
75 {}
76
77 /* Returns true if this interpreter supports using the readline
78 library; false if it uses GDB's own simplified readline
79 emulation. */
80 virtual bool supports_command_editing ()
81 { return false; }
82
83 const char *name () const
84 { return m_name; }
85
86 /* Notify the interpreter that the current inferior has stopped with signal
87 SIG. */
88 virtual void on_signal_received (gdb_signal sig) {}
89
90 /* Notify the interpreter that the current inferior has exited with signal
91 SIG. */
92 virtual void on_signal_exited (gdb_signal sig) {}
93
94 /* Notify the interpreter that the current inferior has stopped normally. */
95 virtual void on_normal_stop (bpstat *bs, int print_frame) {}
96
97 /* Notify the intepreter that the current inferior has exited normally with
98 status STATUS. */
99 virtual void on_exited (int status) {}
100
101 /* Notify the interpreter that the current inferior has stopped reverse
102 execution because there is no more history. */
103 virtual void on_no_history () {}
104
105 /* Notify the interpreter that a synchronous command it started has
106 finished. */
107 virtual void on_sync_execution_done () {}
108
109 /* Notify the interpreter that an error was caught while executing a
110 command on this interpreter. */
111 virtual void on_command_error () {}
112
113 private:
114 /* The memory for this is static, it comes from literal strings (e.g. "cli"). */
115 const char *m_name;
116
117 public:
118 /* Has the init method been run? */
119 bool inited = false;
120 };
121
122 /* Look up the interpreter for NAME, creating one if none exists yet.
123 If NAME is not a interpreter type previously registered with
124 interp_factory_register, return NULL; otherwise return a pointer to
125 the interpreter. */
126 extern struct interp *interp_lookup (struct ui *ui, const char *name);
127
128 /* Set the current UI's top level interpreter to the interpreter named
129 NAME. Throws an error if NAME is not a known interpreter or the
130 interpreter fails to initialize. */
131 extern void set_top_level_interpreter (const char *name);
132
133 /* Temporarily set the current interpreter, and reset it on
134 destruction. */
135 class scoped_restore_interp
136 {
137 public:
138
139 scoped_restore_interp (const char *name)
140 : m_interp (set_interp (name))
141 {
142 }
143
144 ~scoped_restore_interp ()
145 {
146 set_interp (m_interp->name ());
147 }
148
149 scoped_restore_interp (const scoped_restore_interp &) = delete;
150 scoped_restore_interp &operator= (const scoped_restore_interp &) = delete;
151
152 private:
153
154 struct interp *set_interp (const char *name);
155
156 struct interp *m_interp;
157 };
158
159 extern int current_interp_named_p (const char *name);
160
161 /* Call this function to give the current interpreter an opportunity
162 to do any special handling of streams when logging is enabled or
163 disabled. LOGFILE is the stream for the log file when logging is
164 starting and is NULL when logging is ending. LOGGING_REDIRECT is
165 the value of the "set logging redirect" setting. If true, the
166 interpreter should configure the output streams to send output only
167 to the logfile. If false, the interpreter should configure the
168 output streams to send output to both the current output stream
169 (i.e., the terminal) and the log file. DEBUG_REDIRECT is same as
170 LOGGING_REDIRECT, but for the value of "set logging debugredirect"
171 instead. */
172 extern void current_interp_set_logging (ui_file_up logfile,
173 bool logging_redirect,
174 bool debug_redirect);
175
176 /* Returns the top-level interpreter. */
177 extern struct interp *top_level_interpreter (void);
178
179 /* Return the current UI's current interpreter. */
180 extern struct interp *current_interpreter (void);
181
182 extern struct interp *command_interp (void);
183
184 extern void clear_interpreter_hooks (void);
185
186 /* Returns true if INTERP supports using the readline library; false
187 if it uses GDB's own simplified form of readline. */
188 extern int interp_supports_command_editing (struct interp *interp);
189
190 /* Called before starting an event loop, to give the interpreter a
191 chance to e.g., print a prompt. */
192 extern void interp_pre_command_loop (struct interp *interp);
193
194 /* List the possible interpreters which could complete the given
195 text. */
196 extern void interpreter_completer (struct cmd_list_element *ignore,
197 completion_tracker &tracker,
198 const char *text,
199 const char *word);
200
201 /* Notify all interpreters that the current inferior has stopped with signal
202 SIG. */
203 extern void interps_notify_signal_received (gdb_signal sig);
204
205 /* Notify all interpreters that the current inferior has exited with signal
206 SIG. */
207 extern void interps_notify_signal_exited (gdb_signal sig);
208
209 /* Notify all interpreters that the current inferior has stopped normally. */
210 extern void interps_notify_normal_stop (bpstat *bs, int print_frame);
211
212 /* Notify all interpreters that the current inferior has stopped reverse
213 execution because there is no more history. */
214 extern void interps_notify_no_history ();
215
216 /* Notify all interpreters that the current inferior has exited normally with
217 status STATUS. */
218 extern void interps_notify_exited (int status);
219
220 /* well-known interpreters */
221 #define INTERP_CONSOLE "console"
222 #define INTERP_MI2 "mi2"
223 #define INTERP_MI3 "mi3"
224 #define INTERP_MI4 "mi4"
225 #define INTERP_MI "mi"
226 #define INTERP_TUI "tui"
227 #define INTERP_INSIGHT "insight"
228
229 #endif