* This code is derived from software copyrighted by the Free Software
* Modified 1991 by Donn Seeley at UUNET Technologies, Inc.
* Modified 1990 by Van Jacobson at Lawrence Berkeley Laboratory.
static char sccsid
[] = "@(#)source.c 6.3 (Berkeley) 5/8/91";
/* List lines of source files for GDB, the GNU debugger.
Copyright (C) 1986, 1987, 1988, 1989 Free Software Foundation, Inc.
This file is part of GDB.
GDB is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 1, or (at your option)
GDB is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with GDB; see the file COPYING. If not, write to
the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
/* Path of directories to search for source files.
Same format as the PATH environment variable's value. */
static char *source_path
;
/* Symtab of default file for listing lines of. */
struct symtab
*current_source_symtab
;
/* Default next line to list. */
/* Line number of last line printed. Default for various commands.
current_source_line is usually, but not always, the same as this. */
static int last_line_listed
;
/* First line number listed by last listing command. */
static int first_line_listed
;
struct symtab
*psymtab_to_symtab ();
/* Set the source file default for the "list" command, specifying a
symtab. Sigh. Behaivior specification: If it is called with a
non-zero argument, that is the symtab to select. If it is not,
first lookup "main"; if it exists, use the symtab and line it
defines. If not, take the last symtab in the symtab_list (if it
exists) or the last symtab in the psytab_list (if *it* exists). If
none of this works, report an error. */
register struct symtab
*s
;
struct symtabs_and_lines sals
;
struct symtab_and_line sal
;
struct partial_symtab
*ps
, *cs_pst
;
current_source_symtab
= s
;
/* Make the default place to list be the function `main'
if (lookup_symbol ("main", 0, VAR_NAMESPACE
, 0))
sals
= decode_line_spec ("main", 1);
current_source_symtab
= sal
.symtab
;
current_source_line
= max (sal
.line
- 9, 1);
/* All right; find the last file in the symtab list (ignoring .h's). */
char *name
= s
->filename
;
if (! (len
> 2 && !strcmp (&name
[len
- 2], ".h")))
current_source_symtab
= s
;
else if (partial_symtab_list
)
ps
= partial_symtab_list
;
char *name
= ps
->filename
;
if (! (len
> 2 && !strcmp (&name
[len
- 2], ".h")))
fatal ("Internal: select_source_symtab: readin pst found and no symtabs.");
current_source_symtab
= psymtab_to_symtab (cs_pst
);
current_source_symtab
= 0;
printf ("Source directories searched: %s\n", source_path
);
register struct symtab
*s
;
source_path
= savestring (current_directory
, strlen (current_directory
));
/* Forget what we learned about line positions in source files;
must check again now since files may be found in
a different directory now. */
for (s
= symtab_list
; s
; s
= s
->next
)
if (s
->line_charpos
!= 0)
directory_command (dirname
, from_tty
)
if (query ("Reinitialize source path to %s? ", current_directory
))
dirname
= tilde_expand (dirname
);
make_cleanup (free
, dirname
);
char *colon
= index (name
, ':');
char *space
= index (name
, ' ');
char *tab
= index (name
, '\t');
if (colon
== 0 && space
== 0 && tab
== 0)
p
= dirname
= name
+ strlen (name
);
if (colon
!= 0 && (p
== 0 || colon
< p
))
if (space
!= 0 && (p
== 0 || space
< p
))
if (tab
!= 0 && (p
== 0 || tab
< p
))
while (*dirname
== ':' || *dirname
== ' ' || *dirname
== '\t')
/* Sigh. "foo/" => "foo" */
name
= current_directory
;
/* "...foo/." => "...foo". */
name
= concat (current_directory
, "/", name
);
name
= savestring (name
, p
- name
);
make_cleanup (free
, name
);
if (stat (name
, &st
) < 0)
if ((st
.st_mode
& S_IFMT
) != S_IFDIR
)
error ("%s is not a directory.", name
);
register unsigned int len
= strlen (name
);
if (!strncmp (p
, name
, len
)
&& (p
[len
] == '\0' || p
[len
] == ':'))
printf ("\"%s\" is already in the source path.\n", name
);
source_path
= concat (old
, ":", name
);
} while (*dirname
!= '\0');
/* Open a file named STRING, searching path PATH (dir names sep by colons)
using mode MODE and protection bits PROT in the calls to open.
If TRY_CWD_FIRST, try to open ./STRING before searching PATH.
(ie pretend the first element of PATH is ".")
If FILENAMED_OPENED is non-null, set it to a newly allocated string naming
the actual file opened (this string will always start with a "/"
If a file is found, return the descriptor.
Otherwise, return -1, with errno set for the last name we tried to open. */
/* >>>> This should only allow files of certain types,
>>>> eg executable, non-directory */
openp (path
, try_cwd_first
, string
, mode
, prot
, filename_opened
)
while (string
[0] == '.' && string
[1] == '/')
if (try_cwd_first
|| string
[0] == '/')
fd
= open (filename
, mode
, prot
);
if (fd
>= 0 || string
[0] == '/')
filename
= (char *) alloca (strlen (path
) + strlen (string
) + 2);
for (p
= path
; p
; p
= p1
? p1
+ 1 : 0)
p1
= (char *) index (p
, ':');
strncpy (filename
, p
, len
);
strcat (filename
, string
);
fd
= open (filename
, mode
, prot
);
*filename_opened
= (char *) 0;
else if (filename
[0] == '/')
*filename_opened
= savestring (filename
, strlen (filename
));
*filename_opened
= concat (current_directory
, "/", filename
);
/* Create and initialize the table S->line_charpos that records
the positions of the lines in the source file, which is assumed
to be open on descriptor DESC.
All set S->nlines to the number of such lines. */
find_source_lines (s
, desc
)
register char *data
, *p
, *end
;
int lines_allocated
= 1000;
int *line_charpos
= (int *) xmalloc (lines_allocated
* sizeof (int));
if (fstat (desc
, &st
) < 0)
perror_with_name (s
->filename
);
if (get_exec_file (0) != 0 && exec_mtime
< st
.st_mtime
)
printf ("Source file is more recent than executable.\n");
data
= (char *) alloca (st
.st_size
);
if (myread (desc
, data
, st
.st_size
) < 0)
perror_with_name (s
->filename
);
/* A newline at the end does not start a new line. */
if (nlines
== lines_allocated
)
line_charpos
= (int *) xrealloc (line_charpos
,
sizeof (int) * lines_allocated
);
line_charpos
[nlines
++] = p
- data
;
s
->line_charpos
= (int *) xrealloc (line_charpos
, nlines
* sizeof (int));
/* Return the character position of a line LINE in symtab S.
Return 0 if anything is invalid. */
source_line_charpos (s
, line
)
if (!s
->line_charpos
|| line
<= 0) return 0;
return s
->line_charpos
[line
- 1];
/* Return the line number of character position POS in symtab S. */
source_charpos_line (s
, chr
)
register struct symtab
*s
;
if (s
== 0 || s
->line_charpos
== 0) return 0;
/* Files are usually short, so sequential search is Ok */
while (line
< s
->nlines
&& *lnp
<= chr
)
/* Get full pathname and line number positions for a symtab.
Return nonzero if line numbers may have changed.
Set *FULLNAME to actual name of the file as found by `openp',
or to 0 if the file is not found. */
get_filename_and_charpos (s
, line
, fullname
)
register int desc
, linenums_changed
= 0;
desc
= openp (source_path
, 0, s
->filename
, O_RDONLY
, 0, &s
->fullname
);
if (s
->line_charpos
== 0) linenums_changed
= 1;
if (linenums_changed
) find_source_lines (s
, desc
);
/* Print text describing the full name of the source file S
and the line number LINE and its corresponding character position.
The text starts with two Ctrl-z so that the Emacs-GDB interface
MID_STATEMENT is nonzero if the PC is not at the beginning of that line.
Return 1 if successful, 0 if could not find the file. */
identify_source_line (s
, line
, mid_statement
)
if (s
->line_charpos
== 0)
get_filename_and_charpos (s
, line
, 0);
printf ("\032\032%s:%d:%d:%s:0x%x\n", s
->fullname
,
line
, s
->line_charpos
[line
- 1],
mid_statement
? "middle" : "beg",
get_frame_pc (get_current_frame()));
current_source_line
= line
;
first_line_listed
= line
;
current_source_symtab
= s
;
/* Print source lines from the file of symtab S,
starting with line number LINE and stopping before line number STOPLINE. */
print_source_lines (s
, line
, stopline
, noerror
)
int nlines
= stopline
- line
;
desc
= openp (source_path
, 0, s
->filename
, O_RDONLY
, 0, &s
->fullname
);
if (noerror
&& line
+ 1 == stopline
)
/* can't find the file - tell user where we are anyway */
current_source_symtab
= s
;
current_source_line
= line
;
first_line_listed
= line
;
printf_filtered ("%d\t(%s)\n", current_source_line
++, s
->filename
);
perror_with_name (s
->filename
);
print_sys_errmsg (s
->filename
, errno
);
if (s
->line_charpos
== 0)
find_source_lines (s
, desc
);
if (line
< 1 || line
> s
->nlines
)
error ("Line number %d out of range; %s has %d lines.",
line
, s
->filename
, s
->nlines
);
if (lseek (desc
, s
->line_charpos
[line
- 1], 0) < 0)
perror_with_name (s
->filename
);
current_source_symtab
= s
;
current_source_line
= line
;
first_line_listed
= line
;
stream
= fdopen (desc
, "r");
last_line_listed
= current_source_line
;
printf_filtered ("%d\t", current_source_line
++);
if (c
< 040 && c
!= '\t' && c
!= '\n')
printf_filtered ("^%c", c
+ 0100);
printf_filtered ("%c", c
);
} while (c
!= '\n' && (c
= fgetc (stream
)) >= 0);
Print a list of files and line numbers which a user may choose from
in order to list a function which was specified ambiguously
(as with `list classname::overloadedfuncname', for example).
The vector in SALS provides the filenames and line numbers.
ambiguous_line_spec (sals
)
struct symtabs_and_lines
*sals
;
for (i
= 0; i
< sals
->nelts
; ++i
)
printf("file: \"%s\", line number: %d\n",
sals
->sals
[i
].symtab
->filename
, sals
->sals
[i
].line
);
file_command(arg
, from_tty
)
struct symtabs_and_lines sals
;
struct symtab_and_line sal
;
if (symtab_list
== 0 && partial_symtab_list
== 0)
error ("No symbol table is loaded. Use the \"symbol-file\" command.");
/* Pull in a current source symtab if necessary */
if (arg
== 0 || arg
[0] == 0) {
if (current_source_symtab
== 0)
printf("%s\n", current_source_symtab
->filename
);
sals
= decode_line_1 (&arg1
, 0, 0, 0);
ambiguous_line_spec (&sals
);
/* Record whether the BEG arg is all digits. */
for (p
= arg
; p
!= arg1
&& *p
>= '0' && *p
<= '9'; ++p
)
linenum_beg
= (p
== arg1
);
/* if line was specified by address,
print exactly which line, and which file.
In this case, sal.symtab == 0 means address is outside
of all known source files, not that user failed to give a filename. */
error ("No source file for address 0x%x.", sal
.pc
);
sym
= find_pc_function (sal
.pc
);
printf ("0x%x is in %s (%s, line %d).\n",
sal
.pc
, SYMBOL_NAME (sym
), sal
.symtab
->filename
, sal
.line
);
printf ("0x%x is in %s, line %d.\n",
sal
.pc
, sal
.symtab
->filename
, sal
.line
);
/* If line was not specified by just a line number,
and it does not imply a symtab, it must be an undebuggable symbol
which means no source code. */
error ("No line number known for %s.", arg
);
error ("No default source file yet. Do \"help list\".");
current_source_symtab
= sal
.symtab
;
current_source_line
= sal
.line
;
first_line_listed
= sal
.line
;
#define PUSH_STACK_SIZE 32
} push_stack
[PUSH_STACK_SIZE
];
static unsigned int push_stack_ptr
;
push_to_file_command (arg
, from_tty
)
struct symtab
*cursym
= current_source_symtab
;
int curline
= current_source_line
;
file_command(arg
, from_tty
);
/* if we got back, command was successful */
push_stack
[i
].symtab
= cursym
;
push_stack
[i
].line
= curline
;
push_stack_ptr
= (i
+ 1) & (PUSH_STACK_SIZE
- 1);
pop_file_command (arg
, from_tty
)
register unsigned int i
= push_stack_ptr
;
/* if there's something on the stack, pop it & clear the slot. */
i
= (i
+ (PUSH_STACK_SIZE
- 1)) & (PUSH_STACK_SIZE
- 1);
if (push_stack
[i
].symtab
) {
current_source_symtab
= push_stack
[i
].symtab
;
first_line_listed
= current_source_line
= push_stack
[i
].line
;
push_stack
[i
].symtab
= NULL
;
list_command (arg
, from_tty
)
struct symtabs_and_lines sals
, sals_end
;
struct symtab_and_line sal
, sal_end
;
if (symtab_list
== 0 && partial_symtab_list
== 0)
error ("No symbol table is loaded. Use the \"symbol-file\" command.");
/* Pull in a current source symtab if necessary */
if (current_source_symtab
== 0 &&
(arg
== 0 || arg
[0] == '+' || arg
[0] == '-'))
select_source_symtab (0);
/* "l" or "l +" lists next ten lines. */
if (arg
== 0 || !strcmp (arg
, "+"))
if (current_source_symtab
== 0)
error ("No default source file yet. Do \"help list\".");
print_source_lines (current_source_symtab
, current_source_line
,
current_source_line
+ 10, 0);
/* "l -" lists previous ten lines, the ones before the ten just listed. */
if (current_source_symtab
== 0)
error ("No default source file yet. Do \"help list\".");
print_source_lines (current_source_symtab
,
max (first_line_listed
- 10, 1),
/* Now if there is only one argument, decode it in SAL
If there are two arguments, decode them in SAL and SAL_END
and clear NO_END; however, if one of the arguments is blank,
set DUMMY_BEG or DUMMY_END to record that fact. */
sals
= decode_line_1 (&arg1
, 0, 0, 0);
if (! sals
.nelts
) return; /* C++ */
ambiguous_line_spec (&sals
);
/* Record whether the BEG arg is all digits. */
for (p
= arg
; p
!= arg1
&& *p
>= '0' && *p
<= '9'; p
++);
linenum_beg
= (p
== arg1
);
while (*arg1
== ' ' || *arg1
== '\t')
while (*arg1
== ' ' || *arg1
== '\t')
sals_end
= decode_line_1 (&arg1
, 0, 0, 0);
sals_end
= decode_line_1 (&arg1
, 0, sal
.symtab
, sal
.line
);
ambiguous_line_spec (&sals_end
);
sal_end
= sals_end
.sals
[0];
error ("Junk at end of line specification.");
if (!no_end
&& !dummy_beg
&& !dummy_end
&& sal
.symtab
!= sal_end
.symtab
)
error ("Specified start and end are in different files.");
if (dummy_beg
&& dummy_end
)
error ("Two empty args do not say what lines to list.");
/* if line was specified by address,
first print exactly which line, and which file.
In this case, sal.symtab == 0 means address is outside
of all known source files, not that user failed to give a filename. */
error ("No source file for address 0x%x.", sal
.pc
);
sym
= find_pc_function (sal
.pc
);
printf ("0x%x is in %s (%s, line %d).\n",
sal
.pc
, SYMBOL_NAME (sym
), sal
.symtab
->filename
, sal
.line
);
printf ("0x%x is in %s, line %d.\n",
sal
.pc
, sal
.symtab
->filename
, sal
.line
);
/* If line was not specified by just a line number,
and it does not imply a symtab, it must be an undebuggable symbol
which means no source code. */
if (! linenum_beg
&& sal
.symtab
== 0)
error ("No line number known for %s.", arg
);
/* If this command is repeated with RET,
turn it into the no-arg variant. */
if (dummy_beg
&& sal_end
.symtab
== 0)
error ("No default source file yet. Do \"help list\".");
print_source_lines (sal_end
.symtab
, max (sal_end
.line
- 9, 1),
else if (sal
.symtab
== 0)
error ("No default source file yet. Do \"help list\".");
print_source_lines (sal
.symtab
, max (sal
.line
- 5, 1), sal
.line
+ 5, 0);
print_source_lines (sal
.symtab
, sal
.line
,
dummy_end
? sal
.line
+ 10 : sal_end
.line
+ 1,
/* Print info on range of pc's in a specified line. */
line_info (arg
, from_tty
)
struct symtabs_and_lines sals
;
struct symtab_and_line sal
;
sal
.symtab
= current_source_symtab
;
sal
.line
= last_line_listed
;
sals
.sals
= (struct symtab_and_line
*)
xmalloc (sizeof (struct symtab_and_line
));
sals
= decode_line_spec_1 (arg
, 0);
/* If this command is repeated with RET,
turn it into the no-arg variant. */
/* C++ More than one line may have been specified, as when the user
specifies an overloaded function name. Print info on them all. */
for (i
= 0; i
< sals
.nelts
; i
++)
error ("No source file specified.");
&& find_line_pc_range (sal
.symtab
, sal
.line
, &start_pc
, &end_pc
))
printf ("Line %d of \"%s\" is at pc 0x%x but contains no code.\n",
sal
.line
, sal
.symtab
->filename
, start_pc
);
printf ("Line %d of \"%s\" starts at pc 0x%x and ends at 0x%x.\n",
sal
.line
, sal
.symtab
->filename
, start_pc
, end_pc
);
/* x/i should display this line's code. */
set_next_address (start_pc
);
/* Repeating "info line" should do the following line. */
last_line_listed
= sal
.line
+ 1;
printf ("Line number %d is out of range for \"%s\".\n",
sal
.line
, sal
.symtab
->filename
);
/* Commands to search the source file for a regexp. */
forward_search_command (regex
, from_tty
)
int line
= last_line_listed
+ 1;
msg
= (char *) re_comp (regex
);
if (current_source_symtab
== 0)
select_source_symtab (0);
/* Search from last_line_listed+1 in current_source_symtab */
desc
= openp (source_path
, 0, current_source_symtab
->filename
,
O_RDONLY
, 0, ¤t_source_symtab
->fullname
);
perror_with_name (current_source_symtab
->filename
);
if (current_source_symtab
->line_charpos
== 0)
find_source_lines (current_source_symtab
, desc
);
if (line
< 1 || line
> current_source_symtab
->nlines
)
error ("Expression not found");
if (lseek (desc
, current_source_symtab
->line_charpos
[line
- 1], 0) < 0)
perror_with_name (current_source_symtab
->filename
);
stream
= fdopen (desc
, "r");
char buf
[4096]; /* Should be reasonable??? */
} while (c
!= '\n' && (c
= fgetc (stream
)) >= 0);
/* we now have a source line in buf, null terminate and match */
print_source_lines (current_source_symtab
,
current_source_line
= max (line
- 5, 1);
printf ("Expression not found\n");
reverse_search_command (regex
, from_tty
)
int line
= last_line_listed
- 1;
msg
= (char *) re_comp (regex
);
if (current_source_symtab
== 0)
select_source_symtab (0);
/* Search from last_line_listed-1 in current_source_symtab */
desc
= openp (source_path
, 0, current_source_symtab
->filename
,
O_RDONLY
, 0, ¤t_source_symtab
->fullname
);
perror_with_name (current_source_symtab
->filename
);
if (current_source_symtab
->line_charpos
== 0)
find_source_lines (current_source_symtab
, desc
);
if (line
< 1 || line
> current_source_symtab
->nlines
)
error ("Expression not found");
if (lseek (desc
, current_source_symtab
->line_charpos
[line
- 1], 0) < 0)
perror_with_name (current_source_symtab
->filename
);
stream
= fdopen (desc
, "r");
char buf
[4096]; /* Should be reasonable??? */
} while (c
!= '\n' && (c
= fgetc (stream
)) >= 0);
/* We now have a source line in buf; null terminate and match. */
print_source_lines (current_source_symtab
,
current_source_line
= max (line
- 5, 1);
if (fseek (stream
, current_source_symtab
->line_charpos
[line
- 1], 0) < 0)
perror_with_name (current_source_symtab
->filename
);
printf ("Expression not found\n");
current_source_symtab
= 0;
add_com ("directory", class_files
, directory_command
,
"Add directory DIR to end of search path for source files.\n\
With no argument, reset the search path to just the working directory\n\
and forget cached info on line positions in source files.");
add_info ("directories", directories_info
,
"Current search path for finding source files.");
add_info ("line", line_info
,
"Core addresses of the code for a source line.\n\
Line can be specified as\n\
LINENUM, to list around that line in current file,\n\
FILE:LINENUM, to list around that line in that file,\n\
FUNCTION, to list around beginning of that function,\n\
FILE:FUNCTION, to distinguish among like-named static functions.\n\
Default is to describe the last source line that was listed.\n\n\
This sets the default address for \"x\" to the line's first instruction\n\
so that \"x/i\" suffices to start examining the machine code.\n\
The address is also stored as the value of \"$_\".");
add_com ("forward-search", class_files
, forward_search_command
,
"Search for regular expression (see regex(3)) from last line listed.");
add_com_alias ("search", "forward-search", class_files
, 0);
add_com ("reverse-search", class_files
, reverse_search_command
,
"Search backward for regular expression (see regex(3)) from last line listed.");
add_com ("list", class_files
, list_command
,
"List specified function or line.\n\
With no argument, lists ten more lines after or around previous listing.\n\
\"list -\" lists the ten lines before a previous ten-line listing.\n\
One argument specifies a line, and ten lines are listed around that line.\n\
Two arguments with comma between specify starting and ending lines to list.\n\
Lines can be specified in these ways:\n\
LINENUM, to list around that line in current file,\n\
FILE:LINENUM, to list around that line in that file,\n\
FUNCTION, to list around beginning of that function,\n\
FILE:FUNCTION, to distinguish among like-named static functions.\n\
*ADDRESS, to list around the line containing that address.\n\
With two args if one is empty it stands for ten lines away from the other arg.");
add_com ("file", class_files
, file_command
,
"Select current file, function and line for display or list.\n\
Specification can have the form:\n\
LINENUM, to select that line in current file,\n\
FILE:LINENUM, to select that line in that file,\n\
FUNCTION, to select beginning of that function,\n\
FILE:FUNCTION, to distinguish among like-named static functions.\n\
*ADDRESS, to select the line containing that address.");
add_com ("push-to-file", class_files
, push_to_file_command
,
"Like \"file\" command but remembers current file & line on a stack.\n\
Can later return to current file with \"pop-file\" command.\n\
Up to 32 file positions can be pushed on stack.");
add_com ("pop-file", class_files
, pop_file_command
,
"Pops back to file position saved by most recent \"push-to-file\".\n\
If everything has been popped from stack, command does nothing.");