1 /* List lines of source files for GDB, the GNU debugger.
2 Copyright 1986, 1987, 1988, 1989, 1991, 1992, 1993, 1994
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., 675 Mass Ave, Cambridge, MA 02139, USA. */
23 #include "expression.h"
29 #include <sys/types.h>
31 #include <sys/param.h>
40 #ifndef DIRNAME_SEPARATOR
41 #define DIRNAME_SEPARATOR ':'
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 /* Sigh. "foo/" => "foo" */
318 /* "." => getwd (). */
319 name
= current_directory
;
322 else if (p
[-2] == '/')
332 /* "...foo/." => "...foo". */
343 name
= tilde_expand (name
);
344 else if (name
[0] != '/' && name
[0] != '$')
345 name
= concat (current_directory
, "/", name
, NULL
);
347 name
= savestring (name
, p
- name
);
348 make_cleanup (free
, name
);
350 /* Unless it's a variable, check existence. */
351 if (name
[0] != '$') {
352 /* These are warnings, not errors, since we don't want a
353 non-existent directory in a .gdbinit file to stop processing
354 of the .gdbinit file.
356 Whether they get added to the path is more debatable. Current
357 answer is yes, in case the user wants to go make the directory
358 or whatever. If the directory continues to not exist/not be
359 a directory/etc, then having them in the path should be
361 if (stat (name
, &st
) < 0)
363 int save_errno
= errno
;
364 fprintf_unfiltered (gdb_stderr
, "Warning: ");
365 print_sys_errmsg (name
, save_errno
);
367 else if ((st
.st_mode
& S_IFMT
) != S_IFDIR
)
368 warning ("%s is not a directory.", name
);
373 register unsigned int len
= strlen (name
);
378 if (!strncmp (p
, name
, len
)
379 && (p
[len
] == '\0' || p
[len
] == DIRNAME_SEPARATOR
))
381 /* Found it in the search path, remove old copy */
383 p
--; /* Back over leading separator */
384 if (prefix
> p
- *which_path
)
385 goto skip_dup
; /* Same dir twice in one cmd */
386 strcpy (p
, &p
[len
+1]); /* Copy from next \0 or : */
388 p
= strchr (p
, DIRNAME_SEPARATOR
);
398 tinybuf
[0] = DIRNAME_SEPARATOR
;
401 /* 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. */
408 temp
= concat (old
, tinybuf
, name
, NULL
);
410 *which_path
= concat (temp
, "", &old
[prefix
], NULL
);
411 prefix
= strlen (temp
);
416 *which_path
= concat (name
, (old
[0] ? tinybuf
: old
), old
, NULL
);
417 prefix
= strlen (name
);
424 } while (*dirname
!= '\0');
429 source_info (ignore
, from_tty
)
433 register struct symtab
*s
= current_source_symtab
;
437 printf_filtered("No current source file.\n");
440 printf_filtered ("Current source file is %s\n", s
->filename
);
442 printf_filtered ("Compilation directory is %s\n", s
->dirname
);
444 printf_filtered ("Located in %s\n", s
->fullname
);
446 printf_filtered ("Contains %d line%s.\n", s
->nlines
,
447 s
->nlines
== 1 ? "" : "s");
449 printf_filtered("Source language is %s.\n", language_str (s
->language
));
454 /* Open a file named STRING, searching path PATH (dir names sep by some char)
455 using mode MODE and protection bits PROT in the calls to open.
457 If TRY_CWD_FIRST, try to open ./STRING before searching PATH.
458 (ie pretend the first element of PATH is "."). This also indicates
459 that a slash in STRING disables searching of the path (this is
460 so that "exec-file ./foo" or "symbol-file ./foo" insures that you
461 get that particular version of foo or an error message).
463 If FILENAMED_OPENED is non-null, set it to a newly allocated string naming
464 the actual file opened (this string will always start with a "/". We
465 have to take special pains to avoid doubling the "/" between the directory
466 and the file, sigh! Emacs gets confuzzed by this when we print the
469 If a file is found, return the descriptor.
470 Otherwise, return -1, with errno set for the last name we tried to open. */
472 /* >>>> This should only allow files of certain types,
473 >>>> eg executable, non-directory */
475 openp (path
, try_cwd_first
, string
, mode
, prot
, filename_opened
)
481 char **filename_opened
;
484 register char *filename
;
485 register char *p
, *p1
;
492 if (try_cwd_first
|| string
[0] == '/')
495 fd
= open (filename
, mode
, prot
);
496 if (fd
>= 0 || string
[0] == '/' || strchr (string
, '/'))
501 while (string
[0] == '.' && string
[1] == '/')
504 alloclen
= strlen (path
) + strlen (string
) + 2;
505 filename
= (char *) alloca (alloclen
);
507 for (p
= path
; p
; p
= p1
? p1
+ 1 : 0)
509 p1
= (char *) strchr (p
, DIRNAME_SEPARATOR
);
515 if (len
== 4 && p
[0] == '$' && p
[1] == 'c'
516 && p
[2] == 'w' && p
[3] == 'd') {
517 /* Name is $cwd -- insert current directory name instead. */
520 /* First, realloc the filename buffer if too short. */
521 len
= strlen (current_directory
);
522 newlen
= len
+ strlen (string
) + 2;
523 if (newlen
> alloclen
) {
525 filename
= (char *) alloca (alloclen
);
527 strcpy (filename
, current_directory
);
529 /* Normal file name in path -- just use it. */
530 strncpy (filename
, p
, len
);
534 /* Remove trailing slashes */
535 while (len
> 0 && filename
[len
-1] == '/')
538 strcat (filename
+len
, "/");
539 strcat (filename
, string
);
541 fd
= open (filename
, mode
);
549 *filename_opened
= (char *) 0;
550 else if (filename
[0] == '/')
551 *filename_opened
= savestring (filename
, strlen (filename
));
554 /* Beware the // my son, the Emacs barfs, the botch that catch... */
556 *filename_opened
= concat (current_directory
,
557 '/' == current_directory
[strlen(current_directory
)-1]? "": "/",
561 /* start-sanitize-mpw */
564 printf("openp on %s, path %s mode %d prot %d\n returned %d",
565 string
, path
, mode
, prot
, fd
);
566 if (*filename_opened
)
567 printf(" (filename is %s)", *filename_opened
);
571 /* end-sanitize-mpw */
576 /* Open a source file given a symtab S. Returns a file descriptor or
577 negative number for error. */
583 char *path
= source_path
;
588 /* Quick way out if we already know its full name */
591 result
= open (s
->fullname
, O_RDONLY
);
594 /* Didn't work -- free old one, try again. */
595 mfree (s
->objfile
->md
, s
->fullname
);
599 if (s
->dirname
!= NULL
)
601 /* Replace a path entry of $cdir with the compilation directory name */
603 /* We cast strstr's result in case an ANSIhole has made it const,
604 which produces a "required warning" when assigned to a nonconst. */
605 p
= (char *)strstr (source_path
, "$cdir");
606 if (p
&& (p
== path
|| p
[-1] == DIRNAME_SEPARATOR
)
607 && (p
[cdir_len
] == DIRNAME_SEPARATOR
|| p
[cdir_len
] == '\0'))
612 alloca (strlen (source_path
) + 1 + strlen (s
->dirname
) + 1);
613 len
= p
- source_path
;
614 strncpy (path
, source_path
, len
); /* Before $cdir */
615 strcpy (path
+ len
, s
->dirname
); /* new stuff */
616 strcat (path
+ len
, source_path
+ len
+ cdir_len
); /* After $cdir */
620 result
= openp (path
, 0, s
->filename
, O_RDONLY
, 0, &s
->fullname
);
623 /* Didn't work. Try using just the basename. */
624 p
= basename (s
->filename
);
625 if (p
!= s
->filename
)
626 result
= openp (path
, 0, p
, O_RDONLY
, 0, &s
->fullname
);
628 /* start-sanitize-mpw */
632 /* Didn't work. Try using just the MPW basename. */
633 p
= (char *) mpw_basename (s
->filename
);
634 if (p
!= s
->filename
)
635 result
= openp (path
, 0, p
, O_RDONLY
, 0, &s
->fullname
);
639 /* Didn't work. Try using the mixed Unix/MPW basename. */
640 p
= (char *) mpw_mixed_basename (s
->filename
);
641 if (p
!= s
->filename
)
642 result
= openp (path
, 0, p
, O_RDONLY
, 0, &s
->fullname
);
645 /* end-sanitize-mpw */
648 fullname
= s
->fullname
;
649 s
->fullname
= mstrsave (s
->objfile
->md
, s
->fullname
);
655 /* Return the path to the source file associated with symtab. Returns NULL
659 symtab_to_filename (s
)
667 /* If we've seen the file before, just return fullname. */
672 /* Try opening the file to setup fullname */
674 fd
= open_source_file (s
);
676 return s
->filename
; /* File not found. Just use short name */
678 /* Found the file. Cleanup and return the full name */
685 /* Create and initialize the table S->line_charpos that records
686 the positions of the lines in the source file, which is assumed
687 to be open on descriptor DESC.
688 All set S->nlines to the number of such lines. */
691 find_source_lines (s
, desc
)
696 register char *data
, *p
, *end
;
698 int lines_allocated
= 1000;
703 line_charpos
= (int *) xmmalloc (s
-> objfile
-> md
,
704 lines_allocated
* sizeof (int));
705 if (fstat (desc
, &st
) < 0)
706 perror_with_name (s
->filename
);
710 exec_mtime
= bfd_get_mtime(exec_bfd
);
711 if (exec_mtime
&& exec_mtime
< st
.st_mtime
)
712 printf_filtered ("Source file is more recent than executable.\n");
715 #ifdef LSEEK_NOT_LINEAR
719 /* Have to read it byte by byte to find out where the chars live */
721 line_charpos
[0] = tell(desc
);
723 while (myread(desc
, &c
, 1)>0)
727 if (nlines
== lines_allocated
)
729 lines_allocated
*= 2;
731 (int *) xmrealloc (s
-> objfile
-> md
, (char *) line_charpos
,
732 sizeof (int) * lines_allocated
);
734 line_charpos
[nlines
++] = tell(desc
);
738 #else /* lseek linear. */
740 struct cleanup
*old_cleanups
;
742 /* st_size might be a large type, but we only support source files whose
743 size fits in an int. */
744 size
= (int) st
.st_size
;
746 /* Use malloc, not alloca, because this may be pretty large, and we may
747 run into various kinds of limits on stack size. */
748 data
= (char *) xmalloc (size
);
749 old_cleanups
= make_cleanup (free
, data
);
751 if (myread (desc
, data
, size
) < 0)
752 perror_with_name (s
->filename
);
760 /* A newline at the end does not start a new line. */
763 if (nlines
== lines_allocated
)
765 lines_allocated
*= 2;
767 (int *) xmrealloc (s
-> objfile
-> md
, (char *) line_charpos
,
768 sizeof (int) * lines_allocated
);
770 line_charpos
[nlines
++] = p
- data
;
773 do_cleanups (old_cleanups
);
775 #endif /* lseek linear. */
778 (int *) xmrealloc (s
-> objfile
-> md
, (char *) line_charpos
,
779 nlines
* sizeof (int));
783 /* Return the character position of a line LINE in symtab S.
784 Return 0 if anything is invalid. */
786 #if 0 /* Currently unused */
789 source_line_charpos (s
, line
)
794 if (!s
->line_charpos
|| line
<= 0) return 0;
795 if (line
> s
->nlines
)
797 return s
->line_charpos
[line
- 1];
800 /* Return the line number of character position POS in symtab S. */
803 source_charpos_line (s
, chr
)
804 register struct symtab
*s
;
807 register int line
= 0;
810 if (s
== 0 || s
->line_charpos
== 0) return 0;
811 lnp
= s
->line_charpos
;
812 /* Files are usually short, so sequential search is Ok */
813 while (line
< s
->nlines
&& *lnp
<= chr
)
818 if (line
>= s
->nlines
)
826 /* Get full pathname and line number positions for a symtab.
827 Return nonzero if line numbers may have changed.
828 Set *FULLNAME to actual name of the file as found by `openp',
829 or to 0 if the file is not found. */
832 get_filename_and_charpos (s
, fullname
)
836 register int desc
, linenums_changed
= 0;
838 desc
= open_source_file (s
);
846 *fullname
= s
->fullname
;
847 if (s
->line_charpos
== 0) linenums_changed
= 1;
848 if (linenums_changed
) find_source_lines (s
, desc
);
850 return linenums_changed
;
853 /* Print text describing the full name of the source file S
854 and the line number LINE and its corresponding character position.
855 The text starts with two Ctrl-z so that the Emacs-GDB interface
858 MID_STATEMENT is nonzero if the PC is not at the beginning of that line.
860 Return 1 if successful, 0 if could not find the file. */
863 identify_source_line (s
, line
, mid_statement
, pc
)
869 if (s
->line_charpos
== 0)
870 get_filename_and_charpos (s
, (char **)NULL
);
871 if (s
->fullname
== 0)
873 if (line
> s
->nlines
)
874 /* Don't index off the end of the line_charpos array. */
876 annotate_source (s
->fullname
, line
, s
->line_charpos
[line
- 1],
879 current_source_line
= line
;
880 first_line_listed
= line
;
881 last_line_listed
= line
;
882 current_source_symtab
= s
;
886 /* Print source lines from the file of symtab S,
887 starting with line number LINE and stopping before line number STOPLINE. */
890 print_source_lines (s
, line
, stopline
, noerror
)
897 register FILE *stream
;
898 int nlines
= stopline
- line
;
900 /* Regardless of whether we can open the file, set current_source_symtab. */
901 current_source_symtab
= s
;
902 current_source_line
= line
;
903 first_line_listed
= line
;
905 desc
= open_source_file (s
);
909 char *name
= alloca (strlen (s
->filename
) + 100);
910 sprintf (name
, "%s:%d", s
->filename
, line
);
911 print_sys_errmsg (name
, errno
);
916 if (s
->line_charpos
== 0)
917 find_source_lines (s
, desc
);
919 if (line
< 1 || line
> s
->nlines
)
922 error ("Line number %d out of range; %s has %d lines.",
923 line
, s
->filename
, s
->nlines
);
926 if (lseek (desc
, s
->line_charpos
[line
- 1], 0) < 0)
929 perror_with_name (s
->filename
);
932 stream
= fdopen (desc
, FOPEN_RT
);
939 last_line_listed
= current_source_line
;
940 printf_filtered ("%d\t", current_source_line
++);
943 if (c
< 040 && c
!= '\t' && c
!= '\n' && c
!= '\r')
944 printf_filtered ("^%c", c
+ 0100);
946 printf_filtered ("^?");
948 printf_filtered ("%c", c
);
949 } while (c
!= '\n' && (c
= fgetc (stream
)) >= 0);
957 /* Print a list of files and line numbers which a user may choose from
958 in order to list a function which was specified ambiguously (as with
959 `list classname::overloadedfuncname', for example). The vector in
960 SALS provides the filenames and line numbers. */
963 ambiguous_line_spec (sals
)
964 struct symtabs_and_lines
*sals
;
968 for (i
= 0; i
< sals
->nelts
; ++i
)
969 printf_filtered("file: \"%s\", line number: %d\n",
970 sals
->sals
[i
].symtab
->filename
, sals
->sals
[i
].line
);
974 list_command (arg
, from_tty
)
978 struct symtabs_and_lines sals
, sals_end
;
979 struct symtab_and_line sal
, sal_end
;
988 if (!have_full_symbols () && !have_partial_symbols())
989 error ("No symbol table is loaded. Use the \"file\" command.");
991 /* Pull in a current source symtab if necessary */
992 if (current_source_symtab
== 0 &&
993 (arg
== 0 || arg
[0] == '+' || arg
[0] == '-'))
994 select_source_symtab (0);
996 /* "l" or "l +" lists next ten lines. */
998 if (arg
== 0 || STREQ (arg
, "+"))
1000 if (current_source_symtab
== 0)
1001 error ("No default source file yet. Do \"help list\".");
1002 print_source_lines (current_source_symtab
, current_source_line
,
1003 current_source_line
+ lines_to_list
, 0);
1007 /* "l -" lists previous ten lines, the ones before the ten just listed. */
1008 if (STREQ (arg
, "-"))
1010 if (current_source_symtab
== 0)
1011 error ("No default source file yet. Do \"help list\".");
1012 print_source_lines (current_source_symtab
,
1013 max (first_line_listed
- lines_to_list
, 1),
1014 first_line_listed
, 0);
1018 /* Now if there is only one argument, decode it in SAL
1020 If there are two arguments, decode them in SAL and SAL_END
1021 and clear NO_END; however, if one of the arguments is blank,
1022 set DUMMY_BEG or DUMMY_END to record that fact. */
1029 sals
= decode_line_1 (&arg1
, 0, 0, 0, 0);
1031 if (! sals
.nelts
) return; /* C++ */
1034 ambiguous_line_spec (&sals
);
1043 /* Record whether the BEG arg is all digits. */
1045 for (p
= arg
; p
!= arg1
&& *p
>= '0' && *p
<= '9'; p
++);
1046 linenum_beg
= (p
== arg1
);
1048 while (*arg1
== ' ' || *arg1
== '\t')
1054 while (*arg1
== ' ' || *arg1
== '\t')
1061 sals_end
= decode_line_1 (&arg1
, 0, 0, 0, 0);
1063 sals_end
= decode_line_1 (&arg1
, 0, sal
.symtab
, sal
.line
, 0);
1064 if (sals_end
.nelts
== 0)
1066 if (sals_end
.nelts
> 1)
1068 ambiguous_line_spec (&sals_end
);
1069 free (sals_end
.sals
);
1072 sal_end
= sals_end
.sals
[0];
1073 free (sals_end
.sals
);
1078 error ("Junk at end of line specification.");
1080 if (!no_end
&& !dummy_beg
&& !dummy_end
1081 && sal
.symtab
!= sal_end
.symtab
)
1082 error ("Specified start and end are in different files.");
1083 if (dummy_beg
&& dummy_end
)
1084 error ("Two empty args do not say what lines to list.");
1086 /* if line was specified by address,
1087 first print exactly which line, and which file.
1088 In this case, sal.symtab == 0 means address is outside
1089 of all known source files, not that user failed to give a filename. */
1092 if (sal
.symtab
== 0)
1093 /* FIXME-32x64--assumes sal.pc fits in long. */
1094 error ("No source file for address %s.",
1095 local_hex_string((unsigned long) sal
.pc
));
1096 sym
= find_pc_function (sal
.pc
);
1099 print_address_numeric (sal
.pc
, 1, gdb_stdout
);
1100 printf_filtered (" is in ");
1101 fputs_filtered (SYMBOL_SOURCE_NAME (sym
), gdb_stdout
);
1102 printf_filtered (" (%s:%d).\n", sal
.symtab
->filename
, sal
.line
);
1106 print_address_numeric (sal
.pc
, 1, gdb_stdout
);
1107 printf_filtered (" is at %s:%d.\n",
1108 sal
.symtab
->filename
, sal
.line
);
1112 /* If line was not specified by just a line number,
1113 and it does not imply a symtab, it must be an undebuggable symbol
1114 which means no source code. */
1116 if (! linenum_beg
&& sal
.symtab
== 0)
1117 error ("No line number known for %s.", arg
);
1119 /* If this command is repeated with RET,
1120 turn it into the no-arg variant. */
1125 if (dummy_beg
&& sal_end
.symtab
== 0)
1126 error ("No default source file yet. Do \"help list\".");
1128 print_source_lines (sal_end
.symtab
,
1129 max (sal_end
.line
- (lines_to_list
- 1), 1),
1130 sal_end
.line
+ 1, 0);
1131 else if (sal
.symtab
== 0)
1132 error ("No default source file yet. Do \"help list\".");
1134 print_source_lines (sal
.symtab
,
1135 max (sal
.line
- (lines_to_list
/ 2), 1),
1136 sal
.line
+ (lines_to_list
/ 2), 0);
1138 print_source_lines (sal
.symtab
, sal
.line
,
1140 ? sal
.line
+ lines_to_list
1141 : sal_end
.line
+ 1),
1145 /* Print info on range of pc's in a specified line. */
1148 line_info (arg
, from_tty
)
1152 struct symtabs_and_lines sals
;
1153 struct symtab_and_line sal
;
1154 CORE_ADDR start_pc
, end_pc
;
1159 sal
.symtab
= current_source_symtab
;
1160 sal
.line
= last_line_listed
;
1162 sals
.sals
= (struct symtab_and_line
*)
1163 xmalloc (sizeof (struct symtab_and_line
));
1168 sals
= decode_line_spec_1 (arg
, 0);
1173 /* C++ More than one line may have been specified, as when the user
1174 specifies an overloaded function name. Print info on them all. */
1175 for (i
= 0; i
< sals
.nelts
; i
++)
1179 if (sal
.symtab
== 0)
1181 printf_filtered ("No line number information available");
1184 /* This is useful for "info line *0x7f34". If we can't tell the
1185 user about a source line, at least let them have the symbolic
1187 printf_filtered (" for address ");
1189 print_address (sal
.pc
, gdb_stdout
);
1192 printf_filtered (".");
1193 printf_filtered ("\n");
1195 else if (sal
.line
> 0
1196 && find_line_pc_range (sal
, &start_pc
, &end_pc
))
1198 if (start_pc
== end_pc
)
1200 printf_filtered ("Line %d of \"%s\"",
1201 sal
.line
, sal
.symtab
->filename
);
1203 printf_filtered (" is at address ");
1204 print_address (start_pc
, gdb_stdout
);
1206 printf_filtered (" but contains no code.\n");
1210 printf_filtered ("Line %d of \"%s\"",
1211 sal
.line
, sal
.symtab
->filename
);
1213 printf_filtered (" starts at address ");
1214 print_address (start_pc
, gdb_stdout
);
1216 printf_filtered (" and ends at ");
1217 print_address (end_pc
, gdb_stdout
);
1218 printf_filtered (".\n");
1221 /* x/i should display this line's code. */
1222 set_next_address (start_pc
);
1224 /* Repeating "info line" should do the following line. */
1225 last_line_listed
= sal
.line
+ 1;
1227 /* If this is the only line, show the source code. If it could
1228 not find the file, don't do anything special. */
1229 if (annotation_level
&& sals
.nelts
== 1)
1230 identify_source_line (sal
.symtab
, sal
.line
, 0, start_pc
);
1233 /* Is there any case in which we get here, and have an address
1234 which the user would want to see? If we have debugging symbols
1235 and no line numbers? */
1236 printf_filtered ("Line number %d is out of range for \"%s\".\n",
1237 sal
.line
, sal
.symtab
->filename
);
1242 /* Commands to search the source file for a regexp. */
1246 forward_search_command (regex
, from_tty
)
1252 register FILE *stream
;
1253 int line
= last_line_listed
+ 1;
1256 msg
= (char *) re_comp (regex
);
1260 if (current_source_symtab
== 0)
1261 select_source_symtab (0);
1263 /* Search from last_line_listed+1 in current_source_symtab */
1265 desc
= open_source_file (current_source_symtab
);
1267 perror_with_name (current_source_symtab
->filename
);
1269 if (current_source_symtab
->line_charpos
== 0)
1270 find_source_lines (current_source_symtab
, desc
);
1272 if (line
< 1 || line
> current_source_symtab
->nlines
)
1275 error ("Expression not found");
1278 if (lseek (desc
, current_source_symtab
->line_charpos
[line
- 1], 0) < 0)
1281 perror_with_name (current_source_symtab
->filename
);
1284 stream
= fdopen (desc
, FOPEN_RT
);
1287 static char *buf
= NULL
;
1289 int cursize
, newsize
;
1292 buf
= xmalloc (cursize
);
1300 if (p
- buf
== cursize
)
1302 newsize
= cursize
+ cursize
/ 2;
1303 buf
= xrealloc (buf
, newsize
);
1307 } while (c
!= '\n' && (c
= getc (stream
)) >= 0);
1309 /* we now have a source line in buf, null terminate and match */
1311 if (re_exec (buf
) > 0)
1315 print_source_lines (current_source_symtab
, line
, line
+1, 0);
1316 current_source_line
= max (line
- lines_to_list
/ 2, 1);
1322 printf_filtered ("Expression not found\n");
1328 reverse_search_command (regex
, from_tty
)
1334 register FILE *stream
;
1335 int line
= last_line_listed
- 1;
1338 msg
= (char *) re_comp (regex
);
1342 if (current_source_symtab
== 0)
1343 select_source_symtab (0);
1345 /* Search from last_line_listed-1 in current_source_symtab */
1347 desc
= open_source_file (current_source_symtab
);
1349 perror_with_name (current_source_symtab
->filename
);
1351 if (current_source_symtab
->line_charpos
== 0)
1352 find_source_lines (current_source_symtab
, desc
);
1354 if (line
< 1 || line
> current_source_symtab
->nlines
)
1357 error ("Expression not found");
1360 if (lseek (desc
, current_source_symtab
->line_charpos
[line
- 1], 0) < 0)
1363 perror_with_name (current_source_symtab
->filename
);
1366 stream
= fdopen (desc
, FOPEN_RT
);
1370 /* FIXME!!! We walk right off the end of buf if we get a long line!!! */
1371 char buf
[4096]; /* Should be reasonable??? */
1372 register char *p
= buf
;
1379 } while (c
!= '\n' && (c
= getc (stream
)) >= 0);
1381 /* We now have a source line in buf; null terminate and match. */
1383 if (re_exec (buf
) > 0)
1387 print_source_lines (current_source_symtab
,
1389 current_source_line
= max (line
- lines_to_list
/ 2, 1);
1393 if (fseek (stream
, current_source_symtab
->line_charpos
[line
- 1], 0) < 0)
1396 perror_with_name (current_source_symtab
->filename
);
1400 printf_filtered ("Expression not found\n");
1406 _initialize_source ()
1408 struct cmd_list_element
*c
;
1409 current_source_symtab
= 0;
1410 init_source_path ();
1412 /* The intention is to use POSIX Basic Regular Expressions.
1413 Always use the GNU regex routine for consistency across all hosts.
1414 Our current GNU regex.c does not have all the POSIX features, so this is
1415 just an approximation. */
1416 re_set_syntax (RE_SYNTAX_GREP
);
1418 c
= add_cmd ("directory", class_files
, directory_command
,
1419 "Add directory DIR to beginning of search path for source files.\n\
1420 Forget cached info on source file locations and line positions.\n\
1421 DIR can also be $cwd for the current working directory, or $cdir for the\n\
1422 directory in which the source file was compiled into object code.\n\
1423 With no argument, reset the search path to $cdir:$cwd, the default.",
1425 c
->completer
= filename_completer
;
1427 add_cmd ("directories", no_class
, show_directories
,
1428 "Current search path for finding source files.\n\
1429 $cwd in the path means the current working directory.\n\
1430 $cdir in the path means the compilation directory of the source file.",
1433 add_info ("source", source_info
,
1434 "Information about the current source file.");
1436 /* start-sanitize-mpw */
1438 /* end-sanitize-mpw */
1439 add_info ("line", line_info
,
1440 "Core addresses of the code for a source line.\n\
1441 Line can be specified as\n\
1442 LINENUM, to list around that line in current file,\n\
1443 FILE:LINENUM, to list around that line in that file,\n\
1444 FUNCTION, to list around beginning of that function,\n\
1445 FILE:FUNCTION, to distinguish among like-named static functions.\n\
1446 Default is to describe the last source line that was listed.\n\n\
1447 This sets the default address for \"x\" to the line's first instruction\n\
1448 so that \"x/i\" suffices to start examining the machine code.\n\
1449 The address is also stored as the value of \"$_\".");
1450 /* start-sanitize-mpw */
1452 add_info ("line", line_info
,
1453 "Core addresses of the code for a source line. \n\
1454 Line can be specified as \n\
1455 LINENUM, to list around that line in current file, \n\
1456 FILE:LINENUM, to list around that line in that file, \n\
1457 Default is to describe the last source line that was listed. \n\n\
1458 The address is also stored as the value of \"$_\". ");
1460 /* end-sanitize-mpw */
1462 add_com ("forward-search", class_files
, forward_search_command
,
1463 "Search for regular expression (see regex(3)) from last line listed.");
1464 add_com_alias ("search", "forward-search", class_files
, 0);
1466 add_com ("reverse-search", class_files
, reverse_search_command
,
1467 "Search backward for regular expression (see regex(3)) from last line listed.");
1469 /* start-sanitize-mpw */
1471 /* end-sanitize-mpw */
1472 add_com ("list", class_files
, list_command
,
1473 "List specified function or line.\n\
1474 With no argument, lists ten more lines after or around previous listing.\n\
1475 \"list -\" lists the ten lines before a previous ten-line listing.\n\
1476 One argument specifies a line, and ten lines are listed around that line.\n\
1477 Two arguments with comma between specify starting and ending lines to list.\n\
1478 Lines can be specified in these ways:\n\
1479 LINENUM, to list around that line in current file,\n\
1480 FILE:LINENUM, to list around that line in that file,\n\
1481 FUNCTION, to list around beginning of that function,\n\
1482 FILE:FUNCTION, to distinguish among like-named static functions.\n\
1483 *ADDRESS, to list around the line containing that address.\n\
1484 With two args if one is empty it stands for ten lines away from the other arg.");
1485 /* start-sanitize-mpw */
1487 add_com ("list", class_files
, list_command
,
1488 "List specified function or line.\n\
1489 With no argument, lists ten more lines after or around previous listing. \n\
1490 One argument specifies a line, and ten lines are listed around that line. \n\
1491 Two arguments with comma between specify starting and ending lines to list. \n\
1492 Lines can be specified in these ways:\n\
1493 With two args if one is empty it stands for ten lines away from the other arg. ");
1495 /* end-sanitize-mpw */
1496 add_com_alias ("l", "list", class_files
, 1);
1499 (add_set_cmd ("listsize", class_support
, var_uinteger
,
1500 (char *)&lines_to_list
,
1501 "Set number of source lines gdb will list by default.",
This page took 0.062945 seconds and 4 git commands to generate.