7a2829bf77772c8138d72e64e3b1ce87d8cc4de5
[deliverable/binutils-gdb.git] / gdb / source.c
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.
5
6 This file is part of GDB.
7
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.
12
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.
17
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/>. */
20
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"
32
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"
48
49
50 #define OPEN_MODE (O_RDONLY | O_BINARY)
51 #define FDOPEN_MODE FOPEN_RB
52
53 /* Prototypes for exported functions. */
54
55 void _initialize_source (void);
56
57 /* Prototypes for local functions. */
58
59 static int get_filename_and_charpos (struct symtab *, char **);
60
61 static void reverse_search_command (char *, int);
62
63 static void forward_search_command (char *, int);
64
65 static void line_info (char *, int);
66
67 static void source_info (char *, int);
68
69 static void show_directories (char *, int);
70
71 /* Path of directories to search for source files.
72 Same format as the PATH environment variable's value. */
73
74 char *source_path;
75
76 /* Support for source path substitution commands. */
77
78 struct substitute_path_rule
79 {
80 char *from;
81 char *to;
82 struct substitute_path_rule *next;
83 };
84
85 static struct substitute_path_rule *substitute_path_rules = NULL;
86
87 /* Symtab of default file for listing lines of. */
88
89 static struct symtab *current_source_symtab;
90
91 /* Default next line to list. */
92
93 static int current_source_line;
94
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. */
100
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)
105 {
106 fprintf_filtered (file, _("\
107 Number of source lines gdb will list by default is %s.\n"),
108 value);
109 }
110
111 /* Line number of last line printed. Default for various commands.
112 current_source_line is usually, but not always, the same as this. */
113
114 static int last_line_listed;
115
116 /* First line number listed by last listing command. */
117
118 static int first_line_listed;
119
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 */
122
123 static struct symtab *last_source_visited = NULL;
124 static int last_source_error = 0;
125 \f
126 /* Return the first line listed by print_source_lines.
127 Used by command interpreters to request listing from
128 a previous point. */
129
130 int
131 get_first_line_listed (void)
132 {
133 return first_line_listed;
134 }
135
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. */
140
141 int
142 get_lines_to_list (void)
143 {
144 return lines_to_list;
145 }
146
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. */
149
150 struct symtab_and_line
151 get_current_source_symtab_and_line (void)
152 {
153 struct symtab_and_line cursal = { 0 };
154
155 cursal.symtab = current_source_symtab;
156 cursal.line = current_source_line;
157 cursal.pc = 0;
158 cursal.end = 0;
159
160 return cursal;
161 }
162
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. */
170
171 void
172 set_default_source_symtab_and_line (void)
173 {
174 struct symtab_and_line cursal;
175
176 if (!have_full_symbols () && !have_partial_symbols ())
177 error (_("No symbol table is loaded. Use the \"file\" command."));
178
179 /* Pull in a current source symtab if necessary */
180 if (current_source_symtab == 0)
181 select_source_symtab (0);
182 }
183
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. */
188
189 struct symtab_and_line
190 set_current_source_symtab_and_line (const struct symtab_and_line *sal)
191 {
192 struct symtab_and_line cursal = { 0 };
193
194 cursal.symtab = current_source_symtab;
195 cursal.line = current_source_line;
196
197 current_source_symtab = sal->symtab;
198 current_source_line = sal->line;
199 cursal.pc = 0;
200 cursal.end = 0;
201
202 return cursal;
203 }
204
205 /* Reset any information stored about a default file and line to print. */
206
207 void
208 clear_current_source_symtab_and_line (void)
209 {
210 current_source_symtab = 0;
211 current_source_line = 0;
212 }
213
214 /* Set the source file default for the "list" command to be S.
215
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. */
221
222 void
223 select_source_symtab (struct symtab *s)
224 {
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;
230
231 if (s)
232 {
233 current_source_symtab = s;
234 current_source_line = 1;
235 return;
236 }
237
238 if (current_source_symtab)
239 return;
240
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))
244 {
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;
252 }
253
254 /* Alright; find the last file in the symtab list (ignoring .h's
255 and namespace symtabs). */
256
257 current_source_line = 1;
258
259 for (ofp = object_files; ofp != NULL; ofp = ofp->next)
260 {
261 for (s = ofp->symtabs; s; s = s->next)
262 {
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;
268 }
269 }
270 if (current_source_symtab)
271 return;
272
273 /* How about the partial symbol tables? */
274
275 for (ofp = object_files; ofp != NULL; ofp = ofp->next)
276 {
277 for (ps = ofp->psymtabs; ps != NULL; ps = ps->next)
278 {
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;
284 }
285 }
286 if (cs_pst)
287 {
288 if (cs_pst->readin)
289 {
290 internal_error (__FILE__, __LINE__,
291 _("select_source_symtab: "
292 "readin pst found and no symtabs."));
293 }
294 else
295 {
296 current_source_symtab = PSYMTAB_TO_SYMTAB (cs_pst);
297 }
298 }
299 if (current_source_symtab)
300 return;
301
302 error (_("Can't find a default source file"));
303 }
304 \f
305 static void
306 show_directories (char *ignore, int from_tty)
307 {
308 puts_filtered ("Source directories searched: ");
309 puts_filtered (source_path);
310 puts_filtered ("\n");
311 }
312
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. */
316
317 void
318 forget_cached_source_info (void)
319 {
320 struct symtab *s;
321 struct objfile *objfile;
322 struct partial_symtab *pst;
323
324 for (objfile = object_files; objfile != NULL; objfile = objfile->next)
325 {
326 for (s = objfile->symtabs; s != NULL; s = s->next)
327 {
328 if (s->line_charpos != NULL)
329 {
330 xfree (s->line_charpos);
331 s->line_charpos = NULL;
332 }
333 if (s->fullname != NULL)
334 {
335 xfree (s->fullname);
336 s->fullname = NULL;
337 }
338 }
339
340 ALL_OBJFILE_PSYMTABS (objfile, pst)
341 {
342 if (pst->fullname != NULL)
343 {
344 xfree (pst->fullname);
345 pst->fullname = NULL;
346 }
347 }
348 }
349
350 last_source_visited = NULL;
351 }
352
353 void
354 init_source_path (void)
355 {
356 char buf[20];
357
358 sprintf (buf, "$cdir%c$cwd", DIRNAME_SEPARATOR);
359 source_path = xstrdup (buf);
360 forget_cached_source_info ();
361 }
362
363 /* Add zero or more directories to the front of the source path. */
364
365 void
366 directory_command (char *dirname, int from_tty)
367 {
368 dont_repeat ();
369 /* FIXME, this goes to "delete dir"... */
370 if (dirname == 0)
371 {
372 if (!from_tty || query (_("Reinitialize source path to empty? ")))
373 {
374 xfree (source_path);
375 init_source_path ();
376 }
377 }
378 else
379 {
380 mod_path (dirname, &source_path);
381 forget_cached_source_info ();
382 }
383 if (from_tty)
384 show_directories ((char *) 0, from_tty);
385 }
386
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. */
389
390 void
391 directory_switch (char *dirname, int from_tty)
392 {
393 add_path (dirname, &source_path, 0);
394 }
395
396 /* Add zero or more directories to the front of an arbitrary path. */
397
398 void
399 mod_path (char *dirname, char **which_path)
400 {
401 add_path (dirname, which_path, 1);
402 }
403
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. */
409
410 void
411 add_path (char *dirname, char **which_path, int parse_separators)
412 {
413 char *old = *which_path;
414 int prefix = 0;
415 char **argv = NULL;
416 char *arg;
417 int argv_index = 0;
418
419 if (dirname == 0)
420 return;
421
422 if (parse_separators)
423 {
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);
429
430 arg = argv[0];
431 }
432 else
433 {
434 arg = xstrdup (dirname);
435 make_cleanup (xfree, arg);
436 }
437
438 do
439 {
440 char *name = arg;
441 char *p;
442 struct stat st;
443
444 {
445 char *separator = NULL;
446
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);
452
453 if (separator == 0)
454 p = arg = name + strlen (name);
455 else
456 {
457 p = separator;
458 arg = p + 1;
459 while (*arg == DIRNAME_SEPARATOR)
460 ++arg;
461 }
462
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;
467 }
468
469 /* name is the start of the directory.
470 p is the separator (or null) following the end. */
471
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';
481
482 while (p > name && p[-1] == '.')
483 {
484 if (p - name == 1)
485 {
486 /* "." => getwd (). */
487 name = current_directory;
488 goto append;
489 }
490 else if (p > name + 1 && IS_DIR_SEPARATOR (p[-2]))
491 {
492 if (p - name == 2)
493 {
494 /* "/." => "/". */
495 *--p = '\0';
496 goto append;
497 }
498 else
499 {
500 /* "...foo/." => "...foo". */
501 p -= 2;
502 *p = '\0';
503 continue;
504 }
505 }
506 else
507 break;
508 }
509
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);
521
522 /* Unless it's a variable, check existence. */
523 if (name[0] != '$')
524 {
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.
528
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)
535 {
536 int save_errno = errno;
537 fprintf_unfiltered (gdb_stderr, "Warning: ");
538 print_sys_errmsg (name, save_errno);
539 }
540 else if ((st.st_mode & S_IFMT) != S_IFDIR)
541 warning (_("%s is not a directory."), name);
542 }
543
544 append:
545 {
546 unsigned int len = strlen (name);
547
548 p = *which_path;
549 while (1)
550 {
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.
555
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))
561 {
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 : */
568 }
569 p = strchr (p, DIRNAME_SEPARATOR);
570 if (p != 0)
571 ++p;
572 else
573 break;
574 }
575 if (p == 0)
576 {
577 char tinybuf[2];
578
579 tinybuf[0] = DIRNAME_SEPARATOR;
580 tinybuf[1] = '\0';
581
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)
585 {
586 char *temp, c;
587
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);
595 }
596 else
597 {
598 *which_path = concat (name, (old[0] ? tinybuf : old),
599 old, (char *)NULL);
600 prefix = strlen (name);
601 }
602 xfree (old);
603 old = *which_path;
604 }
605 }
606 skip_dup:;
607 }
608 while (arg != NULL);
609 }
610
611
612 static void
613 source_info (char *ignore, int from_tty)
614 {
615 struct symtab *s = current_source_symtab;
616
617 if (!s)
618 {
619 printf_filtered (_("No current source file.\n"));
620 return;
621 }
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");
630
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");
635 }
636 \f
637
638 /* Return True if the file NAME exists and is a regular file */
639 static int
640 is_regular_file (const char *name)
641 {
642 struct stat st;
643 const int status = stat (name, &st);
644
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.
649 */
650 if (status != 0)
651 return (errno != ENOENT);
652
653 return S_ISREG (st.st_mode);
654 }
655
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).
659
660 OPTS specifies the function behaviour in specific cases.
661
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).
667
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).
671
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!!!
677
678 If a file is found, return the descriptor.
679 Otherwise, return -1, with errno set for the last name we tried to open. */
680
681 /* >>>> This should only allow files of certain types,
682 >>>> eg executable, non-directory */
683 int
684 openp (const char *path, int opts, const char *string,
685 int mode, char **filename_opened)
686 {
687 int fd;
688 char *filename;
689 const char *p;
690 const char *p1;
691 int len;
692 int alloclen;
693
694 /* The open syscall MODE parameter is not specified. */
695 gdb_assert ((mode & O_CREAT) == 0);
696
697 if (!path)
698 path = ".";
699
700 mode |= O_BINARY;
701
702 if ((opts & OPF_TRY_CWD_FIRST) || IS_ABSOLUTE_PATH (string))
703 {
704 int i;
705
706 if (is_regular_file (string))
707 {
708 filename = alloca (strlen (string) + 1);
709 strcpy (filename, string);
710 fd = open (filename, mode);
711 if (fd >= 0)
712 goto done;
713 }
714 else
715 {
716 filename = NULL;
717 fd = -1;
718 }
719
720 if (!(opts & OPF_SEARCH_IN_PATH))
721 for (i = 0; string[i]; i++)
722 if (IS_DIR_SEPARATOR (string[i]))
723 goto done;
724 }
725
726 /* /foo => foo, to avoid multiple slashes that Emacs doesn't like. */
727 while (IS_DIR_SEPARATOR(string[0]))
728 string++;
729
730 /* ./foo => foo */
731 while (string[0] == '.' && IS_DIR_SEPARATOR (string[1]))
732 string += 2;
733
734 alloclen = strlen (path) + strlen (string) + 2;
735 filename = alloca (alloclen);
736 fd = -1;
737 for (p = path; p; p = p1 ? p1 + 1 : 0)
738 {
739 p1 = strchr (p, DIRNAME_SEPARATOR);
740 if (p1)
741 len = p1 - p;
742 else
743 len = strlen (p);
744
745 if (len == 4 && p[0] == '$' && p[1] == 'c'
746 && p[2] == 'w' && p[3] == 'd')
747 {
748 /* Name is $cwd -- insert current directory name instead. */
749 int newlen;
750
751 /* First, realloc the filename buffer if too short. */
752 len = strlen (current_directory);
753 newlen = len + strlen (string) + 2;
754 if (newlen > alloclen)
755 {
756 alloclen = newlen;
757 filename = alloca (alloclen);
758 }
759 strcpy (filename, current_directory);
760 }
761 else
762 {
763 /* Normal file name in path -- just use it. */
764 strncpy (filename, p, len);
765 filename[len] = 0;
766 }
767
768 /* Remove trailing slashes */
769 while (len > 0 && IS_DIR_SEPARATOR (filename[len - 1]))
770 filename[--len] = 0;
771
772 strcat (filename + len, SLASH_STRING);
773 strcat (filename, string);
774
775 if (is_regular_file (filename))
776 {
777 fd = open (filename, mode);
778 if (fd >= 0)
779 break;
780 }
781 }
782
783 done:
784 if (filename_opened)
785 {
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
796 {
797 /* Beware the // my son, the Emacs barfs, the botch that catch... */
798
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);
805 }
806 }
807
808 return fd;
809 }
810
811
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).
816
817 The current working directory is searched first.
818
819 If the file was found, this function returns 1, and FULL_PATHNAME is
820 set to the fully-qualified pathname.
821
822 Else, this functions returns 0, and FULL_PATHNAME is set to NULL. */
823 int
824 source_full_path_of (const char *filename, char **full_pathname)
825 {
826 int fd;
827
828 fd = openp (source_path, OPF_TRY_CWD_FIRST | OPF_SEARCH_IN_PATH, filename,
829 O_RDONLY, full_pathname);
830 if (fd < 0)
831 {
832 *full_pathname = NULL;
833 return 0;
834 }
835
836 close (fd);
837 return 1;
838 }
839
840 /* Return non-zero if RULE matches PATH, that is if the rule can be
841 applied to PATH. */
842
843 static int
844 substitute_path_rule_matches (const struct substitute_path_rule *rule,
845 const char *path)
846 {
847 const int from_len = strlen (rule->from);
848 const int path_len = strlen (path);
849 char *path_start;
850
851 if (path_len < from_len)
852 return 0;
853
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. */
858
859 path_start = alloca (from_len + 1);
860 strncpy (path_start, path, from_len);
861 path_start[from_len] = '\0';
862
863 if (FILENAME_CMP (path_start, rule->from) != 0)
864 return 0;
865
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). */
869
870 if (path[from_len] != '\0' && !IS_DIR_SEPARATOR (path[from_len]))
871 return 0;
872
873 return 1;
874 }
875
876 /* Find the substitute-path rule that applies to PATH and return it.
877 Return NULL if no rule applies. */
878
879 static struct substitute_path_rule *
880 get_substitute_path_rule (const char *path)
881 {
882 struct substitute_path_rule *rule = substitute_path_rules;
883
884 while (rule != NULL && !substitute_path_rule_matches (rule, path))
885 rule = rule->next;
886
887 return rule;
888 }
889
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.
893
894 Return NULL if no substitution rule was specified by the user,
895 or if no rule applied to the given PATH. */
896
897 static char *
898 rewrite_source_path (const char *path)
899 {
900 const struct substitute_path_rule *rule = get_substitute_path_rule (path);
901 char *new_path;
902 int from_len;
903
904 if (rule == NULL)
905 return NULL;
906
907 from_len = strlen (rule->from);
908
909 /* Compute the rewritten path and return it. */
910
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);
915
916 return new_path;
917 }
918
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.
922
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.
929
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.
934
935 On Failure
936 An invalid file descriptor is returned. ( the return value is negative )
937 FULLNAME is set to NULL. */
938
939 static int
940 find_and_open_source (const char *filename,
941 const char *dirname,
942 char **fullname)
943 {
944 char *path = source_path;
945 const char *p;
946 int result;
947
948 /* Quick way out if we already know its full name */
949
950 if (*fullname)
951 {
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);
956
957 if (rewritten_fullname != NULL)
958 {
959 xfree (*fullname);
960 *fullname = rewritten_fullname;
961 }
962
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;
969 }
970
971 if (dirname != NULL)
972 {
973 /* If necessary, rewrite the compilation directory name according
974 to the source path substitution rules specified by the user. */
975
976 char *rewritten_dirname = rewrite_source_path (dirname);
977
978 if (rewritten_dirname != NULL)
979 {
980 make_cleanup (xfree, rewritten_dirname);
981 dirname = rewritten_dirname;
982 }
983
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'))
991 {
992 int len;
993
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 */
1000 }
1001 }
1002
1003 if (IS_ABSOLUTE_PATH (filename))
1004 {
1005 /* If filename is absolute path, try the source path
1006 substitution on it. */
1007 char *rewritten_filename = rewrite_source_path (filename);
1008
1009 if (rewritten_filename != NULL)
1010 {
1011 make_cleanup (xfree, rewritten_filename);
1012 filename = rewritten_filename;
1013 }
1014 }
1015
1016 result = openp (path, OPF_SEARCH_IN_PATH, filename, OPEN_MODE, fullname);
1017 if (result < 0)
1018 {
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);
1023 }
1024
1025 return result;
1026 }
1027
1028 /* Open a source file given a symtab S. Returns a file descriptor or
1029 negative number for error.
1030
1031 This function is a convience function to find_and_open_source. */
1032
1033 int
1034 open_source_file (struct symtab *s)
1035 {
1036 if (!s)
1037 return -1;
1038
1039 return find_and_open_source (s->filename, s->dirname, &s->fullname);
1040 }
1041
1042 /* Finds the fullname that a symtab represents.
1043
1044 If this functions finds the fullname, it will save it in s->fullname
1045 and it will also return the value.
1046
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)
1051 {
1052 int r;
1053
1054 if (!s)
1055 return NULL;
1056
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);
1060
1061 if (r >= 0)
1062 {
1063 close (r);
1064 return s->fullname;
1065 }
1066
1067 return NULL;
1068 }
1069
1070 /* Finds the fullname that a partial_symtab represents.
1071
1072 If this functions finds the fullname, it will save it in ps->fullname
1073 and it will also return the value.
1074
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)
1079 {
1080 int r;
1081
1082 if (!ps)
1083 return NULL;
1084
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);
1088
1089 if (r >= 0)
1090 {
1091 close (r);
1092 return ps->fullname;
1093 }
1094
1095 return NULL;
1096 }
1097 \f
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. */
1102
1103 void
1104 find_source_lines (struct symtab *s, int desc)
1105 {
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;
1113
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);
1118
1119 if (s->objfile && s->objfile->obfd)
1120 mtime = s->objfile->mtime;
1121 else if (exec_bfd)
1122 mtime = exec_bfd_mtime;
1123
1124 if (mtime && mtime < st.st_mtime)
1125 warning (_("Source file is more recent than executable."));
1126
1127 #ifdef LSEEK_NOT_LINEAR
1128 {
1129 char c;
1130
1131 /* Have to read it byte by byte to find out where the chars live */
1132
1133 line_charpos[0] = lseek (desc, 0, SEEK_CUR);
1134 nlines = 1;
1135 while (myread (desc, &c, 1) > 0)
1136 {
1137 if (c == '\n')
1138 {
1139 if (nlines == lines_allocated)
1140 {
1141 lines_allocated *= 2;
1142 line_charpos =
1143 (int *) xrealloc ((char *) line_charpos,
1144 sizeof (int) * lines_allocated);
1145 }
1146 line_charpos[nlines++] = lseek (desc, 0, SEEK_CUR);
1147 }
1148 }
1149 }
1150 #else /* lseek linear. */
1151 {
1152 struct cleanup *old_cleanups;
1153
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;
1157
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);
1162
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)
1172 {
1173 if (*p++ == '\n'
1174 /* A newline at the end does not start a new line. */
1175 && p != end)
1176 {
1177 if (nlines == lines_allocated)
1178 {
1179 lines_allocated *= 2;
1180 line_charpos =
1181 (int *) xrealloc ((char *) line_charpos,
1182 sizeof (int) * lines_allocated);
1183 }
1184 line_charpos[nlines++] = p - data;
1185 }
1186 }
1187 do_cleanups (old_cleanups);
1188 }
1189 #endif /* lseek linear. */
1190 s->nlines = nlines;
1191 s->line_charpos =
1192 (int *) xrealloc ((char *) line_charpos, nlines * sizeof (int));
1193
1194 }
1195
1196 /* Return the character position of a line LINE in symtab S.
1197 Return 0 if anything is invalid. */
1198
1199 #if 0 /* Currently unused */
1200
1201 int
1202 source_line_charpos (struct symtab *s, int line)
1203 {
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];
1211 }
1212
1213 /* Return the line number of character position POS in symtab S. */
1214
1215 int
1216 source_charpos_line (struct symtab *s, int chr)
1217 {
1218 int line = 0;
1219 int *lnp;
1220
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)
1226 {
1227 line++;
1228 lnp++;
1229 }
1230 if (line >= s->nlines)
1231 line = s->nlines;
1232 return line;
1233 }
1234
1235 #endif /* 0 */
1236 \f
1237
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. */
1242
1243 static int
1244 get_filename_and_charpos (struct symtab *s, char **fullname)
1245 {
1246 int desc, linenums_changed = 0;
1247 struct cleanup *cleanups;
1248
1249 desc = open_source_file (s);
1250 if (desc < 0)
1251 {
1252 if (fullname)
1253 *fullname = NULL;
1254 return 0;
1255 }
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;
1265 }
1266
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.
1271
1272 MID_STATEMENT is nonzero if the PC is not at the beginning of that line.
1273
1274 Return 1 if successful, 0 if could not find the file. */
1275
1276 int
1277 identify_source_line (struct symtab *s, int line, int mid_statement,
1278 CORE_ADDR pc)
1279 {
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);
1289
1290 current_source_line = line;
1291 first_line_listed = line;
1292 last_line_listed = line;
1293 current_source_symtab = s;
1294 return 1;
1295 }
1296 \f
1297
1298 /* Print source lines from the file of symtab S,
1299 starting with line number LINE and stopping before line number STOPLINE. */
1300
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)
1305 {
1306 int c;
1307 int desc;
1308 FILE *stream;
1309 int nlines = stopline - line;
1310 struct cleanup *cleanup;
1311
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;
1316
1317 /* If printing of source lines is disabled, just print file and line number */
1318 if (ui_out_test_flags (uiout, ui_source_list))
1319 {
1320 /* Only prints "No such file or directory" once */
1321 if ((s != last_source_visited) || (!last_source_error))
1322 {
1323 last_source_visited = s;
1324 desc = open_source_file (s);
1325 }
1326 else
1327 {
1328 desc = last_source_error;
1329 noerror = 1;
1330 }
1331 }
1332 else
1333 {
1334 desc = -1;
1335 noerror = 1;
1336 }
1337
1338 if (desc < 0)
1339 {
1340 last_source_error = desc;
1341
1342 if (!noerror)
1343 {
1344 char *name = alloca (strlen (s->filename) + 100);
1345 sprintf (name, "%d\t%s", line, s->filename);
1346 print_sys_errmsg (name, errno);
1347 }
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");
1353
1354 return;
1355 }
1356
1357 last_source_error = 0;
1358
1359 if (s->line_charpos == 0)
1360 find_source_lines (s, desc);
1361
1362 if (line < 1 || line > s->nlines)
1363 {
1364 close (desc);
1365 error (_("Line number %d out of range; %s has %d lines."),
1366 line, s->filename, s->nlines);
1367 }
1368
1369 if (lseek (desc, s->line_charpos[line - 1], 0) < 0)
1370 {
1371 close (desc);
1372 perror_with_name (s->filename);
1373 }
1374
1375 stream = fdopen (desc, FDOPEN_MODE);
1376 clearerr (stream);
1377 cleanup = make_cleanup_fclose (stream);
1378
1379 while (nlines-- > 0)
1380 {
1381 char buf[20];
1382
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);
1389 do
1390 {
1391 if (c < 040 && c != '\t' && c != '\n' && c != '\r')
1392 {
1393 sprintf (buf, "^%c", c + 0100);
1394 ui_out_text (uiout, buf);
1395 }
1396 else if (c == 0177)
1397 ui_out_text (uiout, "^?");
1398 else if (c == '\r')
1399 {
1400 /* Skip a \r character, but only before a \n. */
1401 int c1 = fgetc (stream);
1402
1403 if (c1 != '\n')
1404 printf_filtered ("^%c", c + 0100);
1405 if (c1 != EOF)
1406 ungetc (c1, stream);
1407 }
1408 else
1409 {
1410 sprintf (buf, "%c", c);
1411 ui_out_text (uiout, buf);
1412 }
1413 }
1414 while (c != '\n' && (c = fgetc (stream)) >= 0);
1415 }
1416
1417 do_cleanups (cleanup);
1418 }
1419 \f
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 */
1424
1425 void
1426 print_source_lines (struct symtab *s, int line, int stopline, int noerror)
1427 {
1428 print_source_lines_base (s, line, stopline, noerror);
1429 }
1430 \f
1431 /* Print info on range of pc's in a specified line. */
1432
1433 static void
1434 line_info (char *arg, int from_tty)
1435 {
1436 struct symtabs_and_lines sals;
1437 struct symtab_and_line sal;
1438 CORE_ADDR start_pc, end_pc;
1439 int i;
1440
1441 init_sal (&sal); /* initialize to zeroes */
1442
1443 if (arg == 0)
1444 {
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;
1451 }
1452 else
1453 {
1454 sals = decode_line_spec_1 (arg, 0);
1455
1456 dont_repeat ();
1457 }
1458
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++)
1462 {
1463 sal = sals.sals[i];
1464
1465 if (sal.symtab == 0)
1466 {
1467 struct gdbarch *gdbarch = get_current_arch ();
1468
1469 printf_filtered (_("No line number information available"));
1470 if (sal.pc != 0)
1471 {
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);
1478 }
1479 else
1480 printf_filtered (".");
1481 printf_filtered ("\n");
1482 }
1483 else if (sal.line > 0
1484 && find_line_pc_range (sal, &start_pc, &end_pc))
1485 {
1486 struct gdbarch *gdbarch = get_objfile_arch (sal.symtab->objfile);
1487
1488 if (start_pc == end_pc)
1489 {
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");
1497 }
1498 else
1499 {
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");
1509 }
1510
1511 /* x/i should display this line's code. */
1512 set_next_address (gdbarch, start_pc);
1513
1514 /* Repeating "info line" should do the following line. */
1515 last_line_listed = sal.line + 1;
1516
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);
1521 }
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);
1528 }
1529 xfree (sals.sals);
1530 }
1531 \f
1532 /* Commands to search the source file for a regexp. */
1533
1534 static void
1535 forward_search_command (char *regex, int from_tty)
1536 {
1537 int c;
1538 int desc;
1539 FILE *stream;
1540 int line;
1541 char *msg;
1542 struct cleanup *cleanups;
1543
1544 line = last_line_listed + 1;
1545
1546 msg = (char *) re_comp (regex);
1547 if (msg)
1548 error (("%s"), msg);
1549
1550 if (current_source_symtab == 0)
1551 select_source_symtab (0);
1552
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);
1557
1558 if (current_source_symtab->line_charpos == 0)
1559 find_source_lines (current_source_symtab, desc);
1560
1561 if (line < 1 || line > current_source_symtab->nlines)
1562 error (_("Expression not found"));
1563
1564 if (lseek (desc, current_source_symtab->line_charpos[line - 1], 0) < 0)
1565 perror_with_name (current_source_symtab->filename);
1566
1567 discard_cleanups (cleanups);
1568 stream = fdopen (desc, FDOPEN_MODE);
1569 clearerr (stream);
1570 cleanups = make_cleanup_fclose (stream);
1571 while (1)
1572 {
1573 static char *buf = NULL;
1574 char *p;
1575 int cursize, newsize;
1576
1577 cursize = 256;
1578 buf = xmalloc (cursize);
1579 p = buf;
1580
1581 c = getc (stream);
1582 if (c == EOF)
1583 break;
1584 do
1585 {
1586 *p++ = c;
1587 if (p - buf == cursize)
1588 {
1589 newsize = cursize + cursize / 2;
1590 buf = xrealloc (buf, newsize);
1591 p = buf + cursize;
1592 cursize = newsize;
1593 }
1594 }
1595 while (c != '\n' && (c = getc (stream)) >= 0);
1596
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')
1600 {
1601 p--;
1602 p[-1] = '\n';
1603 }
1604
1605 /* we now have a source line in buf, null terminate and match */
1606 *p = 0;
1607 if (re_exec (buf) > 0)
1608 {
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;
1615 }
1616 line++;
1617 }
1618
1619 printf_filtered (_("Expression not found\n"));
1620 do_cleanups (cleanups);
1621 }
1622
1623 static void
1624 reverse_search_command (char *regex, int from_tty)
1625 {
1626 int c;
1627 int desc;
1628 FILE *stream;
1629 int line;
1630 char *msg;
1631 struct cleanup *cleanups;
1632
1633 line = last_line_listed - 1;
1634
1635 msg = (char *) re_comp (regex);
1636 if (msg)
1637 error (("%s"), msg);
1638
1639 if (current_source_symtab == 0)
1640 select_source_symtab (0);
1641
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);
1646
1647 if (current_source_symtab->line_charpos == 0)
1648 find_source_lines (current_source_symtab, desc);
1649
1650 if (line < 1 || line > current_source_symtab->nlines)
1651 error (_("Expression not found"));
1652
1653 if (lseek (desc, current_source_symtab->line_charpos[line - 1], 0) < 0)
1654 perror_with_name (current_source_symtab->filename);
1655
1656 discard_cleanups (cleanups);
1657 stream = fdopen (desc, FDOPEN_MODE);
1658 clearerr (stream);
1659 cleanups = make_cleanup_fclose (stream);
1660 while (line > 1)
1661 {
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;
1665
1666 c = getc (stream);
1667 if (c == EOF)
1668 break;
1669 do
1670 {
1671 *p++ = c;
1672 }
1673 while (c != '\n' && (c = getc (stream)) >= 0);
1674
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')
1678 {
1679 p--;
1680 p[-1] = '\n';
1681 }
1682
1683 /* We now have a source line in buf; null terminate and match. */
1684 *p = 0;
1685 if (re_exec (buf) > 0)
1686 {
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;
1693 }
1694 line--;
1695 if (fseek (stream, current_source_symtab->line_charpos[line - 1], 0) < 0)
1696 {
1697 do_cleanups (cleanups);
1698 perror_with_name (current_source_symtab->filename);
1699 }
1700 }
1701
1702 printf_filtered (_("Expression not found\n"));
1703 do_cleanups (cleanups);
1704 return;
1705 }
1706
1707 /* If the last character of PATH is a directory separator, then strip it. */
1708
1709 static void
1710 strip_trailing_directory_separator (char *path)
1711 {
1712 const int last = strlen (path) - 1;
1713
1714 if (last < 0)
1715 return; /* No stripping is needed if PATH is the empty string. */
1716
1717 if (IS_DIR_SEPARATOR (path[last]))
1718 path[last] = '\0';
1719 }
1720
1721 /* Return the path substitution rule that matches FROM.
1722 Return NULL if no rule matches. */
1723
1724 static struct substitute_path_rule *
1725 find_substitute_path_rule (const char *from)
1726 {
1727 struct substitute_path_rule *rule = substitute_path_rules;
1728
1729 while (rule != NULL)
1730 {
1731 if (FILENAME_CMP (rule->from, from) == 0)
1732 return rule;
1733 rule = rule->next;
1734 }
1735
1736 return NULL;
1737 }
1738
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. */
1741
1742 void
1743 add_substitute_path_rule (char *from, char *to)
1744 {
1745 struct substitute_path_rule *rule;
1746 struct substitute_path_rule *new_rule;
1747
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;
1752
1753 /* If the list of rules are empty, then insert the new rule
1754 at the head of the list. */
1755
1756 if (substitute_path_rules == NULL)
1757 {
1758 substitute_path_rules = new_rule;
1759 return;
1760 }
1761
1762 /* Otherwise, skip to the last rule in our list and then append
1763 the new rule. */
1764
1765 rule = substitute_path_rules;
1766 while (rule->next != NULL)
1767 rule = rule->next;
1768
1769 rule->next = new_rule;
1770 }
1771
1772 /* Remove the given source path substitution rule from the current list
1773 of rules. The memory allocated for that rule is also deallocated. */
1774
1775 static void
1776 delete_substitute_path_rule (struct substitute_path_rule *rule)
1777 {
1778 if (rule == substitute_path_rules)
1779 substitute_path_rules = rule->next;
1780 else
1781 {
1782 struct substitute_path_rule *prev = substitute_path_rules;
1783
1784 while (prev != NULL && prev->next != rule)
1785 prev = prev->next;
1786
1787 gdb_assert (prev != NULL);
1788
1789 prev->next = rule->next;
1790 }
1791
1792 xfree (rule->from);
1793 xfree (rule->to);
1794 xfree (rule);
1795 }
1796
1797 /* Implement the "show substitute-path" command. */
1798
1799 static void
1800 show_substitute_path_command (char *args, int from_tty)
1801 {
1802 struct substitute_path_rule *rule = substitute_path_rules;
1803 char **argv;
1804 char *from = NULL;
1805
1806 argv = gdb_buildargv (args);
1807 make_cleanup_freeargv (argv);
1808
1809 /* We expect zero or one argument. */
1810
1811 if (argv != NULL && argv[0] != NULL && argv[1] != NULL)
1812 error (_("Too many arguments in command"));
1813
1814 if (argv != NULL && argv[0] != NULL)
1815 from = argv[0];
1816
1817 /* Print the substitution rules. */
1818
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"));
1824
1825 while (rule != NULL)
1826 {
1827 if (from == NULL || FILENAME_CMP (rule->from, from) == 0)
1828 printf_filtered (" `%s' -> `%s'.\n", rule->from, rule->to);
1829 rule = rule->next;
1830 }
1831 }
1832
1833 /* Implement the "unset substitute-path" command. */
1834
1835 static void
1836 unset_substitute_path_command (char *args, int from_tty)
1837 {
1838 struct substitute_path_rule *rule = substitute_path_rules;
1839 char **argv = gdb_buildargv (args);
1840 char *from = NULL;
1841 int rule_found = 0;
1842
1843 /* This function takes either 0 or 1 argument. */
1844
1845 make_cleanup_freeargv (argv);
1846 if (argv != NULL && argv[0] != NULL && argv[1] != NULL)
1847 error (_("Incorrect usage, too many arguments in command"));
1848
1849 if (argv != NULL && argv[0] != NULL)
1850 from = argv[0];
1851
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. */
1855
1856 if (from == NULL
1857 && !query (_("Delete all source path substitution rules? ")))
1858 error (_("Canceled"));
1859
1860 /* Delete the rule matching the argument. No argument means that
1861 all rules should be deleted. */
1862
1863 while (rule != NULL)
1864 {
1865 struct substitute_path_rule *next = rule->next;
1866
1867 if (from == NULL || FILENAME_CMP (from, rule->from) == 0)
1868 {
1869 delete_substitute_path_rule (rule);
1870 rule_found = 1;
1871 }
1872
1873 rule = next;
1874 }
1875
1876 /* If the user asked for a specific rule to be deleted but
1877 we could not find it, then report an error. */
1878
1879 if (from != NULL && !rule_found)
1880 error (_("No substitution rule defined for `%s'"), from);
1881
1882 forget_cached_source_info ();
1883 }
1884
1885 /* Add a new source path substitution rule. */
1886
1887 static void
1888 set_substitute_path_command (char *args, int from_tty)
1889 {
1890 char *from_path, *to_path;
1891 char **argv;
1892 struct substitute_path_rule *rule;
1893
1894 argv = gdb_buildargv (args);
1895 make_cleanup_freeargv (argv);
1896
1897 if (argv == NULL || argv[0] == NULL || argv [1] == NULL)
1898 error (_("Incorrect usage, too few arguments in command"));
1899
1900 if (argv[2] != NULL)
1901 error (_("Incorrect usage, too many arguments in command"));
1902
1903 if (*(argv[0]) == '\0')
1904 error (_("First argument must be at least one character long"));
1905
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]);
1910
1911 /* If a rule with the same "from" was previously defined, then
1912 delete it. This new rule replaces it. */
1913
1914 rule = find_substitute_path_rule (argv[0]);
1915 if (rule != NULL)
1916 delete_substitute_path_rule (rule);
1917
1918 /* Insert the new substitution rule. */
1919
1920 add_substitute_path_rule (argv[0], argv[1]);
1921 forget_cached_source_info ();
1922 }
1923
1924 \f
1925 void
1926 _initialize_source (void)
1927 {
1928 struct cmd_list_element *c;
1929 current_source_symtab = 0;
1930 init_source_path ();
1931
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);
1937
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);
1945
1946 if (dbx_commands)
1947 add_com_alias ("use", "directory", class_files, 0);
1948
1949 set_cmd_completer (c, filename_completer);
1950
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);
1956
1957 if (xdb_commands)
1958 {
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);
1965 }
1966
1967 add_info ("source", source_info,
1968 _("Information about the current source file."));
1969
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 \"$_\"."));
1981
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);
1986
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 \"$_\"."));
1990
1991 if (xdb_commands)
1992 {
1993 add_com_alias ("/", "forward-search", class_files, 0);
1994 add_com_alias ("?", "reverse-search", class_files, 0);
1995 }
1996
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);
2003
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);
2011
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);
2019
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);
2026 }
This page took 0.10264 seconds and 3 git commands to generate.