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