Remove some TUI static allocations
[deliverable/binutils-gdb.git] / gdb / tui / tui-data.c
CommitLineData
f377b406 1/* TUI data manipulation routines.
f33c6cbf 2
42a4f53d 3 Copyright (C) 1998-2019 Free Software Foundation, Inc.
f33c6cbf 4
f377b406
SC
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
a9762ec7 11 the Free Software Foundation; either version 3 of the License, or
f377b406
SC
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
a9762ec7 20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c 21
96ec9981
DJ
22#include "defs.h"
23#include "symtab.h"
d7b2e967
AC
24#include "tui/tui.h"
25#include "tui/tui-data.h"
26#include "tui/tui-wingeneral.h"
6a83354a 27#include "gdb_curses.h"
4e8f7a8b 28
c906108c
SS
29/****************************
30** GLOBAL DECLARATIONS
31****************************/
7fa29be9 32struct tui_win_info *tui_win_list[MAX_MAJOR_WINDOWS];
c906108c 33
c906108c
SS
34/***************************
35** Private data
36****************************/
6ba8e26f
AC
37static enum tui_layout_type current_layout = UNDEFINED_LAYOUT;
38static int term_height, term_width;
ab313b35 39static struct tui_gen_win_info _locator (LOCATOR_WIN);
ad54d15b 40static std::vector<tui_source_window_base *> source_windows;
e65b5245 41static struct tui_win_info *win_with_focus = NULL;
08ef48c5
MS
42static struct tui_layout_def layout_def = {
43 SRC_WIN, /* DISPLAY_MODE */
7bd0be3a 44 FALSE}; /* SPLIT */
08ef48c5 45
6ba8e26f 46static int win_resized = FALSE;
c906108c
SS
47
48
49/*********************************
50** Static function forward decls
51**********************************/
08ef48c5
MS
52static void free_content (tui_win_content,
53 int,
54 enum tui_win_type);
55static void free_content_elements (tui_win_content,
56 int,
57 enum tui_win_type);
c906108c
SS
58
59
60
61/*********************************
62** PUBLIC FUNCTIONS
63**********************************/
64
6d012f14
AC
65int
66tui_win_is_auxillary (enum tui_win_type win_type)
67{
68 return (win_type > MAX_MAJOR_WINDOWS);
69}
70
c906108c
SS
71/******************************************
72** ACCESSORS & MUTATORS FOR PRIVATE DATA
73******************************************/
74
1cc6d956 75/* Answer a whether the terminal window has been resized or not. */
c906108c 76int
dd1abb8c 77tui_win_resized (void)
c906108c 78{
6ba8e26f 79 return win_resized;
dd1abb8c 80}
c906108c
SS
81
82
1cc6d956 83/* Set a whether the terminal window has been resized or not. */
c906108c 84void
dd1abb8c 85tui_set_win_resized_to (int resized)
c906108c 86{
6ba8e26f 87 win_resized = resized;
dd1abb8c 88}
c906108c
SS
89
90
1cc6d956 91/* Answer a pointer to the current layout definition. */
2a8854a7 92struct tui_layout_def *
dd1abb8c 93tui_layout_def (void)
c906108c 94{
6ba8e26f 95 return &layout_def;
dd1abb8c 96}
c906108c
SS
97
98
1cc6d956 99/* Answer the window with the logical focus. */
2a8854a7 100struct tui_win_info *
dd1abb8c 101tui_win_with_focus (void)
c906108c 102{
6ba8e26f 103 return win_with_focus;
dd1abb8c 104}
c906108c
SS
105
106
1cc6d956 107/* Set the window that has the logical focus. */
c906108c 108void
5b6fe301 109tui_set_win_with_focus (struct tui_win_info *win_info)
c906108c 110{
6ba8e26f 111 win_with_focus = win_info;
dd1abb8c 112}
c906108c
SS
113
114
6ba8e26f
AC
115/* Accessor for the current source window. Usually there is only one
116 source window (either source or disassembly), but both can be
117 displayed at the same time. */
ad54d15b 118std::vector<tui_source_window_base *> &
b4eb2452 119tui_source_windows ()
c906108c 120{
b4eb2452 121 return source_windows;
dd1abb8c 122}
c906108c
SS
123
124
dd1abb8c
AC
125/* Clear the list of source windows. Usually there is only one source
126 window (either source or disassembly), but both can be displayed at
127 the same time. */
c906108c 128void
b4eb2452 129tui_clear_source_windows ()
c906108c 130{
b4eb2452 131 source_windows.clear ();
dd1abb8c 132}
c906108c
SS
133
134
1cc6d956 135/* Clear the pertinant detail in the source windows. */
c906108c 136void
b4eb2452 137tui_clear_source_windows_detail ()
c906108c 138{
ad54d15b 139 for (tui_source_window_base *win : tui_source_windows ())
7778b912 140 win->clear_detail ();
dd1abb8c 141}
c906108c
SS
142
143
dd1abb8c
AC
144/* Add a window to the list of source windows. Usually there is only
145 one source window (either source or disassembly), but both can be
146 displayed at the same time. */
c906108c 147void
ad54d15b 148tui_add_to_source_windows (struct tui_source_window_base *win_info)
c906108c 149{
b4eb2452
TT
150 if (source_windows.size () < 2)
151 source_windows.push_back (win_info);
dd1abb8c 152}
c906108c 153
8761a91b
TT
154/* See tui-data.h. */
155
156void
5cf82909 157tui_source_window_base::clear_detail ()
8761a91b 158{
e6e41501
TT
159 gdbarch = NULL;
160 start_line_or_addr.loa = LOA_ADDRESS;
161 start_line_or_addr.u.addr = 0;
162 horizontal_offset = 0;
8761a91b
TT
163}
164
165/* See tui-data.h. */
166
167void
168tui_cmd_window::clear_detail ()
169{
cb2ce893 170 wmove (handle, 0, 0);
8761a91b
TT
171}
172
173/* See tui-data.h. */
174
175void
176tui_data_window::clear_detail ()
177{
238eb706
TT
178 data_content = NULL;
179 data_content_count = 0;
180 regs_content = NULL;
181 regs_content_count = 0;
182 regs_column_count = 1;
ceb13a13 183 display_regs = false;
8761a91b 184}
c906108c 185
dd1abb8c
AC
186/* Accessor for the locator win info. Answers a pointer to the static
187 locator win info struct. */
2a8854a7 188struct tui_gen_win_info *
dd1abb8c 189tui_locator_win_info_ptr (void)
c906108c
SS
190{
191 return &_locator;
2a8854a7 192}
c906108c
SS
193
194
6ba8e26f 195/* Accessor for the term_height. */
c906108c 196int
dd1abb8c 197tui_term_height (void)
c906108c 198{
6ba8e26f 199 return term_height;
dd1abb8c 200}
c906108c
SS
201
202
1cc6d956 203/* Mutator for the term height. */
c906108c 204void
dd1abb8c 205tui_set_term_height_to (int h)
c906108c 206{
6ba8e26f 207 term_height = h;
dd1abb8c 208}
c906108c
SS
209
210
1cc6d956 211/* Accessor for the term_width. */
c906108c 212int
dd1abb8c 213tui_term_width (void)
c906108c 214{
6ba8e26f 215 return term_width;
dd1abb8c 216}
c906108c
SS
217
218
6ba8e26f 219/* Mutator for the term_width. */
c906108c 220void
dd1abb8c 221tui_set_term_width_to (int w)
c906108c 222{
6ba8e26f 223 term_width = w;
dd1abb8c 224}
c906108c
SS
225
226
1cc6d956 227/* Accessor for the current layout. */
2a8854a7 228enum tui_layout_type
dd1abb8c 229tui_current_layout (void)
c906108c 230{
6ba8e26f 231 return current_layout;
dd1abb8c 232}
c906108c
SS
233
234
dd1abb8c 235/* Mutator for the current layout. */
c906108c 236void
6ba8e26f 237tui_set_current_layout_to (enum tui_layout_type new_layout)
c906108c 238{
6ba8e26f 239 current_layout = new_layout;
dd1abb8c 240}
c906108c
SS
241
242
c906108c
SS
243/*****************************
244** OTHER PUBLIC FUNCTIONS
245*****************************/
246
247
dd1abb8c
AC
248/* Answer the next window in the list, cycling back to the top if
249 necessary. */
2a8854a7 250struct tui_win_info *
5b6fe301 251tui_next_win (struct tui_win_info *cur_win)
c906108c 252{
cb2ce893 253 int type = cur_win->type;
e65b5245 254 struct tui_win_info *next_win = NULL;
c906108c 255
cb2ce893 256 if (cur_win->type == CMD_WIN)
c906108c
SS
257 type = SRC_WIN;
258 else
cb2ce893
TT
259 type = cur_win->type + 1;
260 while (type != cur_win->type && (next_win == NULL))
c906108c 261 {
e5908723 262 if (tui_win_list[type]
cb2ce893 263 && tui_win_list[type]->is_visible)
6ba8e26f 264 next_win = tui_win_list[type];
c906108c
SS
265 else
266 {
267 if (type == CMD_WIN)
268 type = SRC_WIN;
269 else
270 type++;
271 }
272 }
273
6ba8e26f
AC
274 return next_win;
275}
c906108c
SS
276
277
dd1abb8c
AC
278/* Answer the prev window in the list, cycling back to the bottom if
279 necessary. */
2a8854a7 280struct tui_win_info *
5b6fe301 281tui_prev_win (struct tui_win_info *cur_win)
c906108c 282{
cb2ce893 283 int type = cur_win->type;
e65b5245 284 struct tui_win_info *prev = NULL;
c906108c 285
cb2ce893 286 if (cur_win->type == SRC_WIN)
c906108c
SS
287 type = CMD_WIN;
288 else
cb2ce893
TT
289 type = cur_win->type - 1;
290 while (type != cur_win->type && (prev == NULL))
c906108c 291 {
37715c4c 292 if (tui_win_list[type]
cb2ce893 293 && tui_win_list[type]->is_visible)
6d012f14 294 prev = tui_win_list[type];
c906108c
SS
295 else
296 {
297 if (type == SRC_WIN)
298 type = CMD_WIN;
299 else
300 type--;
301 }
302 }
303
304 return prev;
cb50eddd 305}
c906108c
SS
306
307
1cc6d956 308/* Answer the window represented by name. */
2a8854a7 309struct tui_win_info *
a121b7c1 310tui_partial_win_by_name (const char *name)
c906108c 311{
e65b5245 312 struct tui_win_info *win_info = NULL;
c906108c 313
63a33118 314 if (name != NULL)
c906108c
SS
315 {
316 int i = 0;
317
6d012f14 318 while (i < MAX_MAJOR_WINDOWS && win_info == NULL)
c906108c 319 {
6d012f14 320 if (tui_win_list[i] != 0)
a4b99e53 321 {
152f3f4b 322 const char *cur_name = tui_win_list[i]->name ();
1c5313c5 323
e5908723 324 if (strlen (name) <= strlen (cur_name)
61012eef 325 && startswith (cur_name, name))
6d012f14 326 win_info = tui_win_list[i];
a4b99e53 327 }
c906108c
SS
328 i++;
329 }
330 }
331
6d012f14 332 return win_info;
6ba8e26f 333}
c906108c
SS
334
335
c906108c 336void
dd1abb8c 337tui_initialize_static_data (void)
c906108c 338{
dd1abb8c
AC
339 tui_init_generic_part (tui_locator_win_info_ptr ());
340}
c906108c
SS
341
342
c906108c 343void
5b6fe301 344tui_init_generic_part (struct tui_gen_win_info *win)
c906108c
SS
345{
346 win->width =
347 win->height =
348 win->origin.x =
349 win->origin.y =
6d012f14
AC
350 win->viewport_height =
351 win->content_size =
352 win->last_visible_line = 0;
e65b5245 353 win->handle = NULL;
22940a24 354 win->content = NULL;
56122977
TT
355 win->content_in_use = FALSE;
356 win->is_visible = false;
bc6b7f04
SC
357 win->title = 0;
358}
c906108c
SS
359
360
ef5eab5a 361/* init_content_element().
c5aa993b 362 */
2c0b251b 363static void
08ef48c5
MS
364init_content_element (struct tui_win_element *element,
365 enum tui_win_type type)
c906108c 366{
c906108c
SS
367 switch (type)
368 {
369 case SRC_WIN:
370 case DISASSEM_WIN:
e65b5245 371 element->which_element.source.line = NULL;
362c05fe
AS
372 element->which_element.source.line_or_addr.loa = LOA_LINE;
373 element->which_element.source.line_or_addr.u.line_no = 0;
6d012f14
AC
374 element->which_element.source.is_exec_point = FALSE;
375 element->which_element.source.has_break = FALSE;
c906108c
SS
376 break;
377 case DATA_WIN:
ab313b35 378 element->which_element.data_window = new struct tui_gen_win_info (DATA_ITEM_WIN);
dc2c33e4 379 element->which_element.data_window->content =
63ed8182 380 tui_alloc_content (1, DATA_ITEM_WIN);
dc2c33e4 381 element->which_element.data_window->content_size = 1;
c906108c
SS
382 break;
383 case CMD_WIN:
e65b5245 384 element->which_element.command.line = NULL;
c906108c
SS
385 break;
386 case DATA_ITEM_WIN:
e65b5245 387 element->which_element.data.name = NULL;
6d012f14
AC
388 element->which_element.data.type = TUI_REGISTER;
389 element->which_element.data.item_no = UNDEFINED_ITEM;
390 element->which_element.data.value = NULL;
391 element->which_element.data.highlight = FALSE;
e65b5245 392 element->which_element.data.content = NULL;
c906108c
SS
393 break;
394 case LOCATOR_WIN:
56d397a3 395 element->which_element.locator.full_name[0] =
6d012f14
AC
396 element->which_element.locator.proc_name[0] = (char) 0;
397 element->which_element.locator.line_no = 0;
398 element->which_element.locator.addr = 0;
c906108c
SS
399 break;
400 case EXEC_INFO_WIN:
6d012f14
AC
401 memset(element->which_element.simple_string, ' ',
402 sizeof(element->which_element.simple_string));
c906108c
SS
403 break;
404 default:
405 break;
406 }
6ba8e26f 407}
c906108c 408
33b906ab 409tui_win_info::tui_win_info (enum tui_win_type type)
cb2ce893 410 : tui_gen_win_info (type)
c906108c 411{
6ba8e26f 412}
c906108c 413
5cf82909 414tui_source_window_base::tui_source_window_base (enum tui_win_type type)
33b906ab
TT
415 : tui_win_info (type)
416{
417 gdb_assert (type == SRC_WIN || type == DISASSEM_WIN);
e6e41501
TT
418 start_line_or_addr.loa = LOA_ADDRESS;
419 start_line_or_addr.u.addr = 0;
33b906ab
TT
420}
421
2a8854a7 422struct tui_win_info *
22940a24 423tui_alloc_win_info (enum tui_win_type type)
c906108c 424{
33b906ab
TT
425 switch (type)
426 {
427 case SRC_WIN:
5cf82909
TT
428 return new tui_source_window ();
429
33b906ab 430 case DISASSEM_WIN:
5cf82909 431 return new tui_disasm_window ();
c906108c 432
33b906ab
TT
433 case DATA_WIN:
434 return new tui_data_window ();
c906108c 435
33b906ab
TT
436 case CMD_WIN:
437 return new tui_cmd_window ();
438 }
439
440 gdb_assert_not_reached (_("Unhandled window type"));
6ba8e26f 441}
c906108c
SS
442
443
6ba8e26f 444/* Allocates the content and elements in a block. */
2a8854a7 445tui_win_content
6ba8e26f 446tui_alloc_content (int num_elements, enum tui_win_type type)
c906108c 447{
c0645fb5 448 tui_win_content content;
7acd011b 449 struct tui_win_element *element_block_ptr;
c906108c
SS
450 int i;
451
8d749320 452 content = XNEWVEC (struct tui_win_element *, num_elements);
7acd011b
SM
453
454 /*
455 * All windows, except the data window, can allocate the
456 * elements in a chunk. The data window cannot because items
457 * can be added/removed from the data display by the user at any
458 * time.
459 */
460 if (type != DATA_WIN)
c0645fb5 461 {
7acd011b
SM
462 element_block_ptr = XNEWVEC (struct tui_win_element, num_elements);
463 for (i = 0; i < num_elements; i++)
c906108c 464 {
7acd011b
SM
465 content[i] = element_block_ptr;
466 init_content_element (content[i], type);
467 element_block_ptr++;
c906108c
SS
468 }
469 }
470
471 return content;
6ba8e26f 472}
c906108c
SS
473
474
dd1abb8c 475/* Adds the input number of elements to the windows's content. If no
6ba8e26f 476 content has been allocated yet, alloc_content() is called to do
dd1abb8c
AC
477 this. The index of the first element added is returned, unless
478 there is a memory allocation error, in which case, (-1) is
479 returned. */
c906108c 480int
08ef48c5
MS
481tui_add_content_elements (struct tui_gen_win_info *win_info,
482 int num_elements)
c906108c 483{
5b6fe301 484 struct tui_win_element *element_ptr;
6ba8e26f 485 int i, index_start;
c906108c 486
6d012f14 487 if (win_info->content == NULL)
c906108c 488 {
63ed8182 489 win_info->content = tui_alloc_content (num_elements, win_info->type);
6ba8e26f 490 index_start = 0;
c906108c
SS
491 }
492 else
6ba8e26f 493 index_start = win_info->content_size;
6d012f14 494 if (win_info->content != NULL)
c906108c 495 {
6ba8e26f 496 for (i = index_start; (i < num_elements + index_start); i++)
c906108c 497 {
8d749320 498 element_ptr = XNEW (struct tui_win_element);
730ead81
TT
499 win_info->content[i] = element_ptr;
500 init_content_element (element_ptr, win_info->type);
501 win_info->content_size++;
c906108c
SS
502 }
503 }
504
6ba8e26f
AC
505 return index_start;
506}
c906108c 507
f936bca2 508tui_gen_win_info::~tui_gen_win_info ()
c906108c 509{
f936bca2 510 if (handle != NULL)
ee1d42d6 511 {
f936bca2
TT
512 tui_delete_win (handle);
513 handle = NULL;
514 tui_free_win_content (this);
ee1d42d6 515 }
f936bca2
TT
516 xfree (title);
517}
518
519tui_source_window_base::~tui_source_window_base ()
520{
521 xfree (fullname);
522 delete execution_info;
ee1d42d6 523}
c906108c 524
ee1d42d6
TT
525tui_data_window::~tui_data_window ()
526{
cb2ce893 527 if (content != NULL)
c906108c 528 {
238eb706
TT
529 tui_free_data_content (regs_content, regs_content_count);
530 regs_content = NULL;
531 regs_content_count = 0;
532 tui_free_data_content (data_content, data_content_count);
533 data_content = NULL;
534 data_content_count = 0;
535 regs_column_count = 1;
ceb13a13 536 display_regs = false;
cb2ce893
TT
537 content = NULL;
538 content_size = 0;
c906108c 539 }
ee1d42d6
TT
540}
541
c906108c 542void
b4eb2452 543tui_free_all_source_wins_content ()
c906108c 544{
ad54d15b 545 for (tui_source_window_base *win_info : tui_source_windows ())
c906108c 546 {
cb2ce893 547 tui_free_win_content (win_info);
ad54d15b 548 tui_free_win_content (win_info->execution_info);
c906108c 549 }
dd1abb8c 550}
c906108c
SS
551
552
c906108c 553void
5b6fe301 554tui_free_win_content (struct tui_gen_win_info *win_info)
c906108c 555{
6d012f14 556 if (win_info->content != NULL)
c906108c 557 {
63a33118 558 free_content (win_info->content,
6d012f14
AC
559 win_info->content_size,
560 win_info->type);
561 win_info->content = NULL;
c906108c 562 }
6d012f14 563 win_info->content_size = 0;
6ba8e26f 564}
c906108c
SS
565
566
c906108c 567void
08ef48c5
MS
568tui_free_data_content (tui_win_content content,
569 int content_size)
c906108c
SS
570{
571 int i;
572
ef5eab5a
MS
573 /* Remember that data window content elements are of type struct
574 tui_gen_win_info *, each of which whose single element is a data
575 element. */
6ba8e26f 576 for (i = 0; i < content_size; i++)
c906108c 577 {
9a2b4c1b 578 struct tui_gen_win_info *generic_win
dc2c33e4 579 = content[i]->which_element.data_window;
c906108c 580
cafb3438 581 if (generic_win != NULL)
c906108c 582 {
6ba8e26f 583 tui_delete_win (generic_win->handle);
e65b5245 584 generic_win->handle = NULL;
6ba8e26f 585 tui_free_win_content (generic_win);
c906108c
SS
586 }
587 }
6ba8e26f 588 free_content (content,
08ef48c5
MS
589 content_size,
590 DATA_WIN);
6ba8e26f 591}
c906108c
SS
592
593
594/**********************************
595** LOCAL STATIC FUNCTIONS **
596**********************************/
597
598
c906108c 599static void
08ef48c5
MS
600free_content (tui_win_content content,
601 int content_size,
602 enum tui_win_type win_type)
c906108c 603{
d04b44a1 604 if (content != NULL)
c906108c 605 {
6ba8e26f 606 free_content_elements (content, content_size, win_type);
22940a24 607 xfree (content);
c906108c 608 }
6ba8e26f 609}
c906108c
SS
610
611
ef5eab5a 612/* free_content_elements().
c5aa993b 613 */
c906108c 614static void
08ef48c5
MS
615free_content_elements (tui_win_content content,
616 int content_size,
617 enum tui_win_type type)
c906108c 618{
d04b44a1 619 if (content != NULL)
c906108c
SS
620 {
621 int i;
622
62f29fda 623 if (type == DISASSEM_WIN)
c906108c 624 {
1cc6d956 625 /* Free whole source block. */
6d012f14 626 xfree (content[0]->which_element.source.line);
c906108c
SS
627 }
628 else
629 {
6ba8e26f 630 for (i = 0; i < content_size; i++)
c906108c 631 {
5b6fe301 632 struct tui_win_element *element;
c906108c
SS
633
634 element = content[i];
cafb3438 635 if (element != NULL)
c906108c
SS
636 {
637 switch (type)
638 {
62f29fda
TT
639 case SRC_WIN:
640 xfree (element->which_element.source.line);
641 break;
c906108c 642 case DATA_WIN:
ab313b35 643 delete element->which_element.data_window;
22940a24 644 xfree (element);
c906108c
SS
645 break;
646 case DATA_ITEM_WIN:
ef5eab5a
MS
647 /* Note that data elements are not allocated in
648 a single block, but individually, as
649 needed. */
6d012f14
AC
650 if (element->which_element.data.type != TUI_REGISTER)
651 xfree ((void *)element->which_element.data.name);
652 xfree (element->which_element.data.value);
10f59415 653 xfree (element->which_element.data.content);
22940a24 654 xfree (element);
c906108c
SS
655 break;
656 case CMD_WIN:
6d012f14 657 xfree (element->which_element.command.line);
c906108c
SS
658 break;
659 default:
660 break;
661 }
662 }
663 }
664 }
665 if (type != DATA_WIN && type != DATA_ITEM_WIN)
1cc6d956 666 xfree (content[0]); /* Free the element block. */
c906108c 667 }
6ba8e26f 668}
This page took 2.334949 seconds and 4 git commands to generate.