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