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