* dummy-frame.c (deprecated_pc_in_call_dummy): Add GDBARCH parameter,
[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 "symtab.h"
23 #include "expression.h"
24 #include "language.h"
25 #include "command.h"
26 #include "source.h"
27 #include "gdbcmd.h"
28 #include "frame.h"
29 #include "value.h"
30 #include "gdb_assert.h"
31
32 #include <sys/types.h>
33 #include "gdb_string.h"
34 #include "gdb_stat.h"
35 #include <fcntl.h>
36 #include "gdbcore.h"
37 #include "gdb_regex.h"
38 #include "symfile.h"
39 #include "objfiles.h"
40 #include "annotate.h"
41 #include "gdbtypes.h"
42 #include "linespec.h"
43 #include "filenames.h" /* for DOSish file names */
44 #include "completer.h"
45 #include "ui-out.h"
46 #include "readline/readline.h"
47
48
49 #define OPEN_MODE (O_RDONLY | O_BINARY)
50 #define FDOPEN_MODE FOPEN_RB
51
52 /* Prototypes for exported functions. */
53
54 void _initialize_source (void);
55
56 /* Prototypes for local functions. */
57
58 static int get_filename_and_charpos (struct symtab *, char **);
59
60 static void reverse_search_command (char *, int);
61
62 static void forward_search_command (char *, int);
63
64 static void line_info (char *, int);
65
66 static void source_info (char *, int);
67
68 static void show_directories (char *, int);
69
70 /* Path of directories to search for source files.
71 Same format as the PATH environment variable's value. */
72
73 char *source_path;
74
75 /* Support for source path substitution commands. */
76
77 struct substitute_path_rule
78 {
79 char *from;
80 char *to;
81 struct substitute_path_rule *next;
82 };
83
84 static struct substitute_path_rule *substitute_path_rules = NULL;
85
86 /* Symtab of default file for listing lines of. */
87
88 static struct symtab *current_source_symtab;
89
90 /* Default next line to list. */
91
92 static int current_source_line;
93
94 /* Default number of lines to print with commands like "list".
95 This is based on guessing how many long (i.e. more than chars_per_line
96 characters) lines there will be. To be completely correct, "list"
97 and friends should be rewritten to count characters and see where
98 things are wrapping, but that would be a fair amount of work. */
99
100 int lines_to_list = 10;
101 static void
102 show_lines_to_list (struct ui_file *file, int from_tty,
103 struct cmd_list_element *c, const char *value)
104 {
105 fprintf_filtered (file, _("\
106 Number of source lines gdb will list by default is %s.\n"),
107 value);
108 }
109
110 /* Line number of last line printed. Default for various commands.
111 current_source_line is usually, but not always, the same as this. */
112
113 static int last_line_listed;
114
115 /* First line number listed by last listing command. */
116
117 static int first_line_listed;
118
119 /* Saves the name of the last source file visited and a possible error code.
120 Used to prevent repeating annoying "No such file or directories" msgs */
121
122 static struct symtab *last_source_visited = NULL;
123 static int last_source_error = 0;
124 \f
125 /* Return the first line listed by print_source_lines.
126 Used by command interpreters to request listing from
127 a previous point. */
128
129 int
130 get_first_line_listed (void)
131 {
132 return first_line_listed;
133 }
134
135 /* Return the default number of lines to print with commands like the
136 cli "list". The caller of print_source_lines must use this to
137 calculate the end line and use it in the call to print_source_lines
138 as it does not automatically use this value. */
139
140 int
141 get_lines_to_list (void)
142 {
143 return lines_to_list;
144 }
145
146 /* Return the current source file for listing and next line to list.
147 NOTE: The returned sal pc and end fields are not valid. */
148
149 struct symtab_and_line
150 get_current_source_symtab_and_line (void)
151 {
152 struct symtab_and_line cursal = { 0 };
153
154 cursal.symtab = current_source_symtab;
155 cursal.line = current_source_line;
156 cursal.pc = 0;
157 cursal.end = 0;
158
159 return cursal;
160 }
161
162 /* If the current source file for listing is not set, try and get a default.
163 Usually called before get_current_source_symtab_and_line() is called.
164 It may err out if a default cannot be determined.
165 We must be cautious about where it is called, as it can recurse as the
166 process of determining a new default may call the caller!
167 Use get_current_source_symtab_and_line only to get whatever
168 we have without erroring out or trying to get a default. */
169
170 void
171 set_default_source_symtab_and_line (void)
172 {
173 struct symtab_and_line cursal;
174
175 if (!have_full_symbols () && !have_partial_symbols ())
176 error (_("No symbol table is loaded. Use the \"file\" command."));
177
178 /* Pull in a current source symtab if necessary */
179 if (current_source_symtab == 0)
180 select_source_symtab (0);
181 }
182
183 /* Return the current default file for listing and next line to list
184 (the returned sal pc and end fields are not valid.)
185 and set the current default to whatever is in SAL.
186 NOTE: The returned sal pc and end fields are not valid. */
187
188 struct symtab_and_line
189 set_current_source_symtab_and_line (const struct symtab_and_line *sal)
190 {
191 struct symtab_and_line cursal = { 0 };
192
193 cursal.symtab = current_source_symtab;
194 cursal.line = current_source_line;
195
196 current_source_symtab = sal->symtab;
197 current_source_line = sal->line;
198 cursal.pc = 0;
199 cursal.end = 0;
200
201 return cursal;
202 }
203
204 /* Reset any information stored about a default file and line to print. */
205
206 void
207 clear_current_source_symtab_and_line (void)
208 {
209 current_source_symtab = 0;
210 current_source_line = 0;
211 }
212
213 /* Set the source file default for the "list" command to be S.
214
215 If S is NULL, and we don't have a default, find one. This
216 should only be called when the user actually tries to use the
217 default, since we produce an error if we can't find a reasonable
218 default. Also, since this can cause symbols to be read, doing it
219 before we need to would make things slower than necessary. */
220
221 void
222 select_source_symtab (struct symtab *s)
223 {
224 struct symtabs_and_lines sals;
225 struct symtab_and_line sal;
226 struct partial_symtab *ps;
227 struct partial_symtab *cs_pst = 0;
228 struct objfile *ofp;
229
230 if (s)
231 {
232 current_source_symtab = s;
233 current_source_line = 1;
234 return;
235 }
236
237 if (current_source_symtab)
238 return;
239
240 /* Make the default place to list be the function `main'
241 if one exists. */
242 if (lookup_symbol (main_name (), 0, VAR_DOMAIN, 0))
243 {
244 sals = decode_line_spec (main_name (), 1);
245 sal = sals.sals[0];
246 xfree (sals.sals);
247 current_source_symtab = sal.symtab;
248 current_source_line = max (sal.line - (lines_to_list - 1), 1);
249 if (current_source_symtab)
250 return;
251 }
252
253 /* Alright; find the last file in the symtab list (ignoring .h's
254 and namespace symtabs). */
255
256 current_source_line = 1;
257
258 for (ofp = object_files; ofp != NULL; ofp = ofp->next)
259 {
260 for (s = ofp->symtabs; s; s = s->next)
261 {
262 const char *name = s->filename;
263 int len = strlen (name);
264 if (!(len > 2 && (strcmp (&name[len - 2], ".h") == 0
265 || strcmp (name, "<<C++-namespaces>>") == 0)))
266 current_source_symtab = s;
267 }
268 }
269 if (current_source_symtab)
270 return;
271
272 /* How about the partial symbol tables? */
273
274 for (ofp = object_files; ofp != NULL; ofp = ofp->next)
275 {
276 for (ps = ofp->psymtabs; ps != NULL; ps = ps->next)
277 {
278 const char *name = ps->filename;
279 int len = strlen (name);
280 if (!(len > 2 && (strcmp (&name[len - 2], ".h") == 0
281 || strcmp (name, "<<C++-namespaces>>") == 0)))
282 cs_pst = ps;
283 }
284 }
285 if (cs_pst)
286 {
287 if (cs_pst->readin)
288 {
289 internal_error (__FILE__, __LINE__,
290 _("select_source_symtab: "
291 "readin pst found and no symtabs."));
292 }
293 else
294 {
295 current_source_symtab = PSYMTAB_TO_SYMTAB (cs_pst);
296 }
297 }
298 if (current_source_symtab)
299 return;
300
301 error (_("Can't find a default source file"));
302 }
303 \f
304 static void
305 show_directories (char *ignore, int from_tty)
306 {
307 puts_filtered ("Source directories searched: ");
308 puts_filtered (source_path);
309 puts_filtered ("\n");
310 }
311
312 /* Forget what we learned about line positions in source files, and
313 which directories contain them; must check again now since files
314 may be found in a different directory now. */
315
316 void
317 forget_cached_source_info (void)
318 {
319 struct symtab *s;
320 struct objfile *objfile;
321 struct partial_symtab *pst;
322
323 for (objfile = object_files; objfile != NULL; objfile = objfile->next)
324 {
325 for (s = objfile->symtabs; s != NULL; s = s->next)
326 {
327 if (s->line_charpos != NULL)
328 {
329 xfree (s->line_charpos);
330 s->line_charpos = NULL;
331 }
332 if (s->fullname != NULL)
333 {
334 xfree (s->fullname);
335 s->fullname = NULL;
336 }
337 }
338
339 ALL_OBJFILE_PSYMTABS (objfile, pst)
340 {
341 if (pst->fullname != NULL)
342 {
343 xfree (pst->fullname);
344 pst->fullname = NULL;
345 }
346 }
347 }
348 }
349
350 void
351 init_source_path (void)
352 {
353 char buf[20];
354
355 sprintf (buf, "$cdir%c$cwd", DIRNAME_SEPARATOR);
356 source_path = xstrdup (buf);
357 forget_cached_source_info ();
358 }
359
360 void
361 init_last_source_visited (void)
362 {
363 last_source_visited = NULL;
364 }
365
366 /* Add zero or more directories to the front of the source path. */
367
368 void
369 directory_command (char *dirname, int from_tty)
370 {
371 dont_repeat ();
372 /* FIXME, this goes to "delete dir"... */
373 if (dirname == 0)
374 {
375 if (!from_tty || query (_("Reinitialize source path to empty? ")))
376 {
377 xfree (source_path);
378 init_source_path ();
379 }
380 }
381 else
382 {
383 mod_path (dirname, &source_path);
384 last_source_visited = NULL;
385 }
386 if (from_tty)
387 show_directories ((char *) 0, from_tty);
388 forget_cached_source_info ();
389 }
390
391 /* Add a path given with the -d command line switch.
392 This will not be quoted so we must not treat spaces as separators. */
393
394 void
395 directory_switch (char *dirname, int from_tty)
396 {
397 add_path (dirname, &source_path, 0);
398 }
399
400 /* Add zero or more directories to the front of an arbitrary path. */
401
402 void
403 mod_path (char *dirname, char **which_path)
404 {
405 add_path (dirname, which_path, 1);
406 }
407
408 /* Workhorse of mod_path. Takes an extra argument to determine
409 if dirname should be parsed for separators that indicate multiple
410 directories. This allows for interfaces that pre-parse the dirname
411 and allow specification of traditional separator characters such
412 as space or tab. */
413
414 void
415 add_path (char *dirname, char **which_path, int parse_separators)
416 {
417 char *old = *which_path;
418 int prefix = 0;
419 char **argv = NULL;
420 char *arg;
421 int argv_index = 0;
422
423 if (dirname == 0)
424 return;
425
426 if (parse_separators)
427 {
428 /* This will properly parse the space and tab separators
429 and any quotes that may exist. DIRNAME_SEPARATOR will
430 be dealt with later. */
431 argv = gdb_buildargv (dirname);
432 make_cleanup_freeargv (argv);
433
434 arg = argv[0];
435 }
436 else
437 {
438 arg = xstrdup (dirname);
439 make_cleanup (xfree, arg);
440 }
441
442 do
443 {
444 char *name = arg;
445 char *p;
446 struct stat st;
447
448 {
449 char *separator = NULL;
450
451 /* Spaces and tabs will have been removed by buildargv().
452 The directories will there be split into a list but
453 each entry may still contain DIRNAME_SEPARATOR. */
454 if (parse_separators)
455 separator = strchr (name, DIRNAME_SEPARATOR);
456
457 if (separator == 0)
458 p = arg = name + strlen (name);
459 else
460 {
461 p = separator;
462 arg = p + 1;
463 while (*arg == DIRNAME_SEPARATOR)
464 ++arg;
465 }
466
467 /* If there are no more directories in this argument then start
468 on the next argument next time round the loop (if any). */
469 if (*arg == '\0')
470 arg = parse_separators ? argv[++argv_index] : NULL;
471 }
472
473 /* name is the start of the directory.
474 p is the separator (or null) following the end. */
475
476 while (!(IS_DIR_SEPARATOR (*name) && p <= name + 1) /* "/" */
477 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
478 /* On MS-DOS and MS-Windows, h:\ is different from h: */
479 && !(p == name + 3 && name[1] == ':') /* "d:/" */
480 #endif
481 && IS_DIR_SEPARATOR (p[-1]))
482 /* Sigh. "foo/" => "foo" */
483 --p;
484 *p = '\0';
485
486 while (p > name && p[-1] == '.')
487 {
488 if (p - name == 1)
489 {
490 /* "." => getwd (). */
491 name = current_directory;
492 goto append;
493 }
494 else if (p > name + 1 && IS_DIR_SEPARATOR (p[-2]))
495 {
496 if (p - name == 2)
497 {
498 /* "/." => "/". */
499 *--p = '\0';
500 goto append;
501 }
502 else
503 {
504 /* "...foo/." => "...foo". */
505 p -= 2;
506 *p = '\0';
507 continue;
508 }
509 }
510 else
511 break;
512 }
513
514 if (name[0] == '~')
515 name = tilde_expand (name);
516 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
517 else if (IS_ABSOLUTE_PATH (name) && p == name + 2) /* "d:" => "d:." */
518 name = concat (name, ".", (char *)NULL);
519 #endif
520 else if (!IS_ABSOLUTE_PATH (name) && name[0] != '$')
521 name = concat (current_directory, SLASH_STRING, name, (char *)NULL);
522 else
523 name = savestring (name, p - name);
524 make_cleanup (xfree, name);
525
526 /* Unless it's a variable, check existence. */
527 if (name[0] != '$')
528 {
529 /* These are warnings, not errors, since we don't want a
530 non-existent directory in a .gdbinit file to stop processing
531 of the .gdbinit file.
532
533 Whether they get added to the path is more debatable. Current
534 answer is yes, in case the user wants to go make the directory
535 or whatever. If the directory continues to not exist/not be
536 a directory/etc, then having them in the path should be
537 harmless. */
538 if (stat (name, &st) < 0)
539 {
540 int save_errno = errno;
541 fprintf_unfiltered (gdb_stderr, "Warning: ");
542 print_sys_errmsg (name, save_errno);
543 }
544 else if ((st.st_mode & S_IFMT) != S_IFDIR)
545 warning (_("%s is not a directory."), name);
546 }
547
548 append:
549 {
550 unsigned int len = strlen (name);
551
552 p = *which_path;
553 while (1)
554 {
555 /* FIXME: strncmp loses in interesting ways on MS-DOS and
556 MS-Windows because of case-insensitivity and two different
557 but functionally identical slash characters. We need a
558 special filesystem-dependent file-name comparison function.
559
560 Actually, even on Unix I would use realpath() or its work-
561 alike before comparing. Then all the code above which
562 removes excess slashes and dots could simply go away. */
563 if (!strncmp (p, name, len)
564 && (p[len] == '\0' || p[len] == DIRNAME_SEPARATOR))
565 {
566 /* Found it in the search path, remove old copy */
567 if (p > *which_path)
568 p--; /* Back over leading separator */
569 if (prefix > p - *which_path)
570 goto skip_dup; /* Same dir twice in one cmd */
571 strcpy (p, &p[len + 1]); /* Copy from next \0 or : */
572 }
573 p = strchr (p, DIRNAME_SEPARATOR);
574 if (p != 0)
575 ++p;
576 else
577 break;
578 }
579 if (p == 0)
580 {
581 char tinybuf[2];
582
583 tinybuf[0] = DIRNAME_SEPARATOR;
584 tinybuf[1] = '\0';
585
586 /* If we have already tacked on a name(s) in this command, be sure they stay
587 on the front as we tack on some more. */
588 if (prefix)
589 {
590 char *temp, c;
591
592 c = old[prefix];
593 old[prefix] = '\0';
594 temp = concat (old, tinybuf, name, (char *)NULL);
595 old[prefix] = c;
596 *which_path = concat (temp, "", &old[prefix], (char *)NULL);
597 prefix = strlen (temp);
598 xfree (temp);
599 }
600 else
601 {
602 *which_path = concat (name, (old[0] ? tinybuf : old),
603 old, (char *)NULL);
604 prefix = strlen (name);
605 }
606 xfree (old);
607 old = *which_path;
608 }
609 }
610 skip_dup:;
611 }
612 while (arg != NULL);
613 }
614
615
616 static void
617 source_info (char *ignore, int from_tty)
618 {
619 struct symtab *s = current_source_symtab;
620
621 if (!s)
622 {
623 printf_filtered (_("No current source file.\n"));
624 return;
625 }
626 printf_filtered (_("Current source file is %s\n"), s->filename);
627 if (s->dirname)
628 printf_filtered (_("Compilation directory is %s\n"), s->dirname);
629 if (s->fullname)
630 printf_filtered (_("Located in %s\n"), s->fullname);
631 if (s->nlines)
632 printf_filtered (_("Contains %d line%s.\n"), s->nlines,
633 s->nlines == 1 ? "" : "s");
634
635 printf_filtered (_("Source language is %s.\n"), language_str (s->language));
636 printf_filtered (_("Compiled with %s debugging format.\n"), s->debugformat);
637 printf_filtered (_("%s preprocessor macro info.\n"),
638 s->macro_table ? "Includes" : "Does not include");
639 }
640 \f
641
642 /* Return True if the file NAME exists and is a regular file */
643 static int
644 is_regular_file (const char *name)
645 {
646 struct stat st;
647 const int status = stat (name, &st);
648
649 /* Stat should never fail except when the file does not exist.
650 If stat fails, analyze the source of error and return True
651 unless the file does not exist, to avoid returning false results
652 on obscure systems where stat does not work as expected.
653 */
654 if (status != 0)
655 return (errno != ENOENT);
656
657 return S_ISREG (st.st_mode);
658 }
659
660 /* Open a file named STRING, searching path PATH (dir names sep by some char)
661 using mode MODE in the calls to open. You cannot use this function to
662 create files (O_CREAT).
663
664 OPTS specifies the function behaviour in specific cases.
665
666 If OPF_TRY_CWD_FIRST, try to open ./STRING before searching PATH.
667 (ie pretend the first element of PATH is "."). This also indicates
668 that a slash in STRING disables searching of the path (this is
669 so that "exec-file ./foo" or "symbol-file ./foo" insures that you
670 get that particular version of foo or an error message).
671
672 If OPTS has OPF_SEARCH_IN_PATH set, absolute names will also be
673 searched in path (we usually want this for source files but not for
674 executables).
675
676 If FILENAME_OPENED is non-null, set it to a newly allocated string naming
677 the actual file opened (this string will always start with a "/"). We
678 have to take special pains to avoid doubling the "/" between the directory
679 and the file, sigh! Emacs gets confuzzed by this when we print the
680 source file name!!!
681
682 If a file is found, return the descriptor.
683 Otherwise, return -1, with errno set for the last name we tried to open. */
684
685 /* >>>> This should only allow files of certain types,
686 >>>> eg executable, non-directory */
687 int
688 openp (const char *path, int opts, const char *string,
689 int mode, char **filename_opened)
690 {
691 int fd;
692 char *filename;
693 const char *p;
694 const char *p1;
695 int len;
696 int alloclen;
697
698 /* The open syscall MODE parameter is not specified. */
699 gdb_assert ((mode & O_CREAT) == 0);
700
701 if (!path)
702 path = ".";
703
704 mode |= O_BINARY;
705
706 if ((opts & OPF_TRY_CWD_FIRST) || IS_ABSOLUTE_PATH (string))
707 {
708 int i;
709
710 if (is_regular_file (string))
711 {
712 filename = alloca (strlen (string) + 1);
713 strcpy (filename, string);
714 fd = open (filename, mode);
715 if (fd >= 0)
716 goto done;
717 }
718 else
719 {
720 filename = NULL;
721 fd = -1;
722 }
723
724 if (!(opts & OPF_SEARCH_IN_PATH))
725 for (i = 0; string[i]; i++)
726 if (IS_DIR_SEPARATOR (string[i]))
727 goto done;
728 }
729
730 /* /foo => foo, to avoid multiple slashes that Emacs doesn't like. */
731 while (IS_DIR_SEPARATOR(string[0]))
732 string++;
733
734 /* ./foo => foo */
735 while (string[0] == '.' && IS_DIR_SEPARATOR (string[1]))
736 string += 2;
737
738 alloclen = strlen (path) + strlen (string) + 2;
739 filename = alloca (alloclen);
740 fd = -1;
741 for (p = path; p; p = p1 ? p1 + 1 : 0)
742 {
743 p1 = strchr (p, DIRNAME_SEPARATOR);
744 if (p1)
745 len = p1 - p;
746 else
747 len = strlen (p);
748
749 if (len == 4 && p[0] == '$' && p[1] == 'c'
750 && p[2] == 'w' && p[3] == 'd')
751 {
752 /* Name is $cwd -- insert current directory name instead. */
753 int newlen;
754
755 /* First, realloc the filename buffer if too short. */
756 len = strlen (current_directory);
757 newlen = len + strlen (string) + 2;
758 if (newlen > alloclen)
759 {
760 alloclen = newlen;
761 filename = alloca (alloclen);
762 }
763 strcpy (filename, current_directory);
764 }
765 else
766 {
767 /* Normal file name in path -- just use it. */
768 strncpy (filename, p, len);
769 filename[len] = 0;
770 }
771
772 /* Remove trailing slashes */
773 while (len > 0 && IS_DIR_SEPARATOR (filename[len - 1]))
774 filename[--len] = 0;
775
776 strcat (filename + len, SLASH_STRING);
777 strcat (filename, string);
778
779 if (is_regular_file (filename))
780 {
781 fd = open (filename, mode);
782 if (fd >= 0)
783 break;
784 }
785 }
786
787 done:
788 if (filename_opened)
789 {
790 /* If a file was opened, canonicalize its filename. Use xfullpath
791 rather than gdb_realpath to avoid resolving the basename part
792 of filenames when the associated file is a symbolic link. This
793 fixes a potential inconsistency between the filenames known to
794 GDB and the filenames it prints in the annotations. */
795 if (fd < 0)
796 *filename_opened = NULL;
797 else if (IS_ABSOLUTE_PATH (filename))
798 *filename_opened = xfullpath (filename);
799 else
800 {
801 /* Beware the // my son, the Emacs barfs, the botch that catch... */
802
803 char *f = concat (current_directory,
804 IS_DIR_SEPARATOR (current_directory[strlen (current_directory) - 1])
805 ? "" : SLASH_STRING,
806 filename, (char *)NULL);
807 *filename_opened = xfullpath (f);
808 xfree (f);
809 }
810 }
811
812 return fd;
813 }
814
815
816 /* This is essentially a convenience, for clients that want the behaviour
817 of openp, using source_path, but that really don't want the file to be
818 opened but want instead just to know what the full pathname is (as
819 qualified against source_path).
820
821 The current working directory is searched first.
822
823 If the file was found, this function returns 1, and FULL_PATHNAME is
824 set to the fully-qualified pathname.
825
826 Else, this functions returns 0, and FULL_PATHNAME is set to NULL. */
827 int
828 source_full_path_of (const char *filename, char **full_pathname)
829 {
830 int fd;
831
832 fd = openp (source_path, OPF_TRY_CWD_FIRST | OPF_SEARCH_IN_PATH, filename,
833 O_RDONLY, full_pathname);
834 if (fd < 0)
835 {
836 *full_pathname = NULL;
837 return 0;
838 }
839
840 close (fd);
841 return 1;
842 }
843
844 /* Return non-zero if RULE matches PATH, that is if the rule can be
845 applied to PATH. */
846
847 static int
848 substitute_path_rule_matches (const struct substitute_path_rule *rule,
849 const char *path)
850 {
851 const int from_len = strlen (rule->from);
852 const int path_len = strlen (path);
853 char *path_start;
854
855 if (path_len < from_len)
856 return 0;
857
858 /* The substitution rules are anchored at the start of the path,
859 so the path should start with rule->from. There is no filename
860 comparison routine, so we need to extract the first FROM_LEN
861 characters from PATH first and use that to do the comparison. */
862
863 path_start = alloca (from_len + 1);
864 strncpy (path_start, path, from_len);
865 path_start[from_len] = '\0';
866
867 if (FILENAME_CMP (path_start, rule->from) != 0)
868 return 0;
869
870 /* Make sure that the region in the path that matches the substitution
871 rule is immediately followed by a directory separator (or the end of
872 string character). */
873
874 if (path[from_len] != '\0' && !IS_DIR_SEPARATOR (path[from_len]))
875 return 0;
876
877 return 1;
878 }
879
880 /* Find the substitute-path rule that applies to PATH and return it.
881 Return NULL if no rule applies. */
882
883 static struct substitute_path_rule *
884 get_substitute_path_rule (const char *path)
885 {
886 struct substitute_path_rule *rule = substitute_path_rules;
887
888 while (rule != NULL && !substitute_path_rule_matches (rule, path))
889 rule = rule->next;
890
891 return rule;
892 }
893
894 /* If the user specified a source path substitution rule that applies
895 to PATH, then apply it and return the new path. This new path must
896 be deallocated afterwards.
897
898 Return NULL if no substitution rule was specified by the user,
899 or if no rule applied to the given PATH. */
900
901 static char *
902 rewrite_source_path (const char *path)
903 {
904 const struct substitute_path_rule *rule = get_substitute_path_rule (path);
905 char *new_path;
906 int from_len;
907
908 if (rule == NULL)
909 return NULL;
910
911 from_len = strlen (rule->from);
912
913 /* Compute the rewritten path and return it. */
914
915 new_path =
916 (char *) xmalloc (strlen (path) + 1 + strlen (rule->to) - from_len);
917 strcpy (new_path, rule->to);
918 strcat (new_path, path + from_len);
919
920 return new_path;
921 }
922
923 /* This function is capable of finding the absolute path to a
924 source file, and opening it, provided you give it an
925 OBJFILE and FILENAME. Both the DIRNAME and FULLNAME are only
926 added suggestions on where to find the file.
927
928 OBJFILE should be the objfile associated with a psymtab or symtab.
929 FILENAME should be the filename to open.
930 DIRNAME is the compilation directory of a particular source file.
931 Only some debug formats provide this info.
932 FULLNAME can be the last known absolute path to the file in question.
933 Space for the path must have been malloc'd. If a path substitution
934 is applied we free the old value and set a new one.
935
936 On Success
937 A valid file descriptor is returned. ( the return value is positive )
938 FULLNAME is set to the absolute path to the file just opened.
939 The caller is responsible for freeing FULLNAME.
940
941 On Failure
942 An invalid file descriptor is returned. ( the return value is negative )
943 FULLNAME is set to NULL. */
944
945 static int
946 find_and_open_source (struct objfile *objfile,
947 const char *filename,
948 const char *dirname,
949 char **fullname)
950 {
951 char *path = source_path;
952 const char *p;
953 int result;
954
955 /* Quick way out if we already know its full name */
956
957 if (*fullname)
958 {
959 /* The user may have requested that source paths be rewritten
960 according to substitution rules he provided. If a substitution
961 rule applies to this path, then apply it. */
962 char *rewritten_fullname = rewrite_source_path (*fullname);
963
964 if (rewritten_fullname != NULL)
965 {
966 xfree (*fullname);
967 *fullname = rewritten_fullname;
968 }
969
970 result = open (*fullname, OPEN_MODE);
971 if (result >= 0)
972 return result;
973 /* Didn't work -- free old one, try again. */
974 xfree (*fullname);
975 *fullname = NULL;
976 }
977
978 if (dirname != NULL)
979 {
980 /* If necessary, rewrite the compilation directory name according
981 to the source path substitution rules specified by the user. */
982
983 char *rewritten_dirname = rewrite_source_path (dirname);
984
985 if (rewritten_dirname != NULL)
986 {
987 make_cleanup (xfree, rewritten_dirname);
988 dirname = rewritten_dirname;
989 }
990
991 /* Replace a path entry of $cdir with the compilation directory name */
992 #define cdir_len 5
993 /* We cast strstr's result in case an ANSIhole has made it const,
994 which produces a "required warning" when assigned to a nonconst. */
995 p = (char *) strstr (source_path, "$cdir");
996 if (p && (p == path || p[-1] == DIRNAME_SEPARATOR)
997 && (p[cdir_len] == DIRNAME_SEPARATOR || p[cdir_len] == '\0'))
998 {
999 int len;
1000
1001 path = (char *)
1002 alloca (strlen (source_path) + 1 + strlen (dirname) + 1);
1003 len = p - source_path;
1004 strncpy (path, source_path, len); /* Before $cdir */
1005 strcpy (path + len, dirname); /* new stuff */
1006 strcat (path + len, source_path + len + cdir_len); /* After $cdir */
1007 }
1008 }
1009
1010 if (IS_ABSOLUTE_PATH (filename))
1011 {
1012 /* If filename is absolute path, try the source path
1013 substitution on it. */
1014 char *rewritten_filename = rewrite_source_path (filename);
1015
1016 if (rewritten_filename != NULL)
1017 {
1018 make_cleanup (xfree, rewritten_filename);
1019 filename = rewritten_filename;
1020 }
1021 }
1022
1023 result = openp (path, OPF_SEARCH_IN_PATH, filename, OPEN_MODE, fullname);
1024 if (result < 0)
1025 {
1026 /* Didn't work. Try using just the basename. */
1027 p = lbasename (filename);
1028 if (p != filename)
1029 result = openp (path, OPF_SEARCH_IN_PATH, p, OPEN_MODE, fullname);
1030 }
1031
1032 return result;
1033 }
1034
1035 /* Open a source file given a symtab S. Returns a file descriptor or
1036 negative number for error.
1037
1038 This function is a convience function to find_and_open_source. */
1039
1040 int
1041 open_source_file (struct symtab *s)
1042 {
1043 if (!s)
1044 return -1;
1045
1046 return find_and_open_source (s->objfile, s->filename, s->dirname,
1047 &s->fullname);
1048 }
1049
1050 /* Finds the fullname that a symtab represents.
1051
1052 If this functions finds the fullname, it will save it in s->fullname
1053 and it will also return the value.
1054
1055 If this function fails to find the file that this symtab represents,
1056 NULL will be returned and s->fullname will be set to NULL. */
1057 char *
1058 symtab_to_fullname (struct symtab *s)
1059 {
1060 int r;
1061
1062 if (!s)
1063 return NULL;
1064
1065 /* Don't check s->fullname here, the file could have been
1066 deleted/moved/..., look for it again */
1067 r = find_and_open_source (s->objfile, s->filename, s->dirname,
1068 &s->fullname);
1069
1070 if (r >= 0)
1071 {
1072 close (r);
1073 return s->fullname;
1074 }
1075
1076 return NULL;
1077 }
1078
1079 /* Finds the fullname that a partial_symtab represents.
1080
1081 If this functions finds the fullname, it will save it in ps->fullname
1082 and it will also return the value.
1083
1084 If this function fails to find the file that this partial_symtab represents,
1085 NULL will be returned and ps->fullname will be set to NULL. */
1086 char *
1087 psymtab_to_fullname (struct partial_symtab *ps)
1088 {
1089 int r;
1090
1091 if (!ps)
1092 return NULL;
1093
1094 /* Don't check ps->fullname here, the file could have been
1095 deleted/moved/..., look for it again */
1096 r = find_and_open_source (ps->objfile, ps->filename, ps->dirname,
1097 &ps->fullname);
1098
1099 if (r >= 0)
1100 {
1101 close (r);
1102 return ps->fullname;
1103 }
1104
1105 return NULL;
1106 }
1107 \f
1108 /* Create and initialize the table S->line_charpos that records
1109 the positions of the lines in the source file, which is assumed
1110 to be open on descriptor DESC.
1111 All set S->nlines to the number of such lines. */
1112
1113 void
1114 find_source_lines (struct symtab *s, int desc)
1115 {
1116 struct stat st;
1117 char *data, *p, *end;
1118 int nlines = 0;
1119 int lines_allocated = 1000;
1120 int *line_charpos;
1121 long mtime = 0;
1122 int size;
1123
1124 gdb_assert (s);
1125 line_charpos = (int *) xmalloc (lines_allocated * sizeof (int));
1126 if (fstat (desc, &st) < 0)
1127 perror_with_name (s->filename);
1128
1129 if (s->objfile && s->objfile->obfd)
1130 mtime = s->objfile->mtime;
1131 else if (exec_bfd)
1132 mtime = exec_bfd_mtime;
1133
1134 if (mtime && mtime < st.st_mtime)
1135 warning (_("Source file is more recent than executable."));
1136
1137 #ifdef LSEEK_NOT_LINEAR
1138 {
1139 char c;
1140
1141 /* Have to read it byte by byte to find out where the chars live */
1142
1143 line_charpos[0] = lseek (desc, 0, SEEK_CUR);
1144 nlines = 1;
1145 while (myread (desc, &c, 1) > 0)
1146 {
1147 if (c == '\n')
1148 {
1149 if (nlines == lines_allocated)
1150 {
1151 lines_allocated *= 2;
1152 line_charpos =
1153 (int *) xrealloc ((char *) line_charpos,
1154 sizeof (int) * lines_allocated);
1155 }
1156 line_charpos[nlines++] = lseek (desc, 0, SEEK_CUR);
1157 }
1158 }
1159 }
1160 #else /* lseek linear. */
1161 {
1162 struct cleanup *old_cleanups;
1163
1164 /* st_size might be a large type, but we only support source files whose
1165 size fits in an int. */
1166 size = (int) st.st_size;
1167
1168 /* Use malloc, not alloca, because this may be pretty large, and we may
1169 run into various kinds of limits on stack size. */
1170 data = (char *) xmalloc (size);
1171 old_cleanups = make_cleanup (xfree, data);
1172
1173 /* Reassign `size' to result of read for systems where \r\n -> \n. */
1174 size = myread (desc, data, size);
1175 if (size < 0)
1176 perror_with_name (s->filename);
1177 end = data + size;
1178 p = data;
1179 line_charpos[0] = 0;
1180 nlines = 1;
1181 while (p != end)
1182 {
1183 if (*p++ == '\n'
1184 /* A newline at the end does not start a new line. */
1185 && p != end)
1186 {
1187 if (nlines == lines_allocated)
1188 {
1189 lines_allocated *= 2;
1190 line_charpos =
1191 (int *) xrealloc ((char *) line_charpos,
1192 sizeof (int) * lines_allocated);
1193 }
1194 line_charpos[nlines++] = p - data;
1195 }
1196 }
1197 do_cleanups (old_cleanups);
1198 }
1199 #endif /* lseek linear. */
1200 s->nlines = nlines;
1201 s->line_charpos =
1202 (int *) xrealloc ((char *) line_charpos, nlines * sizeof (int));
1203
1204 }
1205
1206 /* Return the character position of a line LINE in symtab S.
1207 Return 0 if anything is invalid. */
1208
1209 #if 0 /* Currently unused */
1210
1211 int
1212 source_line_charpos (struct symtab *s, int line)
1213 {
1214 if (!s)
1215 return 0;
1216 if (!s->line_charpos || line <= 0)
1217 return 0;
1218 if (line > s->nlines)
1219 line = s->nlines;
1220 return s->line_charpos[line - 1];
1221 }
1222
1223 /* Return the line number of character position POS in symtab S. */
1224
1225 int
1226 source_charpos_line (struct symtab *s, int chr)
1227 {
1228 int line = 0;
1229 int *lnp;
1230
1231 if (s == 0 || s->line_charpos == 0)
1232 return 0;
1233 lnp = s->line_charpos;
1234 /* Files are usually short, so sequential search is Ok */
1235 while (line < s->nlines && *lnp <= chr)
1236 {
1237 line++;
1238 lnp++;
1239 }
1240 if (line >= s->nlines)
1241 line = s->nlines;
1242 return line;
1243 }
1244
1245 #endif /* 0 */
1246 \f
1247
1248 /* Get full pathname and line number positions for a symtab.
1249 Return nonzero if line numbers may have changed.
1250 Set *FULLNAME to actual name of the file as found by `openp',
1251 or to 0 if the file is not found. */
1252
1253 static int
1254 get_filename_and_charpos (struct symtab *s, char **fullname)
1255 {
1256 int desc, linenums_changed = 0;
1257 struct cleanup *cleanups;
1258
1259 desc = open_source_file (s);
1260 if (desc < 0)
1261 {
1262 if (fullname)
1263 *fullname = NULL;
1264 return 0;
1265 }
1266 cleanups = make_cleanup_close (desc);
1267 if (fullname)
1268 *fullname = s->fullname;
1269 if (s->line_charpos == 0)
1270 linenums_changed = 1;
1271 if (linenums_changed)
1272 find_source_lines (s, desc);
1273 do_cleanups (cleanups);
1274 return linenums_changed;
1275 }
1276
1277 /* Print text describing the full name of the source file S
1278 and the line number LINE and its corresponding character position.
1279 The text starts with two Ctrl-z so that the Emacs-GDB interface
1280 can easily find it.
1281
1282 MID_STATEMENT is nonzero if the PC is not at the beginning of that line.
1283
1284 Return 1 if successful, 0 if could not find the file. */
1285
1286 int
1287 identify_source_line (struct symtab *s, int line, int mid_statement,
1288 CORE_ADDR pc)
1289 {
1290 if (s->line_charpos == 0)
1291 get_filename_and_charpos (s, (char **) NULL);
1292 if (s->fullname == 0)
1293 return 0;
1294 if (line > s->nlines)
1295 /* Don't index off the end of the line_charpos array. */
1296 return 0;
1297 annotate_source (s->fullname, line, s->line_charpos[line - 1],
1298 mid_statement, pc);
1299
1300 current_source_line = line;
1301 first_line_listed = line;
1302 last_line_listed = line;
1303 current_source_symtab = s;
1304 return 1;
1305 }
1306 \f
1307
1308 /* Print source lines from the file of symtab S,
1309 starting with line number LINE and stopping before line number STOPLINE. */
1310
1311 static void print_source_lines_base (struct symtab *s, int line, int stopline,
1312 int noerror);
1313 static void
1314 print_source_lines_base (struct symtab *s, int line, int stopline, int noerror)
1315 {
1316 int c;
1317 int desc;
1318 FILE *stream;
1319 int nlines = stopline - line;
1320 struct cleanup *cleanup;
1321
1322 /* Regardless of whether we can open the file, set current_source_symtab. */
1323 current_source_symtab = s;
1324 current_source_line = line;
1325 first_line_listed = line;
1326
1327 /* If printing of source lines is disabled, just print file and line number */
1328 if (ui_out_test_flags (uiout, ui_source_list))
1329 {
1330 /* Only prints "No such file or directory" once */
1331 if ((s != last_source_visited) || (!last_source_error))
1332 {
1333 last_source_visited = s;
1334 desc = open_source_file (s);
1335 }
1336 else
1337 {
1338 desc = last_source_error;
1339 noerror = 1;
1340 }
1341 }
1342 else
1343 {
1344 desc = -1;
1345 noerror = 1;
1346 }
1347
1348 if (desc < 0)
1349 {
1350 last_source_error = desc;
1351
1352 if (!noerror)
1353 {
1354 char *name = alloca (strlen (s->filename) + 100);
1355 sprintf (name, "%d\t%s", line, s->filename);
1356 print_sys_errmsg (name, errno);
1357 }
1358 else
1359 ui_out_field_int (uiout, "line", line);
1360 ui_out_text (uiout, "\tin ");
1361 ui_out_field_string (uiout, "file", s->filename);
1362 ui_out_text (uiout, "\n");
1363
1364 return;
1365 }
1366
1367 last_source_error = 0;
1368
1369 if (s->line_charpos == 0)
1370 find_source_lines (s, desc);
1371
1372 if (line < 1 || line > s->nlines)
1373 {
1374 close (desc);
1375 error (_("Line number %d out of range; %s has %d lines."),
1376 line, s->filename, s->nlines);
1377 }
1378
1379 if (lseek (desc, s->line_charpos[line - 1], 0) < 0)
1380 {
1381 close (desc);
1382 perror_with_name (s->filename);
1383 }
1384
1385 stream = fdopen (desc, FDOPEN_MODE);
1386 clearerr (stream);
1387 cleanup = make_cleanup_fclose (stream);
1388
1389 while (nlines-- > 0)
1390 {
1391 char buf[20];
1392
1393 c = fgetc (stream);
1394 if (c == EOF)
1395 break;
1396 last_line_listed = current_source_line;
1397 sprintf (buf, "%d\t", current_source_line++);
1398 ui_out_text (uiout, buf);
1399 do
1400 {
1401 if (c < 040 && c != '\t' && c != '\n' && c != '\r')
1402 {
1403 sprintf (buf, "^%c", c + 0100);
1404 ui_out_text (uiout, buf);
1405 }
1406 else if (c == 0177)
1407 ui_out_text (uiout, "^?");
1408 else if (c == '\r')
1409 {
1410 /* Skip a \r character, but only before a \n. */
1411 int c1 = fgetc (stream);
1412
1413 if (c1 != '\n')
1414 printf_filtered ("^%c", c + 0100);
1415 if (c1 != EOF)
1416 ungetc (c1, stream);
1417 }
1418 else
1419 {
1420 sprintf (buf, "%c", c);
1421 ui_out_text (uiout, buf);
1422 }
1423 }
1424 while (c != '\n' && (c = fgetc (stream)) >= 0);
1425 }
1426
1427 do_cleanups (cleanup);
1428 }
1429 \f
1430 /* Show source lines from the file of symtab S, starting with line
1431 number LINE and stopping before line number STOPLINE. If this is
1432 not the command line version, then the source is shown in the source
1433 window otherwise it is simply printed */
1434
1435 void
1436 print_source_lines (struct symtab *s, int line, int stopline, int noerror)
1437 {
1438 print_source_lines_base (s, line, stopline, noerror);
1439 }
1440 \f
1441 /* Print info on range of pc's in a specified line. */
1442
1443 static void
1444 line_info (char *arg, int from_tty)
1445 {
1446 struct symtabs_and_lines sals;
1447 struct symtab_and_line sal;
1448 CORE_ADDR start_pc, end_pc;
1449 int i;
1450
1451 init_sal (&sal); /* initialize to zeroes */
1452
1453 if (arg == 0)
1454 {
1455 sal.symtab = current_source_symtab;
1456 sal.line = last_line_listed;
1457 sals.nelts = 1;
1458 sals.sals = (struct symtab_and_line *)
1459 xmalloc (sizeof (struct symtab_and_line));
1460 sals.sals[0] = sal;
1461 }
1462 else
1463 {
1464 sals = decode_line_spec_1 (arg, 0);
1465
1466 dont_repeat ();
1467 }
1468
1469 /* C++ More than one line may have been specified, as when the user
1470 specifies an overloaded function name. Print info on them all. */
1471 for (i = 0; i < sals.nelts; i++)
1472 {
1473 sal = sals.sals[i];
1474
1475 if (sal.symtab == 0)
1476 {
1477 printf_filtered (_("No line number information available"));
1478 if (sal.pc != 0)
1479 {
1480 /* This is useful for "info line *0x7f34". If we can't tell the
1481 user about a source line, at least let them have the symbolic
1482 address. */
1483 printf_filtered (" for address ");
1484 wrap_here (" ");
1485 print_address (sal.pc, gdb_stdout);
1486 }
1487 else
1488 printf_filtered (".");
1489 printf_filtered ("\n");
1490 }
1491 else if (sal.line > 0
1492 && find_line_pc_range (sal, &start_pc, &end_pc))
1493 {
1494 if (start_pc == end_pc)
1495 {
1496 printf_filtered ("Line %d of \"%s\"",
1497 sal.line, sal.symtab->filename);
1498 wrap_here (" ");
1499 printf_filtered (" is at address ");
1500 print_address (start_pc, gdb_stdout);
1501 wrap_here (" ");
1502 printf_filtered (" but contains no code.\n");
1503 }
1504 else
1505 {
1506 printf_filtered ("Line %d of \"%s\"",
1507 sal.line, sal.symtab->filename);
1508 wrap_here (" ");
1509 printf_filtered (" starts at address ");
1510 print_address (start_pc, gdb_stdout);
1511 wrap_here (" ");
1512 printf_filtered (" and ends at ");
1513 print_address (end_pc, gdb_stdout);
1514 printf_filtered (".\n");
1515 }
1516
1517 /* x/i should display this line's code. */
1518 set_next_address (get_objfile_arch (sal.symtab->objfile), start_pc);
1519
1520 /* Repeating "info line" should do the following line. */
1521 last_line_listed = sal.line + 1;
1522
1523 /* If this is the only line, show the source code. If it could
1524 not find the file, don't do anything special. */
1525 if (annotation_level && sals.nelts == 1)
1526 identify_source_line (sal.symtab, sal.line, 0, start_pc);
1527 }
1528 else
1529 /* Is there any case in which we get here, and have an address
1530 which the user would want to see? If we have debugging symbols
1531 and no line numbers? */
1532 printf_filtered (_("Line number %d is out of range for \"%s\".\n"),
1533 sal.line, sal.symtab->filename);
1534 }
1535 xfree (sals.sals);
1536 }
1537 \f
1538 /* Commands to search the source file for a regexp. */
1539
1540 static void
1541 forward_search_command (char *regex, int from_tty)
1542 {
1543 int c;
1544 int desc;
1545 FILE *stream;
1546 int line;
1547 char *msg;
1548 struct cleanup *cleanups;
1549
1550 line = last_line_listed + 1;
1551
1552 msg = (char *) re_comp (regex);
1553 if (msg)
1554 error (("%s"), msg);
1555
1556 if (current_source_symtab == 0)
1557 select_source_symtab (0);
1558
1559 desc = open_source_file (current_source_symtab);
1560 if (desc < 0)
1561 perror_with_name (current_source_symtab->filename);
1562 cleanups = make_cleanup_close (desc);
1563
1564 if (current_source_symtab->line_charpos == 0)
1565 find_source_lines (current_source_symtab, desc);
1566
1567 if (line < 1 || line > current_source_symtab->nlines)
1568 error (_("Expression not found"));
1569
1570 if (lseek (desc, current_source_symtab->line_charpos[line - 1], 0) < 0)
1571 perror_with_name (current_source_symtab->filename);
1572
1573 discard_cleanups (cleanups);
1574 stream = fdopen (desc, FDOPEN_MODE);
1575 clearerr (stream);
1576 cleanups = make_cleanup_fclose (stream);
1577 while (1)
1578 {
1579 static char *buf = NULL;
1580 char *p;
1581 int cursize, newsize;
1582
1583 cursize = 256;
1584 buf = xmalloc (cursize);
1585 p = buf;
1586
1587 c = getc (stream);
1588 if (c == EOF)
1589 break;
1590 do
1591 {
1592 *p++ = c;
1593 if (p - buf == cursize)
1594 {
1595 newsize = cursize + cursize / 2;
1596 buf = xrealloc (buf, newsize);
1597 p = buf + cursize;
1598 cursize = newsize;
1599 }
1600 }
1601 while (c != '\n' && (c = getc (stream)) >= 0);
1602
1603 /* Remove the \r, if any, at the end of the line, otherwise
1604 regular expressions that end with $ or \n won't work. */
1605 if (p - buf > 1 && p[-2] == '\r')
1606 {
1607 p--;
1608 p[-1] = '\n';
1609 }
1610
1611 /* we now have a source line in buf, null terminate and match */
1612 *p = 0;
1613 if (re_exec (buf) > 0)
1614 {
1615 /* Match! */
1616 fclose (stream);
1617 print_source_lines (current_source_symtab, line, line + 1, 0);
1618 set_internalvar_integer (lookup_internalvar ("_"), line);
1619 current_source_line = max (line - lines_to_list / 2, 1);
1620 return;
1621 }
1622 line++;
1623 }
1624
1625 printf_filtered (_("Expression not found\n"));
1626 do_cleanups (cleanups);
1627 }
1628
1629 static void
1630 reverse_search_command (char *regex, int from_tty)
1631 {
1632 int c;
1633 int desc;
1634 FILE *stream;
1635 int line;
1636 char *msg;
1637 struct cleanup *cleanups;
1638
1639 line = last_line_listed - 1;
1640
1641 msg = (char *) re_comp (regex);
1642 if (msg)
1643 error (("%s"), msg);
1644
1645 if (current_source_symtab == 0)
1646 select_source_symtab (0);
1647
1648 desc = open_source_file (current_source_symtab);
1649 if (desc < 0)
1650 perror_with_name (current_source_symtab->filename);
1651 cleanups = make_cleanup_close (desc);
1652
1653 if (current_source_symtab->line_charpos == 0)
1654 find_source_lines (current_source_symtab, desc);
1655
1656 if (line < 1 || line > current_source_symtab->nlines)
1657 error (_("Expression not found"));
1658
1659 if (lseek (desc, current_source_symtab->line_charpos[line - 1], 0) < 0)
1660 perror_with_name (current_source_symtab->filename);
1661
1662 discard_cleanups (cleanups);
1663 stream = fdopen (desc, FDOPEN_MODE);
1664 clearerr (stream);
1665 cleanups = make_cleanup_fclose (stream);
1666 while (line > 1)
1667 {
1668 /* FIXME!!! We walk right off the end of buf if we get a long line!!! */
1669 char buf[4096]; /* Should be reasonable??? */
1670 char *p = buf;
1671
1672 c = getc (stream);
1673 if (c == EOF)
1674 break;
1675 do
1676 {
1677 *p++ = c;
1678 }
1679 while (c != '\n' && (c = getc (stream)) >= 0);
1680
1681 /* Remove the \r, if any, at the end of the line, otherwise
1682 regular expressions that end with $ or \n won't work. */
1683 if (p - buf > 1 && p[-2] == '\r')
1684 {
1685 p--;
1686 p[-1] = '\n';
1687 }
1688
1689 /* We now have a source line in buf; null terminate and match. */
1690 *p = 0;
1691 if (re_exec (buf) > 0)
1692 {
1693 /* Match! */
1694 fclose (stream);
1695 print_source_lines (current_source_symtab, line, line + 1, 0);
1696 set_internalvar_integer (lookup_internalvar ("_"), line);
1697 current_source_line = max (line - lines_to_list / 2, 1);
1698 return;
1699 }
1700 line--;
1701 if (fseek (stream, current_source_symtab->line_charpos[line - 1], 0) < 0)
1702 {
1703 fclose (stream);
1704 perror_with_name (current_source_symtab->filename);
1705 }
1706 }
1707
1708 printf_filtered (_("Expression not found\n"));
1709 do_cleanups (cleanups);
1710 return;
1711 }
1712
1713 /* If the last character of PATH is a directory separator, then strip it. */
1714
1715 static void
1716 strip_trailing_directory_separator (char *path)
1717 {
1718 const int last = strlen (path) - 1;
1719
1720 if (last < 0)
1721 return; /* No stripping is needed if PATH is the empty string. */
1722
1723 if (IS_DIR_SEPARATOR (path[last]))
1724 path[last] = '\0';
1725 }
1726
1727 /* Return the path substitution rule that matches FROM.
1728 Return NULL if no rule matches. */
1729
1730 static struct substitute_path_rule *
1731 find_substitute_path_rule (const char *from)
1732 {
1733 struct substitute_path_rule *rule = substitute_path_rules;
1734
1735 while (rule != NULL)
1736 {
1737 if (FILENAME_CMP (rule->from, from) == 0)
1738 return rule;
1739 rule = rule->next;
1740 }
1741
1742 return NULL;
1743 }
1744
1745 /* Add a new substitute-path rule at the end of the current list of rules.
1746 The new rule will replace FROM into TO. */
1747
1748 void
1749 add_substitute_path_rule (char *from, char *to)
1750 {
1751 struct substitute_path_rule *rule;
1752 struct substitute_path_rule *new_rule;
1753
1754 new_rule = xmalloc (sizeof (struct substitute_path_rule));
1755 new_rule->from = xstrdup (from);
1756 new_rule->to = xstrdup (to);
1757 new_rule->next = NULL;
1758
1759 /* If the list of rules are empty, then insert the new rule
1760 at the head of the list. */
1761
1762 if (substitute_path_rules == NULL)
1763 {
1764 substitute_path_rules = new_rule;
1765 return;
1766 }
1767
1768 /* Otherwise, skip to the last rule in our list and then append
1769 the new rule. */
1770
1771 rule = substitute_path_rules;
1772 while (rule->next != NULL)
1773 rule = rule->next;
1774
1775 rule->next = new_rule;
1776 }
1777
1778 /* Remove the given source path substitution rule from the current list
1779 of rules. The memory allocated for that rule is also deallocated. */
1780
1781 static void
1782 delete_substitute_path_rule (struct substitute_path_rule *rule)
1783 {
1784 if (rule == substitute_path_rules)
1785 substitute_path_rules = rule->next;
1786 else
1787 {
1788 struct substitute_path_rule *prev = substitute_path_rules;
1789
1790 while (prev != NULL && prev->next != rule)
1791 prev = prev->next;
1792
1793 gdb_assert (prev != NULL);
1794
1795 prev->next = rule->next;
1796 }
1797
1798 xfree (rule->from);
1799 xfree (rule->to);
1800 xfree (rule);
1801 }
1802
1803 /* Implement the "show substitute-path" command. */
1804
1805 static void
1806 show_substitute_path_command (char *args, int from_tty)
1807 {
1808 struct substitute_path_rule *rule = substitute_path_rules;
1809 char **argv;
1810 char *from = NULL;
1811
1812 argv = gdb_buildargv (args);
1813 make_cleanup_freeargv (argv);
1814
1815 /* We expect zero or one argument. */
1816
1817 if (argv != NULL && argv[0] != NULL && argv[1] != NULL)
1818 error (_("Too many arguments in command"));
1819
1820 if (argv != NULL && argv[0] != NULL)
1821 from = argv[0];
1822
1823 /* Print the substitution rules. */
1824
1825 if (from != NULL)
1826 printf_filtered
1827 (_("Source path substitution rule matching `%s':\n"), from);
1828 else
1829 printf_filtered (_("List of all source path substitution rules:\n"));
1830
1831 while (rule != NULL)
1832 {
1833 if (from == NULL || FILENAME_CMP (rule->from, from) == 0)
1834 printf_filtered (" `%s' -> `%s'.\n", rule->from, rule->to);
1835 rule = rule->next;
1836 }
1837 }
1838
1839 /* Implement the "unset substitute-path" command. */
1840
1841 static void
1842 unset_substitute_path_command (char *args, int from_tty)
1843 {
1844 struct substitute_path_rule *rule = substitute_path_rules;
1845 char **argv = gdb_buildargv (args);
1846 char *from = NULL;
1847 int rule_found = 0;
1848
1849 /* This function takes either 0 or 1 argument. */
1850
1851 make_cleanup_freeargv (argv);
1852 if (argv != NULL && argv[0] != NULL && argv[1] != NULL)
1853 error (_("Incorrect usage, too many arguments in command"));
1854
1855 if (argv != NULL && argv[0] != NULL)
1856 from = argv[0];
1857
1858 /* If the user asked for all the rules to be deleted, ask him
1859 to confirm and give him a chance to abort before the action
1860 is performed. */
1861
1862 if (from == NULL
1863 && !query (_("Delete all source path substitution rules? ")))
1864 error (_("Canceled"));
1865
1866 /* Delete the rule matching the argument. No argument means that
1867 all rules should be deleted. */
1868
1869 while (rule != NULL)
1870 {
1871 struct substitute_path_rule *next = rule->next;
1872
1873 if (from == NULL || FILENAME_CMP (from, rule->from) == 0)
1874 {
1875 delete_substitute_path_rule (rule);
1876 rule_found = 1;
1877 }
1878
1879 rule = next;
1880 }
1881
1882 /* If the user asked for a specific rule to be deleted but
1883 we could not find it, then report an error. */
1884
1885 if (from != NULL && !rule_found)
1886 error (_("No substitution rule defined for `%s'"), from);
1887 }
1888
1889 /* Add a new source path substitution rule. */
1890
1891 static void
1892 set_substitute_path_command (char *args, int from_tty)
1893 {
1894 char *from_path, *to_path;
1895 char **argv;
1896 struct substitute_path_rule *rule;
1897
1898 argv = gdb_buildargv (args);
1899 make_cleanup_freeargv (argv);
1900
1901 if (argv == NULL || argv[0] == NULL || argv [1] == NULL)
1902 error (_("Incorrect usage, too few arguments in command"));
1903
1904 if (argv[2] != NULL)
1905 error (_("Incorrect usage, too many arguments in command"));
1906
1907 if (*(argv[0]) == '\0')
1908 error (_("First argument must be at least one character long"));
1909
1910 /* Strip any trailing directory separator character in either FROM
1911 or TO. The substitution rule already implicitly contains them. */
1912 strip_trailing_directory_separator (argv[0]);
1913 strip_trailing_directory_separator (argv[1]);
1914
1915 /* If a rule with the same "from" was previously defined, then
1916 delete it. This new rule replaces it. */
1917
1918 rule = find_substitute_path_rule (argv[0]);
1919 if (rule != NULL)
1920 delete_substitute_path_rule (rule);
1921
1922 /* Insert the new substitution rule. */
1923
1924 add_substitute_path_rule (argv[0], argv[1]);
1925 }
1926
1927 \f
1928 void
1929 _initialize_source (void)
1930 {
1931 struct cmd_list_element *c;
1932 current_source_symtab = 0;
1933 init_source_path ();
1934
1935 /* The intention is to use POSIX Basic Regular Expressions.
1936 Always use the GNU regex routine for consistency across all hosts.
1937 Our current GNU regex.c does not have all the POSIX features, so this is
1938 just an approximation. */
1939 re_set_syntax (RE_SYNTAX_GREP);
1940
1941 c = add_cmd ("directory", class_files, directory_command, _("\
1942 Add directory DIR to beginning of search path for source files.\n\
1943 Forget cached info on source file locations and line positions.\n\
1944 DIR can also be $cwd for the current working directory, or $cdir for the\n\
1945 directory in which the source file was compiled into object code.\n\
1946 With no argument, reset the search path to $cdir:$cwd, the default."),
1947 &cmdlist);
1948
1949 if (dbx_commands)
1950 add_com_alias ("use", "directory", class_files, 0);
1951
1952 set_cmd_completer (c, filename_completer);
1953
1954 add_cmd ("directories", no_class, show_directories, _("\
1955 Current search path for finding source files.\n\
1956 $cwd in the path means the current working directory.\n\
1957 $cdir in the path means the compilation directory of the source file."),
1958 &showlist);
1959
1960 if (xdb_commands)
1961 {
1962 add_com_alias ("D", "directory", class_files, 0);
1963 add_cmd ("ld", no_class, show_directories, _("\
1964 Current search path for finding source files.\n\
1965 $cwd in the path means the current working directory.\n\
1966 $cdir in the path means the compilation directory of the source file."),
1967 &cmdlist);
1968 }
1969
1970 add_info ("source", source_info,
1971 _("Information about the current source file."));
1972
1973 add_info ("line", line_info, _("\
1974 Core addresses of the code for a source line.\n\
1975 Line can be specified as\n\
1976 LINENUM, to list around that line in current file,\n\
1977 FILE:LINENUM, to list around that line in that file,\n\
1978 FUNCTION, to list around beginning of that function,\n\
1979 FILE:FUNCTION, to distinguish among like-named static functions.\n\
1980 Default is to describe the last source line that was listed.\n\n\
1981 This sets the default address for \"x\" to the line's first instruction\n\
1982 so that \"x/i\" suffices to start examining the machine code.\n\
1983 The address is also stored as the value of \"$_\"."));
1984
1985 add_com ("forward-search", class_files, forward_search_command, _("\
1986 Search for regular expression (see regex(3)) from last line listed.\n\
1987 The matching line number is also stored as the value of \"$_\"."));
1988 add_com_alias ("search", "forward-search", class_files, 0);
1989
1990 add_com ("reverse-search", class_files, reverse_search_command, _("\
1991 Search backward for regular expression (see regex(3)) from last line listed.\n\
1992 The matching line number is also stored as the value of \"$_\"."));
1993
1994 if (xdb_commands)
1995 {
1996 add_com_alias ("/", "forward-search", class_files, 0);
1997 add_com_alias ("?", "reverse-search", class_files, 0);
1998 }
1999
2000 add_setshow_integer_cmd ("listsize", class_support, &lines_to_list, _("\
2001 Set number of source lines gdb will list by default."), _("\
2002 Show number of source lines gdb will list by default."), NULL,
2003 NULL,
2004 show_lines_to_list,
2005 &setlist, &showlist);
2006
2007 add_cmd ("substitute-path", class_files, set_substitute_path_command,
2008 _("\
2009 Usage: set substitute-path FROM TO\n\
2010 Add a substitution rule replacing FROM into TO in source file names.\n\
2011 If a substitution rule was previously set for FROM, the old rule\n\
2012 is replaced by the new one."),
2013 &setlist);
2014
2015 add_cmd ("substitute-path", class_files, unset_substitute_path_command,
2016 _("\
2017 Usage: unset substitute-path [FROM]\n\
2018 Delete the rule for substituting FROM in source file names. If FROM\n\
2019 is not specified, all substituting rules are deleted.\n\
2020 If the debugger cannot find a rule for FROM, it will display a warning."),
2021 &unsetlist);
2022
2023 add_cmd ("substitute-path", class_files, show_substitute_path_command,
2024 _("\
2025 Usage: show substitute-path [FROM]\n\
2026 Print the rule for substituting FROM in source file names. If FROM\n\
2027 is not specified, print all substitution rules."),
2028 &showlist);
2029 }
This page took 0.099706 seconds and 4 git commands to generate.