1 /* List lines of source files for GDB, the GNU debugger.
2 Copyright 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995,
3 1996, 1997, 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
24 #include "expression.h"
32 #include <sys/types.h>
33 #include "gdb_string.h"
37 #include "gdb_regex.h"
43 #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 list_command (char *, int);
84 static void ambiguous_line_spec (struct symtabs_and_lines
*);
86 static void source_info (char *, int);
88 static void show_directories (char *, int);
90 /* Path of directories to search for source files.
91 Same format as the PATH environment variable's value. */
95 /* Symtab of default file for listing lines of. */
97 struct symtab
*current_source_symtab
;
99 /* Default next line to list. */
101 int current_source_line
;
103 /* Default number of lines to print with commands like "list".
104 This is based on guessing how many long (i.e. more than chars_per_line
105 characters) lines there will be. To be completely correct, "list"
106 and friends should be rewritten to count characters and see where
107 things are wrapping, but that would be a fair amount of work. */
109 int lines_to_list
= 10;
111 /* Line number of last line printed. Default for various commands.
112 current_source_line is usually, but not always, the same as this. */
114 static int last_line_listed
;
116 /* First line number listed by last listing command. */
118 static int first_line_listed
;
120 /* Saves the name of the last source file visited and a possible error code.
121 Used to prevent repeating annoying "No such file or directories" msgs */
123 static struct symtab
*last_source_visited
= NULL
;
124 static int last_source_error
= 0;
127 /* Set the source file default for the "list" command to be S.
129 If S is NULL, and we don't have a default, find one. This
130 should only be called when the user actually tries to use the
131 default, since we produce an error if we can't find a reasonable
132 default. Also, since this can cause symbols to be read, doing it
133 before we need to would make things slower than necessary. */
136 select_source_symtab (register struct symtab
*s
)
138 struct symtabs_and_lines sals
;
139 struct symtab_and_line sal
;
140 struct partial_symtab
*ps
;
141 struct partial_symtab
*cs_pst
= 0;
146 current_source_symtab
= s
;
147 current_source_line
= 1;
151 if (current_source_symtab
)
154 /* Make the default place to list be the function `main'
156 if (lookup_symbol ("main", 0, VAR_NAMESPACE
, 0, NULL
))
158 sals
= decode_line_spec ("main", 1);
161 current_source_symtab
= sal
.symtab
;
162 current_source_line
= max (sal
.line
- (lines_to_list
- 1), 1);
163 if (current_source_symtab
)
167 /* All right; find the last file in the symtab list (ignoring .h's). */
169 current_source_line
= 1;
171 for (ofp
= object_files
; ofp
!= NULL
; ofp
= ofp
->next
)
173 for (s
= ofp
->symtabs
; s
; s
= s
->next
)
175 char *name
= s
->filename
;
176 int len
= strlen (name
);
177 if (!(len
> 2 && (STREQ (&name
[len
- 2], ".h"))))
179 current_source_symtab
= s
;
183 if (current_source_symtab
)
186 /* Howabout the partial symbol tables? */
188 for (ofp
= object_files
; ofp
!= NULL
; ofp
= ofp
->next
)
190 for (ps
= ofp
->psymtabs
; ps
!= NULL
; ps
= ps
->next
)
192 char *name
= ps
->filename
;
193 int len
= strlen (name
);
194 if (!(len
> 2 && (STREQ (&name
[len
- 2], ".h"))))
204 internal_error (__FILE__
, __LINE__
,
205 "select_source_symtab: "
206 "readin pst found and no symtabs.");
210 current_source_symtab
= PSYMTAB_TO_SYMTAB (cs_pst
);
213 if (current_source_symtab
)
216 error ("Can't find a default source file");
220 show_directories (char *ignore
, int from_tty
)
222 puts_filtered ("Source directories searched: ");
223 puts_filtered (source_path
);
224 puts_filtered ("\n");
227 /* Forget what we learned about line positions in source files, and
228 which directories contain them; must check again now since files
229 may be found in a different directory now. */
232 forget_cached_source_info (void)
234 register struct symtab
*s
;
235 register struct objfile
*objfile
;
237 for (objfile
= object_files
; objfile
!= NULL
; objfile
= objfile
->next
)
239 for (s
= objfile
->symtabs
; s
!= NULL
; s
= s
->next
)
241 if (s
->line_charpos
!= NULL
)
243 mfree (objfile
->md
, s
->line_charpos
);
244 s
->line_charpos
= NULL
;
246 if (s
->fullname
!= NULL
)
248 mfree (objfile
->md
, s
->fullname
);
256 init_source_path (void)
260 sprintf (buf
, "$cdir%c$cwd", DIRNAME_SEPARATOR
);
261 source_path
= xstrdup (buf
);
262 forget_cached_source_info ();
265 /* Add zero or more directories to the front of the source path. */
268 directory_command (char *dirname
, int from_tty
)
271 /* FIXME, this goes to "delete dir"... */
274 if (from_tty
&& query ("Reinitialize source path to empty? "))
282 mod_path (dirname
, &source_path
);
283 last_source_visited
= NULL
;
286 show_directories ((char *) 0, from_tty
);
287 forget_cached_source_info ();
290 /* Add zero or more directories to the front of an arbitrary path. */
293 mod_path (char *dirname
, char **which_path
)
295 char *old
= *which_path
;
301 dirname
= xstrdup (dirname
);
302 make_cleanup (xfree
, dirname
);
306 char *name
= dirname
;
311 char *separator
= strchr (name
, DIRNAME_SEPARATOR
);
312 char *space
= strchr (name
, ' ');
313 char *tab
= strchr (name
, '\t');
315 if (separator
== 0 && space
== 0 && tab
== 0)
316 p
= dirname
= name
+ strlen (name
);
320 if (separator
!= 0 && (p
== 0 || separator
< p
))
322 if (space
!= 0 && (p
== 0 || space
< p
))
324 if (tab
!= 0 && (p
== 0 || tab
< p
))
327 while (*dirname
== DIRNAME_SEPARATOR
334 if (!(SLASH_P (*name
) && p
<= name
+ 1) /* "/" */
335 #if defined(_WIN32) || defined(__MSDOS__)
336 /* On MS-DOS and MS-Windows, h:\ is different from h: */
337 && !(!SLASH_P (*name
) && ROOTED_P (name
) && p
<= name
+ 3) /* d:/ */
340 /* Sigh. "foo/" => "foo" */
344 while (p
> name
&& p
[-1] == '.')
348 /* "." => getwd (). */
349 name
= current_directory
;
352 else if (p
> name
+ 1 && SLASH_P (p
[-2]))
362 /* "...foo/." => "...foo". */
373 name
= tilde_expand (name
);
374 #if defined(_WIN32) || defined(__MSDOS__)
375 else if (ROOTED_P (name
) && p
== name
+ 2) /* "d:" => "d:." */
376 name
= concat (name
, ".", NULL
);
378 else if (!ROOTED_P (name
) && name
[0] != '$')
379 name
= concat (current_directory
, SLASH_STRING
, name
, NULL
);
381 name
= savestring (name
, p
- name
);
382 make_cleanup (xfree
, name
);
384 /* Unless it's a variable, check existence. */
387 /* These are warnings, not errors, since we don't want a
388 non-existent directory in a .gdbinit file to stop processing
389 of the .gdbinit file.
391 Whether they get added to the path is more debatable. Current
392 answer is yes, in case the user wants to go make the directory
393 or whatever. If the directory continues to not exist/not be
394 a directory/etc, then having them in the path should be
396 if (stat (name
, &st
) < 0)
398 int save_errno
= errno
;
399 fprintf_unfiltered (gdb_stderr
, "Warning: ");
400 print_sys_errmsg (name
, save_errno
);
402 else if ((st
.st_mode
& S_IFMT
) != S_IFDIR
)
403 warning ("%s is not a directory.", name
);
408 register unsigned int len
= strlen (name
);
413 /* FIXME: strncmp loses in interesting ways on MS-DOS and
414 MS-Windows because of case-insensitivity and two different
415 but functionally identical slash characters. We need a
416 special filesystem-dependent file-name comparison function.
418 Actually, even on Unix I would use realpath() or its work-
419 alike before comparing. Then all the code above which
420 removes excess slashes and dots could simply go away. */
421 if (!strncmp (p
, name
, len
)
422 && (p
[len
] == '\0' || p
[len
] == DIRNAME_SEPARATOR
))
424 /* Found it in the search path, remove old copy */
426 p
--; /* Back over leading separator */
427 if (prefix
> p
- *which_path
)
428 goto skip_dup
; /* Same dir twice in one cmd */
429 strcpy (p
, &p
[len
+ 1]); /* Copy from next \0 or : */
431 p
= strchr (p
, DIRNAME_SEPARATOR
);
441 tinybuf
[0] = DIRNAME_SEPARATOR
;
444 /* 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. */
451 temp
= concat (old
, tinybuf
, name
, NULL
);
453 *which_path
= concat (temp
, "", &old
[prefix
], NULL
);
454 prefix
= strlen (temp
);
459 *which_path
= concat (name
, (old
[0] ? tinybuf
: old
), old
, NULL
);
460 prefix
= strlen (name
);
468 while (*dirname
!= '\0');
473 source_info (char *ignore
, int from_tty
)
475 register struct symtab
*s
= current_source_symtab
;
479 printf_filtered ("No current source file.\n");
482 printf_filtered ("Current source file is %s\n", s
->filename
);
484 printf_filtered ("Compilation directory is %s\n", s
->dirname
);
486 printf_filtered ("Located in %s\n", s
->fullname
);
488 printf_filtered ("Contains %d line%s.\n", s
->nlines
,
489 s
->nlines
== 1 ? "" : "s");
491 printf_filtered ("Source language is %s.\n", language_str (s
->language
));
492 printf_filtered ("Compiled with %s debugging format.\n", s
->debugformat
);
497 /* Open a file named STRING, searching path PATH (dir names sep by some char)
498 using mode MODE and protection bits PROT in the calls to open.
500 If TRY_CWD_FIRST, try to open ./STRING before searching PATH.
501 (ie pretend the first element of PATH is "."). This also indicates
502 that a slash in STRING disables searching of the path (this is
503 so that "exec-file ./foo" or "symbol-file ./foo" insures that you
504 get that particular version of foo or an error message).
506 If FILENAME_OPENED is non-null, set it to a newly allocated string naming
507 the actual file opened (this string will always start with a "/". We
508 have to take special pains to avoid doubling the "/" between the directory
509 and the file, sigh! Emacs gets confuzzed by this when we print the
512 If a file is found, return the descriptor.
513 Otherwise, return -1, with errno set for the last name we tried to open. */
515 /* >>>> This should only allow files of certain types,
516 >>>> eg executable, non-directory */
518 openp (char *path
, int try_cwd_first
, char *string
, int mode
, int prot
,
519 char **filename_opened
)
522 register char *filename
;
523 register char *p
, *p1
;
534 if (try_cwd_first
|| ROOTED_P (string
))
538 fd
= open (filename
, mode
, prot
);
541 for (i
= 0; string
[i
]; i
++)
542 if (SLASH_P (string
[i
]))
547 while (string
[0] == '.' && SLASH_P (string
[1]))
550 alloclen
= strlen (path
) + strlen (string
) + 2;
551 filename
= (char *) alloca (alloclen
);
553 for (p
= path
; p
; p
= p1
? p1
+ 1 : 0)
555 p1
= (char *) strchr (p
, DIRNAME_SEPARATOR
);
561 if (len
== 4 && p
[0] == '$' && p
[1] == 'c'
562 && p
[2] == 'w' && p
[3] == 'd')
564 /* Name is $cwd -- insert current directory name instead. */
567 /* First, realloc the filename buffer if too short. */
568 len
= strlen (current_directory
);
569 newlen
= len
+ strlen (string
) + 2;
570 if (newlen
> alloclen
)
573 filename
= (char *) alloca (alloclen
);
575 strcpy (filename
, current_directory
);
579 /* Normal file name in path -- just use it. */
580 strncpy (filename
, p
, len
);
584 /* Remove trailing slashes */
585 while (len
> 0 && SLASH_P (filename
[len
- 1]))
588 strcat (filename
+ len
, SLASH_STRING
);
589 strcat (filename
, string
);
591 fd
= open (filename
, mode
);
600 *filename_opened
= (char *) 0;
601 else if (ROOTED_P (filename
))
602 *filename_opened
= savestring (filename
, strlen (filename
));
605 /* Beware the // my son, the Emacs barfs, the botch that catch... */
607 *filename_opened
= concat (current_directory
,
608 SLASH_P (current_directory
[strlen (current_directory
) - 1])
614 /* This is a debugging hack that can go away when all combinations
615 of Mac and Unix names are handled reasonably. */
617 extern int debug_openp
;
621 printf ("openp on %s, path %s mode %d prot %d\n returned %d",
622 string
, path
, mode
, prot
, fd
);
623 if (*filename_opened
)
624 printf (" (filename is %s)", *filename_opened
);
634 /* This is essentially a convenience, for clients that want the behaviour
635 of openp, using source_path, but that really don't want the file to be
636 opened but want instead just to know what the full pathname is (as
637 qualified against source_path).
639 The current working directory is searched first.
641 If the file was found, this function returns 1, and FULL_PATHNAME is
642 set to the fully-qualified pathname.
644 Else, this functions returns 0, and FULL_PATHNAME is set to NULL.
647 source_full_path_of (char *filename
, char **full_pathname
)
651 fd
= openp (source_path
, 1, filename
, O_RDONLY
, 0, full_pathname
);
654 *full_pathname
= NULL
;
663 /* Open a source file given a symtab S. Returns a file descriptor or
664 negative number for error. */
667 open_source_file (struct symtab
*s
)
669 char *path
= source_path
;
674 /* Quick way out if we already know its full name */
677 result
= open (s
->fullname
, OPEN_MODE
);
680 /* Didn't work -- free old one, try again. */
681 mfree (s
->objfile
->md
, s
->fullname
);
685 if (s
->dirname
!= NULL
)
687 /* Replace a path entry of $cdir with the compilation directory name */
689 /* We cast strstr's result in case an ANSIhole has made it const,
690 which produces a "required warning" when assigned to a nonconst. */
691 p
= (char *) strstr (source_path
, "$cdir");
692 if (p
&& (p
== path
|| p
[-1] == DIRNAME_SEPARATOR
)
693 && (p
[cdir_len
] == DIRNAME_SEPARATOR
|| p
[cdir_len
] == '\0'))
698 alloca (strlen (source_path
) + 1 + strlen (s
->dirname
) + 1);
699 len
= p
- source_path
;
700 strncpy (path
, source_path
, len
); /* Before $cdir */
701 strcpy (path
+ len
, s
->dirname
); /* new stuff */
702 strcat (path
+ len
, source_path
+ len
+ cdir_len
); /* After $cdir */
706 result
= openp (path
, 0, s
->filename
, OPEN_MODE
, 0, &s
->fullname
);
709 /* Didn't work. Try using just the basename. */
710 p
= basename (s
->filename
);
711 if (p
!= s
->filename
)
712 result
= openp (path
, 0, p
, OPEN_MODE
, 0, &s
->fullname
);
717 /* Didn't work. Try using just the MPW basename. */
718 p
= (char *) mpw_basename (s
->filename
);
719 if (p
!= s
->filename
)
720 result
= openp (path
, 0, p
, OPEN_MODE
, 0, &s
->fullname
);
724 /* Didn't work. Try using the mixed Unix/MPW basename. */
725 p
= (char *) mpw_mixed_basename (s
->filename
);
726 if (p
!= s
->filename
)
727 result
= openp (path
, 0, p
, OPEN_MODE
, 0, &s
->fullname
);
733 fullname
= s
->fullname
;
734 s
->fullname
= mstrsave (s
->objfile
->md
, s
->fullname
);
740 /* Return the path to the source file associated with symtab. Returns NULL
744 symtab_to_filename (struct symtab
*s
)
751 /* If we've seen the file before, just return fullname. */
756 /* Try opening the file to setup fullname */
758 fd
= open_source_file (s
);
760 return s
->filename
; /* File not found. Just use short name */
762 /* Found the file. Cleanup and return the full name */
769 /* Create and initialize the table S->line_charpos that records
770 the positions of the lines in the source file, which is assumed
771 to be open on descriptor DESC.
772 All set S->nlines to the number of such lines. */
775 find_source_lines (struct symtab
*s
, int desc
)
778 register char *data
, *p
, *end
;
780 int lines_allocated
= 1000;
785 line_charpos
= (int *) xmmalloc (s
->objfile
->md
,
786 lines_allocated
* sizeof (int));
787 if (fstat (desc
, &st
) < 0)
788 perror_with_name (s
->filename
);
790 if (s
&& s
->objfile
&& s
->objfile
->obfd
)
791 mtime
= bfd_get_mtime (s
->objfile
->obfd
);
793 mtime
= bfd_get_mtime (exec_bfd
);
795 if (mtime
&& mtime
< st
.st_mtime
)
798 printf_filtered ("\n");
799 warning ("Source file is more recent than executable.\n");
802 #ifdef LSEEK_NOT_LINEAR
806 /* Have to read it byte by byte to find out where the chars live */
808 line_charpos
[0] = lseek (desc
, 0, SEEK_CUR
);
810 while (myread (desc
, &c
, 1) > 0)
814 if (nlines
== lines_allocated
)
816 lines_allocated
*= 2;
818 (int *) xmrealloc (s
->objfile
->md
, (char *) line_charpos
,
819 sizeof (int) * lines_allocated
);
821 line_charpos
[nlines
++] = lseek (desc
, 0, SEEK_CUR
);
825 #else /* lseek linear. */
827 struct cleanup
*old_cleanups
;
829 /* st_size might be a large type, but we only support source files whose
830 size fits in an int. */
831 size
= (int) st
.st_size
;
833 /* Use malloc, not alloca, because this may be pretty large, and we may
834 run into various kinds of limits on stack size. */
835 data
= (char *) xmalloc (size
);
836 old_cleanups
= make_cleanup (xfree
, data
);
838 /* Reassign `size' to result of read for systems where \r\n -> \n. */
839 size
= myread (desc
, data
, size
);
841 perror_with_name (s
->filename
);
849 /* A newline at the end does not start a new line. */
852 if (nlines
== lines_allocated
)
854 lines_allocated
*= 2;
856 (int *) xmrealloc (s
->objfile
->md
, (char *) line_charpos
,
857 sizeof (int) * lines_allocated
);
859 line_charpos
[nlines
++] = p
- data
;
862 do_cleanups (old_cleanups
);
864 #endif /* lseek linear. */
867 (int *) xmrealloc (s
->objfile
->md
, (char *) line_charpos
,
868 nlines
* sizeof (int));
872 /* Return the character position of a line LINE in symtab S.
873 Return 0 if anything is invalid. */
875 #if 0 /* Currently unused */
878 source_line_charpos (struct symtab
*s
, int line
)
882 if (!s
->line_charpos
|| line
<= 0)
884 if (line
> s
->nlines
)
886 return s
->line_charpos
[line
- 1];
889 /* Return the line number of character position POS in symtab S. */
892 source_charpos_line (register struct symtab
*s
, register int chr
)
894 register int line
= 0;
897 if (s
== 0 || s
->line_charpos
== 0)
899 lnp
= s
->line_charpos
;
900 /* Files are usually short, so sequential search is Ok */
901 while (line
< s
->nlines
&& *lnp
<= chr
)
906 if (line
>= s
->nlines
)
914 /* Get full pathname and line number positions for a symtab.
915 Return nonzero if line numbers may have changed.
916 Set *FULLNAME to actual name of the file as found by `openp',
917 or to 0 if the file is not found. */
920 get_filename_and_charpos (struct symtab
*s
, char **fullname
)
922 register int desc
, linenums_changed
= 0;
924 desc
= open_source_file (s
);
932 *fullname
= s
->fullname
;
933 if (s
->line_charpos
== 0)
934 linenums_changed
= 1;
935 if (linenums_changed
)
936 find_source_lines (s
, desc
);
938 return linenums_changed
;
941 /* Print text describing the full name of the source file S
942 and the line number LINE and its corresponding character position.
943 The text starts with two Ctrl-z so that the Emacs-GDB interface
946 MID_STATEMENT is nonzero if the PC is not at the beginning of that line.
948 Return 1 if successful, 0 if could not find the file. */
951 identify_source_line (struct symtab
*s
, int line
, int mid_statement
,
954 if (s
->line_charpos
== 0)
955 get_filename_and_charpos (s
, (char **) NULL
);
956 if (s
->fullname
== 0)
958 if (line
> s
->nlines
)
959 /* Don't index off the end of the line_charpos array. */
961 annotate_source (s
->fullname
, line
, s
->line_charpos
[line
- 1],
964 current_source_line
= line
;
965 first_line_listed
= line
;
966 last_line_listed
= line
;
967 current_source_symtab
= s
;
972 /* Print source lines from the file of symtab S,
973 starting with line number LINE and stopping before line number STOPLINE. */
975 static void print_source_lines_base (struct symtab
*s
, int line
, int stopline
,
978 print_source_lines_base (struct symtab
*s
, int line
, int stopline
, int noerror
)
982 register FILE *stream
;
983 int nlines
= stopline
- line
;
985 /* Regardless of whether we can open the file, set current_source_symtab. */
986 current_source_symtab
= s
;
987 current_source_line
= line
;
988 first_line_listed
= line
;
991 /* If printing of source lines is disabled, just print file and line number */
992 if (ui_out_test_flags (uiout
, ui_source_list
))
995 /* Only prints "No such file or directory" once */
996 if ((s
!= last_source_visited
) || (!last_source_error
))
998 last_source_visited
= s
;
999 desc
= open_source_file (s
);
1003 desc
= last_source_error
;
1017 last_source_error
= desc
;
1021 char *name
= alloca (strlen (s
->filename
) + 100);
1022 sprintf (name
, "%d\t%s", line
, s
->filename
);
1023 print_sys_errmsg (name
, errno
);
1027 ui_out_field_int (uiout
, "line", line
);
1028 ui_out_text (uiout
, "\tin ");
1029 ui_out_field_string (uiout
, "file", s
->filename
);
1030 ui_out_text (uiout
, "\n");
1032 printf_filtered ("%d\tin %s\n", line
, s
->filename
);
1038 last_source_error
= 0;
1040 if (s
->line_charpos
== 0)
1041 find_source_lines (s
, desc
);
1043 if (line
< 1 || line
> s
->nlines
)
1046 error ("Line number %d out of range; %s has %d lines.",
1047 line
, s
->filename
, s
->nlines
);
1050 if (lseek (desc
, s
->line_charpos
[line
- 1], 0) < 0)
1053 perror_with_name (s
->filename
);
1056 stream
= fdopen (desc
, FDOPEN_MODE
);
1059 while (nlines
-- > 0)
1067 last_line_listed
= current_source_line
;
1068 sprintf (buf
, "%d\t", current_source_line
++);
1069 ui_out_text (uiout
, buf
);
1072 if (c
< 040 && c
!= '\t' && c
!= '\n' && c
!= '\r')
1074 sprintf (buf
, "^%c", c
+ 0100);
1075 ui_out_text (uiout
, buf
);
1078 ui_out_text (uiout
, "^?");
1079 #ifdef CRLF_SOURCE_FILES
1082 /* Skip a \r character, but only before a \n. */
1083 int c1
= fgetc (stream
);
1086 printf_filtered ("^%c", c
+ 0100);
1088 ungetc (c1
, stream
);
1093 sprintf (buf
, "%c", c
);
1094 ui_out_text (uiout
, buf
);
1097 while (c
!= '\n' && (c
= fgetc (stream
)) >= 0);
1102 last_line_listed
= current_source_line
;
1103 printf_filtered ("%d\t", current_source_line
++);
1106 if (c
< 040 && c
!= '\t' && c
!= '\n' && c
!= '\r')
1107 printf_filtered ("^%c", c
+ 0100);
1109 printf_filtered ("^?");
1110 #ifdef CRLF_SOURCE_FILES
1113 /* Just skip \r characters. */
1117 printf_filtered ("%c", c
);
1119 while (c
!= '\n' && (c
= fgetc (stream
)) >= 0);
1126 /* Show source lines from the file of symtab S, starting with line
1127 number LINE and stopping before line number STOPLINE. If this is the
1128 not the command line version, then the source is shown in the source
1129 window otherwise it is simply printed */
1132 print_source_lines (struct symtab
*s
, int line
, int stopline
, int noerror
)
1136 m_winPtrIsNull (srcWin
) || !srcWin
->generic
.isVisible
)
1137 print_source_lines_base (s
, line
, stopline
, noerror
);
1140 TuiGenWinInfoPtr locator
= locatorWinInfoPtr ();
1141 extern void tui_vAddWinToLayout (va_list);
1142 extern void tui_vUpdateSourceWindowsWithLine (va_list);
1144 /* Regardless of whether we can open the file,
1145 set current_source_symtab. */
1146 current_source_symtab
= s
;
1147 current_source_line
= line
;
1148 first_line_listed
= line
;
1150 /* make sure that the source window is displayed */
1151 tuiDo ((TuiOpaqueFuncPtr
) tui_vAddWinToLayout
, SRC_WIN
);
1153 tuiDo ((TuiOpaqueFuncPtr
) tui_vUpdateSourceWindowsWithLine
, s
, line
);
1154 tuiDo ((TuiOpaqueFuncPtr
) tui_vUpdateLocatorFilename
, s
->filename
);
1157 print_source_lines_base (s
, line
, stopline
, noerror
);
1163 /* Print a list of files and line numbers which a user may choose from
1164 in order to list a function which was specified ambiguously (as with
1165 `list classname::overloadedfuncname', for example). The vector in
1166 SALS provides the filenames and line numbers. */
1169 ambiguous_line_spec (struct symtabs_and_lines
*sals
)
1173 for (i
= 0; i
< sals
->nelts
; ++i
)
1174 printf_filtered ("file: \"%s\", line number: %d\n",
1175 sals
->sals
[i
].symtab
->filename
, sals
->sals
[i
].line
);
1179 list_command (char *arg
, int from_tty
)
1181 struct symtabs_and_lines sals
, sals_end
;
1182 struct symtab_and_line sal
, sal_end
;
1188 int linenum_beg
= 0;
1191 if (!have_full_symbols () && !have_partial_symbols ())
1192 error ("No symbol table is loaded. Use the \"file\" command.");
1194 /* Pull in a current source symtab if necessary */
1195 if (current_source_symtab
== 0 &&
1196 (arg
== 0 || arg
[0] == '+' || arg
[0] == '-'))
1197 select_source_symtab (0);
1199 /* "l" or "l +" lists next ten lines. */
1201 if (arg
== 0 || STREQ (arg
, "+"))
1203 if (current_source_symtab
== 0)
1204 error ("No default source file yet. Do \"help list\".");
1205 print_source_lines (current_source_symtab
, current_source_line
,
1206 current_source_line
+ lines_to_list
, 0);
1210 /* "l -" lists previous ten lines, the ones before the ten just listed. */
1211 if (STREQ (arg
, "-"))
1213 if (current_source_symtab
== 0)
1214 error ("No default source file yet. Do \"help list\".");
1215 print_source_lines (current_source_symtab
,
1216 max (first_line_listed
- lines_to_list
, 1),
1217 first_line_listed
, 0);
1221 /* Now if there is only one argument, decode it in SAL
1223 If there are two arguments, decode them in SAL and SAL_END
1224 and clear NO_END; however, if one of the arguments is blank,
1225 set DUMMY_BEG or DUMMY_END to record that fact. */
1232 sals
= decode_line_1 (&arg1
, 0, 0, 0, 0);
1238 ambiguous_line_spec (&sals
);
1247 /* Record whether the BEG arg is all digits. */
1249 for (p
= arg
; p
!= arg1
&& *p
>= '0' && *p
<= '9'; p
++);
1250 linenum_beg
= (p
== arg1
);
1252 while (*arg1
== ' ' || *arg1
== '\t')
1258 while (*arg1
== ' ' || *arg1
== '\t')
1265 sals_end
= decode_line_1 (&arg1
, 0, 0, 0, 0);
1267 sals_end
= decode_line_1 (&arg1
, 0, sal
.symtab
, sal
.line
, 0);
1268 if (sals_end
.nelts
== 0)
1270 if (sals_end
.nelts
> 1)
1272 ambiguous_line_spec (&sals_end
);
1273 xfree (sals_end
.sals
);
1276 sal_end
= sals_end
.sals
[0];
1277 xfree (sals_end
.sals
);
1282 error ("Junk at end of line specification.");
1284 if (!no_end
&& !dummy_beg
&& !dummy_end
1285 && sal
.symtab
!= sal_end
.symtab
)
1286 error ("Specified start and end are in different files.");
1287 if (dummy_beg
&& dummy_end
)
1288 error ("Two empty args do not say what lines to list.");
1290 /* if line was specified by address,
1291 first print exactly which line, and which file.
1292 In this case, sal.symtab == 0 means address is outside
1293 of all known source files, not that user failed to give a filename. */
1296 if (sal
.symtab
== 0)
1297 /* FIXME-32x64--assumes sal.pc fits in long. */
1298 error ("No source file for address %s.",
1299 local_hex_string ((unsigned long) sal
.pc
));
1300 sym
= find_pc_function (sal
.pc
);
1303 print_address_numeric (sal
.pc
, 1, gdb_stdout
);
1304 printf_filtered (" is in ");
1305 fputs_filtered (SYMBOL_SOURCE_NAME (sym
), gdb_stdout
);
1306 printf_filtered (" (%s:%d).\n", sal
.symtab
->filename
, sal
.line
);
1310 print_address_numeric (sal
.pc
, 1, gdb_stdout
);
1311 printf_filtered (" is at %s:%d.\n",
1312 sal
.symtab
->filename
, sal
.line
);
1316 /* If line was not specified by just a line number,
1317 and it does not imply a symtab, it must be an undebuggable symbol
1318 which means no source code. */
1320 if (!linenum_beg
&& sal
.symtab
== 0)
1321 error ("No line number known for %s.", arg
);
1323 /* If this command is repeated with RET,
1324 turn it into the no-arg variant. */
1329 if (dummy_beg
&& sal_end
.symtab
== 0)
1330 error ("No default source file yet. Do \"help list\".");
1332 print_source_lines (sal_end
.symtab
,
1333 max (sal_end
.line
- (lines_to_list
- 1), 1),
1334 sal_end
.line
+ 1, 0);
1335 else if (sal
.symtab
== 0)
1336 error ("No default source file yet. Do \"help list\".");
1339 int first_line
= sal
.line
- lines_to_list
/ 2;
1341 if (first_line
< 1) first_line
= 1;
1343 print_source_lines (sal
.symtab
, first_line
, first_line
+ lines_to_list
,
1347 print_source_lines (sal
.symtab
, sal
.line
,
1349 ? sal
.line
+ lines_to_list
1350 : sal_end
.line
+ 1),
1354 /* Print info on range of pc's in a specified line. */
1357 line_info (char *arg
, int from_tty
)
1359 struct symtabs_and_lines sals
;
1360 struct symtab_and_line sal
;
1361 CORE_ADDR start_pc
, end_pc
;
1364 INIT_SAL (&sal
); /* initialize to zeroes */
1368 sal
.symtab
= current_source_symtab
;
1369 sal
.line
= last_line_listed
;
1371 sals
.sals
= (struct symtab_and_line
*)
1372 xmalloc (sizeof (struct symtab_and_line
));
1377 sals
= decode_line_spec_1 (arg
, 0);
1382 /* C++ More than one line may have been specified, as when the user
1383 specifies an overloaded function name. Print info on them all. */
1384 for (i
= 0; i
< sals
.nelts
; i
++)
1388 if (sal
.symtab
== 0)
1390 printf_filtered ("No line number information available");
1393 /* This is useful for "info line *0x7f34". If we can't tell the
1394 user about a source line, at least let them have the symbolic
1396 printf_filtered (" for address ");
1398 print_address (sal
.pc
, gdb_stdout
);
1401 printf_filtered (".");
1402 printf_filtered ("\n");
1404 else if (sal
.line
> 0
1405 && find_line_pc_range (sal
, &start_pc
, &end_pc
))
1407 if (start_pc
== end_pc
)
1409 printf_filtered ("Line %d of \"%s\"",
1410 sal
.line
, sal
.symtab
->filename
);
1412 printf_filtered (" is at address ");
1413 print_address (start_pc
, gdb_stdout
);
1415 printf_filtered (" but contains no code.\n");
1419 printf_filtered ("Line %d of \"%s\"",
1420 sal
.line
, sal
.symtab
->filename
);
1422 printf_filtered (" starts at address ");
1423 print_address (start_pc
, gdb_stdout
);
1425 printf_filtered (" and ends at ");
1426 print_address (end_pc
, gdb_stdout
);
1427 printf_filtered (".\n");
1430 /* x/i should display this line's code. */
1431 set_next_address (start_pc
);
1433 /* Repeating "info line" should do the following line. */
1434 last_line_listed
= sal
.line
+ 1;
1436 /* If this is the only line, show the source code. If it could
1437 not find the file, don't do anything special. */
1438 if (annotation_level
&& sals
.nelts
== 1)
1439 identify_source_line (sal
.symtab
, sal
.line
, 0, start_pc
);
1442 /* Is there any case in which we get here, and have an address
1443 which the user would want to see? If we have debugging symbols
1444 and no line numbers? */
1445 printf_filtered ("Line number %d is out of range for \"%s\".\n",
1446 sal
.line
, sal
.symtab
->filename
);
1451 /* Commands to search the source file for a regexp. */
1455 forward_search_command (char *regex
, int from_tty
)
1459 register FILE *stream
;
1465 ** If this is the TUI, search from the first line displayed in
1466 ** the source window, otherwise, search from last_line_listed+1
1467 ** in current_source_symtab
1470 line
= last_line_listed
;
1473 if (srcWin
->generic
.isVisible
&& srcWin
->generic
.contentSize
> 0)
1474 line
= ((TuiWinContent
)
1475 srcWin
->generic
.content
)[0]->whichElement
.source
.lineOrAddr
.lineNo
;
1478 printf_filtered ("No source displayed.\nExpression not found.\n");
1484 line
= last_line_listed
+ 1;
1487 msg
= (char *) re_comp (regex
);
1491 if (current_source_symtab
== 0)
1492 select_source_symtab (0);
1494 desc
= open_source_file (current_source_symtab
);
1496 perror_with_name (current_source_symtab
->filename
);
1498 if (current_source_symtab
->line_charpos
== 0)
1499 find_source_lines (current_source_symtab
, desc
);
1501 if (line
< 1 || line
> current_source_symtab
->nlines
)
1504 error ("Expression not found");
1507 if (lseek (desc
, current_source_symtab
->line_charpos
[line
- 1], 0) < 0)
1510 perror_with_name (current_source_symtab
->filename
);
1513 stream
= fdopen (desc
, FDOPEN_MODE
);
1517 static char *buf
= NULL
;
1519 int cursize
, newsize
;
1522 buf
= xmalloc (cursize
);
1531 if (p
- buf
== cursize
)
1533 newsize
= cursize
+ cursize
/ 2;
1534 buf
= xrealloc (buf
, newsize
);
1539 while (c
!= '\n' && (c
= getc (stream
)) >= 0);
1541 #ifdef CRLF_SOURCE_FILES
1542 /* Remove the \r, if any, at the end of the line, otherwise
1543 regular expressions that end with $ or \n won't work. */
1544 if (p
- buf
> 1 && p
[-2] == '\r')
1551 /* we now have a source line in buf, null terminate and match */
1553 if (re_exec (buf
) > 0)
1558 print_source_lines_base (current_source_symtab
, line
, line
+ 1, 0);
1559 print_source_lines (current_source_symtab
, line
, line
+ 1, 0);
1560 set_internalvar (lookup_internalvar ("_"),
1561 value_from_longest (builtin_type_int
,
1563 current_source_line
= max (line
- lines_to_list
/ 2, 1);
1569 printf_filtered ("Expression not found\n");
1575 reverse_search_command (char *regex
, int from_tty
)
1579 register FILE *stream
;
1584 ** If this is the TUI, search from the first line displayed in
1585 ** the source window, otherwise, search from last_line_listed-1
1586 ** in current_source_symtab
1589 line
= last_line_listed
;
1592 if (srcWin
->generic
.isVisible
&& srcWin
->generic
.contentSize
> 0)
1593 line
= ((TuiWinContent
)
1594 srcWin
->generic
.content
)[0]->whichElement
.source
.lineOrAddr
.lineNo
;
1597 printf_filtered ("No source displayed.\nExpression not found.\n");
1603 line
= last_line_listed
- 1;
1606 msg
= (char *) re_comp (regex
);
1610 if (current_source_symtab
== 0)
1611 select_source_symtab (0);
1613 desc
= open_source_file (current_source_symtab
);
1615 perror_with_name (current_source_symtab
->filename
);
1617 if (current_source_symtab
->line_charpos
== 0)
1618 find_source_lines (current_source_symtab
, desc
);
1620 if (line
< 1 || line
> current_source_symtab
->nlines
)
1623 error ("Expression not found");
1626 if (lseek (desc
, current_source_symtab
->line_charpos
[line
- 1], 0) < 0)
1629 perror_with_name (current_source_symtab
->filename
);
1632 stream
= fdopen (desc
, FDOPEN_MODE
);
1636 /* FIXME!!! We walk right off the end of buf if we get a long line!!! */
1637 char buf
[4096]; /* Should be reasonable??? */
1638 register char *p
= buf
;
1647 while (c
!= '\n' && (c
= getc (stream
)) >= 0);
1649 #ifdef CRLF_SOURCE_FILES
1650 /* Remove the \r, if any, at the end of the line, otherwise
1651 regular expressions that end with $ or \n won't work. */
1652 if (p
- buf
> 1 && p
[-2] == '\r')
1659 /* We now have a source line in buf; null terminate and match. */
1661 if (re_exec (buf
) > 0)
1666 print_source_lines_base (current_source_symtab
, line
, line
+ 1, 0);
1667 print_source_lines (current_source_symtab
, line
, line
+ 1, 0);
1668 set_internalvar (lookup_internalvar ("_"),
1669 value_from_longest (builtin_type_int
,
1671 current_source_line
= max (line
- lines_to_list
/ 2, 1);
1675 if (fseek (stream
, current_source_symtab
->line_charpos
[line
- 1], 0) < 0)
1678 perror_with_name (current_source_symtab
->filename
);
1682 printf_filtered ("Expression not found\n");
1688 _initialize_source (void)
1690 struct cmd_list_element
*c
;
1691 current_source_symtab
= 0;
1692 init_source_path ();
1694 /* The intention is to use POSIX Basic Regular Expressions.
1695 Always use the GNU regex routine for consistency across all hosts.
1696 Our current GNU regex.c does not have all the POSIX features, so this is
1697 just an approximation. */
1698 re_set_syntax (RE_SYNTAX_GREP
);
1700 c
= add_cmd ("directory", class_files
, directory_command
,
1701 "Add directory DIR to beginning of search path for source files.\n\
1702 Forget cached info on source file locations and line positions.\n\
1703 DIR can also be $cwd for the current working directory, or $cdir for the\n\
1704 directory in which the source file was compiled into object code.\n\
1705 With no argument, reset the search path to $cdir:$cwd, the default.",
1709 add_com_alias ("use", "directory", class_files
, 0);
1711 c
->completer
= filename_completer
;
1713 add_cmd ("directories", no_class
, show_directories
,
1714 "Current search path for finding source files.\n\
1715 $cwd in the path means the current working directory.\n\
1716 $cdir in the path means the compilation directory of the source file.",
1721 add_com_alias ("D", "directory", class_files
, 0);
1722 add_cmd ("ld", no_class
, show_directories
,
1723 "Current search path for finding source files.\n\
1724 $cwd in the path means the current working directory.\n\
1725 $cdir in the path means the compilation directory of the source file.",
1729 add_info ("source", source_info
,
1730 "Information about the current source file.");
1732 add_info ("line", line_info
,
1733 concat ("Core addresses of the code for a source line.\n\
1734 Line can be specified as\n\
1735 LINENUM, to list around that line in current file,\n\
1736 FILE:LINENUM, to list around that line in that file,\n\
1737 FUNCTION, to list around beginning of that function,\n\
1738 FILE:FUNCTION, to distinguish among like-named static functions.\n\
1740 Default is to describe the last source line that was listed.\n\n\
1741 This sets the default address for \"x\" to the line's first instruction\n\
1742 so that \"x/i\" suffices to start examining the machine code.\n\
1743 The address is also stored as the value of \"$_\".", NULL
));
1745 add_com ("forward-search", class_files
, forward_search_command
,
1746 "Search for regular expression (see regex(3)) from last line listed.\n\
1747 The matching line number is also stored as the value of \"$_\".");
1748 add_com_alias ("search", "forward-search", class_files
, 0);
1750 add_com ("reverse-search", class_files
, reverse_search_command
,
1751 "Search backward for regular expression (see regex(3)) from last line listed.\n\
1752 The matching line number is also stored as the value of \"$_\".");
1756 add_com_alias ("/", "forward-search", class_files
, 0);
1757 add_com_alias ("?", "reverse-search", class_files
, 0);
1760 add_com ("list", class_files
, list_command
,
1761 concat ("List specified function or line.\n\
1762 With no argument, lists ten more lines after or around previous listing.\n\
1763 \"list -\" lists the ten lines before a previous ten-line listing.\n\
1764 One argument specifies a line, and ten lines are listed around that line.\n\
1765 Two arguments with comma between specify starting and ending lines to list.\n\
1767 Lines can be specified in these ways:\n\
1768 LINENUM, to list around that line in current file,\n\
1769 FILE:LINENUM, to list around that line in that file,\n\
1770 FUNCTION, to list around beginning of that function,\n\
1771 FILE:FUNCTION, to distinguish among like-named static functions.\n\
1772 *ADDRESS, to list around the line containing that address.\n\
1773 With two args if one is empty it stands for ten lines away from the other arg.", NULL
));
1776 add_com_alias ("l", "list", class_files
, 1);
1778 add_com_alias ("v", "list", class_files
, 1);
1781 add_com_alias ("file", "list", class_files
, 1);
1784 (add_set_cmd ("listsize", class_support
, var_uinteger
,
1785 (char *) &lines_to_list
,
1786 "Set number of source lines gdb will list by default.",