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