1 /* GDB hooks for TUI. 2 3 Copyright (C) 2001-2013 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 3 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, see <http://www.gnu.org/licenses/>. */ 19 20 #include "defs.h" 21 #include "symtab.h" 22 #include "inferior.h" 23 #include "command.h" 24 #include "bfd.h" 25 #include "symfile.h" 26 #include "objfiles.h" 27 #include "target.h" 28 #include "gdbcore.h" 29 #include "event-loop.h" 30 #include "event-top.h" 31 #include "frame.h" 32 #include "breakpoint.h" 33 #include "ui-out.h" 34 #include "top.h" 35 #include "observer.h" 36 #include <unistd.h> 37 #include <fcntl.h> 38 39 #include "tui/tui.h" 40 #include "tui/tui-hooks.h" 41 #include "tui/tui-data.h" 42 #include "tui/tui-layout.h" 43 #include "tui/tui-io.h" 44 #include "tui/tui-regs.h" 45 #include "tui/tui-win.h" 46 #include "tui/tui-stack.h" 47 #include "tui/tui-windata.h" 48 #include "tui/tui-winsource.h" 49 50 #include "gdb_curses.h" 51 52 /* This redefines CTRL if it is not already defined, so it must come 53 after terminal state releated include files like <term.h> and 54 "gdb_curses.h". */ 55 #include "readline/readline.h" 56 57 int tui_target_has_run = 0; 58 59 static void 60 tui_new_objfile_hook (struct objfile* objfile) 61 { 62 if (tui_active) 63 tui_display_main (); 64 } 65 66 static int ATTRIBUTE_PRINTF (1, 0) 67 tui_query_hook (const char *msg, va_list argp) 68 { 69 int retval; 70 int ans2; 71 int answer; 72 char *question; 73 struct cleanup *old_chain; 74 75 /* Format the question outside of the loop, to avoid reusing 76 ARGP. */ 77 question = xstrvprintf (msg, argp); 78 old_chain = make_cleanup (xfree, question); 79 80 echo (); 81 while (1) 82 { 83 wrap_here (""); /* Flush any buffered output. */ 84 gdb_flush (gdb_stdout); 85 86 fputs_filtered (question, gdb_stdout); 87 printf_filtered (_("(y or n) ")); 88 89 wrap_here (""); 90 gdb_flush (gdb_stdout); 91 92 answer = tui_getc (stdin); 93 clearerr (stdin); /* in case of C-d */ 94 if (answer == EOF) /* C-d */ 95 { 96 retval = 1; 97 break; 98 } 99 /* Eat rest of input line, to EOF or newline. */ 100 if (answer != '\n') 101 do 102 { 103 ans2 = tui_getc (stdin); 104 clearerr (stdin); 105 } 106 while (ans2 != EOF && ans2 != '\n' && ans2 != '\r'); 107 108 if (answer >= 'a') 109 answer -= 040; 110 if (answer == 'Y') 111 { 112 retval = 1; 113 break; 114 } 115 if (answer == 'N') 116 { 117 retval = 0; 118 break; 119 } 120 printf_filtered (_("Please answer y or n.\n")); 121 } 122 noecho (); 123 124 do_cleanups (old_chain); 125 return retval; 126 } 127 128 /* Prevent recursion of deprecated_register_changed_hook(). */ 129 static int tui_refreshing_registers = 0; 130 131 static void 132 tui_register_changed_hook (int regno) 133 { 134 struct frame_info *fi; 135 136 fi = get_selected_frame (NULL); 137 if (tui_refreshing_registers == 0) 138 { 139 tui_refreshing_registers = 1; 140 tui_check_data_values (fi); 141 tui_refreshing_registers = 0; 142 } 143 } 144 145 /* Breakpoint creation hook. 146 Update the screen to show the new breakpoint. */ 147 static void 148 tui_event_create_breakpoint (struct breakpoint *b) 149 { 150 tui_update_all_breakpoint_info (); 151 } 152 153 /* Breakpoint deletion hook. 154 Refresh the screen to update the breakpoint marks. */ 155 static void 156 tui_event_delete_breakpoint (struct breakpoint *b) 157 { 158 tui_update_all_breakpoint_info (); 159 } 160 161 static void 162 tui_event_modify_breakpoint (struct breakpoint *b) 163 { 164 tui_update_all_breakpoint_info (); 165 } 166 167 /* Called when a command is about to proceed the inferior. */ 168 169 static void 170 tui_about_to_proceed (void) 171 { 172 /* Leave tui mode (optional). */ 173 #if 0 174 if (tui_active) 175 { 176 target_terminal_ours (); 177 endwin (); 178 target_terminal_inferior (); 179 } 180 #endif 181 tui_target_has_run = 1; 182 } 183 184 /* The selected frame has changed. This is happens after a target 185 stop or when the user explicitly changes the frame 186 (up/down/thread/...). */ 187 static void 188 tui_selected_frame_level_changed_hook (int level) 189 { 190 struct frame_info *fi; 191 CORE_ADDR pc; 192 193 /* Negative level means that the selected frame was cleared. */ 194 if (level < 0) 195 return; 196 197 fi = get_selected_frame (NULL); 198 /* Ensure that symbols for this frame are read in. Also, determine 199 the source language of this frame, and switch to it if 200 desired. */ 201 if (get_frame_pc_if_available (fi, &pc)) 202 { 203 struct symtab *s; 204 205 s = find_pc_symtab (pc); 206 /* elz: This if here fixes the problem with the pc not being 207 displayed in the tui asm layout, with no debug symbols. The 208 value of s would be 0 here, and select_source_symtab would 209 abort the command by calling the 'error' function. */ 210 if (s) 211 select_source_symtab (s); 212 } 213 214 /* Display the frame position (even if there is no symbols or the PC 215 is not known). */ 216 tui_show_frame_info (fi); 217 218 /* Refresh the register window if it's visible. */ 219 if (tui_is_window_visible (DATA_WIN)) 220 { 221 tui_refreshing_registers = 1; 222 tui_check_data_values (fi); 223 tui_refreshing_registers = 0; 224 } 225 } 226 227 /* Called from print_frame_info to list the line we stopped in. */ 228 static void 229 tui_print_frame_info_listing_hook (struct symtab *s, 230 int line, 231 int stopline, 232 int noerror) 233 { 234 select_source_symtab (s); 235 tui_show_frame_info (get_selected_frame (NULL)); 236 } 237 238 /* Perform all necessary cleanups regarding our module's inferior data 239 that is required after the inferior INF just exited. */ 240 241 static void 242 tui_inferior_exit (struct inferior *inf) 243 { 244 /* Leave the SingleKey mode to make sure the gdb prompt is visible. */ 245 tui_set_key_mode (TUI_COMMAND_MODE); 246 tui_show_frame_info (0); 247 tui_display_main (); 248 } 249 250 /* Observers created when installing TUI hooks. */ 251 static struct observer *tui_bp_created_observer; 252 static struct observer *tui_bp_deleted_observer; 253 static struct observer *tui_bp_modified_observer; 254 static struct observer *tui_inferior_exit_observer; 255 static struct observer *tui_about_to_proceed_observer; 256 257 /* Install the TUI specific hooks. */ 258 void 259 tui_install_hooks (void) 260 { 261 deprecated_selected_frame_level_changed_hook 262 = tui_selected_frame_level_changed_hook; 263 deprecated_print_frame_info_listing_hook 264 = tui_print_frame_info_listing_hook; 265 266 deprecated_query_hook = tui_query_hook; 267 268 /* Install the event hooks. */ 269 tui_bp_created_observer 270 = observer_attach_breakpoint_created (tui_event_create_breakpoint); 271 tui_bp_deleted_observer 272 = observer_attach_breakpoint_deleted (tui_event_delete_breakpoint); 273 tui_bp_modified_observer 274 = observer_attach_breakpoint_modified (tui_event_modify_breakpoint); 275 tui_inferior_exit_observer 276 = observer_attach_inferior_exit (tui_inferior_exit); 277 tui_about_to_proceed_observer 278 = observer_attach_about_to_proceed (tui_about_to_proceed); 279 280 deprecated_register_changed_hook = tui_register_changed_hook; 281 } 282 283 /* Remove the TUI specific hooks. */ 284 void 285 tui_remove_hooks (void) 286 { 287 deprecated_selected_frame_level_changed_hook = 0; 288 deprecated_print_frame_info_listing_hook = 0; 289 deprecated_query_hook = 0; 290 deprecated_register_changed_hook = 0; 291 292 /* Remove our observers. */ 293 observer_detach_breakpoint_created (tui_bp_created_observer); 294 tui_bp_created_observer = NULL; 295 observer_detach_breakpoint_deleted (tui_bp_deleted_observer); 296 tui_bp_deleted_observer = NULL; 297 observer_detach_breakpoint_modified (tui_bp_modified_observer); 298 tui_bp_modified_observer = NULL; 299 observer_detach_inferior_exit (tui_inferior_exit_observer); 300 tui_inferior_exit_observer = NULL; 301 observer_detach_about_to_proceed (tui_about_to_proceed_observer); 302 tui_about_to_proceed_observer = NULL; 303 } 304 305 void _initialize_tui_hooks (void); 306 307 void 308 _initialize_tui_hooks (void) 309 { 310 /* Install the permanent hooks. */ 311 observer_attach_new_objfile (tui_new_objfile_hook); 312 } 313