8c906c5ed9efc6973a26176d33b87e0cef233d8b
[external/binutils.git] / gdb / tui / tui-hooks.c
1 /* GDB hooks for TUI.
2
3    Copyright 2001, 2002, 2003, 2004, 2005 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 #include "defs.h"
23 #include "symtab.h"
24 #include "inferior.h"
25 #include "command.h"
26 #include "bfd.h"
27 #include "symfile.h"
28 #include "objfiles.h"
29 #include "target.h"
30 #include "gdbcore.h"
31 #include "event-loop.h"
32 #include "event-top.h"
33 #include "frame.h"
34 #include "breakpoint.h"
35 #include "gdb-events.h"
36 #include "ui-out.h"
37 #include "top.h"
38 #include <unistd.h>
39 #include <fcntl.h>
40
41 #include "tui/tui.h"
42 #include "tui/tui-hooks.h"
43 #include "tui/tui-data.h"
44 #include "tui/tui-layout.h"
45 #include "tui/tui-io.h"
46 #include "tui/tui-regs.h"
47 #include "tui/tui-win.h"
48 #include "tui/tui-stack.h"
49 #include "tui/tui-windata.h"
50 #include "tui/tui-winsource.h"
51
52 #include "gdb_curses.h"
53
54 /* This redefines CTRL if it is not already defined, so it must come
55    after terminal state releated include files like <term.h> and
56    "gdb_curses.h".  */
57 #include "readline/readline.h"
58
59 int tui_target_has_run = 0;
60
61 static void (* tui_target_new_objfile_chain) (struct objfile*);
62
63 static void
64 tui_new_objfile_hook (struct objfile* objfile)
65 {
66   if (tui_active)
67     tui_display_main ();
68   
69   if (tui_target_new_objfile_chain)
70     tui_target_new_objfile_chain (objfile);
71 }
72
73 static int ATTR_FORMAT (printf, 1, 0)
74 tui_query_hook (const char * msg, va_list argp)
75 {
76   int retval;
77   int ans2;
78   int answer;
79
80   /* Automatically answer "yes" if input is not from a terminal.  */
81   if (!input_from_terminal_p ())
82     return 1;
83
84   echo ();
85   while (1)
86     {
87       wrap_here ("");           /* Flush any buffered output */
88       gdb_flush (gdb_stdout);
89
90       vfprintf_filtered (gdb_stdout, msg, argp);
91       printf_filtered (_("(y or n) "));
92
93       wrap_here ("");
94       gdb_flush (gdb_stdout);
95
96       answer = tui_getc (stdin);
97       clearerr (stdin);         /* in case of C-d */
98       if (answer == EOF)        /* C-d */
99         {
100           retval = 1;
101           break;
102         }
103       /* Eat rest of input line, to EOF or newline */
104       if (answer != '\n')
105         do
106           {
107             ans2 = tui_getc (stdin);
108             clearerr (stdin);
109           }
110         while (ans2 != EOF && ans2 != '\n' && ans2 != '\r');
111
112       if (answer >= 'a')
113         answer -= 040;
114       if (answer == 'Y')
115         {
116           retval = 1;
117           break;
118         }
119       if (answer == 'N')
120         {
121           retval = 0;
122           break;
123         }
124       printf_filtered (_("Please answer y or n.\n"));
125     }
126   noecho ();
127   return retval;
128 }
129
130 /* Prevent recursion of deprecated_registers_changed_hook().  */
131 static int tui_refreshing_registers = 0;
132
133 static void
134 tui_registers_changed_hook (void)
135 {
136   struct frame_info *fi;
137
138   fi = deprecated_selected_frame;
139   if (fi && tui_refreshing_registers == 0)
140     {
141       tui_refreshing_registers = 1;
142 #if 0
143       tui_check_data_values (fi);
144 #endif
145       tui_refreshing_registers = 0;
146     }
147 }
148
149 static void
150 tui_register_changed_hook (int regno)
151 {
152   struct frame_info *fi;
153
154   fi = deprecated_selected_frame;
155   if (fi && tui_refreshing_registers == 0)
156     {
157       tui_refreshing_registers = 1;
158       tui_check_data_values (fi);
159       tui_refreshing_registers = 0;
160     }
161 }
162
163 /* Breakpoint creation hook.
164    Update the screen to show the new breakpoint.  */
165 static void
166 tui_event_create_breakpoint (int number)
167 {
168   tui_update_all_breakpoint_info ();
169 }
170
171 /* Breakpoint deletion hook.
172    Refresh the screen to update the breakpoint marks.  */
173 static void
174 tui_event_delete_breakpoint (int number)
175 {
176   tui_update_all_breakpoint_info ();
177 }
178
179 static void
180 tui_event_modify_breakpoint (int number)
181 {
182   tui_update_all_breakpoint_info ();
183 }
184
185 static void
186 tui_event_default (int number)
187 {
188   ;
189 }
190
191 static struct gdb_events *tui_old_event_hooks;
192
193 static struct gdb_events tui_event_hooks =
194 {
195   tui_event_create_breakpoint,
196   tui_event_delete_breakpoint,
197   tui_event_modify_breakpoint,
198   tui_event_default,
199   tui_event_default,
200   tui_event_default
201 };
202
203 /* Called when going to wait for the target.
204    Leave curses mode and setup program mode.  */
205 static ptid_t
206 tui_target_wait_hook (ptid_t pid, struct target_waitstatus *status)
207 {
208   ptid_t res;
209
210   /* Leave tui mode (optional).  */
211 #if 0
212   if (tui_active)
213     {
214       target_terminal_ours ();
215       endwin ();
216       target_terminal_inferior ();
217     }
218 #endif
219   tui_target_has_run = 1;
220   res = target_wait (pid, status);
221
222   if (tui_active)
223     {
224       /* TODO: need to refresh (optional).  */
225     }
226   return res;
227 }
228
229 /* The selected frame has changed.  This is happens after a target
230    stop or when the user explicitly changes the frame (up/down/thread/...).  */
231 static void
232 tui_selected_frame_level_changed_hook (int level)
233 {
234   struct frame_info *fi;
235
236   fi = deprecated_selected_frame;
237   /* Ensure that symbols for this frame are read in.  Also, determine the
238      source language of this frame, and switch to it if desired.  */
239   if (fi)
240     {
241       struct symtab *s;
242       
243       s = find_pc_symtab (get_frame_pc (fi));
244       /* elz: this if here fixes the problem with the pc not being displayed
245          in the tui asm layout, with no debug symbols. The value of s 
246          would be 0 here, and select_source_symtab would abort the
247          command by calling the 'error' function */
248       if (s)
249         select_source_symtab (s);
250
251       /* Display the frame position (even if there is no symbols).  */
252       tui_show_frame_info (fi);
253
254       /* Refresh the register window if it's visible.  */
255       if (tui_is_window_visible (DATA_WIN))
256         {
257           tui_refreshing_registers = 1;
258           tui_check_data_values (fi);
259           tui_refreshing_registers = 0;
260         }
261     }
262 }
263
264 /* Called from print_frame_info to list the line we stopped in.  */
265 static void
266 tui_print_frame_info_listing_hook (struct symtab *s, int line,
267                                    int stopline, int noerror)
268 {
269   select_source_symtab (s);
270   tui_show_frame_info (deprecated_selected_frame);
271 }
272
273 /* Called when the target process died or is detached.
274    Update the status line.  */
275 static void
276 tui_detach_hook (void)
277 {
278   tui_show_frame_info (0);
279   tui_display_main ();
280 }
281
282 /* Install the TUI specific hooks.  */
283 void
284 tui_install_hooks (void)
285 {
286   deprecated_target_wait_hook = tui_target_wait_hook;
287   deprecated_selected_frame_level_changed_hook = tui_selected_frame_level_changed_hook;
288   deprecated_print_frame_info_listing_hook = tui_print_frame_info_listing_hook;
289
290   deprecated_query_hook = tui_query_hook;
291
292   /* Install the event hooks.  */
293   tui_old_event_hooks = deprecated_set_gdb_event_hooks (&tui_event_hooks);
294
295   deprecated_registers_changed_hook = tui_registers_changed_hook;
296   deprecated_register_changed_hook = tui_register_changed_hook;
297   deprecated_detach_hook = tui_detach_hook;
298 }
299
300 /* Remove the TUI specific hooks.  */
301 void
302 tui_remove_hooks (void)
303 {
304   deprecated_target_wait_hook = 0;
305   deprecated_selected_frame_level_changed_hook = 0;
306   deprecated_print_frame_info_listing_hook = 0;
307   deprecated_query_hook = 0;
308   deprecated_registers_changed_hook = 0;
309   deprecated_register_changed_hook = 0;
310   deprecated_detach_hook = 0;
311
312   /* Restore the previous event hooks.  */
313   deprecated_set_gdb_event_hooks (tui_old_event_hooks);
314 }
315
316 void _initialize_tui_hooks (void);
317
318 void
319 _initialize_tui_hooks (void)
320 {
321   /* Install the permanent hooks.  */
322   tui_target_new_objfile_chain = deprecated_target_new_objfile_hook;
323   deprecated_target_new_objfile_hook = tui_new_objfile_hook;
324 }