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