1 /* List lines of source files for GDB, the GNU debugger.
2 Copyright 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994,
3 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002
4 Free Software Foundation, Inc.
6 This file is part of GDB.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
25 #include "expression.h"
33 #include <sys/types.h>
34 #include "gdb_string.h"
38 #include "gdb_regex.h"
44 #include "filenames.h" /* for DOSish file names */
45 #include "completer.h"
50 #ifdef CRLF_SOURCE_FILES
52 /* Define CRLF_SOURCE_FILES in an xm-*.h file if source files on the
53 host use \r\n rather than just \n. Defining CRLF_SOURCE_FILES is
54 much faster than defining LSEEK_NOT_LINEAR. */
60 #define OPEN_MODE (O_RDONLY | O_BINARY)
61 #define FDOPEN_MODE FOPEN_RB
63 #else /* ! defined (CRLF_SOURCE_FILES) */
65 #define OPEN_MODE O_RDONLY
66 #define FDOPEN_MODE FOPEN_RT
68 #endif /* ! defined (CRLF_SOURCE_FILES) */
70 /* Prototypes for exported functions. */
72 void _initialize_source (void);
74 /* Prototypes for local functions. */
76 static int get_filename_and_charpos (struct symtab
*, char **);
78 static void reverse_search_command (char *, int);
80 static void forward_search_command (char *, int);
82 static void line_info (char *, int);
84 static void list_command (char *, int);
86 static void ambiguous_line_spec (struct symtabs_and_lines
*);
88 static void source_info (char *, int);
90 static void show_directories (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 (register struct symtab
*s
)
140 struct symtabs_and_lines sals
;
141 struct symtab_and_line sal
;
142 struct partial_symtab
*ps
;
143 struct partial_symtab
*cs_pst
= 0;
148 current_source_symtab
= s
;
149 current_source_line
= 1;
153 if (current_source_symtab
)
156 /* Make the default place to list be the function `main'
158 if (lookup_symbol (main_name (), 0, VAR_NAMESPACE
, 0, NULL
))
160 sals
= decode_line_spec (main_name (), 1);
163 current_source_symtab
= sal
.symtab
;
164 current_source_line
= max (sal
.line
- (lines_to_list
- 1), 1);
165 if (current_source_symtab
)
169 /* All right; find the last file in the symtab list (ignoring .h's). */
171 current_source_line
= 1;
173 for (ofp
= object_files
; ofp
!= NULL
; ofp
= ofp
->next
)
175 for (s
= ofp
->symtabs
; s
; s
= s
->next
)
177 char *name
= s
->filename
;
178 int len
= strlen (name
);
179 if (!(len
> 2 && (STREQ (&name
[len
- 2], ".h"))))
181 current_source_symtab
= s
;
185 if (current_source_symtab
)
188 /* Howabout the partial symbol tables? */
190 for (ofp
= object_files
; ofp
!= NULL
; ofp
= ofp
->next
)
192 for (ps
= ofp
->psymtabs
; ps
!= NULL
; ps
= ps
->next
)
194 char *name
= ps
->filename
;
195 int len
= strlen (name
);
196 if (!(len
> 2 && (STREQ (&name
[len
- 2], ".h"))))
206 internal_error (__FILE__
, __LINE__
,
207 "select_source_symtab: "
208 "readin pst found and no symtabs.");
212 current_source_symtab
= PSYMTAB_TO_SYMTAB (cs_pst
);
215 if (current_source_symtab
)
218 error ("Can't find a default source file");
222 show_directories (char *ignore
, int from_tty
)
224 puts_filtered ("Source directories searched: ");
225 puts_filtered (source_path
);
226 puts_filtered ("\n");
229 /* Forget what we learned about line positions in source files, and
230 which directories contain them; must check again now since files
231 may be found in a different directory now. */
234 forget_cached_source_info (void)
236 register struct symtab
*s
;
237 register struct objfile
*objfile
;
238 struct partial_symtab
*pst
;
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 xmfree (objfile
->md
, s
->line_charpos
);
247 s
->line_charpos
= NULL
;
249 if (s
->fullname
!= NULL
)
251 xmfree (objfile
->md
, s
->fullname
);
256 ALL_OBJFILE_PSYMTABS (objfile
, pst
)
258 if (pst
->fullname
!= NULL
)
260 xfree (pst
->fullname
);
261 pst
->fullname
= NULL
;
268 init_source_path (void)
272 sprintf (buf
, "$cdir%c$cwd", DIRNAME_SEPARATOR
);
273 source_path
= xstrdup (buf
);
274 forget_cached_source_info ();
277 /* Add zero or more directories to the front of the source path. */
280 directory_command (char *dirname
, int from_tty
)
283 /* FIXME, this goes to "delete dir"... */
286 if (from_tty
&& query ("Reinitialize source path to empty? "))
294 mod_path (dirname
, &source_path
);
295 last_source_visited
= NULL
;
298 show_directories ((char *) 0, from_tty
);
299 forget_cached_source_info ();
302 /* Add zero or more directories to the front of an arbitrary path. */
305 mod_path (char *dirname
, char **which_path
)
307 char *old
= *which_path
;
313 dirname
= xstrdup (dirname
);
314 make_cleanup (xfree
, dirname
);
318 char *name
= dirname
;
323 char *separator
= strchr (name
, DIRNAME_SEPARATOR
);
324 char *space
= strchr (name
, ' ');
325 char *tab
= strchr (name
, '\t');
327 if (separator
== 0 && space
== 0 && tab
== 0)
328 p
= dirname
= name
+ strlen (name
);
332 if (separator
!= 0 && (p
== 0 || separator
< p
))
334 if (space
!= 0 && (p
== 0 || space
< p
))
336 if (tab
!= 0 && (p
== 0 || tab
< p
))
339 while (*dirname
== DIRNAME_SEPARATOR
346 if (!(IS_DIR_SEPARATOR (*name
) && p
<= name
+ 1) /* "/" */
347 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
348 /* On MS-DOS and MS-Windows, h:\ is different from h: */
349 && !(p
== name
+ 3 && name
[1] == ':') /* "d:/" */
351 && IS_DIR_SEPARATOR (p
[-1]))
352 /* Sigh. "foo/" => "foo" */
356 while (p
> name
&& p
[-1] == '.')
360 /* "." => getwd (). */
361 name
= current_directory
;
364 else if (p
> name
+ 1 && IS_DIR_SEPARATOR (p
[-2]))
374 /* "...foo/." => "...foo". */
385 name
= tilde_expand (name
);
386 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
387 else if (IS_ABSOLUTE_PATH (name
) && p
== name
+ 2) /* "d:" => "d:." */
388 name
= concat (name
, ".", NULL
);
390 else if (!IS_ABSOLUTE_PATH (name
) && name
[0] != '$')
391 name
= concat (current_directory
, SLASH_STRING
, name
, NULL
);
393 name
= savestring (name
, p
- name
);
394 make_cleanup (xfree
, name
);
396 /* Unless it's a variable, check existence. */
399 /* These are warnings, not errors, since we don't want a
400 non-existent directory in a .gdbinit file to stop processing
401 of the .gdbinit file.
403 Whether they get added to the path is more debatable. Current
404 answer is yes, in case the user wants to go make the directory
405 or whatever. If the directory continues to not exist/not be
406 a directory/etc, then having them in the path should be
408 if (stat (name
, &st
) < 0)
410 int save_errno
= errno
;
411 fprintf_unfiltered (gdb_stderr
, "Warning: ");
412 print_sys_errmsg (name
, save_errno
);
414 else if ((st
.st_mode
& S_IFMT
) != S_IFDIR
)
415 warning ("%s is not a directory.", name
);
420 register unsigned int len
= strlen (name
);
425 /* FIXME: strncmp loses in interesting ways on MS-DOS and
426 MS-Windows because of case-insensitivity and two different
427 but functionally identical slash characters. We need a
428 special filesystem-dependent file-name comparison function.
430 Actually, even on Unix I would use realpath() or its work-
431 alike before comparing. Then all the code above which
432 removes excess slashes and dots could simply go away. */
433 if (!strncmp (p
, name
, len
)
434 && (p
[len
] == '\0' || p
[len
] == DIRNAME_SEPARATOR
))
436 /* Found it in the search path, remove old copy */
438 p
--; /* Back over leading separator */
439 if (prefix
> p
- *which_path
)
440 goto skip_dup
; /* Same dir twice in one cmd */
441 strcpy (p
, &p
[len
+ 1]); /* Copy from next \0 or : */
443 p
= strchr (p
, DIRNAME_SEPARATOR
);
453 tinybuf
[0] = DIRNAME_SEPARATOR
;
456 /* 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. */
463 temp
= concat (old
, tinybuf
, name
, NULL
);
465 *which_path
= concat (temp
, "", &old
[prefix
], NULL
);
466 prefix
= strlen (temp
);
471 *which_path
= concat (name
, (old
[0] ? tinybuf
: old
), old
, NULL
);
472 prefix
= strlen (name
);
480 while (*dirname
!= '\0');
485 source_info (char *ignore
, int from_tty
)
487 register struct symtab
*s
= current_source_symtab
;
491 printf_filtered ("No current source file.\n");
494 printf_filtered ("Current source file is %s\n", s
->filename
);
496 printf_filtered ("Compilation directory is %s\n", s
->dirname
);
498 printf_filtered ("Located in %s\n", s
->fullname
);
500 printf_filtered ("Contains %d line%s.\n", s
->nlines
,
501 s
->nlines
== 1 ? "" : "s");
503 printf_filtered ("Source language is %s.\n", language_str (s
->language
));
504 printf_filtered ("Compiled with %s debugging format.\n", s
->debugformat
);
509 /* Open a file named STRING, searching path PATH (dir names sep by some char)
510 using mode MODE and protection bits PROT in the calls to open.
512 If TRY_CWD_FIRST, try to open ./STRING before searching PATH.
513 (ie pretend the first element of PATH is "."). This also indicates
514 that a slash in STRING disables searching of the path (this is
515 so that "exec-file ./foo" or "symbol-file ./foo" insures that you
516 get that particular version of foo or an error message).
518 If FILENAME_OPENED is non-null, set it to a newly allocated string naming
519 the actual file opened (this string will always start with a "/". We
520 have to take special pains to avoid doubling the "/" between the directory
521 and the file, sigh! Emacs gets confuzzed by this when we print the
524 If a file is found, return the descriptor.
525 Otherwise, return -1, with errno set for the last name we tried to open. */
527 /* >>>> This should only allow files of certain types,
528 >>>> eg executable, non-directory */
530 openp (const char *path
, int try_cwd_first
, const char *string
,
532 char **filename_opened
)
535 register char *filename
;
544 #if defined(_WIN32) || defined(__CYGWIN__)
548 if (try_cwd_first
|| IS_ABSOLUTE_PATH (string
))
551 filename
= alloca (strlen (string
) + 1);
552 strcpy (filename
, string
);
553 fd
= open (filename
, mode
, prot
);
556 for (i
= 0; string
[i
]; i
++)
557 if (IS_DIR_SEPARATOR (string
[i
]))
562 while (string
[0] == '.' && IS_DIR_SEPARATOR (string
[1]))
565 alloclen
= strlen (path
) + strlen (string
) + 2;
566 filename
= alloca (alloclen
);
568 for (p
= path
; p
; p
= p1
? p1
+ 1 : 0)
570 p1
= strchr (p
, DIRNAME_SEPARATOR
);
576 if (len
== 4 && p
[0] == '$' && p
[1] == 'c'
577 && p
[2] == 'w' && p
[3] == 'd')
579 /* Name is $cwd -- insert current directory name instead. */
582 /* First, realloc the filename buffer if too short. */
583 len
= strlen (current_directory
);
584 newlen
= len
+ strlen (string
) + 2;
585 if (newlen
> alloclen
)
588 filename
= alloca (alloclen
);
590 strcpy (filename
, current_directory
);
594 /* Normal file name in path -- just use it. */
595 strncpy (filename
, p
, len
);
599 /* Remove trailing slashes */
600 while (len
> 0 && IS_DIR_SEPARATOR (filename
[len
- 1]))
603 strcat (filename
+ len
, SLASH_STRING
);
604 strcat (filename
, string
);
606 fd
= open (filename
, mode
);
615 *filename_opened
= NULL
;
616 else if (IS_ABSOLUTE_PATH (filename
))
617 *filename_opened
= gdb_realpath (filename
);
620 /* Beware the // my son, the Emacs barfs, the botch that catch... */
622 char *f
= concat (current_directory
,
623 IS_DIR_SEPARATOR (current_directory
[strlen (current_directory
) - 1])
626 *filename_opened
= gdb_realpath (f
);
635 /* This is essentially a convenience, for clients that want the behaviour
636 of openp, using source_path, but that really don't want the file to be
637 opened but want instead just to know what the full pathname is (as
638 qualified against source_path).
640 The current working directory is searched first.
642 If the file was found, this function returns 1, and FULL_PATHNAME is
643 set to the fully-qualified pathname.
645 Else, this functions returns 0, and FULL_PATHNAME is set to NULL.
648 source_full_path_of (char *filename
, char **full_pathname
)
652 fd
= openp (source_path
, 1, filename
, O_RDONLY
, 0, full_pathname
);
655 *full_pathname
= NULL
;
664 /* Open a source file given a symtab S. Returns a file descriptor or
665 negative number for error. */
668 open_source_file (struct symtab
*s
)
670 char *path
= source_path
;
675 /* Quick way out if we already know its full name */
678 result
= open (s
->fullname
, OPEN_MODE
);
681 /* Didn't work -- free old one, try again. */
682 xmfree (s
->objfile
->md
, s
->fullname
);
686 if (s
->dirname
!= NULL
)
688 /* Replace a path entry of $cdir with the compilation directory name */
690 /* We cast strstr's result in case an ANSIhole has made it const,
691 which produces a "required warning" when assigned to a nonconst. */
692 p
= (char *) strstr (source_path
, "$cdir");
693 if (p
&& (p
== path
|| p
[-1] == DIRNAME_SEPARATOR
)
694 && (p
[cdir_len
] == DIRNAME_SEPARATOR
|| p
[cdir_len
] == '\0'))
699 alloca (strlen (source_path
) + 1 + strlen (s
->dirname
) + 1);
700 len
= p
- source_path
;
701 strncpy (path
, source_path
, len
); /* Before $cdir */
702 strcpy (path
+ len
, s
->dirname
); /* new stuff */
703 strcat (path
+ len
, source_path
+ len
+ cdir_len
); /* After $cdir */
707 result
= openp (path
, 0, s
->filename
, OPEN_MODE
, 0, &s
->fullname
);
710 /* Didn't work. Try using just the basename. */
711 p
= lbasename (s
->filename
);
712 if (p
!= s
->filename
)
713 result
= openp (path
, 0, p
, OPEN_MODE
, 0, &s
->fullname
);
718 fullname
= s
->fullname
;
719 s
->fullname
= mstrsave (s
->objfile
->md
, s
->fullname
);
725 /* Return the path to the source file associated with symtab. Returns NULL
729 symtab_to_filename (struct symtab
*s
)
736 /* If we've seen the file before, just return fullname. */
741 /* Try opening the file to setup fullname */
743 fd
= open_source_file (s
);
745 return s
->filename
; /* File not found. Just use short name */
747 /* Found the file. Cleanup and return the full name */
754 /* Create and initialize the table S->line_charpos that records
755 the positions of the lines in the source file, which is assumed
756 to be open on descriptor DESC.
757 All set S->nlines to the number of such lines. */
760 find_source_lines (struct symtab
*s
, int desc
)
763 register char *data
, *p
, *end
;
765 int lines_allocated
= 1000;
770 line_charpos
= (int *) xmmalloc (s
->objfile
->md
,
771 lines_allocated
* sizeof (int));
772 if (fstat (desc
, &st
) < 0)
773 perror_with_name (s
->filename
);
775 if (s
&& s
->objfile
&& s
->objfile
->obfd
)
776 mtime
= bfd_get_mtime (s
->objfile
->obfd
);
778 mtime
= bfd_get_mtime (exec_bfd
);
780 if (mtime
&& mtime
< st
.st_mtime
)
782 warning ("Source file is more recent than executable.\n");
785 #ifdef LSEEK_NOT_LINEAR
789 /* Have to read it byte by byte to find out where the chars live */
791 line_charpos
[0] = lseek (desc
, 0, SEEK_CUR
);
793 while (myread (desc
, &c
, 1) > 0)
797 if (nlines
== lines_allocated
)
799 lines_allocated
*= 2;
801 (int *) xmrealloc (s
->objfile
->md
, (char *) line_charpos
,
802 sizeof (int) * lines_allocated
);
804 line_charpos
[nlines
++] = lseek (desc
, 0, SEEK_CUR
);
808 #else /* lseek linear. */
810 struct cleanup
*old_cleanups
;
812 /* st_size might be a large type, but we only support source files whose
813 size fits in an int. */
814 size
= (int) st
.st_size
;
816 /* Use malloc, not alloca, because this may be pretty large, and we may
817 run into various kinds of limits on stack size. */
818 data
= (char *) xmalloc (size
);
819 old_cleanups
= make_cleanup (xfree
, data
);
821 /* Reassign `size' to result of read for systems where \r\n -> \n. */
822 size
= myread (desc
, data
, size
);
824 perror_with_name (s
->filename
);
832 /* A newline at the end does not start a new line. */
835 if (nlines
== lines_allocated
)
837 lines_allocated
*= 2;
839 (int *) xmrealloc (s
->objfile
->md
, (char *) line_charpos
,
840 sizeof (int) * lines_allocated
);
842 line_charpos
[nlines
++] = p
- data
;
845 do_cleanups (old_cleanups
);
847 #endif /* lseek linear. */
850 (int *) xmrealloc (s
->objfile
->md
, (char *) line_charpos
,
851 nlines
* sizeof (int));
855 /* Return the character position of a line LINE in symtab S.
856 Return 0 if anything is invalid. */
858 #if 0 /* Currently unused */
861 source_line_charpos (struct symtab
*s
, int line
)
865 if (!s
->line_charpos
|| line
<= 0)
867 if (line
> s
->nlines
)
869 return s
->line_charpos
[line
- 1];
872 /* Return the line number of character position POS in symtab S. */
875 source_charpos_line (register struct symtab
*s
, register int chr
)
877 register int line
= 0;
880 if (s
== 0 || s
->line_charpos
== 0)
882 lnp
= s
->line_charpos
;
883 /* Files are usually short, so sequential search is Ok */
884 while (line
< s
->nlines
&& *lnp
<= chr
)
889 if (line
>= s
->nlines
)
897 /* Get full pathname and line number positions for a symtab.
898 Return nonzero if line numbers may have changed.
899 Set *FULLNAME to actual name of the file as found by `openp',
900 or to 0 if the file is not found. */
903 get_filename_and_charpos (struct symtab
*s
, char **fullname
)
905 register int desc
, linenums_changed
= 0;
907 desc
= open_source_file (s
);
915 *fullname
= s
->fullname
;
916 if (s
->line_charpos
== 0)
917 linenums_changed
= 1;
918 if (linenums_changed
)
919 find_source_lines (s
, desc
);
921 return linenums_changed
;
924 /* Print text describing the full name of the source file S
925 and the line number LINE and its corresponding character position.
926 The text starts with two Ctrl-z so that the Emacs-GDB interface
929 MID_STATEMENT is nonzero if the PC is not at the beginning of that line.
931 Return 1 if successful, 0 if could not find the file. */
934 identify_source_line (struct symtab
*s
, int line
, int mid_statement
,
937 if (s
->line_charpos
== 0)
938 get_filename_and_charpos (s
, (char **) NULL
);
939 if (s
->fullname
== 0)
941 if (line
> s
->nlines
)
942 /* Don't index off the end of the line_charpos array. */
944 annotate_source (s
->fullname
, line
, s
->line_charpos
[line
- 1],
947 current_source_line
= line
;
948 first_line_listed
= line
;
949 last_line_listed
= line
;
950 current_source_symtab
= s
;
955 /* Print source lines from the file of symtab S,
956 starting with line number LINE and stopping before line number STOPLINE. */
958 static void print_source_lines_base (struct symtab
*s
, int line
, int stopline
,
961 print_source_lines_base (struct symtab
*s
, int line
, int stopline
, int noerror
)
965 register FILE *stream
;
966 int nlines
= stopline
- line
;
968 /* Regardless of whether we can open the file, set current_source_symtab. */
969 current_source_symtab
= s
;
970 current_source_line
= line
;
971 first_line_listed
= line
;
974 /* If printing of source lines is disabled, just print file and line number */
975 if (ui_out_test_flags (uiout
, ui_source_list
))
978 /* Only prints "No such file or directory" once */
979 if ((s
!= last_source_visited
) || (!last_source_error
))
981 last_source_visited
= s
;
982 desc
= open_source_file (s
);
986 desc
= last_source_error
;
1000 last_source_error
= desc
;
1004 char *name
= alloca (strlen (s
->filename
) + 100);
1005 sprintf (name
, "%d\t%s", line
, s
->filename
);
1006 print_sys_errmsg (name
, errno
);
1010 ui_out_field_int (uiout
, "line", line
);
1011 ui_out_text (uiout
, "\tin ");
1012 ui_out_field_string (uiout
, "file", s
->filename
);
1013 ui_out_text (uiout
, "\n");
1015 printf_filtered ("%d\tin %s\n", line
, s
->filename
);
1021 last_source_error
= 0;
1023 if (s
->line_charpos
== 0)
1024 find_source_lines (s
, desc
);
1026 if (line
< 1 || line
> s
->nlines
)
1029 error ("Line number %d out of range; %s has %d lines.",
1030 line
, s
->filename
, s
->nlines
);
1033 if (lseek (desc
, s
->line_charpos
[line
- 1], 0) < 0)
1036 perror_with_name (s
->filename
);
1039 stream
= fdopen (desc
, FDOPEN_MODE
);
1042 while (nlines
-- > 0)
1050 last_line_listed
= current_source_line
;
1051 sprintf (buf
, "%d\t", current_source_line
++);
1052 ui_out_text (uiout
, buf
);
1055 if (c
< 040 && c
!= '\t' && c
!= '\n' && c
!= '\r')
1057 sprintf (buf
, "^%c", c
+ 0100);
1058 ui_out_text (uiout
, buf
);
1061 ui_out_text (uiout
, "^?");
1062 #ifdef CRLF_SOURCE_FILES
1065 /* Skip a \r character, but only before a \n. */
1066 int c1
= fgetc (stream
);
1069 printf_filtered ("^%c", c
+ 0100);
1071 ungetc (c1
, stream
);
1076 sprintf (buf
, "%c", c
);
1077 ui_out_text (uiout
, buf
);
1080 while (c
!= '\n' && (c
= fgetc (stream
)) >= 0);
1085 last_line_listed
= current_source_line
;
1086 printf_filtered ("%d\t", current_source_line
++);
1089 if (c
< 040 && c
!= '\t' && c
!= '\n' && c
!= '\r')
1090 printf_filtered ("^%c", c
+ 0100);
1092 printf_filtered ("^?");
1093 #ifdef CRLF_SOURCE_FILES
1096 /* Just skip \r characters. */
1100 printf_filtered ("%c", c
);
1102 while (c
!= '\n' && (c
= fgetc (stream
)) >= 0);
1109 /* Show source lines from the file of symtab S, starting with line
1110 number LINE and stopping before line number STOPLINE. If this is the
1111 not the command line version, then the source is shown in the source
1112 window otherwise it is simply printed */
1115 print_source_lines (struct symtab
*s
, int line
, int stopline
, int noerror
)
1117 print_source_lines_base (s
, line
, stopline
, noerror
);
1122 /* Print a list of files and line numbers which a user may choose from
1123 in order to list a function which was specified ambiguously (as with
1124 `list classname::overloadedfuncname', for example). The vector in
1125 SALS provides the filenames and line numbers. */
1128 ambiguous_line_spec (struct symtabs_and_lines
*sals
)
1132 for (i
= 0; i
< sals
->nelts
; ++i
)
1133 printf_filtered ("file: \"%s\", line number: %d\n",
1134 sals
->sals
[i
].symtab
->filename
, sals
->sals
[i
].line
);
1138 list_command (char *arg
, int from_tty
)
1140 struct symtabs_and_lines sals
, sals_end
;
1141 struct symtab_and_line sal
, sal_end
;
1147 int linenum_beg
= 0;
1150 if (!have_full_symbols () && !have_partial_symbols ())
1151 error ("No symbol table is loaded. Use the \"file\" command.");
1153 /* Pull in a current source symtab if necessary */
1154 if (current_source_symtab
== 0 &&
1155 (arg
== 0 || arg
[0] == '+' || arg
[0] == '-'))
1156 select_source_symtab (0);
1158 /* "l" or "l +" lists next ten lines. */
1160 if (arg
== 0 || STREQ (arg
, "+"))
1162 if (current_source_symtab
== 0)
1163 error ("No default source file yet. Do \"help list\".");
1164 print_source_lines (current_source_symtab
, current_source_line
,
1165 current_source_line
+ lines_to_list
, 0);
1169 /* "l -" lists previous ten lines, the ones before the ten just listed. */
1170 if (STREQ (arg
, "-"))
1172 if (current_source_symtab
== 0)
1173 error ("No default source file yet. Do \"help list\".");
1174 print_source_lines (current_source_symtab
,
1175 max (first_line_listed
- lines_to_list
, 1),
1176 first_line_listed
, 0);
1180 /* Now if there is only one argument, decode it in SAL
1182 If there are two arguments, decode them in SAL and SAL_END
1183 and clear NO_END; however, if one of the arguments is blank,
1184 set DUMMY_BEG or DUMMY_END to record that fact. */
1191 sals
= decode_line_1 (&arg1
, 0, 0, 0, 0);
1197 ambiguous_line_spec (&sals
);
1206 /* Record whether the BEG arg is all digits. */
1208 for (p
= arg
; p
!= arg1
&& *p
>= '0' && *p
<= '9'; p
++);
1209 linenum_beg
= (p
== arg1
);
1211 while (*arg1
== ' ' || *arg1
== '\t')
1217 while (*arg1
== ' ' || *arg1
== '\t')
1224 sals_end
= decode_line_1 (&arg1
, 0, 0, 0, 0);
1226 sals_end
= decode_line_1 (&arg1
, 0, sal
.symtab
, sal
.line
, 0);
1227 if (sals_end
.nelts
== 0)
1229 if (sals_end
.nelts
> 1)
1231 ambiguous_line_spec (&sals_end
);
1232 xfree (sals_end
.sals
);
1235 sal_end
= sals_end
.sals
[0];
1236 xfree (sals_end
.sals
);
1241 error ("Junk at end of line specification.");
1243 if (!no_end
&& !dummy_beg
&& !dummy_end
1244 && sal
.symtab
!= sal_end
.symtab
)
1245 error ("Specified start and end are in different files.");
1246 if (dummy_beg
&& dummy_end
)
1247 error ("Two empty args do not say what lines to list.");
1249 /* if line was specified by address,
1250 first print exactly which line, and which file.
1251 In this case, sal.symtab == 0 means address is outside
1252 of all known source files, not that user failed to give a filename. */
1255 if (sal
.symtab
== 0)
1256 /* FIXME-32x64--assumes sal.pc fits in long. */
1257 error ("No source file for address %s.",
1258 local_hex_string ((unsigned long) sal
.pc
));
1259 sym
= find_pc_function (sal
.pc
);
1262 print_address_numeric (sal
.pc
, 1, gdb_stdout
);
1263 printf_filtered (" is in ");
1264 fputs_filtered (SYMBOL_SOURCE_NAME (sym
), gdb_stdout
);
1265 printf_filtered (" (%s:%d).\n", sal
.symtab
->filename
, sal
.line
);
1269 print_address_numeric (sal
.pc
, 1, gdb_stdout
);
1270 printf_filtered (" is at %s:%d.\n",
1271 sal
.symtab
->filename
, sal
.line
);
1275 /* If line was not specified by just a line number,
1276 and it does not imply a symtab, it must be an undebuggable symbol
1277 which means no source code. */
1279 if (!linenum_beg
&& sal
.symtab
== 0)
1280 error ("No line number known for %s.", arg
);
1282 /* If this command is repeated with RET,
1283 turn it into the no-arg variant. */
1288 if (dummy_beg
&& sal_end
.symtab
== 0)
1289 error ("No default source file yet. Do \"help list\".");
1291 print_source_lines (sal_end
.symtab
,
1292 max (sal_end
.line
- (lines_to_list
- 1), 1),
1293 sal_end
.line
+ 1, 0);
1294 else if (sal
.symtab
== 0)
1295 error ("No default source file yet. Do \"help list\".");
1298 int first_line
= sal
.line
- lines_to_list
/ 2;
1300 if (first_line
< 1) first_line
= 1;
1302 print_source_lines (sal
.symtab
, first_line
, first_line
+ lines_to_list
,
1306 print_source_lines (sal
.symtab
, sal
.line
,
1308 ? sal
.line
+ lines_to_list
1309 : sal_end
.line
+ 1),
1313 /* Print info on range of pc's in a specified line. */
1316 line_info (char *arg
, int 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 (char *regex
, int from_tty
)
1418 register FILE *stream
;
1422 line
= last_line_listed
+ 1;
1424 msg
= (char *) re_comp (regex
);
1428 if (current_source_symtab
== 0)
1429 select_source_symtab (0);
1431 desc
= open_source_file (current_source_symtab
);
1433 perror_with_name (current_source_symtab
->filename
);
1435 if (current_source_symtab
->line_charpos
== 0)
1436 find_source_lines (current_source_symtab
, desc
);
1438 if (line
< 1 || line
> current_source_symtab
->nlines
)
1441 error ("Expression not found");
1444 if (lseek (desc
, current_source_symtab
->line_charpos
[line
- 1], 0) < 0)
1447 perror_with_name (current_source_symtab
->filename
);
1450 stream
= fdopen (desc
, FDOPEN_MODE
);
1454 static char *buf
= NULL
;
1456 int cursize
, newsize
;
1459 buf
= xmalloc (cursize
);
1468 if (p
- buf
== cursize
)
1470 newsize
= cursize
+ cursize
/ 2;
1471 buf
= xrealloc (buf
, newsize
);
1476 while (c
!= '\n' && (c
= getc (stream
)) >= 0);
1478 #ifdef CRLF_SOURCE_FILES
1479 /* Remove the \r, if any, at the end of the line, otherwise
1480 regular expressions that end with $ or \n won't work. */
1481 if (p
- buf
> 1 && p
[-2] == '\r')
1488 /* we now have a source line in buf, null terminate and match */
1490 if (re_exec (buf
) > 0)
1494 print_source_lines (current_source_symtab
, line
, line
+ 1, 0);
1495 set_internalvar (lookup_internalvar ("_"),
1496 value_from_longest (builtin_type_int
,
1498 current_source_line
= max (line
- lines_to_list
/ 2, 1);
1504 printf_filtered ("Expression not found\n");
1510 reverse_search_command (char *regex
, int from_tty
)
1514 register FILE *stream
;
1518 line
= last_line_listed
- 1;
1520 msg
= (char *) re_comp (regex
);
1524 if (current_source_symtab
== 0)
1525 select_source_symtab (0);
1527 desc
= open_source_file (current_source_symtab
);
1529 perror_with_name (current_source_symtab
->filename
);
1531 if (current_source_symtab
->line_charpos
== 0)
1532 find_source_lines (current_source_symtab
, desc
);
1534 if (line
< 1 || line
> current_source_symtab
->nlines
)
1537 error ("Expression not found");
1540 if (lseek (desc
, current_source_symtab
->line_charpos
[line
- 1], 0) < 0)
1543 perror_with_name (current_source_symtab
->filename
);
1546 stream
= fdopen (desc
, FDOPEN_MODE
);
1550 /* FIXME!!! We walk right off the end of buf if we get a long line!!! */
1551 char buf
[4096]; /* Should be reasonable??? */
1552 register char *p
= buf
;
1561 while (c
!= '\n' && (c
= getc (stream
)) >= 0);
1563 #ifdef CRLF_SOURCE_FILES
1564 /* Remove the \r, if any, at the end of the line, otherwise
1565 regular expressions that end with $ or \n won't work. */
1566 if (p
- buf
> 1 && p
[-2] == '\r')
1573 /* We now have a source line in buf; null terminate and match. */
1575 if (re_exec (buf
) > 0)
1579 print_source_lines (current_source_symtab
, line
, line
+ 1, 0);
1580 set_internalvar (lookup_internalvar ("_"),
1581 value_from_longest (builtin_type_int
,
1583 current_source_line
= max (line
- lines_to_list
/ 2, 1);
1587 if (fseek (stream
, current_source_symtab
->line_charpos
[line
- 1], 0) < 0)
1590 perror_with_name (current_source_symtab
->filename
);
1594 printf_filtered ("Expression not found\n");
1600 _initialize_source (void)
1602 struct cmd_list_element
*c
;
1603 current_source_symtab
= 0;
1604 init_source_path ();
1606 /* The intention is to use POSIX Basic Regular Expressions.
1607 Always use the GNU regex routine for consistency across all hosts.
1608 Our current GNU regex.c does not have all the POSIX features, so this is
1609 just an approximation. */
1610 re_set_syntax (RE_SYNTAX_GREP
);
1612 c
= add_cmd ("directory", class_files
, directory_command
,
1613 "Add directory DIR to beginning of search path for source files.\n\
1614 Forget cached info on source file locations and line positions.\n\
1615 DIR can also be $cwd for the current working directory, or $cdir for the\n\
1616 directory in which the source file was compiled into object code.\n\
1617 With no argument, reset the search path to $cdir:$cwd, the default.",
1621 add_com_alias ("use", "directory", class_files
, 0);
1623 c
->completer
= filename_completer
;
1625 add_cmd ("directories", no_class
, show_directories
,
1626 "Current search path for finding source files.\n\
1627 $cwd in the path means the current working directory.\n\
1628 $cdir in the path means the compilation directory of the source file.",
1633 add_com_alias ("D", "directory", class_files
, 0);
1634 add_cmd ("ld", no_class
, show_directories
,
1635 "Current search path for finding source files.\n\
1636 $cwd in the path means the current working directory.\n\
1637 $cdir in the path means the compilation directory of the source file.",
1641 add_info ("source", source_info
,
1642 "Information about the current source file.");
1644 add_info ("line", line_info
,
1645 concat ("Core addresses of the code for a source line.\n\
1646 Line can be specified as\n\
1647 LINENUM, to list around that line in current file,\n\
1648 FILE:LINENUM, to list around that line in that file,\n\
1649 FUNCTION, to list around beginning of that function,\n\
1650 FILE:FUNCTION, to distinguish among like-named static functions.\n\
1652 Default is to describe the last source line that was listed.\n\n\
1653 This sets the default address for \"x\" to the line's first instruction\n\
1654 so that \"x/i\" suffices to start examining the machine code.\n\
1655 The address is also stored as the value of \"$_\".", NULL
));
1657 add_com ("forward-search", class_files
, forward_search_command
,
1658 "Search for regular expression (see regex(3)) from last line listed.\n\
1659 The matching line number is also stored as the value of \"$_\".");
1660 add_com_alias ("search", "forward-search", class_files
, 0);
1662 add_com ("reverse-search", class_files
, reverse_search_command
,
1663 "Search backward for regular expression (see regex(3)) from last line listed.\n\
1664 The matching line number is also stored as the value of \"$_\".");
1668 add_com_alias ("/", "forward-search", class_files
, 0);
1669 add_com_alias ("?", "reverse-search", class_files
, 0);
1672 add_com ("list", class_files
, list_command
,
1673 concat ("List specified function or line.\n\
1674 With no argument, lists ten more lines after or around previous listing.\n\
1675 \"list -\" lists the ten lines before a previous ten-line listing.\n\
1676 One argument specifies a line, and ten lines are listed around that line.\n\
1677 Two arguments with comma between specify starting and ending lines to list.\n\
1679 Lines can be specified in these ways:\n\
1680 LINENUM, to list around that line in current file,\n\
1681 FILE:LINENUM, to list around that line in that file,\n\
1682 FUNCTION, to list around beginning of that function,\n\
1683 FILE:FUNCTION, to distinguish among like-named static functions.\n\
1684 *ADDRESS, to list around the line containing that address.\n\
1685 With two args if one is empty it stands for ten lines away from the other arg.", NULL
));
1688 add_com_alias ("l", "list", class_files
, 1);
1690 add_com_alias ("v", "list", class_files
, 1);
1693 add_com_alias ("file", "list", class_files
, 1);
1696 (add_set_cmd ("listsize", class_support
, var_uinteger
,
1697 (char *) &lines_to_list
,
1698 "Set number of source lines gdb will list by default.",