1edf40b71b3613dc08dc9ebf60447745bfcbdf41
[external/binutils.git] / gdb / tui / tui-data.c
1 /* TUI data manipulation routines.
2
3    Copyright (C) 1998-2019 Free Software Foundation, Inc.
4
5    Contributed by Hewlett-Packard Company.
6
7    This file is part of GDB.
8
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 3 of the License, or
12    (at your option) any later version.
13
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18
19    You should have received a copy of the GNU General Public License
20    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
21
22 #include "defs.h"
23 #include "symtab.h"
24 #include "tui/tui.h"
25 #include "tui/tui-data.h"
26 #include "tui/tui-wingeneral.h"
27 #include "gdb_curses.h"
28
29 /****************************
30 ** GLOBAL DECLARATIONS
31 ****************************/
32 struct tui_win_info *tui_win_list[MAX_MAJOR_WINDOWS];
33
34 /***************************
35 ** Private data
36 ****************************/
37 static enum tui_layout_type current_layout = UNDEFINED_LAYOUT;
38 static int term_height, term_width;
39 static struct tui_gen_win_info _locator;
40 static struct tui_gen_win_info exec_info[2];
41 static std::vector<tui_win_info *> source_windows;
42 static struct tui_win_info *win_with_focus = NULL;
43 static struct tui_layout_def layout_def = {
44   SRC_WIN,                      /* DISPLAY_MODE */
45   FALSE};                       /* SPLIT */
46
47 static int win_resized = FALSE;
48
49
50 /*********************************
51 ** Static function forward decls
52 **********************************/
53 static void free_content (tui_win_content, 
54                           int, 
55                           enum tui_win_type);
56 static void free_content_elements (tui_win_content, 
57                                    int, 
58                                    enum tui_win_type);
59
60
61
62 /*********************************
63 ** PUBLIC FUNCTIONS
64 **********************************/
65
66 int
67 tui_win_is_source_type (enum tui_win_type win_type)
68 {
69   return (win_type == SRC_WIN || win_type == DISASSEM_WIN);
70 }
71
72 int
73 tui_win_is_auxillary (enum tui_win_type win_type)
74 {
75   return (win_type > MAX_MAJOR_WINDOWS);
76 }
77
78 int
79 tui_win_has_locator (struct tui_win_info *win_info)
80 {
81   return (win_info != NULL 
82           && win_info->detail.source_info.has_locator);
83 }
84
85 void
86 tui_set_win_highlight (struct tui_win_info *win_info, 
87                        int highlight)
88 {
89   if (win_info != NULL)
90     win_info->is_highlighted = highlight;
91 }
92
93 /******************************************
94 ** ACCESSORS & MUTATORS FOR PRIVATE DATA
95 ******************************************/
96
97 /* Answer a whether the terminal window has been resized or not.  */
98 int
99 tui_win_resized (void)
100 {
101   return win_resized;
102 }
103
104
105 /* Set a whether the terminal window has been resized or not.  */
106 void
107 tui_set_win_resized_to (int resized)
108 {
109   win_resized = resized;
110 }
111
112
113 /* Answer a pointer to the current layout definition.  */
114 struct tui_layout_def *
115 tui_layout_def (void)
116 {
117   return &layout_def;
118 }
119
120
121 /* Answer the window with the logical focus.  */
122 struct tui_win_info *
123 tui_win_with_focus (void)
124 {
125   return win_with_focus;
126 }
127
128
129 /* Set the window that has the logical focus.  */
130 void
131 tui_set_win_with_focus (struct tui_win_info *win_info)
132 {
133   win_with_focus = win_info;
134 }
135
136
137 /* Accessor for the current source window.  Usually there is only one
138    source window (either source or disassembly), but both can be
139    displayed at the same time.  */
140 std::vector<tui_win_info *> &
141 tui_source_windows ()
142 {
143   return source_windows;
144 }
145
146
147 /* Clear the list of source windows.  Usually there is only one source
148    window (either source or disassembly), but both can be displayed at
149    the same time.  */
150 void
151 tui_clear_source_windows ()
152 {
153   source_windows.clear ();
154 }
155
156
157 /* Clear the pertinant detail in the source windows.  */
158 void
159 tui_clear_source_windows_detail ()
160 {
161   for (tui_win_info *win : tui_source_windows ())
162     tui_clear_win_detail (win);
163 }
164
165
166 /* Add a window to the list of source windows.  Usually there is only
167    one source window (either source or disassembly), but both can be
168    displayed at the same time.  */
169 void
170 tui_add_to_source_windows (struct tui_win_info *win_info)
171 {
172   if (source_windows.size () < 2)
173     source_windows.push_back (win_info);
174 }
175
176 /* See tui-data.h.  */
177
178 void
179 tui_source_window_base::clear_detail ()
180 {
181   detail.source_info.gdbarch = NULL;
182   detail.source_info.start_line_or_addr.loa = LOA_ADDRESS;
183   detail.source_info.start_line_or_addr.u.addr = 0;
184   detail.source_info.horizontal_offset = 0;
185 }
186
187 /* See tui-data.h.  */
188
189 void
190 tui_cmd_window::clear_detail ()
191 {
192   wmove (generic.handle, 0, 0);
193 }
194
195 /* See tui-data.h.  */
196
197 void
198 tui_data_window::clear_detail ()
199 {
200   detail.data_display_info.data_content = NULL;
201   detail.data_display_info.data_content_count = 0;
202   detail.data_display_info.regs_content = NULL;
203   detail.data_display_info.regs_content_count = 0;
204   detail.data_display_info.regs_column_count = 1;
205   detail.data_display_info.display_regs = FALSE;
206 }
207
208 /* Clear the pertinant detail in the windows.  */
209 void
210 tui_clear_win_detail (struct tui_win_info *win_info)
211 {
212   if (win_info != NULL)
213     win_info->clear_detail ();
214 }
215
216
217 /* Accessor for the source execution info ptr.  */
218 struct tui_gen_win_info *
219 tui_source_exec_info_win_ptr (void)
220 {
221   return &exec_info[0];
222 }
223
224
225 /* Accessor for the disassem execution info ptr.  */
226 struct tui_gen_win_info *
227 tui_disassem_exec_info_win_ptr (void)
228 {
229   return &exec_info[1];
230 }
231
232
233 /* Accessor for the locator win info.  Answers a pointer to the static
234    locator win info struct.  */
235 struct tui_gen_win_info *
236 tui_locator_win_info_ptr (void)
237 {
238   return &_locator;
239 }
240
241
242 /* Accessor for the term_height.  */
243 int
244 tui_term_height (void)
245 {
246   return term_height;
247 }
248
249
250 /* Mutator for the term height.  */
251 void
252 tui_set_term_height_to (int h)
253 {
254   term_height = h;
255 }
256
257
258 /* Accessor for the term_width.  */
259 int
260 tui_term_width (void)
261 {
262   return term_width;
263 }
264
265
266 /* Mutator for the term_width.  */
267 void
268 tui_set_term_width_to (int w)
269 {
270   term_width = w;
271 }
272
273
274 /* Accessor for the current layout.  */
275 enum tui_layout_type
276 tui_current_layout (void)
277 {
278   return current_layout;
279 }
280
281
282 /* Mutator for the current layout.  */
283 void
284 tui_set_current_layout_to (enum tui_layout_type new_layout)
285 {
286   current_layout = new_layout;
287 }
288
289
290 /*****************************
291 ** OTHER PUBLIC FUNCTIONS
292 *****************************/
293
294
295 /* Answer the next window in the list, cycling back to the top if
296    necessary.  */
297 struct tui_win_info *
298 tui_next_win (struct tui_win_info *cur_win)
299 {
300   int type = cur_win->generic.type;
301   struct tui_win_info *next_win = NULL;
302
303   if (cur_win->generic.type == CMD_WIN)
304     type = SRC_WIN;
305   else
306     type = cur_win->generic.type + 1;
307   while (type != cur_win->generic.type && (next_win == NULL))
308     {
309       if (tui_win_list[type]
310           && tui_win_list[type]->generic.is_visible)
311         next_win = tui_win_list[type];
312       else
313         {
314           if (type == CMD_WIN)
315             type = SRC_WIN;
316           else
317             type++;
318         }
319     }
320
321   return next_win;
322 }
323
324
325 /* Answer the prev window in the list, cycling back to the bottom if
326    necessary.  */
327 struct tui_win_info *
328 tui_prev_win (struct tui_win_info *cur_win)
329 {
330   int type = cur_win->generic.type;
331   struct tui_win_info *prev = NULL;
332
333   if (cur_win->generic.type == SRC_WIN)
334     type = CMD_WIN;
335   else
336     type = cur_win->generic.type - 1;
337   while (type != cur_win->generic.type && (prev == NULL))
338     {
339       if (tui_win_list[type]
340           && tui_win_list[type]->generic.is_visible)
341         prev = tui_win_list[type];
342       else
343         {
344           if (type == SRC_WIN)
345             type = CMD_WIN;
346           else
347             type--;
348         }
349     }
350
351   return prev;
352 }
353
354
355 /* Answer the window represented by name.  */
356 struct tui_win_info *
357 tui_partial_win_by_name (const char *name)
358 {
359   struct tui_win_info *win_info = NULL;
360
361   if (name != NULL)
362     {
363       int i = 0;
364
365       while (i < MAX_MAJOR_WINDOWS && win_info == NULL)
366         {
367           if (tui_win_list[i] != 0)
368             {
369               const char *cur_name =
370                 tui_win_name (&tui_win_list[i]->generic);
371
372               if (strlen (name) <= strlen (cur_name)
373                   && startswith (cur_name, name))
374                 win_info = tui_win_list[i];
375             }
376           i++;
377         }
378     }
379
380   return win_info;
381 }
382
383
384 /* Answer the name of the window.  */
385 const char *
386 tui_win_name (const struct tui_gen_win_info *win_info)
387 {
388   const char *name = NULL;
389
390   switch (win_info->type)
391     {
392     case SRC_WIN:
393       name = SRC_NAME;
394       break;
395     case CMD_WIN:
396       name = CMD_NAME;
397       break;
398     case DISASSEM_WIN:
399       name = DISASSEM_NAME;
400       break;
401     case DATA_WIN:
402       name = DATA_NAME;
403       break;
404     default:
405       name = "";
406       break;
407     }
408
409   return name;
410 }
411
412
413 void
414 tui_initialize_static_data (void)
415 {
416   tui_init_generic_part (tui_source_exec_info_win_ptr ());
417   tui_init_generic_part (tui_disassem_exec_info_win_ptr ());
418   tui_init_generic_part (tui_locator_win_info_ptr ());
419 }
420
421
422 struct tui_gen_win_info *
423 tui_alloc_generic_win_info (void)
424 {
425   struct tui_gen_win_info *win = XNEW (struct tui_gen_win_info);
426
427   tui_init_generic_part (win);
428
429   return win;
430 }
431
432
433 void
434 tui_init_generic_part (struct tui_gen_win_info *win)
435 {
436   win->width =
437     win->height =
438     win->origin.x =
439     win->origin.y =
440     win->viewport_height =
441     win->content_size =
442     win->last_visible_line = 0;
443   win->handle = NULL;
444   win->content = NULL;
445   win->content_in_use =
446     win->is_visible = FALSE;
447   win->title = 0;
448 }
449
450
451 /* init_content_element().
452  */
453 static void
454 init_content_element (struct tui_win_element *element, 
455                       enum tui_win_type type)
456 {
457   switch (type)
458     {
459     case SRC_WIN:
460     case DISASSEM_WIN:
461       element->which_element.source.line = NULL;
462       element->which_element.source.line_or_addr.loa = LOA_LINE;
463       element->which_element.source.line_or_addr.u.line_no = 0;
464       element->which_element.source.is_exec_point = FALSE;
465       element->which_element.source.has_break = FALSE;
466       break;
467     case DATA_WIN:
468       tui_init_generic_part (&element->which_element.data_window);
469       element->which_element.data_window.type = DATA_ITEM_WIN;
470       element->which_element.data_window.content =
471         tui_alloc_content (1, DATA_ITEM_WIN);
472       element->which_element.data_window.content_size = 1;
473       break;
474     case CMD_WIN:
475       element->which_element.command.line = NULL;
476       break;
477     case DATA_ITEM_WIN:
478       element->which_element.data.name = NULL;
479       element->which_element.data.type = TUI_REGISTER;
480       element->which_element.data.item_no = UNDEFINED_ITEM;
481       element->which_element.data.value = NULL;
482       element->which_element.data.highlight = FALSE;
483       element->which_element.data.content = NULL;
484       break;
485     case LOCATOR_WIN:
486       element->which_element.locator.full_name[0] =
487         element->which_element.locator.proc_name[0] = (char) 0;
488       element->which_element.locator.line_no = 0;
489       element->which_element.locator.addr = 0;
490       break;
491     case EXEC_INFO_WIN:
492       memset(element->which_element.simple_string, ' ',
493              sizeof(element->which_element.simple_string));
494       break;
495     default:
496       break;
497     }
498 }
499
500 tui_win_info::tui_win_info (enum tui_win_type type)
501 {
502   generic.type = type;
503   tui_init_generic_part (&generic);
504 }
505
506 tui_source_window_base::tui_source_window_base (enum tui_win_type type)
507   : tui_win_info (type)
508 {
509   gdb_assert (type == SRC_WIN || type == DISASSEM_WIN);
510   detail.source_info.execution_info = NULL;
511   detail.source_info.has_locator = FALSE;
512   detail.source_info.horizontal_offset = 0;
513   detail.source_info.gdbarch = NULL;
514   detail.source_info.start_line_or_addr.loa = LOA_ADDRESS;
515   detail.source_info.start_line_or_addr.u.addr = 0;
516   detail.source_info.fullname = NULL;
517 }
518
519 tui_data_window::tui_data_window ()
520   : tui_win_info (DATA_WIN)
521 {
522   detail.data_display_info.data_content = (tui_win_content) NULL;
523   detail.data_display_info.data_content_count = 0;
524   detail.data_display_info.regs_content = (tui_win_content) NULL;
525   detail.data_display_info.regs_content_count = 0;
526   detail.data_display_info.regs_column_count = 1;
527   detail.data_display_info.display_regs = FALSE;
528   detail.data_display_info.current_group = 0;
529 }
530
531 tui_cmd_window::tui_cmd_window ()
532   : tui_win_info (CMD_WIN)
533 {
534 }
535
536 struct tui_win_info *
537 tui_alloc_win_info (enum tui_win_type type)
538 {
539   switch (type)
540     {
541     case SRC_WIN:
542       return new tui_source_window ();
543
544     case DISASSEM_WIN:
545       return new tui_disasm_window ();
546
547     case DATA_WIN:
548       return new tui_data_window ();
549
550     case CMD_WIN:
551       return new tui_cmd_window ();
552     }
553
554   gdb_assert_not_reached (_("Unhandled window type"));
555 }
556
557
558 /* Allocates the content and elements in a block.  */
559 tui_win_content
560 tui_alloc_content (int num_elements, enum tui_win_type type)
561 {
562   tui_win_content content;
563   struct tui_win_element *element_block_ptr;
564   int i;
565
566   content = XNEWVEC (struct tui_win_element *, num_elements);
567
568   /*
569    * All windows, except the data window, can allocate the
570    * elements in a chunk.  The data window cannot because items
571    * can be added/removed from the data display by the user at any
572    * time.
573    */
574   if (type != DATA_WIN)
575     {
576       element_block_ptr = XNEWVEC (struct tui_win_element, num_elements);
577       for (i = 0; i < num_elements; i++)
578         {
579           content[i] = element_block_ptr;
580           init_content_element (content[i], type);
581           element_block_ptr++;
582         }
583     }
584
585   return content;
586 }
587
588
589 /* Adds the input number of elements to the windows's content.  If no
590    content has been allocated yet, alloc_content() is called to do
591    this.  The index of the first element added is returned, unless
592    there is a memory allocation error, in which case, (-1) is
593    returned.  */
594 int
595 tui_add_content_elements (struct tui_gen_win_info *win_info, 
596                           int num_elements)
597 {
598   struct tui_win_element *element_ptr;
599   int i, index_start;
600
601   if (win_info->content == NULL)
602     {
603       win_info->content = tui_alloc_content (num_elements, win_info->type);
604       index_start = 0;
605     }
606   else
607     index_start = win_info->content_size;
608   if (win_info->content != NULL)
609     {
610       for (i = index_start; (i < num_elements + index_start); i++)
611         {
612           element_ptr = XNEW (struct tui_win_element);
613           win_info->content[i] = element_ptr;
614           init_content_element (element_ptr, win_info->type);
615           win_info->content_size++;
616         }
617     }
618
619   return index_start;
620 }
621
622 tui_source_window_base::~tui_source_window_base ()
623 {
624   if (detail.source_info.fullname)
625     {
626       xfree (detail.source_info.fullname);
627       detail.source_info.fullname = NULL;
628     }
629   struct tui_gen_win_info *generic_win = detail.source_info.execution_info;
630   if (generic_win != NULL)
631     {
632       tui_delete_win (generic_win->handle);
633       generic_win->handle = NULL;
634       tui_free_win_content (generic_win);
635     }
636 }  
637
638 tui_data_window::~tui_data_window ()
639 {
640   if (generic.content != NULL)
641     {
642       tui_free_data_content (detail.data_display_info.regs_content,
643                              detail.data_display_info.regs_content_count);
644       detail.data_display_info.regs_content = NULL;
645       detail.data_display_info.regs_content_count = 0;
646       tui_free_data_content (detail.data_display_info.data_content,
647                              detail.data_display_info.data_content_count);
648       detail.data_display_info.data_content = NULL;
649       detail.data_display_info.data_content_count = 0;
650       detail.data_display_info.regs_column_count = 1;
651       detail.data_display_info.display_regs = FALSE;
652       generic.content = NULL;
653       generic.content_size = 0;
654     }
655 }  
656
657 tui_win_info::~tui_win_info ()
658 {
659   if (generic.handle != NULL)
660     {
661       tui_delete_win (generic.handle);
662       generic.handle = NULL;
663       tui_free_win_content (&generic);
664     }
665   if (generic.title)
666     xfree (generic.title);
667 }
668
669
670 void
671 tui_free_all_source_wins_content ()
672 {
673   for (tui_win_info *win_info : tui_source_windows ())
674     {
675       tui_free_win_content (&(win_info->generic));
676       tui_free_win_content (win_info->detail.source_info.execution_info);
677     }
678 }
679
680
681 void
682 tui_free_win_content (struct tui_gen_win_info *win_info)
683 {
684   if (win_info->content != NULL)
685     {
686       free_content (win_info->content,
687                    win_info->content_size,
688                    win_info->type);
689       win_info->content = NULL;
690     }
691   win_info->content_size = 0;
692 }
693
694
695 void
696 tui_free_data_content (tui_win_content content, 
697                        int content_size)
698 {
699   int i;
700
701   /* Remember that data window content elements are of type struct
702      tui_gen_win_info *, each of which whose single element is a data
703      element.  */
704   for (i = 0; i < content_size; i++)
705     {
706       struct tui_gen_win_info *generic_win
707         = &content[i]->which_element.data_window;
708
709       if (generic_win != NULL)
710         {
711           tui_delete_win (generic_win->handle);
712           generic_win->handle = NULL;
713           tui_free_win_content (generic_win);
714         }
715     }
716   free_content (content,
717                 content_size,
718                 DATA_WIN);
719 }
720
721
722 /**********************************
723 ** LOCAL STATIC FUNCTIONS        **
724 **********************************/
725
726
727 static void
728 free_content (tui_win_content content, 
729               int content_size, 
730               enum tui_win_type win_type)
731 {
732   if (content != NULL)
733     {
734       free_content_elements (content, content_size, win_type);
735       xfree (content);
736     }
737 }
738
739
740 /* free_content_elements().
741  */
742 static void
743 free_content_elements (tui_win_content content, 
744                        int content_size, 
745                        enum tui_win_type type)
746 {
747   if (content != NULL)
748     {
749       int i;
750
751       if (type == DISASSEM_WIN)
752         {
753           /* Free whole source block.  */
754           xfree (content[0]->which_element.source.line);
755         }
756       else
757         {
758           for (i = 0; i < content_size; i++)
759             {
760               struct tui_win_element *element;
761
762               element = content[i];
763               if (element != NULL)
764                 {
765                   switch (type)
766                     {
767                     case SRC_WIN:
768                       xfree (element->which_element.source.line);
769                       break;
770                     case DATA_WIN:
771                       xfree (element);
772                       break;
773                     case DATA_ITEM_WIN:
774                       /* Note that data elements are not allocated in
775                          a single block, but individually, as
776                          needed.  */
777                       if (element->which_element.data.type != TUI_REGISTER)
778                         xfree ((void *)element->which_element.data.name);
779                       xfree (element->which_element.data.value);
780                       xfree (element->which_element.data.content);
781                       xfree (element);
782                       break;
783                     case CMD_WIN:
784                       xfree (element->which_element.command.line);
785                       break;
786                     default:
787                       break;
788                     }
789                 }
790             }
791         }
792       if (type != DATA_WIN && type != DATA_ITEM_WIN)
793         xfree (content[0]);     /* Free the element block.  */
794     }
795 }