3 Copyright 2001, 2002 Free Software Foundation, Inc.
5 This file is part of GDB.
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.
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.
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. */
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>
48 #include "event-loop.h"
49 #include "event-top.h"
51 #include "breakpoint.h"
52 #include "gdb-events.h"
55 #include <readline/readline.h>
61 #include "tuiLayout.h"
66 #include "tuiDataWin.h"
67 #include "tuiSourceWin.h"
69 int tui_target_has_run = 0;
71 static void (* tui_target_new_objfile_chain) (struct objfile*);
72 extern void (*selected_frame_level_changed_hook) (int);
73 static void tui_event_loop (void);
74 static void tui_command_loop (void);
77 tui_new_objfile_hook (struct objfile* objfile)
82 if (tui_target_new_objfile_chain)
83 tui_target_new_objfile_chain (objfile);
87 tui_query_hook (const char * msg, va_list argp)
93 /* Automatically answer "yes" if input is not from a terminal. */
94 if (!input_from_terminal_p ())
100 wrap_here (""); /* Flush any buffered output */
101 gdb_flush (gdb_stdout);
103 vfprintf_filtered (gdb_stdout, msg, argp);
104 printf_filtered ("(y or n) ");
107 gdb_flush (gdb_stdout);
109 answer = tui_getc (stdin);
110 clearerr (stdin); /* in case of C-d */
111 if (answer == EOF) /* C-d */
116 /* Eat rest of input line, to EOF or newline */
120 ans2 = tui_getc (stdin);
123 while (ans2 != EOF && ans2 != '\n' && ans2 != '\r');
137 printf_filtered ("Please answer y or n.\n");
143 /* Prevent recursion of registers_changed_hook(). */
144 static int tui_refreshing_registers = 0;
147 tui_registers_changed_hook (void)
149 struct frame_info *fi;
152 if (fi && tui_refreshing_registers == 0)
154 tui_refreshing_registers = 1;
156 tuiCheckDataValues (fi);
158 tui_refreshing_registers = 0;
163 tui_register_changed_hook (int regno)
165 struct frame_info *fi;
168 if (fi && tui_refreshing_registers == 0)
170 tui_refreshing_registers = 1;
171 tuiCheckDataValues (fi);
172 tui_refreshing_registers = 0;
176 /* Breakpoint creation hook.
177 Update the screen to show the new breakpoint. */
179 tui_event_create_breakpoint (int number)
181 tui_update_all_breakpoint_info ();
184 /* Breakpoint deletion hook.
185 Refresh the screen to update the breakpoint marks. */
187 tui_event_delete_breakpoint (int number)
189 tui_update_all_breakpoint_info ();
193 tui_event_modify_breakpoint (int number)
195 tui_update_all_breakpoint_info ();
199 tui_event_default (int number)
204 static struct gdb_events *tui_old_event_hooks;
206 static struct gdb_events tui_event_hooks =
208 tui_event_create_breakpoint,
209 tui_event_delete_breakpoint,
210 tui_event_modify_breakpoint,
216 /* Called when going to wait for the target.
217 Leave curses mode and setup program mode. */
219 tui_target_wait_hook (ptid_t pid, struct target_waitstatus *status)
223 /* Leave tui mode (optional). */
227 target_terminal_ours ();
229 target_terminal_inferior ();
232 tui_target_has_run = 1;
233 res = target_wait (pid, status);
237 /* TODO: need to refresh (optional). */
242 /* The selected frame has changed. This is happens after a target
243 stop or when the user explicitly changes the frame (up/down/thread/...). */
245 tui_selected_frame_level_changed_hook (int level)
247 struct frame_info *fi;
250 /* Ensure that symbols for this frame are read in. Also, determine the
251 source language of this frame, and switch to it if desired. */
256 s = find_pc_symtab (fi->pc);
257 /* elz: this if here fixes the problem with the pc not being displayed
258 in the tui asm layout, with no debug symbols. The value of s
259 would be 0 here, and select_source_symtab would abort the
260 command by calling the 'error' function */
262 select_source_symtab (s);
264 /* Display the frame position (even if there is no symbols). */
265 tuiShowFrameInfo (fi);
267 /* Refresh the register window if it's visible. */
268 if (tui_is_window_visible (DATA_WIN))
270 tui_refreshing_registers = 1;
271 tuiCheckDataValues (fi);
272 tui_refreshing_registers = 0;
277 /* Called from print_frame_info to list the line we stopped in. */
279 tui_print_frame_info_listing_hook (struct symtab *s, int line,
280 int stopline, int noerror)
282 select_source_symtab (s);
283 tuiShowFrameInfo (selected_frame);
286 /* Called when the target process died or is detached.
287 Update the status line. */
289 tui_detach_hook (void)
291 tuiShowFrameInfo (0);
295 /* Install the TUI specific hooks. */
297 tui_install_hooks (void)
299 target_wait_hook = tui_target_wait_hook;
300 selected_frame_level_changed_hook = tui_selected_frame_level_changed_hook;
301 print_frame_info_listing_hook = tui_print_frame_info_listing_hook;
303 query_hook = tui_query_hook;
305 /* Install the event hooks. */
306 tui_old_event_hooks = set_gdb_event_hooks (&tui_event_hooks);
308 registers_changed_hook = tui_registers_changed_hook;
309 register_changed_hook = tui_register_changed_hook;
310 detach_hook = tui_detach_hook;
313 /* Remove the TUI specific hooks. */
315 tui_remove_hooks (void)
317 target_wait_hook = 0;
318 selected_frame_level_changed_hook = 0;
319 print_frame_info_listing_hook = 0;
321 registers_changed_hook = 0;
322 register_changed_hook = 0;
325 /* Restore the previous event hooks. */
326 set_gdb_event_hooks (tui_old_event_hooks);
329 /* Cleanup the tui before exiting. */
333 /* Disable the tui. Curses mode is left leaving the screen
334 in a clean state (see endwin()). */
338 /* Initialize all the necessary variables, start the event loop,
339 register readline, and stdin, start the loop. */
341 tui_command_loop (void)
345 char *gdb_prompt = get_prompt ();
347 /* If we are using readline, set things up and display the first
348 prompt, otherwise just print the prompt. */
349 if (async_command_editing_p)
351 /* Tell readline what the prompt to display is and what function it
352 will need to call after a whole line is read. This also displays
354 length = strlen (PREFIX (0)) + strlen (gdb_prompt) + strlen (SUFFIX (0)) + 1;
355 a_prompt = (char *) xmalloc (length);
356 strcpy (a_prompt, PREFIX (0));
357 strcat (a_prompt, gdb_prompt);
358 strcat (a_prompt, SUFFIX (0));
359 rl_callback_handler_install (a_prompt, input_handler);
362 display_gdb_prompt (0);
364 /* Now it's time to start the event loop. */
368 /* Start up the event loop. This is the entry point to the event loop
369 from the command loop. */
372 tui_event_loop (void)
374 /* Loop until there is nothing to do. This is the entry point to the
375 event loop engine. gdb_do_one_event, called via catch_errors()
376 will process one event for each invocation. It blocks waits for
377 an event and then processes it. >0 when an event is processed, 0
378 when catch_errors() caught an error and <0 when there are no
379 longer any event sources registered. */
382 int result = catch_errors (gdb_do_one_event, 0, "", RETURN_MASK_ALL);
386 /* Update gdb output according to TUI mode. Since catch_errors
387 preserves the uiout from changing, this must be done at top
388 level of event loop. */
392 uiout = tui_old_uiout;
396 /* FIXME: this should really be a call to a hook that is
397 interface specific, because interfaces can display the
398 prompt in their own way. */
399 display_gdb_prompt (0);
400 /* This call looks bizarre, but it is required. If the user
401 entered a command that caused an error,
402 after_char_processing_hook won't be called from
403 rl_callback_read_char_wrapper. Using a cleanup there
404 won't work, since we want this function to be called
405 after a new prompt is printed. */
406 if (after_char_processing_hook)
407 (*after_char_processing_hook) ();
408 /* Maybe better to set a flag to be checked somewhere as to
409 whether display the prompt or not. */
413 /* We are done with the event loop. There are no more event sources
414 to listen to. So we exit GDB. */
418 /* Initialize the tui by installing several gdb hooks, initializing
419 the tui IO and preparing the readline with the kind binding. */
421 tui_init_hook (char *argv0)
423 /* Install exit handler to leave the screen in a good shape. */
426 initializeStaticData ();
428 /* Install the permanent hooks. */
429 tui_target_new_objfile_chain = target_new_objfile_hook;
430 target_new_objfile_hook = tui_new_objfile_hook;
432 tui_initialize_io ();
433 tui_initialize_readline ();
435 /* Tell gdb to use the tui_command_loop as the main loop. */
436 command_loop_hook = tui_command_loop;
438 /* Decide in which mode to start using GDB (based on -tui). */
445 /* Initialize the tui. */
447 _initialize_tui (void)
449 /* Setup initialization hook. */
450 init_ui_hook = tui_init_hook;