d6051942a23f4566b19911a89dca1aa828587588
[binutils-gdb.git] / gdb / tui / tui-hooks.c
1 /* GDB hooks for TUI.
2
3 Copyright 2001, 2002 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 2 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, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
21
22 /* FIXME: cagney/2002-02-28: The GDB coding standard indicates that
23 "defs.h" should be included first. Unfortunatly some systems
24 (currently Debian GNU/Linux) include the <stdbool.h> via <curses.h>
25 and they clash with "bfd.h"'s definiton of true/false. The correct
26 fix is to remove true/false from "bfd.h", however, until that
27 happens, hack around it by including "config.h" and <curses.h>
28 first. */
29
30 #include "config.h"
31 #ifdef HAVE_NCURSES_H
32 #include <ncurses.h>
33 #else
34 #ifdef HAVE_CURSES_H
35 #include <curses.h>
36 #endif
37 #endif
38
39 #include "defs.h"
40 #include "symtab.h"
41 #include "inferior.h"
42 #include "command.h"
43 #include "bfd.h"
44 #include "symfile.h"
45 #include "objfiles.h"
46 #include "target.h"
47 #include "gdbcore.h"
48 #include "event-loop.h"
49 #include "frame.h"
50 #include "breakpoint.h"
51 #include "gdb-events.h"
52 #include <unistd.h>
53 #include <fcntl.h>
54
55 #include "tui.h"
56 #include "tuiData.h"
57 #include "tuiLayout.h"
58 #include "tuiIO.h"
59 #include "tuiRegs.h"
60 #include "tuiWin.h"
61 #include "tuiStack.h"
62 #include "tuiDataWin.h"
63 #include "tuiSourceWin.h"
64
65 int tui_target_has_run = 0;
66
67 static void (* tui_target_new_objfile_chain) (struct objfile*);
68 extern void (*selected_frame_level_changed_hook) (int);
69
70 static void
71 tui_new_objfile_hook (struct objfile* objfile)
72 {
73 if (tui_active)
74 tui_display_main ();
75
76 if (tui_target_new_objfile_chain)
77 tui_target_new_objfile_chain (objfile);
78 }
79
80 static int
81 tui_query_hook (const char * msg, va_list argp)
82 {
83 int retval;
84 int ans2;
85 int answer;
86
87 /* Automatically answer "yes" if input is not from a terminal. */
88 if (!input_from_terminal_p ())
89 return 1;
90
91 echo ();
92 while (1)
93 {
94 wrap_here (""); /* Flush any buffered output */
95 gdb_flush (gdb_stdout);
96
97 vfprintf_filtered (gdb_stdout, msg, argp);
98 printf_filtered ("(y or n) ");
99
100 wrap_here ("");
101 gdb_flush (gdb_stdout);
102
103 answer = tui_getc (stdin);
104 clearerr (stdin); /* in case of C-d */
105 if (answer == EOF) /* C-d */
106 {
107 retval = 1;
108 break;
109 }
110 /* Eat rest of input line, to EOF or newline */
111 if (answer != '\n')
112 do
113 {
114 ans2 = tui_getc (stdin);
115 clearerr (stdin);
116 }
117 while (ans2 != EOF && ans2 != '\n' && ans2 != '\r');
118
119 if (answer >= 'a')
120 answer -= 040;
121 if (answer == 'Y')
122 {
123 retval = 1;
124 break;
125 }
126 if (answer == 'N')
127 {
128 retval = 0;
129 break;
130 }
131 printf_filtered ("Please answer y or n.\n");
132 }
133 noecho ();
134 return retval;
135 }
136
137 /* Prevent recursion of registers_changed_hook(). */
138 static int tui_refreshing_registers = 0;
139
140 static void
141 tui_registers_changed_hook (void)
142 {
143 struct frame_info *fi;
144
145 fi = selected_frame;
146 if (fi && tui_refreshing_registers == 0)
147 {
148 tui_refreshing_registers = 1;
149 #if 0
150 tuiCheckDataValues (fi);
151 #endif
152 tui_refreshing_registers = 0;
153 }
154 }
155
156 static void
157 tui_register_changed_hook (int regno)
158 {
159 struct frame_info *fi;
160
161 fi = selected_frame;
162 if (fi && tui_refreshing_registers == 0)
163 {
164 tui_refreshing_registers = 1;
165 tuiCheckDataValues (fi);
166 tui_refreshing_registers = 0;
167 }
168 }
169
170 extern struct breakpoint *breakpoint_chain;
171
172 /* Find a breakpoint given its number. Returns null if not found. */
173 static struct breakpoint *
174 get_breakpoint (int number)
175 {
176 struct breakpoint *bp;
177
178 for (bp = breakpoint_chain; bp; bp = bp->next)
179 {
180 if (bp->number == number)
181 return bp;
182 }
183 return 0;
184 }
185
186 /* Breakpoint creation hook.
187 Update the screen to show the new breakpoint. */
188 static void
189 tui_event_create_breakpoint (int number)
190 {
191 struct breakpoint *bp;
192
193 bp = get_breakpoint (number);
194 if (bp)
195 {
196 switch (bp->type)
197 {
198 case bp_breakpoint:
199 case bp_hardware_breakpoint:
200 tuiAllSetHasBreakAt (bp, 1);
201 tuiUpdateAllExecInfos ();
202 break;
203
204 default:
205 break;
206 }
207 }
208 }
209
210 /* Breakpoint deletion hook.
211 Refresh the screen to update the breakpoint marks. */
212 static void
213 tui_event_delete_breakpoint (int number)
214 {
215 struct breakpoint *bp;
216 struct breakpoint *b;
217 int clearIt;
218
219 bp = get_breakpoint (number);
220 if (bp == 0)
221 return;
222
223 /* Before turning off the visuals for the bp, check to see that
224 there are no other bps at the same address. */
225 clearIt = 0;
226 for (b = breakpoint_chain; b; b = b->next)
227 {
228 clearIt = (b == bp || b->address != bp->address);
229 if (!clearIt)
230 break;
231 }
232
233 if (clearIt)
234 {
235 tuiAllSetHasBreakAt (bp, 0);
236 tuiUpdateAllExecInfos ();
237 }
238 }
239
240 static void
241 tui_event_modify_breakpoint (int number)
242 {
243 ;
244 }
245
246 static void
247 tui_event_default (int number)
248 {
249 ;
250 }
251
252 static struct gdb_events *tui_old_event_hooks;
253
254 static struct gdb_events tui_event_hooks =
255 {
256 tui_event_create_breakpoint,
257 tui_event_delete_breakpoint,
258 tui_event_modify_breakpoint,
259 tui_event_default,
260 tui_event_default,
261 tui_event_default
262 };
263
264 /* Called when going to wait for the target.
265 Leave curses mode and setup program mode. */
266 static ptid_t
267 tui_target_wait_hook (ptid_t pid, struct target_waitstatus *status)
268 {
269 ptid_t res;
270
271 /* Leave tui mode (optional). */
272 #if 0
273 if (tui_active)
274 {
275 target_terminal_ours ();
276 endwin ();
277 target_terminal_inferior ();
278 }
279 #endif
280 tui_target_has_run = 1;
281 res = target_wait (pid, status);
282
283 if (tui_active)
284 {
285 /* TODO: need to refresh (optional). */
286 }
287 return res;
288 }
289
290 /* The selected frame has changed. This is happens after a target
291 stop or when the user explicitly changes the frame (up/down/thread/...). */
292 static void
293 tui_selected_frame_level_changed_hook (int level)
294 {
295 struct frame_info *fi;
296
297 fi = selected_frame;
298 /* Ensure that symbols for this frame are read in. Also, determine the
299 source language of this frame, and switch to it if desired. */
300 if (fi)
301 {
302 struct symtab *s;
303
304 s = find_pc_symtab (fi->pc);
305 /* elz: this if here fixes the problem with the pc not being displayed
306 in the tui asm layout, with no debug symbols. The value of s
307 would be 0 here, and select_source_symtab would abort the
308 command by calling the 'error' function */
309 if (s)
310 select_source_symtab (s);
311
312 /* Display the frame position (even if there is no symbols). */
313 tuiShowFrameInfo (fi);
314
315 /* Refresh the register window if it's visible. */
316 if (tui_is_window_visible (DATA_WIN))
317 {
318 tui_refreshing_registers = 1;
319 tuiCheckDataValues (fi);
320 tui_refreshing_registers = 0;
321 }
322 }
323 }
324
325 /* Called from print_frame_info to list the line we stopped in. */
326 static void
327 tui_print_frame_info_listing_hook (struct symtab *s, int line,
328 int stopline, int noerror)
329 {
330 select_source_symtab (s);
331 tuiShowFrameInfo (selected_frame);
332 }
333
334 /* Install the TUI specific hooks. */
335 void
336 tui_install_hooks (void)
337 {
338 target_wait_hook = tui_target_wait_hook;
339 selected_frame_level_changed_hook = tui_selected_frame_level_changed_hook;
340 print_frame_info_listing_hook = tui_print_frame_info_listing_hook;
341
342 query_hook = tui_query_hook;
343
344 /* Install the event hooks. */
345 tui_old_event_hooks = set_gdb_event_hooks (&tui_event_hooks);
346
347 registers_changed_hook = tui_registers_changed_hook;
348 register_changed_hook = tui_register_changed_hook;
349 }
350
351 /* Remove the TUI specific hooks. */
352 void
353 tui_remove_hooks (void)
354 {
355 target_wait_hook = 0;
356 selected_frame_level_changed_hook = 0;
357 print_frame_info_listing_hook = 0;
358 query_hook = 0;
359 registers_changed_hook = 0;
360 register_changed_hook = 0;
361
362 /* Restore the previous event hooks. */
363 set_gdb_event_hooks (tui_old_event_hooks);
364 }
365
366 /* Cleanup the tui before exiting. */
367 static void
368 tui_exit (void)
369 {
370 /* Disable the tui. Curses mode is left leaving the screen
371 in a clean state (see endwin()). */
372 tui_disable ();
373 }
374
375 /* Initialize the tui by installing several gdb hooks, initializing
376 the tui IO and preparing the readline with the kind binding. */
377 static void
378 tui_init_hook (char *argv0)
379 {
380 /* Install exit handler to leave the screen in a good shape. */
381 atexit (tui_exit);
382
383 initializeStaticData ();
384
385 /* Install the permanent hooks. */
386 tui_target_new_objfile_chain = target_new_objfile_hook;
387 target_new_objfile_hook = tui_new_objfile_hook;
388
389 tui_initialize_io ();
390 tui_initialize_readline ();
391
392 /* Decide in which mode to start using GDB (based on -tui). */
393 if (tui_version)
394 {
395 tui_enable ();
396 }
397 }
398
399 /* Initialize the tui. */
400 void
401 _initialize_tui (void)
402 {
403 /* Setup initialization hook. */
404 init_ui_hook = tui_init_hook;
405 }
406