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