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
;
161 /* If the current source file for listing is not set, try and get a default.
162 Usually called before get_current_source_symtab_and_line() is called.
163 It may err out if a default cannot be determined.
164 We must be cautious about where it is called, as it can recurse as the
165 process of determining a new default may call the caller!
166 Use get_current_source_symtab_and_line only to get whatever
167 we have without erroring out or trying to get a default. */
170 set_default_source_symtab_and_line (void)
172 struct symtab_and_line cursal
;
174 if (!have_full_symbols () && !have_partial_symbols ())
175 error ("No symbol table is loaded. Use the \"file\" command.");
177 /* Pull in a current source symtab if necessary */
178 if (current_source_symtab
== 0)
179 select_source_symtab (0);
182 /* Return the current default file for listing and next line to list
183 (the returned sal pc and end fields are not valid.)
184 and set the current default to whatever is in SAL.
185 NOTE: The returned sal pc and end fields are not valid. */
187 struct symtab_and_line
188 set_current_source_symtab_and_line (const struct symtab_and_line
*sal
)
190 struct symtab_and_line cursal
;
192 cursal
.symtab
= current_source_symtab
;
193 cursal
.line
= current_source_line
;
195 current_source_symtab
= sal
->symtab
;
196 current_source_line
= sal
->line
;
203 /* Reset any information stored about a default file and line to print. */
206 clear_current_source_symtab_and_line (void)
208 current_source_symtab
= 0;
209 current_source_line
= 0;
212 /* Set the source file default for the "list" command to be S.
214 If S is NULL, and we don't have a default, find one. This
215 should only be called when the user actually tries to use the
216 default, since we produce an error if we can't find a reasonable
217 default. Also, since this can cause symbols to be read, doing it
218 before we need to would make things slower than necessary. */
221 select_source_symtab (register struct symtab
*s
)
223 struct symtabs_and_lines sals
;
224 struct symtab_and_line sal
;
225 struct partial_symtab
*ps
;
226 struct partial_symtab
*cs_pst
= 0;
231 current_source_symtab
= s
;
232 current_source_line
= 1;
236 if (current_source_symtab
)
239 /* Make the default place to list be the function `main'
241 if (lookup_symbol (main_name (), 0, VAR_NAMESPACE
, 0, NULL
))
243 sals
= decode_line_spec (main_name (), 1);
246 current_source_symtab
= sal
.symtab
;
247 current_source_line
= max (sal
.line
- (lines_to_list
- 1), 1);
248 if (current_source_symtab
)
252 /* All right; find the last file in the symtab list (ignoring .h's). */
254 current_source_line
= 1;
256 for (ofp
= object_files
; ofp
!= NULL
; ofp
= ofp
->next
)
258 for (s
= ofp
->symtabs
; s
; s
= s
->next
)
260 char *name
= s
->filename
;
261 int len
= strlen (name
);
262 if (!(len
> 2 && (STREQ (&name
[len
- 2], ".h"))))
264 current_source_symtab
= s
;
268 if (current_source_symtab
)
271 /* Howabout the partial symbol tables? */
273 for (ofp
= object_files
; ofp
!= NULL
; ofp
= ofp
->next
)
275 for (ps
= ofp
->psymtabs
; ps
!= NULL
; ps
= ps
->next
)
277 char *name
= ps
->filename
;
278 int len
= strlen (name
);
279 if (!(len
> 2 && (STREQ (&name
[len
- 2], ".h"))))
289 internal_error (__FILE__
, __LINE__
,
290 "select_source_symtab: "
291 "readin pst found and no symtabs.");
295 current_source_symtab
= PSYMTAB_TO_SYMTAB (cs_pst
);
298 if (current_source_symtab
)
301 error ("Can't find a default source file");
305 show_directories (char *ignore
, int from_tty
)
307 puts_filtered ("Source directories searched: ");
308 puts_filtered (source_path
);
309 puts_filtered ("\n");
312 /* Forget what we learned about line positions in source files, and
313 which directories contain them; must check again now since files
314 may be found in a different directory now. */
317 forget_cached_source_info (void)
319 register struct symtab
*s
;
320 register struct objfile
*objfile
;
321 struct partial_symtab
*pst
;
323 for (objfile
= object_files
; objfile
!= NULL
; objfile
= objfile
->next
)
325 for (s
= objfile
->symtabs
; s
!= NULL
; s
= s
->next
)
327 if (s
->line_charpos
!= NULL
)
329 xmfree (objfile
->md
, s
->line_charpos
);
330 s
->line_charpos
= NULL
;
332 if (s
->fullname
!= NULL
)
334 xmfree (objfile
->md
, s
->fullname
);
339 ALL_OBJFILE_PSYMTABS (objfile
, pst
)
341 if (pst
->fullname
!= NULL
)
343 xfree (pst
->fullname
);
344 pst
->fullname
= NULL
;
351 init_source_path (void)
355 sprintf (buf
, "$cdir%c$cwd", DIRNAME_SEPARATOR
);
356 source_path
= xstrdup (buf
);
357 forget_cached_source_info ();
360 /* Add zero or more directories to the front of the source path. */
363 directory_command (char *dirname
, int from_tty
)
366 /* FIXME, this goes to "delete dir"... */
369 if (from_tty
&& query ("Reinitialize source path to empty? "))
377 mod_path (dirname
, &source_path
);
378 last_source_visited
= NULL
;
381 show_directories ((char *) 0, from_tty
);
382 forget_cached_source_info ();
385 /* Add zero or more directories to the front of an arbitrary path. */
388 mod_path (char *dirname
, char **which_path
)
390 char *old
= *which_path
;
396 dirname
= xstrdup (dirname
);
397 make_cleanup (xfree
, dirname
);
401 char *name
= dirname
;
406 char *separator
= strchr (name
, DIRNAME_SEPARATOR
);
407 char *space
= strchr (name
, ' ');
408 char *tab
= strchr (name
, '\t');
410 if (separator
== 0 && space
== 0 && tab
== 0)
411 p
= dirname
= name
+ strlen (name
);
415 if (separator
!= 0 && (p
== 0 || separator
< p
))
417 if (space
!= 0 && (p
== 0 || space
< p
))
419 if (tab
!= 0 && (p
== 0 || tab
< p
))
422 while (*dirname
== DIRNAME_SEPARATOR
429 if (!(IS_DIR_SEPARATOR (*name
) && p
<= name
+ 1) /* "/" */
430 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
431 /* On MS-DOS and MS-Windows, h:\ is different from h: */
432 && !(p
== name
+ 3 && name
[1] == ':') /* "d:/" */
434 && IS_DIR_SEPARATOR (p
[-1]))
435 /* Sigh. "foo/" => "foo" */
439 while (p
> name
&& p
[-1] == '.')
443 /* "." => getwd (). */
444 name
= current_directory
;
447 else if (p
> name
+ 1 && IS_DIR_SEPARATOR (p
[-2]))
457 /* "...foo/." => "...foo". */
468 name
= tilde_expand (name
);
469 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
470 else if (IS_ABSOLUTE_PATH (name
) && p
== name
+ 2) /* "d:" => "d:." */
471 name
= concat (name
, ".", NULL
);
473 else if (!IS_ABSOLUTE_PATH (name
) && name
[0] != '$')
474 name
= concat (current_directory
, SLASH_STRING
, name
, NULL
);
476 name
= savestring (name
, p
- name
);
477 make_cleanup (xfree
, name
);
479 /* Unless it's a variable, check existence. */
482 /* These are warnings, not errors, since we don't want a
483 non-existent directory in a .gdbinit file to stop processing
484 of the .gdbinit file.
486 Whether they get added to the path is more debatable. Current
487 answer is yes, in case the user wants to go make the directory
488 or whatever. If the directory continues to not exist/not be
489 a directory/etc, then having them in the path should be
491 if (stat (name
, &st
) < 0)
493 int save_errno
= errno
;
494 fprintf_unfiltered (gdb_stderr
, "Warning: ");
495 print_sys_errmsg (name
, save_errno
);
497 else if ((st
.st_mode
& S_IFMT
) != S_IFDIR
)
498 warning ("%s is not a directory.", name
);
503 register unsigned int len
= strlen (name
);
508 /* FIXME: strncmp loses in interesting ways on MS-DOS and
509 MS-Windows because of case-insensitivity and two different
510 but functionally identical slash characters. We need a
511 special filesystem-dependent file-name comparison function.
513 Actually, even on Unix I would use realpath() or its work-
514 alike before comparing. Then all the code above which
515 removes excess slashes and dots could simply go away. */
516 if (!strncmp (p
, name
, len
)
517 && (p
[len
] == '\0' || p
[len
] == DIRNAME_SEPARATOR
))
519 /* Found it in the search path, remove old copy */
521 p
--; /* Back over leading separator */
522 if (prefix
> p
- *which_path
)
523 goto skip_dup
; /* Same dir twice in one cmd */
524 strcpy (p
, &p
[len
+ 1]); /* Copy from next \0 or : */
526 p
= strchr (p
, DIRNAME_SEPARATOR
);
536 tinybuf
[0] = DIRNAME_SEPARATOR
;
539 /* 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. */
546 temp
= concat (old
, tinybuf
, name
, NULL
);
548 *which_path
= concat (temp
, "", &old
[prefix
], NULL
);
549 prefix
= strlen (temp
);
554 *which_path
= concat (name
, (old
[0] ? tinybuf
: old
), old
, NULL
);
555 prefix
= strlen (name
);
563 while (*dirname
!= '\0');
568 source_info (char *ignore
, int from_tty
)
570 register struct symtab
*s
= current_source_symtab
;
574 printf_filtered ("No current source file.\n");
577 printf_filtered ("Current source file is %s\n", s
->filename
);
579 printf_filtered ("Compilation directory is %s\n", s
->dirname
);
581 printf_filtered ("Located in %s\n", s
->fullname
);
583 printf_filtered ("Contains %d line%s.\n", s
->nlines
,
584 s
->nlines
== 1 ? "" : "s");
586 printf_filtered ("Source language is %s.\n", language_str (s
->language
));
587 printf_filtered ("Compiled with %s debugging format.\n", s
->debugformat
);
588 printf_filtered ("%s preprocessor macro info.\n",
589 s
->macro_table
? "Includes" : "Does not include");
593 /* Return True if the file NAME exists and is a regular file */
595 is_regular_file (const char *name
)
598 const int status
= stat (name
, &st
);
600 /* Stat should never fail except when the file does not exist.
601 If stat fails, analyze the source of error and return True
602 unless the file does not exist, to avoid returning false results
603 on obscure systems where stat does not work as expected.
606 return (errno
!= ENOENT
);
608 return S_ISREG (st
.st_mode
);
611 /* Open a file named STRING, searching path PATH (dir names sep by some char)
612 using mode MODE and protection bits PROT in the calls to open.
614 If TRY_CWD_FIRST, try to open ./STRING before searching PATH.
615 (ie pretend the first element of PATH is "."). This also indicates
616 that a slash in STRING disables searching of the path (this is
617 so that "exec-file ./foo" or "symbol-file ./foo" insures that you
618 get that particular version of foo or an error message).
620 If FILENAME_OPENED is non-null, set it to a newly allocated string naming
621 the actual file opened (this string will always start with a "/"). We
622 have to take special pains to avoid doubling the "/" between the directory
623 and the file, sigh! Emacs gets confuzzed by this when we print the
626 If a file is found, return the descriptor.
627 Otherwise, return -1, with errno set for the last name we tried to open. */
629 /* >>>> This should only allow files of certain types,
630 >>>> eg executable, non-directory */
632 openp (const char *path
, int try_cwd_first
, const char *string
,
634 char **filename_opened
)
637 register char *filename
;
646 #if defined(_WIN32) || defined(__CYGWIN__)
650 if ((try_cwd_first
|| IS_ABSOLUTE_PATH (string
)) && is_regular_file (string
))
653 filename
= alloca (strlen (string
) + 1);
654 strcpy (filename
, string
);
655 fd
= open (filename
, mode
, prot
);
658 for (i
= 0; string
[i
]; i
++)
659 if (IS_DIR_SEPARATOR (string
[i
]))
664 while (string
[0] == '.' && IS_DIR_SEPARATOR (string
[1]))
667 alloclen
= strlen (path
) + strlen (string
) + 2;
668 filename
= alloca (alloclen
);
670 for (p
= path
; p
; p
= p1
? p1
+ 1 : 0)
672 p1
= strchr (p
, DIRNAME_SEPARATOR
);
678 if (len
== 4 && p
[0] == '$' && p
[1] == 'c'
679 && p
[2] == 'w' && p
[3] == 'd')
681 /* Name is $cwd -- insert current directory name instead. */
684 /* First, realloc the filename buffer if too short. */
685 len
= strlen (current_directory
);
686 newlen
= len
+ strlen (string
) + 2;
687 if (newlen
> alloclen
)
690 filename
= alloca (alloclen
);
692 strcpy (filename
, current_directory
);
696 /* Normal file name in path -- just use it. */
697 strncpy (filename
, p
, len
);
701 /* Remove trailing slashes */
702 while (len
> 0 && IS_DIR_SEPARATOR (filename
[len
- 1]))
705 strcat (filename
+ len
, SLASH_STRING
);
706 strcat (filename
, string
);
708 if (is_regular_file (filename
))
710 fd
= open (filename
, mode
);
719 /* If a file was opened, canonicalize its filename. Use xfullpath
720 rather than gdb_realpath to avoid resolving the basename part
721 of filenames when the associated file is a symbolic link. This
722 fixes a potential inconsistency between the filenames known to
723 GDB and the filenames it prints in the annotations. */
725 *filename_opened
= NULL
;
726 else if (IS_ABSOLUTE_PATH (filename
))
727 *filename_opened
= xfullpath (filename
);
730 /* Beware the // my son, the Emacs barfs, the botch that catch... */
732 char *f
= concat (current_directory
,
733 IS_DIR_SEPARATOR (current_directory
[strlen (current_directory
) - 1])
736 *filename_opened
= xfullpath (f
);
745 /* This is essentially a convenience, for clients that want the behaviour
746 of openp, using source_path, but that really don't want the file to be
747 opened but want instead just to know what the full pathname is (as
748 qualified against source_path).
750 The current working directory is searched first.
752 If the file was found, this function returns 1, and FULL_PATHNAME is
753 set to the fully-qualified pathname.
755 Else, this functions returns 0, and FULL_PATHNAME is set to NULL.
758 source_full_path_of (char *filename
, char **full_pathname
)
762 fd
= openp (source_path
, 1, filename
, O_RDONLY
, 0, full_pathname
);
765 *full_pathname
= NULL
;
774 /* Open a source file given a symtab S. Returns a file descriptor or
775 negative number for error. */
778 open_source_file (struct symtab
*s
)
780 char *path
= source_path
;
785 /* Quick way out if we already know its full name */
788 result
= open (s
->fullname
, OPEN_MODE
);
791 /* Didn't work -- free old one, try again. */
792 xmfree (s
->objfile
->md
, s
->fullname
);
796 if (s
->dirname
!= NULL
)
798 /* Replace a path entry of $cdir with the compilation directory name */
800 /* We cast strstr's result in case an ANSIhole has made it const,
801 which produces a "required warning" when assigned to a nonconst. */
802 p
= (char *) strstr (source_path
, "$cdir");
803 if (p
&& (p
== path
|| p
[-1] == DIRNAME_SEPARATOR
)
804 && (p
[cdir_len
] == DIRNAME_SEPARATOR
|| p
[cdir_len
] == '\0'))
809 alloca (strlen (source_path
) + 1 + strlen (s
->dirname
) + 1);
810 len
= p
- source_path
;
811 strncpy (path
, source_path
, len
); /* Before $cdir */
812 strcpy (path
+ len
, s
->dirname
); /* new stuff */
813 strcat (path
+ len
, source_path
+ len
+ cdir_len
); /* After $cdir */
817 result
= openp (path
, 0, s
->filename
, OPEN_MODE
, 0, &s
->fullname
);
820 /* Didn't work. Try using just the basename. */
821 p
= lbasename (s
->filename
);
822 if (p
!= s
->filename
)
823 result
= openp (path
, 0, p
, OPEN_MODE
, 0, &s
->fullname
);
828 fullname
= s
->fullname
;
829 s
->fullname
= mstrsave (s
->objfile
->md
, s
->fullname
);
835 /* Return the path to the source file associated with symtab. Returns NULL
839 symtab_to_filename (struct symtab
*s
)
846 /* If we've seen the file before, just return fullname. */
851 /* Try opening the file to setup fullname */
853 fd
= open_source_file (s
);
855 return s
->filename
; /* File not found. Just use short name */
857 /* Found the file. Cleanup and return the full name */
864 /* Create and initialize the table S->line_charpos that records
865 the positions of the lines in the source file, which is assumed
866 to be open on descriptor DESC.
867 All set S->nlines to the number of such lines. */
870 find_source_lines (struct symtab
*s
, int desc
)
873 register char *data
, *p
, *end
;
875 int lines_allocated
= 1000;
880 line_charpos
= (int *) xmmalloc (s
->objfile
->md
,
881 lines_allocated
* sizeof (int));
882 if (fstat (desc
, &st
) < 0)
883 perror_with_name (s
->filename
);
885 if (s
&& s
->objfile
&& s
->objfile
->obfd
)
886 mtime
= bfd_get_mtime (s
->objfile
->obfd
);
888 mtime
= bfd_get_mtime (exec_bfd
);
890 if (mtime
&& mtime
< st
.st_mtime
)
892 warning ("Source file is more recent than executable.\n");
895 #ifdef LSEEK_NOT_LINEAR
899 /* Have to read it byte by byte to find out where the chars live */
901 line_charpos
[0] = lseek (desc
, 0, SEEK_CUR
);
903 while (myread (desc
, &c
, 1) > 0)
907 if (nlines
== lines_allocated
)
909 lines_allocated
*= 2;
911 (int *) xmrealloc (s
->objfile
->md
, (char *) line_charpos
,
912 sizeof (int) * lines_allocated
);
914 line_charpos
[nlines
++] = lseek (desc
, 0, SEEK_CUR
);
918 #else /* lseek linear. */
920 struct cleanup
*old_cleanups
;
922 /* st_size might be a large type, but we only support source files whose
923 size fits in an int. */
924 size
= (int) st
.st_size
;
926 /* Use malloc, not alloca, because this may be pretty large, and we may
927 run into various kinds of limits on stack size. */
928 data
= (char *) xmalloc (size
);
929 old_cleanups
= make_cleanup (xfree
, data
);
931 /* Reassign `size' to result of read for systems where \r\n -> \n. */
932 size
= myread (desc
, data
, size
);
934 perror_with_name (s
->filename
);
942 /* A newline at the end does not start a new line. */
945 if (nlines
== lines_allocated
)
947 lines_allocated
*= 2;
949 (int *) xmrealloc (s
->objfile
->md
, (char *) line_charpos
,
950 sizeof (int) * lines_allocated
);
952 line_charpos
[nlines
++] = p
- data
;
955 do_cleanups (old_cleanups
);
957 #endif /* lseek linear. */
960 (int *) xmrealloc (s
->objfile
->md
, (char *) line_charpos
,
961 nlines
* sizeof (int));
965 /* Return the character position of a line LINE in symtab S.
966 Return 0 if anything is invalid. */
968 #if 0 /* Currently unused */
971 source_line_charpos (struct symtab
*s
, int line
)
975 if (!s
->line_charpos
|| line
<= 0)
977 if (line
> s
->nlines
)
979 return s
->line_charpos
[line
- 1];
982 /* Return the line number of character position POS in symtab S. */
985 source_charpos_line (register struct symtab
*s
, register int chr
)
987 register int line
= 0;
990 if (s
== 0 || s
->line_charpos
== 0)
992 lnp
= s
->line_charpos
;
993 /* Files are usually short, so sequential search is Ok */
994 while (line
< s
->nlines
&& *lnp
<= chr
)
999 if (line
>= s
->nlines
)
1007 /* Get full pathname and line number positions for a symtab.
1008 Return nonzero if line numbers may have changed.
1009 Set *FULLNAME to actual name of the file as found by `openp',
1010 or to 0 if the file is not found. */
1013 get_filename_and_charpos (struct symtab
*s
, char **fullname
)
1015 register int desc
, linenums_changed
= 0;
1017 desc
= open_source_file (s
);
1025 *fullname
= s
->fullname
;
1026 if (s
->line_charpos
== 0)
1027 linenums_changed
= 1;
1028 if (linenums_changed
)
1029 find_source_lines (s
, desc
);
1031 return linenums_changed
;
1034 /* Print text describing the full name of the source file S
1035 and the line number LINE and its corresponding character position.
1036 The text starts with two Ctrl-z so that the Emacs-GDB interface
1039 MID_STATEMENT is nonzero if the PC is not at the beginning of that line.
1041 Return 1 if successful, 0 if could not find the file. */
1044 identify_source_line (struct symtab
*s
, int line
, int mid_statement
,
1047 if (s
->line_charpos
== 0)
1048 get_filename_and_charpos (s
, (char **) NULL
);
1049 if (s
->fullname
== 0)
1051 if (line
> s
->nlines
)
1052 /* Don't index off the end of the line_charpos array. */
1054 annotate_source (s
->fullname
, line
, s
->line_charpos
[line
- 1],
1057 current_source_line
= line
;
1058 first_line_listed
= line
;
1059 last_line_listed
= line
;
1060 current_source_symtab
= s
;
1065 /* Print source lines from the file of symtab S,
1066 starting with line number LINE and stopping before line number STOPLINE. */
1068 static void print_source_lines_base (struct symtab
*s
, int line
, int stopline
,
1071 print_source_lines_base (struct symtab
*s
, int line
, int stopline
, int noerror
)
1075 register FILE *stream
;
1076 int nlines
= stopline
- line
;
1078 /* Regardless of whether we can open the file, set current_source_symtab. */
1079 current_source_symtab
= s
;
1080 current_source_line
= line
;
1081 first_line_listed
= line
;
1083 /* If printing of source lines is disabled, just print file and line number */
1084 if (ui_out_test_flags (uiout
, ui_source_list
))
1086 /* Only prints "No such file or directory" once */
1087 if ((s
!= last_source_visited
) || (!last_source_error
))
1089 last_source_visited
= s
;
1090 desc
= open_source_file (s
);
1094 desc
= last_source_error
;
1106 last_source_error
= desc
;
1110 char *name
= alloca (strlen (s
->filename
) + 100);
1111 sprintf (name
, "%d\t%s", line
, s
->filename
);
1112 print_sys_errmsg (name
, errno
);
1115 ui_out_field_int (uiout
, "line", line
);
1116 ui_out_text (uiout
, "\tin ");
1117 ui_out_field_string (uiout
, "file", s
->filename
);
1118 ui_out_text (uiout
, "\n");
1123 last_source_error
= 0;
1125 if (s
->line_charpos
== 0)
1126 find_source_lines (s
, desc
);
1128 if (line
< 1 || line
> s
->nlines
)
1131 error ("Line number %d out of range; %s has %d lines.",
1132 line
, s
->filename
, s
->nlines
);
1135 if (lseek (desc
, s
->line_charpos
[line
- 1], 0) < 0)
1138 perror_with_name (s
->filename
);
1141 stream
= fdopen (desc
, FDOPEN_MODE
);
1144 while (nlines
-- > 0)
1151 last_line_listed
= current_source_line
;
1152 sprintf (buf
, "%d\t", current_source_line
++);
1153 ui_out_text (uiout
, buf
);
1156 if (c
< 040 && c
!= '\t' && c
!= '\n' && c
!= '\r')
1158 sprintf (buf
, "^%c", c
+ 0100);
1159 ui_out_text (uiout
, buf
);
1162 ui_out_text (uiout
, "^?");
1163 #ifdef CRLF_SOURCE_FILES
1166 /* Skip a \r character, but only before a \n. */
1167 int c1
= fgetc (stream
);
1170 printf_filtered ("^%c", c
+ 0100);
1172 ungetc (c1
, stream
);
1177 sprintf (buf
, "%c", c
);
1178 ui_out_text (uiout
, buf
);
1181 while (c
!= '\n' && (c
= fgetc (stream
)) >= 0);
1187 /* Show source lines from the file of symtab S, starting with line
1188 number LINE and stopping before line number STOPLINE. If this is the
1189 not the command line version, then the source is shown in the source
1190 window otherwise it is simply printed */
1193 print_source_lines (struct symtab
*s
, int line
, int stopline
, int noerror
)
1195 print_source_lines_base (s
, line
, stopline
, noerror
);
1198 /* Print a list of files and line numbers which a user may choose from
1199 in order to list a function which was specified ambiguously (as with
1200 `list classname::overloadedfuncname', for example). The vector in
1201 SALS provides the filenames and line numbers. */
1204 ambiguous_line_spec (struct symtabs_and_lines
*sals
)
1208 for (i
= 0; i
< sals
->nelts
; ++i
)
1209 printf_filtered ("file: \"%s\", line number: %d\n",
1210 sals
->sals
[i
].symtab
->filename
, sals
->sals
[i
].line
);
1213 /* Print info on range of pc's in a specified line. */
1216 line_info (char *arg
, int from_tty
)
1218 struct symtabs_and_lines sals
;
1219 struct symtab_and_line sal
;
1220 CORE_ADDR start_pc
, end_pc
;
1223 INIT_SAL (&sal
); /* initialize to zeroes */
1227 sal
.symtab
= current_source_symtab
;
1228 sal
.line
= last_line_listed
;
1230 sals
.sals
= (struct symtab_and_line
*)
1231 xmalloc (sizeof (struct symtab_and_line
));
1236 sals
= decode_line_spec_1 (arg
, 0);
1241 /* C++ More than one line may have been specified, as when the user
1242 specifies an overloaded function name. Print info on them all. */
1243 for (i
= 0; i
< sals
.nelts
; i
++)
1247 if (sal
.symtab
== 0)
1249 printf_filtered ("No line number information available");
1252 /* This is useful for "info line *0x7f34". If we can't tell the
1253 user about a source line, at least let them have the symbolic
1255 printf_filtered (" for address ");
1257 print_address (sal
.pc
, gdb_stdout
);
1260 printf_filtered (".");
1261 printf_filtered ("\n");
1263 else if (sal
.line
> 0
1264 && find_line_pc_range (sal
, &start_pc
, &end_pc
))
1266 if (start_pc
== end_pc
)
1268 printf_filtered ("Line %d of \"%s\"",
1269 sal
.line
, sal
.symtab
->filename
);
1271 printf_filtered (" is at address ");
1272 print_address (start_pc
, gdb_stdout
);
1274 printf_filtered (" but contains no code.\n");
1278 printf_filtered ("Line %d of \"%s\"",
1279 sal
.line
, sal
.symtab
->filename
);
1281 printf_filtered (" starts at address ");
1282 print_address (start_pc
, gdb_stdout
);
1284 printf_filtered (" and ends at ");
1285 print_address (end_pc
, gdb_stdout
);
1286 printf_filtered (".\n");
1289 /* x/i should display this line's code. */
1290 set_next_address (start_pc
);
1292 /* Repeating "info line" should do the following line. */
1293 last_line_listed
= sal
.line
+ 1;
1295 /* If this is the only line, show the source code. If it could
1296 not find the file, don't do anything special. */
1297 if (annotation_level
&& sals
.nelts
== 1)
1298 identify_source_line (sal
.symtab
, sal
.line
, 0, start_pc
);
1301 /* Is there any case in which we get here, and have an address
1302 which the user would want to see? If we have debugging symbols
1303 and no line numbers? */
1304 printf_filtered ("Line number %d is out of range for \"%s\".\n",
1305 sal
.line
, sal
.symtab
->filename
);
1310 /* Commands to search the source file for a regexp. */
1314 forward_search_command (char *regex
, int from_tty
)
1318 register FILE *stream
;
1322 line
= last_line_listed
+ 1;
1324 msg
= (char *) re_comp (regex
);
1328 if (current_source_symtab
== 0)
1329 select_source_symtab (0);
1331 desc
= open_source_file (current_source_symtab
);
1333 perror_with_name (current_source_symtab
->filename
);
1335 if (current_source_symtab
->line_charpos
== 0)
1336 find_source_lines (current_source_symtab
, desc
);
1338 if (line
< 1 || line
> current_source_symtab
->nlines
)
1341 error ("Expression not found");
1344 if (lseek (desc
, current_source_symtab
->line_charpos
[line
- 1], 0) < 0)
1347 perror_with_name (current_source_symtab
->filename
);
1350 stream
= fdopen (desc
, FDOPEN_MODE
);
1354 static char *buf
= NULL
;
1356 int cursize
, newsize
;
1359 buf
= xmalloc (cursize
);
1368 if (p
- buf
== cursize
)
1370 newsize
= cursize
+ cursize
/ 2;
1371 buf
= xrealloc (buf
, newsize
);
1376 while (c
!= '\n' && (c
= getc (stream
)) >= 0);
1378 #ifdef CRLF_SOURCE_FILES
1379 /* Remove the \r, if any, at the end of the line, otherwise
1380 regular expressions that end with $ or \n won't work. */
1381 if (p
- buf
> 1 && p
[-2] == '\r')
1388 /* we now have a source line in buf, null terminate and match */
1390 if (re_exec (buf
) > 0)
1394 print_source_lines (current_source_symtab
, line
, line
+ 1, 0);
1395 set_internalvar (lookup_internalvar ("_"),
1396 value_from_longest (builtin_type_int
,
1398 current_source_line
= max (line
- lines_to_list
/ 2, 1);
1404 printf_filtered ("Expression not found\n");
1410 reverse_search_command (char *regex
, int from_tty
)
1414 register FILE *stream
;
1418 line
= last_line_listed
- 1;
1420 msg
= (char *) re_comp (regex
);
1424 if (current_source_symtab
== 0)
1425 select_source_symtab (0);
1427 desc
= open_source_file (current_source_symtab
);
1429 perror_with_name (current_source_symtab
->filename
);
1431 if (current_source_symtab
->line_charpos
== 0)
1432 find_source_lines (current_source_symtab
, desc
);
1434 if (line
< 1 || line
> current_source_symtab
->nlines
)
1437 error ("Expression not found");
1440 if (lseek (desc
, current_source_symtab
->line_charpos
[line
- 1], 0) < 0)
1443 perror_with_name (current_source_symtab
->filename
);
1446 stream
= fdopen (desc
, FDOPEN_MODE
);
1450 /* FIXME!!! We walk right off the end of buf if we get a long line!!! */
1451 char buf
[4096]; /* Should be reasonable??? */
1452 register char *p
= buf
;
1461 while (c
!= '\n' && (c
= getc (stream
)) >= 0);
1463 #ifdef CRLF_SOURCE_FILES
1464 /* Remove the \r, if any, at the end of the line, otherwise
1465 regular expressions that end with $ or \n won't work. */
1466 if (p
- buf
> 1 && p
[-2] == '\r')
1473 /* We now have a source line in buf; null terminate and match. */
1475 if (re_exec (buf
) > 0)
1479 print_source_lines (current_source_symtab
, line
, line
+ 1, 0);
1480 set_internalvar (lookup_internalvar ("_"),
1481 value_from_longest (builtin_type_int
,
1483 current_source_line
= max (line
- lines_to_list
/ 2, 1);
1487 if (fseek (stream
, current_source_symtab
->line_charpos
[line
- 1], 0) < 0)
1490 perror_with_name (current_source_symtab
->filename
);
1494 printf_filtered ("Expression not found\n");
1500 _initialize_source (void)
1502 struct cmd_list_element
*c
;
1503 current_source_symtab
= 0;
1504 init_source_path ();
1506 /* The intention is to use POSIX Basic Regular Expressions.
1507 Always use the GNU regex routine for consistency across all hosts.
1508 Our current GNU regex.c does not have all the POSIX features, so this is
1509 just an approximation. */
1510 re_set_syntax (RE_SYNTAX_GREP
);
1512 c
= add_cmd ("directory", class_files
, directory_command
,
1513 "Add directory DIR to beginning of search path for source files.\n\
1514 Forget cached info on source file locations and line positions.\n\
1515 DIR can also be $cwd for the current working directory, or $cdir for the\n\
1516 directory in which the source file was compiled into object code.\n\
1517 With no argument, reset the search path to $cdir:$cwd, the default.",
1521 add_com_alias ("use", "directory", class_files
, 0);
1523 set_cmd_completer (c
, filename_completer
);
1525 add_cmd ("directories", no_class
, show_directories
,
1526 "Current search path for finding source files.\n\
1527 $cwd in the path means the current working directory.\n\
1528 $cdir in the path means the compilation directory of the source file.",
1533 add_com_alias ("D", "directory", class_files
, 0);
1534 add_cmd ("ld", no_class
, show_directories
,
1535 "Current search path for finding source files.\n\
1536 $cwd in the path means the current working directory.\n\
1537 $cdir in the path means the compilation directory of the source file.",
1541 add_info ("source", source_info
,
1542 "Information about the current source file.");
1544 add_info ("line", line_info
,
1545 concat ("Core addresses of the code for a source line.\n\
1546 Line can be specified as\n\
1547 LINENUM, to list around that line in current file,\n\
1548 FILE:LINENUM, to list around that line in that file,\n\
1549 FUNCTION, to list around beginning of that function,\n\
1550 FILE:FUNCTION, to distinguish among like-named static functions.\n\
1552 Default is to describe the last source line that was listed.\n\n\
1553 This sets the default address for \"x\" to the line's first instruction\n\
1554 so that \"x/i\" suffices to start examining the machine code.\n\
1555 The address is also stored as the value of \"$_\".", NULL
));
1557 add_com ("forward-search", class_files
, forward_search_command
,
1558 "Search for regular expression (see regex(3)) from last line listed.\n\
1559 The matching line number is also stored as the value of \"$_\".");
1560 add_com_alias ("search", "forward-search", class_files
, 0);
1562 add_com ("reverse-search", class_files
, reverse_search_command
,
1563 "Search backward for regular expression (see regex(3)) from last line listed.\n\
1564 The matching line number is also stored as the value of \"$_\".");
1568 add_com_alias ("/", "forward-search", class_files
, 0);
1569 add_com_alias ("?", "reverse-search", class_files
, 0);
1573 (add_set_cmd ("listsize", class_support
, var_uinteger
,
1574 (char *) &lines_to_list
,
1575 "Set number of source lines gdb will list by default.",
This page took 0.062308 seconds and 4 git commands to generate.