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