1 /* List lines of source files for GDB, the GNU debugger.
2 Copyright (C) 1986-2013 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 3 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, see <http://www.gnu.org/licenses/>. */
20 #include "arch-utils.h"
22 #include "expression.h"
29 #include "gdb_assert.h"
31 #include <sys/types.h>
32 #include "gdb_string.h"
36 #include "gdb_regex.h"
42 #include "filenames.h" /* for DOSish file names */
43 #include "completer.h"
45 #include "readline/readline.h"
50 #define OPEN_MODE (O_RDONLY | O_BINARY)
51 #define FDOPEN_MODE FOPEN_RB
53 /* Prototypes for exported functions. */
55 void _initialize_source (void);
57 /* Prototypes for local functions. */
59 static int get_filename_and_charpos (struct symtab
*, char **);
61 static void reverse_search_command (char *, int);
63 static void forward_search_command (char *, int);
65 static void line_info (char *, int);
67 static void source_info (char *, int);
69 /* Path of directories to search for source files.
70 Same format as the PATH environment variable's value. */
74 /* Support for source path substitution commands. */
76 struct substitute_path_rule
80 struct substitute_path_rule
*next
;
83 static struct substitute_path_rule
*substitute_path_rules
= NULL
;
85 /* Symtab of default file for listing lines of. */
87 static struct symtab
*current_source_symtab
;
89 /* Default next line to list. */
91 static int current_source_line
;
93 static struct program_space
*current_source_pspace
;
95 /* Default number of lines to print with commands like "list".
96 This is based on guessing how many long (i.e. more than chars_per_line
97 characters) lines there will be. To be completely correct, "list"
98 and friends should be rewritten to count characters and see where
99 things are wrapping, but that would be a fair amount of work. */
101 int lines_to_list
= 10;
103 show_lines_to_list (struct ui_file
*file
, int from_tty
,
104 struct cmd_list_element
*c
, const char *value
)
106 fprintf_filtered (file
,
107 _("Number of source lines gdb "
108 "will list by default is %s.\n"),
112 /* Possible values of 'set filename-display'. */
113 static const char filename_display_basename
[] = "basename";
114 static const char filename_display_relative
[] = "relative";
115 static const char filename_display_absolute
[] = "absolute";
117 static const char *const filename_display_kind_names
[] = {
118 filename_display_basename
,
119 filename_display_relative
,
120 filename_display_absolute
,
124 static const char *filename_display_string
= filename_display_relative
;
127 show_filename_display_string (struct ui_file
*file
, int from_tty
,
128 struct cmd_list_element
*c
, const char *value
)
130 fprintf_filtered (file
, _("Filenames are displayed as \"%s\".\n"), value
);
133 /* Line number of last line printed. Default for various commands.
134 current_source_line is usually, but not always, the same as this. */
136 static int last_line_listed
;
138 /* First line number listed by last listing command. */
140 static int first_line_listed
;
142 /* Saves the name of the last source file visited and a possible error code.
143 Used to prevent repeating annoying "No such file or directories" msgs. */
145 static struct symtab
*last_source_visited
= NULL
;
146 static int last_source_error
= 0;
148 /* Return the first line listed by print_source_lines.
149 Used by command interpreters to request listing from
153 get_first_line_listed (void)
155 return first_line_listed
;
158 /* Return the default number of lines to print with commands like the
159 cli "list". The caller of print_source_lines must use this to
160 calculate the end line and use it in the call to print_source_lines
161 as it does not automatically use this value. */
164 get_lines_to_list (void)
166 return lines_to_list
;
169 /* Return the current source file for listing and next line to list.
170 NOTE: The returned sal pc and end fields are not valid. */
172 struct symtab_and_line
173 get_current_source_symtab_and_line (void)
175 struct symtab_and_line cursal
= { 0 };
177 cursal
.pspace
= current_source_pspace
;
178 cursal
.symtab
= current_source_symtab
;
179 cursal
.line
= current_source_line
;
186 /* If the current source file for listing is not set, try and get a default.
187 Usually called before get_current_source_symtab_and_line() is called.
188 It may err out if a default cannot be determined.
189 We must be cautious about where it is called, as it can recurse as the
190 process of determining a new default may call the caller!
191 Use get_current_source_symtab_and_line only to get whatever
192 we have without erroring out or trying to get a default. */
195 set_default_source_symtab_and_line (void)
197 if (!have_full_symbols () && !have_partial_symbols ())
198 error (_("No symbol table is loaded. Use the \"file\" command."));
200 /* Pull in a current source symtab if necessary. */
201 if (current_source_symtab
== 0)
202 select_source_symtab (0);
205 /* Return the current default file for listing and next line to list
206 (the returned sal pc and end fields are not valid.)
207 and set the current default to whatever is in SAL.
208 NOTE: The returned sal pc and end fields are not valid. */
210 struct symtab_and_line
211 set_current_source_symtab_and_line (const struct symtab_and_line
*sal
)
213 struct symtab_and_line cursal
= { 0 };
215 cursal
.pspace
= current_source_pspace
;
216 cursal
.symtab
= current_source_symtab
;
217 cursal
.line
= current_source_line
;
221 current_source_pspace
= sal
->pspace
;
222 current_source_symtab
= sal
->symtab
;
223 current_source_line
= sal
->line
;
228 /* Reset any information stored about a default file and line to print. */
231 clear_current_source_symtab_and_line (void)
233 current_source_symtab
= 0;
234 current_source_line
= 0;
237 /* Set the source file default for the "list" command to be S.
239 If S is NULL, and we don't have a default, find one. This
240 should only be called when the user actually tries to use the
241 default, since we produce an error if we can't find a reasonable
242 default. Also, since this can cause symbols to be read, doing it
243 before we need to would make things slower than necessary. */
246 select_source_symtab (struct symtab
*s
)
248 struct symtabs_and_lines sals
;
249 struct symtab_and_line sal
;
254 current_source_symtab
= s
;
255 current_source_line
= 1;
256 current_source_pspace
= SYMTAB_PSPACE (s
);
260 if (current_source_symtab
)
263 /* Make the default place to list be the function `main'
265 if (lookup_symbol (main_name (), 0, VAR_DOMAIN
, 0))
267 sals
= decode_line_with_current_source (main_name (),
268 DECODE_LINE_FUNFIRSTLINE
);
271 current_source_pspace
= sal
.pspace
;
272 current_source_symtab
= sal
.symtab
;
273 current_source_line
= max (sal
.line
- (lines_to_list
- 1), 1);
274 if (current_source_symtab
)
278 /* Alright; find the last file in the symtab list (ignoring .h's
279 and namespace symtabs). */
281 current_source_line
= 1;
285 for (s
= ofp
->symtabs
; s
; s
= s
->next
)
287 const char *name
= s
->filename
;
288 int len
= strlen (name
);
290 if (!(len
> 2 && (strcmp (&name
[len
- 2], ".h") == 0
291 || strcmp (name
, "<<C++-namespaces>>") == 0)))
293 current_source_pspace
= current_program_space
;
294 current_source_symtab
= s
;
299 if (current_source_symtab
)
305 s
= ofp
->sf
->qf
->find_last_source_symtab (ofp
);
307 current_source_symtab
= s
;
309 if (current_source_symtab
)
312 error (_("Can't find a default source file"));
315 /* Handler for "set directories path-list" command.
316 "set dir mumble" doesn't prepend paths, it resets the entire
317 path list. The theory is that set(show(dir)) should be a no-op. */
320 set_directories_command (char *args
, int from_tty
, struct cmd_list_element
*c
)
322 /* This is the value that was set.
323 It needs to be processed to maintain $cdir:$cwd and remove dups. */
324 char *set_path
= source_path
;
326 /* We preserve the invariant that $cdir:$cwd begins life at the end of
327 the list by calling init_source_path. If they appear earlier in
328 SET_PATH then mod_path will move them appropriately.
329 mod_path will also remove duplicates. */
331 if (*set_path
!= '\0')
332 mod_path (set_path
, &source_path
);
337 /* Print the list of source directories.
338 This is used by the "ld" command, so it has the signature of a command
342 show_directories_1 (char *ignore
, int from_tty
)
344 puts_filtered ("Source directories searched: ");
345 puts_filtered (source_path
);
346 puts_filtered ("\n");
349 /* Handler for "show directories" command. */
352 show_directories_command (struct ui_file
*file
, int from_tty
,
353 struct cmd_list_element
*c
, const char *value
)
355 show_directories_1 (NULL
, from_tty
);
358 /* Forget line positions and file names for the symtabs in a
359 particular objfile. */
362 forget_cached_source_info_for_objfile (struct objfile
*objfile
)
366 ALL_OBJFILE_SYMTABS (objfile
, s
)
368 if (s
->line_charpos
!= NULL
)
370 xfree (s
->line_charpos
);
371 s
->line_charpos
= NULL
;
373 if (s
->fullname
!= NULL
)
381 objfile
->sf
->qf
->forget_cached_source_info (objfile
);
384 /* Forget what we learned about line positions in source files, and
385 which directories contain them; must check again now since files
386 may be found in a different directory now. */
389 forget_cached_source_info (void)
391 struct program_space
*pspace
;
392 struct objfile
*objfile
;
395 ALL_PSPACE_OBJFILES (pspace
, objfile
)
397 forget_cached_source_info_for_objfile (objfile
);
400 last_source_visited
= NULL
;
404 init_source_path (void)
408 xsnprintf (buf
, sizeof (buf
), "$cdir%c$cwd", DIRNAME_SEPARATOR
);
409 source_path
= xstrdup (buf
);
410 forget_cached_source_info ();
413 /* Add zero or more directories to the front of the source path. */
416 directory_command (char *dirname
, int from_tty
)
419 /* FIXME, this goes to "delete dir"... */
422 if (!from_tty
|| query (_("Reinitialize source path to empty? ")))
430 mod_path (dirname
, &source_path
);
431 forget_cached_source_info ();
434 show_directories_1 ((char *) 0, from_tty
);
437 /* Add a path given with the -d command line switch.
438 This will not be quoted so we must not treat spaces as separators. */
441 directory_switch (char *dirname
, int from_tty
)
443 add_path (dirname
, &source_path
, 0);
446 /* Add zero or more directories to the front of an arbitrary path. */
449 mod_path (char *dirname
, char **which_path
)
451 add_path (dirname
, which_path
, 1);
454 /* Workhorse of mod_path. Takes an extra argument to determine
455 if dirname should be parsed for separators that indicate multiple
456 directories. This allows for interfaces that pre-parse the dirname
457 and allow specification of traditional separator characters such
461 add_path (char *dirname
, char **which_path
, int parse_separators
)
463 char *old
= *which_path
;
465 VEC (char_ptr
) *dir_vec
= NULL
;
466 struct cleanup
*back_to
;
473 if (parse_separators
)
475 char **argv
, **argvp
;
477 /* This will properly parse the space and tab separators
478 and any quotes that may exist. */
479 argv
= gdb_buildargv (dirname
);
481 for (argvp
= argv
; *argvp
; argvp
++)
482 dirnames_to_char_ptr_vec_append (&dir_vec
, *argvp
);
487 VEC_safe_push (char_ptr
, dir_vec
, xstrdup (dirname
));
488 back_to
= make_cleanup_free_char_ptr_vec (dir_vec
);
490 for (ix
= 0; VEC_iterate (char_ptr
, dir_vec
, ix
, name
); ++ix
)
495 /* Spaces and tabs will have been removed by buildargv().
496 NAME is the start of the directory.
497 P is the '\0' following the end. */
498 p
= name
+ strlen (name
);
500 while (!(IS_DIR_SEPARATOR (*name
) && p
<= name
+ 1) /* "/" */
501 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
502 /* On MS-DOS and MS-Windows, h:\ is different from h: */
503 && !(p
== name
+ 3 && name
[1] == ':') /* "d:/" */
505 && IS_DIR_SEPARATOR (p
[-1]))
506 /* Sigh. "foo/" => "foo" */
510 while (p
> name
&& p
[-1] == '.')
514 /* "." => getwd (). */
515 name
= current_directory
;
518 else if (p
> name
+ 1 && IS_DIR_SEPARATOR (p
[-2]))
528 /* "...foo/." => "...foo". */
539 name
= tilde_expand (name
);
540 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
541 else if (IS_ABSOLUTE_PATH (name
) && p
== name
+ 2) /* "d:" => "d:." */
542 name
= concat (name
, ".", (char *)NULL
);
544 else if (!IS_ABSOLUTE_PATH (name
) && name
[0] != '$')
545 name
= concat (current_directory
, SLASH_STRING
, name
, (char *)NULL
);
547 name
= savestring (name
, p
- name
);
548 make_cleanup (xfree
, name
);
550 /* Unless it's a variable, check existence. */
553 /* These are warnings, not errors, since we don't want a
554 non-existent directory in a .gdbinit file to stop processing
555 of the .gdbinit file.
557 Whether they get added to the path is more debatable. Current
558 answer is yes, in case the user wants to go make the directory
559 or whatever. If the directory continues to not exist/not be
560 a directory/etc, then having them in the path should be
562 if (stat (name
, &st
) < 0)
564 int save_errno
= errno
;
566 fprintf_unfiltered (gdb_stderr
, "Warning: ");
567 print_sys_errmsg (name
, save_errno
);
569 else if ((st
.st_mode
& S_IFMT
) != S_IFDIR
)
570 warning (_("%s is not a directory."), name
);
575 unsigned int len
= strlen (name
);
579 /* FIXME: we should use realpath() or its work-alike
580 before comparing. Then all the code above which
581 removes excess slashes and dots could simply go away. */
582 if (!filename_cmp (p
, name
))
584 /* Found it in the search path, remove old copy. */
586 p
--; /* Back over leading separator. */
587 if (prefix
> p
- *which_path
)
588 goto skip_dup
; /* Same dir twice in one cmd. */
589 memmove (p
, &p
[len
+ 1], strlen (&p
[len
+ 1]) + 1); /* Copy from next \0 or : */
592 tinybuf
[0] = DIRNAME_SEPARATOR
;
595 /* If we have already tacked on a name(s) in this command,
596 be sure they stay on the front as we tack on some
604 temp
= concat (old
, tinybuf
, name
, (char *)NULL
);
606 *which_path
= concat (temp
, "", &old
[prefix
], (char *) NULL
);
607 prefix
= strlen (temp
);
612 *which_path
= concat (name
, (old
[0] ? tinybuf
: old
),
614 prefix
= strlen (name
);
623 do_cleanups (back_to
);
628 source_info (char *ignore
, int from_tty
)
630 struct symtab
*s
= current_source_symtab
;
634 printf_filtered (_("No current source file.\n"));
637 printf_filtered (_("Current source file is %s\n"), s
->filename
);
639 printf_filtered (_("Compilation directory is %s\n"), s
->dirname
);
641 printf_filtered (_("Located in %s\n"), s
->fullname
);
643 printf_filtered (_("Contains %d line%s.\n"), s
->nlines
,
644 s
->nlines
== 1 ? "" : "s");
646 printf_filtered (_("Source language is %s.\n"), language_str (s
->language
));
647 printf_filtered (_("Compiled with %s debugging format.\n"), s
->debugformat
);
648 printf_filtered (_("%s preprocessor macro info.\n"),
649 s
->macro_table
? "Includes" : "Does not include");
653 /* Return True if the file NAME exists and is a regular file. */
655 is_regular_file (const char *name
)
658 const int status
= stat (name
, &st
);
660 /* Stat should never fail except when the file does not exist.
661 If stat fails, analyze the source of error and return True
662 unless the file does not exist, to avoid returning false results
663 on obscure systems where stat does not work as expected. */
666 return (errno
!= ENOENT
);
668 return S_ISREG (st
.st_mode
);
671 /* Open a file named STRING, searching path PATH (dir names sep by some char)
672 using mode MODE in the calls to open. You cannot use this function to
673 create files (O_CREAT).
675 OPTS specifies the function behaviour in specific cases.
677 If OPF_TRY_CWD_FIRST, try to open ./STRING before searching PATH.
678 (ie pretend the first element of PATH is "."). This also indicates
679 that a slash in STRING disables searching of the path (this is
680 so that "exec-file ./foo" or "symbol-file ./foo" insures that you
681 get that particular version of foo or an error message).
683 If OPTS has OPF_SEARCH_IN_PATH set, absolute names will also be
684 searched in path (we usually want this for source files but not for
687 If FILENAME_OPENED is non-null, set it to a newly allocated string naming
688 the actual file opened (this string will always start with a "/"). We
689 have to take special pains to avoid doubling the "/" between the directory
690 and the file, sigh! Emacs gets confuzzed by this when we print the
693 If a file is found, return the descriptor.
694 Otherwise, return -1, with errno set for the last name we tried to open. */
696 /* >>>> This should only allow files of certain types,
697 >>>> eg executable, non-directory. */
699 openp (const char *path
, int opts
, const char *string
,
700 int mode
, char **filename_opened
)
705 VEC (char_ptr
) *dir_vec
;
706 struct cleanup
*back_to
;
710 /* The open syscall MODE parameter is not specified. */
711 gdb_assert ((mode
& O_CREAT
) == 0);
712 gdb_assert (string
!= NULL
);
714 /* A file with an empty name cannot possibly exist. Report a failure
715 without further checking.
717 This is an optimization which also defends us against buggy
718 implementations of the "stat" function. For instance, we have
719 noticed that a MinGW debugger built on Windows XP 32bits crashes
720 when the debugger is started with an empty argument. */
721 if (string
[0] == '\0')
732 if ((opts
& OPF_TRY_CWD_FIRST
) || IS_ABSOLUTE_PATH (string
))
736 if (is_regular_file (string
))
738 filename
= alloca (strlen (string
) + 1);
739 strcpy (filename
, string
);
740 fd
= open (filename
, mode
);
750 if (!(opts
& OPF_SEARCH_IN_PATH
))
751 for (i
= 0; string
[i
]; i
++)
752 if (IS_DIR_SEPARATOR (string
[i
]))
756 /* For dos paths, d:/foo -> /foo, and d:foo -> foo. */
757 if (HAS_DRIVE_SPEC (string
))
758 string
= STRIP_DRIVE_SPEC (string
);
760 /* /foo => foo, to avoid multiple slashes that Emacs doesn't like. */
761 while (IS_DIR_SEPARATOR(string
[0]))
765 while (string
[0] == '.' && IS_DIR_SEPARATOR (string
[1]))
768 alloclen
= strlen (path
) + strlen (string
) + 2;
769 filename
= alloca (alloclen
);
772 dir_vec
= dirnames_to_char_ptr_vec (path
);
773 back_to
= make_cleanup_free_char_ptr_vec (dir_vec
);
775 for (ix
= 0; VEC_iterate (char_ptr
, dir_vec
, ix
, dir
); ++ix
)
777 size_t len
= strlen (dir
);
779 if (strcmp (dir
, "$cwd") == 0)
781 /* Name is $cwd -- insert current directory name instead. */
784 /* First, realloc the filename buffer if too short. */
785 len
= strlen (current_directory
);
786 newlen
= len
+ strlen (string
) + 2;
787 if (newlen
> alloclen
)
790 filename
= alloca (alloclen
);
792 strcpy (filename
, current_directory
);
794 else if (strchr(dir
, '~'))
796 /* See whether we need to expand the tilde. */
798 char *tilde_expanded
;
800 tilde_expanded
= tilde_expand (dir
);
802 /* First, realloc the filename buffer if too short. */
803 len
= strlen (tilde_expanded
);
804 newlen
= len
+ strlen (string
) + 2;
805 if (newlen
> alloclen
)
808 filename
= alloca (alloclen
);
810 strcpy (filename
, tilde_expanded
);
811 xfree (tilde_expanded
);
815 /* Normal file name in path -- just use it. */
816 strcpy (filename
, dir
);
818 /* Don't search $cdir. It's also a magic path like $cwd, but we
819 don't have enough information to expand it. The user *could*
820 have an actual directory named '$cdir' but handling that would
821 be confusing, it would mean different things in different
822 contexts. If the user really has '$cdir' one can use './$cdir'.
823 We can get $cdir when loading scripts. When loading source files
824 $cdir must have already been expanded to the correct value. */
825 if (strcmp (dir
, "$cdir") == 0)
829 /* Remove trailing slashes. */
830 while (len
> 0 && IS_DIR_SEPARATOR (filename
[len
- 1]))
833 strcat (filename
+ len
, SLASH_STRING
);
834 strcat (filename
, string
);
836 if (is_regular_file (filename
))
838 fd
= open (filename
, mode
);
844 do_cleanups (back_to
);
849 /* If a file was opened, canonicalize its filename. */
851 *filename_opened
= NULL
;
852 else if (IS_ABSOLUTE_PATH (filename
))
853 *filename_opened
= gdb_realpath (filename
);
856 /* Beware the // my son, the Emacs barfs, the botch that catch... */
858 char *f
= concat (current_directory
,
859 IS_DIR_SEPARATOR (current_directory
[strlen (current_directory
) - 1])
861 filename
, (char *)NULL
);
863 *filename_opened
= gdb_realpath (f
);
872 /* This is essentially a convenience, for clients that want the behaviour
873 of openp, using source_path, but that really don't want the file to be
874 opened but want instead just to know what the full pathname is (as
875 qualified against source_path).
877 The current working directory is searched first.
879 If the file was found, this function returns 1, and FULL_PATHNAME is
880 set to the fully-qualified pathname.
882 Else, this functions returns 0, and FULL_PATHNAME is set to NULL. */
884 source_full_path_of (const char *filename
, char **full_pathname
)
888 fd
= openp (source_path
, OPF_TRY_CWD_FIRST
| OPF_SEARCH_IN_PATH
, filename
,
889 O_RDONLY
, full_pathname
);
892 *full_pathname
= NULL
;
900 /* Return non-zero if RULE matches PATH, that is if the rule can be
904 substitute_path_rule_matches (const struct substitute_path_rule
*rule
,
907 const int from_len
= strlen (rule
->from
);
908 const int path_len
= strlen (path
);
911 if (path_len
< from_len
)
914 /* The substitution rules are anchored at the start of the path,
915 so the path should start with rule->from. There is no filename
916 comparison routine, so we need to extract the first FROM_LEN
917 characters from PATH first and use that to do the comparison. */
919 path_start
= alloca (from_len
+ 1);
920 strncpy (path_start
, path
, from_len
);
921 path_start
[from_len
] = '\0';
923 if (FILENAME_CMP (path_start
, rule
->from
) != 0)
926 /* Make sure that the region in the path that matches the substitution
927 rule is immediately followed by a directory separator (or the end of
928 string character). */
930 if (path
[from_len
] != '\0' && !IS_DIR_SEPARATOR (path
[from_len
]))
936 /* Find the substitute-path rule that applies to PATH and return it.
937 Return NULL if no rule applies. */
939 static struct substitute_path_rule
*
940 get_substitute_path_rule (const char *path
)
942 struct substitute_path_rule
*rule
= substitute_path_rules
;
944 while (rule
!= NULL
&& !substitute_path_rule_matches (rule
, path
))
950 /* If the user specified a source path substitution rule that applies
951 to PATH, then apply it and return the new path. This new path must
952 be deallocated afterwards.
954 Return NULL if no substitution rule was specified by the user,
955 or if no rule applied to the given PATH. */
958 rewrite_source_path (const char *path
)
960 const struct substitute_path_rule
*rule
= get_substitute_path_rule (path
);
967 from_len
= strlen (rule
->from
);
969 /* Compute the rewritten path and return it. */
972 (char *) xmalloc (strlen (path
) + 1 + strlen (rule
->to
) - from_len
);
973 strcpy (new_path
, rule
->to
);
974 strcat (new_path
, path
+ from_len
);
980 find_and_open_source (const char *filename
,
984 char *path
= source_path
;
988 /* Quick way out if we already know its full name. */
992 /* The user may have requested that source paths be rewritten
993 according to substitution rules he provided. If a substitution
994 rule applies to this path, then apply it. */
995 char *rewritten_fullname
= rewrite_source_path (*fullname
);
997 if (rewritten_fullname
!= NULL
)
1000 *fullname
= rewritten_fullname
;
1003 result
= open (*fullname
, OPEN_MODE
);
1006 char *lpath
= gdb_realpath (*fullname
);
1013 /* Didn't work -- free old one, try again. */
1018 if (dirname
!= NULL
)
1020 /* If necessary, rewrite the compilation directory name according
1021 to the source path substitution rules specified by the user. */
1023 char *rewritten_dirname
= rewrite_source_path (dirname
);
1025 if (rewritten_dirname
!= NULL
)
1027 make_cleanup (xfree
, rewritten_dirname
);
1028 dirname
= rewritten_dirname
;
1031 /* Replace a path entry of $cdir with the compilation directory
1034 /* We cast strstr's result in case an ANSIhole has made it const,
1035 which produces a "required warning" when assigned to a nonconst. */
1036 p
= (char *) strstr (source_path
, "$cdir");
1037 if (p
&& (p
== path
|| p
[-1] == DIRNAME_SEPARATOR
)
1038 && (p
[cdir_len
] == DIRNAME_SEPARATOR
|| p
[cdir_len
] == '\0'))
1043 alloca (strlen (source_path
) + 1 + strlen (dirname
) + 1);
1044 len
= p
- source_path
;
1045 strncpy (path
, source_path
, len
); /* Before $cdir */
1046 strcpy (path
+ len
, dirname
); /* new stuff */
1047 strcat (path
+ len
, source_path
+ len
+ cdir_len
); /* After
1052 if (IS_ABSOLUTE_PATH (filename
))
1054 /* If filename is absolute path, try the source path
1055 substitution on it. */
1056 char *rewritten_filename
= rewrite_source_path (filename
);
1058 if (rewritten_filename
!= NULL
)
1060 make_cleanup (xfree
, rewritten_filename
);
1061 filename
= rewritten_filename
;
1065 result
= openp (path
, OPF_SEARCH_IN_PATH
, filename
, OPEN_MODE
, fullname
);
1068 /* Didn't work. Try using just the basename. */
1069 p
= lbasename (filename
);
1071 result
= openp (path
, OPF_SEARCH_IN_PATH
, p
, OPEN_MODE
, fullname
);
1077 /* Open a source file given a symtab S. Returns a file descriptor or
1078 negative number for error.
1080 This function is a convience function to find_and_open_source. */
1083 open_source_file (struct symtab
*s
)
1088 return find_and_open_source (s
->filename
, s
->dirname
, &s
->fullname
);
1091 /* Finds the fullname that a symtab represents.
1093 This functions finds the fullname and saves it in s->fullname.
1094 It will also return the value.
1096 If this function fails to find the file that this symtab represents,
1097 the expected fullname is used. Therefore the files does not have to
1101 symtab_to_fullname (struct symtab
*s
)
1103 /* Use cached copy if we have it.
1104 We rely on forget_cached_source_info being called appropriately
1105 to handle cases like the file being moved. */
1106 if (s
->fullname
== NULL
)
1108 int fd
= find_and_open_source (s
->filename
, s
->dirname
, &s
->fullname
);
1115 struct cleanup
*back_to
;
1117 /* rewrite_source_path would be applied by find_and_open_source, we
1118 should report the pathname where GDB tried to find the file. */
1120 if (s
->dirname
== NULL
|| IS_ABSOLUTE_PATH (s
->filename
))
1121 fullname
= xstrdup (s
->filename
);
1123 fullname
= concat (s
->dirname
, SLASH_STRING
, s
->filename
, NULL
);
1125 back_to
= make_cleanup (xfree
, fullname
);
1126 s
->fullname
= rewrite_source_path (fullname
);
1127 if (s
->fullname
== NULL
)
1128 s
->fullname
= xstrdup (fullname
);
1129 do_cleanups (back_to
);
1136 /* See commentary in source.h. */
1139 symtab_to_filename_for_display (struct symtab
*symtab
)
1141 if (filename_display_string
== filename_display_basename
)
1142 return lbasename (symtab
->filename
);
1143 else if (filename_display_string
== filename_display_absolute
)
1144 return symtab_to_fullname (symtab
);
1145 else if (filename_display_string
== filename_display_relative
)
1146 return symtab
->filename
;
1148 internal_error (__FILE__
, __LINE__
, _("invalid filename_display_string"));
1151 /* Create and initialize the table S->line_charpos that records
1152 the positions of the lines in the source file, which is assumed
1153 to be open on descriptor DESC.
1154 All set S->nlines to the number of such lines. */
1157 find_source_lines (struct symtab
*s
, int desc
)
1160 char *data
, *p
, *end
;
1162 int lines_allocated
= 1000;
1168 line_charpos
= (int *) xmalloc (lines_allocated
* sizeof (int));
1169 if (fstat (desc
, &st
) < 0)
1170 perror_with_name (s
->filename
);
1172 if (s
->objfile
&& s
->objfile
->obfd
)
1173 mtime
= s
->objfile
->mtime
;
1175 mtime
= exec_bfd_mtime
;
1177 if (mtime
&& mtime
< st
.st_mtime
)
1178 warning (_("Source file is more recent than executable."));
1181 struct cleanup
*old_cleanups
;
1183 /* st_size might be a large type, but we only support source files whose
1184 size fits in an int. */
1185 size
= (int) st
.st_size
;
1187 /* Use malloc, not alloca, because this may be pretty large, and we may
1188 run into various kinds of limits on stack size. */
1189 data
= (char *) xmalloc (size
);
1190 old_cleanups
= make_cleanup (xfree
, data
);
1192 /* Reassign `size' to result of read for systems where \r\n -> \n. */
1193 size
= myread (desc
, data
, size
);
1195 perror_with_name (s
->filename
);
1198 line_charpos
[0] = 0;
1203 /* A newline at the end does not start a new line. */
1206 if (nlines
== lines_allocated
)
1208 lines_allocated
*= 2;
1210 (int *) xrealloc ((char *) line_charpos
,
1211 sizeof (int) * lines_allocated
);
1213 line_charpos
[nlines
++] = p
- data
;
1216 do_cleanups (old_cleanups
);
1221 (int *) xrealloc ((char *) line_charpos
, nlines
* sizeof (int));
1227 /* Get full pathname and line number positions for a symtab.
1228 Return nonzero if line numbers may have changed.
1229 Set *FULLNAME to actual name of the file as found by `openp',
1230 or to 0 if the file is not found. */
1233 get_filename_and_charpos (struct symtab
*s
, char **fullname
)
1235 int desc
, linenums_changed
= 0;
1236 struct cleanup
*cleanups
;
1238 desc
= open_source_file (s
);
1245 cleanups
= make_cleanup_close (desc
);
1247 *fullname
= s
->fullname
;
1248 if (s
->line_charpos
== 0)
1249 linenums_changed
= 1;
1250 if (linenums_changed
)
1251 find_source_lines (s
, desc
);
1252 do_cleanups (cleanups
);
1253 return linenums_changed
;
1256 /* Print text describing the full name of the source file S
1257 and the line number LINE and its corresponding character position.
1258 The text starts with two Ctrl-z so that the Emacs-GDB interface
1261 MID_STATEMENT is nonzero if the PC is not at the beginning of that line.
1263 Return 1 if successful, 0 if could not find the file. */
1266 identify_source_line (struct symtab
*s
, int line
, int mid_statement
,
1269 if (s
->line_charpos
== 0)
1270 get_filename_and_charpos (s
, (char **) NULL
);
1271 if (s
->fullname
== 0)
1273 if (line
> s
->nlines
)
1274 /* Don't index off the end of the line_charpos array. */
1276 annotate_source (s
->fullname
, line
, s
->line_charpos
[line
- 1],
1277 mid_statement
, get_objfile_arch (s
->objfile
), pc
);
1279 current_source_line
= line
;
1280 first_line_listed
= line
;
1281 last_line_listed
= line
;
1282 current_source_symtab
= s
;
1287 /* Print source lines from the file of symtab S,
1288 starting with line number LINE and stopping before line number STOPLINE. */
1291 print_source_lines_base (struct symtab
*s
, int line
, int stopline
,
1292 enum print_source_lines_flags flags
)
1298 int nlines
= stopline
- line
;
1299 struct cleanup
*cleanup
;
1300 struct ui_out
*uiout
= current_uiout
;
1302 /* Regardless of whether we can open the file, set current_source_symtab. */
1303 current_source_symtab
= s
;
1304 current_source_line
= line
;
1305 first_line_listed
= line
;
1307 /* If printing of source lines is disabled, just print file and line
1309 if (ui_out_test_flags (uiout
, ui_source_list
))
1311 /* Only prints "No such file or directory" once. */
1312 if ((s
!= last_source_visited
) || (!last_source_error
))
1314 last_source_visited
= s
;
1315 desc
= open_source_file (s
);
1319 desc
= last_source_error
;
1320 flags
|= PRINT_SOURCE_LINES_NOERROR
;
1325 desc
= last_source_error
;
1326 flags
|= PRINT_SOURCE_LINES_NOERROR
;
1330 if (desc
< 0 || noprint
)
1332 last_source_error
= desc
;
1334 if (!(flags
& PRINT_SOURCE_LINES_NOERROR
))
1336 int len
= strlen (s
->filename
) + 100;
1337 char *name
= alloca (len
);
1339 xsnprintf (name
, len
, "%d\t%s", line
, s
->filename
);
1340 print_sys_errmsg (name
, errno
);
1344 ui_out_field_int (uiout
, "line", line
);
1345 ui_out_text (uiout
, "\tin ");
1346 ui_out_field_string (uiout
, "file", s
->filename
);
1347 if (ui_out_is_mi_like_p (uiout
))
1349 const char *fullname
= symtab_to_fullname (s
);
1351 ui_out_field_string (uiout
, "fullname", fullname
);
1353 ui_out_text (uiout
, "\n");
1359 last_source_error
= 0;
1361 if (s
->line_charpos
== 0)
1362 find_source_lines (s
, desc
);
1364 if (line
< 1 || line
> s
->nlines
)
1367 error (_("Line number %d out of range; %s has %d lines."),
1368 line
, s
->filename
, s
->nlines
);
1371 if (lseek (desc
, s
->line_charpos
[line
- 1], 0) < 0)
1374 perror_with_name (s
->filename
);
1377 stream
= fdopen (desc
, FDOPEN_MODE
);
1379 cleanup
= make_cleanup_fclose (stream
);
1381 while (nlines
-- > 0)
1388 last_line_listed
= current_source_line
;
1389 if (flags
& PRINT_SOURCE_LINES_FILENAME
)
1391 ui_out_text (uiout
, s
->filename
);
1392 ui_out_text (uiout
, ":");
1394 xsnprintf (buf
, sizeof (buf
), "%d\t", current_source_line
++);
1395 ui_out_text (uiout
, buf
);
1398 if (c
< 040 && c
!= '\t' && c
!= '\n' && c
!= '\r')
1400 xsnprintf (buf
, sizeof (buf
), "^%c", c
+ 0100);
1401 ui_out_text (uiout
, buf
);
1404 ui_out_text (uiout
, "^?");
1407 /* Skip a \r character, but only before a \n. */
1408 int c1
= fgetc (stream
);
1411 printf_filtered ("^%c", c
+ 0100);
1413 ungetc (c1
, stream
);
1417 xsnprintf (buf
, sizeof (buf
), "%c", c
);
1418 ui_out_text (uiout
, buf
);
1421 while (c
!= '\n' && (c
= fgetc (stream
)) >= 0);
1424 do_cleanups (cleanup
);
1427 /* Show source lines from the file of symtab S, starting with line
1428 number LINE and stopping before line number STOPLINE. If this is
1429 not the command line version, then the source is shown in the source
1430 window otherwise it is simply printed. */
1433 print_source_lines (struct symtab
*s
, int line
, int stopline
,
1434 enum print_source_lines_flags flags
)
1436 print_source_lines_base (s
, line
, stopline
, flags
);
1439 /* Print info on range of pc's in a specified line. */
1442 line_info (char *arg
, int from_tty
)
1444 struct symtabs_and_lines sals
;
1445 struct symtab_and_line sal
;
1446 CORE_ADDR start_pc
, end_pc
;
1448 struct cleanup
*cleanups
;
1450 init_sal (&sal
); /* initialize to zeroes */
1454 sal
.symtab
= current_source_symtab
;
1455 sal
.pspace
= current_program_space
;
1456 sal
.line
= last_line_listed
;
1458 sals
.sals
= (struct symtab_and_line
*)
1459 xmalloc (sizeof (struct symtab_and_line
));
1464 sals
= decode_line_with_last_displayed (arg
, DECODE_LINE_LIST_MODE
);
1469 cleanups
= make_cleanup (xfree
, sals
.sals
);
1471 /* C++ More than one line may have been specified, as when the user
1472 specifies an overloaded function name. Print info on them all. */
1473 for (i
= 0; i
< sals
.nelts
; i
++)
1476 if (sal
.pspace
!= current_program_space
)
1479 if (sal
.symtab
== 0)
1481 struct gdbarch
*gdbarch
= get_current_arch ();
1483 printf_filtered (_("No line number information available"));
1486 /* This is useful for "info line *0x7f34". If we can't tell the
1487 user about a source line, at least let them have the symbolic
1489 printf_filtered (" for address ");
1491 print_address (gdbarch
, sal
.pc
, gdb_stdout
);
1494 printf_filtered (".");
1495 printf_filtered ("\n");
1497 else if (sal
.line
> 0
1498 && find_line_pc_range (sal
, &start_pc
, &end_pc
))
1500 struct gdbarch
*gdbarch
= get_objfile_arch (sal
.symtab
->objfile
);
1502 if (start_pc
== end_pc
)
1504 printf_filtered ("Line %d of \"%s\"",
1505 sal
.line
, sal
.symtab
->filename
);
1507 printf_filtered (" is at address ");
1508 print_address (gdbarch
, start_pc
, gdb_stdout
);
1510 printf_filtered (" but contains no code.\n");
1514 printf_filtered ("Line %d of \"%s\"",
1515 sal
.line
, sal
.symtab
->filename
);
1517 printf_filtered (" starts at address ");
1518 print_address (gdbarch
, start_pc
, gdb_stdout
);
1520 printf_filtered (" and ends at ");
1521 print_address (gdbarch
, end_pc
, gdb_stdout
);
1522 printf_filtered (".\n");
1525 /* x/i should display this line's code. */
1526 set_next_address (gdbarch
, start_pc
);
1528 /* Repeating "info line" should do the following line. */
1529 last_line_listed
= sal
.line
+ 1;
1531 /* If this is the only line, show the source code. If it could
1532 not find the file, don't do anything special. */
1533 if (annotation_level
&& sals
.nelts
== 1)
1534 identify_source_line (sal
.symtab
, sal
.line
, 0, start_pc
);
1537 /* Is there any case in which we get here, and have an address
1538 which the user would want to see? If we have debugging symbols
1539 and no line numbers? */
1540 printf_filtered (_("Line number %d is out of range for \"%s\".\n"),
1541 sal
.line
, sal
.symtab
->filename
);
1543 do_cleanups (cleanups
);
1546 /* Commands to search the source file for a regexp. */
1549 forward_search_command (char *regex
, int from_tty
)
1556 struct cleanup
*cleanups
;
1558 line
= last_line_listed
+ 1;
1560 msg
= (char *) re_comp (regex
);
1562 error (("%s"), msg
);
1564 if (current_source_symtab
== 0)
1565 select_source_symtab (0);
1567 desc
= open_source_file (current_source_symtab
);
1569 perror_with_name (current_source_symtab
->filename
);
1570 cleanups
= make_cleanup_close (desc
);
1572 if (current_source_symtab
->line_charpos
== 0)
1573 find_source_lines (current_source_symtab
, desc
);
1575 if (line
< 1 || line
> current_source_symtab
->nlines
)
1576 error (_("Expression not found"));
1578 if (lseek (desc
, current_source_symtab
->line_charpos
[line
- 1], 0) < 0)
1579 perror_with_name (current_source_symtab
->filename
);
1581 discard_cleanups (cleanups
);
1582 stream
= fdopen (desc
, FDOPEN_MODE
);
1584 cleanups
= make_cleanup_fclose (stream
);
1587 static char *buf
= NULL
;
1589 int cursize
, newsize
;
1592 buf
= xmalloc (cursize
);
1601 if (p
- buf
== cursize
)
1603 newsize
= cursize
+ cursize
/ 2;
1604 buf
= xrealloc (buf
, newsize
);
1609 while (c
!= '\n' && (c
= getc (stream
)) >= 0);
1611 /* Remove the \r, if any, at the end of the line, otherwise
1612 regular expressions that end with $ or \n won't work. */
1613 if (p
- buf
> 1 && p
[-2] == '\r')
1619 /* We now have a source line in buf, null terminate and match. */
1621 if (re_exec (buf
) > 0)
1624 do_cleanups (cleanups
);
1625 print_source_lines (current_source_symtab
, line
, line
+ 1, 0);
1626 set_internalvar_integer (lookup_internalvar ("_"), line
);
1627 current_source_line
= max (line
- lines_to_list
/ 2, 1);
1633 printf_filtered (_("Expression not found\n"));
1634 do_cleanups (cleanups
);
1638 reverse_search_command (char *regex
, int from_tty
)
1645 struct cleanup
*cleanups
;
1647 line
= last_line_listed
- 1;
1649 msg
= (char *) re_comp (regex
);
1651 error (("%s"), msg
);
1653 if (current_source_symtab
== 0)
1654 select_source_symtab (0);
1656 desc
= open_source_file (current_source_symtab
);
1658 perror_with_name (current_source_symtab
->filename
);
1659 cleanups
= make_cleanup_close (desc
);
1661 if (current_source_symtab
->line_charpos
== 0)
1662 find_source_lines (current_source_symtab
, desc
);
1664 if (line
< 1 || line
> current_source_symtab
->nlines
)
1665 error (_("Expression not found"));
1667 if (lseek (desc
, current_source_symtab
->line_charpos
[line
- 1], 0) < 0)
1668 perror_with_name (current_source_symtab
->filename
);
1670 discard_cleanups (cleanups
);
1671 stream
= fdopen (desc
, FDOPEN_MODE
);
1673 cleanups
= make_cleanup_fclose (stream
);
1676 /* FIXME!!! We walk right off the end of buf if we get a long line!!! */
1677 char buf
[4096]; /* Should be reasonable??? */
1687 while (c
!= '\n' && (c
= getc (stream
)) >= 0);
1689 /* Remove the \r, if any, at the end of the line, otherwise
1690 regular expressions that end with $ or \n won't work. */
1691 if (p
- buf
> 1 && p
[-2] == '\r')
1697 /* We now have a source line in buf; null terminate and match. */
1699 if (re_exec (buf
) > 0)
1702 do_cleanups (cleanups
);
1703 print_source_lines (current_source_symtab
, line
, line
+ 1, 0);
1704 set_internalvar_integer (lookup_internalvar ("_"), line
);
1705 current_source_line
= max (line
- lines_to_list
/ 2, 1);
1709 if (fseek (stream
, current_source_symtab
->line_charpos
[line
- 1], 0) < 0)
1711 do_cleanups (cleanups
);
1712 perror_with_name (current_source_symtab
->filename
);
1716 printf_filtered (_("Expression not found\n"));
1717 do_cleanups (cleanups
);
1721 /* If the last character of PATH is a directory separator, then strip it. */
1724 strip_trailing_directory_separator (char *path
)
1726 const int last
= strlen (path
) - 1;
1729 return; /* No stripping is needed if PATH is the empty string. */
1731 if (IS_DIR_SEPARATOR (path
[last
]))
1735 /* Return the path substitution rule that matches FROM.
1736 Return NULL if no rule matches. */
1738 static struct substitute_path_rule
*
1739 find_substitute_path_rule (const char *from
)
1741 struct substitute_path_rule
*rule
= substitute_path_rules
;
1743 while (rule
!= NULL
)
1745 if (FILENAME_CMP (rule
->from
, from
) == 0)
1753 /* Add a new substitute-path rule at the end of the current list of rules.
1754 The new rule will replace FROM into TO. */
1757 add_substitute_path_rule (char *from
, char *to
)
1759 struct substitute_path_rule
*rule
;
1760 struct substitute_path_rule
*new_rule
;
1762 new_rule
= xmalloc (sizeof (struct substitute_path_rule
));
1763 new_rule
->from
= xstrdup (from
);
1764 new_rule
->to
= xstrdup (to
);
1765 new_rule
->next
= NULL
;
1767 /* If the list of rules are empty, then insert the new rule
1768 at the head of the list. */
1770 if (substitute_path_rules
== NULL
)
1772 substitute_path_rules
= new_rule
;
1776 /* Otherwise, skip to the last rule in our list and then append
1779 rule
= substitute_path_rules
;
1780 while (rule
->next
!= NULL
)
1783 rule
->next
= new_rule
;
1786 /* Remove the given source path substitution rule from the current list
1787 of rules. The memory allocated for that rule is also deallocated. */
1790 delete_substitute_path_rule (struct substitute_path_rule
*rule
)
1792 if (rule
== substitute_path_rules
)
1793 substitute_path_rules
= rule
->next
;
1796 struct substitute_path_rule
*prev
= substitute_path_rules
;
1798 while (prev
!= NULL
&& prev
->next
!= rule
)
1801 gdb_assert (prev
!= NULL
);
1803 prev
->next
= rule
->next
;
1811 /* Implement the "show substitute-path" command. */
1814 show_substitute_path_command (char *args
, int from_tty
)
1816 struct substitute_path_rule
*rule
= substitute_path_rules
;
1820 argv
= gdb_buildargv (args
);
1821 make_cleanup_freeargv (argv
);
1823 /* We expect zero or one argument. */
1825 if (argv
!= NULL
&& argv
[0] != NULL
&& argv
[1] != NULL
)
1826 error (_("Too many arguments in command"));
1828 if (argv
!= NULL
&& argv
[0] != NULL
)
1831 /* Print the substitution rules. */
1835 (_("Source path substitution rule matching `%s':\n"), from
);
1837 printf_filtered (_("List of all source path substitution rules:\n"));
1839 while (rule
!= NULL
)
1841 if (from
== NULL
|| FILENAME_CMP (rule
->from
, from
) == 0)
1842 printf_filtered (" `%s' -> `%s'.\n", rule
->from
, rule
->to
);
1847 /* Implement the "unset substitute-path" command. */
1850 unset_substitute_path_command (char *args
, int from_tty
)
1852 struct substitute_path_rule
*rule
= substitute_path_rules
;
1853 char **argv
= gdb_buildargv (args
);
1857 /* This function takes either 0 or 1 argument. */
1859 make_cleanup_freeargv (argv
);
1860 if (argv
!= NULL
&& argv
[0] != NULL
&& argv
[1] != NULL
)
1861 error (_("Incorrect usage, too many arguments in command"));
1863 if (argv
!= NULL
&& argv
[0] != NULL
)
1866 /* If the user asked for all the rules to be deleted, ask him
1867 to confirm and give him a chance to abort before the action
1871 && !query (_("Delete all source path substitution rules? ")))
1872 error (_("Canceled"));
1874 /* Delete the rule matching the argument. No argument means that
1875 all rules should be deleted. */
1877 while (rule
!= NULL
)
1879 struct substitute_path_rule
*next
= rule
->next
;
1881 if (from
== NULL
|| FILENAME_CMP (from
, rule
->from
) == 0)
1883 delete_substitute_path_rule (rule
);
1890 /* If the user asked for a specific rule to be deleted but
1891 we could not find it, then report an error. */
1893 if (from
!= NULL
&& !rule_found
)
1894 error (_("No substitution rule defined for `%s'"), from
);
1896 forget_cached_source_info ();
1899 /* Add a new source path substitution rule. */
1902 set_substitute_path_command (char *args
, int from_tty
)
1905 struct substitute_path_rule
*rule
;
1907 argv
= gdb_buildargv (args
);
1908 make_cleanup_freeargv (argv
);
1910 if (argv
== NULL
|| argv
[0] == NULL
|| argv
[1] == NULL
)
1911 error (_("Incorrect usage, too few arguments in command"));
1913 if (argv
[2] != NULL
)
1914 error (_("Incorrect usage, too many arguments in command"));
1916 if (*(argv
[0]) == '\0')
1917 error (_("First argument must be at least one character long"));
1919 /* Strip any trailing directory separator character in either FROM
1920 or TO. The substitution rule already implicitly contains them. */
1921 strip_trailing_directory_separator (argv
[0]);
1922 strip_trailing_directory_separator (argv
[1]);
1924 /* If a rule with the same "from" was previously defined, then
1925 delete it. This new rule replaces it. */
1927 rule
= find_substitute_path_rule (argv
[0]);
1929 delete_substitute_path_rule (rule
);
1931 /* Insert the new substitution rule. */
1933 add_substitute_path_rule (argv
[0], argv
[1]);
1934 forget_cached_source_info ();
1939 _initialize_source (void)
1941 struct cmd_list_element
*c
;
1943 current_source_symtab
= 0;
1944 init_source_path ();
1946 /* The intention is to use POSIX Basic Regular Expressions.
1947 Always use the GNU regex routine for consistency across all hosts.
1948 Our current GNU regex.c does not have all the POSIX features, so this is
1949 just an approximation. */
1950 re_set_syntax (RE_SYNTAX_GREP
);
1952 c
= add_cmd ("directory", class_files
, directory_command
, _("\
1953 Add directory DIR to beginning of search path for source files.\n\
1954 Forget cached info on source file locations and line positions.\n\
1955 DIR can also be $cwd for the current working directory, or $cdir for the\n\
1956 directory in which the source file was compiled into object code.\n\
1957 With no argument, reset the search path to $cdir:$cwd, the default."),
1961 add_com_alias ("use", "directory", class_files
, 0);
1963 set_cmd_completer (c
, filename_completer
);
1965 add_setshow_optional_filename_cmd ("directories",
1969 Set the search path for finding source files."),
1971 Show the search path for finding source files."),
1973 $cwd in the path means the current working directory.\n\
1974 $cdir in the path means the compilation directory of the source file.\n\
1975 GDB ensures the search path always ends with $cdir:$cwd by\n\
1976 appending these directories if necessary.\n\
1977 Setting the value to an empty string sets it to $cdir:$cwd, the default."),
1978 set_directories_command
,
1979 show_directories_command
,
1980 &setlist
, &showlist
);
1984 add_com_alias ("D", "directory", class_files
, 0);
1985 add_cmd ("ld", no_class
, show_directories_1
, _("\
1986 Current search path for finding source files.\n\
1987 $cwd in the path means the current working directory.\n\
1988 $cdir in the path means the compilation directory of the source file."),
1992 add_info ("source", source_info
,
1993 _("Information about the current source file."));
1995 add_info ("line", line_info
, _("\
1996 Core addresses of the code for a source line.\n\
1997 Line can be specified as\n\
1998 LINENUM, to list around that line in current file,\n\
1999 FILE:LINENUM, to list around that line in that file,\n\
2000 FUNCTION, to list around beginning of that function,\n\
2001 FILE:FUNCTION, to distinguish among like-named static functions.\n\
2002 Default is to describe the last source line that was listed.\n\n\
2003 This sets the default address for \"x\" to the line's first instruction\n\
2004 so that \"x/i\" suffices to start examining the machine code.\n\
2005 The address is also stored as the value of \"$_\"."));
2007 add_com ("forward-search", class_files
, forward_search_command
, _("\
2008 Search for regular expression (see regex(3)) from last line listed.\n\
2009 The matching line number is also stored as the value of \"$_\"."));
2010 add_com_alias ("search", "forward-search", class_files
, 0);
2011 add_com_alias ("fo", "forward-search", class_files
, 1);
2013 add_com ("reverse-search", class_files
, reverse_search_command
, _("\
2014 Search backward for regular expression (see regex(3)) from last line listed.\n\
2015 The matching line number is also stored as the value of \"$_\"."));
2016 add_com_alias ("rev", "reverse-search", class_files
, 1);
2020 add_com_alias ("/", "forward-search", class_files
, 0);
2021 add_com_alias ("?", "reverse-search", class_files
, 0);
2024 add_setshow_zuinteger_unlimited_cmd ("listsize", class_support
,
2025 &lines_to_list
, _("\
2026 Set number of source lines gdb will list by default."), _("\
2027 Show number of source lines gdb will list by default."), NULL
,
2028 NULL
, show_lines_to_list
,
2029 &setlist
, &showlist
);
2031 add_cmd ("substitute-path", class_files
, set_substitute_path_command
,
2033 Usage: set substitute-path FROM TO\n\
2034 Add a substitution rule replacing FROM into TO in source file names.\n\
2035 If a substitution rule was previously set for FROM, the old rule\n\
2036 is replaced by the new one."),
2039 add_cmd ("substitute-path", class_files
, unset_substitute_path_command
,
2041 Usage: unset substitute-path [FROM]\n\
2042 Delete the rule for substituting FROM in source file names. If FROM\n\
2043 is not specified, all substituting rules are deleted.\n\
2044 If the debugger cannot find a rule for FROM, it will display a warning."),
2047 add_cmd ("substitute-path", class_files
, show_substitute_path_command
,
2049 Usage: show substitute-path [FROM]\n\
2050 Print the rule for substituting FROM in source file names. If FROM\n\
2051 is not specified, print all substitution rules."),
2054 add_setshow_enum_cmd ("filename-display", class_files
,
2055 filename_display_kind_names
,
2056 &filename_display_string
, _("\
2057 Set how to display filenames."), _("\
2058 Show how to display filenames."), _("\
2059 filename-display can be:\n\
2060 basename - display only basename of a filename\n\
2061 relative - display a filename relative to the compilation directory\n\
2062 absolute - display an absolute filename\n\
2063 By default, relative filenames are displayed."),
2065 show_filename_display_string
,
2066 &setlist
, &showlist
);