1 /* List lines of source files for GDB, the GNU debugger.
2 Copyright 1986, 87, 88, 89, 91, 92, 93, 94, 95, 96, 97, 1998
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
23 #include "expression.h"
30 #include <sys/types.h>
31 #include "gdb_string.h"
38 #include "gnu-regex.h"
44 #ifdef CRLF_SOURCE_FILES
46 /* Define CRLF_SOURCE_FILES in an xm-*.h file if source files on the
47 host use \r\n rather than just \n. Defining CRLF_SOURCE_FILES is
48 much faster than defining LSEEK_NOT_LINEAR. */
54 #define OPEN_MODE (O_RDONLY | O_BINARY)
55 #define FDOPEN_MODE FOPEN_RB
57 #else /* ! defined (CRLF_SOURCE_FILES) */
59 #define OPEN_MODE O_RDONLY
60 #define FDOPEN_MODE FOPEN_RT
62 #endif /* ! defined (CRLF_SOURCE_FILES) */
64 /* Forward declarations */
66 int open_source_file PARAMS ((struct symtab *));
68 void find_source_lines PARAMS ((struct symtab *, int));
70 /* Prototypes for exported functions. */
72 void _initialize_source PARAMS ((void));
74 /* Prototypes for local functions. */
76 static int get_filename_and_charpos PARAMS ((struct symtab *, char **));
78 static void reverse_search_command PARAMS ((char *, int));
80 static void forward_search_command PARAMS ((char *, int));
82 static void line_info PARAMS ((char *, int));
84 static void list_command PARAMS ((char *, int));
86 static void ambiguous_line_spec PARAMS ((struct symtabs_and_lines *));
88 static void source_info PARAMS ((char *, int));
90 static void show_directories PARAMS ((char *, int));
92 /* Path of directories to search for source files.
93 Same format as the PATH environment variable's value. */
97 /* Symtab of default file for listing lines of. */
99 struct symtab *current_source_symtab;
101 /* Default next line to list. */
103 int current_source_line;
105 /* Default number of lines to print with commands like "list".
106 This is based on guessing how many long (i.e. more than chars_per_line
107 characters) lines there will be. To be completely correct, "list"
108 and friends should be rewritten to count characters and see where
109 things are wrapping, but that would be a fair amount of work. */
111 int lines_to_list = 10;
113 /* Line number of last line printed. Default for various commands.
114 current_source_line is usually, but not always, the same as this. */
116 static int last_line_listed;
118 /* First line number listed by last listing command. */
120 static int first_line_listed;
122 /* Saves the name of the last source file visited and a possible error code.
123 Used to prevent repeating annoying "No such file or directories" msgs */
125 static struct symtab *last_source_visited = NULL;
126 static int last_source_error = 0;
129 /* Set the source file default for the "list" command to be S.
131 If S is NULL, and we don't have a default, find one. This
132 should only be called when the user actually tries to use the
133 default, since we produce an error if we can't find a reasonable
134 default. Also, since this can cause symbols to be read, doing it
135 before we need to would make things slower than necessary. */
138 select_source_symtab (s)
139 register struct symtab *s;
141 struct symtabs_and_lines sals;
142 struct symtab_and_line sal;
143 struct partial_symtab *ps;
144 struct partial_symtab *cs_pst = 0;
149 current_source_symtab = s;
150 current_source_line = 1;
154 if (current_source_symtab)
157 /* Make the default place to list be the function `main'
159 if (lookup_symbol ("main", 0, VAR_NAMESPACE, 0, NULL))
161 sals = decode_line_spec ("main", 1);
164 current_source_symtab = sal.symtab;
165 current_source_line = max (sal.line - (lines_to_list - 1), 1);
166 if (current_source_symtab)
170 /* All right; find the last file in the symtab list (ignoring .h's). */
172 current_source_line = 1;
174 for (ofp = object_files; ofp != NULL; ofp = ofp -> next)
176 for (s = ofp -> symtabs; s; s = s->next)
178 char *name = s -> filename;
179 int len = strlen (name);
180 if (! (len > 2 && (STREQ (&name[len - 2], ".h"))))
182 current_source_symtab = s;
186 if (current_source_symtab)
189 /* Howabout the partial symbol tables? */
191 for (ofp = object_files; ofp != NULL; ofp = ofp -> next)
193 for (ps = ofp -> psymtabs; ps != NULL; ps = ps -> next)
195 char *name = ps -> filename;
196 int len = strlen (name);
197 if (! (len > 2 && (STREQ (&name[len - 2], ".h"))))
205 if (cs_pst -> readin)
207 fatal ("Internal: select_source_symtab: readin pst found and no symtabs.");
211 current_source_symtab = PSYMTAB_TO_SYMTAB (cs_pst);
214 if (current_source_symtab)
217 error ("Can't find a default source file");
221 show_directories (ignore, from_tty)
225 puts_filtered ("Source directories searched: ");
226 puts_filtered (source_path);
227 puts_filtered ("\n");
230 /* Forget what we learned about line positions in source files, and
231 which directories contain them; must check again now since files
232 may be found in a different directory now. */
235 forget_cached_source_info ()
237 register struct symtab *s;
238 register struct objfile *objfile;
240 for (objfile = object_files; objfile != NULL; objfile = objfile -> next)
242 for (s = objfile -> symtabs; s != NULL; s = s -> next)
244 if (s -> line_charpos != NULL)
246 mfree (objfile -> md, s -> line_charpos);
247 s -> line_charpos = NULL;
249 if (s -> fullname != NULL)
251 mfree (objfile -> md, s -> fullname);
252 s -> fullname = NULL;
263 sprintf (buf, "$cdir%c$cwd", DIRNAME_SEPARATOR);
264 source_path = strsave (buf);
265 forget_cached_source_info ();
268 /* Add zero or more directories to the front of the source path. */
271 directory_command (dirname, from_tty)
276 /* FIXME, this goes to "delete dir"... */
279 if (query ("Reinitialize source path to empty? "))
287 mod_path (dirname, &source_path);
288 last_source_visited = NULL;
291 show_directories ((char *)0, from_tty);
292 forget_cached_source_info ();
295 /* Add zero or more directories to the front of an arbitrary path. */
298 mod_path (dirname, which_path)
302 char *old = *which_path;
308 dirname = strsave (dirname);
309 make_cleanup (free, dirname);
313 char *name = dirname;
318 char *separator = strchr (name, DIRNAME_SEPARATOR);
319 char *space = strchr (name, ' ');
320 char *tab = strchr (name, '\t');
322 if (separator == 0 && space == 0 && tab == 0)
323 p = dirname = name + strlen (name);
327 if (separator != 0 && (p == 0 || separator < p))
329 if (space != 0 && (p == 0 || space < p))
331 if (tab != 0 && (p == 0 || tab < p))
334 while (*dirname == DIRNAME_SEPARATOR
342 /* On win32 h:\ is different to h: */
344 /* Sigh. "foo/" => "foo" */
353 /* "." => getwd (). */
354 name = current_directory;
357 else if (SLASH_P (p[-2]))
367 /* "...foo/." => "...foo". */
378 name = tilde_expand (name);
379 else if (!ROOTED_P (name) && name[0] != '$')
380 name = concat (current_directory, SLASH_STRING, name, NULL);
382 name = savestring (name, p - name);
383 make_cleanup (free, name);
385 /* Unless it's a variable, check existence. */
386 if (name[0] != '$') {
387 /* These are warnings, not errors, since we don't want a
388 non-existent directory in a .gdbinit file to stop processing
389 of the .gdbinit file.
391 Whether they get added to the path is more debatable. Current
392 answer is yes, in case the user wants to go make the directory
393 or whatever. If the directory continues to not exist/not be
394 a directory/etc, then having them in the path should be
396 if (stat (name, &st) < 0)
398 int save_errno = errno;
399 fprintf_unfiltered (gdb_stderr, "Warning: ");
400 print_sys_errmsg (name, save_errno);
402 else if ((st.st_mode & S_IFMT) != S_IFDIR)
403 warning ("%s is not a directory.", name);
408 register unsigned int len = strlen (name);
413 if (!strncmp (p, name, len)
414 && (p[len] == '\0' || p[len] == DIRNAME_SEPARATOR))
416 /* Found it in the search path, remove old copy */
418 p--; /* Back over leading separator */
419 if (prefix > p - *which_path)
420 goto skip_dup; /* Same dir twice in one cmd */
421 strcpy (p, &p[len+1]); /* Copy from next \0 or : */
423 p = strchr (p, DIRNAME_SEPARATOR);
433 tinybuf[0] = DIRNAME_SEPARATOR;
436 /* 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. */
443 temp = concat (old, tinybuf, name, NULL);
445 *which_path = concat (temp, "", &old[prefix], NULL);
446 prefix = strlen (temp);
451 *which_path = concat (name, (old[0] ? tinybuf : old), old, NULL);
452 prefix = strlen (name);
459 } while (*dirname != '\0');
464 source_info (ignore, from_tty)
468 register struct symtab *s = current_source_symtab;
472 printf_filtered("No current source file.\n");
475 printf_filtered ("Current source file is %s\n", s->filename);
477 printf_filtered ("Compilation directory is %s\n", s->dirname);
479 printf_filtered ("Located in %s\n", s->fullname);
481 printf_filtered ("Contains %d line%s.\n", s->nlines,
482 s->nlines == 1 ? "" : "s");
484 printf_filtered ("Source language is %s.\n", language_str (s->language));
485 printf_filtered ("Compiled with %s debugging format.\n", s->debugformat);
490 /* Open a file named STRING, searching path PATH (dir names sep by some char)
491 using mode MODE and protection bits PROT in the calls to open.
493 If TRY_CWD_FIRST, try to open ./STRING before searching PATH.
494 (ie pretend the first element of PATH is "."). This also indicates
495 that a slash in STRING disables searching of the path (this is
496 so that "exec-file ./foo" or "symbol-file ./foo" insures that you
497 get that particular version of foo or an error message).
499 If FILENAMED_OPENED is non-null, set it to a newly allocated string naming
500 the actual file opened (this string will always start with a "/". We
501 have to take special pains to avoid doubling the "/" between the directory
502 and the file, sigh! Emacs gets confuzzed by this when we print the
505 If a file is found, return the descriptor.
506 Otherwise, return -1, with errno set for the last name we tried to open. */
508 /* >>>> This should only allow files of certain types,
509 >>>> eg executable, non-directory */
511 openp (path, try_cwd_first, string, mode, prot, filename_opened)
517 char **filename_opened;
520 register char *filename;
521 register char *p, *p1;
532 if (try_cwd_first || ROOTED_P (string))
536 fd = open (filename, mode, prot);
539 for (i = 0; string[i]; i++)
540 if (SLASH_P (string[i]))
545 while (string[0] == '.' && SLASH_P (string[1]))
548 alloclen = strlen (path) + strlen (string) + 2;
549 filename = (char *) alloca (alloclen);
551 for (p = path; p; p = p1 ? p1 + 1 : 0)
553 p1 = (char *) strchr (p, DIRNAME_SEPARATOR);
559 if (len == 4 && p[0] == '$' && p[1] == 'c'
560 && p[2] == 'w' && p[3] == 'd') {
561 /* Name is $cwd -- insert current directory name instead. */
564 /* First, realloc the filename buffer if too short. */
565 len = strlen (current_directory);
566 newlen = len + strlen (string) + 2;
567 if (newlen > alloclen) {
569 filename = (char *) alloca (alloclen);
571 strcpy (filename, current_directory);
573 /* Normal file name in path -- just use it. */
574 strncpy (filename, p, len);
578 /* Remove trailing slashes */
579 while (len > 0 && SLASH_P (filename[len-1]))
582 strcat (filename+len, SLASH_STRING);
583 strcat (filename, string);
585 fd = open (filename, mode);
593 *filename_opened = (char *) 0;
594 else if (ROOTED_P (filename))
595 *filename_opened = savestring (filename, strlen (filename));
598 /* Beware the // my son, the Emacs barfs, the botch that catch... */
600 *filename_opened = concat (current_directory,
602 == current_directory[strlen(current_directory)-1]
608 /* This is a debugging hack that can go away when all combinations
609 of Mac and Unix names are handled reasonably. */
611 extern int debug_openp;
615 printf("openp on %s, path %s mode %d prot %d\n returned %d",
616 string, path, mode, prot, fd);
617 if (*filename_opened)
618 printf(" (filename is %s)", *filename_opened);
628 /* This is essentially a convenience, for clients that want the behaviour
629 of openp, using source_path, but that really don't want the file to be
630 opened but want instead just to know what the full pathname is (as
631 qualified against source_path).
633 The current working directory is searched first.
635 If the file was found, this function returns 1, and FULL_PATHNAME is
636 set to the fully-qualified pathname.
638 Else, this functions returns 0, and FULL_PATHNAME is set to NULL.
641 source_full_path_of (filename, full_pathname)
643 char ** full_pathname;
647 fd = openp (source_path, 1, filename, O_RDONLY, 0, full_pathname);
650 *full_pathname = NULL;
659 /* Open a source file given a symtab S. Returns a file descriptor or
660 negative number for error. */
666 char *path = source_path;
671 /* Quick way out if we already know its full name */
674 result = open (s->fullname, OPEN_MODE);
677 /* Didn't work -- free old one, try again. */
678 mfree (s->objfile->md, s->fullname);
682 if (s->dirname != NULL)
684 /* Replace a path entry of $cdir with the compilation directory name */
686 /* We cast strstr's result in case an ANSIhole has made it const,
687 which produces a "required warning" when assigned to a nonconst. */
688 p = (char *)strstr (source_path, "$cdir");
689 if (p && (p == path || p[-1] == DIRNAME_SEPARATOR)
690 && (p[cdir_len] == DIRNAME_SEPARATOR || p[cdir_len] == '\0'))
695 alloca (strlen (source_path) + 1 + strlen (s->dirname) + 1);
696 len = p - source_path;
697 strncpy (path, source_path, len); /* Before $cdir */
698 strcpy (path + len, s->dirname); /* new stuff */
699 strcat (path + len, source_path + len + cdir_len); /* After $cdir */
703 result = openp (path, 0, s->filename, OPEN_MODE, 0, &s->fullname);
706 /* Didn't work. Try using just the basename. */
707 p = basename (s->filename);
708 if (p != s->filename)
709 result = openp (path, 0, p, OPEN_MODE, 0, &s->fullname);
714 /* Didn't work. Try using just the MPW basename. */
715 p = (char *) mpw_basename (s->filename);
716 if (p != s->filename)
717 result = openp (path, 0, p, OPEN_MODE, 0, &s->fullname);
721 /* Didn't work. Try using the mixed Unix/MPW basename. */
722 p = (char *) mpw_mixed_basename (s->filename);
723 if (p != s->filename)
724 result = openp (path, 0, p, OPEN_MODE, 0, &s->fullname);
730 fullname = s->fullname;
731 s->fullname = mstrsave (s->objfile->md, s->fullname);
737 /* Return the path to the source file associated with symtab. Returns NULL
741 symtab_to_filename (s)
749 /* If we've seen the file before, just return fullname. */
754 /* Try opening the file to setup fullname */
756 fd = open_source_file (s);
758 return s->filename; /* File not found. Just use short name */
760 /* Found the file. Cleanup and return the full name */
767 /* Create and initialize the table S->line_charpos that records
768 the positions of the lines in the source file, which is assumed
769 to be open on descriptor DESC.
770 All set S->nlines to the number of such lines. */
773 find_source_lines (s, desc)
778 register char *data, *p, *end;
780 int lines_allocated = 1000;
785 line_charpos = (int *) xmmalloc (s -> objfile -> md,
786 lines_allocated * sizeof (int));
787 if (fstat (desc, &st) < 0)
788 perror_with_name (s->filename);
790 if (s && s->objfile && s->objfile->obfd)
791 mtime = bfd_get_mtime(s->objfile->obfd);
793 mtime = bfd_get_mtime(exec_bfd);
795 if (mtime && mtime < st.st_mtime)
798 printf_filtered ("\n");
799 warning("Source file is more recent than executable.\n");
802 #ifdef LSEEK_NOT_LINEAR
806 /* Have to read it byte by byte to find out where the chars live */
808 line_charpos[0] = lseek (desc, 0, SEEK_CUR);
810 while (myread(desc, &c, 1)>0)
814 if (nlines == lines_allocated)
816 lines_allocated *= 2;
818 (int *) xmrealloc (s -> objfile -> md, (char *) line_charpos,
819 sizeof (int) * lines_allocated);
821 line_charpos[nlines++] = lseek (desc, 0, SEEK_CUR);
825 #else /* lseek linear. */
827 struct cleanup *old_cleanups;
829 /* st_size might be a large type, but we only support source files whose
830 size fits in an int. */
831 size = (int) st.st_size;
833 /* Use malloc, not alloca, because this may be pretty large, and we may
834 run into various kinds of limits on stack size. */
835 data = (char *) xmalloc (size);
836 old_cleanups = make_cleanup (free, data);
838 /* Reassign `size' to result of read for systems where \r\n -> \n. */
839 size = myread (desc, data, size);
841 perror_with_name (s->filename);
849 /* A newline at the end does not start a new line. */
852 if (nlines == lines_allocated)
854 lines_allocated *= 2;
856 (int *) xmrealloc (s -> objfile -> md, (char *) line_charpos,
857 sizeof (int) * lines_allocated);
859 line_charpos[nlines++] = p - data;
862 do_cleanups (old_cleanups);
864 #endif /* lseek linear. */
867 (int *) xmrealloc (s -> objfile -> md, (char *) line_charpos,
868 nlines * sizeof (int));
872 /* Return the character position of a line LINE in symtab S.
873 Return 0 if anything is invalid. */
875 #if 0 /* Currently unused */
878 source_line_charpos (s, line)
883 if (!s->line_charpos || line <= 0) return 0;
884 if (line > s->nlines)
886 return s->line_charpos[line - 1];
889 /* Return the line number of character position POS in symtab S. */
892 source_charpos_line (s, chr)
893 register struct symtab *s;
896 register int line = 0;
899 if (s == 0 || s->line_charpos == 0) return 0;
900 lnp = s->line_charpos;
901 /* Files are usually short, so sequential search is Ok */
902 while (line < s->nlines && *lnp <= chr)
907 if (line >= s->nlines)
915 /* Get full pathname and line number positions for a symtab.
916 Return nonzero if line numbers may have changed.
917 Set *FULLNAME to actual name of the file as found by `openp',
918 or to 0 if the file is not found. */
921 get_filename_and_charpos (s, fullname)
925 register int desc, linenums_changed = 0;
927 desc = open_source_file (s);
935 *fullname = s->fullname;
936 if (s->line_charpos == 0) linenums_changed = 1;
937 if (linenums_changed) find_source_lines (s, desc);
939 return linenums_changed;
942 /* Print text describing the full name of the source file S
943 and the line number LINE and its corresponding character position.
944 The text starts with two Ctrl-z so that the Emacs-GDB interface
947 MID_STATEMENT is nonzero if the PC is not at the beginning of that line.
949 Return 1 if successful, 0 if could not find the file. */
952 identify_source_line (s, line, mid_statement, pc)
958 if (s->line_charpos == 0)
959 get_filename_and_charpos (s, (char **)NULL);
960 if (s->fullname == 0)
962 if (line > s->nlines)
963 /* Don't index off the end of the line_charpos array. */
965 annotate_source (s->fullname, line, s->line_charpos[line - 1],
968 current_source_line = line;
969 first_line_listed = line;
970 last_line_listed = line;
971 current_source_symtab = s;
976 /* Print source lines from the file of symtab S,
977 starting with line number LINE and stopping before line number STOPLINE. */
979 static void print_source_lines_base PARAMS ((struct symtab *s, int line, int stopline, int noerror));
981 print_source_lines_base (s, line, stopline, noerror)
989 register FILE *stream;
990 int nlines = stopline - line;
992 /* Regardless of whether we can open the file, set current_source_symtab. */
993 current_source_symtab = s;
994 current_source_line = line;
995 first_line_listed = line;
997 /* Only prints "No such file or directory" once */
998 if ((s != last_source_visited) || (! last_source_error))
1000 last_source_visited = s;
1001 desc = open_source_file (s);
1005 desc = last_source_error;
1011 last_source_error = desc;
1015 char *name = alloca (strlen (s->filename) + 100);
1016 sprintf (name, "%d\t%s", line, s->filename);
1017 print_sys_errmsg (name, errno);
1020 printf_filtered ("%d\tin %s\n", line, s->filename);
1025 last_source_error = 0;
1027 if (s->line_charpos == 0)
1028 find_source_lines (s, desc);
1030 if (line < 1 || line > s->nlines)
1033 error ("Line number %d out of range; %s has %d lines.",
1034 line, s->filename, s->nlines);
1037 if (lseek (desc, s->line_charpos[line - 1], 0) < 0)
1040 perror_with_name (s->filename);
1043 stream = fdopen (desc, FDOPEN_MODE);
1046 while (nlines-- > 0)
1049 if (c == EOF) break;
1050 last_line_listed = current_source_line;
1051 printf_filtered ("%d\t", current_source_line++);
1054 if (c < 040 && c != '\t' && c != '\n' && c != '\r')
1055 printf_filtered ("^%c", c + 0100);
1057 printf_filtered ("^?");
1058 #ifdef CRLF_SOURCE_FILES
1061 /* Just skip \r characters. */
1065 printf_filtered ("%c", c);
1066 } while (c != '\n' && (c = fgetc (stream)) >= 0);
1072 /* Show source lines from the file of symtab S, starting with line
1073 number LINE and stopping before line number STOPLINE. If this is the
1074 not the command line version, then the source is shown in the source
1075 window otherwise it is simply printed */
1078 print_source_lines (s, line, stopline, noerror)
1080 int line, stopline, noerror;
1084 m_winPtrIsNull(srcWin) || !srcWin->generic.isVisible )
1085 print_source_lines_base(s, line, stopline, noerror);
1088 TuiGenWinInfoPtr locator = locatorWinInfoPtr();
1089 extern void tui_vAddWinToLayout PARAMS ((va_list));
1090 extern void tui_vUpdateSourceWindowsWithLine PARAMS ((va_list));
1092 /* Regardless of whether we can open the file,
1093 set current_source_symtab. */
1094 current_source_symtab = s;
1095 current_source_line = line;
1096 first_line_listed = line;
1098 /* make sure that the source window is displayed */
1099 tuiDo((TuiOpaqueFuncPtr)tui_vAddWinToLayout, SRC_WIN);
1101 tuiDo((TuiOpaqueFuncPtr)tui_vUpdateSourceWindowsWithLine, s, line);
1102 tuiDo((TuiOpaqueFuncPtr)tui_vUpdateLocatorFilename, s->filename);
1105 print_source_lines_base(s, line, stopline, noerror);
1111 /* Print a list of files and line numbers which a user may choose from
1112 in order to list a function which was specified ambiguously (as with
1113 `list classname::overloadedfuncname', for example). The vector in
1114 SALS provides the filenames and line numbers. */
1117 ambiguous_line_spec (sals)
1118 struct symtabs_and_lines *sals;
1122 for (i = 0; i < sals->nelts; ++i)
1123 printf_filtered("file: \"%s\", line number: %d\n",
1124 sals->sals[i].symtab->filename, sals->sals[i].line);
1128 list_command (arg, from_tty)
1132 struct symtabs_and_lines sals, sals_end;
1133 struct symtab_and_line sal, sal_end;
1139 int linenum_beg = 0;
1142 if (!have_full_symbols () && !have_partial_symbols())
1143 error ("No symbol table is loaded. Use the \"file\" command.");
1145 /* Pull in a current source symtab if necessary */
1146 if (current_source_symtab == 0 &&
1147 (arg == 0 || arg[0] == '+' || arg[0] == '-'))
1148 select_source_symtab (0);
1150 /* "l" or "l +" lists next ten lines. */
1152 if (arg == 0 || STREQ (arg, "+"))
1154 if (current_source_symtab == 0)
1155 error ("No default source file yet. Do \"help list\".");
1156 print_source_lines (current_source_symtab, current_source_line,
1157 current_source_line + lines_to_list, 0);
1161 /* "l -" lists previous ten lines, the ones before the ten just listed. */
1162 if (STREQ (arg, "-"))
1164 if (current_source_symtab == 0)
1165 error ("No default source file yet. Do \"help list\".");
1166 print_source_lines (current_source_symtab,
1167 max (first_line_listed - lines_to_list, 1),
1168 first_line_listed, 0);
1172 /* Now if there is only one argument, decode it in SAL
1174 If there are two arguments, decode them in SAL and SAL_END
1175 and clear NO_END; however, if one of the arguments is blank,
1176 set DUMMY_BEG or DUMMY_END to record that fact. */
1183 sals = decode_line_1 (&arg1, 0, 0, 0, 0);
1185 if (! sals.nelts) return; /* C++ */
1188 ambiguous_line_spec (&sals);
1197 /* Record whether the BEG arg is all digits. */
1199 for (p = arg; p != arg1 && *p >= '0' && *p <= '9'; p++);
1200 linenum_beg = (p == arg1);
1202 while (*arg1 == ' ' || *arg1 == '\t')
1208 while (*arg1 == ' ' || *arg1 == '\t')
1215 sals_end = decode_line_1 (&arg1, 0, 0, 0, 0);
1217 sals_end = decode_line_1 (&arg1, 0, sal.symtab, sal.line, 0);
1218 if (sals_end.nelts == 0)
1220 if (sals_end.nelts > 1)
1222 ambiguous_line_spec (&sals_end);
1223 free (sals_end.sals);
1226 sal_end = sals_end.sals[0];
1227 free (sals_end.sals);
1232 error ("Junk at end of line specification.");
1234 if (!no_end && !dummy_beg && !dummy_end
1235 && sal.symtab != sal_end.symtab)
1236 error ("Specified start and end are in different files.");
1237 if (dummy_beg && dummy_end)
1238 error ("Two empty args do not say what lines to list.");
1240 /* if line was specified by address,
1241 first print exactly which line, and which file.
1242 In this case, sal.symtab == 0 means address is outside
1243 of all known source files, not that user failed to give a filename. */
1246 if (sal.symtab == 0)
1247 /* FIXME-32x64--assumes sal.pc fits in long. */
1248 error ("No source file for address %s.",
1249 local_hex_string((unsigned long) sal.pc));
1250 sym = find_pc_function (sal.pc);
1253 print_address_numeric (sal.pc, 1, gdb_stdout);
1254 printf_filtered (" is in ");
1255 fputs_filtered (SYMBOL_SOURCE_NAME (sym), gdb_stdout);
1256 printf_filtered (" (%s:%d).\n", sal.symtab->filename, sal.line);
1260 print_address_numeric (sal.pc, 1, gdb_stdout);
1261 printf_filtered (" is at %s:%d.\n",
1262 sal.symtab->filename, sal.line);
1266 /* If line was not specified by just a line number,
1267 and it does not imply a symtab, it must be an undebuggable symbol
1268 which means no source code. */
1270 if (! linenum_beg && sal.symtab == 0)
1271 error ("No line number known for %s.", arg);
1273 /* If this command is repeated with RET,
1274 turn it into the no-arg variant. */
1279 if (dummy_beg && sal_end.symtab == 0)
1280 error ("No default source file yet. Do \"help list\".");
1282 print_source_lines (sal_end.symtab,
1283 max (sal_end.line - (lines_to_list - 1), 1),
1284 sal_end.line + 1, 0);
1285 else if (sal.symtab == 0)
1286 error ("No default source file yet. Do \"help list\".");
1289 if (lines_to_list % 2 == 0)
1290 print_source_lines (sal.symtab,
1291 max (sal.line - (lines_to_list / 2), 1),
1292 sal.line + (lines_to_list / 2), 0);
1294 /* If lines_to_list is odd, then we round down in
1295 * one of the lines_to_list/2 computations, round up in
1296 * the other, so the total window size around the specified
1297 * line comes out right.
1299 print_source_lines (sal.symtab,
1300 max (sal.line - (lines_to_list / 2), 1),
1301 sal.line + ((1+lines_to_list) / 2), 0);
1304 print_source_lines (sal.symtab, sal.line,
1306 ? sal.line + lines_to_list
1307 : sal_end.line + 1),
1311 /* Print info on range of pc's in a specified line. */
1314 line_info (arg, from_tty)
1318 struct symtabs_and_lines sals;
1319 struct symtab_and_line sal;
1320 CORE_ADDR start_pc, end_pc;
1323 INIT_SAL (&sal); /* initialize to zeroes */
1327 sal.symtab = current_source_symtab;
1328 sal.line = last_line_listed;
1330 sals.sals = (struct symtab_and_line *)
1331 xmalloc (sizeof (struct symtab_and_line));
1336 sals = decode_line_spec_1 (arg, 0);
1341 /* C++ More than one line may have been specified, as when the user
1342 specifies an overloaded function name. Print info on them all. */
1343 for (i = 0; i < sals.nelts; i++)
1347 if (sal.symtab == 0)
1349 printf_filtered ("No line number information available");
1352 /* This is useful for "info line *0x7f34". If we can't tell the
1353 user about a source line, at least let them have the symbolic
1355 printf_filtered (" for address ");
1357 print_address (sal.pc, gdb_stdout);
1360 printf_filtered (".");
1361 printf_filtered ("\n");
1363 else if (sal.line > 0
1364 && find_line_pc_range (sal, &start_pc, &end_pc))
1366 if (start_pc == end_pc)
1368 printf_filtered ("Line %d of \"%s\"",
1369 sal.line, sal.symtab->filename);
1371 printf_filtered (" is at address ");
1372 print_address (start_pc, gdb_stdout);
1374 printf_filtered (" but contains no code.\n");
1378 printf_filtered ("Line %d of \"%s\"",
1379 sal.line, sal.symtab->filename);
1381 printf_filtered (" starts at address ");
1382 print_address (start_pc, gdb_stdout);
1384 printf_filtered (" and ends at ");
1385 print_address (end_pc, gdb_stdout);
1386 printf_filtered (".\n");
1389 /* x/i should display this line's code. */
1390 set_next_address (start_pc);
1392 /* Repeating "info line" should do the following line. */
1393 last_line_listed = sal.line + 1;
1395 /* If this is the only line, show the source code. If it could
1396 not find the file, don't do anything special. */
1397 if (annotation_level && sals.nelts == 1)
1398 identify_source_line (sal.symtab, sal.line, 0, start_pc);
1401 /* Is there any case in which we get here, and have an address
1402 which the user would want to see? If we have debugging symbols
1403 and no line numbers? */
1404 printf_filtered ("Line number %d is out of range for \"%s\".\n",
1405 sal.line, sal.symtab->filename);
1410 /* Commands to search the source file for a regexp. */
1414 forward_search_command (regex, from_tty)
1420 register FILE *stream;
1426 ** If this is the TUI, search from the first line displayed in
1427 ** the source window, otherwise, search from last_line_listed+1
1428 ** in current_source_symtab
1431 line = last_line_listed;
1434 if (srcWin->generic.isVisible && srcWin->generic.contentSize > 0)
1435 line = ((TuiWinContent)
1436 srcWin->generic.content)[0]->whichElement.source.lineOrAddr.lineNo;
1439 printf_filtered("No source displayed.\nExpression not found.\n");
1445 line = last_line_listed + 1;
1448 msg = (char *) re_comp (regex);
1452 if (current_source_symtab == 0)
1453 select_source_symtab (0);
1455 desc = open_source_file (current_source_symtab);
1457 perror_with_name (current_source_symtab->filename);
1459 if (current_source_symtab->line_charpos == 0)
1460 find_source_lines (current_source_symtab, desc);
1462 if (line < 1 || line > current_source_symtab->nlines)
1465 error ("Expression not found");
1468 if (lseek (desc, current_source_symtab->line_charpos[line - 1], 0) < 0)
1471 perror_with_name (current_source_symtab->filename);
1474 stream = fdopen (desc, FDOPEN_MODE);
1477 static char *buf = NULL;
1479 int cursize, newsize;
1482 buf = xmalloc (cursize);
1490 if (p - buf == cursize)
1492 newsize = cursize + cursize / 2;
1493 buf = xrealloc (buf, newsize);
1497 } while (c != '\n' && (c = getc (stream)) >= 0);
1499 /* we now have a source line in buf, null terminate and match */
1501 if (re_exec (buf) > 0)
1506 print_source_lines_base (current_source_symtab, line, line+1, 0);
1507 print_source_lines (current_source_symtab, line, line+1, 0);
1508 set_internalvar (lookup_internalvar ("_"),
1509 value_from_longest (builtin_type_int,
1511 current_source_line = max (line - lines_to_list / 2, 1);
1517 printf_filtered ("Expression not found\n");
1523 reverse_search_command (regex, from_tty)
1529 register FILE *stream;
1534 ** If this is the TUI, search from the first line displayed in
1535 ** the source window, otherwise, search from last_line_listed-1
1536 ** in current_source_symtab
1539 line = last_line_listed;
1542 if (srcWin->generic.isVisible && srcWin->generic.contentSize > 0)
1543 line = ((TuiWinContent)
1544 srcWin->generic.content)[0]->whichElement.source.lineOrAddr.lineNo;
1547 printf_filtered("No source displayed.\nExpression not found.\n");
1553 line = last_line_listed - 1;
1556 msg = (char *) re_comp (regex);
1560 if (current_source_symtab == 0)
1561 select_source_symtab (0);
1563 desc = open_source_file (current_source_symtab);
1565 perror_with_name (current_source_symtab->filename);
1567 if (current_source_symtab->line_charpos == 0)
1568 find_source_lines (current_source_symtab, desc);
1570 if (line < 1 || line > current_source_symtab->nlines)
1573 error ("Expression not found");
1576 if (lseek (desc, current_source_symtab->line_charpos[line - 1], 0) < 0)
1579 perror_with_name (current_source_symtab->filename);
1582 stream = fdopen (desc, FDOPEN_MODE);
1586 /* FIXME!!! We walk right off the end of buf if we get a long line!!! */
1587 char buf[4096]; /* Should be reasonable??? */
1588 register char *p = buf;
1595 } while (c != '\n' && (c = getc (stream)) >= 0);
1597 /* We now have a source line in buf; null terminate and match. */
1599 if (re_exec (buf) > 0)
1604 print_source_lines_base (current_source_symtab, line, line+1, 0);
1605 print_source_lines (current_source_symtab, line, line+1, 0);
1606 set_internalvar (lookup_internalvar ("_"),
1607 value_from_longest (builtin_type_int,
1609 current_source_line = max (line - lines_to_list / 2, 1);
1613 if (fseek (stream, current_source_symtab->line_charpos[line - 1], 0) < 0)
1616 perror_with_name (current_source_symtab->filename);
1620 printf_filtered ("Expression not found\n");
1626 _initialize_source ()
1628 struct cmd_list_element *c;
1629 current_source_symtab = 0;
1630 init_source_path ();
1632 /* The intention is to use POSIX Basic Regular Expressions.
1633 Always use the GNU regex routine for consistency across all hosts.
1634 Our current GNU regex.c does not have all the POSIX features, so this is
1635 just an approximation. */
1636 re_set_syntax (RE_SYNTAX_GREP);
1638 c = add_cmd ("directory", class_files, directory_command,
1639 "Add directory DIR to beginning of search path for source files.\n\
1640 Forget cached info on source file locations and line positions.\n\
1641 DIR can also be $cwd for the current working directory, or $cdir for the\n\
1642 directory in which the source file was compiled into object code.\n\
1643 With no argument, reset the search path to $cdir:$cwd, the default.",
1647 add_com_alias("use", "directory", class_files, 0);
1649 c->completer = filename_completer;
1651 add_cmd ("directories", no_class, show_directories,
1652 "Current search path for finding source files.\n\
1653 $cwd in the path means the current working directory.\n\
1654 $cdir in the path means the compilation directory of the source file.",
1659 add_com_alias("D", "directory", class_files, 0);
1660 add_cmd ("ld", no_class, show_directories,
1661 "Current search path for finding source files.\n\
1662 $cwd in the path means the current working directory.\n\
1663 $cdir in the path means the compilation directory of the source file.",
1667 add_info ("source", source_info,
1668 "Information about the current source file.");
1670 add_info ("line", line_info,
1671 concat ("Core addresses of the code for a source line.\n\
1672 Line can be specified as\n\
1673 LINENUM, to list around that line in current file,\n\
1674 FILE:LINENUM, to list around that line in that file,\n\
1675 FUNCTION, to list around beginning of that function,\n\
1676 FILE:FUNCTION, to distinguish among like-named static functions.\n\
1678 Default is to describe the last source line that was listed.\n\n\
1679 This sets the default address for \"x\" to the line's first instruction\n\
1680 so that \"x/i\" suffices to start examining the machine code.\n\
1681 The address is also stored as the value of \"$_\".", NULL));
1683 add_com ("forward-search", class_files, forward_search_command,
1684 "Search for regular expression (see regex(3)) from last line listed.\n\
1685 The matching line number is also stored as the value of \"$_\".");
1686 add_com_alias ("search", "forward-search", class_files, 0);
1688 add_com ("reverse-search", class_files, reverse_search_command,
1689 "Search backward for regular expression (see regex(3)) from last line listed.\n\
1690 The matching line number is also stored as the value of \"$_\".");
1694 add_com_alias("/", "forward-search", class_files, 0);
1695 add_com_alias("?", "reverse-search", class_files, 0);
1698 add_com ("list", class_files, list_command,
1699 concat ("List specified function or line.\n\
1700 With no argument, lists ten more lines after or around previous listing.\n\
1701 \"list -\" lists the ten lines before a previous ten-line listing.\n\
1702 One argument specifies a line, and ten lines are listed around that line.\n\
1703 Two arguments with comma between specify starting and ending lines to list.\n\
1705 Lines can be specified in these ways:\n\
1706 LINENUM, to list around that line in current file,\n\
1707 FILE:LINENUM, to list around that line in that file,\n\
1708 FUNCTION, to list around beginning of that function,\n\
1709 FILE:FUNCTION, to distinguish among like-named static functions.\n\
1710 *ADDRESS, to list around the line containing that address.\n\
1711 With two args if one is empty it stands for ten lines away from the other arg.", NULL));
1714 add_com_alias ("l", "list", class_files, 1);
1716 add_com_alias ("v", "list", class_files, 1);
1719 add_com_alias ("file", "list", class_files, 1);
1722 (add_set_cmd ("listsize", class_support, var_uinteger,
1723 (char *)&lines_to_list,
1724 "Set number of source lines gdb will list by default.",