2004-01-18 Andrew Cagney <cagney@redhat.com>
[deliverable/binutils-gdb.git] / gdb / source.c
1 /* List lines of source files for GDB, the GNU debugger.
2 Copyright 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994,
3 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003
4 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 2 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, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
22
23 #include "defs.h"
24 #include "symtab.h"
25 #include "expression.h"
26 #include "language.h"
27 #include "command.h"
28 #include "source.h"
29 #include "gdbcmd.h"
30 #include "frame.h"
31 #include "value.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 #ifdef CRLF_SOURCE_FILES
50
51 /* Define CRLF_SOURCE_FILES in an xm-*.h file if source files on the
52 host use \r\n rather than just \n. Defining CRLF_SOURCE_FILES is
53 much faster than defining LSEEK_NOT_LINEAR. */
54
55 #ifndef O_BINARY
56 #define O_BINARY 0
57 #endif
58
59 #define OPEN_MODE (O_RDONLY | O_BINARY)
60 #define FDOPEN_MODE FOPEN_RB
61
62 #else /* ! defined (CRLF_SOURCE_FILES) */
63
64 #define OPEN_MODE O_RDONLY
65 #define FDOPEN_MODE FOPEN_RT
66
67 #endif /* ! defined (CRLF_SOURCE_FILES) */
68
69 /* Prototypes for exported functions. */
70
71 void _initialize_source (void);
72
73 /* Prototypes for local functions. */
74
75 static int get_filename_and_charpos (struct symtab *, char **);
76
77 static void reverse_search_command (char *, int);
78
79 static void forward_search_command (char *, int);
80
81 static void line_info (char *, int);
82
83 static void ambiguous_line_spec (struct symtabs_and_lines *);
84
85 static void source_info (char *, int);
86
87 static void show_directories (char *, int);
88
89 /* Path of directories to search for source files.
90 Same format as the PATH environment variable's value. */
91
92 char *source_path;
93
94 /* Symtab of default file for listing lines of. */
95
96 static struct symtab *current_source_symtab;
97
98 /* Default next line to list. */
99
100 static int current_source_line;
101
102 /* Default number of lines to print with commands like "list".
103 This is based on guessing how many long (i.e. more than chars_per_line
104 characters) lines there will be. To be completely correct, "list"
105 and friends should be rewritten to count characters and see where
106 things are wrapping, but that would be a fair amount of work. */
107
108 int lines_to_list = 10;
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;
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;
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, NULL))
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 /* All right; find the last file in the symtab list (ignoring .h's). */
254
255 current_source_line = 1;
256
257 for (ofp = object_files; ofp != NULL; ofp = ofp->next)
258 {
259 for (s = ofp->symtabs; s; s = s->next)
260 {
261 char *name = s->filename;
262 int len = strlen (name);
263 if (!(len > 2 && (DEPRECATED_STREQ (&name[len - 2], ".h"))))
264 {
265 current_source_symtab = s;
266 }
267 }
268 }
269 if (current_source_symtab)
270 return;
271
272 /* Howabout 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 char *name = ps->filename;
279 int len = strlen (name);
280 if (!(len > 2 && (DEPRECATED_STREQ (&name[len - 2], ".h"))))
281 {
282 cs_pst = ps;
283 }
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 xmfree (objfile->md, s->line_charpos);
331 s->line_charpos = NULL;
332 }
333 if (s->fullname != NULL)
334 {
335 xmfree (objfile->md, 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
351 void
352 init_source_path (void)
353 {
354 char buf[20];
355
356 sprintf (buf, "$cdir%c$cwd", DIRNAME_SEPARATOR);
357 source_path = xstrdup (buf);
358 forget_cached_source_info ();
359 }
360
361 void
362 init_last_source_visited (void)
363 {
364 last_source_visited = NULL;
365 }
366
367 /* Add zero or more directories to the front of the source path. */
368
369 void
370 directory_command (char *dirname, int from_tty)
371 {
372 dont_repeat ();
373 /* FIXME, this goes to "delete dir"... */
374 if (dirname == 0)
375 {
376 if (from_tty && query ("Reinitialize source path to empty? "))
377 {
378 xfree (source_path);
379 init_source_path ();
380 }
381 }
382 else
383 {
384 mod_path (dirname, &source_path);
385 last_source_visited = NULL;
386 }
387 if (from_tty)
388 show_directories ((char *) 0, from_tty);
389 forget_cached_source_info ();
390 }
391
392 /* Add zero or more directories to the front of an arbitrary path. */
393
394 void
395 mod_path (char *dirname, char **which_path)
396 {
397 add_path (dirname, which_path, 1);
398 }
399
400 /* Workhorse of mod_path. Takes an extra argument to determine
401 if dirname should be parsed for separators that indicate multiple
402 directories. This allows for interfaces that pre-parse the dirname
403 and allow specification of traditional separator characters such
404 as space or tab. */
405
406 void
407 add_path (char *dirname, char **which_path, int parse_separators)
408 {
409 char *old = *which_path;
410 int prefix = 0;
411
412 if (dirname == 0)
413 return;
414
415 dirname = xstrdup (dirname);
416 make_cleanup (xfree, dirname);
417
418 do
419 {
420 char *name = dirname;
421 char *p;
422 struct stat st;
423
424 {
425 char *separator = NULL;
426 char *space = NULL;
427 char *tab = NULL;
428
429 if (parse_separators)
430 {
431 separator = strchr (name, DIRNAME_SEPARATOR);
432 space = strchr (name, ' ');
433 tab = strchr (name, '\t');
434 }
435
436 if (separator == 0 && space == 0 && tab == 0)
437 p = dirname = name + strlen (name);
438 else
439 {
440 p = 0;
441 if (separator != 0 && (p == 0 || separator < p))
442 p = separator;
443 if (space != 0 && (p == 0 || space < p))
444 p = space;
445 if (tab != 0 && (p == 0 || tab < p))
446 p = tab;
447 dirname = p + 1;
448 while (*dirname == DIRNAME_SEPARATOR
449 || *dirname == ' '
450 || *dirname == '\t')
451 ++dirname;
452 }
453 }
454
455 if (!(IS_DIR_SEPARATOR (*name) && p <= name + 1) /* "/" */
456 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
457 /* On MS-DOS and MS-Windows, h:\ is different from h: */
458 && !(p == name + 3 && name[1] == ':') /* "d:/" */
459 #endif
460 && IS_DIR_SEPARATOR (p[-1]))
461 /* Sigh. "foo/" => "foo" */
462 --p;
463 *p = '\0';
464
465 while (p > name && p[-1] == '.')
466 {
467 if (p - name == 1)
468 {
469 /* "." => getwd (). */
470 name = current_directory;
471 goto append;
472 }
473 else if (p > name + 1 && IS_DIR_SEPARATOR (p[-2]))
474 {
475 if (p - name == 2)
476 {
477 /* "/." => "/". */
478 *--p = '\0';
479 goto append;
480 }
481 else
482 {
483 /* "...foo/." => "...foo". */
484 p -= 2;
485 *p = '\0';
486 continue;
487 }
488 }
489 else
490 break;
491 }
492
493 if (name[0] == '~')
494 name = tilde_expand (name);
495 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
496 else if (IS_ABSOLUTE_PATH (name) && p == name + 2) /* "d:" => "d:." */
497 name = concat (name, ".", NULL);
498 #endif
499 else if (!IS_ABSOLUTE_PATH (name) && name[0] != '$')
500 name = concat (current_directory, SLASH_STRING, name, NULL);
501 else
502 name = savestring (name, p - name);
503 make_cleanup (xfree, name);
504
505 /* Unless it's a variable, check existence. */
506 if (name[0] != '$')
507 {
508 /* These are warnings, not errors, since we don't want a
509 non-existent directory in a .gdbinit file to stop processing
510 of the .gdbinit file.
511
512 Whether they get added to the path is more debatable. Current
513 answer is yes, in case the user wants to go make the directory
514 or whatever. If the directory continues to not exist/not be
515 a directory/etc, then having them in the path should be
516 harmless. */
517 if (stat (name, &st) < 0)
518 {
519 int save_errno = errno;
520 fprintf_unfiltered (gdb_stderr, "Warning: ");
521 print_sys_errmsg (name, save_errno);
522 }
523 else if ((st.st_mode & S_IFMT) != S_IFDIR)
524 warning ("%s is not a directory.", name);
525 }
526
527 append:
528 {
529 unsigned int len = strlen (name);
530
531 p = *which_path;
532 while (1)
533 {
534 /* FIXME: strncmp loses in interesting ways on MS-DOS and
535 MS-Windows because of case-insensitivity and two different
536 but functionally identical slash characters. We need a
537 special filesystem-dependent file-name comparison function.
538
539 Actually, even on Unix I would use realpath() or its work-
540 alike before comparing. Then all the code above which
541 removes excess slashes and dots could simply go away. */
542 if (!strncmp (p, name, len)
543 && (p[len] == '\0' || p[len] == DIRNAME_SEPARATOR))
544 {
545 /* Found it in the search path, remove old copy */
546 if (p > *which_path)
547 p--; /* Back over leading separator */
548 if (prefix > p - *which_path)
549 goto skip_dup; /* Same dir twice in one cmd */
550 strcpy (p, &p[len + 1]); /* Copy from next \0 or : */
551 }
552 p = strchr (p, DIRNAME_SEPARATOR);
553 if (p != 0)
554 ++p;
555 else
556 break;
557 }
558 if (p == 0)
559 {
560 char tinybuf[2];
561
562 tinybuf[0] = DIRNAME_SEPARATOR;
563 tinybuf[1] = '\0';
564
565 /* If we have already tacked on a name(s) in this command, be sure they stay
566 on the front as we tack on some more. */
567 if (prefix)
568 {
569 char *temp, c;
570
571 c = old[prefix];
572 old[prefix] = '\0';
573 temp = concat (old, tinybuf, name, NULL);
574 old[prefix] = c;
575 *which_path = concat (temp, "", &old[prefix], NULL);
576 prefix = strlen (temp);
577 xfree (temp);
578 }
579 else
580 {
581 *which_path = concat (name, (old[0] ? tinybuf : old), old, NULL);
582 prefix = strlen (name);
583 }
584 xfree (old);
585 old = *which_path;
586 }
587 }
588 skip_dup:;
589 }
590 while (*dirname != '\0');
591 }
592
593
594 static void
595 source_info (char *ignore, int from_tty)
596 {
597 struct symtab *s = current_source_symtab;
598
599 if (!s)
600 {
601 printf_filtered ("No current source file.\n");
602 return;
603 }
604 printf_filtered ("Current source file is %s\n", s->filename);
605 if (s->dirname)
606 printf_filtered ("Compilation directory is %s\n", s->dirname);
607 if (s->fullname)
608 printf_filtered ("Located in %s\n", s->fullname);
609 if (s->nlines)
610 printf_filtered ("Contains %d line%s.\n", s->nlines,
611 s->nlines == 1 ? "" : "s");
612
613 printf_filtered ("Source language is %s.\n", language_str (s->language));
614 printf_filtered ("Compiled with %s debugging format.\n", s->debugformat);
615 printf_filtered ("%s preprocessor macro info.\n",
616 s->macro_table ? "Includes" : "Does not include");
617 }
618 \f
619
620 /* Return True if the file NAME exists and is a regular file */
621 static int
622 is_regular_file (const char *name)
623 {
624 struct stat st;
625 const int status = stat (name, &st);
626
627 /* Stat should never fail except when the file does not exist.
628 If stat fails, analyze the source of error and return True
629 unless the file does not exist, to avoid returning false results
630 on obscure systems where stat does not work as expected.
631 */
632 if (status != 0)
633 return (errno != ENOENT);
634
635 return S_ISREG (st.st_mode);
636 }
637
638 /* Open a file named STRING, searching path PATH (dir names sep by some char)
639 using mode MODE and protection bits PROT in the calls to open.
640
641 If TRY_CWD_FIRST, try to open ./STRING before searching PATH.
642 (ie pretend the first element of PATH is "."). This also indicates
643 that a slash in STRING disables searching of the path (this is
644 so that "exec-file ./foo" or "symbol-file ./foo" insures that you
645 get that particular version of foo or an error message).
646
647 If FILENAME_OPENED is non-null, set it to a newly allocated string naming
648 the actual file opened (this string will always start with a "/"). We
649 have to take special pains to avoid doubling the "/" between the directory
650 and the file, sigh! Emacs gets confuzzed by this when we print the
651 source file name!!!
652
653 If a file is found, return the descriptor.
654 Otherwise, return -1, with errno set for the last name we tried to open. */
655
656 /* >>>> This should only allow files of certain types,
657 >>>> eg executable, non-directory */
658 int
659 openp (const char *path, int try_cwd_first, const char *string,
660 int mode, int prot,
661 char **filename_opened)
662 {
663 int fd;
664 char *filename;
665 const char *p;
666 const char *p1;
667 int len;
668 int alloclen;
669
670 if (!path)
671 path = ".";
672
673 #if defined(_WIN32) || defined(__CYGWIN__)
674 mode |= O_BINARY;
675 #endif
676
677 if (try_cwd_first || IS_ABSOLUTE_PATH (string))
678 {
679 int i;
680
681 if (is_regular_file (string))
682 {
683 filename = alloca (strlen (string) + 1);
684 strcpy (filename, string);
685 fd = open (filename, mode, prot);
686 if (fd >= 0)
687 goto done;
688 }
689 else
690 {
691 filename = NULL;
692 fd = -1;
693 }
694
695 for (i = 0; string[i]; i++)
696 if (IS_DIR_SEPARATOR (string[i]))
697 goto done;
698 }
699
700 /* ./foo => foo */
701 while (string[0] == '.' && IS_DIR_SEPARATOR (string[1]))
702 string += 2;
703
704 alloclen = strlen (path) + strlen (string) + 2;
705 filename = alloca (alloclen);
706 fd = -1;
707 for (p = path; p; p = p1 ? p1 + 1 : 0)
708 {
709 p1 = strchr (p, DIRNAME_SEPARATOR);
710 if (p1)
711 len = p1 - p;
712 else
713 len = strlen (p);
714
715 if (len == 4 && p[0] == '$' && p[1] == 'c'
716 && p[2] == 'w' && p[3] == 'd')
717 {
718 /* Name is $cwd -- insert current directory name instead. */
719 int newlen;
720
721 /* First, realloc the filename buffer if too short. */
722 len = strlen (current_directory);
723 newlen = len + strlen (string) + 2;
724 if (newlen > alloclen)
725 {
726 alloclen = newlen;
727 filename = alloca (alloclen);
728 }
729 strcpy (filename, current_directory);
730 }
731 else
732 {
733 /* Normal file name in path -- just use it. */
734 strncpy (filename, p, len);
735 filename[len] = 0;
736 }
737
738 /* Remove trailing slashes */
739 while (len > 0 && IS_DIR_SEPARATOR (filename[len - 1]))
740 filename[--len] = 0;
741
742 strcat (filename + len, SLASH_STRING);
743 strcat (filename, string);
744
745 if (is_regular_file (filename))
746 {
747 fd = open (filename, mode);
748 if (fd >= 0)
749 break;
750 }
751 }
752
753 done:
754 if (filename_opened)
755 {
756 /* If a file was opened, canonicalize its filename. Use xfullpath
757 rather than gdb_realpath to avoid resolving the basename part
758 of filenames when the associated file is a symbolic link. This
759 fixes a potential inconsistency between the filenames known to
760 GDB and the filenames it prints in the annotations. */
761 if (fd < 0)
762 *filename_opened = NULL;
763 else if (IS_ABSOLUTE_PATH (filename))
764 *filename_opened = xfullpath (filename);
765 else
766 {
767 /* Beware the // my son, the Emacs barfs, the botch that catch... */
768
769 char *f = concat (current_directory,
770 IS_DIR_SEPARATOR (current_directory[strlen (current_directory) - 1])
771 ? "" : SLASH_STRING,
772 filename, NULL);
773 *filename_opened = xfullpath (f);
774 xfree (f);
775 }
776 }
777
778 return fd;
779 }
780
781
782 /* This is essentially a convenience, for clients that want the behaviour
783 of openp, using source_path, but that really don't want the file to be
784 opened but want instead just to know what the full pathname is (as
785 qualified against source_path).
786
787 The current working directory is searched first.
788
789 If the file was found, this function returns 1, and FULL_PATHNAME is
790 set to the fully-qualified pathname.
791
792 Else, this functions returns 0, and FULL_PATHNAME is set to NULL.
793 */
794 int
795 source_full_path_of (char *filename, char **full_pathname)
796 {
797 int fd;
798
799 fd = openp (source_path, 1, filename, O_RDONLY, 0, full_pathname);
800 if (fd < 0)
801 {
802 *full_pathname = NULL;
803 return 0;
804 }
805
806 close (fd);
807 return 1;
808 }
809
810
811 /* Open a source file given a symtab S. Returns a file descriptor or
812 negative number for error. */
813
814 int
815 open_source_file (struct symtab *s)
816 {
817 char *path = source_path;
818 const char *p;
819 int result;
820 char *fullname;
821
822 /* Quick way out if we already know its full name */
823 if (s->fullname)
824 {
825 result = open (s->fullname, OPEN_MODE);
826 if (result >= 0)
827 return result;
828 /* Didn't work -- free old one, try again. */
829 xmfree (s->objfile->md, s->fullname);
830 s->fullname = NULL;
831 }
832
833 if (s->dirname != NULL)
834 {
835 /* Replace a path entry of $cdir with the compilation directory name */
836 #define cdir_len 5
837 /* We cast strstr's result in case an ANSIhole has made it const,
838 which produces a "required warning" when assigned to a nonconst. */
839 p = (char *) strstr (source_path, "$cdir");
840 if (p && (p == path || p[-1] == DIRNAME_SEPARATOR)
841 && (p[cdir_len] == DIRNAME_SEPARATOR || p[cdir_len] == '\0'))
842 {
843 int len;
844
845 path = (char *)
846 alloca (strlen (source_path) + 1 + strlen (s->dirname) + 1);
847 len = p - source_path;
848 strncpy (path, source_path, len); /* Before $cdir */
849 strcpy (path + len, s->dirname); /* new stuff */
850 strcat (path + len, source_path + len + cdir_len); /* After $cdir */
851 }
852 }
853
854 result = openp (path, 0, s->filename, OPEN_MODE, 0, &s->fullname);
855 if (result < 0)
856 {
857 /* Didn't work. Try using just the basename. */
858 p = lbasename (s->filename);
859 if (p != s->filename)
860 result = openp (path, 0, p, OPEN_MODE, 0, &s->fullname);
861 }
862
863 if (result >= 0)
864 {
865 fullname = s->fullname;
866 s->fullname = mstrsave (s->objfile->md, s->fullname);
867 xfree (fullname);
868 }
869 return result;
870 }
871
872 /* Return the path to the source file associated with symtab. Returns NULL
873 if no symtab. */
874
875 char *
876 symtab_to_filename (struct symtab *s)
877 {
878 int fd;
879
880 if (!s)
881 return NULL;
882
883 /* If we've seen the file before, just return fullname. */
884
885 if (s->fullname)
886 return s->fullname;
887
888 /* Try opening the file to setup fullname */
889
890 fd = open_source_file (s);
891 if (fd < 0)
892 return s->filename; /* File not found. Just use short name */
893
894 /* Found the file. Cleanup and return the full name */
895
896 close (fd);
897 return s->fullname;
898 }
899 \f
900
901 /* Create and initialize the table S->line_charpos that records
902 the positions of the lines in the source file, which is assumed
903 to be open on descriptor DESC.
904 All set S->nlines to the number of such lines. */
905
906 void
907 find_source_lines (struct symtab *s, int desc)
908 {
909 struct stat st;
910 char *data, *p, *end;
911 int nlines = 0;
912 int lines_allocated = 1000;
913 int *line_charpos;
914 long mtime = 0;
915 int size;
916
917 line_charpos = (int *) xmmalloc (s->objfile->md,
918 lines_allocated * sizeof (int));
919 if (fstat (desc, &st) < 0)
920 perror_with_name (s->filename);
921
922 if (s && s->objfile && s->objfile->obfd)
923 mtime = bfd_get_mtime (s->objfile->obfd);
924 else if (exec_bfd)
925 mtime = bfd_get_mtime (exec_bfd);
926
927 if (mtime && mtime < st.st_mtime)
928 {
929 warning ("Source file is more recent than executable.\n");
930 }
931
932 #ifdef LSEEK_NOT_LINEAR
933 {
934 char c;
935
936 /* Have to read it byte by byte to find out where the chars live */
937
938 line_charpos[0] = lseek (desc, 0, SEEK_CUR);
939 nlines = 1;
940 while (myread (desc, &c, 1) > 0)
941 {
942 if (c == '\n')
943 {
944 if (nlines == lines_allocated)
945 {
946 lines_allocated *= 2;
947 line_charpos =
948 (int *) xmrealloc (s->objfile->md, (char *) line_charpos,
949 sizeof (int) * lines_allocated);
950 }
951 line_charpos[nlines++] = lseek (desc, 0, SEEK_CUR);
952 }
953 }
954 }
955 #else /* lseek linear. */
956 {
957 struct cleanup *old_cleanups;
958
959 /* st_size might be a large type, but we only support source files whose
960 size fits in an int. */
961 size = (int) st.st_size;
962
963 /* Use malloc, not alloca, because this may be pretty large, and we may
964 run into various kinds of limits on stack size. */
965 data = (char *) xmalloc (size);
966 old_cleanups = make_cleanup (xfree, data);
967
968 /* Reassign `size' to result of read for systems where \r\n -> \n. */
969 size = myread (desc, data, size);
970 if (size < 0)
971 perror_with_name (s->filename);
972 end = data + size;
973 p = data;
974 line_charpos[0] = 0;
975 nlines = 1;
976 while (p != end)
977 {
978 if (*p++ == '\n'
979 /* A newline at the end does not start a new line. */
980 && p != end)
981 {
982 if (nlines == lines_allocated)
983 {
984 lines_allocated *= 2;
985 line_charpos =
986 (int *) xmrealloc (s->objfile->md, (char *) line_charpos,
987 sizeof (int) * lines_allocated);
988 }
989 line_charpos[nlines++] = p - data;
990 }
991 }
992 do_cleanups (old_cleanups);
993 }
994 #endif /* lseek linear. */
995 s->nlines = nlines;
996 s->line_charpos =
997 (int *) xmrealloc (s->objfile->md, (char *) line_charpos,
998 nlines * sizeof (int));
999
1000 }
1001
1002 /* Return the character position of a line LINE in symtab S.
1003 Return 0 if anything is invalid. */
1004
1005 #if 0 /* Currently unused */
1006
1007 int
1008 source_line_charpos (struct symtab *s, int line)
1009 {
1010 if (!s)
1011 return 0;
1012 if (!s->line_charpos || line <= 0)
1013 return 0;
1014 if (line > s->nlines)
1015 line = s->nlines;
1016 return s->line_charpos[line - 1];
1017 }
1018
1019 /* Return the line number of character position POS in symtab S. */
1020
1021 int
1022 source_charpos_line (struct symtab *s, int chr)
1023 {
1024 int line = 0;
1025 int *lnp;
1026
1027 if (s == 0 || s->line_charpos == 0)
1028 return 0;
1029 lnp = s->line_charpos;
1030 /* Files are usually short, so sequential search is Ok */
1031 while (line < s->nlines && *lnp <= chr)
1032 {
1033 line++;
1034 lnp++;
1035 }
1036 if (line >= s->nlines)
1037 line = s->nlines;
1038 return line;
1039 }
1040
1041 #endif /* 0 */
1042 \f
1043
1044 /* Get full pathname and line number positions for a symtab.
1045 Return nonzero if line numbers may have changed.
1046 Set *FULLNAME to actual name of the file as found by `openp',
1047 or to 0 if the file is not found. */
1048
1049 static int
1050 get_filename_and_charpos (struct symtab *s, char **fullname)
1051 {
1052 int desc, linenums_changed = 0;
1053
1054 desc = open_source_file (s);
1055 if (desc < 0)
1056 {
1057 if (fullname)
1058 *fullname = NULL;
1059 return 0;
1060 }
1061 if (fullname)
1062 *fullname = s->fullname;
1063 if (s->line_charpos == 0)
1064 linenums_changed = 1;
1065 if (linenums_changed)
1066 find_source_lines (s, desc);
1067 close (desc);
1068 return linenums_changed;
1069 }
1070
1071 /* Print text describing the full name of the source file S
1072 and the line number LINE and its corresponding character position.
1073 The text starts with two Ctrl-z so that the Emacs-GDB interface
1074 can easily find it.
1075
1076 MID_STATEMENT is nonzero if the PC is not at the beginning of that line.
1077
1078 Return 1 if successful, 0 if could not find the file. */
1079
1080 int
1081 identify_source_line (struct symtab *s, int line, int mid_statement,
1082 CORE_ADDR pc)
1083 {
1084 if (s->line_charpos == 0)
1085 get_filename_and_charpos (s, (char **) NULL);
1086 if (s->fullname == 0)
1087 return 0;
1088 if (line > s->nlines)
1089 /* Don't index off the end of the line_charpos array. */
1090 return 0;
1091 annotate_source (s->fullname, line, s->line_charpos[line - 1],
1092 mid_statement, pc);
1093
1094 current_source_line = line;
1095 first_line_listed = line;
1096 last_line_listed = line;
1097 current_source_symtab = s;
1098 return 1;
1099 }
1100 \f
1101
1102 /* Print source lines from the file of symtab S,
1103 starting with line number LINE and stopping before line number STOPLINE. */
1104
1105 static void print_source_lines_base (struct symtab *s, int line, int stopline,
1106 int noerror);
1107 static void
1108 print_source_lines_base (struct symtab *s, int line, int stopline, int noerror)
1109 {
1110 int c;
1111 int desc;
1112 FILE *stream;
1113 int nlines = stopline - line;
1114
1115 /* Regardless of whether we can open the file, set current_source_symtab. */
1116 current_source_symtab = s;
1117 current_source_line = line;
1118 first_line_listed = line;
1119
1120 /* If printing of source lines is disabled, just print file and line number */
1121 if (ui_out_test_flags (uiout, ui_source_list))
1122 {
1123 /* Only prints "No such file or directory" once */
1124 if ((s != last_source_visited) || (!last_source_error))
1125 {
1126 last_source_visited = s;
1127 desc = open_source_file (s);
1128 }
1129 else
1130 {
1131 desc = last_source_error;
1132 noerror = 1;
1133 }
1134 }
1135 else
1136 {
1137 desc = -1;
1138 noerror = 1;
1139 }
1140
1141 if (desc < 0)
1142 {
1143 last_source_error = desc;
1144
1145 if (!noerror)
1146 {
1147 char *name = alloca (strlen (s->filename) + 100);
1148 sprintf (name, "%d\t%s", line, s->filename);
1149 print_sys_errmsg (name, errno);
1150 }
1151 else
1152 ui_out_field_int (uiout, "line", line);
1153 ui_out_text (uiout, "\tin ");
1154 ui_out_field_string (uiout, "file", s->filename);
1155 ui_out_text (uiout, "\n");
1156
1157 return;
1158 }
1159
1160 last_source_error = 0;
1161
1162 if (s->line_charpos == 0)
1163 find_source_lines (s, desc);
1164
1165 if (line < 1 || line > s->nlines)
1166 {
1167 close (desc);
1168 error ("Line number %d out of range; %s has %d lines.",
1169 line, s->filename, s->nlines);
1170 }
1171
1172 if (lseek (desc, s->line_charpos[line - 1], 0) < 0)
1173 {
1174 close (desc);
1175 perror_with_name (s->filename);
1176 }
1177
1178 stream = fdopen (desc, FDOPEN_MODE);
1179 clearerr (stream);
1180
1181 while (nlines-- > 0)
1182 {
1183 char buf[20];
1184
1185 c = fgetc (stream);
1186 if (c == EOF)
1187 break;
1188 last_line_listed = current_source_line;
1189 sprintf (buf, "%d\t", current_source_line++);
1190 ui_out_text (uiout, buf);
1191 do
1192 {
1193 if (c < 040 && c != '\t' && c != '\n' && c != '\r')
1194 {
1195 sprintf (buf, "^%c", c + 0100);
1196 ui_out_text (uiout, buf);
1197 }
1198 else if (c == 0177)
1199 ui_out_text (uiout, "^?");
1200 #ifdef CRLF_SOURCE_FILES
1201 else if (c == '\r')
1202 {
1203 /* Skip a \r character, but only before a \n. */
1204 int c1 = fgetc (stream);
1205
1206 if (c1 != '\n')
1207 printf_filtered ("^%c", c + 0100);
1208 if (c1 != EOF)
1209 ungetc (c1, stream);
1210 }
1211 #endif
1212 else
1213 {
1214 sprintf (buf, "%c", c);
1215 ui_out_text (uiout, buf);
1216 }
1217 }
1218 while (c != '\n' && (c = fgetc (stream)) >= 0);
1219 }
1220
1221 fclose (stream);
1222 }
1223 \f
1224 /* Show source lines from the file of symtab S, starting with line
1225 number LINE and stopping before line number STOPLINE. If this is the
1226 not the command line version, then the source is shown in the source
1227 window otherwise it is simply printed */
1228
1229 void
1230 print_source_lines (struct symtab *s, int line, int stopline, int noerror)
1231 {
1232 print_source_lines_base (s, line, stopline, noerror);
1233 }
1234 \f
1235 /* Print info on range of pc's in a specified line. */
1236
1237 static void
1238 line_info (char *arg, int from_tty)
1239 {
1240 struct symtabs_and_lines sals;
1241 struct symtab_and_line sal;
1242 CORE_ADDR start_pc, end_pc;
1243 int i;
1244
1245 init_sal (&sal); /* initialize to zeroes */
1246
1247 if (arg == 0)
1248 {
1249 sal.symtab = current_source_symtab;
1250 sal.line = last_line_listed;
1251 sals.nelts = 1;
1252 sals.sals = (struct symtab_and_line *)
1253 xmalloc (sizeof (struct symtab_and_line));
1254 sals.sals[0] = sal;
1255 }
1256 else
1257 {
1258 sals = decode_line_spec_1 (arg, 0);
1259
1260 dont_repeat ();
1261 }
1262
1263 /* C++ More than one line may have been specified, as when the user
1264 specifies an overloaded function name. Print info on them all. */
1265 for (i = 0; i < sals.nelts; i++)
1266 {
1267 sal = sals.sals[i];
1268
1269 if (sal.symtab == 0)
1270 {
1271 printf_filtered ("No line number information available");
1272 if (sal.pc != 0)
1273 {
1274 /* This is useful for "info line *0x7f34". If we can't tell the
1275 user about a source line, at least let them have the symbolic
1276 address. */
1277 printf_filtered (" for address ");
1278 wrap_here (" ");
1279 print_address (sal.pc, gdb_stdout);
1280 }
1281 else
1282 printf_filtered (".");
1283 printf_filtered ("\n");
1284 }
1285 else if (sal.line > 0
1286 && find_line_pc_range (sal, &start_pc, &end_pc))
1287 {
1288 if (start_pc == end_pc)
1289 {
1290 printf_filtered ("Line %d of \"%s\"",
1291 sal.line, sal.symtab->filename);
1292 wrap_here (" ");
1293 printf_filtered (" is at address ");
1294 print_address (start_pc, gdb_stdout);
1295 wrap_here (" ");
1296 printf_filtered (" but contains no code.\n");
1297 }
1298 else
1299 {
1300 printf_filtered ("Line %d of \"%s\"",
1301 sal.line, sal.symtab->filename);
1302 wrap_here (" ");
1303 printf_filtered (" starts at address ");
1304 print_address (start_pc, gdb_stdout);
1305 wrap_here (" ");
1306 printf_filtered (" and ends at ");
1307 print_address (end_pc, gdb_stdout);
1308 printf_filtered (".\n");
1309 }
1310
1311 /* x/i should display this line's code. */
1312 set_next_address (start_pc);
1313
1314 /* Repeating "info line" should do the following line. */
1315 last_line_listed = sal.line + 1;
1316
1317 /* If this is the only line, show the source code. If it could
1318 not find the file, don't do anything special. */
1319 if (annotation_level && sals.nelts == 1)
1320 identify_source_line (sal.symtab, sal.line, 0, start_pc);
1321 }
1322 else
1323 /* Is there any case in which we get here, and have an address
1324 which the user would want to see? If we have debugging symbols
1325 and no line numbers? */
1326 printf_filtered ("Line number %d is out of range for \"%s\".\n",
1327 sal.line, sal.symtab->filename);
1328 }
1329 xfree (sals.sals);
1330 }
1331 \f
1332 /* Commands to search the source file for a regexp. */
1333
1334 static void
1335 forward_search_command (char *regex, int from_tty)
1336 {
1337 int c;
1338 int desc;
1339 FILE *stream;
1340 int line;
1341 char *msg;
1342
1343 line = last_line_listed + 1;
1344
1345 msg = (char *) re_comp (regex);
1346 if (msg)
1347 error ("%s", msg);
1348
1349 if (current_source_symtab == 0)
1350 select_source_symtab (0);
1351
1352 desc = open_source_file (current_source_symtab);
1353 if (desc < 0)
1354 perror_with_name (current_source_symtab->filename);
1355
1356 if (current_source_symtab->line_charpos == 0)
1357 find_source_lines (current_source_symtab, desc);
1358
1359 if (line < 1 || line > current_source_symtab->nlines)
1360 {
1361 close (desc);
1362 error ("Expression not found");
1363 }
1364
1365 if (lseek (desc, current_source_symtab->line_charpos[line - 1], 0) < 0)
1366 {
1367 close (desc);
1368 perror_with_name (current_source_symtab->filename);
1369 }
1370
1371 stream = fdopen (desc, FDOPEN_MODE);
1372 clearerr (stream);
1373 while (1)
1374 {
1375 static char *buf = NULL;
1376 char *p;
1377 int cursize, newsize;
1378
1379 cursize = 256;
1380 buf = xmalloc (cursize);
1381 p = buf;
1382
1383 c = getc (stream);
1384 if (c == EOF)
1385 break;
1386 do
1387 {
1388 *p++ = c;
1389 if (p - buf == cursize)
1390 {
1391 newsize = cursize + cursize / 2;
1392 buf = xrealloc (buf, newsize);
1393 p = buf + cursize;
1394 cursize = newsize;
1395 }
1396 }
1397 while (c != '\n' && (c = getc (stream)) >= 0);
1398
1399 #ifdef CRLF_SOURCE_FILES
1400 /* Remove the \r, if any, at the end of the line, otherwise
1401 regular expressions that end with $ or \n won't work. */
1402 if (p - buf > 1 && p[-2] == '\r')
1403 {
1404 p--;
1405 p[-1] = '\n';
1406 }
1407 #endif
1408
1409 /* we now have a source line in buf, null terminate and match */
1410 *p = 0;
1411 if (re_exec (buf) > 0)
1412 {
1413 /* Match! */
1414 fclose (stream);
1415 print_source_lines (current_source_symtab, line, line + 1, 0);
1416 set_internalvar (lookup_internalvar ("_"),
1417 value_from_longest (builtin_type_int,
1418 (LONGEST) line));
1419 current_source_line = max (line - lines_to_list / 2, 1);
1420 return;
1421 }
1422 line++;
1423 }
1424
1425 printf_filtered ("Expression not found\n");
1426 fclose (stream);
1427 }
1428
1429 static void
1430 reverse_search_command (char *regex, int from_tty)
1431 {
1432 int c;
1433 int desc;
1434 FILE *stream;
1435 int line;
1436 char *msg;
1437
1438 line = last_line_listed - 1;
1439
1440 msg = (char *) re_comp (regex);
1441 if (msg)
1442 error ("%s", msg);
1443
1444 if (current_source_symtab == 0)
1445 select_source_symtab (0);
1446
1447 desc = open_source_file (current_source_symtab);
1448 if (desc < 0)
1449 perror_with_name (current_source_symtab->filename);
1450
1451 if (current_source_symtab->line_charpos == 0)
1452 find_source_lines (current_source_symtab, desc);
1453
1454 if (line < 1 || line > current_source_symtab->nlines)
1455 {
1456 close (desc);
1457 error ("Expression not found");
1458 }
1459
1460 if (lseek (desc, current_source_symtab->line_charpos[line - 1], 0) < 0)
1461 {
1462 close (desc);
1463 perror_with_name (current_source_symtab->filename);
1464 }
1465
1466 stream = fdopen (desc, FDOPEN_MODE);
1467 clearerr (stream);
1468 while (line > 1)
1469 {
1470 /* FIXME!!! We walk right off the end of buf if we get a long line!!! */
1471 char buf[4096]; /* Should be reasonable??? */
1472 char *p = buf;
1473
1474 c = getc (stream);
1475 if (c == EOF)
1476 break;
1477 do
1478 {
1479 *p++ = c;
1480 }
1481 while (c != '\n' && (c = getc (stream)) >= 0);
1482
1483 #ifdef CRLF_SOURCE_FILES
1484 /* Remove the \r, if any, at the end of the line, otherwise
1485 regular expressions that end with $ or \n won't work. */
1486 if (p - buf > 1 && p[-2] == '\r')
1487 {
1488 p--;
1489 p[-1] = '\n';
1490 }
1491 #endif
1492
1493 /* We now have a source line in buf; null terminate and match. */
1494 *p = 0;
1495 if (re_exec (buf) > 0)
1496 {
1497 /* Match! */
1498 fclose (stream);
1499 print_source_lines (current_source_symtab, line, line + 1, 0);
1500 set_internalvar (lookup_internalvar ("_"),
1501 value_from_longest (builtin_type_int,
1502 (LONGEST) line));
1503 current_source_line = max (line - lines_to_list / 2, 1);
1504 return;
1505 }
1506 line--;
1507 if (fseek (stream, current_source_symtab->line_charpos[line - 1], 0) < 0)
1508 {
1509 fclose (stream);
1510 perror_with_name (current_source_symtab->filename);
1511 }
1512 }
1513
1514 printf_filtered ("Expression not found\n");
1515 fclose (stream);
1516 return;
1517 }
1518 \f
1519 void
1520 _initialize_source (void)
1521 {
1522 struct cmd_list_element *c;
1523 current_source_symtab = 0;
1524 init_source_path ();
1525
1526 /* The intention is to use POSIX Basic Regular Expressions.
1527 Always use the GNU regex routine for consistency across all hosts.
1528 Our current GNU regex.c does not have all the POSIX features, so this is
1529 just an approximation. */
1530 re_set_syntax (RE_SYNTAX_GREP);
1531
1532 c = add_cmd ("directory", class_files, directory_command,
1533 "Add directory DIR to beginning of search path for source files.\n\
1534 Forget cached info on source file locations and line positions.\n\
1535 DIR can also be $cwd for the current working directory, or $cdir for the\n\
1536 directory in which the source file was compiled into object code.\n\
1537 With no argument, reset the search path to $cdir:$cwd, the default.",
1538 &cmdlist);
1539
1540 if (dbx_commands)
1541 add_com_alias ("use", "directory", class_files, 0);
1542
1543 set_cmd_completer (c, filename_completer);
1544
1545 add_cmd ("directories", no_class, show_directories,
1546 "Current search path for finding source files.\n\
1547 $cwd in the path means the current working directory.\n\
1548 $cdir in the path means the compilation directory of the source file.",
1549 &showlist);
1550
1551 if (xdb_commands)
1552 {
1553 add_com_alias ("D", "directory", class_files, 0);
1554 add_cmd ("ld", no_class, show_directories,
1555 "Current search path for finding source files.\n\
1556 $cwd in the path means the current working directory.\n\
1557 $cdir in the path means the compilation directory of the source file.",
1558 &cmdlist);
1559 }
1560
1561 add_info ("source", source_info,
1562 "Information about the current source file.");
1563
1564 add_info ("line", line_info,
1565 concat ("Core addresses of the code for a source line.\n\
1566 Line can be specified as\n\
1567 LINENUM, to list around that line in current file,\n\
1568 FILE:LINENUM, to list around that line in that file,\n\
1569 FUNCTION, to list around beginning of that function,\n\
1570 FILE:FUNCTION, to distinguish among like-named static functions.\n\
1571 ", "\
1572 Default is to describe the last source line that was listed.\n\n\
1573 This sets the default address for \"x\" to the line's first instruction\n\
1574 so that \"x/i\" suffices to start examining the machine code.\n\
1575 The address is also stored as the value of \"$_\".", NULL));
1576
1577 add_com ("forward-search", class_files, forward_search_command,
1578 "Search for regular expression (see regex(3)) from last line listed.\n\
1579 The matching line number is also stored as the value of \"$_\".");
1580 add_com_alias ("search", "forward-search", class_files, 0);
1581
1582 add_com ("reverse-search", class_files, reverse_search_command,
1583 "Search backward for regular expression (see regex(3)) from last line listed.\n\
1584 The matching line number is also stored as the value of \"$_\".");
1585
1586 if (xdb_commands)
1587 {
1588 add_com_alias ("/", "forward-search", class_files, 0);
1589 add_com_alias ("?", "reverse-search", class_files, 0);
1590 }
1591
1592 add_show_from_set
1593 (add_set_cmd ("listsize", class_support, var_uinteger,
1594 (char *) &lines_to_list,
1595 "Set number of source lines gdb will list by default.",
1596 &setlist),
1597 &showlist);
1598 }
This page took 0.110074 seconds and 4 git commands to generate.