1 /* List lines of source files for GDB, the GNU debugger.
2 Copyright 1986, 1987, 1988, 1989, 1991, 1992, 1993, 1994, 1995
3 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
23 #include "expression.h"
30 #include <sys/types.h>
31 #include "gdb_string.h"
38 #include "gnu-regex.h"
44 /* Prototypes for local functions. */
46 static int open_source_file
PARAMS ((struct symtab
*));
48 static int get_filename_and_charpos
PARAMS ((struct symtab
*, char **));
50 static void reverse_search_command
PARAMS ((char *, int));
52 static void forward_search_command
PARAMS ((char *, int));
54 static void line_info
PARAMS ((char *, int));
56 static void list_command
PARAMS ((char *, int));
58 static void ambiguous_line_spec
PARAMS ((struct symtabs_and_lines
*));
60 static void source_info
PARAMS ((char *, int));
62 static void show_directories
PARAMS ((char *, int));
64 static void find_source_lines
PARAMS ((struct symtab
*, int));
66 /* If we use this declaration, it breaks because of fucking ANSI "const" stuff
67 on some systems. We just have to not declare it at all, have it default
68 to int, and possibly botch on a few systems. Thanks, ANSIholes... */
69 /* extern char *strstr(); */
71 /* Path of directories to search for source files.
72 Same format as the PATH environment variable's value. */
76 /* Symtab of default file for listing lines of. */
78 struct symtab
*current_source_symtab
;
80 /* Default next line to list. */
82 int current_source_line
;
84 /* Default number of lines to print with commands like "list".
85 This is based on guessing how many long (i.e. more than chars_per_line
86 characters) lines there will be. To be completely correct, "list"
87 and friends should be rewritten to count characters and see where
88 things are wrapping, but that would be a fair amount of work. */
90 int lines_to_list
= 10;
92 /* Line number of last line printed. Default for various commands.
93 current_source_line is usually, but not always, the same as this. */
95 static int last_line_listed
;
97 /* First line number listed by last listing command. */
99 static int first_line_listed
;
102 /* Set the source file default for the "list" command to be S.
104 If S is NULL, and we don't have a default, find one. This
105 should only be called when the user actually tries to use the
106 default, since we produce an error if we can't find a reasonable
107 default. Also, since this can cause symbols to be read, doing it
108 before we need to would make things slower than necessary. */
111 select_source_symtab (s
)
112 register struct symtab
*s
;
114 struct symtabs_and_lines sals
;
115 struct symtab_and_line sal
;
116 struct partial_symtab
*ps
;
117 struct partial_symtab
*cs_pst
= 0;
122 current_source_symtab
= s
;
123 current_source_line
= 1;
127 if (current_source_symtab
)
130 /* Make the default place to list be the function `main'
132 if (lookup_symbol ("main", 0, VAR_NAMESPACE
, 0, NULL
))
134 sals
= decode_line_spec ("main", 1);
137 current_source_symtab
= sal
.symtab
;
138 current_source_line
= max (sal
.line
- (lines_to_list
- 1), 1);
139 if (current_source_symtab
)
143 /* All right; find the last file in the symtab list (ignoring .h's). */
145 current_source_line
= 1;
147 for (ofp
= object_files
; ofp
!= NULL
; ofp
= ofp
-> next
)
149 for (s
= ofp
-> symtabs
; s
; s
= s
->next
)
151 char *name
= s
-> filename
;
152 int len
= strlen (name
);
153 if (! (len
> 2 && (STREQ (&name
[len
- 2], ".h"))))
155 current_source_symtab
= s
;
159 if (current_source_symtab
)
162 /* Howabout the partial symbol tables? */
164 for (ofp
= object_files
; ofp
!= NULL
; ofp
= ofp
-> next
)
166 for (ps
= ofp
-> psymtabs
; ps
!= NULL
; ps
= ps
-> next
)
168 char *name
= ps
-> filename
;
169 int len
= strlen (name
);
170 if (! (len
> 2 && (STREQ (&name
[len
- 2], ".h"))))
178 if (cs_pst
-> readin
)
180 fatal ("Internal: select_source_symtab: readin pst found and no symtabs.");
184 current_source_symtab
= PSYMTAB_TO_SYMTAB (cs_pst
);
188 error ("Can't find a default source file");
192 show_directories (ignore
, from_tty
)
196 puts_filtered ("Source directories searched: ");
197 puts_filtered (source_path
);
198 puts_filtered ("\n");
201 /* Forget what we learned about line positions in source files, and
202 which directories contain them; must check again now since files
203 may be found in a different directory now. */
206 forget_cached_source_info ()
208 register struct symtab
*s
;
209 register struct objfile
*objfile
;
211 for (objfile
= object_files
; objfile
!= NULL
; objfile
= objfile
-> next
)
213 for (s
= objfile
-> symtabs
; s
!= NULL
; s
= s
-> next
)
215 if (s
-> line_charpos
!= NULL
)
217 mfree (objfile
-> md
, s
-> line_charpos
);
218 s
-> line_charpos
= NULL
;
220 if (s
-> fullname
!= NULL
)
222 mfree (objfile
-> md
, s
-> fullname
);
223 s
-> fullname
= NULL
;
234 sprintf (buf
, "$cdir%c$cwd", DIRNAME_SEPARATOR
);
235 source_path
= strsave (buf
);
236 forget_cached_source_info ();
239 /* Add zero or more directories to the front of the source path. */
242 directory_command (dirname
, from_tty
)
247 /* FIXME, this goes to "delete dir"... */
250 if (query ("Reinitialize source path to empty? "))
257 mod_path (dirname
, &source_path
);
259 show_directories ((char *)0, from_tty
);
260 forget_cached_source_info ();
263 /* Add zero or more directories to the front of an arbitrary path. */
266 mod_path (dirname
, which_path
)
270 char *old
= *which_path
;
276 dirname
= strsave (dirname
);
277 make_cleanup (free
, dirname
);
281 char *name
= dirname
;
286 char *separator
= strchr (name
, DIRNAME_SEPARATOR
);
287 char *space
= strchr (name
, ' ');
288 char *tab
= strchr (name
, '\t');
290 if (separator
== 0 && space
== 0 && tab
== 0)
291 p
= dirname
= name
+ strlen (name
);
295 if (separator
!= 0 && (p
== 0 || separator
< p
))
297 if (space
!= 0 && (p
== 0 || space
< p
))
299 if (tab
!= 0 && (p
== 0 || tab
< p
))
302 while (*dirname
== DIRNAME_SEPARATOR
310 /* On win32 h:\ is different to h: */
312 /* Sigh. "foo/" => "foo" */
321 /* "." => getwd (). */
322 name
= current_directory
;
325 else if (SLASH_P (p
[-2]))
335 /* "...foo/." => "...foo". */
346 name
= tilde_expand (name
);
347 else if (!ROOTED_P (name
) && name
[0] != '$')
348 name
= concat (current_directory
, SLASH_STRING
, name
, NULL
);
350 name
= savestring (name
, p
- name
);
351 make_cleanup (free
, name
);
353 /* Unless it's a variable, check existence. */
354 if (name
[0] != '$') {
355 /* These are warnings, not errors, since we don't want a
356 non-existent directory in a .gdbinit file to stop processing
357 of the .gdbinit file.
359 Whether they get added to the path is more debatable. Current
360 answer is yes, in case the user wants to go make the directory
361 or whatever. If the directory continues to not exist/not be
362 a directory/etc, then having them in the path should be
364 if (stat (name
, &st
) < 0)
366 int save_errno
= errno
;
367 fprintf_unfiltered (gdb_stderr
, "Warning: ");
368 print_sys_errmsg (name
, save_errno
);
370 else if ((st
.st_mode
& S_IFMT
) != S_IFDIR
)
371 warning ("%s is not a directory.", name
);
376 register unsigned int len
= strlen (name
);
381 if (!strncmp (p
, name
, len
)
382 && (p
[len
] == '\0' || p
[len
] == DIRNAME_SEPARATOR
))
384 /* Found it in the search path, remove old copy */
386 p
--; /* Back over leading separator */
387 if (prefix
> p
- *which_path
)
388 goto skip_dup
; /* Same dir twice in one cmd */
389 strcpy (p
, &p
[len
+1]); /* Copy from next \0 or : */
391 p
= strchr (p
, DIRNAME_SEPARATOR
);
401 tinybuf
[0] = DIRNAME_SEPARATOR
;
404 /* 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. */
411 temp
= concat (old
, tinybuf
, name
, NULL
);
413 *which_path
= concat (temp
, "", &old
[prefix
], NULL
);
414 prefix
= strlen (temp
);
419 *which_path
= concat (name
, (old
[0] ? tinybuf
: old
), old
, NULL
);
420 prefix
= strlen (name
);
427 } while (*dirname
!= '\0');
432 source_info (ignore
, from_tty
)
436 register struct symtab
*s
= current_source_symtab
;
440 printf_filtered("No current source file.\n");
443 printf_filtered ("Current source file is %s\n", s
->filename
);
445 printf_filtered ("Compilation directory is %s\n", s
->dirname
);
447 printf_filtered ("Located in %s\n", s
->fullname
);
449 printf_filtered ("Contains %d line%s.\n", s
->nlines
,
450 s
->nlines
== 1 ? "" : "s");
452 printf_filtered ("Source language is %s.\n", language_str (s
->language
));
453 printf_filtered ("Compiled with %s debugging format.\n", s
->debugformat
);
458 /* Open a file named STRING, searching path PATH (dir names sep by some char)
459 using mode MODE and protection bits PROT in the calls to open.
461 If TRY_CWD_FIRST, try to open ./STRING before searching PATH.
462 (ie pretend the first element of PATH is "."). This also indicates
463 that a slash in STRING disables searching of the path (this is
464 so that "exec-file ./foo" or "symbol-file ./foo" insures that you
465 get that particular version of foo or an error message).
467 If FILENAMED_OPENED is non-null, set it to a newly allocated string naming
468 the actual file opened (this string will always start with a "/". We
469 have to take special pains to avoid doubling the "/" between the directory
470 and the file, sigh! Emacs gets confuzzed by this when we print the
473 If a file is found, return the descriptor.
474 Otherwise, return -1, with errno set for the last name we tried to open. */
476 /* >>>> This should only allow files of certain types,
477 >>>> eg executable, non-directory */
479 openp (path
, try_cwd_first
, string
, mode
, prot
, filename_opened
)
485 char **filename_opened
;
488 register char *filename
;
489 register char *p
, *p1
;
500 if (try_cwd_first
|| SLASH_P (string
[0]))
504 fd
= open (filename
, mode
, prot
);
507 for (i
= 0; string
[i
]; i
++)
508 if (SLASH_P (string
[i
]))
513 while (string
[0] == '.' && SLASH_P (string
[1]))
516 alloclen
= strlen (path
) + strlen (string
) + 2;
517 filename
= (char *) alloca (alloclen
);
519 for (p
= path
; p
; p
= p1
? p1
+ 1 : 0)
521 p1
= (char *) strchr (p
, DIRNAME_SEPARATOR
);
527 if (len
== 4 && p
[0] == '$' && p
[1] == 'c'
528 && p
[2] == 'w' && p
[3] == 'd') {
529 /* Name is $cwd -- insert current directory name instead. */
532 /* First, realloc the filename buffer if too short. */
533 len
= strlen (current_directory
);
534 newlen
= len
+ strlen (string
) + 2;
535 if (newlen
> alloclen
) {
537 filename
= (char *) alloca (alloclen
);
539 strcpy (filename
, current_directory
);
541 /* Normal file name in path -- just use it. */
542 strncpy (filename
, p
, len
);
546 /* Remove trailing slashes */
547 while (len
> 0 && SLASH_P (filename
[len
-1]))
550 strcat (filename
+len
, SLASH_STRING
);
551 strcat (filename
, string
);
553 fd
= open (filename
, mode
);
561 *filename_opened
= (char *) 0;
562 else if (ROOTED_P (filename
))
563 *filename_opened
= savestring (filename
, strlen (filename
));
566 /* Beware the // my son, the Emacs barfs, the botch that catch... */
568 *filename_opened
= concat (current_directory
,
570 == current_directory
[strlen(current_directory
)-1]
576 /* This is a debugging hack that can go away when all combinations
577 of Mac and Unix names are handled reasonably. */
579 extern int debug_openp
;
583 printf("openp on %s, path %s mode %d prot %d\n returned %d",
584 string
, path
, mode
, prot
, fd
);
585 if (*filename_opened
)
586 printf(" (filename is %s)", *filename_opened
);
595 /* Open a source file given a symtab S. Returns a file descriptor or
596 negative number for error. */
602 char *path
= source_path
;
607 /* Quick way out if we already know its full name */
610 result
= open (s
->fullname
, O_RDONLY
);
613 /* Didn't work -- free old one, try again. */
614 mfree (s
->objfile
->md
, s
->fullname
);
618 if (s
->dirname
!= NULL
)
620 /* Replace a path entry of $cdir with the compilation directory name */
622 /* We cast strstr's result in case an ANSIhole has made it const,
623 which produces a "required warning" when assigned to a nonconst. */
624 p
= (char *)strstr (source_path
, "$cdir");
625 if (p
&& (p
== path
|| p
[-1] == DIRNAME_SEPARATOR
)
626 && (p
[cdir_len
] == DIRNAME_SEPARATOR
|| p
[cdir_len
] == '\0'))
631 alloca (strlen (source_path
) + 1 + strlen (s
->dirname
) + 1);
632 len
= p
- source_path
;
633 strncpy (path
, source_path
, len
); /* Before $cdir */
634 strcpy (path
+ len
, s
->dirname
); /* new stuff */
635 strcat (path
+ len
, source_path
+ len
+ cdir_len
); /* After $cdir */
639 result
= openp (path
, 0, s
->filename
, O_RDONLY
, 0, &s
->fullname
);
642 /* Didn't work. Try using just the basename. */
643 p
= basename (s
->filename
);
644 if (p
!= s
->filename
)
645 result
= openp (path
, 0, p
, O_RDONLY
, 0, &s
->fullname
);
650 /* Didn't work. Try using just the MPW basename. */
651 p
= (char *) mpw_basename (s
->filename
);
652 if (p
!= s
->filename
)
653 result
= openp (path
, 0, p
, O_RDONLY
, 0, &s
->fullname
);
657 /* Didn't work. Try using the mixed Unix/MPW basename. */
658 p
= (char *) mpw_mixed_basename (s
->filename
);
659 if (p
!= s
->filename
)
660 result
= openp (path
, 0, p
, O_RDONLY
, 0, &s
->fullname
);
666 fullname
= s
->fullname
;
667 s
->fullname
= mstrsave (s
->objfile
->md
, s
->fullname
);
673 /* Return the path to the source file associated with symtab. Returns NULL
677 symtab_to_filename (s
)
685 /* If we've seen the file before, just return fullname. */
690 /* Try opening the file to setup fullname */
692 fd
= open_source_file (s
);
694 return s
->filename
; /* File not found. Just use short name */
696 /* Found the file. Cleanup and return the full name */
703 /* Create and initialize the table S->line_charpos that records
704 the positions of the lines in the source file, which is assumed
705 to be open on descriptor DESC.
706 All set S->nlines to the number of such lines. */
709 find_source_lines (s
, desc
)
714 register char *data
, *p
, *end
;
716 int lines_allocated
= 1000;
721 line_charpos
= (int *) xmmalloc (s
-> objfile
-> md
,
722 lines_allocated
* sizeof (int));
723 if (fstat (desc
, &st
) < 0)
724 perror_with_name (s
->filename
);
726 if (s
&& s
->objfile
&& s
->objfile
->obfd
)
728 mtime
= bfd_get_mtime(s
->objfile
->obfd
);
729 if (mtime
&& mtime
< st
.st_mtime
)
730 printf_filtered ("Source file is more recent than executable.\n");
734 mtime
= bfd_get_mtime(exec_bfd
);
735 if (mtime
&& mtime
< st
.st_mtime
)
736 printf_filtered ("Source file is more recent than executable.\n");
739 #ifdef LSEEK_NOT_LINEAR
743 /* Have to read it byte by byte to find out where the chars live */
745 line_charpos
[0] = lseek (desc
, 0, SEEK_CUR
);
747 while (myread(desc
, &c
, 1)>0)
751 if (nlines
== lines_allocated
)
753 lines_allocated
*= 2;
755 (int *) xmrealloc (s
-> objfile
-> md
, (char *) line_charpos
,
756 sizeof (int) * lines_allocated
);
758 line_charpos
[nlines
++] = lseek (desc
, 0, SEEK_CUR
);
762 #else /* lseek linear. */
764 struct cleanup
*old_cleanups
;
766 /* st_size might be a large type, but we only support source files whose
767 size fits in an int. */
768 size
= (int) st
.st_size
;
770 /* Use malloc, not alloca, because this may be pretty large, and we may
771 run into various kinds of limits on stack size. */
772 data
= (char *) xmalloc (size
);
773 old_cleanups
= make_cleanup (free
, data
);
775 /* Reassign `size' to result of read for systems where \r\n -> \n. */
776 size
= myread (desc
, data
, size
);
778 perror_with_name (s
->filename
);
786 /* A newline at the end does not start a new line. */
789 if (nlines
== lines_allocated
)
791 lines_allocated
*= 2;
793 (int *) xmrealloc (s
-> objfile
-> md
, (char *) line_charpos
,
794 sizeof (int) * lines_allocated
);
796 line_charpos
[nlines
++] = p
- data
;
799 do_cleanups (old_cleanups
);
801 #endif /* lseek linear. */
804 (int *) xmrealloc (s
-> objfile
-> md
, (char *) line_charpos
,
805 nlines
* sizeof (int));
809 /* Return the character position of a line LINE in symtab S.
810 Return 0 if anything is invalid. */
812 #if 0 /* Currently unused */
815 source_line_charpos (s
, line
)
820 if (!s
->line_charpos
|| line
<= 0) return 0;
821 if (line
> s
->nlines
)
823 return s
->line_charpos
[line
- 1];
826 /* Return the line number of character position POS in symtab S. */
829 source_charpos_line (s
, chr
)
830 register struct symtab
*s
;
833 register int line
= 0;
836 if (s
== 0 || s
->line_charpos
== 0) return 0;
837 lnp
= s
->line_charpos
;
838 /* Files are usually short, so sequential search is Ok */
839 while (line
< s
->nlines
&& *lnp
<= chr
)
844 if (line
>= s
->nlines
)
852 /* Get full pathname and line number positions for a symtab.
853 Return nonzero if line numbers may have changed.
854 Set *FULLNAME to actual name of the file as found by `openp',
855 or to 0 if the file is not found. */
858 get_filename_and_charpos (s
, fullname
)
862 register int desc
, linenums_changed
= 0;
864 desc
= open_source_file (s
);
872 *fullname
= s
->fullname
;
873 if (s
->line_charpos
== 0) linenums_changed
= 1;
874 if (linenums_changed
) find_source_lines (s
, desc
);
876 return linenums_changed
;
879 /* Print text describing the full name of the source file S
880 and the line number LINE and its corresponding character position.
881 The text starts with two Ctrl-z so that the Emacs-GDB interface
884 MID_STATEMENT is nonzero if the PC is not at the beginning of that line.
886 Return 1 if successful, 0 if could not find the file. */
889 identify_source_line (s
, line
, mid_statement
, pc
)
895 if (s
->line_charpos
== 0)
896 get_filename_and_charpos (s
, (char **)NULL
);
897 if (s
->fullname
== 0)
899 if (line
> s
->nlines
)
900 /* Don't index off the end of the line_charpos array. */
902 annotate_source (s
->fullname
, line
, s
->line_charpos
[line
- 1],
905 current_source_line
= line
;
906 first_line_listed
= line
;
907 last_line_listed
= line
;
908 current_source_symtab
= s
;
912 /* Print source lines from the file of symtab S,
913 starting with line number LINE and stopping before line number STOPLINE. */
916 print_source_lines (s
, line
, stopline
, noerror
)
923 register FILE *stream
;
924 int nlines
= stopline
- line
;
926 /* Regardless of whether we can open the file, set current_source_symtab. */
927 current_source_symtab
= s
;
928 current_source_line
= line
;
929 first_line_listed
= line
;
931 desc
= open_source_file (s
);
935 char *name
= alloca (strlen (s
->filename
) + 100);
936 sprintf (name
, "%s:%d", s
->filename
, line
);
937 print_sys_errmsg (name
, errno
);
942 if (s
->line_charpos
== 0)
943 find_source_lines (s
, desc
);
945 if (line
< 1 || line
> s
->nlines
)
948 error ("Line number %d out of range; %s has %d lines.",
949 line
, s
->filename
, s
->nlines
);
952 if (lseek (desc
, s
->line_charpos
[line
- 1], 0) < 0)
955 perror_with_name (s
->filename
);
958 stream
= fdopen (desc
, FOPEN_RT
);
965 last_line_listed
= current_source_line
;
966 printf_filtered ("%d\t", current_source_line
++);
969 if (c
< 040 && c
!= '\t' && c
!= '\n' && c
!= '\r')
970 printf_filtered ("^%c", c
+ 0100);
972 printf_filtered ("^?");
974 printf_filtered ("%c", c
);
975 } while (c
!= '\n' && (c
= fgetc (stream
)) >= 0);
983 /* Print a list of files and line numbers which a user may choose from
984 in order to list a function which was specified ambiguously (as with
985 `list classname::overloadedfuncname', for example). The vector in
986 SALS provides the filenames and line numbers. */
989 ambiguous_line_spec (sals
)
990 struct symtabs_and_lines
*sals
;
994 for (i
= 0; i
< sals
->nelts
; ++i
)
995 printf_filtered("file: \"%s\", line number: %d\n",
996 sals
->sals
[i
].symtab
->filename
, sals
->sals
[i
].line
);
1000 list_command (arg
, from_tty
)
1004 struct symtabs_and_lines sals
, sals_end
;
1005 struct symtab_and_line sal
, sal_end
;
1011 int linenum_beg
= 0;
1014 if (!have_full_symbols () && !have_partial_symbols())
1015 error ("No symbol table is loaded. Use the \"file\" command.");
1017 /* Pull in a current source symtab if necessary */
1018 if (current_source_symtab
== 0 &&
1019 (arg
== 0 || arg
[0] == '+' || arg
[0] == '-'))
1020 select_source_symtab (0);
1022 /* "l" or "l +" lists next ten lines. */
1024 if (arg
== 0 || STREQ (arg
, "+"))
1026 if (current_source_symtab
== 0)
1027 error ("No default source file yet. Do \"help list\".");
1028 print_source_lines (current_source_symtab
, current_source_line
,
1029 current_source_line
+ lines_to_list
, 0);
1033 /* "l -" lists previous ten lines, the ones before the ten just listed. */
1034 if (STREQ (arg
, "-"))
1036 if (current_source_symtab
== 0)
1037 error ("No default source file yet. Do \"help list\".");
1038 print_source_lines (current_source_symtab
,
1039 max (first_line_listed
- lines_to_list
, 1),
1040 first_line_listed
, 0);
1044 /* Now if there is only one argument, decode it in SAL
1046 If there are two arguments, decode them in SAL and SAL_END
1047 and clear NO_END; however, if one of the arguments is blank,
1048 set DUMMY_BEG or DUMMY_END to record that fact. */
1055 sals
= decode_line_1 (&arg1
, 0, 0, 0, 0);
1057 if (! sals
.nelts
) return; /* C++ */
1060 ambiguous_line_spec (&sals
);
1069 /* Record whether the BEG arg is all digits. */
1071 for (p
= arg
; p
!= arg1
&& *p
>= '0' && *p
<= '9'; p
++);
1072 linenum_beg
= (p
== arg1
);
1074 while (*arg1
== ' ' || *arg1
== '\t')
1080 while (*arg1
== ' ' || *arg1
== '\t')
1087 sals_end
= decode_line_1 (&arg1
, 0, 0, 0, 0);
1089 sals_end
= decode_line_1 (&arg1
, 0, sal
.symtab
, sal
.line
, 0);
1090 if (sals_end
.nelts
== 0)
1092 if (sals_end
.nelts
> 1)
1094 ambiguous_line_spec (&sals_end
);
1095 free (sals_end
.sals
);
1098 sal_end
= sals_end
.sals
[0];
1099 free (sals_end
.sals
);
1104 error ("Junk at end of line specification.");
1106 if (!no_end
&& !dummy_beg
&& !dummy_end
1107 && sal
.symtab
!= sal_end
.symtab
)
1108 error ("Specified start and end are in different files.");
1109 if (dummy_beg
&& dummy_end
)
1110 error ("Two empty args do not say what lines to list.");
1112 /* if line was specified by address,
1113 first print exactly which line, and which file.
1114 In this case, sal.symtab == 0 means address is outside
1115 of all known source files, not that user failed to give a filename. */
1118 if (sal
.symtab
== 0)
1119 /* FIXME-32x64--assumes sal.pc fits in long. */
1120 error ("No source file for address %s.",
1121 local_hex_string((unsigned long) sal
.pc
));
1122 sym
= find_pc_function (sal
.pc
);
1125 print_address_numeric (sal
.pc
, 1, gdb_stdout
);
1126 printf_filtered (" is in ");
1127 fputs_filtered (SYMBOL_SOURCE_NAME (sym
), gdb_stdout
);
1128 printf_filtered (" (%s:%d).\n", sal
.symtab
->filename
, sal
.line
);
1132 print_address_numeric (sal
.pc
, 1, gdb_stdout
);
1133 printf_filtered (" is at %s:%d.\n",
1134 sal
.symtab
->filename
, sal
.line
);
1138 /* If line was not specified by just a line number,
1139 and it does not imply a symtab, it must be an undebuggable symbol
1140 which means no source code. */
1142 if (! linenum_beg
&& sal
.symtab
== 0)
1143 error ("No line number known for %s.", arg
);
1145 /* If this command is repeated with RET,
1146 turn it into the no-arg variant. */
1151 if (dummy_beg
&& sal_end
.symtab
== 0)
1152 error ("No default source file yet. Do \"help list\".");
1154 print_source_lines (sal_end
.symtab
,
1155 max (sal_end
.line
- (lines_to_list
- 1), 1),
1156 sal_end
.line
+ 1, 0);
1157 else if (sal
.symtab
== 0)
1158 error ("No default source file yet. Do \"help list\".");
1160 print_source_lines (sal
.symtab
,
1161 max (sal
.line
- (lines_to_list
/ 2), 1),
1162 sal
.line
+ (lines_to_list
/ 2), 0);
1164 print_source_lines (sal
.symtab
, sal
.line
,
1166 ? sal
.line
+ lines_to_list
1167 : sal_end
.line
+ 1),
1171 /* Print info on range of pc's in a specified line. */
1174 line_info (arg
, from_tty
)
1178 struct symtabs_and_lines sals
;
1179 struct symtab_and_line sal
;
1180 CORE_ADDR start_pc
, end_pc
;
1183 INIT_SAL (&sal
); /* initialize to zeroes */
1187 sal
.symtab
= current_source_symtab
;
1188 sal
.line
= last_line_listed
;
1190 sals
.sals
= (struct symtab_and_line
*)
1191 xmalloc (sizeof (struct symtab_and_line
));
1196 sals
= decode_line_spec_1 (arg
, 0);
1201 /* C++ More than one line may have been specified, as when the user
1202 specifies an overloaded function name. Print info on them all. */
1203 for (i
= 0; i
< sals
.nelts
; i
++)
1207 if (sal
.symtab
== 0)
1209 printf_filtered ("No line number information available");
1212 /* This is useful for "info line *0x7f34". If we can't tell the
1213 user about a source line, at least let them have the symbolic
1215 printf_filtered (" for address ");
1217 print_address (sal
.pc
, gdb_stdout
);
1220 printf_filtered (".");
1221 printf_filtered ("\n");
1223 else if (sal
.line
> 0
1224 && find_line_pc_range (sal
, &start_pc
, &end_pc
))
1226 if (start_pc
== end_pc
)
1228 printf_filtered ("Line %d of \"%s\"",
1229 sal
.line
, sal
.symtab
->filename
);
1231 printf_filtered (" is at address ");
1232 print_address (start_pc
, gdb_stdout
);
1234 printf_filtered (" but contains no code.\n");
1238 printf_filtered ("Line %d of \"%s\"",
1239 sal
.line
, sal
.symtab
->filename
);
1241 printf_filtered (" starts at address ");
1242 print_address (start_pc
, gdb_stdout
);
1244 printf_filtered (" and ends at ");
1245 print_address (end_pc
, gdb_stdout
);
1246 printf_filtered (".\n");
1249 /* x/i should display this line's code. */
1250 set_next_address (start_pc
);
1252 /* Repeating "info line" should do the following line. */
1253 last_line_listed
= sal
.line
+ 1;
1255 /* If this is the only line, show the source code. If it could
1256 not find the file, don't do anything special. */
1257 if (annotation_level
&& sals
.nelts
== 1)
1258 identify_source_line (sal
.symtab
, sal
.line
, 0, start_pc
);
1261 /* Is there any case in which we get here, and have an address
1262 which the user would want to see? If we have debugging symbols
1263 and no line numbers? */
1264 printf_filtered ("Line number %d is out of range for \"%s\".\n",
1265 sal
.line
, sal
.symtab
->filename
);
1270 /* Commands to search the source file for a regexp. */
1274 forward_search_command (regex
, from_tty
)
1280 register FILE *stream
;
1281 int line
= last_line_listed
+ 1;
1284 msg
= (char *) re_comp (regex
);
1288 if (current_source_symtab
== 0)
1289 select_source_symtab (0);
1291 /* Search from last_line_listed+1 in current_source_symtab */
1293 desc
= open_source_file (current_source_symtab
);
1295 perror_with_name (current_source_symtab
->filename
);
1297 if (current_source_symtab
->line_charpos
== 0)
1298 find_source_lines (current_source_symtab
, desc
);
1300 if (line
< 1 || line
> current_source_symtab
->nlines
)
1303 error ("Expression not found");
1306 if (lseek (desc
, current_source_symtab
->line_charpos
[line
- 1], 0) < 0)
1309 perror_with_name (current_source_symtab
->filename
);
1312 stream
= fdopen (desc
, FOPEN_RT
);
1315 static char *buf
= NULL
;
1317 int cursize
, newsize
;
1320 buf
= xmalloc (cursize
);
1328 if (p
- buf
== cursize
)
1330 newsize
= cursize
+ cursize
/ 2;
1331 buf
= xrealloc (buf
, newsize
);
1335 } while (c
!= '\n' && (c
= getc (stream
)) >= 0);
1337 /* we now have a source line in buf, null terminate and match */
1339 if (re_exec (buf
) > 0)
1343 print_source_lines (current_source_symtab
, line
, line
+1, 0);
1344 set_internalvar (lookup_internalvar ("_"),
1345 value_from_longest (builtin_type_int
,
1347 current_source_line
= max (line
- lines_to_list
/ 2, 1);
1353 printf_filtered ("Expression not found\n");
1359 reverse_search_command (regex
, from_tty
)
1365 register FILE *stream
;
1366 int line
= last_line_listed
- 1;
1369 msg
= (char *) re_comp (regex
);
1373 if (current_source_symtab
== 0)
1374 select_source_symtab (0);
1376 /* Search from last_line_listed-1 in current_source_symtab */
1378 desc
= open_source_file (current_source_symtab
);
1380 perror_with_name (current_source_symtab
->filename
);
1382 if (current_source_symtab
->line_charpos
== 0)
1383 find_source_lines (current_source_symtab
, desc
);
1385 if (line
< 1 || line
> current_source_symtab
->nlines
)
1388 error ("Expression not found");
1391 if (lseek (desc
, current_source_symtab
->line_charpos
[line
- 1], 0) < 0)
1394 perror_with_name (current_source_symtab
->filename
);
1397 stream
= fdopen (desc
, FOPEN_RT
);
1401 /* FIXME!!! We walk right off the end of buf if we get a long line!!! */
1402 char buf
[4096]; /* Should be reasonable??? */
1403 register char *p
= buf
;
1410 } while (c
!= '\n' && (c
= getc (stream
)) >= 0);
1412 /* We now have a source line in buf; null terminate and match. */
1414 if (re_exec (buf
) > 0)
1418 print_source_lines (current_source_symtab
,
1420 set_internalvar (lookup_internalvar ("_"),
1421 value_from_longest (builtin_type_int
,
1423 current_source_line
= max (line
- lines_to_list
/ 2, 1);
1427 if (fseek (stream
, current_source_symtab
->line_charpos
[line
- 1], 0) < 0)
1430 perror_with_name (current_source_symtab
->filename
);
1434 printf_filtered ("Expression not found\n");
1440 _initialize_source ()
1442 struct cmd_list_element
*c
;
1443 current_source_symtab
= 0;
1444 init_source_path ();
1446 /* The intention is to use POSIX Basic Regular Expressions.
1447 Always use the GNU regex routine for consistency across all hosts.
1448 Our current GNU regex.c does not have all the POSIX features, so this is
1449 just an approximation. */
1450 re_set_syntax (RE_SYNTAX_GREP
);
1452 c
= add_cmd ("directory", class_files
, directory_command
,
1453 "Add directory DIR to beginning of search path for source files.\n\
1454 Forget cached info on source file locations and line positions.\n\
1455 DIR can also be $cwd for the current working directory, or $cdir for the\n\
1456 directory in which the source file was compiled into object code.\n\
1457 With no argument, reset the search path to $cdir:$cwd, the default.",
1459 c
->completer
= filename_completer
;
1461 add_cmd ("directories", no_class
, show_directories
,
1462 "Current search path for finding source files.\n\
1463 $cwd in the path means the current working directory.\n\
1464 $cdir in the path means the compilation directory of the source file.",
1467 add_info ("source", source_info
,
1468 "Information about the current source file.");
1470 add_info ("line", line_info
,
1471 concat ("Core addresses of the code for a source line.\n\
1472 Line can be specified as\n\
1473 LINENUM, to list around that line in current file,\n\
1474 FILE:LINENUM, to list around that line in that file,\n\
1475 FUNCTION, to list around beginning of that function,\n\
1476 FILE:FUNCTION, to distinguish among like-named static functions.\n\
1478 Default is to describe the last source line that was listed.\n\n\
1479 This sets the default address for \"x\" to the line's first instruction\n\
1480 so that \"x/i\" suffices to start examining the machine code.\n\
1481 The address is also stored as the value of \"$_\".", NULL
));
1483 add_com ("forward-search", class_files
, forward_search_command
,
1484 "Search for regular expression (see regex(3)) from last line listed.\n\
1485 The matching line number is also stored as the value of \"$_\".");
1486 add_com_alias ("search", "forward-search", class_files
, 0);
1488 add_com ("reverse-search", class_files
, reverse_search_command
,
1489 "Search backward for regular expression (see regex(3)) from last line listed.\n\
1490 The matching line number is also stored as the value of \"$_\".");
1492 add_com ("list", class_files
, list_command
,
1493 concat ("List specified function or line.\n\
1494 With no argument, lists ten more lines after or around previous listing.\n\
1495 \"list -\" lists the ten lines before a previous ten-line listing.\n\
1496 One argument specifies a line, and ten lines are listed around that line.\n\
1497 Two arguments with comma between specify starting and ending lines to list.\n\
1499 Lines can be specified in these ways:\n\
1500 LINENUM, to list around that line in current file,\n\
1501 FILE:LINENUM, to list around that line in that file,\n\
1502 FUNCTION, to list around beginning of that function,\n\
1503 FILE:FUNCTION, to distinguish among like-named static functions.\n\
1504 *ADDRESS, to list around the line containing that address.\n\
1505 With two args if one is empty it stands for ten lines away from the other arg.", NULL
));
1507 add_com_alias ("l", "list", class_files
, 1);
1510 (add_set_cmd ("listsize", class_support
, var_uinteger
,
1511 (char *)&lines_to_list
,
1512 "Set number of source lines gdb will list by default.",
This page took 0.065504 seconds and 4 git commands to generate.