1 /* List lines of source files for GDB, the GNU debugger.
2 Copyright (C) 1986, 1987, 1988, 1989 Free Software Foundation, Inc.
4 This file is part of GDB.
6 GDB 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 1, or (at your option)
11 GDB 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.
16 You should have received a copy of the GNU General Public License
17 along with GDB; see the file COPYING. If not, write to
18 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
26 #include <sys/types.h>
30 #include <sys/param.h>
34 /* Path of directories to search for source files.
35 Same format as the PATH environment variable's value. */
37 static char *source_path;
39 /* Symtab of default file for listing lines of. */
41 struct symtab *current_source_symtab;
43 /* Default next line to list. */
45 int current_source_line;
47 /* Line number of last line printed. Default for various commands.
48 current_source_line is usually, but not always, the same as this. */
50 static int last_line_listed;
52 /* First line number listed by last listing command. */
54 static int first_line_listed;
57 struct symtab *psymtab_to_symtab ();
59 /* Set the source file default for the "list" command, specifying a
60 symtab. Sigh. Behaivior specification: If it is called with a
61 non-zero argument, that is the symtab to select. If it is not,
62 first lookup "main"; if it exists, use the symtab and line it
63 defines. If not, take the last symtab in the symtab_list (if it
64 exists) or the last symtab in the psytab_list (if *it* exists). If
65 none of this works, report an error. */
68 select_source_symtab (s)
69 register struct symtab *s;
71 struct symtabs_and_lines sals;
72 struct symtab_and_line sal;
73 struct partial_symtab *ps, *cs_pst;
77 current_source_symtab = s;
78 current_source_line = 1;
82 /* Make the default place to list be the function `main'
84 if (lookup_symbol ("main", 0, VAR_NAMESPACE, 0))
86 sals = decode_line_spec ("main", 1);
89 current_source_symtab = sal.symtab;
90 current_source_line = max (sal.line - 9, 1);
94 /* All right; find the last file in the symtab list (ignoring .h's). */
100 char *name = s->filename;
101 int len = strlen (name);
102 if (! (len > 2 && !strcmp (&name[len - 2], ".h")))
103 current_source_symtab = s;
107 current_source_line = 1;
109 else if (partial_symtab_list)
111 ps = partial_symtab_list;
114 char *name = ps->filename;
115 int len = strlen (name);
116 if (! (len > 2 && !strcmp (&name[len - 2], ".h")))
122 fatal ("Internal: select_source_symtab: readin pst found and no symtabs.");
124 current_source_symtab = psymtab_to_symtab (cs_pst);
126 current_source_symtab = 0;
127 current_source_line = 1;
134 printf ("Source directories searched: %s\n", source_path);
140 register struct symtab *s;
142 source_path = savestring (current_directory, strlen (current_directory));
144 /* Forget what we learned about line positions in source files;
145 must check again now since files may be found in
146 a different directory now. */
147 for (s = symtab_list; s; s = s->next)
148 if (s->line_charpos != 0)
150 free (s->line_charpos);
156 directory_command (dirname, from_tty)
160 char *old = source_path;
166 if (query ("Reinitialize source path to %s? ", current_directory))
174 dirname = tilde_expand (dirname);
175 make_cleanup (free, dirname);
179 extern char *index ();
180 char *name = dirname;
185 char *colon = index (name, ':');
186 char *space = index (name, ' ');
187 char *tab = index (name, '\t');
188 if (colon == 0 && space == 0 && tab == 0)
189 p = dirname = name + strlen (name);
193 if (colon != 0 && (p == 0 || colon < p))
195 if (space != 0 && (p == 0 || space < p))
197 if (tab != 0 && (p == 0 || tab < p))
200 while (*dirname == ':' || *dirname == ' ' || *dirname == '\t')
206 /* Sigh. "foo/" => "foo" */
214 /* "." => getwd (). */
215 name = current_directory;
218 else if (p[-2] == '/')
228 /* "...foo/." => "...foo". */
239 name = concat (current_directory, "/", name);
241 name = savestring (name, p - name);
242 make_cleanup (free, name);
244 if (stat (name, &st) < 0)
245 perror_with_name (name);
246 if ((st.st_mode & S_IFMT) != S_IFDIR)
247 error ("%s is not a directory.", name);
251 register unsigned int len = strlen (name);
256 if (!strncmp (p, name, len)
257 && (p[len] == '\0' || p[len] == ':'))
260 printf ("\"%s\" is already in the source path.\n", name);
271 source_path = concat (old, ":", name);
276 } while (*dirname != '\0');
282 /* Open a file named STRING, searching path PATH (dir names sep by colons)
283 using mode MODE and protection bits PROT in the calls to open.
284 If TRY_CWD_FIRST, try to open ./STRING before searching PATH.
285 (ie pretend the first element of PATH is ".")
286 If FILENAMED_OPENED is non-null, set it to a newly allocated string naming
287 the actual file opened (this string will always start with a "/"
289 If a file is found, return the descriptor.
290 Otherwise, return -1, with errno set for the last name we tried to open. */
292 /* >>>> This should only allow files of certain types,
293 >>>> eg executable, non-directory */
295 openp (path, try_cwd_first, string, mode, prot, filename_opened)
301 char **filename_opened;
304 register char *filename;
305 register char *p, *p1;
312 while (string[0] == '.' && string[1] == '/')
315 if (try_cwd_first || string[0] == '/')
318 fd = open (filename, mode, prot);
319 if (fd >= 0 || string[0] == '/')
323 filename = (char *) alloca (strlen (path) + strlen (string) + 2);
325 for (p = path; p; p = p1 ? p1 + 1 : 0)
327 p1 = (char *) index (p, ':');
333 strncpy (filename, p, len);
335 strcat (filename, "/");
336 strcat (filename, string);
338 fd = open (filename, mode, prot);
345 *filename_opened = (char *) 0;
346 else if (filename[0] == '/')
347 *filename_opened = savestring (filename, strlen (filename));
350 *filename_opened = concat (current_directory, "/", filename);
356 /* Create and initialize the table S->line_charpos that records
357 the positions of the lines in the source file, which is assumed
358 to be open on descriptor DESC.
359 All set S->nlines to the number of such lines. */
362 find_source_lines (s, desc)
367 register char *data, *p, *end;
369 int lines_allocated = 1000;
370 int *line_charpos = (int *) xmalloc (lines_allocated * sizeof (int));
371 extern int exec_mtime;
373 if (fstat (desc, &st) < 0)
374 perror_with_name (s->filename);
375 if (get_exec_file (0) != 0 && exec_mtime < st.st_mtime)
376 printf ("Source file is more recent than executable.\n");
378 data = (char *) alloca (st.st_size);
379 if (myread (desc, data, st.st_size) < 0)
380 perror_with_name (s->filename);
381 end = data + st.st_size;
388 /* A newline at the end does not start a new line. */
391 if (nlines == lines_allocated)
393 lines_allocated *= 2;
394 line_charpos = (int *) xrealloc (line_charpos,
395 sizeof (int) * lines_allocated);
397 line_charpos[nlines++] = p - data;
401 s->line_charpos = (int *) xrealloc (line_charpos, nlines * sizeof (int));
404 /* Return the character position of a line LINE in symtab S.
405 Return 0 if anything is invalid. */
408 source_line_charpos (s, line)
413 if (!s->line_charpos || line <= 0) return 0;
414 if (line > s->nlines)
416 return s->line_charpos[line - 1];
419 /* Return the line number of character position POS in symtab S. */
422 source_charpos_line (s, chr)
423 register struct symtab *s;
426 register int line = 0;
429 if (s == 0 || s->line_charpos == 0) return 0;
430 lnp = s->line_charpos;
431 /* Files are usually short, so sequential search is Ok */
432 while (line < s->nlines && *lnp <= chr)
437 if (line >= s->nlines)
442 /* Get full pathname and line number positions for a symtab.
443 Return nonzero if line numbers may have changed.
444 Set *FULLNAME to actual name of the file as found by `openp',
445 or to 0 if the file is not found. */
448 get_filename_and_charpos (s, line, fullname)
453 register int desc, linenums_changed = 0;
455 desc = openp (source_path, 0, s->filename, O_RDONLY, 0, &s->fullname);
463 *fullname = s->fullname;
464 if (s->line_charpos == 0) linenums_changed = 1;
465 if (linenums_changed) find_source_lines (s, desc);
467 return linenums_changed;
470 /* Print text describing the full name of the source file S
471 and the line number LINE and its corresponding character position.
472 The text starts with two Ctrl-z so that the Emacs-GDB interface
475 MID_STATEMENT is nonzero if the PC is not at the beginning of that line.
477 Return 1 if successful, 0 if could not find the file. */
480 identify_source_line (s, line, mid_statement)
485 if (s->line_charpos == 0)
486 get_filename_and_charpos (s, line, 0);
487 if (s->fullname == 0)
489 printf ("\032\032%s:%d:%d:%s:0x%x\n", s->fullname,
490 line, s->line_charpos[line - 1],
491 mid_statement ? "middle" : "beg",
492 get_frame_pc (get_current_frame()));
493 current_source_line = line;
494 first_line_listed = line;
495 last_line_listed = line;
496 current_source_symtab = s;
500 /* Print source lines from the file of symtab S,
501 starting with line number LINE and stopping before line number STOPLINE. */
504 print_source_lines (s, line, stopline, noerror)
511 register FILE *stream;
512 int nlines = stopline - line;
514 desc = openp (source_path, 0, s->filename, O_RDONLY, 0, &s->fullname);
519 perror_with_name (s->filename);
520 print_sys_errmsg (s->filename, errno);
524 if (s->line_charpos == 0)
525 find_source_lines (s, desc);
527 if (line < 1 || line > s->nlines)
530 error ("Line number %d out of range; %s has %d lines.",
531 line, s->filename, s->nlines);
534 if (lseek (desc, s->line_charpos[line - 1], 0) < 0)
537 perror_with_name (s->filename);
540 current_source_symtab = s;
541 current_source_line = line;
542 first_line_listed = line;
544 stream = fdopen (desc, "r");
551 last_line_listed = current_source_line;
552 printf_filtered ("%d\t", current_source_line++);
555 if (c < 040 && c != '\t' && c != '\n')
556 printf_filtered ("^%c", c + 0100);
558 printf_filtered ("^?");
560 printf_filtered ("%c", c);
561 } while (c != '\n' && (c = fgetc (stream)) >= 0);
571 Print a list of files and line numbers which a user may choose from
572 in order to list a function which was specified ambiguously
573 (as with `list classname::overloadedfuncname', for example).
574 The vector in SALS provides the filenames and line numbers.
577 ambiguous_line_spec (sals)
578 struct symtabs_and_lines *sals;
582 for (i = 0; i < sals->nelts; ++i)
583 printf("file: \"%s\", line number: %d\n",
584 sals->sals[i].symtab->filename, sals->sals[i].line);
589 list_command (arg, from_tty)
593 struct symtabs_and_lines sals, sals_end;
594 struct symtab_and_line sal, sal_end;
603 if (symtab_list == 0 && partial_symtab_list == 0)
604 error ("No symbol table is loaded. Use the \"symbol-file\" command.");
606 /* Pull in a current source symtab if necessary */
607 if (current_source_symtab == 0 &&
608 (arg == 0 || arg[0] == '+' || arg[0] == '-'))
609 select_source_symtab (0);
611 /* "l" or "l +" lists next ten lines. */
613 if (arg == 0 || !strcmp (arg, "+"))
615 if (current_source_symtab == 0)
616 error ("No default source file yet. Do \"help list\".");
617 print_source_lines (current_source_symtab, current_source_line,
618 current_source_line + 10, 0);
622 /* "l -" lists previous ten lines, the ones before the ten just listed. */
623 if (!strcmp (arg, "-"))
625 if (current_source_symtab == 0)
626 error ("No default source file yet. Do \"help list\".");
627 print_source_lines (current_source_symtab,
628 max (first_line_listed - 10, 1),
629 first_line_listed, 0);
633 /* Now if there is only one argument, decode it in SAL
635 If there are two arguments, decode them in SAL and SAL_END
636 and clear NO_END; however, if one of the arguments is blank,
637 set DUMMY_BEG or DUMMY_END to record that fact. */
644 sals = decode_line_1 (&arg1, 0, 0, 0);
646 if (! sals.nelts) return; /* C++ */
649 ambiguous_line_spec (&sals);
658 /* Record whether the BEG arg is all digits. */
660 for (p = arg; p != arg1 && *p >= '0' && *p <= '9'; p++);
661 linenum_beg = (p == arg1);
663 while (*arg1 == ' ' || *arg1 == '\t')
669 while (*arg1 == ' ' || *arg1 == '\t')
676 sals_end = decode_line_1 (&arg1, 0, 0, 0);
678 sals_end = decode_line_1 (&arg1, 0, sal.symtab, sal.line);
679 if (sals_end.nelts == 0)
681 if (sals_end.nelts > 1)
683 ambiguous_line_spec (&sals_end);
684 free (sals_end.sals);
687 sal_end = sals_end.sals[0];
688 free (sals_end.sals);
693 error ("Junk at end of line specification.");
695 if (!no_end && !dummy_beg && !dummy_end
696 && sal.symtab != sal_end.symtab)
697 error ("Specified start and end are in different files.");
698 if (dummy_beg && dummy_end)
699 error ("Two empty args do not say what lines to list.");
701 /* if line was specified by address,
702 first print exactly which line, and which file.
703 In this case, sal.symtab == 0 means address is outside
704 of all known source files, not that user failed to give a filename. */
708 error ("No source file for address 0x%x.", sal.pc);
709 sym = find_pc_function (sal.pc);
711 printf ("0x%x is in %s (%s, line %d).\n",
712 sal.pc, SYMBOL_NAME (sym), sal.symtab->filename, sal.line);
714 printf ("0x%x is in %s, line %d.\n",
715 sal.pc, sal.symtab->filename, sal.line);
718 /* If line was not specified by just a line number,
719 and it does not imply a symtab, it must be an undebuggable symbol
720 which means no source code. */
722 if (! linenum_beg && sal.symtab == 0)
723 error ("No line number known for %s.", arg);
725 /* If this command is repeated with RET,
726 turn it into the no-arg variant. */
731 if (dummy_beg && sal_end.symtab == 0)
732 error ("No default source file yet. Do \"help list\".");
734 print_source_lines (sal_end.symtab, max (sal_end.line - 9, 1),
735 sal_end.line + 1, 0);
736 else if (sal.symtab == 0)
737 error ("No default source file yet. Do \"help list\".");
739 print_source_lines (sal.symtab, max (sal.line - 5, 1), sal.line + 5, 0);
741 print_source_lines (sal.symtab, sal.line,
742 dummy_end ? sal.line + 10 : sal_end.line + 1,
746 /* Print info on range of pc's in a specified line. */
749 line_info (arg, from_tty)
753 struct symtabs_and_lines sals;
754 struct symtab_and_line sal;
755 int start_pc, end_pc;
760 sal.symtab = current_source_symtab;
761 sal.line = last_line_listed;
763 sals.sals = (struct symtab_and_line *)
764 xmalloc (sizeof (struct symtab_and_line));
769 sals = decode_line_spec_1 (arg, 0);
771 /* If this command is repeated with RET,
772 turn it into the no-arg variant. */
777 /* C++ More than one line may have been specified, as when the user
778 specifies an overloaded function name. Print info on them all. */
779 for (i = 0; i < sals.nelts; i++)
784 error ("No source file specified.");
787 && find_line_pc_range (sal.symtab, sal.line, &start_pc, &end_pc))
789 if (start_pc == end_pc)
790 printf ("Line %d of \"%s\" is at pc 0x%x but contains no code.\n",
791 sal.line, sal.symtab->filename, start_pc);
793 printf ("Line %d of \"%s\" starts at pc 0x%x and ends at 0x%x.\n",
794 sal.line, sal.symtab->filename, start_pc, end_pc);
795 /* x/i should display this line's code. */
796 set_next_address (start_pc);
797 /* Repeating "info line" should do the following line. */
798 last_line_listed = sal.line + 1;
801 printf ("Line number %d is out of range for \"%s\".\n",
802 sal.line, sal.symtab->filename);
806 /* Commands to search the source file for a regexp. */
809 forward_search_command (regex, from_tty)
814 register FILE *stream;
815 int line = last_line_listed + 1;
818 msg = (char *) re_comp (regex);
822 if (current_source_symtab == 0)
823 select_source_symtab (0);
825 /* Search from last_line_listed+1 in current_source_symtab */
827 desc = openp (source_path, 0, current_source_symtab->filename,
828 O_RDONLY, 0, ¤t_source_symtab->fullname);
830 perror_with_name (current_source_symtab->filename);
832 if (current_source_symtab->line_charpos == 0)
833 find_source_lines (current_source_symtab, desc);
835 if (line < 1 || line > current_source_symtab->nlines)
838 error ("Expression not found");
841 if (lseek (desc, current_source_symtab->line_charpos[line - 1], 0) < 0)
844 perror_with_name (current_source_symtab->filename);
847 stream = fdopen (desc, "r");
850 char buf[4096]; /* Should be reasonable??? */
851 register char *p = buf;
858 } while (c != '\n' && (c = fgetc (stream)) >= 0);
860 /* we now have a source line in buf, null terminate and match */
862 if (re_exec (buf) > 0)
866 print_source_lines (current_source_symtab,
868 current_source_line = max (line - 5, 1);
874 printf ("Expression not found\n");
879 reverse_search_command (regex, from_tty)
884 register FILE *stream;
885 int line = last_line_listed - 1;
888 msg = (char *) re_comp (regex);
892 if (current_source_symtab == 0)
893 select_source_symtab (0);
895 /* Search from last_line_listed-1 in current_source_symtab */
897 desc = openp (source_path, 0, current_source_symtab->filename,
898 O_RDONLY, 0, ¤t_source_symtab->fullname);
900 perror_with_name (current_source_symtab->filename);
902 if (current_source_symtab->line_charpos == 0)
903 find_source_lines (current_source_symtab, desc);
905 if (line < 1 || line > current_source_symtab->nlines)
908 error ("Expression not found");
911 if (lseek (desc, current_source_symtab->line_charpos[line - 1], 0) < 0)
914 perror_with_name (current_source_symtab->filename);
917 stream = fdopen (desc, "r");
921 char buf[4096]; /* Should be reasonable??? */
922 register char *p = buf;
929 } while (c != '\n' && (c = fgetc (stream)) >= 0);
931 /* We now have a source line in buf; null terminate and match. */
933 if (re_exec (buf) > 0)
937 print_source_lines (current_source_symtab,
939 current_source_line = max (line - 5, 1);
943 if (fseek (stream, current_source_symtab->line_charpos[line - 1], 0) < 0)
946 perror_with_name (current_source_symtab->filename);
950 printf ("Expression not found\n");
956 _initialize_source ()
958 current_source_symtab = 0;
961 add_com ("directory", class_files, directory_command,
962 "Add directory DIR to end of search path for source files.\n\
963 With no argument, reset the search path to just the working directory\n\
964 and forget cached info on line positions in source files.");
966 add_info ("directories", directories_info,
967 "Current search path for finding source files.");
969 add_info ("line", line_info,
970 "Core addresses of the code for a source line.\n\
971 Line can be specified as\n\
972 LINENUM, to list around that line in current file,\n\
973 FILE:LINENUM, to list around that line in that file,\n\
974 FUNCTION, to list around beginning of that function,\n\
975 FILE:FUNCTION, to distinguish among like-named static functions.\n\
976 Default is to describe the last source line that was listed.\n\n\
977 This sets the default address for \"x\" to the line's first instruction\n\
978 so that \"x/i\" suffices to start examining the machine code.\n\
979 The address is also stored as the value of \"$_\".");
981 add_com ("forward-search", class_files, forward_search_command,
982 "Search for regular expression (see regex(3)) from last line listed.");
983 add_com_alias ("search", "forward-search", class_files, 0);
985 add_com ("reverse-search", class_files, reverse_search_command,
986 "Search backward for regular expression (see regex(3)) from last line listed.");
988 add_com ("list", class_files, list_command,
989 "List specified function or line.\n\
990 With no argument, lists ten more lines after or around previous listing.\n\
991 \"list -\" lists the ten lines before a previous ten-line listing.\n\
992 One argument specifies a line, and ten lines are listed around that line.\n\
993 Two arguments with comma between specify starting and ending lines to list.\n\
994 Lines can be specified in these ways:\n\
995 LINENUM, to list around that line in current file,\n\
996 FILE:LINENUM, to list around that line in that file,\n\
997 FUNCTION, to list around beginning of that function,\n\
998 FILE:FUNCTION, to distinguish among like-named static functions.\n\
999 *ADDRESS, to list around the line containing that address.\n\
1000 With two args if one is empty it stands for ten lines away from the other arg.");