gdb/
[platform/upstream/binutils.git] / gdb / tui / tui-stack.c
1 /* TUI display locator.
2
3    Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2006, 2007, 2008,
4    2009, 2010, 2011 Free Software Foundation, Inc.
5
6    Contributed by Hewlett-Packard Company.
7
8    This file is part of GDB.
9
10    This program is free software; you can redistribute it and/or modify
11    it under the terms of the GNU General Public License as published by
12    the Free Software Foundation; either version 3 of the License, or
13    (at your option) any later version.
14
15    This program is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18    GNU General Public License for more details.
19
20    You should have received a copy of the GNU General Public License
21    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
22
23 #include "defs.h"
24 #include "symtab.h"
25 #include "breakpoint.h"
26 #include "frame.h"
27 #include "command.h"
28 #include "inferior.h"
29 #include "target.h"
30 #include "top.h"
31 #include "gdb-demangle.h"
32 #include "gdb_string.h"
33 #include "tui/tui.h"
34 #include "tui/tui-data.h"
35 #include "tui/tui-stack.h"
36 #include "tui/tui-wingeneral.h"
37 #include "tui/tui-source.h"
38 #include "tui/tui-winsource.h"
39 #include "tui/tui-file.h"
40
41 #include "gdb_curses.h"
42
43 /* Get a printable name for the function at the address.
44    The symbol name is demangled if demangling is turned on.
45    Returns a pointer to a static area holding the result.  */
46 static char *tui_get_function_from_frame (struct frame_info *fi);
47
48 /* Set the filename portion of the locator.  */
49 static void tui_set_locator_filename (const char *filename);
50
51 /* Update the locator, with the provided arguments.  */
52 static void tui_set_locator_info (struct gdbarch *gdbarch,
53                                   const char *filename,
54                                   const char *procname,
55                                   int lineno, CORE_ADDR addr);
56
57 static void tui_update_command (char *, int);
58 \f
59
60 /* Create the status line to display as much information as we can on
61    this single line: target name, process number, current function,
62    current line, current PC, SingleKey mode.  */
63 static char*
64 tui_make_status_line (struct tui_locator_element *loc)
65 {
66   char *string;
67   char line_buf[50], *pname;
68   char *buf;
69   int status_size;
70   int i, proc_width;
71   const char *pid_name;
72   const char *pc_buf;
73   int target_width;
74   int pid_width;
75   int line_width;
76   int pc_width;
77   struct ui_file *pc_out;
78
79   if (ptid_equal (inferior_ptid, null_ptid))
80     pid_name = "No process";
81   else
82     pid_name = target_pid_to_str (inferior_ptid);
83
84   target_width = strlen (target_shortname);
85   if (target_width > MAX_TARGET_WIDTH)
86     target_width = MAX_TARGET_WIDTH;
87
88   pid_width = strlen (pid_name);
89   if (pid_width > MAX_PID_WIDTH)
90     pid_width = MAX_PID_WIDTH;
91
92   status_size = tui_term_width ();
93   string = (char *) xmalloc (status_size + 1);
94   buf = (char*) alloca (status_size + 1);
95
96   /* Translate line number and obtain its size.  */
97   if (loc->line_no > 0)
98     sprintf (line_buf, "%d", loc->line_no);
99   else
100     strcpy (line_buf, "??");
101   line_width = strlen (line_buf);
102   if (line_width < MIN_LINE_WIDTH)
103     line_width = MIN_LINE_WIDTH;
104
105   /* Translate PC address.  */
106   pc_out = tui_sfileopen (128);
107   fputs_filtered (loc->gdbarch? paddress (loc->gdbarch, loc->addr) : "??",
108                   pc_out);
109   pc_buf = tui_file_get_strbuf (pc_out);
110   pc_width = strlen (pc_buf);
111   
112   /* First determine the amount of proc name width we have available.
113      The +1 are for a space separator between fields.
114      The -1 are to take into account the \0 counted by sizeof.  */
115   proc_width = (status_size
116                 - (target_width + 1)
117                 - (pid_width + 1)
118                 - (sizeof (PROC_PREFIX) - 1 + 1)
119                 - (sizeof (LINE_PREFIX) - 1 + line_width + 1)
120                 - (sizeof (PC_PREFIX) - 1 + pc_width + 1)
121                 - (tui_current_key_mode == TUI_SINGLE_KEY_MODE
122                    ? (sizeof (SINGLE_KEY) - 1 + 1)
123                    : 0));
124
125   /* If there is no room to print the function name, try by removing
126      some fields.  */
127   if (proc_width < MIN_PROC_WIDTH)
128     {
129       proc_width += target_width + 1;
130       target_width = 0;
131       if (proc_width < MIN_PROC_WIDTH)
132         {
133           proc_width += pid_width + 1;
134           pid_width = 0;
135           if (proc_width <= MIN_PROC_WIDTH)
136             {
137               proc_width += pc_width + sizeof (PC_PREFIX) - 1 + 1;
138               pc_width = 0;
139               if (proc_width < 0)
140                 {
141                   proc_width += line_width + sizeof (LINE_PREFIX) - 1 + 1;
142                   line_width = 0;
143                   if (proc_width < 0)
144                     proc_width = 0;
145                 }
146             }
147         }
148     }
149
150   /* Now convert elements to string form.  */
151   pname = loc->proc_name;
152
153   /* Now create the locator line from the string version of the
154      elements.  We could use sprintf() here but that wouldn't ensure
155      that we don't overrun the size of the allocated buffer.
156      strcat_to_buf() will.  */
157   *string = (char) 0;
158
159   if (target_width > 0)
160     {
161       sprintf (buf, "%*.*s ",
162                -target_width, target_width, target_shortname);
163       strcat_to_buf (string, status_size, buf);
164     }
165   if (pid_width > 0)
166     {
167       sprintf (buf, "%*.*s ",
168                -pid_width, pid_width, pid_name);
169       strcat_to_buf (string, status_size, buf);
170     }
171   
172   /* Show whether we are in SingleKey mode.  */
173   if (tui_current_key_mode == TUI_SINGLE_KEY_MODE)
174     {
175       strcat_to_buf (string, status_size, SINGLE_KEY);
176       strcat_to_buf (string, status_size, " ");
177     }
178
179   /* Procedure/class name.  */
180   if (proc_width > 0)
181     {
182       if (strlen (pname) > proc_width)
183         sprintf (buf, "%s%*.*s* ", PROC_PREFIX,
184                  1 - proc_width, proc_width - 1, pname);
185       else
186         sprintf (buf, "%s%*.*s ", PROC_PREFIX,
187                  -proc_width, proc_width, pname);
188       strcat_to_buf (string, status_size, buf);
189     }
190
191   if (line_width > 0)
192     {
193       sprintf (buf, "%s%*.*s ", LINE_PREFIX,
194                -line_width, line_width, line_buf);
195       strcat_to_buf (string, status_size, buf);
196     }
197   if (pc_width > 0)
198     {
199       strcat_to_buf (string, status_size, PC_PREFIX);
200       strcat_to_buf (string, status_size, pc_buf);
201     }
202   
203   
204   for (i = strlen (string); i < status_size; i++)
205     string[i] = ' ';
206   string[status_size] = (char) 0;
207
208   ui_file_delete (pc_out);
209   return string;
210 }
211
212 /* Get a printable name for the function at the address.  The symbol
213    name is demangled if demangling is turned on.  Returns a pointer to
214    a static area holding the result.  */
215 static char*
216 tui_get_function_from_frame (struct frame_info *fi)
217 {
218   static char name[256];
219   struct ui_file *stream = tui_sfileopen (256);
220   char *p;
221
222   print_address_symbolic (get_frame_arch (fi), get_frame_pc (fi),
223                           stream, demangle, "");
224   p = tui_file_get_strbuf (stream);
225
226   /* Use simple heuristics to isolate the function name.  The symbol
227      can be demangled and we can have function parameters.  Remove
228      them because the status line is too short to display them.  */
229   if (*p == '<')
230     p++;
231   strncpy (name, p, sizeof (name) - 1);
232   p = strchr (name, '(');
233   if (!p)
234     p = strchr (name, '>');
235   if (p)
236     *p = 0;
237   p = strchr (name, '+');
238   if (p)
239     *p = 0;
240   ui_file_delete (stream);
241   return name;
242 }
243
244 void
245 tui_show_locator_content (void)
246 {
247   char *string;
248   struct tui_gen_win_info *locator;
249
250   locator = tui_locator_win_info_ptr ();
251
252   if (locator != NULL && locator->handle != (WINDOW *) NULL)
253     {
254       struct tui_win_element *element;
255
256       element = (struct tui_win_element *) locator->content[0];
257
258       string = tui_make_status_line (&element->which_element.locator);
259       wmove (locator->handle, 0, 0);
260       /* We ignore the return value from wstandout and wstandend, casting
261          them to void in order to avoid a compiler warning.  The warning
262          itself was introduced by a patch to ncurses 5.7 dated 2009-08-29,
263          changing these macro to expand to code that causes the compiler
264          to generate an unused-value warning.  */
265       (void) wstandout (locator->handle);
266       waddstr (locator->handle, string);
267       wclrtoeol (locator->handle);
268       (void) wstandend (locator->handle);
269       tui_refresh_win (locator);
270       wmove (locator->handle, 0, 0);
271       xfree (string);
272       locator->content_in_use = TRUE;
273     }
274 }
275
276
277 /* Set the filename portion of the locator.  */
278 static void
279 tui_set_locator_filename (const char *filename)
280 {
281   struct tui_gen_win_info *locator = tui_locator_win_info_ptr ();
282   struct tui_locator_element *element;
283
284   if (locator->content[0] == NULL)
285     {
286       tui_set_locator_info (NULL, filename, NULL, 0, 0);
287       return;
288     }
289
290   element = &((struct tui_win_element *)
291               locator->content[0])->which_element.locator;
292   element->file_name[0] = 0;
293   strcat_to_buf (element->file_name, MAX_LOCATOR_ELEMENT_LEN, filename);
294 }
295
296 /* Update the locator, with the provided arguments.  */
297 static void
298 tui_set_locator_info (struct gdbarch *gdbarch,
299                       const char *filename,
300                       const char *procname, 
301                       int lineno,
302                       CORE_ADDR addr)
303 {
304   struct tui_gen_win_info *locator = tui_locator_win_info_ptr ();
305   struct tui_locator_element *element;
306
307   /* Allocate the locator content if necessary.  */
308   if (locator->content_size <= 0)
309     {
310       locator->content = (void **) tui_alloc_content (1, locator->type);
311       locator->content_size = 1;
312     }
313
314   element = &((struct tui_win_element *)
315               locator->content[0])->which_element.locator;
316   element->proc_name[0] = (char) 0;
317   strcat_to_buf (element->proc_name, MAX_LOCATOR_ELEMENT_LEN, procname);
318   element->line_no = lineno;
319   element->addr = addr;
320   element->gdbarch = gdbarch;
321   tui_set_locator_filename (filename);
322 }
323
324 /* Update only the filename portion of the locator.  */
325 void
326 tui_update_locator_filename (const char *filename)
327 {
328   tui_set_locator_filename (filename);
329   tui_show_locator_content ();
330 }
331
332 /* Function to print the frame information for the TUI.  */
333 void
334 tui_show_frame_info (struct frame_info *fi)
335 {
336   struct tui_win_info *win_info;
337   int i;
338
339   if (fi)
340     {
341       int start_line, i;
342       CORE_ADDR low;
343       struct tui_gen_win_info *locator = tui_locator_win_info_ptr ();
344       int source_already_displayed;
345       struct symtab_and_line sal;
346       CORE_ADDR pc;
347
348       find_frame_sal (fi, &sal);
349
350       source_already_displayed = sal.symtab != 0
351         && tui_source_is_displayed (sal.symtab->filename);
352
353       if (get_frame_pc_if_available (fi, &pc))
354         tui_set_locator_info (get_frame_arch (fi),
355                               sal.symtab == 0 ? "??" : sal.symtab->filename,
356                               tui_get_function_from_frame (fi),
357                               sal.line,
358                               pc);
359       else
360         tui_set_locator_info (get_frame_arch (fi),
361                               "??", _("<unavailable>"), sal.line, 0);
362
363       tui_show_locator_content ();
364       start_line = 0;
365       for (i = 0; i < (tui_source_windows ())->count; i++)
366         {
367           union tui_which_element *item;
368
369           win_info = (tui_source_windows ())->list[i];
370
371           item = &((struct tui_win_element *)
372                    locator->content[0])->which_element;
373           if (win_info == TUI_SRC_WIN)
374             {
375               start_line = (item->locator.line_no -
376                            (win_info->generic.viewport_height / 2)) + 1;
377               if (start_line <= 0)
378                 start_line = 1;
379             }
380           else
381             {
382               if (find_pc_partial_function (get_frame_pc (fi), (char **) NULL,
383                                             &low, (CORE_ADDR) 0) == 0)
384                 {
385                   /* There is no symbol available for current PC.  There is no
386                      safe way how to "disassemble backwards".  */
387                   low = get_frame_pc (fi);
388                 }
389               else
390                 low = tui_get_low_disassembly_address (get_frame_arch (fi),
391                                                        low, get_frame_pc (fi));
392             }
393
394           if (win_info == TUI_SRC_WIN)
395             {
396               struct tui_line_or_address l;
397
398               l.loa = LOA_LINE;
399               l.u.line_no = start_line;
400               if (!(source_already_displayed
401                     && tui_line_is_displayed (item->locator.line_no,
402                                               win_info, TRUE)))
403                 tui_update_source_window (win_info, get_frame_arch (fi),
404                                           sal.symtab, l, TRUE);
405               else
406                 {
407                   l.u.line_no = item->locator.line_no;
408                   tui_set_is_exec_point_at (l, win_info);
409                 }
410             }
411           else
412             {
413               if (win_info == TUI_DISASM_WIN)
414                 {
415                   struct tui_line_or_address a;
416
417                   a.loa = LOA_ADDRESS;
418                   a.u.addr = low;
419                   if (!tui_addr_is_displayed (item->locator.addr,
420                                               win_info, TRUE))
421                     tui_update_source_window (win_info, get_frame_arch (fi),
422                                               sal.symtab, a, TRUE);
423                   else
424                     {
425                       a.u.addr = item->locator.addr;
426                       tui_set_is_exec_point_at (a, win_info);
427                     }
428                 }
429             }
430           tui_update_exec_info (win_info);
431         }
432     }
433   else
434     {
435       tui_set_locator_info (NULL, NULL, NULL, 0, (CORE_ADDR) 0);
436       tui_show_locator_content ();
437       for (i = 0; i < (tui_source_windows ())->count; i++)
438         {
439           win_info = (tui_source_windows ())->list[i];
440           tui_clear_source_content (win_info, EMPTY_SOURCE_PROMPT);
441           tui_update_exec_info (win_info);
442         }
443     }
444 }
445
446 /* Function to initialize gdb commands, for tui window stack
447    manipulation.  */
448
449 /* Provide a prototype to silence -Wmissing-prototypes.  */
450 extern initialize_file_ftype _initialize_tui_stack;
451
452 void
453 _initialize_tui_stack (void)
454 {
455   add_com ("update", class_tui, tui_update_command,
456            _("Update the source window and locator to "
457              "display the current execution point.\n"));
458 }
459
460 /* Command to update the display with the current execution point.  */
461 static void
462 tui_update_command (char *arg, int from_tty)
463 {
464   char cmd[sizeof("frame 0")];
465
466   strcpy (cmd, "frame 0");
467   execute_command (cmd, from_tty);
468 }