* Makefile.in (objdump.o): Deleted special rule.
[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, 1991 Free Software Foundation, Inc.
3
4 This file is part of GDB.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
19
20 #include "defs.h"
21 #include "symtab.h"
22 #include "expression.h"
23 #include "language.h"
24 #include "command.h"
25 #include "gdbcmd.h"
26 #include "frame.h"
27
28 #include <sys/types.h>
29 #include <string.h>
30 #include <sys/param.h>
31 #include <sys/stat.h>
32 #include <fcntl.h>
33 #include "gdbcore.h"
34 #include "regex.h"
35 #include "symfile.h"
36 #include "objfiles.h"
37
38 #ifndef DIRNAME_SEPARATOR
39 #define DIRNAME_SEPARATOR ':'
40 #endif
41
42 /* Prototypes for local functions. */
43
44 static int
45 open_source_file PARAMS ((struct symtab *));
46
47 static int
48 get_filename_and_charpos PARAMS ((struct symtab *, char **));
49
50 static void
51 reverse_search_command PARAMS ((char *, int));
52
53 static void
54 forward_search_command PARAMS ((char *, int));
55
56 static void
57 line_info PARAMS ((char *, int));
58
59 static void
60 list_command PARAMS ((char *, int));
61
62 static void
63 ambiguous_line_spec PARAMS ((struct symtabs_and_lines *));
64
65 static void
66 source_info PARAMS ((char *, int));
67
68 static void
69 show_directories PARAMS ((char *, int));
70
71 static void
72 find_source_lines PARAMS ((struct symtab *, int));
73
74 /* If we use this declaration, it breaks because of fucking ANSI "const" stuff
75 on some systems. We just have to not declare it at all, have it default
76 to int, and possibly botch on a few systems. Thanks, ANSIholes... */
77 /* extern char *strstr(); */
78
79 /* Path of directories to search for source files.
80 Same format as the PATH environment variable's value. */
81
82 char *source_path;
83
84 /* Symtab of default file for listing lines of. */
85
86 struct symtab *current_source_symtab;
87
88 /* Default next line to list. */
89
90 int current_source_line;
91
92 /* Default number of lines to print with commands like "list".
93 This is based on guessing how many long (i.e. more than chars_per_line
94 characters) lines there will be. To be completely correct, "list"
95 and friends should be rewritten to count characters and see where
96 things are wrapping, but that would be a fair amount of work. */
97
98 int lines_to_list = 10;
99
100 /* Line number of last line printed. Default for various commands.
101 current_source_line is usually, but not always, the same as this. */
102
103 static int last_line_listed;
104
105 /* First line number listed by last listing command. */
106
107 static int first_line_listed;
108
109 \f
110 /* Set the source file default for the "list" command to be S.
111
112 If S is NULL, and we don't have a default, find one. This
113 should only be called when the user actually tries to use the
114 default, since we produce an error if we can't find a reasonable
115 default. Also, since this can cause symbols to be read, doing it
116 before we need to would make things slower than necessary. */
117
118 void
119 select_source_symtab (s)
120 register struct symtab *s;
121 {
122 struct symtabs_and_lines sals;
123 struct symtab_and_line sal;
124 struct partial_symtab *ps;
125 struct partial_symtab *cs_pst = 0;
126 struct objfile *ofp;
127
128 if (s)
129 {
130 current_source_symtab = s;
131 current_source_line = 1;
132 return;
133 }
134
135 if (current_source_symtab)
136 return;
137
138 /* Make the default place to list be the function `main'
139 if one exists. */
140 if (lookup_symbol ("main", 0, VAR_NAMESPACE, 0, NULL))
141 {
142 sals = decode_line_spec ("main", 1);
143 sal = sals.sals[0];
144 free (sals.sals);
145 current_source_symtab = sal.symtab;
146 current_source_line = max (sal.line - (lines_to_list - 1), 1);
147 if (current_source_symtab)
148 return;
149 }
150
151 /* All right; find the last file in the symtab list (ignoring .h's). */
152
153 current_source_line = 1;
154
155 for (ofp = object_files; ofp != NULL; ofp = ofp -> next)
156 {
157 for (s = ofp -> symtabs; s; s = s->next)
158 {
159 char *name = s -> filename;
160 int len = strlen (name);
161 if (! (len > 2 && (STREQ (&name[len - 2], ".h"))))
162 {
163 current_source_symtab = s;
164 }
165 }
166 }
167 if (current_source_symtab)
168 return;
169
170 /* Howabout the partial symbol tables? */
171
172 for (ofp = object_files; ofp != NULL; ofp = ofp -> next)
173 {
174 for (ps = ofp -> psymtabs; ps != NULL; ps = ps -> next)
175 {
176 char *name = ps -> filename;
177 int len = strlen (name);
178 if (! (len > 2 && (STREQ (&name[len - 2], ".h"))))
179 {
180 cs_pst = ps;
181 }
182 }
183 }
184 if (cs_pst)
185 {
186 if (cs_pst -> readin)
187 {
188 fatal ("Internal: select_source_symtab: readin pst found and no symtabs.");
189 }
190 else
191 {
192 current_source_symtab = PSYMTAB_TO_SYMTAB (cs_pst);
193 }
194 }
195
196 error ("Can't find a default source file");
197 }
198 \f
199 static void
200 show_directories (ignore, from_tty)
201 char *ignore;
202 int from_tty;
203 {
204 puts_filtered ("Source directories searched: ");
205 puts_filtered (source_path);
206 puts_filtered ("\n");
207 }
208
209 /* Forget what we learned about line positions in source files,
210 and which directories contain them;
211 must check again now since files may be found in
212 a different directory now. */
213
214 void
215 forget_cached_source_info ()
216 {
217 register struct symtab *s;
218 register struct objfile *objfile;
219
220 for (objfile = object_files; objfile != NULL; objfile = objfile -> next)
221 {
222 for (s = objfile -> symtabs; s != NULL; s = s -> next)
223 {
224 if (s -> line_charpos != NULL)
225 {
226 mfree (objfile -> md, s -> line_charpos);
227 s -> line_charpos = NULL;
228 }
229 if (s -> fullname != NULL)
230 {
231 mfree (objfile -> md, s -> fullname);
232 s -> fullname = NULL;
233 }
234 }
235 }
236 }
237
238 void
239 init_source_path ()
240 {
241 char buf[20];
242
243 sprintf (buf, "$cdir%c$cwd", DIRNAME_SEPARATOR);
244 source_path = strsave (buf);
245 forget_cached_source_info ();
246 }
247
248 /* Add zero or more directories to the front of the source path. */
249
250 void
251 directory_command (dirname, from_tty)
252 char *dirname;
253 int from_tty;
254 {
255 dont_repeat ();
256 /* FIXME, this goes to "delete dir"... */
257 if (dirname == 0)
258 {
259 if (query ("Reinitialize source path to empty? ", ""))
260 {
261 free (source_path);
262 init_source_path ();
263 }
264 }
265 else
266 mod_path (dirname, &source_path);
267 if (from_tty)
268 show_directories ((char *)0, from_tty);
269 forget_cached_source_info ();
270 }
271
272 /* Add zero or more directories to the front of an arbitrary path. */
273
274 void
275 mod_path (dirname, which_path)
276 char *dirname;
277 char **which_path;
278 {
279 char *old = *which_path;
280 int prefix = 0;
281
282 if (dirname == 0)
283 return;
284
285 dirname = strsave (dirname);
286 make_cleanup (free, dirname);
287
288 do
289 {
290 char *name = dirname;
291 register char *p;
292 struct stat st;
293
294 {
295 char *colon = strchr (name, DIRNAME_SEPARATOR);
296 char *space = strchr (name, ' ');
297 char *tab = strchr (name, '\t');
298 if (colon == 0 && space == 0 && tab == 0)
299 p = dirname = name + strlen (name);
300 else
301 {
302 p = 0;
303 if (colon != 0 && (p == 0 || colon < p))
304 p = colon;
305 if (space != 0 && (p == 0 || space < p))
306 p = space;
307 if (tab != 0 && (p == 0 || tab < p))
308 p = tab;
309 dirname = p + 1;
310 while (*dirname == DIRNAME_SEPARATOR || *dirname == ' ' || *dirname == '\t')
311 ++dirname;
312 }
313 }
314
315 if (p[-1] == '/')
316 /* Sigh. "foo/" => "foo" */
317 --p;
318 *p = '\0';
319
320 while (p[-1] == '.')
321 {
322 if (p - name == 1)
323 {
324 /* "." => getwd (). */
325 name = current_directory;
326 goto append;
327 }
328 else if (p[-2] == '/')
329 {
330 if (p - name == 2)
331 {
332 /* "/." => "/". */
333 *--p = '\0';
334 goto append;
335 }
336 else
337 {
338 /* "...foo/." => "...foo". */
339 p -= 2;
340 *p = '\0';
341 continue;
342 }
343 }
344 else
345 break;
346 }
347
348 if (name[0] == '~')
349 name = tilde_expand (name);
350 else if (name[0] != '/' && name[0] != '$')
351 name = concat (current_directory, "/", name, NULL);
352 else
353 name = savestring (name, p - name);
354 make_cleanup (free, name);
355
356 /* Unless it's a variable, check existence. */
357 if (name[0] != '$') {
358 /* These are warnings, not errors, since we don't want a
359 non-existent directory in a .gdbinit file to stop processing
360 of the .gdbinit file.
361
362 Whether they get added to the path is more debatable. Current
363 answer is yes, in case the user wants to go make the directory
364 or whatever. If the directory continues to not exist/not be
365 a directory/etc, then having them in the path should be
366 harmless. */
367 if (stat (name, &st) < 0)
368 {
369 int save_errno = errno;
370 fprintf_unfiltered (gdb_stderr, "Warning: ");
371 print_sys_errmsg (name, save_errno);
372 }
373 else if ((st.st_mode & S_IFMT) != S_IFDIR)
374 warning ("%s is not a directory.", name);
375 }
376
377 append:
378 {
379 register unsigned int len = strlen (name);
380
381 p = *which_path;
382 while (1)
383 {
384 if (!strncmp (p, name, len)
385 && (p[len] == '\0' || p[len] == DIRNAME_SEPARATOR))
386 {
387 /* Found it in the search path, remove old copy */
388 if (p > *which_path)
389 p--; /* Back over leading colon */
390 if (prefix > p - *which_path)
391 goto skip_dup; /* Same dir twice in one cmd */
392 strcpy (p, &p[len+1]); /* Copy from next \0 or : */
393 }
394 p = strchr (p, DIRNAME_SEPARATOR);
395 if (p != 0)
396 ++p;
397 else
398 break;
399 }
400 if (p == 0)
401 {
402 char tinybuf[2];
403
404 tinybuf[0] = DIRNAME_SEPARATOR;
405 tinybuf[1] = '\0';
406
407 /* If we have already tacked on a name(s) in this command, be sure they stay on the front as we tack on some more. */
408 if (prefix)
409 {
410 char *temp, c;
411
412 c = old[prefix];
413 old[prefix] = '\0';
414 temp = concat (old, tinybuf, name, NULL);
415 old[prefix] = c;
416 *which_path = concat (temp, "", &old[prefix], NULL);
417 prefix = strlen (temp);
418 free (temp);
419 }
420 else
421 {
422 *which_path = concat (name, (old[0] ? tinybuf : old), old, NULL);
423 prefix = strlen (name);
424 }
425 free (old);
426 old = *which_path;
427 }
428 }
429 skip_dup: ;
430 } while (*dirname != '\0');
431 }
432
433
434 static void
435 source_info (ignore, from_tty)
436 char *ignore;
437 int from_tty;
438 {
439 register struct symtab *s = current_source_symtab;
440
441 if (!s)
442 {
443 printf_filtered("No current source file.\n");
444 return;
445 }
446 printf_filtered ("Current source file is %s\n", s->filename);
447 if (s->dirname)
448 printf_filtered ("Compilation directory is %s\n", s->dirname);
449 if (s->fullname)
450 printf_filtered ("Located in %s\n", s->fullname);
451 if (s->nlines)
452 printf_filtered ("Contains %d line%s.\n", s->nlines,
453 s->nlines == 1 ? "" : "s");
454
455 printf_filtered("Source language is %s.\n", language_str (s->language));
456 }
457
458
459 \f
460 /* Open a file named STRING, searching path PATH (dir names sep by some char)
461 using mode MODE and protection bits PROT in the calls to open.
462
463 If TRY_CWD_FIRST, try to open ./STRING before searching PATH.
464 (ie pretend the first element of PATH is "."). This also indicates
465 that a slash in STRING disables searching of the path (this is
466 so that "exec-file ./foo" or "symbol-file ./foo" insures that you
467 get that particular version of foo or an error message).
468
469 If FILENAMED_OPENED is non-null, set it to a newly allocated string naming
470 the actual file opened (this string will always start with a "/". We
471 have to take special pains to avoid doubling the "/" between the directory
472 and the file, sigh! Emacs gets confuzzed by this when we print the
473 source file name!!!
474
475 If a file is found, return the descriptor.
476 Otherwise, return -1, with errno set for the last name we tried to open. */
477
478 /* >>>> This should only allow files of certain types,
479 >>>> eg executable, non-directory */
480 int
481 openp (path, try_cwd_first, string, mode, prot, filename_opened)
482 char *path;
483 int try_cwd_first;
484 char *string;
485 int mode;
486 int prot;
487 char **filename_opened;
488 {
489 register int fd;
490 register char *filename;
491 register char *p, *p1;
492 register int len;
493 int alloclen;
494
495 if (!path)
496 path = ".";
497
498 if (try_cwd_first || string[0] == '/')
499 {
500 filename = string;
501 fd = open (filename, mode, prot);
502 if (fd >= 0 || string[0] == '/' || strchr (string, '/'))
503 goto done;
504 }
505
506 /* ./foo => foo */
507 while (string[0] == '.' && string[1] == '/')
508 string += 2;
509
510 alloclen = strlen (path) + strlen (string) + 2;
511 filename = (char *) alloca (alloclen);
512 fd = -1;
513 for (p = path; p; p = p1 ? p1 + 1 : 0)
514 {
515 p1 = (char *) strchr (p, DIRNAME_SEPARATOR);
516 if (p1)
517 len = p1 - p;
518 else
519 len = strlen (p);
520
521 if (len == 4 && p[0] == '$' && p[1] == 'c'
522 && p[2] == 'w' && p[3] == 'd') {
523 /* Name is $cwd -- insert current directory name instead. */
524 int newlen;
525
526 /* First, realloc the filename buffer if too short. */
527 len = strlen (current_directory);
528 newlen = len + strlen (string) + 2;
529 if (newlen > alloclen) {
530 alloclen = newlen;
531 filename = (char *) alloca (alloclen);
532 }
533 strcpy (filename, current_directory);
534 } else {
535 /* Normal file name in path -- just use it. */
536 strncpy (filename, p, len);
537 filename[len] = 0;
538 }
539
540 /* Remove trailing slashes */
541 while (len > 0 && filename[len-1] == '/')
542 filename[--len] = 0;
543
544 strcat (filename+len, "/");
545 strcat (filename, string);
546
547 fd = open (filename, mode);
548 if (fd >= 0) break;
549 }
550
551 done:
552 if (filename_opened)
553 if (fd < 0)
554 *filename_opened = (char *) 0;
555 else if (filename[0] == '/')
556 *filename_opened = savestring (filename, strlen (filename));
557 else
558 {
559 /* Beware the // my son, the Emacs barfs, the botch that catch... */
560
561 *filename_opened = concat (current_directory,
562 '/' == current_directory[strlen(current_directory)-1]? "": "/",
563 filename, NULL);
564 }
565
566 return fd;
567 }
568
569 /* Open a source file given a symtab S. Returns a file descriptor
570 or negative number for error. */
571
572 static int
573 open_source_file (s)
574 struct symtab *s;
575 {
576 char *path = source_path;
577 char *p;
578 int result;
579 char *fullname;
580
581 /* Quick way out if we already know its full name */
582 if (s->fullname)
583 {
584 result = open (s->fullname, O_RDONLY);
585 if (result >= 0)
586 return result;
587 /* Didn't work -- free old one, try again. */
588 mfree (s->objfile->md, s->fullname);
589 s->fullname = NULL;
590 }
591
592 if (s->dirname != NULL)
593 {
594 /* Replace a path entry of $cdir with the compilation directory name */
595 #define cdir_len 5
596 /* We cast strstr's result in case an ANSIhole has made it const,
597 which produces a "required warning" when assigned to a nonconst. */
598 p = (char *)strstr (source_path, "$cdir");
599 if (p && (p == path || p[-1] == DIRNAME_SEPARATOR)
600 && (p[cdir_len] == DIRNAME_SEPARATOR || p[cdir_len] == '\0')) {
601 int len;
602
603 path = (char *)
604 alloca (strlen (source_path) + 1 + strlen (s->dirname) + 1);
605 len = p - source_path;
606 strncpy (path, source_path, len); /* Before $cdir */
607 strcpy (path + len, s->dirname); /* new stuff */
608 strcat (path + len, source_path + len + cdir_len); /* After $cdir */
609 }
610 }
611
612 result = openp (path, 0, s->filename, O_RDONLY, 0, &s->fullname);
613 if (result < 0)
614 {
615 /* Didn't work. Try using just the basename. */
616 p = basename (s->filename);
617 if (p != s->filename)
618 result = openp (path, 0, p, O_RDONLY, 0, &s->fullname);
619 }
620 if (result >= 0)
621 {
622 fullname = s->fullname;
623 s->fullname = mstrsave (s->objfile->md, s->fullname);
624 free (fullname);
625 }
626 return result;
627 }
628
629 \f
630 /* Create and initialize the table S->line_charpos that records
631 the positions of the lines in the source file, which is assumed
632 to be open on descriptor DESC.
633 All set S->nlines to the number of such lines. */
634
635 static void
636 find_source_lines (s, desc)
637 struct symtab *s;
638 int desc;
639 {
640 struct stat st;
641 register char *data, *p, *end;
642 int nlines = 0;
643 int lines_allocated = 1000;
644 int *line_charpos;
645 long exec_mtime;
646 int size;
647 #ifdef LSEEK_NOT_LINEAR
648 char c;
649 #endif
650
651 line_charpos = (int *) xmmalloc (s -> objfile -> md,
652 lines_allocated * sizeof (int));
653 if (fstat (desc, &st) < 0)
654 perror_with_name (s->filename);
655
656 if (exec_bfd) {
657 exec_mtime = bfd_get_mtime(exec_bfd);
658 if (exec_mtime && exec_mtime < st.st_mtime)
659 printf_filtered ("Source file is more recent than executable.\n");
660 }
661
662 #ifdef LSEEK_NOT_LINEAR
663 /* Have to read it byte by byte to find out where the chars live */
664
665 line_charpos[0] = tell(desc);
666 nlines = 1;
667 while (myread(desc, &c, 1)>0)
668 {
669 if (c == '\n')
670 {
671 if (nlines == lines_allocated)
672 {
673 lines_allocated *= 2;
674 line_charpos =
675 (int *) xmrealloc (s -> objfile -> md, (char *) line_charpos,
676 sizeof (int) * lines_allocated);
677 }
678 line_charpos[nlines++] = tell(desc);
679 }
680 }
681
682 #else
683 /* st_size might be a large type, but we only support source files whose
684 size fits in an int. FIXME. */
685 size = (int) st.st_size;
686
687 #ifdef BROKEN_LARGE_ALLOCA
688 data = (char *) xmalloc (size);
689 make_cleanup (free, data);
690 #else
691 data = (char *) alloca (size);
692 #endif
693 if (myread (desc, data, size) < 0)
694 perror_with_name (s->filename);
695 end = data + size;
696 p = data;
697 line_charpos[0] = 0;
698 nlines = 1;
699 while (p != end)
700 {
701 if (*p++ == '\n'
702 /* A newline at the end does not start a new line. */
703 && p != end)
704 {
705 if (nlines == lines_allocated)
706 {
707 lines_allocated *= 2;
708 line_charpos =
709 (int *) xmrealloc (s -> objfile -> md, (char *) line_charpos,
710 sizeof (int) * lines_allocated);
711 }
712 line_charpos[nlines++] = p - data;
713 }
714 }
715 #endif
716 s->nlines = nlines;
717 s->line_charpos =
718 (int *) xmrealloc (s -> objfile -> md, (char *) line_charpos,
719 nlines * sizeof (int));
720
721 }
722
723 /* Return the character position of a line LINE in symtab S.
724 Return 0 if anything is invalid. */
725
726 #if 0 /* Currently unused */
727
728 int
729 source_line_charpos (s, line)
730 struct symtab *s;
731 int line;
732 {
733 if (!s) return 0;
734 if (!s->line_charpos || line <= 0) return 0;
735 if (line > s->nlines)
736 line = s->nlines;
737 return s->line_charpos[line - 1];
738 }
739
740 /* Return the line number of character position POS in symtab S. */
741
742 int
743 source_charpos_line (s, chr)
744 register struct symtab *s;
745 register int chr;
746 {
747 register int line = 0;
748 register int *lnp;
749
750 if (s == 0 || s->line_charpos == 0) return 0;
751 lnp = s->line_charpos;
752 /* Files are usually short, so sequential search is Ok */
753 while (line < s->nlines && *lnp <= chr)
754 {
755 line++;
756 lnp++;
757 }
758 if (line >= s->nlines)
759 line = s->nlines;
760 return line;
761 }
762
763 #endif /* 0 */
764
765 \f
766 /* Get full pathname and line number positions for a symtab.
767 Return nonzero if line numbers may have changed.
768 Set *FULLNAME to actual name of the file as found by `openp',
769 or to 0 if the file is not found. */
770
771 static int
772 get_filename_and_charpos (s, fullname)
773 struct symtab *s;
774 char **fullname;
775 {
776 register int desc, linenums_changed = 0;
777
778 desc = open_source_file (s);
779 if (desc < 0)
780 {
781 if (fullname)
782 *fullname = NULL;
783 return 0;
784 }
785 if (fullname)
786 *fullname = s->fullname;
787 if (s->line_charpos == 0) linenums_changed = 1;
788 if (linenums_changed) find_source_lines (s, desc);
789 close (desc);
790 return linenums_changed;
791 }
792
793 /* Print text describing the full name of the source file S
794 and the line number LINE and its corresponding character position.
795 The text starts with two Ctrl-z so that the Emacs-GDB interface
796 can easily find it.
797
798 MID_STATEMENT is nonzero if the PC is not at the beginning of that line.
799
800 Return 1 if successful, 0 if could not find the file. */
801
802 int
803 identify_source_line (s, line, mid_statement, pc)
804 struct symtab *s;
805 int line;
806 int mid_statement;
807 CORE_ADDR pc;
808 {
809 if (s->line_charpos == 0)
810 get_filename_and_charpos (s, (char **)NULL);
811 if (s->fullname == 0)
812 return 0;
813 if (line > s->nlines)
814 /* Don't index off the end of the line_charpos array. */
815 return 0;
816 /* FIXME-32x64: Need a version of print_address_numeric which does
817 not pass use_local to print_longest. */
818 printf_unfiltered ("\032\032%s:%d:%d:%s:0x%lx\n", s->fullname,
819 line, s->line_charpos[line - 1],
820 mid_statement ? "middle" : "beg",
821 (unsigned long) pc);
822 current_source_line = line;
823 first_line_listed = line;
824 last_line_listed = line;
825 current_source_symtab = s;
826 return 1;
827 }
828 \f
829 /* Print source lines from the file of symtab S,
830 starting with line number LINE and stopping before line number STOPLINE. */
831
832 void
833 print_source_lines (s, line, stopline, noerror)
834 struct symtab *s;
835 int line, stopline;
836 int noerror;
837 {
838 register int c;
839 register int desc;
840 register FILE *stream;
841 int nlines = stopline - line;
842
843 /* Regardless of whether we can open the file, set current_source_symtab. */
844 current_source_symtab = s;
845 current_source_line = line;
846 first_line_listed = line;
847
848 desc = open_source_file (s);
849 if (desc < 0)
850 {
851 if (! noerror) {
852 char *name = alloca (strlen (s->filename) + 100);
853 sprintf (name, "%s:%d", s->filename, line);
854 print_sys_errmsg (name, errno);
855 }
856 return;
857 }
858
859 if (s->line_charpos == 0)
860 find_source_lines (s, desc);
861
862 if (line < 1 || line > s->nlines)
863 {
864 close (desc);
865 error ("Line number %d out of range; %s has %d lines.",
866 line, s->filename, s->nlines);
867 }
868
869 if (lseek (desc, s->line_charpos[line - 1], 0) < 0)
870 {
871 close (desc);
872 perror_with_name (s->filename);
873 }
874
875 stream = fdopen (desc, FOPEN_RT);
876 clearerr (stream);
877
878 while (nlines-- > 0)
879 {
880 c = fgetc (stream);
881 if (c == EOF) break;
882 last_line_listed = current_source_line;
883 printf_filtered ("%d\t", current_source_line++);
884 do
885 {
886 if (c < 040 && c != '\t' && c != '\n' && c != '\r')
887 printf_filtered ("^%c", c + 0100);
888 else if (c == 0177)
889 printf_filtered ("^?");
890 else
891 printf_filtered ("%c", c);
892 } while (c != '\n' && (c = fgetc (stream)) >= 0);
893 }
894
895 fclose (stream);
896 }
897 \f
898
899
900 /*
901 C++
902 Print a list of files and line numbers which a user may choose from
903 in order to list a function which was specified ambiguously
904 (as with `list classname::overloadedfuncname', for example).
905 The vector in SALS provides the filenames and line numbers.
906 */
907 static void
908 ambiguous_line_spec (sals)
909 struct symtabs_and_lines *sals;
910 {
911 int i;
912
913 for (i = 0; i < sals->nelts; ++i)
914 printf_filtered("file: \"%s\", line number: %d\n",
915 sals->sals[i].symtab->filename, sals->sals[i].line);
916 }
917
918
919 static void
920 list_command (arg, from_tty)
921 char *arg;
922 int from_tty;
923 {
924 struct symtabs_and_lines sals, sals_end;
925 struct symtab_and_line sal, sal_end;
926 struct symbol *sym;
927 char *arg1;
928 int no_end = 1;
929 int dummy_end = 0;
930 int dummy_beg = 0;
931 int linenum_beg = 0;
932 char *p;
933
934 if (!have_full_symbols () && !have_partial_symbols())
935 error ("No symbol table is loaded. Use the \"file\" command.");
936
937 /* Pull in a current source symtab if necessary */
938 if (current_source_symtab == 0 &&
939 (arg == 0 || arg[0] == '+' || arg[0] == '-'))
940 select_source_symtab (0);
941
942 /* "l" or "l +" lists next ten lines. */
943
944 if (arg == 0 || STREQ (arg, "+"))
945 {
946 if (current_source_symtab == 0)
947 error ("No default source file yet. Do \"help list\".");
948 print_source_lines (current_source_symtab, current_source_line,
949 current_source_line + lines_to_list, 0);
950 return;
951 }
952
953 /* "l -" lists previous ten lines, the ones before the ten just listed. */
954 if (STREQ (arg, "-"))
955 {
956 if (current_source_symtab == 0)
957 error ("No default source file yet. Do \"help list\".");
958 print_source_lines (current_source_symtab,
959 max (first_line_listed - lines_to_list, 1),
960 first_line_listed, 0);
961 return;
962 }
963
964 /* Now if there is only one argument, decode it in SAL
965 and set NO_END.
966 If there are two arguments, decode them in SAL and SAL_END
967 and clear NO_END; however, if one of the arguments is blank,
968 set DUMMY_BEG or DUMMY_END to record that fact. */
969
970 arg1 = arg;
971 if (*arg1 == ',')
972 dummy_beg = 1;
973 else
974 {
975 sals = decode_line_1 (&arg1, 0, 0, 0, 0);
976
977 if (! sals.nelts) return; /* C++ */
978 if (sals.nelts > 1)
979 {
980 ambiguous_line_spec (&sals);
981 free (sals.sals);
982 return;
983 }
984
985 sal = sals.sals[0];
986 free (sals.sals);
987 }
988
989 /* Record whether the BEG arg is all digits. */
990
991 for (p = arg; p != arg1 && *p >= '0' && *p <= '9'; p++);
992 linenum_beg = (p == arg1);
993
994 while (*arg1 == ' ' || *arg1 == '\t')
995 arg1++;
996 if (*arg1 == ',')
997 {
998 no_end = 0;
999 arg1++;
1000 while (*arg1 == ' ' || *arg1 == '\t')
1001 arg1++;
1002 if (*arg1 == 0)
1003 dummy_end = 1;
1004 else
1005 {
1006 if (dummy_beg)
1007 sals_end = decode_line_1 (&arg1, 0, 0, 0, 0);
1008 else
1009 sals_end = decode_line_1 (&arg1, 0, sal.symtab, sal.line, 0);
1010 if (sals_end.nelts == 0)
1011 return;
1012 if (sals_end.nelts > 1)
1013 {
1014 ambiguous_line_spec (&sals_end);
1015 free (sals_end.sals);
1016 return;
1017 }
1018 sal_end = sals_end.sals[0];
1019 free (sals_end.sals);
1020 }
1021 }
1022
1023 if (*arg1)
1024 error ("Junk at end of line specification.");
1025
1026 if (!no_end && !dummy_beg && !dummy_end
1027 && sal.symtab != sal_end.symtab)
1028 error ("Specified start and end are in different files.");
1029 if (dummy_beg && dummy_end)
1030 error ("Two empty args do not say what lines to list.");
1031
1032 /* if line was specified by address,
1033 first print exactly which line, and which file.
1034 In this case, sal.symtab == 0 means address is outside
1035 of all known source files, not that user failed to give a filename. */
1036 if (*arg == '*')
1037 {
1038 if (sal.symtab == 0)
1039 /* FIXME-32x64--assumes sal.pc fits in long. */
1040 error ("No source file for address %s.",
1041 local_hex_string((unsigned long) sal.pc));
1042 sym = find_pc_function (sal.pc);
1043 if (sym)
1044 {
1045 print_address_numeric (sal.pc, gdb_stdout);
1046 printf_filtered (" is in ");
1047 fputs_filtered (SYMBOL_SOURCE_NAME (sym), gdb_stdout);
1048 printf_filtered (" (%s:%d).\n", sal.symtab->filename, sal.line);
1049 }
1050 else
1051 {
1052 print_address_numeric (sal.pc, gdb_stdout);
1053 printf_filtered (" is at %s:%d.\n",
1054 sal.symtab->filename, sal.line);
1055 }
1056 }
1057
1058 /* If line was not specified by just a line number,
1059 and it does not imply a symtab, it must be an undebuggable symbol
1060 which means no source code. */
1061
1062 if (! linenum_beg && sal.symtab == 0)
1063 error ("No line number known for %s.", arg);
1064
1065 /* If this command is repeated with RET,
1066 turn it into the no-arg variant. */
1067
1068 if (from_tty)
1069 *arg = 0;
1070
1071 if (dummy_beg && sal_end.symtab == 0)
1072 error ("No default source file yet. Do \"help list\".");
1073 if (dummy_beg)
1074 print_source_lines (sal_end.symtab,
1075 max (sal_end.line - (lines_to_list - 1), 1),
1076 sal_end.line + 1, 0);
1077 else if (sal.symtab == 0)
1078 error ("No default source file yet. Do \"help list\".");
1079 else if (no_end)
1080 print_source_lines (sal.symtab,
1081 max (sal.line - (lines_to_list / 2), 1),
1082 sal.line + (lines_to_list / 2), 0);
1083 else
1084 print_source_lines (sal.symtab, sal.line,
1085 (dummy_end
1086 ? sal.line + lines_to_list
1087 : sal_end.line + 1),
1088 0);
1089 }
1090 \f
1091 /* Print info on range of pc's in a specified line. */
1092
1093 static void
1094 line_info (arg, from_tty)
1095 char *arg;
1096 int from_tty;
1097 {
1098 struct symtabs_and_lines sals;
1099 struct symtab_and_line sal;
1100 CORE_ADDR start_pc, end_pc;
1101 int i;
1102
1103 if (arg == 0)
1104 {
1105 sal.symtab = current_source_symtab;
1106 sal.line = last_line_listed;
1107 sals.nelts = 1;
1108 sals.sals = (struct symtab_and_line *)
1109 xmalloc (sizeof (struct symtab_and_line));
1110 sals.sals[0] = sal;
1111 }
1112 else
1113 {
1114 sals = decode_line_spec_1 (arg, 0);
1115
1116 dont_repeat ();
1117 }
1118
1119 /* C++ More than one line may have been specified, as when the user
1120 specifies an overloaded function name. Print info on them all. */
1121 for (i = 0; i < sals.nelts; i++)
1122 {
1123 sal = sals.sals[i];
1124
1125 if (sal.symtab == 0)
1126 {
1127 printf_filtered ("No line number information available");
1128 if (sal.pc != 0)
1129 {
1130 /* This is useful for "info line *0x7f34". If we can't tell the
1131 user about a source line, at least let them have the symbolic
1132 address. */
1133 printf_filtered (" for address ");
1134 wrap_here (" ");
1135 print_address (sal.pc, gdb_stdout);
1136 }
1137 else
1138 printf_filtered (".");
1139 printf_filtered ("\n");
1140 }
1141 else if (sal.line > 0
1142 && find_line_pc_range (sal, &start_pc, &end_pc))
1143 {
1144 if (start_pc == end_pc)
1145 {
1146 printf_filtered ("Line %d of \"%s\"",
1147 sal.line, sal.symtab->filename);
1148 wrap_here (" ");
1149 printf_filtered (" is at address ");
1150 print_address (start_pc, gdb_stdout);
1151 wrap_here (" ");
1152 printf_filtered (" but contains no code.\n");
1153 }
1154 else
1155 {
1156 printf_filtered ("Line %d of \"%s\"",
1157 sal.line, sal.symtab->filename);
1158 wrap_here (" ");
1159 printf_filtered (" starts at address ");
1160 print_address (start_pc, gdb_stdout);
1161 wrap_here (" ");
1162 printf_filtered (" and ends at ");
1163 print_address (end_pc, gdb_stdout);
1164 printf_filtered (".\n");
1165 }
1166
1167 /* x/i should display this line's code. */
1168 set_next_address (start_pc);
1169
1170 /* Repeating "info line" should do the following line. */
1171 last_line_listed = sal.line + 1;
1172
1173 /* If this is the only line, show the source code. If it could
1174 not find the file, don't do anything special. */
1175 if (frame_file_full_name && sals.nelts == 1)
1176 identify_source_line (sal.symtab, sal.line, 0, start_pc);
1177 }
1178 else
1179 /* Is there any case in which we get here, and have an address
1180 which the user would want to see? If we have debugging symbols
1181 and no line numbers? */
1182 printf_filtered ("Line number %d is out of range for \"%s\".\n",
1183 sal.line, sal.symtab->filename);
1184 }
1185 free (sals.sals);
1186 }
1187 \f
1188 /* Commands to search the source file for a regexp. */
1189
1190 /* ARGSUSED */
1191 static void
1192 forward_search_command (regex, from_tty)
1193 char *regex;
1194 int from_tty;
1195 {
1196 register int c;
1197 register int desc;
1198 register FILE *stream;
1199 int line = last_line_listed + 1;
1200 char *msg;
1201
1202 msg = (char *) re_comp (regex);
1203 if (msg)
1204 error (msg);
1205
1206 if (current_source_symtab == 0)
1207 select_source_symtab (0);
1208
1209 /* Search from last_line_listed+1 in current_source_symtab */
1210
1211 desc = open_source_file (current_source_symtab);
1212 if (desc < 0)
1213 perror_with_name (current_source_symtab->filename);
1214
1215 if (current_source_symtab->line_charpos == 0)
1216 find_source_lines (current_source_symtab, desc);
1217
1218 if (line < 1 || line > current_source_symtab->nlines)
1219 {
1220 close (desc);
1221 error ("Expression not found");
1222 }
1223
1224 if (lseek (desc, current_source_symtab->line_charpos[line - 1], 0) < 0)
1225 {
1226 close (desc);
1227 perror_with_name (current_source_symtab->filename);
1228 }
1229
1230 stream = fdopen (desc, FOPEN_RT);
1231 clearerr (stream);
1232 while (1) {
1233 /* FIXME!!! We walk right off the end of buf if we get a long line!!! */
1234 char buf[4096]; /* Should be reasonable??? */
1235 register char *p = buf;
1236
1237 c = getc (stream);
1238 if (c == EOF)
1239 break;
1240 do {
1241 *p++ = c;
1242 } while (c != '\n' && (c = getc (stream)) >= 0);
1243
1244 /* we now have a source line in buf, null terminate and match */
1245 *p = 0;
1246 if (re_exec (buf) > 0)
1247 {
1248 /* Match! */
1249 fclose (stream);
1250 print_source_lines (current_source_symtab,
1251 line, line+1, 0);
1252 current_source_line = max (line - lines_to_list / 2, 1);
1253 return;
1254 }
1255 line++;
1256 }
1257
1258 printf_filtered ("Expression not found\n");
1259 fclose (stream);
1260 }
1261
1262 /* ARGSUSED */
1263 static void
1264 reverse_search_command (regex, from_tty)
1265 char *regex;
1266 int from_tty;
1267 {
1268 register int c;
1269 register int desc;
1270 register FILE *stream;
1271 int line = last_line_listed - 1;
1272 char *msg;
1273
1274 msg = (char *) re_comp (regex);
1275 if (msg)
1276 error (msg);
1277
1278 if (current_source_symtab == 0)
1279 select_source_symtab (0);
1280
1281 /* Search from last_line_listed-1 in current_source_symtab */
1282
1283 desc = open_source_file (current_source_symtab);
1284 if (desc < 0)
1285 perror_with_name (current_source_symtab->filename);
1286
1287 if (current_source_symtab->line_charpos == 0)
1288 find_source_lines (current_source_symtab, desc);
1289
1290 if (line < 1 || line > current_source_symtab->nlines)
1291 {
1292 close (desc);
1293 error ("Expression not found");
1294 }
1295
1296 if (lseek (desc, current_source_symtab->line_charpos[line - 1], 0) < 0)
1297 {
1298 close (desc);
1299 perror_with_name (current_source_symtab->filename);
1300 }
1301
1302 stream = fdopen (desc, FOPEN_RT);
1303 clearerr (stream);
1304 while (line > 1)
1305 {
1306 /* FIXME!!! We walk right off the end of buf if we get a long line!!! */
1307 char buf[4096]; /* Should be reasonable??? */
1308 register char *p = buf;
1309
1310 c = getc (stream);
1311 if (c == EOF)
1312 break;
1313 do {
1314 *p++ = c;
1315 } while (c != '\n' && (c = getc (stream)) >= 0);
1316
1317 /* We now have a source line in buf; null terminate and match. */
1318 *p = 0;
1319 if (re_exec (buf) > 0)
1320 {
1321 /* Match! */
1322 fclose (stream);
1323 print_source_lines (current_source_symtab,
1324 line, line+1, 0);
1325 current_source_line = max (line - lines_to_list / 2, 1);
1326 return;
1327 }
1328 line--;
1329 if (fseek (stream, current_source_symtab->line_charpos[line - 1], 0) < 0)
1330 {
1331 fclose (stream);
1332 perror_with_name (current_source_symtab->filename);
1333 }
1334 }
1335
1336 printf_filtered ("Expression not found\n");
1337 fclose (stream);
1338 return;
1339 }
1340 \f
1341 void
1342 _initialize_source ()
1343 {
1344 struct cmd_list_element *c;
1345 current_source_symtab = 0;
1346 init_source_path ();
1347
1348 /* The intention is to use POSIX Basic Regular Expressions.
1349 Always use the GNU regex routine for consistency across all hosts.
1350 Our current GNU regex.c does not have all the POSIX features, so this is
1351 just an approximation. */
1352 re_set_syntax (RE_SYNTAX_GREP);
1353
1354 c = add_cmd ("directory", class_files, directory_command,
1355 "Add directory DIR to beginning of search path for source files.\n\
1356 Forget cached info on source file locations and line positions.\n\
1357 DIR can also be $cwd for the current working directory, or $cdir for the\n\
1358 directory in which the source file was compiled into object code.\n\
1359 With no argument, reset the search path to $cdir:$cwd, the default.",
1360 &cmdlist);
1361 c->completer = filename_completer;
1362
1363 add_cmd ("directories", no_class, show_directories,
1364 "Current search path for finding source files.\n\
1365 $cwd in the path means the current working directory.\n\
1366 $cdir in the path means the compilation directory of the source file.",
1367 &showlist);
1368
1369 add_info ("source", source_info,
1370 "Information about the current source file.");
1371
1372 add_info ("line", line_info,
1373 "Core addresses of the code for a source line.\n\
1374 Line can be specified as\n\
1375 LINENUM, to list around that line in current file,\n\
1376 FILE:LINENUM, to list around that line in that file,\n\
1377 FUNCTION, to list around beginning of that function,\n\
1378 FILE:FUNCTION, to distinguish among like-named static functions.\n\
1379 Default is to describe the last source line that was listed.\n\n\
1380 This sets the default address for \"x\" to the line's first instruction\n\
1381 so that \"x/i\" suffices to start examining the machine code.\n\
1382 The address is also stored as the value of \"$_\".");
1383
1384 add_com ("forward-search", class_files, forward_search_command,
1385 "Search for regular expression (see regex(3)) from last line listed.");
1386 add_com_alias ("search", "forward-search", class_files, 0);
1387
1388 add_com ("reverse-search", class_files, reverse_search_command,
1389 "Search backward for regular expression (see regex(3)) from last line listed.");
1390
1391 add_com ("list", class_files, list_command,
1392 "List specified function or line.\n\
1393 With no argument, lists ten more lines after or around previous listing.\n\
1394 \"list -\" lists the ten lines before a previous ten-line listing.\n\
1395 One argument specifies a line, and ten lines are listed around that line.\n\
1396 Two arguments with comma between specify starting and ending lines to list.\n\
1397 Lines can be specified in these ways:\n\
1398 LINENUM, to list around that line in current file,\n\
1399 FILE:LINENUM, to list around that line in that file,\n\
1400 FUNCTION, to list around beginning of that function,\n\
1401 FILE:FUNCTION, to distinguish among like-named static functions.\n\
1402 *ADDRESS, to list around the line containing that address.\n\
1403 With two args if one is empty it stands for ten lines away from the other arg.");
1404 add_com_alias ("l", "list", class_files, 1);
1405
1406 add_show_from_set
1407 (add_set_cmd ("listsize", class_support, var_uinteger,
1408 (char *)&lines_to_list,
1409 "Set number of source lines gdb will list by default.",
1410 &setlist),
1411 &showlist);
1412 }
This page took 0.057585 seconds and 4 git commands to generate.