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