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, 2003, 2004
4 Free Software Foundation, Inc.
6 This file is part of GDB.
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.
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.
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. */
25 #include "expression.h"
33 #include <sys/types.h>
34 #include "gdb_string.h"
38 #include "gdb_regex.h"
44 #include "filenames.h" /* for DOSish file names */
45 #include "completer.h"
47 #include "readline/readline.h"
53 #define OPEN_MODE (O_RDONLY | O_BINARY)
54 #define FDOPEN_MODE FOPEN_RB
56 /* Prototypes for exported functions. */
58 void _initialize_source (void);
60 /* Prototypes for local functions. */
62 static int get_filename_and_charpos (struct symtab
*, char **);
64 static void reverse_search_command (char *, int);
66 static void forward_search_command (char *, int);
68 static void line_info (char *, int);
70 static void source_info (char *, int);
72 static void show_directories (char *, int);
74 /* Path of directories to search for source files.
75 Same format as the PATH environment variable's value. */
79 /* Symtab of default file for listing lines of. */
81 static struct symtab
*current_source_symtab
;
83 /* Default next line to list. */
85 static int current_source_line
;
87 /* Default number of lines to print with commands like "list".
88 This is based on guessing how many long (i.e. more than chars_per_line
89 characters) lines there will be. To be completely correct, "list"
90 and friends should be rewritten to count characters and see where
91 things are wrapping, but that would be a fair amount of work. */
93 int lines_to_list
= 10;
95 /* Line number of last line printed. Default for various commands.
96 current_source_line is usually, but not always, the same as this. */
98 static int last_line_listed
;
100 /* First line number listed by last listing command. */
102 static int first_line_listed
;
104 /* Saves the name of the last source file visited and a possible error code.
105 Used to prevent repeating annoying "No such file or directories" msgs */
107 static struct symtab
*last_source_visited
= NULL
;
108 static int last_source_error
= 0;
110 /* Return the first line listed by print_source_lines.
111 Used by command interpreters to request listing from
115 get_first_line_listed (void)
117 return first_line_listed
;
120 /* Return the default number of lines to print with commands like the
121 cli "list". The caller of print_source_lines must use this to
122 calculate the end line and use it in the call to print_source_lines
123 as it does not automatically use this value. */
126 get_lines_to_list (void)
128 return lines_to_list
;
131 /* Return the current source file for listing and next line to list.
132 NOTE: The returned sal pc and end fields are not valid. */
134 struct symtab_and_line
135 get_current_source_symtab_and_line (void)
137 struct symtab_and_line cursal
;
139 cursal
.symtab
= current_source_symtab
;
140 cursal
.line
= current_source_line
;
147 /* If the current source file for listing is not set, try and get a default.
148 Usually called before get_current_source_symtab_and_line() is called.
149 It may err out if a default cannot be determined.
150 We must be cautious about where it is called, as it can recurse as the
151 process of determining a new default may call the caller!
152 Use get_current_source_symtab_and_line only to get whatever
153 we have without erroring out or trying to get a default. */
156 set_default_source_symtab_and_line (void)
158 struct symtab_and_line cursal
;
160 if (!have_full_symbols () && !have_partial_symbols ())
161 error ("No symbol table is loaded. Use the \"file\" command.");
163 /* Pull in a current source symtab if necessary */
164 if (current_source_symtab
== 0)
165 select_source_symtab (0);
168 /* Return the current default file for listing and next line to list
169 (the returned sal pc and end fields are not valid.)
170 and set the current default to whatever is in SAL.
171 NOTE: The returned sal pc and end fields are not valid. */
173 struct symtab_and_line
174 set_current_source_symtab_and_line (const struct symtab_and_line
*sal
)
176 struct symtab_and_line cursal
;
178 cursal
.symtab
= current_source_symtab
;
179 cursal
.line
= current_source_line
;
181 current_source_symtab
= sal
->symtab
;
182 current_source_line
= sal
->line
;
189 /* Reset any information stored about a default file and line to print. */
192 clear_current_source_symtab_and_line (void)
194 current_source_symtab
= 0;
195 current_source_line
= 0;
198 /* Set the source file default for the "list" command to be S.
200 If S is NULL, and we don't have a default, find one. This
201 should only be called when the user actually tries to use the
202 default, since we produce an error if we can't find a reasonable
203 default. Also, since this can cause symbols to be read, doing it
204 before we need to would make things slower than necessary. */
207 select_source_symtab (struct symtab
*s
)
209 struct symtabs_and_lines sals
;
210 struct symtab_and_line sal
;
211 struct partial_symtab
*ps
;
212 struct partial_symtab
*cs_pst
= 0;
217 current_source_symtab
= s
;
218 current_source_line
= 1;
222 if (current_source_symtab
)
225 /* Make the default place to list be the function `main'
227 if (lookup_symbol (main_name (), 0, VAR_DOMAIN
, 0, NULL
))
229 sals
= decode_line_spec (main_name (), 1);
232 current_source_symtab
= sal
.symtab
;
233 current_source_line
= max (sal
.line
- (lines_to_list
- 1), 1);
234 if (current_source_symtab
)
238 /* All right; find the last file in the symtab list (ignoring .h's). */
240 current_source_line
= 1;
242 for (ofp
= object_files
; ofp
!= NULL
; ofp
= ofp
->next
)
244 for (s
= ofp
->symtabs
; s
; s
= s
->next
)
246 char *name
= s
->filename
;
247 int len
= strlen (name
);
248 if (!(len
> 2 && (DEPRECATED_STREQ (&name
[len
- 2], ".h"))))
250 current_source_symtab
= s
;
254 if (current_source_symtab
)
257 /* Howabout the partial symbol tables? */
259 for (ofp
= object_files
; ofp
!= NULL
; ofp
= ofp
->next
)
261 for (ps
= ofp
->psymtabs
; ps
!= NULL
; ps
= ps
->next
)
263 char *name
= ps
->filename
;
264 int len
= strlen (name
);
265 if (!(len
> 2 && (DEPRECATED_STREQ (&name
[len
- 2], ".h"))))
275 internal_error (__FILE__
, __LINE__
,
276 "select_source_symtab: "
277 "readin pst found and no symtabs.");
281 current_source_symtab
= PSYMTAB_TO_SYMTAB (cs_pst
);
284 if (current_source_symtab
)
287 error ("Can't find a default source file");
291 show_directories (char *ignore
, int from_tty
)
293 puts_filtered ("Source directories searched: ");
294 puts_filtered (source_path
);
295 puts_filtered ("\n");
298 /* Forget what we learned about line positions in source files, and
299 which directories contain them; must check again now since files
300 may be found in a different directory now. */
303 forget_cached_source_info (void)
306 struct objfile
*objfile
;
307 struct partial_symtab
*pst
;
309 for (objfile
= object_files
; objfile
!= NULL
; objfile
= objfile
->next
)
311 for (s
= objfile
->symtabs
; s
!= NULL
; s
= s
->next
)
313 if (s
->line_charpos
!= NULL
)
315 xfree (s
->line_charpos
);
316 s
->line_charpos
= NULL
;
318 if (s
->fullname
!= NULL
)
325 ALL_OBJFILE_PSYMTABS (objfile
, pst
)
327 if (pst
->fullname
!= NULL
)
329 xfree (pst
->fullname
);
330 pst
->fullname
= NULL
;
337 init_source_path (void)
341 sprintf (buf
, "$cdir%c$cwd", DIRNAME_SEPARATOR
);
342 source_path
= xstrdup (buf
);
343 forget_cached_source_info ();
347 init_last_source_visited (void)
349 last_source_visited
= NULL
;
352 /* Add zero or more directories to the front of the source path. */
355 directory_command (char *dirname
, int from_tty
)
358 /* FIXME, this goes to "delete dir"... */
361 if (from_tty
&& query ("Reinitialize source path to empty? "))
369 mod_path (dirname
, &source_path
);
370 last_source_visited
= NULL
;
373 show_directories ((char *) 0, from_tty
);
374 forget_cached_source_info ();
377 /* Add zero or more directories to the front of an arbitrary path. */
380 mod_path (char *dirname
, char **which_path
)
382 add_path (dirname
, which_path
, 1);
385 /* Workhorse of mod_path. Takes an extra argument to determine
386 if dirname should be parsed for separators that indicate multiple
387 directories. This allows for interfaces that pre-parse the dirname
388 and allow specification of traditional separator characters such
392 add_path (char *dirname
, char **which_path
, int parse_separators
)
394 char *old
= *which_path
;
400 dirname
= xstrdup (dirname
);
401 make_cleanup (xfree
, dirname
);
405 char *name
= dirname
;
410 char *separator
= NULL
;
414 if (parse_separators
)
416 separator
= strchr (name
, DIRNAME_SEPARATOR
);
417 space
= strchr (name
, ' ');
418 tab
= strchr (name
, '\t');
421 if (separator
== 0 && space
== 0 && tab
== 0)
422 p
= dirname
= name
+ strlen (name
);
426 if (separator
!= 0 && (p
== 0 || separator
< p
))
428 if (space
!= 0 && (p
== 0 || space
< p
))
430 if (tab
!= 0 && (p
== 0 || tab
< p
))
433 while (*dirname
== DIRNAME_SEPARATOR
440 if (!(IS_DIR_SEPARATOR (*name
) && p
<= name
+ 1) /* "/" */
441 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
442 /* On MS-DOS and MS-Windows, h:\ is different from h: */
443 && !(p
== name
+ 3 && name
[1] == ':') /* "d:/" */
445 && IS_DIR_SEPARATOR (p
[-1]))
446 /* Sigh. "foo/" => "foo" */
450 while (p
> name
&& p
[-1] == '.')
454 /* "." => getwd (). */
455 name
= current_directory
;
458 else if (p
> name
+ 1 && IS_DIR_SEPARATOR (p
[-2]))
468 /* "...foo/." => "...foo". */
479 name
= tilde_expand (name
);
480 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
481 else if (IS_ABSOLUTE_PATH (name
) && p
== name
+ 2) /* "d:" => "d:." */
482 name
= concat (name
, ".", NULL
);
484 else if (!IS_ABSOLUTE_PATH (name
) && name
[0] != '$')
485 name
= concat (current_directory
, SLASH_STRING
, name
, NULL
);
487 name
= savestring (name
, p
- name
);
488 make_cleanup (xfree
, name
);
490 /* Unless it's a variable, check existence. */
493 /* These are warnings, not errors, since we don't want a
494 non-existent directory in a .gdbinit file to stop processing
495 of the .gdbinit file.
497 Whether they get added to the path is more debatable. Current
498 answer is yes, in case the user wants to go make the directory
499 or whatever. If the directory continues to not exist/not be
500 a directory/etc, then having them in the path should be
502 if (stat (name
, &st
) < 0)
504 int save_errno
= errno
;
505 fprintf_unfiltered (gdb_stderr
, "Warning: ");
506 print_sys_errmsg (name
, save_errno
);
508 else if ((st
.st_mode
& S_IFMT
) != S_IFDIR
)
509 warning ("%s is not a directory.", name
);
514 unsigned int len
= strlen (name
);
519 /* FIXME: strncmp loses in interesting ways on MS-DOS and
520 MS-Windows because of case-insensitivity and two different
521 but functionally identical slash characters. We need a
522 special filesystem-dependent file-name comparison function.
524 Actually, even on Unix I would use realpath() or its work-
525 alike before comparing. Then all the code above which
526 removes excess slashes and dots could simply go away. */
527 if (!strncmp (p
, name
, len
)
528 && (p
[len
] == '\0' || p
[len
] == DIRNAME_SEPARATOR
))
530 /* Found it in the search path, remove old copy */
532 p
--; /* Back over leading separator */
533 if (prefix
> p
- *which_path
)
534 goto skip_dup
; /* Same dir twice in one cmd */
535 strcpy (p
, &p
[len
+ 1]); /* Copy from next \0 or : */
537 p
= strchr (p
, DIRNAME_SEPARATOR
);
547 tinybuf
[0] = DIRNAME_SEPARATOR
;
550 /* If we have already tacked on a name(s) in this command, be sure they stay
551 on the front as we tack on some more. */
558 temp
= concat (old
, tinybuf
, name
, NULL
);
560 *which_path
= concat (temp
, "", &old
[prefix
], NULL
);
561 prefix
= strlen (temp
);
566 *which_path
= concat (name
, (old
[0] ? tinybuf
: old
), old
, NULL
);
567 prefix
= strlen (name
);
575 while (*dirname
!= '\0');
580 source_info (char *ignore
, int from_tty
)
582 struct symtab
*s
= current_source_symtab
;
586 printf_filtered ("No current source file.\n");
589 printf_filtered ("Current source file is %s\n", s
->filename
);
591 printf_filtered ("Compilation directory is %s\n", s
->dirname
);
593 printf_filtered ("Located in %s\n", s
->fullname
);
595 printf_filtered ("Contains %d line%s.\n", s
->nlines
,
596 s
->nlines
== 1 ? "" : "s");
598 printf_filtered ("Source language is %s.\n", language_str (s
->language
));
599 printf_filtered ("Compiled with %s debugging format.\n", s
->debugformat
);
600 printf_filtered ("%s preprocessor macro info.\n",
601 s
->macro_table
? "Includes" : "Does not include");
605 /* Return True if the file NAME exists and is a regular file */
607 is_regular_file (const char *name
)
610 const int status
= stat (name
, &st
);
612 /* Stat should never fail except when the file does not exist.
613 If stat fails, analyze the source of error and return True
614 unless the file does not exist, to avoid returning false results
615 on obscure systems where stat does not work as expected.
618 return (errno
!= ENOENT
);
620 return S_ISREG (st
.st_mode
);
623 /* Open a file named STRING, searching path PATH (dir names sep by some char)
624 using mode MODE and protection bits PROT in the calls to open.
626 OPTS specifies the function behaviour in specific cases.
628 If OPF_TRY_CWD_FIRST, try to open ./STRING before searching PATH.
629 (ie pretend the first element of PATH is "."). This also indicates
630 that a slash in STRING disables searching of the path (this is
631 so that "exec-file ./foo" or "symbol-file ./foo" insures that you
632 get that particular version of foo or an error message).
634 If OPTS has OPF_SEARCH_IN_PATH set, absolute names will also be
635 searched in path (we usually want this for source files but not for
638 If FILENAME_OPENED is non-null, set it to a newly allocated string naming
639 the actual file opened (this string will always start with a "/"). We
640 have to take special pains to avoid doubling the "/" between the directory
641 and the file, sigh! Emacs gets confuzzed by this when we print the
644 If a file is found, return the descriptor.
645 Otherwise, return -1, with errno set for the last name we tried to open. */
647 /* >>>> This should only allow files of certain types,
648 >>>> eg executable, non-directory */
650 openp (const char *path
, int opts
, const char *string
,
652 char **filename_opened
)
666 if ((opts
& OPF_TRY_CWD_FIRST
) || IS_ABSOLUTE_PATH (string
))
670 if (is_regular_file (string
))
672 filename
= alloca (strlen (string
) + 1);
673 strcpy (filename
, string
);
674 fd
= open (filename
, mode
, prot
);
684 if (!(opts
& OPF_SEARCH_IN_PATH
))
685 for (i
= 0; string
[i
]; i
++)
686 if (IS_DIR_SEPARATOR (string
[i
]))
690 /* /foo => foo, to avoid multiple slashes that Emacs doesn't like. */
691 while (IS_DIR_SEPARATOR(string
[0]))
695 while (string
[0] == '.' && IS_DIR_SEPARATOR (string
[1]))
698 alloclen
= strlen (path
) + strlen (string
) + 2;
699 filename
= alloca (alloclen
);
701 for (p
= path
; p
; p
= p1
? p1
+ 1 : 0)
703 p1
= strchr (p
, DIRNAME_SEPARATOR
);
709 if (len
== 4 && p
[0] == '$' && p
[1] == 'c'
710 && p
[2] == 'w' && p
[3] == 'd')
712 /* Name is $cwd -- insert current directory name instead. */
715 /* First, realloc the filename buffer if too short. */
716 len
= strlen (current_directory
);
717 newlen
= len
+ strlen (string
) + 2;
718 if (newlen
> alloclen
)
721 filename
= alloca (alloclen
);
723 strcpy (filename
, current_directory
);
727 /* Normal file name in path -- just use it. */
728 strncpy (filename
, p
, len
);
732 /* Remove trailing slashes */
733 while (len
> 0 && IS_DIR_SEPARATOR (filename
[len
- 1]))
736 strcat (filename
+ len
, SLASH_STRING
);
737 strcat (filename
, string
);
739 if (is_regular_file (filename
))
741 fd
= open (filename
, mode
);
750 /* If a file was opened, canonicalize its filename. Use xfullpath
751 rather than gdb_realpath to avoid resolving the basename part
752 of filenames when the associated file is a symbolic link. This
753 fixes a potential inconsistency between the filenames known to
754 GDB and the filenames it prints in the annotations. */
756 *filename_opened
= NULL
;
757 else if (IS_ABSOLUTE_PATH (filename
))
758 *filename_opened
= xfullpath (filename
);
761 /* Beware the // my son, the Emacs barfs, the botch that catch... */
763 char *f
= concat (current_directory
,
764 IS_DIR_SEPARATOR (current_directory
[strlen (current_directory
) - 1])
767 *filename_opened
= xfullpath (f
);
776 /* This is essentially a convenience, for clients that want the behaviour
777 of openp, using source_path, but that really don't want the file to be
778 opened but want instead just to know what the full pathname is (as
779 qualified against source_path).
781 The current working directory is searched first.
783 If the file was found, this function returns 1, and FULL_PATHNAME is
784 set to the fully-qualified pathname.
786 Else, this functions returns 0, and FULL_PATHNAME is set to NULL. */
788 source_full_path_of (char *filename
, char **full_pathname
)
792 fd
= openp (source_path
, OPF_TRY_CWD_FIRST
| OPF_SEARCH_IN_PATH
, filename
,
793 O_RDONLY
, 0, full_pathname
);
796 *full_pathname
= NULL
;
804 /* This function is capable of finding the absolute path to a
805 source file, and opening it, provided you give it an
806 OBJFILE and FILENAME. Both the DIRNAME and FULLNAME are only
807 added suggestions on where to find the file.
809 OBJFILE should be the objfile associated with a psymtab or symtab.
810 FILENAME should be the filename to open.
811 DIRNAME is the compilation directory of a particular source file.
812 Only some debug formats provide this info.
813 FULLNAME can be the last known absolute path to the file in question.
816 A valid file descriptor is returned. ( the return value is positive )
817 FULLNAME is set to the absolute path to the file just opened.
820 A non valid file descriptor is returned. ( the return value is negitive )
821 FULLNAME is set to NULL. */
823 find_and_open_source (struct objfile
*objfile
,
824 const char *filename
,
828 char *path
= source_path
;
832 /* Quick way out if we already know its full name */
835 result
= open (*fullname
, OPEN_MODE
);
838 /* Didn't work -- free old one, try again. */
845 /* Replace a path entry of $cdir with the compilation directory name */
847 /* We cast strstr's result in case an ANSIhole has made it const,
848 which produces a "required warning" when assigned to a nonconst. */
849 p
= (char *) strstr (source_path
, "$cdir");
850 if (p
&& (p
== path
|| p
[-1] == DIRNAME_SEPARATOR
)
851 && (p
[cdir_len
] == DIRNAME_SEPARATOR
|| p
[cdir_len
] == '\0'))
856 alloca (strlen (source_path
) + 1 + strlen (dirname
) + 1);
857 len
= p
- source_path
;
858 strncpy (path
, source_path
, len
); /* Before $cdir */
859 strcpy (path
+ len
, dirname
); /* new stuff */
860 strcat (path
+ len
, source_path
+ len
+ cdir_len
); /* After $cdir */
864 result
= openp (path
, OPF_SEARCH_IN_PATH
, filename
, OPEN_MODE
, 0, fullname
);
867 /* Didn't work. Try using just the basename. */
868 p
= lbasename (filename
);
870 result
= openp (path
, OPF_SEARCH_IN_PATH
, p
, OPEN_MODE
, 0, fullname
);
876 tmp_fullname
= *fullname
;
877 *fullname
= xstrdup (tmp_fullname
);
878 xfree (tmp_fullname
);
883 /* Open a source file given a symtab S. Returns a file descriptor or
884 negative number for error.
886 This function is a convience function to find_and_open_source. */
889 open_source_file (struct symtab
*s
)
894 return find_and_open_source (s
->objfile
, s
->filename
, s
->dirname
,
898 /* Finds the fullname that a symtab represents.
900 If this functions finds the fullname, it will save it in ps->fullname
901 and it will also return the value.
903 If this function fails to find the file that this symtab represents,
904 NULL will be returned and ps->fullname will be set to NULL. */
906 symtab_to_fullname (struct symtab
*s
)
913 /* Don't check s->fullname here, the file could have been
914 deleted/moved/..., look for it again */
915 r
= find_and_open_source (s
->objfile
, s
->filename
, s
->dirname
,
927 /* Finds the fullname that a partial_symtab represents.
929 If this functions finds the fullname, it will save it in ps->fullname
930 and it will also return the value.
932 If this function fails to find the file that this partial_symtab represents,
933 NULL will be returned and ps->fullname will be set to NULL. */
935 psymtab_to_fullname (struct partial_symtab
*ps
)
942 /* Don't check ps->fullname here, the file could have been
943 deleted/moved/..., look for it again */
944 r
= find_and_open_source (ps
->objfile
, ps
->filename
, ps
->dirname
,
956 /* Create and initialize the table S->line_charpos that records
957 the positions of the lines in the source file, which is assumed
958 to be open on descriptor DESC.
959 All set S->nlines to the number of such lines. */
962 find_source_lines (struct symtab
*s
, int desc
)
965 char *data
, *p
, *end
;
967 int lines_allocated
= 1000;
972 line_charpos
= (int *) xmalloc (lines_allocated
* sizeof (int));
973 if (fstat (desc
, &st
) < 0)
974 perror_with_name (s
->filename
);
976 if (s
&& s
->objfile
&& s
->objfile
->obfd
)
977 mtime
= bfd_get_mtime (s
->objfile
->obfd
);
979 mtime
= bfd_get_mtime (exec_bfd
);
981 if (mtime
&& mtime
< st
.st_mtime
)
983 warning ("Source file is more recent than executable.\n");
986 #ifdef LSEEK_NOT_LINEAR
990 /* Have to read it byte by byte to find out where the chars live */
992 line_charpos
[0] = lseek (desc
, 0, SEEK_CUR
);
994 while (myread (desc
, &c
, 1) > 0)
998 if (nlines
== lines_allocated
)
1000 lines_allocated
*= 2;
1002 (int *) xrealloc ((char *) line_charpos
,
1003 sizeof (int) * lines_allocated
);
1005 line_charpos
[nlines
++] = lseek (desc
, 0, SEEK_CUR
);
1009 #else /* lseek linear. */
1011 struct cleanup
*old_cleanups
;
1013 /* st_size might be a large type, but we only support source files whose
1014 size fits in an int. */
1015 size
= (int) st
.st_size
;
1017 /* Use malloc, not alloca, because this may be pretty large, and we may
1018 run into various kinds of limits on stack size. */
1019 data
= (char *) xmalloc (size
);
1020 old_cleanups
= make_cleanup (xfree
, data
);
1022 /* Reassign `size' to result of read for systems where \r\n -> \n. */
1023 size
= myread (desc
, data
, size
);
1025 perror_with_name (s
->filename
);
1028 line_charpos
[0] = 0;
1033 /* A newline at the end does not start a new line. */
1036 if (nlines
== lines_allocated
)
1038 lines_allocated
*= 2;
1040 (int *) xrealloc ((char *) line_charpos
,
1041 sizeof (int) * lines_allocated
);
1043 line_charpos
[nlines
++] = p
- data
;
1046 do_cleanups (old_cleanups
);
1048 #endif /* lseek linear. */
1051 (int *) xrealloc ((char *) line_charpos
, nlines
* sizeof (int));
1055 /* Return the character position of a line LINE in symtab S.
1056 Return 0 if anything is invalid. */
1058 #if 0 /* Currently unused */
1061 source_line_charpos (struct symtab
*s
, int line
)
1065 if (!s
->line_charpos
|| line
<= 0)
1067 if (line
> s
->nlines
)
1069 return s
->line_charpos
[line
- 1];
1072 /* Return the line number of character position POS in symtab S. */
1075 source_charpos_line (struct symtab
*s
, int chr
)
1080 if (s
== 0 || s
->line_charpos
== 0)
1082 lnp
= s
->line_charpos
;
1083 /* Files are usually short, so sequential search is Ok */
1084 while (line
< s
->nlines
&& *lnp
<= chr
)
1089 if (line
>= s
->nlines
)
1097 /* Get full pathname and line number positions for a symtab.
1098 Return nonzero if line numbers may have changed.
1099 Set *FULLNAME to actual name of the file as found by `openp',
1100 or to 0 if the file is not found. */
1103 get_filename_and_charpos (struct symtab
*s
, char **fullname
)
1105 int desc
, linenums_changed
= 0;
1107 desc
= open_source_file (s
);
1115 *fullname
= s
->fullname
;
1116 if (s
->line_charpos
== 0)
1117 linenums_changed
= 1;
1118 if (linenums_changed
)
1119 find_source_lines (s
, desc
);
1121 return linenums_changed
;
1124 /* Print text describing the full name of the source file S
1125 and the line number LINE and its corresponding character position.
1126 The text starts with two Ctrl-z so that the Emacs-GDB interface
1129 MID_STATEMENT is nonzero if the PC is not at the beginning of that line.
1131 Return 1 if successful, 0 if could not find the file. */
1134 identify_source_line (struct symtab
*s
, int line
, int mid_statement
,
1137 if (s
->line_charpos
== 0)
1138 get_filename_and_charpos (s
, (char **) NULL
);
1139 if (s
->fullname
== 0)
1141 if (line
> s
->nlines
)
1142 /* Don't index off the end of the line_charpos array. */
1144 annotate_source (s
->fullname
, line
, s
->line_charpos
[line
- 1],
1147 current_source_line
= line
;
1148 first_line_listed
= line
;
1149 last_line_listed
= line
;
1150 current_source_symtab
= s
;
1155 /* Print source lines from the file of symtab S,
1156 starting with line number LINE and stopping before line number STOPLINE. */
1158 static void print_source_lines_base (struct symtab
*s
, int line
, int stopline
,
1161 print_source_lines_base (struct symtab
*s
, int line
, int stopline
, int noerror
)
1166 int nlines
= stopline
- line
;
1168 /* Regardless of whether we can open the file, set current_source_symtab. */
1169 current_source_symtab
= s
;
1170 current_source_line
= line
;
1171 first_line_listed
= line
;
1173 /* If printing of source lines is disabled, just print file and line number */
1174 if (ui_out_test_flags (uiout
, ui_source_list
))
1176 /* Only prints "No such file or directory" once */
1177 if ((s
!= last_source_visited
) || (!last_source_error
))
1179 last_source_visited
= s
;
1180 desc
= open_source_file (s
);
1184 desc
= last_source_error
;
1196 last_source_error
= desc
;
1200 char *name
= alloca (strlen (s
->filename
) + 100);
1201 sprintf (name
, "%d\t%s", line
, s
->filename
);
1202 print_sys_errmsg (name
, errno
);
1205 ui_out_field_int (uiout
, "line", line
);
1206 ui_out_text (uiout
, "\tin ");
1207 ui_out_field_string (uiout
, "file", s
->filename
);
1208 ui_out_text (uiout
, "\n");
1213 last_source_error
= 0;
1215 if (s
->line_charpos
== 0)
1216 find_source_lines (s
, desc
);
1218 if (line
< 1 || line
> s
->nlines
)
1221 error ("Line number %d out of range; %s has %d lines.",
1222 line
, s
->filename
, s
->nlines
);
1225 if (lseek (desc
, s
->line_charpos
[line
- 1], 0) < 0)
1228 perror_with_name (s
->filename
);
1231 stream
= fdopen (desc
, FDOPEN_MODE
);
1234 while (nlines
-- > 0)
1241 last_line_listed
= current_source_line
;
1242 sprintf (buf
, "%d\t", current_source_line
++);
1243 ui_out_text (uiout
, buf
);
1246 if (c
< 040 && c
!= '\t' && c
!= '\n' && c
!= '\r')
1248 sprintf (buf
, "^%c", c
+ 0100);
1249 ui_out_text (uiout
, buf
);
1252 ui_out_text (uiout
, "^?");
1255 /* Skip a \r character, but only before a \n. */
1256 int c1
= fgetc (stream
);
1259 printf_filtered ("^%c", c
+ 0100);
1261 ungetc (c1
, stream
);
1265 sprintf (buf
, "%c", c
);
1266 ui_out_text (uiout
, buf
);
1269 while (c
!= '\n' && (c
= fgetc (stream
)) >= 0);
1275 /* Show source lines from the file of symtab S, starting with line
1276 number LINE and stopping before line number STOPLINE. If this is the
1277 not the command line version, then the source is shown in the source
1278 window otherwise it is simply printed */
1281 print_source_lines (struct symtab
*s
, int line
, int stopline
, int noerror
)
1283 print_source_lines_base (s
, line
, stopline
, noerror
);
1286 /* Print info on range of pc's in a specified line. */
1289 line_info (char *arg
, int from_tty
)
1291 struct symtabs_and_lines sals
;
1292 struct symtab_and_line sal
;
1293 CORE_ADDR start_pc
, end_pc
;
1296 init_sal (&sal
); /* initialize to zeroes */
1300 sal
.symtab
= current_source_symtab
;
1301 sal
.line
= last_line_listed
;
1303 sals
.sals
= (struct symtab_and_line
*)
1304 xmalloc (sizeof (struct symtab_and_line
));
1309 sals
= decode_line_spec_1 (arg
, 0);
1314 /* C++ More than one line may have been specified, as when the user
1315 specifies an overloaded function name. Print info on them all. */
1316 for (i
= 0; i
< sals
.nelts
; i
++)
1320 if (sal
.symtab
== 0)
1322 printf_filtered ("No line number information available");
1325 /* This is useful for "info line *0x7f34". If we can't tell the
1326 user about a source line, at least let them have the symbolic
1328 printf_filtered (" for address ");
1330 print_address (sal
.pc
, gdb_stdout
);
1333 printf_filtered (".");
1334 printf_filtered ("\n");
1336 else if (sal
.line
> 0
1337 && find_line_pc_range (sal
, &start_pc
, &end_pc
))
1339 if (start_pc
== end_pc
)
1341 printf_filtered ("Line %d of \"%s\"",
1342 sal
.line
, sal
.symtab
->filename
);
1344 printf_filtered (" is at address ");
1345 print_address (start_pc
, gdb_stdout
);
1347 printf_filtered (" but contains no code.\n");
1351 printf_filtered ("Line %d of \"%s\"",
1352 sal
.line
, sal
.symtab
->filename
);
1354 printf_filtered (" starts at address ");
1355 print_address (start_pc
, gdb_stdout
);
1357 printf_filtered (" and ends at ");
1358 print_address (end_pc
, gdb_stdout
);
1359 printf_filtered (".\n");
1362 /* x/i should display this line's code. */
1363 set_next_address (start_pc
);
1365 /* Repeating "info line" should do the following line. */
1366 last_line_listed
= sal
.line
+ 1;
1368 /* If this is the only line, show the source code. If it could
1369 not find the file, don't do anything special. */
1370 if (annotation_level
&& sals
.nelts
== 1)
1371 identify_source_line (sal
.symtab
, sal
.line
, 0, start_pc
);
1374 /* Is there any case in which we get here, and have an address
1375 which the user would want to see? If we have debugging symbols
1376 and no line numbers? */
1377 printf_filtered ("Line number %d is out of range for \"%s\".\n",
1378 sal
.line
, sal
.symtab
->filename
);
1383 /* Commands to search the source file for a regexp. */
1386 forward_search_command (char *regex
, int from_tty
)
1394 line
= last_line_listed
+ 1;
1396 msg
= (char *) re_comp (regex
);
1400 if (current_source_symtab
== 0)
1401 select_source_symtab (0);
1403 desc
= open_source_file (current_source_symtab
);
1405 perror_with_name (current_source_symtab
->filename
);
1407 if (current_source_symtab
->line_charpos
== 0)
1408 find_source_lines (current_source_symtab
, desc
);
1410 if (line
< 1 || line
> current_source_symtab
->nlines
)
1413 error ("Expression not found");
1416 if (lseek (desc
, current_source_symtab
->line_charpos
[line
- 1], 0) < 0)
1419 perror_with_name (current_source_symtab
->filename
);
1422 stream
= fdopen (desc
, FDOPEN_MODE
);
1426 static char *buf
= NULL
;
1428 int cursize
, newsize
;
1431 buf
= xmalloc (cursize
);
1440 if (p
- buf
== cursize
)
1442 newsize
= cursize
+ cursize
/ 2;
1443 buf
= xrealloc (buf
, newsize
);
1448 while (c
!= '\n' && (c
= getc (stream
)) >= 0);
1450 /* Remove the \r, if any, at the end of the line, otherwise
1451 regular expressions that end with $ or \n won't work. */
1452 if (p
- buf
> 1 && p
[-2] == '\r')
1458 /* we now have a source line in buf, null terminate and match */
1460 if (re_exec (buf
) > 0)
1464 print_source_lines (current_source_symtab
, line
, line
+ 1, 0);
1465 set_internalvar (lookup_internalvar ("_"),
1466 value_from_longest (builtin_type_int
,
1468 current_source_line
= max (line
- lines_to_list
/ 2, 1);
1474 printf_filtered ("Expression not found\n");
1479 reverse_search_command (char *regex
, int from_tty
)
1487 line
= last_line_listed
- 1;
1489 msg
= (char *) re_comp (regex
);
1493 if (current_source_symtab
== 0)
1494 select_source_symtab (0);
1496 desc
= open_source_file (current_source_symtab
);
1498 perror_with_name (current_source_symtab
->filename
);
1500 if (current_source_symtab
->line_charpos
== 0)
1501 find_source_lines (current_source_symtab
, desc
);
1503 if (line
< 1 || line
> current_source_symtab
->nlines
)
1506 error ("Expression not found");
1509 if (lseek (desc
, current_source_symtab
->line_charpos
[line
- 1], 0) < 0)
1512 perror_with_name (current_source_symtab
->filename
);
1515 stream
= fdopen (desc
, FDOPEN_MODE
);
1519 /* FIXME!!! We walk right off the end of buf if we get a long line!!! */
1520 char buf
[4096]; /* Should be reasonable??? */
1530 while (c
!= '\n' && (c
= getc (stream
)) >= 0);
1532 /* Remove the \r, if any, at the end of the line, otherwise
1533 regular expressions that end with $ or \n won't work. */
1534 if (p
- buf
> 1 && p
[-2] == '\r')
1540 /* We now have a source line in buf; null terminate and match. */
1542 if (re_exec (buf
) > 0)
1546 print_source_lines (current_source_symtab
, line
, line
+ 1, 0);
1547 set_internalvar (lookup_internalvar ("_"),
1548 value_from_longest (builtin_type_int
,
1550 current_source_line
= max (line
- lines_to_list
/ 2, 1);
1554 if (fseek (stream
, current_source_symtab
->line_charpos
[line
- 1], 0) < 0)
1557 perror_with_name (current_source_symtab
->filename
);
1561 printf_filtered ("Expression not found\n");
1567 _initialize_source (void)
1569 struct cmd_list_element
*c
;
1570 current_source_symtab
= 0;
1571 init_source_path ();
1573 /* The intention is to use POSIX Basic Regular Expressions.
1574 Always use the GNU regex routine for consistency across all hosts.
1575 Our current GNU regex.c does not have all the POSIX features, so this is
1576 just an approximation. */
1577 re_set_syntax (RE_SYNTAX_GREP
);
1579 c
= add_cmd ("directory", class_files
, directory_command
,
1580 "Add directory DIR to beginning of search path for source files.\n\
1581 Forget cached info on source file locations and line positions.\n\
1582 DIR can also be $cwd for the current working directory, or $cdir for the\n\
1583 directory in which the source file was compiled into object code.\n\
1584 With no argument, reset the search path to $cdir:$cwd, the default.",
1588 add_com_alias ("use", "directory", class_files
, 0);
1590 set_cmd_completer (c
, filename_completer
);
1592 add_cmd ("directories", no_class
, show_directories
,
1593 "Current search path for finding source files.\n\
1594 $cwd in the path means the current working directory.\n\
1595 $cdir in the path means the compilation directory of the source file.",
1600 add_com_alias ("D", "directory", class_files
, 0);
1601 add_cmd ("ld", no_class
, show_directories
,
1602 "Current search path for finding source files.\n\
1603 $cwd in the path means the current working directory.\n\
1604 $cdir in the path means the compilation directory of the source file.",
1608 add_info ("source", source_info
,
1609 "Information about the current source file.");
1611 add_info ("line", line_info
,
1612 concat ("Core addresses of the code for a source line.\n\
1613 Line can be specified as\n\
1614 LINENUM, to list around that line in current file,\n\
1615 FILE:LINENUM, to list around that line in that file,\n\
1616 FUNCTION, to list around beginning of that function,\n\
1617 FILE:FUNCTION, to distinguish among like-named static functions.\n\
1619 Default is to describe the last source line that was listed.\n\n\
1620 This sets the default address for \"x\" to the line's first instruction\n\
1621 so that \"x/i\" suffices to start examining the machine code.\n\
1622 The address is also stored as the value of \"$_\".", NULL
));
1624 add_com ("forward-search", class_files
, forward_search_command
,
1625 "Search for regular expression (see regex(3)) from last line listed.\n\
1626 The matching line number is also stored as the value of \"$_\".");
1627 add_com_alias ("search", "forward-search", class_files
, 0);
1629 add_com ("reverse-search", class_files
, reverse_search_command
,
1630 "Search backward for regular expression (see regex(3)) from last line listed.\n\
1631 The matching line number is also stored as the value of \"$_\".");
1635 add_com_alias ("/", "forward-search", class_files
, 0);
1636 add_com_alias ("?", "reverse-search", class_files
, 0);
1639 deprecated_add_show_from_set
1640 (add_set_cmd ("listsize", class_support
, var_uinteger
,
1641 (char *) &lines_to_list
,
1642 "Set number of source lines gdb will list by default.",