x86-64: tighten convert-load-reloc checking
[binutils-gdb.git] / gdb / tui / tui-source.c
blob9a0a04142b23700f95f366e9f0485a42d6946a0f
1 /* TUI display source window.
3 Copyright (C) 1998-2024 Free Software Foundation, Inc.
5 Contributed by Hewlett-Packard Company.
7 This file is part of GDB.
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.
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.
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/>. */
22 #include <math.h>
23 #include "symtab.h"
24 #include "frame.h"
25 #include "breakpoint.h"
26 #include "source.h"
27 #include "objfiles.h"
28 #include "filenames.h"
29 #include "source-cache.h"
30 #include "tui/tui-status.h"
31 #include "tui/tui-win.h"
32 #include "tui/tui-winsource.h"
33 #include "tui/tui-source.h"
34 #include "tui/tui-location.h"
35 #include "tui/tui-io.h"
36 #include "cli/cli-style.h"
38 tui_source_window::tui_source_window ()
40 line_number_style.changed.attach
41 (std::bind (&tui_source_window::style_changed, this),
42 m_src_observable, "tui-source");
45 tui_source_window::~tui_source_window ()
47 line_number_style.changed.detach (m_src_observable);
50 /* Function to display source in the source window. */
51 bool
52 tui_source_window::set_contents (struct gdbarch *arch,
53 const struct symtab_and_line &sal)
55 struct symtab *s = sal.symtab;
56 int line_no = sal.line;
58 if (s == NULL)
59 return false;
61 /* Take hilite (window border) into account, when
62 calculating the number of lines. */
63 int nlines = height - box_size ();
65 std::string srclines;
66 const std::vector<off_t> *offsets;
67 if (!g_source_cache.get_source_lines (s, line_no, line_no + nlines,
68 &srclines)
69 || !g_source_cache.get_line_charpos (s, &offsets))
70 return false;
72 int cur_line_no, cur_line;
73 const char *s_filename = symtab_to_filename_for_display (s);
75 set_title (s_filename);
77 m_fullname = make_unique_xstrdup (symtab_to_fullname (s));
79 cur_line = 0;
80 m_gdbarch = s->compunit ()->objfile ()->arch ();
81 m_start_line_or_addr.loa = LOA_LINE;
82 cur_line_no = m_start_line_or_addr.u.line_no = line_no;
84 m_digits = 7;
85 if (compact_source)
87 /* Solaris 11+gcc 5.5 has ambiguous overloads of log10, so we
88 cast to double to get the right one. */
89 int lines_in_file = offsets->size ();
90 int max_line_nr = lines_in_file;
91 int digits_needed = 1 + (int)log10 ((double) max_line_nr);
92 int trailing_space = 1;
93 m_digits = digits_needed + trailing_space;
96 m_max_length = -1;
97 const char *iter = srclines.c_str ();
98 m_content.resize (nlines);
99 while (cur_line < nlines)
101 struct tui_source_element *element = &m_content[cur_line];
103 std::string text;
104 if (*iter != '\0')
106 int line_len;
107 text = tui_copy_source_line (&iter, &line_len);
108 m_max_length = std::max (m_max_length, line_len);
110 else
112 /* Line not in source file. */
113 cur_line_no = -1;
116 /* Set whether element is the execution point
117 and whether there is a break point on it. */
118 element->line_or_addr.loa = LOA_LINE;
119 element->line_or_addr.u.line_no = cur_line_no;
120 element->is_exec_point
121 = (filename_cmp (tui_location.full_name ().c_str (),
122 symtab_to_fullname (s)) == 0
123 && cur_line_no == tui_location.line_no ());
125 m_content[cur_line].line = std::move (text);
127 cur_line++;
128 cur_line_no++;
131 return true;
135 /* Answer whether the source is currently displayed in the source
136 window. */
137 bool
138 tui_source_window::showing_source_p (const char *fullname) const
140 return (!m_content.empty ()
141 && (filename_cmp (tui_location.full_name ().c_str (),
142 fullname) == 0));
146 /* Scroll the source forward or backward vertically. */
147 void
148 tui_source_window::do_scroll_vertical (int num_to_scroll)
150 if (!m_content.empty ())
152 struct symtab *s;
153 symtab_and_line cursal
154 = get_current_source_symtab_and_line (current_program_space);
155 struct gdbarch *arch = m_gdbarch;
157 if (cursal.symtab == NULL)
159 frame_info_ptr fi = get_selected_frame (NULL);
160 s = find_pc_line_symtab (get_frame_pc (fi));
161 arch = get_frame_arch (fi);
163 else
164 s = cursal.symtab;
166 int line_no = m_start_line_or_addr.u.line_no + num_to_scroll;
167 if (line_no <= 0)
168 line_no = 1;
169 const std::vector<off_t> *offsets;
170 if (g_source_cache.get_line_charpos (s, &offsets)
171 && line_no > offsets->size ())
172 line_no = m_start_line_or_addr.u.line_no;
174 cursal.line = line_no;
175 find_line_pc (cursal.symtab, cursal.line, &cursal.pc);
176 for (struct tui_source_window_base *win_info : tui_source_windows ())
177 win_info->update_source_window_as_is (arch, cursal);
181 bool
182 tui_source_window::location_matches_p (struct bp_location *loc, int line_no)
184 return (m_content[line_no].line_or_addr.loa == LOA_LINE
185 && m_content[line_no].line_or_addr.u.line_no == loc->line_number
186 && loc->symtab != NULL
187 && filename_cmp (m_fullname.get (),
188 symtab_to_fullname (loc->symtab)) == 0);
191 /* See tui-source.h. */
193 bool
194 tui_source_window::line_is_displayed (int line) const
196 if (m_content.size () < SCROLL_THRESHOLD)
197 return false;
199 for (size_t i = 0; i < m_content.size () - SCROLL_THRESHOLD; ++i)
201 if (m_content[i].line_or_addr.loa == LOA_LINE
202 && m_content[i].line_or_addr.u.line_no == line)
203 return true;
206 return false;
209 void
210 tui_source_window::maybe_update (const frame_info_ptr &fi, symtab_and_line sal)
212 int start_line = (sal.line - ((height - box_size ()) / 2)) + 1;
213 if (start_line <= 0)
214 start_line = 1;
216 bool source_already_displayed = (sal.symtab != 0
217 && showing_source_p (m_fullname.get ()));
219 if (!(source_already_displayed && line_is_displayed (sal.line)))
221 sal.line = start_line;
222 update_source_window (get_frame_arch (fi), sal);
224 else
226 struct tui_line_or_address l;
228 l.loa = LOA_LINE;
229 l.u.line_no = sal.line;
230 set_is_exec_point_at (l);
234 void
235 tui_source_window::display_start_addr (struct gdbarch **gdbarch_p,
236 CORE_ADDR *addr_p)
238 symtab_and_line cursal = get_current_source_symtab_and_line (current_program_space);
240 *gdbarch_p = m_gdbarch;
241 find_line_pc (cursal.symtab, m_start_line_or_addr.u.line_no, addr_p);
244 /* See tui-winsource.h. */
246 void
247 tui_source_window::show_line_number (int offset) const
249 int lineno = m_content[offset].line_or_addr.u.line_no;
250 char text[20];
251 char space = tui_left_margin_verbose ? '_' : ' ';
252 if (lineno == -1)
254 /* Line not in source file, don't show line number. */
255 for (int i = 0; i <= m_digits; ++i)
256 text[i] = (i == m_digits) ? '\0' : space;
258 else
260 xsnprintf (text, sizeof (text),
261 tui_left_margin_verbose ? "%0*d%c" : "%*d%c", m_digits - 1,
262 lineno, space);
264 tui_apply_style (handle.get (), line_number_style.style ());
265 display_string (text);
266 tui_apply_style (handle.get (), ui_file_style ());