]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/tui/tui-hooks.c
2002-10-02 Elena Zannoni <ezannoni@redhat.com>
[thirdparty/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 "event-top.h"
50 #include "frame.h"
51 #include "breakpoint.h"
52 #include "gdb-events.h"
53 #include "ui-out.h"
54 #include "top.h"
55 #include <readline/readline.h>
56 #include <unistd.h>
57 #include <fcntl.h>
58
59 #include "tui.h"
60 #include "tuiData.h"
61 #include "tuiLayout.h"
62 #include "tuiIO.h"
63 #include "tuiRegs.h"
64 #include "tuiWin.h"
65 #include "tuiStack.h"
66 #include "tuiDataWin.h"
67 #include "tuiSourceWin.h"
68
69 int tui_target_has_run = 0;
70
71 static void (* tui_target_new_objfile_chain) (struct objfile*);
72 static void tui_event_loop (void);
73 static void tui_command_loop (void);
74
75 static void
76 tui_new_objfile_hook (struct objfile* objfile)
77 {
78 if (tui_active)
79 tui_display_main ();
80
81 if (tui_target_new_objfile_chain)
82 tui_target_new_objfile_chain (objfile);
83 }
84
85 static int
86 tui_query_hook (const char * msg, va_list argp)
87 {
88 int retval;
89 int ans2;
90 int answer;
91
92 /* Automatically answer "yes" if input is not from a terminal. */
93 if (!input_from_terminal_p ())
94 return 1;
95
96 echo ();
97 while (1)
98 {
99 wrap_here (""); /* Flush any buffered output */
100 gdb_flush (gdb_stdout);
101
102 vfprintf_filtered (gdb_stdout, msg, argp);
103 printf_filtered ("(y or n) ");
104
105 wrap_here ("");
106 gdb_flush (gdb_stdout);
107
108 answer = tui_getc (stdin);
109 clearerr (stdin); /* in case of C-d */
110 if (answer == EOF) /* C-d */
111 {
112 retval = 1;
113 break;
114 }
115 /* Eat rest of input line, to EOF or newline */
116 if (answer != '\n')
117 do
118 {
119 ans2 = tui_getc (stdin);
120 clearerr (stdin);
121 }
122 while (ans2 != EOF && ans2 != '\n' && ans2 != '\r');
123
124 if (answer >= 'a')
125 answer -= 040;
126 if (answer == 'Y')
127 {
128 retval = 1;
129 break;
130 }
131 if (answer == 'N')
132 {
133 retval = 0;
134 break;
135 }
136 printf_filtered ("Please answer y or n.\n");
137 }
138 noecho ();
139 return retval;
140 }
141
142 /* Prevent recursion of registers_changed_hook(). */
143 static int tui_refreshing_registers = 0;
144
145 static void
146 tui_registers_changed_hook (void)
147 {
148 struct frame_info *fi;
149
150 fi = selected_frame;
151 if (fi && tui_refreshing_registers == 0)
152 {
153 tui_refreshing_registers = 1;
154 #if 0
155 tuiCheckDataValues (fi);
156 #endif
157 tui_refreshing_registers = 0;
158 }
159 }
160
161 static void
162 tui_register_changed_hook (int regno)
163 {
164 struct frame_info *fi;
165
166 fi = selected_frame;
167 if (fi && tui_refreshing_registers == 0)
168 {
169 tui_refreshing_registers = 1;
170 tuiCheckDataValues (fi);
171 tui_refreshing_registers = 0;
172 }
173 }
174
175 /* Breakpoint creation hook.
176 Update the screen to show the new breakpoint. */
177 static void
178 tui_event_create_breakpoint (int number)
179 {
180 tui_update_all_breakpoint_info ();
181 }
182
183 /* Breakpoint deletion hook.
184 Refresh the screen to update the breakpoint marks. */
185 static void
186 tui_event_delete_breakpoint (int number)
187 {
188 tui_update_all_breakpoint_info ();
189 }
190
191 static void
192 tui_event_modify_breakpoint (int number)
193 {
194 tui_update_all_breakpoint_info ();
195 }
196
197 static void
198 tui_event_default (int number)
199 {
200 ;
201 }
202
203 static struct gdb_events *tui_old_event_hooks;
204
205 static struct gdb_events tui_event_hooks =
206 {
207 tui_event_create_breakpoint,
208 tui_event_delete_breakpoint,
209 tui_event_modify_breakpoint,
210 tui_event_default,
211 tui_event_default,
212 tui_event_default
213 };
214
215 /* Called when going to wait for the target.
216 Leave curses mode and setup program mode. */
217 static ptid_t
218 tui_target_wait_hook (ptid_t pid, struct target_waitstatus *status)
219 {
220 ptid_t res;
221
222 /* Leave tui mode (optional). */
223 #if 0
224 if (tui_active)
225 {
226 target_terminal_ours ();
227 endwin ();
228 target_terminal_inferior ();
229 }
230 #endif
231 tui_target_has_run = 1;
232 res = target_wait (pid, status);
233
234 if (tui_active)
235 {
236 /* TODO: need to refresh (optional). */
237 }
238 return res;
239 }
240
241 /* The selected frame has changed. This is happens after a target
242 stop or when the user explicitly changes the frame (up/down/thread/...). */
243 static void
244 tui_selected_frame_level_changed_hook (int level)
245 {
246 struct frame_info *fi;
247
248 fi = selected_frame;
249 /* Ensure that symbols for this frame are read in. Also, determine the
250 source language of this frame, and switch to it if desired. */
251 if (fi)
252 {
253 struct symtab *s;
254
255 s = find_pc_symtab (fi->pc);
256 /* elz: this if here fixes the problem with the pc not being displayed
257 in the tui asm layout, with no debug symbols. The value of s
258 would be 0 here, and select_source_symtab would abort the
259 command by calling the 'error' function */
260 if (s)
261 select_source_symtab (s);
262
263 /* Display the frame position (even if there is no symbols). */
264 tuiShowFrameInfo (fi);
265
266 /* Refresh the register window if it's visible. */
267 if (tui_is_window_visible (DATA_WIN))
268 {
269 tui_refreshing_registers = 1;
270 tuiCheckDataValues (fi);
271 tui_refreshing_registers = 0;
272 }
273 }
274 }
275
276 /* Called from print_frame_info to list the line we stopped in. */
277 static void
278 tui_print_frame_info_listing_hook (struct symtab *s, int line,
279 int stopline, int noerror)
280 {
281 select_source_symtab (s);
282 tuiShowFrameInfo (selected_frame);
283 }
284
285 /* Called when the target process died or is detached.
286 Update the status line. */
287 static void
288 tui_detach_hook (void)
289 {
290 tuiShowFrameInfo (0);
291 tui_display_main ();
292 }
293
294 /* Install the TUI specific hooks. */
295 void
296 tui_install_hooks (void)
297 {
298 target_wait_hook = tui_target_wait_hook;
299 selected_frame_level_changed_hook = tui_selected_frame_level_changed_hook;
300 print_frame_info_listing_hook = tui_print_frame_info_listing_hook;
301
302 query_hook = tui_query_hook;
303
304 /* Install the event hooks. */
305 tui_old_event_hooks = set_gdb_event_hooks (&tui_event_hooks);
306
307 registers_changed_hook = tui_registers_changed_hook;
308 register_changed_hook = tui_register_changed_hook;
309 detach_hook = tui_detach_hook;
310 }
311
312 /* Remove the TUI specific hooks. */
313 void
314 tui_remove_hooks (void)
315 {
316 target_wait_hook = 0;
317 selected_frame_level_changed_hook = 0;
318 print_frame_info_listing_hook = 0;
319 query_hook = 0;
320 registers_changed_hook = 0;
321 register_changed_hook = 0;
322 detach_hook = 0;
323
324 /* Restore the previous event hooks. */
325 set_gdb_event_hooks (tui_old_event_hooks);
326 }
327
328 /* Cleanup the tui before exiting. */
329 static void
330 tui_exit (void)
331 {
332 /* Disable the tui. Curses mode is left leaving the screen
333 in a clean state (see endwin()). */
334 tui_disable ();
335 }
336
337 /* Initialize all the necessary variables, start the event loop,
338 register readline, and stdin, start the loop. */
339 static void
340 tui_command_loop (void)
341 {
342 int length;
343 char *a_prompt;
344 char *gdb_prompt = get_prompt ();
345
346 /* If we are using readline, set things up and display the first
347 prompt, otherwise just print the prompt. */
348 if (async_command_editing_p)
349 {
350 /* Tell readline what the prompt to display is and what function it
351 will need to call after a whole line is read. This also displays
352 the first prompt. */
353 length = strlen (PREFIX (0)) + strlen (gdb_prompt) + strlen (SUFFIX (0)) + 1;
354 a_prompt = (char *) xmalloc (length);
355 strcpy (a_prompt, PREFIX (0));
356 strcat (a_prompt, gdb_prompt);
357 strcat (a_prompt, SUFFIX (0));
358 rl_callback_handler_install (a_prompt, input_handler);
359 }
360 else
361 display_gdb_prompt (0);
362
363 /* Now it's time to start the event loop. */
364 tui_event_loop ();
365 }
366
367 /* Start up the event loop. This is the entry point to the event loop
368 from the command loop. */
369
370 static void
371 tui_event_loop (void)
372 {
373 /* Loop until there is nothing to do. This is the entry point to the
374 event loop engine. gdb_do_one_event, called via catch_errors()
375 will process one event for each invocation. It blocks waits for
376 an event and then processes it. >0 when an event is processed, 0
377 when catch_errors() caught an error and <0 when there are no
378 longer any event sources registered. */
379 while (1)
380 {
381 int result = catch_errors (gdb_do_one_event, 0, "", RETURN_MASK_ALL);
382 if (result < 0)
383 break;
384
385 /* Update gdb output according to TUI mode. Since catch_errors
386 preserves the uiout from changing, this must be done at top
387 level of event loop. */
388 if (tui_active)
389 uiout = tui_out;
390 else
391 uiout = tui_old_uiout;
392
393 if (result == 0)
394 {
395 /* FIXME: this should really be a call to a hook that is
396 interface specific, because interfaces can display the
397 prompt in their own way. */
398 display_gdb_prompt (0);
399 /* This call looks bizarre, but it is required. If the user
400 entered a command that caused an error,
401 after_char_processing_hook won't be called from
402 rl_callback_read_char_wrapper. Using a cleanup there
403 won't work, since we want this function to be called
404 after a new prompt is printed. */
405 if (after_char_processing_hook)
406 (*after_char_processing_hook) ();
407 /* Maybe better to set a flag to be checked somewhere as to
408 whether display the prompt or not. */
409 }
410 }
411
412 /* We are done with the event loop. There are no more event sources
413 to listen to. So we exit GDB. */
414 return;
415 }
416
417 /* Initialize the tui by installing several gdb hooks, initializing
418 the tui IO and preparing the readline with the kind binding. */
419 static void
420 tui_init_hook (char *argv0)
421 {
422 /* Install exit handler to leave the screen in a good shape. */
423 atexit (tui_exit);
424
425 initializeStaticData ();
426
427 /* Install the permanent hooks. */
428 tui_target_new_objfile_chain = target_new_objfile_hook;
429 target_new_objfile_hook = tui_new_objfile_hook;
430
431 tui_initialize_io ();
432 tui_initialize_readline ();
433
434 /* Tell gdb to use the tui_command_loop as the main loop. */
435 command_loop_hook = tui_command_loop;
436
437 /* Decide in which mode to start using GDB (based on -tui). */
438 if (tui_version)
439 {
440 tui_enable ();
441 }
442 }
443
444 /* Initialize the tui. */
445 void
446 _initialize_tui (void)
447 {
448 /* Setup initialization hook. */
449 init_ui_hook = tui_init_hook;
450 }
451