]>
git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/source.c
3869e1a169a96249f5844e22e9ed74f99542c51e
1 /* List lines of source files for GDB, the GNU debugger.
2 Copyright (C) 1986, 1987, 1988, 1989, 1991 Free Software Foundation, Inc.
4 This file is part of GDB.
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.
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.
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. */
22 #include "expression.h"
29 #include <sys/types.h>
33 #include <sys/param.h>
41 /* Prototypes for local functions. */
44 open_source_file
PARAMS ((struct symtab
*));
47 get_filename_and_charpos
PARAMS ((struct symtab
*, char **));
50 reverse_search_command
PARAMS ((char *, int));
53 forward_search_command
PARAMS ((char *, int));
56 line_info
PARAMS ((char *, int));
59 list_command
PARAMS ((char *, int));
62 ambiguous_line_spec
PARAMS ((struct symtabs_and_lines
*));
65 source_info
PARAMS ((char *, int));
68 show_directories
PARAMS ((char *, int));
71 find_source_lines
PARAMS ((struct symtab
*, int));
73 /* If we use this declaration, it breaks because of fucking ANSI "const" stuff
74 on some systems. We just have to not declare it at all, have it default
75 to int, and possibly botch on a few systems. Thanks, ANSIholes... */
76 /* extern char *strstr(); */
78 /* Path of directories to search for source files.
79 Same format as the PATH environment variable's value. */
83 /* Symtab of default file for listing lines of. */
85 struct symtab
*current_source_symtab
;
87 /* Default next line to list. */
89 int current_source_line
;
91 /* Default number of lines to print with commands like "list".
92 This is based on guessing how many long (i.e. more than chars_per_line
93 characters) lines there will be. To be completely correct, "list"
94 and friends should be rewritten to count characters and see where
95 things are wrapping, but that would be a fair amount of work. */
97 int lines_to_list
= 10;
99 /* Line number of last line printed. Default for various commands.
100 current_source_line is usually, but not always, the same as this. */
102 static int last_line_listed
;
104 /* First line number listed by last listing command. */
106 static int first_line_listed
;
109 /* Set the source file default for the "list" command, specifying a
110 symtab. Sigh. Behavior specification: If it is called with a
111 non-zero argument, that is the symtab to select. If it is not,
112 first lookup "main"; if it exists, use the symtab and line it
113 defines. If not, take the last symtab in the symtab lists (if it
114 exists) or the last symtab in the psymtab lists (if *it* exists). If
115 none of this works, report an error. */
118 select_source_symtab (s
)
119 register struct symtab
*s
;
121 struct symtabs_and_lines sals
;
122 struct symtab_and_line sal
;
123 struct partial_symtab
*ps
;
124 struct partial_symtab
*cs_pst
= 0;
129 current_source_symtab
= s
;
130 current_source_line
= 1;
134 /* Make the default place to list be the function `main'
136 if (lookup_symbol ("main", 0, VAR_NAMESPACE
, 0, NULL
))
138 sals
= decode_line_spec ("main", 1);
141 current_source_symtab
= sal
.symtab
;
142 current_source_line
= max (sal
.line
- (lines_to_list
- 1), 1);
143 if (current_source_symtab
)
147 /* All right; find the last file in the symtab list (ignoring .h's). */
149 current_source_line
= 1;
151 for (ofp
= object_files
; ofp
!= NULL
; ofp
= ofp
-> next
)
153 for (s
= ofp
-> symtabs
; s
; s
= s
->next
)
155 char *name
= s
-> filename
;
156 int len
= strlen (name
);
157 if (! (len
> 2 && (strcmp (&name
[len
- 2], ".h") == 0)))
159 current_source_symtab
= s
;
163 if (current_source_symtab
)
166 /* Howabout the partial symbol tables? */
168 for (ofp
= object_files
; ofp
!= NULL
; ofp
= ofp
-> next
)
170 for (ps
= ofp
-> psymtabs
; ps
!= NULL
; ps
= ps
-> next
)
172 char *name
= ps
-> filename
;
173 int len
= strlen (name
);
174 if (! (len
> 2 && (strcmp (&name
[len
- 2], ".h") == 0)))
182 if (cs_pst
-> readin
)
184 fatal ("Internal: select_source_symtab: readin pst found and no symtabs.");
188 current_source_symtab
= PSYMTAB_TO_SYMTAB (cs_pst
);
192 if (current_source_symtab
)
195 error ("Can't find a default source file");
199 show_directories (ignore
, from_tty
)
203 puts_filtered ("Source directories searched: ");
204 puts_filtered (source_path
);
205 puts_filtered ("\n");
208 /* Forget what we learned about line positions in source files,
209 and which directories contain them;
210 must check again now since files may be found in
211 a different directory now. */
214 forget_cached_source_info ()
216 register struct symtab
*s
;
217 register struct objfile
*objfile
;
219 for (objfile
= object_files
; objfile
!= NULL
; objfile
= objfile
-> next
)
221 for (s
= objfile
-> symtabs
; s
!= NULL
; s
= s
-> next
)
223 if (s
-> line_charpos
!= NULL
)
225 mfree (objfile
-> md
, s
-> line_charpos
);
226 s
-> line_charpos
= NULL
;
228 if (s
-> fullname
!= NULL
)
230 mfree (objfile
-> md
, s
-> fullname
);
231 s
-> fullname
= NULL
;
240 source_path
= savestring ("$cdir:$cwd", /* strlen of it */ 10);
241 forget_cached_source_info ();
244 /* Add zero or more directories to the front of the source path. */
247 directory_command (dirname
, from_tty
)
252 /* FIXME, this goes to "delete dir"... */
255 if (query ("Reinitialize source path to empty? ", ""))
262 mod_path (dirname
, &source_path
);
264 show_directories ((char *)0, from_tty
);
265 forget_cached_source_info ();
268 /* Add zero or more directories to the front of an arbitrary path. */
271 mod_path (dirname
, which_path
)
275 char *old
= *which_path
;
281 dirname
= strsave (dirname
);
282 make_cleanup (free
, dirname
);
286 char *name
= dirname
;
291 char *colon
= strchr (name
, ':');
292 char *space
= strchr (name
, ' ');
293 char *tab
= strchr (name
, '\t');
294 if (colon
== 0 && space
== 0 && tab
== 0)
295 p
= dirname
= name
+ strlen (name
);
299 if (colon
!= 0 && (p
== 0 || colon
< p
))
301 if (space
!= 0 && (p
== 0 || space
< p
))
303 if (tab
!= 0 && (p
== 0 || tab
< p
))
306 while (*dirname
== ':' || *dirname
== ' ' || *dirname
== '\t')
312 /* Sigh. "foo/" => "foo" */
320 /* "." => getwd (). */
321 name
= current_directory
;
324 else if (p
[-2] == '/')
334 /* "...foo/." => "...foo". */
345 name
= tilde_expand (name
);
346 else if (name
[0] != '/' && name
[0] != '$')
347 name
= concat (current_directory
, "/", name
, NULL
);
349 name
= savestring (name
, p
- name
);
350 make_cleanup (free
, name
);
352 /* Unless it's a variable, check existence. */
353 if (name
[0] != '$') {
354 if (stat (name
, &st
) < 0)
355 perror_with_name (name
);
356 if ((st
.st_mode
& S_IFMT
) != S_IFDIR
)
357 error ("%s is not a directory.", name
);
362 register unsigned int len
= strlen (name
);
367 if (!strncmp (p
, name
, len
)
368 && (p
[len
] == '\0' || p
[len
] == ':'))
370 /* Found it in the search path, remove old copy */
372 p
--; /* Back over leading colon */
373 if (prefix
> p
- *which_path
)
374 goto skip_dup
; /* Same dir twice in one cmd */
375 strcpy (p
, &p
[len
+1]); /* Copy from next \0 or : */
385 /* If we have already tacked on a name(s) in this command, be sure they stay on the front as we tack on some more. */
392 temp
= concat (old
, ":", name
, NULL
);
394 *which_path
= concat (temp
, "", &old
[prefix
], NULL
);
395 prefix
= strlen (temp
);
400 *which_path
= concat (name
, (old
[0]? ":" : old
), old
, NULL
);
401 prefix
= strlen (name
);
408 } while (*dirname
!= '\0');
413 source_info (ignore
, from_tty
)
417 register struct symtab
*s
= current_source_symtab
;
421 printf_filtered("No current source file.\n");
424 printf_filtered ("Current source file is %s\n", s
->filename
);
426 printf_filtered ("Compilation directory is %s\n", s
->dirname
);
428 printf_filtered ("Located in %s\n", s
->fullname
);
430 printf_filtered ("Contains %d lines\n", s
->nlines
);
432 printf_filtered("Source language %s.\n", language_str (s
->language
));
437 /* Open a file named STRING, searching path PATH (dir names sep by colons)
438 using mode MODE and protection bits PROT in the calls to open.
439 If TRY_CWD_FIRST, try to open ./STRING before searching PATH.
440 (ie pretend the first element of PATH is ".")
441 If FILENAMED_OPENED is non-null, set it to a newly allocated string naming
442 the actual file opened (this string will always start with a "/". We
443 have to take special pains to avoid doubling the "/" between the directory
444 and the file, sigh! Emacs gets confuzzed by this when we print the
447 If a file is found, return the descriptor.
448 Otherwise, return -1, with errno set for the last name we tried to open. */
450 /* >>>> This should only allow files of certain types,
451 >>>> eg executable, non-directory */
453 openp (path
, try_cwd_first
, string
, mode
, prot
, filename_opened
)
459 char **filename_opened
;
462 register char *filename
;
463 register char *p
, *p1
;
471 while (string
[0] == '.' && string
[1] == '/')
474 if (try_cwd_first
|| string
[0] == '/')
477 fd
= open (filename
, mode
, prot
);
478 if (fd
>= 0 || string
[0] == '/')
482 alloclen
= strlen (path
) + strlen (string
) + 2;
483 filename
= (char *) alloca (alloclen
);
485 for (p
= path
; p
; p
= p1
? p1
+ 1 : 0)
487 p1
= (char *) strchr (p
, ':');
493 if (len
== 4 && p
[0] == '$' && p
[1] == 'c'
494 && p
[2] == 'w' && p
[3] == 'd') {
495 /* Name is $cwd -- insert current directory name instead. */
498 /* First, realloc the filename buffer if too short. */
499 len
= strlen (current_directory
);
500 newlen
= len
+ strlen (string
) + 2;
501 if (newlen
> alloclen
) {
503 filename
= (char *) alloca (alloclen
);
505 strcpy (filename
, current_directory
);
507 /* Normal file name in path -- just use it. */
508 strncpy (filename
, p
, len
);
512 /* Remove trailing slashes */
513 while (len
> 0 && filename
[len
-1] == '/')
516 strcat (filename
+len
, "/");
517 strcat (filename
, string
);
519 fd
= open (filename
, mode
, prot
);
526 *filename_opened
= (char *) 0;
527 else if (filename
[0] == '/')
528 *filename_opened
= savestring (filename
, strlen (filename
));
531 /* Beware the // my son, the Emacs barfs, the botch that catch... */
533 *filename_opened
= concat (current_directory
,
534 '/' == current_directory
[strlen(current_directory
)-1]? "": "/",
541 /* Open a source file given a symtab S. Returns a file descriptor
542 or negative number for error. */
548 char *path
= source_path
;
553 /* Quick way out if we already know its full name */
556 result
= open (s
->fullname
, O_RDONLY
);
559 /* Didn't work -- free old one, try again. */
560 mfree (s
->objfile
->md
, s
->fullname
);
564 if (s
->dirname
!= NULL
)
566 /* Replace a path entry of $cdir with the compilation directory name */
568 /* We cast strstr's result in case an ANSIhole has made it const,
569 which produces a "required warning" when assigned to a nonconst. */
570 p
= (char *)strstr (source_path
, "$cdir");
571 if (p
&& (p
== path
|| p
[-1] == ':')
572 && (p
[cdir_len
] == ':' || p
[cdir_len
] == '\0')) {
576 alloca (strlen (source_path
) + 1 + strlen (s
->dirname
) + 1);
577 len
= p
- source_path
;
578 strncpy (path
, source_path
, len
); /* Before $cdir */
579 strcpy (path
+ len
, s
->dirname
); /* new stuff */
580 strcat (path
+ len
, source_path
+ len
+ cdir_len
); /* After $cdir */
584 result
= openp (path
, 0, s
->filename
, O_RDONLY
, 0, &s
->fullname
);
587 /* Didn't work. Try using just the basename. */
588 p
= basename (s
->filename
);
589 if (p
!= s
->filename
)
590 result
= openp(path
, 0, p
, O_RDONLY
,0, &s
->fullname
);
594 fullname
= s
-> fullname
;
595 s
-> fullname
= mstrsave (s
-> objfile
-> md
, s
-> fullname
);
602 /* Create and initialize the table S->line_charpos that records
603 the positions of the lines in the source file, which is assumed
604 to be open on descriptor DESC.
605 All set S->nlines to the number of such lines. */
608 find_source_lines (s
, desc
)
614 register char *data
, *p
, *end
;
616 int lines_allocated
= 1000;
621 line_charpos
= (int *) xmmalloc (s
-> objfile
-> md
,
622 lines_allocated
* sizeof (int));
623 if (fstat (desc
, &st
) < 0)
624 perror_with_name (s
->filename
);
627 exec_mtime
= bfd_get_mtime(exec_bfd
);
628 if (exec_mtime
&& exec_mtime
< st
.st_mtime
)
629 printf_filtered ("Source file is more recent than executable.\n");
632 #ifdef LSEEK_NOT_LINEAR
633 /* Have to read it byte by byte to find out where the chars live */
635 line_charpos
[0] = tell(desc
);
637 while (myread(desc
, &c
, 1)>0)
641 if (nlines
== lines_allocated
)
643 lines_allocated
*= 2;
645 (int *) xmrealloc (s
-> objfile
-> md
, (char *) line_charpos
,
646 sizeof (int) * lines_allocated
);
648 line_charpos
[nlines
++] = tell(desc
);
653 /* st_size might be a large type, but we only support source files whose
654 size fits in an int. FIXME. */
655 size
= (int) st
.st_size
;
657 #ifdef BROKEN_LARGE_ALLOCA
658 data
= (char *) xmalloc (size
);
659 make_cleanup (free
, data
);
661 data
= (char *) alloca (size
);
663 if (myread (desc
, data
, size
) < 0)
664 perror_with_name (s
->filename
);
672 /* A newline at the end does not start a new line. */
675 if (nlines
== lines_allocated
)
677 lines_allocated
*= 2;
679 (int *) xmrealloc (s
-> objfile
-> md
, (char *) line_charpos
,
680 sizeof (int) * lines_allocated
);
682 line_charpos
[nlines
++] = p
- data
;
688 (int *) xmrealloc (s
-> objfile
-> md
, (char *) line_charpos
,
689 nlines
* sizeof (int));
693 /* Return the character position of a line LINE in symtab S.
694 Return 0 if anything is invalid. */
696 #if 0 /* Currently unused */
699 source_line_charpos (s
, line
)
704 if (!s
->line_charpos
|| line
<= 0) return 0;
705 if (line
> s
->nlines
)
707 return s
->line_charpos
[line
- 1];
710 /* Return the line number of character position POS in symtab S. */
713 source_charpos_line (s
, chr
)
714 register struct symtab
*s
;
717 register int line
= 0;
720 if (s
== 0 || s
->line_charpos
== 0) return 0;
721 lnp
= s
->line_charpos
;
722 /* Files are usually short, so sequential search is Ok */
723 while (line
< s
->nlines
&& *lnp
<= chr
)
728 if (line
>= s
->nlines
)
736 /* Get full pathname and line number positions for a symtab.
737 Return nonzero if line numbers may have changed.
738 Set *FULLNAME to actual name of the file as found by `openp',
739 or to 0 if the file is not found. */
742 get_filename_and_charpos (s
, fullname
)
746 register int desc
, linenums_changed
= 0;
748 desc
= open_source_file (s
);
756 *fullname
= s
->fullname
;
757 if (s
->line_charpos
== 0) linenums_changed
= 1;
758 if (linenums_changed
) find_source_lines (s
, desc
);
760 return linenums_changed
;
763 /* Print text describing the full name of the source file S
764 and the line number LINE and its corresponding character position.
765 The text starts with two Ctrl-z so that the Emacs-GDB interface
768 MID_STATEMENT is nonzero if the PC is not at the beginning of that line.
770 Return 1 if successful, 0 if could not find the file. */
773 identify_source_line (s
, line
, mid_statement
)
778 if (s
->line_charpos
== 0)
779 get_filename_and_charpos (s
, (char **)NULL
);
780 if (s
->fullname
== 0)
782 printf ("\032\032%s:%d:%d:%s:0x%x\n", s
->fullname
,
783 line
, s
->line_charpos
[line
- 1],
784 mid_statement
? "middle" : "beg",
785 get_frame_pc (get_current_frame()));
786 current_source_line
= line
;
787 first_line_listed
= line
;
788 last_line_listed
= line
;
789 current_source_symtab
= s
;
793 /* Print source lines from the file of symtab S,
794 starting with line number LINE and stopping before line number STOPLINE. */
797 print_source_lines (s
, line
, stopline
, noerror
)
804 register FILE *stream
;
805 int nlines
= stopline
- line
;
807 /* Regardless of whether we can open the file, set current_source_symtab. */
808 current_source_symtab
= s
;
809 current_source_line
= line
;
810 first_line_listed
= line
;
812 desc
= open_source_file (s
);
816 char *name
= alloca (strlen (s
->filename
) + 100);
817 sprintf (name
, "%s:%d", s
->filename
, line
);
818 print_sys_errmsg (name
, errno
);
823 if (s
->line_charpos
== 0)
824 find_source_lines (s
, desc
);
826 if (line
< 1 || line
> s
->nlines
)
829 error ("Line number %d out of range; %s has %d lines.",
830 line
, s
->filename
, s
->nlines
);
833 if (lseek (desc
, s
->line_charpos
[line
- 1], 0) < 0)
836 perror_with_name (s
->filename
);
839 stream
= fdopen (desc
, "r");
846 last_line_listed
= current_source_line
;
847 printf_filtered ("%d\t", current_source_line
++);
850 if (c
< 040 && c
!= '\t' && c
!= '\n' && c
!= '\r')
851 printf_filtered ("^%c", c
+ 0100);
853 printf_filtered ("^?");
855 printf_filtered ("%c", c
);
856 } while (c
!= '\n' && (c
= fgetc (stream
)) >= 0);
866 Print a list of files and line numbers which a user may choose from
867 in order to list a function which was specified ambiguously
868 (as with `list classname::overloadedfuncname', for example).
869 The vector in SALS provides the filenames and line numbers.
872 ambiguous_line_spec (sals
)
873 struct symtabs_and_lines
*sals
;
877 for (i
= 0; i
< sals
->nelts
; ++i
)
878 printf_filtered("file: \"%s\", line number: %d\n",
879 sals
->sals
[i
].symtab
->filename
, sals
->sals
[i
].line
);
884 list_command (arg
, from_tty
)
888 struct symtabs_and_lines sals
, sals_end
;
889 struct symtab_and_line sal
, sal_end
;
898 if (!have_full_symbols () && !have_partial_symbols())
899 error ("No symbol table is loaded. Use the \"file\" command.");
901 /* Pull in a current source symtab if necessary */
902 if (current_source_symtab
== 0 &&
903 (arg
== 0 || arg
[0] == '+' || arg
[0] == '-'))
904 select_source_symtab (0);
906 /* "l" or "l +" lists next ten lines. */
908 if (arg
== 0 || !strcmp (arg
, "+"))
910 if (current_source_symtab
== 0)
911 error ("No default source file yet. Do \"help list\".");
912 print_source_lines (current_source_symtab
, current_source_line
,
913 current_source_line
+ lines_to_list
, 0);
917 /* "l -" lists previous ten lines, the ones before the ten just listed. */
918 if (!strcmp (arg
, "-"))
920 if (current_source_symtab
== 0)
921 error ("No default source file yet. Do \"help list\".");
922 print_source_lines (current_source_symtab
,
923 max (first_line_listed
- lines_to_list
, 1),
924 first_line_listed
, 0);
928 /* Now if there is only one argument, decode it in SAL
930 If there are two arguments, decode them in SAL and SAL_END
931 and clear NO_END; however, if one of the arguments is blank,
932 set DUMMY_BEG or DUMMY_END to record that fact. */
939 sals
= decode_line_1 (&arg1
, 0, 0, 0);
941 if (! sals
.nelts
) return; /* C++ */
944 ambiguous_line_spec (&sals
);
953 /* Record whether the BEG arg is all digits. */
955 for (p
= arg
; p
!= arg1
&& *p
>= '0' && *p
<= '9'; p
++);
956 linenum_beg
= (p
== arg1
);
958 while (*arg1
== ' ' || *arg1
== '\t')
964 while (*arg1
== ' ' || *arg1
== '\t')
971 sals_end
= decode_line_1 (&arg1
, 0, 0, 0);
973 sals_end
= decode_line_1 (&arg1
, 0, sal
.symtab
, sal
.line
);
974 if (sals_end
.nelts
== 0)
976 if (sals_end
.nelts
> 1)
978 ambiguous_line_spec (&sals_end
);
979 free (sals_end
.sals
);
982 sal_end
= sals_end
.sals
[0];
983 free (sals_end
.sals
);
988 error ("Junk at end of line specification.");
990 if (!no_end
&& !dummy_beg
&& !dummy_end
991 && sal
.symtab
!= sal_end
.symtab
)
992 error ("Specified start and end are in different files.");
993 if (dummy_beg
&& dummy_end
)
994 error ("Two empty args do not say what lines to list.");
996 /* if line was specified by address,
997 first print exactly which line, and which file.
998 In this case, sal.symtab == 0 means address is outside
999 of all known source files, not that user failed to give a filename. */
1002 if (sal
.symtab
== 0)
1003 error ("No source file for address %s.", local_hex_string(sal
.pc
));
1004 sym
= find_pc_function (sal
.pc
);
1007 printf_filtered ("%s is in ", local_hex_string(sal
.pc
));
1008 fprint_symbol (stdout
, SYMBOL_NAME (sym
));
1009 printf_filtered (" (%s:%d).\n", sal
.symtab
->filename
, sal
.line
);
1012 printf_filtered ("%s is at %s:%d.\n",
1013 local_hex_string(sal
.pc
),
1014 sal
.symtab
->filename
, sal
.line
);
1017 /* If line was not specified by just a line number,
1018 and it does not imply a symtab, it must be an undebuggable symbol
1019 which means no source code. */
1021 if (! linenum_beg
&& sal
.symtab
== 0)
1022 error ("No line number known for %s.", arg
);
1024 /* If this command is repeated with RET,
1025 turn it into the no-arg variant. */
1030 if (dummy_beg
&& sal_end
.symtab
== 0)
1031 error ("No default source file yet. Do \"help list\".");
1033 print_source_lines (sal_end
.symtab
,
1034 max (sal_end
.line
- (lines_to_list
- 1), 1),
1035 sal_end
.line
+ 1, 0);
1036 else if (sal
.symtab
== 0)
1037 error ("No default source file yet. Do \"help list\".");
1039 print_source_lines (sal
.symtab
,
1040 max (sal
.line
- (lines_to_list
/ 2), 1),
1041 sal
.line
+ (lines_to_list
/ 2), 0);
1043 print_source_lines (sal
.symtab
, sal
.line
,
1045 ? sal
.line
+ lines_to_list
1046 : sal_end
.line
+ 1),
1050 /* Print info on range of pc's in a specified line. */
1053 line_info (arg
, from_tty
)
1057 struct symtabs_and_lines sals
;
1058 struct symtab_and_line sal
;
1059 CORE_ADDR start_pc
, end_pc
;
1064 sal
.symtab
= current_source_symtab
;
1065 sal
.line
= last_line_listed
;
1067 sals
.sals
= (struct symtab_and_line
*)
1068 xmalloc (sizeof (struct symtab_and_line
));
1073 sals
= decode_line_spec_1 (arg
, 0);
1075 /* If this command is repeated with RET,
1076 turn it into the no-arg variant. */
1081 /* C++ More than one line may have been specified, as when the user
1082 specifies an overloaded function name. Print info on them all. */
1083 for (i
= 0; i
< sals
.nelts
; i
++)
1087 if (sal
.symtab
== 0)
1088 error ("No source file specified.");
1091 && find_line_pc_range (sal
.symtab
, sal
.line
, &start_pc
, &end_pc
))
1093 if (start_pc
== end_pc
)
1094 printf_filtered ("Line %d of \"%s\" is at pc %s but contains no code.\n",
1095 sal
.line
, sal
.symtab
->filename
, local_hex_string(start_pc
));
1098 printf_filtered ("Line %d of \"%s\" starts at pc %s",
1099 sal
.line
, sal
.symtab
->filename
,
1100 local_hex_string(start_pc
));
1101 printf_filtered (" and ends at %s.\n",
1102 local_hex_string(end_pc
));
1104 /* x/i should display this line's code. */
1105 set_next_address (start_pc
);
1106 /* Repeating "info line" should do the following line. */
1107 last_line_listed
= sal
.line
+ 1;
1110 printf_filtered ("Line number %d is out of range for \"%s\".\n",
1111 sal
.line
, sal
.symtab
->filename
);
1115 /* Commands to search the source file for a regexp. */
1119 forward_search_command (regex
, from_tty
)
1125 register FILE *stream
;
1126 int line
= last_line_listed
+ 1;
1129 msg
= (char *) re_comp (regex
);
1133 if (current_source_symtab
== 0)
1134 select_source_symtab (0);
1136 /* Search from last_line_listed+1 in current_source_symtab */
1138 desc
= open_source_file (current_source_symtab
);
1140 perror_with_name (current_source_symtab
->filename
);
1142 if (current_source_symtab
->line_charpos
== 0)
1143 find_source_lines (current_source_symtab
, desc
);
1145 if (line
< 1 || line
> current_source_symtab
->nlines
)
1148 error ("Expression not found");
1151 if (lseek (desc
, current_source_symtab
->line_charpos
[line
- 1], 0) < 0)
1154 perror_with_name (current_source_symtab
->filename
);
1157 stream
= fdopen (desc
, "r");
1160 /* FIXME!!! We walk right off the end of buf if we get a long line!!! */
1161 char buf
[4096]; /* Should be reasonable??? */
1162 register char *p
= buf
;
1169 } while (c
!= '\n' && (c
= getc (stream
)) >= 0);
1171 /* we now have a source line in buf, null terminate and match */
1173 if (re_exec (buf
) > 0)
1177 print_source_lines (current_source_symtab
,
1179 current_source_line
= max (line
- lines_to_list
/ 2, 1);
1185 printf_filtered ("Expression not found\n");
1191 reverse_search_command (regex
, from_tty
)
1197 register FILE *stream
;
1198 int line
= last_line_listed
- 1;
1201 msg
= (char *) re_comp (regex
);
1205 if (current_source_symtab
== 0)
1206 select_source_symtab (0);
1208 /* Search from last_line_listed-1 in current_source_symtab */
1210 desc
= open_source_file (current_source_symtab
);
1212 perror_with_name (current_source_symtab
->filename
);
1214 if (current_source_symtab
->line_charpos
== 0)
1215 find_source_lines (current_source_symtab
, desc
);
1217 if (line
< 1 || line
> current_source_symtab
->nlines
)
1220 error ("Expression not found");
1223 if (lseek (desc
, current_source_symtab
->line_charpos
[line
- 1], 0) < 0)
1226 perror_with_name (current_source_symtab
->filename
);
1229 stream
= fdopen (desc
, "r");
1233 /* FIXME!!! We walk right off the end of buf if we get a long line!!! */
1234 char buf
[4096]; /* Should be reasonable??? */
1235 register char *p
= buf
;
1242 } while (c
!= '\n' && (c
= getc (stream
)) >= 0);
1244 /* We now have a source line in buf; null terminate and match. */
1246 if (re_exec (buf
) > 0)
1250 print_source_lines (current_source_symtab
,
1252 current_source_line
= max (line
- lines_to_list
/ 2, 1);
1256 if (fseek (stream
, current_source_symtab
->line_charpos
[line
- 1], 0) < 0)
1259 perror_with_name (current_source_symtab
->filename
);
1263 printf_filtered ("Expression not found\n");
1269 _initialize_source ()
1271 current_source_symtab
= 0;
1272 init_source_path ();
1274 add_com ("directory", class_files
, directory_command
,
1275 "Add directory DIR to beginning of search path for source files.\n\
1276 Forget cached info on source file locations and line positions.\n\
1277 DIR can also be $cwd for the current working directory, or $cdir for the\n\
1278 directory in which the source file was compiled into object code.\n\
1279 With no argument, reset the search path to $cdir:$cwd, the default.");
1281 add_cmd ("directories", no_class
, show_directories
,
1282 "Current search path for finding source files.\n\
1283 $cwd in the path means the current working directory.\n\
1284 $cdir in the path means the compilation directory of the source file.",
1287 add_info ("source", source_info
,
1288 "Information about the current source file.");
1290 add_info ("line", line_info
,
1291 "Core addresses of the code for a source line.\n\
1292 Line can be specified as\n\
1293 LINENUM, to list around that line in current file,\n\
1294 FILE:LINENUM, to list around that line in that file,\n\
1295 FUNCTION, to list around beginning of that function,\n\
1296 FILE:FUNCTION, to distinguish among like-named static functions.\n\
1297 Default is to describe the last source line that was listed.\n\n\
1298 This sets the default address for \"x\" to the line's first instruction\n\
1299 so that \"x/i\" suffices to start examining the machine code.\n\
1300 The address is also stored as the value of \"$_\".");
1302 add_com ("forward-search", class_files
, forward_search_command
,
1303 "Search for regular expression (see regex(3)) from last line listed.");
1304 add_com_alias ("search", "forward-search", class_files
, 0);
1306 add_com ("reverse-search", class_files
, reverse_search_command
,
1307 "Search backward for regular expression (see regex(3)) from last line listed.");
1309 add_com ("list", class_files
, list_command
,
1310 "List specified function or line.\n\
1311 With no argument, lists ten more lines after or around previous listing.\n\
1312 \"list -\" lists the ten lines before a previous ten-line listing.\n\
1313 One argument specifies a line, and ten lines are listed around that line.\n\
1314 Two arguments with comma between specify starting and ending lines to list.\n\
1315 Lines can be specified in these ways:\n\
1316 LINENUM, to list around that line in current file,\n\
1317 FILE:LINENUM, to list around that line in that file,\n\
1318 FUNCTION, to list around beginning of that function,\n\
1319 FILE:FUNCTION, to distinguish among like-named static functions.\n\
1320 *ADDRESS, to list around the line containing that address.\n\
1321 With two args if one is empty it stands for ten lines away from the other arg.");
1322 add_com_alias ("l", "list", class_files
, 1);
1325 (add_set_cmd ("listsize", class_support
, var_uinteger
,
1326 (char *)&lines_to_list
,
1327 "Set number of source lines gdb will list by default.",