Introduce tui_source_window_base::location_matches_p method
[platform/upstream/binutils.git] / gdb / tui / tui-data.h
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 #ifndef TUI_TUI_DATA_H
23 #define TUI_TUI_DATA_H
24
25 #include "tui/tui.h"    /* For enum tui_win_type.  */
26 #include "gdb_curses.h" /* For WINDOW.  */
27 #include "observable.h"
28
29 /* This is a point definition.  */
30 struct tui_point
31 {
32   int x, y;
33 };
34
35 /* Generic window information.  */
36 struct tui_gen_win_info
37 {
38 protected:
39
40   explicit tui_gen_win_info (enum tui_win_type t)
41     : type (t)
42   {
43   }
44
45 public:
46
47   virtual ~tui_gen_win_info ();
48
49   /* Call to refresh this window.  */
50   virtual void refresh_window ();
51
52   /* Make this window visible or invisible.  */
53   virtual void make_visible (bool visible);
54
55   /* Return the name of this type of window.  */
56   virtual const char *name () const
57   {
58     return "";
59   }
60
61   /* Reset this window.  WIN_TYPE must match the existing type of this
62      window (it is only passed for self-test purposes).  The other
63      parameters are used to set the window's size and position.  */
64   void reset (enum tui_win_type win_type,
65               int height, int width,
66               int origin_x, int origin_y);
67
68   /* Window handle.  */
69   WINDOW *handle = nullptr;
70   /* Type of window.  */
71   enum tui_win_type type;
72   /* Window width.  */
73   int width = 0;
74   /* Window height.  */
75   int height = 0;
76   /* Origin of window.  */
77   struct tui_point origin = {0, 0};
78   /* Can it be used, or is it already used?  */
79   int content_in_use = FALSE;
80   /* Viewport height.  */
81   int viewport_height = 0;
82   /* Index of last visible line.  */
83   int last_visible_line = 0;
84   /* Whether the window is visible or not.  */
85   bool is_visible = false;
86   /* Window title to display.  */
87   char *title = nullptr;
88 };
89
90 /* Whether or not a window should be drawn with a box.  */
91 enum tui_box
92 {
93   DONT_BOX_WINDOW = 0,
94   BOX_WINDOW
95 };
96
97 /* Constant definitions.  */
98 #define DEFAULT_TAB_LEN         8
99 #define NO_SRC_STRING           "[ No Source Available ]"
100 #define NO_DISASSEM_STRING      "[ No Assembly Available ]"
101 #define NO_REGS_STRING          "[ Register Values Unavailable ]"
102 #define NO_DATA_STRING          "[ No Data Values Displayed ]"
103 #define MAX_CONTENT_COUNT       100
104 #define SRC_NAME                "src"
105 #define CMD_NAME                "cmd"
106 #define DATA_NAME               "regs"
107 #define DISASSEM_NAME           "asm"
108 #define TUI_NULL_STR            ""
109 #define DEFAULT_HISTORY_COUNT   25
110 #define HILITE                  TRUE
111 #define NO_HILITE               FALSE
112 #define WITH_LOCATOR            TRUE
113 #define NO_LOCATOR              FALSE
114 #define EMPTY_SOURCE_PROMPT     TRUE
115 #define NO_EMPTY_SOURCE_PROMPT  FALSE
116 #define UNDEFINED_ITEM          -1
117 #define MIN_WIN_HEIGHT          3
118 #define MIN_CMD_WIN_HEIGHT      3
119
120 /* Strings to display in the TUI status line.  */
121 #define PROC_PREFIX             "In: "
122 #define LINE_PREFIX             "L"
123 #define PC_PREFIX               "PC: "
124 #define SINGLE_KEY              "(SingleKey)"
125
126 /* Minimum/Maximum length of some fields displayed in the TUI status
127    line.  */
128 #define MIN_LINE_WIDTH     4    /* Use at least 4 digits for line
129                                    numbers.  */
130 #define MIN_PROC_WIDTH    12
131 #define MAX_TARGET_WIDTH  10
132 #define MAX_PID_WIDTH     19
133
134 /* The kinds of layouts available.  */
135 enum tui_layout_type
136 {
137   SRC_COMMAND,
138   DISASSEM_COMMAND,
139   SRC_DISASSEM_COMMAND,
140   SRC_DATA_COMMAND,
141   DISASSEM_DATA_COMMAND,
142   UNDEFINED_LAYOUT
143 };
144
145 enum tui_line_or_address_kind
146 {
147   LOA_LINE,
148   LOA_ADDRESS
149 };
150
151 /* Structure describing source line or line address.  */
152 struct tui_line_or_address
153 {
154   enum tui_line_or_address_kind loa;
155   union
156     {
157       int line_no;
158       CORE_ADDR addr;
159     } u;
160 };
161
162 /* Current Layout definition.  */
163 struct tui_layout_def
164 {
165   enum tui_win_type display_mode;
166 };
167
168 /* Flags to tell what kind of breakpoint is at current line.  */
169 enum tui_bp_flag
170 {
171   TUI_BP_ENABLED = 0x01,
172   TUI_BP_DISABLED = 0x02,
173   TUI_BP_HIT = 0x04,
174   TUI_BP_CONDITIONAL = 0x08,
175   TUI_BP_HARDWARE = 0x10
176 };
177
178 DEF_ENUM_FLAGS_TYPE (enum tui_bp_flag, tui_bp_flags);
179
180 /* Elements in the Source/Disassembly Window.  */
181 struct tui_source_element
182 {
183   tui_source_element ()
184   {
185     line_or_addr.loa = LOA_LINE;
186     line_or_addr.u.line_no = 0;
187   }
188
189   ~tui_source_element ()
190   {
191     xfree (line);
192   }
193
194   char *line = nullptr;
195   struct tui_line_or_address line_or_addr;
196   bool is_exec_point = false;
197   tui_bp_flags break_mode = 0;
198 };
199
200
201 #ifdef PATH_MAX
202 # define MAX_LOCATOR_ELEMENT_LEN        PATH_MAX
203 #else
204 # define MAX_LOCATOR_ELEMENT_LEN        1024
205 #endif
206
207 /* Position of breakpoint markers in the exec info string.  */
208 #define TUI_BP_HIT_POS      0
209 #define TUI_BP_BREAK_POS    1
210 #define TUI_EXEC_POS        2
211 #define TUI_EXECINFO_SIZE   4
212
213 typedef char tui_exec_info_content[TUI_EXECINFO_SIZE];
214
215 /* Execution info window class.  */
216
217 struct tui_exec_info_window : public tui_gen_win_info
218 {
219   tui_exec_info_window ()
220     : tui_gen_win_info (EXEC_INFO_WIN)
221   {
222   }
223
224   ~tui_exec_info_window () override
225   {
226     xfree (m_content);
227   }
228
229   /* Get or allocate contents.  */
230   tui_exec_info_content *maybe_allocate_content (int n_elements);
231
232   /* Return the contents.  */
233   const tui_exec_info_content *get_content () const
234   {
235     return m_content;
236   }
237
238 private:
239
240   tui_exec_info_content *m_content = nullptr;
241 };
242
243 /* Locator window class.  */
244
245 struct tui_locator_window : public tui_gen_win_info
246 {
247   tui_locator_window ()
248     : tui_gen_win_info (LOCATOR_WIN)
249   {
250     full_name[0] = 0;
251     proc_name[0] = 0;
252   }
253
254   char full_name[MAX_LOCATOR_ELEMENT_LEN];
255   char proc_name[MAX_LOCATOR_ELEMENT_LEN];
256   int line_no = 0;
257   CORE_ADDR addr = 0;
258   /* Architecture associated with code at this location.  */
259   struct gdbarch *gdbarch = nullptr;
260 };
261
262 /* A data item window.  */
263
264 struct tui_data_item_window : public tui_gen_win_info
265 {
266   tui_data_item_window ()
267     : tui_gen_win_info (DATA_ITEM_WIN)
268   {
269   }
270
271   ~tui_data_item_window () override;
272
273   const char *name = nullptr;
274   /* The register number, or data display number.  */
275   int item_no = UNDEFINED_ITEM;
276   void *value = nullptr;
277   bool highlight = false;
278   char *content = nullptr;
279 };
280
281 /* This defines information about each logical window.  */
282 struct tui_win_info : public tui_gen_win_info
283 {
284 protected:
285
286   explicit tui_win_info (enum tui_win_type type);
287   DISABLE_COPY_AND_ASSIGN (tui_win_info);
288
289   /* Scroll the contents vertically.  This is only called via
290      forward_scroll and backward_scroll.  */
291   virtual void do_scroll_vertical (int num_to_scroll) = 0;
292
293   /* Scroll the contents horizontally.  This is only called via
294      left_scroll and right_scroll.  */
295   virtual void do_scroll_horizontal (int num_to_scroll) = 0;
296
297   /* Called after make_visible_with_new_height sets the new height.
298      Should update the window.  */
299   virtual void do_make_visible_with_new_height () = 0;
300
301 public:
302
303   ~tui_win_info () override
304   {
305   }
306
307   /* Clear the pertinent detail in the window.  */
308   virtual void clear_detail () = 0;
309
310   /* Return true if this window has the locator.  */
311   virtual bool has_locator () const
312   {
313     return false;
314   }
315
316   /* Refresh this window and any associated windows.  */
317   virtual void refresh ();
318
319   /* Called after all the TUI windows are refreshed, to let this
320      window have a chance to update itself further.  */
321   virtual void refresh_all ()
322   {
323   }
324
325   /* Called after a TUI window is given a new height; this updates any
326      related auxiliary windows.  */
327   virtual void set_new_height (int height)
328   {
329   }
330
331   /* Compute the maximum height of this window.  */
332   virtual int max_height () const;
333
334   /* Called after the tab width has been changed.  */
335   virtual void update_tab_width ()
336   {
337   }
338
339   /* Make the window visible after the height has been changed.  */
340   void make_visible_with_new_height ();
341
342   /* Set whether this window is highglighted.  */
343   void set_highlight (bool highlight)
344   {
345     is_highlighted = highlight;
346   }
347
348   /* Methods to scroll the contents of this window.  Note that they
349      are named with "_scroll" coming at the end because the more
350      obvious "scroll_forward" is defined as a macro in term.h.  */
351   void forward_scroll (int num_to_scroll);
352   void backward_scroll (int num_to_scroll);
353   void left_scroll (int num_to_scroll);
354   void right_scroll (int num_to_scroll);
355
356   /* Return true if this window can be scrolled, false otherwise.  */
357   virtual bool can_scroll () const
358   {
359     return true;
360   }
361
362   /* Can this window ever be highlighted?  */
363   bool can_highlight = true;
364
365   /* Is this window highlighted?  */
366   bool is_highlighted = false;
367 };
368
369 /* The base class for all source-like windows, namely the source and
370    disassembly windows.  */
371
372 struct tui_source_window_base : public tui_win_info
373 {
374 protected:
375   explicit tui_source_window_base (enum tui_win_type type);
376   ~tui_source_window_base () override;
377   DISABLE_COPY_AND_ASSIGN (tui_source_window_base);
378
379   void do_scroll_horizontal (int num_to_scroll) override;
380   void do_make_visible_with_new_height () override;
381
382 public:
383
384   void clear_detail () override;
385
386   /* Return true if this window has the locator.  */
387   bool has_locator () const override
388   {
389     return m_has_locator;
390   }
391
392   void make_visible (bool visible) override;
393   void refresh () override;
394   void refresh_all () override;
395
396   /* Refill the source window's source cache and update it.  If this
397      is a disassembly window, then just update it.  */
398   void refill ();
399
400   /* Set the location of the execution point.  */
401   void set_is_exec_point_at (struct tui_line_or_address l);
402
403   void set_new_height (int height) override;
404
405   void update_tab_width () override;
406
407   /* Return true if the location LOC corresponds to the line number
408      LINE_NO in this source window; false otherwise.  */
409   virtual bool location_matches_p (struct bp_location *loc, int line_no) = 0;
410
411   /* Does the locator belong to this window?  */
412   bool m_has_locator = false;
413   /* Execution information window.  */
414   struct tui_exec_info_window *execution_info = nullptr;
415   /* Used for horizontal scroll.  */
416   int horizontal_offset = 0;
417   struct tui_line_or_address start_line_or_addr;
418
419   /* It is the resolved form as returned by symtab_to_fullname.  */
420   char *fullname = nullptr;
421
422   /* Architecture associated with code at this location.  */
423   struct gdbarch *gdbarch = nullptr;
424
425   std::vector<tui_source_element> content;
426 };
427
428 /* A TUI source window.  */
429
430 struct tui_source_window : public tui_source_window_base
431 {
432   tui_source_window ();
433   ~tui_source_window ();
434
435   DISABLE_COPY_AND_ASSIGN (tui_source_window);
436
437   const char *name () const override
438   {
439     return SRC_NAME;
440   }
441
442   bool location_matches_p (struct bp_location *loc, int line_no) override;
443
444 protected:
445
446   void do_scroll_vertical (int num_to_scroll) override;
447
448 private:
449
450   void style_changed ();
451
452   /* A token used to register and unregister an observer.  */
453   gdb::observers::token m_observable;
454 };
455
456 /* A TUI disassembly window.  */
457
458 struct tui_disasm_window : public tui_source_window_base
459 {
460   tui_disasm_window ()
461     : tui_source_window_base (DISASSEM_WIN)
462   {
463   }
464
465   DISABLE_COPY_AND_ASSIGN (tui_disasm_window);
466
467   const char *name () const override
468   {
469     return DISASSEM_NAME;
470   }
471
472   bool location_matches_p (struct bp_location *loc, int line_no) override;
473
474 protected:
475
476   void do_scroll_vertical (int num_to_scroll) override;
477 };
478
479 struct tui_data_window : public tui_win_info
480 {
481   tui_data_window ()
482     : tui_win_info (DATA_WIN)
483   {
484   }
485
486   DISABLE_COPY_AND_ASSIGN (tui_data_window);
487
488   void clear_detail () override;
489   void refresh_all () override;
490
491   void set_new_height (int height) override;
492
493   void refresh_window () override;
494
495   const char *name () const override
496   {
497     return DATA_NAME;
498   }
499
500   /* Windows that are used to display registers.  */
501   std::vector<std::unique_ptr<tui_data_item_window>> regs_content;
502   int regs_column_count = 0;
503   /* Should regs be displayed at all?  */
504   bool display_regs = false;
505   struct reggroup *current_group = nullptr;
506
507   /* Answer the number of the last line in the regs display.  If there
508      are no registers (-1) is returned.  */
509   int last_regs_line_no () const;
510
511   /* Answer the line number that the register element at element_no is
512      on.  If element_no is greater than the number of register
513      elements there are, -1 is returned.  */
514   int line_from_reg_element_no (int element_no) const;
515
516   /* Answer the index of the first element in line_no.  If line_no is
517      past the register area (-1) is returned.  */
518   int first_reg_element_no_inline (int line_no) const;
519
520   /* Displays the data that is in the data window's content.  It does
521      not set the content.  */
522   void display_all_data ();
523
524   /* Delete all the item windows in the data window.  This is usually
525      done when the data window is scrolled.  */
526   void delete_data_content_windows ();
527
528   void erase_data_content (const char *prompt);
529
530   /* Display the registers in the content from 'start_element_no'
531      until the end of the register content or the end of the display
532      height.  No checking for displaying past the end of the registers
533      is done here.  */
534   void display_registers_from (int start_element_no);
535
536   /* Display the registers starting at line line_no in the data
537      window.  Answers the line number that the display actually
538      started from.  If nothing is displayed (-1) is returned.  */
539   int display_registers_from_line (int line_no);
540
541 protected:
542
543   void do_scroll_vertical (int num_to_scroll) override;
544   void do_scroll_horizontal (int num_to_scroll) override
545   {
546   }
547   void do_make_visible_with_new_height () override;
548
549   /* Return the index of the first element displayed.  If none are
550      displayed, then return -1.  */
551   int first_data_item_displayed ();
552
553   /* Display the registers in the content from 'start_element_no' on
554      'start_line_no' until the end of the register content or the end
555      of the display height.  This function checks that we won't
556      display off the end of the register display.  */
557   void display_reg_element_at_line (int start_element_no, int start_line_no);
558 };
559
560 struct tui_cmd_window : public tui_win_info
561 {
562   tui_cmd_window ()
563     : tui_win_info (CMD_WIN)
564   {
565     can_highlight = false;
566   }
567
568   DISABLE_COPY_AND_ASSIGN (tui_cmd_window);
569
570   void clear_detail () override;
571
572   void make_visible (bool visible) override
573   {
574   }
575
576   int max_height () const override;
577
578   void refresh_window () override
579   {
580   }
581
582   const char *name () const override
583   {
584     return CMD_NAME;
585   }
586
587   bool can_scroll () const override
588   {
589     return false;
590   }
591
592   int start_line = 0;
593
594 protected:
595
596   void do_scroll_vertical (int num_to_scroll) override
597   {
598   }
599
600   void do_scroll_horizontal (int num_to_scroll) override
601   {
602   }
603
604   void do_make_visible_with_new_height () override;
605 };
606
607 extern int tui_win_is_auxiliary (enum tui_win_type win_type);
608
609
610 /* Global Data.  */
611 extern struct tui_win_info *tui_win_list[MAX_MAJOR_WINDOWS];
612
613 #define TUI_SRC_WIN     ((tui_source_window_base *) tui_win_list[SRC_WIN])
614 #define TUI_DISASM_WIN  ((tui_source_window_base *) tui_win_list[DISASSEM_WIN])
615 #define TUI_DATA_WIN    ((tui_data_window *) tui_win_list[DATA_WIN])
616 #define TUI_CMD_WIN     ((tui_cmd_window *) tui_win_list[CMD_WIN])
617
618 /* An iterator that iterates over all windows.  */
619
620 class tui_window_iterator
621 {
622 public:
623
624   typedef tui_window_iterator self_type;
625   typedef struct tui_win_info *value_type;
626   typedef struct tui_win_info *&reference;
627   typedef struct tui_win_info **pointer;
628   typedef std::forward_iterator_tag iterator_category;
629   typedef int difference_type;
630
631   explicit tui_window_iterator (enum tui_win_type type)
632     : m_type (type)
633   {
634     advance ();
635   }
636
637   tui_window_iterator ()
638     : m_type (MAX_MAJOR_WINDOWS)
639   {
640   }
641
642   bool operator!= (const self_type &other) const
643   {
644     return m_type != other.m_type;
645   }
646
647   value_type operator* () const
648   {
649     gdb_assert (m_type < MAX_MAJOR_WINDOWS);
650     return tui_win_list[m_type];
651   }
652
653   self_type &operator++ ()
654   {
655     ++m_type;
656     advance ();
657     return *this;
658   }
659
660 private:
661
662   void advance ()
663   {
664     while (m_type < MAX_MAJOR_WINDOWS && tui_win_list[m_type] == nullptr)
665       ++m_type;
666   }
667
668   int m_type;
669 };
670
671 /* A range adapter for iterating over TUI windows.  */
672
673 struct all_tui_windows
674 {
675   tui_window_iterator begin () const
676   {
677     return tui_window_iterator (SRC_WIN);
678   }
679
680   tui_window_iterator end () const
681   {
682     return tui_window_iterator ();
683   }
684 };
685
686
687 /* Data Manipulation Functions.  */
688 extern void tui_initialize_static_data (void);
689 extern struct tui_win_info *tui_partial_win_by_name (const char *);
690 extern enum tui_layout_type tui_current_layout (void);
691 extern void tui_set_current_layout_to (enum tui_layout_type);
692 extern int tui_term_height (void);
693 extern void tui_set_term_height_to (int);
694 extern int tui_term_width (void);
695 extern void tui_set_term_width_to (int);
696 extern struct tui_locator_window *tui_locator_win_info_ptr (void);
697 extern std::vector<tui_source_window_base *> &tui_source_windows ();
698 extern void tui_clear_source_windows (void);
699 extern void tui_clear_source_windows_detail (void);
700 extern void tui_add_to_source_windows (struct tui_source_window_base *);
701 extern struct tui_win_info *tui_win_with_focus (void);
702 extern void tui_set_win_with_focus (struct tui_win_info *);
703 extern struct tui_layout_def *tui_layout_def (void);
704 extern int tui_win_resized (void);
705 extern void tui_set_win_resized_to (int);
706
707 extern struct tui_win_info *tui_next_win (struct tui_win_info *);
708 extern struct tui_win_info *tui_prev_win (struct tui_win_info *);
709
710 extern unsigned int tui_tab_width;
711
712 #endif /* TUI_TUI_DATA_H */