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