GDB (xrefs)

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