Use a curses pad for source and disassembly windows
[binutils-gdb.git] / gdb / tui / tui-winsource.c
1 /* TUI display source/assembly window.
2
3 Copyright (C) 1998-2020 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 <ctype.h>
24 #include "symtab.h"
25 #include "frame.h"
26 #include "breakpoint.h"
27 #include "value.h"
28 #include "source.h"
29 #include "objfiles.h"
30 #include "filenames.h"
31
32 #include "tui/tui.h"
33 #include "tui/tui-data.h"
34 #include "tui/tui-io.h"
35 #include "tui/tui-stack.h"
36 #include "tui/tui-win.h"
37 #include "tui/tui-wingeneral.h"
38 #include "tui/tui-winsource.h"
39 #include "tui/tui-source.h"
40 #include "tui/tui-disasm.h"
41 #include "gdb_curses.h"
42
43 /* Function to display the "main" routine. */
44 void
45 tui_display_main ()
46 {
47 auto adapter = tui_source_windows ();
48 if (adapter.begin () != adapter.end ())
49 {
50 struct gdbarch *gdbarch;
51 CORE_ADDR addr;
52
53 tui_get_begin_asm_address (&gdbarch, &addr);
54 if (addr != (CORE_ADDR) 0)
55 {
56 struct symtab *s;
57
58 tui_update_source_windows_with_addr (gdbarch, addr);
59 s = find_pc_line_symtab (addr);
60 tui_update_locator_fullname (s);
61 }
62 }
63 }
64
65 /* See tui-winsource.h. */
66
67 std::string
68 tui_copy_source_line (const char **ptr, int *length)
69 {
70 const char *lineptr = *ptr;
71
72 /* Init the line with the line number. */
73 std::string result;
74
75 int column = 0;
76 char c;
77 do
78 {
79 int skip_bytes;
80
81 c = *lineptr;
82 if (c == '\033' && skip_ansi_escape (lineptr, &skip_bytes))
83 {
84 /* We always have to preserve escapes. */
85 result.append (lineptr, lineptr + skip_bytes);
86 lineptr += skip_bytes;
87 continue;
88 }
89 if (c == '\0')
90 break;
91
92 ++lineptr;
93 ++column;
94
95 auto process_tab = [&] ()
96 {
97 int max_tab_len = tui_tab_width;
98
99 --column;
100 for (int j = column % max_tab_len;
101 j < max_tab_len;
102 column++, j++)
103 result.push_back (' ');
104 };
105
106 if (c == '\n' || c == '\r' || c == '\0')
107 {
108 /* Nothing. */
109 }
110 else if (c < 040 && c != '\t')
111 {
112 result.push_back ('^');
113 result.push_back (c + 0100);
114 ++column;
115 }
116 else if (c == 0177)
117 {
118 result.push_back ('^');
119 result.push_back ('?');
120 ++column;
121 }
122 else if (c == '\t')
123 process_tab ();
124 else
125 result.push_back (c);
126 }
127 while (c != '\0' && c != '\n' && c != '\r');
128
129 if (c == '\r' && *lineptr == '\n')
130 ++lineptr;
131 *ptr = lineptr;
132
133 if (length != nullptr)
134 *length = column;
135
136 return result;
137 }
138
139 void
140 tui_source_window_base::style_changed ()
141 {
142 if (tui_active && is_visible ())
143 refill ();
144 }
145
146 /* Function to display source in the source window. This function
147 initializes the horizontal scroll to 0. */
148 void
149 tui_source_window_base::update_source_window
150 (struct gdbarch *gdbarch,
151 const struct symtab_and_line &sal)
152 {
153 m_horizontal_offset = 0;
154 update_source_window_as_is (gdbarch, sal);
155 }
156
157
158 /* Function to display source in the source/asm window. This function
159 shows the source as specified by the horizontal offset. */
160 void
161 tui_source_window_base::update_source_window_as_is
162 (struct gdbarch *gdbarch,
163 const struct symtab_and_line &sal)
164 {
165 bool ret = set_contents (gdbarch, sal);
166
167 if (!ret)
168 erase_source_content ();
169 else
170 {
171 update_breakpoint_info (nullptr, false);
172 show_source_content ();
173 update_exec_info ();
174 }
175 }
176
177
178 /* Function to ensure that the source and/or disassemly windows
179 reflect the input address. */
180 void
181 tui_update_source_windows_with_addr (struct gdbarch *gdbarch, CORE_ADDR addr)
182 {
183 struct symtab_and_line sal {};
184 if (addr != 0)
185 sal = find_pc_line (addr, 0);
186
187 for (struct tui_source_window_base *win_info : tui_source_windows ())
188 win_info->update_source_window (gdbarch, sal);
189 }
190
191 /* Function to ensure that the source and/or disassembly windows
192 reflect the symtab and line. */
193 void
194 tui_update_source_windows_with_line (struct symtab_and_line sal)
195 {
196 struct gdbarch *gdbarch = nullptr;
197 if (sal.symtab != nullptr)
198 {
199 find_line_pc (sal.symtab, sal.line, &sal.pc);
200 gdbarch = SYMTAB_OBJFILE (sal.symtab)->arch ();
201 }
202
203 for (struct tui_source_window_base *win_info : tui_source_windows ())
204 win_info->update_source_window (gdbarch, sal);
205 }
206
207 void
208 tui_source_window_base::do_erase_source_content (const char *str)
209 {
210 int x_pos;
211 int half_width = (width - 2) / 2;
212
213 m_content.clear ();
214 if (handle != NULL)
215 {
216 werase (handle.get ());
217 check_and_display_highlight_if_needed ();
218
219 if (strlen (str) >= half_width)
220 x_pos = 1;
221 else
222 x_pos = half_width - strlen (str);
223 mvwaddstr (handle.get (),
224 (height / 2),
225 x_pos,
226 (char *) str);
227
228 refresh_window ();
229 }
230 }
231
232
233 /* Redraw the complete line of a source or disassembly window. */
234 void
235 tui_source_window_base::show_source_line (int lineno)
236 {
237 struct tui_source_element *line;
238
239 line = &m_content[lineno];
240 if (line->is_exec_point)
241 tui_set_reverse_mode (m_pad.get (), true);
242
243 wmove (m_pad.get (), lineno, 0);
244 tui_puts (line->line.c_str (), m_pad.get ());
245 if (line->is_exec_point)
246 tui_set_reverse_mode (m_pad.get (), false);
247 }
248
249 /* See tui-winsource.h. */
250
251 void
252 tui_source_window_base::refresh_pad ()
253 {
254 int pad_width = std::max (m_max_length, width);
255 int left_margin = 1 + TUI_EXECINFO_SIZE + extra_margin ();
256 int view_width = width - left_margin - 1;
257 int pad_x = std::min (pad_width - view_width, m_horizontal_offset);
258 /* Ensure that an equal number of scrolls will work if the user
259 scrolled beyond where we clip. */
260 m_horizontal_offset = pad_x;
261 prefresh (m_pad.get (), 0, pad_x, y + 1, x + left_margin,
262 y + 1 + m_content.size (), x + left_margin + view_width - 1);
263 }
264
265 void
266 tui_source_window_base::show_source_content ()
267 {
268 gdb_assert (!m_content.empty ());
269
270 check_and_display_highlight_if_needed ();
271
272 int pad_width = std::max (m_max_length, width);
273 if (m_pad == nullptr || pad_width > getmaxx (m_pad.get ()))
274 m_pad.reset (newpad (m_content.size (), pad_width));
275
276 werase (m_pad.get ());
277 for (int lineno = 0; lineno < m_content.size (); lineno++)
278 show_source_line (lineno);
279
280 refresh_pad ();
281
282 refresh_window ();
283 }
284
285 tui_source_window_base::tui_source_window_base ()
286 {
287 m_start_line_or_addr.loa = LOA_ADDRESS;
288 m_start_line_or_addr.u.addr = 0;
289
290 gdb::observers::source_styling_changed.attach
291 (std::bind (&tui_source_window::style_changed, this),
292 m_observable);
293 }
294
295 tui_source_window_base::~tui_source_window_base ()
296 {
297 gdb::observers::source_styling_changed.detach (m_observable);
298 }
299
300 /* See tui-data.h. */
301
302 void
303 tui_source_window_base::update_tab_width ()
304 {
305 werase (handle.get ());
306 rerender ();
307 }
308
309 void
310 tui_source_window_base::rerender ()
311 {
312 if (!m_content.empty ())
313 {
314 struct symtab_and_line cursal
315 = get_current_source_symtab_and_line ();
316
317 if (m_start_line_or_addr.loa == LOA_LINE)
318 cursal.line = m_start_line_or_addr.u.line_no;
319 else
320 cursal.pc = m_start_line_or_addr.u.addr;
321 update_source_window (m_gdbarch, cursal);
322 }
323 else if (deprecated_safe_get_selected_frame () != NULL)
324 {
325 struct symtab_and_line cursal
326 = get_current_source_symtab_and_line ();
327 struct frame_info *frame = deprecated_safe_get_selected_frame ();
328 struct gdbarch *gdbarch = get_frame_arch (frame);
329
330 struct symtab *s = find_pc_line_symtab (get_frame_pc (frame));
331 if (this != TUI_SRC_WIN)
332 find_line_pc (s, cursal.line, &cursal.pc);
333 update_source_window (gdbarch, cursal);
334 }
335 else
336 erase_source_content ();
337 }
338
339 /* See tui-data.h. */
340
341 void
342 tui_source_window_base::refill ()
343 {
344 symtab_and_line sal {};
345
346 if (this == TUI_SRC_WIN)
347 {
348 sal = get_current_source_symtab_and_line ();
349 if (sal.symtab == NULL)
350 {
351 struct frame_info *fi = deprecated_safe_get_selected_frame ();
352 if (fi != nullptr)
353 sal = find_pc_line (get_frame_pc (fi), 0);
354 }
355 }
356
357 if (sal.pspace == nullptr)
358 sal.pspace = current_program_space;
359
360 if (m_start_line_or_addr.loa == LOA_LINE)
361 sal.line = m_start_line_or_addr.u.line_no;
362 else
363 sal.pc = m_start_line_or_addr.u.addr;
364
365 update_source_window_as_is (m_gdbarch, sal);
366 }
367
368 /* Scroll the source forward or backward horizontally. */
369
370 void
371 tui_source_window_base::do_scroll_horizontal (int num_to_scroll)
372 {
373 if (!m_content.empty ())
374 {
375 int offset = m_horizontal_offset + num_to_scroll;
376 if (offset < 0)
377 offset = 0;
378 m_horizontal_offset = offset;
379 refresh_pad ();
380 }
381 }
382
383
384 /* Set or clear the is_exec_point flag in the line whose line is
385 line_no. */
386
387 void
388 tui_source_window_base::set_is_exec_point_at (struct tui_line_or_address l)
389 {
390 bool changed = false;
391 int i;
392
393 i = 0;
394 while (i < m_content.size ())
395 {
396 bool new_state;
397 struct tui_line_or_address content_loa =
398 m_content[i].line_or_addr;
399
400 if (content_loa.loa == l.loa
401 && ((l.loa == LOA_LINE && content_loa.u.line_no == l.u.line_no)
402 || (l.loa == LOA_ADDRESS && content_loa.u.addr == l.u.addr)))
403 new_state = true;
404 else
405 new_state = false;
406 if (new_state != m_content[i].is_exec_point)
407 {
408 changed = true;
409 m_content[i].is_exec_point = new_state;
410 }
411 i++;
412 }
413 if (changed)
414 refill ();
415 }
416
417 /* See tui-winsource.h. */
418
419 void
420 tui_update_all_breakpoint_info (struct breakpoint *being_deleted)
421 {
422 for (tui_source_window_base *win : tui_source_windows ())
423 {
424 if (win->update_breakpoint_info (being_deleted, false))
425 win->update_exec_info ();
426 }
427 }
428
429
430 /* Scan the source window and the breakpoints to update the break_mode
431 information for each line.
432
433 Returns true if something changed and the execution window must be
434 refreshed. */
435
436 bool
437 tui_source_window_base::update_breakpoint_info
438 (struct breakpoint *being_deleted, bool current_only)
439 {
440 int i;
441 bool need_refresh = false;
442
443 for (i = 0; i < m_content.size (); i++)
444 {
445 struct tui_source_element *line;
446
447 line = &m_content[i];
448 if (current_only && !line->is_exec_point)
449 continue;
450
451 /* Scan each breakpoint to see if the current line has something to
452 do with it. Identify enable/disabled breakpoints as well as
453 those that we already hit. */
454 tui_bp_flags mode = 0;
455 iterate_over_breakpoints ([&] (breakpoint *bp) -> bool
456 {
457 struct bp_location *loc;
458
459 if (bp == being_deleted)
460 return false;
461
462 for (loc = bp->loc; loc != NULL; loc = loc->next)
463 {
464 if (location_matches_p (loc, i))
465 {
466 if (bp->enable_state == bp_disabled)
467 mode |= TUI_BP_DISABLED;
468 else
469 mode |= TUI_BP_ENABLED;
470 if (bp->hit_count)
471 mode |= TUI_BP_HIT;
472 if (bp->loc->cond)
473 mode |= TUI_BP_CONDITIONAL;
474 if (bp->type == bp_hardware_breakpoint)
475 mode |= TUI_BP_HARDWARE;
476 }
477 }
478 return false;
479 });
480 if (line->break_mode != mode)
481 {
482 line->break_mode = mode;
483 need_refresh = true;
484 }
485 }
486 return need_refresh;
487 }
488
489 /* Function to initialize the content of the execution info window,
490 based upon the input window which is either the source or
491 disassembly window. */
492 void
493 tui_source_window_base::update_exec_info ()
494 {
495 update_breakpoint_info (nullptr, true);
496 for (int i = 0; i < m_content.size (); i++)
497 {
498 struct tui_source_element *src_element = &m_content[i];
499 char element[TUI_EXECINFO_SIZE] = " ";
500
501 /* Now update the exec info content based upon the state
502 of each line as indicated by the source content. */
503 tui_bp_flags mode = src_element->break_mode;
504 if (mode & TUI_BP_HIT)
505 element[TUI_BP_HIT_POS] = (mode & TUI_BP_HARDWARE) ? 'H' : 'B';
506 else if (mode & (TUI_BP_ENABLED | TUI_BP_DISABLED))
507 element[TUI_BP_HIT_POS] = (mode & TUI_BP_HARDWARE) ? 'h' : 'b';
508
509 if (mode & TUI_BP_ENABLED)
510 element[TUI_BP_BREAK_POS] = '+';
511 else if (mode & TUI_BP_DISABLED)
512 element[TUI_BP_BREAK_POS] = '-';
513
514 if (src_element->is_exec_point)
515 element[TUI_EXEC_POS] = '>';
516
517 mvwaddstr (handle.get (), i + 1, 1, element);
518
519 show_line_number (i);
520 }
521 refresh_window ();
522 }