e5d99d3db09b4b34ee2d0ffa783e555b21eaa3a6
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"
48 #ifdef CRLF_SOURCE_FILES
50 /* Define CRLF_SOURCE_FILES in an xm-*.h file if source files on the
51 host use \r\n rather than just \n. Defining CRLF_SOURCE_FILES is
52 much faster than defining LSEEK_NOT_LINEAR. */
58 #define OPEN_MODE (O_RDONLY | O_BINARY)
59 #define FDOPEN_MODE FOPEN_RB
61 #else /* ! defined (CRLF_SOURCE_FILES) */
63 #define OPEN_MODE O_RDONLY
64 #define FDOPEN_MODE FOPEN_RT
66 #endif /* ! defined (CRLF_SOURCE_FILES) */
68 /* Prototypes for exported functions. */
70 void _initialize_source (void);
72 /* Prototypes for local functions. */
74 static int get_filename_and_charpos (struct symtab
*, char **);
76 static void reverse_search_command (char *, int);
78 static void forward_search_command (char *, int);
80 static void line_info (char *, int);
82 static void ambiguous_line_spec (struct symtabs_and_lines
*);
84 static void source_info (char *, int);
86 static void show_directories (char *, int);
88 /* Path of directories to search for source files.
89 Same format as the PATH environment variable's value. */
93 /* Symtab of default file for listing lines of. */
95 static struct symtab
*current_source_symtab
;
97 /* Default next line to list. */
99 static int current_source_line
;
101 /* Default number of lines to print with commands like "list".
102 This is based on guessing how many long (i.e. more than chars_per_line
103 characters) lines there will be. To be completely correct, "list"
104 and friends should be rewritten to count characters and see where
105 things are wrapping, but that would be a fair amount of work. */
107 int lines_to_list
= 10;
109 /* Line number of last line printed. Default for various commands.
110 current_source_line is usually, but not always, the same as this. */
112 static int last_line_listed
;
114 /* First line number listed by last listing command. */
116 static int first_line_listed
;
118 /* Saves the name of the last source file visited and a possible error code.
119 Used to prevent repeating annoying "No such file or directories" msgs */
121 static struct symtab
*last_source_visited
= NULL
;
122 static int last_source_error
= 0;
124 /* Return the first line listed by print_source_lines.
125 Used by command interpreters to request listing from
129 get_first_line_listed (void)
131 return first_line_listed
;
134 /* Return the default number of lines to print with commands like the
135 cli "list". The caller of print_source_lines must use this to
136 calculate the end line and use it in the call to print_source_lines
137 as it does not automatically use this value. */
140 get_lines_to_list (void)
142 return lines_to_list
;
145 /* Return the current source file for listing and next line to list.
146 NOTE: The returned sal pc and end fields are not valid. */
148 struct symtab_and_line
149 get_current_source_symtab_and_line (void)
151 struct symtab_and_line cursal
;
153 cursal
.symtab
= current_source_symtab
;
154 cursal
.line
= current_source_line
;
159 /* Return the current source file for listing and next line to list.
160 If a file is not set, try and get a default.
161 It may err out if a default cannot be determined.
162 Depending on where it is called, it can recurse as the process of
163 determining a new default may call the caler!
164 Use get_current_source_symtab_and_line instead to get whatever
165 we have without erroring out or trying to get a default.
166 NOTE: The returned sal pc and end fields are not valid. */
168 struct symtab_and_line
169 get_current_or_default_source_symtab_and_line (void)
171 struct symtab_and_line cursal
;
173 if (!have_full_symbols () && !have_partial_symbols ())
174 error ("No symbol table is loaded. Use the \"file\" command.");
176 /* Pull in a current source symtab if necessary */
177 if (current_source_symtab
== 0)
178 select_source_symtab (0);
180 cursal
.symtab
= current_source_symtab
;
181 cursal
.line
= current_source_line
;
186 /* Return the current default file for listing and next line to list
187 (the returned sal pc and end fields are not valid.)
188 and set the surrent default to whatever is in SAL */
190 struct symtab_and_line
191 set_current_source_symtab_and_line (struct symtab_and_line
*sal
)
193 struct symtab_and_line cursal
;
195 cursal
.symtab
= current_source_symtab
;
196 cursal
.line
= current_source_line
;
198 current_source_symtab
= sal
->symtab
;
199 current_source_line
= sal
->line
;
204 /* Reset any information stored about a default file and line to print. */
207 clear_current_source_symtab_and_line (void)
209 current_source_symtab
= 0;
210 current_source_line
= 0;
213 /* Set the source file default for the "list" command to be S.
215 If S is NULL, and we don't have a default, find one. This
216 should only be called when the user actually tries to use the
217 default, since we produce an error if we can't find a reasonable
218 default. Also, since this can cause symbols to be read, doing it
219 before we need to would make things slower than necessary. */
222 select_source_symtab (register struct symtab
*s
)
224 struct symtabs_and_lines sals
;
225 struct symtab_and_line sal
;
226 struct partial_symtab
*ps
;
227 struct partial_symtab
*cs_pst
= 0;
232 current_source_symtab
= s
;
233 current_source_line
= 1;
237 if (current_source_symtab
)
240 /* Make the default place to list be the function `main'
242 if (lookup_symbol (main_name (), 0, VAR_NAMESPACE
, 0, NULL
))
244 sals
= decode_line_spec (main_name (), 1);
247 current_source_symtab
= sal
.symtab
;
248 current_source_line
= max (sal
.line
- (lines_to_list
- 1), 1);
249 if (current_source_symtab
)
253 /* All right; find the last file in the symtab list (ignoring .h's). */
255 current_source_line
= 1;
257 for (ofp
= object_files
; ofp
!= NULL
; ofp
= ofp
->next
)
259 for (s
= ofp
->symtabs
; s
; s
= s
->next
)
261 char *name
= s
->filename
;
262 int len
= strlen (name
);
263 if (!(len
> 2 && (STREQ (&name
[len
- 2], ".h"))))
265 current_source_symtab
= s
;
269 if (current_source_symtab
)
272 /* Howabout the partial symbol tables? */
274 for (ofp
= object_files
; ofp
!= NULL
; ofp
= ofp
->next
)
276 for (ps
= ofp
->psymtabs
; ps
!= NULL
; ps
= ps
->next
)
278 char *name
= ps
->filename
;
279 int len
= strlen (name
);
280 if (!(len
> 2 && (STREQ (&name
[len
- 2], ".h"))))
290 internal_error (__FILE__
, __LINE__
,
291 "select_source_symtab: "
292 "readin pst found and no symtabs.");
296 current_source_symtab
= PSYMTAB_TO_SYMTAB (cs_pst
);
299 if (current_source_symtab
)
302 error ("Can't find a default source file");
306 show_directories (char *ignore
, int from_tty
)
308 puts_filtered ("Source directories searched: ");
309 puts_filtered (source_path
);
310 puts_filtered ("\n");
313 /* Forget what we learned about line positions in source files, and
314 which directories contain them; must check again now since files
315 may be found in a different directory now. */
318 forget_cached_source_info (void)
320 register struct symtab
*s
;
321 register struct objfile
*objfile
;
322 struct partial_symtab
*pst
;
324 for (objfile
= object_files
; objfile
!= NULL
; objfile
= objfile
->next
)
326 for (s
= objfile
->symtabs
; s
!= NULL
; s
= s
->next
)
328 if (s
->line_charpos
!= NULL
)
330 xmfree (objfile
->md
, s
->line_charpos
);
331 s
->line_charpos
= NULL
;
333 if (s
->fullname
!= NULL
)
335 xmfree (objfile
->md
, s
->fullname
);
340 ALL_OBJFILE_PSYMTABS (objfile
, pst
)
342 if (pst
->fullname
!= NULL
)
344 xfree (pst
->fullname
);
345 pst
->fullname
= NULL
;
352 init_source_path (void)
356 sprintf (buf
, "$cdir%c$cwd", DIRNAME_SEPARATOR
);
357 source_path
= xstrdup (buf
);
358 forget_cached_source_info ();
361 /* Add zero or more directories to the front of the source path. */
364 directory_command (char *dirname
, int from_tty
)
367 /* FIXME, this goes to "delete dir"... */
370 if (from_tty
&& query ("Reinitialize source path to empty? "))
378 mod_path (dirname
, &source_path
);
379 last_source_visited
= NULL
;
382 show_directories ((char *) 0, from_tty
);
383 forget_cached_source_info ();
386 /* Add zero or more directories to the front of an arbitrary path. */
389 mod_path (char *dirname
, char **which_path
)
391 char *old
= *which_path
;
397 dirname
= xstrdup (dirname
);
398 make_cleanup (xfree
, dirname
);
402 char *name
= dirname
;
407 char *separator
= strchr (name
, DIRNAME_SEPARATOR
);
408 char *space
= strchr (name
, ' ');
409 char *tab
= strchr (name
, '\t');
411 if (separator
== 0 && space
== 0 && tab
== 0)
412 p
= dirname
= name
+ strlen (name
);
416 if (separator
!= 0 && (p
== 0 || separator
< p
))
418 if (space
!= 0 && (p
== 0 || space
< p
))
420 if (tab
!= 0 && (p
== 0 || tab
< p
))
423 while (*dirname
== DIRNAME_SEPARATOR
430 if (!(IS_DIR_SEPARATOR (*name
) && p
<= name
+ 1) /* "/" */
431 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
432 /* On MS-DOS and MS-Windows, h:\ is different from h: */
433 && !(p
== name
+ 3 && name
[1] == ':') /* "d:/" */
435 && IS_DIR_SEPARATOR (p
[-1]))
436 /* Sigh. "foo/" => "foo" */
440 while (p
> name
&& p
[-1] == '.')
444 /* "." => getwd (). */
445 name
= current_directory
;
448 else if (p
> name
+ 1 && IS_DIR_SEPARATOR (p
[-2]))
458 /* "...foo/." => "...foo". */
469 name
= tilde_expand (name
);
470 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
471 else if (IS_ABSOLUTE_PATH (name
) && p
== name
+ 2) /* "d:" => "d:." */
472 name
= concat (name
, ".", NULL
);
474 else if (!IS_ABSOLUTE_PATH (name
) && name
[0] != '$')
475 name
= concat (current_directory
, SLASH_STRING
, name
, NULL
);
477 name
= savestring (name
, p
- name
);
478 make_cleanup (xfree
, name
);
480 /* Unless it's a variable, check existence. */
483 /* These are warnings, not errors, since we don't want a
484 non-existent directory in a .gdbinit file to stop processing
485 of the .gdbinit file.
487 Whether they get added to the path is more debatable. Current
488 answer is yes, in case the user wants to go make the directory
489 or whatever. If the directory continues to not exist/not be
490 a directory/etc, then having them in the path should be
492 if (stat (name
, &st
) < 0)
494 int save_errno
= errno
;
495 fprintf_unfiltered (gdb_stderr
, "Warning: ");
496 print_sys_errmsg (name
, save_errno
);
498 else if ((st
.st_mode
& S_IFMT
) != S_IFDIR
)
499 warning ("%s is not a directory.", name
);
504 register unsigned int len
= strlen (name
);
509 /* FIXME: strncmp loses in interesting ways on MS-DOS and
510 MS-Windows because of case-insensitivity and two different
511 but functionally identical slash characters. We need a
512 special filesystem-dependent file-name comparison function.
514 Actually, even on Unix I would use realpath() or its work-
515 alike before comparing. Then all the code above which
516 removes excess slashes and dots could simply go away. */
517 if (!strncmp (p
, name
, len
)
518 && (p
[len
] == '\0' || p
[len
] == DIRNAME_SEPARATOR
))
520 /* Found it in the search path, remove old copy */
522 p
--; /* Back over leading separator */
523 if (prefix
> p
- *which_path
)
524 goto skip_dup
; /* Same dir twice in one cmd */
525 strcpy (p
, &p
[len
+ 1]); /* Copy from next \0 or : */
527 p
= strchr (p
, DIRNAME_SEPARATOR
);
537 tinybuf
[0] = DIRNAME_SEPARATOR
;
540 /* 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. */
547 temp
= concat (old
, tinybuf
, name
, NULL
);
549 *which_path
= concat (temp
, "", &old
[prefix
], NULL
);
550 prefix
= strlen (temp
);
555 *which_path
= concat (name
, (old
[0] ? tinybuf
: old
), old
, NULL
);
556 prefix
= strlen (name
);
564 while (*dirname
!= '\0');
569 source_info (char *ignore
, int from_tty
)
571 register struct symtab
*s
= current_source_symtab
;
575 printf_filtered ("No current source file.\n");
578 printf_filtered ("Current source file is %s\n", s
->filename
);
580 printf_filtered ("Compilation directory is %s\n", s
->dirname
);
582 printf_filtered ("Located in %s\n", s
->fullname
);
584 printf_filtered ("Contains %d line%s.\n", s
->nlines
,
585 s
->nlines
== 1 ? "" : "s");
587 printf_filtered ("Source language is %s.\n", language_str (s
->language
));
588 printf_filtered ("Compiled with %s debugging format.\n", s
->debugformat
);
589 printf_filtered ("%s preprocessor macro info.\n",
590 s
->macro_table
? "Includes" : "Does not include");
594 /* Return True if the file NAME exists and is a regular file */
596 is_regular_file (const char *name
)
599 const int status
= stat (name
, &st
);
601 /* Stat should never fail except when the file does not exist.
602 If stat fails, analyze the source of error and return True
603 unless the file does not exist, to avoid returning false results
604 on obscure systems where stat does not work as expected.
607 return (errno
!= ENOENT
);
609 return S_ISREG (st
.st_mode
);
612 /* Open a file named STRING, searching path PATH (dir names sep by some char)
613 using mode MODE and protection bits PROT in the calls to open.
615 If TRY_CWD_FIRST, try to open ./STRING before searching PATH.
616 (ie pretend the first element of PATH is "."). This also indicates
617 that a slash in STRING disables searching of the path (this is
618 so that "exec-file ./foo" or "symbol-file ./foo" insures that you
619 get that particular version of foo or an error message).
621 If FILENAME_OPENED is non-null, set it to a newly allocated string naming
622 the actual file opened (this string will always start with a "/"). We
623 have to take special pains to avoid doubling the "/" between the directory
624 and the file, sigh! Emacs gets confuzzed by this when we print the
627 If a file is found, return the descriptor.
628 Otherwise, return -1, with errno set for the last name we tried to open. */
630 /* >>>> This should only allow files of certain types,
631 >>>> eg executable, non-directory */
633 openp (const char *path
, int try_cwd_first
, const char *string
,
635 char **filename_opened
)
638 register char *filename
;
647 #if defined(_WIN32) || defined(__CYGWIN__)
651 if ((try_cwd_first
|| IS_ABSOLUTE_PATH (string
)) && is_regular_file (string
))
654 filename
= alloca (strlen (string
) + 1);
655 strcpy (filename
, string
);
656 fd
= open (filename
, mode
, prot
);
659 for (i
= 0; string
[i
]; i
++)
660 if (IS_DIR_SEPARATOR (string
[i
]))
665 while (string
[0] == '.' && IS_DIR_SEPARATOR (string
[1]))
668 alloclen
= strlen (path
) + strlen (string
) + 2;
669 filename
= alloca (alloclen
);
671 for (p
= path
; p
; p
= p1
? p1
+ 1 : 0)
673 p1
= strchr (p
, DIRNAME_SEPARATOR
);
679 if (len
== 4 && p
[0] == '$' && p
[1] == 'c'
680 && p
[2] == 'w' && p
[3] == 'd')
682 /* Name is $cwd -- insert current directory name instead. */
685 /* First, realloc the filename buffer if too short. */
686 len
= strlen (current_directory
);
687 newlen
= len
+ strlen (string
) + 2;
688 if (newlen
> alloclen
)
691 filename
= alloca (alloclen
);
693 strcpy (filename
, current_directory
);
697 /* Normal file name in path -- just use it. */
698 strncpy (filename
, p
, len
);
702 /* Remove trailing slashes */
703 while (len
> 0 && IS_DIR_SEPARATOR (filename
[len
- 1]))
706 strcat (filename
+ len
, SLASH_STRING
);
707 strcat (filename
, string
);
709 if (is_regular_file (filename
))
711 fd
= open (filename
, mode
);
720 /* If a file was opened, canonicalize its filename. Use xfullpath
721 rather than gdb_realpath to avoid resolving the basename part
722 of filenames when the associated file is a symbolic link. This
723 fixes a potential inconsistency between the filenames known to
724 GDB and the filenames it prints in the annotations. */
726 *filename_opened
= NULL
;
727 else if (IS_ABSOLUTE_PATH (filename
))
728 *filename_opened
= xfullpath (filename
);
731 /* Beware the // my son, the Emacs barfs, the botch that catch... */
733 char *f
= concat (current_directory
,
734 IS_DIR_SEPARATOR (current_directory
[strlen (current_directory
) - 1])
737 *filename_opened
= xfullpath (f
);
746 /* This is essentially a convenience, for clients that want the behaviour
747 of openp, using source_path, but that really don't want the file to be
748 opened but want instead just to know what the full pathname is (as
749 qualified against source_path).
751 The current working directory is searched first.
753 If the file was found, this function returns 1, and FULL_PATHNAME is
754 set to the fully-qualified pathname.
756 Else, this functions returns 0, and FULL_PATHNAME is set to NULL.
759 source_full_path_of (char *filename
, char **full_pathname
)
763 fd
= openp (source_path
, 1, filename
, O_RDONLY
, 0, full_pathname
);
766 *full_pathname
= NULL
;
775 /* Open a source file given a symtab S. Returns a file descriptor or
776 negative number for error. */
779 open_source_file (struct symtab
*s
)
781 char *path
= source_path
;
786 /* Quick way out if we already know its full name */
789 result
= open (s
->fullname
, OPEN_MODE
);
792 /* Didn't work -- free old one, try again. */
793 xmfree (s
->objfile
->md
, s
->fullname
);
797 if (s
->dirname
!= NULL
)
799 /* Replace a path entry of $cdir with the compilation directory name */
801 /* We cast strstr's result in case an ANSIhole has made it const,
802 which produces a "required warning" when assigned to a nonconst. */
803 p
= (char *) strstr (source_path
, "$cdir");
804 if (p
&& (p
== path
|| p
[-1] == DIRNAME_SEPARATOR
)
805 && (p
[cdir_len
] == DIRNAME_SEPARATOR
|| p
[cdir_len
] == '\0'))
810 alloca (strlen (source_path
) + 1 + strlen (s
->dirname
) + 1);
811 len
= p
- source_path
;
812 strncpy (path
, source_path
, len
); /* Before $cdir */
813 strcpy (path
+ len
, s
->dirname
); /* new stuff */
814 strcat (path
+ len
, source_path
+ len
+ cdir_len
); /* After $cdir */
818 result
= openp (path
, 0, s
->filename
, OPEN_MODE
, 0, &s
->fullname
);
821 /* Didn't work. Try using just the basename. */
822 p
= lbasename (s
->filename
);
823 if (p
!= s
->filename
)
824 result
= openp (path
, 0, p
, OPEN_MODE
, 0, &s
->fullname
);
829 fullname
= s
->fullname
;
830 s
->fullname
= mstrsave (s
->objfile
->md
, s
->fullname
);
836 /* Return the path to the source file associated with symtab. Returns NULL
840 symtab_to_filename (struct symtab
*s
)
847 /* If we've seen the file before, just return fullname. */
852 /* Try opening the file to setup fullname */
854 fd
= open_source_file (s
);
856 return s
->filename
; /* File not found. Just use short name */
858 /* Found the file. Cleanup and return the full name */
865 /* Create and initialize the table S->line_charpos that records
866 the positions of the lines in the source file, which is assumed
867 to be open on descriptor DESC.
868 All set S->nlines to the number of such lines. */
871 find_source_lines (struct symtab
*s
, int desc
)
874 register char *data
, *p
, *end
;
876 int lines_allocated
= 1000;
881 line_charpos
= (int *) xmmalloc (s
->objfile
->md
,
882 lines_allocated
* sizeof (int));
883 if (fstat (desc
, &st
) < 0)
884 perror_with_name (s
->filename
);
886 if (s
&& s
->objfile
&& s
->objfile
->obfd
)
887 mtime
= bfd_get_mtime (s
->objfile
->obfd
);
889 mtime
= bfd_get_mtime (exec_bfd
);
891 if (mtime
&& mtime
< st
.st_mtime
)
893 warning ("Source file is more recent than executable.\n");
896 #ifdef LSEEK_NOT_LINEAR
900 /* Have to read it byte by byte to find out where the chars live */
902 line_charpos
[0] = lseek (desc
, 0, SEEK_CUR
);
904 while (myread (desc
, &c
, 1) > 0)
908 if (nlines
== lines_allocated
)
910 lines_allocated
*= 2;
912 (int *) xmrealloc (s
->objfile
->md
, (char *) line_charpos
,
913 sizeof (int) * lines_allocated
);
915 line_charpos
[nlines
++] = lseek (desc
, 0, SEEK_CUR
);
919 #else /* lseek linear. */
921 struct cleanup
*old_cleanups
;
923 /* st_size might be a large type, but we only support source files whose
924 size fits in an int. */
925 size
= (int) st
.st_size
;
927 /* Use malloc, not alloca, because this may be pretty large, and we may
928 run into various kinds of limits on stack size. */
929 data
= (char *) xmalloc (size
);
930 old_cleanups
= make_cleanup (xfree
, data
);
932 /* Reassign `size' to result of read for systems where \r\n -> \n. */
933 size
= myread (desc
, data
, size
);
935 perror_with_name (s
->filename
);
943 /* A newline at the end does not start a new line. */
946 if (nlines
== lines_allocated
)
948 lines_allocated
*= 2;
950 (int *) xmrealloc (s
->objfile
->md
, (char *) line_charpos
,
951 sizeof (int) * lines_allocated
);
953 line_charpos
[nlines
++] = p
- data
;
956 do_cleanups (old_cleanups
);
958 #endif /* lseek linear. */
961 (int *) xmrealloc (s
->objfile
->md
, (char *) line_charpos
,
962 nlines
* sizeof (int));
966 /* Return the character position of a line LINE in symtab S.
967 Return 0 if anything is invalid. */
969 #if 0 /* Currently unused */
972 source_line_charpos (struct symtab
*s
, int line
)
976 if (!s
->line_charpos
|| line
<= 0)
978 if (line
> s
->nlines
)
980 return s
->line_charpos
[line
- 1];
983 /* Return the line number of character position POS in symtab S. */
986 source_charpos_line (register struct symtab
*s
, register int chr
)
988 register int line
= 0;
991 if (s
== 0 || s
->line_charpos
== 0)
993 lnp
= s
->line_charpos
;
994 /* Files are usually short, so sequential search is Ok */
995 while (line
< s
->nlines
&& *lnp
<= chr
)
1000 if (line
>= s
->nlines
)
1008 /* Get full pathname and line number positions for a symtab.
1009 Return nonzero if line numbers may have changed.
1010 Set *FULLNAME to actual name of the file as found by `openp',
1011 or to 0 if the file is not found. */
1014 get_filename_and_charpos (struct symtab
*s
, char **fullname
)
1016 register int desc
, linenums_changed
= 0;
1018 desc
= open_source_file (s
);
1026 *fullname
= s
->fullname
;
1027 if (s
->line_charpos
== 0)
1028 linenums_changed
= 1;
1029 if (linenums_changed
)
1030 find_source_lines (s
, desc
);
1032 return linenums_changed
;
1035 /* Print text describing the full name of the source file S
1036 and the line number LINE and its corresponding character position.
1037 The text starts with two Ctrl-z so that the Emacs-GDB interface
1040 MID_STATEMENT is nonzero if the PC is not at the beginning of that line.
1042 Return 1 if successful, 0 if could not find the file. */
1045 identify_source_line (struct symtab
*s
, int line
, int mid_statement
,
1048 if (s
->line_charpos
== 0)
1049 get_filename_and_charpos (s
, (char **) NULL
);
1050 if (s
->fullname
== 0)
1052 if (line
> s
->nlines
)
1053 /* Don't index off the end of the line_charpos array. */
1055 annotate_source (s
->fullname
, line
, s
->line_charpos
[line
- 1],
1058 current_source_line
= line
;
1059 first_line_listed
= line
;
1060 last_line_listed
= line
;
1061 current_source_symtab
= s
;
1066 /* Print source lines from the file of symtab S,
1067 starting with line number LINE and stopping before line number STOPLINE. */
1069 static void print_source_lines_base (struct symtab
*s
, int line
, int stopline
,
1072 print_source_lines_base (struct symtab
*s
, int line
, int stopline
, int noerror
)
1076 register FILE *stream
;
1077 int nlines
= stopline
- line
;
1079 /* Regardless of whether we can open the file, set current_source_symtab. */
1080 current_source_symtab
= s
;
1081 current_source_line
= line
;
1082 first_line_listed
= line
;
1084 /* If printing of source lines is disabled, just print file and line number */
1085 if (ui_out_test_flags (uiout
, ui_source_list
))
1087 /* Only prints "No such file or directory" once */
1088 if ((s
!= last_source_visited
) || (!last_source_error
))
1090 last_source_visited
= s
;
1091 desc
= open_source_file (s
);
1095 desc
= last_source_error
;
1107 last_source_error
= desc
;
1111 char *name
= alloca (strlen (s
->filename
) + 100);
1112 sprintf (name
, "%d\t%s", line
, s
->filename
);
1113 print_sys_errmsg (name
, errno
);
1116 ui_out_field_int (uiout
, "line", line
);
1117 ui_out_text (uiout
, "\tin ");
1118 ui_out_field_string (uiout
, "file", s
->filename
);
1119 ui_out_text (uiout
, "\n");
1124 last_source_error
= 0;
1126 if (s
->line_charpos
== 0)
1127 find_source_lines (s
, desc
);
1129 if (line
< 1 || line
> s
->nlines
)
1132 error ("Line number %d out of range; %s has %d lines.",
1133 line
, s
->filename
, s
->nlines
);
1136 if (lseek (desc
, s
->line_charpos
[line
- 1], 0) < 0)
1139 perror_with_name (s
->filename
);
1142 stream
= fdopen (desc
, FDOPEN_MODE
);
1145 while (nlines
-- > 0)
1152 last_line_listed
= current_source_line
;
1153 sprintf (buf
, "%d\t", current_source_line
++);
1154 ui_out_text (uiout
, buf
);
1157 if (c
< 040 && c
!= '\t' && c
!= '\n' && c
!= '\r')
1159 sprintf (buf
, "^%c", c
+ 0100);
1160 ui_out_text (uiout
, buf
);
1163 ui_out_text (uiout
, "^?");
1164 #ifdef CRLF_SOURCE_FILES
1167 /* Skip a \r character, but only before a \n. */
1168 int c1
= fgetc (stream
);
1171 printf_filtered ("^%c", c
+ 0100);
1173 ungetc (c1
, stream
);
1178 sprintf (buf
, "%c", c
);
1179 ui_out_text (uiout
, buf
);
1182 while (c
!= '\n' && (c
= fgetc (stream
)) >= 0);
1188 /* Show source lines from the file of symtab S, starting with line
1189 number LINE and stopping before line number STOPLINE. If this is the
1190 not the command line version, then the source is shown in the source
1191 window otherwise it is simply printed */
1194 print_source_lines (struct symtab
*s
, int line
, int stopline
, int noerror
)
1196 print_source_lines_base (s
, line
, stopline
, noerror
);
1199 /* Print a list of files and line numbers which a user may choose from
1200 in order to list a function which was specified ambiguously (as with
1201 `list classname::overloadedfuncname', for example). The vector in
1202 SALS provides the filenames and line numbers. */
1205 ambiguous_line_spec (struct symtabs_and_lines
*sals
)
1209 for (i
= 0; i
< sals
->nelts
; ++i
)
1210 printf_filtered ("file: \"%s\", line number: %d\n",
1211 sals
->sals
[i
].symtab
->filename
, sals
->sals
[i
].line
);
1214 /* Print info on range of pc's in a specified line. */
1217 line_info (char *arg
, int from_tty
)
1219 struct symtabs_and_lines sals
;
1220 struct symtab_and_line sal
;
1221 CORE_ADDR start_pc
, end_pc
;
1224 INIT_SAL (&sal
); /* initialize to zeroes */
1228 sal
.symtab
= current_source_symtab
;
1229 sal
.line
= last_line_listed
;
1231 sals
.sals
= (struct symtab_and_line
*)
1232 xmalloc (sizeof (struct symtab_and_line
));
1237 sals
= decode_line_spec_1 (arg
, 0);
1242 /* C++ More than one line may have been specified, as when the user
1243 specifies an overloaded function name. Print info on them all. */
1244 for (i
= 0; i
< sals
.nelts
; i
++)
1248 if (sal
.symtab
== 0)
1250 printf_filtered ("No line number information available");
1253 /* This is useful for "info line *0x7f34". If we can't tell the
1254 user about a source line, at least let them have the symbolic
1256 printf_filtered (" for address ");
1258 print_address (sal
.pc
, gdb_stdout
);
1261 printf_filtered (".");
1262 printf_filtered ("\n");
1264 else if (sal
.line
> 0
1265 && find_line_pc_range (sal
, &start_pc
, &end_pc
))
1267 if (start_pc
== end_pc
)
1269 printf_filtered ("Line %d of \"%s\"",
1270 sal
.line
, sal
.symtab
->filename
);
1272 printf_filtered (" is at address ");
1273 print_address (start_pc
, gdb_stdout
);
1275 printf_filtered (" but contains no code.\n");
1279 printf_filtered ("Line %d of \"%s\"",
1280 sal
.line
, sal
.symtab
->filename
);
1282 printf_filtered (" starts at address ");
1283 print_address (start_pc
, gdb_stdout
);
1285 printf_filtered (" and ends at ");
1286 print_address (end_pc
, gdb_stdout
);
1287 printf_filtered (".\n");
1290 /* x/i should display this line's code. */
1291 set_next_address (start_pc
);
1293 /* Repeating "info line" should do the following line. */
1294 last_line_listed
= sal
.line
+ 1;
1296 /* If this is the only line, show the source code. If it could
1297 not find the file, don't do anything special. */
1298 if (annotation_level
&& sals
.nelts
== 1)
1299 identify_source_line (sal
.symtab
, sal
.line
, 0, start_pc
);
1302 /* Is there any case in which we get here, and have an address
1303 which the user would want to see? If we have debugging symbols
1304 and no line numbers? */
1305 printf_filtered ("Line number %d is out of range for \"%s\".\n",
1306 sal
.line
, sal
.symtab
->filename
);
1311 /* Commands to search the source file for a regexp. */
1315 forward_search_command (char *regex
, int from_tty
)
1319 register FILE *stream
;
1323 line
= last_line_listed
+ 1;
1325 msg
= (char *) re_comp (regex
);
1329 if (current_source_symtab
== 0)
1330 select_source_symtab (0);
1332 desc
= open_source_file (current_source_symtab
);
1334 perror_with_name (current_source_symtab
->filename
);
1336 if (current_source_symtab
->line_charpos
== 0)
1337 find_source_lines (current_source_symtab
, desc
);
1339 if (line
< 1 || line
> current_source_symtab
->nlines
)
1342 error ("Expression not found");
1345 if (lseek (desc
, current_source_symtab
->line_charpos
[line
- 1], 0) < 0)
1348 perror_with_name (current_source_symtab
->filename
);
1351 stream
= fdopen (desc
, FDOPEN_MODE
);
1355 static char *buf
= NULL
;
1357 int cursize
, newsize
;
1360 buf
= xmalloc (cursize
);
1369 if (p
- buf
== cursize
)
1371 newsize
= cursize
+ cursize
/ 2;
1372 buf
= xrealloc (buf
, newsize
);
1377 while (c
!= '\n' && (c
= getc (stream
)) >= 0);
1379 #ifdef CRLF_SOURCE_FILES
1380 /* Remove the \r, if any, at the end of the line, otherwise
1381 regular expressions that end with $ or \n won't work. */
1382 if (p
- buf
> 1 && p
[-2] == '\r')
1389 /* we now have a source line in buf, null terminate and match */
1391 if (re_exec (buf
) > 0)
1395 print_source_lines (current_source_symtab
, line
, line
+ 1, 0);
1396 set_internalvar (lookup_internalvar ("_"),
1397 value_from_longest (builtin_type_int
,
1399 current_source_line
= max (line
- lines_to_list
/ 2, 1);
1405 printf_filtered ("Expression not found\n");
1411 reverse_search_command (char *regex
, int from_tty
)
1415 register FILE *stream
;
1419 line
= last_line_listed
- 1;
1421 msg
= (char *) re_comp (regex
);
1425 if (current_source_symtab
== 0)
1426 select_source_symtab (0);
1428 desc
= open_source_file (current_source_symtab
);
1430 perror_with_name (current_source_symtab
->filename
);
1432 if (current_source_symtab
->line_charpos
== 0)
1433 find_source_lines (current_source_symtab
, desc
);
1435 if (line
< 1 || line
> current_source_symtab
->nlines
)
1438 error ("Expression not found");
1441 if (lseek (desc
, current_source_symtab
->line_charpos
[line
- 1], 0) < 0)
1444 perror_with_name (current_source_symtab
->filename
);
1447 stream
= fdopen (desc
, FDOPEN_MODE
);
1451 /* FIXME!!! We walk right off the end of buf if we get a long line!!! */
1452 char buf
[4096]; /* Should be reasonable??? */
1453 register char *p
= buf
;
1462 while (c
!= '\n' && (c
= getc (stream
)) >= 0);
1464 #ifdef CRLF_SOURCE_FILES
1465 /* Remove the \r, if any, at the end of the line, otherwise
1466 regular expressions that end with $ or \n won't work. */
1467 if (p
- buf
> 1 && p
[-2] == '\r')
1474 /* We now have a source line in buf; null terminate and match. */
1476 if (re_exec (buf
) > 0)
1480 print_source_lines (current_source_symtab
, line
, line
+ 1, 0);
1481 set_internalvar (lookup_internalvar ("_"),
1482 value_from_longest (builtin_type_int
,
1484 current_source_line
= max (line
- lines_to_list
/ 2, 1);
1488 if (fseek (stream
, current_source_symtab
->line_charpos
[line
- 1], 0) < 0)
1491 perror_with_name (current_source_symtab
->filename
);
1495 printf_filtered ("Expression not found\n");
1501 _initialize_source (void)
1503 struct cmd_list_element
*c
;
1504 current_source_symtab
= 0;
1505 init_source_path ();
1507 /* The intention is to use POSIX Basic Regular Expressions.
1508 Always use the GNU regex routine for consistency across all hosts.
1509 Our current GNU regex.c does not have all the POSIX features, so this is
1510 just an approximation. */
1511 re_set_syntax (RE_SYNTAX_GREP
);
1513 c
= add_cmd ("directory", class_files
, directory_command
,
1514 "Add directory DIR to beginning of search path for source files.\n\
1515 Forget cached info on source file locations and line positions.\n\
1516 DIR can also be $cwd for the current working directory, or $cdir for the\n\
1517 directory in which the source file was compiled into object code.\n\
1518 With no argument, reset the search path to $cdir:$cwd, the default.",
1522 add_com_alias ("use", "directory", class_files
, 0);
1524 set_cmd_completer (c
, filename_completer
);
1526 add_cmd ("directories", no_class
, show_directories
,
1527 "Current search path for finding source files.\n\
1528 $cwd in the path means the current working directory.\n\
1529 $cdir in the path means the compilation directory of the source file.",
1534 add_com_alias ("D", "directory", class_files
, 0);
1535 add_cmd ("ld", no_class
, show_directories
,
1536 "Current search path for finding source files.\n\
1537 $cwd in the path means the current working directory.\n\
1538 $cdir in the path means the compilation directory of the source file.",
1542 add_info ("source", source_info
,
1543 "Information about the current source file.");
1545 add_info ("line", line_info
,
1546 concat ("Core addresses of the code for a source line.\n\
1547 Line can be specified as\n\
1548 LINENUM, to list around that line in current file,\n\
1549 FILE:LINENUM, to list around that line in that file,\n\
1550 FUNCTION, to list around beginning of that function,\n\
1551 FILE:FUNCTION, to distinguish among like-named static functions.\n\
1553 Default is to describe the last source line that was listed.\n\n\
1554 This sets the default address for \"x\" to the line's first instruction\n\
1555 so that \"x/i\" suffices to start examining the machine code.\n\
1556 The address is also stored as the value of \"$_\".", NULL
));
1558 add_com ("forward-search", class_files
, forward_search_command
,
1559 "Search for regular expression (see regex(3)) from last line listed.\n\
1560 The matching line number is also stored as the value of \"$_\".");
1561 add_com_alias ("search", "forward-search", class_files
, 0);
1563 add_com ("reverse-search", class_files
, reverse_search_command
,
1564 "Search backward for regular expression (see regex(3)) from last line listed.\n\
1565 The matching line number is also stored as the value of \"$_\".");
1569 add_com_alias ("/", "forward-search", class_files
, 0);
1570 add_com_alias ("?", "reverse-search", class_files
, 0);
1574 (add_set_cmd ("listsize", class_support
, var_uinteger
,
1575 (char *) &lines_to_list
,
1576 "Set number of source lines gdb will list by default.",