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"
47 #include <readline/readline.h>
49 #ifdef CRLF_SOURCE_FILES
51 /* Define CRLF_SOURCE_FILES in an xm-*.h file if source files on the
52 host use \r\n rather than just \n. Defining CRLF_SOURCE_FILES is
53 much faster than defining LSEEK_NOT_LINEAR. */
59 #define OPEN_MODE (O_RDONLY | O_BINARY)
60 #define FDOPEN_MODE FOPEN_RB
62 #else /* ! defined (CRLF_SOURCE_FILES) */
64 #define OPEN_MODE O_RDONLY
65 #define FDOPEN_MODE FOPEN_RT
67 #endif /* ! defined (CRLF_SOURCE_FILES) */
69 /* Prototypes for exported functions. */
71 void _initialize_source (void);
73 /* Prototypes for local functions. */
75 static int get_filename_and_charpos (struct symtab
*, char **);
77 static void reverse_search_command (char *, int);
79 static void forward_search_command (char *, int);
81 static void line_info (char *, int);
83 static void ambiguous_line_spec (struct symtabs_and_lines
*);
85 static void source_info (char *, int);
87 static void show_directories (char *, int);
89 /* Path of directories to search for source files.
90 Same format as the PATH environment variable's value. */
94 /* Symtab of default file for listing lines of. */
96 static struct symtab
*current_source_symtab
;
98 /* Default next line to list. */
100 static int current_source_line
;
102 /* Default number of lines to print with commands like "list".
103 This is based on guessing how many long (i.e. more than chars_per_line
104 characters) lines there will be. To be completely correct, "list"
105 and friends should be rewritten to count characters and see where
106 things are wrapping, but that would be a fair amount of work. */
108 int lines_to_list
= 10;
110 /* Line number of last line printed. Default for various commands.
111 current_source_line is usually, but not always, the same as this. */
113 static int last_line_listed
;
115 /* First line number listed by last listing command. */
117 static int first_line_listed
;
119 /* Saves the name of the last source file visited and a possible error code.
120 Used to prevent repeating annoying "No such file or directories" msgs */
122 static struct symtab
*last_source_visited
= NULL
;
123 static int last_source_error
= 0;
125 /* Return the first line listed by print_source_lines.
126 Used by command interpreters to request listing from
130 get_first_line_listed (void)
132 return first_line_listed
;
135 /* Return the default number of lines to print with commands like the
136 cli "list". The caller of print_source_lines must use this to
137 calculate the end line and use it in the call to print_source_lines
138 as it does not automatically use this value. */
141 get_lines_to_list (void)
143 return lines_to_list
;
146 /* Return the current source file for listing and next line to list.
147 NOTE: The returned sal pc and end fields are not valid. */
149 struct symtab_and_line
150 get_current_source_symtab_and_line (void)
152 struct symtab_and_line cursal
;
154 cursal
.symtab
= current_source_symtab
;
155 cursal
.line
= current_source_line
;
162 /* If the current source file for listing is not set, try and get a default.
163 Usually called before get_current_source_symtab_and_line() is called.
164 It may err out if a default cannot be determined.
165 We must be cautious about where it is called, as it can recurse as the
166 process of determining a new default may call the caller!
167 Use get_current_source_symtab_and_line only to get whatever
168 we have without erroring out or trying to get a default. */
171 set_default_source_symtab_and_line (void)
173 struct symtab_and_line cursal
;
175 if (!have_full_symbols () && !have_partial_symbols ())
176 error ("No symbol table is loaded. Use the \"file\" command.");
178 /* Pull in a current source symtab if necessary */
179 if (current_source_symtab
== 0)
180 select_source_symtab (0);
183 /* Return the current default file for listing and next line to list
184 (the returned sal pc and end fields are not valid.)
185 and set the current default to whatever is in SAL.
186 NOTE: The returned sal pc and end fields are not valid. */
188 struct symtab_and_line
189 set_current_source_symtab_and_line (const struct symtab_and_line
*sal
)
191 struct symtab_and_line cursal
;
193 cursal
.symtab
= current_source_symtab
;
194 cursal
.line
= current_source_line
;
196 current_source_symtab
= sal
->symtab
;
197 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 ();
362 init_last_source_visited (void)
364 last_source_visited
= NULL
;
367 /* Add zero or more directories to the front of the source path. */
370 directory_command (char *dirname
, int from_tty
)
373 /* FIXME, this goes to "delete dir"... */
376 if (from_tty
&& query ("Reinitialize source path to empty? "))
384 mod_path (dirname
, &source_path
);
385 last_source_visited
= NULL
;
388 show_directories ((char *) 0, from_tty
);
389 forget_cached_source_info ();
392 /* Add zero or more directories to the front of an arbitrary path. */
395 mod_path (char *dirname
, char **which_path
)
397 add_path (dirname
, which_path
, 1);
400 /* Workhorse of mod_path. Takes an extra argument to determine
401 if dirname should be parsed for separators that indicate multiple
402 directories. This allows for interfaces that pre-parse the dirname
403 and allow specification of traditional separator characters such
407 add_path (char *dirname
, char **which_path
, int parse_separators
)
409 char *old
= *which_path
;
415 dirname
= xstrdup (dirname
);
416 make_cleanup (xfree
, dirname
);
420 char *name
= dirname
;
425 char *separator
= NULL
;
429 if (parse_separators
)
431 separator
= strchr (name
, DIRNAME_SEPARATOR
);
432 space
= strchr (name
, ' ');
433 tab
= strchr (name
, '\t');
436 if (separator
== 0 && space
== 0 && tab
== 0)
437 p
= dirname
= name
+ strlen (name
);
441 if (separator
!= 0 && (p
== 0 || separator
< p
))
443 if (space
!= 0 && (p
== 0 || space
< p
))
445 if (tab
!= 0 && (p
== 0 || tab
< p
))
448 while (*dirname
== DIRNAME_SEPARATOR
455 if (!(IS_DIR_SEPARATOR (*name
) && p
<= name
+ 1) /* "/" */
456 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
457 /* On MS-DOS and MS-Windows, h:\ is different from h: */
458 && !(p
== name
+ 3 && name
[1] == ':') /* "d:/" */
460 && IS_DIR_SEPARATOR (p
[-1]))
461 /* Sigh. "foo/" => "foo" */
465 while (p
> name
&& p
[-1] == '.')
469 /* "." => getwd (). */
470 name
= current_directory
;
473 else if (p
> name
+ 1 && IS_DIR_SEPARATOR (p
[-2]))
483 /* "...foo/." => "...foo". */
494 name
= tilde_expand (name
);
495 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
496 else if (IS_ABSOLUTE_PATH (name
) && p
== name
+ 2) /* "d:" => "d:." */
497 name
= concat (name
, ".", NULL
);
499 else if (!IS_ABSOLUTE_PATH (name
) && name
[0] != '$')
500 name
= concat (current_directory
, SLASH_STRING
, name
, NULL
);
502 name
= savestring (name
, p
- name
);
503 make_cleanup (xfree
, name
);
505 /* Unless it's a variable, check existence. */
508 /* These are warnings, not errors, since we don't want a
509 non-existent directory in a .gdbinit file to stop processing
510 of the .gdbinit file.
512 Whether they get added to the path is more debatable. Current
513 answer is yes, in case the user wants to go make the directory
514 or whatever. If the directory continues to not exist/not be
515 a directory/etc, then having them in the path should be
517 if (stat (name
, &st
) < 0)
519 int save_errno
= errno
;
520 fprintf_unfiltered (gdb_stderr
, "Warning: ");
521 print_sys_errmsg (name
, save_errno
);
523 else if ((st
.st_mode
& S_IFMT
) != S_IFDIR
)
524 warning ("%s is not a directory.", name
);
529 register unsigned int len
= strlen (name
);
534 /* FIXME: strncmp loses in interesting ways on MS-DOS and
535 MS-Windows because of case-insensitivity and two different
536 but functionally identical slash characters. We need a
537 special filesystem-dependent file-name comparison function.
539 Actually, even on Unix I would use realpath() or its work-
540 alike before comparing. Then all the code above which
541 removes excess slashes and dots could simply go away. */
542 if (!strncmp (p
, name
, len
)
543 && (p
[len
] == '\0' || p
[len
] == DIRNAME_SEPARATOR
))
545 /* Found it in the search path, remove old copy */
547 p
--; /* Back over leading separator */
548 if (prefix
> p
- *which_path
)
549 goto skip_dup
; /* Same dir twice in one cmd */
550 strcpy (p
, &p
[len
+ 1]); /* Copy from next \0 or : */
552 p
= strchr (p
, DIRNAME_SEPARATOR
);
562 tinybuf
[0] = DIRNAME_SEPARATOR
;
565 /* If we have already tacked on a name(s) in this command, be sure they stay
566 on the front as we tack on some more. */
573 temp
= concat (old
, tinybuf
, name
, NULL
);
575 *which_path
= concat (temp
, "", &old
[prefix
], NULL
);
576 prefix
= strlen (temp
);
581 *which_path
= concat (name
, (old
[0] ? tinybuf
: old
), old
, NULL
);
582 prefix
= strlen (name
);
590 while (*dirname
!= '\0');
595 source_info (char *ignore
, int from_tty
)
597 register struct symtab
*s
= current_source_symtab
;
601 printf_filtered ("No current source file.\n");
604 printf_filtered ("Current source file is %s\n", s
->filename
);
606 printf_filtered ("Compilation directory is %s\n", s
->dirname
);
608 printf_filtered ("Located in %s\n", s
->fullname
);
610 printf_filtered ("Contains %d line%s.\n", s
->nlines
,
611 s
->nlines
== 1 ? "" : "s");
613 printf_filtered ("Source language is %s.\n", language_str (s
->language
));
614 printf_filtered ("Compiled with %s debugging format.\n", s
->debugformat
);
615 printf_filtered ("%s preprocessor macro info.\n",
616 s
->macro_table
? "Includes" : "Does not include");
620 /* Return True if the file NAME exists and is a regular file */
622 is_regular_file (const char *name
)
625 const int status
= stat (name
, &st
);
627 /* Stat should never fail except when the file does not exist.
628 If stat fails, analyze the source of error and return True
629 unless the file does not exist, to avoid returning false results
630 on obscure systems where stat does not work as expected.
633 return (errno
!= ENOENT
);
635 return S_ISREG (st
.st_mode
);
638 /* Open a file named STRING, searching path PATH (dir names sep by some char)
639 using mode MODE and protection bits PROT in the calls to open.
641 If TRY_CWD_FIRST, try to open ./STRING before searching PATH.
642 (ie pretend the first element of PATH is "."). This also indicates
643 that a slash in STRING disables searching of the path (this is
644 so that "exec-file ./foo" or "symbol-file ./foo" insures that you
645 get that particular version of foo or an error message).
647 If FILENAME_OPENED is non-null, set it to a newly allocated string naming
648 the actual file opened (this string will always start with a "/"). We
649 have to take special pains to avoid doubling the "/" between the directory
650 and the file, sigh! Emacs gets confuzzed by this when we print the
653 If a file is found, return the descriptor.
654 Otherwise, return -1, with errno set for the last name we tried to open. */
656 /* >>>> This should only allow files of certain types,
657 >>>> eg executable, non-directory */
659 openp (const char *path
, int try_cwd_first
, const char *string
,
661 char **filename_opened
)
664 register char *filename
;
673 #if defined(_WIN32) || defined(__CYGWIN__)
677 if ((try_cwd_first
|| IS_ABSOLUTE_PATH (string
)) && is_regular_file (string
))
680 filename
= alloca (strlen (string
) + 1);
681 strcpy (filename
, string
);
682 fd
= open (filename
, mode
, prot
);
685 for (i
= 0; string
[i
]; i
++)
686 if (IS_DIR_SEPARATOR (string
[i
]))
691 while (string
[0] == '.' && IS_DIR_SEPARATOR (string
[1]))
694 alloclen
= strlen (path
) + strlen (string
) + 2;
695 filename
= alloca (alloclen
);
697 for (p
= path
; p
; p
= p1
? p1
+ 1 : 0)
699 p1
= strchr (p
, DIRNAME_SEPARATOR
);
705 if (len
== 4 && p
[0] == '$' && p
[1] == 'c'
706 && p
[2] == 'w' && p
[3] == 'd')
708 /* Name is $cwd -- insert current directory name instead. */
711 /* First, realloc the filename buffer if too short. */
712 len
= strlen (current_directory
);
713 newlen
= len
+ strlen (string
) + 2;
714 if (newlen
> alloclen
)
717 filename
= alloca (alloclen
);
719 strcpy (filename
, current_directory
);
723 /* Normal file name in path -- just use it. */
724 strncpy (filename
, p
, len
);
728 /* Remove trailing slashes */
729 while (len
> 0 && IS_DIR_SEPARATOR (filename
[len
- 1]))
732 strcat (filename
+ len
, SLASH_STRING
);
733 strcat (filename
, string
);
735 if (is_regular_file (filename
))
737 fd
= open (filename
, mode
);
746 /* If a file was opened, canonicalize its filename. Use xfullpath
747 rather than gdb_realpath to avoid resolving the basename part
748 of filenames when the associated file is a symbolic link. This
749 fixes a potential inconsistency between the filenames known to
750 GDB and the filenames it prints in the annotations. */
752 *filename_opened
= NULL
;
753 else if (IS_ABSOLUTE_PATH (filename
))
754 *filename_opened
= xfullpath (filename
);
757 /* Beware the // my son, the Emacs barfs, the botch that catch... */
759 char *f
= concat (current_directory
,
760 IS_DIR_SEPARATOR (current_directory
[strlen (current_directory
) - 1])
763 *filename_opened
= xfullpath (f
);
772 /* This is essentially a convenience, for clients that want the behaviour
773 of openp, using source_path, but that really don't want the file to be
774 opened but want instead just to know what the full pathname is (as
775 qualified against source_path).
777 The current working directory is searched first.
779 If the file was found, this function returns 1, and FULL_PATHNAME is
780 set to the fully-qualified pathname.
782 Else, this functions returns 0, and FULL_PATHNAME is set to NULL.
785 source_full_path_of (char *filename
, char **full_pathname
)
789 fd
= openp (source_path
, 1, filename
, O_RDONLY
, 0, full_pathname
);
792 *full_pathname
= NULL
;
801 /* Open a source file given a symtab S. Returns a file descriptor or
802 negative number for error. */
805 open_source_file (struct symtab
*s
)
807 char *path
= source_path
;
812 /* Quick way out if we already know its full name */
815 result
= open (s
->fullname
, OPEN_MODE
);
818 /* Didn't work -- free old one, try again. */
819 xmfree (s
->objfile
->md
, s
->fullname
);
823 if (s
->dirname
!= NULL
)
825 /* Replace a path entry of $cdir with the compilation directory name */
827 /* We cast strstr's result in case an ANSIhole has made it const,
828 which produces a "required warning" when assigned to a nonconst. */
829 p
= (char *) strstr (source_path
, "$cdir");
830 if (p
&& (p
== path
|| p
[-1] == DIRNAME_SEPARATOR
)
831 && (p
[cdir_len
] == DIRNAME_SEPARATOR
|| p
[cdir_len
] == '\0'))
836 alloca (strlen (source_path
) + 1 + strlen (s
->dirname
) + 1);
837 len
= p
- source_path
;
838 strncpy (path
, source_path
, len
); /* Before $cdir */
839 strcpy (path
+ len
, s
->dirname
); /* new stuff */
840 strcat (path
+ len
, source_path
+ len
+ cdir_len
); /* After $cdir */
844 result
= openp (path
, 0, s
->filename
, OPEN_MODE
, 0, &s
->fullname
);
847 /* Didn't work. Try using just the basename. */
848 p
= lbasename (s
->filename
);
849 if (p
!= s
->filename
)
850 result
= openp (path
, 0, p
, OPEN_MODE
, 0, &s
->fullname
);
855 fullname
= s
->fullname
;
856 s
->fullname
= mstrsave (s
->objfile
->md
, s
->fullname
);
862 /* Return the path to the source file associated with symtab. Returns NULL
866 symtab_to_filename (struct symtab
*s
)
873 /* If we've seen the file before, just return fullname. */
878 /* Try opening the file to setup fullname */
880 fd
= open_source_file (s
);
882 return s
->filename
; /* File not found. Just use short name */
884 /* Found the file. Cleanup and return the full name */
891 /* Create and initialize the table S->line_charpos that records
892 the positions of the lines in the source file, which is assumed
893 to be open on descriptor DESC.
894 All set S->nlines to the number of such lines. */
897 find_source_lines (struct symtab
*s
, int desc
)
900 register char *data
, *p
, *end
;
902 int lines_allocated
= 1000;
907 line_charpos
= (int *) xmmalloc (s
->objfile
->md
,
908 lines_allocated
* sizeof (int));
909 if (fstat (desc
, &st
) < 0)
910 perror_with_name (s
->filename
);
912 if (s
&& s
->objfile
&& s
->objfile
->obfd
)
913 mtime
= bfd_get_mtime (s
->objfile
->obfd
);
915 mtime
= bfd_get_mtime (exec_bfd
);
917 if (mtime
&& mtime
< st
.st_mtime
)
919 warning ("Source file is more recent than executable.\n");
922 #ifdef LSEEK_NOT_LINEAR
926 /* Have to read it byte by byte to find out where the chars live */
928 line_charpos
[0] = lseek (desc
, 0, SEEK_CUR
);
930 while (myread (desc
, &c
, 1) > 0)
934 if (nlines
== lines_allocated
)
936 lines_allocated
*= 2;
938 (int *) xmrealloc (s
->objfile
->md
, (char *) line_charpos
,
939 sizeof (int) * lines_allocated
);
941 line_charpos
[nlines
++] = lseek (desc
, 0, SEEK_CUR
);
945 #else /* lseek linear. */
947 struct cleanup
*old_cleanups
;
949 /* st_size might be a large type, but we only support source files whose
950 size fits in an int. */
951 size
= (int) st
.st_size
;
953 /* Use malloc, not alloca, because this may be pretty large, and we may
954 run into various kinds of limits on stack size. */
955 data
= (char *) xmalloc (size
);
956 old_cleanups
= make_cleanup (xfree
, data
);
958 /* Reassign `size' to result of read for systems where \r\n -> \n. */
959 size
= myread (desc
, data
, size
);
961 perror_with_name (s
->filename
);
969 /* A newline at the end does not start a new line. */
972 if (nlines
== lines_allocated
)
974 lines_allocated
*= 2;
976 (int *) xmrealloc (s
->objfile
->md
, (char *) line_charpos
,
977 sizeof (int) * lines_allocated
);
979 line_charpos
[nlines
++] = p
- data
;
982 do_cleanups (old_cleanups
);
984 #endif /* lseek linear. */
987 (int *) xmrealloc (s
->objfile
->md
, (char *) line_charpos
,
988 nlines
* sizeof (int));
992 /* Return the character position of a line LINE in symtab S.
993 Return 0 if anything is invalid. */
995 #if 0 /* Currently unused */
998 source_line_charpos (struct symtab
*s
, int line
)
1002 if (!s
->line_charpos
|| line
<= 0)
1004 if (line
> s
->nlines
)
1006 return s
->line_charpos
[line
- 1];
1009 /* Return the line number of character position POS in symtab S. */
1012 source_charpos_line (register struct symtab
*s
, register int chr
)
1014 register int line
= 0;
1017 if (s
== 0 || s
->line_charpos
== 0)
1019 lnp
= s
->line_charpos
;
1020 /* Files are usually short, so sequential search is Ok */
1021 while (line
< s
->nlines
&& *lnp
<= chr
)
1026 if (line
>= s
->nlines
)
1034 /* Get full pathname and line number positions for a symtab.
1035 Return nonzero if line numbers may have changed.
1036 Set *FULLNAME to actual name of the file as found by `openp',
1037 or to 0 if the file is not found. */
1040 get_filename_and_charpos (struct symtab
*s
, char **fullname
)
1042 register int desc
, linenums_changed
= 0;
1044 desc
= open_source_file (s
);
1052 *fullname
= s
->fullname
;
1053 if (s
->line_charpos
== 0)
1054 linenums_changed
= 1;
1055 if (linenums_changed
)
1056 find_source_lines (s
, desc
);
1058 return linenums_changed
;
1061 /* Print text describing the full name of the source file S
1062 and the line number LINE and its corresponding character position.
1063 The text starts with two Ctrl-z so that the Emacs-GDB interface
1066 MID_STATEMENT is nonzero if the PC is not at the beginning of that line.
1068 Return 1 if successful, 0 if could not find the file. */
1071 identify_source_line (struct symtab
*s
, int line
, int mid_statement
,
1074 if (s
->line_charpos
== 0)
1075 get_filename_and_charpos (s
, (char **) NULL
);
1076 if (s
->fullname
== 0)
1078 if (line
> s
->nlines
)
1079 /* Don't index off the end of the line_charpos array. */
1081 annotate_source (s
->fullname
, line
, s
->line_charpos
[line
- 1],
1084 current_source_line
= line
;
1085 first_line_listed
= line
;
1086 last_line_listed
= line
;
1087 current_source_symtab
= s
;
1092 /* Print source lines from the file of symtab S,
1093 starting with line number LINE and stopping before line number STOPLINE. */
1095 static void print_source_lines_base (struct symtab
*s
, int line
, int stopline
,
1098 print_source_lines_base (struct symtab
*s
, int line
, int stopline
, int noerror
)
1102 register FILE *stream
;
1103 int nlines
= stopline
- line
;
1105 /* Regardless of whether we can open the file, set current_source_symtab. */
1106 current_source_symtab
= s
;
1107 current_source_line
= line
;
1108 first_line_listed
= line
;
1110 /* If printing of source lines is disabled, just print file and line number */
1111 if (ui_out_test_flags (uiout
, ui_source_list
))
1113 /* Only prints "No such file or directory" once */
1114 if ((s
!= last_source_visited
) || (!last_source_error
))
1116 last_source_visited
= s
;
1117 desc
= open_source_file (s
);
1121 desc
= last_source_error
;
1133 last_source_error
= desc
;
1137 char *name
= alloca (strlen (s
->filename
) + 100);
1138 sprintf (name
, "%d\t%s", line
, s
->filename
);
1139 print_sys_errmsg (name
, errno
);
1142 ui_out_field_int (uiout
, "line", line
);
1143 ui_out_text (uiout
, "\tin ");
1144 ui_out_field_string (uiout
, "file", s
->filename
);
1145 ui_out_text (uiout
, "\n");
1150 last_source_error
= 0;
1152 if (s
->line_charpos
== 0)
1153 find_source_lines (s
, desc
);
1155 if (line
< 1 || line
> s
->nlines
)
1158 error ("Line number %d out of range; %s has %d lines.",
1159 line
, s
->filename
, s
->nlines
);
1162 if (lseek (desc
, s
->line_charpos
[line
- 1], 0) < 0)
1165 perror_with_name (s
->filename
);
1168 stream
= fdopen (desc
, FDOPEN_MODE
);
1171 while (nlines
-- > 0)
1178 last_line_listed
= current_source_line
;
1179 sprintf (buf
, "%d\t", current_source_line
++);
1180 ui_out_text (uiout
, buf
);
1183 if (c
< 040 && c
!= '\t' && c
!= '\n' && c
!= '\r')
1185 sprintf (buf
, "^%c", c
+ 0100);
1186 ui_out_text (uiout
, buf
);
1189 ui_out_text (uiout
, "^?");
1190 #ifdef CRLF_SOURCE_FILES
1193 /* Skip a \r character, but only before a \n. */
1194 int c1
= fgetc (stream
);
1197 printf_filtered ("^%c", c
+ 0100);
1199 ungetc (c1
, stream
);
1204 sprintf (buf
, "%c", c
);
1205 ui_out_text (uiout
, buf
);
1208 while (c
!= '\n' && (c
= fgetc (stream
)) >= 0);
1214 /* Show source lines from the file of symtab S, starting with line
1215 number LINE and stopping before line number STOPLINE. If this is the
1216 not the command line version, then the source is shown in the source
1217 window otherwise it is simply printed */
1220 print_source_lines (struct symtab
*s
, int line
, int stopline
, int noerror
)
1222 print_source_lines_base (s
, line
, stopline
, noerror
);
1225 /* Print a list of files and line numbers which a user may choose from
1226 in order to list a function which was specified ambiguously (as with
1227 `list classname::overloadedfuncname', or 'list objectiveCSelector:).
1228 The vector in SALS provides the filenames and line numbers.
1229 NOTE: some of the SALS may have no filename or line information! */
1232 ambiguous_line_spec (struct symtabs_and_lines
*sals
)
1236 for (i
= 0; i
< sals
->nelts
; ++i
)
1237 printf_filtered ("file: \"%s\", line number: %d\n",
1238 sals
->sals
[i
].symtab
->filename
, sals
->sals
[i
].line
);
1241 /* Print info on range of pc's in a specified line. */
1244 line_info (char *arg
, int from_tty
)
1246 struct symtabs_and_lines sals
;
1247 struct symtab_and_line sal
;
1248 CORE_ADDR start_pc
, end_pc
;
1251 init_sal (&sal
); /* initialize to zeroes */
1255 sal
.symtab
= current_source_symtab
;
1256 sal
.line
= last_line_listed
;
1258 sals
.sals
= (struct symtab_and_line
*)
1259 xmalloc (sizeof (struct symtab_and_line
));
1264 sals
= decode_line_spec_1 (arg
, 0);
1269 /* C++ More than one line may have been specified, as when the user
1270 specifies an overloaded function name. Print info on them all. */
1271 for (i
= 0; i
< sals
.nelts
; i
++)
1275 if (sal
.symtab
== 0)
1277 printf_filtered ("No line number information available");
1280 /* This is useful for "info line *0x7f34". If we can't tell the
1281 user about a source line, at least let them have the symbolic
1283 printf_filtered (" for address ");
1285 print_address (sal
.pc
, gdb_stdout
);
1288 printf_filtered (".");
1289 printf_filtered ("\n");
1291 else if (sal
.line
> 0
1292 && find_line_pc_range (sal
, &start_pc
, &end_pc
))
1294 if (start_pc
== end_pc
)
1296 printf_filtered ("Line %d of \"%s\"",
1297 sal
.line
, sal
.symtab
->filename
);
1299 printf_filtered (" is at address ");
1300 print_address (start_pc
, gdb_stdout
);
1302 printf_filtered (" but contains no code.\n");
1306 printf_filtered ("Line %d of \"%s\"",
1307 sal
.line
, sal
.symtab
->filename
);
1309 printf_filtered (" starts at address ");
1310 print_address (start_pc
, gdb_stdout
);
1312 printf_filtered (" and ends at ");
1313 print_address (end_pc
, gdb_stdout
);
1314 printf_filtered (".\n");
1317 /* x/i should display this line's code. */
1318 set_next_address (start_pc
);
1320 /* Repeating "info line" should do the following line. */
1321 last_line_listed
= sal
.line
+ 1;
1323 /* If this is the only line, show the source code. If it could
1324 not find the file, don't do anything special. */
1325 if (annotation_level
&& sals
.nelts
== 1)
1326 identify_source_line (sal
.symtab
, sal
.line
, 0, start_pc
);
1329 /* Is there any case in which we get here, and have an address
1330 which the user would want to see? If we have debugging symbols
1331 and no line numbers? */
1332 printf_filtered ("Line number %d is out of range for \"%s\".\n",
1333 sal
.line
, sal
.symtab
->filename
);
1338 /* Commands to search the source file for a regexp. */
1342 forward_search_command (char *regex
, int from_tty
)
1346 register FILE *stream
;
1350 line
= last_line_listed
+ 1;
1352 msg
= (char *) re_comp (regex
);
1356 if (current_source_symtab
== 0)
1357 select_source_symtab (0);
1359 desc
= open_source_file (current_source_symtab
);
1361 perror_with_name (current_source_symtab
->filename
);
1363 if (current_source_symtab
->line_charpos
== 0)
1364 find_source_lines (current_source_symtab
, desc
);
1366 if (line
< 1 || line
> current_source_symtab
->nlines
)
1369 error ("Expression not found");
1372 if (lseek (desc
, current_source_symtab
->line_charpos
[line
- 1], 0) < 0)
1375 perror_with_name (current_source_symtab
->filename
);
1378 stream
= fdopen (desc
, FDOPEN_MODE
);
1382 static char *buf
= NULL
;
1384 int cursize
, newsize
;
1387 buf
= xmalloc (cursize
);
1396 if (p
- buf
== cursize
)
1398 newsize
= cursize
+ cursize
/ 2;
1399 buf
= xrealloc (buf
, newsize
);
1404 while (c
!= '\n' && (c
= getc (stream
)) >= 0);
1406 #ifdef CRLF_SOURCE_FILES
1407 /* Remove the \r, if any, at the end of the line, otherwise
1408 regular expressions that end with $ or \n won't work. */
1409 if (p
- buf
> 1 && p
[-2] == '\r')
1416 /* we now have a source line in buf, null terminate and match */
1418 if (re_exec (buf
) > 0)
1422 print_source_lines (current_source_symtab
, line
, line
+ 1, 0);
1423 set_internalvar (lookup_internalvar ("_"),
1424 value_from_longest (builtin_type_int
,
1426 current_source_line
= max (line
- lines_to_list
/ 2, 1);
1432 printf_filtered ("Expression not found\n");
1438 reverse_search_command (char *regex
, int from_tty
)
1442 register FILE *stream
;
1446 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
);
1478 /* FIXME!!! We walk right off the end of buf if we get a long line!!! */
1479 char buf
[4096]; /* Should be reasonable??? */
1480 register char *p
= buf
;
1489 while (c
!= '\n' && (c
= getc (stream
)) >= 0);
1491 #ifdef CRLF_SOURCE_FILES
1492 /* Remove the \r, if any, at the end of the line, otherwise
1493 regular expressions that end with $ or \n won't work. */
1494 if (p
- buf
> 1 && p
[-2] == '\r')
1501 /* We now have a source line in buf; null terminate and match. */
1503 if (re_exec (buf
) > 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);
1515 if (fseek (stream
, current_source_symtab
->line_charpos
[line
- 1], 0) < 0)
1518 perror_with_name (current_source_symtab
->filename
);
1522 printf_filtered ("Expression not found\n");
1528 _initialize_source (void)
1530 struct cmd_list_element
*c
;
1531 current_source_symtab
= 0;
1532 init_source_path ();
1534 /* The intention is to use POSIX Basic Regular Expressions.
1535 Always use the GNU regex routine for consistency across all hosts.
1536 Our current GNU regex.c does not have all the POSIX features, so this is
1537 just an approximation. */
1538 re_set_syntax (RE_SYNTAX_GREP
);
1540 c
= add_cmd ("directory", class_files
, directory_command
,
1541 "Add directory DIR to beginning of search path for source files.\n\
1542 Forget cached info on source file locations and line positions.\n\
1543 DIR can also be $cwd for the current working directory, or $cdir for the\n\
1544 directory in which the source file was compiled into object code.\n\
1545 With no argument, reset the search path to $cdir:$cwd, the default.",
1549 add_com_alias ("use", "directory", class_files
, 0);
1551 set_cmd_completer (c
, filename_completer
);
1553 add_cmd ("directories", no_class
, show_directories
,
1554 "Current search path for finding source files.\n\
1555 $cwd in the path means the current working directory.\n\
1556 $cdir in the path means the compilation directory of the source file.",
1561 add_com_alias ("D", "directory", class_files
, 0);
1562 add_cmd ("ld", no_class
, show_directories
,
1563 "Current search path for finding source files.\n\
1564 $cwd in the path means the current working directory.\n\
1565 $cdir in the path means the compilation directory of the source file.",
1569 add_info ("source", source_info
,
1570 "Information about the current source file.");
1572 add_info ("line", line_info
,
1573 concat ("Core addresses of the code for a source line.\n\
1574 Line can be specified as\n\
1575 LINENUM, to list around that line in current file,\n\
1576 FILE:LINENUM, to list around that line in that file,\n\
1577 FUNCTION, to list around beginning of that function,\n\
1578 FILE:FUNCTION, to distinguish among like-named static functions.\n\
1580 Default is to describe the last source line that was listed.\n\n\
1581 This sets the default address for \"x\" to the line's first instruction\n\
1582 so that \"x/i\" suffices to start examining the machine code.\n\
1583 The address is also stored as the value of \"$_\".", NULL
));
1585 add_com ("forward-search", class_files
, forward_search_command
,
1586 "Search for regular expression (see regex(3)) from last line listed.\n\
1587 The matching line number is also stored as the value of \"$_\".");
1588 add_com_alias ("search", "forward-search", class_files
, 0);
1590 add_com ("reverse-search", class_files
, reverse_search_command
,
1591 "Search backward for regular expression (see regex(3)) from last line listed.\n\
1592 The matching line number is also stored as the value of \"$_\".");
1596 add_com_alias ("/", "forward-search", class_files
, 0);
1597 add_com_alias ("?", "reverse-search", class_files
, 0);
1601 (add_set_cmd ("listsize", class_support
, var_uinteger
,
1602 (char *) &lines_to_list
,
1603 "Set number of source lines gdb will list by default.",
This page took 0.116067 seconds and 4 git commands to generate.