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