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"
30 #include <sys/types.h>
31 #include "gdb_string.h"
38 #include "gnu-regex.h"
44 #ifdef CRLF_SOURCE_FILES
46 /* Define CRLF_SOURCE_FILES in an xm-*.h file if source files on the
47 host use \r\n rather than just \n. Defining CRLF_SOURCE_FILES is
48 much faster than defining LSEEK_NOT_LINEAR. */
54 #define OPEN_MODE (O_RDONLY | O_BINARY)
55 #define FDOPEN_MODE FOPEN_RB
57 #else /* ! defined (CRLF_SOURCE_FILES) */
59 #define OPEN_MODE O_RDONLY
60 #define FDOPEN_MODE FOPEN_RT
62 #endif /* ! defined (CRLF_SOURCE_FILES) */
64 /* Forward declarations */
66 int open_source_file
PARAMS ((struct symtab
*));
68 void find_source_lines
PARAMS ((struct symtab
*, int));
70 /* Prototypes for exported functions. */
72 void _initialize_source
PARAMS ((void));
74 /* Prototypes for local functions. */
76 static int get_filename_and_charpos
PARAMS ((struct symtab
*, char **));
78 static void reverse_search_command
PARAMS ((char *, int));
80 static void forward_search_command
PARAMS ((char *, int));
82 static void line_info
PARAMS ((char *, int));
84 static void list_command
PARAMS ((char *, int));
86 static void ambiguous_line_spec
PARAMS ((struct symtabs_and_lines
*));
88 static void source_info
PARAMS ((char *, int));
90 static void show_directories
PARAMS ((char *, int));
92 /* Path of directories to search for source files.
93 Same format as the PATH environment variable's value. */
97 /* Symtab of default file for listing lines of. */
99 struct symtab
*current_source_symtab
;
101 /* Default next line to list. */
103 int current_source_line
;
105 /* Default number of lines to print with commands like "list".
106 This is based on guessing how many long (i.e. more than chars_per_line
107 characters) lines there will be. To be completely correct, "list"
108 and friends should be rewritten to count characters and see where
109 things are wrapping, but that would be a fair amount of work. */
111 int lines_to_list
= 10;
113 /* Line number of last line printed. Default for various commands.
114 current_source_line is usually, but not always, the same as this. */
116 static int last_line_listed
;
118 /* First line number listed by last listing command. */
120 static int first_line_listed
;
122 /* Saves the name of the last source file visited and a possible error code.
123 Used to prevent repeating annoying "No such file or directories" msgs */
125 static struct symtab
*last_source_visited
= NULL
;
126 static int last_source_error
= 0;
129 /* Set the source file default for the "list" command to be S.
131 If S is NULL, and we don't have a default, find one. This
132 should only be called when the user actually tries to use the
133 default, since we produce an error if we can't find a reasonable
134 default. Also, since this can cause symbols to be read, doing it
135 before we need to would make things slower than necessary. */
138 select_source_symtab (s
)
139 register struct symtab
*s
;
141 struct symtabs_and_lines sals
;
142 struct symtab_and_line sal
;
143 struct partial_symtab
*ps
;
144 struct partial_symtab
*cs_pst
= 0;
149 current_source_symtab
= s
;
150 current_source_line
= 1;
154 if (current_source_symtab
)
157 /* Make the default place to list be the function `main'
159 if (lookup_symbol ("main", 0, VAR_NAMESPACE
, 0, NULL
))
161 sals
= decode_line_spec ("main", 1);
164 current_source_symtab
= sal
.symtab
;
165 current_source_line
= max (sal
.line
- (lines_to_list
- 1), 1);
166 if (current_source_symtab
)
170 /* All right; find the last file in the symtab list (ignoring .h's). */
172 current_source_line
= 1;
174 for (ofp
= object_files
; ofp
!= NULL
; ofp
= ofp
->next
)
176 for (s
= ofp
->symtabs
; s
; s
= s
->next
)
178 char *name
= s
->filename
;
179 int len
= strlen (name
);
180 if (!(len
> 2 && (STREQ (&name
[len
- 2], ".h"))))
182 current_source_symtab
= s
;
186 if (current_source_symtab
)
189 /* Howabout the partial symbol tables? */
191 for (ofp
= object_files
; ofp
!= NULL
; ofp
= ofp
->next
)
193 for (ps
= ofp
->psymtabs
; ps
!= NULL
; ps
= ps
->next
)
195 char *name
= ps
->filename
;
196 int len
= strlen (name
);
197 if (!(len
> 2 && (STREQ (&name
[len
- 2], ".h"))))
207 internal_error ("select_source_symtab: readin pst found and no symtabs.");
211 current_source_symtab
= PSYMTAB_TO_SYMTAB (cs_pst
);
214 if (current_source_symtab
)
217 error ("Can't find a default source file");
221 show_directories (ignore
, from_tty
)
225 puts_filtered ("Source directories searched: ");
226 puts_filtered (source_path
);
227 puts_filtered ("\n");
230 /* Forget what we learned about line positions in source files, and
231 which directories contain them; must check again now since files
232 may be found in a different directory now. */
235 forget_cached_source_info ()
237 register struct symtab
*s
;
238 register struct objfile
*objfile
;
240 for (objfile
= object_files
; objfile
!= NULL
; objfile
= objfile
->next
)
242 for (s
= objfile
->symtabs
; s
!= NULL
; s
= s
->next
)
244 if (s
->line_charpos
!= NULL
)
246 mfree (objfile
->md
, s
->line_charpos
);
247 s
->line_charpos
= NULL
;
249 if (s
->fullname
!= NULL
)
251 mfree (objfile
->md
, s
->fullname
);
263 sprintf (buf
, "$cdir%c$cwd", DIRNAME_SEPARATOR
);
264 source_path
= strsave (buf
);
265 forget_cached_source_info ();
268 /* Add zero or more directories to the front of the source path. */
271 directory_command (dirname
, from_tty
)
276 /* FIXME, this goes to "delete dir"... */
279 if (from_tty
&& query ("Reinitialize source path to empty? "))
287 mod_path (dirname
, &source_path
);
288 last_source_visited
= NULL
;
291 show_directories ((char *) 0, from_tty
);
292 forget_cached_source_info ();
295 /* Add zero or more directories to the front of an arbitrary path. */
298 mod_path (dirname
, which_path
)
302 char *old
= *which_path
;
308 dirname
= strsave (dirname
);
309 make_cleanup (free
, dirname
);
313 char *name
= dirname
;
318 char *separator
= strchr (name
, DIRNAME_SEPARATOR
);
319 char *space
= strchr (name
, ' ');
320 char *tab
= strchr (name
, '\t');
322 if (separator
== 0 && space
== 0 && tab
== 0)
323 p
= dirname
= name
+ strlen (name
);
327 if (separator
!= 0 && (p
== 0 || separator
< p
))
329 if (space
!= 0 && (p
== 0 || space
< p
))
331 if (tab
!= 0 && (p
== 0 || tab
< p
))
334 while (*dirname
== DIRNAME_SEPARATOR
341 if (!(SLASH_P (*name
) && p
<= name
+ 1) /* "/" */
342 #if defined(_WIN32) || defined(__MSDOS__)
343 /* On MS-DOS and MS-Windows, h:\ is different from h: */
344 && !(!SLASH_P (*name
) && ROOTED_P (name
) && p
<= name
+ 3) /* d:/ */
347 /* Sigh. "foo/" => "foo" */
351 while (p
> name
&& p
[-1] == '.')
355 /* "." => getwd (). */
356 name
= current_directory
;
359 else if (p
> name
+ 1 && SLASH_P (p
[-2]))
369 /* "...foo/." => "...foo". */
380 name
= tilde_expand (name
);
381 #if defined(_WIN32) || defined(__MSDOS__)
382 else if (ROOTED_P (name
) && p
== name
+ 2) /* "d:" => "d:." */
383 name
= concat (name
, ".", NULL
);
385 else if (!ROOTED_P (name
) && name
[0] != '$')
386 name
= concat (current_directory
, SLASH_STRING
, name
, NULL
);
388 name
= savestring (name
, p
- name
);
389 make_cleanup (free
, name
);
391 /* Unless it's a variable, check existence. */
394 /* These are warnings, not errors, since we don't want a
395 non-existent directory in a .gdbinit file to stop processing
396 of the .gdbinit file.
398 Whether they get added to the path is more debatable. Current
399 answer is yes, in case the user wants to go make the directory
400 or whatever. If the directory continues to not exist/not be
401 a directory/etc, then having them in the path should be
403 if (stat (name
, &st
) < 0)
405 int save_errno
= errno
;
406 fprintf_unfiltered (gdb_stderr
, "Warning: ");
407 print_sys_errmsg (name
, save_errno
);
409 else if ((st
.st_mode
& S_IFMT
) != S_IFDIR
)
410 warning ("%s is not a directory.", name
);
415 register unsigned int len
= strlen (name
);
420 /* FIXME: strncmp loses in interesting ways on MS-DOS and
421 MS-Windows because of case-insensitivity and two different
422 but functionally identical slash characters. We need a
423 special filesystem-dependent file-name comparison function.
425 Actually, even on Unix I would use realpath() or its work-
426 alike before comparing. Then all the code above which
427 removes excess slashes and dots could simply go away. */
428 if (!strncmp (p
, name
, len
)
429 && (p
[len
] == '\0' || p
[len
] == DIRNAME_SEPARATOR
))
431 /* Found it in the search path, remove old copy */
433 p
--; /* Back over leading separator */
434 if (prefix
> p
- *which_path
)
435 goto skip_dup
; /* Same dir twice in one cmd */
436 strcpy (p
, &p
[len
+ 1]); /* Copy from next \0 or : */
438 p
= strchr (p
, DIRNAME_SEPARATOR
);
448 tinybuf
[0] = DIRNAME_SEPARATOR
;
451 /* 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. */
458 temp
= concat (old
, tinybuf
, name
, NULL
);
460 *which_path
= concat (temp
, "", &old
[prefix
], NULL
);
461 prefix
= strlen (temp
);
466 *which_path
= concat (name
, (old
[0] ? tinybuf
: old
), old
, NULL
);
467 prefix
= strlen (name
);
475 while (*dirname
!= '\0');
480 source_info (ignore
, from_tty
)
484 register struct symtab
*s
= current_source_symtab
;
488 printf_filtered ("No current source file.\n");
491 printf_filtered ("Current source file is %s\n", s
->filename
);
493 printf_filtered ("Compilation directory is %s\n", s
->dirname
);
495 printf_filtered ("Located in %s\n", s
->fullname
);
497 printf_filtered ("Contains %d line%s.\n", s
->nlines
,
498 s
->nlines
== 1 ? "" : "s");
500 printf_filtered ("Source language is %s.\n", language_str (s
->language
));
501 printf_filtered ("Compiled with %s debugging format.\n", s
->debugformat
);
506 /* Open a file named STRING, searching path PATH (dir names sep by some char)
507 using mode MODE and protection bits PROT in the calls to open.
509 If TRY_CWD_FIRST, try to open ./STRING before searching PATH.
510 (ie pretend the first element of PATH is "."). This also indicates
511 that a slash in STRING disables searching of the path (this is
512 so that "exec-file ./foo" or "symbol-file ./foo" insures that you
513 get that particular version of foo or an error message).
515 If FILENAMED_OPENED is non-null, set it to a newly allocated string naming
516 the actual file opened (this string will always start with a "/". We
517 have to take special pains to avoid doubling the "/" between the directory
518 and the file, sigh! Emacs gets confuzzed by this when we print the
521 If a file is found, return the descriptor.
522 Otherwise, return -1, with errno set for the last name we tried to open. */
524 /* >>>> This should only allow files of certain types,
525 >>>> eg executable, non-directory */
527 openp (path
, try_cwd_first
, string
, mode
, prot
, filename_opened
)
533 char **filename_opened
;
536 register char *filename
;
537 register char *p
, *p1
;
548 if (try_cwd_first
|| ROOTED_P (string
))
552 fd
= open (filename
, mode
, prot
);
555 for (i
= 0; string
[i
]; i
++)
556 if (SLASH_P (string
[i
]))
561 while (string
[0] == '.' && SLASH_P (string
[1]))
564 alloclen
= strlen (path
) + strlen (string
) + 2;
565 filename
= (char *) alloca (alloclen
);
567 for (p
= path
; p
; p
= p1
? p1
+ 1 : 0)
569 p1
= (char *) strchr (p
, DIRNAME_SEPARATOR
);
575 if (len
== 4 && p
[0] == '$' && p
[1] == 'c'
576 && p
[2] == 'w' && p
[3] == 'd')
578 /* Name is $cwd -- insert current directory name instead. */
581 /* First, realloc the filename buffer if too short. */
582 len
= strlen (current_directory
);
583 newlen
= len
+ strlen (string
) + 2;
584 if (newlen
> alloclen
)
587 filename
= (char *) alloca (alloclen
);
589 strcpy (filename
, current_directory
);
593 /* Normal file name in path -- just use it. */
594 strncpy (filename
, p
, len
);
598 /* Remove trailing slashes */
599 while (len
> 0 && SLASH_P (filename
[len
- 1]))
602 strcat (filename
+ len
, SLASH_STRING
);
603 strcat (filename
, string
);
605 fd
= open (filename
, mode
);
614 *filename_opened
= (char *) 0;
615 else if (ROOTED_P (filename
))
616 *filename_opened
= savestring (filename
, strlen (filename
));
619 /* Beware the // my son, the Emacs barfs, the botch that catch... */
621 *filename_opened
= concat (current_directory
,
622 SLASH_P (current_directory
[strlen (current_directory
) - 1])
628 /* This is a debugging hack that can go away when all combinations
629 of Mac and Unix names are handled reasonably. */
631 extern int debug_openp
;
635 printf ("openp on %s, path %s mode %d prot %d\n returned %d",
636 string
, path
, mode
, prot
, fd
);
637 if (*filename_opened
)
638 printf (" (filename is %s)", *filename_opened
);
648 /* This is essentially a convenience, for clients that want the behaviour
649 of openp, using source_path, but that really don't want the file to be
650 opened but want instead just to know what the full pathname is (as
651 qualified against source_path).
653 The current working directory is searched first.
655 If the file was found, this function returns 1, and FULL_PATHNAME is
656 set to the fully-qualified pathname.
658 Else, this functions returns 0, and FULL_PATHNAME is set to NULL.
661 source_full_path_of (filename
, full_pathname
)
663 char **full_pathname
;
667 fd
= openp (source_path
, 1, filename
, O_RDONLY
, 0, full_pathname
);
670 *full_pathname
= NULL
;
679 /* Open a source file given a symtab S. Returns a file descriptor or
680 negative number for error. */
686 char *path
= source_path
;
691 /* Quick way out if we already know its full name */
694 result
= open (s
->fullname
, OPEN_MODE
);
697 /* Didn't work -- free old one, try again. */
698 mfree (s
->objfile
->md
, s
->fullname
);
702 if (s
->dirname
!= NULL
)
704 /* Replace a path entry of $cdir with the compilation directory name */
706 /* We cast strstr's result in case an ANSIhole has made it const,
707 which produces a "required warning" when assigned to a nonconst. */
708 p
= (char *) strstr (source_path
, "$cdir");
709 if (p
&& (p
== path
|| p
[-1] == DIRNAME_SEPARATOR
)
710 && (p
[cdir_len
] == DIRNAME_SEPARATOR
|| p
[cdir_len
] == '\0'))
715 alloca (strlen (source_path
) + 1 + strlen (s
->dirname
) + 1);
716 len
= p
- source_path
;
717 strncpy (path
, source_path
, len
); /* Before $cdir */
718 strcpy (path
+ len
, s
->dirname
); /* new stuff */
719 strcat (path
+ len
, source_path
+ len
+ cdir_len
); /* After $cdir */
723 result
= openp (path
, 0, s
->filename
, OPEN_MODE
, 0, &s
->fullname
);
726 /* Didn't work. Try using just the basename. */
727 p
= basename (s
->filename
);
728 if (p
!= s
->filename
)
729 result
= openp (path
, 0, p
, OPEN_MODE
, 0, &s
->fullname
);
734 /* Didn't work. Try using just the MPW basename. */
735 p
= (char *) mpw_basename (s
->filename
);
736 if (p
!= s
->filename
)
737 result
= openp (path
, 0, p
, OPEN_MODE
, 0, &s
->fullname
);
741 /* Didn't work. Try using the mixed Unix/MPW basename. */
742 p
= (char *) mpw_mixed_basename (s
->filename
);
743 if (p
!= s
->filename
)
744 result
= openp (path
, 0, p
, OPEN_MODE
, 0, &s
->fullname
);
750 fullname
= s
->fullname
;
751 s
->fullname
= mstrsave (s
->objfile
->md
, s
->fullname
);
757 /* Return the path to the source file associated with symtab. Returns NULL
761 symtab_to_filename (s
)
769 /* If we've seen the file before, just return fullname. */
774 /* Try opening the file to setup fullname */
776 fd
= open_source_file (s
);
778 return s
->filename
; /* File not found. Just use short name */
780 /* Found the file. Cleanup and return the full name */
787 /* Create and initialize the table S->line_charpos that records
788 the positions of the lines in the source file, which is assumed
789 to be open on descriptor DESC.
790 All set S->nlines to the number of such lines. */
793 find_source_lines (s
, desc
)
798 register char *data
, *p
, *end
;
800 int lines_allocated
= 1000;
805 line_charpos
= (int *) xmmalloc (s
->objfile
->md
,
806 lines_allocated
* sizeof (int));
807 if (fstat (desc
, &st
) < 0)
808 perror_with_name (s
->filename
);
810 if (s
&& s
->objfile
&& s
->objfile
->obfd
)
811 mtime
= bfd_get_mtime (s
->objfile
->obfd
);
813 mtime
= bfd_get_mtime (exec_bfd
);
815 if (mtime
&& mtime
< st
.st_mtime
)
818 printf_filtered ("\n");
819 warning ("Source file is more recent than executable.\n");
822 #ifdef LSEEK_NOT_LINEAR
826 /* Have to read it byte by byte to find out where the chars live */
828 line_charpos
[0] = lseek (desc
, 0, SEEK_CUR
);
830 while (myread (desc
, &c
, 1) > 0)
834 if (nlines
== lines_allocated
)
836 lines_allocated
*= 2;
838 (int *) xmrealloc (s
->objfile
->md
, (char *) line_charpos
,
839 sizeof (int) * lines_allocated
);
841 line_charpos
[nlines
++] = lseek (desc
, 0, SEEK_CUR
);
845 #else /* lseek linear. */
847 struct cleanup
*old_cleanups
;
849 /* st_size might be a large type, but we only support source files whose
850 size fits in an int. */
851 size
= (int) st
.st_size
;
853 /* Use malloc, not alloca, because this may be pretty large, and we may
854 run into various kinds of limits on stack size. */
855 data
= (char *) xmalloc (size
);
856 old_cleanups
= make_cleanup (free
, data
);
858 /* Reassign `size' to result of read for systems where \r\n -> \n. */
859 size
= myread (desc
, data
, size
);
861 perror_with_name (s
->filename
);
869 /* A newline at the end does not start a new line. */
872 if (nlines
== lines_allocated
)
874 lines_allocated
*= 2;
876 (int *) xmrealloc (s
->objfile
->md
, (char *) line_charpos
,
877 sizeof (int) * lines_allocated
);
879 line_charpos
[nlines
++] = p
- data
;
882 do_cleanups (old_cleanups
);
884 #endif /* lseek linear. */
887 (int *) xmrealloc (s
->objfile
->md
, (char *) line_charpos
,
888 nlines
* sizeof (int));
892 /* Return the character position of a line LINE in symtab S.
893 Return 0 if anything is invalid. */
895 #if 0 /* Currently unused */
898 source_line_charpos (s
, line
)
904 if (!s
->line_charpos
|| line
<= 0)
906 if (line
> s
->nlines
)
908 return s
->line_charpos
[line
- 1];
911 /* Return the line number of character position POS in symtab S. */
914 source_charpos_line (s
, chr
)
915 register struct symtab
*s
;
918 register int line
= 0;
921 if (s
== 0 || s
->line_charpos
== 0)
923 lnp
= s
->line_charpos
;
924 /* Files are usually short, so sequential search is Ok */
925 while (line
< s
->nlines
&& *lnp
<= chr
)
930 if (line
>= s
->nlines
)
938 /* Get full pathname and line number positions for a symtab.
939 Return nonzero if line numbers may have changed.
940 Set *FULLNAME to actual name of the file as found by `openp',
941 or to 0 if the file is not found. */
944 get_filename_and_charpos (s
, fullname
)
948 register int desc
, linenums_changed
= 0;
950 desc
= open_source_file (s
);
958 *fullname
= s
->fullname
;
959 if (s
->line_charpos
== 0)
960 linenums_changed
= 1;
961 if (linenums_changed
)
962 find_source_lines (s
, desc
);
964 return linenums_changed
;
967 /* Print text describing the full name of the source file S
968 and the line number LINE and its corresponding character position.
969 The text starts with two Ctrl-z so that the Emacs-GDB interface
972 MID_STATEMENT is nonzero if the PC is not at the beginning of that line.
974 Return 1 if successful, 0 if could not find the file. */
977 identify_source_line (s
, line
, mid_statement
, pc
)
983 if (s
->line_charpos
== 0)
984 get_filename_and_charpos (s
, (char **) NULL
);
985 if (s
->fullname
== 0)
987 if (line
> s
->nlines
)
988 /* Don't index off the end of the line_charpos array. */
990 annotate_source (s
->fullname
, line
, s
->line_charpos
[line
- 1],
993 current_source_line
= line
;
994 first_line_listed
= line
;
995 last_line_listed
= line
;
996 current_source_symtab
= s
;
1001 /* Print source lines from the file of symtab S,
1002 starting with line number LINE and stopping before line number STOPLINE. */
1004 static void print_source_lines_base
PARAMS ((struct symtab
* s
, int line
, int stopline
, int noerror
));
1006 print_source_lines_base (s
, line
, stopline
, noerror
)
1014 register FILE *stream
;
1015 int nlines
= stopline
- line
;
1017 /* Regardless of whether we can open the file, set current_source_symtab. */
1018 current_source_symtab
= s
;
1019 current_source_line
= line
;
1020 first_line_listed
= line
;
1022 /* Only prints "No such file or directory" once */
1023 if ((s
!= last_source_visited
) || (!last_source_error
))
1025 last_source_visited
= s
;
1026 desc
= open_source_file (s
);
1030 desc
= last_source_error
;
1036 last_source_error
= desc
;
1040 char *name
= alloca (strlen (s
->filename
) + 100);
1041 sprintf (name
, "%d\t%s", line
, s
->filename
);
1042 print_sys_errmsg (name
, errno
);
1045 printf_filtered ("%d\tin %s\n", line
, s
->filename
);
1050 last_source_error
= 0;
1052 if (s
->line_charpos
== 0)
1053 find_source_lines (s
, desc
);
1055 if (line
< 1 || line
> s
->nlines
)
1058 error ("Line number %d out of range; %s has %d lines.",
1059 line
, s
->filename
, s
->nlines
);
1062 if (lseek (desc
, s
->line_charpos
[line
- 1], 0) < 0)
1065 perror_with_name (s
->filename
);
1068 stream
= fdopen (desc
, FDOPEN_MODE
);
1071 while (nlines
-- > 0)
1076 last_line_listed
= current_source_line
;
1077 printf_filtered ("%d\t", current_source_line
++);
1080 if (c
< 040 && c
!= '\t' && c
!= '\n' && c
!= '\r')
1081 printf_filtered ("^%c", c
+ 0100);
1083 printf_filtered ("^?");
1084 #ifdef CRLF_SOURCE_FILES
1087 /* Just skip \r characters. */
1091 printf_filtered ("%c", c
);
1093 while (c
!= '\n' && (c
= fgetc (stream
)) >= 0);
1099 /* Show source lines from the file of symtab S, starting with line
1100 number LINE and stopping before line number STOPLINE. If this is the
1101 not the command line version, then the source is shown in the source
1102 window otherwise it is simply printed */
1105 print_source_lines (s
, line
, stopline
, noerror
)
1107 int line
, stopline
, noerror
;
1111 m_winPtrIsNull (srcWin
) || !srcWin
->generic
.isVisible
)
1112 print_source_lines_base (s
, line
, stopline
, noerror
);
1115 TuiGenWinInfoPtr locator
= locatorWinInfoPtr ();
1116 extern void tui_vAddWinToLayout
PARAMS ((va_list));
1117 extern void tui_vUpdateSourceWindowsWithLine
PARAMS ((va_list));
1119 /* Regardless of whether we can open the file,
1120 set current_source_symtab. */
1121 current_source_symtab
= s
;
1122 current_source_line
= line
;
1123 first_line_listed
= line
;
1125 /* make sure that the source window is displayed */
1126 tuiDo ((TuiOpaqueFuncPtr
) tui_vAddWinToLayout
, SRC_WIN
);
1128 tuiDo ((TuiOpaqueFuncPtr
) tui_vUpdateSourceWindowsWithLine
, s
, line
);
1129 tuiDo ((TuiOpaqueFuncPtr
) tui_vUpdateLocatorFilename
, s
->filename
);
1132 print_source_lines_base (s
, line
, stopline
, noerror
);
1138 /* Print a list of files and line numbers which a user may choose from
1139 in order to list a function which was specified ambiguously (as with
1140 `list classname::overloadedfuncname', for example). The vector in
1141 SALS provides the filenames and line numbers. */
1144 ambiguous_line_spec (sals
)
1145 struct symtabs_and_lines
*sals
;
1149 for (i
= 0; i
< sals
->nelts
; ++i
)
1150 printf_filtered ("file: \"%s\", line number: %d\n",
1151 sals
->sals
[i
].symtab
->filename
, sals
->sals
[i
].line
);
1155 list_command (arg
, from_tty
)
1159 struct symtabs_and_lines sals
, sals_end
;
1160 struct symtab_and_line sal
, sal_end
;
1166 int linenum_beg
= 0;
1169 if (!have_full_symbols () && !have_partial_symbols ())
1170 error ("No symbol table is loaded. Use the \"file\" command.");
1172 /* Pull in a current source symtab if necessary */
1173 if (current_source_symtab
== 0 &&
1174 (arg
== 0 || arg
[0] == '+' || arg
[0] == '-'))
1175 select_source_symtab (0);
1177 /* "l" or "l +" lists next ten lines. */
1179 if (arg
== 0 || STREQ (arg
, "+"))
1181 if (current_source_symtab
== 0)
1182 error ("No default source file yet. Do \"help list\".");
1183 print_source_lines (current_source_symtab
, current_source_line
,
1184 current_source_line
+ lines_to_list
, 0);
1188 /* "l -" lists previous ten lines, the ones before the ten just listed. */
1189 if (STREQ (arg
, "-"))
1191 if (current_source_symtab
== 0)
1192 error ("No default source file yet. Do \"help list\".");
1193 print_source_lines (current_source_symtab
,
1194 max (first_line_listed
- lines_to_list
, 1),
1195 first_line_listed
, 0);
1199 /* Now if there is only one argument, decode it in SAL
1201 If there are two arguments, decode them in SAL and SAL_END
1202 and clear NO_END; however, if one of the arguments is blank,
1203 set DUMMY_BEG or DUMMY_END to record that fact. */
1210 sals
= decode_line_1 (&arg1
, 0, 0, 0, 0);
1216 ambiguous_line_spec (&sals
);
1225 /* Record whether the BEG arg is all digits. */
1227 for (p
= arg
; p
!= arg1
&& *p
>= '0' && *p
<= '9'; p
++);
1228 linenum_beg
= (p
== arg1
);
1230 while (*arg1
== ' ' || *arg1
== '\t')
1236 while (*arg1
== ' ' || *arg1
== '\t')
1243 sals_end
= decode_line_1 (&arg1
, 0, 0, 0, 0);
1245 sals_end
= decode_line_1 (&arg1
, 0, sal
.symtab
, sal
.line
, 0);
1246 if (sals_end
.nelts
== 0)
1248 if (sals_end
.nelts
> 1)
1250 ambiguous_line_spec (&sals_end
);
1251 free (sals_end
.sals
);
1254 sal_end
= sals_end
.sals
[0];
1255 free (sals_end
.sals
);
1260 error ("Junk at end of line specification.");
1262 if (!no_end
&& !dummy_beg
&& !dummy_end
1263 && sal
.symtab
!= sal_end
.symtab
)
1264 error ("Specified start and end are in different files.");
1265 if (dummy_beg
&& dummy_end
)
1266 error ("Two empty args do not say what lines to list.");
1268 /* if line was specified by address,
1269 first print exactly which line, and which file.
1270 In this case, sal.symtab == 0 means address is outside
1271 of all known source files, not that user failed to give a filename. */
1274 if (sal
.symtab
== 0)
1275 /* FIXME-32x64--assumes sal.pc fits in long. */
1276 error ("No source file for address %s.",
1277 local_hex_string ((unsigned long) sal
.pc
));
1278 sym
= find_pc_function (sal
.pc
);
1281 print_address_numeric (sal
.pc
, 1, gdb_stdout
);
1282 printf_filtered (" is in ");
1283 fputs_filtered (SYMBOL_SOURCE_NAME (sym
), gdb_stdout
);
1284 printf_filtered (" (%s:%d).\n", sal
.symtab
->filename
, sal
.line
);
1288 print_address_numeric (sal
.pc
, 1, gdb_stdout
);
1289 printf_filtered (" is at %s:%d.\n",
1290 sal
.symtab
->filename
, sal
.line
);
1294 /* If line was not specified by just a line number,
1295 and it does not imply a symtab, it must be an undebuggable symbol
1296 which means no source code. */
1298 if (!linenum_beg
&& sal
.symtab
== 0)
1299 error ("No line number known for %s.", arg
);
1301 /* If this command is repeated with RET,
1302 turn it into the no-arg variant. */
1307 if (dummy_beg
&& sal_end
.symtab
== 0)
1308 error ("No default source file yet. Do \"help list\".");
1310 print_source_lines (sal_end
.symtab
,
1311 max (sal_end
.line
- (lines_to_list
- 1), 1),
1312 sal_end
.line
+ 1, 0);
1313 else if (sal
.symtab
== 0)
1314 error ("No default source file yet. Do \"help list\".");
1317 if (lines_to_list
% 2 == 0)
1318 print_source_lines (sal
.symtab
,
1319 max (sal
.line
- (lines_to_list
/ 2), 1),
1320 sal
.line
+ (lines_to_list
/ 2), 0);
1322 /* If lines_to_list is odd, then we round down in
1323 * one of the lines_to_list/2 computations, round up in
1324 * the other, so the total window size around the specified
1325 * line comes out right.
1327 print_source_lines (sal
.symtab
,
1328 max (sal
.line
- (lines_to_list
/ 2), 1),
1329 sal
.line
+ ((1 + lines_to_list
) / 2), 0);
1332 print_source_lines (sal
.symtab
, sal
.line
,
1334 ? sal
.line
+ lines_to_list
1335 : sal_end
.line
+ 1),
1339 /* Print info on range of pc's in a specified line. */
1342 line_info (arg
, from_tty
)
1346 struct symtabs_and_lines sals
;
1347 struct symtab_and_line sal
;
1348 CORE_ADDR start_pc
, end_pc
;
1351 INIT_SAL (&sal
); /* initialize to zeroes */
1355 sal
.symtab
= current_source_symtab
;
1356 sal
.line
= last_line_listed
;
1358 sals
.sals
= (struct symtab_and_line
*)
1359 xmalloc (sizeof (struct symtab_and_line
));
1364 sals
= decode_line_spec_1 (arg
, 0);
1369 /* C++ More than one line may have been specified, as when the user
1370 specifies an overloaded function name. Print info on them all. */
1371 for (i
= 0; i
< sals
.nelts
; i
++)
1375 if (sal
.symtab
== 0)
1377 printf_filtered ("No line number information available");
1380 /* This is useful for "info line *0x7f34". If we can't tell the
1381 user about a source line, at least let them have the symbolic
1383 printf_filtered (" for address ");
1385 print_address (sal
.pc
, gdb_stdout
);
1388 printf_filtered (".");
1389 printf_filtered ("\n");
1391 else if (sal
.line
> 0
1392 && find_line_pc_range (sal
, &start_pc
, &end_pc
))
1394 if (start_pc
== end_pc
)
1396 printf_filtered ("Line %d of \"%s\"",
1397 sal
.line
, sal
.symtab
->filename
);
1399 printf_filtered (" is at address ");
1400 print_address (start_pc
, gdb_stdout
);
1402 printf_filtered (" but contains no code.\n");
1406 printf_filtered ("Line %d of \"%s\"",
1407 sal
.line
, sal
.symtab
->filename
);
1409 printf_filtered (" starts at address ");
1410 print_address (start_pc
, gdb_stdout
);
1412 printf_filtered (" and ends at ");
1413 print_address (end_pc
, gdb_stdout
);
1414 printf_filtered (".\n");
1417 /* x/i should display this line's code. */
1418 set_next_address (start_pc
);
1420 /* Repeating "info line" should do the following line. */
1421 last_line_listed
= sal
.line
+ 1;
1423 /* If this is the only line, show the source code. If it could
1424 not find the file, don't do anything special. */
1425 if (annotation_level
&& sals
.nelts
== 1)
1426 identify_source_line (sal
.symtab
, sal
.line
, 0, start_pc
);
1429 /* Is there any case in which we get here, and have an address
1430 which the user would want to see? If we have debugging symbols
1431 and no line numbers? */
1432 printf_filtered ("Line number %d is out of range for \"%s\".\n",
1433 sal
.line
, sal
.symtab
->filename
);
1438 /* Commands to search the source file for a regexp. */
1442 forward_search_command (regex
, from_tty
)
1448 register FILE *stream
;
1454 ** If this is the TUI, search from the first line displayed in
1455 ** the source window, otherwise, search from last_line_listed+1
1456 ** in current_source_symtab
1459 line
= last_line_listed
;
1462 if (srcWin
->generic
.isVisible
&& srcWin
->generic
.contentSize
> 0)
1463 line
= ((TuiWinContent
)
1464 srcWin
->generic
.content
)[0]->whichElement
.source
.lineOrAddr
.lineNo
;
1467 printf_filtered ("No source displayed.\nExpression not found.\n");
1473 line
= last_line_listed
+ 1;
1476 msg
= (char *) re_comp (regex
);
1480 if (current_source_symtab
== 0)
1481 select_source_symtab (0);
1483 desc
= open_source_file (current_source_symtab
);
1485 perror_with_name (current_source_symtab
->filename
);
1487 if (current_source_symtab
->line_charpos
== 0)
1488 find_source_lines (current_source_symtab
, desc
);
1490 if (line
< 1 || line
> current_source_symtab
->nlines
)
1493 error ("Expression not found");
1496 if (lseek (desc
, current_source_symtab
->line_charpos
[line
- 1], 0) < 0)
1499 perror_with_name (current_source_symtab
->filename
);
1502 stream
= fdopen (desc
, FDOPEN_MODE
);
1506 static char *buf
= NULL
;
1508 int cursize
, newsize
;
1511 buf
= xmalloc (cursize
);
1520 if (p
- buf
== cursize
)
1522 newsize
= cursize
+ cursize
/ 2;
1523 buf
= xrealloc (buf
, newsize
);
1528 while (c
!= '\n' && (c
= getc (stream
)) >= 0);
1530 #ifdef CRLF_SOURCE_FILES
1531 /* Remove the \r, if any, at the end of the line, otherwise
1532 regular expressions that end with $ or \n won't work. */
1533 if (p
- buf
> 1 && p
[-2] == '\r')
1540 /* we now have a source line in buf, null terminate and match */
1542 if (re_exec (buf
) > 0)
1547 print_source_lines_base (current_source_symtab
, line
, line
+ 1, 0);
1548 print_source_lines (current_source_symtab
, line
, line
+ 1, 0);
1549 set_internalvar (lookup_internalvar ("_"),
1550 value_from_longest (builtin_type_int
,
1552 current_source_line
= max (line
- lines_to_list
/ 2, 1);
1558 printf_filtered ("Expression not found\n");
1564 reverse_search_command (regex
, from_tty
)
1570 register FILE *stream
;
1575 ** If this is the TUI, search from the first line displayed in
1576 ** the source window, otherwise, search from last_line_listed-1
1577 ** in current_source_symtab
1580 line
= last_line_listed
;
1583 if (srcWin
->generic
.isVisible
&& srcWin
->generic
.contentSize
> 0)
1584 line
= ((TuiWinContent
)
1585 srcWin
->generic
.content
)[0]->whichElement
.source
.lineOrAddr
.lineNo
;
1588 printf_filtered ("No source displayed.\nExpression not found.\n");
1594 line
= last_line_listed
- 1;
1597 msg
= (char *) re_comp (regex
);
1601 if (current_source_symtab
== 0)
1602 select_source_symtab (0);
1604 desc
= open_source_file (current_source_symtab
);
1606 perror_with_name (current_source_symtab
->filename
);
1608 if (current_source_symtab
->line_charpos
== 0)
1609 find_source_lines (current_source_symtab
, desc
);
1611 if (line
< 1 || line
> current_source_symtab
->nlines
)
1614 error ("Expression not found");
1617 if (lseek (desc
, current_source_symtab
->line_charpos
[line
- 1], 0) < 0)
1620 perror_with_name (current_source_symtab
->filename
);
1623 stream
= fdopen (desc
, FDOPEN_MODE
);
1627 /* FIXME!!! We walk right off the end of buf if we get a long line!!! */
1628 char buf
[4096]; /* Should be reasonable??? */
1629 register char *p
= buf
;
1638 while (c
!= '\n' && (c
= getc (stream
)) >= 0);
1640 #ifdef CRLF_SOURCE_FILES
1641 /* Remove the \r, if any, at the end of the line, otherwise
1642 regular expressions that end with $ or \n won't work. */
1643 if (p
- buf
> 1 && p
[-2] == '\r')
1650 /* We now have a source line in buf; null terminate and match. */
1652 if (re_exec (buf
) > 0)
1657 print_source_lines_base (current_source_symtab
, line
, line
+ 1, 0);
1658 print_source_lines (current_source_symtab
, line
, line
+ 1, 0);
1659 set_internalvar (lookup_internalvar ("_"),
1660 value_from_longest (builtin_type_int
,
1662 current_source_line
= max (line
- lines_to_list
/ 2, 1);
1666 if (fseek (stream
, current_source_symtab
->line_charpos
[line
- 1], 0) < 0)
1669 perror_with_name (current_source_symtab
->filename
);
1673 printf_filtered ("Expression not found\n");
1679 _initialize_source ()
1681 struct cmd_list_element
*c
;
1682 current_source_symtab
= 0;
1683 init_source_path ();
1685 /* The intention is to use POSIX Basic Regular Expressions.
1686 Always use the GNU regex routine for consistency across all hosts.
1687 Our current GNU regex.c does not have all the POSIX features, so this is
1688 just an approximation. */
1689 re_set_syntax (RE_SYNTAX_GREP
);
1691 c
= add_cmd ("directory", class_files
, directory_command
,
1692 "Add directory DIR to beginning of search path for source files.\n\
1693 Forget cached info on source file locations and line positions.\n\
1694 DIR can also be $cwd for the current working directory, or $cdir for the\n\
1695 directory in which the source file was compiled into object code.\n\
1696 With no argument, reset the search path to $cdir:$cwd, the default.",
1700 add_com_alias ("use", "directory", class_files
, 0);
1702 c
->completer
= filename_completer
;
1704 add_cmd ("directories", no_class
, show_directories
,
1705 "Current search path for finding source files.\n\
1706 $cwd in the path means the current working directory.\n\
1707 $cdir in the path means the compilation directory of the source file.",
1712 add_com_alias ("D", "directory", class_files
, 0);
1713 add_cmd ("ld", 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.",
1720 add_info ("source", source_info
,
1721 "Information about the current source file.");
1723 add_info ("line", line_info
,
1724 concat ("Core addresses of the code for a source line.\n\
1725 Line can be specified as\n\
1726 LINENUM, to list around that line in current file,\n\
1727 FILE:LINENUM, to list around that line in that file,\n\
1728 FUNCTION, to list around beginning of that function,\n\
1729 FILE:FUNCTION, to distinguish among like-named static functions.\n\
1731 Default is to describe the last source line that was listed.\n\n\
1732 This sets the default address for \"x\" to the line's first instruction\n\
1733 so that \"x/i\" suffices to start examining the machine code.\n\
1734 The address is also stored as the value of \"$_\".", NULL
));
1736 add_com ("forward-search", class_files
, forward_search_command
,
1737 "Search for regular expression (see regex(3)) from last line listed.\n\
1738 The matching line number is also stored as the value of \"$_\".");
1739 add_com_alias ("search", "forward-search", class_files
, 0);
1741 add_com ("reverse-search", class_files
, reverse_search_command
,
1742 "Search backward for regular expression (see regex(3)) from last line listed.\n\
1743 The matching line number is also stored as the value of \"$_\".");
1747 add_com_alias ("/", "forward-search", class_files
, 0);
1748 add_com_alias ("?", "reverse-search", class_files
, 0);
1751 add_com ("list", class_files
, list_command
,
1752 concat ("List specified function or line.\n\
1753 With no argument, lists ten more lines after or around previous listing.\n\
1754 \"list -\" lists the ten lines before a previous ten-line listing.\n\
1755 One argument specifies a line, and ten lines are listed around that line.\n\
1756 Two arguments with comma between specify starting and ending lines to list.\n\
1758 Lines can be specified in these ways:\n\
1759 LINENUM, to list around that line in current file,\n\
1760 FILE:LINENUM, to list around that line in that file,\n\
1761 FUNCTION, to list around beginning of that function,\n\
1762 FILE:FUNCTION, to distinguish among like-named static functions.\n\
1763 *ADDRESS, to list around the line containing that address.\n\
1764 With two args if one is empty it stands for ten lines away from the other arg.", NULL
));
1767 add_com_alias ("l", "list", class_files
, 1);
1769 add_com_alias ("v", "list", class_files
, 1);
1772 add_com_alias ("file", "list", class_files
, 1);
1775 (add_set_cmd ("listsize", class_support
, var_uinteger
,
1776 (char *) &lines_to_list
,
1777 "Set number of source lines gdb will list by default.",