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"
49 #ifdef CRLF_SOURCE_FILES
51 /* Define CRLF_SOURCE_FILES in an xm-*.h file if source files on the
52 host use \r\n rather than just \n. Defining CRLF_SOURCE_FILES is
53 much faster than defining LSEEK_NOT_LINEAR. */
59 #define OPEN_MODE (O_RDONLY | O_BINARY)
60 #define FDOPEN_MODE FOPEN_RB
62 #else /* ! defined (CRLF_SOURCE_FILES) */
64 #define OPEN_MODE O_RDONLY
65 #define FDOPEN_MODE FOPEN_RT
67 #endif /* ! defined (CRLF_SOURCE_FILES) */
69 /* Prototypes for exported functions. */
71 void _initialize_source (void);
73 /* Prototypes for local functions. */
75 static int get_filename_and_charpos (struct symtab *, char **);
77 static void reverse_search_command (char *, int);
79 static void forward_search_command (char *, int);
81 static void line_info (char *, int);
83 static void source_info (char *, int);
85 static void show_directories (char *, int);
87 /* Path of directories to search for source files.
88 Same format as the PATH environment variable's value. */
92 /* Symtab of default file for listing lines of. */
94 static struct symtab *current_source_symtab;
96 /* Default next line to list. */
98 static int current_source_line;
100 /* Default number of lines to print with commands like "list".
101 This is based on guessing how many long (i.e. more than chars_per_line
102 characters) lines there will be. To be completely correct, "list"
103 and friends should be rewritten to count characters and see where
104 things are wrapping, but that would be a fair amount of work. */
106 int lines_to_list = 10;
108 /* Line number of last line printed. Default for various commands.
109 current_source_line is usually, but not always, the same as this. */
111 static int last_line_listed;
113 /* First line number listed by last listing command. */
115 static int first_line_listed;
117 /* Saves the name of the last source file visited and a possible error code.
118 Used to prevent repeating annoying "No such file or directories" msgs */
120 static struct symtab *last_source_visited = NULL;
121 static int last_source_error = 0;
123 /* Return the first line listed by print_source_lines.
124 Used by command interpreters to request listing from
128 get_first_line_listed (void)
130 return first_line_listed;
133 /* Return the default number of lines to print with commands like the
134 cli "list". The caller of print_source_lines must use this to
135 calculate the end line and use it in the call to print_source_lines
136 as it does not automatically use this value. */
139 get_lines_to_list (void)
141 return lines_to_list;
144 /* Return the current source file for listing and next line to list.
145 NOTE: The returned sal pc and end fields are not valid. */
147 struct symtab_and_line
148 get_current_source_symtab_and_line (void)
150 struct symtab_and_line cursal;
152 cursal.symtab = current_source_symtab;
153 cursal.line = current_source_line;
160 /* If the current source file for listing is not set, try and get a default.
161 Usually called before get_current_source_symtab_and_line() is called.
162 It may err out if a default cannot be determined.
163 We must be cautious about where it is called, as it can recurse as the
164 process of determining a new default may call the caller!
165 Use get_current_source_symtab_and_line only to get whatever
166 we have without erroring out or trying to get a default. */
169 set_default_source_symtab_and_line (void)
171 struct symtab_and_line cursal;
173 if (!have_full_symbols () && !have_partial_symbols ())
174 error ("No symbol table is loaded. Use the \"file\" command.");
176 /* Pull in a current source symtab if necessary */
177 if (current_source_symtab == 0)
178 select_source_symtab (0);
181 /* Return the current default file for listing and next line to list
182 (the returned sal pc and end fields are not valid.)
183 and set the current default to whatever is in SAL.
184 NOTE: The returned sal pc and end fields are not valid. */
186 struct symtab_and_line
187 set_current_source_symtab_and_line (const struct symtab_and_line *sal)
189 struct symtab_and_line cursal;
191 cursal.symtab = current_source_symtab;
192 cursal.line = current_source_line;
194 current_source_symtab = sal->symtab;
195 current_source_line = sal->line;
202 /* Reset any information stored about a default file and line to print. */
205 clear_current_source_symtab_and_line (void)
207 current_source_symtab = 0;
208 current_source_line = 0;
211 /* Set the source file default for the "list" command to be S.
213 If S is NULL, and we don't have a default, find one. This
214 should only be called when the user actually tries to use the
215 default, since we produce an error if we can't find a reasonable
216 default. Also, since this can cause symbols to be read, doing it
217 before we need to would make things slower than necessary. */
220 select_source_symtab (struct symtab *s)
222 struct symtabs_and_lines sals;
223 struct symtab_and_line sal;
224 struct partial_symtab *ps;
225 struct partial_symtab *cs_pst = 0;
230 current_source_symtab = s;
231 current_source_line = 1;
235 if (current_source_symtab)
238 /* Make the default place to list be the function `main'
240 if (lookup_symbol (main_name (), 0, VAR_DOMAIN, 0, NULL))
242 sals = decode_line_spec (main_name (), 1);
245 current_source_symtab = sal.symtab;
246 current_source_line = max (sal.line - (lines_to_list - 1), 1);
247 if (current_source_symtab)
251 /* All right; find the last file in the symtab list (ignoring .h's). */
253 current_source_line = 1;
255 for (ofp = object_files; ofp != NULL; ofp = ofp->next)
257 for (s = ofp->symtabs; s; s = s->next)
259 char *name = s->filename;
260 int len = strlen (name);
261 if (!(len > 2 && (DEPRECATED_STREQ (&name[len - 2], ".h"))))
263 current_source_symtab = s;
267 if (current_source_symtab)
270 /* Howabout the partial symbol tables? */
272 for (ofp = object_files; ofp != NULL; ofp = ofp->next)
274 for (ps = ofp->psymtabs; ps != NULL; ps = ps->next)
276 char *name = ps->filename;
277 int len = strlen (name);
278 if (!(len > 2 && (DEPRECATED_STREQ (&name[len - 2], ".h"))))
288 internal_error (__FILE__, __LINE__,
289 "select_source_symtab: "
290 "readin pst found and no symtabs.");
294 current_source_symtab = PSYMTAB_TO_SYMTAB (cs_pst);
297 if (current_source_symtab)
300 error ("Can't find a default source file");
304 show_directories (char *ignore, int from_tty)
306 puts_filtered ("Source directories searched: ");
307 puts_filtered (source_path);
308 puts_filtered ("\n");
311 /* Forget what we learned about line positions in source files, and
312 which directories contain them; must check again now since files
313 may be found in a different directory now. */
316 forget_cached_source_info (void)
319 struct objfile *objfile;
320 struct partial_symtab *pst;
322 for (objfile = object_files; objfile != NULL; objfile = objfile->next)
324 for (s = objfile->symtabs; s != NULL; s = s->next)
326 if (s->line_charpos != NULL)
328 xmfree (objfile->md, s->line_charpos);
329 s->line_charpos = NULL;
331 if (s->fullname != NULL)
333 xmfree (objfile->md, s->fullname);
338 ALL_OBJFILE_PSYMTABS (objfile, pst)
340 if (pst->fullname != NULL)
342 xfree (pst->fullname);
343 pst->fullname = NULL;
350 init_source_path (void)
354 sprintf (buf, "$cdir%c$cwd", DIRNAME_SEPARATOR);
355 source_path = xstrdup (buf);
356 forget_cached_source_info ();
360 init_last_source_visited (void)
362 last_source_visited = NULL;
365 /* Add zero or more directories to the front of the source path. */
368 directory_command (char *dirname, int from_tty)
371 /* FIXME, this goes to "delete dir"... */
374 if (from_tty && query ("Reinitialize source path to empty? "))
382 mod_path (dirname, &source_path);
383 last_source_visited = NULL;
386 show_directories ((char *) 0, from_tty);
387 forget_cached_source_info ();
390 /* Add zero or more directories to the front of an arbitrary path. */
393 mod_path (char *dirname, char **which_path)
395 add_path (dirname, which_path, 1);
398 /* Workhorse of mod_path. Takes an extra argument to determine
399 if dirname should be parsed for separators that indicate multiple
400 directories. This allows for interfaces that pre-parse the dirname
401 and allow specification of traditional separator characters such
405 add_path (char *dirname, char **which_path, int parse_separators)
407 char *old = *which_path;
413 dirname = xstrdup (dirname);
414 make_cleanup (xfree, dirname);
418 char *name = dirname;
423 char *separator = NULL;
427 if (parse_separators)
429 separator = strchr (name, DIRNAME_SEPARATOR);
430 space = strchr (name, ' ');
431 tab = strchr (name, '\t');
434 if (separator == 0 && space == 0 && tab == 0)
435 p = dirname = name + strlen (name);
439 if (separator != 0 && (p == 0 || separator < p))
441 if (space != 0 && (p == 0 || space < p))
443 if (tab != 0 && (p == 0 || tab < p))
446 while (*dirname == DIRNAME_SEPARATOR
453 if (!(IS_DIR_SEPARATOR (*name) && p <= name + 1) /* "/" */
454 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
455 /* On MS-DOS and MS-Windows, h:\ is different from h: */
456 && !(p == name + 3 && name[1] == ':') /* "d:/" */
458 && IS_DIR_SEPARATOR (p[-1]))
459 /* Sigh. "foo/" => "foo" */
463 while (p > name && p[-1] == '.')
467 /* "." => getwd (). */
468 name = current_directory;
471 else if (p > name + 1 && IS_DIR_SEPARATOR (p[-2]))
481 /* "...foo/." => "...foo". */
492 name = tilde_expand (name);
493 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
494 else if (IS_ABSOLUTE_PATH (name) && p == name + 2) /* "d:" => "d:." */
495 name = concat (name, ".", NULL);
497 else if (!IS_ABSOLUTE_PATH (name) && name[0] != '$')
498 name = concat (current_directory, SLASH_STRING, name, NULL);
500 name = savestring (name, p - name);
501 make_cleanup (xfree, name);
503 /* Unless it's a variable, check existence. */
506 /* These are warnings, not errors, since we don't want a
507 non-existent directory in a .gdbinit file to stop processing
508 of the .gdbinit file.
510 Whether they get added to the path is more debatable. Current
511 answer is yes, in case the user wants to go make the directory
512 or whatever. If the directory continues to not exist/not be
513 a directory/etc, then having them in the path should be
515 if (stat (name, &st) < 0)
517 int save_errno = errno;
518 fprintf_unfiltered (gdb_stderr, "Warning: ");
519 print_sys_errmsg (name, save_errno);
521 else if ((st.st_mode & S_IFMT) != S_IFDIR)
522 warning ("%s is not a directory.", name);
527 unsigned int len = strlen (name);
532 /* FIXME: strncmp loses in interesting ways on MS-DOS and
533 MS-Windows because of case-insensitivity and two different
534 but functionally identical slash characters. We need a
535 special filesystem-dependent file-name comparison function.
537 Actually, even on Unix I would use realpath() or its work-
538 alike before comparing. Then all the code above which
539 removes excess slashes and dots could simply go away. */
540 if (!strncmp (p, name, len)
541 && (p[len] == '\0' || p[len] == DIRNAME_SEPARATOR))
543 /* Found it in the search path, remove old copy */
545 p--; /* Back over leading separator */
546 if (prefix > p - *which_path)
547 goto skip_dup; /* Same dir twice in one cmd */
548 strcpy (p, &p[len + 1]); /* Copy from next \0 or : */
550 p = strchr (p, DIRNAME_SEPARATOR);
560 tinybuf[0] = DIRNAME_SEPARATOR;
563 /* If we have already tacked on a name(s) in this command, be sure they stay
564 on the front as we tack on some more. */
571 temp = concat (old, tinybuf, name, NULL);
573 *which_path = concat (temp, "", &old[prefix], NULL);
574 prefix = strlen (temp);
579 *which_path = concat (name, (old[0] ? tinybuf : old), old, NULL);
580 prefix = strlen (name);
588 while (*dirname != '\0');
593 source_info (char *ignore, int from_tty)
595 struct symtab *s = current_source_symtab;
599 printf_filtered ("No current source file.\n");
602 printf_filtered ("Current source file is %s\n", s->filename);
604 printf_filtered ("Compilation directory is %s\n", s->dirname);
606 printf_filtered ("Located in %s\n", s->fullname);
608 printf_filtered ("Contains %d line%s.\n", s->nlines,
609 s->nlines == 1 ? "" : "s");
611 printf_filtered ("Source language is %s.\n", language_str (s->language));
612 printf_filtered ("Compiled with %s debugging format.\n", s->debugformat);
613 printf_filtered ("%s preprocessor macro info.\n",
614 s->macro_table ? "Includes" : "Does not include");
618 /* Return True if the file NAME exists and is a regular file */
620 is_regular_file (const char *name)
623 const int status = stat (name, &st);
625 /* Stat should never fail except when the file does not exist.
626 If stat fails, analyze the source of error and return True
627 unless the file does not exist, to avoid returning false results
628 on obscure systems where stat does not work as expected.
631 return (errno != ENOENT);
633 return S_ISREG (st.st_mode);
636 /* Open a file named STRING, searching path PATH (dir names sep by some char)
637 using mode MODE and protection bits PROT in the calls to open.
639 If TRY_CWD_FIRST, try to open ./STRING before searching PATH.
640 (ie pretend the first element of PATH is "."). This also indicates
641 that a slash in STRING disables searching of the path (this is
642 so that "exec-file ./foo" or "symbol-file ./foo" insures that you
643 get that particular version of foo or an error message).
645 If FILENAME_OPENED is non-null, set it to a newly allocated string naming
646 the actual file opened (this string will always start with a "/"). We
647 have to take special pains to avoid doubling the "/" between the directory
648 and the file, sigh! Emacs gets confuzzed by this when we print the
651 If a file is found, return the descriptor.
652 Otherwise, return -1, with errno set for the last name we tried to open. */
654 /* >>>> This should only allow files of certain types,
655 >>>> eg executable, non-directory */
657 openp (const char *path, int try_cwd_first, const char *string,
659 char **filename_opened)
671 #if defined(_WIN32) || defined(__CYGWIN__)
675 if (try_cwd_first || IS_ABSOLUTE_PATH (string))
679 if (is_regular_file (string))
681 filename = alloca (strlen (string) + 1);
682 strcpy (filename, string);
683 fd = open (filename, mode, prot);
693 for (i = 0; string[i]; i++)
694 if (IS_DIR_SEPARATOR (string[i]))
699 while (string[0] == '.' && IS_DIR_SEPARATOR (string[1]))
702 alloclen = strlen (path) + strlen (string) + 2;
703 filename = alloca (alloclen);
705 for (p = path; p; p = p1 ? p1 + 1 : 0)
707 p1 = strchr (p, DIRNAME_SEPARATOR);
713 if (len == 4 && p[0] == '$' && p[1] == 'c'
714 && p[2] == 'w' && p[3] == 'd')
716 /* Name is $cwd -- insert current directory name instead. */
719 /* First, realloc the filename buffer if too short. */
720 len = strlen (current_directory);
721 newlen = len + strlen (string) + 2;
722 if (newlen > alloclen)
725 filename = alloca (alloclen);
727 strcpy (filename, current_directory);
731 /* Normal file name in path -- just use it. */
732 strncpy (filename, p, len);
736 /* Remove trailing slashes */
737 while (len > 0 && IS_DIR_SEPARATOR (filename[len - 1]))
740 strcat (filename + len, SLASH_STRING);
741 strcat (filename, string);
743 if (is_regular_file (filename))
745 fd = open (filename, mode);
754 /* If a file was opened, canonicalize its filename. Use xfullpath
755 rather than gdb_realpath to avoid resolving the basename part
756 of filenames when the associated file is a symbolic link. This
757 fixes a potential inconsistency between the filenames known to
758 GDB and the filenames it prints in the annotations. */
760 *filename_opened = NULL;
761 else if (IS_ABSOLUTE_PATH (filename))
762 *filename_opened = xfullpath (filename);
765 /* Beware the // my son, the Emacs barfs, the botch that catch... */
767 char *f = concat (current_directory,
768 IS_DIR_SEPARATOR (current_directory[strlen (current_directory) - 1])
771 *filename_opened = xfullpath (f);
780 /* This is essentially a convenience, for clients that want the behaviour
781 of openp, using source_path, but that really don't want the file to be
782 opened but want instead just to know what the full pathname is (as
783 qualified against source_path).
785 The current working directory is searched first.
787 If the file was found, this function returns 1, and FULL_PATHNAME is
788 set to the fully-qualified pathname.
790 Else, this functions returns 0, and FULL_PATHNAME is set to NULL. */
792 source_full_path_of (char *filename, char **full_pathname)
796 fd = openp (source_path, 1, filename, O_RDONLY, 0, full_pathname);
799 *full_pathname = NULL;
807 /* This function is capable of finding the absolute path to a
808 source file, and opening it, provided you give it an
809 OBJFILE and FILENAME. Both the DIRNAME and FULLNAME are only
810 added suggestions on where to find the file.
812 OBJFILE should be the objfile associated with a psymtab or symtab.
813 FILENAME should be the filename to open.
814 DIRNAME is the compilation directory of a particular source file.
815 Only some debug formats provide this info.
816 FULLNAME can be the last known absolute path to the file in question.
819 A valid file descriptor is returned. ( the return value is positive )
820 FULLNAME is set to the absolute path to the file just opened.
823 A non valid file descriptor is returned. ( the return value is negitive )
824 FULLNAME is set to NULL. */
826 find_and_open_source (struct objfile *objfile,
827 const char *filename,
831 char *path = source_path;
835 /* Quick way out if we already know its full name */
838 result = open (*fullname, OPEN_MODE);
841 /* Didn't work -- free old one, try again. */
842 xmfree (objfile->md, *fullname);
848 /* Replace a path entry of $cdir with the compilation directory name */
850 /* We cast strstr's result in case an ANSIhole has made it const,
851 which produces a "required warning" when assigned to a nonconst. */
852 p = (char *) strstr (source_path, "$cdir");
853 if (p && (p == path || p[-1] == DIRNAME_SEPARATOR)
854 && (p[cdir_len] == DIRNAME_SEPARATOR || p[cdir_len] == '\0'))
859 alloca (strlen (source_path) + 1 + strlen (dirname) + 1);
860 len = p - source_path;
861 strncpy (path, source_path, len); /* Before $cdir */
862 strcpy (path + len, dirname); /* new stuff */
863 strcat (path + len, source_path + len + cdir_len); /* After $cdir */
867 result = openp (path, 0, filename, OPEN_MODE, 0, fullname);
870 /* Didn't work. Try using just the basename. */
871 p = lbasename (filename);
873 result = openp (path, 0, p, OPEN_MODE, 0, fullname);
879 tmp_fullname = *fullname;
880 *fullname = mstrsave (objfile->md, *fullname);
881 xfree (tmp_fullname);
886 /* Open a source file given a symtab S. Returns a file descriptor or
887 negative number for error.
889 This function is a convience function to find_and_open_source. */
892 open_source_file (struct symtab *s)
897 return find_and_open_source (s->objfile, s->filename, s->dirname,
901 /* Finds the fullname that a symtab represents.
903 If this functions finds the fullname, it will save it in ps->fullname
904 and it will also return the value.
906 If this function fails to find the file that this symtab represents,
907 NULL will be returned and ps->fullname will be set to NULL. */
909 symtab_to_fullname (struct symtab *s)
916 /* Don't check s->fullname here, the file could have been
917 deleted/moved/..., look for it again */
918 r = find_and_open_source (s->objfile, s->filename, s->dirname,
930 /* Finds the fullname that a partial_symtab represents.
932 If this functions finds the fullname, it will save it in ps->fullname
933 and it will also return the value.
935 If this function fails to find the file that this partial_symtab represents,
936 NULL will be returned and ps->fullname will be set to NULL. */
938 psymtab_to_fullname (struct partial_symtab *ps)
945 /* Don't check ps->fullname here, the file could have been
946 deleted/moved/..., look for it again */
947 r = find_and_open_source (ps->objfile, ps->filename, ps->dirname,
959 /* Create and initialize the table S->line_charpos that records
960 the positions of the lines in the source file, which is assumed
961 to be open on descriptor DESC.
962 All set S->nlines to the number of such lines. */
965 find_source_lines (struct symtab *s, int desc)
968 char *data, *p, *end;
970 int lines_allocated = 1000;
975 line_charpos = (int *) xmmalloc (s->objfile->md,
976 lines_allocated * sizeof (int));
977 if (fstat (desc, &st) < 0)
978 perror_with_name (s->filename);
980 if (s && s->objfile && s->objfile->obfd)
981 mtime = bfd_get_mtime (s->objfile->obfd);
983 mtime = bfd_get_mtime (exec_bfd);
985 if (mtime && mtime < st.st_mtime)
987 warning ("Source file is more recent than executable.\n");
990 #ifdef LSEEK_NOT_LINEAR
994 /* Have to read it byte by byte to find out where the chars live */
996 line_charpos[0] = lseek (desc, 0, SEEK_CUR);
998 while (myread (desc, &c, 1) > 0)
1002 if (nlines == lines_allocated)
1004 lines_allocated *= 2;
1006 (int *) xmrealloc (s->objfile->md, (char *) line_charpos,
1007 sizeof (int) * lines_allocated);
1009 line_charpos[nlines++] = lseek (desc, 0, SEEK_CUR);
1013 #else /* lseek linear. */
1015 struct cleanup *old_cleanups;
1017 /* st_size might be a large type, but we only support source files whose
1018 size fits in an int. */
1019 size = (int) st.st_size;
1021 /* Use malloc, not alloca, because this may be pretty large, and we may
1022 run into various kinds of limits on stack size. */
1023 data = (char *) xmalloc (size);
1024 old_cleanups = make_cleanup (xfree, data);
1026 /* Reassign `size' to result of read for systems where \r\n -> \n. */
1027 size = myread (desc, data, size);
1029 perror_with_name (s->filename);
1032 line_charpos[0] = 0;
1037 /* A newline at the end does not start a new line. */
1040 if (nlines == lines_allocated)
1042 lines_allocated *= 2;
1044 (int *) xmrealloc (s->objfile->md, (char *) line_charpos,
1045 sizeof (int) * lines_allocated);
1047 line_charpos[nlines++] = p - data;
1050 do_cleanups (old_cleanups);
1052 #endif /* lseek linear. */
1055 (int *) xmrealloc (s->objfile->md, (char *) line_charpos,
1056 nlines * sizeof (int));
1060 /* Return the character position of a line LINE in symtab S.
1061 Return 0 if anything is invalid. */
1063 #if 0 /* Currently unused */
1066 source_line_charpos (struct symtab *s, int line)
1070 if (!s->line_charpos || line <= 0)
1072 if (line > s->nlines)
1074 return s->line_charpos[line - 1];
1077 /* Return the line number of character position POS in symtab S. */
1080 source_charpos_line (struct symtab *s, int chr)
1085 if (s == 0 || s->line_charpos == 0)
1087 lnp = s->line_charpos;
1088 /* Files are usually short, so sequential search is Ok */
1089 while (line < s->nlines && *lnp <= chr)
1094 if (line >= s->nlines)
1102 /* Get full pathname and line number positions for a symtab.
1103 Return nonzero if line numbers may have changed.
1104 Set *FULLNAME to actual name of the file as found by `openp',
1105 or to 0 if the file is not found. */
1108 get_filename_and_charpos (struct symtab *s, char **fullname)
1110 int desc, linenums_changed = 0;
1112 desc = open_source_file (s);
1120 *fullname = s->fullname;
1121 if (s->line_charpos == 0)
1122 linenums_changed = 1;
1123 if (linenums_changed)
1124 find_source_lines (s, desc);
1126 return linenums_changed;
1129 /* Print text describing the full name of the source file S
1130 and the line number LINE and its corresponding character position.
1131 The text starts with two Ctrl-z so that the Emacs-GDB interface
1134 MID_STATEMENT is nonzero if the PC is not at the beginning of that line.
1136 Return 1 if successful, 0 if could not find the file. */
1139 identify_source_line (struct symtab *s, int line, int mid_statement,
1142 if (s->line_charpos == 0)
1143 get_filename_and_charpos (s, (char **) NULL);
1144 if (s->fullname == 0)
1146 if (line > s->nlines)
1147 /* Don't index off the end of the line_charpos array. */
1149 annotate_source (s->fullname, line, s->line_charpos[line - 1],
1152 current_source_line = line;
1153 first_line_listed = line;
1154 last_line_listed = line;
1155 current_source_symtab = s;
1160 /* Print source lines from the file of symtab S,
1161 starting with line number LINE and stopping before line number STOPLINE. */
1163 static void print_source_lines_base (struct symtab *s, int line, int stopline,
1166 print_source_lines_base (struct symtab *s, int line, int stopline, int noerror)
1171 int nlines = stopline - line;
1173 /* Regardless of whether we can open the file, set current_source_symtab. */
1174 current_source_symtab = s;
1175 current_source_line = line;
1176 first_line_listed = line;
1178 /* If printing of source lines is disabled, just print file and line number */
1179 if (ui_out_test_flags (uiout, ui_source_list))
1181 /* Only prints "No such file or directory" once */
1182 if ((s != last_source_visited) || (!last_source_error))
1184 last_source_visited = s;
1185 desc = open_source_file (s);
1189 desc = last_source_error;
1201 last_source_error = desc;
1205 char *name = alloca (strlen (s->filename) + 100);
1206 sprintf (name, "%d\t%s", line, s->filename);
1207 print_sys_errmsg (name, errno);
1210 ui_out_field_int (uiout, "line", line);
1211 ui_out_text (uiout, "\tin ");
1212 ui_out_field_string (uiout, "file", s->filename);
1213 ui_out_text (uiout, "\n");
1218 last_source_error = 0;
1220 if (s->line_charpos == 0)
1221 find_source_lines (s, desc);
1223 if (line < 1 || line > s->nlines)
1226 error ("Line number %d out of range; %s has %d lines.",
1227 line, s->filename, s->nlines);
1230 if (lseek (desc, s->line_charpos[line - 1], 0) < 0)
1233 perror_with_name (s->filename);
1236 stream = fdopen (desc, FDOPEN_MODE);
1239 while (nlines-- > 0)
1246 last_line_listed = current_source_line;
1247 sprintf (buf, "%d\t", current_source_line++);
1248 ui_out_text (uiout, buf);
1251 if (c < 040 && c != '\t' && c != '\n' && c != '\r')
1253 sprintf (buf, "^%c", c + 0100);
1254 ui_out_text (uiout, buf);
1257 ui_out_text (uiout, "^?");
1258 #ifdef CRLF_SOURCE_FILES
1261 /* Skip a \r character, but only before a \n. */
1262 int c1 = fgetc (stream);
1265 printf_filtered ("^%c", c + 0100);
1267 ungetc (c1, stream);
1272 sprintf (buf, "%c", c);
1273 ui_out_text (uiout, buf);
1276 while (c != '\n' && (c = fgetc (stream)) >= 0);
1282 /* Show source lines from the file of symtab S, starting with line
1283 number LINE and stopping before line number STOPLINE. If this is the
1284 not the command line version, then the source is shown in the source
1285 window otherwise it is simply printed */
1288 print_source_lines (struct symtab *s, int line, int stopline, int noerror)
1290 print_source_lines_base (s, line, stopline, noerror);
1293 /* Print info on range of pc's in a specified line. */
1296 line_info (char *arg, int from_tty)
1298 struct symtabs_and_lines sals;
1299 struct symtab_and_line sal;
1300 CORE_ADDR start_pc, end_pc;
1303 init_sal (&sal); /* initialize to zeroes */
1307 sal.symtab = current_source_symtab;
1308 sal.line = last_line_listed;
1310 sals.sals = (struct symtab_and_line *)
1311 xmalloc (sizeof (struct symtab_and_line));
1316 sals = decode_line_spec_1 (arg, 0);
1321 /* C++ More than one line may have been specified, as when the user
1322 specifies an overloaded function name. Print info on them all. */
1323 for (i = 0; i < sals.nelts; i++)
1327 if (sal.symtab == 0)
1329 printf_filtered ("No line number information available");
1332 /* This is useful for "info line *0x7f34". If we can't tell the
1333 user about a source line, at least let them have the symbolic
1335 printf_filtered (" for address ");
1337 print_address (sal.pc, gdb_stdout);
1340 printf_filtered (".");
1341 printf_filtered ("\n");
1343 else if (sal.line > 0
1344 && find_line_pc_range (sal, &start_pc, &end_pc))
1346 if (start_pc == end_pc)
1348 printf_filtered ("Line %d of \"%s\"",
1349 sal.line, sal.symtab->filename);
1351 printf_filtered (" is at address ");
1352 print_address (start_pc, gdb_stdout);
1354 printf_filtered (" but contains no code.\n");
1358 printf_filtered ("Line %d of \"%s\"",
1359 sal.line, sal.symtab->filename);
1361 printf_filtered (" starts at address ");
1362 print_address (start_pc, gdb_stdout);
1364 printf_filtered (" and ends at ");
1365 print_address (end_pc, gdb_stdout);
1366 printf_filtered (".\n");
1369 /* x/i should display this line's code. */
1370 set_next_address (start_pc);
1372 /* Repeating "info line" should do the following line. */
1373 last_line_listed = sal.line + 1;
1375 /* If this is the only line, show the source code. If it could
1376 not find the file, don't do anything special. */
1377 if (annotation_level && sals.nelts == 1)
1378 identify_source_line (sal.symtab, sal.line, 0, start_pc);
1381 /* Is there any case in which we get here, and have an address
1382 which the user would want to see? If we have debugging symbols
1383 and no line numbers? */
1384 printf_filtered ("Line number %d is out of range for \"%s\".\n",
1385 sal.line, sal.symtab->filename);
1390 /* Commands to search the source file for a regexp. */
1393 forward_search_command (char *regex, int from_tty)
1401 line = last_line_listed + 1;
1403 msg = (char *) re_comp (regex);
1407 if (current_source_symtab == 0)
1408 select_source_symtab (0);
1410 desc = open_source_file (current_source_symtab);
1412 perror_with_name (current_source_symtab->filename);
1414 if (current_source_symtab->line_charpos == 0)
1415 find_source_lines (current_source_symtab, desc);
1417 if (line < 1 || line > current_source_symtab->nlines)
1420 error ("Expression not found");
1423 if (lseek (desc, current_source_symtab->line_charpos[line - 1], 0) < 0)
1426 perror_with_name (current_source_symtab->filename);
1429 stream = fdopen (desc, FDOPEN_MODE);
1433 static char *buf = NULL;
1435 int cursize, newsize;
1438 buf = xmalloc (cursize);
1447 if (p - buf == cursize)
1449 newsize = cursize + cursize / 2;
1450 buf = xrealloc (buf, newsize);
1455 while (c != '\n' && (c = getc (stream)) >= 0);
1457 #ifdef CRLF_SOURCE_FILES
1458 /* Remove the \r, if any, at the end of the line, otherwise
1459 regular expressions that end with $ or \n won't work. */
1460 if (p - buf > 1 && p[-2] == '\r')
1467 /* we now have a source line in buf, null terminate and match */
1469 if (re_exec (buf) > 0)
1473 print_source_lines (current_source_symtab, line, line + 1, 0);
1474 set_internalvar (lookup_internalvar ("_"),
1475 value_from_longest (builtin_type_int,
1477 current_source_line = max (line - lines_to_list / 2, 1);
1483 printf_filtered ("Expression not found\n");
1488 reverse_search_command (char *regex, int from_tty)
1496 line = last_line_listed - 1;
1498 msg = (char *) re_comp (regex);
1502 if (current_source_symtab == 0)
1503 select_source_symtab (0);
1505 desc = open_source_file (current_source_symtab);
1507 perror_with_name (current_source_symtab->filename);
1509 if (current_source_symtab->line_charpos == 0)
1510 find_source_lines (current_source_symtab, desc);
1512 if (line < 1 || line > current_source_symtab->nlines)
1515 error ("Expression not found");
1518 if (lseek (desc, current_source_symtab->line_charpos[line - 1], 0) < 0)
1521 perror_with_name (current_source_symtab->filename);
1524 stream = fdopen (desc, FDOPEN_MODE);
1528 /* FIXME!!! We walk right off the end of buf if we get a long line!!! */
1529 char buf[4096]; /* Should be reasonable??? */
1539 while (c != '\n' && (c = getc (stream)) >= 0);
1541 #ifdef CRLF_SOURCE_FILES
1542 /* Remove the \r, if any, at the end of the line, otherwise
1543 regular expressions that end with $ or \n won't work. */
1544 if (p - buf > 1 && p[-2] == '\r')
1551 /* We now have a source line in buf; null terminate and match. */
1553 if (re_exec (buf) > 0)
1557 print_source_lines (current_source_symtab, line, line + 1, 0);
1558 set_internalvar (lookup_internalvar ("_"),
1559 value_from_longest (builtin_type_int,
1561 current_source_line = max (line - lines_to_list / 2, 1);
1565 if (fseek (stream, current_source_symtab->line_charpos[line - 1], 0) < 0)
1568 perror_with_name (current_source_symtab->filename);
1572 printf_filtered ("Expression not found\n");
1578 _initialize_source (void)
1580 struct cmd_list_element *c;
1581 current_source_symtab = 0;
1582 init_source_path ();
1584 /* The intention is to use POSIX Basic Regular Expressions.
1585 Always use the GNU regex routine for consistency across all hosts.
1586 Our current GNU regex.c does not have all the POSIX features, so this is
1587 just an approximation. */
1588 re_set_syntax (RE_SYNTAX_GREP);
1590 c = add_cmd ("directory", class_files, directory_command,
1591 "Add directory DIR to beginning of search path for source files.\n\
1592 Forget cached info on source file locations and line positions.\n\
1593 DIR can also be $cwd for the current working directory, or $cdir for the\n\
1594 directory in which the source file was compiled into object code.\n\
1595 With no argument, reset the search path to $cdir:$cwd, the default.",
1599 add_com_alias ("use", "directory", class_files, 0);
1601 set_cmd_completer (c, filename_completer);
1603 add_cmd ("directories", no_class, show_directories,
1604 "Current search path for finding source files.\n\
1605 $cwd in the path means the current working directory.\n\
1606 $cdir in the path means the compilation directory of the source file.",
1611 add_com_alias ("D", "directory", class_files, 0);
1612 add_cmd ("ld", no_class, show_directories,
1613 "Current search path for finding source files.\n\
1614 $cwd in the path means the current working directory.\n\
1615 $cdir in the path means the compilation directory of the source file.",
1619 add_info ("source", source_info,
1620 "Information about the current source file.");
1622 add_info ("line", line_info,
1623 concat ("Core addresses of the code for a source line.\n\
1624 Line can be specified as\n\
1625 LINENUM, to list around that line in current file,\n\
1626 FILE:LINENUM, to list around that line in that file,\n\
1627 FUNCTION, to list around beginning of that function,\n\
1628 FILE:FUNCTION, to distinguish among like-named static functions.\n\
1630 Default is to describe the last source line that was listed.\n\n\
1631 This sets the default address for \"x\" to the line's first instruction\n\
1632 so that \"x/i\" suffices to start examining the machine code.\n\
1633 The address is also stored as the value of \"$_\".", NULL));
1635 add_com ("forward-search", class_files, forward_search_command,
1636 "Search for regular expression (see regex(3)) from last line listed.\n\
1637 The matching line number is also stored as the value of \"$_\".");
1638 add_com_alias ("search", "forward-search", class_files, 0);
1640 add_com ("reverse-search", class_files, reverse_search_command,
1641 "Search backward for regular expression (see regex(3)) from last line listed.\n\
1642 The matching line number is also stored as the value of \"$_\".");
1646 add_com_alias ("/", "forward-search", class_files, 0);
1647 add_com_alias ("?", "reverse-search", class_files, 0);
1650 deprecated_add_show_from_set
1651 (add_set_cmd ("listsize", class_support, var_uinteger,
1652 (char *) &lines_to_list,
1653 "Set number of source lines gdb will list by default.",