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