* configure: Move stray line back to where it belongs.
[deliverable/binutils-gdb.git] / gdb / source.c
1 /* List lines of source files for GDB, the GNU debugger.
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.
5
6 This file is part of GDB.
7
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.
12
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.
17
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. */
22
23 #include "defs.h"
24 #include "symtab.h"
25 #include "expression.h"
26 #include "language.h"
27 #include "command.h"
28 #include "source.h"
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>
37 #include "gdbcore.h"
38 #include "gdb_regex.h"
39 #include "symfile.h"
40 #include "objfiles.h"
41 #include "annotate.h"
42 #include "gdbtypes.h"
43 #include "linespec.h"
44 #include "filenames.h" /* for DOSish file names */
45 #include "completer.h"
46 #include "ui-out.h"
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
68 /* Prototypes for exported functions. */
69
70 void _initialize_source (void);
71
72 /* Prototypes for local functions. */
73
74 static int get_filename_and_charpos (struct symtab *, char **);
75
76 static void reverse_search_command (char *, int);
77
78 static void forward_search_command (char *, int);
79
80 static void line_info (char *, int);
81
82 static void ambiguous_line_spec (struct symtabs_and_lines *);
83
84 static void source_info (char *, int);
85
86 static void show_directories (char *, int);
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
95 static struct symtab *current_source_symtab;
96
97 /* Default next line to list. */
98
99 static int current_source_line;
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;
123 \f
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;
155 cursal.pc = 0;
156 cursal.end = 0;
157
158 return cursal;
159 }
160
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.
163 It may err out if a default cannot be determined.
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. */
168
169 void
170 set_default_source_symtab_and_line (void)
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);
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.)
184 and set the current default to whatever is in SAL.
185 NOTE: The returned sal pc and end fields are not valid. */
186
187 struct symtab_and_line
188 set_current_source_symtab_and_line (const struct symtab_and_line *sal)
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;
197 cursal.pc = 0;
198 cursal.end = 0;
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 }
211
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
221 select_source_symtab (register struct symtab *s)
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;
228
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. */
241 if (lookup_symbol (main_name (), 0, VAR_NAMESPACE, 0, NULL))
242 {
243 sals = decode_line_spec (main_name (), 1);
244 sal = sals.sals[0];
245 xfree (sals.sals);
246 current_source_symtab = sal.symtab;
247 current_source_line = max (sal.line - (lines_to_list - 1), 1);
248 if (current_source_symtab)
249 return;
250 }
251
252 /* All right; find the last file in the symtab list (ignoring .h's). */
253
254 current_source_line = 1;
255
256 for (ofp = object_files; ofp != NULL; ofp = ofp->next)
257 {
258 for (s = ofp->symtabs; s; s = s->next)
259 {
260 char *name = s->filename;
261 int len = strlen (name);
262 if (!(len > 2 && (STREQ (&name[len - 2], ".h"))))
263 {
264 current_source_symtab = s;
265 }
266 }
267 }
268 if (current_source_symtab)
269 return;
270
271 /* Howabout the partial symbol tables? */
272
273 for (ofp = object_files; ofp != NULL; ofp = ofp->next)
274 {
275 for (ps = ofp->psymtabs; ps != NULL; ps = ps->next)
276 {
277 char *name = ps->filename;
278 int len = strlen (name);
279 if (!(len > 2 && (STREQ (&name[len - 2], ".h"))))
280 {
281 cs_pst = ps;
282 }
283 }
284 }
285 if (cs_pst)
286 {
287 if (cs_pst->readin)
288 {
289 internal_error (__FILE__, __LINE__,
290 "select_source_symtab: "
291 "readin pst found and no symtabs.");
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
305 show_directories (char *ignore, int from_tty)
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
317 forget_cached_source_info (void)
318 {
319 register struct symtab *s;
320 register struct objfile *objfile;
321 struct partial_symtab *pst;
322
323 for (objfile = object_files; objfile != NULL; objfile = objfile->next)
324 {
325 for (s = objfile->symtabs; s != NULL; s = s->next)
326 {
327 if (s->line_charpos != NULL)
328 {
329 xmfree (objfile->md, s->line_charpos);
330 s->line_charpos = NULL;
331 }
332 if (s->fullname != NULL)
333 {
334 xmfree (objfile->md, s->fullname);
335 s->fullname = NULL;
336 }
337 }
338
339 ALL_OBJFILE_PSYMTABS (objfile, pst)
340 {
341 if (pst->fullname != NULL)
342 {
343 xfree (pst->fullname);
344 pst->fullname = NULL;
345 }
346 }
347 }
348 }
349
350 void
351 init_source_path (void)
352 {
353 char buf[20];
354
355 sprintf (buf, "$cdir%c$cwd", DIRNAME_SEPARATOR);
356 source_path = xstrdup (buf);
357 forget_cached_source_info ();
358 }
359
360 /* Add zero or more directories to the front of the source path. */
361
362 void
363 directory_command (char *dirname, int from_tty)
364 {
365 dont_repeat ();
366 /* FIXME, this goes to "delete dir"... */
367 if (dirname == 0)
368 {
369 if (from_tty && query ("Reinitialize source path to empty? "))
370 {
371 xfree (source_path);
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)
381 show_directories ((char *) 0, from_tty);
382 forget_cached_source_info ();
383 }
384
385 /* Add zero or more directories to the front of an arbitrary path. */
386
387 void
388 mod_path (char *dirname, char **which_path)
389 {
390 char *old = *which_path;
391 int prefix = 0;
392
393 if (dirname == 0)
394 return;
395
396 dirname = xstrdup (dirname);
397 make_cleanup (xfree, dirname);
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
410 if (separator == 0 && space == 0 && tab == 0)
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
429 if (!(IS_DIR_SEPARATOR (*name) && p <= name + 1) /* "/" */
430 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
431 /* On MS-DOS and MS-Windows, h:\ is different from h: */
432 && !(p == name + 3 && name[1] == ':') /* "d:/" */
433 #endif
434 && IS_DIR_SEPARATOR (p[-1]))
435 /* Sigh. "foo/" => "foo" */
436 --p;
437 *p = '\0';
438
439 while (p > name && p[-1] == '.')
440 {
441 if (p - name == 1)
442 {
443 /* "." => getwd (). */
444 name = current_directory;
445 goto append;
446 }
447 else if (p > name + 1 && IS_DIR_SEPARATOR (p[-2]))
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);
469 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
470 else if (IS_ABSOLUTE_PATH (name) && p == name + 2) /* "d:" => "d:." */
471 name = concat (name, ".", NULL);
472 #endif
473 else if (!IS_ABSOLUTE_PATH (name) && name[0] != '$')
474 name = concat (current_directory, SLASH_STRING, name, NULL);
475 else
476 name = savestring (name, p - name);
477 make_cleanup (xfree, name);
478
479 /* Unless it's a variable, check existence. */
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 }
500
501 append:
502 {
503 register unsigned int len = strlen (name);
504
505 p = *which_path;
506 while (1)
507 {
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. */
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)
521 p--; /* Back over leading separator */
522 if (prefix > p - *which_path)
523 goto skip_dup; /* Same dir twice in one cmd */
524 strcpy (p, &p[len + 1]); /* Copy from next \0 or : */
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
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. */
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);
550 xfree (temp);
551 }
552 else
553 {
554 *which_path = concat (name, (old[0] ? tinybuf : old), old, NULL);
555 prefix = strlen (name);
556 }
557 xfree (old);
558 old = *which_path;
559 }
560 }
561 skip_dup:;
562 }
563 while (*dirname != '\0');
564 }
565
566
567 static void
568 source_info (char *ignore, int from_tty)
569 {
570 register struct symtab *s = current_source_symtab;
571
572 if (!s)
573 {
574 printf_filtered ("No current source file.\n");
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);
588 printf_filtered ("%s preprocessor macro info.\n",
589 s->macro_table ? "Includes" : "Does not include");
590 }
591 \f
592
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 }
610
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
620 If FILENAME_OPENED is non-null, set it to a newly allocated string naming
621 the actual file opened (this string will always start with a "/"). We
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,
630 >>>> eg executable, non-directory */
631 int
632 openp (const char *path, int try_cwd_first, const char *string,
633 int mode, int prot,
634 char **filename_opened)
635 {
636 register int fd;
637 register char *filename;
638 const char *p;
639 const char *p1;
640 register int len;
641 int alloclen;
642
643 if (!path)
644 path = ".";
645
646 #if defined(_WIN32) || defined(__CYGWIN__)
647 mode |= O_BINARY;
648 #endif
649
650 if ((try_cwd_first || IS_ABSOLUTE_PATH (string)) && is_regular_file (string))
651 {
652 int i;
653 filename = alloca (strlen (string) + 1);
654 strcpy (filename, string);
655 fd = open (filename, mode, prot);
656 if (fd >= 0)
657 goto done;
658 for (i = 0; string[i]; i++)
659 if (IS_DIR_SEPARATOR (string[i]))
660 goto done;
661 }
662
663 /* ./foo => foo */
664 while (string[0] == '.' && IS_DIR_SEPARATOR (string[1]))
665 string += 2;
666
667 alloclen = strlen (path) + strlen (string) + 2;
668 filename = alloca (alloclen);
669 fd = -1;
670 for (p = path; p; p = p1 ? p1 + 1 : 0)
671 {
672 p1 = strchr (p, DIRNAME_SEPARATOR);
673 if (p1)
674 len = p1 - p;
675 else
676 len = strlen (p);
677
678 if (len == 4 && p[0] == '$' && p[1] == 'c'
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;
690 filename = alloca (alloclen);
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;
699 }
700
701 /* Remove trailing slashes */
702 while (len > 0 && IS_DIR_SEPARATOR (filename[len - 1]))
703 filename[--len] = 0;
704
705 strcat (filename + len, SLASH_STRING);
706 strcat (filename, string);
707
708 if (is_regular_file (filename))
709 {
710 fd = open (filename, mode);
711 if (fd >= 0)
712 break;
713 }
714 }
715
716 done:
717 if (filename_opened)
718 {
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. */
724 if (fd < 0)
725 *filename_opened = NULL;
726 else if (IS_ABSOLUTE_PATH (filename))
727 *filename_opened = xfullpath (filename);
728 else
729 {
730 /* Beware the // my son, the Emacs barfs, the botch that catch... */
731
732 char *f = concat (current_directory,
733 IS_DIR_SEPARATOR (current_directory[strlen (current_directory) - 1])
734 ? "" : SLASH_STRING,
735 filename, NULL);
736 *filename_opened = xfullpath (f);
737 xfree (f);
738 }
739 }
740
741 return fd;
742 }
743
744
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.
756 */
757 int
758 source_full_path_of (char *filename, char **full_pathname)
759 {
760 int fd;
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
778 open_source_file (struct symtab *s)
779 {
780 char *path = source_path;
781 const char *p;
782 int result;
783 char *fullname;
784
785 /* Quick way out if we already know its full name */
786 if (s->fullname)
787 {
788 result = open (s->fullname, OPEN_MODE);
789 if (result >= 0)
790 return result;
791 /* Didn't work -- free old one, try again. */
792 xmfree (s->objfile->md, s->fullname);
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,
801 which produces a "required warning" when assigned to a nonconst. */
802 p = (char *) strstr (source_path, "$cdir");
803 if (p && (p == path || p[-1] == DIRNAME_SEPARATOR)
804 && (p[cdir_len] == DIRNAME_SEPARATOR || p[cdir_len] == '\0'))
805 {
806 int len;
807
808 path = (char *)
809 alloca (strlen (source_path) + 1 + strlen (s->dirname) + 1);
810 len = p - source_path;
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 */
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. */
821 p = lbasename (s->filename);
822 if (p != s->filename)
823 result = openp (path, 0, p, OPEN_MODE, 0, &s->fullname);
824 }
825
826 if (result >= 0)
827 {
828 fullname = s->fullname;
829 s->fullname = mstrsave (s->objfile->md, s->fullname);
830 xfree (fullname);
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 *
839 symtab_to_filename (struct symtab *s)
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 }
862 \f
863
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
870 find_source_lines (struct symtab *s, int desc)
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
880 line_charpos = (int *) xmmalloc (s->objfile->md,
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)
886 mtime = bfd_get_mtime (s->objfile->obfd);
887 else if (exec_bfd)
888 mtime = bfd_get_mtime (exec_bfd);
889
890 if (mtime && mtime < st.st_mtime)
891 {
892 warning ("Source file is more recent than executable.\n");
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;
903 while (myread (desc, &c, 1) > 0)
904 {
905 if (c == '\n')
906 {
907 if (nlines == lines_allocated)
908 {
909 lines_allocated *= 2;
910 line_charpos =
911 (int *) xmrealloc (s->objfile->md, (char *) line_charpos,
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);
929 old_cleanups = make_cleanup (xfree, data);
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'
942 /* A newline at the end does not start a new line. */
943 && p != end)
944 {
945 if (nlines == lines_allocated)
946 {
947 lines_allocated *= 2;
948 line_charpos =
949 (int *) xmrealloc (s->objfile->md, (char *) line_charpos,
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 =
960 (int *) xmrealloc (s->objfile->md, (char *) line_charpos,
961 nlines * sizeof (int));
962
963 }
964
965 /* Return the character position of a line LINE in symtab S.
966 Return 0 if anything is invalid. */
967
968 #if 0 /* Currently unused */
969
970 int
971 source_line_charpos (struct symtab *s, int line)
972 {
973 if (!s)
974 return 0;
975 if (!s->line_charpos || line <= 0)
976 return 0;
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
985 source_charpos_line (register struct symtab *s, register int chr)
986 {
987 register int line = 0;
988 register int *lnp;
989
990 if (s == 0 || s->line_charpos == 0)
991 return 0;
992 lnp = s->line_charpos;
993 /* Files are usually short, so sequential search is Ok */
994 while (line < s->nlines && *lnp <= chr)
995 {
996 line++;
997 lnp++;
998 }
999 if (line >= s->nlines)
1000 line = s->nlines;
1001 return line;
1002 }
1003
1004 #endif /* 0 */
1005 \f
1006
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
1013 get_filename_and_charpos (struct symtab *s, char **fullname)
1014 {
1015 register int desc, linenums_changed = 0;
1016
1017 desc = open_source_file (s);
1018 if (desc < 0)
1019 {
1020 if (fullname)
1021 *fullname = NULL;
1022 return 0;
1023 }
1024 if (fullname)
1025 *fullname = s->fullname;
1026 if (s->line_charpos == 0)
1027 linenums_changed = 1;
1028 if (linenums_changed)
1029 find_source_lines (s, desc);
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
1044 identify_source_line (struct symtab *s, int line, int mid_statement,
1045 CORE_ADDR pc)
1046 {
1047 if (s->line_charpos == 0)
1048 get_filename_and_charpos (s, (char **) NULL);
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 }
1063 \f
1064
1065 /* Print source lines from the file of symtab S,
1066 starting with line number LINE and stopping before line number STOPLINE. */
1067
1068 static void print_source_lines_base (struct symtab *s, int line, int stopline,
1069 int noerror);
1070 static void
1071 print_source_lines_base (struct symtab *s, int line, int stopline, int noerror)
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
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 {
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 }
1097 }
1098 else
1099 {
1100 desc = -1;
1101 noerror = 1;
1102 }
1103
1104 if (desc < 0)
1105 {
1106 last_source_error = desc;
1107
1108 if (!noerror)
1109 {
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
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");
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 {
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);
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
1192 void
1193 print_source_lines (struct symtab *s, int line, int stopline, int noerror)
1194 {
1195 print_source_lines_base (s, line, stopline, noerror);
1196 }
1197 \f
1198 /* Print a list of files and line numbers which a user may choose from
1199 in order to list a function which was specified ambiguously (as with
1200 `list classname::overloadedfuncname', for example). The vector in
1201 SALS provides the filenames and line numbers. */
1202
1203 static void
1204 ambiguous_line_spec (struct symtabs_and_lines *sals)
1205 {
1206 int i;
1207
1208 for (i = 0; i < sals->nelts; ++i)
1209 printf_filtered ("file: \"%s\", line number: %d\n",
1210 sals->sals[i].symtab->filename, sals->sals[i].line);
1211 }
1212 \f
1213 /* Print info on range of pc's in a specified line. */
1214
1215 static void
1216 line_info (char *arg, int from_tty)
1217 {
1218 struct symtabs_and_lines sals;
1219 struct symtab_and_line sal;
1220 CORE_ADDR start_pc, end_pc;
1221 int i;
1222
1223 INIT_SAL (&sal); /* initialize to zeroes */
1224
1225 if (arg == 0)
1226 {
1227 sal.symtab = current_source_symtab;
1228 sal.line = last_line_listed;
1229 sals.nelts = 1;
1230 sals.sals = (struct symtab_and_line *)
1231 xmalloc (sizeof (struct symtab_and_line));
1232 sals.sals[0] = sal;
1233 }
1234 else
1235 {
1236 sals = decode_line_spec_1 (arg, 0);
1237
1238 dont_repeat ();
1239 }
1240
1241 /* C++ More than one line may have been specified, as when the user
1242 specifies an overloaded function name. Print info on them all. */
1243 for (i = 0; i < sals.nelts; i++)
1244 {
1245 sal = sals.sals[i];
1246
1247 if (sal.symtab == 0)
1248 {
1249 printf_filtered ("No line number information available");
1250 if (sal.pc != 0)
1251 {
1252 /* This is useful for "info line *0x7f34". If we can't tell the
1253 user about a source line, at least let them have the symbolic
1254 address. */
1255 printf_filtered (" for address ");
1256 wrap_here (" ");
1257 print_address (sal.pc, gdb_stdout);
1258 }
1259 else
1260 printf_filtered (".");
1261 printf_filtered ("\n");
1262 }
1263 else if (sal.line > 0
1264 && find_line_pc_range (sal, &start_pc, &end_pc))
1265 {
1266 if (start_pc == end_pc)
1267 {
1268 printf_filtered ("Line %d of \"%s\"",
1269 sal.line, sal.symtab->filename);
1270 wrap_here (" ");
1271 printf_filtered (" is at address ");
1272 print_address (start_pc, gdb_stdout);
1273 wrap_here (" ");
1274 printf_filtered (" but contains no code.\n");
1275 }
1276 else
1277 {
1278 printf_filtered ("Line %d of \"%s\"",
1279 sal.line, sal.symtab->filename);
1280 wrap_here (" ");
1281 printf_filtered (" starts at address ");
1282 print_address (start_pc, gdb_stdout);
1283 wrap_here (" ");
1284 printf_filtered (" and ends at ");
1285 print_address (end_pc, gdb_stdout);
1286 printf_filtered (".\n");
1287 }
1288
1289 /* x/i should display this line's code. */
1290 set_next_address (start_pc);
1291
1292 /* Repeating "info line" should do the following line. */
1293 last_line_listed = sal.line + 1;
1294
1295 /* If this is the only line, show the source code. If it could
1296 not find the file, don't do anything special. */
1297 if (annotation_level && sals.nelts == 1)
1298 identify_source_line (sal.symtab, sal.line, 0, start_pc);
1299 }
1300 else
1301 /* Is there any case in which we get here, and have an address
1302 which the user would want to see? If we have debugging symbols
1303 and no line numbers? */
1304 printf_filtered ("Line number %d is out of range for \"%s\".\n",
1305 sal.line, sal.symtab->filename);
1306 }
1307 xfree (sals.sals);
1308 }
1309 \f
1310 /* Commands to search the source file for a regexp. */
1311
1312 /* ARGSUSED */
1313 static void
1314 forward_search_command (char *regex, int from_tty)
1315 {
1316 register int c;
1317 register int desc;
1318 register FILE *stream;
1319 int line;
1320 char *msg;
1321
1322 line = last_line_listed + 1;
1323
1324 msg = (char *) re_comp (regex);
1325 if (msg)
1326 error (msg);
1327
1328 if (current_source_symtab == 0)
1329 select_source_symtab (0);
1330
1331 desc = open_source_file (current_source_symtab);
1332 if (desc < 0)
1333 perror_with_name (current_source_symtab->filename);
1334
1335 if (current_source_symtab->line_charpos == 0)
1336 find_source_lines (current_source_symtab, desc);
1337
1338 if (line < 1 || line > current_source_symtab->nlines)
1339 {
1340 close (desc);
1341 error ("Expression not found");
1342 }
1343
1344 if (lseek (desc, current_source_symtab->line_charpos[line - 1], 0) < 0)
1345 {
1346 close (desc);
1347 perror_with_name (current_source_symtab->filename);
1348 }
1349
1350 stream = fdopen (desc, FDOPEN_MODE);
1351 clearerr (stream);
1352 while (1)
1353 {
1354 static char *buf = NULL;
1355 register char *p;
1356 int cursize, newsize;
1357
1358 cursize = 256;
1359 buf = xmalloc (cursize);
1360 p = buf;
1361
1362 c = getc (stream);
1363 if (c == EOF)
1364 break;
1365 do
1366 {
1367 *p++ = c;
1368 if (p - buf == cursize)
1369 {
1370 newsize = cursize + cursize / 2;
1371 buf = xrealloc (buf, newsize);
1372 p = buf + cursize;
1373 cursize = newsize;
1374 }
1375 }
1376 while (c != '\n' && (c = getc (stream)) >= 0);
1377
1378 #ifdef CRLF_SOURCE_FILES
1379 /* Remove the \r, if any, at the end of the line, otherwise
1380 regular expressions that end with $ or \n won't work. */
1381 if (p - buf > 1 && p[-2] == '\r')
1382 {
1383 p--;
1384 p[-1] = '\n';
1385 }
1386 #endif
1387
1388 /* we now have a source line in buf, null terminate and match */
1389 *p = 0;
1390 if (re_exec (buf) > 0)
1391 {
1392 /* Match! */
1393 fclose (stream);
1394 print_source_lines (current_source_symtab, line, line + 1, 0);
1395 set_internalvar (lookup_internalvar ("_"),
1396 value_from_longest (builtin_type_int,
1397 (LONGEST) line));
1398 current_source_line = max (line - lines_to_list / 2, 1);
1399 return;
1400 }
1401 line++;
1402 }
1403
1404 printf_filtered ("Expression not found\n");
1405 fclose (stream);
1406 }
1407
1408 /* ARGSUSED */
1409 static void
1410 reverse_search_command (char *regex, int from_tty)
1411 {
1412 register int c;
1413 register int desc;
1414 register FILE *stream;
1415 int line;
1416 char *msg;
1417
1418 line = last_line_listed - 1;
1419
1420 msg = (char *) re_comp (regex);
1421 if (msg)
1422 error (msg);
1423
1424 if (current_source_symtab == 0)
1425 select_source_symtab (0);
1426
1427 desc = open_source_file (current_source_symtab);
1428 if (desc < 0)
1429 perror_with_name (current_source_symtab->filename);
1430
1431 if (current_source_symtab->line_charpos == 0)
1432 find_source_lines (current_source_symtab, desc);
1433
1434 if (line < 1 || line > current_source_symtab->nlines)
1435 {
1436 close (desc);
1437 error ("Expression not found");
1438 }
1439
1440 if (lseek (desc, current_source_symtab->line_charpos[line - 1], 0) < 0)
1441 {
1442 close (desc);
1443 perror_with_name (current_source_symtab->filename);
1444 }
1445
1446 stream = fdopen (desc, FDOPEN_MODE);
1447 clearerr (stream);
1448 while (line > 1)
1449 {
1450 /* FIXME!!! We walk right off the end of buf if we get a long line!!! */
1451 char buf[4096]; /* Should be reasonable??? */
1452 register char *p = buf;
1453
1454 c = getc (stream);
1455 if (c == EOF)
1456 break;
1457 do
1458 {
1459 *p++ = c;
1460 }
1461 while (c != '\n' && (c = getc (stream)) >= 0);
1462
1463 #ifdef CRLF_SOURCE_FILES
1464 /* Remove the \r, if any, at the end of the line, otherwise
1465 regular expressions that end with $ or \n won't work. */
1466 if (p - buf > 1 && p[-2] == '\r')
1467 {
1468 p--;
1469 p[-1] = '\n';
1470 }
1471 #endif
1472
1473 /* We now have a source line in buf; null terminate and match. */
1474 *p = 0;
1475 if (re_exec (buf) > 0)
1476 {
1477 /* Match! */
1478 fclose (stream);
1479 print_source_lines (current_source_symtab, line, line + 1, 0);
1480 set_internalvar (lookup_internalvar ("_"),
1481 value_from_longest (builtin_type_int,
1482 (LONGEST) line));
1483 current_source_line = max (line - lines_to_list / 2, 1);
1484 return;
1485 }
1486 line--;
1487 if (fseek (stream, current_source_symtab->line_charpos[line - 1], 0) < 0)
1488 {
1489 fclose (stream);
1490 perror_with_name (current_source_symtab->filename);
1491 }
1492 }
1493
1494 printf_filtered ("Expression not found\n");
1495 fclose (stream);
1496 return;
1497 }
1498 \f
1499 void
1500 _initialize_source (void)
1501 {
1502 struct cmd_list_element *c;
1503 current_source_symtab = 0;
1504 init_source_path ();
1505
1506 /* The intention is to use POSIX Basic Regular Expressions.
1507 Always use the GNU regex routine for consistency across all hosts.
1508 Our current GNU regex.c does not have all the POSIX features, so this is
1509 just an approximation. */
1510 re_set_syntax (RE_SYNTAX_GREP);
1511
1512 c = add_cmd ("directory", class_files, directory_command,
1513 "Add directory DIR to beginning of search path for source files.\n\
1514 Forget cached info on source file locations and line positions.\n\
1515 DIR can also be $cwd for the current working directory, or $cdir for the\n\
1516 directory in which the source file was compiled into object code.\n\
1517 With no argument, reset the search path to $cdir:$cwd, the default.",
1518 &cmdlist);
1519
1520 if (dbx_commands)
1521 add_com_alias ("use", "directory", class_files, 0);
1522
1523 set_cmd_completer (c, filename_completer);
1524
1525 add_cmd ("directories", no_class, show_directories,
1526 "Current search path for finding source files.\n\
1527 $cwd in the path means the current working directory.\n\
1528 $cdir in the path means the compilation directory of the source file.",
1529 &showlist);
1530
1531 if (xdb_commands)
1532 {
1533 add_com_alias ("D", "directory", class_files, 0);
1534 add_cmd ("ld", no_class, show_directories,
1535 "Current search path for finding source files.\n\
1536 $cwd in the path means the current working directory.\n\
1537 $cdir in the path means the compilation directory of the source file.",
1538 &cmdlist);
1539 }
1540
1541 add_info ("source", source_info,
1542 "Information about the current source file.");
1543
1544 add_info ("line", line_info,
1545 concat ("Core addresses of the code for a source line.\n\
1546 Line can be specified as\n\
1547 LINENUM, to list around that line in current file,\n\
1548 FILE:LINENUM, to list around that line in that file,\n\
1549 FUNCTION, to list around beginning of that function,\n\
1550 FILE:FUNCTION, to distinguish among like-named static functions.\n\
1551 ", "\
1552 Default is to describe the last source line that was listed.\n\n\
1553 This sets the default address for \"x\" to the line's first instruction\n\
1554 so that \"x/i\" suffices to start examining the machine code.\n\
1555 The address is also stored as the value of \"$_\".", NULL));
1556
1557 add_com ("forward-search", class_files, forward_search_command,
1558 "Search for regular expression (see regex(3)) from last line listed.\n\
1559 The matching line number is also stored as the value of \"$_\".");
1560 add_com_alias ("search", "forward-search", class_files, 0);
1561
1562 add_com ("reverse-search", class_files, reverse_search_command,
1563 "Search backward for regular expression (see regex(3)) from last line listed.\n\
1564 The matching line number is also stored as the value of \"$_\".");
1565
1566 if (xdb_commands)
1567 {
1568 add_com_alias ("/", "forward-search", class_files, 0);
1569 add_com_alias ("?", "reverse-search", class_files, 0);
1570 }
1571
1572 add_show_from_set
1573 (add_set_cmd ("listsize", class_support, var_uinteger,
1574 (char *) &lines_to_list,
1575 "Set number of source lines gdb will list by default.",
1576 &setlist),
1577 &showlist);
1578 }
This page took 0.061774 seconds and 4 git commands to generate.