1 /* List lines of source files for GDB, the GNU debugger.
2 Copyright 1986, 1987, 1988, 1989, 1991, 1992, 1993, 1994, 1995
3 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
23 #include "expression.h"
29 #include <sys/types.h>
31 #include <sys/param.h>
41 #ifndef DIRNAME_SEPARATOR
42 #define DIRNAME_SEPARATOR ':'
45 /* Prototypes for local functions. */
47 static int open_source_file PARAMS ((struct symtab *));
49 static int get_filename_and_charpos PARAMS ((struct symtab *, char **));
51 static void reverse_search_command PARAMS ((char *, int));
53 static void forward_search_command PARAMS ((char *, int));
55 static void line_info PARAMS ((char *, int));
57 static void list_command PARAMS ((char *, int));
59 static void ambiguous_line_spec PARAMS ((struct symtabs_and_lines *));
61 static void source_info PARAMS ((char *, int));
63 static void show_directories PARAMS ((char *, int));
65 static void find_source_lines PARAMS ((struct symtab *, int));
67 /* If we use this declaration, it breaks because of fucking ANSI "const" stuff
68 on some systems. We just have to not declare it at all, have it default
69 to int, and possibly botch on a few systems. Thanks, ANSIholes... */
70 /* extern char *strstr(); */
72 /* Path of directories to search for source files.
73 Same format as the PATH environment variable's value. */
77 /* Symtab of default file for listing lines of. */
79 struct symtab *current_source_symtab;
81 /* Default next line to list. */
83 int current_source_line;
85 /* Default number of lines to print with commands like "list".
86 This is based on guessing how many long (i.e. more than chars_per_line
87 characters) lines there will be. To be completely correct, "list"
88 and friends should be rewritten to count characters and see where
89 things are wrapping, but that would be a fair amount of work. */
91 int lines_to_list = 10;
93 /* Line number of last line printed. Default for various commands.
94 current_source_line is usually, but not always, the same as this. */
96 static int last_line_listed;
98 /* First line number listed by last listing command. */
100 static int first_line_listed;
103 /* Set the source file default for the "list" command to be S.
105 If S is NULL, and we don't have a default, find one. This
106 should only be called when the user actually tries to use the
107 default, since we produce an error if we can't find a reasonable
108 default. Also, since this can cause symbols to be read, doing it
109 before we need to would make things slower than necessary. */
112 select_source_symtab (s)
113 register struct symtab *s;
115 struct symtabs_and_lines sals;
116 struct symtab_and_line sal;
117 struct partial_symtab *ps;
118 struct partial_symtab *cs_pst = 0;
123 current_source_symtab = s;
124 current_source_line = 1;
128 if (current_source_symtab)
131 /* Make the default place to list be the function `main'
133 if (lookup_symbol ("main", 0, VAR_NAMESPACE, 0, NULL))
135 sals = decode_line_spec ("main", 1);
138 current_source_symtab = sal.symtab;
139 current_source_line = max (sal.line - (lines_to_list - 1), 1);
140 if (current_source_symtab)
144 /* All right; find the last file in the symtab list (ignoring .h's). */
146 current_source_line = 1;
148 for (ofp = object_files; ofp != NULL; ofp = ofp -> next)
150 for (s = ofp -> symtabs; s; s = s->next)
152 char *name = s -> filename;
153 int len = strlen (name);
154 if (! (len > 2 && (STREQ (&name[len - 2], ".h"))))
156 current_source_symtab = s;
160 if (current_source_symtab)
163 /* Howabout the partial symbol tables? */
165 for (ofp = object_files; ofp != NULL; ofp = ofp -> next)
167 for (ps = ofp -> psymtabs; ps != NULL; ps = ps -> next)
169 char *name = ps -> filename;
170 int len = strlen (name);
171 if (! (len > 2 && (STREQ (&name[len - 2], ".h"))))
179 if (cs_pst -> readin)
181 fatal ("Internal: select_source_symtab: readin pst found and no symtabs.");
185 current_source_symtab = PSYMTAB_TO_SYMTAB (cs_pst);
189 error ("Can't find a default source file");
193 show_directories (ignore, from_tty)
197 puts_filtered ("Source directories searched: ");
198 puts_filtered (source_path);
199 puts_filtered ("\n");
202 /* Forget what we learned about line positions in source files, and
203 which directories contain them; must check again now since files
204 may be found in a different directory now. */
207 forget_cached_source_info ()
209 register struct symtab *s;
210 register struct objfile *objfile;
212 for (objfile = object_files; objfile != NULL; objfile = objfile -> next)
214 for (s = objfile -> symtabs; s != NULL; s = s -> next)
216 if (s -> line_charpos != NULL)
218 mfree (objfile -> md, s -> line_charpos);
219 s -> line_charpos = NULL;
221 if (s -> fullname != NULL)
223 mfree (objfile -> md, s -> fullname);
224 s -> fullname = NULL;
235 sprintf (buf, "$cdir%c$cwd", DIRNAME_SEPARATOR);
236 source_path = strsave (buf);
237 forget_cached_source_info ();
240 /* Add zero or more directories to the front of the source path. */
243 directory_command (dirname, from_tty)
248 /* FIXME, this goes to "delete dir"... */
251 if (query ("Reinitialize source path to empty? ", ""))
258 mod_path (dirname, &source_path);
260 show_directories ((char *)0, from_tty);
261 forget_cached_source_info ();
264 /* Add zero or more directories to the front of an arbitrary path. */
267 mod_path (dirname, which_path)
271 char *old = *which_path;
277 dirname = strsave (dirname);
278 make_cleanup (free, dirname);
282 char *name = dirname;
287 char *separator = strchr (name, DIRNAME_SEPARATOR);
288 char *space = strchr (name, ' ');
289 char *tab = strchr (name, '\t');
291 if (separator == 0 && space == 0 && tab == 0)
292 p = dirname = name + strlen (name);
296 if (separator != 0 && (p == 0 || separator < p))
298 if (space != 0 && (p == 0 || space < p))
300 if (tab != 0 && (p == 0 || tab < p))
303 while (*dirname == DIRNAME_SEPARATOR
311 /* Sigh. "foo/" => "foo" */
319 /* "." => getwd (). */
320 name = current_directory;
323 else if (p[-2] == '/')
333 /* "...foo/." => "...foo". */
344 name = tilde_expand (name);
345 else if (name[0] != '/' && name[0] != '$')
346 name = concat (current_directory, "/", name, NULL);
348 name = savestring (name, p - name);
349 make_cleanup (free, name);
351 /* Unless it's a variable, check existence. */
352 if (name[0] != '$') {
353 /* These are warnings, not errors, since we don't want a
354 non-existent directory in a .gdbinit file to stop processing
355 of the .gdbinit file.
357 Whether they get added to the path is more debatable. Current
358 answer is yes, in case the user wants to go make the directory
359 or whatever. If the directory continues to not exist/not be
360 a directory/etc, then having them in the path should be
362 if (stat (name, &st) < 0)
364 int save_errno = errno;
365 fprintf_unfiltered (gdb_stderr, "Warning: ");
366 print_sys_errmsg (name, save_errno);
368 else if ((st.st_mode & S_IFMT) != S_IFDIR)
369 warning ("%s is not a directory.", name);
374 register unsigned int len = strlen (name);
379 if (!strncmp (p, name, len)
380 && (p[len] == '\0' || p[len] == DIRNAME_SEPARATOR))
382 /* Found it in the search path, remove old copy */
384 p--; /* Back over leading separator */
385 if (prefix > p - *which_path)
386 goto skip_dup; /* Same dir twice in one cmd */
387 strcpy (p, &p[len+1]); /* Copy from next \0 or : */
389 p = strchr (p, DIRNAME_SEPARATOR);
399 tinybuf[0] = DIRNAME_SEPARATOR;
402 /* 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. */
409 temp = concat (old, tinybuf, name, NULL);
411 *which_path = concat (temp, "", &old[prefix], NULL);
412 prefix = strlen (temp);
417 *which_path = concat (name, (old[0] ? tinybuf : old), old, NULL);
418 prefix = strlen (name);
425 } while (*dirname != '\0');
430 source_info (ignore, from_tty)
434 register struct symtab *s = current_source_symtab;
438 printf_filtered("No current source file.\n");
441 printf_filtered ("Current source file is %s\n", s->filename);
443 printf_filtered ("Compilation directory is %s\n", s->dirname);
445 printf_filtered ("Located in %s\n", s->fullname);
447 printf_filtered ("Contains %d line%s.\n", s->nlines,
448 s->nlines == 1 ? "" : "s");
450 printf_filtered("Source language is %s.\n", language_str (s->language));
455 /* Open a file named STRING, searching path PATH (dir names sep by some char)
456 using mode MODE and protection bits PROT in the calls to open.
458 If TRY_CWD_FIRST, try to open ./STRING before searching PATH.
459 (ie pretend the first element of PATH is "."). This also indicates
460 that a slash in STRING disables searching of the path (this is
461 so that "exec-file ./foo" or "symbol-file ./foo" insures that you
462 get that particular version of foo or an error message).
464 If FILENAMED_OPENED is non-null, set it to a newly allocated string naming
465 the actual file opened (this string will always start with a "/". We
466 have to take special pains to avoid doubling the "/" between the directory
467 and the file, sigh! Emacs gets confuzzed by this when we print the
470 If a file is found, return the descriptor.
471 Otherwise, return -1, with errno set for the last name we tried to open. */
473 /* >>>> This should only allow files of certain types,
474 >>>> eg executable, non-directory */
476 openp (path, try_cwd_first, string, mode, prot, filename_opened)
482 char **filename_opened;
485 register char *filename;
486 register char *p, *p1;
493 if (try_cwd_first || string[0] == '/')
496 fd = open (filename, mode, prot);
497 if (fd >= 0 || string[0] == '/' || strchr (string, '/'))
502 while (string[0] == '.' && string[1] == '/')
505 alloclen = strlen (path) + strlen (string) + 2;
506 filename = (char *) alloca (alloclen);
508 for (p = path; p; p = p1 ? p1 + 1 : 0)
510 p1 = (char *) strchr (p, DIRNAME_SEPARATOR);
516 if (len == 4 && p[0] == '$' && p[1] == 'c'
517 && p[2] == 'w' && p[3] == 'd') {
518 /* Name is $cwd -- insert current directory name instead. */
521 /* First, realloc the filename buffer if too short. */
522 len = strlen (current_directory);
523 newlen = len + strlen (string) + 2;
524 if (newlen > alloclen) {
526 filename = (char *) alloca (alloclen);
528 strcpy (filename, current_directory);
530 /* Normal file name in path -- just use it. */
531 strncpy (filename, p, len);
535 /* Remove trailing slashes */
536 while (len > 0 && filename[len-1] == '/')
539 strcat (filename+len, "/");
540 strcat (filename, string);
542 fd = open (filename, mode);
550 *filename_opened = (char *) 0;
551 else if (filename[0] == '/')
552 *filename_opened = savestring (filename, strlen (filename));
555 /* Beware the // my son, the Emacs barfs, the botch that catch... */
557 *filename_opened = concat (current_directory,
558 '/' == current_directory[strlen(current_directory)-1]? "": "/",
562 /* start-sanitize-mpw */
564 /* This is a debugging hack that can go away when all combinations
565 of Mac and Unix names are handled reasonably. */
567 extern int debug_openp;
571 printf("openp on %s, path %s mode %d prot %d\n returned %d",
572 string, path, mode, prot, fd);
573 if (*filename_opened)
574 printf(" (filename is %s)", *filename_opened);
579 /* end-sanitize-mpw */
584 /* Open a source file given a symtab S. Returns a file descriptor or
585 negative number for error. */
591 char *path = source_path;
596 /* Quick way out if we already know its full name */
599 result = open (s->fullname, O_RDONLY);
602 /* Didn't work -- free old one, try again. */
603 mfree (s->objfile->md, s->fullname);
607 if (s->dirname != NULL)
609 /* Replace a path entry of $cdir with the compilation directory name */
611 /* We cast strstr's result in case an ANSIhole has made it const,
612 which produces a "required warning" when assigned to a nonconst. */
613 p = (char *)strstr (source_path, "$cdir");
614 if (p && (p == path || p[-1] == DIRNAME_SEPARATOR)
615 && (p[cdir_len] == DIRNAME_SEPARATOR || p[cdir_len] == '\0'))
620 alloca (strlen (source_path) + 1 + strlen (s->dirname) + 1);
621 len = p - source_path;
622 strncpy (path, source_path, len); /* Before $cdir */
623 strcpy (path + len, s->dirname); /* new stuff */
624 strcat (path + len, source_path + len + cdir_len); /* After $cdir */
628 result = openp (path, 0, s->filename, O_RDONLY, 0, &s->fullname);
631 /* Didn't work. Try using just the basename. */
632 p = basename (s->filename);
633 if (p != s->filename)
634 result = openp (path, 0, p, O_RDONLY, 0, &s->fullname);
636 /* start-sanitize-mpw */
640 /* Didn't work. Try using just the MPW basename. */
641 p = (char *) mpw_basename (s->filename);
642 if (p != s->filename)
643 result = openp (path, 0, p, O_RDONLY, 0, &s->fullname);
647 /* Didn't work. Try using the mixed Unix/MPW basename. */
648 p = (char *) mpw_mixed_basename (s->filename);
649 if (p != s->filename)
650 result = openp (path, 0, p, O_RDONLY, 0, &s->fullname);
653 /* end-sanitize-mpw */
656 fullname = s->fullname;
657 s->fullname = mstrsave (s->objfile->md, s->fullname);
663 /* Return the path to the source file associated with symtab. Returns NULL
667 symtab_to_filename (s)
675 /* If we've seen the file before, just return fullname. */
680 /* Try opening the file to setup fullname */
682 fd = open_source_file (s);
684 return s->filename; /* File not found. Just use short name */
686 /* Found the file. Cleanup and return the full name */
693 /* Create and initialize the table S->line_charpos that records
694 the positions of the lines in the source file, which is assumed
695 to be open on descriptor DESC.
696 All set S->nlines to the number of such lines. */
699 find_source_lines (s, desc)
704 register char *data, *p, *end;
706 int lines_allocated = 1000;
711 line_charpos = (int *) xmmalloc (s -> objfile -> md,
712 lines_allocated * sizeof (int));
713 if (fstat (desc, &st) < 0)
714 perror_with_name (s->filename);
718 exec_mtime = bfd_get_mtime(exec_bfd);
719 if (exec_mtime && exec_mtime < st.st_mtime)
720 printf_filtered ("Source file is more recent than executable.\n");
723 #ifdef LSEEK_NOT_LINEAR
727 /* Have to read it byte by byte to find out where the chars live */
729 line_charpos[0] = tell(desc);
731 while (myread(desc, &c, 1)>0)
735 if (nlines == lines_allocated)
737 lines_allocated *= 2;
739 (int *) xmrealloc (s -> objfile -> md, (char *) line_charpos,
740 sizeof (int) * lines_allocated);
742 line_charpos[nlines++] = tell(desc);
746 #else /* lseek linear. */
748 struct cleanup *old_cleanups;
750 /* st_size might be a large type, but we only support source files whose
751 size fits in an int. */
752 size = (int) st.st_size;
754 /* Use malloc, not alloca, because this may be pretty large, and we may
755 run into various kinds of limits on stack size. */
756 data = (char *) xmalloc (size);
757 old_cleanups = make_cleanup (free, data);
759 if (myread (desc, data, size) < 0)
760 perror_with_name (s->filename);
768 /* A newline at the end does not start a new line. */
771 if (nlines == lines_allocated)
773 lines_allocated *= 2;
775 (int *) xmrealloc (s -> objfile -> md, (char *) line_charpos,
776 sizeof (int) * lines_allocated);
778 line_charpos[nlines++] = p - data;
781 do_cleanups (old_cleanups);
783 #endif /* lseek linear. */
786 (int *) xmrealloc (s -> objfile -> md, (char *) line_charpos,
787 nlines * sizeof (int));
791 /* Return the character position of a line LINE in symtab S.
792 Return 0 if anything is invalid. */
794 #if 0 /* Currently unused */
797 source_line_charpos (s, line)
802 if (!s->line_charpos || line <= 0) return 0;
803 if (line > s->nlines)
805 return s->line_charpos[line - 1];
808 /* Return the line number of character position POS in symtab S. */
811 source_charpos_line (s, chr)
812 register struct symtab *s;
815 register int line = 0;
818 if (s == 0 || s->line_charpos == 0) return 0;
819 lnp = s->line_charpos;
820 /* Files are usually short, so sequential search is Ok */
821 while (line < s->nlines && *lnp <= chr)
826 if (line >= s->nlines)
834 /* Get full pathname and line number positions for a symtab.
835 Return nonzero if line numbers may have changed.
836 Set *FULLNAME to actual name of the file as found by `openp',
837 or to 0 if the file is not found. */
840 get_filename_and_charpos (s, fullname)
844 register int desc, linenums_changed = 0;
846 desc = open_source_file (s);
854 *fullname = s->fullname;
855 if (s->line_charpos == 0) linenums_changed = 1;
856 if (linenums_changed) find_source_lines (s, desc);
858 return linenums_changed;
861 /* Print text describing the full name of the source file S
862 and the line number LINE and its corresponding character position.
863 The text starts with two Ctrl-z so that the Emacs-GDB interface
866 MID_STATEMENT is nonzero if the PC is not at the beginning of that line.
868 Return 1 if successful, 0 if could not find the file. */
871 identify_source_line (s, line, mid_statement, pc)
877 if (s->line_charpos == 0)
878 get_filename_and_charpos (s, (char **)NULL);
879 if (s->fullname == 0)
881 if (line > s->nlines)
882 /* Don't index off the end of the line_charpos array. */
884 annotate_source (s->fullname, line, s->line_charpos[line - 1],
887 current_source_line = line;
888 first_line_listed = line;
889 last_line_listed = line;
890 current_source_symtab = s;
894 /* Print source lines from the file of symtab S,
895 starting with line number LINE and stopping before line number STOPLINE. */
898 print_source_lines (s, line, stopline, noerror)
905 register FILE *stream;
906 int nlines = stopline - line;
908 /* Regardless of whether we can open the file, set current_source_symtab. */
909 current_source_symtab = s;
910 current_source_line = line;
911 first_line_listed = line;
913 desc = open_source_file (s);
917 char *name = alloca (strlen (s->filename) + 100);
918 sprintf (name, "%s:%d", s->filename, line);
919 print_sys_errmsg (name, errno);
924 if (s->line_charpos == 0)
925 find_source_lines (s, desc);
927 if (line < 1 || line > s->nlines)
930 error ("Line number %d out of range; %s has %d lines.",
931 line, s->filename, s->nlines);
934 if (lseek (desc, s->line_charpos[line - 1], 0) < 0)
937 perror_with_name (s->filename);
940 stream = fdopen (desc, FOPEN_RT);
947 last_line_listed = current_source_line;
948 printf_filtered ("%d\t", current_source_line++);
951 if (c < 040 && c != '\t' && c != '\n' && c != '\r')
952 printf_filtered ("^%c", c + 0100);
954 printf_filtered ("^?");
956 printf_filtered ("%c", c);
957 } while (c != '\n' && (c = fgetc (stream)) >= 0);
965 /* Print a list of files and line numbers which a user may choose from
966 in order to list a function which was specified ambiguously (as with
967 `list classname::overloadedfuncname', for example). The vector in
968 SALS provides the filenames and line numbers. */
971 ambiguous_line_spec (sals)
972 struct symtabs_and_lines *sals;
976 for (i = 0; i < sals->nelts; ++i)
977 printf_filtered("file: \"%s\", line number: %d\n",
978 sals->sals[i].symtab->filename, sals->sals[i].line);
982 list_command (arg, from_tty)
986 struct symtabs_and_lines sals, sals_end;
987 struct symtab_and_line sal, sal_end;
996 if (!have_full_symbols () && !have_partial_symbols())
997 error ("No symbol table is loaded. Use the \"file\" command.");
999 /* Pull in a current source symtab if necessary */
1000 if (current_source_symtab == 0 &&
1001 (arg == 0 || arg[0] == '+' || arg[0] == '-'))
1002 select_source_symtab (0);
1004 /* "l" or "l +" lists next ten lines. */
1006 if (arg == 0 || STREQ (arg, "+"))
1008 if (current_source_symtab == 0)
1009 error ("No default source file yet. Do \"help list\".");
1010 print_source_lines (current_source_symtab, current_source_line,
1011 current_source_line + lines_to_list, 0);
1015 /* "l -" lists previous ten lines, the ones before the ten just listed. */
1016 if (STREQ (arg, "-"))
1018 if (current_source_symtab == 0)
1019 error ("No default source file yet. Do \"help list\".");
1020 print_source_lines (current_source_symtab,
1021 max (first_line_listed - lines_to_list, 1),
1022 first_line_listed, 0);
1026 /* Now if there is only one argument, decode it in SAL
1028 If there are two arguments, decode them in SAL and SAL_END
1029 and clear NO_END; however, if one of the arguments is blank,
1030 set DUMMY_BEG or DUMMY_END to record that fact. */
1037 sals = decode_line_1 (&arg1, 0, 0, 0, 0);
1039 if (! sals.nelts) return; /* C++ */
1042 ambiguous_line_spec (&sals);
1051 /* Record whether the BEG arg is all digits. */
1053 for (p = arg; p != arg1 && *p >= '0' && *p <= '9'; p++);
1054 linenum_beg = (p == arg1);
1056 while (*arg1 == ' ' || *arg1 == '\t')
1062 while (*arg1 == ' ' || *arg1 == '\t')
1069 sals_end = decode_line_1 (&arg1, 0, 0, 0, 0);
1071 sals_end = decode_line_1 (&arg1, 0, sal.symtab, sal.line, 0);
1072 if (sals_end.nelts == 0)
1074 if (sals_end.nelts > 1)
1076 ambiguous_line_spec (&sals_end);
1077 free (sals_end.sals);
1080 sal_end = sals_end.sals[0];
1081 free (sals_end.sals);
1086 error ("Junk at end of line specification.");
1088 if (!no_end && !dummy_beg && !dummy_end
1089 && sal.symtab != sal_end.symtab)
1090 error ("Specified start and end are in different files.");
1091 if (dummy_beg && dummy_end)
1092 error ("Two empty args do not say what lines to list.");
1094 /* if line was specified by address,
1095 first print exactly which line, and which file.
1096 In this case, sal.symtab == 0 means address is outside
1097 of all known source files, not that user failed to give a filename. */
1100 if (sal.symtab == 0)
1101 /* FIXME-32x64--assumes sal.pc fits in long. */
1102 error ("No source file for address %s.",
1103 local_hex_string((unsigned long) sal.pc));
1104 sym = find_pc_function (sal.pc);
1107 print_address_numeric (sal.pc, 1, gdb_stdout);
1108 printf_filtered (" is in ");
1109 fputs_filtered (SYMBOL_SOURCE_NAME (sym), gdb_stdout);
1110 printf_filtered (" (%s:%d).\n", sal.symtab->filename, sal.line);
1114 print_address_numeric (sal.pc, 1, gdb_stdout);
1115 printf_filtered (" is at %s:%d.\n",
1116 sal.symtab->filename, sal.line);
1120 /* If line was not specified by just a line number,
1121 and it does not imply a symtab, it must be an undebuggable symbol
1122 which means no source code. */
1124 if (! linenum_beg && sal.symtab == 0)
1125 error ("No line number known for %s.", arg);
1127 /* If this command is repeated with RET,
1128 turn it into the no-arg variant. */
1133 if (dummy_beg && sal_end.symtab == 0)
1134 error ("No default source file yet. Do \"help list\".");
1136 print_source_lines (sal_end.symtab,
1137 max (sal_end.line - (lines_to_list - 1), 1),
1138 sal_end.line + 1, 0);
1139 else if (sal.symtab == 0)
1140 error ("No default source file yet. Do \"help list\".");
1142 print_source_lines (sal.symtab,
1143 max (sal.line - (lines_to_list / 2), 1),
1144 sal.line + (lines_to_list / 2), 0);
1146 print_source_lines (sal.symtab, sal.line,
1148 ? sal.line + lines_to_list
1149 : sal_end.line + 1),
1153 /* Print info on range of pc's in a specified line. */
1156 line_info (arg, from_tty)
1160 struct symtabs_and_lines sals;
1161 struct symtab_and_line sal;
1162 CORE_ADDR start_pc, end_pc;
1167 sal.symtab = current_source_symtab;
1168 sal.line = last_line_listed;
1171 sals.sals = (struct symtab_and_line *)
1172 xmalloc (sizeof (struct symtab_and_line));
1177 sals = decode_line_spec_1 (arg, 0);
1182 /* C++ More than one line may have been specified, as when the user
1183 specifies an overloaded function name. Print info on them all. */
1184 for (i = 0; i < sals.nelts; i++)
1188 if (sal.symtab == 0)
1190 printf_filtered ("No line number information available");
1193 /* This is useful for "info line *0x7f34". If we can't tell the
1194 user about a source line, at least let them have the symbolic
1196 printf_filtered (" for address ");
1198 print_address (sal.pc, gdb_stdout);
1201 printf_filtered (".");
1202 printf_filtered ("\n");
1204 else if (sal.line > 0
1205 && find_line_pc_range (sal, &start_pc, &end_pc))
1207 if (start_pc == end_pc)
1209 printf_filtered ("Line %d of \"%s\"",
1210 sal.line, sal.symtab->filename);
1212 printf_filtered (" is at address ");
1213 print_address (start_pc, gdb_stdout);
1215 printf_filtered (" but contains no code.\n");
1219 printf_filtered ("Line %d of \"%s\"",
1220 sal.line, sal.symtab->filename);
1222 printf_filtered (" starts at address ");
1223 print_address (start_pc, gdb_stdout);
1225 printf_filtered (" and ends at ");
1226 print_address (end_pc, gdb_stdout);
1227 printf_filtered (".\n");
1230 /* x/i should display this line's code. */
1231 set_next_address (start_pc);
1233 /* Repeating "info line" should do the following line. */
1234 last_line_listed = sal.line + 1;
1236 /* If this is the only line, show the source code. If it could
1237 not find the file, don't do anything special. */
1238 if (annotation_level && sals.nelts == 1)
1239 identify_source_line (sal.symtab, sal.line, 0, start_pc);
1242 /* Is there any case in which we get here, and have an address
1243 which the user would want to see? If we have debugging symbols
1244 and no line numbers? */
1245 printf_filtered ("Line number %d is out of range for \"%s\".\n",
1246 sal.line, sal.symtab->filename);
1251 /* Commands to search the source file for a regexp. */
1255 forward_search_command (regex, from_tty)
1261 register FILE *stream;
1262 int line = last_line_listed + 1;
1265 msg = (char *) re_comp (regex);
1269 if (current_source_symtab == 0)
1270 select_source_symtab (0);
1272 /* Search from last_line_listed+1 in current_source_symtab */
1274 desc = open_source_file (current_source_symtab);
1276 perror_with_name (current_source_symtab->filename);
1278 if (current_source_symtab->line_charpos == 0)
1279 find_source_lines (current_source_symtab, desc);
1281 if (line < 1 || line > current_source_symtab->nlines)
1284 error ("Expression not found");
1287 if (lseek (desc, current_source_symtab->line_charpos[line - 1], 0) < 0)
1290 perror_with_name (current_source_symtab->filename);
1293 stream = fdopen (desc, FOPEN_RT);
1296 static char *buf = NULL;
1298 int cursize, newsize;
1301 buf = xmalloc (cursize);
1309 if (p - buf == cursize)
1311 newsize = cursize + cursize / 2;
1312 buf = xrealloc (buf, newsize);
1316 } while (c != '\n' && (c = getc (stream)) >= 0);
1318 /* we now have a source line in buf, null terminate and match */
1320 if (re_exec (buf) > 0)
1324 print_source_lines (current_source_symtab, line, line+1, 0);
1325 set_internalvar (lookup_internalvar ("_"),
1326 value_from_longest (builtin_type_int,
1328 current_source_line = max (line - lines_to_list / 2, 1);
1334 printf_filtered ("Expression not found\n");
1340 reverse_search_command (regex, from_tty)
1346 register FILE *stream;
1347 int line = last_line_listed - 1;
1350 msg = (char *) re_comp (regex);
1354 if (current_source_symtab == 0)
1355 select_source_symtab (0);
1357 /* Search from last_line_listed-1 in current_source_symtab */
1359 desc = open_source_file (current_source_symtab);
1361 perror_with_name (current_source_symtab->filename);
1363 if (current_source_symtab->line_charpos == 0)
1364 find_source_lines (current_source_symtab, desc);
1366 if (line < 1 || line > current_source_symtab->nlines)
1369 error ("Expression not found");
1372 if (lseek (desc, current_source_symtab->line_charpos[line - 1], 0) < 0)
1375 perror_with_name (current_source_symtab->filename);
1378 stream = fdopen (desc, FOPEN_RT);
1382 /* FIXME!!! We walk right off the end of buf if we get a long line!!! */
1383 char buf[4096]; /* Should be reasonable??? */
1384 register char *p = buf;
1391 } while (c != '\n' && (c = getc (stream)) >= 0);
1393 /* We now have a source line in buf; null terminate and match. */
1395 if (re_exec (buf) > 0)
1399 print_source_lines (current_source_symtab,
1401 set_internalvar (lookup_internalvar ("_"),
1402 value_from_longest (builtin_type_int,
1404 current_source_line = max (line - lines_to_list / 2, 1);
1408 if (fseek (stream, current_source_symtab->line_charpos[line - 1], 0) < 0)
1411 perror_with_name (current_source_symtab->filename);
1415 printf_filtered ("Expression not found\n");
1421 _initialize_source ()
1423 struct cmd_list_element *c;
1424 current_source_symtab = 0;
1425 init_source_path ();
1427 /* The intention is to use POSIX Basic Regular Expressions.
1428 Always use the GNU regex routine for consistency across all hosts.
1429 Our current GNU regex.c does not have all the POSIX features, so this is
1430 just an approximation. */
1431 re_set_syntax (RE_SYNTAX_GREP);
1433 c = add_cmd ("directory", class_files, directory_command,
1434 "Add directory DIR to beginning of search path for source files.\n\
1435 Forget cached info on source file locations and line positions.\n\
1436 DIR can also be $cwd for the current working directory, or $cdir for the\n\
1437 directory in which the source file was compiled into object code.\n\
1438 With no argument, reset the search path to $cdir:$cwd, the default.",
1440 c->completer = filename_completer;
1442 add_cmd ("directories", no_class, show_directories,
1443 "Current search path for finding source files.\n\
1444 $cwd in the path means the current working directory.\n\
1445 $cdir in the path means the compilation directory of the source file.",
1448 add_info ("source", source_info,
1449 "Information about the current source file.");
1451 add_info ("line", line_info,
1452 concat ("Core addresses of the code for a source line.\n\
1453 Line can be specified as\n\
1454 LINENUM, to list around that line in current file,\n\
1455 FILE:LINENUM, to list around that line in that file,\n\
1456 FUNCTION, to list around beginning of that function,\n\
1457 FILE:FUNCTION, to distinguish among like-named static functions.\n\
1459 Default is to describe the last source line that was listed.\n\n\
1460 This sets the default address for \"x\" to the line's first instruction\n\
1461 so that \"x/i\" suffices to start examining the machine code.\n\
1462 The address is also stored as the value of \"$_\".", NULL));
1464 add_com ("forward-search", class_files, forward_search_command,
1465 "Search for regular expression (see regex(3)) from last line listed.\n\
1466 The matching line number is also stored as the value of \"$_\".");
1467 add_com_alias ("search", "forward-search", class_files, 0);
1469 add_com ("reverse-search", class_files, reverse_search_command,
1470 "Search backward for regular expression (see regex(3)) from last line listed.\n\
1471 The matching line number is also stored as the value of \"$_\".");
1473 add_com ("list", class_files, list_command,
1474 concat ("List specified function or line.\n\
1475 With no argument, lists ten more lines after or around previous listing.\n\
1476 \"list -\" lists the ten lines before a previous ten-line listing.\n\
1477 One argument specifies a line, and ten lines are listed around that line.\n\
1478 Two arguments with comma between specify starting and ending lines to list.\n\
1480 Lines can be specified in these ways:\n\
1481 LINENUM, to list around that line in current file,\n\
1482 FILE:LINENUM, to list around that line in that file,\n\
1483 FUNCTION, to list around beginning of that function,\n\
1484 FILE:FUNCTION, to distinguish among like-named static functions.\n\
1485 *ADDRESS, to list around the line containing that address.\n\
1486 With two args if one is empty it stands for ten lines away from the other arg.", NULL));
1488 add_com_alias ("l", "list", class_files, 1);
1491 (add_set_cmd ("listsize", class_support, var_uinteger,
1492 (char *)&lines_to_list,
1493 "Set number of source lines gdb will list by default.",