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 "filenames.h" /* for DOSish file names */
44 #include "completer.h"
49 #ifdef CRLF_SOURCE_FILES
51 /* Define CRLF_SOURCE_FILES in an xm-*.h file if source files on the
52 host use \r\n rather than just \n. Defining CRLF_SOURCE_FILES is
53 much faster than defining LSEEK_NOT_LINEAR. */
59 #define OPEN_MODE (O_RDONLY | O_BINARY)
60 #define FDOPEN_MODE FOPEN_RB
62 #else /* ! defined (CRLF_SOURCE_FILES) */
64 #define OPEN_MODE O_RDONLY
65 #define FDOPEN_MODE FOPEN_RT
67 #endif /* ! defined (CRLF_SOURCE_FILES) */
69 /* Prototypes for exported functions. */
71 void _initialize_source (void);
73 /* Prototypes for local functions. */
75 static int get_filename_and_charpos (struct symtab
*, char **);
77 static void reverse_search_command (char *, int);
79 static void forward_search_command (char *, int);
81 static void line_info (char *, int);
83 static void list_command (char *, int);
85 static void ambiguous_line_spec (struct symtabs_and_lines
*);
87 static void source_info (char *, int);
89 static void show_directories (char *, int);
91 /* Path of directories to search for source files.
92 Same format as the PATH environment variable's value. */
96 /* Symtab of default file for listing lines of. */
98 struct symtab
*current_source_symtab
;
100 /* Default next line to list. */
102 int current_source_line
;
104 /* Default number of lines to print with commands like "list".
105 This is based on guessing how many long (i.e. more than chars_per_line
106 characters) lines there will be. To be completely correct, "list"
107 and friends should be rewritten to count characters and see where
108 things are wrapping, but that would be a fair amount of work. */
110 int lines_to_list
= 10;
112 /* Line number of last line printed. Default for various commands.
113 current_source_line is usually, but not always, the same as this. */
115 static int last_line_listed
;
117 /* First line number listed by last listing command. */
119 static int first_line_listed
;
121 /* Saves the name of the last source file visited and a possible error code.
122 Used to prevent repeating annoying "No such file or directories" msgs */
124 static struct symtab
*last_source_visited
= NULL
;
125 static int last_source_error
= 0;
128 /* Set the source file default for the "list" command to be S.
130 If S is NULL, and we don't have a default, find one. This
131 should only be called when the user actually tries to use the
132 default, since we produce an error if we can't find a reasonable
133 default. Also, since this can cause symbols to be read, doing it
134 before we need to would make things slower than necessary. */
137 select_source_symtab (register struct symtab
*s
)
139 struct symtabs_and_lines sals
;
140 struct symtab_and_line sal
;
141 struct partial_symtab
*ps
;
142 struct partial_symtab
*cs_pst
= 0;
147 current_source_symtab
= s
;
148 current_source_line
= 1;
152 if (current_source_symtab
)
155 /* Make the default place to list be the function `main'
157 if (lookup_symbol (main_name (), 0, VAR_NAMESPACE
, 0, NULL
))
159 sals
= decode_line_spec (main_name (), 1);
162 current_source_symtab
= sal
.symtab
;
163 current_source_line
= max (sal
.line
- (lines_to_list
- 1), 1);
164 if (current_source_symtab
)
168 /* All right; find the last file in the symtab list (ignoring .h's). */
170 current_source_line
= 1;
172 for (ofp
= object_files
; ofp
!= NULL
; ofp
= ofp
->next
)
174 for (s
= ofp
->symtabs
; s
; s
= s
->next
)
176 char *name
= s
->filename
;
177 int len
= strlen (name
);
178 if (!(len
> 2 && (STREQ (&name
[len
- 2], ".h"))))
180 current_source_symtab
= s
;
184 if (current_source_symtab
)
187 /* Howabout the partial symbol tables? */
189 for (ofp
= object_files
; ofp
!= NULL
; ofp
= ofp
->next
)
191 for (ps
= ofp
->psymtabs
; ps
!= NULL
; ps
= ps
->next
)
193 char *name
= ps
->filename
;
194 int len
= strlen (name
);
195 if (!(len
> 2 && (STREQ (&name
[len
- 2], ".h"))))
205 internal_error (__FILE__
, __LINE__
,
206 "select_source_symtab: "
207 "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 (char *ignore
, int from_tty
)
223 puts_filtered ("Source directories searched: ");
224 puts_filtered (source_path
);
225 puts_filtered ("\n");
228 /* Forget what we learned about line positions in source files, and
229 which directories contain them; must check again now since files
230 may be found in a different directory now. */
233 forget_cached_source_info (void)
235 register struct symtab
*s
;
236 register struct objfile
*objfile
;
238 for (objfile
= object_files
; objfile
!= NULL
; objfile
= objfile
->next
)
240 for (s
= objfile
->symtabs
; s
!= NULL
; s
= s
->next
)
242 if (s
->line_charpos
!= NULL
)
244 mfree (objfile
->md
, s
->line_charpos
);
245 s
->line_charpos
= NULL
;
247 if (s
->fullname
!= NULL
)
249 mfree (objfile
->md
, s
->fullname
);
257 init_source_path (void)
261 sprintf (buf
, "$cdir%c$cwd", DIRNAME_SEPARATOR
);
262 source_path
= xstrdup (buf
);
263 forget_cached_source_info ();
266 /* Add zero or more directories to the front of the source path. */
269 directory_command (char *dirname
, int from_tty
)
272 /* FIXME, this goes to "delete dir"... */
275 if (from_tty
&& query ("Reinitialize source path to empty? "))
283 mod_path (dirname
, &source_path
);
284 last_source_visited
= NULL
;
287 show_directories ((char *) 0, from_tty
);
288 forget_cached_source_info ();
291 /* Add zero or more directories to the front of an arbitrary path. */
294 mod_path (char *dirname
, char **which_path
)
296 char *old
= *which_path
;
302 dirname
= xstrdup (dirname
);
303 make_cleanup (xfree
, dirname
);
307 char *name
= dirname
;
312 char *separator
= strchr (name
, DIRNAME_SEPARATOR
);
313 char *space
= strchr (name
, ' ');
314 char *tab
= strchr (name
, '\t');
316 if (separator
== 0 && space
== 0 && tab
== 0)
317 p
= dirname
= name
+ strlen (name
);
321 if (separator
!= 0 && (p
== 0 || separator
< p
))
323 if (space
!= 0 && (p
== 0 || space
< p
))
325 if (tab
!= 0 && (p
== 0 || tab
< p
))
328 while (*dirname
== DIRNAME_SEPARATOR
335 if (!(IS_DIR_SEPARATOR (*name
) && p
<= name
+ 1) /* "/" */
336 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
337 /* On MS-DOS and MS-Windows, h:\ is different from h: */
338 && !(p
== name
+ 3 && name
[1] == ':') /* "d:/" */
340 && IS_DIR_SEPARATOR (p
[-1]))
341 /* Sigh. "foo/" => "foo" */
345 while (p
> name
&& p
[-1] == '.')
349 /* "." => getwd (). */
350 name
= current_directory
;
353 else if (p
> name
+ 1 && IS_DIR_SEPARATOR (p
[-2]))
363 /* "...foo/." => "...foo". */
374 name
= tilde_expand (name
);
375 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
376 else if (IS_ABSOLUTE_PATH (name
) && p
== name
+ 2) /* "d:" => "d:." */
377 name
= concat (name
, ".", NULL
);
379 else if (!IS_ABSOLUTE_PATH (name
) && name
[0] != '$')
380 name
= concat (current_directory
, SLASH_STRING
, name
, NULL
);
382 name
= savestring (name
, p
- name
);
383 make_cleanup (xfree
, name
);
385 /* Unless it's a variable, check existence. */
388 /* These are warnings, not errors, since we don't want a
389 non-existent directory in a .gdbinit file to stop processing
390 of the .gdbinit file.
392 Whether they get added to the path is more debatable. Current
393 answer is yes, in case the user wants to go make the directory
394 or whatever. If the directory continues to not exist/not be
395 a directory/etc, then having them in the path should be
397 if (stat (name
, &st
) < 0)
399 int save_errno
= errno
;
400 fprintf_unfiltered (gdb_stderr
, "Warning: ");
401 print_sys_errmsg (name
, save_errno
);
403 else if ((st
.st_mode
& S_IFMT
) != S_IFDIR
)
404 warning ("%s is not a directory.", name
);
409 register unsigned int len
= strlen (name
);
414 /* FIXME: strncmp loses in interesting ways on MS-DOS and
415 MS-Windows because of case-insensitivity and two different
416 but functionally identical slash characters. We need a
417 special filesystem-dependent file-name comparison function.
419 Actually, even on Unix I would use realpath() or its work-
420 alike before comparing. Then all the code above which
421 removes excess slashes and dots could simply go away. */
422 if (!strncmp (p
, name
, len
)
423 && (p
[len
] == '\0' || p
[len
] == DIRNAME_SEPARATOR
))
425 /* Found it in the search path, remove old copy */
427 p
--; /* Back over leading separator */
428 if (prefix
> p
- *which_path
)
429 goto skip_dup
; /* Same dir twice in one cmd */
430 strcpy (p
, &p
[len
+ 1]); /* Copy from next \0 or : */
432 p
= strchr (p
, DIRNAME_SEPARATOR
);
442 tinybuf
[0] = DIRNAME_SEPARATOR
;
445 /* 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. */
452 temp
= concat (old
, tinybuf
, name
, NULL
);
454 *which_path
= concat (temp
, "", &old
[prefix
], NULL
);
455 prefix
= strlen (temp
);
460 *which_path
= concat (name
, (old
[0] ? tinybuf
: old
), old
, NULL
);
461 prefix
= strlen (name
);
469 while (*dirname
!= '\0');
474 source_info (char *ignore
, int from_tty
)
476 register struct symtab
*s
= current_source_symtab
;
480 printf_filtered ("No current source file.\n");
483 printf_filtered ("Current source file is %s\n", s
->filename
);
485 printf_filtered ("Compilation directory is %s\n", s
->dirname
);
487 printf_filtered ("Located in %s\n", s
->fullname
);
489 printf_filtered ("Contains %d line%s.\n", s
->nlines
,
490 s
->nlines
== 1 ? "" : "s");
492 printf_filtered ("Source language is %s.\n", language_str (s
->language
));
493 printf_filtered ("Compiled with %s debugging format.\n", s
->debugformat
);
498 /* Open a file named STRING, searching path PATH (dir names sep by some char)
499 using mode MODE and protection bits PROT in the calls to open.
501 If TRY_CWD_FIRST, try to open ./STRING before searching PATH.
502 (ie pretend the first element of PATH is "."). This also indicates
503 that a slash in STRING disables searching of the path (this is
504 so that "exec-file ./foo" or "symbol-file ./foo" insures that you
505 get that particular version of foo or an error message).
507 If FILENAME_OPENED is non-null, set it to a newly allocated string naming
508 the actual file opened (this string will always start with a "/". We
509 have to take special pains to avoid doubling the "/" between the directory
510 and the file, sigh! Emacs gets confuzzed by this when we print the
513 If a file is found, return the descriptor.
514 Otherwise, return -1, with errno set for the last name we tried to open. */
516 /* >>>> This should only allow files of certain types,
517 >>>> eg executable, non-directory */
519 openp (const char *path
, int try_cwd_first
, const char *string
,
521 char **filename_opened
)
524 register char *filename
;
533 #if defined(_WIN32) || defined(__CYGWIN__)
537 if (try_cwd_first
|| IS_ABSOLUTE_PATH (string
))
540 filename
= alloca (strlen (string
) + 1);
541 strcpy (filename
, string
);
542 fd
= open (filename
, mode
, prot
);
545 for (i
= 0; string
[i
]; i
++)
546 if (IS_DIR_SEPARATOR (string
[i
]))
551 while (string
[0] == '.' && IS_DIR_SEPARATOR (string
[1]))
554 alloclen
= strlen (path
) + strlen (string
) + 2;
555 filename
= alloca (alloclen
);
557 for (p
= path
; p
; p
= p1
? p1
+ 1 : 0)
559 p1
= strchr (p
, DIRNAME_SEPARATOR
);
565 if (len
== 4 && p
[0] == '$' && p
[1] == 'c'
566 && p
[2] == 'w' && p
[3] == 'd')
568 /* Name is $cwd -- insert current directory name instead. */
571 /* First, realloc the filename buffer if too short. */
572 len
= strlen (current_directory
);
573 newlen
= len
+ strlen (string
) + 2;
574 if (newlen
> alloclen
)
577 filename
= alloca (alloclen
);
579 strcpy (filename
, current_directory
);
583 /* Normal file name in path -- just use it. */
584 strncpy (filename
, p
, len
);
588 /* Remove trailing slashes */
589 while (len
> 0 && IS_DIR_SEPARATOR (filename
[len
- 1]))
592 strcat (filename
+ len
, SLASH_STRING
);
593 strcat (filename
, string
);
595 fd
= open (filename
, mode
);
604 *filename_opened
= NULL
;
605 else if (IS_ABSOLUTE_PATH (filename
))
606 *filename_opened
= savestring (filename
, strlen (filename
));
609 /* Beware the // my son, the Emacs barfs, the botch that catch... */
611 *filename_opened
= concat (current_directory
,
612 IS_DIR_SEPARATOR (current_directory
[strlen (current_directory
) - 1])
617 /* OBSOLETE #ifdef MPW */
618 /* OBSOLETE This is a debugging hack that can go away when all combinations */
619 /* OBSOLETE of Mac and Unix names are handled reasonably. */
621 /* OBSOLETE extern int debug_openp; */
623 /* OBSOLETE if (debug_openp) */
625 /* OBSOLETE printf ("openp on %s, path %s mode %d prot %d\n returned %d", */
626 /* OBSOLETE string, path, mode, prot, fd); */
627 /* OBSOLETE if (*filename_opened) */
628 /* OBSOLETE printf (" (filename is %s)", *filename_opened); */
629 /* OBSOLETE printf ("\n"); */
632 /* OBSOLETE #endif *//* MPW */
638 /* This is essentially a convenience, for clients that want the behaviour
639 of openp, using source_path, but that really don't want the file to be
640 opened but want instead just to know what the full pathname is (as
641 qualified against source_path).
643 The current working directory is searched first.
645 If the file was found, this function returns 1, and FULL_PATHNAME is
646 set to the fully-qualified pathname.
648 Else, this functions returns 0, and FULL_PATHNAME is set to NULL.
651 source_full_path_of (char *filename
, char **full_pathname
)
655 fd
= openp (source_path
, 1, filename
, O_RDONLY
, 0, full_pathname
);
658 *full_pathname
= NULL
;
667 /* Open a source file given a symtab S. Returns a file descriptor or
668 negative number for error. */
671 open_source_file (struct symtab
*s
)
673 char *path
= source_path
;
678 /* Quick way out if we already know its full name */
681 result
= open (s
->fullname
, OPEN_MODE
);
684 /* Didn't work -- free old one, try again. */
685 mfree (s
->objfile
->md
, s
->fullname
);
689 if (s
->dirname
!= NULL
)
691 /* Replace a path entry of $cdir with the compilation directory name */
693 /* We cast strstr's result in case an ANSIhole has made it const,
694 which produces a "required warning" when assigned to a nonconst. */
695 p
= (char *) strstr (source_path
, "$cdir");
696 if (p
&& (p
== path
|| p
[-1] == DIRNAME_SEPARATOR
)
697 && (p
[cdir_len
] == DIRNAME_SEPARATOR
|| p
[cdir_len
] == '\0'))
702 alloca (strlen (source_path
) + 1 + strlen (s
->dirname
) + 1);
703 len
= p
- source_path
;
704 strncpy (path
, source_path
, len
); /* Before $cdir */
705 strcpy (path
+ len
, s
->dirname
); /* new stuff */
706 strcat (path
+ len
, source_path
+ len
+ cdir_len
); /* After $cdir */
710 result
= openp (path
, 0, s
->filename
, OPEN_MODE
, 0, &s
->fullname
);
713 /* Didn't work. Try using just the basename. */
714 p
= lbasename (s
->filename
);
715 if (p
!= s
->filename
)
716 result
= openp (path
, 0, p
, OPEN_MODE
, 0, &s
->fullname
);
718 /* OBSOLETE #ifdef MPW */
719 /* OBSOLETE if (result < 0) */
721 /* OBSOLETE *//* Didn't work. Try using just the MPW basename. */
722 /* OBSOLETE p = (char *) mpw_basename (s->filename); */
723 /* OBSOLETE if (p != s->filename) */
724 /* OBSOLETE result = openp (path, 0, p, OPEN_MODE, 0, &s->fullname); */
726 /* OBSOLETE if (result < 0) */
728 /* OBSOLETE *//* Didn't work. Try using the mixed Unix/MPW basename. */
729 /* OBSOLETE p = (char *) mpw_mixed_basename (s->filename); */
730 /* OBSOLETE if (p != s->filename) */
731 /* OBSOLETE result = openp (path, 0, p, OPEN_MODE, 0, &s->fullname); */
733 /* OBSOLETE #endif MPW */
737 fullname
= s
->fullname
;
738 s
->fullname
= mstrsave (s
->objfile
->md
, s
->fullname
);
744 /* Return the path to the source file associated with symtab. Returns NULL
748 symtab_to_filename (struct symtab
*s
)
755 /* If we've seen the file before, just return fullname. */
760 /* Try opening the file to setup fullname */
762 fd
= open_source_file (s
);
764 return s
->filename
; /* File not found. Just use short name */
766 /* Found the file. Cleanup and return the full name */
773 /* Create and initialize the table S->line_charpos that records
774 the positions of the lines in the source file, which is assumed
775 to be open on descriptor DESC.
776 All set S->nlines to the number of such lines. */
779 find_source_lines (struct symtab
*s
, int desc
)
782 register char *data
, *p
, *end
;
784 int lines_allocated
= 1000;
789 line_charpos
= (int *) xmmalloc (s
->objfile
->md
,
790 lines_allocated
* sizeof (int));
791 if (fstat (desc
, &st
) < 0)
792 perror_with_name (s
->filename
);
794 if (s
&& s
->objfile
&& s
->objfile
->obfd
)
795 mtime
= bfd_get_mtime (s
->objfile
->obfd
);
797 mtime
= bfd_get_mtime (exec_bfd
);
799 if (mtime
&& mtime
< st
.st_mtime
)
801 warning ("Source file is more recent than executable.\n");
804 #ifdef LSEEK_NOT_LINEAR
808 /* Have to read it byte by byte to find out where the chars live */
810 line_charpos
[0] = lseek (desc
, 0, SEEK_CUR
);
812 while (myread (desc
, &c
, 1) > 0)
816 if (nlines
== lines_allocated
)
818 lines_allocated
*= 2;
820 (int *) xmrealloc (s
->objfile
->md
, (char *) line_charpos
,
821 sizeof (int) * lines_allocated
);
823 line_charpos
[nlines
++] = lseek (desc
, 0, SEEK_CUR
);
827 #else /* lseek linear. */
829 struct cleanup
*old_cleanups
;
831 /* st_size might be a large type, but we only support source files whose
832 size fits in an int. */
833 size
= (int) st
.st_size
;
835 /* Use malloc, not alloca, because this may be pretty large, and we may
836 run into various kinds of limits on stack size. */
837 data
= (char *) xmalloc (size
);
838 old_cleanups
= make_cleanup (xfree
, data
);
840 /* Reassign `size' to result of read for systems where \r\n -> \n. */
841 size
= myread (desc
, data
, size
);
843 perror_with_name (s
->filename
);
851 /* A newline at the end does not start a new line. */
854 if (nlines
== lines_allocated
)
856 lines_allocated
*= 2;
858 (int *) xmrealloc (s
->objfile
->md
, (char *) line_charpos
,
859 sizeof (int) * lines_allocated
);
861 line_charpos
[nlines
++] = p
- data
;
864 do_cleanups (old_cleanups
);
866 #endif /* lseek linear. */
869 (int *) xmrealloc (s
->objfile
->md
, (char *) line_charpos
,
870 nlines
* sizeof (int));
874 /* Return the character position of a line LINE in symtab S.
875 Return 0 if anything is invalid. */
877 #if 0 /* Currently unused */
880 source_line_charpos (struct symtab
*s
, int line
)
884 if (!s
->line_charpos
|| line
<= 0)
886 if (line
> s
->nlines
)
888 return s
->line_charpos
[line
- 1];
891 /* Return the line number of character position POS in symtab S. */
894 source_charpos_line (register struct symtab
*s
, register int chr
)
896 register int line
= 0;
899 if (s
== 0 || s
->line_charpos
== 0)
901 lnp
= s
->line_charpos
;
902 /* Files are usually short, so sequential search is Ok */
903 while (line
< s
->nlines
&& *lnp
<= chr
)
908 if (line
>= s
->nlines
)
916 /* Get full pathname and line number positions for a symtab.
917 Return nonzero if line numbers may have changed.
918 Set *FULLNAME to actual name of the file as found by `openp',
919 or to 0 if the file is not found. */
922 get_filename_and_charpos (struct symtab
*s
, char **fullname
)
924 register int desc
, linenums_changed
= 0;
926 desc
= open_source_file (s
);
934 *fullname
= s
->fullname
;
935 if (s
->line_charpos
== 0)
936 linenums_changed
= 1;
937 if (linenums_changed
)
938 find_source_lines (s
, desc
);
940 return linenums_changed
;
943 /* Print text describing the full name of the source file S
944 and the line number LINE and its corresponding character position.
945 The text starts with two Ctrl-z so that the Emacs-GDB interface
948 MID_STATEMENT is nonzero if the PC is not at the beginning of that line.
950 Return 1 if successful, 0 if could not find the file. */
953 identify_source_line (struct symtab
*s
, int line
, int mid_statement
,
956 if (s
->line_charpos
== 0)
957 get_filename_and_charpos (s
, (char **) NULL
);
958 if (s
->fullname
== 0)
960 if (line
> s
->nlines
)
961 /* Don't index off the end of the line_charpos array. */
963 annotate_source (s
->fullname
, line
, s
->line_charpos
[line
- 1],
966 current_source_line
= line
;
967 first_line_listed
= line
;
968 last_line_listed
= line
;
969 current_source_symtab
= s
;
974 /* Print source lines from the file of symtab S,
975 starting with line number LINE and stopping before line number STOPLINE. */
977 static void print_source_lines_base (struct symtab
*s
, int line
, int stopline
,
980 print_source_lines_base (struct symtab
*s
, int line
, int stopline
, int noerror
)
984 register FILE *stream
;
985 int nlines
= stopline
- line
;
987 /* Regardless of whether we can open the file, set current_source_symtab. */
988 current_source_symtab
= s
;
989 current_source_line
= line
;
990 first_line_listed
= line
;
993 /* If printing of source lines is disabled, just print file and line number */
994 if (ui_out_test_flags (uiout
, ui_source_list
))
997 /* Only prints "No such file or directory" once */
998 if ((s
!= last_source_visited
) || (!last_source_error
))
1000 last_source_visited
= s
;
1001 desc
= open_source_file (s
);
1005 desc
= last_source_error
;
1019 last_source_error
= desc
;
1023 char *name
= alloca (strlen (s
->filename
) + 100);
1024 sprintf (name
, "%d\t%s", line
, s
->filename
);
1025 print_sys_errmsg (name
, errno
);
1029 ui_out_field_int (uiout
, "line", line
);
1030 ui_out_text (uiout
, "\tin ");
1031 ui_out_field_string (uiout
, "file", s
->filename
);
1032 ui_out_text (uiout
, "\n");
1034 printf_filtered ("%d\tin %s\n", line
, s
->filename
);
1040 last_source_error
= 0;
1042 if (s
->line_charpos
== 0)
1043 find_source_lines (s
, desc
);
1045 if (line
< 1 || line
> s
->nlines
)
1048 error ("Line number %d out of range; %s has %d lines.",
1049 line
, s
->filename
, s
->nlines
);
1052 if (lseek (desc
, s
->line_charpos
[line
- 1], 0) < 0)
1055 perror_with_name (s
->filename
);
1058 stream
= fdopen (desc
, FDOPEN_MODE
);
1061 while (nlines
-- > 0)
1069 last_line_listed
= current_source_line
;
1070 sprintf (buf
, "%d\t", current_source_line
++);
1071 ui_out_text (uiout
, buf
);
1074 if (c
< 040 && c
!= '\t' && c
!= '\n' && c
!= '\r')
1076 sprintf (buf
, "^%c", c
+ 0100);
1077 ui_out_text (uiout
, buf
);
1080 ui_out_text (uiout
, "^?");
1081 #ifdef CRLF_SOURCE_FILES
1084 /* Skip a \r character, but only before a \n. */
1085 int c1
= fgetc (stream
);
1088 printf_filtered ("^%c", c
+ 0100);
1090 ungetc (c1
, stream
);
1095 sprintf (buf
, "%c", c
);
1096 ui_out_text (uiout
, buf
);
1099 while (c
!= '\n' && (c
= fgetc (stream
)) >= 0);
1104 last_line_listed
= current_source_line
;
1105 printf_filtered ("%d\t", current_source_line
++);
1108 if (c
< 040 && c
!= '\t' && c
!= '\n' && c
!= '\r')
1109 printf_filtered ("^%c", c
+ 0100);
1111 printf_filtered ("^?");
1112 #ifdef CRLF_SOURCE_FILES
1115 /* Just skip \r characters. */
1119 printf_filtered ("%c", c
);
1121 while (c
!= '\n' && (c
= fgetc (stream
)) >= 0);
1128 /* Show source lines from the file of symtab S, starting with line
1129 number LINE and stopping before line number STOPLINE. If this is the
1130 not the command line version, then the source is shown in the source
1131 window otherwise it is simply printed */
1134 print_source_lines (struct symtab
*s
, int line
, int stopline
, int noerror
)
1136 print_source_lines_base (s
, line
, stopline
, noerror
);
1141 /* Print a list of files and line numbers which a user may choose from
1142 in order to list a function which was specified ambiguously (as with
1143 `list classname::overloadedfuncname', for example). The vector in
1144 SALS provides the filenames and line numbers. */
1147 ambiguous_line_spec (struct symtabs_and_lines
*sals
)
1151 for (i
= 0; i
< sals
->nelts
; ++i
)
1152 printf_filtered ("file: \"%s\", line number: %d\n",
1153 sals
->sals
[i
].symtab
->filename
, sals
->sals
[i
].line
);
1157 list_command (char *arg
, int 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 xfree (sals_end
.sals
);
1254 sal_end
= sals_end
.sals
[0];
1255 xfree (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 int first_line
= sal
.line
- lines_to_list
/ 2;
1319 if (first_line
< 1) first_line
= 1;
1321 print_source_lines (sal
.symtab
, first_line
, first_line
+ lines_to_list
,
1325 print_source_lines (sal
.symtab
, sal
.line
,
1327 ? sal
.line
+ lines_to_list
1328 : sal_end
.line
+ 1),
1332 /* Print info on range of pc's in a specified line. */
1335 line_info (char *arg
, int from_tty
)
1337 struct symtabs_and_lines sals
;
1338 struct symtab_and_line sal
;
1339 CORE_ADDR start_pc
, end_pc
;
1342 INIT_SAL (&sal
); /* initialize to zeroes */
1346 sal
.symtab
= current_source_symtab
;
1347 sal
.line
= last_line_listed
;
1349 sals
.sals
= (struct symtab_and_line
*)
1350 xmalloc (sizeof (struct symtab_and_line
));
1355 sals
= decode_line_spec_1 (arg
, 0);
1360 /* C++ More than one line may have been specified, as when the user
1361 specifies an overloaded function name. Print info on them all. */
1362 for (i
= 0; i
< sals
.nelts
; i
++)
1366 if (sal
.symtab
== 0)
1368 printf_filtered ("No line number information available");
1371 /* This is useful for "info line *0x7f34". If we can't tell the
1372 user about a source line, at least let them have the symbolic
1374 printf_filtered (" for address ");
1376 print_address (sal
.pc
, gdb_stdout
);
1379 printf_filtered (".");
1380 printf_filtered ("\n");
1382 else if (sal
.line
> 0
1383 && find_line_pc_range (sal
, &start_pc
, &end_pc
))
1385 if (start_pc
== end_pc
)
1387 printf_filtered ("Line %d of \"%s\"",
1388 sal
.line
, sal
.symtab
->filename
);
1390 printf_filtered (" is at address ");
1391 print_address (start_pc
, gdb_stdout
);
1393 printf_filtered (" but contains no code.\n");
1397 printf_filtered ("Line %d of \"%s\"",
1398 sal
.line
, sal
.symtab
->filename
);
1400 printf_filtered (" starts at address ");
1401 print_address (start_pc
, gdb_stdout
);
1403 printf_filtered (" and ends at ");
1404 print_address (end_pc
, gdb_stdout
);
1405 printf_filtered (".\n");
1408 /* x/i should display this line's code. */
1409 set_next_address (start_pc
);
1411 /* Repeating "info line" should do the following line. */
1412 last_line_listed
= sal
.line
+ 1;
1414 /* If this is the only line, show the source code. If it could
1415 not find the file, don't do anything special. */
1416 if (annotation_level
&& sals
.nelts
== 1)
1417 identify_source_line (sal
.symtab
, sal
.line
, 0, start_pc
);
1420 /* Is there any case in which we get here, and have an address
1421 which the user would want to see? If we have debugging symbols
1422 and no line numbers? */
1423 printf_filtered ("Line number %d is out of range for \"%s\".\n",
1424 sal
.line
, sal
.symtab
->filename
);
1429 /* Commands to search the source file for a regexp. */
1433 forward_search_command (char *regex
, int from_tty
)
1437 register FILE *stream
;
1441 line
= last_line_listed
+ 1;
1443 msg
= (char *) re_comp (regex
);
1447 if (current_source_symtab
== 0)
1448 select_source_symtab (0);
1450 desc
= open_source_file (current_source_symtab
);
1452 perror_with_name (current_source_symtab
->filename
);
1454 if (current_source_symtab
->line_charpos
== 0)
1455 find_source_lines (current_source_symtab
, desc
);
1457 if (line
< 1 || line
> current_source_symtab
->nlines
)
1460 error ("Expression not found");
1463 if (lseek (desc
, current_source_symtab
->line_charpos
[line
- 1], 0) < 0)
1466 perror_with_name (current_source_symtab
->filename
);
1469 stream
= fdopen (desc
, FDOPEN_MODE
);
1473 static char *buf
= NULL
;
1475 int cursize
, newsize
;
1478 buf
= xmalloc (cursize
);
1487 if (p
- buf
== cursize
)
1489 newsize
= cursize
+ cursize
/ 2;
1490 buf
= xrealloc (buf
, newsize
);
1495 while (c
!= '\n' && (c
= getc (stream
)) >= 0);
1497 #ifdef CRLF_SOURCE_FILES
1498 /* Remove the \r, if any, at the end of the line, otherwise
1499 regular expressions that end with $ or \n won't work. */
1500 if (p
- buf
> 1 && p
[-2] == '\r')
1507 /* we now have a source line in buf, null terminate and match */
1509 if (re_exec (buf
) > 0)
1513 print_source_lines (current_source_symtab
, line
, line
+ 1, 0);
1514 set_internalvar (lookup_internalvar ("_"),
1515 value_from_longest (builtin_type_int
,
1517 current_source_line
= max (line
- lines_to_list
/ 2, 1);
1523 printf_filtered ("Expression not found\n");
1529 reverse_search_command (char *regex
, int from_tty
)
1533 register FILE *stream
;
1537 line
= last_line_listed
- 1;
1539 msg
= (char *) re_comp (regex
);
1543 if (current_source_symtab
== 0)
1544 select_source_symtab (0);
1546 desc
= open_source_file (current_source_symtab
);
1548 perror_with_name (current_source_symtab
->filename
);
1550 if (current_source_symtab
->line_charpos
== 0)
1551 find_source_lines (current_source_symtab
, desc
);
1553 if (line
< 1 || line
> current_source_symtab
->nlines
)
1556 error ("Expression not found");
1559 if (lseek (desc
, current_source_symtab
->line_charpos
[line
- 1], 0) < 0)
1562 perror_with_name (current_source_symtab
->filename
);
1565 stream
= fdopen (desc
, FDOPEN_MODE
);
1569 /* FIXME!!! We walk right off the end of buf if we get a long line!!! */
1570 char buf
[4096]; /* Should be reasonable??? */
1571 register char *p
= buf
;
1580 while (c
!= '\n' && (c
= getc (stream
)) >= 0);
1582 #ifdef CRLF_SOURCE_FILES
1583 /* Remove the \r, if any, at the end of the line, otherwise
1584 regular expressions that end with $ or \n won't work. */
1585 if (p
- buf
> 1 && p
[-2] == '\r')
1592 /* We now have a source line in buf; null terminate and match. */
1594 if (re_exec (buf
) > 0)
1598 print_source_lines (current_source_symtab
, line
, line
+ 1, 0);
1599 set_internalvar (lookup_internalvar ("_"),
1600 value_from_longest (builtin_type_int
,
1602 current_source_line
= max (line
- lines_to_list
/ 2, 1);
1606 if (fseek (stream
, current_source_symtab
->line_charpos
[line
- 1], 0) < 0)
1609 perror_with_name (current_source_symtab
->filename
);
1613 printf_filtered ("Expression not found\n");
1619 _initialize_source (void)
1621 struct cmd_list_element
*c
;
1622 current_source_symtab
= 0;
1623 init_source_path ();
1625 /* The intention is to use POSIX Basic Regular Expressions.
1626 Always use the GNU regex routine for consistency across all hosts.
1627 Our current GNU regex.c does not have all the POSIX features, so this is
1628 just an approximation. */
1629 re_set_syntax (RE_SYNTAX_GREP
);
1631 c
= add_cmd ("directory", class_files
, directory_command
,
1632 "Add directory DIR to beginning of search path for source files.\n\
1633 Forget cached info on source file locations and line positions.\n\
1634 DIR can also be $cwd for the current working directory, or $cdir for the\n\
1635 directory in which the source file was compiled into object code.\n\
1636 With no argument, reset the search path to $cdir:$cwd, the default.",
1640 add_com_alias ("use", "directory", class_files
, 0);
1642 c
->completer
= filename_completer
;
1644 add_cmd ("directories", no_class
, show_directories
,
1645 "Current search path for finding source files.\n\
1646 $cwd in the path means the current working directory.\n\
1647 $cdir in the path means the compilation directory of the source file.",
1652 add_com_alias ("D", "directory", class_files
, 0);
1653 add_cmd ("ld", no_class
, show_directories
,
1654 "Current search path for finding source files.\n\
1655 $cwd in the path means the current working directory.\n\
1656 $cdir in the path means the compilation directory of the source file.",
1660 add_info ("source", source_info
,
1661 "Information about the current source file.");
1663 add_info ("line", line_info
,
1664 concat ("Core addresses of the code for a source line.\n\
1665 Line can be specified as\n\
1666 LINENUM, to list around that line in current file,\n\
1667 FILE:LINENUM, to list around that line in that file,\n\
1668 FUNCTION, to list around beginning of that function,\n\
1669 FILE:FUNCTION, to distinguish among like-named static functions.\n\
1671 Default is to describe the last source line that was listed.\n\n\
1672 This sets the default address for \"x\" to the line's first instruction\n\
1673 so that \"x/i\" suffices to start examining the machine code.\n\
1674 The address is also stored as the value of \"$_\".", NULL
));
1676 add_com ("forward-search", class_files
, forward_search_command
,
1677 "Search for regular expression (see regex(3)) from last line listed.\n\
1678 The matching line number is also stored as the value of \"$_\".");
1679 add_com_alias ("search", "forward-search", class_files
, 0);
1681 add_com ("reverse-search", class_files
, reverse_search_command
,
1682 "Search backward for regular expression (see regex(3)) from last line listed.\n\
1683 The matching line number is also stored as the value of \"$_\".");
1687 add_com_alias ("/", "forward-search", class_files
, 0);
1688 add_com_alias ("?", "reverse-search", class_files
, 0);
1691 add_com ("list", class_files
, list_command
,
1692 concat ("List specified function or line.\n\
1693 With no argument, lists ten more lines after or around previous listing.\n\
1694 \"list -\" lists the ten lines before a previous ten-line listing.\n\
1695 One argument specifies a line, and ten lines are listed around that line.\n\
1696 Two arguments with comma between specify starting and ending lines to list.\n\
1698 Lines can be specified in these ways:\n\
1699 LINENUM, to list around that line in current file,\n\
1700 FILE:LINENUM, to list around that line in that file,\n\
1701 FUNCTION, to list around beginning of that function,\n\
1702 FILE:FUNCTION, to distinguish among like-named static functions.\n\
1703 *ADDRESS, to list around the line containing that address.\n\
1704 With two args if one is empty it stands for ten lines away from the other arg.", NULL
));
1707 add_com_alias ("l", "list", class_files
, 1);
1709 add_com_alias ("v", "list", class_files
, 1);
1712 add_com_alias ("file", "list", class_files
, 1);
1715 (add_set_cmd ("listsize", class_support
, var_uinteger
,
1716 (char *) &lines_to_list
,
1717 "Set number of source lines gdb will list by default.",
This page took 0.066558 seconds and 4 git commands to generate.