1 /* List lines of source files for GDB, the GNU debugger.
2 Copyright (C) 1986, 1987, 1988, 1989, 1991 Free Software Foundation, Inc.
4 This file is part of GDB.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
22 #include "expression.h"
28 #include <sys/types.h>
30 #include <sys/param.h>
39 #ifndef DIRNAME_SEPARATOR
40 #define DIRNAME_SEPARATOR ':'
43 /* Prototypes for local functions. */
46 open_source_file PARAMS ((struct symtab *));
49 get_filename_and_charpos PARAMS ((struct symtab *, char **));
52 reverse_search_command PARAMS ((char *, int));
55 forward_search_command PARAMS ((char *, int));
58 line_info PARAMS ((char *, int));
61 list_command PARAMS ((char *, int));
64 ambiguous_line_spec PARAMS ((struct symtabs_and_lines *));
67 source_info PARAMS ((char *, int));
70 show_directories PARAMS ((char *, int));
73 find_source_lines PARAMS ((struct symtab *, int));
75 /* If we use this declaration, it breaks because of fucking ANSI "const" stuff
76 on some systems. We just have to not declare it at all, have it default
77 to int, and possibly botch on a few systems. Thanks, ANSIholes... */
78 /* extern char *strstr(); */
80 /* Path of directories to search for source files.
81 Same format as the PATH environment variable's value. */
85 /* Symtab of default file for listing lines of. */
87 struct symtab *current_source_symtab;
89 /* Default next line to list. */
91 int current_source_line;
93 /* Default number of lines to print with commands like "list".
94 This is based on guessing how many long (i.e. more than chars_per_line
95 characters) lines there will be. To be completely correct, "list"
96 and friends should be rewritten to count characters and see where
97 things are wrapping, but that would be a fair amount of work. */
99 int lines_to_list = 10;
101 /* Line number of last line printed. Default for various commands.
102 current_source_line is usually, but not always, the same as this. */
104 static int last_line_listed;
106 /* First line number listed by last listing command. */
108 static int first_line_listed;
111 /* Set the source file default for the "list" command to be S.
113 If S is NULL, and we don't have a default, find one. This
114 should only be called when the user actually tries to use the
115 default, since we produce an error if we can't find a reasonable
116 default. Also, since this can cause symbols to be read, doing it
117 before we need to would make things slower than necessary. */
120 select_source_symtab (s)
121 register struct symtab *s;
123 struct symtabs_and_lines sals;
124 struct symtab_and_line sal;
125 struct partial_symtab *ps;
126 struct partial_symtab *cs_pst = 0;
131 current_source_symtab = s;
132 current_source_line = 1;
136 if (current_source_symtab)
139 /* Make the default place to list be the function `main'
141 if (lookup_symbol ("main", 0, VAR_NAMESPACE, 0, NULL))
143 sals = decode_line_spec ("main", 1);
146 current_source_symtab = sal.symtab;
147 current_source_line = max (sal.line - (lines_to_list - 1), 1);
148 if (current_source_symtab)
152 /* All right; find the last file in the symtab list (ignoring .h's). */
154 current_source_line = 1;
156 for (ofp = object_files; ofp != NULL; ofp = ofp -> next)
158 for (s = ofp -> symtabs; s; s = s->next)
160 char *name = s -> filename;
161 int len = strlen (name);
162 if (! (len > 2 && (STREQ (&name[len - 2], ".h"))))
164 current_source_symtab = s;
168 if (current_source_symtab)
171 /* Howabout the partial symbol tables? */
173 for (ofp = object_files; ofp != NULL; ofp = ofp -> next)
175 for (ps = ofp -> psymtabs; ps != NULL; ps = ps -> next)
177 char *name = ps -> filename;
178 int len = strlen (name);
179 if (! (len > 2 && (STREQ (&name[len - 2], ".h"))))
187 if (cs_pst -> readin)
189 fatal ("Internal: select_source_symtab: readin pst found and no symtabs.");
193 current_source_symtab = PSYMTAB_TO_SYMTAB (cs_pst);
197 error ("Can't find a default source file");
201 show_directories (ignore, from_tty)
205 puts_filtered ("Source directories searched: ");
206 puts_filtered (source_path);
207 puts_filtered ("\n");
210 /* Forget what we learned about line positions in source files,
211 and which directories contain them;
212 must check again now since files may be found in
213 a different directory now. */
216 forget_cached_source_info ()
218 register struct symtab *s;
219 register struct objfile *objfile;
221 for (objfile = object_files; objfile != NULL; objfile = objfile -> next)
223 for (s = objfile -> symtabs; s != NULL; s = s -> next)
225 if (s -> line_charpos != NULL)
227 mfree (objfile -> md, s -> line_charpos);
228 s -> line_charpos = NULL;
230 if (s -> fullname != NULL)
232 mfree (objfile -> md, s -> fullname);
233 s -> fullname = NULL;
244 sprintf (buf, "$cdir%c$cwd", DIRNAME_SEPARATOR);
245 source_path = strsave (buf);
246 forget_cached_source_info ();
249 /* Add zero or more directories to the front of the source path. */
252 directory_command (dirname, from_tty)
257 /* FIXME, this goes to "delete dir"... */
260 if (query ("Reinitialize source path to empty? ", ""))
267 mod_path (dirname, &source_path);
269 show_directories ((char *)0, from_tty);
270 forget_cached_source_info ();
273 /* Add zero or more directories to the front of an arbitrary path. */
276 mod_path (dirname, which_path)
280 char *old = *which_path;
286 dirname = strsave (dirname);
287 make_cleanup (free, dirname);
291 char *name = dirname;
296 char *colon = strchr (name, DIRNAME_SEPARATOR);
297 char *space = strchr (name, ' ');
298 char *tab = strchr (name, '\t');
299 if (colon == 0 && space == 0 && tab == 0)
300 p = dirname = name + strlen (name);
304 if (colon != 0 && (p == 0 || colon < p))
306 if (space != 0 && (p == 0 || space < p))
308 if (tab != 0 && (p == 0 || tab < p))
311 while (*dirname == DIRNAME_SEPARATOR || *dirname == ' ' || *dirname == '\t')
317 /* Sigh. "foo/" => "foo" */
325 /* "." => getwd (). */
326 name = current_directory;
329 else if (p[-2] == '/')
339 /* "...foo/." => "...foo". */
350 name = tilde_expand (name);
351 else if (name[0] != '/' && name[0] != '$')
352 name = concat (current_directory, "/", name, NULL);
354 name = savestring (name, p - name);
355 make_cleanup (free, name);
357 /* Unless it's a variable, check existence. */
358 if (name[0] != '$') {
359 /* These are warnings, not errors, since we don't want a
360 non-existent directory in a .gdbinit file to stop processing
361 of the .gdbinit file.
363 Whether they get added to the path is more debatable. Current
364 answer is yes, in case the user wants to go make the directory
365 or whatever. If the directory continues to not exist/not be
366 a directory/etc, then having them in the path should be
368 if (stat (name, &st) < 0)
370 int save_errno = errno;
371 fprintf_unfiltered (gdb_stderr, "Warning: ");
372 print_sys_errmsg (name, save_errno);
374 else if ((st.st_mode & S_IFMT) != S_IFDIR)
375 warning ("%s is not a directory.", name);
380 register unsigned int len = strlen (name);
385 if (!strncmp (p, name, len)
386 && (p[len] == '\0' || p[len] == DIRNAME_SEPARATOR))
388 /* Found it in the search path, remove old copy */
390 p--; /* Back over leading colon */
391 if (prefix > p - *which_path)
392 goto skip_dup; /* Same dir twice in one cmd */
393 strcpy (p, &p[len+1]); /* Copy from next \0 or : */
395 p = strchr (p, DIRNAME_SEPARATOR);
405 tinybuf[0] = DIRNAME_SEPARATOR;
408 /* If we have already tacked on a name(s) in this command, be sure they stay on the front as we tack on some more. */
415 temp = concat (old, tinybuf, name, NULL);
417 *which_path = concat (temp, "", &old[prefix], NULL);
418 prefix = strlen (temp);
423 *which_path = concat (name, (old[0] ? tinybuf : old), old, NULL);
424 prefix = strlen (name);
431 } while (*dirname != '\0');
436 source_info (ignore, from_tty)
440 register struct symtab *s = current_source_symtab;
444 printf_filtered("No current source file.\n");
447 printf_filtered ("Current source file is %s\n", s->filename);
449 printf_filtered ("Compilation directory is %s\n", s->dirname);
451 printf_filtered ("Located in %s\n", s->fullname);
453 printf_filtered ("Contains %d line%s.\n", s->nlines,
454 s->nlines == 1 ? "" : "s");
456 printf_filtered("Source language is %s.\n", language_str (s->language));
461 /* Open a file named STRING, searching path PATH (dir names sep by some char)
462 using mode MODE and protection bits PROT in the calls to open.
464 If TRY_CWD_FIRST, try to open ./STRING before searching PATH.
465 (ie pretend the first element of PATH is "."). This also indicates
466 that a slash in STRING disables searching of the path (this is
467 so that "exec-file ./foo" or "symbol-file ./foo" insures that you
468 get that particular version of foo or an error message).
470 If FILENAMED_OPENED is non-null, set it to a newly allocated string naming
471 the actual file opened (this string will always start with a "/". We
472 have to take special pains to avoid doubling the "/" between the directory
473 and the file, sigh! Emacs gets confuzzed by this when we print the
476 If a file is found, return the descriptor.
477 Otherwise, return -1, with errno set for the last name we tried to open. */
479 /* >>>> This should only allow files of certain types,
480 >>>> eg executable, non-directory */
482 openp (path, try_cwd_first, string, mode, prot, filename_opened)
488 char **filename_opened;
491 register char *filename;
492 register char *p, *p1;
499 if (try_cwd_first || string[0] == '/')
502 fd = open (filename, mode, prot);
503 if (fd >= 0 || string[0] == '/' || strchr (string, '/'))
508 while (string[0] == '.' && string[1] == '/')
511 alloclen = strlen (path) + strlen (string) + 2;
512 filename = (char *) alloca (alloclen);
514 for (p = path; p; p = p1 ? p1 + 1 : 0)
516 p1 = (char *) strchr (p, DIRNAME_SEPARATOR);
522 if (len == 4 && p[0] == '$' && p[1] == 'c'
523 && p[2] == 'w' && p[3] == 'd') {
524 /* Name is $cwd -- insert current directory name instead. */
527 /* First, realloc the filename buffer if too short. */
528 len = strlen (current_directory);
529 newlen = len + strlen (string) + 2;
530 if (newlen > alloclen) {
532 filename = (char *) alloca (alloclen);
534 strcpy (filename, current_directory);
536 /* Normal file name in path -- just use it. */
537 strncpy (filename, p, len);
541 /* Remove trailing slashes */
542 while (len > 0 && filename[len-1] == '/')
545 strcat (filename+len, "/");
546 strcat (filename, string);
548 fd = open (filename, mode);
555 *filename_opened = (char *) 0;
556 else if (filename[0] == '/')
557 *filename_opened = savestring (filename, strlen (filename));
560 /* Beware the // my son, the Emacs barfs, the botch that catch... */
562 *filename_opened = concat (current_directory,
563 '/' == current_directory[strlen(current_directory)-1]? "": "/",
570 /* Open a source file given a symtab S. Returns a file descriptor
571 or negative number for error. */
577 char *path = source_path;
582 /* Quick way out if we already know its full name */
585 result = open (s->fullname, O_RDONLY);
588 /* Didn't work -- free old one, try again. */
589 mfree (s->objfile->md, s->fullname);
593 if (s->dirname != NULL)
595 /* Replace a path entry of $cdir with the compilation directory name */
597 /* We cast strstr's result in case an ANSIhole has made it const,
598 which produces a "required warning" when assigned to a nonconst. */
599 p = (char *)strstr (source_path, "$cdir");
600 if (p && (p == path || p[-1] == DIRNAME_SEPARATOR)
601 && (p[cdir_len] == DIRNAME_SEPARATOR || p[cdir_len] == '\0')) {
605 alloca (strlen (source_path) + 1 + strlen (s->dirname) + 1);
606 len = p - source_path;
607 strncpy (path, source_path, len); /* Before $cdir */
608 strcpy (path + len, s->dirname); /* new stuff */
609 strcat (path + len, source_path + len + cdir_len); /* After $cdir */
613 result = openp (path, 0, s->filename, O_RDONLY, 0, &s->fullname);
616 /* Didn't work. Try using just the basename. */
617 p = basename (s->filename);
618 if (p != s->filename)
619 result = openp (path, 0, p, O_RDONLY, 0, &s->fullname);
623 fullname = s->fullname;
624 s->fullname = mstrsave (s->objfile->md, s->fullname);
631 /* Create and initialize the table S->line_charpos that records
632 the positions of the lines in the source file, which is assumed
633 to be open on descriptor DESC.
634 All set S->nlines to the number of such lines. */
637 find_source_lines (s, desc)
642 register char *data, *p, *end;
644 int lines_allocated = 1000;
648 #ifdef LSEEK_NOT_LINEAR
652 line_charpos = (int *) xmmalloc (s -> objfile -> md,
653 lines_allocated * sizeof (int));
654 if (fstat (desc, &st) < 0)
655 perror_with_name (s->filename);
658 exec_mtime = bfd_get_mtime(exec_bfd);
659 if (exec_mtime && exec_mtime < st.st_mtime)
660 printf_filtered ("Source file is more recent than executable.\n");
663 #ifdef LSEEK_NOT_LINEAR
664 /* Have to read it byte by byte to find out where the chars live */
666 line_charpos[0] = tell(desc);
668 while (myread(desc, &c, 1)>0)
672 if (nlines == lines_allocated)
674 lines_allocated *= 2;
676 (int *) xmrealloc (s -> objfile -> md, (char *) line_charpos,
677 sizeof (int) * lines_allocated);
679 line_charpos[nlines++] = tell(desc);
684 /* st_size might be a large type, but we only support source files whose
685 size fits in an int. FIXME. */
686 size = (int) st.st_size;
688 #ifdef BROKEN_LARGE_ALLOCA
689 data = (char *) xmalloc (size);
690 make_cleanup (free, data);
692 data = (char *) alloca (size);
694 if (myread (desc, data, size) < 0)
695 perror_with_name (s->filename);
703 /* A newline at the end does not start a new line. */
706 if (nlines == lines_allocated)
708 lines_allocated *= 2;
710 (int *) xmrealloc (s -> objfile -> md, (char *) line_charpos,
711 sizeof (int) * lines_allocated);
713 line_charpos[nlines++] = p - data;
719 (int *) xmrealloc (s -> objfile -> md, (char *) line_charpos,
720 nlines * sizeof (int));
724 /* Return the character position of a line LINE in symtab S.
725 Return 0 if anything is invalid. */
727 #if 0 /* Currently unused */
730 source_line_charpos (s, line)
735 if (!s->line_charpos || line <= 0) return 0;
736 if (line > s->nlines)
738 return s->line_charpos[line - 1];
741 /* Return the line number of character position POS in symtab S. */
744 source_charpos_line (s, chr)
745 register struct symtab *s;
748 register int line = 0;
751 if (s == 0 || s->line_charpos == 0) return 0;
752 lnp = s->line_charpos;
753 /* Files are usually short, so sequential search is Ok */
754 while (line < s->nlines && *lnp <= chr)
759 if (line >= s->nlines)
767 /* Get full pathname and line number positions for a symtab.
768 Return nonzero if line numbers may have changed.
769 Set *FULLNAME to actual name of the file as found by `openp',
770 or to 0 if the file is not found. */
773 get_filename_and_charpos (s, fullname)
777 register int desc, linenums_changed = 0;
779 desc = open_source_file (s);
787 *fullname = s->fullname;
788 if (s->line_charpos == 0) linenums_changed = 1;
789 if (linenums_changed) find_source_lines (s, desc);
791 return linenums_changed;
794 /* Print text describing the full name of the source file S
795 and the line number LINE and its corresponding character position.
796 The text starts with two Ctrl-z so that the Emacs-GDB interface
799 MID_STATEMENT is nonzero if the PC is not at the beginning of that line.
801 Return 1 if successful, 0 if could not find the file. */
804 identify_source_line (s, line, mid_statement, pc)
810 if (s->line_charpos == 0)
811 get_filename_and_charpos (s, (char **)NULL);
812 if (s->fullname == 0)
814 if (line > s->nlines)
815 /* Don't index off the end of the line_charpos array. */
817 annotate_source (s->fullname, line, s->line_charpos[line - 1],
820 current_source_line = line;
821 first_line_listed = line;
822 last_line_listed = line;
823 current_source_symtab = s;
827 /* Print source lines from the file of symtab S,
828 starting with line number LINE and stopping before line number STOPLINE. */
831 print_source_lines (s, line, stopline, noerror)
838 register FILE *stream;
839 int nlines = stopline - line;
841 /* Regardless of whether we can open the file, set current_source_symtab. */
842 current_source_symtab = s;
843 current_source_line = line;
844 first_line_listed = line;
846 desc = open_source_file (s);
850 char *name = alloca (strlen (s->filename) + 100);
851 sprintf (name, "%s:%d", s->filename, line);
852 print_sys_errmsg (name, errno);
857 if (s->line_charpos == 0)
858 find_source_lines (s, desc);
860 if (line < 1 || line > s->nlines)
863 error ("Line number %d out of range; %s has %d lines.",
864 line, s->filename, s->nlines);
867 if (lseek (desc, s->line_charpos[line - 1], 0) < 0)
870 perror_with_name (s->filename);
873 stream = fdopen (desc, FOPEN_RT);
880 last_line_listed = current_source_line;
881 printf_filtered ("%d\t", current_source_line++);
884 if (c < 040 && c != '\t' && c != '\n' && c != '\r')
885 printf_filtered ("^%c", c + 0100);
887 printf_filtered ("^?");
889 printf_filtered ("%c", c);
890 } while (c != '\n' && (c = fgetc (stream)) >= 0);
900 Print a list of files and line numbers which a user may choose from
901 in order to list a function which was specified ambiguously
902 (as with `list classname::overloadedfuncname', for example).
903 The vector in SALS provides the filenames and line numbers.
906 ambiguous_line_spec (sals)
907 struct symtabs_and_lines *sals;
911 for (i = 0; i < sals->nelts; ++i)
912 printf_filtered("file: \"%s\", line number: %d\n",
913 sals->sals[i].symtab->filename, sals->sals[i].line);
918 list_command (arg, from_tty)
922 struct symtabs_and_lines sals, sals_end;
923 struct symtab_and_line sal, sal_end;
932 if (!have_full_symbols () && !have_partial_symbols())
933 error ("No symbol table is loaded. Use the \"file\" command.");
935 /* Pull in a current source symtab if necessary */
936 if (current_source_symtab == 0 &&
937 (arg == 0 || arg[0] == '+' || arg[0] == '-'))
938 select_source_symtab (0);
940 /* "l" or "l +" lists next ten lines. */
942 if (arg == 0 || STREQ (arg, "+"))
944 if (current_source_symtab == 0)
945 error ("No default source file yet. Do \"help list\".");
946 print_source_lines (current_source_symtab, current_source_line,
947 current_source_line + lines_to_list, 0);
951 /* "l -" lists previous ten lines, the ones before the ten just listed. */
952 if (STREQ (arg, "-"))
954 if (current_source_symtab == 0)
955 error ("No default source file yet. Do \"help list\".");
956 print_source_lines (current_source_symtab,
957 max (first_line_listed - lines_to_list, 1),
958 first_line_listed, 0);
962 /* Now if there is only one argument, decode it in SAL
964 If there are two arguments, decode them in SAL and SAL_END
965 and clear NO_END; however, if one of the arguments is blank,
966 set DUMMY_BEG or DUMMY_END to record that fact. */
973 sals = decode_line_1 (&arg1, 0, 0, 0, 0);
975 if (! sals.nelts) return; /* C++ */
978 ambiguous_line_spec (&sals);
987 /* Record whether the BEG arg is all digits. */
989 for (p = arg; p != arg1 && *p >= '0' && *p <= '9'; p++);
990 linenum_beg = (p == arg1);
992 while (*arg1 == ' ' || *arg1 == '\t')
998 while (*arg1 == ' ' || *arg1 == '\t')
1005 sals_end = decode_line_1 (&arg1, 0, 0, 0, 0);
1007 sals_end = decode_line_1 (&arg1, 0, sal.symtab, sal.line, 0);
1008 if (sals_end.nelts == 0)
1010 if (sals_end.nelts > 1)
1012 ambiguous_line_spec (&sals_end);
1013 free (sals_end.sals);
1016 sal_end = sals_end.sals[0];
1017 free (sals_end.sals);
1022 error ("Junk at end of line specification.");
1024 if (!no_end && !dummy_beg && !dummy_end
1025 && sal.symtab != sal_end.symtab)
1026 error ("Specified start and end are in different files.");
1027 if (dummy_beg && dummy_end)
1028 error ("Two empty args do not say what lines to list.");
1030 /* if line was specified by address,
1031 first print exactly which line, and which file.
1032 In this case, sal.symtab == 0 means address is outside
1033 of all known source files, not that user failed to give a filename. */
1036 if (sal.symtab == 0)
1037 /* FIXME-32x64--assumes sal.pc fits in long. */
1038 error ("No source file for address %s.",
1039 local_hex_string((unsigned long) sal.pc));
1040 sym = find_pc_function (sal.pc);
1043 print_address_numeric (sal.pc, 1, gdb_stdout);
1044 printf_filtered (" is in ");
1045 fputs_filtered (SYMBOL_SOURCE_NAME (sym), gdb_stdout);
1046 printf_filtered (" (%s:%d).\n", sal.symtab->filename, sal.line);
1050 print_address_numeric (sal.pc, 1, gdb_stdout);
1051 printf_filtered (" is at %s:%d.\n",
1052 sal.symtab->filename, sal.line);
1056 /* If line was not specified by just a line number,
1057 and it does not imply a symtab, it must be an undebuggable symbol
1058 which means no source code. */
1060 if (! linenum_beg && sal.symtab == 0)
1061 error ("No line number known for %s.", arg);
1063 /* If this command is repeated with RET,
1064 turn it into the no-arg variant. */
1069 if (dummy_beg && sal_end.symtab == 0)
1070 error ("No default source file yet. Do \"help list\".");
1072 print_source_lines (sal_end.symtab,
1073 max (sal_end.line - (lines_to_list - 1), 1),
1074 sal_end.line + 1, 0);
1075 else if (sal.symtab == 0)
1076 error ("No default source file yet. Do \"help list\".");
1078 print_source_lines (sal.symtab,
1079 max (sal.line - (lines_to_list / 2), 1),
1080 sal.line + (lines_to_list / 2), 0);
1082 print_source_lines (sal.symtab, sal.line,
1084 ? sal.line + lines_to_list
1085 : sal_end.line + 1),
1089 /* Print info on range of pc's in a specified line. */
1092 line_info (arg, from_tty)
1096 struct symtabs_and_lines sals;
1097 struct symtab_and_line sal;
1098 CORE_ADDR start_pc, end_pc;
1103 sal.symtab = current_source_symtab;
1104 sal.line = last_line_listed;
1106 sals.sals = (struct symtab_and_line *)
1107 xmalloc (sizeof (struct symtab_and_line));
1112 sals = decode_line_spec_1 (arg, 0);
1117 /* C++ More than one line may have been specified, as when the user
1118 specifies an overloaded function name. Print info on them all. */
1119 for (i = 0; i < sals.nelts; i++)
1123 if (sal.symtab == 0)
1125 printf_filtered ("No line number information available");
1128 /* This is useful for "info line *0x7f34". If we can't tell the
1129 user about a source line, at least let them have the symbolic
1131 printf_filtered (" for address ");
1133 print_address (sal.pc, gdb_stdout);
1136 printf_filtered (".");
1137 printf_filtered ("\n");
1139 else if (sal.line > 0
1140 && find_line_pc_range (sal, &start_pc, &end_pc))
1142 if (start_pc == end_pc)
1144 printf_filtered ("Line %d of \"%s\"",
1145 sal.line, sal.symtab->filename);
1147 printf_filtered (" is at address ");
1148 print_address (start_pc, gdb_stdout);
1150 printf_filtered (" but contains no code.\n");
1154 printf_filtered ("Line %d of \"%s\"",
1155 sal.line, sal.symtab->filename);
1157 printf_filtered (" starts at address ");
1158 print_address (start_pc, gdb_stdout);
1160 printf_filtered (" and ends at ");
1161 print_address (end_pc, gdb_stdout);
1162 printf_filtered (".\n");
1165 /* x/i should display this line's code. */
1166 set_next_address (start_pc);
1168 /* Repeating "info line" should do the following line. */
1169 last_line_listed = sal.line + 1;
1171 /* If this is the only line, show the source code. If it could
1172 not find the file, don't do anything special. */
1173 if (annotation_level && sals.nelts == 1)
1174 identify_source_line (sal.symtab, sal.line, 0, start_pc);
1177 /* Is there any case in which we get here, and have an address
1178 which the user would want to see? If we have debugging symbols
1179 and no line numbers? */
1180 printf_filtered ("Line number %d is out of range for \"%s\".\n",
1181 sal.line, sal.symtab->filename);
1186 /* Commands to search the source file for a regexp. */
1190 forward_search_command (regex, from_tty)
1196 register FILE *stream;
1197 int line = last_line_listed + 1;
1200 msg = (char *) re_comp (regex);
1204 if (current_source_symtab == 0)
1205 select_source_symtab (0);
1207 /* Search from last_line_listed+1 in current_source_symtab */
1209 desc = open_source_file (current_source_symtab);
1211 perror_with_name (current_source_symtab->filename);
1213 if (current_source_symtab->line_charpos == 0)
1214 find_source_lines (current_source_symtab, desc);
1216 if (line < 1 || line > current_source_symtab->nlines)
1219 error ("Expression not found");
1222 if (lseek (desc, current_source_symtab->line_charpos[line - 1], 0) < 0)
1225 perror_with_name (current_source_symtab->filename);
1228 stream = fdopen (desc, FOPEN_RT);
1231 /* FIXME!!! We walk right off the end of buf if we get a long line!!! */
1232 char buf[4096]; /* Should be reasonable??? */
1233 register char *p = buf;
1240 } while (c != '\n' && (c = getc (stream)) >= 0);
1242 /* we now have a source line in buf, null terminate and match */
1244 if (re_exec (buf) > 0)
1248 print_source_lines (current_source_symtab,
1250 current_source_line = max (line - lines_to_list / 2, 1);
1256 printf_filtered ("Expression not found\n");
1262 reverse_search_command (regex, from_tty)
1268 register FILE *stream;
1269 int line = last_line_listed - 1;
1272 msg = (char *) re_comp (regex);
1276 if (current_source_symtab == 0)
1277 select_source_symtab (0);
1279 /* Search from last_line_listed-1 in current_source_symtab */
1281 desc = open_source_file (current_source_symtab);
1283 perror_with_name (current_source_symtab->filename);
1285 if (current_source_symtab->line_charpos == 0)
1286 find_source_lines (current_source_symtab, desc);
1288 if (line < 1 || line > current_source_symtab->nlines)
1291 error ("Expression not found");
1294 if (lseek (desc, current_source_symtab->line_charpos[line - 1], 0) < 0)
1297 perror_with_name (current_source_symtab->filename);
1300 stream = fdopen (desc, FOPEN_RT);
1304 /* FIXME!!! We walk right off the end of buf if we get a long line!!! */
1305 char buf[4096]; /* Should be reasonable??? */
1306 register char *p = buf;
1313 } while (c != '\n' && (c = getc (stream)) >= 0);
1315 /* We now have a source line in buf; null terminate and match. */
1317 if (re_exec (buf) > 0)
1321 print_source_lines (current_source_symtab,
1323 current_source_line = max (line - lines_to_list / 2, 1);
1327 if (fseek (stream, current_source_symtab->line_charpos[line - 1], 0) < 0)
1330 perror_with_name (current_source_symtab->filename);
1334 printf_filtered ("Expression not found\n");
1340 _initialize_source ()
1342 struct cmd_list_element *c;
1343 current_source_symtab = 0;
1344 init_source_path ();
1346 /* The intention is to use POSIX Basic Regular Expressions.
1347 Always use the GNU regex routine for consistency across all hosts.
1348 Our current GNU regex.c does not have all the POSIX features, so this is
1349 just an approximation. */
1350 re_set_syntax (RE_SYNTAX_GREP);
1352 c = add_cmd ("directory", class_files, directory_command,
1353 "Add directory DIR to beginning of search path for source files.\n\
1354 Forget cached info on source file locations and line positions.\n\
1355 DIR can also be $cwd for the current working directory, or $cdir for the\n\
1356 directory in which the source file was compiled into object code.\n\
1357 With no argument, reset the search path to $cdir:$cwd, the default.",
1359 c->completer = filename_completer;
1361 add_cmd ("directories", no_class, show_directories,
1362 "Current search path for finding source files.\n\
1363 $cwd in the path means the current working directory.\n\
1364 $cdir in the path means the compilation directory of the source file.",
1367 add_info ("source", source_info,
1368 "Information about the current source file.");
1370 add_info ("line", line_info,
1371 "Core addresses of the code for a source line.\n\
1372 Line can be specified as\n\
1373 LINENUM, to list around that line in current file,\n\
1374 FILE:LINENUM, to list around that line in that file,\n\
1375 FUNCTION, to list around beginning of that function,\n\
1376 FILE:FUNCTION, to distinguish among like-named static functions.\n\
1377 Default is to describe the last source line that was listed.\n\n\
1378 This sets the default address for \"x\" to the line's first instruction\n\
1379 so that \"x/i\" suffices to start examining the machine code.\n\
1380 The address is also stored as the value of \"$_\".");
1382 add_com ("forward-search", class_files, forward_search_command,
1383 "Search for regular expression (see regex(3)) from last line listed.");
1384 add_com_alias ("search", "forward-search", class_files, 0);
1386 add_com ("reverse-search", class_files, reverse_search_command,
1387 "Search backward for regular expression (see regex(3)) from last line listed.");
1389 add_com ("list", class_files, list_command,
1390 "List specified function or line.\n\
1391 With no argument, lists ten more lines after or around previous listing.\n\
1392 \"list -\" lists the ten lines before a previous ten-line listing.\n\
1393 One argument specifies a line, and ten lines are listed around that line.\n\
1394 Two arguments with comma between specify starting and ending lines to list.\n\
1395 Lines can be specified in these ways:\n\
1396 LINENUM, to list around that line in current file,\n\
1397 FILE:LINENUM, to list around that line in that file,\n\
1398 FUNCTION, to list around beginning of that function,\n\
1399 FILE:FUNCTION, to distinguish among like-named static functions.\n\
1400 *ADDRESS, to list around the line containing that address.\n\
1401 With two args if one is empty it stands for ten lines away from the other arg.");
1402 add_com_alias ("l", "list", class_files, 1);
1405 (add_set_cmd ("listsize", class_support, var_uinteger,
1406 (char *)&lines_to_list,
1407 "Set number of source lines gdb will list by default.",