kernel NFS - Fix another deadlock in the readdirplus code
[dragonfly.git] / contrib / gdb-7 / gdb / source.c
blob7a2829bf77772c8138d72e64e3b1ce87d8cc4de5
1 /* List lines of source files for GDB, the GNU debugger.
2 Copyright (C) 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995,
3 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2007, 2008,
4 2009 Free Software Foundation, Inc.
6 This file is part of GDB.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
21 #include "defs.h"
22 #include "arch-utils.h"
23 #include "symtab.h"
24 #include "expression.h"
25 #include "language.h"
26 #include "command.h"
27 #include "source.h"
28 #include "gdbcmd.h"
29 #include "frame.h"
30 #include "value.h"
31 #include "gdb_assert.h"
33 #include <sys/types.h>
34 #include "gdb_string.h"
35 #include "gdb_stat.h"
36 #include <fcntl.h>
37 #include "gdbcore.h"
38 #include "gdb_regex.h"
39 #include "symfile.h"
40 #include "objfiles.h"
41 #include "annotate.h"
42 #include "gdbtypes.h"
43 #include "linespec.h"
44 #include "filenames.h" /* for DOSish file names */
45 #include "completer.h"
46 #include "ui-out.h"
47 #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 static void show_directories (char *, int);
71 /* Path of directories to search for source files.
72 Same format as the PATH environment variable's value. */
74 char *source_path;
76 /* Support for source path substitution commands. */
78 struct substitute_path_rule
80 char *from;
81 char *to;
82 struct substitute_path_rule *next;
85 static struct substitute_path_rule *substitute_path_rules = NULL;
87 /* Symtab of default file for listing lines of. */
89 static struct symtab *current_source_symtab;
91 /* Default next line to list. */
93 static int current_source_line;
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;
102 static void
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 will list by default is %s.\n"),
108 value);
111 /* Line number of last line printed. Default for various commands.
112 current_source_line is usually, but not always, the same as this. */
114 static int last_line_listed;
116 /* First line number listed by last listing command. */
118 static int first_line_listed;
120 /* Saves the name of the last source file visited and a possible error code.
121 Used to prevent repeating annoying "No such file or directories" msgs */
123 static struct symtab *last_source_visited = NULL;
124 static int last_source_error = 0;
126 /* Return the first line listed by print_source_lines.
127 Used by command interpreters to request listing from
128 a previous point. */
131 get_first_line_listed (void)
133 return first_line_listed;
136 /* Return the default number of lines to print with commands like the
137 cli "list". The caller of print_source_lines must use this to
138 calculate the end line and use it in the call to print_source_lines
139 as it does not automatically use this value. */
142 get_lines_to_list (void)
144 return lines_to_list;
147 /* Return the current source file for listing and next line to list.
148 NOTE: The returned sal pc and end fields are not valid. */
150 struct symtab_and_line
151 get_current_source_symtab_and_line (void)
153 struct symtab_and_line cursal = { 0 };
155 cursal.symtab = current_source_symtab;
156 cursal.line = current_source_line;
157 cursal.pc = 0;
158 cursal.end = 0;
160 return cursal;
163 /* If the current source file for listing is not set, try and get a default.
164 Usually called before get_current_source_symtab_and_line() is called.
165 It may err out if a default cannot be determined.
166 We must be cautious about where it is called, as it can recurse as the
167 process of determining a new default may call the caller!
168 Use get_current_source_symtab_and_line only to get whatever
169 we have without erroring out or trying to get a default. */
171 void
172 set_default_source_symtab_and_line (void)
174 struct symtab_and_line cursal;
176 if (!have_full_symbols () && !have_partial_symbols ())
177 error (_("No symbol table is loaded. Use the \"file\" command."));
179 /* Pull in a current source symtab if necessary */
180 if (current_source_symtab == 0)
181 select_source_symtab (0);
184 /* Return the current default file for listing and next line to list
185 (the returned sal pc and end fields are not valid.)
186 and set the current default to whatever is in SAL.
187 NOTE: The returned sal pc and end fields are not valid. */
189 struct symtab_and_line
190 set_current_source_symtab_and_line (const struct symtab_and_line *sal)
192 struct symtab_and_line cursal = { 0 };
194 cursal.symtab = current_source_symtab;
195 cursal.line = current_source_line;
197 current_source_symtab = sal->symtab;
198 current_source_line = sal->line;
199 cursal.pc = 0;
200 cursal.end = 0;
202 return cursal;
205 /* Reset any information stored about a default file and line to print. */
207 void
208 clear_current_source_symtab_and_line (void)
210 current_source_symtab = 0;
211 current_source_line = 0;
214 /* Set the source file default for the "list" command to be S.
216 If S is NULL, and we don't have a default, find one. This
217 should only be called when the user actually tries to use the
218 default, since we produce an error if we can't find a reasonable
219 default. Also, since this can cause symbols to be read, doing it
220 before we need to would make things slower than necessary. */
222 void
223 select_source_symtab (struct symtab *s)
225 struct symtabs_and_lines sals;
226 struct symtab_and_line sal;
227 struct partial_symtab *ps;
228 struct partial_symtab *cs_pst = 0;
229 struct objfile *ofp;
231 if (s)
233 current_source_symtab = s;
234 current_source_line = 1;
235 return;
238 if (current_source_symtab)
239 return;
241 /* Make the default place to list be the function `main'
242 if one exists. */
243 if (lookup_symbol (main_name (), 0, VAR_DOMAIN, 0))
245 sals = decode_line_spec (main_name (), 1);
246 sal = sals.sals[0];
247 xfree (sals.sals);
248 current_source_symtab = sal.symtab;
249 current_source_line = max (sal.line - (lines_to_list - 1), 1);
250 if (current_source_symtab)
251 return;
254 /* Alright; find the last file in the symtab list (ignoring .h's
255 and namespace symtabs). */
257 current_source_line = 1;
259 for (ofp = object_files; ofp != NULL; ofp = ofp->next)
261 for (s = ofp->symtabs; s; s = s->next)
263 const char *name = s->filename;
264 int len = strlen (name);
265 if (!(len > 2 && (strcmp (&name[len - 2], ".h") == 0
266 || strcmp (name, "<<C++-namespaces>>") == 0)))
267 current_source_symtab = s;
270 if (current_source_symtab)
271 return;
273 /* How about the partial symbol tables? */
275 for (ofp = object_files; ofp != NULL; ofp = ofp->next)
277 for (ps = ofp->psymtabs; ps != NULL; ps = ps->next)
279 const char *name = ps->filename;
280 int len = strlen (name);
281 if (!(len > 2 && (strcmp (&name[len - 2], ".h") == 0
282 || strcmp (name, "<<C++-namespaces>>") == 0)))
283 cs_pst = ps;
286 if (cs_pst)
288 if (cs_pst->readin)
290 internal_error (__FILE__, __LINE__,
291 _("select_source_symtab: "
292 "readin pst found and no symtabs."));
294 else
296 current_source_symtab = PSYMTAB_TO_SYMTAB (cs_pst);
299 if (current_source_symtab)
300 return;
302 error (_("Can't find a default source file"));
305 static void
306 show_directories (char *ignore, int from_tty)
308 puts_filtered ("Source directories searched: ");
309 puts_filtered (source_path);
310 puts_filtered ("\n");
313 /* Forget what we learned about line positions in source files, and
314 which directories contain them; must check again now since files
315 may be found in a different directory now. */
317 void
318 forget_cached_source_info (void)
320 struct symtab *s;
321 struct objfile *objfile;
322 struct partial_symtab *pst;
324 for (objfile = object_files; objfile != NULL; objfile = objfile->next)
326 for (s = objfile->symtabs; s != NULL; s = s->next)
328 if (s->line_charpos != NULL)
330 xfree (s->line_charpos);
331 s->line_charpos = NULL;
333 if (s->fullname != NULL)
335 xfree (s->fullname);
336 s->fullname = NULL;
340 ALL_OBJFILE_PSYMTABS (objfile, pst)
342 if (pst->fullname != NULL)
344 xfree (pst->fullname);
345 pst->fullname = NULL;
350 last_source_visited = NULL;
353 void
354 init_source_path (void)
356 char buf[20];
358 sprintf (buf, "$cdir%c$cwd", DIRNAME_SEPARATOR);
359 source_path = xstrdup (buf);
360 forget_cached_source_info ();
363 /* Add zero or more directories to the front of the source path. */
365 void
366 directory_command (char *dirname, int from_tty)
368 dont_repeat ();
369 /* FIXME, this goes to "delete dir"... */
370 if (dirname == 0)
372 if (!from_tty || query (_("Reinitialize source path to empty? ")))
374 xfree (source_path);
375 init_source_path ();
378 else
380 mod_path (dirname, &source_path);
381 forget_cached_source_info ();
383 if (from_tty)
384 show_directories ((char *) 0, from_tty);
387 /* Add a path given with the -d command line switch.
388 This will not be quoted so we must not treat spaces as separators. */
390 void
391 directory_switch (char *dirname, int from_tty)
393 add_path (dirname, &source_path, 0);
396 /* Add zero or more directories to the front of an arbitrary path. */
398 void
399 mod_path (char *dirname, char **which_path)
401 add_path (dirname, which_path, 1);
404 /* Workhorse of mod_path. Takes an extra argument to determine
405 if dirname should be parsed for separators that indicate multiple
406 directories. This allows for interfaces that pre-parse the dirname
407 and allow specification of traditional separator characters such
408 as space or tab. */
410 void
411 add_path (char *dirname, char **which_path, int parse_separators)
413 char *old = *which_path;
414 int prefix = 0;
415 char **argv = NULL;
416 char *arg;
417 int argv_index = 0;
419 if (dirname == 0)
420 return;
422 if (parse_separators)
424 /* This will properly parse the space and tab separators
425 and any quotes that may exist. DIRNAME_SEPARATOR will
426 be dealt with later. */
427 argv = gdb_buildargv (dirname);
428 make_cleanup_freeargv (argv);
430 arg = argv[0];
432 else
434 arg = xstrdup (dirname);
435 make_cleanup (xfree, arg);
440 char *name = arg;
441 char *p;
442 struct stat st;
445 char *separator = NULL;
447 /* Spaces and tabs will have been removed by buildargv().
448 The directories will there be split into a list but
449 each entry may still contain DIRNAME_SEPARATOR. */
450 if (parse_separators)
451 separator = strchr (name, DIRNAME_SEPARATOR);
453 if (separator == 0)
454 p = arg = name + strlen (name);
455 else
457 p = separator;
458 arg = p + 1;
459 while (*arg == DIRNAME_SEPARATOR)
460 ++arg;
463 /* If there are no more directories in this argument then start
464 on the next argument next time round the loop (if any). */
465 if (*arg == '\0')
466 arg = parse_separators ? argv[++argv_index] : NULL;
469 /* name is the start of the directory.
470 p is the separator (or null) following the end. */
472 while (!(IS_DIR_SEPARATOR (*name) && p <= name + 1) /* "/" */
473 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
474 /* On MS-DOS and MS-Windows, h:\ is different from h: */
475 && !(p == name + 3 && name[1] == ':') /* "d:/" */
476 #endif
477 && IS_DIR_SEPARATOR (p[-1]))
478 /* Sigh. "foo/" => "foo" */
479 --p;
480 *p = '\0';
482 while (p > name && p[-1] == '.')
484 if (p - name == 1)
486 /* "." => getwd (). */
487 name = current_directory;
488 goto append;
490 else if (p > name + 1 && IS_DIR_SEPARATOR (p[-2]))
492 if (p - name == 2)
494 /* "/." => "/". */
495 *--p = '\0';
496 goto append;
498 else
500 /* "...foo/." => "...foo". */
501 p -= 2;
502 *p = '\0';
503 continue;
506 else
507 break;
510 if (name[0] == '~')
511 name = tilde_expand (name);
512 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
513 else if (IS_ABSOLUTE_PATH (name) && p == name + 2) /* "d:" => "d:." */
514 name = concat (name, ".", (char *)NULL);
515 #endif
516 else if (!IS_ABSOLUTE_PATH (name) && name[0] != '$')
517 name = concat (current_directory, SLASH_STRING, name, (char *)NULL);
518 else
519 name = savestring (name, p - name);
520 make_cleanup (xfree, name);
522 /* Unless it's a variable, check existence. */
523 if (name[0] != '$')
525 /* These are warnings, not errors, since we don't want a
526 non-existent directory in a .gdbinit file to stop processing
527 of the .gdbinit file.
529 Whether they get added to the path is more debatable. Current
530 answer is yes, in case the user wants to go make the directory
531 or whatever. If the directory continues to not exist/not be
532 a directory/etc, then having them in the path should be
533 harmless. */
534 if (stat (name, &st) < 0)
536 int save_errno = errno;
537 fprintf_unfiltered (gdb_stderr, "Warning: ");
538 print_sys_errmsg (name, save_errno);
540 else if ((st.st_mode & S_IFMT) != S_IFDIR)
541 warning (_("%s is not a directory."), name);
544 append:
546 unsigned int len = strlen (name);
548 p = *which_path;
549 while (1)
551 /* FIXME: strncmp loses in interesting ways on MS-DOS and
552 MS-Windows because of case-insensitivity and two different
553 but functionally identical slash characters. We need a
554 special filesystem-dependent file-name comparison function.
556 Actually, even on Unix I would use realpath() or its work-
557 alike before comparing. Then all the code above which
558 removes excess slashes and dots could simply go away. */
559 if (!strncmp (p, name, len)
560 && (p[len] == '\0' || p[len] == DIRNAME_SEPARATOR))
562 /* Found it in the search path, remove old copy */
563 if (p > *which_path)
564 p--; /* Back over leading separator */
565 if (prefix > p - *which_path)
566 goto skip_dup; /* Same dir twice in one cmd */
567 strcpy (p, &p[len + 1]); /* Copy from next \0 or : */
569 p = strchr (p, DIRNAME_SEPARATOR);
570 if (p != 0)
571 ++p;
572 else
573 break;
575 if (p == 0)
577 char tinybuf[2];
579 tinybuf[0] = DIRNAME_SEPARATOR;
580 tinybuf[1] = '\0';
582 /* If we have already tacked on a name(s) in this command, be sure they stay
583 on the front as we tack on some more. */
584 if (prefix)
586 char *temp, c;
588 c = old[prefix];
589 old[prefix] = '\0';
590 temp = concat (old, tinybuf, name, (char *)NULL);
591 old[prefix] = c;
592 *which_path = concat (temp, "", &old[prefix], (char *)NULL);
593 prefix = strlen (temp);
594 xfree (temp);
596 else
598 *which_path = concat (name, (old[0] ? tinybuf : old),
599 old, (char *)NULL);
600 prefix = strlen (name);
602 xfree (old);
603 old = *which_path;
606 skip_dup:;
608 while (arg != NULL);
612 static void
613 source_info (char *ignore, int from_tty)
615 struct symtab *s = current_source_symtab;
617 if (!s)
619 printf_filtered (_("No current source file.\n"));
620 return;
622 printf_filtered (_("Current source file is %s\n"), s->filename);
623 if (s->dirname)
624 printf_filtered (_("Compilation directory is %s\n"), s->dirname);
625 if (s->fullname)
626 printf_filtered (_("Located in %s\n"), s->fullname);
627 if (s->nlines)
628 printf_filtered (_("Contains %d line%s.\n"), s->nlines,
629 s->nlines == 1 ? "" : "s");
631 printf_filtered (_("Source language is %s.\n"), language_str (s->language));
632 printf_filtered (_("Compiled with %s debugging format.\n"), s->debugformat);
633 printf_filtered (_("%s preprocessor macro info.\n"),
634 s->macro_table ? "Includes" : "Does not include");
638 /* Return True if the file NAME exists and is a regular file */
639 static int
640 is_regular_file (const char *name)
642 struct stat st;
643 const int status = stat (name, &st);
645 /* Stat should never fail except when the file does not exist.
646 If stat fails, analyze the source of error and return True
647 unless the file does not exist, to avoid returning false results
648 on obscure systems where stat does not work as expected.
650 if (status != 0)
651 return (errno != ENOENT);
653 return S_ISREG (st.st_mode);
656 /* Open a file named STRING, searching path PATH (dir names sep by some char)
657 using mode MODE in the calls to open. You cannot use this function to
658 create files (O_CREAT).
660 OPTS specifies the function behaviour in specific cases.
662 If OPF_TRY_CWD_FIRST, try to open ./STRING before searching PATH.
663 (ie pretend the first element of PATH is "."). This also indicates
664 that a slash in STRING disables searching of the path (this is
665 so that "exec-file ./foo" or "symbol-file ./foo" insures that you
666 get that particular version of foo or an error message).
668 If OPTS has OPF_SEARCH_IN_PATH set, absolute names will also be
669 searched in path (we usually want this for source files but not for
670 executables).
672 If FILENAME_OPENED is non-null, set it to a newly allocated string naming
673 the actual file opened (this string will always start with a "/"). We
674 have to take special pains to avoid doubling the "/" between the directory
675 and the file, sigh! Emacs gets confuzzed by this when we print the
676 source file name!!!
678 If a file is found, return the descriptor.
679 Otherwise, return -1, with errno set for the last name we tried to open. */
681 /* >>>> This should only allow files of certain types,
682 >>>> eg executable, non-directory */
684 openp (const char *path, int opts, const char *string,
685 int mode, char **filename_opened)
687 int fd;
688 char *filename;
689 const char *p;
690 const char *p1;
691 int len;
692 int alloclen;
694 /* The open syscall MODE parameter is not specified. */
695 gdb_assert ((mode & O_CREAT) == 0);
697 if (!path)
698 path = ".";
700 mode |= O_BINARY;
702 if ((opts & OPF_TRY_CWD_FIRST) || IS_ABSOLUTE_PATH (string))
704 int i;
706 if (is_regular_file (string))
708 filename = alloca (strlen (string) + 1);
709 strcpy (filename, string);
710 fd = open (filename, mode);
711 if (fd >= 0)
712 goto done;
714 else
716 filename = NULL;
717 fd = -1;
720 if (!(opts & OPF_SEARCH_IN_PATH))
721 for (i = 0; string[i]; i++)
722 if (IS_DIR_SEPARATOR (string[i]))
723 goto done;
726 /* /foo => foo, to avoid multiple slashes that Emacs doesn't like. */
727 while (IS_DIR_SEPARATOR(string[0]))
728 string++;
730 /* ./foo => foo */
731 while (string[0] == '.' && IS_DIR_SEPARATOR (string[1]))
732 string += 2;
734 alloclen = strlen (path) + strlen (string) + 2;
735 filename = alloca (alloclen);
736 fd = -1;
737 for (p = path; p; p = p1 ? p1 + 1 : 0)
739 p1 = strchr (p, DIRNAME_SEPARATOR);
740 if (p1)
741 len = p1 - p;
742 else
743 len = strlen (p);
745 if (len == 4 && p[0] == '$' && p[1] == 'c'
746 && p[2] == 'w' && p[3] == 'd')
748 /* Name is $cwd -- insert current directory name instead. */
749 int newlen;
751 /* First, realloc the filename buffer if too short. */
752 len = strlen (current_directory);
753 newlen = len + strlen (string) + 2;
754 if (newlen > alloclen)
756 alloclen = newlen;
757 filename = alloca (alloclen);
759 strcpy (filename, current_directory);
761 else
763 /* Normal file name in path -- just use it. */
764 strncpy (filename, p, len);
765 filename[len] = 0;
768 /* Remove trailing slashes */
769 while (len > 0 && IS_DIR_SEPARATOR (filename[len - 1]))
770 filename[--len] = 0;
772 strcat (filename + len, SLASH_STRING);
773 strcat (filename, string);
775 if (is_regular_file (filename))
777 fd = open (filename, mode);
778 if (fd >= 0)
779 break;
783 done:
784 if (filename_opened)
786 /* If a file was opened, canonicalize its filename. Use xfullpath
787 rather than gdb_realpath to avoid resolving the basename part
788 of filenames when the associated file is a symbolic link. This
789 fixes a potential inconsistency between the filenames known to
790 GDB and the filenames it prints in the annotations. */
791 if (fd < 0)
792 *filename_opened = NULL;
793 else if (IS_ABSOLUTE_PATH (filename))
794 *filename_opened = xfullpath (filename);
795 else
797 /* Beware the // my son, the Emacs barfs, the botch that catch... */
799 char *f = concat (current_directory,
800 IS_DIR_SEPARATOR (current_directory[strlen (current_directory) - 1])
801 ? "" : SLASH_STRING,
802 filename, (char *)NULL);
803 *filename_opened = xfullpath (f);
804 xfree (f);
808 return fd;
812 /* This is essentially a convenience, for clients that want the behaviour
813 of openp, using source_path, but that really don't want the file to be
814 opened but want instead just to know what the full pathname is (as
815 qualified against source_path).
817 The current working directory is searched first.
819 If the file was found, this function returns 1, and FULL_PATHNAME is
820 set to the fully-qualified pathname.
822 Else, this functions returns 0, and FULL_PATHNAME is set to NULL. */
824 source_full_path_of (const char *filename, char **full_pathname)
826 int fd;
828 fd = openp (source_path, OPF_TRY_CWD_FIRST | OPF_SEARCH_IN_PATH, filename,
829 O_RDONLY, full_pathname);
830 if (fd < 0)
832 *full_pathname = NULL;
833 return 0;
836 close (fd);
837 return 1;
840 /* Return non-zero if RULE matches PATH, that is if the rule can be
841 applied to PATH. */
843 static int
844 substitute_path_rule_matches (const struct substitute_path_rule *rule,
845 const char *path)
847 const int from_len = strlen (rule->from);
848 const int path_len = strlen (path);
849 char *path_start;
851 if (path_len < from_len)
852 return 0;
854 /* The substitution rules are anchored at the start of the path,
855 so the path should start with rule->from. There is no filename
856 comparison routine, so we need to extract the first FROM_LEN
857 characters from PATH first and use that to do the comparison. */
859 path_start = alloca (from_len + 1);
860 strncpy (path_start, path, from_len);
861 path_start[from_len] = '\0';
863 if (FILENAME_CMP (path_start, rule->from) != 0)
864 return 0;
866 /* Make sure that the region in the path that matches the substitution
867 rule is immediately followed by a directory separator (or the end of
868 string character). */
870 if (path[from_len] != '\0' && !IS_DIR_SEPARATOR (path[from_len]))
871 return 0;
873 return 1;
876 /* Find the substitute-path rule that applies to PATH and return it.
877 Return NULL if no rule applies. */
879 static struct substitute_path_rule *
880 get_substitute_path_rule (const char *path)
882 struct substitute_path_rule *rule = substitute_path_rules;
884 while (rule != NULL && !substitute_path_rule_matches (rule, path))
885 rule = rule->next;
887 return rule;
890 /* If the user specified a source path substitution rule that applies
891 to PATH, then apply it and return the new path. This new path must
892 be deallocated afterwards.
894 Return NULL if no substitution rule was specified by the user,
895 or if no rule applied to the given PATH. */
897 static char *
898 rewrite_source_path (const char *path)
900 const struct substitute_path_rule *rule = get_substitute_path_rule (path);
901 char *new_path;
902 int from_len;
904 if (rule == NULL)
905 return NULL;
907 from_len = strlen (rule->from);
909 /* Compute the rewritten path and return it. */
911 new_path =
912 (char *) xmalloc (strlen (path) + 1 + strlen (rule->to) - from_len);
913 strcpy (new_path, rule->to);
914 strcat (new_path, path + from_len);
916 return new_path;
919 /* This function is capable of finding the absolute path to a
920 source file, and opening it, provided you give it a FILENAME. Both the
921 DIRNAME and FULLNAME are only added suggestions on where to find the file.
923 FILENAME should be the filename to open.
924 DIRNAME is the compilation directory of a particular source file.
925 Only some debug formats provide this info.
926 FULLNAME can be the last known absolute path to the file in question.
927 Space for the path must have been malloc'd. If a path substitution
928 is applied we free the old value and set a new one.
930 On Success
931 A valid file descriptor is returned. ( the return value is positive )
932 FULLNAME is set to the absolute path to the file just opened.
933 The caller is responsible for freeing FULLNAME.
935 On Failure
936 An invalid file descriptor is returned. ( the return value is negative )
937 FULLNAME is set to NULL. */
939 static int
940 find_and_open_source (const char *filename,
941 const char *dirname,
942 char **fullname)
944 char *path = source_path;
945 const char *p;
946 int result;
948 /* Quick way out if we already know its full name */
950 if (*fullname)
952 /* The user may have requested that source paths be rewritten
953 according to substitution rules he provided. If a substitution
954 rule applies to this path, then apply it. */
955 char *rewritten_fullname = rewrite_source_path (*fullname);
957 if (rewritten_fullname != NULL)
959 xfree (*fullname);
960 *fullname = rewritten_fullname;
963 result = open (*fullname, OPEN_MODE);
964 if (result >= 0)
965 return result;
966 /* Didn't work -- free old one, try again. */
967 xfree (*fullname);
968 *fullname = NULL;
971 if (dirname != NULL)
973 /* If necessary, rewrite the compilation directory name according
974 to the source path substitution rules specified by the user. */
976 char *rewritten_dirname = rewrite_source_path (dirname);
978 if (rewritten_dirname != NULL)
980 make_cleanup (xfree, rewritten_dirname);
981 dirname = rewritten_dirname;
984 /* Replace a path entry of $cdir with the compilation directory name */
985 #define cdir_len 5
986 /* We cast strstr's result in case an ANSIhole has made it const,
987 which produces a "required warning" when assigned to a nonconst. */
988 p = (char *) strstr (source_path, "$cdir");
989 if (p && (p == path || p[-1] == DIRNAME_SEPARATOR)
990 && (p[cdir_len] == DIRNAME_SEPARATOR || p[cdir_len] == '\0'))
992 int len;
994 path = (char *)
995 alloca (strlen (source_path) + 1 + strlen (dirname) + 1);
996 len = p - source_path;
997 strncpy (path, source_path, len); /* Before $cdir */
998 strcpy (path + len, dirname); /* new stuff */
999 strcat (path + len, source_path + len + cdir_len); /* After $cdir */
1003 if (IS_ABSOLUTE_PATH (filename))
1005 /* If filename is absolute path, try the source path
1006 substitution on it. */
1007 char *rewritten_filename = rewrite_source_path (filename);
1009 if (rewritten_filename != NULL)
1011 make_cleanup (xfree, rewritten_filename);
1012 filename = rewritten_filename;
1016 result = openp (path, OPF_SEARCH_IN_PATH, filename, OPEN_MODE, fullname);
1017 if (result < 0)
1019 /* Didn't work. Try using just the basename. */
1020 p = lbasename (filename);
1021 if (p != filename)
1022 result = openp (path, OPF_SEARCH_IN_PATH, p, OPEN_MODE, fullname);
1025 return result;
1028 /* Open a source file given a symtab S. Returns a file descriptor or
1029 negative number for error.
1031 This function is a convience function to find_and_open_source. */
1034 open_source_file (struct symtab *s)
1036 if (!s)
1037 return -1;
1039 return find_and_open_source (s->filename, s->dirname, &s->fullname);
1042 /* Finds the fullname that a symtab represents.
1044 If this functions finds the fullname, it will save it in s->fullname
1045 and it will also return the value.
1047 If this function fails to find the file that this symtab represents,
1048 NULL will be returned and s->fullname will be set to NULL. */
1049 char *
1050 symtab_to_fullname (struct symtab *s)
1052 int r;
1054 if (!s)
1055 return NULL;
1057 /* Don't check s->fullname here, the file could have been
1058 deleted/moved/..., look for it again */
1059 r = find_and_open_source (s->filename, s->dirname, &s->fullname);
1061 if (r >= 0)
1063 close (r);
1064 return s->fullname;
1067 return NULL;
1070 /* Finds the fullname that a partial_symtab represents.
1072 If this functions finds the fullname, it will save it in ps->fullname
1073 and it will also return the value.
1075 If this function fails to find the file that this partial_symtab represents,
1076 NULL will be returned and ps->fullname will be set to NULL. */
1077 char *
1078 psymtab_to_fullname (struct partial_symtab *ps)
1080 int r;
1082 if (!ps)
1083 return NULL;
1085 /* Don't check ps->fullname here, the file could have been
1086 deleted/moved/..., look for it again */
1087 r = find_and_open_source (ps->filename, ps->dirname, &ps->fullname);
1089 if (r >= 0)
1091 close (r);
1092 return ps->fullname;
1095 return NULL;
1098 /* Create and initialize the table S->line_charpos that records
1099 the positions of the lines in the source file, which is assumed
1100 to be open on descriptor DESC.
1101 All set S->nlines to the number of such lines. */
1103 void
1104 find_source_lines (struct symtab *s, int desc)
1106 struct stat st;
1107 char *data, *p, *end;
1108 int nlines = 0;
1109 int lines_allocated = 1000;
1110 int *line_charpos;
1111 long mtime = 0;
1112 int size;
1114 gdb_assert (s);
1115 line_charpos = (int *) xmalloc (lines_allocated * sizeof (int));
1116 if (fstat (desc, &st) < 0)
1117 perror_with_name (s->filename);
1119 if (s->objfile && s->objfile->obfd)
1120 mtime = s->objfile->mtime;
1121 else if (exec_bfd)
1122 mtime = exec_bfd_mtime;
1124 if (mtime && mtime < st.st_mtime)
1125 warning (_("Source file is more recent than executable."));
1127 #ifdef LSEEK_NOT_LINEAR
1129 char c;
1131 /* Have to read it byte by byte to find out where the chars live */
1133 line_charpos[0] = lseek (desc, 0, SEEK_CUR);
1134 nlines = 1;
1135 while (myread (desc, &c, 1) > 0)
1137 if (c == '\n')
1139 if (nlines == lines_allocated)
1141 lines_allocated *= 2;
1142 line_charpos =
1143 (int *) xrealloc ((char *) line_charpos,
1144 sizeof (int) * lines_allocated);
1146 line_charpos[nlines++] = lseek (desc, 0, SEEK_CUR);
1150 #else /* lseek linear. */
1152 struct cleanup *old_cleanups;
1154 /* st_size might be a large type, but we only support source files whose
1155 size fits in an int. */
1156 size = (int) st.st_size;
1158 /* Use malloc, not alloca, because this may be pretty large, and we may
1159 run into various kinds of limits on stack size. */
1160 data = (char *) xmalloc (size);
1161 old_cleanups = make_cleanup (xfree, data);
1163 /* Reassign `size' to result of read for systems where \r\n -> \n. */
1164 size = myread (desc, data, size);
1165 if (size < 0)
1166 perror_with_name (s->filename);
1167 end = data + size;
1168 p = data;
1169 line_charpos[0] = 0;
1170 nlines = 1;
1171 while (p != end)
1173 if (*p++ == '\n'
1174 /* A newline at the end does not start a new line. */
1175 && p != end)
1177 if (nlines == lines_allocated)
1179 lines_allocated *= 2;
1180 line_charpos =
1181 (int *) xrealloc ((char *) line_charpos,
1182 sizeof (int) * lines_allocated);
1184 line_charpos[nlines++] = p - data;
1187 do_cleanups (old_cleanups);
1189 #endif /* lseek linear. */
1190 s->nlines = nlines;
1191 s->line_charpos =
1192 (int *) xrealloc ((char *) line_charpos, nlines * sizeof (int));
1196 /* Return the character position of a line LINE in symtab S.
1197 Return 0 if anything is invalid. */
1199 #if 0 /* Currently unused */
1202 source_line_charpos (struct symtab *s, int line)
1204 if (!s)
1205 return 0;
1206 if (!s->line_charpos || line <= 0)
1207 return 0;
1208 if (line > s->nlines)
1209 line = s->nlines;
1210 return s->line_charpos[line - 1];
1213 /* Return the line number of character position POS in symtab S. */
1216 source_charpos_line (struct symtab *s, int chr)
1218 int line = 0;
1219 int *lnp;
1221 if (s == 0 || s->line_charpos == 0)
1222 return 0;
1223 lnp = s->line_charpos;
1224 /* Files are usually short, so sequential search is Ok */
1225 while (line < s->nlines && *lnp <= chr)
1227 line++;
1228 lnp++;
1230 if (line >= s->nlines)
1231 line = s->nlines;
1232 return line;
1235 #endif /* 0 */
1238 /* Get full pathname and line number positions for a symtab.
1239 Return nonzero if line numbers may have changed.
1240 Set *FULLNAME to actual name of the file as found by `openp',
1241 or to 0 if the file is not found. */
1243 static int
1244 get_filename_and_charpos (struct symtab *s, char **fullname)
1246 int desc, linenums_changed = 0;
1247 struct cleanup *cleanups;
1249 desc = open_source_file (s);
1250 if (desc < 0)
1252 if (fullname)
1253 *fullname = NULL;
1254 return 0;
1256 cleanups = make_cleanup_close (desc);
1257 if (fullname)
1258 *fullname = s->fullname;
1259 if (s->line_charpos == 0)
1260 linenums_changed = 1;
1261 if (linenums_changed)
1262 find_source_lines (s, desc);
1263 do_cleanups (cleanups);
1264 return linenums_changed;
1267 /* Print text describing the full name of the source file S
1268 and the line number LINE and its corresponding character position.
1269 The text starts with two Ctrl-z so that the Emacs-GDB interface
1270 can easily find it.
1272 MID_STATEMENT is nonzero if the PC is not at the beginning of that line.
1274 Return 1 if successful, 0 if could not find the file. */
1277 identify_source_line (struct symtab *s, int line, int mid_statement,
1278 CORE_ADDR pc)
1280 if (s->line_charpos == 0)
1281 get_filename_and_charpos (s, (char **) NULL);
1282 if (s->fullname == 0)
1283 return 0;
1284 if (line > s->nlines)
1285 /* Don't index off the end of the line_charpos array. */
1286 return 0;
1287 annotate_source (s->fullname, line, s->line_charpos[line - 1],
1288 mid_statement, get_objfile_arch (s->objfile), pc);
1290 current_source_line = line;
1291 first_line_listed = line;
1292 last_line_listed = line;
1293 current_source_symtab = s;
1294 return 1;
1298 /* Print source lines from the file of symtab S,
1299 starting with line number LINE and stopping before line number STOPLINE. */
1301 static void print_source_lines_base (struct symtab *s, int line, int stopline,
1302 int noerror);
1303 static void
1304 print_source_lines_base (struct symtab *s, int line, int stopline, int noerror)
1306 int c;
1307 int desc;
1308 FILE *stream;
1309 int nlines = stopline - line;
1310 struct cleanup *cleanup;
1312 /* Regardless of whether we can open the file, set current_source_symtab. */
1313 current_source_symtab = s;
1314 current_source_line = line;
1315 first_line_listed = line;
1317 /* If printing of source lines is disabled, just print file and line number */
1318 if (ui_out_test_flags (uiout, ui_source_list))
1320 /* Only prints "No such file or directory" once */
1321 if ((s != last_source_visited) || (!last_source_error))
1323 last_source_visited = s;
1324 desc = open_source_file (s);
1326 else
1328 desc = last_source_error;
1329 noerror = 1;
1332 else
1334 desc = -1;
1335 noerror = 1;
1338 if (desc < 0)
1340 last_source_error = desc;
1342 if (!noerror)
1344 char *name = alloca (strlen (s->filename) + 100);
1345 sprintf (name, "%d\t%s", line, s->filename);
1346 print_sys_errmsg (name, errno);
1348 else
1349 ui_out_field_int (uiout, "line", line);
1350 ui_out_text (uiout, "\tin ");
1351 ui_out_field_string (uiout, "file", s->filename);
1352 ui_out_text (uiout, "\n");
1354 return;
1357 last_source_error = 0;
1359 if (s->line_charpos == 0)
1360 find_source_lines (s, desc);
1362 if (line < 1 || line > s->nlines)
1364 close (desc);
1365 error (_("Line number %d out of range; %s has %d lines."),
1366 line, s->filename, s->nlines);
1369 if (lseek (desc, s->line_charpos[line - 1], 0) < 0)
1371 close (desc);
1372 perror_with_name (s->filename);
1375 stream = fdopen (desc, FDOPEN_MODE);
1376 clearerr (stream);
1377 cleanup = make_cleanup_fclose (stream);
1379 while (nlines-- > 0)
1381 char buf[20];
1383 c = fgetc (stream);
1384 if (c == EOF)
1385 break;
1386 last_line_listed = current_source_line;
1387 sprintf (buf, "%d\t", current_source_line++);
1388 ui_out_text (uiout, buf);
1391 if (c < 040 && c != '\t' && c != '\n' && c != '\r')
1393 sprintf (buf, "^%c", c + 0100);
1394 ui_out_text (uiout, buf);
1396 else if (c == 0177)
1397 ui_out_text (uiout, "^?");
1398 else if (c == '\r')
1400 /* Skip a \r character, but only before a \n. */
1401 int c1 = fgetc (stream);
1403 if (c1 != '\n')
1404 printf_filtered ("^%c", c + 0100);
1405 if (c1 != EOF)
1406 ungetc (c1, stream);
1408 else
1410 sprintf (buf, "%c", c);
1411 ui_out_text (uiout, buf);
1414 while (c != '\n' && (c = fgetc (stream)) >= 0);
1417 do_cleanups (cleanup);
1420 /* Show source lines from the file of symtab S, starting with line
1421 number LINE and stopping before line number STOPLINE. If this is
1422 not the command line version, then the source is shown in the source
1423 window otherwise it is simply printed */
1425 void
1426 print_source_lines (struct symtab *s, int line, int stopline, int noerror)
1428 print_source_lines_base (s, line, stopline, noerror);
1431 /* Print info on range of pc's in a specified line. */
1433 static void
1434 line_info (char *arg, int from_tty)
1436 struct symtabs_and_lines sals;
1437 struct symtab_and_line sal;
1438 CORE_ADDR start_pc, end_pc;
1439 int i;
1441 init_sal (&sal); /* initialize to zeroes */
1443 if (arg == 0)
1445 sal.symtab = current_source_symtab;
1446 sal.line = last_line_listed;
1447 sals.nelts = 1;
1448 sals.sals = (struct symtab_and_line *)
1449 xmalloc (sizeof (struct symtab_and_line));
1450 sals.sals[0] = sal;
1452 else
1454 sals = decode_line_spec_1 (arg, 0);
1456 dont_repeat ();
1459 /* C++ More than one line may have been specified, as when the user
1460 specifies an overloaded function name. Print info on them all. */
1461 for (i = 0; i < sals.nelts; i++)
1463 sal = sals.sals[i];
1465 if (sal.symtab == 0)
1467 struct gdbarch *gdbarch = get_current_arch ();
1469 printf_filtered (_("No line number information available"));
1470 if (sal.pc != 0)
1472 /* This is useful for "info line *0x7f34". If we can't tell the
1473 user about a source line, at least let them have the symbolic
1474 address. */
1475 printf_filtered (" for address ");
1476 wrap_here (" ");
1477 print_address (gdbarch, sal.pc, gdb_stdout);
1479 else
1480 printf_filtered (".");
1481 printf_filtered ("\n");
1483 else if (sal.line > 0
1484 && find_line_pc_range (sal, &start_pc, &end_pc))
1486 struct gdbarch *gdbarch = get_objfile_arch (sal.symtab->objfile);
1488 if (start_pc == end_pc)
1490 printf_filtered ("Line %d of \"%s\"",
1491 sal.line, sal.symtab->filename);
1492 wrap_here (" ");
1493 printf_filtered (" is at address ");
1494 print_address (gdbarch, start_pc, gdb_stdout);
1495 wrap_here (" ");
1496 printf_filtered (" but contains no code.\n");
1498 else
1500 printf_filtered ("Line %d of \"%s\"",
1501 sal.line, sal.symtab->filename);
1502 wrap_here (" ");
1503 printf_filtered (" starts at address ");
1504 print_address (gdbarch, start_pc, gdb_stdout);
1505 wrap_here (" ");
1506 printf_filtered (" and ends at ");
1507 print_address (gdbarch, end_pc, gdb_stdout);
1508 printf_filtered (".\n");
1511 /* x/i should display this line's code. */
1512 set_next_address (gdbarch, start_pc);
1514 /* Repeating "info line" should do the following line. */
1515 last_line_listed = sal.line + 1;
1517 /* If this is the only line, show the source code. If it could
1518 not find the file, don't do anything special. */
1519 if (annotation_level && sals.nelts == 1)
1520 identify_source_line (sal.symtab, sal.line, 0, start_pc);
1522 else
1523 /* Is there any case in which we get here, and have an address
1524 which the user would want to see? If we have debugging symbols
1525 and no line numbers? */
1526 printf_filtered (_("Line number %d is out of range for \"%s\".\n"),
1527 sal.line, sal.symtab->filename);
1529 xfree (sals.sals);
1532 /* Commands to search the source file for a regexp. */
1534 static void
1535 forward_search_command (char *regex, int from_tty)
1537 int c;
1538 int desc;
1539 FILE *stream;
1540 int line;
1541 char *msg;
1542 struct cleanup *cleanups;
1544 line = last_line_listed + 1;
1546 msg = (char *) re_comp (regex);
1547 if (msg)
1548 error (("%s"), msg);
1550 if (current_source_symtab == 0)
1551 select_source_symtab (0);
1553 desc = open_source_file (current_source_symtab);
1554 if (desc < 0)
1555 perror_with_name (current_source_symtab->filename);
1556 cleanups = make_cleanup_close (desc);
1558 if (current_source_symtab->line_charpos == 0)
1559 find_source_lines (current_source_symtab, desc);
1561 if (line < 1 || line > current_source_symtab->nlines)
1562 error (_("Expression not found"));
1564 if (lseek (desc, current_source_symtab->line_charpos[line - 1], 0) < 0)
1565 perror_with_name (current_source_symtab->filename);
1567 discard_cleanups (cleanups);
1568 stream = fdopen (desc, FDOPEN_MODE);
1569 clearerr (stream);
1570 cleanups = make_cleanup_fclose (stream);
1571 while (1)
1573 static char *buf = NULL;
1574 char *p;
1575 int cursize, newsize;
1577 cursize = 256;
1578 buf = xmalloc (cursize);
1579 p = buf;
1581 c = getc (stream);
1582 if (c == EOF)
1583 break;
1586 *p++ = c;
1587 if (p - buf == cursize)
1589 newsize = cursize + cursize / 2;
1590 buf = xrealloc (buf, newsize);
1591 p = buf + cursize;
1592 cursize = newsize;
1595 while (c != '\n' && (c = getc (stream)) >= 0);
1597 /* Remove the \r, if any, at the end of the line, otherwise
1598 regular expressions that end with $ or \n won't work. */
1599 if (p - buf > 1 && p[-2] == '\r')
1601 p--;
1602 p[-1] = '\n';
1605 /* we now have a source line in buf, null terminate and match */
1606 *p = 0;
1607 if (re_exec (buf) > 0)
1609 /* Match! */
1610 do_cleanups (cleanups);
1611 print_source_lines (current_source_symtab, line, line + 1, 0);
1612 set_internalvar_integer (lookup_internalvar ("_"), line);
1613 current_source_line = max (line - lines_to_list / 2, 1);
1614 return;
1616 line++;
1619 printf_filtered (_("Expression not found\n"));
1620 do_cleanups (cleanups);
1623 static void
1624 reverse_search_command (char *regex, int from_tty)
1626 int c;
1627 int desc;
1628 FILE *stream;
1629 int line;
1630 char *msg;
1631 struct cleanup *cleanups;
1633 line = last_line_listed - 1;
1635 msg = (char *) re_comp (regex);
1636 if (msg)
1637 error (("%s"), msg);
1639 if (current_source_symtab == 0)
1640 select_source_symtab (0);
1642 desc = open_source_file (current_source_symtab);
1643 if (desc < 0)
1644 perror_with_name (current_source_symtab->filename);
1645 cleanups = make_cleanup_close (desc);
1647 if (current_source_symtab->line_charpos == 0)
1648 find_source_lines (current_source_symtab, desc);
1650 if (line < 1 || line > current_source_symtab->nlines)
1651 error (_("Expression not found"));
1653 if (lseek (desc, current_source_symtab->line_charpos[line - 1], 0) < 0)
1654 perror_with_name (current_source_symtab->filename);
1656 discard_cleanups (cleanups);
1657 stream = fdopen (desc, FDOPEN_MODE);
1658 clearerr (stream);
1659 cleanups = make_cleanup_fclose (stream);
1660 while (line > 1)
1662 /* FIXME!!! We walk right off the end of buf if we get a long line!!! */
1663 char buf[4096]; /* Should be reasonable??? */
1664 char *p = buf;
1666 c = getc (stream);
1667 if (c == EOF)
1668 break;
1671 *p++ = c;
1673 while (c != '\n' && (c = getc (stream)) >= 0);
1675 /* Remove the \r, if any, at the end of the line, otherwise
1676 regular expressions that end with $ or \n won't work. */
1677 if (p - buf > 1 && p[-2] == '\r')
1679 p--;
1680 p[-1] = '\n';
1683 /* We now have a source line in buf; null terminate and match. */
1684 *p = 0;
1685 if (re_exec (buf) > 0)
1687 /* Match! */
1688 do_cleanups (cleanups);
1689 print_source_lines (current_source_symtab, line, line + 1, 0);
1690 set_internalvar_integer (lookup_internalvar ("_"), line);
1691 current_source_line = max (line - lines_to_list / 2, 1);
1692 return;
1694 line--;
1695 if (fseek (stream, current_source_symtab->line_charpos[line - 1], 0) < 0)
1697 do_cleanups (cleanups);
1698 perror_with_name (current_source_symtab->filename);
1702 printf_filtered (_("Expression not found\n"));
1703 do_cleanups (cleanups);
1704 return;
1707 /* If the last character of PATH is a directory separator, then strip it. */
1709 static void
1710 strip_trailing_directory_separator (char *path)
1712 const int last = strlen (path) - 1;
1714 if (last < 0)
1715 return; /* No stripping is needed if PATH is the empty string. */
1717 if (IS_DIR_SEPARATOR (path[last]))
1718 path[last] = '\0';
1721 /* Return the path substitution rule that matches FROM.
1722 Return NULL if no rule matches. */
1724 static struct substitute_path_rule *
1725 find_substitute_path_rule (const char *from)
1727 struct substitute_path_rule *rule = substitute_path_rules;
1729 while (rule != NULL)
1731 if (FILENAME_CMP (rule->from, from) == 0)
1732 return rule;
1733 rule = rule->next;
1736 return NULL;
1739 /* Add a new substitute-path rule at the end of the current list of rules.
1740 The new rule will replace FROM into TO. */
1742 void
1743 add_substitute_path_rule (char *from, char *to)
1745 struct substitute_path_rule *rule;
1746 struct substitute_path_rule *new_rule;
1748 new_rule = xmalloc (sizeof (struct substitute_path_rule));
1749 new_rule->from = xstrdup (from);
1750 new_rule->to = xstrdup (to);
1751 new_rule->next = NULL;
1753 /* If the list of rules are empty, then insert the new rule
1754 at the head of the list. */
1756 if (substitute_path_rules == NULL)
1758 substitute_path_rules = new_rule;
1759 return;
1762 /* Otherwise, skip to the last rule in our list and then append
1763 the new rule. */
1765 rule = substitute_path_rules;
1766 while (rule->next != NULL)
1767 rule = rule->next;
1769 rule->next = new_rule;
1772 /* Remove the given source path substitution rule from the current list
1773 of rules. The memory allocated for that rule is also deallocated. */
1775 static void
1776 delete_substitute_path_rule (struct substitute_path_rule *rule)
1778 if (rule == substitute_path_rules)
1779 substitute_path_rules = rule->next;
1780 else
1782 struct substitute_path_rule *prev = substitute_path_rules;
1784 while (prev != NULL && prev->next != rule)
1785 prev = prev->next;
1787 gdb_assert (prev != NULL);
1789 prev->next = rule->next;
1792 xfree (rule->from);
1793 xfree (rule->to);
1794 xfree (rule);
1797 /* Implement the "show substitute-path" command. */
1799 static void
1800 show_substitute_path_command (char *args, int from_tty)
1802 struct substitute_path_rule *rule = substitute_path_rules;
1803 char **argv;
1804 char *from = NULL;
1806 argv = gdb_buildargv (args);
1807 make_cleanup_freeargv (argv);
1809 /* We expect zero or one argument. */
1811 if (argv != NULL && argv[0] != NULL && argv[1] != NULL)
1812 error (_("Too many arguments in command"));
1814 if (argv != NULL && argv[0] != NULL)
1815 from = argv[0];
1817 /* Print the substitution rules. */
1819 if (from != NULL)
1820 printf_filtered
1821 (_("Source path substitution rule matching `%s':\n"), from);
1822 else
1823 printf_filtered (_("List of all source path substitution rules:\n"));
1825 while (rule != NULL)
1827 if (from == NULL || FILENAME_CMP (rule->from, from) == 0)
1828 printf_filtered (" `%s' -> `%s'.\n", rule->from, rule->to);
1829 rule = rule->next;
1833 /* Implement the "unset substitute-path" command. */
1835 static void
1836 unset_substitute_path_command (char *args, int from_tty)
1838 struct substitute_path_rule *rule = substitute_path_rules;
1839 char **argv = gdb_buildargv (args);
1840 char *from = NULL;
1841 int rule_found = 0;
1843 /* This function takes either 0 or 1 argument. */
1845 make_cleanup_freeargv (argv);
1846 if (argv != NULL && argv[0] != NULL && argv[1] != NULL)
1847 error (_("Incorrect usage, too many arguments in command"));
1849 if (argv != NULL && argv[0] != NULL)
1850 from = argv[0];
1852 /* If the user asked for all the rules to be deleted, ask him
1853 to confirm and give him a chance to abort before the action
1854 is performed. */
1856 if (from == NULL
1857 && !query (_("Delete all source path substitution rules? ")))
1858 error (_("Canceled"));
1860 /* Delete the rule matching the argument. No argument means that
1861 all rules should be deleted. */
1863 while (rule != NULL)
1865 struct substitute_path_rule *next = rule->next;
1867 if (from == NULL || FILENAME_CMP (from, rule->from) == 0)
1869 delete_substitute_path_rule (rule);
1870 rule_found = 1;
1873 rule = next;
1876 /* If the user asked for a specific rule to be deleted but
1877 we could not find it, then report an error. */
1879 if (from != NULL && !rule_found)
1880 error (_("No substitution rule defined for `%s'"), from);
1882 forget_cached_source_info ();
1885 /* Add a new source path substitution rule. */
1887 static void
1888 set_substitute_path_command (char *args, int from_tty)
1890 char *from_path, *to_path;
1891 char **argv;
1892 struct substitute_path_rule *rule;
1894 argv = gdb_buildargv (args);
1895 make_cleanup_freeargv (argv);
1897 if (argv == NULL || argv[0] == NULL || argv [1] == NULL)
1898 error (_("Incorrect usage, too few arguments in command"));
1900 if (argv[2] != NULL)
1901 error (_("Incorrect usage, too many arguments in command"));
1903 if (*(argv[0]) == '\0')
1904 error (_("First argument must be at least one character long"));
1906 /* Strip any trailing directory separator character in either FROM
1907 or TO. The substitution rule already implicitly contains them. */
1908 strip_trailing_directory_separator (argv[0]);
1909 strip_trailing_directory_separator (argv[1]);
1911 /* If a rule with the same "from" was previously defined, then
1912 delete it. This new rule replaces it. */
1914 rule = find_substitute_path_rule (argv[0]);
1915 if (rule != NULL)
1916 delete_substitute_path_rule (rule);
1918 /* Insert the new substitution rule. */
1920 add_substitute_path_rule (argv[0], argv[1]);
1921 forget_cached_source_info ();
1925 void
1926 _initialize_source (void)
1928 struct cmd_list_element *c;
1929 current_source_symtab = 0;
1930 init_source_path ();
1932 /* The intention is to use POSIX Basic Regular Expressions.
1933 Always use the GNU regex routine for consistency across all hosts.
1934 Our current GNU regex.c does not have all the POSIX features, so this is
1935 just an approximation. */
1936 re_set_syntax (RE_SYNTAX_GREP);
1938 c = add_cmd ("directory", class_files, directory_command, _("\
1939 Add directory DIR to beginning of search path for source files.\n\
1940 Forget cached info on source file locations and line positions.\n\
1941 DIR can also be $cwd for the current working directory, or $cdir for the\n\
1942 directory in which the source file was compiled into object code.\n\
1943 With no argument, reset the search path to $cdir:$cwd, the default."),
1944 &cmdlist);
1946 if (dbx_commands)
1947 add_com_alias ("use", "directory", class_files, 0);
1949 set_cmd_completer (c, filename_completer);
1951 add_cmd ("directories", no_class, show_directories, _("\
1952 Current search path for finding source files.\n\
1953 $cwd in the path means the current working directory.\n\
1954 $cdir in the path means the compilation directory of the source file."),
1955 &showlist);
1957 if (xdb_commands)
1959 add_com_alias ("D", "directory", class_files, 0);
1960 add_cmd ("ld", no_class, show_directories, _("\
1961 Current search path for finding source files.\n\
1962 $cwd in the path means the current working directory.\n\
1963 $cdir in the path means the compilation directory of the source file."),
1964 &cmdlist);
1967 add_info ("source", source_info,
1968 _("Information about the current source file."));
1970 add_info ("line", line_info, _("\
1971 Core addresses of the code for a source line.\n\
1972 Line can be specified as\n\
1973 LINENUM, to list around that line in current file,\n\
1974 FILE:LINENUM, to list around that line in that file,\n\
1975 FUNCTION, to list around beginning of that function,\n\
1976 FILE:FUNCTION, to distinguish among like-named static functions.\n\
1977 Default is to describe the last source line that was listed.\n\n\
1978 This sets the default address for \"x\" to the line's first instruction\n\
1979 so that \"x/i\" suffices to start examining the machine code.\n\
1980 The address is also stored as the value of \"$_\"."));
1982 add_com ("forward-search", class_files, forward_search_command, _("\
1983 Search for regular expression (see regex(3)) from last line listed.\n\
1984 The matching line number is also stored as the value of \"$_\"."));
1985 add_com_alias ("search", "forward-search", class_files, 0);
1987 add_com ("reverse-search", class_files, reverse_search_command, _("\
1988 Search backward for regular expression (see regex(3)) from last line listed.\n\
1989 The matching line number is also stored as the value of \"$_\"."));
1991 if (xdb_commands)
1993 add_com_alias ("/", "forward-search", class_files, 0);
1994 add_com_alias ("?", "reverse-search", class_files, 0);
1997 add_setshow_integer_cmd ("listsize", class_support, &lines_to_list, _("\
1998 Set number of source lines gdb will list by default."), _("\
1999 Show number of source lines gdb will list by default."), NULL,
2000 NULL,
2001 show_lines_to_list,
2002 &setlist, &showlist);
2004 add_cmd ("substitute-path", class_files, set_substitute_path_command,
2005 _("\
2006 Usage: set substitute-path FROM TO\n\
2007 Add a substitution rule replacing FROM into TO in source file names.\n\
2008 If a substitution rule was previously set for FROM, the old rule\n\
2009 is replaced by the new one."),
2010 &setlist);
2012 add_cmd ("substitute-path", class_files, unset_substitute_path_command,
2013 _("\
2014 Usage: unset substitute-path [FROM]\n\
2015 Delete the rule for substituting FROM in source file names. If FROM\n\
2016 is not specified, all substituting rules are deleted.\n\
2017 If the debugger cannot find a rule for FROM, it will display a warning."),
2018 &unsetlist);
2020 add_cmd ("substitute-path", class_files, show_substitute_path_command,
2021 _("\
2022 Usage: show substitute-path [FROM]\n\
2023 Print the rule for substituting FROM in source file names. If FROM\n\
2024 is not specified, print all substitution rules."),
2025 &showlist);