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"
29 #include <sys/types.h>
33 #include <sys/param.h>
41 /* Prototypes for local functions. */
44 open_source_file PARAMS ((struct symtab *));
47 get_filename_and_charpos PARAMS ((struct symtab *, char **));
50 reverse_search_command PARAMS ((char *, int));
53 forward_search_command PARAMS ((char *, int));
56 line_info PARAMS ((char *, int));
59 list_command PARAMS ((char *, int));
62 ambiguous_line_spec PARAMS ((struct symtabs_and_lines *));
65 source_info PARAMS ((char *, int));
68 show_directories PARAMS ((char *, int));
71 find_source_lines PARAMS ((struct symtab *, int));
73 /* If we use this declaration, it breaks because of fucking ANSI "const" stuff
74 on some systems. We just have to not declare it at all, have it default
75 to int, and possibly botch on a few systems. Thanks, ANSIholes... */
76 /* extern char *strstr(); */
78 /* Path of directories to search for source files.
79 Same format as the PATH environment variable's value. */
83 /* Symtab of default file for listing lines of. */
85 struct symtab *current_source_symtab;
87 /* Default next line to list. */
89 int current_source_line;
91 /* Default number of lines to print with commands like "list".
92 This is based on guessing how many long (i.e. more than chars_per_line
93 characters) lines there will be. To be completely correct, "list"
94 and friends should be rewritten to count characters and see where
95 things are wrapping, but that would be a fair amount of work. */
97 int lines_to_list = 10;
99 /* Line number of last line printed. Default for various commands.
100 current_source_line is usually, but not always, the same as this. */
102 static int last_line_listed;
104 /* First line number listed by last listing command. */
106 static int first_line_listed;
109 /* Set the source file default for the "list" command to be S.
111 If S is NULL, and we don't have a default, find one. This
112 should only be called when the user actually tries to use the
113 default, since we produce an error if we can't find a reasonable
114 default. Also, since this can cause symbols to be read, doing it
115 before we need to would make things slower than necessary. */
118 select_source_symtab (s)
119 register struct symtab *s;
121 struct symtabs_and_lines sals;
122 struct symtab_and_line sal;
123 struct partial_symtab *ps;
124 struct partial_symtab *cs_pst = 0;
129 current_source_symtab = s;
130 current_source_line = 1;
134 if (current_source_symtab)
137 /* Make the default place to list be the function `main'
139 if (lookup_symbol ("main", 0, VAR_NAMESPACE, 0, NULL))
141 sals = decode_line_spec ("main", 1);
144 current_source_symtab = sal.symtab;
145 current_source_line = max (sal.line - (lines_to_list - 1), 1);
146 if (current_source_symtab)
150 /* All right; find the last file in the symtab list (ignoring .h's). */
152 current_source_line = 1;
154 for (ofp = object_files; ofp != NULL; ofp = ofp -> next)
156 for (s = ofp -> symtabs; s; s = s->next)
158 char *name = s -> filename;
159 int len = strlen (name);
160 if (! (len > 2 && (STREQ (&name[len - 2], ".h"))))
162 current_source_symtab = s;
166 if (current_source_symtab)
169 /* Howabout the partial symbol tables? */
171 for (ofp = object_files; ofp != NULL; ofp = ofp -> next)
173 for (ps = ofp -> psymtabs; ps != NULL; ps = ps -> next)
175 char *name = ps -> filename;
176 int len = strlen (name);
177 if (! (len > 2 && (STREQ (&name[len - 2], ".h"))))
185 if (cs_pst -> readin)
187 fatal ("Internal: select_source_symtab: readin pst found and no symtabs.");
191 current_source_symtab = PSYMTAB_TO_SYMTAB (cs_pst);
195 error ("Can't find a default source file");
199 show_directories (ignore, from_tty)
203 puts_filtered ("Source directories searched: ");
204 puts_filtered (source_path);
205 puts_filtered ("\n");
208 /* Forget what we learned about line positions in source files,
209 and which directories contain them;
210 must check again now since files may be found in
211 a different directory now. */
214 forget_cached_source_info ()
216 register struct symtab *s;
217 register struct objfile *objfile;
219 for (objfile = object_files; objfile != NULL; objfile = objfile -> next)
221 for (s = objfile -> symtabs; s != NULL; s = s -> next)
223 if (s -> line_charpos != NULL)
225 mfree (objfile -> md, s -> line_charpos);
226 s -> line_charpos = NULL;
228 if (s -> fullname != NULL)
230 mfree (objfile -> md, s -> fullname);
231 s -> fullname = NULL;
240 source_path = savestring ("$cdir:$cwd", /* strlen of it */ 10);
241 forget_cached_source_info ();
244 /* Add zero or more directories to the front of the source path. */
247 directory_command (dirname, from_tty)
252 /* FIXME, this goes to "delete dir"... */
255 if (query ("Reinitialize source path to empty? ", ""))
262 mod_path (dirname, &source_path);
264 show_directories ((char *)0, from_tty);
265 forget_cached_source_info ();
268 /* Add zero or more directories to the front of an arbitrary path. */
271 mod_path (dirname, which_path)
275 char *old = *which_path;
281 dirname = strsave (dirname);
282 make_cleanup (free, dirname);
286 char *name = dirname;
291 char *colon = strchr (name, ':');
292 char *space = strchr (name, ' ');
293 char *tab = strchr (name, '\t');
294 if (colon == 0 && space == 0 && tab == 0)
295 p = dirname = name + strlen (name);
299 if (colon != 0 && (p == 0 || colon < p))
301 if (space != 0 && (p == 0 || space < p))
303 if (tab != 0 && (p == 0 || tab < p))
306 while (*dirname == ':' || *dirname == ' ' || *dirname == '\t')
312 /* Sigh. "foo/" => "foo" */
320 /* "." => getwd (). */
321 name = current_directory;
324 else if (p[-2] == '/')
334 /* "...foo/." => "...foo". */
345 name = tilde_expand (name);
346 else if (name[0] != '/' && name[0] != '$')
347 name = concat (current_directory, "/", name, NULL);
349 name = savestring (name, p - name);
350 make_cleanup (free, name);
352 /* Unless it's a variable, check existence. */
353 if (name[0] != '$') {
354 /* These are warnings, not errors, since we don't want a
355 non-existent directory in a .gdbinit file to stop processing
356 of the .gdbinit file.
358 Whether they get added to the path is more debatable. Current
359 answer is yes, in case the user wants to go make the directory
360 or whatever. If the directory continues to not exist/not be
361 a directory/etc, then having them in the path should be
363 if (stat (name, &st) < 0)
365 int save_errno = errno;
366 fprintf_unfiltered (gdb_stderr, "Warning: ");
367 print_sys_errmsg (name, save_errno);
369 else if ((st.st_mode & S_IFMT) != S_IFDIR)
370 warning ("%s is not a directory.", name);
375 register unsigned int len = strlen (name);
380 if (!strncmp (p, name, len)
381 && (p[len] == '\0' || p[len] == ':'))
383 /* Found it in the search path, remove old copy */
385 p--; /* Back over leading colon */
386 if (prefix > p - *which_path)
387 goto skip_dup; /* Same dir twice in one cmd */
388 strcpy (p, &p[len+1]); /* Copy from next \0 or : */
398 /* 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. */
405 temp = concat (old, ":", name, NULL);
407 *which_path = concat (temp, "", &old[prefix], NULL);
408 prefix = strlen (temp);
413 *which_path = concat (name, (old[0]? ":" : old), old, NULL);
414 prefix = strlen (name);
421 } while (*dirname != '\0');
426 source_info (ignore, from_tty)
430 register struct symtab *s = current_source_symtab;
434 printf_filtered("No current source file.\n");
437 printf_filtered ("Current source file is %s\n", s->filename);
439 printf_filtered ("Compilation directory is %s\n", s->dirname);
441 printf_filtered ("Located in %s\n", s->fullname);
443 printf_filtered ("Contains %d line%s.\n", s->nlines,
444 s->nlines == 1 ? "" : "s");
446 printf_filtered("Source language is %s.\n", language_str (s->language));
451 /* Open a file named STRING, searching path PATH (dir names sep by colons)
452 using mode MODE and protection bits PROT in the calls to open.
454 If TRY_CWD_FIRST, try to open ./STRING before searching PATH.
455 (ie pretend the first element of PATH is "."). This also indicates
456 that a slash in STRING disables searching of the path (this is
457 so that "exec-file ./foo" or "symbol-file ./foo" insures that you
458 get that particular version of foo or an error message).
460 If FILENAMED_OPENED is non-null, set it to a newly allocated string naming
461 the actual file opened (this string will always start with a "/". We
462 have to take special pains to avoid doubling the "/" between the directory
463 and the file, sigh! Emacs gets confuzzed by this when we print the
466 If a file is found, return the descriptor.
467 Otherwise, return -1, with errno set for the last name we tried to open. */
469 /* >>>> This should only allow files of certain types,
470 >>>> eg executable, non-directory */
472 openp (path, try_cwd_first, string, mode, prot, filename_opened)
478 char **filename_opened;
481 register char *filename;
482 register char *p, *p1;
489 if (try_cwd_first || string[0] == '/')
492 fd = open (filename, mode, prot);
493 if (fd >= 0 || string[0] == '/' || strchr (string, '/'))
498 while (string[0] == '.' && string[1] == '/')
501 alloclen = strlen (path) + strlen (string) + 2;
502 filename = (char *) alloca (alloclen);
504 for (p = path; p; p = p1 ? p1 + 1 : 0)
506 p1 = (char *) strchr (p, ':');
512 if (len == 4 && p[0] == '$' && p[1] == 'c'
513 && p[2] == 'w' && p[3] == 'd') {
514 /* Name is $cwd -- insert current directory name instead. */
517 /* First, realloc the filename buffer if too short. */
518 len = strlen (current_directory);
519 newlen = len + strlen (string) + 2;
520 if (newlen > alloclen) {
522 filename = (char *) alloca (alloclen);
524 strcpy (filename, current_directory);
526 /* Normal file name in path -- just use it. */
527 strncpy (filename, p, len);
531 /* Remove trailing slashes */
532 while (len > 0 && filename[len-1] == '/')
535 strcat (filename+len, "/");
536 strcat (filename, string);
538 fd = open (filename, mode, prot);
545 *filename_opened = (char *) 0;
546 else if (filename[0] == '/')
547 *filename_opened = savestring (filename, strlen (filename));
550 /* Beware the // my son, the Emacs barfs, the botch that catch... */
552 *filename_opened = concat (current_directory,
553 '/' == current_directory[strlen(current_directory)-1]? "": "/",
560 /* Open a source file given a symtab S. Returns a file descriptor
561 or negative number for error. */
567 char *path = source_path;
572 /* Quick way out if we already know its full name */
575 result = open (s->fullname, O_RDONLY);
578 /* Didn't work -- free old one, try again. */
579 mfree (s->objfile->md, s->fullname);
583 if (s->dirname != NULL)
585 /* Replace a path entry of $cdir with the compilation directory name */
587 /* We cast strstr's result in case an ANSIhole has made it const,
588 which produces a "required warning" when assigned to a nonconst. */
589 p = (char *)strstr (source_path, "$cdir");
590 if (p && (p == path || p[-1] == ':')
591 && (p[cdir_len] == ':' || p[cdir_len] == '\0')) {
595 alloca (strlen (source_path) + 1 + strlen (s->dirname) + 1);
596 len = p - source_path;
597 strncpy (path, source_path, len); /* Before $cdir */
598 strcpy (path + len, s->dirname); /* new stuff */
599 strcat (path + len, source_path + len + cdir_len); /* After $cdir */
603 result = openp (path, 0, s->filename, O_RDONLY, 0, &s->fullname);
606 /* Didn't work. Try using just the basename. */
607 p = basename (s->filename);
608 if (p != s->filename)
609 result = openp(path, 0, p, O_RDONLY,0, &s->fullname);
613 fullname = s -> fullname;
614 s -> fullname = mstrsave (s -> objfile -> md, s -> fullname);
621 /* Create and initialize the table S->line_charpos that records
622 the positions of the lines in the source file, which is assumed
623 to be open on descriptor DESC.
624 All set S->nlines to the number of such lines. */
627 find_source_lines (s, desc)
632 register char *data, *p, *end;
634 int lines_allocated = 1000;
638 #ifdef LSEEK_NOT_LINEAR
642 line_charpos = (int *) xmmalloc (s -> objfile -> md,
643 lines_allocated * sizeof (int));
644 if (fstat (desc, &st) < 0)
645 perror_with_name (s->filename);
648 exec_mtime = bfd_get_mtime(exec_bfd);
649 if (exec_mtime && exec_mtime < st.st_mtime)
650 printf_filtered ("Source file is more recent than executable.\n");
653 #ifdef LSEEK_NOT_LINEAR
654 /* Have to read it byte by byte to find out where the chars live */
656 line_charpos[0] = tell(desc);
658 while (myread(desc, &c, 1)>0)
662 if (nlines == lines_allocated)
664 lines_allocated *= 2;
666 (int *) xmrealloc (s -> objfile -> md, (char *) line_charpos,
667 sizeof (int) * lines_allocated);
669 line_charpos[nlines++] = tell(desc);
674 /* st_size might be a large type, but we only support source files whose
675 size fits in an int. FIXME. */
676 size = (int) st.st_size;
678 #ifdef BROKEN_LARGE_ALLOCA
679 data = (char *) xmalloc (size);
680 make_cleanup (free, data);
682 data = (char *) alloca (size);
684 if (myread (desc, data, size) < 0)
685 perror_with_name (s->filename);
693 /* A newline at the end does not start a new line. */
696 if (nlines == lines_allocated)
698 lines_allocated *= 2;
700 (int *) xmrealloc (s -> objfile -> md, (char *) line_charpos,
701 sizeof (int) * lines_allocated);
703 line_charpos[nlines++] = p - data;
709 (int *) xmrealloc (s -> objfile -> md, (char *) line_charpos,
710 nlines * sizeof (int));
714 /* Return the character position of a line LINE in symtab S.
715 Return 0 if anything is invalid. */
717 #if 0 /* Currently unused */
720 source_line_charpos (s, line)
725 if (!s->line_charpos || line <= 0) return 0;
726 if (line > s->nlines)
728 return s->line_charpos[line - 1];
731 /* Return the line number of character position POS in symtab S. */
734 source_charpos_line (s, chr)
735 register struct symtab *s;
738 register int line = 0;
741 if (s == 0 || s->line_charpos == 0) return 0;
742 lnp = s->line_charpos;
743 /* Files are usually short, so sequential search is Ok */
744 while (line < s->nlines && *lnp <= chr)
749 if (line >= s->nlines)
757 /* Get full pathname and line number positions for a symtab.
758 Return nonzero if line numbers may have changed.
759 Set *FULLNAME to actual name of the file as found by `openp',
760 or to 0 if the file is not found. */
763 get_filename_and_charpos (s, fullname)
767 register int desc, linenums_changed = 0;
769 desc = open_source_file (s);
777 *fullname = s->fullname;
778 if (s->line_charpos == 0) linenums_changed = 1;
779 if (linenums_changed) find_source_lines (s, desc);
781 return linenums_changed;
784 /* Print text describing the full name of the source file S
785 and the line number LINE and its corresponding character position.
786 The text starts with two Ctrl-z so that the Emacs-GDB interface
789 MID_STATEMENT is nonzero if the PC is not at the beginning of that line.
791 Return 1 if successful, 0 if could not find the file. */
794 identify_source_line (s, line, mid_statement, pc)
800 if (s->line_charpos == 0)
801 get_filename_and_charpos (s, (char **)NULL);
802 if (s->fullname == 0)
804 if (line > s->nlines)
805 /* Don't index off the end of the line_charpos array. */
807 printf_unfiltered ("\032\032%s:%d:%d:%s:0x%lx\n", s->fullname,
808 line, s->line_charpos[line - 1],
809 mid_statement ? "middle" : "beg",
811 current_source_line = line;
812 first_line_listed = line;
813 last_line_listed = line;
814 current_source_symtab = s;
818 /* Print source lines from the file of symtab S,
819 starting with line number LINE and stopping before line number STOPLINE. */
822 print_source_lines (s, line, stopline, noerror)
829 register FILE *stream;
830 int nlines = stopline - line;
832 /* Regardless of whether we can open the file, set current_source_symtab. */
833 current_source_symtab = s;
834 current_source_line = line;
835 first_line_listed = line;
837 desc = open_source_file (s);
841 char *name = alloca (strlen (s->filename) + 100);
842 sprintf (name, "%s:%d", s->filename, line);
843 print_sys_errmsg (name, errno);
848 if (s->line_charpos == 0)
849 find_source_lines (s, desc);
851 if (line < 1 || line > s->nlines)
854 error ("Line number %d out of range; %s has %d lines.",
855 line, s->filename, s->nlines);
858 if (lseek (desc, s->line_charpos[line - 1], 0) < 0)
861 perror_with_name (s->filename);
864 stream = fdopen (desc, FOPEN_RT);
871 last_line_listed = current_source_line;
872 printf_filtered ("%d\t", current_source_line++);
875 if (c < 040 && c != '\t' && c != '\n' && c != '\r')
876 printf_filtered ("^%c", c + 0100);
878 printf_filtered ("^?");
880 printf_filtered ("%c", c);
881 } while (c != '\n' && (c = fgetc (stream)) >= 0);
891 Print a list of files and line numbers which a user may choose from
892 in order to list a function which was specified ambiguously
893 (as with `list classname::overloadedfuncname', for example).
894 The vector in SALS provides the filenames and line numbers.
897 ambiguous_line_spec (sals)
898 struct symtabs_and_lines *sals;
902 for (i = 0; i < sals->nelts; ++i)
903 printf_filtered("file: \"%s\", line number: %d\n",
904 sals->sals[i].symtab->filename, sals->sals[i].line);
909 list_command (arg, from_tty)
913 struct symtabs_and_lines sals, sals_end;
914 struct symtab_and_line sal, sal_end;
923 if (!have_full_symbols () && !have_partial_symbols())
924 error ("No symbol table is loaded. Use the \"file\" command.");
926 /* Pull in a current source symtab if necessary */
927 if (current_source_symtab == 0 &&
928 (arg == 0 || arg[0] == '+' || arg[0] == '-'))
929 select_source_symtab (0);
931 /* "l" or "l +" lists next ten lines. */
933 if (arg == 0 || STREQ (arg, "+"))
935 if (current_source_symtab == 0)
936 error ("No default source file yet. Do \"help list\".");
937 print_source_lines (current_source_symtab, current_source_line,
938 current_source_line + lines_to_list, 0);
942 /* "l -" lists previous ten lines, the ones before the ten just listed. */
943 if (STREQ (arg, "-"))
945 if (current_source_symtab == 0)
946 error ("No default source file yet. Do \"help list\".");
947 print_source_lines (current_source_symtab,
948 max (first_line_listed - lines_to_list, 1),
949 first_line_listed, 0);
953 /* Now if there is only one argument, decode it in SAL
955 If there are two arguments, decode them in SAL and SAL_END
956 and clear NO_END; however, if one of the arguments is blank,
957 set DUMMY_BEG or DUMMY_END to record that fact. */
964 sals = decode_line_1 (&arg1, 0, 0, 0, 0);
966 if (! sals.nelts) return; /* C++ */
969 ambiguous_line_spec (&sals);
978 /* Record whether the BEG arg is all digits. */
980 for (p = arg; p != arg1 && *p >= '0' && *p <= '9'; p++);
981 linenum_beg = (p == arg1);
983 while (*arg1 == ' ' || *arg1 == '\t')
989 while (*arg1 == ' ' || *arg1 == '\t')
996 sals_end = decode_line_1 (&arg1, 0, 0, 0, 0);
998 sals_end = decode_line_1 (&arg1, 0, sal.symtab, sal.line, 0);
999 if (sals_end.nelts == 0)
1001 if (sals_end.nelts > 1)
1003 ambiguous_line_spec (&sals_end);
1004 free (sals_end.sals);
1007 sal_end = sals_end.sals[0];
1008 free (sals_end.sals);
1013 error ("Junk at end of line specification.");
1015 if (!no_end && !dummy_beg && !dummy_end
1016 && sal.symtab != sal_end.symtab)
1017 error ("Specified start and end are in different files.");
1018 if (dummy_beg && dummy_end)
1019 error ("Two empty args do not say what lines to list.");
1021 /* if line was specified by address,
1022 first print exactly which line, and which file.
1023 In this case, sal.symtab == 0 means address is outside
1024 of all known source files, not that user failed to give a filename. */
1027 if (sal.symtab == 0)
1028 error ("No source file for address %s.",
1029 local_hex_string((unsigned long) sal.pc));
1030 sym = find_pc_function (sal.pc);
1033 printf_filtered ("%s is in ",
1034 local_hex_string((unsigned long) sal.pc));
1035 fputs_filtered (SYMBOL_SOURCE_NAME (sym), gdb_stdout);
1036 printf_filtered (" (%s:%d).\n", sal.symtab->filename, sal.line);
1039 printf_filtered ("%s is at %s:%d.\n",
1040 local_hex_string((unsigned long) sal.pc),
1041 sal.symtab->filename, sal.line);
1044 /* If line was not specified by just a line number,
1045 and it does not imply a symtab, it must be an undebuggable symbol
1046 which means no source code. */
1048 if (! linenum_beg && sal.symtab == 0)
1049 error ("No line number known for %s.", arg);
1051 /* If this command is repeated with RET,
1052 turn it into the no-arg variant. */
1057 if (dummy_beg && sal_end.symtab == 0)
1058 error ("No default source file yet. Do \"help list\".");
1060 print_source_lines (sal_end.symtab,
1061 max (sal_end.line - (lines_to_list - 1), 1),
1062 sal_end.line + 1, 0);
1063 else if (sal.symtab == 0)
1064 error ("No default source file yet. Do \"help list\".");
1066 print_source_lines (sal.symtab,
1067 max (sal.line - (lines_to_list / 2), 1),
1068 sal.line + (lines_to_list / 2), 0);
1070 print_source_lines (sal.symtab, sal.line,
1072 ? sal.line + lines_to_list
1073 : sal_end.line + 1),
1077 /* Print info on range of pc's in a specified line. */
1080 line_info (arg, from_tty)
1084 struct symtabs_and_lines sals;
1085 struct symtab_and_line sal;
1086 CORE_ADDR start_pc, end_pc;
1091 sal.symtab = current_source_symtab;
1092 sal.line = last_line_listed;
1094 sals.sals = (struct symtab_and_line *)
1095 xmalloc (sizeof (struct symtab_and_line));
1100 sals = decode_line_spec_1 (arg, 0);
1105 /* C++ More than one line may have been specified, as when the user
1106 specifies an overloaded function name. Print info on them all. */
1107 for (i = 0; i < sals.nelts; i++)
1111 if (sal.symtab == 0)
1113 printf_filtered ("No line number information available");
1116 /* This is useful for "info line *0x7f34". If we can't tell the
1117 user about a source line, at least let them have the symbolic
1119 printf_filtered (" for address ");
1121 print_address (sal.pc, gdb_stdout);
1124 printf_filtered (".");
1125 printf_filtered ("\n");
1127 else if (sal.line > 0
1128 && find_line_pc_range (sal.symtab, sal.line, &start_pc, &end_pc))
1130 if (start_pc == end_pc)
1132 printf_filtered ("Line %d of \"%s\"",
1133 sal.line, sal.symtab->filename);
1135 printf_filtered (" is at address ");
1136 print_address (start_pc, gdb_stdout);
1138 printf_filtered (" but contains no code.\n");
1142 printf_filtered ("Line %d of \"%s\"",
1143 sal.line, sal.symtab->filename);
1145 printf_filtered (" starts at address ");
1146 print_address (start_pc, gdb_stdout);
1148 printf_filtered (" and ends at ");
1149 print_address (end_pc, gdb_stdout);
1150 printf_filtered (".\n");
1153 /* x/i should display this line's code. */
1154 set_next_address (start_pc);
1156 /* Repeating "info line" should do the following line. */
1157 last_line_listed = sal.line + 1;
1159 /* If this is the only line, show the source code. If it could
1160 not find the file, don't do anything special. */
1161 if (frame_file_full_name && sals.nelts == 1)
1162 identify_source_line (sal.symtab, sal.line, 0, start_pc);
1165 /* Is there any case in which we get here, and have an address
1166 which the user would want to see? If we have debugging symbols
1167 and no line numbers? */
1168 printf_filtered ("Line number %d is out of range for \"%s\".\n",
1169 sal.line, sal.symtab->filename);
1174 /* Commands to search the source file for a regexp. */
1178 forward_search_command (regex, from_tty)
1184 register FILE *stream;
1185 int line = last_line_listed + 1;
1188 msg = (char *) re_comp (regex);
1192 if (current_source_symtab == 0)
1193 select_source_symtab (0);
1195 /* Search from last_line_listed+1 in current_source_symtab */
1197 desc = open_source_file (current_source_symtab);
1199 perror_with_name (current_source_symtab->filename);
1201 if (current_source_symtab->line_charpos == 0)
1202 find_source_lines (current_source_symtab, desc);
1204 if (line < 1 || line > current_source_symtab->nlines)
1207 error ("Expression not found");
1210 if (lseek (desc, current_source_symtab->line_charpos[line - 1], 0) < 0)
1213 perror_with_name (current_source_symtab->filename);
1216 stream = fdopen (desc, FOPEN_RT);
1219 /* FIXME!!! We walk right off the end of buf if we get a long line!!! */
1220 char buf[4096]; /* Should be reasonable??? */
1221 register char *p = buf;
1228 } while (c != '\n' && (c = getc (stream)) >= 0);
1230 /* we now have a source line in buf, null terminate and match */
1232 if (re_exec (buf) > 0)
1236 print_source_lines (current_source_symtab,
1238 current_source_line = max (line - lines_to_list / 2, 1);
1244 printf_filtered ("Expression not found\n");
1250 reverse_search_command (regex, from_tty)
1256 register FILE *stream;
1257 int line = last_line_listed - 1;
1260 msg = (char *) re_comp (regex);
1264 if (current_source_symtab == 0)
1265 select_source_symtab (0);
1267 /* Search from last_line_listed-1 in current_source_symtab */
1269 desc = open_source_file (current_source_symtab);
1271 perror_with_name (current_source_symtab->filename);
1273 if (current_source_symtab->line_charpos == 0)
1274 find_source_lines (current_source_symtab, desc);
1276 if (line < 1 || line > current_source_symtab->nlines)
1279 error ("Expression not found");
1282 if (lseek (desc, current_source_symtab->line_charpos[line - 1], 0) < 0)
1285 perror_with_name (current_source_symtab->filename);
1288 stream = fdopen (desc, FOPEN_RT);
1292 /* FIXME!!! We walk right off the end of buf if we get a long line!!! */
1293 char buf[4096]; /* Should be reasonable??? */
1294 register char *p = buf;
1301 } while (c != '\n' && (c = getc (stream)) >= 0);
1303 /* We now have a source line in buf; null terminate and match. */
1305 if (re_exec (buf) > 0)
1309 print_source_lines (current_source_symtab,
1311 current_source_line = max (line - lines_to_list / 2, 1);
1315 if (fseek (stream, current_source_symtab->line_charpos[line - 1], 0) < 0)
1318 perror_with_name (current_source_symtab->filename);
1322 printf_filtered ("Expression not found\n");
1328 _initialize_source ()
1330 struct cmd_list_element *c;
1331 current_source_symtab = 0;
1332 init_source_path ();
1334 /* The intention is to use POSIX Basic Regular Expressions.
1335 Always use the GNU regex routine for consistency across all hosts.
1336 Our current GNU regex.c does not have all the POSIX features, so this is
1337 just an approximation. */
1338 re_set_syntax (RE_SYNTAX_GREP);
1340 c = add_cmd ("directory", class_files, directory_command,
1341 "Add directory DIR to beginning of search path for source files.\n\
1342 Forget cached info on source file locations and line positions.\n\
1343 DIR can also be $cwd for the current working directory, or $cdir for the\n\
1344 directory in which the source file was compiled into object code.\n\
1345 With no argument, reset the search path to $cdir:$cwd, the default.",
1347 c->completer = filename_completer;
1349 add_cmd ("directories", no_class, show_directories,
1350 "Current search path for finding source files.\n\
1351 $cwd in the path means the current working directory.\n\
1352 $cdir in the path means the compilation directory of the source file.",
1355 add_info ("source", source_info,
1356 "Information about the current source file.");
1358 add_info ("line", line_info,
1359 "Core addresses of the code for a source line.\n\
1360 Line can be specified as\n\
1361 LINENUM, to list around that line in current file,\n\
1362 FILE:LINENUM, to list around that line in that file,\n\
1363 FUNCTION, to list around beginning of that function,\n\
1364 FILE:FUNCTION, to distinguish among like-named static functions.\n\
1365 Default is to describe the last source line that was listed.\n\n\
1366 This sets the default address for \"x\" to the line's first instruction\n\
1367 so that \"x/i\" suffices to start examining the machine code.\n\
1368 The address is also stored as the value of \"$_\".");
1370 add_com ("forward-search", class_files, forward_search_command,
1371 "Search for regular expression (see regex(3)) from last line listed.");
1372 add_com_alias ("search", "forward-search", class_files, 0);
1374 add_com ("reverse-search", class_files, reverse_search_command,
1375 "Search backward for regular expression (see regex(3)) from last line listed.");
1377 add_com ("list", class_files, list_command,
1378 "List specified function or line.\n\
1379 With no argument, lists ten more lines after or around previous listing.\n\
1380 \"list -\" lists the ten lines before a previous ten-line listing.\n\
1381 One argument specifies a line, and ten lines are listed around that line.\n\
1382 Two arguments with comma between specify starting and ending lines to list.\n\
1383 Lines can be specified in these ways:\n\
1384 LINENUM, to list around that line in current file,\n\
1385 FILE:LINENUM, to list around that line in that file,\n\
1386 FUNCTION, to list around beginning of that function,\n\
1387 FILE:FUNCTION, to distinguish among like-named static functions.\n\
1388 *ADDRESS, to list around the line containing that address.\n\
1389 With two args if one is empty it stands for ten lines away from the other arg.");
1390 add_com_alias ("l", "list", class_files, 1);
1393 (add_set_cmd ("listsize", class_support, var_uinteger,
1394 (char *)&lines_to_list,
1395 "Set number of source lines gdb will list by default.",