1 /* List lines of source files for GDB, the GNU debugger.
2 Copyright 1986-1989, 1991-1999 Free Software Foundation, Inc.
4 This file is part of GDB.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
23 #include "expression.h"
31 #include <sys/types.h>
32 #include "gdb_string.h"
36 #include "gdb_regex.h"
45 #ifdef CRLF_SOURCE_FILES
47 /* Define CRLF_SOURCE_FILES in an xm-*.h file if source files on the
48 host use \r\n rather than just \n. Defining CRLF_SOURCE_FILES is
49 much faster than defining LSEEK_NOT_LINEAR. */
55 #define OPEN_MODE (O_RDONLY | O_BINARY)
56 #define FDOPEN_MODE FOPEN_RB
58 #else /* ! defined (CRLF_SOURCE_FILES) */
60 #define OPEN_MODE O_RDONLY
61 #define FDOPEN_MODE FOPEN_RT
63 #endif /* ! defined (CRLF_SOURCE_FILES) */
65 /* Prototypes for exported functions. */
67 void _initialize_source (void);
69 /* Prototypes for local functions. */
71 static int get_filename_and_charpos (struct symtab
*, char **);
73 static void reverse_search_command (char *, int);
75 static void forward_search_command (char *, int);
77 static void line_info (char *, int);
79 static void list_command (char *, int);
81 static void ambiguous_line_spec (struct symtabs_and_lines
*);
83 static void source_info (char *, int);
85 static void show_directories (char *, int);
87 /* Path of directories to search for source files.
88 Same format as the PATH environment variable's value. */
92 /* Symtab of default file for listing lines of. */
94 struct symtab
*current_source_symtab
;
96 /* Default next line to list. */
98 int current_source_line
;
100 /* Default number of lines to print with commands like "list".
101 This is based on guessing how many long (i.e. more than chars_per_line
102 characters) lines there will be. To be completely correct, "list"
103 and friends should be rewritten to count characters and see where
104 things are wrapping, but that would be a fair amount of work. */
106 int lines_to_list
= 10;
108 /* Line number of last line printed. Default for various commands.
109 current_source_line is usually, but not always, the same as this. */
111 static int last_line_listed
;
113 /* First line number listed by last listing command. */
115 static int first_line_listed
;
117 /* Saves the name of the last source file visited and a possible error code.
118 Used to prevent repeating annoying "No such file or directories" msgs */
120 static struct symtab
*last_source_visited
= NULL
;
121 static int last_source_error
= 0;
124 /* Set the source file default for the "list" command to be S.
126 If S is NULL, and we don't have a default, find one. This
127 should only be called when the user actually tries to use the
128 default, since we produce an error if we can't find a reasonable
129 default. Also, since this can cause symbols to be read, doing it
130 before we need to would make things slower than necessary. */
133 select_source_symtab (register struct symtab
*s
)
135 struct symtabs_and_lines sals
;
136 struct symtab_and_line sal
;
137 struct partial_symtab
*ps
;
138 struct partial_symtab
*cs_pst
= 0;
143 current_source_symtab
= s
;
144 current_source_line
= 1;
148 if (current_source_symtab
)
151 /* Make the default place to list be the function `main'
153 if (lookup_symbol ("main", 0, VAR_NAMESPACE
, 0, NULL
))
155 sals
= decode_line_spec ("main", 1);
158 current_source_symtab
= sal
.symtab
;
159 current_source_line
= max (sal
.line
- (lines_to_list
- 1), 1);
160 if (current_source_symtab
)
164 /* All right; find the last file in the symtab list (ignoring .h's). */
166 current_source_line
= 1;
168 for (ofp
= object_files
; ofp
!= NULL
; ofp
= ofp
->next
)
170 for (s
= ofp
->symtabs
; s
; s
= s
->next
)
172 char *name
= s
->filename
;
173 int len
= strlen (name
);
174 if (!(len
> 2 && (STREQ (&name
[len
- 2], ".h"))))
176 current_source_symtab
= s
;
180 if (current_source_symtab
)
183 /* Howabout the partial symbol tables? */
185 for (ofp
= object_files
; ofp
!= NULL
; ofp
= ofp
->next
)
187 for (ps
= ofp
->psymtabs
; ps
!= NULL
; ps
= ps
->next
)
189 char *name
= ps
->filename
;
190 int len
= strlen (name
);
191 if (!(len
> 2 && (STREQ (&name
[len
- 2], ".h"))))
201 internal_error ("select_source_symtab: readin pst found and no symtabs.");
205 current_source_symtab
= PSYMTAB_TO_SYMTAB (cs_pst
);
208 if (current_source_symtab
)
211 error ("Can't find a default source file");
215 show_directories (char *ignore
, int from_tty
)
217 puts_filtered ("Source directories searched: ");
218 puts_filtered (source_path
);
219 puts_filtered ("\n");
222 /* Forget what we learned about line positions in source files, and
223 which directories contain them; must check again now since files
224 may be found in a different directory now. */
227 forget_cached_source_info (void)
229 register struct symtab
*s
;
230 register struct objfile
*objfile
;
232 for (objfile
= object_files
; objfile
!= NULL
; objfile
= objfile
->next
)
234 for (s
= objfile
->symtabs
; s
!= NULL
; s
= s
->next
)
236 if (s
->line_charpos
!= NULL
)
238 mfree (objfile
->md
, s
->line_charpos
);
239 s
->line_charpos
= NULL
;
241 if (s
->fullname
!= NULL
)
243 mfree (objfile
->md
, s
->fullname
);
251 init_source_path (void)
255 sprintf (buf
, "$cdir%c$cwd", DIRNAME_SEPARATOR
);
256 source_path
= strsave (buf
);
257 forget_cached_source_info ();
260 /* Add zero or more directories to the front of the source path. */
263 directory_command (char *dirname
, int from_tty
)
266 /* FIXME, this goes to "delete dir"... */
269 if (from_tty
&& query ("Reinitialize source path to empty? "))
277 mod_path (dirname
, &source_path
);
278 last_source_visited
= NULL
;
281 show_directories ((char *) 0, from_tty
);
282 forget_cached_source_info ();
285 /* Add zero or more directories to the front of an arbitrary path. */
288 mod_path (char *dirname
, char **which_path
)
290 char *old
= *which_path
;
296 dirname
= strsave (dirname
);
297 make_cleanup (free
, dirname
);
301 char *name
= dirname
;
306 char *separator
= strchr (name
, DIRNAME_SEPARATOR
);
307 char *space
= strchr (name
, ' ');
308 char *tab
= strchr (name
, '\t');
310 if (separator
== 0 && space
== 0 && tab
== 0)
311 p
= dirname
= name
+ strlen (name
);
315 if (separator
!= 0 && (p
== 0 || separator
< p
))
317 if (space
!= 0 && (p
== 0 || space
< p
))
319 if (tab
!= 0 && (p
== 0 || tab
< p
))
322 while (*dirname
== DIRNAME_SEPARATOR
329 if (!(SLASH_P (*name
) && p
<= name
+ 1) /* "/" */
330 #if defined(_WIN32) || defined(__MSDOS__)
331 /* On MS-DOS and MS-Windows, h:\ is different from h: */
332 && !(!SLASH_P (*name
) && ROOTED_P (name
) && p
<= name
+ 3) /* d:/ */
335 /* Sigh. "foo/" => "foo" */
339 while (p
> name
&& p
[-1] == '.')
343 /* "." => getwd (). */
344 name
= current_directory
;
347 else if (p
> name
+ 1 && SLASH_P (p
[-2]))
357 /* "...foo/." => "...foo". */
368 name
= tilde_expand (name
);
369 #if defined(_WIN32) || defined(__MSDOS__)
370 else if (ROOTED_P (name
) && p
== name
+ 2) /* "d:" => "d:." */
371 name
= concat (name
, ".", NULL
);
373 else if (!ROOTED_P (name
) && name
[0] != '$')
374 name
= concat (current_directory
, SLASH_STRING
, name
, NULL
);
376 name
= savestring (name
, p
- name
);
377 make_cleanup (free
, name
);
379 /* Unless it's a variable, check existence. */
382 /* These are warnings, not errors, since we don't want a
383 non-existent directory in a .gdbinit file to stop processing
384 of the .gdbinit file.
386 Whether they get added to the path is more debatable. Current
387 answer is yes, in case the user wants to go make the directory
388 or whatever. If the directory continues to not exist/not be
389 a directory/etc, then having them in the path should be
391 if (stat (name
, &st
) < 0)
393 int save_errno
= errno
;
394 fprintf_unfiltered (gdb_stderr
, "Warning: ");
395 print_sys_errmsg (name
, save_errno
);
397 else if ((st
.st_mode
& S_IFMT
) != S_IFDIR
)
398 warning ("%s is not a directory.", name
);
403 register unsigned int len
= strlen (name
);
408 /* FIXME: strncmp loses in interesting ways on MS-DOS and
409 MS-Windows because of case-insensitivity and two different
410 but functionally identical slash characters. We need a
411 special filesystem-dependent file-name comparison function.
413 Actually, even on Unix I would use realpath() or its work-
414 alike before comparing. Then all the code above which
415 removes excess slashes and dots could simply go away. */
416 if (!strncmp (p
, name
, len
)
417 && (p
[len
] == '\0' || p
[len
] == DIRNAME_SEPARATOR
))
419 /* Found it in the search path, remove old copy */
421 p
--; /* Back over leading separator */
422 if (prefix
> p
- *which_path
)
423 goto skip_dup
; /* Same dir twice in one cmd */
424 strcpy (p
, &p
[len
+ 1]); /* Copy from next \0 or : */
426 p
= strchr (p
, DIRNAME_SEPARATOR
);
436 tinybuf
[0] = DIRNAME_SEPARATOR
;
439 /* 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. */
446 temp
= concat (old
, tinybuf
, name
, NULL
);
448 *which_path
= concat (temp
, "", &old
[prefix
], NULL
);
449 prefix
= strlen (temp
);
454 *which_path
= concat (name
, (old
[0] ? tinybuf
: old
), old
, NULL
);
455 prefix
= strlen (name
);
463 while (*dirname
!= '\0');
468 source_info (char *ignore
, int from_tty
)
470 register struct symtab
*s
= current_source_symtab
;
474 printf_filtered ("No current source file.\n");
477 printf_filtered ("Current source file is %s\n", s
->filename
);
479 printf_filtered ("Compilation directory is %s\n", s
->dirname
);
481 printf_filtered ("Located in %s\n", s
->fullname
);
483 printf_filtered ("Contains %d line%s.\n", s
->nlines
,
484 s
->nlines
== 1 ? "" : "s");
486 printf_filtered ("Source language is %s.\n", language_str (s
->language
));
487 printf_filtered ("Compiled with %s debugging format.\n", s
->debugformat
);
492 /* Open a file named STRING, searching path PATH (dir names sep by some char)
493 using mode MODE and protection bits PROT in the calls to open.
495 If TRY_CWD_FIRST, try to open ./STRING before searching PATH.
496 (ie pretend the first element of PATH is "."). This also indicates
497 that a slash in STRING disables searching of the path (this is
498 so that "exec-file ./foo" or "symbol-file ./foo" insures that you
499 get that particular version of foo or an error message).
501 If FILENAMED_OPENED is non-null, set it to a newly allocated string naming
502 the actual file opened (this string will always start with a "/". We
503 have to take special pains to avoid doubling the "/" between the directory
504 and the file, sigh! Emacs gets confuzzed by this when we print the
507 If a file is found, return the descriptor.
508 Otherwise, return -1, with errno set for the last name we tried to open. */
510 /* >>>> This should only allow files of certain types,
511 >>>> eg executable, non-directory */
513 openp (char *path
, int try_cwd_first
, char *string
, int mode
, int prot
,
514 char **filename_opened
)
517 register char *filename
;
518 register char *p
, *p1
;
529 if (try_cwd_first
|| ROOTED_P (string
))
533 fd
= open (filename
, mode
, prot
);
536 for (i
= 0; string
[i
]; i
++)
537 if (SLASH_P (string
[i
]))
542 while (string
[0] == '.' && SLASH_P (string
[1]))
545 alloclen
= strlen (path
) + strlen (string
) + 2;
546 filename
= (char *) alloca (alloclen
);
548 for (p
= path
; p
; p
= p1
? p1
+ 1 : 0)
550 p1
= (char *) strchr (p
, DIRNAME_SEPARATOR
);
556 if (len
== 4 && p
[0] == '$' && p
[1] == 'c'
557 && p
[2] == 'w' && p
[3] == 'd')
559 /* Name is $cwd -- insert current directory name instead. */
562 /* First, realloc the filename buffer if too short. */
563 len
= strlen (current_directory
);
564 newlen
= len
+ strlen (string
) + 2;
565 if (newlen
> alloclen
)
568 filename
= (char *) alloca (alloclen
);
570 strcpy (filename
, current_directory
);
574 /* Normal file name in path -- just use it. */
575 strncpy (filename
, p
, len
);
579 /* Remove trailing slashes */
580 while (len
> 0 && SLASH_P (filename
[len
- 1]))
583 strcat (filename
+ len
, SLASH_STRING
);
584 strcat (filename
, string
);
586 fd
= open (filename
, mode
);
595 *filename_opened
= (char *) 0;
596 else if (ROOTED_P (filename
))
597 *filename_opened
= savestring (filename
, strlen (filename
));
600 /* Beware the // my son, the Emacs barfs, the botch that catch... */
602 *filename_opened
= concat (current_directory
,
603 SLASH_P (current_directory
[strlen (current_directory
) - 1])
609 /* This is a debugging hack that can go away when all combinations
610 of Mac and Unix names are handled reasonably. */
612 extern int debug_openp
;
616 printf ("openp on %s, path %s mode %d prot %d\n returned %d",
617 string
, path
, mode
, prot
, fd
);
618 if (*filename_opened
)
619 printf (" (filename is %s)", *filename_opened
);
629 /* This is essentially a convenience, for clients that want the behaviour
630 of openp, using source_path, but that really don't want the file to be
631 opened but want instead just to know what the full pathname is (as
632 qualified against source_path).
634 The current working directory is searched first.
636 If the file was found, this function returns 1, and FULL_PATHNAME is
637 set to the fully-qualified pathname.
639 Else, this functions returns 0, and FULL_PATHNAME is set to NULL.
642 source_full_path_of (char *filename
, char **full_pathname
)
646 fd
= openp (source_path
, 1, filename
, O_RDONLY
, 0, full_pathname
);
649 *full_pathname
= NULL
;
658 /* Open a source file given a symtab S. Returns a file descriptor or
659 negative number for error. */
662 open_source_file (struct symtab
*s
)
664 char *path
= source_path
;
669 /* Quick way out if we already know its full name */
672 result
= open (s
->fullname
, OPEN_MODE
);
675 /* Didn't work -- free old one, try again. */
676 mfree (s
->objfile
->md
, s
->fullname
);
680 if (s
->dirname
!= NULL
)
682 /* Replace a path entry of $cdir with the compilation directory name */
684 /* We cast strstr's result in case an ANSIhole has made it const,
685 which produces a "required warning" when assigned to a nonconst. */
686 p
= (char *) strstr (source_path
, "$cdir");
687 if (p
&& (p
== path
|| p
[-1] == DIRNAME_SEPARATOR
)
688 && (p
[cdir_len
] == DIRNAME_SEPARATOR
|| p
[cdir_len
] == '\0'))
693 alloca (strlen (source_path
) + 1 + strlen (s
->dirname
) + 1);
694 len
= p
- source_path
;
695 strncpy (path
, source_path
, len
); /* Before $cdir */
696 strcpy (path
+ len
, s
->dirname
); /* new stuff */
697 strcat (path
+ len
, source_path
+ len
+ cdir_len
); /* After $cdir */
701 result
= openp (path
, 0, s
->filename
, OPEN_MODE
, 0, &s
->fullname
);
704 /* Didn't work. Try using just the basename. */
705 p
= basename (s
->filename
);
706 if (p
!= s
->filename
)
707 result
= openp (path
, 0, p
, OPEN_MODE
, 0, &s
->fullname
);
712 /* Didn't work. Try using just the MPW basename. */
713 p
= (char *) mpw_basename (s
->filename
);
714 if (p
!= s
->filename
)
715 result
= openp (path
, 0, p
, OPEN_MODE
, 0, &s
->fullname
);
719 /* Didn't work. Try using the mixed Unix/MPW basename. */
720 p
= (char *) mpw_mixed_basename (s
->filename
);
721 if (p
!= s
->filename
)
722 result
= openp (path
, 0, p
, OPEN_MODE
, 0, &s
->fullname
);
728 fullname
= s
->fullname
;
729 s
->fullname
= mstrsave (s
->objfile
->md
, s
->fullname
);
735 /* Return the path to the source file associated with symtab. Returns NULL
739 symtab_to_filename (struct symtab
*s
)
746 /* If we've seen the file before, just return fullname. */
751 /* Try opening the file to setup fullname */
753 fd
= open_source_file (s
);
755 return s
->filename
; /* File not found. Just use short name */
757 /* Found the file. Cleanup and return the full name */
764 /* Create and initialize the table S->line_charpos that records
765 the positions of the lines in the source file, which is assumed
766 to be open on descriptor DESC.
767 All set S->nlines to the number of such lines. */
770 find_source_lines (struct symtab
*s
, int desc
)
773 register char *data
, *p
, *end
;
775 int lines_allocated
= 1000;
780 line_charpos
= (int *) xmmalloc (s
->objfile
->md
,
781 lines_allocated
* sizeof (int));
782 if (fstat (desc
, &st
) < 0)
783 perror_with_name (s
->filename
);
785 if (s
&& s
->objfile
&& s
->objfile
->obfd
)
786 mtime
= bfd_get_mtime (s
->objfile
->obfd
);
788 mtime
= bfd_get_mtime (exec_bfd
);
790 if (mtime
&& mtime
< st
.st_mtime
)
793 printf_filtered ("\n");
794 warning ("Source file is more recent than executable.\n");
797 #ifdef LSEEK_NOT_LINEAR
801 /* Have to read it byte by byte to find out where the chars live */
803 line_charpos
[0] = lseek (desc
, 0, SEEK_CUR
);
805 while (myread (desc
, &c
, 1) > 0)
809 if (nlines
== lines_allocated
)
811 lines_allocated
*= 2;
813 (int *) xmrealloc (s
->objfile
->md
, (char *) line_charpos
,
814 sizeof (int) * lines_allocated
);
816 line_charpos
[nlines
++] = lseek (desc
, 0, SEEK_CUR
);
820 #else /* lseek linear. */
822 struct cleanup
*old_cleanups
;
824 /* st_size might be a large type, but we only support source files whose
825 size fits in an int. */
826 size
= (int) st
.st_size
;
828 /* Use malloc, not alloca, because this may be pretty large, and we may
829 run into various kinds of limits on stack size. */
830 data
= (char *) xmalloc (size
);
831 old_cleanups
= make_cleanup (free
, data
);
833 /* Reassign `size' to result of read for systems where \r\n -> \n. */
834 size
= myread (desc
, data
, size
);
836 perror_with_name (s
->filename
);
844 /* A newline at the end does not start a new line. */
847 if (nlines
== lines_allocated
)
849 lines_allocated
*= 2;
851 (int *) xmrealloc (s
->objfile
->md
, (char *) line_charpos
,
852 sizeof (int) * lines_allocated
);
854 line_charpos
[nlines
++] = p
- data
;
857 do_cleanups (old_cleanups
);
859 #endif /* lseek linear. */
862 (int *) xmrealloc (s
->objfile
->md
, (char *) line_charpos
,
863 nlines
* sizeof (int));
867 /* Return the character position of a line LINE in symtab S.
868 Return 0 if anything is invalid. */
870 #if 0 /* Currently unused */
873 source_line_charpos (struct symtab
*s
, int line
)
877 if (!s
->line_charpos
|| line
<= 0)
879 if (line
> s
->nlines
)
881 return s
->line_charpos
[line
- 1];
884 /* Return the line number of character position POS in symtab S. */
887 source_charpos_line (register struct symtab
*s
, register int chr
)
889 register int line
= 0;
892 if (s
== 0 || s
->line_charpos
== 0)
894 lnp
= s
->line_charpos
;
895 /* Files are usually short, so sequential search is Ok */
896 while (line
< s
->nlines
&& *lnp
<= chr
)
901 if (line
>= s
->nlines
)
909 /* Get full pathname and line number positions for a symtab.
910 Return nonzero if line numbers may have changed.
911 Set *FULLNAME to actual name of the file as found by `openp',
912 or to 0 if the file is not found. */
915 get_filename_and_charpos (struct symtab
*s
, char **fullname
)
917 register int desc
, linenums_changed
= 0;
919 desc
= open_source_file (s
);
927 *fullname
= s
->fullname
;
928 if (s
->line_charpos
== 0)
929 linenums_changed
= 1;
930 if (linenums_changed
)
931 find_source_lines (s
, desc
);
933 return linenums_changed
;
936 /* Print text describing the full name of the source file S
937 and the line number LINE and its corresponding character position.
938 The text starts with two Ctrl-z so that the Emacs-GDB interface
941 MID_STATEMENT is nonzero if the PC is not at the beginning of that line.
943 Return 1 if successful, 0 if could not find the file. */
946 identify_source_line (struct symtab
*s
, int line
, int mid_statement
,
949 if (s
->line_charpos
== 0)
950 get_filename_and_charpos (s
, (char **) NULL
);
951 if (s
->fullname
== 0)
953 if (line
> s
->nlines
)
954 /* Don't index off the end of the line_charpos array. */
956 annotate_source (s
->fullname
, line
, s
->line_charpos
[line
- 1],
959 current_source_line
= line
;
960 first_line_listed
= line
;
961 last_line_listed
= line
;
962 current_source_symtab
= s
;
967 /* Print source lines from the file of symtab S,
968 starting with line number LINE and stopping before line number STOPLINE. */
970 static void print_source_lines_base (struct symtab
*s
, int line
, int stopline
,
973 print_source_lines_base (struct symtab
*s
, int line
, int stopline
, int noerror
)
977 register FILE *stream
;
978 int nlines
= stopline
- line
;
980 /* Regardless of whether we can open the file, set current_source_symtab. */
981 current_source_symtab
= s
;
982 current_source_line
= line
;
983 first_line_listed
= line
;
986 /* If printing of source lines is disabled, just print file and line number */
987 if (ui_out_test_flags (uiout
, ui_source_list
))
990 /* Only prints "No such file or directory" once */
991 if ((s
!= last_source_visited
) || (!last_source_error
))
993 last_source_visited
= s
;
994 desc
= open_source_file (s
);
998 desc
= last_source_error
;
1012 last_source_error
= desc
;
1016 char *name
= alloca (strlen (s
->filename
) + 100);
1017 sprintf (name
, "%d\t%s", line
, s
->filename
);
1018 print_sys_errmsg (name
, errno
);
1022 ui_out_field_int (uiout
, "line", line
);
1023 ui_out_text (uiout
, "\tin ");
1024 ui_out_field_string (uiout
, "file", s
->filename
);
1025 ui_out_text (uiout
, "\n");
1027 printf_filtered ("%d\tin %s\n", line
, s
->filename
);
1033 last_source_error
= 0;
1035 if (s
->line_charpos
== 0)
1036 find_source_lines (s
, desc
);
1038 if (line
< 1 || line
> s
->nlines
)
1041 error ("Line number %d out of range; %s has %d lines.",
1042 line
, s
->filename
, s
->nlines
);
1045 if (lseek (desc
, s
->line_charpos
[line
- 1], 0) < 0)
1048 perror_with_name (s
->filename
);
1051 stream
= fdopen (desc
, FDOPEN_MODE
);
1054 while (nlines
-- > 0)
1062 last_line_listed
= current_source_line
;
1063 sprintf (buf
, "%d\t", current_source_line
++);
1064 ui_out_text (uiout
, buf
);
1067 if (c
< 040 && c
!= '\t' && c
!= '\n' && c
!= '\r')
1069 sprintf (buf
, "^%c", c
+ 0100);
1070 ui_out_text (uiout
, buf
);
1073 ui_out_text (uiout
, "^?");
1074 #ifdef CRLF_SOURCE_FILES
1077 /* Skip a \r character, but only before a \n. */
1078 int c1
= fgetc (stream
);
1081 printf_filtered ("^%c", c
+ 0100);
1083 ungetc (c1
, stream
);
1088 sprintf (buf
, "%c", c
);
1089 ui_out_text (uiout
, buf
);
1092 while (c
!= '\n' && (c
= fgetc (stream
)) >= 0);
1097 last_line_listed
= current_source_line
;
1098 printf_filtered ("%d\t", current_source_line
++);
1101 if (c
< 040 && c
!= '\t' && c
!= '\n' && c
!= '\r')
1102 printf_filtered ("^%c", c
+ 0100);
1104 printf_filtered ("^?");
1105 #ifdef CRLF_SOURCE_FILES
1108 /* Just skip \r characters. */
1112 printf_filtered ("%c", c
);
1114 while (c
!= '\n' && (c
= fgetc (stream
)) >= 0);
1121 /* Show source lines from the file of symtab S, starting with line
1122 number LINE and stopping before line number STOPLINE. If this is the
1123 not the command line version, then the source is shown in the source
1124 window otherwise it is simply printed */
1127 print_source_lines (struct symtab
*s
, int line
, int stopline
, int noerror
)
1131 m_winPtrIsNull (srcWin
) || !srcWin
->generic
.isVisible
)
1132 print_source_lines_base (s
, line
, stopline
, noerror
);
1135 TuiGenWinInfoPtr locator
= locatorWinInfoPtr ();
1136 extern void tui_vAddWinToLayout (va_list);
1137 extern void tui_vUpdateSourceWindowsWithLine (va_list);
1139 /* Regardless of whether we can open the file,
1140 set current_source_symtab. */
1141 current_source_symtab
= s
;
1142 current_source_line
= line
;
1143 first_line_listed
= line
;
1145 /* make sure that the source window is displayed */
1146 tuiDo ((TuiOpaqueFuncPtr
) tui_vAddWinToLayout
, SRC_WIN
);
1148 tuiDo ((TuiOpaqueFuncPtr
) tui_vUpdateSourceWindowsWithLine
, s
, line
);
1149 tuiDo ((TuiOpaqueFuncPtr
) tui_vUpdateLocatorFilename
, s
->filename
);
1152 print_source_lines_base (s
, line
, stopline
, noerror
);
1158 /* Print a list of files and line numbers which a user may choose from
1159 in order to list a function which was specified ambiguously (as with
1160 `list classname::overloadedfuncname', for example). The vector in
1161 SALS provides the filenames and line numbers. */
1164 ambiguous_line_spec (struct symtabs_and_lines
*sals
)
1168 for (i
= 0; i
< sals
->nelts
; ++i
)
1169 printf_filtered ("file: \"%s\", line number: %d\n",
1170 sals
->sals
[i
].symtab
->filename
, sals
->sals
[i
].line
);
1174 list_command (char *arg
, int from_tty
)
1176 struct symtabs_and_lines sals
, sals_end
;
1177 struct symtab_and_line sal
, sal_end
;
1183 int linenum_beg
= 0;
1186 if (!have_full_symbols () && !have_partial_symbols ())
1187 error ("No symbol table is loaded. Use the \"file\" command.");
1189 /* Pull in a current source symtab if necessary */
1190 if (current_source_symtab
== 0 &&
1191 (arg
== 0 || arg
[0] == '+' || arg
[0] == '-'))
1192 select_source_symtab (0);
1194 /* "l" or "l +" lists next ten lines. */
1196 if (arg
== 0 || STREQ (arg
, "+"))
1198 if (current_source_symtab
== 0)
1199 error ("No default source file yet. Do \"help list\".");
1200 print_source_lines (current_source_symtab
, current_source_line
,
1201 current_source_line
+ lines_to_list
, 0);
1205 /* "l -" lists previous ten lines, the ones before the ten just listed. */
1206 if (STREQ (arg
, "-"))
1208 if (current_source_symtab
== 0)
1209 error ("No default source file yet. Do \"help list\".");
1210 print_source_lines (current_source_symtab
,
1211 max (first_line_listed
- lines_to_list
, 1),
1212 first_line_listed
, 0);
1216 /* Now if there is only one argument, decode it in SAL
1218 If there are two arguments, decode them in SAL and SAL_END
1219 and clear NO_END; however, if one of the arguments is blank,
1220 set DUMMY_BEG or DUMMY_END to record that fact. */
1227 sals
= decode_line_1 (&arg1
, 0, 0, 0, 0);
1233 ambiguous_line_spec (&sals
);
1242 /* Record whether the BEG arg is all digits. */
1244 for (p
= arg
; p
!= arg1
&& *p
>= '0' && *p
<= '9'; p
++);
1245 linenum_beg
= (p
== arg1
);
1247 while (*arg1
== ' ' || *arg1
== '\t')
1253 while (*arg1
== ' ' || *arg1
== '\t')
1260 sals_end
= decode_line_1 (&arg1
, 0, 0, 0, 0);
1262 sals_end
= decode_line_1 (&arg1
, 0, sal
.symtab
, sal
.line
, 0);
1263 if (sals_end
.nelts
== 0)
1265 if (sals_end
.nelts
> 1)
1267 ambiguous_line_spec (&sals_end
);
1268 free (sals_end
.sals
);
1271 sal_end
= sals_end
.sals
[0];
1272 free (sals_end
.sals
);
1277 error ("Junk at end of line specification.");
1279 if (!no_end
&& !dummy_beg
&& !dummy_end
1280 && sal
.symtab
!= sal_end
.symtab
)
1281 error ("Specified start and end are in different files.");
1282 if (dummy_beg
&& dummy_end
)
1283 error ("Two empty args do not say what lines to list.");
1285 /* if line was specified by address,
1286 first print exactly which line, and which file.
1287 In this case, sal.symtab == 0 means address is outside
1288 of all known source files, not that user failed to give a filename. */
1291 if (sal
.symtab
== 0)
1292 /* FIXME-32x64--assumes sal.pc fits in long. */
1293 error ("No source file for address %s.",
1294 local_hex_string ((unsigned long) sal
.pc
));
1295 sym
= find_pc_function (sal
.pc
);
1298 print_address_numeric (sal
.pc
, 1, gdb_stdout
);
1299 printf_filtered (" is in ");
1300 fputs_filtered (SYMBOL_SOURCE_NAME (sym
), gdb_stdout
);
1301 printf_filtered (" (%s:%d).\n", sal
.symtab
->filename
, sal
.line
);
1305 print_address_numeric (sal
.pc
, 1, gdb_stdout
);
1306 printf_filtered (" is at %s:%d.\n",
1307 sal
.symtab
->filename
, sal
.line
);
1311 /* If line was not specified by just a line number,
1312 and it does not imply a symtab, it must be an undebuggable symbol
1313 which means no source code. */
1315 if (!linenum_beg
&& sal
.symtab
== 0)
1316 error ("No line number known for %s.", arg
);
1318 /* If this command is repeated with RET,
1319 turn it into the no-arg variant. */
1324 if (dummy_beg
&& sal_end
.symtab
== 0)
1325 error ("No default source file yet. Do \"help list\".");
1327 print_source_lines (sal_end
.symtab
,
1328 max (sal_end
.line
- (lines_to_list
- 1), 1),
1329 sal_end
.line
+ 1, 0);
1330 else if (sal
.symtab
== 0)
1331 error ("No default source file yet. Do \"help list\".");
1334 int first_line
= sal
.line
- lines_to_list
/ 2;
1336 if (first_line
< 1) first_line
= 1;
1338 print_source_lines (sal
.symtab
, first_line
, first_line
+ lines_to_list
,
1342 print_source_lines (sal
.symtab
, sal
.line
,
1344 ? sal
.line
+ lines_to_list
1345 : sal_end
.line
+ 1),
1349 /* Print info on range of pc's in a specified line. */
1352 line_info (char *arg
, int from_tty
)
1354 struct symtabs_and_lines sals
;
1355 struct symtab_and_line sal
;
1356 CORE_ADDR start_pc
, end_pc
;
1359 INIT_SAL (&sal
); /* initialize to zeroes */
1363 sal
.symtab
= current_source_symtab
;
1364 sal
.line
= last_line_listed
;
1366 sals
.sals
= (struct symtab_and_line
*)
1367 xmalloc (sizeof (struct symtab_and_line
));
1372 sals
= decode_line_spec_1 (arg
, 0);
1377 /* C++ More than one line may have been specified, as when the user
1378 specifies an overloaded function name. Print info on them all. */
1379 for (i
= 0; i
< sals
.nelts
; i
++)
1383 if (sal
.symtab
== 0)
1385 printf_filtered ("No line number information available");
1388 /* This is useful for "info line *0x7f34". If we can't tell the
1389 user about a source line, at least let them have the symbolic
1391 printf_filtered (" for address ");
1393 print_address (sal
.pc
, gdb_stdout
);
1396 printf_filtered (".");
1397 printf_filtered ("\n");
1399 else if (sal
.line
> 0
1400 && find_line_pc_range (sal
, &start_pc
, &end_pc
))
1402 if (start_pc
== end_pc
)
1404 printf_filtered ("Line %d of \"%s\"",
1405 sal
.line
, sal
.symtab
->filename
);
1407 printf_filtered (" is at address ");
1408 print_address (start_pc
, gdb_stdout
);
1410 printf_filtered (" but contains no code.\n");
1414 printf_filtered ("Line %d of \"%s\"",
1415 sal
.line
, sal
.symtab
->filename
);
1417 printf_filtered (" starts at address ");
1418 print_address (start_pc
, gdb_stdout
);
1420 printf_filtered (" and ends at ");
1421 print_address (end_pc
, gdb_stdout
);
1422 printf_filtered (".\n");
1425 /* x/i should display this line's code. */
1426 set_next_address (start_pc
);
1428 /* Repeating "info line" should do the following line. */
1429 last_line_listed
= sal
.line
+ 1;
1431 /* If this is the only line, show the source code. If it could
1432 not find the file, don't do anything special. */
1433 if (annotation_level
&& sals
.nelts
== 1)
1434 identify_source_line (sal
.symtab
, sal
.line
, 0, start_pc
);
1437 /* Is there any case in which we get here, and have an address
1438 which the user would want to see? If we have debugging symbols
1439 and no line numbers? */
1440 printf_filtered ("Line number %d is out of range for \"%s\".\n",
1441 sal
.line
, sal
.symtab
->filename
);
1446 /* Commands to search the source file for a regexp. */
1450 forward_search_command (char *regex
, int from_tty
)
1454 register FILE *stream
;
1460 ** If this is the TUI, search from the first line displayed in
1461 ** the source window, otherwise, search from last_line_listed+1
1462 ** in current_source_symtab
1465 line
= last_line_listed
;
1468 if (srcWin
->generic
.isVisible
&& srcWin
->generic
.contentSize
> 0)
1469 line
= ((TuiWinContent
)
1470 srcWin
->generic
.content
)[0]->whichElement
.source
.lineOrAddr
.lineNo
;
1473 printf_filtered ("No source displayed.\nExpression not found.\n");
1479 line
= last_line_listed
+ 1;
1482 msg
= (char *) re_comp (regex
);
1486 if (current_source_symtab
== 0)
1487 select_source_symtab (0);
1489 desc
= open_source_file (current_source_symtab
);
1491 perror_with_name (current_source_symtab
->filename
);
1493 if (current_source_symtab
->line_charpos
== 0)
1494 find_source_lines (current_source_symtab
, desc
);
1496 if (line
< 1 || line
> current_source_symtab
->nlines
)
1499 error ("Expression not found");
1502 if (lseek (desc
, current_source_symtab
->line_charpos
[line
- 1], 0) < 0)
1505 perror_with_name (current_source_symtab
->filename
);
1508 stream
= fdopen (desc
, FDOPEN_MODE
);
1512 static char *buf
= NULL
;
1514 int cursize
, newsize
;
1517 buf
= xmalloc (cursize
);
1526 if (p
- buf
== cursize
)
1528 newsize
= cursize
+ cursize
/ 2;
1529 buf
= xrealloc (buf
, newsize
);
1534 while (c
!= '\n' && (c
= getc (stream
)) >= 0);
1536 #ifdef CRLF_SOURCE_FILES
1537 /* Remove the \r, if any, at the end of the line, otherwise
1538 regular expressions that end with $ or \n won't work. */
1539 if (p
- buf
> 1 && p
[-2] == '\r')
1546 /* we now have a source line in buf, null terminate and match */
1548 if (re_exec (buf
) > 0)
1553 print_source_lines_base (current_source_symtab
, line
, line
+ 1, 0);
1554 print_source_lines (current_source_symtab
, line
, line
+ 1, 0);
1555 set_internalvar (lookup_internalvar ("_"),
1556 value_from_longest (builtin_type_int
,
1558 current_source_line
= max (line
- lines_to_list
/ 2, 1);
1564 printf_filtered ("Expression not found\n");
1570 reverse_search_command (char *regex
, int from_tty
)
1574 register FILE *stream
;
1579 ** If this is the TUI, search from the first line displayed in
1580 ** the source window, otherwise, search from last_line_listed-1
1581 ** in current_source_symtab
1584 line
= last_line_listed
;
1587 if (srcWin
->generic
.isVisible
&& srcWin
->generic
.contentSize
> 0)
1588 line
= ((TuiWinContent
)
1589 srcWin
->generic
.content
)[0]->whichElement
.source
.lineOrAddr
.lineNo
;
1592 printf_filtered ("No source displayed.\nExpression not found.\n");
1598 line
= last_line_listed
- 1;
1601 msg
= (char *) re_comp (regex
);
1605 if (current_source_symtab
== 0)
1606 select_source_symtab (0);
1608 desc
= open_source_file (current_source_symtab
);
1610 perror_with_name (current_source_symtab
->filename
);
1612 if (current_source_symtab
->line_charpos
== 0)
1613 find_source_lines (current_source_symtab
, desc
);
1615 if (line
< 1 || line
> current_source_symtab
->nlines
)
1618 error ("Expression not found");
1621 if (lseek (desc
, current_source_symtab
->line_charpos
[line
- 1], 0) < 0)
1624 perror_with_name (current_source_symtab
->filename
);
1627 stream
= fdopen (desc
, FDOPEN_MODE
);
1631 /* FIXME!!! We walk right off the end of buf if we get a long line!!! */
1632 char buf
[4096]; /* Should be reasonable??? */
1633 register char *p
= buf
;
1642 while (c
!= '\n' && (c
= getc (stream
)) >= 0);
1644 #ifdef CRLF_SOURCE_FILES
1645 /* Remove the \r, if any, at the end of the line, otherwise
1646 regular expressions that end with $ or \n won't work. */
1647 if (p
- buf
> 1 && p
[-2] == '\r')
1654 /* We now have a source line in buf; null terminate and match. */
1656 if (re_exec (buf
) > 0)
1661 print_source_lines_base (current_source_symtab
, line
, line
+ 1, 0);
1662 print_source_lines (current_source_symtab
, line
, line
+ 1, 0);
1663 set_internalvar (lookup_internalvar ("_"),
1664 value_from_longest (builtin_type_int
,
1666 current_source_line
= max (line
- lines_to_list
/ 2, 1);
1670 if (fseek (stream
, current_source_symtab
->line_charpos
[line
- 1], 0) < 0)
1673 perror_with_name (current_source_symtab
->filename
);
1677 printf_filtered ("Expression not found\n");
1683 _initialize_source (void)
1685 struct cmd_list_element
*c
;
1686 current_source_symtab
= 0;
1687 init_source_path ();
1689 /* The intention is to use POSIX Basic Regular Expressions.
1690 Always use the GNU regex routine for consistency across all hosts.
1691 Our current GNU regex.c does not have all the POSIX features, so this is
1692 just an approximation. */
1693 re_set_syntax (RE_SYNTAX_GREP
);
1695 c
= add_cmd ("directory", class_files
, directory_command
,
1696 "Add directory DIR to beginning of search path for source files.\n\
1697 Forget cached info on source file locations and line positions.\n\
1698 DIR can also be $cwd for the current working directory, or $cdir for the\n\
1699 directory in which the source file was compiled into object code.\n\
1700 With no argument, reset the search path to $cdir:$cwd, the default.",
1704 add_com_alias ("use", "directory", class_files
, 0);
1706 c
->completer
= filename_completer
;
1708 add_cmd ("directories", no_class
, show_directories
,
1709 "Current search path for finding source files.\n\
1710 $cwd in the path means the current working directory.\n\
1711 $cdir in the path means the compilation directory of the source file.",
1716 add_com_alias ("D", "directory", class_files
, 0);
1717 add_cmd ("ld", no_class
, show_directories
,
1718 "Current search path for finding source files.\n\
1719 $cwd in the path means the current working directory.\n\
1720 $cdir in the path means the compilation directory of the source file.",
1724 add_info ("source", source_info
,
1725 "Information about the current source file.");
1727 add_info ("line", line_info
,
1728 concat ("Core addresses of the code for a source line.\n\
1729 Line can be specified as\n\
1730 LINENUM, to list around that line in current file,\n\
1731 FILE:LINENUM, to list around that line in that file,\n\
1732 FUNCTION, to list around beginning of that function,\n\
1733 FILE:FUNCTION, to distinguish among like-named static functions.\n\
1735 Default is to describe the last source line that was listed.\n\n\
1736 This sets the default address for \"x\" to the line's first instruction\n\
1737 so that \"x/i\" suffices to start examining the machine code.\n\
1738 The address is also stored as the value of \"$_\".", NULL
));
1740 add_com ("forward-search", class_files
, forward_search_command
,
1741 "Search for regular expression (see regex(3)) from last line listed.\n\
1742 The matching line number is also stored as the value of \"$_\".");
1743 add_com_alias ("search", "forward-search", class_files
, 0);
1745 add_com ("reverse-search", class_files
, reverse_search_command
,
1746 "Search backward for regular expression (see regex(3)) from last line listed.\n\
1747 The matching line number is also stored as the value of \"$_\".");
1751 add_com_alias ("/", "forward-search", class_files
, 0);
1752 add_com_alias ("?", "reverse-search", class_files
, 0);
1755 add_com ("list", class_files
, list_command
,
1756 concat ("List specified function or line.\n\
1757 With no argument, lists ten more lines after or around previous listing.\n\
1758 \"list -\" lists the ten lines before a previous ten-line listing.\n\
1759 One argument specifies a line, and ten lines are listed around that line.\n\
1760 Two arguments with comma between specify starting and ending lines to list.\n\
1762 Lines can be specified in these ways:\n\
1763 LINENUM, to list around that line in current file,\n\
1764 FILE:LINENUM, to list around that line in that file,\n\
1765 FUNCTION, to list around beginning of that function,\n\
1766 FILE:FUNCTION, to distinguish among like-named static functions.\n\
1767 *ADDRESS, to list around the line containing that address.\n\
1768 With two args if one is empty it stands for ten lines away from the other arg.", NULL
));
1771 add_com_alias ("l", "list", class_files
, 1);
1773 add_com_alias ("v", "list", class_files
, 1);
1776 add_com_alias ("file", "list", class_files
, 1);
1779 (add_set_cmd ("listsize", class_support
, var_uinteger
,
1780 (char *) &lines_to_list
,
1781 "Set number of source lines gdb will list by default.",