Commit | Line | Data |
---|---|---|
f377b406 | 1 | /* TUI display source window. |
f33c6cbf | 2 | |
e2882c85 | 3 | Copyright (C) 1998-2018 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 SS |
21 | |
22 | #include "defs.h" | |
23 | #include <ctype.h> | |
24 | #include "symtab.h" | |
25 | #include "frame.h" | |
26 | #include "breakpoint.h" | |
c2c6d25f | 27 | #include "source.h" |
a4b99e53 | 28 | #include "symtab.h" |
13274fc3 | 29 | #include "objfiles.h" |
a7417d46 | 30 | #include "filenames.h" |
c906108c | 31 | |
d7b2e967 AC |
32 | #include "tui/tui.h" |
33 | #include "tui/tui-data.h" | |
34 | #include "tui/tui-stack.h" | |
35 | #include "tui/tui-winsource.h" | |
36 | #include "tui/tui-source.h" | |
6a83354a | 37 | #include "gdb_curses.h" |
c906108c | 38 | |
98427f35 | 39 | /* Function to display source in the source window. */ |
a358af15 | 40 | enum tui_status |
08ef48c5 MS |
41 | tui_set_source_content (struct symtab *s, |
42 | int line_no, | |
43 | int noerror) | |
c906108c | 44 | { |
22940a24 | 45 | enum tui_status ret = TUI_FAILURE; |
c906108c | 46 | |
4e04028d | 47 | if (s != (struct symtab *) NULL) |
c906108c | 48 | { |
2179fbc3 | 49 | int i, c, line_width, nlines; |
d02c80cd | 50 | char *src_line = 0; |
c906108c | 51 | |
6d012f14 | 52 | if ((ret = tui_alloc_source_buffer (TUI_SRC_WIN)) == TUI_SUCCESS) |
c906108c | 53 | { |
6ba8e26f | 54 | line_width = TUI_SRC_WIN->generic.width - 1; |
1cc6d956 MS |
55 | /* Take hilite (window border) into account, when |
56 | calculating the number of lines. */ | |
6d012f14 | 57 | nlines = (line_no + (TUI_SRC_WIN->generic.height - 2)) - line_no; |
2179fbc3 TT |
58 | scoped_fd desc = open_source_file (s); |
59 | if (desc.get () < 0) | |
c906108c SS |
60 | { |
61 | if (!noerror) | |
62 | { | |
05cba821 | 63 | const char *filename = symtab_to_filename_for_display (s); |
224c3ddb | 64 | char *name = (char *) alloca (strlen (filename) + 100); |
1c5313c5 | 65 | |
05cba821 | 66 | sprintf (name, "%s:%d", filename, line_no); |
c906108c SS |
67 | print_sys_errmsg (name, errno); |
68 | } | |
69 | ret = TUI_FAILURE; | |
70 | } | |
71 | else | |
72 | { | |
73 | if (s->line_charpos == 0) | |
2179fbc3 | 74 | find_source_lines (s, desc.get ()); |
c906108c | 75 | |
6d012f14 | 76 | if (line_no < 1 || line_no > s->nlines) |
2179fbc3 TT |
77 | printf_unfiltered ("Line number %d out of range; " |
78 | "%s has %d lines.\n", | |
79 | line_no, | |
80 | symtab_to_filename_for_display (s), | |
81 | s->nlines); | |
82 | else if (lseek (desc.get (), s->line_charpos[line_no - 1], 0) | |
83 | < 0) | |
84 | perror_with_name (symtab_to_filename_for_display (s)); | |
c906108c SS |
85 | else |
86 | { | |
d02c80cd | 87 | int offset, cur_line_no, cur_line, cur_len, threshold; |
9a2b4c1b MS |
88 | struct tui_gen_win_info *locator |
89 | = tui_locator_win_info_ptr (); | |
90 | struct tui_source_info *src | |
91 | = &TUI_SRC_WIN->detail.source_info; | |
05cba821 | 92 | const char *s_filename = symtab_to_filename_for_display (s); |
bc6b7f04 | 93 | |
6d012f14 AC |
94 | if (TUI_SRC_WIN->generic.title) |
95 | xfree (TUI_SRC_WIN->generic.title); | |
05cba821 | 96 | TUI_SRC_WIN->generic.title = xstrdup (s_filename); |
bc6b7f04 | 97 | |
aa079c93 JK |
98 | xfree (src->fullname); |
99 | src->fullname = xstrdup (symtab_to_fullname (s)); | |
bc6b7f04 | 100 | |
1cc6d956 MS |
101 | /* Determine the threshold for the length of the |
102 | line and the offset to start the display. */ | |
6d012f14 | 103 | offset = src->horizontal_offset; |
6ba8e26f | 104 | threshold = (line_width - 1) + offset; |
2179fbc3 TT |
105 | gdb_file_up stream = desc.to_file (FOPEN_RT); |
106 | clearerr (stream.get ()); | |
6ba8e26f | 107 | cur_line = 0; |
eb822aa6 | 108 | src->gdbarch = get_objfile_arch (SYMTAB_OBJFILE (s)); |
362c05fe AS |
109 | src->start_line_or_addr.loa = LOA_LINE; |
110 | cur_line_no = src->start_line_or_addr.u.line_no = line_no; | |
c906108c | 111 | if (offset > 0) |
6ba8e26f | 112 | src_line = (char *) xmalloc ( |
c906108c | 113 | (threshold + 1) * sizeof (char)); |
6ba8e26f | 114 | while (cur_line < nlines) |
c906108c | 115 | { |
9a2b4c1b | 116 | struct tui_win_element *element |
63ed8182 | 117 | = TUI_SRC_WIN->generic.content[cur_line]; |
c906108c | 118 | |
1cc6d956 | 119 | /* Get the first character in the line. */ |
2179fbc3 | 120 | c = fgetc (stream.get ()); |
c906108c SS |
121 | |
122 | if (offset == 0) | |
63ed8182 PP |
123 | src_line = TUI_SRC_WIN->generic.content[cur_line] |
124 | ->which_element.source.line; | |
1cc6d956 | 125 | /* Init the line with the line number. */ |
6ba8e26f AC |
126 | sprintf (src_line, "%-6d", cur_line_no); |
127 | cur_len = strlen (src_line); | |
7806cea7 TT |
128 | i = cur_len - ((cur_len / tui_tab_width) |
129 | * tui_tab_width); | |
130 | while (i < tui_tab_width) | |
c906108c | 131 | { |
6ba8e26f | 132 | src_line[cur_len] = ' '; |
c906108c | 133 | i++; |
6ba8e26f | 134 | cur_len++; |
c906108c | 135 | } |
6ba8e26f | 136 | src_line[cur_len] = (char) 0; |
c906108c | 137 | |
1cc6d956 MS |
138 | /* Set whether element is the execution point |
139 | and whether there is a break point on it. */ | |
362c05fe AS |
140 | element->which_element.source.line_or_addr.loa = |
141 | LOA_LINE; | |
142 | element->which_element.source.line_or_addr.u.line_no = | |
6ba8e26f | 143 | cur_line_no; |
6d012f14 | 144 | element->which_element.source.is_exec_point = |
63ed8182 PP |
145 | (filename_cmp (locator->content[0] |
146 | ->which_element.locator.full_name, | |
56d397a3 | 147 | symtab_to_fullname (s)) == 0 |
63ed8182 PP |
148 | && cur_line_no |
149 | == locator->content[0] | |
150 | ->which_element.locator.line_no); | |
c906108c SS |
151 | if (c != EOF) |
152 | { | |
6ba8e26f | 153 | i = strlen (src_line) - 1; |
c906108c SS |
154 | do |
155 | { | |
e5908723 MS |
156 | if ((c != '\n') && (c != '\r') |
157 | && (++i < threshold)) | |
c906108c SS |
158 | { |
159 | if (c < 040 && c != '\t') | |
160 | { | |
6ba8e26f AC |
161 | src_line[i++] = '^'; |
162 | src_line[i] = c + 0100; | |
c906108c SS |
163 | } |
164 | else if (c == 0177) | |
165 | { | |
6ba8e26f AC |
166 | src_line[i++] = '^'; |
167 | src_line[i] = '?'; | |
c906108c SS |
168 | } |
169 | else | |
1cc6d956 MS |
170 | { /* Store the charcter in the |
171 | line buffer. If it is a tab, | |
172 | then translate to the correct | |
173 | number of chars so we don't | |
174 | overwrite our buffer. */ | |
c906108c SS |
175 | if (c == '\t') |
176 | { | |
9a2b4c1b | 177 | int j, max_tab_len |
7806cea7 | 178 | = tui_tab_width; |
c906108c | 179 | |
9a2b4c1b MS |
180 | for (j = i - ((i / max_tab_len) |
181 | * max_tab_len); | |
e5908723 MS |
182 | j < max_tab_len |
183 | && i < threshold; | |
c906108c | 184 | i++, j++) |
6ba8e26f | 185 | src_line[i] = ' '; |
c906108c SS |
186 | i--; |
187 | } | |
188 | else | |
6ba8e26f | 189 | src_line[i] = c; |
c906108c | 190 | } |
6ba8e26f | 191 | src_line[i + 1] = 0; |
c906108c SS |
192 | } |
193 | else | |
1cc6d956 MS |
194 | { /* If we have not reached EOL, then |
195 | eat chars until we do. */ | |
c906108c | 196 | while (c != EOF && c != '\n' && c != '\r') |
2179fbc3 | 197 | c = fgetc (stream.get ()); |
861cf606 | 198 | /* Handle non-'\n' end-of-line. */ |
e5908723 | 199 | if (c == '\r' |
2179fbc3 | 200 | && (c = fgetc (stream.get ())) != '\n' |
e5908723 | 201 | && c != EOF) |
861cf606 | 202 | { |
2179fbc3 TT |
203 | ungetc (c, stream.get ()); |
204 | c = '\r'; | |
861cf606 FR |
205 | } |
206 | ||
c906108c SS |
207 | } |
208 | } | |
e5908723 MS |
209 | while (c != EOF && c != '\n' && c != '\r' |
210 | && i < threshold | |
2179fbc3 | 211 | && (c = fgetc (stream.get ()))); |
c906108c | 212 | } |
1cc6d956 MS |
213 | /* Now copy the line taking the offset into |
214 | account. */ | |
2eb639cb PP |
215 | if (offset == 0) |
216 | ; | |
217 | else if (strlen (src_line) > offset) | |
63ed8182 PP |
218 | strcpy (TUI_SRC_WIN->generic.content[cur_line] |
219 | ->which_element.source.line, | |
6ba8e26f | 220 | &src_line[offset]); |
c906108c | 221 | else |
63ed8182 PP |
222 | TUI_SRC_WIN->generic.content[cur_line] |
223 | ->which_element.source.line[0] = (char) 0; | |
6ba8e26f AC |
224 | cur_line++; |
225 | cur_line_no++; | |
c906108c SS |
226 | } |
227 | if (offset > 0) | |
6ba8e26f | 228 | xfree (src_line); |
6d012f14 | 229 | TUI_SRC_WIN->generic.content_size = nlines; |
c906108c SS |
230 | ret = TUI_SUCCESS; |
231 | } | |
232 | } | |
233 | } | |
234 | } | |
235 | return ret; | |
98427f35 | 236 | } |
c906108c SS |
237 | |
238 | ||
1cc6d956 | 239 | /* elz: This function sets the contents of the source window to empty |
c906108c | 240 | except for a line in the middle with a warning message about the |
1cc6d956 | 241 | source not being available. This function is called by |
6ba8e26f AC |
242 | tui_erase_source_contents(), which in turn is invoked when the |
243 | source files cannot be accessed. */ | |
c906108c SS |
244 | |
245 | void | |
08ef48c5 | 246 | tui_set_source_content_nil (struct tui_win_info *win_info, |
a121b7c1 | 247 | const char *warning_string) |
c906108c | 248 | { |
6ba8e26f AC |
249 | int line_width; |
250 | int n_lines; | |
c906108c SS |
251 | int curr_line = 0; |
252 | ||
6ba8e26f AC |
253 | line_width = win_info->generic.width - 1; |
254 | n_lines = win_info->generic.height - 2; | |
c906108c | 255 | |
1cc6d956 MS |
256 | /* Set to empty each line in the window, except for the one which |
257 | contains the message. */ | |
6ba8e26f | 258 | while (curr_line < win_info->generic.content_size) |
c906108c | 259 | { |
1cc6d956 MS |
260 | /* Set the information related to each displayed line to null: |
261 | i.e. the line number is 0, there is no bp, it is not where | |
262 | the program is stopped. */ | |
c906108c | 263 | |
63ed8182 | 264 | struct tui_win_element *element = win_info->generic.content[curr_line]; |
1c5313c5 | 265 | |
362c05fe AS |
266 | element->which_element.source.line_or_addr.loa = LOA_LINE; |
267 | element->which_element.source.line_or_addr.u.line_no = 0; | |
6d012f14 AC |
268 | element->which_element.source.is_exec_point = FALSE; |
269 | element->which_element.source.has_break = FALSE; | |
c906108c | 270 | |
1cc6d956 | 271 | /* Set the contents of the line to blank. */ |
6d012f14 | 272 | element->which_element.source.line[0] = (char) 0; |
c906108c | 273 | |
1cc6d956 | 274 | /* If the current line is in the middle of the screen, then we |
6ba8e26f AC |
275 | want to display the 'no source available' message in it. |
276 | Note: the 'weird' arithmetic with the line width and height | |
1cc6d956 MS |
277 | comes from the function tui_erase_source_content(). We need |
278 | to keep the screen and the window's actual contents in | |
279 | synch. */ | |
c906108c | 280 | |
6ba8e26f | 281 | if (curr_line == (n_lines / 2 + 1)) |
c906108c SS |
282 | { |
283 | int i; | |
284 | int xpos; | |
285 | int warning_length = strlen (warning_string); | |
6ba8e26f | 286 | char *src_line; |
c906108c | 287 | |
6ba8e26f | 288 | src_line = element->which_element.source.line; |
c906108c | 289 | |
6ba8e26f | 290 | if (warning_length >= ((line_width - 1) / 2)) |
c906108c SS |
291 | xpos = 1; |
292 | else | |
6ba8e26f | 293 | xpos = (line_width - 1) / 2 - warning_length; |
c906108c SS |
294 | |
295 | for (i = 0; i < xpos; i++) | |
6ba8e26f | 296 | src_line[i] = ' '; |
c906108c | 297 | |
6ba8e26f | 298 | sprintf (src_line + i, "%s", warning_string); |
c906108c | 299 | |
6ba8e26f AC |
300 | for (i = xpos + warning_length; i < line_width; i++) |
301 | src_line[i] = ' '; | |
c906108c | 302 | |
6ba8e26f | 303 | src_line[i] = '\n'; |
c906108c SS |
304 | |
305 | } /* end if */ | |
306 | ||
307 | curr_line++; | |
308 | ||
c5aa993b | 309 | } /* end while */ |
98427f35 | 310 | } |
c906108c SS |
311 | |
312 | ||
98427f35 SC |
313 | /* Function to display source in the source window. This function |
314 | initializes the horizontal scroll to 0. */ | |
c906108c | 315 | void |
13274fc3 | 316 | tui_show_symtab_source (struct gdbarch *gdbarch, struct symtab *s, |
08ef48c5 MS |
317 | struct tui_line_or_address line, |
318 | int noerror) | |
c906108c | 319 | { |
6d012f14 | 320 | TUI_SRC_WIN->detail.source_info.horizontal_offset = 0; |
13274fc3 | 321 | tui_update_source_window_as_is (TUI_SRC_WIN, gdbarch, s, line, noerror); |
98427f35 | 322 | } |
c906108c | 323 | |
c906108c | 324 | |
a358af15 AC |
325 | /* Answer whether the source is currently displayed in the source |
326 | window. */ | |
c906108c | 327 | int |
56d397a3 | 328 | tui_source_is_displayed (const char *fullname) |
c906108c | 329 | { |
f629cd75 L |
330 | return (TUI_SRC_WIN != NULL |
331 | && TUI_SRC_WIN->generic.content_in_use | |
63ed8182 PP |
332 | && (filename_cmp (tui_locator_win_info_ptr ()->content[0] |
333 | ->which_element.locator.full_name, | |
56d397a3 | 334 | fullname) == 0)); |
98427f35 | 335 | } |
c906108c SS |
336 | |
337 | ||
98427f35 | 338 | /* Scroll the source forward or backward vertically. */ |
c906108c | 339 | void |
6ba8e26f AC |
340 | tui_vertical_source_scroll (enum tui_scroll_direction scroll_direction, |
341 | int num_to_scroll) | |
c906108c | 342 | { |
6d012f14 | 343 | if (TUI_SRC_WIN->generic.content != NULL) |
c906108c | 344 | { |
362c05fe | 345 | struct tui_line_or_address l; |
c906108c | 346 | struct symtab *s; |
63a33118 | 347 | tui_win_content content = TUI_SRC_WIN->generic.content; |
52575520 | 348 | struct symtab_and_line cursal = get_current_source_symtab_and_line (); |
c906108c | 349 | |
52575520 | 350 | if (cursal.symtab == (struct symtab *) NULL) |
34248c3a | 351 | s = find_pc_line_symtab (get_frame_pc (get_selected_frame (NULL))); |
c906108c | 352 | else |
52575520 | 353 | s = cursal.symtab; |
c906108c | 354 | |
362c05fe | 355 | l.loa = LOA_LINE; |
6ba8e26f | 356 | if (scroll_direction == FORWARD_SCROLL) |
c906108c | 357 | { |
362c05fe AS |
358 | l.u.line_no = content[0]->which_element.source.line_or_addr.u.line_no |
359 | + num_to_scroll; | |
360 | if (l.u.line_no > s->nlines) | |
1cc6d956 MS |
361 | /* line = s->nlines - win_info->generic.content_size + 1; */ |
362 | /* elz: fix for dts 23398. */ | |
9a2b4c1b MS |
363 | l.u.line_no |
364 | = content[0]->which_element.source.line_or_addr.u.line_no; | |
c906108c SS |
365 | } |
366 | else | |
367 | { | |
362c05fe AS |
368 | l.u.line_no = content[0]->which_element.source.line_or_addr.u.line_no |
369 | - num_to_scroll; | |
370 | if (l.u.line_no <= 0) | |
371 | l.u.line_no = 1; | |
c906108c | 372 | } |
27229e99 | 373 | |
362c05fe | 374 | print_source_lines (s, l.u.line_no, l.u.line_no + 1, 0); |
c906108c | 375 | } |
98427f35 | 376 | } |