35a9259447c8627d83904e3c3bef4b051f54ac02
[deliverable/binutils-gdb.git] / gdb / tui / tui-hooks.c
1 /* GDB hooks for TUI.
2
3 Copyright (C) 2001-2019 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 #include "defs.h"
21 #include "symtab.h"
22 #include "inferior.h"
23 #include "command.h"
24 #include "bfd.h"
25 #include "symfile.h"
26 #include "objfiles.h"
27 #include "target.h"
28 #include "gdbcore.h"
29 #include "event-loop.h"
30 #include "event-top.h"
31 #include "frame.h"
32 #include "breakpoint.h"
33 #include "ui-out.h"
34 #include "top.h"
35 #include "observable.h"
36 #include "source.h"
37 #include <unistd.h>
38 #include <fcntl.h>
39
40 #include "tui/tui.h"
41 #include "tui/tui-hooks.h"
42 #include "tui/tui-data.h"
43 #include "tui/tui-layout.h"
44 #include "tui/tui-io.h"
45 #include "tui/tui-regs.h"
46 #include "tui/tui-win.h"
47 #include "tui/tui-stack.h"
48 #include "tui/tui-winsource.h"
49
50 #include "gdb_curses.h"
51
52 static void
53 tui_new_objfile_hook (struct objfile* objfile)
54 {
55 if (tui_active)
56 tui_display_main ();
57 }
58
59 /* Prevent recursion of deprecated_register_changed_hook(). */
60 static int tui_refreshing_registers = 0;
61
62 /* Observer for the register_changed notification. */
63
64 static void
65 tui_register_changed (struct frame_info *frame, int regno)
66 {
67 struct frame_info *fi;
68
69 if (!tui_is_window_visible (DATA_WIN))
70 return;
71
72 /* The frame of the register that was changed may differ from the selected
73 frame, but we only want to show the register values of the selected frame.
74 And even if the frames differ a register change made in one can still show
75 up in the other. So we always use the selected frame here, and ignore
76 FRAME. */
77 fi = get_selected_frame (NULL);
78 if (tui_refreshing_registers == 0)
79 {
80 tui_refreshing_registers = 1;
81 TUI_DATA_WIN->check_register_values (fi);
82 tui_refreshing_registers = 0;
83 }
84 }
85
86 /* Breakpoint creation hook.
87 Update the screen to show the new breakpoint. */
88 static void
89 tui_event_create_breakpoint (struct breakpoint *b)
90 {
91 tui_update_all_breakpoint_info (nullptr);
92 }
93
94 /* Breakpoint deletion hook.
95 Refresh the screen to update the breakpoint marks. */
96 static void
97 tui_event_delete_breakpoint (struct breakpoint *b)
98 {
99 tui_update_all_breakpoint_info (b);
100 }
101
102 static void
103 tui_event_modify_breakpoint (struct breakpoint *b)
104 {
105 tui_update_all_breakpoint_info (nullptr);
106 }
107
108 /* Refresh TUI's frame and register information. This is a hook intended to be
109 used to update the screen after potential frame and register changes.
110
111 REGISTERS_TOO_P controls whether to refresh our register information even
112 if frame information hasn't changed. */
113
114 static void
115 tui_refresh_frame_and_register_information (bool registers_too_p)
116 {
117 struct frame_info *fi;
118 CORE_ADDR pc;
119 int frame_info_changed_p;
120
121 if (!has_stack_frames ())
122 return;
123
124 target_terminal::scoped_restore_terminal_state term_state;
125 target_terminal::ours_for_output ();
126
127 fi = get_selected_frame (NULL);
128 /* Ensure that symbols for this frame are read in. Also, determine
129 the source language of this frame, and switch to it if
130 desired. */
131 if (get_frame_pc_if_available (fi, &pc))
132 {
133 struct symtab *s;
134
135 s = find_pc_line_symtab (pc);
136 /* elz: This if here fixes the problem with the pc not being
137 displayed in the tui asm layout, with no debug symbols. The
138 value of s would be 0 here, and select_source_symtab would
139 abort the command by calling the 'error' function. */
140 if (s)
141 select_source_symtab (s);
142 }
143
144 /* Display the frame position (even if there is no symbols or the PC
145 is not known). */
146 frame_info_changed_p = tui_show_frame_info (fi);
147
148 /* Refresh the register window if it's visible. */
149 if (tui_is_window_visible (DATA_WIN)
150 && (frame_info_changed_p || registers_too_p))
151 {
152 tui_refreshing_registers = 1;
153 TUI_DATA_WIN->check_register_values (fi);
154 tui_refreshing_registers = 0;
155 }
156 }
157
158 /* Dummy callback for deprecated_print_frame_info_listing_hook which is called
159 from print_frame_info. */
160
161 static void
162 tui_dummy_print_frame_info_listing_hook (struct symtab *s,
163 int line,
164 int stopline,
165 int noerror)
166 {
167 }
168
169 /* Perform all necessary cleanups regarding our module's inferior data
170 that is required after the inferior INF just exited. */
171
172 static void
173 tui_inferior_exit (struct inferior *inf)
174 {
175 /* Leave the SingleKey mode to make sure the gdb prompt is visible. */
176 tui_set_key_mode (TUI_COMMAND_MODE);
177 tui_show_frame_info (0);
178 tui_display_main ();
179 }
180
181 /* Observer for the before_prompt notification. */
182
183 static void
184 tui_before_prompt (const char *current_gdb_prompt)
185 {
186 /* This refresh is intended to catch changes to the selected frame following
187 a call to "up", "down" or "frame". As such we don't necessarily want to
188 refresh registers here unless the frame actually changed by one of these
189 commands. Registers will otherwise be refreshed after a normal stop or by
190 our tui_register_changed_hook. */
191 tui_refresh_frame_and_register_information (/*registers_too_p=*/false);
192 }
193
194 /* Observer for the normal_stop notification. */
195
196 static void
197 tui_normal_stop (struct bpstats *bs, int print_frame)
198 {
199 /* This refresh is intended to catch changes to the selected frame and to
200 registers following a normal stop. */
201 tui_refresh_frame_and_register_information (/*registers_too_p=*/true);
202 }
203
204 /* Token associated with observers registered while TUI hooks are
205 installed. */
206 static const gdb::observers::token tui_observers_token {};
207
208 /* Attach or detach a single observer, according to ATTACH. */
209
210 template<typename T>
211 static void
212 attach_or_detach (T &observable, typename T::func_type func, bool attach)
213 {
214 if (attach)
215 observable.attach (func, tui_observers_token);
216 else
217 observable.detach (tui_observers_token);
218 }
219
220 /* Attach or detach TUI observers, according to ATTACH. */
221
222 static void
223 tui_attach_detach_observers (bool attach)
224 {
225 attach_or_detach (gdb::observers::breakpoint_created,
226 tui_event_create_breakpoint, attach);
227 attach_or_detach (gdb::observers::breakpoint_deleted,
228 tui_event_delete_breakpoint, attach);
229 attach_or_detach (gdb::observers::breakpoint_modified,
230 tui_event_modify_breakpoint, attach);
231 attach_or_detach (gdb::observers::inferior_exit,
232 tui_inferior_exit, attach);
233 attach_or_detach (gdb::observers::before_prompt,
234 tui_before_prompt, attach);
235 attach_or_detach (gdb::observers::normal_stop,
236 tui_normal_stop, attach);
237 attach_or_detach (gdb::observers::register_changed,
238 tui_register_changed, attach);
239 }
240
241 /* Install the TUI specific hooks. */
242 void
243 tui_install_hooks (void)
244 {
245 /* If this hook is not set to something then print_frame_info will
246 assume that the CLI, not the TUI, is active, and will print the frame info
247 for us in such a way that we are not prepared to handle. This hook is
248 otherwise effectively obsolete. */
249 deprecated_print_frame_info_listing_hook
250 = tui_dummy_print_frame_info_listing_hook;
251
252 /* Install the event hooks. */
253 tui_attach_detach_observers (true);
254 }
255
256 /* Remove the TUI specific hooks. */
257 void
258 tui_remove_hooks (void)
259 {
260 deprecated_print_frame_info_listing_hook = 0;
261
262 /* Remove our observers. */
263 tui_attach_detach_observers (false);
264 }
265
266 void
267 _initialize_tui_hooks (void)
268 {
269 /* Install the permanent hooks. */
270 gdb::observers::new_objfile.attach (tui_new_objfile_hook);
271 }
This page took 0.035728 seconds and 3 git commands to generate.