Commit | Line | Data |
---|---|---|
c906108c | 1 | /* List lines of source files for GDB, the GNU debugger. |
42a4f53d | 2 | Copyright (C) 1986-2019 Free Software Foundation, Inc. |
c906108c | 3 | |
c5aa993b | 4 | This file is part of GDB. |
c906108c | 5 | |
c5aa993b JM |
6 | This program is free software; you can redistribute it and/or modify |
7 | it under the terms of the GNU General Public License as published by | |
a9762ec7 | 8 | the Free Software Foundation; either version 3 of the License, or |
c5aa993b | 9 | (at your option) any later version. |
c906108c | 10 | |
c5aa993b JM |
11 | This program is distributed in the hope that it will be useful, |
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | GNU General Public License for more details. | |
c906108c | 15 | |
c5aa993b | 16 | You should have received a copy of the GNU General Public License |
a9762ec7 | 17 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
c906108c SS |
18 | |
19 | #include "defs.h" | |
5af949e3 | 20 | #include "arch-utils.h" |
c906108c SS |
21 | #include "symtab.h" |
22 | #include "expression.h" | |
23 | #include "language.h" | |
24 | #include "command.h" | |
c2c6d25f | 25 | #include "source.h" |
c906108c SS |
26 | #include "gdbcmd.h" |
27 | #include "frame.h" | |
28 | #include "value.h" | |
268a13a5 | 29 | #include "gdbsupport/filestuff.h" |
c906108c SS |
30 | |
31 | #include <sys/types.h> | |
c906108c | 32 | #include <fcntl.h> |
c906108c | 33 | #include "gdbcore.h" |
88987551 | 34 | #include "gdb_regex.h" |
c906108c SS |
35 | #include "symfile.h" |
36 | #include "objfiles.h" | |
37 | #include "annotate.h" | |
38 | #include "gdbtypes.h" | |
c5f0f3d0 | 39 | #include "linespec.h" |
fe4e3eb8 | 40 | #include "filenames.h" /* for DOSish file names */ |
d75b5104 | 41 | #include "completer.h" |
8b93c638 | 42 | #include "ui-out.h" |
dbda9972 | 43 | #include "readline/readline.h" |
268a13a5 TT |
44 | #include "gdbsupport/enum-flags.h" |
45 | #include "gdbsupport/scoped_fd.h" | |
325fac50 | 46 | #include <algorithm> |
268a13a5 | 47 | #include "gdbsupport/pathstuff.h" |
62f29fda | 48 | #include "source-cache.h" |
e43b10e1 | 49 | #include "cli/cli-style.h" |
c906108c | 50 | |
c906108c SS |
51 | #define OPEN_MODE (O_RDONLY | O_BINARY) |
52 | #define FDOPEN_MODE FOPEN_RB | |
53 | ||
c906108c SS |
54 | /* Path of directories to search for source files. |
55 | Same format as the PATH environment variable's value. */ | |
56 | ||
57 | char *source_path; | |
58 | ||
2f61ca93 JB |
59 | /* Support for source path substitution commands. */ |
60 | ||
61 | struct substitute_path_rule | |
62 | { | |
63 | char *from; | |
64 | char *to; | |
65 | struct substitute_path_rule *next; | |
66 | }; | |
67 | ||
68 | static struct substitute_path_rule *substitute_path_rules = NULL; | |
69 | ||
c906108c SS |
70 | /* Symtab of default file for listing lines of. */ |
71 | ||
0378c332 | 72 | static struct symtab *current_source_symtab; |
c906108c SS |
73 | |
74 | /* Default next line to list. */ | |
75 | ||
0378c332 | 76 | static int current_source_line; |
c906108c | 77 | |
6c95b8df PA |
78 | static struct program_space *current_source_pspace; |
79 | ||
c906108c SS |
80 | /* Default number of lines to print with commands like "list". |
81 | This is based on guessing how many long (i.e. more than chars_per_line | |
82 | characters) lines there will be. To be completely correct, "list" | |
83 | and friends should be rewritten to count characters and see where | |
84 | things are wrapping, but that would be a fair amount of work. */ | |
85 | ||
f43f8571 | 86 | static int lines_to_list = 10; |
920d2a44 AC |
87 | static void |
88 | show_lines_to_list (struct ui_file *file, int from_tty, | |
89 | struct cmd_list_element *c, const char *value) | |
90 | { | |
3e43a32a MS |
91 | fprintf_filtered (file, |
92 | _("Number of source lines gdb " | |
93 | "will list by default is %s.\n"), | |
920d2a44 AC |
94 | value); |
95 | } | |
c906108c | 96 | |
1b56eb55 JK |
97 | /* Possible values of 'set filename-display'. */ |
98 | static const char filename_display_basename[] = "basename"; | |
99 | static const char filename_display_relative[] = "relative"; | |
100 | static const char filename_display_absolute[] = "absolute"; | |
101 | ||
102 | static const char *const filename_display_kind_names[] = { | |
103 | filename_display_basename, | |
104 | filename_display_relative, | |
105 | filename_display_absolute, | |
106 | NULL | |
107 | }; | |
108 | ||
109 | static const char *filename_display_string = filename_display_relative; | |
110 | ||
111 | static void | |
112 | show_filename_display_string (struct ui_file *file, int from_tty, | |
113 | struct cmd_list_element *c, const char *value) | |
114 | { | |
115 | fprintf_filtered (file, _("Filenames are displayed as \"%s\".\n"), value); | |
116 | } | |
117 | ||
c906108c SS |
118 | /* Line number of last line printed. Default for various commands. |
119 | current_source_line is usually, but not always, the same as this. */ | |
120 | ||
121 | static int last_line_listed; | |
122 | ||
5166082f PA |
123 | /* First line number listed by last listing command. If 0, then no |
124 | source lines have yet been listed since the last time the current | |
125 | source line was changed. */ | |
c906108c SS |
126 | |
127 | static int first_line_listed; | |
128 | ||
129 | /* Saves the name of the last source file visited and a possible error code. | |
c378eb4e | 130 | Used to prevent repeating annoying "No such file or directories" msgs. */ |
c906108c SS |
131 | |
132 | static struct symtab *last_source_visited = NULL; | |
b08b16c8 | 133 | static bool last_source_error = false; |
c906108c | 134 | \f |
0378c332 FN |
135 | /* Return the first line listed by print_source_lines. |
136 | Used by command interpreters to request listing from | |
c378eb4e | 137 | a previous point. */ |
0378c332 FN |
138 | |
139 | int | |
140 | get_first_line_listed (void) | |
141 | { | |
142 | return first_line_listed; | |
143 | } | |
144 | ||
5166082f PA |
145 | /* Clear line listed range. This makes the next "list" center the |
146 | printed source lines around the current source line. */ | |
147 | ||
148 | static void | |
149 | clear_lines_listed_range (void) | |
150 | { | |
151 | first_line_listed = 0; | |
152 | last_line_listed = 0; | |
153 | } | |
154 | ||
0378c332 FN |
155 | /* Return the default number of lines to print with commands like the |
156 | cli "list". The caller of print_source_lines must use this to | |
157 | calculate the end line and use it in the call to print_source_lines | |
c378eb4e | 158 | as it does not automatically use this value. */ |
0378c332 FN |
159 | |
160 | int | |
161 | get_lines_to_list (void) | |
162 | { | |
163 | return lines_to_list; | |
164 | } | |
165 | ||
166 | /* Return the current source file for listing and next line to list. | |
c378eb4e | 167 | NOTE: The returned sal pc and end fields are not valid. */ |
0378c332 FN |
168 | |
169 | struct symtab_and_line | |
170 | get_current_source_symtab_and_line (void) | |
171 | { | |
51abb421 | 172 | symtab_and_line cursal; |
0378c332 | 173 | |
6c95b8df | 174 | cursal.pspace = current_source_pspace; |
0378c332 FN |
175 | cursal.symtab = current_source_symtab; |
176 | cursal.line = current_source_line; | |
53cb0458 FN |
177 | cursal.pc = 0; |
178 | cursal.end = 0; | |
0378c332 FN |
179 | |
180 | return cursal; | |
181 | } | |
182 | ||
53cb0458 FN |
183 | /* If the current source file for listing is not set, try and get a default. |
184 | Usually called before get_current_source_symtab_and_line() is called. | |
0378c332 | 185 | It may err out if a default cannot be determined. |
53cb0458 FN |
186 | We must be cautious about where it is called, as it can recurse as the |
187 | process of determining a new default may call the caller! | |
188 | Use get_current_source_symtab_and_line only to get whatever | |
c378eb4e | 189 | we have without erroring out or trying to get a default. */ |
0378c332 | 190 | |
53cb0458 FN |
191 | void |
192 | set_default_source_symtab_and_line (void) | |
0378c332 | 193 | { |
0378c332 | 194 | if (!have_full_symbols () && !have_partial_symbols ()) |
8a3fe4f8 | 195 | error (_("No symbol table is loaded. Use the \"file\" command.")); |
0378c332 | 196 | |
c378eb4e | 197 | /* Pull in a current source symtab if necessary. */ |
0378c332 FN |
198 | if (current_source_symtab == 0) |
199 | select_source_symtab (0); | |
0378c332 FN |
200 | } |
201 | ||
202 | /* Return the current default file for listing and next line to list | |
203 | (the returned sal pc and end fields are not valid.) | |
53cb0458 | 204 | and set the current default to whatever is in SAL. |
c378eb4e | 205 | NOTE: The returned sal pc and end fields are not valid. */ |
0378c332 FN |
206 | |
207 | struct symtab_and_line | |
51abb421 | 208 | set_current_source_symtab_and_line (const symtab_and_line &sal) |
0378c332 | 209 | { |
51abb421 | 210 | symtab_and_line cursal; |
6c95b8df PA |
211 | |
212 | cursal.pspace = current_source_pspace; | |
0378c332 FN |
213 | cursal.symtab = current_source_symtab; |
214 | cursal.line = current_source_line; | |
6c95b8df PA |
215 | cursal.pc = 0; |
216 | cursal.end = 0; | |
0378c332 | 217 | |
51abb421 PA |
218 | current_source_pspace = sal.pspace; |
219 | current_source_symtab = sal.symtab; | |
220 | current_source_line = sal.line; | |
6c95b8df | 221 | |
5166082f PA |
222 | /* Force the next "list" to center around the current line. */ |
223 | clear_lines_listed_range (); | |
224 | ||
0378c332 FN |
225 | return cursal; |
226 | } | |
227 | ||
c378eb4e | 228 | /* Reset any information stored about a default file and line to print. */ |
0378c332 FN |
229 | |
230 | void | |
231 | clear_current_source_symtab_and_line (void) | |
232 | { | |
233 | current_source_symtab = 0; | |
234 | current_source_line = 0; | |
235 | } | |
c5aa993b | 236 | |
583068ca | 237 | /* See source.h. */ |
c906108c SS |
238 | |
239 | void | |
aa1ee363 | 240 | select_source_symtab (struct symtab *s) |
c906108c | 241 | { |
c906108c SS |
242 | if (s) |
243 | { | |
244 | current_source_symtab = s; | |
245 | current_source_line = 1; | |
6c95b8df | 246 | current_source_pspace = SYMTAB_PSPACE (s); |
c906108c SS |
247 | return; |
248 | } | |
249 | ||
250 | if (current_source_symtab) | |
251 | return; | |
252 | ||
253 | /* Make the default place to list be the function `main' | |
254 | if one exists. */ | |
5c281dbb TT |
255 | block_symbol bsym = lookup_symbol (main_name (), 0, VAR_DOMAIN, 0); |
256 | if (bsym.symbol != nullptr && SYMBOL_CLASS (bsym.symbol) == LOC_BLOCK) | |
c906108c | 257 | { |
5c281dbb | 258 | symtab_and_line sal = find_function_start_sal (bsym.symbol, true); |
6c95b8df | 259 | current_source_pspace = sal.pspace; |
c906108c | 260 | current_source_symtab = sal.symtab; |
325fac50 | 261 | current_source_line = std::max (sal.line - (lines_to_list - 1), 1); |
5c281dbb | 262 | return; |
c906108c | 263 | } |
c5aa993b | 264 | |
8340a3fb LM |
265 | /* Alright; find the last file in the symtab list (ignoring .h's |
266 | and namespace symtabs). */ | |
c906108c SS |
267 | |
268 | current_source_line = 1; | |
269 | ||
2030c079 | 270 | for (objfile *ofp : current_program_space->objfiles ()) |
c906108c | 271 | { |
b669c953 | 272 | for (compunit_symtab *cu : ofp->compunits ()) |
c906108c | 273 | { |
8b31193a TT |
274 | for (symtab *symtab : compunit_filetabs (cu)) |
275 | { | |
276 | const char *name = symtab->filename; | |
277 | int len = strlen (name); | |
278 | ||
279 | if (!(len > 2 && (strcmp (&name[len - 2], ".h") == 0 | |
280 | || strcmp (name, "<<C++-namespaces>>") == 0))) | |
281 | { | |
282 | current_source_pspace = current_program_space; | |
283 | current_source_symtab = symtab; | |
284 | } | |
285 | } | |
c906108c SS |
286 | } |
287 | } | |
6c95b8df | 288 | |
c906108c SS |
289 | if (current_source_symtab) |
290 | return; | |
291 | ||
2030c079 | 292 | for (objfile *objfile : current_program_space->objfiles ()) |
aed57c53 TT |
293 | { |
294 | if (objfile->sf) | |
295 | s = objfile->sf->qf->find_last_source_symtab (objfile); | |
296 | if (s) | |
297 | current_source_symtab = s; | |
298 | } | |
c906108c SS |
299 | if (current_source_symtab) |
300 | return; | |
301 | ||
8a3fe4f8 | 302 | error (_("Can't find a default source file")); |
c906108c SS |
303 | } |
304 | \f | |
99e7ae30 DE |
305 | /* Handler for "set directories path-list" command. |
306 | "set dir mumble" doesn't prepend paths, it resets the entire | |
307 | path list. The theory is that set(show(dir)) should be a no-op. */ | |
308 | ||
309 | static void | |
eb4c3f4a TT |
310 | set_directories_command (const char *args, |
311 | int from_tty, struct cmd_list_element *c) | |
99e7ae30 DE |
312 | { |
313 | /* This is the value that was set. | |
314 | It needs to be processed to maintain $cdir:$cwd and remove dups. */ | |
315 | char *set_path = source_path; | |
316 | ||
317 | /* We preserve the invariant that $cdir:$cwd begins life at the end of | |
318 | the list by calling init_source_path. If they appear earlier in | |
319 | SET_PATH then mod_path will move them appropriately. | |
320 | mod_path will also remove duplicates. */ | |
321 | init_source_path (); | |
322 | if (*set_path != '\0') | |
323 | mod_path (set_path, &source_path); | |
324 | ||
325 | xfree (set_path); | |
326 | } | |
327 | ||
328 | /* Print the list of source directories. | |
329 | This is used by the "ld" command, so it has the signature of a command | |
330 | function. */ | |
331 | ||
c906108c | 332 | static void |
99e7ae30 | 333 | show_directories_1 (char *ignore, int from_tty) |
c906108c SS |
334 | { |
335 | puts_filtered ("Source directories searched: "); | |
336 | puts_filtered (source_path); | |
337 | puts_filtered ("\n"); | |
338 | } | |
339 | ||
99e7ae30 DE |
340 | /* Handler for "show directories" command. */ |
341 | ||
342 | static void | |
343 | show_directories_command (struct ui_file *file, int from_tty, | |
344 | struct cmd_list_element *c, const char *value) | |
345 | { | |
346 | show_directories_1 (NULL, from_tty); | |
347 | } | |
348 | ||
583068ca | 349 | /* See source.h. */ |
00174a86 TT |
350 | |
351 | void | |
352 | forget_cached_source_info_for_objfile (struct objfile *objfile) | |
353 | { | |
b669c953 | 354 | for (compunit_symtab *cu : objfile->compunits ()) |
00174a86 | 355 | { |
d5da8b3c | 356 | for (symtab *s : compunit_filetabs (cu)) |
00174a86 | 357 | { |
d5da8b3c TT |
358 | if (s->fullname != NULL) |
359 | { | |
360 | xfree (s->fullname); | |
361 | s->fullname = NULL; | |
362 | } | |
00174a86 | 363 | } |
00174a86 | 364 | } |
6f809020 DE |
365 | |
366 | if (objfile->sf) | |
367 | objfile->sf->qf->forget_cached_source_info (objfile); | |
00174a86 TT |
368 | } |
369 | ||
583068ca | 370 | /* See source.h. */ |
c906108c SS |
371 | |
372 | void | |
fba45db2 | 373 | forget_cached_source_info (void) |
c906108c | 374 | { |
6c95b8df | 375 | struct program_space *pspace; |
c906108c | 376 | |
6c95b8df | 377 | ALL_PSPACES (pspace) |
2030c079 | 378 | for (objfile *objfile : pspace->objfiles ()) |
99d89cde TT |
379 | { |
380 | forget_cached_source_info_for_objfile (objfile); | |
381 | } | |
c4e86dd4 | 382 | |
62f29fda | 383 | g_source_cache.clear (); |
c4e86dd4 | 384 | last_source_visited = NULL; |
c906108c SS |
385 | } |
386 | ||
387 | void | |
fba45db2 | 388 | init_source_path (void) |
c906108c SS |
389 | { |
390 | char buf[20]; | |
391 | ||
08850b56 | 392 | xsnprintf (buf, sizeof (buf), "$cdir%c$cwd", DIRNAME_SEPARATOR); |
4fcf66da | 393 | source_path = xstrdup (buf); |
c906108c SS |
394 | forget_cached_source_info (); |
395 | } | |
396 | ||
397 | /* Add zero or more directories to the front of the source path. */ | |
c5aa993b | 398 | |
28da1647 | 399 | static void |
5fed81ff | 400 | directory_command (const char *dirname, int from_tty) |
c906108c SS |
401 | { |
402 | dont_repeat (); | |
c378eb4e | 403 | /* FIXME, this goes to "delete dir"... */ |
c906108c SS |
404 | if (dirname == 0) |
405 | { | |
80618b99 | 406 | if (!from_tty || query (_("Reinitialize source path to empty? "))) |
c906108c | 407 | { |
b8c9b27d | 408 | xfree (source_path); |
c906108c SS |
409 | init_source_path (); |
410 | } | |
411 | } | |
412 | else | |
413 | { | |
414 | mod_path (dirname, &source_path); | |
c4e86dd4 | 415 | forget_cached_source_info (); |
c906108c SS |
416 | } |
417 | if (from_tty) | |
99e7ae30 | 418 | show_directories_1 ((char *) 0, from_tty); |
c906108c SS |
419 | } |
420 | ||
13d35ae5 AS |
421 | /* Add a path given with the -d command line switch. |
422 | This will not be quoted so we must not treat spaces as separators. */ | |
423 | ||
424 | void | |
5614fb77 | 425 | directory_switch (const char *dirname, int from_tty) |
13d35ae5 AS |
426 | { |
427 | add_path (dirname, &source_path, 0); | |
428 | } | |
429 | ||
c906108c SS |
430 | /* Add zero or more directories to the front of an arbitrary path. */ |
431 | ||
432 | void | |
5614fb77 | 433 | mod_path (const char *dirname, char **which_path) |
c04e0a08 JJ |
434 | { |
435 | add_path (dirname, which_path, 1); | |
436 | } | |
437 | ||
438 | /* Workhorse of mod_path. Takes an extra argument to determine | |
439 | if dirname should be parsed for separators that indicate multiple | |
440 | directories. This allows for interfaces that pre-parse the dirname | |
441 | and allow specification of traditional separator characters such | |
c378eb4e | 442 | as space or tab. */ |
c04e0a08 JJ |
443 | |
444 | void | |
5614fb77 | 445 | add_path (const char *dirname, char **which_path, int parse_separators) |
c906108c SS |
446 | { |
447 | char *old = *which_path; | |
448 | int prefix = 0; | |
e80aaf61 | 449 | std::vector<gdb::unique_xmalloc_ptr<char>> dir_vec; |
c906108c SS |
450 | |
451 | if (dirname == 0) | |
452 | return; | |
453 | ||
13d35ae5 AS |
454 | if (parse_separators) |
455 | { | |
456 | /* This will properly parse the space and tab separators | |
e4ab2fad | 457 | and any quotes that may exist. */ |
773a1edc | 458 | gdb_argv argv (dirname); |
e4ab2fad | 459 | |
773a1edc TT |
460 | for (char *arg : argv) |
461 | dirnames_to_char_ptr_vec_append (&dir_vec, arg); | |
13d35ae5 AS |
462 | } |
463 | else | |
e80aaf61 | 464 | dir_vec.emplace_back (xstrdup (dirname)); |
c906108c | 465 | |
e80aaf61 | 466 | for (const gdb::unique_xmalloc_ptr<char> &name_up : dir_vec) |
c906108c | 467 | { |
e80aaf61 | 468 | char *name = name_up.get (); |
aa1ee363 | 469 | char *p; |
c906108c | 470 | struct stat st; |
db68fbe2 | 471 | gdb::unique_xmalloc_ptr<char> new_name_holder; |
c906108c | 472 | |
e4ab2fad JK |
473 | /* Spaces and tabs will have been removed by buildargv(). |
474 | NAME is the start of the directory. | |
475 | P is the '\0' following the end. */ | |
476 | p = name + strlen (name); | |
13d35ae5 AS |
477 | |
478 | while (!(IS_DIR_SEPARATOR (*name) && p <= name + 1) /* "/" */ | |
c3690141 | 479 | #ifdef HAVE_DOS_BASED_FILE_SYSTEM |
7be570e7 | 480 | /* On MS-DOS and MS-Windows, h:\ is different from h: */ |
13d35ae5 | 481 | && !(p == name + 3 && name[1] == ':') /* "d:/" */ |
7be570e7 | 482 | #endif |
13d35ae5 | 483 | && IS_DIR_SEPARATOR (p[-1])) |
c378eb4e | 484 | /* Sigh. "foo/" => "foo" */ |
c906108c | 485 | --p; |
c906108c SS |
486 | *p = '\0'; |
487 | ||
7be570e7 | 488 | while (p > name && p[-1] == '.') |
c906108c SS |
489 | { |
490 | if (p - name == 1) | |
491 | { | |
492 | /* "." => getwd (). */ | |
493 | name = current_directory; | |
494 | goto append; | |
495 | } | |
fe4e3eb8 | 496 | else if (p > name + 1 && IS_DIR_SEPARATOR (p[-2])) |
c906108c SS |
497 | { |
498 | if (p - name == 2) | |
499 | { | |
500 | /* "/." => "/". */ | |
501 | *--p = '\0'; | |
502 | goto append; | |
503 | } | |
504 | else | |
505 | { | |
506 | /* "...foo/." => "...foo". */ | |
507 | p -= 2; | |
508 | *p = '\0'; | |
509 | continue; | |
510 | } | |
511 | } | |
512 | else | |
513 | break; | |
514 | } | |
515 | ||
516 | if (name[0] == '~') | |
db68fbe2 | 517 | new_name_holder.reset (tilde_expand (name)); |
c3690141 | 518 | #ifdef HAVE_DOS_BASED_FILE_SYSTEM |
fe4e3eb8 | 519 | else if (IS_ABSOLUTE_PATH (name) && p == name + 2) /* "d:" => "d:." */ |
db68fbe2 | 520 | new_name_holder.reset (concat (name, ".", (char *) NULL)); |
7be570e7 | 521 | #endif |
fe4e3eb8 | 522 | else if (!IS_ABSOLUTE_PATH (name) && name[0] != '$') |
db68fbe2 TT |
523 | new_name_holder.reset (concat (current_directory, SLASH_STRING, name, |
524 | (char *) NULL)); | |
c906108c | 525 | else |
db68fbe2 TT |
526 | new_name_holder.reset (savestring (name, p - name)); |
527 | name = new_name_holder.get (); | |
c906108c SS |
528 | |
529 | /* Unless it's a variable, check existence. */ | |
c5aa993b JM |
530 | if (name[0] != '$') |
531 | { | |
532 | /* These are warnings, not errors, since we don't want a | |
533 | non-existent directory in a .gdbinit file to stop processing | |
534 | of the .gdbinit file. | |
535 | ||
536 | Whether they get added to the path is more debatable. Current | |
537 | answer is yes, in case the user wants to go make the directory | |
538 | or whatever. If the directory continues to not exist/not be | |
539 | a directory/etc, then having them in the path should be | |
540 | harmless. */ | |
541 | if (stat (name, &st) < 0) | |
542 | { | |
543 | int save_errno = errno; | |
433759f7 | 544 | |
c5aa993b JM |
545 | fprintf_unfiltered (gdb_stderr, "Warning: "); |
546 | print_sys_errmsg (name, save_errno); | |
547 | } | |
548 | else if ((st.st_mode & S_IFMT) != S_IFDIR) | |
8a3fe4f8 | 549 | warning (_("%s is not a directory."), name); |
c5aa993b | 550 | } |
c906108c SS |
551 | |
552 | append: | |
553 | { | |
aa1ee363 | 554 | unsigned int len = strlen (name); |
5ee4ed9f | 555 | char tinybuf[2]; |
c906108c SS |
556 | |
557 | p = *which_path; | |
5e3f4fab | 558 | while (1) |
c906108c | 559 | { |
5e3f4fab EBM |
560 | /* FIXME: we should use realpath() or its work-alike |
561 | before comparing. Then all the code above which | |
562 | removes excess slashes and dots could simply go away. */ | |
563 | if (!filename_ncmp (p, name, len) | |
564 | && (p[len] == '\0' || p[len] == DIRNAME_SEPARATOR)) | |
565 | { | |
566 | /* Found it in the search path, remove old copy. */ | |
567 | if (p > *which_path) | |
568 | { | |
569 | /* Back over leading separator. */ | |
570 | p--; | |
571 | } | |
572 | if (prefix > p - *which_path) | |
573 | { | |
574 | /* Same dir twice in one cmd. */ | |
575 | goto skip_dup; | |
576 | } | |
577 | /* Copy from next '\0' or ':'. */ | |
578 | memmove (p, &p[len + 1], strlen (&p[len + 1]) + 1); | |
579 | } | |
580 | p = strchr (p, DIRNAME_SEPARATOR); | |
581 | if (p != 0) | |
582 | ++p; | |
583 | else | |
584 | break; | |
c906108c | 585 | } |
c906108c | 586 | |
5ee4ed9f JK |
587 | tinybuf[0] = DIRNAME_SEPARATOR; |
588 | tinybuf[1] = '\0'; | |
c906108c | 589 | |
5ee4ed9f JK |
590 | /* If we have already tacked on a name(s) in this command, |
591 | be sure they stay on the front as we tack on some | |
592 | more. */ | |
593 | if (prefix) | |
594 | { | |
595 | char *temp, c; | |
596 | ||
597 | c = old[prefix]; | |
598 | old[prefix] = '\0'; | |
599 | temp = concat (old, tinybuf, name, (char *)NULL); | |
600 | old[prefix] = c; | |
601 | *which_path = concat (temp, "", &old[prefix], (char *) NULL); | |
602 | prefix = strlen (temp); | |
603 | xfree (temp); | |
604 | } | |
605 | else | |
606 | { | |
607 | *which_path = concat (name, (old[0] ? tinybuf : old), | |
608 | old, (char *)NULL); | |
609 | prefix = strlen (name); | |
c906108c | 610 | } |
5ee4ed9f JK |
611 | xfree (old); |
612 | old = *which_path; | |
c906108c | 613 | } |
82ae4854 | 614 | skip_dup: |
b27cf2b3 | 615 | ; |
c5aa993b | 616 | } |
c906108c SS |
617 | } |
618 | ||
619 | ||
620 | static void | |
1d12d88f | 621 | info_source_command (const char *ignore, int from_tty) |
c906108c | 622 | { |
52f0bd74 | 623 | struct symtab *s = current_source_symtab; |
b6577aab | 624 | struct compunit_symtab *cust; |
c906108c SS |
625 | |
626 | if (!s) | |
627 | { | |
a3f17187 | 628 | printf_filtered (_("No current source file.\n")); |
c906108c SS |
629 | return; |
630 | } | |
b6577aab DE |
631 | |
632 | cust = SYMTAB_COMPUNIT (s); | |
a3f17187 | 633 | printf_filtered (_("Current source file is %s\n"), s->filename); |
ee6f8984 DE |
634 | if (SYMTAB_DIRNAME (s) != NULL) |
635 | printf_filtered (_("Compilation directory is %s\n"), SYMTAB_DIRNAME (s)); | |
c906108c | 636 | if (s->fullname) |
a3f17187 | 637 | printf_filtered (_("Located in %s\n"), s->fullname); |
cb44333d TT |
638 | const std::vector<off_t> *offsets; |
639 | if (g_source_cache.get_line_charpos (s, &offsets)) | |
640 | printf_filtered (_("Contains %d line%s.\n"), (int) offsets->size (), | |
641 | offsets->size () == 1 ? "" : "s"); | |
c906108c | 642 | |
a3f17187 | 643 | printf_filtered (_("Source language is %s.\n"), language_str (s->language)); |
b6577aab DE |
644 | printf_filtered (_("Producer is %s.\n"), |
645 | COMPUNIT_PRODUCER (cust) != NULL | |
646 | ? COMPUNIT_PRODUCER (cust) : _("unknown")); | |
43f3e411 | 647 | printf_filtered (_("Compiled with %s debugging format.\n"), |
b6577aab | 648 | COMPUNIT_DEBUGFORMAT (cust)); |
a3f17187 | 649 | printf_filtered (_("%s preprocessor macro info.\n"), |
b6577aab | 650 | COMPUNIT_MACRO_TABLE (cust) != NULL |
43f3e411 | 651 | ? "Includes" : "Does not include"); |
c906108c | 652 | } |
c5aa993b | 653 | \f |
c906108c | 654 | |
f1b620e9 MG |
655 | /* Helper function to remove characters from the start of PATH so that |
656 | PATH can then be appended to a directory name. We remove leading drive | |
657 | letters (for dos) as well as leading '/' characters and './' | |
658 | sequences. */ | |
659 | ||
660 | const char * | |
661 | prepare_path_for_appending (const char *path) | |
662 | { | |
663 | /* For dos paths, d:/foo -> /foo, and d:foo -> foo. */ | |
664 | if (HAS_DRIVE_SPEC (path)) | |
665 | path = STRIP_DRIVE_SPEC (path); | |
666 | ||
667 | const char *old_path; | |
668 | do | |
669 | { | |
670 | old_path = path; | |
671 | ||
672 | /* /foo => foo, to avoid multiple slashes that Emacs doesn't like. */ | |
673 | while (IS_DIR_SEPARATOR(path[0])) | |
674 | path++; | |
675 | ||
676 | /* ./foo => foo */ | |
677 | while (path[0] == '.' && IS_DIR_SEPARATOR (path[1])) | |
678 | path += 2; | |
679 | } | |
680 | while (old_path != path); | |
681 | ||
682 | return path; | |
683 | } | |
684 | ||
c906108c | 685 | /* Open a file named STRING, searching path PATH (dir names sep by some char) |
fbdebf46 JK |
686 | using mode MODE in the calls to open. You cannot use this function to |
687 | create files (O_CREAT). | |
c906108c | 688 | |
014d698b EZ |
689 | OPTS specifies the function behaviour in specific cases. |
690 | ||
691 | If OPF_TRY_CWD_FIRST, try to open ./STRING before searching PATH. | |
c906108c | 692 | (ie pretend the first element of PATH is "."). This also indicates |
e3e06db3 DE |
693 | that, unless OPF_SEARCH_IN_PATH is also specified, a slash in STRING |
694 | disables searching of the path (this is so that "exec-file ./foo" or | |
695 | "symbol-file ./foo" insures that you get that particular version of | |
696 | foo or an error message). | |
c906108c | 697 | |
014d698b EZ |
698 | If OPTS has OPF_SEARCH_IN_PATH set, absolute names will also be |
699 | searched in path (we usually want this for source files but not for | |
700 | executables). | |
701 | ||
e7a8479f | 702 | If FILENAME_OPENED is non-null, set it to a newly allocated string naming |
a89f66e4 | 703 | the actual file opened (this string will always start with a "/"). We |
c906108c SS |
704 | have to take special pains to avoid doubling the "/" between the directory |
705 | and the file, sigh! Emacs gets confuzzed by this when we print the | |
706 | source file name!!! | |
707 | ||
492c0ab7 JK |
708 | If OPTS has OPF_RETURN_REALPATH set return FILENAME_OPENED resolved by |
709 | gdb_realpath. Even without OPF_RETURN_REALPATH this function still returns | |
710 | filename starting with "/". If FILENAME_OPENED is NULL this option has no | |
711 | effect. | |
1f0c4988 | 712 | |
c906108c SS |
713 | If a file is found, return the descriptor. |
714 | Otherwise, return -1, with errno set for the last name we tried to open. */ | |
715 | ||
716 | /* >>>> This should only allow files of certain types, | |
c378eb4e | 717 | >>>> eg executable, non-directory. */ |
c906108c | 718 | int |
24b9144d | 719 | openp (const char *path, openp_flags opts, const char *string, |
e0cc99a6 | 720 | int mode, gdb::unique_xmalloc_ptr<char> *filename_opened) |
c906108c | 721 | { |
52f0bd74 AC |
722 | int fd; |
723 | char *filename; | |
c906108c | 724 | int alloclen; |
79b289e2 PA |
725 | /* The errno set for the last name we tried to open (and |
726 | failed). */ | |
727 | int last_errno = 0; | |
e80aaf61 | 728 | std::vector<gdb::unique_xmalloc_ptr<char>> dir_vec; |
c906108c | 729 | |
fbdebf46 JK |
730 | /* The open syscall MODE parameter is not specified. */ |
731 | gdb_assert ((mode & O_CREAT) == 0); | |
f91e5ac3 JB |
732 | gdb_assert (string != NULL); |
733 | ||
734 | /* A file with an empty name cannot possibly exist. Report a failure | |
735 | without further checking. | |
736 | ||
737 | This is an optimization which also defends us against buggy | |
738 | implementations of the "stat" function. For instance, we have | |
739 | noticed that a MinGW debugger built on Windows XP 32bits crashes | |
740 | when the debugger is started with an empty argument. */ | |
741 | if (string[0] == '\0') | |
742 | { | |
743 | errno = ENOENT; | |
744 | return -1; | |
745 | } | |
fbdebf46 | 746 | |
c906108c SS |
747 | if (!path) |
748 | path = "."; | |
749 | ||
c906108c | 750 | mode |= O_BINARY; |
c906108c | 751 | |
014d698b | 752 | if ((opts & OPF_TRY_CWD_FIRST) || IS_ABSOLUTE_PATH (string)) |
c906108c | 753 | { |
a55411b9 | 754 | int i, reg_file_errno; |
072b1022 | 755 | |
a55411b9 | 756 | if (is_regular_file (string, ®_file_errno)) |
072b1022 | 757 | { |
224c3ddb | 758 | filename = (char *) alloca (strlen (string) + 1); |
072b1022 | 759 | strcpy (filename, string); |
614c279d | 760 | fd = gdb_open_cloexec (filename, mode, 0); |
072b1022 DJ |
761 | if (fd >= 0) |
762 | goto done; | |
a55411b9 | 763 | last_errno = errno; |
072b1022 DJ |
764 | } |
765 | else | |
3f565f1e DJ |
766 | { |
767 | filename = NULL; | |
768 | fd = -1; | |
a55411b9 | 769 | last_errno = reg_file_errno; |
3f565f1e | 770 | } |
072b1022 | 771 | |
014d698b EZ |
772 | if (!(opts & OPF_SEARCH_IN_PATH)) |
773 | for (i = 0; string[i]; i++) | |
774 | if (IS_DIR_SEPARATOR (string[i])) | |
775 | goto done; | |
c906108c SS |
776 | } |
777 | ||
f1b620e9 MG |
778 | /* Remove characters from the start of PATH that we don't need when PATH |
779 | is appended to a directory name. */ | |
780 | string = prepare_path_for_appending (string); | |
c906108c SS |
781 | |
782 | alloclen = strlen (path) + strlen (string) + 2; | |
224c3ddb | 783 | filename = (char *) alloca (alloclen); |
c906108c | 784 | fd = -1; |
79b289e2 | 785 | last_errno = ENOENT; |
e4ab2fad JK |
786 | |
787 | dir_vec = dirnames_to_char_ptr_vec (path); | |
e4ab2fad | 788 | |
e80aaf61 | 789 | for (const gdb::unique_xmalloc_ptr<char> &dir_up : dir_vec) |
c906108c | 790 | { |
e80aaf61 | 791 | char *dir = dir_up.get (); |
e4ab2fad | 792 | size_t len = strlen (dir); |
a55411b9 | 793 | int reg_file_errno; |
c906108c | 794 | |
e4ab2fad | 795 | if (strcmp (dir, "$cwd") == 0) |
c5aa993b JM |
796 | { |
797 | /* Name is $cwd -- insert current directory name instead. */ | |
798 | int newlen; | |
799 | ||
c378eb4e | 800 | /* First, realloc the filename buffer if too short. */ |
c5aa993b JM |
801 | len = strlen (current_directory); |
802 | newlen = len + strlen (string) + 2; | |
803 | if (newlen > alloclen) | |
804 | { | |
805 | alloclen = newlen; | |
224c3ddb | 806 | filename = (char *) alloca (alloclen); |
c5aa993b JM |
807 | } |
808 | strcpy (filename, current_directory); | |
809 | } | |
ebd86fb5 TJB |
810 | else if (strchr(dir, '~')) |
811 | { | |
812 | /* See whether we need to expand the tilde. */ | |
813 | int newlen; | |
ebd86fb5 | 814 | |
ee0c3293 | 815 | gdb::unique_xmalloc_ptr<char> tilde_expanded (tilde_expand (dir)); |
ebd86fb5 TJB |
816 | |
817 | /* First, realloc the filename buffer if too short. */ | |
ee0c3293 | 818 | len = strlen (tilde_expanded.get ()); |
ebd86fb5 TJB |
819 | newlen = len + strlen (string) + 2; |
820 | if (newlen > alloclen) | |
821 | { | |
822 | alloclen = newlen; | |
224c3ddb | 823 | filename = (char *) alloca (alloclen); |
ebd86fb5 | 824 | } |
ee0c3293 | 825 | strcpy (filename, tilde_expanded.get ()); |
ebd86fb5 | 826 | } |
c5aa993b JM |
827 | else |
828 | { | |
829 | /* Normal file name in path -- just use it. */ | |
e4ab2fad | 830 | strcpy (filename, dir); |
08001717 DE |
831 | |
832 | /* Don't search $cdir. It's also a magic path like $cwd, but we | |
833 | don't have enough information to expand it. The user *could* | |
834 | have an actual directory named '$cdir' but handling that would | |
835 | be confusing, it would mean different things in different | |
836 | contexts. If the user really has '$cdir' one can use './$cdir'. | |
837 | We can get $cdir when loading scripts. When loading source files | |
838 | $cdir must have already been expanded to the correct value. */ | |
e4ab2fad | 839 | if (strcmp (dir, "$cdir") == 0) |
08001717 | 840 | continue; |
c906108c | 841 | } |
c906108c | 842 | |
c378eb4e | 843 | /* Remove trailing slashes. */ |
fe4e3eb8 | 844 | while (len > 0 && IS_DIR_SEPARATOR (filename[len - 1])) |
c906108c SS |
845 | filename[--len] = 0; |
846 | ||
c5aa993b | 847 | strcat (filename + len, SLASH_STRING); |
c906108c SS |
848 | strcat (filename, string); |
849 | ||
a55411b9 | 850 | if (is_regular_file (filename, ®_file_errno)) |
5e987968 | 851 | { |
614c279d | 852 | fd = gdb_open_cloexec (filename, mode, 0); |
5e987968 AS |
853 | if (fd >= 0) |
854 | break; | |
79b289e2 | 855 | last_errno = errno; |
5e987968 | 856 | } |
a55411b9 DE |
857 | else |
858 | last_errno = reg_file_errno; | |
c906108c SS |
859 | } |
860 | ||
c5aa993b | 861 | done: |
c906108c SS |
862 | if (filename_opened) |
863 | { | |
f5b95b50 | 864 | /* If a file was opened, canonicalize its filename. */ |
c906108c | 865 | if (fd < 0) |
e0cc99a6 | 866 | filename_opened->reset (NULL); |
04affae3 | 867 | else if ((opts & OPF_RETURN_REALPATH) != 0) |
e0cc99a6 | 868 | *filename_opened = gdb_realpath (filename); |
c906108c | 869 | else |
e0cc99a6 | 870 | *filename_opened = gdb_abspath (filename); |
c906108c | 871 | } |
c906108c | 872 | |
79b289e2 | 873 | errno = last_errno; |
c906108c SS |
874 | return fd; |
875 | } | |
876 | ||
c5aa993b | 877 | |
c906108c SS |
878 | /* This is essentially a convenience, for clients that want the behaviour |
879 | of openp, using source_path, but that really don't want the file to be | |
880 | opened but want instead just to know what the full pathname is (as | |
881 | qualified against source_path). | |
882 | ||
883 | The current working directory is searched first. | |
884 | ||
885 | If the file was found, this function returns 1, and FULL_PATHNAME is | |
886 | set to the fully-qualified pathname. | |
887 | ||
5e987968 | 888 | Else, this functions returns 0, and FULL_PATHNAME is set to NULL. */ |
c906108c | 889 | int |
e0cc99a6 TT |
890 | source_full_path_of (const char *filename, |
891 | gdb::unique_xmalloc_ptr<char> *full_pathname) | |
c906108c | 892 | { |
c5aa993b | 893 | int fd; |
c906108c | 894 | |
492c0ab7 JK |
895 | fd = openp (source_path, |
896 | OPF_TRY_CWD_FIRST | OPF_SEARCH_IN_PATH | OPF_RETURN_REALPATH, | |
897 | filename, O_RDONLY, full_pathname); | |
c906108c SS |
898 | if (fd < 0) |
899 | { | |
e0cc99a6 | 900 | full_pathname->reset (NULL); |
c906108c SS |
901 | return 0; |
902 | } | |
903 | ||
904 | close (fd); | |
905 | return 1; | |
906 | } | |
907 | ||
2f61ca93 JB |
908 | /* Return non-zero if RULE matches PATH, that is if the rule can be |
909 | applied to PATH. */ | |
910 | ||
911 | static int | |
912 | substitute_path_rule_matches (const struct substitute_path_rule *rule, | |
913 | const char *path) | |
914 | { | |
915 | const int from_len = strlen (rule->from); | |
916 | const int path_len = strlen (path); | |
2f61ca93 JB |
917 | |
918 | if (path_len < from_len) | |
919 | return 0; | |
920 | ||
921 | /* The substitution rules are anchored at the start of the path, | |
486ef3b9 | 922 | so the path should start with rule->from. */ |
2f61ca93 | 923 | |
486ef3b9 | 924 | if (filename_ncmp (path, rule->from, from_len) != 0) |
2f61ca93 JB |
925 | return 0; |
926 | ||
927 | /* Make sure that the region in the path that matches the substitution | |
928 | rule is immediately followed by a directory separator (or the end of | |
929 | string character). */ | |
230cd560 | 930 | |
2f61ca93 JB |
931 | if (path[from_len] != '\0' && !IS_DIR_SEPARATOR (path[from_len])) |
932 | return 0; | |
933 | ||
934 | return 1; | |
935 | } | |
936 | ||
937 | /* Find the substitute-path rule that applies to PATH and return it. | |
938 | Return NULL if no rule applies. */ | |
939 | ||
940 | static struct substitute_path_rule * | |
941 | get_substitute_path_rule (const char *path) | |
942 | { | |
943 | struct substitute_path_rule *rule = substitute_path_rules; | |
944 | ||
945 | while (rule != NULL && !substitute_path_rule_matches (rule, path)) | |
946 | rule = rule->next; | |
947 | ||
948 | return rule; | |
949 | } | |
950 | ||
951 | /* If the user specified a source path substitution rule that applies | |
0b581c69 TT |
952 | to PATH, then apply it and return the new path. |
953 | ||
2f61ca93 JB |
954 | Return NULL if no substitution rule was specified by the user, |
955 | or if no rule applied to the given PATH. */ | |
0b581c69 TT |
956 | |
957 | gdb::unique_xmalloc_ptr<char> | |
2f61ca93 JB |
958 | rewrite_source_path (const char *path) |
959 | { | |
960 | const struct substitute_path_rule *rule = get_substitute_path_rule (path); | |
961 | char *new_path; | |
962 | int from_len; | |
963 | ||
964 | if (rule == NULL) | |
965 | return NULL; | |
966 | ||
967 | from_len = strlen (rule->from); | |
968 | ||
969 | /* Compute the rewritten path and return it. */ | |
970 | ||
971 | new_path = | |
972 | (char *) xmalloc (strlen (path) + 1 + strlen (rule->to) - from_len); | |
973 | strcpy (new_path, rule->to); | |
974 | strcat (new_path, path + from_len); | |
975 | ||
0b581c69 | 976 | return gdb::unique_xmalloc_ptr<char> (new_path); |
2f61ca93 JB |
977 | } |
978 | ||
2179fbc3 TT |
979 | /* See source.h. */ |
980 | ||
981 | scoped_fd | |
e2357892 | 982 | find_and_open_source (const char *filename, |
5e987968 | 983 | const char *dirname, |
e0cc99a6 | 984 | gdb::unique_xmalloc_ptr<char> *fullname) |
c906108c SS |
985 | { |
986 | char *path = source_path; | |
31889e00 | 987 | const char *p; |
c906108c | 988 | int result; |
c906108c | 989 | |
c378eb4e | 990 | /* Quick way out if we already know its full name. */ |
2f61ca93 | 991 | |
57c22c6c | 992 | if (*fullname) |
c906108c | 993 | { |
2f61ca93 JB |
994 | /* The user may have requested that source paths be rewritten |
995 | according to substitution rules he provided. If a substitution | |
996 | rule applies to this path, then apply it. */ | |
e0cc99a6 TT |
997 | gdb::unique_xmalloc_ptr<char> rewritten_fullname |
998 | = rewrite_source_path (fullname->get ()); | |
2f61ca93 JB |
999 | |
1000 | if (rewritten_fullname != NULL) | |
e0cc99a6 | 1001 | *fullname = std::move (rewritten_fullname); |
2f61ca93 | 1002 | |
e0cc99a6 | 1003 | result = gdb_open_cloexec (fullname->get (), OPEN_MODE, 0); |
c906108c | 1004 | if (result >= 0) |
bc3aa6c3 | 1005 | { |
a0c1ffed TT |
1006 | if (basenames_may_differ) |
1007 | *fullname = gdb_realpath (fullname->get ()); | |
1008 | else | |
1009 | *fullname = gdb_abspath (fullname->get ()); | |
2179fbc3 | 1010 | return scoped_fd (result); |
bc3aa6c3 DE |
1011 | } |
1012 | ||
c378eb4e | 1013 | /* Didn't work -- free old one, try again. */ |
e0cc99a6 | 1014 | fullname->reset (NULL); |
c906108c SS |
1015 | } |
1016 | ||
0b581c69 | 1017 | gdb::unique_xmalloc_ptr<char> rewritten_dirname; |
57c22c6c | 1018 | if (dirname != NULL) |
c906108c | 1019 | { |
2f61ca93 JB |
1020 | /* If necessary, rewrite the compilation directory name according |
1021 | to the source path substitution rules specified by the user. */ | |
1022 | ||
0b581c69 | 1023 | rewritten_dirname = rewrite_source_path (dirname); |
2f61ca93 JB |
1024 | |
1025 | if (rewritten_dirname != NULL) | |
0b581c69 TT |
1026 | dirname = rewritten_dirname.get (); |
1027 | ||
c378eb4e MS |
1028 | /* Replace a path entry of $cdir with the compilation directory |
1029 | name. */ | |
c906108c | 1030 | #define cdir_len 5 |
33d0e35a | 1031 | p = strstr (source_path, "$cdir"); |
c906108c | 1032 | if (p && (p == path || p[-1] == DIRNAME_SEPARATOR) |
c5aa993b | 1033 | && (p[cdir_len] == DIRNAME_SEPARATOR || p[cdir_len] == '\0')) |
c906108c SS |
1034 | { |
1035 | int len; | |
1036 | ||
1037 | path = (char *) | |
57c22c6c | 1038 | alloca (strlen (source_path) + 1 + strlen (dirname) + 1); |
c906108c | 1039 | len = p - source_path; |
c5aa993b | 1040 | strncpy (path, source_path, len); /* Before $cdir */ |
3e43a32a MS |
1041 | strcpy (path + len, dirname); /* new stuff */ |
1042 | strcat (path + len, source_path + len + cdir_len); /* After | |
1043 | $cdir */ | |
c906108c SS |
1044 | } |
1045 | } | |
8da2a1df | 1046 | |
4fa0265e РИ |
1047 | gdb::unique_xmalloc_ptr<char> rewritten_filename |
1048 | = rewrite_source_path (filename); | |
56163ce1 | 1049 | |
4fa0265e РИ |
1050 | if (rewritten_filename != NULL) |
1051 | filename = rewritten_filename.get (); | |
c906108c | 1052 | |
a0c1ffed TT |
1053 | openp_flags flags = OPF_SEARCH_IN_PATH; |
1054 | if (basenames_may_differ) | |
1055 | flags |= OPF_RETURN_REALPATH; | |
f1b620e9 MG |
1056 | |
1057 | /* Try to locate file using filename. */ | |
a0c1ffed | 1058 | result = openp (path, flags, filename, OPEN_MODE, fullname); |
f1b620e9 MG |
1059 | if (result < 0 && dirname != NULL) |
1060 | { | |
1061 | /* Remove characters from the start of PATH that we don't need when | |
1062 | PATH is appended to a directory name. */ | |
1063 | const char *filename_start = prepare_path_for_appending (filename); | |
1064 | ||
1065 | /* Try to locate file using compilation dir + filename. This is | |
1066 | helpful if part of the compilation directory was removed, | |
1067 | e.g. using gcc's -fdebug-prefix-map, and we have added the missing | |
1068 | prefix to source_path. */ | |
1069 | std::string cdir_filename (dirname); | |
1070 | ||
1071 | /* Remove any trailing directory separators. */ | |
1072 | while (IS_DIR_SEPARATOR (cdir_filename.back ())) | |
1073 | cdir_filename.pop_back (); | |
1074 | ||
1075 | /* Add our own directory separator. */ | |
1076 | cdir_filename.append (SLASH_STRING); | |
1077 | cdir_filename.append (filename_start); | |
1078 | ||
1079 | result = openp (path, flags, cdir_filename.c_str (), OPEN_MODE, | |
1080 | fullname); | |
1081 | } | |
c906108c SS |
1082 | if (result < 0) |
1083 | { | |
c378eb4e | 1084 | /* Didn't work. Try using just the basename. */ |
57c22c6c BR |
1085 | p = lbasename (filename); |
1086 | if (p != filename) | |
a0c1ffed | 1087 | result = openp (path, flags, p, OPEN_MODE, fullname); |
c906108c | 1088 | } |
c906108c | 1089 | |
2179fbc3 | 1090 | return scoped_fd (result); |
c906108c SS |
1091 | } |
1092 | ||
57c22c6c BR |
1093 | /* Open a source file given a symtab S. Returns a file descriptor or |
1094 | negative number for error. | |
1095 | ||
c378eb4e | 1096 | This function is a convience function to find_and_open_source. */ |
57c22c6c | 1097 | |
2179fbc3 | 1098 | scoped_fd |
57c22c6c BR |
1099 | open_source_file (struct symtab *s) |
1100 | { | |
5e987968 | 1101 | if (!s) |
2179fbc3 | 1102 | return scoped_fd (-1); |
57c22c6c | 1103 | |
54460946 | 1104 | gdb::unique_xmalloc_ptr<char> fullname (s->fullname); |
8e6a5953 | 1105 | s->fullname = NULL; |
2179fbc3 TT |
1106 | scoped_fd fd = find_and_open_source (s->filename, SYMTAB_DIRNAME (s), |
1107 | &fullname); | |
e0cc99a6 TT |
1108 | s->fullname = fullname.release (); |
1109 | return fd; | |
57c22c6c BR |
1110 | } |
1111 | ||
1112 | /* Finds the fullname that a symtab represents. | |
c906108c | 1113 | |
f35a17b5 JK |
1114 | This functions finds the fullname and saves it in s->fullname. |
1115 | It will also return the value. | |
57c22c6c BR |
1116 | |
1117 | If this function fails to find the file that this symtab represents, | |
f35a17b5 JK |
1118 | the expected fullname is used. Therefore the files does not have to |
1119 | exist. */ | |
256f06f3 | 1120 | |
0b0865da | 1121 | const char * |
57c22c6c | 1122 | symtab_to_fullname (struct symtab *s) |
c906108c | 1123 | { |
256f06f3 DE |
1124 | /* Use cached copy if we have it. |
1125 | We rely on forget_cached_source_info being called appropriately | |
1126 | to handle cases like the file being moved. */ | |
f35a17b5 | 1127 | if (s->fullname == NULL) |
5e987968 | 1128 | { |
2179fbc3 | 1129 | scoped_fd fd = open_source_file (s); |
f35a17b5 | 1130 | |
2179fbc3 | 1131 | if (fd.get () < 0) |
f0a4b570 | 1132 | { |
0b581c69 | 1133 | gdb::unique_xmalloc_ptr<char> fullname; |
f0a4b570 JK |
1134 | |
1135 | /* rewrite_source_path would be applied by find_and_open_source, we | |
1136 | should report the pathname where GDB tried to find the file. */ | |
1137 | ||
ee6f8984 | 1138 | if (SYMTAB_DIRNAME (s) == NULL || IS_ABSOLUTE_PATH (s->filename)) |
0b581c69 | 1139 | fullname.reset (xstrdup (s->filename)); |
f0a4b570 | 1140 | else |
0b581c69 TT |
1141 | fullname.reset (concat (SYMTAB_DIRNAME (s), SLASH_STRING, |
1142 | s->filename, (char *) NULL)); | |
f0a4b570 | 1143 | |
0b581c69 | 1144 | s->fullname = rewrite_source_path (fullname.get ()).release (); |
f0a4b570 | 1145 | if (s->fullname == NULL) |
0b581c69 | 1146 | s->fullname = fullname.release (); |
f0a4b570 | 1147 | } |
f35a17b5 | 1148 | } |
c906108c | 1149 | |
f35a17b5 | 1150 | return s->fullname; |
57c22c6c | 1151 | } |
1b56eb55 JK |
1152 | |
1153 | /* See commentary in source.h. */ | |
1154 | ||
1155 | const char * | |
1156 | symtab_to_filename_for_display (struct symtab *symtab) | |
1157 | { | |
1158 | if (filename_display_string == filename_display_basename) | |
1159 | return lbasename (symtab->filename); | |
1160 | else if (filename_display_string == filename_display_absolute) | |
1161 | return symtab_to_fullname (symtab); | |
1162 | else if (filename_display_string == filename_display_relative) | |
1163 | return symtab->filename; | |
1164 | else | |
1165 | internal_error (__FILE__, __LINE__, _("invalid filename_display_string")); | |
1166 | } | |
c906108c | 1167 | |
00df30ae AB |
1168 | \f |
1169 | ||
c906108c | 1170 | /* Print source lines from the file of symtab S, |
c378eb4e | 1171 | starting with line number LINE and stopping before line number STOPLINE. */ |
c906108c SS |
1172 | |
1173 | static void | |
dfaae886 | 1174 | print_source_lines_base (struct symtab *s, int line, int stopline, |
8d297bbf | 1175 | print_source_lines_flags flags) |
c906108c | 1176 | { |
b08b16c8 | 1177 | bool noprint = false; |
c906108c | 1178 | int nlines = stopline - line; |
79a45e25 | 1179 | struct ui_out *uiout = current_uiout; |
c906108c | 1180 | |
c378eb4e | 1181 | /* Regardless of whether we can open the file, set current_source_symtab. */ |
c906108c SS |
1182 | current_source_symtab = s; |
1183 | current_source_line = line; | |
1184 | first_line_listed = line; | |
1185 | ||
3e43a32a | 1186 | /* If printing of source lines is disabled, just print file and line |
c378eb4e | 1187 | number. */ |
112e8700 | 1188 | if (uiout->test_flags (ui_source_list)) |
8b93c638 | 1189 | { |
c378eb4e | 1190 | /* Only prints "No such file or directory" once. */ |
b08b16c8 | 1191 | if (s == last_source_visited) |
c5aa993b | 1192 | { |
b08b16c8 | 1193 | if (last_source_error) |
2179fbc3 | 1194 | { |
b08b16c8 TT |
1195 | flags |= PRINT_SOURCE_LINES_NOERROR; |
1196 | noprint = true; | |
2179fbc3 | 1197 | } |
c5aa993b JM |
1198 | } |
1199 | else | |
1200 | { | |
b08b16c8 TT |
1201 | last_source_visited = s; |
1202 | scoped_fd desc = open_source_file (s); | |
1203 | last_source_error = desc.get () < 0; | |
1204 | if (last_source_error) | |
1205 | noprint = true; | |
c5aa993b | 1206 | } |
8b93c638 JM |
1207 | } |
1208 | else | |
1209 | { | |
8d297bbf | 1210 | flags |= PRINT_SOURCE_LINES_NOERROR; |
b08b16c8 | 1211 | noprint = true; |
8b93c638 | 1212 | } |
c906108c | 1213 | |
2179fbc3 | 1214 | if (noprint) |
c906108c | 1215 | { |
dfaae886 | 1216 | if (!(flags & PRINT_SOURCE_LINES_NOERROR)) |
c5aa993b | 1217 | { |
05cba821 JK |
1218 | const char *filename = symtab_to_filename_for_display (s); |
1219 | int len = strlen (filename) + 100; | |
224c3ddb | 1220 | char *name = (char *) alloca (len); |
08850b56 | 1221 | |
05cba821 | 1222 | xsnprintf (name, len, "%d\t%s", line, filename); |
c906108c SS |
1223 | print_sys_errmsg (name, errno); |
1224 | } | |
1225 | else | |
fc0ae648 | 1226 | { |
381befee | 1227 | uiout->field_signed ("line", line); |
112e8700 | 1228 | uiout->text ("\tin "); |
56d397a3 | 1229 | |
23eb71e4 JK |
1230 | /* CLI expects only the "file" field. TUI expects only the |
1231 | "fullname" field (and TUI does break if "file" is printed). | |
1232 | MI expects both fields. ui_source_list is set only for CLI, | |
1233 | not for TUI. */ | |
112e8700 | 1234 | if (uiout->is_mi_like_p () || uiout->test_flags (ui_source_list)) |
cbe56571 | 1235 | uiout->field_string ("file", symtab_to_filename_for_display (s), |
e43b10e1 | 1236 | file_name_style.style ()); |
112e8700 | 1237 | if (uiout->is_mi_like_p () || !uiout->test_flags (ui_source_list)) |
8f1b8b82 JK |
1238 | { |
1239 | const char *s_fullname = symtab_to_fullname (s); | |
1240 | char *local_fullname; | |
1241 | ||
1242 | /* ui_out_field_string may free S_FULLNAME by calling | |
1243 | open_source_file for it again. See e.g., | |
1244 | tui_field_string->tui_show_source. */ | |
224c3ddb | 1245 | local_fullname = (char *) alloca (strlen (s_fullname) + 1); |
8f1b8b82 JK |
1246 | strcpy (local_fullname, s_fullname); |
1247 | ||
112e8700 | 1248 | uiout->field_string ("fullname", local_fullname); |
8f1b8b82 | 1249 | } |
23eb71e4 | 1250 | |
112e8700 | 1251 | uiout->text ("\n"); |
fc0ae648 | 1252 | } |
c906108c SS |
1253 | |
1254 | return; | |
1255 | } | |
1256 | ||
ec98a4ad | 1257 | /* If the user requested a sequence of lines that seems to go backward |
0e2a2133 AB |
1258 | (from high to low line numbers) then we don't print anything. */ |
1259 | if (stopline <= line) | |
ec98a4ad AB |
1260 | return; |
1261 | ||
62f29fda TT |
1262 | std::string lines; |
1263 | if (!g_source_cache.get_source_lines (s, line, stopline - 1, &lines)) | |
cb44333d TT |
1264 | { |
1265 | const std::vector<off_t> *offsets = nullptr; | |
1266 | g_source_cache.get_line_charpos (s, &offsets); | |
1267 | error (_("Line number %d out of range; %s has %d lines."), | |
1268 | line, symtab_to_filename_for_display (s), | |
1269 | offsets == nullptr ? 0 : (int) offsets->size ()); | |
1270 | } | |
c906108c | 1271 | |
62f29fda | 1272 | const char *iter = lines.c_str (); |
a0087920 | 1273 | while (nlines-- > 0 && *iter != '\0') |
c906108c | 1274 | { |
8b93c638 JM |
1275 | char buf[20]; |
1276 | ||
8b93c638 | 1277 | last_line_listed = current_source_line; |
4cd29721 MM |
1278 | if (flags & PRINT_SOURCE_LINES_FILENAME) |
1279 | { | |
112e8700 SM |
1280 | uiout->text (symtab_to_filename_for_display (s)); |
1281 | uiout->text (":"); | |
4cd29721 | 1282 | } |
08850b56 | 1283 | xsnprintf (buf, sizeof (buf), "%d\t", current_source_line++); |
112e8700 | 1284 | uiout->text (buf); |
a0087920 TT |
1285 | |
1286 | while (*iter != '\0') | |
8b93c638 | 1287 | { |
a0087920 TT |
1288 | /* Find a run of characters that can be emitted at once. |
1289 | This is done so that escape sequences are kept | |
1290 | together. */ | |
1291 | const char *start = iter; | |
1292 | while (true) | |
8b93c638 | 1293 | { |
a0087920 TT |
1294 | int skip_bytes; |
1295 | ||
1296 | char c = *iter; | |
1297 | if (c == '\033' && skip_ansi_escape (iter, &skip_bytes)) | |
1298 | iter += skip_bytes; | |
62160ec9 | 1299 | else if (c >= 0 && c < 040 && c != '\t') |
a0087920 TT |
1300 | break; |
1301 | else if (c == 0177) | |
1302 | break; | |
1303 | else | |
1304 | ++iter; | |
8b93c638 | 1305 | } |
a0087920 | 1306 | if (iter > start) |
8b93c638 | 1307 | { |
a0087920 TT |
1308 | std::string text (start, iter); |
1309 | uiout->text (text.c_str ()); | |
8b93c638 | 1310 | } |
a0087920 TT |
1311 | if (*iter == '\r') |
1312 | { | |
1313 | /* Treat either \r or \r\n as a single newline. */ | |
1314 | ++iter; | |
1315 | if (*iter == '\n') | |
1316 | ++iter; | |
1317 | break; | |
1318 | } | |
1319 | else if (*iter == '\n') | |
1320 | { | |
1321 | ++iter; | |
1322 | break; | |
1323 | } | |
1324 | else if (*iter > 0 && *iter < 040) | |
8b93c638 | 1325 | { |
a0087920 | 1326 | xsnprintf (buf, sizeof (buf), "^%c", *iter + 0100); |
112e8700 | 1327 | uiout->text (buf); |
62160ec9 | 1328 | ++iter; |
8b93c638 | 1329 | } |
a0087920 | 1330 | else if (*iter == 0177) |
62160ec9 TT |
1331 | { |
1332 | uiout->text ("^?"); | |
1333 | ++iter; | |
1334 | } | |
8b93c638 | 1335 | } |
a0087920 | 1336 | uiout->text ("\n"); |
c906108c | 1337 | } |
c906108c SS |
1338 | } |
1339 | \f | |
583068ca AB |
1340 | |
1341 | /* See source.h. */ | |
c906108c | 1342 | |
c5aa993b | 1343 | void |
dfaae886 | 1344 | print_source_lines (struct symtab *s, int line, int stopline, |
8d297bbf | 1345 | print_source_lines_flags flags) |
c906108c | 1346 | { |
dfaae886 | 1347 | print_source_lines_base (s, line, stopline, flags); |
c906108c | 1348 | } |
0e2a2133 AB |
1349 | |
1350 | /* See source.h. */ | |
1351 | ||
1352 | void | |
1353 | print_source_lines (struct symtab *s, source_lines_range line_range, | |
1354 | print_source_lines_flags flags) | |
1355 | { | |
1356 | print_source_lines_base (s, line_range.startline (), | |
1357 | line_range.stopline (), flags); | |
1358 | } | |
1359 | ||
1360 | ||
c906108c | 1361 | \f |
c906108c SS |
1362 | /* Print info on range of pc's in a specified line. */ |
1363 | ||
1364 | static void | |
1d12d88f | 1365 | info_line_command (const char *arg, int from_tty) |
c906108c | 1366 | { |
c906108c | 1367 | CORE_ADDR start_pc, end_pc; |
c906108c | 1368 | |
6c5b2ebe PA |
1369 | std::vector<symtab_and_line> decoded_sals; |
1370 | symtab_and_line curr_sal; | |
1371 | gdb::array_view<symtab_and_line> sals; | |
c906108c SS |
1372 | |
1373 | if (arg == 0) | |
1374 | { | |
6c5b2ebe PA |
1375 | curr_sal.symtab = current_source_symtab; |
1376 | curr_sal.pspace = current_program_space; | |
5166082f | 1377 | if (last_line_listed != 0) |
6c5b2ebe | 1378 | curr_sal.line = last_line_listed; |
5166082f | 1379 | else |
6c5b2ebe | 1380 | curr_sal.line = current_source_line; |
5166082f | 1381 | |
6c5b2ebe | 1382 | sals = curr_sal; |
c906108c SS |
1383 | } |
1384 | else | |
1385 | { | |
6c5b2ebe PA |
1386 | decoded_sals = decode_line_with_last_displayed (arg, |
1387 | DECODE_LINE_LIST_MODE); | |
1388 | sals = decoded_sals; | |
c5aa993b | 1389 | |
c906108c SS |
1390 | dont_repeat (); |
1391 | } | |
1392 | ||
1393 | /* C++ More than one line may have been specified, as when the user | |
c378eb4e | 1394 | specifies an overloaded function name. Print info on them all. */ |
6c5b2ebe | 1395 | for (const auto &sal : sals) |
c906108c | 1396 | { |
f8eba3c6 TT |
1397 | if (sal.pspace != current_program_space) |
1398 | continue; | |
c5aa993b | 1399 | |
c906108c SS |
1400 | if (sal.symtab == 0) |
1401 | { | |
5af949e3 UW |
1402 | struct gdbarch *gdbarch = get_current_arch (); |
1403 | ||
a3f17187 | 1404 | printf_filtered (_("No line number information available")); |
c906108c SS |
1405 | if (sal.pc != 0) |
1406 | { | |
1407 | /* This is useful for "info line *0x7f34". If we can't tell the | |
c5aa993b JM |
1408 | user about a source line, at least let them have the symbolic |
1409 | address. */ | |
c906108c SS |
1410 | printf_filtered (" for address "); |
1411 | wrap_here (" "); | |
5af949e3 | 1412 | print_address (gdbarch, sal.pc, gdb_stdout); |
c906108c SS |
1413 | } |
1414 | else | |
1415 | printf_filtered ("."); | |
1416 | printf_filtered ("\n"); | |
1417 | } | |
1418 | else if (sal.line > 0 | |
1419 | && find_line_pc_range (sal, &start_pc, &end_pc)) | |
1420 | { | |
eb822aa6 DE |
1421 | struct gdbarch *gdbarch |
1422 | = get_objfile_arch (SYMTAB_OBJFILE (sal.symtab)); | |
5af949e3 | 1423 | |
c906108c SS |
1424 | if (start_pc == end_pc) |
1425 | { | |
1426 | printf_filtered ("Line %d of \"%s\"", | |
05cba821 JK |
1427 | sal.line, |
1428 | symtab_to_filename_for_display (sal.symtab)); | |
c906108c SS |
1429 | wrap_here (" "); |
1430 | printf_filtered (" is at address "); | |
5af949e3 | 1431 | print_address (gdbarch, start_pc, gdb_stdout); |
c906108c SS |
1432 | wrap_here (" "); |
1433 | printf_filtered (" but contains no code.\n"); | |
1434 | } | |
1435 | else | |
1436 | { | |
1437 | printf_filtered ("Line %d of \"%s\"", | |
05cba821 JK |
1438 | sal.line, |
1439 | symtab_to_filename_for_display (sal.symtab)); | |
c906108c SS |
1440 | wrap_here (" "); |
1441 | printf_filtered (" starts at address "); | |
5af949e3 | 1442 | print_address (gdbarch, start_pc, gdb_stdout); |
c906108c SS |
1443 | wrap_here (" "); |
1444 | printf_filtered (" and ends at "); | |
5af949e3 | 1445 | print_address (gdbarch, end_pc, gdb_stdout); |
c906108c SS |
1446 | printf_filtered (".\n"); |
1447 | } | |
1448 | ||
1449 | /* x/i should display this line's code. */ | |
5af949e3 | 1450 | set_next_address (gdbarch, start_pc); |
c906108c SS |
1451 | |
1452 | /* Repeating "info line" should do the following line. */ | |
1453 | last_line_listed = sal.line + 1; | |
1454 | ||
1455 | /* If this is the only line, show the source code. If it could | |
1456 | not find the file, don't do anything special. */ | |
0d3abd8c AB |
1457 | if (sals.size () == 1) |
1458 | annotate_source_line (sal.symtab, sal.line, 0, start_pc); | |
c906108c SS |
1459 | } |
1460 | else | |
1461 | /* Is there any case in which we get here, and have an address | |
1462 | which the user would want to see? If we have debugging symbols | |
1463 | and no line numbers? */ | |
a3f17187 | 1464 | printf_filtered (_("Line number %d is out of range for \"%s\".\n"), |
05cba821 | 1465 | sal.line, symtab_to_filename_for_display (sal.symtab)); |
c906108c | 1466 | } |
c906108c SS |
1467 | } |
1468 | \f | |
1469 | /* Commands to search the source file for a regexp. */ | |
1470 | ||
73e8dc90 PA |
1471 | /* Helper for forward_search_command/reverse_search_command. FORWARD |
1472 | indicates direction: true for forward, false for | |
1473 | backward/reverse. */ | |
1474 | ||
c906108c | 1475 | static void |
73e8dc90 | 1476 | search_command_helper (const char *regex, int from_tty, bool forward) |
c906108c | 1477 | { |
73e8dc90 | 1478 | const char *msg = re_comp (regex); |
c906108c | 1479 | if (msg) |
8a3fe4f8 | 1480 | error (("%s"), msg); |
c906108c SS |
1481 | |
1482 | if (current_source_symtab == 0) | |
1483 | select_source_symtab (0); | |
1484 | ||
cb44333d | 1485 | scoped_fd desc (open_source_file (current_source_symtab)); |
77ad7394 | 1486 | if (desc.get () < 0) |
05cba821 | 1487 | perror_with_name (symtab_to_filename_for_display (current_source_symtab)); |
c906108c | 1488 | |
73e8dc90 PA |
1489 | int line = (forward |
1490 | ? last_line_listed + 1 | |
1491 | : last_line_listed - 1); | |
1492 | ||
cb44333d TT |
1493 | const std::vector<off_t> *offsets; |
1494 | if (line < 1 | |
1495 | || !g_source_cache.get_line_charpos (current_source_symtab, &offsets) | |
1496 | || line > offsets->size ()) | |
9fe4a216 | 1497 | error (_("Expression not found")); |
c906108c | 1498 | |
cb44333d | 1499 | if (lseek (desc.get (), (*offsets)[line - 1], 0) < 0) |
05cba821 | 1500 | perror_with_name (symtab_to_filename_for_display (current_source_symtab)); |
c906108c | 1501 | |
2179fbc3 | 1502 | gdb_file_up stream = desc.to_file (FDOPEN_MODE); |
4a45905b | 1503 | clearerr (stream.get ()); |
73e8dc90 PA |
1504 | |
1505 | gdb::def_vector<char> buf; | |
1506 | buf.reserve (256); | |
1507 | ||
c5aa993b JM |
1508 | while (1) |
1509 | { | |
73e8dc90 | 1510 | buf.resize (0); |
c5aa993b | 1511 | |
73e8dc90 | 1512 | int c = fgetc (stream.get ()); |
c5aa993b JM |
1513 | if (c == EOF) |
1514 | break; | |
1515 | do | |
c906108c | 1516 | { |
73e8dc90 | 1517 | buf.push_back (c); |
c906108c | 1518 | } |
4a45905b | 1519 | while (c != '\n' && (c = fgetc (stream.get ())) >= 0); |
c906108c | 1520 | |
7be570e7 JM |
1521 | /* Remove the \r, if any, at the end of the line, otherwise |
1522 | regular expressions that end with $ or \n won't work. */ | |
73e8dc90 PA |
1523 | size_t sz = buf.size (); |
1524 | if (sz >= 2 && buf[sz - 2] == '\r') | |
7be570e7 | 1525 | { |
73e8dc90 PA |
1526 | buf[sz - 2] = '\n'; |
1527 | buf.resize (sz - 1); | |
7be570e7 | 1528 | } |
7be570e7 | 1529 | |
c378eb4e | 1530 | /* We now have a source line in buf, null terminate and match. */ |
73e8dc90 PA |
1531 | buf.push_back ('\0'); |
1532 | if (re_exec (buf.data ()) > 0) | |
c5aa993b | 1533 | { |
c378eb4e | 1534 | /* Match! */ |
c5aa993b | 1535 | print_source_lines (current_source_symtab, line, line + 1, 0); |
4fa62494 | 1536 | set_internalvar_integer (lookup_internalvar ("_"), line); |
325fac50 | 1537 | current_source_line = std::max (line - lines_to_list / 2, 1); |
c5aa993b JM |
1538 | return; |
1539 | } | |
73e8dc90 PA |
1540 | |
1541 | if (forward) | |
1542 | line++; | |
1543 | else | |
1544 | { | |
1545 | line--; | |
b18ca514 PW |
1546 | if (line < 1) |
1547 | break; | |
cb44333d | 1548 | if (fseek (stream.get (), (*offsets)[line - 1], 0) < 0) |
73e8dc90 PA |
1549 | { |
1550 | const char *filename | |
1551 | = symtab_to_filename_for_display (current_source_symtab); | |
1552 | perror_with_name (filename); | |
1553 | } | |
1554 | } | |
c5aa993b | 1555 | } |
c906108c | 1556 | |
a3f17187 | 1557 | printf_filtered (_("Expression not found\n")); |
c906108c SS |
1558 | } |
1559 | ||
c906108c | 1560 | static void |
73e8dc90 | 1561 | forward_search_command (const char *regex, int from_tty) |
c906108c | 1562 | { |
73e8dc90 PA |
1563 | search_command_helper (regex, from_tty, true); |
1564 | } | |
c906108c | 1565 | |
73e8dc90 PA |
1566 | static void |
1567 | reverse_search_command (const char *regex, int from_tty) | |
1568 | { | |
1569 | search_command_helper (regex, from_tty, false); | |
c906108c | 1570 | } |
2f61ca93 JB |
1571 | |
1572 | /* If the last character of PATH is a directory separator, then strip it. */ | |
1573 | ||
1574 | static void | |
1575 | strip_trailing_directory_separator (char *path) | |
1576 | { | |
1577 | const int last = strlen (path) - 1; | |
1578 | ||
1579 | if (last < 0) | |
1580 | return; /* No stripping is needed if PATH is the empty string. */ | |
1581 | ||
1582 | if (IS_DIR_SEPARATOR (path[last])) | |
1583 | path[last] = '\0'; | |
1584 | } | |
1585 | ||
1586 | /* Return the path substitution rule that matches FROM. | |
1587 | Return NULL if no rule matches. */ | |
1588 | ||
1589 | static struct substitute_path_rule * | |
1590 | find_substitute_path_rule (const char *from) | |
1591 | { | |
1592 | struct substitute_path_rule *rule = substitute_path_rules; | |
1593 | ||
1594 | while (rule != NULL) | |
1595 | { | |
1596 | if (FILENAME_CMP (rule->from, from) == 0) | |
1597 | return rule; | |
1598 | rule = rule->next; | |
1599 | } | |
1600 | ||
1601 | return NULL; | |
1602 | } | |
1603 | ||
1604 | /* Add a new substitute-path rule at the end of the current list of rules. | |
1605 | The new rule will replace FROM into TO. */ | |
1606 | ||
29b0e8a2 | 1607 | void |
2f61ca93 JB |
1608 | add_substitute_path_rule (char *from, char *to) |
1609 | { | |
1610 | struct substitute_path_rule *rule; | |
8d749320 | 1611 | struct substitute_path_rule *new_rule = XNEW (struct substitute_path_rule); |
2f61ca93 | 1612 | |
2f61ca93 JB |
1613 | new_rule->from = xstrdup (from); |
1614 | new_rule->to = xstrdup (to); | |
1615 | new_rule->next = NULL; | |
1616 | ||
1617 | /* If the list of rules are empty, then insert the new rule | |
1618 | at the head of the list. */ | |
1619 | ||
1620 | if (substitute_path_rules == NULL) | |
1621 | { | |
1622 | substitute_path_rules = new_rule; | |
1623 | return; | |
1624 | } | |
1625 | ||
1626 | /* Otherwise, skip to the last rule in our list and then append | |
1627 | the new rule. */ | |
1628 | ||
1629 | rule = substitute_path_rules; | |
1630 | while (rule->next != NULL) | |
1631 | rule = rule->next; | |
1632 | ||
1633 | rule->next = new_rule; | |
1634 | } | |
1635 | ||
1636 | /* Remove the given source path substitution rule from the current list | |
1637 | of rules. The memory allocated for that rule is also deallocated. */ | |
1638 | ||
1639 | static void | |
1640 | delete_substitute_path_rule (struct substitute_path_rule *rule) | |
1641 | { | |
1642 | if (rule == substitute_path_rules) | |
1643 | substitute_path_rules = rule->next; | |
1644 | else | |
1645 | { | |
1646 | struct substitute_path_rule *prev = substitute_path_rules; | |
1647 | ||
1648 | while (prev != NULL && prev->next != rule) | |
1649 | prev = prev->next; | |
1650 | ||
1651 | gdb_assert (prev != NULL); | |
1652 | ||
1653 | prev->next = rule->next; | |
1654 | } | |
1655 | ||
1656 | xfree (rule->from); | |
1657 | xfree (rule->to); | |
1658 | xfree (rule); | |
1659 | } | |
1660 | ||
1661 | /* Implement the "show substitute-path" command. */ | |
1662 | ||
1663 | static void | |
a0d65762 | 1664 | show_substitute_path_command (const char *args, int from_tty) |
2f61ca93 JB |
1665 | { |
1666 | struct substitute_path_rule *rule = substitute_path_rules; | |
2f61ca93 JB |
1667 | char *from = NULL; |
1668 | ||
773a1edc | 1669 | gdb_argv argv (args); |
2f61ca93 JB |
1670 | |
1671 | /* We expect zero or one argument. */ | |
1672 | ||
1673 | if (argv != NULL && argv[0] != NULL && argv[1] != NULL) | |
1674 | error (_("Too many arguments in command")); | |
1675 | ||
1676 | if (argv != NULL && argv[0] != NULL) | |
1677 | from = argv[0]; | |
1678 | ||
1679 | /* Print the substitution rules. */ | |
1680 | ||
1681 | if (from != NULL) | |
1682 | printf_filtered | |
1683 | (_("Source path substitution rule matching `%s':\n"), from); | |
1684 | else | |
1685 | printf_filtered (_("List of all source path substitution rules:\n")); | |
1686 | ||
1687 | while (rule != NULL) | |
1688 | { | |
1e2ccb61 | 1689 | if (from == NULL || substitute_path_rule_matches (rule, from) != 0) |
2f61ca93 JB |
1690 | printf_filtered (" `%s' -> `%s'.\n", rule->from, rule->to); |
1691 | rule = rule->next; | |
1692 | } | |
1693 | } | |
1694 | ||
1695 | /* Implement the "unset substitute-path" command. */ | |
1696 | ||
1697 | static void | |
a0d65762 | 1698 | unset_substitute_path_command (const char *args, int from_tty) |
2f61ca93 JB |
1699 | { |
1700 | struct substitute_path_rule *rule = substitute_path_rules; | |
773a1edc | 1701 | gdb_argv argv (args); |
2f61ca93 JB |
1702 | char *from = NULL; |
1703 | int rule_found = 0; | |
1704 | ||
1705 | /* This function takes either 0 or 1 argument. */ | |
1706 | ||
1707 | if (argv != NULL && argv[0] != NULL && argv[1] != NULL) | |
1708 | error (_("Incorrect usage, too many arguments in command")); | |
1709 | ||
1710 | if (argv != NULL && argv[0] != NULL) | |
1711 | from = argv[0]; | |
1712 | ||
1713 | /* If the user asked for all the rules to be deleted, ask him | |
1714 | to confirm and give him a chance to abort before the action | |
1715 | is performed. */ | |
1716 | ||
1717 | if (from == NULL | |
1718 | && !query (_("Delete all source path substitution rules? "))) | |
1719 | error (_("Canceled")); | |
1720 | ||
1721 | /* Delete the rule matching the argument. No argument means that | |
1722 | all rules should be deleted. */ | |
1723 | ||
1724 | while (rule != NULL) | |
1725 | { | |
1726 | struct substitute_path_rule *next = rule->next; | |
1727 | ||
1728 | if (from == NULL || FILENAME_CMP (from, rule->from) == 0) | |
1729 | { | |
1730 | delete_substitute_path_rule (rule); | |
1731 | rule_found = 1; | |
1732 | } | |
1733 | ||
1734 | rule = next; | |
1735 | } | |
1736 | ||
1737 | /* If the user asked for a specific rule to be deleted but | |
1738 | we could not find it, then report an error. */ | |
1739 | ||
1740 | if (from != NULL && !rule_found) | |
1741 | error (_("No substitution rule defined for `%s'"), from); | |
c4e86dd4 DJ |
1742 | |
1743 | forget_cached_source_info (); | |
2f61ca93 JB |
1744 | } |
1745 | ||
1746 | /* Add a new source path substitution rule. */ | |
1747 | ||
1748 | static void | |
a0d65762 | 1749 | set_substitute_path_command (const char *args, int from_tty) |
2f61ca93 | 1750 | { |
2f61ca93 JB |
1751 | struct substitute_path_rule *rule; |
1752 | ||
773a1edc | 1753 | gdb_argv argv (args); |
2f61ca93 JB |
1754 | |
1755 | if (argv == NULL || argv[0] == NULL || argv [1] == NULL) | |
1756 | error (_("Incorrect usage, too few arguments in command")); | |
1757 | ||
1758 | if (argv[2] != NULL) | |
1759 | error (_("Incorrect usage, too many arguments in command")); | |
1760 | ||
1761 | if (*(argv[0]) == '\0') | |
1762 | error (_("First argument must be at least one character long")); | |
1763 | ||
1764 | /* Strip any trailing directory separator character in either FROM | |
1765 | or TO. The substitution rule already implicitly contains them. */ | |
1766 | strip_trailing_directory_separator (argv[0]); | |
1767 | strip_trailing_directory_separator (argv[1]); | |
1768 | ||
1769 | /* If a rule with the same "from" was previously defined, then | |
1770 | delete it. This new rule replaces it. */ | |
1771 | ||
1772 | rule = find_substitute_path_rule (argv[0]); | |
1773 | if (rule != NULL) | |
1774 | delete_substitute_path_rule (rule); | |
1775 | ||
1776 | /* Insert the new substitution rule. */ | |
1777 | ||
1778 | add_substitute_path_rule (argv[0], argv[1]); | |
c4e86dd4 | 1779 | forget_cached_source_info (); |
2f61ca93 JB |
1780 | } |
1781 | ||
0e2a2133 AB |
1782 | /* See source.h. */ |
1783 | ||
1784 | source_lines_range::source_lines_range (int startline, | |
1785 | source_lines_range::direction dir) | |
1786 | { | |
1787 | if (dir == source_lines_range::FORWARD) | |
1788 | { | |
1789 | LONGEST end = static_cast <LONGEST> (startline) + get_lines_to_list (); | |
1790 | ||
1791 | if (end > INT_MAX) | |
1792 | end = INT_MAX; | |
1793 | ||
1794 | m_startline = startline; | |
1795 | m_stopline = static_cast <int> (end); | |
1796 | } | |
1797 | else | |
1798 | { | |
1799 | LONGEST start = static_cast <LONGEST> (startline) - get_lines_to_list (); | |
1800 | ||
1801 | if (start < 1) | |
1802 | start = 1; | |
1803 | ||
1804 | m_startline = static_cast <int> (start); | |
1805 | m_stopline = startline; | |
1806 | } | |
1807 | } | |
1808 | ||
c906108c SS |
1809 | \f |
1810 | void | |
fba45db2 | 1811 | _initialize_source (void) |
c906108c SS |
1812 | { |
1813 | struct cmd_list_element *c; | |
433759f7 | 1814 | |
c906108c SS |
1815 | current_source_symtab = 0; |
1816 | init_source_path (); | |
1817 | ||
1818 | /* The intention is to use POSIX Basic Regular Expressions. | |
1819 | Always use the GNU regex routine for consistency across all hosts. | |
1820 | Our current GNU regex.c does not have all the POSIX features, so this is | |
1821 | just an approximation. */ | |
1822 | re_set_syntax (RE_SYNTAX_GREP); | |
1823 | ||
1a966eab AC |
1824 | c = add_cmd ("directory", class_files, directory_command, _("\ |
1825 | Add directory DIR to beginning of search path for source files.\n\ | |
c906108c SS |
1826 | Forget cached info on source file locations and line positions.\n\ |
1827 | DIR can also be $cwd for the current working directory, or $cdir for the\n\ | |
1828 | directory in which the source file was compiled into object code.\n\ | |
1a966eab | 1829 | With no argument, reset the search path to $cdir:$cwd, the default."), |
c906108c SS |
1830 | &cmdlist); |
1831 | ||
1832 | if (dbx_commands) | |
c5aa993b | 1833 | add_com_alias ("use", "directory", class_files, 0); |
c906108c | 1834 | |
5ba2abeb | 1835 | set_cmd_completer (c, filename_completer); |
c906108c | 1836 | |
99e7ae30 DE |
1837 | add_setshow_optional_filename_cmd ("directories", |
1838 | class_files, | |
1839 | &source_path, | |
1840 | _("\ | |
1841 | Set the search path for finding source files."), | |
1842 | _("\ | |
1843 | Show the search path for finding source files."), | |
1844 | _("\ | |
c906108c | 1845 | $cwd in the path means the current working directory.\n\ |
99e7ae30 DE |
1846 | $cdir in the path means the compilation directory of the source file.\n\ |
1847 | GDB ensures the search path always ends with $cdir:$cwd by\n\ | |
1848 | appending these directories if necessary.\n\ | |
1849 | Setting the value to an empty string sets it to $cdir:$cwd, the default."), | |
1850 | set_directories_command, | |
1851 | show_directories_command, | |
1852 | &setlist, &showlist); | |
c906108c | 1853 | |
11db9430 | 1854 | add_info ("source", info_source_command, |
1bedd215 | 1855 | _("Information about the current source file.")); |
c906108c | 1856 | |
11db9430 | 1857 | add_info ("line", info_line_command, _("\ |
1bedd215 | 1858 | Core addresses of the code for a source line.\n\ |
c906108c SS |
1859 | Line can be specified as\n\ |
1860 | LINENUM, to list around that line in current file,\n\ | |
1861 | FILE:LINENUM, to list around that line in that file,\n\ | |
1862 | FUNCTION, to list around beginning of that function,\n\ | |
1863 | FILE:FUNCTION, to distinguish among like-named static functions.\n\ | |
c906108c SS |
1864 | Default is to describe the last source line that was listed.\n\n\ |
1865 | This sets the default address for \"x\" to the line's first instruction\n\ | |
1866 | so that \"x/i\" suffices to start examining the machine code.\n\ | |
1bedd215 | 1867 | The address is also stored as the value of \"$_\".")); |
c906108c | 1868 | |
1bedd215 AC |
1869 | add_com ("forward-search", class_files, forward_search_command, _("\ |
1870 | Search for regular expression (see regex(3)) from last line listed.\n\ | |
1871 | The matching line number is also stored as the value of \"$_\".")); | |
c906108c | 1872 | add_com_alias ("search", "forward-search", class_files, 0); |
1e96de83 | 1873 | add_com_alias ("fo", "forward-search", class_files, 1); |
c906108c | 1874 | |
1bedd215 AC |
1875 | add_com ("reverse-search", class_files, reverse_search_command, _("\ |
1876 | Search backward for regular expression (see regex(3)) from last line listed.\n\ | |
1877 | The matching line number is also stored as the value of \"$_\".")); | |
dd304d53 | 1878 | add_com_alias ("rev", "reverse-search", class_files, 1); |
c906108c | 1879 | |
7f7cc265 | 1880 | add_setshow_integer_cmd ("listsize", class_support, &lines_to_list, _("\ |
35096d9d | 1881 | Set number of source lines gdb will list by default."), _("\ |
f81d1120 PA |
1882 | Show number of source lines gdb will list by default."), _("\ |
1883 | Use this to choose how many source lines the \"list\" displays (unless\n\ | |
1884 | the \"list\" argument explicitly specifies some other number).\n\ | |
1885 | A value of \"unlimited\", or zero, means there's no limit."), | |
7f7cc265 PA |
1886 | NULL, |
1887 | show_lines_to_list, | |
1888 | &setlist, &showlist); | |
2f61ca93 JB |
1889 | |
1890 | add_cmd ("substitute-path", class_files, set_substitute_path_command, | |
1891 | _("\ | |
590042fc | 1892 | Add a substitution rule to rewrite the source directories.\n\ |
7ef2b397 | 1893 | Usage: set substitute-path FROM TO\n\ |
590042fc PW |
1894 | The rule is applied only if the directory name starts with FROM\n\ |
1895 | directly followed by a directory separator.\n\ | |
7ef2b397 JB |
1896 | If a substitution rule was previously set for FROM, the old rule\n\ |
1897 | is replaced by the new one."), | |
1898 | &setlist); | |
2f61ca93 JB |
1899 | |
1900 | add_cmd ("substitute-path", class_files, unset_substitute_path_command, | |
1901 | _("\ | |
590042fc | 1902 | Delete one or all substitution rules rewriting the source directories.\n\ |
7ef2b397 | 1903 | Usage: unset substitute-path [FROM]\n\ |
590042fc | 1904 | Delete the rule for substituting FROM in source directories. If FROM\n\ |
7ef2b397 JB |
1905 | is not specified, all substituting rules are deleted.\n\ |
1906 | If the debugger cannot find a rule for FROM, it will display a warning."), | |
2f61ca93 JB |
1907 | &unsetlist); |
1908 | ||
1909 | add_cmd ("substitute-path", class_files, show_substitute_path_command, | |
7ef2b397 | 1910 | _("\ |
590042fc | 1911 | Show one or all substitution rules rewriting the source directories.\n\ |
7ef2b397 | 1912 | Usage: show substitute-path [FROM]\n\ |
590042fc | 1913 | Print the rule for substituting FROM in source directories. If FROM\n\ |
7ef2b397 | 1914 | is not specified, print all substitution rules."), |
2f61ca93 | 1915 | &showlist); |
1b56eb55 JK |
1916 | |
1917 | add_setshow_enum_cmd ("filename-display", class_files, | |
1918 | filename_display_kind_names, | |
1919 | &filename_display_string, _("\ | |
1920 | Set how to display filenames."), _("\ | |
1921 | Show how to display filenames."), _("\ | |
1922 | filename-display can be:\n\ | |
1923 | basename - display only basename of a filename\n\ | |
1924 | relative - display a filename relative to the compilation directory\n\ | |
1925 | absolute - display an absolute filename\n\ | |
1926 | By default, relative filenames are displayed."), | |
1927 | NULL, | |
1928 | show_filename_display_string, | |
1929 | &setlist, &showlist); | |
1930 | ||
c906108c | 1931 | } |