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 static void tui_event_loop (void);
73 static void tui_command_loop (void);
76 tui_new_objfile_hook (struct objfile
* objfile
)
81 if (tui_target_new_objfile_chain
)
82 tui_target_new_objfile_chain (objfile
);
86 tui_query_hook (const char * msg
, va_list argp
)
92 /* Automatically answer "yes" if input is not from a terminal. */
93 if (!input_from_terminal_p ())
99 wrap_here (""); /* Flush any buffered output */
100 gdb_flush (gdb_stdout
);
102 vfprintf_filtered (gdb_stdout
, msg
, argp
);
103 printf_filtered ("(y or n) ");
106 gdb_flush (gdb_stdout
);
108 answer
= tui_getc (stdin
);
109 clearerr (stdin
); /* in case of C-d */
110 if (answer
== EOF
) /* C-d */
115 /* Eat rest of input line, to EOF or newline */
119 ans2
= tui_getc (stdin
);
122 while (ans2
!= EOF
&& ans2
!= '\n' && ans2
!= '\r');
136 printf_filtered ("Please answer y or n.\n");
142 /* Prevent recursion of registers_changed_hook(). */
143 static int tui_refreshing_registers
= 0;
146 tui_registers_changed_hook (void)
148 struct frame_info
*fi
;
150 fi
= deprecated_selected_frame
;
151 if (fi
&& tui_refreshing_registers
== 0)
153 tui_refreshing_registers
= 1;
155 tuiCheckDataValues (fi
);
157 tui_refreshing_registers
= 0;
162 tui_register_changed_hook (int regno
)
164 struct frame_info
*fi
;
166 fi
= deprecated_selected_frame
;
167 if (fi
&& tui_refreshing_registers
== 0)
169 tui_refreshing_registers
= 1;
170 tuiCheckDataValues (fi
);
171 tui_refreshing_registers
= 0;
175 /* Breakpoint creation hook.
176 Update the screen to show the new breakpoint. */
178 tui_event_create_breakpoint (int number
)
180 tui_update_all_breakpoint_info ();
183 /* Breakpoint deletion hook.
184 Refresh the screen to update the breakpoint marks. */
186 tui_event_delete_breakpoint (int number
)
188 tui_update_all_breakpoint_info ();
192 tui_event_modify_breakpoint (int number
)
194 tui_update_all_breakpoint_info ();
198 tui_event_default (int number
)
203 static struct gdb_events
*tui_old_event_hooks
;
205 static struct gdb_events tui_event_hooks
=
207 tui_event_create_breakpoint
,
208 tui_event_delete_breakpoint
,
209 tui_event_modify_breakpoint
,
215 /* Called when going to wait for the target.
216 Leave curses mode and setup program mode. */
218 tui_target_wait_hook (ptid_t pid
, struct target_waitstatus
*status
)
222 /* Leave tui mode (optional). */
226 target_terminal_ours ();
228 target_terminal_inferior ();
231 tui_target_has_run
= 1;
232 res
= target_wait (pid
, status
);
236 /* TODO: need to refresh (optional). */
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/...). */
244 tui_selected_frame_level_changed_hook (int level
)
246 struct frame_info
*fi
;
248 fi
= deprecated_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. */
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 */
261 select_source_symtab (s
);
263 /* Display the frame position (even if there is no symbols). */
264 tuiShowFrameInfo (fi
);
266 /* Refresh the register window if it's visible. */
267 if (tui_is_window_visible (DATA_WIN
))
269 tui_refreshing_registers
= 1;
270 tuiCheckDataValues (fi
);
271 tui_refreshing_registers
= 0;
276 /* Called from print_frame_info to list the line we stopped in. */
278 tui_print_frame_info_listing_hook (struct symtab
*s
, int line
,
279 int stopline
, int noerror
)
281 select_source_symtab (s
);
282 tuiShowFrameInfo (deprecated_selected_frame
);
285 /* Called when the target process died or is detached.
286 Update the status line. */
288 tui_detach_hook (void)
290 tuiShowFrameInfo (0);
294 /* Install the TUI specific hooks. */
296 tui_install_hooks (void)
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
;
302 query_hook
= tui_query_hook
;
304 /* Install the event hooks. */
305 tui_old_event_hooks
= set_gdb_event_hooks (&tui_event_hooks
);
307 registers_changed_hook
= tui_registers_changed_hook
;
308 register_changed_hook
= tui_register_changed_hook
;
309 detach_hook
= tui_detach_hook
;
312 /* Remove the TUI specific hooks. */
314 tui_remove_hooks (void)
316 target_wait_hook
= 0;
317 selected_frame_level_changed_hook
= 0;
318 print_frame_info_listing_hook
= 0;
320 registers_changed_hook
= 0;
321 register_changed_hook
= 0;
324 /* Restore the previous event hooks. */
325 set_gdb_event_hooks (tui_old_event_hooks
);
328 /* Cleanup the tui before exiting. */
332 /* Disable the tui. Curses mode is left leaving the screen
333 in a clean state (see endwin()). */
337 /* Initialize all the necessary variables, start the event loop,
338 register readline, and stdin, start the loop. */
340 tui_command_loop (void)
344 char *gdb_prompt
= get_prompt ();
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
)
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
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
);
361 display_gdb_prompt (0);
363 /* Now it's time to start the event loop. */
367 /* Start up the event loop. This is the entry point to the event loop
368 from the command loop. */
371 tui_event_loop (void)
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. */
381 int result
= catch_errors (gdb_do_one_event
, 0, "", RETURN_MASK_ALL
);
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. */
391 uiout
= tui_old_uiout
;
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. */
412 /* We are done with the event loop. There are no more event sources
413 to listen to. So we exit GDB. */
417 /* Initialize the tui by installing several gdb hooks, initializing
418 the tui IO and preparing the readline with the kind binding. */
420 tui_init_hook (char *argv0
)
422 /* Don't enable the TUI if a specific interpreter is installed. */
426 /* Install exit handler to leave the screen in a good shape. */
429 initializeStaticData ();
431 /* Install the permanent hooks. */
432 tui_target_new_objfile_chain
= target_new_objfile_hook
;
433 target_new_objfile_hook
= tui_new_objfile_hook
;
435 tui_initialize_io ();
436 tui_initialize_readline ();
438 /* Tell gdb to use the tui_command_loop as the main loop. */
439 command_loop_hook
= tui_command_loop
;
441 /* Decide in which mode to start using GDB (based on -tui). */
448 /* Initialize the tui. */
450 _initialize_tui (void)
452 /* Setup initialization hook. */
453 init_ui_hook
= tui_init_hook
;