]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/tui/tui-hooks.c
Copyright year update in most files of the GDB Project.
[thirdparty/binutils-gdb.git] / gdb / tui / tui-hooks.c
1 /* GDB hooks for TUI.
2
3 Copyright (C) 2001-2012 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 "observer.h"
36 #include <unistd.h>
37 #include <fcntl.h>
38
39 #include "tui/tui.h"
40 #include "tui/tui-hooks.h"
41 #include "tui/tui-data.h"
42 #include "tui/tui-layout.h"
43 #include "tui/tui-io.h"
44 #include "tui/tui-regs.h"
45 #include "tui/tui-win.h"
46 #include "tui/tui-stack.h"
47 #include "tui/tui-windata.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 int tui_target_has_run = 0;
58
59 static void
60 tui_new_objfile_hook (struct objfile* objfile)
61 {
62 if (tui_active)
63 tui_display_main ();
64 }
65
66 static int ATTRIBUTE_PRINTF (1, 0)
67 tui_query_hook (const char *msg, va_list argp)
68 {
69 int retval;
70 int ans2;
71 int answer;
72
73 echo ();
74 while (1)
75 {
76 wrap_here (""); /* Flush any buffered output. */
77 gdb_flush (gdb_stdout);
78
79 vfprintf_filtered (gdb_stdout, msg, argp);
80 printf_filtered (_("(y or n) "));
81
82 wrap_here ("");
83 gdb_flush (gdb_stdout);
84
85 answer = tui_getc (stdin);
86 clearerr (stdin); /* in case of C-d */
87 if (answer == EOF) /* C-d */
88 {
89 retval = 1;
90 break;
91 }
92 /* Eat rest of input line, to EOF or newline. */
93 if (answer != '\n')
94 do
95 {
96 ans2 = tui_getc (stdin);
97 clearerr (stdin);
98 }
99 while (ans2 != EOF && ans2 != '\n' && ans2 != '\r');
100
101 if (answer >= 'a')
102 answer -= 040;
103 if (answer == 'Y')
104 {
105 retval = 1;
106 break;
107 }
108 if (answer == 'N')
109 {
110 retval = 0;
111 break;
112 }
113 printf_filtered (_("Please answer y or n.\n"));
114 }
115 noecho ();
116 return retval;
117 }
118
119 /* Prevent recursion of deprecated_register_changed_hook(). */
120 static int tui_refreshing_registers = 0;
121
122 static void
123 tui_register_changed_hook (int regno)
124 {
125 struct frame_info *fi;
126
127 fi = get_selected_frame (NULL);
128 if (tui_refreshing_registers == 0)
129 {
130 tui_refreshing_registers = 1;
131 tui_check_data_values (fi);
132 tui_refreshing_registers = 0;
133 }
134 }
135
136 /* Breakpoint creation hook.
137 Update the screen to show the new breakpoint. */
138 static void
139 tui_event_create_breakpoint (struct breakpoint *b)
140 {
141 tui_update_all_breakpoint_info ();
142 }
143
144 /* Breakpoint deletion hook.
145 Refresh the screen to update the breakpoint marks. */
146 static void
147 tui_event_delete_breakpoint (struct breakpoint *b)
148 {
149 tui_update_all_breakpoint_info ();
150 }
151
152 static void
153 tui_event_modify_breakpoint (struct breakpoint *b)
154 {
155 tui_update_all_breakpoint_info ();
156 }
157
158 /* Called when going to wait for the target.
159 Leave curses mode and setup program mode. */
160 static ptid_t
161 tui_target_wait_hook (ptid_t pid,
162 struct target_waitstatus *status, int options)
163 {
164 ptid_t res;
165
166 /* Leave tui mode (optional). */
167 #if 0
168 if (tui_active)
169 {
170 target_terminal_ours ();
171 endwin ();
172 target_terminal_inferior ();
173 }
174 #endif
175 tui_target_has_run = 1;
176 res = target_wait (pid, status, options);
177
178 if (tui_active)
179 {
180 /* TODO: need to refresh (optional). */
181 }
182 return res;
183 }
184
185 /* The selected frame has changed. This is happens after a target
186 stop or when the user explicitly changes the frame
187 (up/down/thread/...). */
188 static void
189 tui_selected_frame_level_changed_hook (int level)
190 {
191 struct frame_info *fi;
192 CORE_ADDR pc;
193
194 /* Negative level means that the selected frame was cleared. */
195 if (level < 0)
196 return;
197
198 fi = get_selected_frame (NULL);
199 /* Ensure that symbols for this frame are read in. Also, determine
200 the source language of this frame, and switch to it if
201 desired. */
202 if (get_frame_pc_if_available (fi, &pc))
203 {
204 struct symtab *s;
205
206 s = find_pc_symtab (pc);
207 /* elz: This if here fixes the problem with the pc not being
208 displayed in the tui asm layout, with no debug symbols. The
209 value of s would be 0 here, and select_source_symtab would
210 abort the command by calling the 'error' function. */
211 if (s)
212 select_source_symtab (s);
213 }
214
215 /* Display the frame position (even if there is no symbols or the PC
216 is not known). */
217 tui_show_frame_info (fi);
218
219 /* Refresh the register window if it's visible. */
220 if (tui_is_window_visible (DATA_WIN))
221 {
222 tui_refreshing_registers = 1;
223 tui_check_data_values (fi);
224 tui_refreshing_registers = 0;
225 }
226 }
227
228 /* Called from print_frame_info to list the line we stopped in. */
229 static void
230 tui_print_frame_info_listing_hook (struct symtab *s,
231 int line,
232 int stopline,
233 int noerror)
234 {
235 select_source_symtab (s);
236 tui_show_frame_info (get_selected_frame (NULL));
237 }
238
239 /* Called when the target process died or is detached.
240 Update the status line. */
241 static void
242 tui_detach_hook (void)
243 {
244 tui_show_frame_info (0);
245 tui_display_main ();
246 }
247
248 /* Observers created when installing TUI hooks. */
249 static struct observer *tui_bp_created_observer;
250 static struct observer *tui_bp_deleted_observer;
251 static struct observer *tui_bp_modified_observer;
252
253 /* Install the TUI specific hooks. */
254 void
255 tui_install_hooks (void)
256 {
257 deprecated_target_wait_hook = tui_target_wait_hook;
258 deprecated_selected_frame_level_changed_hook
259 = tui_selected_frame_level_changed_hook;
260 deprecated_print_frame_info_listing_hook
261 = tui_print_frame_info_listing_hook;
262
263 deprecated_query_hook = tui_query_hook;
264
265 /* Install the event hooks. */
266 tui_bp_created_observer
267 = observer_attach_breakpoint_created (tui_event_create_breakpoint);
268 tui_bp_deleted_observer
269 = observer_attach_breakpoint_deleted (tui_event_delete_breakpoint);
270 tui_bp_modified_observer
271 = observer_attach_breakpoint_modified (tui_event_modify_breakpoint);
272
273 deprecated_register_changed_hook = tui_register_changed_hook;
274 deprecated_detach_hook = tui_detach_hook;
275 }
276
277 /* Remove the TUI specific hooks. */
278 void
279 tui_remove_hooks (void)
280 {
281 deprecated_target_wait_hook = 0;
282 deprecated_selected_frame_level_changed_hook = 0;
283 deprecated_print_frame_info_listing_hook = 0;
284 deprecated_query_hook = 0;
285 deprecated_register_changed_hook = 0;
286 deprecated_detach_hook = 0;
287
288 /* Remove our observers. */
289 observer_detach_breakpoint_created (tui_bp_created_observer);
290 tui_bp_created_observer = NULL;
291 observer_detach_breakpoint_deleted (tui_bp_deleted_observer);
292 tui_bp_deleted_observer = NULL;
293 observer_detach_breakpoint_modified (tui_bp_modified_observer);
294 tui_bp_modified_observer = NULL;
295 }
296
297 void _initialize_tui_hooks (void);
298
299 void
300 _initialize_tui_hooks (void)
301 {
302 /* Install the permanent hooks. */
303 observer_attach_new_objfile (tui_new_objfile_hook);
304 }