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