1 /* List lines of source files for GDB, the GNU debugger.
2 Copyright 1986-1989, 1991-1999 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., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
23 #include "expression.h"
31 #include <sys/types.h>
32 #include "gdb_string.h"
36 #include "gdb_regex.h"
45 #ifdef CRLF_SOURCE_FILES
47 /* Define CRLF_SOURCE_FILES in an xm-*.h file if source files on the
48 host use \r\n rather than just \n. Defining CRLF_SOURCE_FILES is
49 much faster than defining LSEEK_NOT_LINEAR. */
55 #define OPEN_MODE (O_RDONLY | O_BINARY)
56 #define FDOPEN_MODE FOPEN_RB
58 #else /* ! defined (CRLF_SOURCE_FILES) */
60 #define OPEN_MODE O_RDONLY
61 #define FDOPEN_MODE FOPEN_RT
63 #endif /* ! defined (CRLF_SOURCE_FILES) */
65 /* Prototypes for exported functions. */
67 void _initialize_source (void);
69 /* Prototypes for local functions. */
71 static int get_filename_and_charpos (struct symtab *, char **);
73 static void reverse_search_command (char *, int);
75 static void forward_search_command (char *, int);
77 static void line_info (char *, int);
79 static void list_command (char *, int);
81 static void ambiguous_line_spec (struct symtabs_and_lines *);
83 static void source_info (char *, int);
85 static void show_directories (char *, int);
87 /* Path of directories to search for source files.
88 Same format as the PATH environment variable's value. */
92 /* Symtab of default file for listing lines of. */
94 struct symtab *current_source_symtab;
96 /* Default next line to list. */
98 int current_source_line;
100 /* Default number of lines to print with commands like "list".
101 This is based on guessing how many long (i.e. more than chars_per_line
102 characters) lines there will be. To be completely correct, "list"
103 and friends should be rewritten to count characters and see where
104 things are wrapping, but that would be a fair amount of work. */
106 int lines_to_list = 10;
108 /* Line number of last line printed. Default for various commands.
109 current_source_line is usually, but not always, the same as this. */
111 static int last_line_listed;
113 /* First line number listed by last listing command. */
115 static int first_line_listed;
117 /* Saves the name of the last source file visited and a possible error code.
118 Used to prevent repeating annoying "No such file or directories" msgs */
120 static struct symtab *last_source_visited = NULL;
121 static int last_source_error = 0;
124 /* Set the source file default for the "list" command to be S.
126 If S is NULL, and we don't have a default, find one. This
127 should only be called when the user actually tries to use the
128 default, since we produce an error if we can't find a reasonable
129 default. Also, since this can cause symbols to be read, doing it
130 before we need to would make things slower than necessary. */
133 select_source_symtab (s)
134 register struct symtab *s;
136 struct symtabs_and_lines sals;
137 struct symtab_and_line sal;
138 struct partial_symtab *ps;
139 struct partial_symtab *cs_pst = 0;
144 current_source_symtab = s;
145 current_source_line = 1;
149 if (current_source_symtab)
152 /* Make the default place to list be the function `main'
154 if (lookup_symbol ("main", 0, VAR_NAMESPACE, 0, NULL))
156 sals = decode_line_spec ("main", 1);
159 current_source_symtab = sal.symtab;
160 current_source_line = max (sal.line - (lines_to_list - 1), 1);
161 if (current_source_symtab)
165 /* All right; find the last file in the symtab list (ignoring .h's). */
167 current_source_line = 1;
169 for (ofp = object_files; ofp != NULL; ofp = ofp->next)
171 for (s = ofp->symtabs; s; s = s->next)
173 char *name = s->filename;
174 int len = strlen (name);
175 if (!(len > 2 && (STREQ (&name[len - 2], ".h"))))
177 current_source_symtab = s;
181 if (current_source_symtab)
184 /* Howabout the partial symbol tables? */
186 for (ofp = object_files; ofp != NULL; ofp = ofp->next)
188 for (ps = ofp->psymtabs; ps != NULL; ps = ps->next)
190 char *name = ps->filename;
191 int len = strlen (name);
192 if (!(len > 2 && (STREQ (&name[len - 2], ".h"))))
202 internal_error ("select_source_symtab: readin pst found and no symtabs.");
206 current_source_symtab = PSYMTAB_TO_SYMTAB (cs_pst);
209 if (current_source_symtab)
212 error ("Can't find a default source file");
216 show_directories (ignore, from_tty)
220 puts_filtered ("Source directories searched: ");
221 puts_filtered (source_path);
222 puts_filtered ("\n");
225 /* Forget what we learned about line positions in source files, and
226 which directories contain them; must check again now since files
227 may be found in a different directory now. */
230 forget_cached_source_info ()
232 register struct symtab *s;
233 register struct objfile *objfile;
235 for (objfile = object_files; objfile != NULL; objfile = objfile->next)
237 for (s = objfile->symtabs; s != NULL; s = s->next)
239 if (s->line_charpos != NULL)
241 mfree (objfile->md, s->line_charpos);
242 s->line_charpos = NULL;
244 if (s->fullname != NULL)
246 mfree (objfile->md, s->fullname);
258 sprintf (buf, "$cdir%c$cwd", DIRNAME_SEPARATOR);
259 source_path = strsave (buf);
260 forget_cached_source_info ();
263 /* Add zero or more directories to the front of the source path. */
266 directory_command (dirname, from_tty)
271 /* FIXME, this goes to "delete dir"... */
274 if (from_tty && query ("Reinitialize source path to empty? "))
282 mod_path (dirname, &source_path);
283 last_source_visited = NULL;
286 show_directories ((char *) 0, from_tty);
287 forget_cached_source_info ();
290 /* Add zero or more directories to the front of an arbitrary path. */
293 mod_path (dirname, which_path)
297 char *old = *which_path;
303 dirname = strsave (dirname);
304 make_cleanup (free, dirname);
308 char *name = dirname;
313 char *separator = strchr (name, DIRNAME_SEPARATOR);
314 char *space = strchr (name, ' ');
315 char *tab = strchr (name, '\t');
317 if (separator == 0 && space == 0 && tab == 0)
318 p = dirname = name + strlen (name);
322 if (separator != 0 && (p == 0 || separator < p))
324 if (space != 0 && (p == 0 || space < p))
326 if (tab != 0 && (p == 0 || tab < p))
329 while (*dirname == DIRNAME_SEPARATOR
336 if (!(SLASH_P (*name) && p <= name + 1) /* "/" */
337 #if defined(_WIN32) || defined(__MSDOS__)
338 /* On MS-DOS and MS-Windows, h:\ is different from h: */
339 && !(!SLASH_P (*name) && ROOTED_P (name) && p <= name + 3) /* d:/ */
342 /* Sigh. "foo/" => "foo" */
346 while (p > name && p[-1] == '.')
350 /* "." => getwd (). */
351 name = current_directory;
354 else if (p > name + 1 && SLASH_P (p[-2]))
364 /* "...foo/." => "...foo". */
375 name = tilde_expand (name);
376 #if defined(_WIN32) || defined(__MSDOS__)
377 else if (ROOTED_P (name) && p == name + 2) /* "d:" => "d:." */
378 name = concat (name, ".", NULL);
380 else if (!ROOTED_P (name) && name[0] != '$')
381 name = concat (current_directory, SLASH_STRING, name, NULL);
383 name = savestring (name, p - name);
384 make_cleanup (free, name);
386 /* Unless it's a variable, check existence. */
389 /* These are warnings, not errors, since we don't want a
390 non-existent directory in a .gdbinit file to stop processing
391 of the .gdbinit file.
393 Whether they get added to the path is more debatable. Current
394 answer is yes, in case the user wants to go make the directory
395 or whatever. If the directory continues to not exist/not be
396 a directory/etc, then having them in the path should be
398 if (stat (name, &st) < 0)
400 int save_errno = errno;
401 fprintf_unfiltered (gdb_stderr, "Warning: ");
402 print_sys_errmsg (name, save_errno);
404 else if ((st.st_mode & S_IFMT) != S_IFDIR)
405 warning ("%s is not a directory.", name);
410 register unsigned int len = strlen (name);
415 /* FIXME: strncmp loses in interesting ways on MS-DOS and
416 MS-Windows because of case-insensitivity and two different
417 but functionally identical slash characters. We need a
418 special filesystem-dependent file-name comparison function.
420 Actually, even on Unix I would use realpath() or its work-
421 alike before comparing. Then all the code above which
422 removes excess slashes and dots could simply go away. */
423 if (!strncmp (p, name, len)
424 && (p[len] == '\0' || p[len] == DIRNAME_SEPARATOR))
426 /* Found it in the search path, remove old copy */
428 p--; /* Back over leading separator */
429 if (prefix > p - *which_path)
430 goto skip_dup; /* Same dir twice in one cmd */
431 strcpy (p, &p[len + 1]); /* Copy from next \0 or : */
433 p = strchr (p, DIRNAME_SEPARATOR);
443 tinybuf[0] = DIRNAME_SEPARATOR;
446 /* 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. */
453 temp = concat (old, tinybuf, name, NULL);
455 *which_path = concat (temp, "", &old[prefix], NULL);
456 prefix = strlen (temp);
461 *which_path = concat (name, (old[0] ? tinybuf : old), old, NULL);
462 prefix = strlen (name);
470 while (*dirname != '\0');
475 source_info (ignore, from_tty)
479 register struct symtab *s = current_source_symtab;
483 printf_filtered ("No current source file.\n");
486 printf_filtered ("Current source file is %s\n", s->filename);
488 printf_filtered ("Compilation directory is %s\n", s->dirname);
490 printf_filtered ("Located in %s\n", s->fullname);
492 printf_filtered ("Contains %d line%s.\n", s->nlines,
493 s->nlines == 1 ? "" : "s");
495 printf_filtered ("Source language is %s.\n", language_str (s->language));
496 printf_filtered ("Compiled with %s debugging format.\n", s->debugformat);
501 /* Open a file named STRING, searching path PATH (dir names sep by some char)
502 using mode MODE and protection bits PROT in the calls to open.
504 If TRY_CWD_FIRST, try to open ./STRING before searching PATH.
505 (ie pretend the first element of PATH is "."). This also indicates
506 that a slash in STRING disables searching of the path (this is
507 so that "exec-file ./foo" or "symbol-file ./foo" insures that you
508 get that particular version of foo or an error message).
510 If FILENAMED_OPENED is non-null, set it to a newly allocated string naming
511 the actual file opened (this string will always start with a "/". We
512 have to take special pains to avoid doubling the "/" between the directory
513 and the file, sigh! Emacs gets confuzzed by this when we print the
516 If a file is found, return the descriptor.
517 Otherwise, return -1, with errno set for the last name we tried to open. */
519 /* >>>> This should only allow files of certain types,
520 >>>> eg executable, non-directory */
522 openp (path, try_cwd_first, string, mode, prot, filename_opened)
528 char **filename_opened;
531 register char *filename;
532 register char *p, *p1;
543 if (try_cwd_first || ROOTED_P (string))
547 fd = open (filename, mode, prot);
550 for (i = 0; string[i]; i++)
551 if (SLASH_P (string[i]))
556 while (string[0] == '.' && SLASH_P (string[1]))
559 alloclen = strlen (path) + strlen (string) + 2;
560 filename = (char *) alloca (alloclen);
562 for (p = path; p; p = p1 ? p1 + 1 : 0)
564 p1 = (char *) strchr (p, DIRNAME_SEPARATOR);
570 if (len == 4 && p[0] == '$' && p[1] == 'c'
571 && p[2] == 'w' && p[3] == 'd')
573 /* Name is $cwd -- insert current directory name instead. */
576 /* First, realloc the filename buffer if too short. */
577 len = strlen (current_directory);
578 newlen = len + strlen (string) + 2;
579 if (newlen > alloclen)
582 filename = (char *) alloca (alloclen);
584 strcpy (filename, current_directory);
588 /* Normal file name in path -- just use it. */
589 strncpy (filename, p, len);
593 /* Remove trailing slashes */
594 while (len > 0 && SLASH_P (filename[len - 1]))
597 strcat (filename + len, SLASH_STRING);
598 strcat (filename, string);
600 fd = open (filename, mode);
609 *filename_opened = (char *) 0;
610 else if (ROOTED_P (filename))
611 *filename_opened = savestring (filename, strlen (filename));
614 /* Beware the // my son, the Emacs barfs, the botch that catch... */
616 *filename_opened = concat (current_directory,
617 SLASH_P (current_directory[strlen (current_directory) - 1])
623 /* This is a debugging hack that can go away when all combinations
624 of Mac and Unix names are handled reasonably. */
626 extern int debug_openp;
630 printf ("openp on %s, path %s mode %d prot %d\n returned %d",
631 string, path, mode, prot, fd);
632 if (*filename_opened)
633 printf (" (filename is %s)", *filename_opened);
643 /* This is essentially a convenience, for clients that want the behaviour
644 of openp, using source_path, but that really don't want the file to be
645 opened but want instead just to know what the full pathname is (as
646 qualified against source_path).
648 The current working directory is searched first.
650 If the file was found, this function returns 1, and FULL_PATHNAME is
651 set to the fully-qualified pathname.
653 Else, this functions returns 0, and FULL_PATHNAME is set to NULL.
656 source_full_path_of (filename, full_pathname)
658 char **full_pathname;
662 fd = openp (source_path, 1, filename, O_RDONLY, 0, full_pathname);
665 *full_pathname = NULL;
674 /* Open a source file given a symtab S. Returns a file descriptor or
675 negative number for error. */
681 char *path = source_path;
686 /* Quick way out if we already know its full name */
689 result = open (s->fullname, OPEN_MODE);
692 /* Didn't work -- free old one, try again. */
693 mfree (s->objfile->md, s->fullname);
697 if (s->dirname != NULL)
699 /* Replace a path entry of $cdir with the compilation directory name */
701 /* We cast strstr's result in case an ANSIhole has made it const,
702 which produces a "required warning" when assigned to a nonconst. */
703 p = (char *) strstr (source_path, "$cdir");
704 if (p && (p == path || p[-1] == DIRNAME_SEPARATOR)
705 && (p[cdir_len] == DIRNAME_SEPARATOR || p[cdir_len] == '\0'))
710 alloca (strlen (source_path) + 1 + strlen (s->dirname) + 1);
711 len = p - source_path;
712 strncpy (path, source_path, len); /* Before $cdir */
713 strcpy (path + len, s->dirname); /* new stuff */
714 strcat (path + len, source_path + len + cdir_len); /* After $cdir */
718 result = openp (path, 0, s->filename, OPEN_MODE, 0, &s->fullname);
721 /* Didn't work. Try using just the basename. */
722 p = basename (s->filename);
723 if (p != s->filename)
724 result = openp (path, 0, p, OPEN_MODE, 0, &s->fullname);
729 /* Didn't work. Try using just the MPW basename. */
730 p = (char *) mpw_basename (s->filename);
731 if (p != s->filename)
732 result = openp (path, 0, p, OPEN_MODE, 0, &s->fullname);
736 /* Didn't work. Try using the mixed Unix/MPW basename. */
737 p = (char *) mpw_mixed_basename (s->filename);
738 if (p != s->filename)
739 result = openp (path, 0, p, OPEN_MODE, 0, &s->fullname);
745 fullname = s->fullname;
746 s->fullname = mstrsave (s->objfile->md, s->fullname);
752 /* Return the path to the source file associated with symtab. Returns NULL
756 symtab_to_filename (s)
764 /* If we've seen the file before, just return fullname. */
769 /* Try opening the file to setup fullname */
771 fd = open_source_file (s);
773 return s->filename; /* File not found. Just use short name */
775 /* Found the file. Cleanup and return the full name */
782 /* Create and initialize the table S->line_charpos that records
783 the positions of the lines in the source file, which is assumed
784 to be open on descriptor DESC.
785 All set S->nlines to the number of such lines. */
788 find_source_lines (s, desc)
793 register char *data, *p, *end;
795 int lines_allocated = 1000;
800 line_charpos = (int *) xmmalloc (s->objfile->md,
801 lines_allocated * sizeof (int));
802 if (fstat (desc, &st) < 0)
803 perror_with_name (s->filename);
805 if (s && s->objfile && s->objfile->obfd)
806 mtime = bfd_get_mtime (s->objfile->obfd);
808 mtime = bfd_get_mtime (exec_bfd);
810 if (mtime && mtime < st.st_mtime)
813 printf_filtered ("\n");
814 warning ("Source file is more recent than executable.\n");
817 #ifdef LSEEK_NOT_LINEAR
821 /* Have to read it byte by byte to find out where the chars live */
823 line_charpos[0] = lseek (desc, 0, SEEK_CUR);
825 while (myread (desc, &c, 1) > 0)
829 if (nlines == lines_allocated)
831 lines_allocated *= 2;
833 (int *) xmrealloc (s->objfile->md, (char *) line_charpos,
834 sizeof (int) * lines_allocated);
836 line_charpos[nlines++] = lseek (desc, 0, SEEK_CUR);
840 #else /* lseek linear. */
842 struct cleanup *old_cleanups;
844 /* st_size might be a large type, but we only support source files whose
845 size fits in an int. */
846 size = (int) st.st_size;
848 /* Use malloc, not alloca, because this may be pretty large, and we may
849 run into various kinds of limits on stack size. */
850 data = (char *) xmalloc (size);
851 old_cleanups = make_cleanup (free, data);
853 /* Reassign `size' to result of read for systems where \r\n -> \n. */
854 size = myread (desc, data, size);
856 perror_with_name (s->filename);
864 /* A newline at the end does not start a new line. */
867 if (nlines == lines_allocated)
869 lines_allocated *= 2;
871 (int *) xmrealloc (s->objfile->md, (char *) line_charpos,
872 sizeof (int) * lines_allocated);
874 line_charpos[nlines++] = p - data;
877 do_cleanups (old_cleanups);
879 #endif /* lseek linear. */
882 (int *) xmrealloc (s->objfile->md, (char *) line_charpos,
883 nlines * sizeof (int));
887 /* Return the character position of a line LINE in symtab S.
888 Return 0 if anything is invalid. */
890 #if 0 /* Currently unused */
893 source_line_charpos (s, line)
899 if (!s->line_charpos || line <= 0)
901 if (line > s->nlines)
903 return s->line_charpos[line - 1];
906 /* Return the line number of character position POS in symtab S. */
909 source_charpos_line (s, chr)
910 register struct symtab *s;
913 register int line = 0;
916 if (s == 0 || s->line_charpos == 0)
918 lnp = s->line_charpos;
919 /* Files are usually short, so sequential search is Ok */
920 while (line < s->nlines && *lnp <= chr)
925 if (line >= s->nlines)
933 /* Get full pathname and line number positions for a symtab.
934 Return nonzero if line numbers may have changed.
935 Set *FULLNAME to actual name of the file as found by `openp',
936 or to 0 if the file is not found. */
939 get_filename_and_charpos (s, fullname)
943 register int desc, linenums_changed = 0;
945 desc = open_source_file (s);
953 *fullname = s->fullname;
954 if (s->line_charpos == 0)
955 linenums_changed = 1;
956 if (linenums_changed)
957 find_source_lines (s, desc);
959 return linenums_changed;
962 /* Print text describing the full name of the source file S
963 and the line number LINE and its corresponding character position.
964 The text starts with two Ctrl-z so that the Emacs-GDB interface
967 MID_STATEMENT is nonzero if the PC is not at the beginning of that line.
969 Return 1 if successful, 0 if could not find the file. */
972 identify_source_line (s, line, mid_statement, pc)
978 if (s->line_charpos == 0)
979 get_filename_and_charpos (s, (char **) NULL);
980 if (s->fullname == 0)
982 if (line > s->nlines)
983 /* Don't index off the end of the line_charpos array. */
985 annotate_source (s->fullname, line, s->line_charpos[line - 1],
988 current_source_line = line;
989 first_line_listed = line;
990 last_line_listed = line;
991 current_source_symtab = s;
996 /* Print source lines from the file of symtab S,
997 starting with line number LINE and stopping before line number STOPLINE. */
999 static void print_source_lines_base (struct symtab *s, int line, int stopline,
1002 print_source_lines_base (s, line, stopline, noerror)
1010 register FILE *stream;
1011 int nlines = stopline - line;
1013 /* Regardless of whether we can open the file, set current_source_symtab. */
1014 current_source_symtab = s;
1015 current_source_line = line;
1016 first_line_listed = line;
1019 /* If printing of source lines is disabled, just print file and line number */
1020 if (ui_out_test_flags (uiout, ui_source_list))
1023 /* Only prints "No such file or directory" once */
1024 if ((s != last_source_visited) || (!last_source_error))
1026 last_source_visited = s;
1027 desc = open_source_file (s);
1031 desc = last_source_error;
1045 last_source_error = desc;
1049 char *name = alloca (strlen (s->filename) + 100);
1050 sprintf (name, "%d\t%s", line, s->filename);
1051 print_sys_errmsg (name, errno);
1055 ui_out_field_int (uiout, "line", line);
1056 ui_out_text (uiout, "\tin ");
1057 ui_out_field_string (uiout, "file", s->filename);
1058 ui_out_text (uiout, "\n");
1060 printf_filtered ("%d\tin %s\n", line, s->filename);
1066 last_source_error = 0;
1068 if (s->line_charpos == 0)
1069 find_source_lines (s, desc);
1071 if (line < 1 || line > s->nlines)
1074 error ("Line number %d out of range; %s has %d lines.",
1075 line, s->filename, s->nlines);
1078 if (lseek (desc, s->line_charpos[line - 1], 0) < 0)
1081 perror_with_name (s->filename);
1084 stream = fdopen (desc, FDOPEN_MODE);
1087 while (nlines-- > 0)
1095 last_line_listed = current_source_line;
1096 sprintf (buf, "%d\t", current_source_line++);
1097 ui_out_text (uiout, buf);
1100 if (c < 040 && c != '\t' && c != '\n' && c != '\r')
1102 sprintf (buf, "^%c", c + 0100);
1103 ui_out_text (uiout, buf);
1106 ui_out_text (uiout, "^?");
1107 #ifdef CRLF_SOURCE_FILES
1110 /* Skip a \r character, but only before a \n. */
1111 int c1 = fgetc (stream);
1114 printf_filtered ("^%c", c + 0100);
1116 ungetc (c1, stream);
1121 sprintf (buf, "%c", c);
1122 ui_out_text (uiout, buf);
1125 while (c != '\n' && (c = fgetc (stream)) >= 0);
1130 last_line_listed = current_source_line;
1131 printf_filtered ("%d\t", current_source_line++);
1134 if (c < 040 && c != '\t' && c != '\n' && c != '\r')
1135 printf_filtered ("^%c", c + 0100);
1137 printf_filtered ("^?");
1138 #ifdef CRLF_SOURCE_FILES
1141 /* Just skip \r characters. */
1145 printf_filtered ("%c", c);
1147 while (c != '\n' && (c = fgetc (stream)) >= 0);
1154 /* Show source lines from the file of symtab S, starting with line
1155 number LINE and stopping before line number STOPLINE. If this is the
1156 not the command line version, then the source is shown in the source
1157 window otherwise it is simply printed */
1160 print_source_lines (s, line, stopline, noerror)
1162 int line, stopline, noerror;
1166 m_winPtrIsNull (srcWin) || !srcWin->generic.isVisible)
1167 print_source_lines_base (s, line, stopline, noerror);
1170 TuiGenWinInfoPtr locator = locatorWinInfoPtr ();
1171 extern void tui_vAddWinToLayout (va_list);
1172 extern void tui_vUpdateSourceWindowsWithLine (va_list);
1174 /* Regardless of whether we can open the file,
1175 set current_source_symtab. */
1176 current_source_symtab = s;
1177 current_source_line = line;
1178 first_line_listed = line;
1180 /* make sure that the source window is displayed */
1181 tuiDo ((TuiOpaqueFuncPtr) tui_vAddWinToLayout, SRC_WIN);
1183 tuiDo ((TuiOpaqueFuncPtr) tui_vUpdateSourceWindowsWithLine, s, line);
1184 tuiDo ((TuiOpaqueFuncPtr) tui_vUpdateLocatorFilename, s->filename);
1187 print_source_lines_base (s, line, stopline, noerror);
1193 /* Print a list of files and line numbers which a user may choose from
1194 in order to list a function which was specified ambiguously (as with
1195 `list classname::overloadedfuncname', for example). The vector in
1196 SALS provides the filenames and line numbers. */
1199 ambiguous_line_spec (sals)
1200 struct symtabs_and_lines *sals;
1204 for (i = 0; i < sals->nelts; ++i)
1205 printf_filtered ("file: \"%s\", line number: %d\n",
1206 sals->sals[i].symtab->filename, sals->sals[i].line);
1210 list_command (arg, from_tty)
1214 struct symtabs_and_lines sals, sals_end;
1215 struct symtab_and_line sal, sal_end;
1221 int linenum_beg = 0;
1224 if (!have_full_symbols () && !have_partial_symbols ())
1225 error ("No symbol table is loaded. Use the \"file\" command.");
1227 /* Pull in a current source symtab if necessary */
1228 if (current_source_symtab == 0 &&
1229 (arg == 0 || arg[0] == '+' || arg[0] == '-'))
1230 select_source_symtab (0);
1232 /* "l" or "l +" lists next ten lines. */
1234 if (arg == 0 || STREQ (arg, "+"))
1236 if (current_source_symtab == 0)
1237 error ("No default source file yet. Do \"help list\".");
1238 print_source_lines (current_source_symtab, current_source_line,
1239 current_source_line + lines_to_list, 0);
1243 /* "l -" lists previous ten lines, the ones before the ten just listed. */
1244 if (STREQ (arg, "-"))
1246 if (current_source_symtab == 0)
1247 error ("No default source file yet. Do \"help list\".");
1248 print_source_lines (current_source_symtab,
1249 max (first_line_listed - lines_to_list, 1),
1250 first_line_listed, 0);
1254 /* Now if there is only one argument, decode it in SAL
1256 If there are two arguments, decode them in SAL and SAL_END
1257 and clear NO_END; however, if one of the arguments is blank,
1258 set DUMMY_BEG or DUMMY_END to record that fact. */
1265 sals = decode_line_1 (&arg1, 0, 0, 0, 0);
1271 ambiguous_line_spec (&sals);
1280 /* Record whether the BEG arg is all digits. */
1282 for (p = arg; p != arg1 && *p >= '0' && *p <= '9'; p++);
1283 linenum_beg = (p == arg1);
1285 while (*arg1 == ' ' || *arg1 == '\t')
1291 while (*arg1 == ' ' || *arg1 == '\t')
1298 sals_end = decode_line_1 (&arg1, 0, 0, 0, 0);
1300 sals_end = decode_line_1 (&arg1, 0, sal.symtab, sal.line, 0);
1301 if (sals_end.nelts == 0)
1303 if (sals_end.nelts > 1)
1305 ambiguous_line_spec (&sals_end);
1306 free (sals_end.sals);
1309 sal_end = sals_end.sals[0];
1310 free (sals_end.sals);
1315 error ("Junk at end of line specification.");
1317 if (!no_end && !dummy_beg && !dummy_end
1318 && sal.symtab != sal_end.symtab)
1319 error ("Specified start and end are in different files.");
1320 if (dummy_beg && dummy_end)
1321 error ("Two empty args do not say what lines to list.");
1323 /* if line was specified by address,
1324 first print exactly which line, and which file.
1325 In this case, sal.symtab == 0 means address is outside
1326 of all known source files, not that user failed to give a filename. */
1329 if (sal.symtab == 0)
1330 /* FIXME-32x64--assumes sal.pc fits in long. */
1331 error ("No source file for address %s.",
1332 local_hex_string ((unsigned long) sal.pc));
1333 sym = find_pc_function (sal.pc);
1336 print_address_numeric (sal.pc, 1, gdb_stdout);
1337 printf_filtered (" is in ");
1338 fputs_filtered (SYMBOL_SOURCE_NAME (sym), gdb_stdout);
1339 printf_filtered (" (%s:%d).\n", sal.symtab->filename, sal.line);
1343 print_address_numeric (sal.pc, 1, gdb_stdout);
1344 printf_filtered (" is at %s:%d.\n",
1345 sal.symtab->filename, sal.line);
1349 /* If line was not specified by just a line number,
1350 and it does not imply a symtab, it must be an undebuggable symbol
1351 which means no source code. */
1353 if (!linenum_beg && sal.symtab == 0)
1354 error ("No line number known for %s.", arg);
1356 /* If this command is repeated with RET,
1357 turn it into the no-arg variant. */
1362 if (dummy_beg && sal_end.symtab == 0)
1363 error ("No default source file yet. Do \"help list\".");
1365 print_source_lines (sal_end.symtab,
1366 max (sal_end.line - (lines_to_list - 1), 1),
1367 sal_end.line + 1, 0);
1368 else if (sal.symtab == 0)
1369 error ("No default source file yet. Do \"help list\".");
1372 int first_line = sal.line - lines_to_list / 2;
1374 if (first_line < 1) first_line = 1;
1376 print_source_lines (sal.symtab, first_line, first_line + lines_to_list,
1380 print_source_lines (sal.symtab, sal.line,
1382 ? sal.line + lines_to_list
1383 : sal_end.line + 1),
1387 /* Print info on range of pc's in a specified line. */
1390 line_info (arg, from_tty)
1394 struct symtabs_and_lines sals;
1395 struct symtab_and_line sal;
1396 CORE_ADDR start_pc, end_pc;
1399 INIT_SAL (&sal); /* initialize to zeroes */
1403 sal.symtab = current_source_symtab;
1404 sal.line = last_line_listed;
1406 sals.sals = (struct symtab_and_line *)
1407 xmalloc (sizeof (struct symtab_and_line));
1412 sals = decode_line_spec_1 (arg, 0);
1417 /* C++ More than one line may have been specified, as when the user
1418 specifies an overloaded function name. Print info on them all. */
1419 for (i = 0; i < sals.nelts; i++)
1423 if (sal.symtab == 0)
1425 printf_filtered ("No line number information available");
1428 /* This is useful for "info line *0x7f34". If we can't tell the
1429 user about a source line, at least let them have the symbolic
1431 printf_filtered (" for address ");
1433 print_address (sal.pc, gdb_stdout);
1436 printf_filtered (".");
1437 printf_filtered ("\n");
1439 else if (sal.line > 0
1440 && find_line_pc_range (sal, &start_pc, &end_pc))
1442 if (start_pc == end_pc)
1444 printf_filtered ("Line %d of \"%s\"",
1445 sal.line, sal.symtab->filename);
1447 printf_filtered (" is at address ");
1448 print_address (start_pc, gdb_stdout);
1450 printf_filtered (" but contains no code.\n");
1454 printf_filtered ("Line %d of \"%s\"",
1455 sal.line, sal.symtab->filename);
1457 printf_filtered (" starts at address ");
1458 print_address (start_pc, gdb_stdout);
1460 printf_filtered (" and ends at ");
1461 print_address (end_pc, gdb_stdout);
1462 printf_filtered (".\n");
1465 /* x/i should display this line's code. */
1466 set_next_address (start_pc);
1468 /* Repeating "info line" should do the following line. */
1469 last_line_listed = sal.line + 1;
1471 /* If this is the only line, show the source code. If it could
1472 not find the file, don't do anything special. */
1473 if (annotation_level && sals.nelts == 1)
1474 identify_source_line (sal.symtab, sal.line, 0, start_pc);
1477 /* Is there any case in which we get here, and have an address
1478 which the user would want to see? If we have debugging symbols
1479 and no line numbers? */
1480 printf_filtered ("Line number %d is out of range for \"%s\".\n",
1481 sal.line, sal.symtab->filename);
1486 /* Commands to search the source file for a regexp. */
1490 forward_search_command (regex, from_tty)
1496 register FILE *stream;
1502 ** If this is the TUI, search from the first line displayed in
1503 ** the source window, otherwise, search from last_line_listed+1
1504 ** in current_source_symtab
1507 line = last_line_listed;
1510 if (srcWin->generic.isVisible && srcWin->generic.contentSize > 0)
1511 line = ((TuiWinContent)
1512 srcWin->generic.content)[0]->whichElement.source.lineOrAddr.lineNo;
1515 printf_filtered ("No source displayed.\nExpression not found.\n");
1521 line = last_line_listed + 1;
1524 msg = (char *) re_comp (regex);
1528 if (current_source_symtab == 0)
1529 select_source_symtab (0);
1531 desc = open_source_file (current_source_symtab);
1533 perror_with_name (current_source_symtab->filename);
1535 if (current_source_symtab->line_charpos == 0)
1536 find_source_lines (current_source_symtab, desc);
1538 if (line < 1 || line > current_source_symtab->nlines)
1541 error ("Expression not found");
1544 if (lseek (desc, current_source_symtab->line_charpos[line - 1], 0) < 0)
1547 perror_with_name (current_source_symtab->filename);
1550 stream = fdopen (desc, FDOPEN_MODE);
1554 static char *buf = NULL;
1556 int cursize, newsize;
1559 buf = xmalloc (cursize);
1568 if (p - buf == cursize)
1570 newsize = cursize + cursize / 2;
1571 buf = xrealloc (buf, newsize);
1576 while (c != '\n' && (c = getc (stream)) >= 0);
1578 #ifdef CRLF_SOURCE_FILES
1579 /* Remove the \r, if any, at the end of the line, otherwise
1580 regular expressions that end with $ or \n won't work. */
1581 if (p - buf > 1 && p[-2] == '\r')
1588 /* we now have a source line in buf, null terminate and match */
1590 if (re_exec (buf) > 0)
1595 print_source_lines_base (current_source_symtab, line, line + 1, 0);
1596 print_source_lines (current_source_symtab, line, line + 1, 0);
1597 set_internalvar (lookup_internalvar ("_"),
1598 value_from_longest (builtin_type_int,
1600 current_source_line = max (line - lines_to_list / 2, 1);
1606 printf_filtered ("Expression not found\n");
1612 reverse_search_command (regex, from_tty)
1618 register FILE *stream;
1623 ** If this is the TUI, search from the first line displayed in
1624 ** the source window, otherwise, search from last_line_listed-1
1625 ** in current_source_symtab
1628 line = last_line_listed;
1631 if (srcWin->generic.isVisible && srcWin->generic.contentSize > 0)
1632 line = ((TuiWinContent)
1633 srcWin->generic.content)[0]->whichElement.source.lineOrAddr.lineNo;
1636 printf_filtered ("No source displayed.\nExpression not found.\n");
1642 line = last_line_listed - 1;
1645 msg = (char *) re_comp (regex);
1649 if (current_source_symtab == 0)
1650 select_source_symtab (0);
1652 desc = open_source_file (current_source_symtab);
1654 perror_with_name (current_source_symtab->filename);
1656 if (current_source_symtab->line_charpos == 0)
1657 find_source_lines (current_source_symtab, desc);
1659 if (line < 1 || line > current_source_symtab->nlines)
1662 error ("Expression not found");
1665 if (lseek (desc, current_source_symtab->line_charpos[line - 1], 0) < 0)
1668 perror_with_name (current_source_symtab->filename);
1671 stream = fdopen (desc, FDOPEN_MODE);
1675 /* FIXME!!! We walk right off the end of buf if we get a long line!!! */
1676 char buf[4096]; /* Should be reasonable??? */
1677 register char *p = buf;
1686 while (c != '\n' && (c = getc (stream)) >= 0);
1688 #ifdef CRLF_SOURCE_FILES
1689 /* Remove the \r, if any, at the end of the line, otherwise
1690 regular expressions that end with $ or \n won't work. */
1691 if (p - buf > 1 && p[-2] == '\r')
1698 /* We now have a source line in buf; null terminate and match. */
1700 if (re_exec (buf) > 0)
1705 print_source_lines_base (current_source_symtab, line, line + 1, 0);
1706 print_source_lines (current_source_symtab, line, line + 1, 0);
1707 set_internalvar (lookup_internalvar ("_"),
1708 value_from_longest (builtin_type_int,
1710 current_source_line = max (line - lines_to_list / 2, 1);
1714 if (fseek (stream, current_source_symtab->line_charpos[line - 1], 0) < 0)
1717 perror_with_name (current_source_symtab->filename);
1721 printf_filtered ("Expression not found\n");
1727 _initialize_source ()
1729 struct cmd_list_element *c;
1730 current_source_symtab = 0;
1731 init_source_path ();
1733 /* The intention is to use POSIX Basic Regular Expressions.
1734 Always use the GNU regex routine for consistency across all hosts.
1735 Our current GNU regex.c does not have all the POSIX features, so this is
1736 just an approximation. */
1737 re_set_syntax (RE_SYNTAX_GREP);
1739 c = add_cmd ("directory", class_files, directory_command,
1740 "Add directory DIR to beginning of search path for source files.\n\
1741 Forget cached info on source file locations and line positions.\n\
1742 DIR can also be $cwd for the current working directory, or $cdir for the\n\
1743 directory in which the source file was compiled into object code.\n\
1744 With no argument, reset the search path to $cdir:$cwd, the default.",
1748 add_com_alias ("use", "directory", class_files, 0);
1750 c->completer = filename_completer;
1752 add_cmd ("directories", no_class, show_directories,
1753 "Current search path for finding source files.\n\
1754 $cwd in the path means the current working directory.\n\
1755 $cdir in the path means the compilation directory of the source file.",
1760 add_com_alias ("D", "directory", class_files, 0);
1761 add_cmd ("ld", no_class, show_directories,
1762 "Current search path for finding source files.\n\
1763 $cwd in the path means the current working directory.\n\
1764 $cdir in the path means the compilation directory of the source file.",
1768 add_info ("source", source_info,
1769 "Information about the current source file.");
1771 add_info ("line", line_info,
1772 concat ("Core addresses of the code for a source line.\n\
1773 Line can be specified as\n\
1774 LINENUM, to list around that line in current file,\n\
1775 FILE:LINENUM, to list around that line in that file,\n\
1776 FUNCTION, to list around beginning of that function,\n\
1777 FILE:FUNCTION, to distinguish among like-named static functions.\n\
1779 Default is to describe the last source line that was listed.\n\n\
1780 This sets the default address for \"x\" to the line's first instruction\n\
1781 so that \"x/i\" suffices to start examining the machine code.\n\
1782 The address is also stored as the value of \"$_\".", NULL));
1784 add_com ("forward-search", class_files, forward_search_command,
1785 "Search for regular expression (see regex(3)) from last line listed.\n\
1786 The matching line number is also stored as the value of \"$_\".");
1787 add_com_alias ("search", "forward-search", class_files, 0);
1789 add_com ("reverse-search", class_files, reverse_search_command,
1790 "Search backward for regular expression (see regex(3)) from last line listed.\n\
1791 The matching line number is also stored as the value of \"$_\".");
1795 add_com_alias ("/", "forward-search", class_files, 0);
1796 add_com_alias ("?", "reverse-search", class_files, 0);
1799 add_com ("list", class_files, list_command,
1800 concat ("List specified function or line.\n\
1801 With no argument, lists ten more lines after or around previous listing.\n\
1802 \"list -\" lists the ten lines before a previous ten-line listing.\n\
1803 One argument specifies a line, and ten lines are listed around that line.\n\
1804 Two arguments with comma between specify starting and ending lines to list.\n\
1806 Lines can be specified in these ways:\n\
1807 LINENUM, to list around that line in current file,\n\
1808 FILE:LINENUM, to list around that line in that file,\n\
1809 FUNCTION, to list around beginning of that function,\n\
1810 FILE:FUNCTION, to distinguish among like-named static functions.\n\
1811 *ADDRESS, to list around the line containing that address.\n\
1812 With two args if one is empty it stands for ten lines away from the other arg.", NULL));
1815 add_com_alias ("l", "list", class_files, 1);
1817 add_com_alias ("v", "list", class_files, 1);
1820 add_com_alias ("file", "list", class_files, 1);
1823 (add_set_cmd ("listsize", class_support, var_uinteger,
1824 (char *) &lines_to_list,
1825 "Set number of source lines gdb will list by default.",