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"
28 #include <sys/types.h>
30 #include <sys/param.h>
38 #ifndef DIRNAME_SEPARATOR
39 #define DIRNAME_SEPARATOR ':'
42 /* Prototypes for local functions. */
45 open_source_file
PARAMS ((struct symtab
*));
48 get_filename_and_charpos
PARAMS ((struct symtab
*, char **));
51 reverse_search_command
PARAMS ((char *, int));
54 forward_search_command
PARAMS ((char *, int));
57 line_info
PARAMS ((char *, int));
60 list_command
PARAMS ((char *, int));
63 ambiguous_line_spec
PARAMS ((struct symtabs_and_lines
*));
66 source_info
PARAMS ((char *, int));
69 show_directories
PARAMS ((char *, int));
72 find_source_lines
PARAMS ((struct symtab
*, int));
74 /* If we use this declaration, it breaks because of fucking ANSI "const" stuff
75 on some systems. We just have to not declare it at all, have it default
76 to int, and possibly botch on a few systems. Thanks, ANSIholes... */
77 /* extern char *strstr(); */
79 /* Path of directories to search for source files.
80 Same format as the PATH environment variable's value. */
84 /* Symtab of default file for listing lines of. */
86 struct symtab
*current_source_symtab
;
88 /* Default next line to list. */
90 int current_source_line
;
92 /* Default number of lines to print with commands like "list".
93 This is based on guessing how many long (i.e. more than chars_per_line
94 characters) lines there will be. To be completely correct, "list"
95 and friends should be rewritten to count characters and see where
96 things are wrapping, but that would be a fair amount of work. */
98 int lines_to_list
= 10;
100 /* Line number of last line printed. Default for various commands.
101 current_source_line is usually, but not always, the same as this. */
103 static int last_line_listed
;
105 /* First line number listed by last listing command. */
107 static int first_line_listed
;
110 /* Set the source file default for the "list" command to be S.
112 If S is NULL, and we don't have a default, find one. This
113 should only be called when the user actually tries to use the
114 default, since we produce an error if we can't find a reasonable
115 default. Also, since this can cause symbols to be read, doing it
116 before we need to would make things slower than necessary. */
119 select_source_symtab (s
)
120 register struct symtab
*s
;
122 struct symtabs_and_lines sals
;
123 struct symtab_and_line sal
;
124 struct partial_symtab
*ps
;
125 struct partial_symtab
*cs_pst
= 0;
130 current_source_symtab
= s
;
131 current_source_line
= 1;
135 if (current_source_symtab
)
138 /* Make the default place to list be the function `main'
140 if (lookup_symbol ("main", 0, VAR_NAMESPACE
, 0, NULL
))
142 sals
= decode_line_spec ("main", 1);
145 current_source_symtab
= sal
.symtab
;
146 current_source_line
= max (sal
.line
- (lines_to_list
- 1), 1);
147 if (current_source_symtab
)
151 /* All right; find the last file in the symtab list (ignoring .h's). */
153 current_source_line
= 1;
155 for (ofp
= object_files
; ofp
!= NULL
; ofp
= ofp
-> next
)
157 for (s
= ofp
-> symtabs
; s
; s
= s
->next
)
159 char *name
= s
-> filename
;
160 int len
= strlen (name
);
161 if (! (len
> 2 && (STREQ (&name
[len
- 2], ".h"))))
163 current_source_symtab
= s
;
167 if (current_source_symtab
)
170 /* Howabout the partial symbol tables? */
172 for (ofp
= object_files
; ofp
!= NULL
; ofp
= ofp
-> next
)
174 for (ps
= ofp
-> psymtabs
; ps
!= NULL
; ps
= ps
-> next
)
176 char *name
= ps
-> filename
;
177 int len
= strlen (name
);
178 if (! (len
> 2 && (STREQ (&name
[len
- 2], ".h"))))
186 if (cs_pst
-> readin
)
188 fatal ("Internal: select_source_symtab: readin pst found and no symtabs.");
192 current_source_symtab
= PSYMTAB_TO_SYMTAB (cs_pst
);
196 error ("Can't find a default source file");
200 show_directories (ignore
, from_tty
)
204 puts_filtered ("Source directories searched: ");
205 puts_filtered (source_path
);
206 puts_filtered ("\n");
209 /* Forget what we learned about line positions in source files,
210 and which directories contain them;
211 must check again now since files may be found in
212 a different directory now. */
215 forget_cached_source_info ()
217 register struct symtab
*s
;
218 register struct objfile
*objfile
;
220 for (objfile
= object_files
; objfile
!= NULL
; objfile
= objfile
-> next
)
222 for (s
= objfile
-> symtabs
; s
!= NULL
; s
= s
-> next
)
224 if (s
-> line_charpos
!= NULL
)
226 mfree (objfile
-> md
, s
-> line_charpos
);
227 s
-> line_charpos
= NULL
;
229 if (s
-> fullname
!= NULL
)
231 mfree (objfile
-> md
, s
-> fullname
);
232 s
-> fullname
= NULL
;
243 sprintf (buf
, "$cdir%c$cwd", DIRNAME_SEPARATOR
);
244 source_path
= strsave (buf
);
245 forget_cached_source_info ();
248 /* Add zero or more directories to the front of the source path. */
251 directory_command (dirname
, from_tty
)
256 /* FIXME, this goes to "delete dir"... */
259 if (query ("Reinitialize source path to empty? ", ""))
266 mod_path (dirname
, &source_path
);
268 show_directories ((char *)0, from_tty
);
269 forget_cached_source_info ();
272 /* Add zero or more directories to the front of an arbitrary path. */
275 mod_path (dirname
, which_path
)
279 char *old
= *which_path
;
285 dirname
= strsave (dirname
);
286 make_cleanup (free
, dirname
);
290 char *name
= dirname
;
295 char *colon
= strchr (name
, DIRNAME_SEPARATOR
);
296 char *space
= strchr (name
, ' ');
297 char *tab
= strchr (name
, '\t');
298 if (colon
== 0 && space
== 0 && tab
== 0)
299 p
= dirname
= name
+ strlen (name
);
303 if (colon
!= 0 && (p
== 0 || colon
< p
))
305 if (space
!= 0 && (p
== 0 || space
< p
))
307 if (tab
!= 0 && (p
== 0 || tab
< p
))
310 while (*dirname
== DIRNAME_SEPARATOR
|| *dirname
== ' ' || *dirname
== '\t')
316 /* Sigh. "foo/" => "foo" */
324 /* "." => getwd (). */
325 name
= current_directory
;
328 else if (p
[-2] == '/')
338 /* "...foo/." => "...foo". */
349 name
= tilde_expand (name
);
350 else if (name
[0] != '/' && name
[0] != '$')
351 name
= concat (current_directory
, "/", name
, NULL
);
353 name
= savestring (name
, p
- name
);
354 make_cleanup (free
, name
);
356 /* Unless it's a variable, check existence. */
357 if (name
[0] != '$') {
358 /* These are warnings, not errors, since we don't want a
359 non-existent directory in a .gdbinit file to stop processing
360 of the .gdbinit file.
362 Whether they get added to the path is more debatable. Current
363 answer is yes, in case the user wants to go make the directory
364 or whatever. If the directory continues to not exist/not be
365 a directory/etc, then having them in the path should be
367 if (stat (name
, &st
) < 0)
369 int save_errno
= errno
;
370 fprintf_unfiltered (gdb_stderr
, "Warning: ");
371 print_sys_errmsg (name
, save_errno
);
373 else if ((st
.st_mode
& S_IFMT
) != S_IFDIR
)
374 warning ("%s is not a directory.", name
);
379 register unsigned int len
= strlen (name
);
384 if (!strncmp (p
, name
, len
)
385 && (p
[len
] == '\0' || p
[len
] == DIRNAME_SEPARATOR
))
387 /* Found it in the search path, remove old copy */
389 p
--; /* Back over leading colon */
390 if (prefix
> p
- *which_path
)
391 goto skip_dup
; /* Same dir twice in one cmd */
392 strcpy (p
, &p
[len
+1]); /* Copy from next \0 or : */
394 p
= strchr (p
, DIRNAME_SEPARATOR
);
404 tinybuf
[0] = DIRNAME_SEPARATOR
;
407 /* 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. */
414 temp
= concat (old
, tinybuf
, name
, NULL
);
416 *which_path
= concat (temp
, "", &old
[prefix
], NULL
);
417 prefix
= strlen (temp
);
422 *which_path
= concat (name
, (old
[0] ? tinybuf
: old
), old
, NULL
);
423 prefix
= strlen (name
);
430 } while (*dirname
!= '\0');
435 source_info (ignore
, from_tty
)
439 register struct symtab
*s
= current_source_symtab
;
443 printf_filtered("No current source file.\n");
446 printf_filtered ("Current source file is %s\n", s
->filename
);
448 printf_filtered ("Compilation directory is %s\n", s
->dirname
);
450 printf_filtered ("Located in %s\n", s
->fullname
);
452 printf_filtered ("Contains %d line%s.\n", s
->nlines
,
453 s
->nlines
== 1 ? "" : "s");
455 printf_filtered("Source language is %s.\n", language_str (s
->language
));
460 /* Open a file named STRING, searching path PATH (dir names sep by some char)
461 using mode MODE and protection bits PROT in the calls to open.
463 If TRY_CWD_FIRST, try to open ./STRING before searching PATH.
464 (ie pretend the first element of PATH is "."). This also indicates
465 that a slash in STRING disables searching of the path (this is
466 so that "exec-file ./foo" or "symbol-file ./foo" insures that you
467 get that particular version of foo or an error message).
469 If FILENAMED_OPENED is non-null, set it to a newly allocated string naming
470 the actual file opened (this string will always start with a "/". We
471 have to take special pains to avoid doubling the "/" between the directory
472 and the file, sigh! Emacs gets confuzzed by this when we print the
475 If a file is found, return the descriptor.
476 Otherwise, return -1, with errno set for the last name we tried to open. */
478 /* >>>> This should only allow files of certain types,
479 >>>> eg executable, non-directory */
481 openp (path
, try_cwd_first
, string
, mode
, prot
, filename_opened
)
487 char **filename_opened
;
490 register char *filename
;
491 register char *p
, *p1
;
498 if (try_cwd_first
|| string
[0] == '/')
501 fd
= open (filename
, mode
, prot
);
502 if (fd
>= 0 || string
[0] == '/' || strchr (string
, '/'))
507 while (string
[0] == '.' && string
[1] == '/')
510 alloclen
= strlen (path
) + strlen (string
) + 2;
511 filename
= (char *) alloca (alloclen
);
513 for (p
= path
; p
; p
= p1
? p1
+ 1 : 0)
515 p1
= (char *) strchr (p
, DIRNAME_SEPARATOR
);
521 if (len
== 4 && p
[0] == '$' && p
[1] == 'c'
522 && p
[2] == 'w' && p
[3] == 'd') {
523 /* Name is $cwd -- insert current directory name instead. */
526 /* First, realloc the filename buffer if too short. */
527 len
= strlen (current_directory
);
528 newlen
= len
+ strlen (string
) + 2;
529 if (newlen
> alloclen
) {
531 filename
= (char *) alloca (alloclen
);
533 strcpy (filename
, current_directory
);
535 /* Normal file name in path -- just use it. */
536 strncpy (filename
, p
, len
);
540 /* Remove trailing slashes */
541 while (len
> 0 && filename
[len
-1] == '/')
544 strcat (filename
+len
, "/");
545 strcat (filename
, string
);
547 fd
= open (filename
, mode
);
554 *filename_opened
= (char *) 0;
555 else if (filename
[0] == '/')
556 *filename_opened
= savestring (filename
, strlen (filename
));
559 /* Beware the // my son, the Emacs barfs, the botch that catch... */
561 *filename_opened
= concat (current_directory
,
562 '/' == current_directory
[strlen(current_directory
)-1]? "": "/",
569 /* Open a source file given a symtab S. Returns a file descriptor
570 or negative number for error. */
576 char *path
= source_path
;
581 /* Quick way out if we already know its full name */
584 result
= open (s
->fullname
, O_RDONLY
);
587 /* Didn't work -- free old one, try again. */
588 mfree (s
->objfile
->md
, s
->fullname
);
592 if (s
->dirname
!= NULL
)
594 /* Replace a path entry of $cdir with the compilation directory name */
596 /* We cast strstr's result in case an ANSIhole has made it const,
597 which produces a "required warning" when assigned to a nonconst. */
598 p
= (char *)strstr (source_path
, "$cdir");
599 if (p
&& (p
== path
|| p
[-1] == DIRNAME_SEPARATOR
)
600 && (p
[cdir_len
] == DIRNAME_SEPARATOR
|| p
[cdir_len
] == '\0')) {
604 alloca (strlen (source_path
) + 1 + strlen (s
->dirname
) + 1);
605 len
= p
- source_path
;
606 strncpy (path
, source_path
, len
); /* Before $cdir */
607 strcpy (path
+ len
, s
->dirname
); /* new stuff */
608 strcat (path
+ len
, source_path
+ len
+ cdir_len
); /* After $cdir */
612 result
= openp (path
, 0, s
->filename
, O_RDONLY
, 0, &s
->fullname
);
615 /* Didn't work. Try using just the basename. */
616 p
= basename (s
->filename
);
617 if (p
!= s
->filename
)
618 result
= openp (path
, 0, p
, O_RDONLY
, 0, &s
->fullname
);
622 fullname
= s
->fullname
;
623 s
->fullname
= mstrsave (s
->objfile
->md
, s
->fullname
);
630 /* Create and initialize the table S->line_charpos that records
631 the positions of the lines in the source file, which is assumed
632 to be open on descriptor DESC.
633 All set S->nlines to the number of such lines. */
636 find_source_lines (s
, desc
)
641 register char *data
, *p
, *end
;
643 int lines_allocated
= 1000;
647 #ifdef LSEEK_NOT_LINEAR
651 line_charpos
= (int *) xmmalloc (s
-> objfile
-> md
,
652 lines_allocated
* sizeof (int));
653 if (fstat (desc
, &st
) < 0)
654 perror_with_name (s
->filename
);
657 exec_mtime
= bfd_get_mtime(exec_bfd
);
658 if (exec_mtime
&& exec_mtime
< st
.st_mtime
)
659 printf_filtered ("Source file is more recent than executable.\n");
662 #ifdef LSEEK_NOT_LINEAR
663 /* Have to read it byte by byte to find out where the chars live */
665 line_charpos
[0] = tell(desc
);
667 while (myread(desc
, &c
, 1)>0)
671 if (nlines
== lines_allocated
)
673 lines_allocated
*= 2;
675 (int *) xmrealloc (s
-> objfile
-> md
, (char *) line_charpos
,
676 sizeof (int) * lines_allocated
);
678 line_charpos
[nlines
++] = tell(desc
);
683 /* st_size might be a large type, but we only support source files whose
684 size fits in an int. FIXME. */
685 size
= (int) st
.st_size
;
687 #ifdef BROKEN_LARGE_ALLOCA
688 data
= (char *) xmalloc (size
);
689 make_cleanup (free
, data
);
691 data
= (char *) alloca (size
);
693 if (myread (desc
, data
, size
) < 0)
694 perror_with_name (s
->filename
);
702 /* A newline at the end does not start a new line. */
705 if (nlines
== lines_allocated
)
707 lines_allocated
*= 2;
709 (int *) xmrealloc (s
-> objfile
-> md
, (char *) line_charpos
,
710 sizeof (int) * lines_allocated
);
712 line_charpos
[nlines
++] = p
- data
;
718 (int *) xmrealloc (s
-> objfile
-> md
, (char *) line_charpos
,
719 nlines
* sizeof (int));
723 /* Return the character position of a line LINE in symtab S.
724 Return 0 if anything is invalid. */
726 #if 0 /* Currently unused */
729 source_line_charpos (s
, line
)
734 if (!s
->line_charpos
|| line
<= 0) return 0;
735 if (line
> s
->nlines
)
737 return s
->line_charpos
[line
- 1];
740 /* Return the line number of character position POS in symtab S. */
743 source_charpos_line (s
, chr
)
744 register struct symtab
*s
;
747 register int line
= 0;
750 if (s
== 0 || s
->line_charpos
== 0) return 0;
751 lnp
= s
->line_charpos
;
752 /* Files are usually short, so sequential search is Ok */
753 while (line
< s
->nlines
&& *lnp
<= chr
)
758 if (line
>= s
->nlines
)
766 /* Get full pathname and line number positions for a symtab.
767 Return nonzero if line numbers may have changed.
768 Set *FULLNAME to actual name of the file as found by `openp',
769 or to 0 if the file is not found. */
772 get_filename_and_charpos (s
, fullname
)
776 register int desc
, linenums_changed
= 0;
778 desc
= open_source_file (s
);
786 *fullname
= s
->fullname
;
787 if (s
->line_charpos
== 0) linenums_changed
= 1;
788 if (linenums_changed
) find_source_lines (s
, desc
);
790 return linenums_changed
;
793 /* Print text describing the full name of the source file S
794 and the line number LINE and its corresponding character position.
795 The text starts with two Ctrl-z so that the Emacs-GDB interface
798 MID_STATEMENT is nonzero if the PC is not at the beginning of that line.
800 Return 1 if successful, 0 if could not find the file. */
803 identify_source_line (s
, line
, mid_statement
, pc
)
809 if (s
->line_charpos
== 0)
810 get_filename_and_charpos (s
, (char **)NULL
);
811 if (s
->fullname
== 0)
813 if (line
> s
->nlines
)
814 /* Don't index off the end of the line_charpos array. */
816 /* FIXME-32x64: Need a version of print_address_numeric which does
817 not pass use_local to print_longest. */
818 printf_unfiltered ("\032\032%s:%d:%d:%s:0x%lx\n", s
->fullname
,
819 line
, s
->line_charpos
[line
- 1],
820 mid_statement
? "middle" : "beg",
822 current_source_line
= line
;
823 first_line_listed
= line
;
824 last_line_listed
= line
;
825 current_source_symtab
= s
;
829 /* Print source lines from the file of symtab S,
830 starting with line number LINE and stopping before line number STOPLINE. */
833 print_source_lines (s
, line
, stopline
, noerror
)
840 register FILE *stream
;
841 int nlines
= stopline
- line
;
843 /* Regardless of whether we can open the file, set current_source_symtab. */
844 current_source_symtab
= s
;
845 current_source_line
= line
;
846 first_line_listed
= line
;
848 desc
= open_source_file (s
);
852 char *name
= alloca (strlen (s
->filename
) + 100);
853 sprintf (name
, "%s:%d", s
->filename
, line
);
854 print_sys_errmsg (name
, errno
);
859 if (s
->line_charpos
== 0)
860 find_source_lines (s
, desc
);
862 if (line
< 1 || line
> s
->nlines
)
865 error ("Line number %d out of range; %s has %d lines.",
866 line
, s
->filename
, s
->nlines
);
869 if (lseek (desc
, s
->line_charpos
[line
- 1], 0) < 0)
872 perror_with_name (s
->filename
);
875 stream
= fdopen (desc
, FOPEN_RT
);
882 last_line_listed
= current_source_line
;
883 printf_filtered ("%d\t", current_source_line
++);
886 if (c
< 040 && c
!= '\t' && c
!= '\n' && c
!= '\r')
887 printf_filtered ("^%c", c
+ 0100);
889 printf_filtered ("^?");
891 printf_filtered ("%c", c
);
892 } while (c
!= '\n' && (c
= fgetc (stream
)) >= 0);
902 Print a list of files and line numbers which a user may choose from
903 in order to list a function which was specified ambiguously
904 (as with `list classname::overloadedfuncname', for example).
905 The vector in SALS provides the filenames and line numbers.
908 ambiguous_line_spec (sals
)
909 struct symtabs_and_lines
*sals
;
913 for (i
= 0; i
< sals
->nelts
; ++i
)
914 printf_filtered("file: \"%s\", line number: %d\n",
915 sals
->sals
[i
].symtab
->filename
, sals
->sals
[i
].line
);
920 list_command (arg
, from_tty
)
924 struct symtabs_and_lines sals
, sals_end
;
925 struct symtab_and_line sal
, sal_end
;
934 if (!have_full_symbols () && !have_partial_symbols())
935 error ("No symbol table is loaded. Use the \"file\" command.");
937 /* Pull in a current source symtab if necessary */
938 if (current_source_symtab
== 0 &&
939 (arg
== 0 || arg
[0] == '+' || arg
[0] == '-'))
940 select_source_symtab (0);
942 /* "l" or "l +" lists next ten lines. */
944 if (arg
== 0 || STREQ (arg
, "+"))
946 if (current_source_symtab
== 0)
947 error ("No default source file yet. Do \"help list\".");
948 print_source_lines (current_source_symtab
, current_source_line
,
949 current_source_line
+ lines_to_list
, 0);
953 /* "l -" lists previous ten lines, the ones before the ten just listed. */
954 if (STREQ (arg
, "-"))
956 if (current_source_symtab
== 0)
957 error ("No default source file yet. Do \"help list\".");
958 print_source_lines (current_source_symtab
,
959 max (first_line_listed
- lines_to_list
, 1),
960 first_line_listed
, 0);
964 /* Now if there is only one argument, decode it in SAL
966 If there are two arguments, decode them in SAL and SAL_END
967 and clear NO_END; however, if one of the arguments is blank,
968 set DUMMY_BEG or DUMMY_END to record that fact. */
975 sals
= decode_line_1 (&arg1
, 0, 0, 0, 0);
977 if (! sals
.nelts
) return; /* C++ */
980 ambiguous_line_spec (&sals
);
989 /* Record whether the BEG arg is all digits. */
991 for (p
= arg
; p
!= arg1
&& *p
>= '0' && *p
<= '9'; p
++);
992 linenum_beg
= (p
== arg1
);
994 while (*arg1
== ' ' || *arg1
== '\t')
1000 while (*arg1
== ' ' || *arg1
== '\t')
1007 sals_end
= decode_line_1 (&arg1
, 0, 0, 0, 0);
1009 sals_end
= decode_line_1 (&arg1
, 0, sal
.symtab
, sal
.line
, 0);
1010 if (sals_end
.nelts
== 0)
1012 if (sals_end
.nelts
> 1)
1014 ambiguous_line_spec (&sals_end
);
1015 free (sals_end
.sals
);
1018 sal_end
= sals_end
.sals
[0];
1019 free (sals_end
.sals
);
1024 error ("Junk at end of line specification.");
1026 if (!no_end
&& !dummy_beg
&& !dummy_end
1027 && sal
.symtab
!= sal_end
.symtab
)
1028 error ("Specified start and end are in different files.");
1029 if (dummy_beg
&& dummy_end
)
1030 error ("Two empty args do not say what lines to list.");
1032 /* if line was specified by address,
1033 first print exactly which line, and which file.
1034 In this case, sal.symtab == 0 means address is outside
1035 of all known source files, not that user failed to give a filename. */
1038 if (sal
.symtab
== 0)
1039 /* FIXME-32x64--assumes sal.pc fits in long. */
1040 error ("No source file for address %s.",
1041 local_hex_string((unsigned long) sal
.pc
));
1042 sym
= find_pc_function (sal
.pc
);
1045 print_address_numeric (sal
.pc
, gdb_stdout
);
1046 printf_filtered (" is in ");
1047 fputs_filtered (SYMBOL_SOURCE_NAME (sym
), gdb_stdout
);
1048 printf_filtered (" (%s:%d).\n", sal
.symtab
->filename
, sal
.line
);
1052 print_address_numeric (sal
.pc
, gdb_stdout
);
1053 printf_filtered (" is at %s:%d.\n",
1054 sal
.symtab
->filename
, sal
.line
);
1058 /* If line was not specified by just a line number,
1059 and it does not imply a symtab, it must be an undebuggable symbol
1060 which means no source code. */
1062 if (! linenum_beg
&& sal
.symtab
== 0)
1063 error ("No line number known for %s.", arg
);
1065 /* If this command is repeated with RET,
1066 turn it into the no-arg variant. */
1071 if (dummy_beg
&& sal_end
.symtab
== 0)
1072 error ("No default source file yet. Do \"help list\".");
1074 print_source_lines (sal_end
.symtab
,
1075 max (sal_end
.line
- (lines_to_list
- 1), 1),
1076 sal_end
.line
+ 1, 0);
1077 else if (sal
.symtab
== 0)
1078 error ("No default source file yet. Do \"help list\".");
1080 print_source_lines (sal
.symtab
,
1081 max (sal
.line
- (lines_to_list
/ 2), 1),
1082 sal
.line
+ (lines_to_list
/ 2), 0);
1084 print_source_lines (sal
.symtab
, sal
.line
,
1086 ? sal
.line
+ lines_to_list
1087 : sal_end
.line
+ 1),
1091 /* Print info on range of pc's in a specified line. */
1094 line_info (arg
, from_tty
)
1098 struct symtabs_and_lines sals
;
1099 struct symtab_and_line sal
;
1100 CORE_ADDR start_pc
, end_pc
;
1105 sal
.symtab
= current_source_symtab
;
1106 sal
.line
= last_line_listed
;
1108 sals
.sals
= (struct symtab_and_line
*)
1109 xmalloc (sizeof (struct symtab_and_line
));
1114 sals
= decode_line_spec_1 (arg
, 0);
1119 /* C++ More than one line may have been specified, as when the user
1120 specifies an overloaded function name. Print info on them all. */
1121 for (i
= 0; i
< sals
.nelts
; i
++)
1125 if (sal
.symtab
== 0)
1127 printf_filtered ("No line number information available");
1130 /* This is useful for "info line *0x7f34". If we can't tell the
1131 user about a source line, at least let them have the symbolic
1133 printf_filtered (" for address ");
1135 print_address (sal
.pc
, gdb_stdout
);
1138 printf_filtered (".");
1139 printf_filtered ("\n");
1141 else if (sal
.line
> 0
1142 && find_line_pc_range (sal
, &start_pc
, &end_pc
))
1144 if (start_pc
== end_pc
)
1146 printf_filtered ("Line %d of \"%s\"",
1147 sal
.line
, sal
.symtab
->filename
);
1149 printf_filtered (" is at address ");
1150 print_address (start_pc
, gdb_stdout
);
1152 printf_filtered (" but contains no code.\n");
1156 printf_filtered ("Line %d of \"%s\"",
1157 sal
.line
, sal
.symtab
->filename
);
1159 printf_filtered (" starts at address ");
1160 print_address (start_pc
, gdb_stdout
);
1162 printf_filtered (" and ends at ");
1163 print_address (end_pc
, gdb_stdout
);
1164 printf_filtered (".\n");
1167 /* x/i should display this line's code. */
1168 set_next_address (start_pc
);
1170 /* Repeating "info line" should do the following line. */
1171 last_line_listed
= sal
.line
+ 1;
1173 /* If this is the only line, show the source code. If it could
1174 not find the file, don't do anything special. */
1175 if (frame_file_full_name
&& sals
.nelts
== 1)
1176 identify_source_line (sal
.symtab
, sal
.line
, 0, start_pc
);
1179 /* Is there any case in which we get here, and have an address
1180 which the user would want to see? If we have debugging symbols
1181 and no line numbers? */
1182 printf_filtered ("Line number %d is out of range for \"%s\".\n",
1183 sal
.line
, sal
.symtab
->filename
);
1188 /* Commands to search the source file for a regexp. */
1192 forward_search_command (regex
, from_tty
)
1198 register FILE *stream
;
1199 int line
= last_line_listed
+ 1;
1202 msg
= (char *) re_comp (regex
);
1206 if (current_source_symtab
== 0)
1207 select_source_symtab (0);
1209 /* Search from last_line_listed+1 in current_source_symtab */
1211 desc
= open_source_file (current_source_symtab
);
1213 perror_with_name (current_source_symtab
->filename
);
1215 if (current_source_symtab
->line_charpos
== 0)
1216 find_source_lines (current_source_symtab
, desc
);
1218 if (line
< 1 || line
> current_source_symtab
->nlines
)
1221 error ("Expression not found");
1224 if (lseek (desc
, current_source_symtab
->line_charpos
[line
- 1], 0) < 0)
1227 perror_with_name (current_source_symtab
->filename
);
1230 stream
= fdopen (desc
, FOPEN_RT
);
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);
1258 printf_filtered ("Expression not found\n");
1264 reverse_search_command (regex
, from_tty
)
1270 register FILE *stream
;
1271 int line
= last_line_listed
- 1;
1274 msg
= (char *) re_comp (regex
);
1278 if (current_source_symtab
== 0)
1279 select_source_symtab (0);
1281 /* Search from last_line_listed-1 in current_source_symtab */
1283 desc
= open_source_file (current_source_symtab
);
1285 perror_with_name (current_source_symtab
->filename
);
1287 if (current_source_symtab
->line_charpos
== 0)
1288 find_source_lines (current_source_symtab
, desc
);
1290 if (line
< 1 || line
> current_source_symtab
->nlines
)
1293 error ("Expression not found");
1296 if (lseek (desc
, current_source_symtab
->line_charpos
[line
- 1], 0) < 0)
1299 perror_with_name (current_source_symtab
->filename
);
1302 stream
= fdopen (desc
, FOPEN_RT
);
1306 /* FIXME!!! We walk right off the end of buf if we get a long line!!! */
1307 char buf
[4096]; /* Should be reasonable??? */
1308 register char *p
= buf
;
1315 } while (c
!= '\n' && (c
= getc (stream
)) >= 0);
1317 /* We now have a source line in buf; null terminate and match. */
1319 if (re_exec (buf
) > 0)
1323 print_source_lines (current_source_symtab
,
1325 current_source_line
= max (line
- lines_to_list
/ 2, 1);
1329 if (fseek (stream
, current_source_symtab
->line_charpos
[line
- 1], 0) < 0)
1332 perror_with_name (current_source_symtab
->filename
);
1336 printf_filtered ("Expression not found\n");
1342 _initialize_source ()
1344 struct cmd_list_element
*c
;
1345 current_source_symtab
= 0;
1346 init_source_path ();
1348 /* The intention is to use POSIX Basic Regular Expressions.
1349 Always use the GNU regex routine for consistency across all hosts.
1350 Our current GNU regex.c does not have all the POSIX features, so this is
1351 just an approximation. */
1352 re_set_syntax (RE_SYNTAX_GREP
);
1354 c
= add_cmd ("directory", class_files
, directory_command
,
1355 "Add directory DIR to beginning of search path for source files.\n\
1356 Forget cached info on source file locations and line positions.\n\
1357 DIR can also be $cwd for the current working directory, or $cdir for the\n\
1358 directory in which the source file was compiled into object code.\n\
1359 With no argument, reset the search path to $cdir:$cwd, the default.",
1361 c
->completer
= filename_completer
;
1363 add_cmd ("directories", no_class
, show_directories
,
1364 "Current search path for finding source files.\n\
1365 $cwd in the path means the current working directory.\n\
1366 $cdir in the path means the compilation directory of the source file.",
1369 add_info ("source", source_info
,
1370 "Information about the current source file.");
1372 add_info ("line", line_info
,
1373 "Core addresses of the code for a source line.\n\
1374 Line can be specified as\n\
1375 LINENUM, to list around that line in current file,\n\
1376 FILE:LINENUM, to list around that line in that file,\n\
1377 FUNCTION, to list around beginning of that function,\n\
1378 FILE:FUNCTION, to distinguish among like-named static functions.\n\
1379 Default is to describe the last source line that was listed.\n\n\
1380 This sets the default address for \"x\" to the line's first instruction\n\
1381 so that \"x/i\" suffices to start examining the machine code.\n\
1382 The address is also stored as the value of \"$_\".");
1384 add_com ("forward-search", class_files
, forward_search_command
,
1385 "Search for regular expression (see regex(3)) from last line listed.");
1386 add_com_alias ("search", "forward-search", class_files
, 0);
1388 add_com ("reverse-search", class_files
, reverse_search_command
,
1389 "Search backward for regular expression (see regex(3)) from last line listed.");
1391 add_com ("list", class_files
, list_command
,
1392 "List specified function or line.\n\
1393 With no argument, lists ten more lines after or around previous listing.\n\
1394 \"list -\" lists the ten lines before a previous ten-line listing.\n\
1395 One argument specifies a line, and ten lines are listed around that line.\n\
1396 Two arguments with comma between specify starting and ending lines to list.\n\
1397 Lines can be specified in these ways:\n\
1398 LINENUM, to list around that line in current file,\n\
1399 FILE:LINENUM, to list around that line in that file,\n\
1400 FUNCTION, to list around beginning of that function,\n\
1401 FILE:FUNCTION, to distinguish among like-named static functions.\n\
1402 *ADDRESS, to list around the line containing that address.\n\
1403 With two args if one is empty it stands for ten lines away from the other arg.");
1404 add_com_alias ("l", "list", class_files
, 1);
1407 (add_set_cmd ("listsize", class_support
, var_uinteger
,
1408 (char *)&lines_to_list
,
1409 "Set number of source lines gdb will list by default.",
This page took 0.107042 seconds and 4 git commands to generate.