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