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