| 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 | /* Breakpoint creation hook. |
| 171 | Update the screen to show the new breakpoint. */ |
| 172 | static void |
| 173 | tui_event_create_breakpoint (int number) |
| 174 | { |
| 175 | tui_update_all_breakpoint_info (); |
| 176 | } |
| 177 | |
| 178 | /* Breakpoint deletion hook. |
| 179 | Refresh the screen to update the breakpoint marks. */ |
| 180 | static void |
| 181 | tui_event_delete_breakpoint (int number) |
| 182 | { |
| 183 | tui_update_all_breakpoint_info (); |
| 184 | } |
| 185 | |
| 186 | static void |
| 187 | tui_event_modify_breakpoint (int number) |
| 188 | { |
| 189 | tui_update_all_breakpoint_info (); |
| 190 | } |
| 191 | |
| 192 | static void |
| 193 | tui_event_default (int number) |
| 194 | { |
| 195 | ; |
| 196 | } |
| 197 | |
| 198 | static struct gdb_events *tui_old_event_hooks; |
| 199 | |
| 200 | static struct gdb_events tui_event_hooks = |
| 201 | { |
| 202 | tui_event_create_breakpoint, |
| 203 | tui_event_delete_breakpoint, |
| 204 | tui_event_modify_breakpoint, |
| 205 | tui_event_default, |
| 206 | tui_event_default, |
| 207 | tui_event_default |
| 208 | }; |
| 209 | |
| 210 | /* Called when going to wait for the target. |
| 211 | Leave curses mode and setup program mode. */ |
| 212 | static ptid_t |
| 213 | tui_target_wait_hook (ptid_t pid, struct target_waitstatus *status) |
| 214 | { |
| 215 | ptid_t res; |
| 216 | |
| 217 | /* Leave tui mode (optional). */ |
| 218 | #if 0 |
| 219 | if (tui_active) |
| 220 | { |
| 221 | target_terminal_ours (); |
| 222 | endwin (); |
| 223 | target_terminal_inferior (); |
| 224 | } |
| 225 | #endif |
| 226 | tui_target_has_run = 1; |
| 227 | res = target_wait (pid, status); |
| 228 | |
| 229 | if (tui_active) |
| 230 | { |
| 231 | /* TODO: need to refresh (optional). */ |
| 232 | } |
| 233 | return res; |
| 234 | } |
| 235 | |
| 236 | /* The selected frame has changed. This is happens after a target |
| 237 | stop or when the user explicitly changes the frame (up/down/thread/...). */ |
| 238 | static void |
| 239 | tui_selected_frame_level_changed_hook (int level) |
| 240 | { |
| 241 | struct frame_info *fi; |
| 242 | |
| 243 | fi = selected_frame; |
| 244 | /* Ensure that symbols for this frame are read in. Also, determine the |
| 245 | source language of this frame, and switch to it if desired. */ |
| 246 | if (fi) |
| 247 | { |
| 248 | struct symtab *s; |
| 249 | |
| 250 | s = find_pc_symtab (fi->pc); |
| 251 | /* elz: this if here fixes the problem with the pc not being displayed |
| 252 | in the tui asm layout, with no debug symbols. The value of s |
| 253 | would be 0 here, and select_source_symtab would abort the |
| 254 | command by calling the 'error' function */ |
| 255 | if (s) |
| 256 | select_source_symtab (s); |
| 257 | |
| 258 | /* Display the frame position (even if there is no symbols). */ |
| 259 | tuiShowFrameInfo (fi); |
| 260 | |
| 261 | /* Refresh the register window if it's visible. */ |
| 262 | if (tui_is_window_visible (DATA_WIN)) |
| 263 | { |
| 264 | tui_refreshing_registers = 1; |
| 265 | tuiCheckDataValues (fi); |
| 266 | tui_refreshing_registers = 0; |
| 267 | } |
| 268 | } |
| 269 | } |
| 270 | |
| 271 | /* Called from print_frame_info to list the line we stopped in. */ |
| 272 | static void |
| 273 | tui_print_frame_info_listing_hook (struct symtab *s, int line, |
| 274 | int stopline, int noerror) |
| 275 | { |
| 276 | select_source_symtab (s); |
| 277 | tuiShowFrameInfo (selected_frame); |
| 278 | } |
| 279 | |
| 280 | /* Install the TUI specific hooks. */ |
| 281 | void |
| 282 | tui_install_hooks (void) |
| 283 | { |
| 284 | target_wait_hook = tui_target_wait_hook; |
| 285 | selected_frame_level_changed_hook = tui_selected_frame_level_changed_hook; |
| 286 | print_frame_info_listing_hook = tui_print_frame_info_listing_hook; |
| 287 | |
| 288 | query_hook = tui_query_hook; |
| 289 | |
| 290 | /* Install the event hooks. */ |
| 291 | tui_old_event_hooks = set_gdb_event_hooks (&tui_event_hooks); |
| 292 | |
| 293 | registers_changed_hook = tui_registers_changed_hook; |
| 294 | register_changed_hook = tui_register_changed_hook; |
| 295 | } |
| 296 | |
| 297 | /* Remove the TUI specific hooks. */ |
| 298 | void |
| 299 | tui_remove_hooks (void) |
| 300 | { |
| 301 | target_wait_hook = 0; |
| 302 | selected_frame_level_changed_hook = 0; |
| 303 | print_frame_info_listing_hook = 0; |
| 304 | query_hook = 0; |
| 305 | registers_changed_hook = 0; |
| 306 | register_changed_hook = 0; |
| 307 | |
| 308 | /* Restore the previous event hooks. */ |
| 309 | set_gdb_event_hooks (tui_old_event_hooks); |
| 310 | } |
| 311 | |
| 312 | /* Cleanup the tui before exiting. */ |
| 313 | static void |
| 314 | tui_exit (void) |
| 315 | { |
| 316 | /* Disable the tui. Curses mode is left leaving the screen |
| 317 | in a clean state (see endwin()). */ |
| 318 | tui_disable (); |
| 319 | } |
| 320 | |
| 321 | /* Initialize the tui by installing several gdb hooks, initializing |
| 322 | the tui IO and preparing the readline with the kind binding. */ |
| 323 | static void |
| 324 | tui_init_hook (char *argv0) |
| 325 | { |
| 326 | /* Install exit handler to leave the screen in a good shape. */ |
| 327 | atexit (tui_exit); |
| 328 | |
| 329 | initializeStaticData (); |
| 330 | |
| 331 | /* Install the permanent hooks. */ |
| 332 | tui_target_new_objfile_chain = target_new_objfile_hook; |
| 333 | target_new_objfile_hook = tui_new_objfile_hook; |
| 334 | |
| 335 | tui_initialize_io (); |
| 336 | tui_initialize_readline (); |
| 337 | |
| 338 | /* Decide in which mode to start using GDB (based on -tui). */ |
| 339 | if (tui_version) |
| 340 | { |
| 341 | tui_enable (); |
| 342 | } |
| 343 | } |
| 344 | |
| 345 | /* Initialize the tui. */ |
| 346 | void |
| 347 | _initialize_tui (void) |
| 348 | { |
| 349 | /* Setup initialization hook. */ |
| 350 | init_ui_hook = tui_init_hook; |
| 351 | } |
| 352 | |