1 /* List lines of source files for GDB, the GNU debugger.
2 Copyright (C) 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994,
3 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005
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., 51 Franklin Street, Fifth Floor,
21 Boston, MA 02110-1301, 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"
50 #define OPEN_MODE (O_RDONLY | O_BINARY)
51 #define FDOPEN_MODE FOPEN_RB
53 /* Prototypes for exported functions. */
55 void _initialize_source (void);
57 /* Prototypes for local functions. */
59 static int get_filename_and_charpos (struct symtab
*, char **);
61 static void reverse_search_command (char *, int);
63 static void forward_search_command (char *, int);
65 static void line_info (char *, int);
67 static void source_info (char *, int);
69 static void show_directories (char *, int);
71 /* Path of directories to search for source files.
72 Same format as the PATH environment variable's value. */
76 /* Symtab of default file for listing lines of. */
78 static struct symtab
*current_source_symtab
;
80 /* Default next line to list. */
82 static int current_source_line
;
84 /* Default number of lines to print with commands like "list".
85 This is based on guessing how many long (i.e. more than chars_per_line
86 characters) lines there will be. To be completely correct, "list"
87 and friends should be rewritten to count characters and see where
88 things are wrapping, but that would be a fair amount of work. */
90 int lines_to_list
= 10;
92 show_lines_to_list (struct ui_file
*file
, int from_tty
,
93 struct cmd_list_element
*c
, const char *value
)
95 fprintf_filtered (file
, _("\
96 Number of source lines gdb will list by default is %s.\n"),
100 /* Line number of last line printed. Default for various commands.
101 current_source_line is usually, but not always, the same as this. */
103 static int last_line_listed
;
105 /* First line number listed by last listing command. */
107 static int first_line_listed
;
109 /* Saves the name of the last source file visited and a possible error code.
110 Used to prevent repeating annoying "No such file or directories" msgs */
112 static struct symtab
*last_source_visited
= NULL
;
113 static int last_source_error
= 0;
115 /* Return the first line listed by print_source_lines.
116 Used by command interpreters to request listing from
120 get_first_line_listed (void)
122 return first_line_listed
;
125 /* Return the default number of lines to print with commands like the
126 cli "list". The caller of print_source_lines must use this to
127 calculate the end line and use it in the call to print_source_lines
128 as it does not automatically use this value. */
131 get_lines_to_list (void)
133 return lines_to_list
;
136 /* Return the current source file for listing and next line to list.
137 NOTE: The returned sal pc and end fields are not valid. */
139 struct symtab_and_line
140 get_current_source_symtab_and_line (void)
142 struct symtab_and_line cursal
= { };
144 cursal
.symtab
= current_source_symtab
;
145 cursal
.line
= current_source_line
;
152 /* If the current source file for listing is not set, try and get a default.
153 Usually called before get_current_source_symtab_and_line() is called.
154 It may err out if a default cannot be determined.
155 We must be cautious about where it is called, as it can recurse as the
156 process of determining a new default may call the caller!
157 Use get_current_source_symtab_and_line only to get whatever
158 we have without erroring out or trying to get a default. */
161 set_default_source_symtab_and_line (void)
163 struct symtab_and_line cursal
;
165 if (!have_full_symbols () && !have_partial_symbols ())
166 error (_("No symbol table is loaded. Use the \"file\" command."));
168 /* Pull in a current source symtab if necessary */
169 if (current_source_symtab
== 0)
170 select_source_symtab (0);
173 /* Return the current default file for listing and next line to list
174 (the returned sal pc and end fields are not valid.)
175 and set the current default to whatever is in SAL.
176 NOTE: The returned sal pc and end fields are not valid. */
178 struct symtab_and_line
179 set_current_source_symtab_and_line (const struct symtab_and_line
*sal
)
181 struct symtab_and_line cursal
= { };
183 cursal
.symtab
= current_source_symtab
;
184 cursal
.line
= current_source_line
;
186 current_source_symtab
= sal
->symtab
;
187 current_source_line
= sal
->line
;
194 /* Reset any information stored about a default file and line to print. */
197 clear_current_source_symtab_and_line (void)
199 current_source_symtab
= 0;
200 current_source_line
= 0;
203 /* Set the source file default for the "list" command to be S.
205 If S is NULL, and we don't have a default, find one. This
206 should only be called when the user actually tries to use the
207 default, since we produce an error if we can't find a reasonable
208 default. Also, since this can cause symbols to be read, doing it
209 before we need to would make things slower than necessary. */
212 select_source_symtab (struct symtab
*s
)
214 struct symtabs_and_lines sals
;
215 struct symtab_and_line sal
;
216 struct partial_symtab
*ps
;
217 struct partial_symtab
*cs_pst
= 0;
222 current_source_symtab
= s
;
223 current_source_line
= 1;
227 if (current_source_symtab
)
230 /* Make the default place to list be the function `main'
232 if (lookup_symbol (main_name (), 0, VAR_DOMAIN
, 0, NULL
))
234 sals
= decode_line_spec (main_name (), 1);
237 current_source_symtab
= sal
.symtab
;
238 current_source_line
= max (sal
.line
- (lines_to_list
- 1), 1);
239 if (current_source_symtab
)
243 /* All right; find the last file in the symtab list (ignoring .h's). */
245 current_source_line
= 1;
247 for (ofp
= object_files
; ofp
!= NULL
; ofp
= ofp
->next
)
249 for (s
= ofp
->symtabs
; s
; s
= s
->next
)
251 const char *name
= s
->filename
;
252 int len
= strlen (name
);
253 if (!(len
> 2 && strcmp(&name
[len
- 2], ".h") == 0))
254 current_source_symtab
= s
;
257 if (current_source_symtab
)
260 /* Howabout the partial symbol tables? */
262 for (ofp
= object_files
; ofp
!= NULL
; ofp
= ofp
->next
)
264 for (ps
= ofp
->psymtabs
; ps
!= NULL
; ps
= ps
->next
)
266 const char *name
= ps
->filename
;
267 int len
= strlen (name
);
268 if (!(len
> 2 && strcmp (&name
[len
- 2], ".h") == 0))
276 internal_error (__FILE__
, __LINE__
,
277 _("select_source_symtab: "
278 "readin pst found and no symtabs."));
282 current_source_symtab
= PSYMTAB_TO_SYMTAB (cs_pst
);
285 if (current_source_symtab
)
288 error (_("Can't find a default source file"));
292 show_directories (char *ignore
, int from_tty
)
294 puts_filtered ("Source directories searched: ");
295 puts_filtered (source_path
);
296 puts_filtered ("\n");
299 /* Forget what we learned about line positions in source files, and
300 which directories contain them; must check again now since files
301 may be found in a different directory now. */
304 forget_cached_source_info (void)
307 struct objfile
*objfile
;
308 struct partial_symtab
*pst
;
310 for (objfile
= object_files
; objfile
!= NULL
; objfile
= objfile
->next
)
312 for (s
= objfile
->symtabs
; s
!= NULL
; s
= s
->next
)
314 if (s
->line_charpos
!= NULL
)
316 xfree (s
->line_charpos
);
317 s
->line_charpos
= NULL
;
319 if (s
->fullname
!= NULL
)
326 ALL_OBJFILE_PSYMTABS (objfile
, pst
)
328 if (pst
->fullname
!= NULL
)
330 xfree (pst
->fullname
);
331 pst
->fullname
= NULL
;
338 init_source_path (void)
342 sprintf (buf
, "$cdir%c$cwd", DIRNAME_SEPARATOR
);
343 source_path
= xstrdup (buf
);
344 forget_cached_source_info ();
348 init_last_source_visited (void)
350 last_source_visited
= NULL
;
353 /* Add zero or more directories to the front of the source path. */
356 directory_command (char *dirname
, int from_tty
)
359 /* FIXME, this goes to "delete dir"... */
362 if (from_tty
&& query (_("Reinitialize source path to empty? ")))
370 mod_path (dirname
, &source_path
);
371 last_source_visited
= NULL
;
374 show_directories ((char *) 0, from_tty
);
375 forget_cached_source_info ();
378 /* Add a path given with the -d command line switch.
379 This will not be quoted so we must not treat spaces as separators. */
382 directory_switch (char *dirname
, int from_tty
)
384 add_path (dirname
, &source_path
, 0);
387 /* Add zero or more directories to the front of an arbitrary path. */
390 mod_path (char *dirname
, char **which_path
)
392 add_path (dirname
, which_path
, 1);
395 /* Workhorse of mod_path. Takes an extra argument to determine
396 if dirname should be parsed for separators that indicate multiple
397 directories. This allows for interfaces that pre-parse the dirname
398 and allow specification of traditional separator characters such
402 add_path (char *dirname
, char **which_path
, int parse_separators
)
404 char *old
= *which_path
;
413 if (parse_separators
)
415 /* This will properly parse the space and tab separators
416 and any quotes that may exist. DIRNAME_SEPARATOR will
417 be dealt with later. */
418 argv
= buildargv (dirname
);
419 make_cleanup_freeargv (argv
);
428 arg
= xstrdup (dirname
);
429 make_cleanup (xfree
, arg
);
439 char *separator
= NULL
;
441 /* Spaces and tabs will have been removed by buildargv().
442 The directories will there be split into a list but
443 each entry may still contain DIRNAME_SEPARATOR. */
444 if (parse_separators
)
445 separator
= strchr (name
, DIRNAME_SEPARATOR
);
448 p
= arg
= name
+ strlen (name
);
453 while (*arg
== DIRNAME_SEPARATOR
)
457 /* If there are no more directories in this argument then start
458 on the next argument next time round the loop (if any). */
460 arg
= parse_separators
? argv
[++argv_index
] : NULL
;
463 /* name is the start of the directory.
464 p is the separator (or null) following the end. */
466 while (!(IS_DIR_SEPARATOR (*name
) && p
<= name
+ 1) /* "/" */
467 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
468 /* On MS-DOS and MS-Windows, h:\ is different from h: */
469 && !(p
== name
+ 3 && name
[1] == ':') /* "d:/" */
471 && IS_DIR_SEPARATOR (p
[-1]))
472 /* Sigh. "foo/" => "foo" */
476 while (p
> name
&& p
[-1] == '.')
480 /* "." => getwd (). */
481 name
= current_directory
;
484 else if (p
> name
+ 1 && IS_DIR_SEPARATOR (p
[-2]))
494 /* "...foo/." => "...foo". */
505 name
= tilde_expand (name
);
506 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
507 else if (IS_ABSOLUTE_PATH (name
) && p
== name
+ 2) /* "d:" => "d:." */
508 name
= concat (name
, ".", (char *)NULL
);
510 else if (!IS_ABSOLUTE_PATH (name
) && name
[0] != '$')
511 name
= concat (current_directory
, SLASH_STRING
, name
, (char *)NULL
);
513 name
= savestring (name
, p
- name
);
514 make_cleanup (xfree
, name
);
516 /* Unless it's a variable, check existence. */
519 /* These are warnings, not errors, since we don't want a
520 non-existent directory in a .gdbinit file to stop processing
521 of the .gdbinit file.
523 Whether they get added to the path is more debatable. Current
524 answer is yes, in case the user wants to go make the directory
525 or whatever. If the directory continues to not exist/not be
526 a directory/etc, then having them in the path should be
528 if (stat (name
, &st
) < 0)
530 int save_errno
= errno
;
531 fprintf_unfiltered (gdb_stderr
, "Warning: ");
532 print_sys_errmsg (name
, save_errno
);
534 else if ((st
.st_mode
& S_IFMT
) != S_IFDIR
)
535 warning (_("%s is not a directory."), name
);
540 unsigned int len
= strlen (name
);
545 /* FIXME: strncmp loses in interesting ways on MS-DOS and
546 MS-Windows because of case-insensitivity and two different
547 but functionally identical slash characters. We need a
548 special filesystem-dependent file-name comparison function.
550 Actually, even on Unix I would use realpath() or its work-
551 alike before comparing. Then all the code above which
552 removes excess slashes and dots could simply go away. */
553 if (!strncmp (p
, name
, len
)
554 && (p
[len
] == '\0' || p
[len
] == DIRNAME_SEPARATOR
))
556 /* Found it in the search path, remove old copy */
558 p
--; /* Back over leading separator */
559 if (prefix
> p
- *which_path
)
560 goto skip_dup
; /* Same dir twice in one cmd */
561 strcpy (p
, &p
[len
+ 1]); /* Copy from next \0 or : */
563 p
= strchr (p
, DIRNAME_SEPARATOR
);
573 tinybuf
[0] = DIRNAME_SEPARATOR
;
576 /* If we have already tacked on a name(s) in this command, be sure they stay
577 on the front as we tack on some more. */
584 temp
= concat (old
, tinybuf
, name
, (char *)NULL
);
586 *which_path
= concat (temp
, "", &old
[prefix
], (char *)NULL
);
587 prefix
= strlen (temp
);
592 *which_path
= concat (name
, (old
[0] ? tinybuf
: old
),
594 prefix
= strlen (name
);
607 source_info (char *ignore
, int from_tty
)
609 struct symtab
*s
= current_source_symtab
;
613 printf_filtered (_("No current source file.\n"));
616 printf_filtered (_("Current source file is %s\n"), s
->filename
);
618 printf_filtered (_("Compilation directory is %s\n"), s
->dirname
);
620 printf_filtered (_("Located in %s\n"), s
->fullname
);
622 printf_filtered (_("Contains %d line%s.\n"), s
->nlines
,
623 s
->nlines
== 1 ? "" : "s");
625 printf_filtered (_("Source language is %s.\n"), language_str (s
->language
));
626 printf_filtered (_("Compiled with %s debugging format.\n"), s
->debugformat
);
627 printf_filtered (_("%s preprocessor macro info.\n"),
628 s
->macro_table
? "Includes" : "Does not include");
632 /* Return True if the file NAME exists and is a regular file */
634 is_regular_file (const char *name
)
637 const int status
= stat (name
, &st
);
639 /* Stat should never fail except when the file does not exist.
640 If stat fails, analyze the source of error and return True
641 unless the file does not exist, to avoid returning false results
642 on obscure systems where stat does not work as expected.
645 return (errno
!= ENOENT
);
647 return S_ISREG (st
.st_mode
);
650 /* Open a file named STRING, searching path PATH (dir names sep by some char)
651 using mode MODE and protection bits PROT in the calls to open.
653 OPTS specifies the function behaviour in specific cases.
655 If OPF_TRY_CWD_FIRST, try to open ./STRING before searching PATH.
656 (ie pretend the first element of PATH is "."). This also indicates
657 that a slash in STRING disables searching of the path (this is
658 so that "exec-file ./foo" or "symbol-file ./foo" insures that you
659 get that particular version of foo or an error message).
661 If OPTS has OPF_SEARCH_IN_PATH set, absolute names will also be
662 searched in path (we usually want this for source files but not for
665 If FILENAME_OPENED is non-null, set it to a newly allocated string naming
666 the actual file opened (this string will always start with a "/"). We
667 have to take special pains to avoid doubling the "/" between the directory
668 and the file, sigh! Emacs gets confuzzed by this when we print the
671 If a file is found, return the descriptor.
672 Otherwise, return -1, with errno set for the last name we tried to open. */
674 /* >>>> This should only allow files of certain types,
675 >>>> eg executable, non-directory */
677 openp (const char *path
, int opts
, const char *string
,
679 char **filename_opened
)
693 if ((opts
& OPF_TRY_CWD_FIRST
) || IS_ABSOLUTE_PATH (string
))
697 if (is_regular_file (string
))
699 filename
= alloca (strlen (string
) + 1);
700 strcpy (filename
, string
);
701 fd
= open (filename
, mode
, prot
);
711 if (!(opts
& OPF_SEARCH_IN_PATH
))
712 for (i
= 0; string
[i
]; i
++)
713 if (IS_DIR_SEPARATOR (string
[i
]))
717 /* /foo => foo, to avoid multiple slashes that Emacs doesn't like. */
718 while (IS_DIR_SEPARATOR(string
[0]))
722 while (string
[0] == '.' && IS_DIR_SEPARATOR (string
[1]))
725 alloclen
= strlen (path
) + strlen (string
) + 2;
726 filename
= alloca (alloclen
);
728 for (p
= path
; p
; p
= p1
? p1
+ 1 : 0)
730 p1
= strchr (p
, DIRNAME_SEPARATOR
);
736 if (len
== 4 && p
[0] == '$' && p
[1] == 'c'
737 && p
[2] == 'w' && p
[3] == 'd')
739 /* Name is $cwd -- insert current directory name instead. */
742 /* First, realloc the filename buffer if too short. */
743 len
= strlen (current_directory
);
744 newlen
= len
+ strlen (string
) + 2;
745 if (newlen
> alloclen
)
748 filename
= alloca (alloclen
);
750 strcpy (filename
, current_directory
);
754 /* Normal file name in path -- just use it. */
755 strncpy (filename
, p
, len
);
759 /* Remove trailing slashes */
760 while (len
> 0 && IS_DIR_SEPARATOR (filename
[len
- 1]))
763 strcat (filename
+ len
, SLASH_STRING
);
764 strcat (filename
, string
);
766 if (is_regular_file (filename
))
768 fd
= open (filename
, mode
);
777 /* If a file was opened, canonicalize its filename. Use xfullpath
778 rather than gdb_realpath to avoid resolving the basename part
779 of filenames when the associated file is a symbolic link. This
780 fixes a potential inconsistency between the filenames known to
781 GDB and the filenames it prints in the annotations. */
783 *filename_opened
= NULL
;
784 else if (IS_ABSOLUTE_PATH (filename
))
785 *filename_opened
= xfullpath (filename
);
788 /* Beware the // my son, the Emacs barfs, the botch that catch... */
790 char *f
= concat (current_directory
,
791 IS_DIR_SEPARATOR (current_directory
[strlen (current_directory
) - 1])
793 filename
, (char *)NULL
);
794 *filename_opened
= xfullpath (f
);
803 /* This is essentially a convenience, for clients that want the behaviour
804 of openp, using source_path, but that really don't want the file to be
805 opened but want instead just to know what the full pathname is (as
806 qualified against source_path).
808 The current working directory is searched first.
810 If the file was found, this function returns 1, and FULL_PATHNAME is
811 set to the fully-qualified pathname.
813 Else, this functions returns 0, and FULL_PATHNAME is set to NULL. */
815 source_full_path_of (char *filename
, char **full_pathname
)
819 fd
= openp (source_path
, OPF_TRY_CWD_FIRST
| OPF_SEARCH_IN_PATH
, filename
,
820 O_RDONLY
, 0, full_pathname
);
823 *full_pathname
= NULL
;
831 /* This function is capable of finding the absolute path to a
832 source file, and opening it, provided you give it an
833 OBJFILE and FILENAME. Both the DIRNAME and FULLNAME are only
834 added suggestions on where to find the file.
836 OBJFILE should be the objfile associated with a psymtab or symtab.
837 FILENAME should be the filename to open.
838 DIRNAME is the compilation directory of a particular source file.
839 Only some debug formats provide this info.
840 FULLNAME can be the last known absolute path to the file in question.
843 A valid file descriptor is returned. ( the return value is positive )
844 FULLNAME is set to the absolute path to the file just opened.
847 A non valid file descriptor is returned. ( the return value is negitive )
848 FULLNAME is set to NULL. */
850 find_and_open_source (struct objfile
*objfile
,
851 const char *filename
,
855 char *path
= source_path
;
859 /* Quick way out if we already know its full name */
862 result
= open (*fullname
, OPEN_MODE
);
865 /* Didn't work -- free old one, try again. */
872 /* Replace a path entry of $cdir with the compilation directory name */
874 /* We cast strstr's result in case an ANSIhole has made it const,
875 which produces a "required warning" when assigned to a nonconst. */
876 p
= (char *) strstr (source_path
, "$cdir");
877 if (p
&& (p
== path
|| p
[-1] == DIRNAME_SEPARATOR
)
878 && (p
[cdir_len
] == DIRNAME_SEPARATOR
|| p
[cdir_len
] == '\0'))
883 alloca (strlen (source_path
) + 1 + strlen (dirname
) + 1);
884 len
= p
- source_path
;
885 strncpy (path
, source_path
, len
); /* Before $cdir */
886 strcpy (path
+ len
, dirname
); /* new stuff */
887 strcat (path
+ len
, source_path
+ len
+ cdir_len
); /* After $cdir */
891 result
= openp (path
, OPF_SEARCH_IN_PATH
, filename
, OPEN_MODE
, 0, fullname
);
894 /* Didn't work. Try using just the basename. */
895 p
= lbasename (filename
);
897 result
= openp (path
, OPF_SEARCH_IN_PATH
, p
, OPEN_MODE
, 0, fullname
);
903 tmp_fullname
= *fullname
;
904 *fullname
= xstrdup (tmp_fullname
);
905 xfree (tmp_fullname
);
910 /* Open a source file given a symtab S. Returns a file descriptor or
911 negative number for error.
913 This function is a convience function to find_and_open_source. */
916 open_source_file (struct symtab
*s
)
921 return find_and_open_source (s
->objfile
, s
->filename
, s
->dirname
,
925 /* Finds the fullname that a symtab represents.
927 If this functions finds the fullname, it will save it in ps->fullname
928 and it will also return the value.
930 If this function fails to find the file that this symtab represents,
931 NULL will be returned and ps->fullname will be set to NULL. */
933 symtab_to_fullname (struct symtab
*s
)
940 /* Don't check s->fullname here, the file could have been
941 deleted/moved/..., look for it again */
942 r
= find_and_open_source (s
->objfile
, s
->filename
, s
->dirname
,
954 /* Finds the fullname that a partial_symtab represents.
956 If this functions finds the fullname, it will save it in ps->fullname
957 and it will also return the value.
959 If this function fails to find the file that this partial_symtab represents,
960 NULL will be returned and ps->fullname will be set to NULL. */
962 psymtab_to_fullname (struct partial_symtab
*ps
)
969 /* Don't check ps->fullname here, the file could have been
970 deleted/moved/..., look for it again */
971 r
= find_and_open_source (ps
->objfile
, ps
->filename
, ps
->dirname
,
983 /* Create and initialize the table S->line_charpos that records
984 the positions of the lines in the source file, which is assumed
985 to be open on descriptor DESC.
986 All set S->nlines to the number of such lines. */
989 find_source_lines (struct symtab
*s
, int desc
)
992 char *data
, *p
, *end
;
994 int lines_allocated
= 1000;
999 line_charpos
= (int *) xmalloc (lines_allocated
* sizeof (int));
1000 if (fstat (desc
, &st
) < 0)
1001 perror_with_name (s
->filename
);
1003 if (s
&& s
->objfile
&& s
->objfile
->obfd
)
1004 mtime
= bfd_get_mtime (s
->objfile
->obfd
);
1006 mtime
= bfd_get_mtime (exec_bfd
);
1008 if (mtime
&& mtime
< st
.st_mtime
)
1009 warning (_("Source file is more recent than executable."));
1011 #ifdef LSEEK_NOT_LINEAR
1015 /* Have to read it byte by byte to find out where the chars live */
1017 line_charpos
[0] = lseek (desc
, 0, SEEK_CUR
);
1019 while (myread (desc
, &c
, 1) > 0)
1023 if (nlines
== lines_allocated
)
1025 lines_allocated
*= 2;
1027 (int *) xrealloc ((char *) line_charpos
,
1028 sizeof (int) * lines_allocated
);
1030 line_charpos
[nlines
++] = lseek (desc
, 0, SEEK_CUR
);
1034 #else /* lseek linear. */
1036 struct cleanup
*old_cleanups
;
1038 /* st_size might be a large type, but we only support source files whose
1039 size fits in an int. */
1040 size
= (int) st
.st_size
;
1042 /* Use malloc, not alloca, because this may be pretty large, and we may
1043 run into various kinds of limits on stack size. */
1044 data
= (char *) xmalloc (size
);
1045 old_cleanups
= make_cleanup (xfree
, data
);
1047 /* Reassign `size' to result of read for systems where \r\n -> \n. */
1048 size
= myread (desc
, data
, size
);
1050 perror_with_name (s
->filename
);
1053 line_charpos
[0] = 0;
1058 /* A newline at the end does not start a new line. */
1061 if (nlines
== lines_allocated
)
1063 lines_allocated
*= 2;
1065 (int *) xrealloc ((char *) line_charpos
,
1066 sizeof (int) * lines_allocated
);
1068 line_charpos
[nlines
++] = p
- data
;
1071 do_cleanups (old_cleanups
);
1073 #endif /* lseek linear. */
1076 (int *) xrealloc ((char *) line_charpos
, nlines
* sizeof (int));
1080 /* Return the character position of a line LINE in symtab S.
1081 Return 0 if anything is invalid. */
1083 #if 0 /* Currently unused */
1086 source_line_charpos (struct symtab
*s
, int line
)
1090 if (!s
->line_charpos
|| line
<= 0)
1092 if (line
> s
->nlines
)
1094 return s
->line_charpos
[line
- 1];
1097 /* Return the line number of character position POS in symtab S. */
1100 source_charpos_line (struct symtab
*s
, int chr
)
1105 if (s
== 0 || s
->line_charpos
== 0)
1107 lnp
= s
->line_charpos
;
1108 /* Files are usually short, so sequential search is Ok */
1109 while (line
< s
->nlines
&& *lnp
<= chr
)
1114 if (line
>= s
->nlines
)
1122 /* Get full pathname and line number positions for a symtab.
1123 Return nonzero if line numbers may have changed.
1124 Set *FULLNAME to actual name of the file as found by `openp',
1125 or to 0 if the file is not found. */
1128 get_filename_and_charpos (struct symtab
*s
, char **fullname
)
1130 int desc
, linenums_changed
= 0;
1132 desc
= open_source_file (s
);
1140 *fullname
= s
->fullname
;
1141 if (s
->line_charpos
== 0)
1142 linenums_changed
= 1;
1143 if (linenums_changed
)
1144 find_source_lines (s
, desc
);
1146 return linenums_changed
;
1149 /* Print text describing the full name of the source file S
1150 and the line number LINE and its corresponding character position.
1151 The text starts with two Ctrl-z so that the Emacs-GDB interface
1154 MID_STATEMENT is nonzero if the PC is not at the beginning of that line.
1156 Return 1 if successful, 0 if could not find the file. */
1159 identify_source_line (struct symtab
*s
, int line
, int mid_statement
,
1162 if (s
->line_charpos
== 0)
1163 get_filename_and_charpos (s
, (char **) NULL
);
1164 if (s
->fullname
== 0)
1166 if (line
> s
->nlines
)
1167 /* Don't index off the end of the line_charpos array. */
1169 annotate_source (s
->fullname
, line
, s
->line_charpos
[line
- 1],
1172 current_source_line
= line
;
1173 first_line_listed
= line
;
1174 last_line_listed
= line
;
1175 current_source_symtab
= s
;
1180 /* Print source lines from the file of symtab S,
1181 starting with line number LINE and stopping before line number STOPLINE. */
1183 static void print_source_lines_base (struct symtab
*s
, int line
, int stopline
,
1186 print_source_lines_base (struct symtab
*s
, int line
, int stopline
, int noerror
)
1191 int nlines
= stopline
- line
;
1193 /* Regardless of whether we can open the file, set current_source_symtab. */
1194 current_source_symtab
= s
;
1195 current_source_line
= line
;
1196 first_line_listed
= line
;
1198 /* If printing of source lines is disabled, just print file and line number */
1199 if (ui_out_test_flags (uiout
, ui_source_list
))
1201 /* Only prints "No such file or directory" once */
1202 if ((s
!= last_source_visited
) || (!last_source_error
))
1204 last_source_visited
= s
;
1205 desc
= open_source_file (s
);
1209 desc
= last_source_error
;
1221 last_source_error
= desc
;
1225 char *name
= alloca (strlen (s
->filename
) + 100);
1226 sprintf (name
, "%d\t%s", line
, s
->filename
);
1227 print_sys_errmsg (name
, errno
);
1230 ui_out_field_int (uiout
, "line", line
);
1231 ui_out_text (uiout
, "\tin ");
1232 ui_out_field_string (uiout
, "file", s
->filename
);
1233 ui_out_text (uiout
, "\n");
1238 last_source_error
= 0;
1240 if (s
->line_charpos
== 0)
1241 find_source_lines (s
, desc
);
1243 if (line
< 1 || line
> s
->nlines
)
1246 error (_("Line number %d out of range; %s has %d lines."),
1247 line
, s
->filename
, s
->nlines
);
1250 if (lseek (desc
, s
->line_charpos
[line
- 1], 0) < 0)
1253 perror_with_name (s
->filename
);
1256 stream
= fdopen (desc
, FDOPEN_MODE
);
1259 while (nlines
-- > 0)
1266 last_line_listed
= current_source_line
;
1267 sprintf (buf
, "%d\t", current_source_line
++);
1268 ui_out_text (uiout
, buf
);
1271 if (c
< 040 && c
!= '\t' && c
!= '\n' && c
!= '\r')
1273 sprintf (buf
, "^%c", c
+ 0100);
1274 ui_out_text (uiout
, buf
);
1277 ui_out_text (uiout
, "^?");
1280 /* Skip a \r character, but only before a \n. */
1281 int c1
= fgetc (stream
);
1284 printf_filtered ("^%c", c
+ 0100);
1286 ungetc (c1
, stream
);
1290 sprintf (buf
, "%c", c
);
1291 ui_out_text (uiout
, buf
);
1294 while (c
!= '\n' && (c
= fgetc (stream
)) >= 0);
1300 /* Show source lines from the file of symtab S, starting with line
1301 number LINE and stopping before line number STOPLINE. If this is the
1302 not the command line version, then the source is shown in the source
1303 window otherwise it is simply printed */
1306 print_source_lines (struct symtab
*s
, int line
, int stopline
, int noerror
)
1308 print_source_lines_base (s
, line
, stopline
, noerror
);
1311 /* Print info on range of pc's in a specified line. */
1314 line_info (char *arg
, int from_tty
)
1316 struct symtabs_and_lines sals
;
1317 struct symtab_and_line sal
;
1318 CORE_ADDR start_pc
, end_pc
;
1321 init_sal (&sal
); /* initialize to zeroes */
1325 sal
.symtab
= current_source_symtab
;
1326 sal
.line
= last_line_listed
;
1328 sals
.sals
= (struct symtab_and_line
*)
1329 xmalloc (sizeof (struct symtab_and_line
));
1334 sals
= decode_line_spec_1 (arg
, 0);
1339 /* C++ More than one line may have been specified, as when the user
1340 specifies an overloaded function name. Print info on them all. */
1341 for (i
= 0; i
< sals
.nelts
; i
++)
1345 if (sal
.symtab
== 0)
1347 printf_filtered (_("No line number information available"));
1350 /* This is useful for "info line *0x7f34". If we can't tell the
1351 user about a source line, at least let them have the symbolic
1353 printf_filtered (" for address ");
1355 print_address (sal
.pc
, gdb_stdout
);
1358 printf_filtered (".");
1359 printf_filtered ("\n");
1361 else if (sal
.line
> 0
1362 && find_line_pc_range (sal
, &start_pc
, &end_pc
))
1364 if (start_pc
== end_pc
)
1366 printf_filtered ("Line %d of \"%s\"",
1367 sal
.line
, sal
.symtab
->filename
);
1369 printf_filtered (" is at address ");
1370 print_address (start_pc
, gdb_stdout
);
1372 printf_filtered (" but contains no code.\n");
1376 printf_filtered ("Line %d of \"%s\"",
1377 sal
.line
, sal
.symtab
->filename
);
1379 printf_filtered (" starts at address ");
1380 print_address (start_pc
, gdb_stdout
);
1382 printf_filtered (" and ends at ");
1383 print_address (end_pc
, gdb_stdout
);
1384 printf_filtered (".\n");
1387 /* x/i should display this line's code. */
1388 set_next_address (start_pc
);
1390 /* Repeating "info line" should do the following line. */
1391 last_line_listed
= sal
.line
+ 1;
1393 /* If this is the only line, show the source code. If it could
1394 not find the file, don't do anything special. */
1395 if (annotation_level
&& sals
.nelts
== 1)
1396 identify_source_line (sal
.symtab
, sal
.line
, 0, start_pc
);
1399 /* Is there any case in which we get here, and have an address
1400 which the user would want to see? If we have debugging symbols
1401 and no line numbers? */
1402 printf_filtered (_("Line number %d is out of range for \"%s\".\n"),
1403 sal
.line
, sal
.symtab
->filename
);
1408 /* Commands to search the source file for a regexp. */
1411 forward_search_command (char *regex
, int from_tty
)
1419 line
= last_line_listed
+ 1;
1421 msg
= (char *) re_comp (regex
);
1423 error (("%s"), msg
);
1425 if (current_source_symtab
== 0)
1426 select_source_symtab (0);
1428 desc
= open_source_file (current_source_symtab
);
1430 perror_with_name (current_source_symtab
->filename
);
1432 if (current_source_symtab
->line_charpos
== 0)
1433 find_source_lines (current_source_symtab
, desc
);
1435 if (line
< 1 || line
> current_source_symtab
->nlines
)
1438 error (_("Expression not found"));
1441 if (lseek (desc
, current_source_symtab
->line_charpos
[line
- 1], 0) < 0)
1444 perror_with_name (current_source_symtab
->filename
);
1447 stream
= fdopen (desc
, FDOPEN_MODE
);
1451 static char *buf
= NULL
;
1453 int cursize
, newsize
;
1456 buf
= xmalloc (cursize
);
1465 if (p
- buf
== cursize
)
1467 newsize
= cursize
+ cursize
/ 2;
1468 buf
= xrealloc (buf
, newsize
);
1473 while (c
!= '\n' && (c
= getc (stream
)) >= 0);
1475 /* Remove the \r, if any, at the end of the line, otherwise
1476 regular expressions that end with $ or \n won't work. */
1477 if (p
- buf
> 1 && p
[-2] == '\r')
1483 /* we now have a source line in buf, null terminate and match */
1485 if (re_exec (buf
) > 0)
1489 print_source_lines (current_source_symtab
, line
, line
+ 1, 0);
1490 set_internalvar (lookup_internalvar ("_"),
1491 value_from_longest (builtin_type_int
,
1493 current_source_line
= max (line
- lines_to_list
/ 2, 1);
1499 printf_filtered (_("Expression not found\n"));
1504 reverse_search_command (char *regex
, int from_tty
)
1512 line
= last_line_listed
- 1;
1514 msg
= (char *) re_comp (regex
);
1516 error (("%s"), msg
);
1518 if (current_source_symtab
== 0)
1519 select_source_symtab (0);
1521 desc
= open_source_file (current_source_symtab
);
1523 perror_with_name (current_source_symtab
->filename
);
1525 if (current_source_symtab
->line_charpos
== 0)
1526 find_source_lines (current_source_symtab
, desc
);
1528 if (line
< 1 || line
> current_source_symtab
->nlines
)
1531 error (_("Expression not found"));
1534 if (lseek (desc
, current_source_symtab
->line_charpos
[line
- 1], 0) < 0)
1537 perror_with_name (current_source_symtab
->filename
);
1540 stream
= fdopen (desc
, FDOPEN_MODE
);
1544 /* FIXME!!! We walk right off the end of buf if we get a long line!!! */
1545 char buf
[4096]; /* Should be reasonable??? */
1555 while (c
!= '\n' && (c
= getc (stream
)) >= 0);
1557 /* Remove the \r, if any, at the end of the line, otherwise
1558 regular expressions that end with $ or \n won't work. */
1559 if (p
- buf
> 1 && p
[-2] == '\r')
1565 /* We now have a source line in buf; null terminate and match. */
1567 if (re_exec (buf
) > 0)
1571 print_source_lines (current_source_symtab
, line
, line
+ 1, 0);
1572 set_internalvar (lookup_internalvar ("_"),
1573 value_from_longest (builtin_type_int
,
1575 current_source_line
= max (line
- lines_to_list
/ 2, 1);
1579 if (fseek (stream
, current_source_symtab
->line_charpos
[line
- 1], 0) < 0)
1582 perror_with_name (current_source_symtab
->filename
);
1586 printf_filtered (_("Expression not found\n"));
1592 _initialize_source (void)
1594 struct cmd_list_element
*c
;
1595 current_source_symtab
= 0;
1596 init_source_path ();
1598 /* The intention is to use POSIX Basic Regular Expressions.
1599 Always use the GNU regex routine for consistency across all hosts.
1600 Our current GNU regex.c does not have all the POSIX features, so this is
1601 just an approximation. */
1602 re_set_syntax (RE_SYNTAX_GREP
);
1604 c
= add_cmd ("directory", class_files
, directory_command
, _("\
1605 Add directory DIR to beginning of search path for source files.\n\
1606 Forget cached info on source file locations and line positions.\n\
1607 DIR can also be $cwd for the current working directory, or $cdir for the\n\
1608 directory in which the source file was compiled into object code.\n\
1609 With no argument, reset the search path to $cdir:$cwd, the default."),
1613 add_com_alias ("use", "directory", class_files
, 0);
1615 set_cmd_completer (c
, filename_completer
);
1617 add_cmd ("directories", no_class
, show_directories
, _("\
1618 Current search path for finding source files.\n\
1619 $cwd in the path means the current working directory.\n\
1620 $cdir in the path means the compilation directory of the source file."),
1625 add_com_alias ("D", "directory", class_files
, 0);
1626 add_cmd ("ld", no_class
, show_directories
, _("\
1627 Current search path for finding source files.\n\
1628 $cwd in the path means the current working directory.\n\
1629 $cdir in the path means the compilation directory of the source file."),
1633 add_info ("source", source_info
,
1634 _("Information about the current source file."));
1636 add_info ("line", line_info
, _("\
1637 Core addresses of the code for a source line.\n\
1638 Line can be specified as\n\
1639 LINENUM, to list around that line in current file,\n\
1640 FILE:LINENUM, to list around that line in that file,\n\
1641 FUNCTION, to list around beginning of that function,\n\
1642 FILE:FUNCTION, to distinguish among like-named static functions.\n\
1643 Default is to describe the last source line that was listed.\n\n\
1644 This sets the default address for \"x\" to the line's first instruction\n\
1645 so that \"x/i\" suffices to start examining the machine code.\n\
1646 The address is also stored as the value of \"$_\"."));
1648 add_com ("forward-search", class_files
, forward_search_command
, _("\
1649 Search for regular expression (see regex(3)) from last line listed.\n\
1650 The matching line number is also stored as the value of \"$_\"."));
1651 add_com_alias ("search", "forward-search", class_files
, 0);
1653 add_com ("reverse-search", class_files
, reverse_search_command
, _("\
1654 Search backward for regular expression (see regex(3)) from last line listed.\n\
1655 The matching line number is also stored as the value of \"$_\"."));
1659 add_com_alias ("/", "forward-search", class_files
, 0);
1660 add_com_alias ("?", "reverse-search", class_files
, 0);
1663 add_setshow_integer_cmd ("listsize", class_support
, &lines_to_list
, _("\
1664 Set number of source lines gdb will list by default."), _("\
1665 Show number of source lines gdb will list by default."), NULL
,
1668 &setlist
, &showlist
);