xref: /dflybsd-src/contrib/gdb-7/gdb/source.c (revision 5796c8dc12c637f18a1740c26afd8d40ffa9b719)
1*5796c8dcSSimon Schubert /* List lines of source files for GDB, the GNU debugger.
2*5796c8dcSSimon Schubert    Copyright (C) 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995,
3*5796c8dcSSimon Schubert    1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2007, 2008,
4*5796c8dcSSimon Schubert    2009 Free Software Foundation, Inc.
5*5796c8dcSSimon Schubert 
6*5796c8dcSSimon Schubert    This file is part of GDB.
7*5796c8dcSSimon Schubert 
8*5796c8dcSSimon Schubert    This program is free software; you can redistribute it and/or modify
9*5796c8dcSSimon Schubert    it under the terms of the GNU General Public License as published by
10*5796c8dcSSimon Schubert    the Free Software Foundation; either version 3 of the License, or
11*5796c8dcSSimon Schubert    (at your option) any later version.
12*5796c8dcSSimon Schubert 
13*5796c8dcSSimon Schubert    This program is distributed in the hope that it will be useful,
14*5796c8dcSSimon Schubert    but WITHOUT ANY WARRANTY; without even the implied warranty of
15*5796c8dcSSimon Schubert    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16*5796c8dcSSimon Schubert    GNU General Public License for more details.
17*5796c8dcSSimon Schubert 
18*5796c8dcSSimon Schubert    You should have received a copy of the GNU General Public License
19*5796c8dcSSimon Schubert    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
20*5796c8dcSSimon Schubert 
21*5796c8dcSSimon Schubert #include "defs.h"
22*5796c8dcSSimon Schubert #include "arch-utils.h"
23*5796c8dcSSimon Schubert #include "symtab.h"
24*5796c8dcSSimon Schubert #include "expression.h"
25*5796c8dcSSimon Schubert #include "language.h"
26*5796c8dcSSimon Schubert #include "command.h"
27*5796c8dcSSimon Schubert #include "source.h"
28*5796c8dcSSimon Schubert #include "gdbcmd.h"
29*5796c8dcSSimon Schubert #include "frame.h"
30*5796c8dcSSimon Schubert #include "value.h"
31*5796c8dcSSimon Schubert #include "gdb_assert.h"
32*5796c8dcSSimon Schubert 
33*5796c8dcSSimon Schubert #include <sys/types.h>
34*5796c8dcSSimon Schubert #include "gdb_string.h"
35*5796c8dcSSimon Schubert #include "gdb_stat.h"
36*5796c8dcSSimon Schubert #include <fcntl.h>
37*5796c8dcSSimon Schubert #include "gdbcore.h"
38*5796c8dcSSimon Schubert #include "gdb_regex.h"
39*5796c8dcSSimon Schubert #include "symfile.h"
40*5796c8dcSSimon Schubert #include "objfiles.h"
41*5796c8dcSSimon Schubert #include "annotate.h"
42*5796c8dcSSimon Schubert #include "gdbtypes.h"
43*5796c8dcSSimon Schubert #include "linespec.h"
44*5796c8dcSSimon Schubert #include "filenames.h"		/* for DOSish file names */
45*5796c8dcSSimon Schubert #include "completer.h"
46*5796c8dcSSimon Schubert #include "ui-out.h"
47*5796c8dcSSimon Schubert #include "readline/readline.h"
48*5796c8dcSSimon Schubert 
49*5796c8dcSSimon Schubert 
50*5796c8dcSSimon Schubert #define OPEN_MODE (O_RDONLY | O_BINARY)
51*5796c8dcSSimon Schubert #define FDOPEN_MODE FOPEN_RB
52*5796c8dcSSimon Schubert 
53*5796c8dcSSimon Schubert /* Prototypes for exported functions. */
54*5796c8dcSSimon Schubert 
55*5796c8dcSSimon Schubert void _initialize_source (void);
56*5796c8dcSSimon Schubert 
57*5796c8dcSSimon Schubert /* Prototypes for local functions. */
58*5796c8dcSSimon Schubert 
59*5796c8dcSSimon Schubert static int get_filename_and_charpos (struct symtab *, char **);
60*5796c8dcSSimon Schubert 
61*5796c8dcSSimon Schubert static void reverse_search_command (char *, int);
62*5796c8dcSSimon Schubert 
63*5796c8dcSSimon Schubert static void forward_search_command (char *, int);
64*5796c8dcSSimon Schubert 
65*5796c8dcSSimon Schubert static void line_info (char *, int);
66*5796c8dcSSimon Schubert 
67*5796c8dcSSimon Schubert static void source_info (char *, int);
68*5796c8dcSSimon Schubert 
69*5796c8dcSSimon Schubert static void show_directories (char *, int);
70*5796c8dcSSimon Schubert 
71*5796c8dcSSimon Schubert /* Path of directories to search for source files.
72*5796c8dcSSimon Schubert    Same format as the PATH environment variable's value.  */
73*5796c8dcSSimon Schubert 
74*5796c8dcSSimon Schubert char *source_path;
75*5796c8dcSSimon Schubert 
76*5796c8dcSSimon Schubert /* Support for source path substitution commands.  */
77*5796c8dcSSimon Schubert 
78*5796c8dcSSimon Schubert struct substitute_path_rule
79*5796c8dcSSimon Schubert {
80*5796c8dcSSimon Schubert   char *from;
81*5796c8dcSSimon Schubert   char *to;
82*5796c8dcSSimon Schubert   struct substitute_path_rule *next;
83*5796c8dcSSimon Schubert };
84*5796c8dcSSimon Schubert 
85*5796c8dcSSimon Schubert static struct substitute_path_rule *substitute_path_rules = NULL;
86*5796c8dcSSimon Schubert 
87*5796c8dcSSimon Schubert /* Symtab of default file for listing lines of.  */
88*5796c8dcSSimon Schubert 
89*5796c8dcSSimon Schubert static struct symtab *current_source_symtab;
90*5796c8dcSSimon Schubert 
91*5796c8dcSSimon Schubert /* Default next line to list.  */
92*5796c8dcSSimon Schubert 
93*5796c8dcSSimon Schubert static int current_source_line;
94*5796c8dcSSimon Schubert 
95*5796c8dcSSimon Schubert /* Default number of lines to print with commands like "list".
96*5796c8dcSSimon Schubert    This is based on guessing how many long (i.e. more than chars_per_line
97*5796c8dcSSimon Schubert    characters) lines there will be.  To be completely correct, "list"
98*5796c8dcSSimon Schubert    and friends should be rewritten to count characters and see where
99*5796c8dcSSimon Schubert    things are wrapping, but that would be a fair amount of work.  */
100*5796c8dcSSimon Schubert 
101*5796c8dcSSimon Schubert int lines_to_list = 10;
102*5796c8dcSSimon Schubert static void
103*5796c8dcSSimon Schubert show_lines_to_list (struct ui_file *file, int from_tty,
104*5796c8dcSSimon Schubert 		    struct cmd_list_element *c, const char *value)
105*5796c8dcSSimon Schubert {
106*5796c8dcSSimon Schubert   fprintf_filtered (file, _("\
107*5796c8dcSSimon Schubert Number of source lines gdb will list by default is %s.\n"),
108*5796c8dcSSimon Schubert 		    value);
109*5796c8dcSSimon Schubert }
110*5796c8dcSSimon Schubert 
111*5796c8dcSSimon Schubert /* Line number of last line printed.  Default for various commands.
112*5796c8dcSSimon Schubert    current_source_line is usually, but not always, the same as this.  */
113*5796c8dcSSimon Schubert 
114*5796c8dcSSimon Schubert static int last_line_listed;
115*5796c8dcSSimon Schubert 
116*5796c8dcSSimon Schubert /* First line number listed by last listing command.  */
117*5796c8dcSSimon Schubert 
118*5796c8dcSSimon Schubert static int first_line_listed;
119*5796c8dcSSimon Schubert 
120*5796c8dcSSimon Schubert /* Saves the name of the last source file visited and a possible error code.
121*5796c8dcSSimon Schubert    Used to prevent repeating annoying "No such file or directories" msgs */
122*5796c8dcSSimon Schubert 
123*5796c8dcSSimon Schubert static struct symtab *last_source_visited = NULL;
124*5796c8dcSSimon Schubert static int last_source_error = 0;
125*5796c8dcSSimon Schubert 
126*5796c8dcSSimon Schubert /* Return the first line listed by print_source_lines.
127*5796c8dcSSimon Schubert    Used by command interpreters to request listing from
128*5796c8dcSSimon Schubert    a previous point. */
129*5796c8dcSSimon Schubert 
130*5796c8dcSSimon Schubert int
131*5796c8dcSSimon Schubert get_first_line_listed (void)
132*5796c8dcSSimon Schubert {
133*5796c8dcSSimon Schubert   return first_line_listed;
134*5796c8dcSSimon Schubert }
135*5796c8dcSSimon Schubert 
136*5796c8dcSSimon Schubert /* Return the default number of lines to print with commands like the
137*5796c8dcSSimon Schubert    cli "list".  The caller of print_source_lines must use this to
138*5796c8dcSSimon Schubert    calculate the end line and use it in the call to print_source_lines
139*5796c8dcSSimon Schubert    as it does not automatically use this value. */
140*5796c8dcSSimon Schubert 
141*5796c8dcSSimon Schubert int
142*5796c8dcSSimon Schubert get_lines_to_list (void)
143*5796c8dcSSimon Schubert {
144*5796c8dcSSimon Schubert   return lines_to_list;
145*5796c8dcSSimon Schubert }
146*5796c8dcSSimon Schubert 
147*5796c8dcSSimon Schubert /* Return the current source file for listing and next line to list.
148*5796c8dcSSimon Schubert    NOTE: The returned sal pc and end fields are not valid. */
149*5796c8dcSSimon Schubert 
150*5796c8dcSSimon Schubert struct symtab_and_line
151*5796c8dcSSimon Schubert get_current_source_symtab_and_line (void)
152*5796c8dcSSimon Schubert {
153*5796c8dcSSimon Schubert   struct symtab_and_line cursal = { 0 };
154*5796c8dcSSimon Schubert 
155*5796c8dcSSimon Schubert   cursal.symtab = current_source_symtab;
156*5796c8dcSSimon Schubert   cursal.line = current_source_line;
157*5796c8dcSSimon Schubert   cursal.pc = 0;
158*5796c8dcSSimon Schubert   cursal.end = 0;
159*5796c8dcSSimon Schubert 
160*5796c8dcSSimon Schubert   return cursal;
161*5796c8dcSSimon Schubert }
162*5796c8dcSSimon Schubert 
163*5796c8dcSSimon Schubert /* If the current source file for listing is not set, try and get a default.
164*5796c8dcSSimon Schubert    Usually called before get_current_source_symtab_and_line() is called.
165*5796c8dcSSimon Schubert    It may err out if a default cannot be determined.
166*5796c8dcSSimon Schubert    We must be cautious about where it is called, as it can recurse as the
167*5796c8dcSSimon Schubert    process of determining a new default may call the caller!
168*5796c8dcSSimon Schubert    Use get_current_source_symtab_and_line only to get whatever
169*5796c8dcSSimon Schubert    we have without erroring out or trying to get a default. */
170*5796c8dcSSimon Schubert 
171*5796c8dcSSimon Schubert void
172*5796c8dcSSimon Schubert set_default_source_symtab_and_line (void)
173*5796c8dcSSimon Schubert {
174*5796c8dcSSimon Schubert   struct symtab_and_line cursal;
175*5796c8dcSSimon Schubert 
176*5796c8dcSSimon Schubert   if (!have_full_symbols () && !have_partial_symbols ())
177*5796c8dcSSimon Schubert     error (_("No symbol table is loaded.  Use the \"file\" command."));
178*5796c8dcSSimon Schubert 
179*5796c8dcSSimon Schubert   /* Pull in a current source symtab if necessary */
180*5796c8dcSSimon Schubert   if (current_source_symtab == 0)
181*5796c8dcSSimon Schubert     select_source_symtab (0);
182*5796c8dcSSimon Schubert }
183*5796c8dcSSimon Schubert 
184*5796c8dcSSimon Schubert /* Return the current default file for listing and next line to list
185*5796c8dcSSimon Schubert    (the returned sal pc and end fields are not valid.)
186*5796c8dcSSimon Schubert    and set the current default to whatever is in SAL.
187*5796c8dcSSimon Schubert    NOTE: The returned sal pc and end fields are not valid. */
188*5796c8dcSSimon Schubert 
189*5796c8dcSSimon Schubert struct symtab_and_line
190*5796c8dcSSimon Schubert set_current_source_symtab_and_line (const struct symtab_and_line *sal)
191*5796c8dcSSimon Schubert {
192*5796c8dcSSimon Schubert   struct symtab_and_line cursal = { 0 };
193*5796c8dcSSimon Schubert 
194*5796c8dcSSimon Schubert   cursal.symtab = current_source_symtab;
195*5796c8dcSSimon Schubert   cursal.line = current_source_line;
196*5796c8dcSSimon Schubert 
197*5796c8dcSSimon Schubert   current_source_symtab = sal->symtab;
198*5796c8dcSSimon Schubert   current_source_line = sal->line;
199*5796c8dcSSimon Schubert   cursal.pc = 0;
200*5796c8dcSSimon Schubert   cursal.end = 0;
201*5796c8dcSSimon Schubert 
202*5796c8dcSSimon Schubert   return cursal;
203*5796c8dcSSimon Schubert }
204*5796c8dcSSimon Schubert 
205*5796c8dcSSimon Schubert /* Reset any information stored about a default file and line to print. */
206*5796c8dcSSimon Schubert 
207*5796c8dcSSimon Schubert void
208*5796c8dcSSimon Schubert clear_current_source_symtab_and_line (void)
209*5796c8dcSSimon Schubert {
210*5796c8dcSSimon Schubert   current_source_symtab = 0;
211*5796c8dcSSimon Schubert   current_source_line = 0;
212*5796c8dcSSimon Schubert }
213*5796c8dcSSimon Schubert 
214*5796c8dcSSimon Schubert /* Set the source file default for the "list" command to be S.
215*5796c8dcSSimon Schubert 
216*5796c8dcSSimon Schubert    If S is NULL, and we don't have a default, find one.  This
217*5796c8dcSSimon Schubert    should only be called when the user actually tries to use the
218*5796c8dcSSimon Schubert    default, since we produce an error if we can't find a reasonable
219*5796c8dcSSimon Schubert    default.  Also, since this can cause symbols to be read, doing it
220*5796c8dcSSimon Schubert    before we need to would make things slower than necessary.  */
221*5796c8dcSSimon Schubert 
222*5796c8dcSSimon Schubert void
223*5796c8dcSSimon Schubert select_source_symtab (struct symtab *s)
224*5796c8dcSSimon Schubert {
225*5796c8dcSSimon Schubert   struct symtabs_and_lines sals;
226*5796c8dcSSimon Schubert   struct symtab_and_line sal;
227*5796c8dcSSimon Schubert   struct partial_symtab *ps;
228*5796c8dcSSimon Schubert   struct partial_symtab *cs_pst = 0;
229*5796c8dcSSimon Schubert   struct objfile *ofp;
230*5796c8dcSSimon Schubert 
231*5796c8dcSSimon Schubert   if (s)
232*5796c8dcSSimon Schubert     {
233*5796c8dcSSimon Schubert       current_source_symtab = s;
234*5796c8dcSSimon Schubert       current_source_line = 1;
235*5796c8dcSSimon Schubert       return;
236*5796c8dcSSimon Schubert     }
237*5796c8dcSSimon Schubert 
238*5796c8dcSSimon Schubert   if (current_source_symtab)
239*5796c8dcSSimon Schubert     return;
240*5796c8dcSSimon Schubert 
241*5796c8dcSSimon Schubert   /* Make the default place to list be the function `main'
242*5796c8dcSSimon Schubert      if one exists.  */
243*5796c8dcSSimon Schubert   if (lookup_symbol (main_name (), 0, VAR_DOMAIN, 0))
244*5796c8dcSSimon Schubert     {
245*5796c8dcSSimon Schubert       sals = decode_line_spec (main_name (), 1);
246*5796c8dcSSimon Schubert       sal = sals.sals[0];
247*5796c8dcSSimon Schubert       xfree (sals.sals);
248*5796c8dcSSimon Schubert       current_source_symtab = sal.symtab;
249*5796c8dcSSimon Schubert       current_source_line = max (sal.line - (lines_to_list - 1), 1);
250*5796c8dcSSimon Schubert       if (current_source_symtab)
251*5796c8dcSSimon Schubert 	return;
252*5796c8dcSSimon Schubert     }
253*5796c8dcSSimon Schubert 
254*5796c8dcSSimon Schubert   /* Alright; find the last file in the symtab list (ignoring .h's
255*5796c8dcSSimon Schubert      and namespace symtabs).  */
256*5796c8dcSSimon Schubert 
257*5796c8dcSSimon Schubert   current_source_line = 1;
258*5796c8dcSSimon Schubert 
259*5796c8dcSSimon Schubert   for (ofp = object_files; ofp != NULL; ofp = ofp->next)
260*5796c8dcSSimon Schubert     {
261*5796c8dcSSimon Schubert       for (s = ofp->symtabs; s; s = s->next)
262*5796c8dcSSimon Schubert 	{
263*5796c8dcSSimon Schubert 	  const char *name = s->filename;
264*5796c8dcSSimon Schubert 	  int len = strlen (name);
265*5796c8dcSSimon Schubert 	  if (!(len > 2 && (strcmp (&name[len - 2], ".h") == 0
266*5796c8dcSSimon Schubert 	      || strcmp (name, "<<C++-namespaces>>") == 0)))
267*5796c8dcSSimon Schubert 	    current_source_symtab = s;
268*5796c8dcSSimon Schubert 	}
269*5796c8dcSSimon Schubert     }
270*5796c8dcSSimon Schubert   if (current_source_symtab)
271*5796c8dcSSimon Schubert     return;
272*5796c8dcSSimon Schubert 
273*5796c8dcSSimon Schubert   /* How about the partial symbol tables?  */
274*5796c8dcSSimon Schubert 
275*5796c8dcSSimon Schubert   for (ofp = object_files; ofp != NULL; ofp = ofp->next)
276*5796c8dcSSimon Schubert     {
277*5796c8dcSSimon Schubert       for (ps = ofp->psymtabs; ps != NULL; ps = ps->next)
278*5796c8dcSSimon Schubert 	{
279*5796c8dcSSimon Schubert 	  const char *name = ps->filename;
280*5796c8dcSSimon Schubert 	  int len = strlen (name);
281*5796c8dcSSimon Schubert 	  if (!(len > 2 && (strcmp (&name[len - 2], ".h") == 0
282*5796c8dcSSimon Schubert 	      || strcmp (name, "<<C++-namespaces>>") == 0)))
283*5796c8dcSSimon Schubert 	    cs_pst = ps;
284*5796c8dcSSimon Schubert 	}
285*5796c8dcSSimon Schubert     }
286*5796c8dcSSimon Schubert   if (cs_pst)
287*5796c8dcSSimon Schubert     {
288*5796c8dcSSimon Schubert       if (cs_pst->readin)
289*5796c8dcSSimon Schubert 	{
290*5796c8dcSSimon Schubert 	  internal_error (__FILE__, __LINE__,
291*5796c8dcSSimon Schubert 			  _("select_source_symtab: "
292*5796c8dcSSimon Schubert 			  "readin pst found and no symtabs."));
293*5796c8dcSSimon Schubert 	}
294*5796c8dcSSimon Schubert       else
295*5796c8dcSSimon Schubert 	{
296*5796c8dcSSimon Schubert 	  current_source_symtab = PSYMTAB_TO_SYMTAB (cs_pst);
297*5796c8dcSSimon Schubert 	}
298*5796c8dcSSimon Schubert     }
299*5796c8dcSSimon Schubert   if (current_source_symtab)
300*5796c8dcSSimon Schubert     return;
301*5796c8dcSSimon Schubert 
302*5796c8dcSSimon Schubert   error (_("Can't find a default source file"));
303*5796c8dcSSimon Schubert }
304*5796c8dcSSimon Schubert 
305*5796c8dcSSimon Schubert static void
306*5796c8dcSSimon Schubert show_directories (char *ignore, int from_tty)
307*5796c8dcSSimon Schubert {
308*5796c8dcSSimon Schubert   puts_filtered ("Source directories searched: ");
309*5796c8dcSSimon Schubert   puts_filtered (source_path);
310*5796c8dcSSimon Schubert   puts_filtered ("\n");
311*5796c8dcSSimon Schubert }
312*5796c8dcSSimon Schubert 
313*5796c8dcSSimon Schubert /* Forget what we learned about line positions in source files, and
314*5796c8dcSSimon Schubert    which directories contain them; must check again now since files
315*5796c8dcSSimon Schubert    may be found in a different directory now.  */
316*5796c8dcSSimon Schubert 
317*5796c8dcSSimon Schubert void
318*5796c8dcSSimon Schubert forget_cached_source_info (void)
319*5796c8dcSSimon Schubert {
320*5796c8dcSSimon Schubert   struct symtab *s;
321*5796c8dcSSimon Schubert   struct objfile *objfile;
322*5796c8dcSSimon Schubert   struct partial_symtab *pst;
323*5796c8dcSSimon Schubert 
324*5796c8dcSSimon Schubert   for (objfile = object_files; objfile != NULL; objfile = objfile->next)
325*5796c8dcSSimon Schubert     {
326*5796c8dcSSimon Schubert       for (s = objfile->symtabs; s != NULL; s = s->next)
327*5796c8dcSSimon Schubert 	{
328*5796c8dcSSimon Schubert 	  if (s->line_charpos != NULL)
329*5796c8dcSSimon Schubert 	    {
330*5796c8dcSSimon Schubert 	      xfree (s->line_charpos);
331*5796c8dcSSimon Schubert 	      s->line_charpos = NULL;
332*5796c8dcSSimon Schubert 	    }
333*5796c8dcSSimon Schubert 	  if (s->fullname != NULL)
334*5796c8dcSSimon Schubert 	    {
335*5796c8dcSSimon Schubert 	      xfree (s->fullname);
336*5796c8dcSSimon Schubert 	      s->fullname = NULL;
337*5796c8dcSSimon Schubert 	    }
338*5796c8dcSSimon Schubert 	}
339*5796c8dcSSimon Schubert 
340*5796c8dcSSimon Schubert       ALL_OBJFILE_PSYMTABS (objfile, pst)
341*5796c8dcSSimon Schubert       {
342*5796c8dcSSimon Schubert 	if (pst->fullname != NULL)
343*5796c8dcSSimon Schubert 	  {
344*5796c8dcSSimon Schubert 	    xfree (pst->fullname);
345*5796c8dcSSimon Schubert 	    pst->fullname = NULL;
346*5796c8dcSSimon Schubert 	  }
347*5796c8dcSSimon Schubert       }
348*5796c8dcSSimon Schubert     }
349*5796c8dcSSimon Schubert 
350*5796c8dcSSimon Schubert   last_source_visited = NULL;
351*5796c8dcSSimon Schubert }
352*5796c8dcSSimon Schubert 
353*5796c8dcSSimon Schubert void
354*5796c8dcSSimon Schubert init_source_path (void)
355*5796c8dcSSimon Schubert {
356*5796c8dcSSimon Schubert   char buf[20];
357*5796c8dcSSimon Schubert 
358*5796c8dcSSimon Schubert   sprintf (buf, "$cdir%c$cwd", DIRNAME_SEPARATOR);
359*5796c8dcSSimon Schubert   source_path = xstrdup (buf);
360*5796c8dcSSimon Schubert   forget_cached_source_info ();
361*5796c8dcSSimon Schubert }
362*5796c8dcSSimon Schubert 
363*5796c8dcSSimon Schubert /* Add zero or more directories to the front of the source path.  */
364*5796c8dcSSimon Schubert 
365*5796c8dcSSimon Schubert void
366*5796c8dcSSimon Schubert directory_command (char *dirname, int from_tty)
367*5796c8dcSSimon Schubert {
368*5796c8dcSSimon Schubert   dont_repeat ();
369*5796c8dcSSimon Schubert   /* FIXME, this goes to "delete dir"... */
370*5796c8dcSSimon Schubert   if (dirname == 0)
371*5796c8dcSSimon Schubert     {
372*5796c8dcSSimon Schubert       if (!from_tty || query (_("Reinitialize source path to empty? ")))
373*5796c8dcSSimon Schubert 	{
374*5796c8dcSSimon Schubert 	  xfree (source_path);
375*5796c8dcSSimon Schubert 	  init_source_path ();
376*5796c8dcSSimon Schubert 	}
377*5796c8dcSSimon Schubert     }
378*5796c8dcSSimon Schubert   else
379*5796c8dcSSimon Schubert     {
380*5796c8dcSSimon Schubert       mod_path (dirname, &source_path);
381*5796c8dcSSimon Schubert       forget_cached_source_info ();
382*5796c8dcSSimon Schubert     }
383*5796c8dcSSimon Schubert   if (from_tty)
384*5796c8dcSSimon Schubert     show_directories ((char *) 0, from_tty);
385*5796c8dcSSimon Schubert }
386*5796c8dcSSimon Schubert 
387*5796c8dcSSimon Schubert /* Add a path given with the -d command line switch.
388*5796c8dcSSimon Schubert    This will not be quoted so we must not treat spaces as separators.  */
389*5796c8dcSSimon Schubert 
390*5796c8dcSSimon Schubert void
391*5796c8dcSSimon Schubert directory_switch (char *dirname, int from_tty)
392*5796c8dcSSimon Schubert {
393*5796c8dcSSimon Schubert   add_path (dirname, &source_path, 0);
394*5796c8dcSSimon Schubert }
395*5796c8dcSSimon Schubert 
396*5796c8dcSSimon Schubert /* Add zero or more directories to the front of an arbitrary path.  */
397*5796c8dcSSimon Schubert 
398*5796c8dcSSimon Schubert void
399*5796c8dcSSimon Schubert mod_path (char *dirname, char **which_path)
400*5796c8dcSSimon Schubert {
401*5796c8dcSSimon Schubert   add_path (dirname, which_path, 1);
402*5796c8dcSSimon Schubert }
403*5796c8dcSSimon Schubert 
404*5796c8dcSSimon Schubert /* Workhorse of mod_path.  Takes an extra argument to determine
405*5796c8dcSSimon Schubert    if dirname should be parsed for separators that indicate multiple
406*5796c8dcSSimon Schubert    directories.  This allows for interfaces that pre-parse the dirname
407*5796c8dcSSimon Schubert    and allow specification of traditional separator characters such
408*5796c8dcSSimon Schubert    as space or tab. */
409*5796c8dcSSimon Schubert 
410*5796c8dcSSimon Schubert void
411*5796c8dcSSimon Schubert add_path (char *dirname, char **which_path, int parse_separators)
412*5796c8dcSSimon Schubert {
413*5796c8dcSSimon Schubert   char *old = *which_path;
414*5796c8dcSSimon Schubert   int prefix = 0;
415*5796c8dcSSimon Schubert   char **argv = NULL;
416*5796c8dcSSimon Schubert   char *arg;
417*5796c8dcSSimon Schubert   int argv_index = 0;
418*5796c8dcSSimon Schubert 
419*5796c8dcSSimon Schubert   if (dirname == 0)
420*5796c8dcSSimon Schubert     return;
421*5796c8dcSSimon Schubert 
422*5796c8dcSSimon Schubert   if (parse_separators)
423*5796c8dcSSimon Schubert     {
424*5796c8dcSSimon Schubert       /* This will properly parse the space and tab separators
425*5796c8dcSSimon Schubert 	 and any quotes that may exist. DIRNAME_SEPARATOR will
426*5796c8dcSSimon Schubert 	 be dealt with later.  */
427*5796c8dcSSimon Schubert       argv = gdb_buildargv (dirname);
428*5796c8dcSSimon Schubert       make_cleanup_freeargv (argv);
429*5796c8dcSSimon Schubert 
430*5796c8dcSSimon Schubert       arg = argv[0];
431*5796c8dcSSimon Schubert     }
432*5796c8dcSSimon Schubert   else
433*5796c8dcSSimon Schubert     {
434*5796c8dcSSimon Schubert       arg = xstrdup (dirname);
435*5796c8dcSSimon Schubert       make_cleanup (xfree, arg);
436*5796c8dcSSimon Schubert     }
437*5796c8dcSSimon Schubert 
438*5796c8dcSSimon Schubert   do
439*5796c8dcSSimon Schubert     {
440*5796c8dcSSimon Schubert       char *name = arg;
441*5796c8dcSSimon Schubert       char *p;
442*5796c8dcSSimon Schubert       struct stat st;
443*5796c8dcSSimon Schubert 
444*5796c8dcSSimon Schubert       {
445*5796c8dcSSimon Schubert 	char *separator = NULL;
446*5796c8dcSSimon Schubert 
447*5796c8dcSSimon Schubert 	/* Spaces and tabs will have been removed by buildargv().
448*5796c8dcSSimon Schubert 	   The directories will there be split into a list but
449*5796c8dcSSimon Schubert 	   each entry may still contain DIRNAME_SEPARATOR.  */
450*5796c8dcSSimon Schubert 	if (parse_separators)
451*5796c8dcSSimon Schubert 	  separator = strchr (name, DIRNAME_SEPARATOR);
452*5796c8dcSSimon Schubert 
453*5796c8dcSSimon Schubert 	if (separator == 0)
454*5796c8dcSSimon Schubert 	  p = arg = name + strlen (name);
455*5796c8dcSSimon Schubert 	else
456*5796c8dcSSimon Schubert 	  {
457*5796c8dcSSimon Schubert 	    p = separator;
458*5796c8dcSSimon Schubert 	    arg = p + 1;
459*5796c8dcSSimon Schubert 	    while (*arg == DIRNAME_SEPARATOR)
460*5796c8dcSSimon Schubert 	      ++arg;
461*5796c8dcSSimon Schubert 	  }
462*5796c8dcSSimon Schubert 
463*5796c8dcSSimon Schubert 	/* If there are no more directories in this argument then start
464*5796c8dcSSimon Schubert 	   on the next argument next time round the loop (if any).  */
465*5796c8dcSSimon Schubert 	if (*arg == '\0')
466*5796c8dcSSimon Schubert 	  arg = parse_separators ? argv[++argv_index] : NULL;
467*5796c8dcSSimon Schubert       }
468*5796c8dcSSimon Schubert 
469*5796c8dcSSimon Schubert       /* name is the start of the directory.
470*5796c8dcSSimon Schubert 	 p is the separator (or null) following the end.  */
471*5796c8dcSSimon Schubert 
472*5796c8dcSSimon Schubert       while (!(IS_DIR_SEPARATOR (*name) && p <= name + 1)	/* "/" */
473*5796c8dcSSimon Schubert #ifdef HAVE_DOS_BASED_FILE_SYSTEM
474*5796c8dcSSimon Schubert       /* On MS-DOS and MS-Windows, h:\ is different from h: */
475*5796c8dcSSimon Schubert 	     && !(p == name + 3 && name[1] == ':')		/* "d:/" */
476*5796c8dcSSimon Schubert #endif
477*5796c8dcSSimon Schubert 	     && IS_DIR_SEPARATOR (p[-1]))
478*5796c8dcSSimon Schubert 	/* Sigh. "foo/" => "foo" */
479*5796c8dcSSimon Schubert 	--p;
480*5796c8dcSSimon Schubert       *p = '\0';
481*5796c8dcSSimon Schubert 
482*5796c8dcSSimon Schubert       while (p > name && p[-1] == '.')
483*5796c8dcSSimon Schubert 	{
484*5796c8dcSSimon Schubert 	  if (p - name == 1)
485*5796c8dcSSimon Schubert 	    {
486*5796c8dcSSimon Schubert 	      /* "." => getwd ().  */
487*5796c8dcSSimon Schubert 	      name = current_directory;
488*5796c8dcSSimon Schubert 	      goto append;
489*5796c8dcSSimon Schubert 	    }
490*5796c8dcSSimon Schubert 	  else if (p > name + 1 && IS_DIR_SEPARATOR (p[-2]))
491*5796c8dcSSimon Schubert 	    {
492*5796c8dcSSimon Schubert 	      if (p - name == 2)
493*5796c8dcSSimon Schubert 		{
494*5796c8dcSSimon Schubert 		  /* "/." => "/".  */
495*5796c8dcSSimon Schubert 		  *--p = '\0';
496*5796c8dcSSimon Schubert 		  goto append;
497*5796c8dcSSimon Schubert 		}
498*5796c8dcSSimon Schubert 	      else
499*5796c8dcSSimon Schubert 		{
500*5796c8dcSSimon Schubert 		  /* "...foo/." => "...foo".  */
501*5796c8dcSSimon Schubert 		  p -= 2;
502*5796c8dcSSimon Schubert 		  *p = '\0';
503*5796c8dcSSimon Schubert 		  continue;
504*5796c8dcSSimon Schubert 		}
505*5796c8dcSSimon Schubert 	    }
506*5796c8dcSSimon Schubert 	  else
507*5796c8dcSSimon Schubert 	    break;
508*5796c8dcSSimon Schubert 	}
509*5796c8dcSSimon Schubert 
510*5796c8dcSSimon Schubert       if (name[0] == '~')
511*5796c8dcSSimon Schubert 	name = tilde_expand (name);
512*5796c8dcSSimon Schubert #ifdef HAVE_DOS_BASED_FILE_SYSTEM
513*5796c8dcSSimon Schubert       else if (IS_ABSOLUTE_PATH (name) && p == name + 2) /* "d:" => "d:." */
514*5796c8dcSSimon Schubert 	name = concat (name, ".", (char *)NULL);
515*5796c8dcSSimon Schubert #endif
516*5796c8dcSSimon Schubert       else if (!IS_ABSOLUTE_PATH (name) && name[0] != '$')
517*5796c8dcSSimon Schubert 	name = concat (current_directory, SLASH_STRING, name, (char *)NULL);
518*5796c8dcSSimon Schubert       else
519*5796c8dcSSimon Schubert 	name = savestring (name, p - name);
520*5796c8dcSSimon Schubert       make_cleanup (xfree, name);
521*5796c8dcSSimon Schubert 
522*5796c8dcSSimon Schubert       /* Unless it's a variable, check existence.  */
523*5796c8dcSSimon Schubert       if (name[0] != '$')
524*5796c8dcSSimon Schubert 	{
525*5796c8dcSSimon Schubert 	  /* These are warnings, not errors, since we don't want a
526*5796c8dcSSimon Schubert 	     non-existent directory in a .gdbinit file to stop processing
527*5796c8dcSSimon Schubert 	     of the .gdbinit file.
528*5796c8dcSSimon Schubert 
529*5796c8dcSSimon Schubert 	     Whether they get added to the path is more debatable.  Current
530*5796c8dcSSimon Schubert 	     answer is yes, in case the user wants to go make the directory
531*5796c8dcSSimon Schubert 	     or whatever.  If the directory continues to not exist/not be
532*5796c8dcSSimon Schubert 	     a directory/etc, then having them in the path should be
533*5796c8dcSSimon Schubert 	     harmless.  */
534*5796c8dcSSimon Schubert 	  if (stat (name, &st) < 0)
535*5796c8dcSSimon Schubert 	    {
536*5796c8dcSSimon Schubert 	      int save_errno = errno;
537*5796c8dcSSimon Schubert 	      fprintf_unfiltered (gdb_stderr, "Warning: ");
538*5796c8dcSSimon Schubert 	      print_sys_errmsg (name, save_errno);
539*5796c8dcSSimon Schubert 	    }
540*5796c8dcSSimon Schubert 	  else if ((st.st_mode & S_IFMT) != S_IFDIR)
541*5796c8dcSSimon Schubert 	    warning (_("%s is not a directory."), name);
542*5796c8dcSSimon Schubert 	}
543*5796c8dcSSimon Schubert 
544*5796c8dcSSimon Schubert     append:
545*5796c8dcSSimon Schubert       {
546*5796c8dcSSimon Schubert 	unsigned int len = strlen (name);
547*5796c8dcSSimon Schubert 
548*5796c8dcSSimon Schubert 	p = *which_path;
549*5796c8dcSSimon Schubert 	while (1)
550*5796c8dcSSimon Schubert 	  {
551*5796c8dcSSimon Schubert 	    /* FIXME: strncmp loses in interesting ways on MS-DOS and
552*5796c8dcSSimon Schubert 	       MS-Windows because of case-insensitivity and two different
553*5796c8dcSSimon Schubert 	       but functionally identical slash characters.  We need a
554*5796c8dcSSimon Schubert 	       special filesystem-dependent file-name comparison function.
555*5796c8dcSSimon Schubert 
556*5796c8dcSSimon Schubert 	       Actually, even on Unix I would use realpath() or its work-
557*5796c8dcSSimon Schubert 	       alike before comparing.  Then all the code above which
558*5796c8dcSSimon Schubert 	       removes excess slashes and dots could simply go away.  */
559*5796c8dcSSimon Schubert 	    if (!strncmp (p, name, len)
560*5796c8dcSSimon Schubert 		&& (p[len] == '\0' || p[len] == DIRNAME_SEPARATOR))
561*5796c8dcSSimon Schubert 	      {
562*5796c8dcSSimon Schubert 		/* Found it in the search path, remove old copy */
563*5796c8dcSSimon Schubert 		if (p > *which_path)
564*5796c8dcSSimon Schubert 		  p--;		/* Back over leading separator */
565*5796c8dcSSimon Schubert 		if (prefix > p - *which_path)
566*5796c8dcSSimon Schubert 		  goto skip_dup;	/* Same dir twice in one cmd */
567*5796c8dcSSimon Schubert 		strcpy (p, &p[len + 1]);	/* Copy from next \0 or  : */
568*5796c8dcSSimon Schubert 	      }
569*5796c8dcSSimon Schubert 	    p = strchr (p, DIRNAME_SEPARATOR);
570*5796c8dcSSimon Schubert 	    if (p != 0)
571*5796c8dcSSimon Schubert 	      ++p;
572*5796c8dcSSimon Schubert 	    else
573*5796c8dcSSimon Schubert 	      break;
574*5796c8dcSSimon Schubert 	  }
575*5796c8dcSSimon Schubert 	if (p == 0)
576*5796c8dcSSimon Schubert 	  {
577*5796c8dcSSimon Schubert 	    char tinybuf[2];
578*5796c8dcSSimon Schubert 
579*5796c8dcSSimon Schubert 	    tinybuf[0] = DIRNAME_SEPARATOR;
580*5796c8dcSSimon Schubert 	    tinybuf[1] = '\0';
581*5796c8dcSSimon Schubert 
582*5796c8dcSSimon Schubert 	    /* If we have already tacked on a name(s) in this command, be sure they stay
583*5796c8dcSSimon Schubert 	       on the front as we tack on some more.  */
584*5796c8dcSSimon Schubert 	    if (prefix)
585*5796c8dcSSimon Schubert 	      {
586*5796c8dcSSimon Schubert 		char *temp, c;
587*5796c8dcSSimon Schubert 
588*5796c8dcSSimon Schubert 		c = old[prefix];
589*5796c8dcSSimon Schubert 		old[prefix] = '\0';
590*5796c8dcSSimon Schubert 		temp = concat (old, tinybuf, name, (char *)NULL);
591*5796c8dcSSimon Schubert 		old[prefix] = c;
592*5796c8dcSSimon Schubert 		*which_path = concat (temp, "", &old[prefix], (char *)NULL);
593*5796c8dcSSimon Schubert 		prefix = strlen (temp);
594*5796c8dcSSimon Schubert 		xfree (temp);
595*5796c8dcSSimon Schubert 	      }
596*5796c8dcSSimon Schubert 	    else
597*5796c8dcSSimon Schubert 	      {
598*5796c8dcSSimon Schubert 		*which_path = concat (name, (old[0] ? tinybuf : old),
599*5796c8dcSSimon Schubert 				      old, (char *)NULL);
600*5796c8dcSSimon Schubert 		prefix = strlen (name);
601*5796c8dcSSimon Schubert 	      }
602*5796c8dcSSimon Schubert 	    xfree (old);
603*5796c8dcSSimon Schubert 	    old = *which_path;
604*5796c8dcSSimon Schubert 	  }
605*5796c8dcSSimon Schubert       }
606*5796c8dcSSimon Schubert     skip_dup:;
607*5796c8dcSSimon Schubert     }
608*5796c8dcSSimon Schubert   while (arg != NULL);
609*5796c8dcSSimon Schubert }
610*5796c8dcSSimon Schubert 
611*5796c8dcSSimon Schubert 
612*5796c8dcSSimon Schubert static void
613*5796c8dcSSimon Schubert source_info (char *ignore, int from_tty)
614*5796c8dcSSimon Schubert {
615*5796c8dcSSimon Schubert   struct symtab *s = current_source_symtab;
616*5796c8dcSSimon Schubert 
617*5796c8dcSSimon Schubert   if (!s)
618*5796c8dcSSimon Schubert     {
619*5796c8dcSSimon Schubert       printf_filtered (_("No current source file.\n"));
620*5796c8dcSSimon Schubert       return;
621*5796c8dcSSimon Schubert     }
622*5796c8dcSSimon Schubert   printf_filtered (_("Current source file is %s\n"), s->filename);
623*5796c8dcSSimon Schubert   if (s->dirname)
624*5796c8dcSSimon Schubert     printf_filtered (_("Compilation directory is %s\n"), s->dirname);
625*5796c8dcSSimon Schubert   if (s->fullname)
626*5796c8dcSSimon Schubert     printf_filtered (_("Located in %s\n"), s->fullname);
627*5796c8dcSSimon Schubert   if (s->nlines)
628*5796c8dcSSimon Schubert     printf_filtered (_("Contains %d line%s.\n"), s->nlines,
629*5796c8dcSSimon Schubert 		     s->nlines == 1 ? "" : "s");
630*5796c8dcSSimon Schubert 
631*5796c8dcSSimon Schubert   printf_filtered (_("Source language is %s.\n"), language_str (s->language));
632*5796c8dcSSimon Schubert   printf_filtered (_("Compiled with %s debugging format.\n"), s->debugformat);
633*5796c8dcSSimon Schubert   printf_filtered (_("%s preprocessor macro info.\n"),
634*5796c8dcSSimon Schubert                    s->macro_table ? "Includes" : "Does not include");
635*5796c8dcSSimon Schubert }
636*5796c8dcSSimon Schubert 
637*5796c8dcSSimon Schubert 
638*5796c8dcSSimon Schubert /* Return True if the file NAME exists and is a regular file */
639*5796c8dcSSimon Schubert static int
640*5796c8dcSSimon Schubert is_regular_file (const char *name)
641*5796c8dcSSimon Schubert {
642*5796c8dcSSimon Schubert   struct stat st;
643*5796c8dcSSimon Schubert   const int status = stat (name, &st);
644*5796c8dcSSimon Schubert 
645*5796c8dcSSimon Schubert   /* Stat should never fail except when the file does not exist.
646*5796c8dcSSimon Schubert      If stat fails, analyze the source of error and return True
647*5796c8dcSSimon Schubert      unless the file does not exist, to avoid returning false results
648*5796c8dcSSimon Schubert      on obscure systems where stat does not work as expected.
649*5796c8dcSSimon Schubert    */
650*5796c8dcSSimon Schubert   if (status != 0)
651*5796c8dcSSimon Schubert     return (errno != ENOENT);
652*5796c8dcSSimon Schubert 
653*5796c8dcSSimon Schubert   return S_ISREG (st.st_mode);
654*5796c8dcSSimon Schubert }
655*5796c8dcSSimon Schubert 
656*5796c8dcSSimon Schubert /* Open a file named STRING, searching path PATH (dir names sep by some char)
657*5796c8dcSSimon Schubert    using mode MODE in the calls to open.  You cannot use this function to
658*5796c8dcSSimon Schubert    create files (O_CREAT).
659*5796c8dcSSimon Schubert 
660*5796c8dcSSimon Schubert    OPTS specifies the function behaviour in specific cases.
661*5796c8dcSSimon Schubert 
662*5796c8dcSSimon Schubert    If OPF_TRY_CWD_FIRST, try to open ./STRING before searching PATH.
663*5796c8dcSSimon Schubert    (ie pretend the first element of PATH is ".").  This also indicates
664*5796c8dcSSimon Schubert    that a slash in STRING disables searching of the path (this is
665*5796c8dcSSimon Schubert    so that "exec-file ./foo" or "symbol-file ./foo" insures that you
666*5796c8dcSSimon Schubert    get that particular version of foo or an error message).
667*5796c8dcSSimon Schubert 
668*5796c8dcSSimon Schubert    If OPTS has OPF_SEARCH_IN_PATH set, absolute names will also be
669*5796c8dcSSimon Schubert    searched in path (we usually want this for source files but not for
670*5796c8dcSSimon Schubert    executables).
671*5796c8dcSSimon Schubert 
672*5796c8dcSSimon Schubert    If FILENAME_OPENED is non-null, set it to a newly allocated string naming
673*5796c8dcSSimon Schubert    the actual file opened (this string will always start with a "/").  We
674*5796c8dcSSimon Schubert    have to take special pains to avoid doubling the "/" between the directory
675*5796c8dcSSimon Schubert    and the file, sigh!  Emacs gets confuzzed by this when we print the
676*5796c8dcSSimon Schubert    source file name!!!
677*5796c8dcSSimon Schubert 
678*5796c8dcSSimon Schubert    If a file is found, return the descriptor.
679*5796c8dcSSimon Schubert    Otherwise, return -1, with errno set for the last name we tried to open.  */
680*5796c8dcSSimon Schubert 
681*5796c8dcSSimon Schubert /*  >>>> This should only allow files of certain types,
682*5796c8dcSSimon Schubert     >>>>  eg executable, non-directory */
683*5796c8dcSSimon Schubert int
684*5796c8dcSSimon Schubert openp (const char *path, int opts, const char *string,
685*5796c8dcSSimon Schubert        int mode, char **filename_opened)
686*5796c8dcSSimon Schubert {
687*5796c8dcSSimon Schubert   int fd;
688*5796c8dcSSimon Schubert   char *filename;
689*5796c8dcSSimon Schubert   const char *p;
690*5796c8dcSSimon Schubert   const char *p1;
691*5796c8dcSSimon Schubert   int len;
692*5796c8dcSSimon Schubert   int alloclen;
693*5796c8dcSSimon Schubert 
694*5796c8dcSSimon Schubert   /* The open syscall MODE parameter is not specified.  */
695*5796c8dcSSimon Schubert   gdb_assert ((mode & O_CREAT) == 0);
696*5796c8dcSSimon Schubert 
697*5796c8dcSSimon Schubert   if (!path)
698*5796c8dcSSimon Schubert     path = ".";
699*5796c8dcSSimon Schubert 
700*5796c8dcSSimon Schubert   mode |= O_BINARY;
701*5796c8dcSSimon Schubert 
702*5796c8dcSSimon Schubert   if ((opts & OPF_TRY_CWD_FIRST) || IS_ABSOLUTE_PATH (string))
703*5796c8dcSSimon Schubert     {
704*5796c8dcSSimon Schubert       int i;
705*5796c8dcSSimon Schubert 
706*5796c8dcSSimon Schubert       if (is_regular_file (string))
707*5796c8dcSSimon Schubert 	{
708*5796c8dcSSimon Schubert 	  filename = alloca (strlen (string) + 1);
709*5796c8dcSSimon Schubert 	  strcpy (filename, string);
710*5796c8dcSSimon Schubert 	  fd = open (filename, mode);
711*5796c8dcSSimon Schubert 	  if (fd >= 0)
712*5796c8dcSSimon Schubert 	    goto done;
713*5796c8dcSSimon Schubert 	}
714*5796c8dcSSimon Schubert       else
715*5796c8dcSSimon Schubert 	{
716*5796c8dcSSimon Schubert 	  filename = NULL;
717*5796c8dcSSimon Schubert 	  fd = -1;
718*5796c8dcSSimon Schubert 	}
719*5796c8dcSSimon Schubert 
720*5796c8dcSSimon Schubert       if (!(opts & OPF_SEARCH_IN_PATH))
721*5796c8dcSSimon Schubert 	for (i = 0; string[i]; i++)
722*5796c8dcSSimon Schubert 	  if (IS_DIR_SEPARATOR (string[i]))
723*5796c8dcSSimon Schubert 	    goto done;
724*5796c8dcSSimon Schubert     }
725*5796c8dcSSimon Schubert 
726*5796c8dcSSimon Schubert   /* /foo => foo, to avoid multiple slashes that Emacs doesn't like. */
727*5796c8dcSSimon Schubert   while (IS_DIR_SEPARATOR(string[0]))
728*5796c8dcSSimon Schubert     string++;
729*5796c8dcSSimon Schubert 
730*5796c8dcSSimon Schubert   /* ./foo => foo */
731*5796c8dcSSimon Schubert   while (string[0] == '.' && IS_DIR_SEPARATOR (string[1]))
732*5796c8dcSSimon Schubert     string += 2;
733*5796c8dcSSimon Schubert 
734*5796c8dcSSimon Schubert   alloclen = strlen (path) + strlen (string) + 2;
735*5796c8dcSSimon Schubert   filename = alloca (alloclen);
736*5796c8dcSSimon Schubert   fd = -1;
737*5796c8dcSSimon Schubert   for (p = path; p; p = p1 ? p1 + 1 : 0)
738*5796c8dcSSimon Schubert     {
739*5796c8dcSSimon Schubert       p1 = strchr (p, DIRNAME_SEPARATOR);
740*5796c8dcSSimon Schubert       if (p1)
741*5796c8dcSSimon Schubert 	len = p1 - p;
742*5796c8dcSSimon Schubert       else
743*5796c8dcSSimon Schubert 	len = strlen (p);
744*5796c8dcSSimon Schubert 
745*5796c8dcSSimon Schubert       if (len == 4 && p[0] == '$' && p[1] == 'c'
746*5796c8dcSSimon Schubert 	  && p[2] == 'w' && p[3] == 'd')
747*5796c8dcSSimon Schubert 	{
748*5796c8dcSSimon Schubert 	  /* Name is $cwd -- insert current directory name instead.  */
749*5796c8dcSSimon Schubert 	  int newlen;
750*5796c8dcSSimon Schubert 
751*5796c8dcSSimon Schubert 	  /* First, realloc the filename buffer if too short. */
752*5796c8dcSSimon Schubert 	  len = strlen (current_directory);
753*5796c8dcSSimon Schubert 	  newlen = len + strlen (string) + 2;
754*5796c8dcSSimon Schubert 	  if (newlen > alloclen)
755*5796c8dcSSimon Schubert 	    {
756*5796c8dcSSimon Schubert 	      alloclen = newlen;
757*5796c8dcSSimon Schubert 	      filename = alloca (alloclen);
758*5796c8dcSSimon Schubert 	    }
759*5796c8dcSSimon Schubert 	  strcpy (filename, current_directory);
760*5796c8dcSSimon Schubert 	}
761*5796c8dcSSimon Schubert       else
762*5796c8dcSSimon Schubert 	{
763*5796c8dcSSimon Schubert 	  /* Normal file name in path -- just use it.  */
764*5796c8dcSSimon Schubert 	  strncpy (filename, p, len);
765*5796c8dcSSimon Schubert 	  filename[len] = 0;
766*5796c8dcSSimon Schubert 	}
767*5796c8dcSSimon Schubert 
768*5796c8dcSSimon Schubert       /* Remove trailing slashes */
769*5796c8dcSSimon Schubert       while (len > 0 && IS_DIR_SEPARATOR (filename[len - 1]))
770*5796c8dcSSimon Schubert 	filename[--len] = 0;
771*5796c8dcSSimon Schubert 
772*5796c8dcSSimon Schubert       strcat (filename + len, SLASH_STRING);
773*5796c8dcSSimon Schubert       strcat (filename, string);
774*5796c8dcSSimon Schubert 
775*5796c8dcSSimon Schubert       if (is_regular_file (filename))
776*5796c8dcSSimon Schubert 	{
777*5796c8dcSSimon Schubert 	  fd = open (filename, mode);
778*5796c8dcSSimon Schubert 	  if (fd >= 0)
779*5796c8dcSSimon Schubert 	    break;
780*5796c8dcSSimon Schubert 	}
781*5796c8dcSSimon Schubert     }
782*5796c8dcSSimon Schubert 
783*5796c8dcSSimon Schubert done:
784*5796c8dcSSimon Schubert   if (filename_opened)
785*5796c8dcSSimon Schubert     {
786*5796c8dcSSimon Schubert       /* If a file was opened, canonicalize its filename. Use xfullpath
787*5796c8dcSSimon Schubert          rather than gdb_realpath to avoid resolving the basename part
788*5796c8dcSSimon Schubert          of filenames when the associated file is a symbolic link. This
789*5796c8dcSSimon Schubert          fixes a potential inconsistency between the filenames known to
790*5796c8dcSSimon Schubert          GDB and the filenames it prints in the annotations.  */
791*5796c8dcSSimon Schubert       if (fd < 0)
792*5796c8dcSSimon Schubert 	*filename_opened = NULL;
793*5796c8dcSSimon Schubert       else if (IS_ABSOLUTE_PATH (filename))
794*5796c8dcSSimon Schubert 	*filename_opened = xfullpath (filename);
795*5796c8dcSSimon Schubert       else
796*5796c8dcSSimon Schubert 	{
797*5796c8dcSSimon Schubert 	  /* Beware the // my son, the Emacs barfs, the botch that catch... */
798*5796c8dcSSimon Schubert 
799*5796c8dcSSimon Schubert 	  char *f = concat (current_directory,
800*5796c8dcSSimon Schubert 			    IS_DIR_SEPARATOR (current_directory[strlen (current_directory) - 1])
801*5796c8dcSSimon Schubert 			    ? "" : SLASH_STRING,
802*5796c8dcSSimon Schubert 			    filename, (char *)NULL);
803*5796c8dcSSimon Schubert 	  *filename_opened = xfullpath (f);
804*5796c8dcSSimon Schubert 	  xfree (f);
805*5796c8dcSSimon Schubert 	}
806*5796c8dcSSimon Schubert     }
807*5796c8dcSSimon Schubert 
808*5796c8dcSSimon Schubert   return fd;
809*5796c8dcSSimon Schubert }
810*5796c8dcSSimon Schubert 
811*5796c8dcSSimon Schubert 
812*5796c8dcSSimon Schubert /* This is essentially a convenience, for clients that want the behaviour
813*5796c8dcSSimon Schubert    of openp, using source_path, but that really don't want the file to be
814*5796c8dcSSimon Schubert    opened but want instead just to know what the full pathname is (as
815*5796c8dcSSimon Schubert    qualified against source_path).
816*5796c8dcSSimon Schubert 
817*5796c8dcSSimon Schubert    The current working directory is searched first.
818*5796c8dcSSimon Schubert 
819*5796c8dcSSimon Schubert    If the file was found, this function returns 1, and FULL_PATHNAME is
820*5796c8dcSSimon Schubert    set to the fully-qualified pathname.
821*5796c8dcSSimon Schubert 
822*5796c8dcSSimon Schubert    Else, this functions returns 0, and FULL_PATHNAME is set to NULL.  */
823*5796c8dcSSimon Schubert int
824*5796c8dcSSimon Schubert source_full_path_of (const char *filename, char **full_pathname)
825*5796c8dcSSimon Schubert {
826*5796c8dcSSimon Schubert   int fd;
827*5796c8dcSSimon Schubert 
828*5796c8dcSSimon Schubert   fd = openp (source_path, OPF_TRY_CWD_FIRST | OPF_SEARCH_IN_PATH, filename,
829*5796c8dcSSimon Schubert 	      O_RDONLY, full_pathname);
830*5796c8dcSSimon Schubert   if (fd < 0)
831*5796c8dcSSimon Schubert     {
832*5796c8dcSSimon Schubert       *full_pathname = NULL;
833*5796c8dcSSimon Schubert       return 0;
834*5796c8dcSSimon Schubert     }
835*5796c8dcSSimon Schubert 
836*5796c8dcSSimon Schubert   close (fd);
837*5796c8dcSSimon Schubert   return 1;
838*5796c8dcSSimon Schubert }
839*5796c8dcSSimon Schubert 
840*5796c8dcSSimon Schubert /* Return non-zero if RULE matches PATH, that is if the rule can be
841*5796c8dcSSimon Schubert    applied to PATH.  */
842*5796c8dcSSimon Schubert 
843*5796c8dcSSimon Schubert static int
844*5796c8dcSSimon Schubert substitute_path_rule_matches (const struct substitute_path_rule *rule,
845*5796c8dcSSimon Schubert                               const char *path)
846*5796c8dcSSimon Schubert {
847*5796c8dcSSimon Schubert   const int from_len = strlen (rule->from);
848*5796c8dcSSimon Schubert   const int path_len = strlen (path);
849*5796c8dcSSimon Schubert   char *path_start;
850*5796c8dcSSimon Schubert 
851*5796c8dcSSimon Schubert   if (path_len < from_len)
852*5796c8dcSSimon Schubert     return 0;
853*5796c8dcSSimon Schubert 
854*5796c8dcSSimon Schubert   /* The substitution rules are anchored at the start of the path,
855*5796c8dcSSimon Schubert      so the path should start with rule->from.  There is no filename
856*5796c8dcSSimon Schubert      comparison routine, so we need to extract the first FROM_LEN
857*5796c8dcSSimon Schubert      characters from PATH first and use that to do the comparison.  */
858*5796c8dcSSimon Schubert 
859*5796c8dcSSimon Schubert   path_start = alloca (from_len + 1);
860*5796c8dcSSimon Schubert   strncpy (path_start, path, from_len);
861*5796c8dcSSimon Schubert   path_start[from_len] = '\0';
862*5796c8dcSSimon Schubert 
863*5796c8dcSSimon Schubert   if (FILENAME_CMP (path_start, rule->from) != 0)
864*5796c8dcSSimon Schubert     return 0;
865*5796c8dcSSimon Schubert 
866*5796c8dcSSimon Schubert   /* Make sure that the region in the path that matches the substitution
867*5796c8dcSSimon Schubert      rule is immediately followed by a directory separator (or the end of
868*5796c8dcSSimon Schubert      string character).  */
869*5796c8dcSSimon Schubert 
870*5796c8dcSSimon Schubert   if (path[from_len] != '\0' && !IS_DIR_SEPARATOR (path[from_len]))
871*5796c8dcSSimon Schubert     return 0;
872*5796c8dcSSimon Schubert 
873*5796c8dcSSimon Schubert   return 1;
874*5796c8dcSSimon Schubert }
875*5796c8dcSSimon Schubert 
876*5796c8dcSSimon Schubert /* Find the substitute-path rule that applies to PATH and return it.
877*5796c8dcSSimon Schubert    Return NULL if no rule applies.  */
878*5796c8dcSSimon Schubert 
879*5796c8dcSSimon Schubert static struct substitute_path_rule *
880*5796c8dcSSimon Schubert get_substitute_path_rule (const char *path)
881*5796c8dcSSimon Schubert {
882*5796c8dcSSimon Schubert   struct substitute_path_rule *rule = substitute_path_rules;
883*5796c8dcSSimon Schubert 
884*5796c8dcSSimon Schubert   while (rule != NULL && !substitute_path_rule_matches (rule, path))
885*5796c8dcSSimon Schubert     rule = rule->next;
886*5796c8dcSSimon Schubert 
887*5796c8dcSSimon Schubert   return rule;
888*5796c8dcSSimon Schubert }
889*5796c8dcSSimon Schubert 
890*5796c8dcSSimon Schubert /* If the user specified a source path substitution rule that applies
891*5796c8dcSSimon Schubert    to PATH, then apply it and return the new path.  This new path must
892*5796c8dcSSimon Schubert    be deallocated afterwards.
893*5796c8dcSSimon Schubert 
894*5796c8dcSSimon Schubert    Return NULL if no substitution rule was specified by the user,
895*5796c8dcSSimon Schubert    or if no rule applied to the given PATH.  */
896*5796c8dcSSimon Schubert 
897*5796c8dcSSimon Schubert static char *
898*5796c8dcSSimon Schubert rewrite_source_path (const char *path)
899*5796c8dcSSimon Schubert {
900*5796c8dcSSimon Schubert   const struct substitute_path_rule *rule = get_substitute_path_rule (path);
901*5796c8dcSSimon Schubert   char *new_path;
902*5796c8dcSSimon Schubert   int from_len;
903*5796c8dcSSimon Schubert 
904*5796c8dcSSimon Schubert   if (rule == NULL)
905*5796c8dcSSimon Schubert     return NULL;
906*5796c8dcSSimon Schubert 
907*5796c8dcSSimon Schubert   from_len = strlen (rule->from);
908*5796c8dcSSimon Schubert 
909*5796c8dcSSimon Schubert   /* Compute the rewritten path and return it.  */
910*5796c8dcSSimon Schubert 
911*5796c8dcSSimon Schubert   new_path =
912*5796c8dcSSimon Schubert     (char *) xmalloc (strlen (path) + 1 + strlen (rule->to) - from_len);
913*5796c8dcSSimon Schubert   strcpy (new_path, rule->to);
914*5796c8dcSSimon Schubert   strcat (new_path, path + from_len);
915*5796c8dcSSimon Schubert 
916*5796c8dcSSimon Schubert   return new_path;
917*5796c8dcSSimon Schubert }
918*5796c8dcSSimon Schubert 
919*5796c8dcSSimon Schubert /* This function is capable of finding the absolute path to a
920*5796c8dcSSimon Schubert    source file, and opening it, provided you give it a FILENAME. Both the
921*5796c8dcSSimon Schubert    DIRNAME and FULLNAME are only added suggestions on where to find the file.
922*5796c8dcSSimon Schubert 
923*5796c8dcSSimon Schubert    FILENAME should be the filename to open.
924*5796c8dcSSimon Schubert    DIRNAME is the compilation directory of a particular source file.
925*5796c8dcSSimon Schubert            Only some debug formats provide this info.
926*5796c8dcSSimon Schubert    FULLNAME can be the last known absolute path to the file in question.
927*5796c8dcSSimon Schubert      Space for the path must have been malloc'd.  If a path substitution
928*5796c8dcSSimon Schubert      is applied we free the old value and set a new one.
929*5796c8dcSSimon Schubert 
930*5796c8dcSSimon Schubert    On Success
931*5796c8dcSSimon Schubert      A valid file descriptor is returned. ( the return value is positive )
932*5796c8dcSSimon Schubert      FULLNAME is set to the absolute path to the file just opened.
933*5796c8dcSSimon Schubert      The caller is responsible for freeing FULLNAME.
934*5796c8dcSSimon Schubert 
935*5796c8dcSSimon Schubert    On Failure
936*5796c8dcSSimon Schubert      An invalid file descriptor is returned. ( the return value is negative )
937*5796c8dcSSimon Schubert      FULLNAME is set to NULL.  */
938*5796c8dcSSimon Schubert 
939*5796c8dcSSimon Schubert static int
940*5796c8dcSSimon Schubert find_and_open_source (const char *filename,
941*5796c8dcSSimon Schubert 		      const char *dirname,
942*5796c8dcSSimon Schubert 		      char **fullname)
943*5796c8dcSSimon Schubert {
944*5796c8dcSSimon Schubert   char *path = source_path;
945*5796c8dcSSimon Schubert   const char *p;
946*5796c8dcSSimon Schubert   int result;
947*5796c8dcSSimon Schubert 
948*5796c8dcSSimon Schubert   /* Quick way out if we already know its full name */
949*5796c8dcSSimon Schubert 
950*5796c8dcSSimon Schubert   if (*fullname)
951*5796c8dcSSimon Schubert     {
952*5796c8dcSSimon Schubert       /* The user may have requested that source paths be rewritten
953*5796c8dcSSimon Schubert          according to substitution rules he provided.  If a substitution
954*5796c8dcSSimon Schubert          rule applies to this path, then apply it.  */
955*5796c8dcSSimon Schubert       char *rewritten_fullname = rewrite_source_path (*fullname);
956*5796c8dcSSimon Schubert 
957*5796c8dcSSimon Schubert       if (rewritten_fullname != NULL)
958*5796c8dcSSimon Schubert         {
959*5796c8dcSSimon Schubert           xfree (*fullname);
960*5796c8dcSSimon Schubert           *fullname = rewritten_fullname;
961*5796c8dcSSimon Schubert         }
962*5796c8dcSSimon Schubert 
963*5796c8dcSSimon Schubert       result = open (*fullname, OPEN_MODE);
964*5796c8dcSSimon Schubert       if (result >= 0)
965*5796c8dcSSimon Schubert 	return result;
966*5796c8dcSSimon Schubert       /* Didn't work -- free old one, try again. */
967*5796c8dcSSimon Schubert       xfree (*fullname);
968*5796c8dcSSimon Schubert       *fullname = NULL;
969*5796c8dcSSimon Schubert     }
970*5796c8dcSSimon Schubert 
971*5796c8dcSSimon Schubert   if (dirname != NULL)
972*5796c8dcSSimon Schubert     {
973*5796c8dcSSimon Schubert       /* If necessary, rewrite the compilation directory name according
974*5796c8dcSSimon Schubert          to the source path substitution rules specified by the user.  */
975*5796c8dcSSimon Schubert 
976*5796c8dcSSimon Schubert       char *rewritten_dirname = rewrite_source_path (dirname);
977*5796c8dcSSimon Schubert 
978*5796c8dcSSimon Schubert       if (rewritten_dirname != NULL)
979*5796c8dcSSimon Schubert         {
980*5796c8dcSSimon Schubert           make_cleanup (xfree, rewritten_dirname);
981*5796c8dcSSimon Schubert           dirname = rewritten_dirname;
982*5796c8dcSSimon Schubert         }
983*5796c8dcSSimon Schubert 
984*5796c8dcSSimon Schubert       /* Replace a path entry of  $cdir  with the compilation directory name */
985*5796c8dcSSimon Schubert #define	cdir_len	5
986*5796c8dcSSimon Schubert       /* We cast strstr's result in case an ANSIhole has made it const,
987*5796c8dcSSimon Schubert          which produces a "required warning" when assigned to a nonconst. */
988*5796c8dcSSimon Schubert       p = (char *) strstr (source_path, "$cdir");
989*5796c8dcSSimon Schubert       if (p && (p == path || p[-1] == DIRNAME_SEPARATOR)
990*5796c8dcSSimon Schubert 	  && (p[cdir_len] == DIRNAME_SEPARATOR || p[cdir_len] == '\0'))
991*5796c8dcSSimon Schubert 	{
992*5796c8dcSSimon Schubert 	  int len;
993*5796c8dcSSimon Schubert 
994*5796c8dcSSimon Schubert 	  path = (char *)
995*5796c8dcSSimon Schubert 	    alloca (strlen (source_path) + 1 + strlen (dirname) + 1);
996*5796c8dcSSimon Schubert 	  len = p - source_path;
997*5796c8dcSSimon Schubert 	  strncpy (path, source_path, len);	/* Before $cdir */
998*5796c8dcSSimon Schubert 	  strcpy (path + len, dirname);	/* new stuff */
999*5796c8dcSSimon Schubert 	  strcat (path + len, source_path + len + cdir_len);	/* After $cdir */
1000*5796c8dcSSimon Schubert 	}
1001*5796c8dcSSimon Schubert     }
1002*5796c8dcSSimon Schubert 
1003*5796c8dcSSimon Schubert   if (IS_ABSOLUTE_PATH (filename))
1004*5796c8dcSSimon Schubert     {
1005*5796c8dcSSimon Schubert       /* If filename is absolute path, try the source path
1006*5796c8dcSSimon Schubert 	 substitution on it.  */
1007*5796c8dcSSimon Schubert       char *rewritten_filename = rewrite_source_path (filename);
1008*5796c8dcSSimon Schubert 
1009*5796c8dcSSimon Schubert       if (rewritten_filename != NULL)
1010*5796c8dcSSimon Schubert         {
1011*5796c8dcSSimon Schubert           make_cleanup (xfree, rewritten_filename);
1012*5796c8dcSSimon Schubert           filename = rewritten_filename;
1013*5796c8dcSSimon Schubert         }
1014*5796c8dcSSimon Schubert     }
1015*5796c8dcSSimon Schubert 
1016*5796c8dcSSimon Schubert   result = openp (path, OPF_SEARCH_IN_PATH, filename, OPEN_MODE, fullname);
1017*5796c8dcSSimon Schubert   if (result < 0)
1018*5796c8dcSSimon Schubert     {
1019*5796c8dcSSimon Schubert       /* Didn't work.  Try using just the basename. */
1020*5796c8dcSSimon Schubert       p = lbasename (filename);
1021*5796c8dcSSimon Schubert       if (p != filename)
1022*5796c8dcSSimon Schubert 	result = openp (path, OPF_SEARCH_IN_PATH, p, OPEN_MODE, fullname);
1023*5796c8dcSSimon Schubert     }
1024*5796c8dcSSimon Schubert 
1025*5796c8dcSSimon Schubert   return result;
1026*5796c8dcSSimon Schubert }
1027*5796c8dcSSimon Schubert 
1028*5796c8dcSSimon Schubert /* Open a source file given a symtab S.  Returns a file descriptor or
1029*5796c8dcSSimon Schubert    negative number for error.
1030*5796c8dcSSimon Schubert 
1031*5796c8dcSSimon Schubert    This function is a convience function to find_and_open_source. */
1032*5796c8dcSSimon Schubert 
1033*5796c8dcSSimon Schubert int
1034*5796c8dcSSimon Schubert open_source_file (struct symtab *s)
1035*5796c8dcSSimon Schubert {
1036*5796c8dcSSimon Schubert   if (!s)
1037*5796c8dcSSimon Schubert     return -1;
1038*5796c8dcSSimon Schubert 
1039*5796c8dcSSimon Schubert   return find_and_open_source (s->filename, s->dirname, &s->fullname);
1040*5796c8dcSSimon Schubert }
1041*5796c8dcSSimon Schubert 
1042*5796c8dcSSimon Schubert /* Finds the fullname that a symtab represents.
1043*5796c8dcSSimon Schubert 
1044*5796c8dcSSimon Schubert    If this functions finds the fullname, it will save it in s->fullname
1045*5796c8dcSSimon Schubert    and it will also return the value.
1046*5796c8dcSSimon Schubert 
1047*5796c8dcSSimon Schubert    If this function fails to find the file that this symtab represents,
1048*5796c8dcSSimon Schubert    NULL will be returned and s->fullname will be set to NULL.  */
1049*5796c8dcSSimon Schubert char *
1050*5796c8dcSSimon Schubert symtab_to_fullname (struct symtab *s)
1051*5796c8dcSSimon Schubert {
1052*5796c8dcSSimon Schubert   int r;
1053*5796c8dcSSimon Schubert 
1054*5796c8dcSSimon Schubert   if (!s)
1055*5796c8dcSSimon Schubert     return NULL;
1056*5796c8dcSSimon Schubert 
1057*5796c8dcSSimon Schubert   /* Don't check s->fullname here, the file could have been
1058*5796c8dcSSimon Schubert      deleted/moved/..., look for it again */
1059*5796c8dcSSimon Schubert   r = find_and_open_source (s->filename, s->dirname, &s->fullname);
1060*5796c8dcSSimon Schubert 
1061*5796c8dcSSimon Schubert   if (r >= 0)
1062*5796c8dcSSimon Schubert     {
1063*5796c8dcSSimon Schubert       close (r);
1064*5796c8dcSSimon Schubert       return s->fullname;
1065*5796c8dcSSimon Schubert     }
1066*5796c8dcSSimon Schubert 
1067*5796c8dcSSimon Schubert   return NULL;
1068*5796c8dcSSimon Schubert }
1069*5796c8dcSSimon Schubert 
1070*5796c8dcSSimon Schubert /* Finds the fullname that a partial_symtab represents.
1071*5796c8dcSSimon Schubert 
1072*5796c8dcSSimon Schubert    If this functions finds the fullname, it will save it in ps->fullname
1073*5796c8dcSSimon Schubert    and it will also return the value.
1074*5796c8dcSSimon Schubert 
1075*5796c8dcSSimon Schubert    If this function fails to find the file that this partial_symtab represents,
1076*5796c8dcSSimon Schubert    NULL will be returned and ps->fullname will be set to NULL.  */
1077*5796c8dcSSimon Schubert char *
1078*5796c8dcSSimon Schubert psymtab_to_fullname (struct partial_symtab *ps)
1079*5796c8dcSSimon Schubert {
1080*5796c8dcSSimon Schubert   int r;
1081*5796c8dcSSimon Schubert 
1082*5796c8dcSSimon Schubert   if (!ps)
1083*5796c8dcSSimon Schubert     return NULL;
1084*5796c8dcSSimon Schubert 
1085*5796c8dcSSimon Schubert   /* Don't check ps->fullname here, the file could have been
1086*5796c8dcSSimon Schubert      deleted/moved/..., look for it again */
1087*5796c8dcSSimon Schubert   r = find_and_open_source (ps->filename, ps->dirname, &ps->fullname);
1088*5796c8dcSSimon Schubert 
1089*5796c8dcSSimon Schubert   if (r >= 0)
1090*5796c8dcSSimon Schubert     {
1091*5796c8dcSSimon Schubert       close (r);
1092*5796c8dcSSimon Schubert       return ps->fullname;
1093*5796c8dcSSimon Schubert     }
1094*5796c8dcSSimon Schubert 
1095*5796c8dcSSimon Schubert   return NULL;
1096*5796c8dcSSimon Schubert }
1097*5796c8dcSSimon Schubert 
1098*5796c8dcSSimon Schubert /* Create and initialize the table S->line_charpos that records
1099*5796c8dcSSimon Schubert    the positions of the lines in the source file, which is assumed
1100*5796c8dcSSimon Schubert    to be open on descriptor DESC.
1101*5796c8dcSSimon Schubert    All set S->nlines to the number of such lines.  */
1102*5796c8dcSSimon Schubert 
1103*5796c8dcSSimon Schubert void
1104*5796c8dcSSimon Schubert find_source_lines (struct symtab *s, int desc)
1105*5796c8dcSSimon Schubert {
1106*5796c8dcSSimon Schubert   struct stat st;
1107*5796c8dcSSimon Schubert   char *data, *p, *end;
1108*5796c8dcSSimon Schubert   int nlines = 0;
1109*5796c8dcSSimon Schubert   int lines_allocated = 1000;
1110*5796c8dcSSimon Schubert   int *line_charpos;
1111*5796c8dcSSimon Schubert   long mtime = 0;
1112*5796c8dcSSimon Schubert   int size;
1113*5796c8dcSSimon Schubert 
1114*5796c8dcSSimon Schubert   gdb_assert (s);
1115*5796c8dcSSimon Schubert   line_charpos = (int *) xmalloc (lines_allocated * sizeof (int));
1116*5796c8dcSSimon Schubert   if (fstat (desc, &st) < 0)
1117*5796c8dcSSimon Schubert     perror_with_name (s->filename);
1118*5796c8dcSSimon Schubert 
1119*5796c8dcSSimon Schubert   if (s->objfile && s->objfile->obfd)
1120*5796c8dcSSimon Schubert     mtime = s->objfile->mtime;
1121*5796c8dcSSimon Schubert   else if (exec_bfd)
1122*5796c8dcSSimon Schubert     mtime = exec_bfd_mtime;
1123*5796c8dcSSimon Schubert 
1124*5796c8dcSSimon Schubert   if (mtime && mtime < st.st_mtime)
1125*5796c8dcSSimon Schubert     warning (_("Source file is more recent than executable."));
1126*5796c8dcSSimon Schubert 
1127*5796c8dcSSimon Schubert #ifdef LSEEK_NOT_LINEAR
1128*5796c8dcSSimon Schubert   {
1129*5796c8dcSSimon Schubert     char c;
1130*5796c8dcSSimon Schubert 
1131*5796c8dcSSimon Schubert     /* Have to read it byte by byte to find out where the chars live */
1132*5796c8dcSSimon Schubert 
1133*5796c8dcSSimon Schubert     line_charpos[0] = lseek (desc, 0, SEEK_CUR);
1134*5796c8dcSSimon Schubert     nlines = 1;
1135*5796c8dcSSimon Schubert     while (myread (desc, &c, 1) > 0)
1136*5796c8dcSSimon Schubert       {
1137*5796c8dcSSimon Schubert 	if (c == '\n')
1138*5796c8dcSSimon Schubert 	  {
1139*5796c8dcSSimon Schubert 	    if (nlines == lines_allocated)
1140*5796c8dcSSimon Schubert 	      {
1141*5796c8dcSSimon Schubert 		lines_allocated *= 2;
1142*5796c8dcSSimon Schubert 		line_charpos =
1143*5796c8dcSSimon Schubert 		  (int *) xrealloc ((char *) line_charpos,
1144*5796c8dcSSimon Schubert 				    sizeof (int) * lines_allocated);
1145*5796c8dcSSimon Schubert 	      }
1146*5796c8dcSSimon Schubert 	    line_charpos[nlines++] = lseek (desc, 0, SEEK_CUR);
1147*5796c8dcSSimon Schubert 	  }
1148*5796c8dcSSimon Schubert       }
1149*5796c8dcSSimon Schubert   }
1150*5796c8dcSSimon Schubert #else /* lseek linear.  */
1151*5796c8dcSSimon Schubert   {
1152*5796c8dcSSimon Schubert     struct cleanup *old_cleanups;
1153*5796c8dcSSimon Schubert 
1154*5796c8dcSSimon Schubert     /* st_size might be a large type, but we only support source files whose
1155*5796c8dcSSimon Schubert        size fits in an int.  */
1156*5796c8dcSSimon Schubert     size = (int) st.st_size;
1157*5796c8dcSSimon Schubert 
1158*5796c8dcSSimon Schubert     /* Use malloc, not alloca, because this may be pretty large, and we may
1159*5796c8dcSSimon Schubert        run into various kinds of limits on stack size.  */
1160*5796c8dcSSimon Schubert     data = (char *) xmalloc (size);
1161*5796c8dcSSimon Schubert     old_cleanups = make_cleanup (xfree, data);
1162*5796c8dcSSimon Schubert 
1163*5796c8dcSSimon Schubert     /* Reassign `size' to result of read for systems where \r\n -> \n.  */
1164*5796c8dcSSimon Schubert     size = myread (desc, data, size);
1165*5796c8dcSSimon Schubert     if (size < 0)
1166*5796c8dcSSimon Schubert       perror_with_name (s->filename);
1167*5796c8dcSSimon Schubert     end = data + size;
1168*5796c8dcSSimon Schubert     p = data;
1169*5796c8dcSSimon Schubert     line_charpos[0] = 0;
1170*5796c8dcSSimon Schubert     nlines = 1;
1171*5796c8dcSSimon Schubert     while (p != end)
1172*5796c8dcSSimon Schubert       {
1173*5796c8dcSSimon Schubert 	if (*p++ == '\n'
1174*5796c8dcSSimon Schubert 	/* A newline at the end does not start a new line.  */
1175*5796c8dcSSimon Schubert 	    && p != end)
1176*5796c8dcSSimon Schubert 	  {
1177*5796c8dcSSimon Schubert 	    if (nlines == lines_allocated)
1178*5796c8dcSSimon Schubert 	      {
1179*5796c8dcSSimon Schubert 		lines_allocated *= 2;
1180*5796c8dcSSimon Schubert 		line_charpos =
1181*5796c8dcSSimon Schubert 		  (int *) xrealloc ((char *) line_charpos,
1182*5796c8dcSSimon Schubert 				    sizeof (int) * lines_allocated);
1183*5796c8dcSSimon Schubert 	      }
1184*5796c8dcSSimon Schubert 	    line_charpos[nlines++] = p - data;
1185*5796c8dcSSimon Schubert 	  }
1186*5796c8dcSSimon Schubert       }
1187*5796c8dcSSimon Schubert     do_cleanups (old_cleanups);
1188*5796c8dcSSimon Schubert   }
1189*5796c8dcSSimon Schubert #endif /* lseek linear.  */
1190*5796c8dcSSimon Schubert   s->nlines = nlines;
1191*5796c8dcSSimon Schubert   s->line_charpos =
1192*5796c8dcSSimon Schubert     (int *) xrealloc ((char *) line_charpos, nlines * sizeof (int));
1193*5796c8dcSSimon Schubert 
1194*5796c8dcSSimon Schubert }
1195*5796c8dcSSimon Schubert 
1196*5796c8dcSSimon Schubert /* Return the character position of a line LINE in symtab S.
1197*5796c8dcSSimon Schubert    Return 0 if anything is invalid.  */
1198*5796c8dcSSimon Schubert 
1199*5796c8dcSSimon Schubert #if 0				/* Currently unused */
1200*5796c8dcSSimon Schubert 
1201*5796c8dcSSimon Schubert int
1202*5796c8dcSSimon Schubert source_line_charpos (struct symtab *s, int line)
1203*5796c8dcSSimon Schubert {
1204*5796c8dcSSimon Schubert   if (!s)
1205*5796c8dcSSimon Schubert     return 0;
1206*5796c8dcSSimon Schubert   if (!s->line_charpos || line <= 0)
1207*5796c8dcSSimon Schubert     return 0;
1208*5796c8dcSSimon Schubert   if (line > s->nlines)
1209*5796c8dcSSimon Schubert     line = s->nlines;
1210*5796c8dcSSimon Schubert   return s->line_charpos[line - 1];
1211*5796c8dcSSimon Schubert }
1212*5796c8dcSSimon Schubert 
1213*5796c8dcSSimon Schubert /* Return the line number of character position POS in symtab S.  */
1214*5796c8dcSSimon Schubert 
1215*5796c8dcSSimon Schubert int
1216*5796c8dcSSimon Schubert source_charpos_line (struct symtab *s, int chr)
1217*5796c8dcSSimon Schubert {
1218*5796c8dcSSimon Schubert   int line = 0;
1219*5796c8dcSSimon Schubert   int *lnp;
1220*5796c8dcSSimon Schubert 
1221*5796c8dcSSimon Schubert   if (s == 0 || s->line_charpos == 0)
1222*5796c8dcSSimon Schubert     return 0;
1223*5796c8dcSSimon Schubert   lnp = s->line_charpos;
1224*5796c8dcSSimon Schubert   /* Files are usually short, so sequential search is Ok */
1225*5796c8dcSSimon Schubert   while (line < s->nlines && *lnp <= chr)
1226*5796c8dcSSimon Schubert     {
1227*5796c8dcSSimon Schubert       line++;
1228*5796c8dcSSimon Schubert       lnp++;
1229*5796c8dcSSimon Schubert     }
1230*5796c8dcSSimon Schubert   if (line >= s->nlines)
1231*5796c8dcSSimon Schubert     line = s->nlines;
1232*5796c8dcSSimon Schubert   return line;
1233*5796c8dcSSimon Schubert }
1234*5796c8dcSSimon Schubert 
1235*5796c8dcSSimon Schubert #endif /* 0 */
1236*5796c8dcSSimon Schubert 
1237*5796c8dcSSimon Schubert 
1238*5796c8dcSSimon Schubert /* Get full pathname and line number positions for a symtab.
1239*5796c8dcSSimon Schubert    Return nonzero if line numbers may have changed.
1240*5796c8dcSSimon Schubert    Set *FULLNAME to actual name of the file as found by `openp',
1241*5796c8dcSSimon Schubert    or to 0 if the file is not found.  */
1242*5796c8dcSSimon Schubert 
1243*5796c8dcSSimon Schubert static int
1244*5796c8dcSSimon Schubert get_filename_and_charpos (struct symtab *s, char **fullname)
1245*5796c8dcSSimon Schubert {
1246*5796c8dcSSimon Schubert   int desc, linenums_changed = 0;
1247*5796c8dcSSimon Schubert   struct cleanup *cleanups;
1248*5796c8dcSSimon Schubert 
1249*5796c8dcSSimon Schubert   desc = open_source_file (s);
1250*5796c8dcSSimon Schubert   if (desc < 0)
1251*5796c8dcSSimon Schubert     {
1252*5796c8dcSSimon Schubert       if (fullname)
1253*5796c8dcSSimon Schubert 	*fullname = NULL;
1254*5796c8dcSSimon Schubert       return 0;
1255*5796c8dcSSimon Schubert     }
1256*5796c8dcSSimon Schubert   cleanups = make_cleanup_close (desc);
1257*5796c8dcSSimon Schubert   if (fullname)
1258*5796c8dcSSimon Schubert     *fullname = s->fullname;
1259*5796c8dcSSimon Schubert   if (s->line_charpos == 0)
1260*5796c8dcSSimon Schubert     linenums_changed = 1;
1261*5796c8dcSSimon Schubert   if (linenums_changed)
1262*5796c8dcSSimon Schubert     find_source_lines (s, desc);
1263*5796c8dcSSimon Schubert   do_cleanups (cleanups);
1264*5796c8dcSSimon Schubert   return linenums_changed;
1265*5796c8dcSSimon Schubert }
1266*5796c8dcSSimon Schubert 
1267*5796c8dcSSimon Schubert /* Print text describing the full name of the source file S
1268*5796c8dcSSimon Schubert    and the line number LINE and its corresponding character position.
1269*5796c8dcSSimon Schubert    The text starts with two Ctrl-z so that the Emacs-GDB interface
1270*5796c8dcSSimon Schubert    can easily find it.
1271*5796c8dcSSimon Schubert 
1272*5796c8dcSSimon Schubert    MID_STATEMENT is nonzero if the PC is not at the beginning of that line.
1273*5796c8dcSSimon Schubert 
1274*5796c8dcSSimon Schubert    Return 1 if successful, 0 if could not find the file.  */
1275*5796c8dcSSimon Schubert 
1276*5796c8dcSSimon Schubert int
1277*5796c8dcSSimon Schubert identify_source_line (struct symtab *s, int line, int mid_statement,
1278*5796c8dcSSimon Schubert 		      CORE_ADDR pc)
1279*5796c8dcSSimon Schubert {
1280*5796c8dcSSimon Schubert   if (s->line_charpos == 0)
1281*5796c8dcSSimon Schubert     get_filename_and_charpos (s, (char **) NULL);
1282*5796c8dcSSimon Schubert   if (s->fullname == 0)
1283*5796c8dcSSimon Schubert     return 0;
1284*5796c8dcSSimon Schubert   if (line > s->nlines)
1285*5796c8dcSSimon Schubert     /* Don't index off the end of the line_charpos array.  */
1286*5796c8dcSSimon Schubert     return 0;
1287*5796c8dcSSimon Schubert   annotate_source (s->fullname, line, s->line_charpos[line - 1],
1288*5796c8dcSSimon Schubert 		   mid_statement, get_objfile_arch (s->objfile), pc);
1289*5796c8dcSSimon Schubert 
1290*5796c8dcSSimon Schubert   current_source_line = line;
1291*5796c8dcSSimon Schubert   first_line_listed = line;
1292*5796c8dcSSimon Schubert   last_line_listed = line;
1293*5796c8dcSSimon Schubert   current_source_symtab = s;
1294*5796c8dcSSimon Schubert   return 1;
1295*5796c8dcSSimon Schubert }
1296*5796c8dcSSimon Schubert 
1297*5796c8dcSSimon Schubert 
1298*5796c8dcSSimon Schubert /* Print source lines from the file of symtab S,
1299*5796c8dcSSimon Schubert    starting with line number LINE and stopping before line number STOPLINE. */
1300*5796c8dcSSimon Schubert 
1301*5796c8dcSSimon Schubert static void print_source_lines_base (struct symtab *s, int line, int stopline,
1302*5796c8dcSSimon Schubert 				     int noerror);
1303*5796c8dcSSimon Schubert static void
1304*5796c8dcSSimon Schubert print_source_lines_base (struct symtab *s, int line, int stopline, int noerror)
1305*5796c8dcSSimon Schubert {
1306*5796c8dcSSimon Schubert   int c;
1307*5796c8dcSSimon Schubert   int desc;
1308*5796c8dcSSimon Schubert   FILE *stream;
1309*5796c8dcSSimon Schubert   int nlines = stopline - line;
1310*5796c8dcSSimon Schubert   struct cleanup *cleanup;
1311*5796c8dcSSimon Schubert 
1312*5796c8dcSSimon Schubert   /* Regardless of whether we can open the file, set current_source_symtab. */
1313*5796c8dcSSimon Schubert   current_source_symtab = s;
1314*5796c8dcSSimon Schubert   current_source_line = line;
1315*5796c8dcSSimon Schubert   first_line_listed = line;
1316*5796c8dcSSimon Schubert 
1317*5796c8dcSSimon Schubert   /* If printing of source lines is disabled, just print file and line number */
1318*5796c8dcSSimon Schubert   if (ui_out_test_flags (uiout, ui_source_list))
1319*5796c8dcSSimon Schubert     {
1320*5796c8dcSSimon Schubert       /* Only prints "No such file or directory" once */
1321*5796c8dcSSimon Schubert       if ((s != last_source_visited) || (!last_source_error))
1322*5796c8dcSSimon Schubert 	{
1323*5796c8dcSSimon Schubert 	  last_source_visited = s;
1324*5796c8dcSSimon Schubert 	  desc = open_source_file (s);
1325*5796c8dcSSimon Schubert 	}
1326*5796c8dcSSimon Schubert       else
1327*5796c8dcSSimon Schubert 	{
1328*5796c8dcSSimon Schubert 	  desc = last_source_error;
1329*5796c8dcSSimon Schubert 	  noerror = 1;
1330*5796c8dcSSimon Schubert 	}
1331*5796c8dcSSimon Schubert     }
1332*5796c8dcSSimon Schubert   else
1333*5796c8dcSSimon Schubert     {
1334*5796c8dcSSimon Schubert       desc = -1;
1335*5796c8dcSSimon Schubert       noerror = 1;
1336*5796c8dcSSimon Schubert     }
1337*5796c8dcSSimon Schubert 
1338*5796c8dcSSimon Schubert   if (desc < 0)
1339*5796c8dcSSimon Schubert     {
1340*5796c8dcSSimon Schubert       last_source_error = desc;
1341*5796c8dcSSimon Schubert 
1342*5796c8dcSSimon Schubert       if (!noerror)
1343*5796c8dcSSimon Schubert 	{
1344*5796c8dcSSimon Schubert 	  char *name = alloca (strlen (s->filename) + 100);
1345*5796c8dcSSimon Schubert 	  sprintf (name, "%d\t%s", line, s->filename);
1346*5796c8dcSSimon Schubert 	  print_sys_errmsg (name, errno);
1347*5796c8dcSSimon Schubert 	}
1348*5796c8dcSSimon Schubert       else
1349*5796c8dcSSimon Schubert 	ui_out_field_int (uiout, "line", line);
1350*5796c8dcSSimon Schubert       ui_out_text (uiout, "\tin ");
1351*5796c8dcSSimon Schubert       ui_out_field_string (uiout, "file", s->filename);
1352*5796c8dcSSimon Schubert       ui_out_text (uiout, "\n");
1353*5796c8dcSSimon Schubert 
1354*5796c8dcSSimon Schubert       return;
1355*5796c8dcSSimon Schubert     }
1356*5796c8dcSSimon Schubert 
1357*5796c8dcSSimon Schubert   last_source_error = 0;
1358*5796c8dcSSimon Schubert 
1359*5796c8dcSSimon Schubert   if (s->line_charpos == 0)
1360*5796c8dcSSimon Schubert     find_source_lines (s, desc);
1361*5796c8dcSSimon Schubert 
1362*5796c8dcSSimon Schubert   if (line < 1 || line > s->nlines)
1363*5796c8dcSSimon Schubert     {
1364*5796c8dcSSimon Schubert       close (desc);
1365*5796c8dcSSimon Schubert       error (_("Line number %d out of range; %s has %d lines."),
1366*5796c8dcSSimon Schubert 	     line, s->filename, s->nlines);
1367*5796c8dcSSimon Schubert     }
1368*5796c8dcSSimon Schubert 
1369*5796c8dcSSimon Schubert   if (lseek (desc, s->line_charpos[line - 1], 0) < 0)
1370*5796c8dcSSimon Schubert     {
1371*5796c8dcSSimon Schubert       close (desc);
1372*5796c8dcSSimon Schubert       perror_with_name (s->filename);
1373*5796c8dcSSimon Schubert     }
1374*5796c8dcSSimon Schubert 
1375*5796c8dcSSimon Schubert   stream = fdopen (desc, FDOPEN_MODE);
1376*5796c8dcSSimon Schubert   clearerr (stream);
1377*5796c8dcSSimon Schubert   cleanup = make_cleanup_fclose (stream);
1378*5796c8dcSSimon Schubert 
1379*5796c8dcSSimon Schubert   while (nlines-- > 0)
1380*5796c8dcSSimon Schubert     {
1381*5796c8dcSSimon Schubert       char buf[20];
1382*5796c8dcSSimon Schubert 
1383*5796c8dcSSimon Schubert       c = fgetc (stream);
1384*5796c8dcSSimon Schubert       if (c == EOF)
1385*5796c8dcSSimon Schubert 	break;
1386*5796c8dcSSimon Schubert       last_line_listed = current_source_line;
1387*5796c8dcSSimon Schubert       sprintf (buf, "%d\t", current_source_line++);
1388*5796c8dcSSimon Schubert       ui_out_text (uiout, buf);
1389*5796c8dcSSimon Schubert       do
1390*5796c8dcSSimon Schubert 	{
1391*5796c8dcSSimon Schubert 	  if (c < 040 && c != '\t' && c != '\n' && c != '\r')
1392*5796c8dcSSimon Schubert 	    {
1393*5796c8dcSSimon Schubert 	      sprintf (buf, "^%c", c + 0100);
1394*5796c8dcSSimon Schubert 	      ui_out_text (uiout, buf);
1395*5796c8dcSSimon Schubert 	    }
1396*5796c8dcSSimon Schubert 	  else if (c == 0177)
1397*5796c8dcSSimon Schubert 	    ui_out_text (uiout, "^?");
1398*5796c8dcSSimon Schubert 	  else if (c == '\r')
1399*5796c8dcSSimon Schubert 	    {
1400*5796c8dcSSimon Schubert 	      /* Skip a \r character, but only before a \n.  */
1401*5796c8dcSSimon Schubert 	      int c1 = fgetc (stream);
1402*5796c8dcSSimon Schubert 
1403*5796c8dcSSimon Schubert 	      if (c1 != '\n')
1404*5796c8dcSSimon Schubert 		printf_filtered ("^%c", c + 0100);
1405*5796c8dcSSimon Schubert 	      if (c1 != EOF)
1406*5796c8dcSSimon Schubert 		ungetc (c1, stream);
1407*5796c8dcSSimon Schubert 	    }
1408*5796c8dcSSimon Schubert 	  else
1409*5796c8dcSSimon Schubert 	    {
1410*5796c8dcSSimon Schubert 	      sprintf (buf, "%c", c);
1411*5796c8dcSSimon Schubert 	      ui_out_text (uiout, buf);
1412*5796c8dcSSimon Schubert 	    }
1413*5796c8dcSSimon Schubert 	}
1414*5796c8dcSSimon Schubert       while (c != '\n' && (c = fgetc (stream)) >= 0);
1415*5796c8dcSSimon Schubert     }
1416*5796c8dcSSimon Schubert 
1417*5796c8dcSSimon Schubert   do_cleanups (cleanup);
1418*5796c8dcSSimon Schubert }
1419*5796c8dcSSimon Schubert 
1420*5796c8dcSSimon Schubert /* Show source lines from the file of symtab S, starting with line
1421*5796c8dcSSimon Schubert    number LINE and stopping before line number STOPLINE.  If this is
1422*5796c8dcSSimon Schubert    not the command line version, then the source is shown in the source
1423*5796c8dcSSimon Schubert    window otherwise it is simply printed */
1424*5796c8dcSSimon Schubert 
1425*5796c8dcSSimon Schubert void
1426*5796c8dcSSimon Schubert print_source_lines (struct symtab *s, int line, int stopline, int noerror)
1427*5796c8dcSSimon Schubert {
1428*5796c8dcSSimon Schubert   print_source_lines_base (s, line, stopline, noerror);
1429*5796c8dcSSimon Schubert }
1430*5796c8dcSSimon Schubert 
1431*5796c8dcSSimon Schubert /* Print info on range of pc's in a specified line.  */
1432*5796c8dcSSimon Schubert 
1433*5796c8dcSSimon Schubert static void
1434*5796c8dcSSimon Schubert line_info (char *arg, int from_tty)
1435*5796c8dcSSimon Schubert {
1436*5796c8dcSSimon Schubert   struct symtabs_and_lines sals;
1437*5796c8dcSSimon Schubert   struct symtab_and_line sal;
1438*5796c8dcSSimon Schubert   CORE_ADDR start_pc, end_pc;
1439*5796c8dcSSimon Schubert   int i;
1440*5796c8dcSSimon Schubert 
1441*5796c8dcSSimon Schubert   init_sal (&sal);		/* initialize to zeroes */
1442*5796c8dcSSimon Schubert 
1443*5796c8dcSSimon Schubert   if (arg == 0)
1444*5796c8dcSSimon Schubert     {
1445*5796c8dcSSimon Schubert       sal.symtab = current_source_symtab;
1446*5796c8dcSSimon Schubert       sal.line = last_line_listed;
1447*5796c8dcSSimon Schubert       sals.nelts = 1;
1448*5796c8dcSSimon Schubert       sals.sals = (struct symtab_and_line *)
1449*5796c8dcSSimon Schubert 	xmalloc (sizeof (struct symtab_and_line));
1450*5796c8dcSSimon Schubert       sals.sals[0] = sal;
1451*5796c8dcSSimon Schubert     }
1452*5796c8dcSSimon Schubert   else
1453*5796c8dcSSimon Schubert     {
1454*5796c8dcSSimon Schubert       sals = decode_line_spec_1 (arg, 0);
1455*5796c8dcSSimon Schubert 
1456*5796c8dcSSimon Schubert       dont_repeat ();
1457*5796c8dcSSimon Schubert     }
1458*5796c8dcSSimon Schubert 
1459*5796c8dcSSimon Schubert   /* C++  More than one line may have been specified, as when the user
1460*5796c8dcSSimon Schubert      specifies an overloaded function name. Print info on them all. */
1461*5796c8dcSSimon Schubert   for (i = 0; i < sals.nelts; i++)
1462*5796c8dcSSimon Schubert     {
1463*5796c8dcSSimon Schubert       sal = sals.sals[i];
1464*5796c8dcSSimon Schubert 
1465*5796c8dcSSimon Schubert       if (sal.symtab == 0)
1466*5796c8dcSSimon Schubert 	{
1467*5796c8dcSSimon Schubert 	  struct gdbarch *gdbarch = get_current_arch ();
1468*5796c8dcSSimon Schubert 
1469*5796c8dcSSimon Schubert 	  printf_filtered (_("No line number information available"));
1470*5796c8dcSSimon Schubert 	  if (sal.pc != 0)
1471*5796c8dcSSimon Schubert 	    {
1472*5796c8dcSSimon Schubert 	      /* This is useful for "info line *0x7f34".  If we can't tell the
1473*5796c8dcSSimon Schubert 	         user about a source line, at least let them have the symbolic
1474*5796c8dcSSimon Schubert 	         address.  */
1475*5796c8dcSSimon Schubert 	      printf_filtered (" for address ");
1476*5796c8dcSSimon Schubert 	      wrap_here ("  ");
1477*5796c8dcSSimon Schubert 	      print_address (gdbarch, sal.pc, gdb_stdout);
1478*5796c8dcSSimon Schubert 	    }
1479*5796c8dcSSimon Schubert 	  else
1480*5796c8dcSSimon Schubert 	    printf_filtered (".");
1481*5796c8dcSSimon Schubert 	  printf_filtered ("\n");
1482*5796c8dcSSimon Schubert 	}
1483*5796c8dcSSimon Schubert       else if (sal.line > 0
1484*5796c8dcSSimon Schubert 	       && find_line_pc_range (sal, &start_pc, &end_pc))
1485*5796c8dcSSimon Schubert 	{
1486*5796c8dcSSimon Schubert 	  struct gdbarch *gdbarch = get_objfile_arch (sal.symtab->objfile);
1487*5796c8dcSSimon Schubert 
1488*5796c8dcSSimon Schubert 	  if (start_pc == end_pc)
1489*5796c8dcSSimon Schubert 	    {
1490*5796c8dcSSimon Schubert 	      printf_filtered ("Line %d of \"%s\"",
1491*5796c8dcSSimon Schubert 			       sal.line, sal.symtab->filename);
1492*5796c8dcSSimon Schubert 	      wrap_here ("  ");
1493*5796c8dcSSimon Schubert 	      printf_filtered (" is at address ");
1494*5796c8dcSSimon Schubert 	      print_address (gdbarch, start_pc, gdb_stdout);
1495*5796c8dcSSimon Schubert 	      wrap_here ("  ");
1496*5796c8dcSSimon Schubert 	      printf_filtered (" but contains no code.\n");
1497*5796c8dcSSimon Schubert 	    }
1498*5796c8dcSSimon Schubert 	  else
1499*5796c8dcSSimon Schubert 	    {
1500*5796c8dcSSimon Schubert 	      printf_filtered ("Line %d of \"%s\"",
1501*5796c8dcSSimon Schubert 			       sal.line, sal.symtab->filename);
1502*5796c8dcSSimon Schubert 	      wrap_here ("  ");
1503*5796c8dcSSimon Schubert 	      printf_filtered (" starts at address ");
1504*5796c8dcSSimon Schubert 	      print_address (gdbarch, start_pc, gdb_stdout);
1505*5796c8dcSSimon Schubert 	      wrap_here ("  ");
1506*5796c8dcSSimon Schubert 	      printf_filtered (" and ends at ");
1507*5796c8dcSSimon Schubert 	      print_address (gdbarch, end_pc, gdb_stdout);
1508*5796c8dcSSimon Schubert 	      printf_filtered (".\n");
1509*5796c8dcSSimon Schubert 	    }
1510*5796c8dcSSimon Schubert 
1511*5796c8dcSSimon Schubert 	  /* x/i should display this line's code.  */
1512*5796c8dcSSimon Schubert 	  set_next_address (gdbarch, start_pc);
1513*5796c8dcSSimon Schubert 
1514*5796c8dcSSimon Schubert 	  /* Repeating "info line" should do the following line.  */
1515*5796c8dcSSimon Schubert 	  last_line_listed = sal.line + 1;
1516*5796c8dcSSimon Schubert 
1517*5796c8dcSSimon Schubert 	  /* If this is the only line, show the source code.  If it could
1518*5796c8dcSSimon Schubert 	     not find the file, don't do anything special.  */
1519*5796c8dcSSimon Schubert 	  if (annotation_level && sals.nelts == 1)
1520*5796c8dcSSimon Schubert 	    identify_source_line (sal.symtab, sal.line, 0, start_pc);
1521*5796c8dcSSimon Schubert 	}
1522*5796c8dcSSimon Schubert       else
1523*5796c8dcSSimon Schubert 	/* Is there any case in which we get here, and have an address
1524*5796c8dcSSimon Schubert 	   which the user would want to see?  If we have debugging symbols
1525*5796c8dcSSimon Schubert 	   and no line numbers?  */
1526*5796c8dcSSimon Schubert 	printf_filtered (_("Line number %d is out of range for \"%s\".\n"),
1527*5796c8dcSSimon Schubert 			 sal.line, sal.symtab->filename);
1528*5796c8dcSSimon Schubert     }
1529*5796c8dcSSimon Schubert   xfree (sals.sals);
1530*5796c8dcSSimon Schubert }
1531*5796c8dcSSimon Schubert 
1532*5796c8dcSSimon Schubert /* Commands to search the source file for a regexp.  */
1533*5796c8dcSSimon Schubert 
1534*5796c8dcSSimon Schubert static void
1535*5796c8dcSSimon Schubert forward_search_command (char *regex, int from_tty)
1536*5796c8dcSSimon Schubert {
1537*5796c8dcSSimon Schubert   int c;
1538*5796c8dcSSimon Schubert   int desc;
1539*5796c8dcSSimon Schubert   FILE *stream;
1540*5796c8dcSSimon Schubert   int line;
1541*5796c8dcSSimon Schubert   char *msg;
1542*5796c8dcSSimon Schubert   struct cleanup *cleanups;
1543*5796c8dcSSimon Schubert 
1544*5796c8dcSSimon Schubert   line = last_line_listed + 1;
1545*5796c8dcSSimon Schubert 
1546*5796c8dcSSimon Schubert   msg = (char *) re_comp (regex);
1547*5796c8dcSSimon Schubert   if (msg)
1548*5796c8dcSSimon Schubert     error (("%s"), msg);
1549*5796c8dcSSimon Schubert 
1550*5796c8dcSSimon Schubert   if (current_source_symtab == 0)
1551*5796c8dcSSimon Schubert     select_source_symtab (0);
1552*5796c8dcSSimon Schubert 
1553*5796c8dcSSimon Schubert   desc = open_source_file (current_source_symtab);
1554*5796c8dcSSimon Schubert   if (desc < 0)
1555*5796c8dcSSimon Schubert     perror_with_name (current_source_symtab->filename);
1556*5796c8dcSSimon Schubert   cleanups = make_cleanup_close (desc);
1557*5796c8dcSSimon Schubert 
1558*5796c8dcSSimon Schubert   if (current_source_symtab->line_charpos == 0)
1559*5796c8dcSSimon Schubert     find_source_lines (current_source_symtab, desc);
1560*5796c8dcSSimon Schubert 
1561*5796c8dcSSimon Schubert   if (line < 1 || line > current_source_symtab->nlines)
1562*5796c8dcSSimon Schubert     error (_("Expression not found"));
1563*5796c8dcSSimon Schubert 
1564*5796c8dcSSimon Schubert   if (lseek (desc, current_source_symtab->line_charpos[line - 1], 0) < 0)
1565*5796c8dcSSimon Schubert     perror_with_name (current_source_symtab->filename);
1566*5796c8dcSSimon Schubert 
1567*5796c8dcSSimon Schubert   discard_cleanups (cleanups);
1568*5796c8dcSSimon Schubert   stream = fdopen (desc, FDOPEN_MODE);
1569*5796c8dcSSimon Schubert   clearerr (stream);
1570*5796c8dcSSimon Schubert   cleanups = make_cleanup_fclose (stream);
1571*5796c8dcSSimon Schubert   while (1)
1572*5796c8dcSSimon Schubert     {
1573*5796c8dcSSimon Schubert       static char *buf = NULL;
1574*5796c8dcSSimon Schubert       char *p;
1575*5796c8dcSSimon Schubert       int cursize, newsize;
1576*5796c8dcSSimon Schubert 
1577*5796c8dcSSimon Schubert       cursize = 256;
1578*5796c8dcSSimon Schubert       buf = xmalloc (cursize);
1579*5796c8dcSSimon Schubert       p = buf;
1580*5796c8dcSSimon Schubert 
1581*5796c8dcSSimon Schubert       c = getc (stream);
1582*5796c8dcSSimon Schubert       if (c == EOF)
1583*5796c8dcSSimon Schubert 	break;
1584*5796c8dcSSimon Schubert       do
1585*5796c8dcSSimon Schubert 	{
1586*5796c8dcSSimon Schubert 	  *p++ = c;
1587*5796c8dcSSimon Schubert 	  if (p - buf == cursize)
1588*5796c8dcSSimon Schubert 	    {
1589*5796c8dcSSimon Schubert 	      newsize = cursize + cursize / 2;
1590*5796c8dcSSimon Schubert 	      buf = xrealloc (buf, newsize);
1591*5796c8dcSSimon Schubert 	      p = buf + cursize;
1592*5796c8dcSSimon Schubert 	      cursize = newsize;
1593*5796c8dcSSimon Schubert 	    }
1594*5796c8dcSSimon Schubert 	}
1595*5796c8dcSSimon Schubert       while (c != '\n' && (c = getc (stream)) >= 0);
1596*5796c8dcSSimon Schubert 
1597*5796c8dcSSimon Schubert       /* Remove the \r, if any, at the end of the line, otherwise
1598*5796c8dcSSimon Schubert          regular expressions that end with $ or \n won't work.  */
1599*5796c8dcSSimon Schubert       if (p - buf > 1 && p[-2] == '\r')
1600*5796c8dcSSimon Schubert 	{
1601*5796c8dcSSimon Schubert 	  p--;
1602*5796c8dcSSimon Schubert 	  p[-1] = '\n';
1603*5796c8dcSSimon Schubert 	}
1604*5796c8dcSSimon Schubert 
1605*5796c8dcSSimon Schubert       /* we now have a source line in buf, null terminate and match */
1606*5796c8dcSSimon Schubert       *p = 0;
1607*5796c8dcSSimon Schubert       if (re_exec (buf) > 0)
1608*5796c8dcSSimon Schubert 	{
1609*5796c8dcSSimon Schubert 	  /* Match! */
1610*5796c8dcSSimon Schubert 	  do_cleanups (cleanups);
1611*5796c8dcSSimon Schubert 	  print_source_lines (current_source_symtab, line, line + 1, 0);
1612*5796c8dcSSimon Schubert 	  set_internalvar_integer (lookup_internalvar ("_"), line);
1613*5796c8dcSSimon Schubert 	  current_source_line = max (line - lines_to_list / 2, 1);
1614*5796c8dcSSimon Schubert 	  return;
1615*5796c8dcSSimon Schubert 	}
1616*5796c8dcSSimon Schubert       line++;
1617*5796c8dcSSimon Schubert     }
1618*5796c8dcSSimon Schubert 
1619*5796c8dcSSimon Schubert   printf_filtered (_("Expression not found\n"));
1620*5796c8dcSSimon Schubert   do_cleanups (cleanups);
1621*5796c8dcSSimon Schubert }
1622*5796c8dcSSimon Schubert 
1623*5796c8dcSSimon Schubert static void
1624*5796c8dcSSimon Schubert reverse_search_command (char *regex, int from_tty)
1625*5796c8dcSSimon Schubert {
1626*5796c8dcSSimon Schubert   int c;
1627*5796c8dcSSimon Schubert   int desc;
1628*5796c8dcSSimon Schubert   FILE *stream;
1629*5796c8dcSSimon Schubert   int line;
1630*5796c8dcSSimon Schubert   char *msg;
1631*5796c8dcSSimon Schubert   struct cleanup *cleanups;
1632*5796c8dcSSimon Schubert 
1633*5796c8dcSSimon Schubert   line = last_line_listed - 1;
1634*5796c8dcSSimon Schubert 
1635*5796c8dcSSimon Schubert   msg = (char *) re_comp (regex);
1636*5796c8dcSSimon Schubert   if (msg)
1637*5796c8dcSSimon Schubert     error (("%s"), msg);
1638*5796c8dcSSimon Schubert 
1639*5796c8dcSSimon Schubert   if (current_source_symtab == 0)
1640*5796c8dcSSimon Schubert     select_source_symtab (0);
1641*5796c8dcSSimon Schubert 
1642*5796c8dcSSimon Schubert   desc = open_source_file (current_source_symtab);
1643*5796c8dcSSimon Schubert   if (desc < 0)
1644*5796c8dcSSimon Schubert     perror_with_name (current_source_symtab->filename);
1645*5796c8dcSSimon Schubert   cleanups = make_cleanup_close (desc);
1646*5796c8dcSSimon Schubert 
1647*5796c8dcSSimon Schubert   if (current_source_symtab->line_charpos == 0)
1648*5796c8dcSSimon Schubert     find_source_lines (current_source_symtab, desc);
1649*5796c8dcSSimon Schubert 
1650*5796c8dcSSimon Schubert   if (line < 1 || line > current_source_symtab->nlines)
1651*5796c8dcSSimon Schubert     error (_("Expression not found"));
1652*5796c8dcSSimon Schubert 
1653*5796c8dcSSimon Schubert   if (lseek (desc, current_source_symtab->line_charpos[line - 1], 0) < 0)
1654*5796c8dcSSimon Schubert     perror_with_name (current_source_symtab->filename);
1655*5796c8dcSSimon Schubert 
1656*5796c8dcSSimon Schubert   discard_cleanups (cleanups);
1657*5796c8dcSSimon Schubert   stream = fdopen (desc, FDOPEN_MODE);
1658*5796c8dcSSimon Schubert   clearerr (stream);
1659*5796c8dcSSimon Schubert   cleanups = make_cleanup_fclose (stream);
1660*5796c8dcSSimon Schubert   while (line > 1)
1661*5796c8dcSSimon Schubert     {
1662*5796c8dcSSimon Schubert /* FIXME!!!  We walk right off the end of buf if we get a long line!!! */
1663*5796c8dcSSimon Schubert       char buf[4096];		/* Should be reasonable??? */
1664*5796c8dcSSimon Schubert       char *p = buf;
1665*5796c8dcSSimon Schubert 
1666*5796c8dcSSimon Schubert       c = getc (stream);
1667*5796c8dcSSimon Schubert       if (c == EOF)
1668*5796c8dcSSimon Schubert 	break;
1669*5796c8dcSSimon Schubert       do
1670*5796c8dcSSimon Schubert 	{
1671*5796c8dcSSimon Schubert 	  *p++ = c;
1672*5796c8dcSSimon Schubert 	}
1673*5796c8dcSSimon Schubert       while (c != '\n' && (c = getc (stream)) >= 0);
1674*5796c8dcSSimon Schubert 
1675*5796c8dcSSimon Schubert       /* Remove the \r, if any, at the end of the line, otherwise
1676*5796c8dcSSimon Schubert          regular expressions that end with $ or \n won't work.  */
1677*5796c8dcSSimon Schubert       if (p - buf > 1 && p[-2] == '\r')
1678*5796c8dcSSimon Schubert 	{
1679*5796c8dcSSimon Schubert 	  p--;
1680*5796c8dcSSimon Schubert 	  p[-1] = '\n';
1681*5796c8dcSSimon Schubert 	}
1682*5796c8dcSSimon Schubert 
1683*5796c8dcSSimon Schubert       /* We now have a source line in buf; null terminate and match.  */
1684*5796c8dcSSimon Schubert       *p = 0;
1685*5796c8dcSSimon Schubert       if (re_exec (buf) > 0)
1686*5796c8dcSSimon Schubert 	{
1687*5796c8dcSSimon Schubert 	  /* Match! */
1688*5796c8dcSSimon Schubert 	  do_cleanups (cleanups);
1689*5796c8dcSSimon Schubert 	  print_source_lines (current_source_symtab, line, line + 1, 0);
1690*5796c8dcSSimon Schubert 	  set_internalvar_integer (lookup_internalvar ("_"), line);
1691*5796c8dcSSimon Schubert 	  current_source_line = max (line - lines_to_list / 2, 1);
1692*5796c8dcSSimon Schubert 	  return;
1693*5796c8dcSSimon Schubert 	}
1694*5796c8dcSSimon Schubert       line--;
1695*5796c8dcSSimon Schubert       if (fseek (stream, current_source_symtab->line_charpos[line - 1], 0) < 0)
1696*5796c8dcSSimon Schubert 	{
1697*5796c8dcSSimon Schubert 	  do_cleanups (cleanups);
1698*5796c8dcSSimon Schubert 	  perror_with_name (current_source_symtab->filename);
1699*5796c8dcSSimon Schubert 	}
1700*5796c8dcSSimon Schubert     }
1701*5796c8dcSSimon Schubert 
1702*5796c8dcSSimon Schubert   printf_filtered (_("Expression not found\n"));
1703*5796c8dcSSimon Schubert   do_cleanups (cleanups);
1704*5796c8dcSSimon Schubert   return;
1705*5796c8dcSSimon Schubert }
1706*5796c8dcSSimon Schubert 
1707*5796c8dcSSimon Schubert /* If the last character of PATH is a directory separator, then strip it.  */
1708*5796c8dcSSimon Schubert 
1709*5796c8dcSSimon Schubert static void
1710*5796c8dcSSimon Schubert strip_trailing_directory_separator (char *path)
1711*5796c8dcSSimon Schubert {
1712*5796c8dcSSimon Schubert   const int last = strlen (path) - 1;
1713*5796c8dcSSimon Schubert 
1714*5796c8dcSSimon Schubert   if (last < 0)
1715*5796c8dcSSimon Schubert     return;  /* No stripping is needed if PATH is the empty string.  */
1716*5796c8dcSSimon Schubert 
1717*5796c8dcSSimon Schubert   if (IS_DIR_SEPARATOR (path[last]))
1718*5796c8dcSSimon Schubert     path[last] = '\0';
1719*5796c8dcSSimon Schubert }
1720*5796c8dcSSimon Schubert 
1721*5796c8dcSSimon Schubert /* Return the path substitution rule that matches FROM.
1722*5796c8dcSSimon Schubert    Return NULL if no rule matches.  */
1723*5796c8dcSSimon Schubert 
1724*5796c8dcSSimon Schubert static struct substitute_path_rule *
1725*5796c8dcSSimon Schubert find_substitute_path_rule (const char *from)
1726*5796c8dcSSimon Schubert {
1727*5796c8dcSSimon Schubert   struct substitute_path_rule *rule = substitute_path_rules;
1728*5796c8dcSSimon Schubert 
1729*5796c8dcSSimon Schubert   while (rule != NULL)
1730*5796c8dcSSimon Schubert     {
1731*5796c8dcSSimon Schubert       if (FILENAME_CMP (rule->from, from) == 0)
1732*5796c8dcSSimon Schubert         return rule;
1733*5796c8dcSSimon Schubert       rule = rule->next;
1734*5796c8dcSSimon Schubert     }
1735*5796c8dcSSimon Schubert 
1736*5796c8dcSSimon Schubert   return NULL;
1737*5796c8dcSSimon Schubert }
1738*5796c8dcSSimon Schubert 
1739*5796c8dcSSimon Schubert /* Add a new substitute-path rule at the end of the current list of rules.
1740*5796c8dcSSimon Schubert    The new rule will replace FROM into TO.  */
1741*5796c8dcSSimon Schubert 
1742*5796c8dcSSimon Schubert void
1743*5796c8dcSSimon Schubert add_substitute_path_rule (char *from, char *to)
1744*5796c8dcSSimon Schubert {
1745*5796c8dcSSimon Schubert   struct substitute_path_rule *rule;
1746*5796c8dcSSimon Schubert   struct substitute_path_rule *new_rule;
1747*5796c8dcSSimon Schubert 
1748*5796c8dcSSimon Schubert   new_rule = xmalloc (sizeof (struct substitute_path_rule));
1749*5796c8dcSSimon Schubert   new_rule->from = xstrdup (from);
1750*5796c8dcSSimon Schubert   new_rule->to = xstrdup (to);
1751*5796c8dcSSimon Schubert   new_rule->next = NULL;
1752*5796c8dcSSimon Schubert 
1753*5796c8dcSSimon Schubert   /* If the list of rules are empty, then insert the new rule
1754*5796c8dcSSimon Schubert      at the head of the list.  */
1755*5796c8dcSSimon Schubert 
1756*5796c8dcSSimon Schubert   if (substitute_path_rules == NULL)
1757*5796c8dcSSimon Schubert     {
1758*5796c8dcSSimon Schubert       substitute_path_rules = new_rule;
1759*5796c8dcSSimon Schubert       return;
1760*5796c8dcSSimon Schubert     }
1761*5796c8dcSSimon Schubert 
1762*5796c8dcSSimon Schubert   /* Otherwise, skip to the last rule in our list and then append
1763*5796c8dcSSimon Schubert      the new rule.  */
1764*5796c8dcSSimon Schubert 
1765*5796c8dcSSimon Schubert   rule = substitute_path_rules;
1766*5796c8dcSSimon Schubert   while (rule->next != NULL)
1767*5796c8dcSSimon Schubert     rule = rule->next;
1768*5796c8dcSSimon Schubert 
1769*5796c8dcSSimon Schubert   rule->next = new_rule;
1770*5796c8dcSSimon Schubert }
1771*5796c8dcSSimon Schubert 
1772*5796c8dcSSimon Schubert /* Remove the given source path substitution rule from the current list
1773*5796c8dcSSimon Schubert    of rules.  The memory allocated for that rule is also deallocated.  */
1774*5796c8dcSSimon Schubert 
1775*5796c8dcSSimon Schubert static void
1776*5796c8dcSSimon Schubert delete_substitute_path_rule (struct substitute_path_rule *rule)
1777*5796c8dcSSimon Schubert {
1778*5796c8dcSSimon Schubert   if (rule == substitute_path_rules)
1779*5796c8dcSSimon Schubert     substitute_path_rules = rule->next;
1780*5796c8dcSSimon Schubert   else
1781*5796c8dcSSimon Schubert     {
1782*5796c8dcSSimon Schubert       struct substitute_path_rule *prev = substitute_path_rules;
1783*5796c8dcSSimon Schubert 
1784*5796c8dcSSimon Schubert       while (prev != NULL && prev->next != rule)
1785*5796c8dcSSimon Schubert         prev = prev->next;
1786*5796c8dcSSimon Schubert 
1787*5796c8dcSSimon Schubert       gdb_assert (prev != NULL);
1788*5796c8dcSSimon Schubert 
1789*5796c8dcSSimon Schubert       prev->next = rule->next;
1790*5796c8dcSSimon Schubert     }
1791*5796c8dcSSimon Schubert 
1792*5796c8dcSSimon Schubert   xfree (rule->from);
1793*5796c8dcSSimon Schubert   xfree (rule->to);
1794*5796c8dcSSimon Schubert   xfree (rule);
1795*5796c8dcSSimon Schubert }
1796*5796c8dcSSimon Schubert 
1797*5796c8dcSSimon Schubert /* Implement the "show substitute-path" command.  */
1798*5796c8dcSSimon Schubert 
1799*5796c8dcSSimon Schubert static void
1800*5796c8dcSSimon Schubert show_substitute_path_command (char *args, int from_tty)
1801*5796c8dcSSimon Schubert {
1802*5796c8dcSSimon Schubert   struct substitute_path_rule *rule = substitute_path_rules;
1803*5796c8dcSSimon Schubert   char **argv;
1804*5796c8dcSSimon Schubert   char *from = NULL;
1805*5796c8dcSSimon Schubert 
1806*5796c8dcSSimon Schubert   argv = gdb_buildargv (args);
1807*5796c8dcSSimon Schubert   make_cleanup_freeargv (argv);
1808*5796c8dcSSimon Schubert 
1809*5796c8dcSSimon Schubert   /* We expect zero or one argument.  */
1810*5796c8dcSSimon Schubert 
1811*5796c8dcSSimon Schubert   if (argv != NULL && argv[0] != NULL && argv[1] != NULL)
1812*5796c8dcSSimon Schubert     error (_("Too many arguments in command"));
1813*5796c8dcSSimon Schubert 
1814*5796c8dcSSimon Schubert   if (argv != NULL && argv[0] != NULL)
1815*5796c8dcSSimon Schubert     from = argv[0];
1816*5796c8dcSSimon Schubert 
1817*5796c8dcSSimon Schubert   /* Print the substitution rules.  */
1818*5796c8dcSSimon Schubert 
1819*5796c8dcSSimon Schubert   if (from != NULL)
1820*5796c8dcSSimon Schubert     printf_filtered
1821*5796c8dcSSimon Schubert       (_("Source path substitution rule matching `%s':\n"), from);
1822*5796c8dcSSimon Schubert   else
1823*5796c8dcSSimon Schubert     printf_filtered (_("List of all source path substitution rules:\n"));
1824*5796c8dcSSimon Schubert 
1825*5796c8dcSSimon Schubert   while (rule != NULL)
1826*5796c8dcSSimon Schubert     {
1827*5796c8dcSSimon Schubert       if (from == NULL || FILENAME_CMP (rule->from, from) == 0)
1828*5796c8dcSSimon Schubert         printf_filtered ("  `%s' -> `%s'.\n", rule->from, rule->to);
1829*5796c8dcSSimon Schubert       rule = rule->next;
1830*5796c8dcSSimon Schubert     }
1831*5796c8dcSSimon Schubert }
1832*5796c8dcSSimon Schubert 
1833*5796c8dcSSimon Schubert /* Implement the "unset substitute-path" command.  */
1834*5796c8dcSSimon Schubert 
1835*5796c8dcSSimon Schubert static void
1836*5796c8dcSSimon Schubert unset_substitute_path_command (char *args, int from_tty)
1837*5796c8dcSSimon Schubert {
1838*5796c8dcSSimon Schubert   struct substitute_path_rule *rule = substitute_path_rules;
1839*5796c8dcSSimon Schubert   char **argv = gdb_buildargv (args);
1840*5796c8dcSSimon Schubert   char *from = NULL;
1841*5796c8dcSSimon Schubert   int rule_found = 0;
1842*5796c8dcSSimon Schubert 
1843*5796c8dcSSimon Schubert   /* This function takes either 0 or 1 argument.  */
1844*5796c8dcSSimon Schubert 
1845*5796c8dcSSimon Schubert   make_cleanup_freeargv (argv);
1846*5796c8dcSSimon Schubert   if (argv != NULL && argv[0] != NULL && argv[1] != NULL)
1847*5796c8dcSSimon Schubert     error (_("Incorrect usage, too many arguments in command"));
1848*5796c8dcSSimon Schubert 
1849*5796c8dcSSimon Schubert   if (argv != NULL && argv[0] != NULL)
1850*5796c8dcSSimon Schubert     from = argv[0];
1851*5796c8dcSSimon Schubert 
1852*5796c8dcSSimon Schubert   /* If the user asked for all the rules to be deleted, ask him
1853*5796c8dcSSimon Schubert      to confirm and give him a chance to abort before the action
1854*5796c8dcSSimon Schubert      is performed.  */
1855*5796c8dcSSimon Schubert 
1856*5796c8dcSSimon Schubert   if (from == NULL
1857*5796c8dcSSimon Schubert       && !query (_("Delete all source path substitution rules? ")))
1858*5796c8dcSSimon Schubert     error (_("Canceled"));
1859*5796c8dcSSimon Schubert 
1860*5796c8dcSSimon Schubert   /* Delete the rule matching the argument.  No argument means that
1861*5796c8dcSSimon Schubert      all rules should be deleted.  */
1862*5796c8dcSSimon Schubert 
1863*5796c8dcSSimon Schubert   while (rule != NULL)
1864*5796c8dcSSimon Schubert     {
1865*5796c8dcSSimon Schubert       struct substitute_path_rule *next = rule->next;
1866*5796c8dcSSimon Schubert 
1867*5796c8dcSSimon Schubert       if (from == NULL || FILENAME_CMP (from, rule->from) == 0)
1868*5796c8dcSSimon Schubert         {
1869*5796c8dcSSimon Schubert           delete_substitute_path_rule (rule);
1870*5796c8dcSSimon Schubert           rule_found = 1;
1871*5796c8dcSSimon Schubert         }
1872*5796c8dcSSimon Schubert 
1873*5796c8dcSSimon Schubert       rule = next;
1874*5796c8dcSSimon Schubert     }
1875*5796c8dcSSimon Schubert 
1876*5796c8dcSSimon Schubert   /* If the user asked for a specific rule to be deleted but
1877*5796c8dcSSimon Schubert      we could not find it, then report an error.  */
1878*5796c8dcSSimon Schubert 
1879*5796c8dcSSimon Schubert   if (from != NULL && !rule_found)
1880*5796c8dcSSimon Schubert     error (_("No substitution rule defined for `%s'"), from);
1881*5796c8dcSSimon Schubert 
1882*5796c8dcSSimon Schubert   forget_cached_source_info ();
1883*5796c8dcSSimon Schubert }
1884*5796c8dcSSimon Schubert 
1885*5796c8dcSSimon Schubert /* Add a new source path substitution rule.  */
1886*5796c8dcSSimon Schubert 
1887*5796c8dcSSimon Schubert static void
1888*5796c8dcSSimon Schubert set_substitute_path_command (char *args, int from_tty)
1889*5796c8dcSSimon Schubert {
1890*5796c8dcSSimon Schubert   char *from_path, *to_path;
1891*5796c8dcSSimon Schubert   char **argv;
1892*5796c8dcSSimon Schubert   struct substitute_path_rule *rule;
1893*5796c8dcSSimon Schubert 
1894*5796c8dcSSimon Schubert   argv = gdb_buildargv (args);
1895*5796c8dcSSimon Schubert   make_cleanup_freeargv (argv);
1896*5796c8dcSSimon Schubert 
1897*5796c8dcSSimon Schubert   if (argv == NULL || argv[0] == NULL || argv [1] == NULL)
1898*5796c8dcSSimon Schubert     error (_("Incorrect usage, too few arguments in command"));
1899*5796c8dcSSimon Schubert 
1900*5796c8dcSSimon Schubert   if (argv[2] != NULL)
1901*5796c8dcSSimon Schubert     error (_("Incorrect usage, too many arguments in command"));
1902*5796c8dcSSimon Schubert 
1903*5796c8dcSSimon Schubert   if (*(argv[0]) == '\0')
1904*5796c8dcSSimon Schubert     error (_("First argument must be at least one character long"));
1905*5796c8dcSSimon Schubert 
1906*5796c8dcSSimon Schubert   /* Strip any trailing directory separator character in either FROM
1907*5796c8dcSSimon Schubert      or TO.  The substitution rule already implicitly contains them.  */
1908*5796c8dcSSimon Schubert   strip_trailing_directory_separator (argv[0]);
1909*5796c8dcSSimon Schubert   strip_trailing_directory_separator (argv[1]);
1910*5796c8dcSSimon Schubert 
1911*5796c8dcSSimon Schubert   /* If a rule with the same "from" was previously defined, then
1912*5796c8dcSSimon Schubert      delete it.  This new rule replaces it.  */
1913*5796c8dcSSimon Schubert 
1914*5796c8dcSSimon Schubert   rule = find_substitute_path_rule (argv[0]);
1915*5796c8dcSSimon Schubert   if (rule != NULL)
1916*5796c8dcSSimon Schubert     delete_substitute_path_rule (rule);
1917*5796c8dcSSimon Schubert 
1918*5796c8dcSSimon Schubert   /* Insert the new substitution rule.  */
1919*5796c8dcSSimon Schubert 
1920*5796c8dcSSimon Schubert   add_substitute_path_rule (argv[0], argv[1]);
1921*5796c8dcSSimon Schubert   forget_cached_source_info ();
1922*5796c8dcSSimon Schubert }
1923*5796c8dcSSimon Schubert 
1924*5796c8dcSSimon Schubert 
1925*5796c8dcSSimon Schubert void
1926*5796c8dcSSimon Schubert _initialize_source (void)
1927*5796c8dcSSimon Schubert {
1928*5796c8dcSSimon Schubert   struct cmd_list_element *c;
1929*5796c8dcSSimon Schubert   current_source_symtab = 0;
1930*5796c8dcSSimon Schubert   init_source_path ();
1931*5796c8dcSSimon Schubert 
1932*5796c8dcSSimon Schubert   /* The intention is to use POSIX Basic Regular Expressions.
1933*5796c8dcSSimon Schubert      Always use the GNU regex routine for consistency across all hosts.
1934*5796c8dcSSimon Schubert      Our current GNU regex.c does not have all the POSIX features, so this is
1935*5796c8dcSSimon Schubert      just an approximation.  */
1936*5796c8dcSSimon Schubert   re_set_syntax (RE_SYNTAX_GREP);
1937*5796c8dcSSimon Schubert 
1938*5796c8dcSSimon Schubert   c = add_cmd ("directory", class_files, directory_command, _("\
1939*5796c8dcSSimon Schubert Add directory DIR to beginning of search path for source files.\n\
1940*5796c8dcSSimon Schubert Forget cached info on source file locations and line positions.\n\
1941*5796c8dcSSimon Schubert DIR can also be $cwd for the current working directory, or $cdir for the\n\
1942*5796c8dcSSimon Schubert directory in which the source file was compiled into object code.\n\
1943*5796c8dcSSimon Schubert With no argument, reset the search path to $cdir:$cwd, the default."),
1944*5796c8dcSSimon Schubert 	       &cmdlist);
1945*5796c8dcSSimon Schubert 
1946*5796c8dcSSimon Schubert   if (dbx_commands)
1947*5796c8dcSSimon Schubert     add_com_alias ("use", "directory", class_files, 0);
1948*5796c8dcSSimon Schubert 
1949*5796c8dcSSimon Schubert   set_cmd_completer (c, filename_completer);
1950*5796c8dcSSimon Schubert 
1951*5796c8dcSSimon Schubert   add_cmd ("directories", no_class, show_directories, _("\
1952*5796c8dcSSimon Schubert Current search path for finding source files.\n\
1953*5796c8dcSSimon Schubert $cwd in the path means the current working directory.\n\
1954*5796c8dcSSimon Schubert $cdir in the path means the compilation directory of the source file."),
1955*5796c8dcSSimon Schubert 	   &showlist);
1956*5796c8dcSSimon Schubert 
1957*5796c8dcSSimon Schubert   if (xdb_commands)
1958*5796c8dcSSimon Schubert     {
1959*5796c8dcSSimon Schubert       add_com_alias ("D", "directory", class_files, 0);
1960*5796c8dcSSimon Schubert       add_cmd ("ld", no_class, show_directories, _("\
1961*5796c8dcSSimon Schubert Current search path for finding source files.\n\
1962*5796c8dcSSimon Schubert $cwd in the path means the current working directory.\n\
1963*5796c8dcSSimon Schubert $cdir in the path means the compilation directory of the source file."),
1964*5796c8dcSSimon Schubert 	       &cmdlist);
1965*5796c8dcSSimon Schubert     }
1966*5796c8dcSSimon Schubert 
1967*5796c8dcSSimon Schubert   add_info ("source", source_info,
1968*5796c8dcSSimon Schubert 	    _("Information about the current source file."));
1969*5796c8dcSSimon Schubert 
1970*5796c8dcSSimon Schubert   add_info ("line", line_info, _("\
1971*5796c8dcSSimon Schubert Core addresses of the code for a source line.\n\
1972*5796c8dcSSimon Schubert Line can be specified as\n\
1973*5796c8dcSSimon Schubert   LINENUM, to list around that line in current file,\n\
1974*5796c8dcSSimon Schubert   FILE:LINENUM, to list around that line in that file,\n\
1975*5796c8dcSSimon Schubert   FUNCTION, to list around beginning of that function,\n\
1976*5796c8dcSSimon Schubert   FILE:FUNCTION, to distinguish among like-named static functions.\n\
1977*5796c8dcSSimon Schubert Default is to describe the last source line that was listed.\n\n\
1978*5796c8dcSSimon Schubert This sets the default address for \"x\" to the line's first instruction\n\
1979*5796c8dcSSimon Schubert so that \"x/i\" suffices to start examining the machine code.\n\
1980*5796c8dcSSimon Schubert The address is also stored as the value of \"$_\"."));
1981*5796c8dcSSimon Schubert 
1982*5796c8dcSSimon Schubert   add_com ("forward-search", class_files, forward_search_command, _("\
1983*5796c8dcSSimon Schubert Search for regular expression (see regex(3)) from last line listed.\n\
1984*5796c8dcSSimon Schubert The matching line number is also stored as the value of \"$_\"."));
1985*5796c8dcSSimon Schubert   add_com_alias ("search", "forward-search", class_files, 0);
1986*5796c8dcSSimon Schubert 
1987*5796c8dcSSimon Schubert   add_com ("reverse-search", class_files, reverse_search_command, _("\
1988*5796c8dcSSimon Schubert Search backward for regular expression (see regex(3)) from last line listed.\n\
1989*5796c8dcSSimon Schubert The matching line number is also stored as the value of \"$_\"."));
1990*5796c8dcSSimon Schubert 
1991*5796c8dcSSimon Schubert   if (xdb_commands)
1992*5796c8dcSSimon Schubert     {
1993*5796c8dcSSimon Schubert       add_com_alias ("/", "forward-search", class_files, 0);
1994*5796c8dcSSimon Schubert       add_com_alias ("?", "reverse-search", class_files, 0);
1995*5796c8dcSSimon Schubert     }
1996*5796c8dcSSimon Schubert 
1997*5796c8dcSSimon Schubert   add_setshow_integer_cmd ("listsize", class_support, &lines_to_list, _("\
1998*5796c8dcSSimon Schubert Set number of source lines gdb will list by default."), _("\
1999*5796c8dcSSimon Schubert Show number of source lines gdb will list by default."), NULL,
2000*5796c8dcSSimon Schubert 			    NULL,
2001*5796c8dcSSimon Schubert 			    show_lines_to_list,
2002*5796c8dcSSimon Schubert 			    &setlist, &showlist);
2003*5796c8dcSSimon Schubert 
2004*5796c8dcSSimon Schubert   add_cmd ("substitute-path", class_files, set_substitute_path_command,
2005*5796c8dcSSimon Schubert            _("\
2006*5796c8dcSSimon Schubert Usage: set substitute-path FROM TO\n\
2007*5796c8dcSSimon Schubert Add a substitution rule replacing FROM into TO in source file names.\n\
2008*5796c8dcSSimon Schubert If a substitution rule was previously set for FROM, the old rule\n\
2009*5796c8dcSSimon Schubert is replaced by the new one."),
2010*5796c8dcSSimon Schubert            &setlist);
2011*5796c8dcSSimon Schubert 
2012*5796c8dcSSimon Schubert   add_cmd ("substitute-path", class_files, unset_substitute_path_command,
2013*5796c8dcSSimon Schubert            _("\
2014*5796c8dcSSimon Schubert Usage: unset substitute-path [FROM]\n\
2015*5796c8dcSSimon Schubert Delete the rule for substituting FROM in source file names.  If FROM\n\
2016*5796c8dcSSimon Schubert is not specified, all substituting rules are deleted.\n\
2017*5796c8dcSSimon Schubert If the debugger cannot find a rule for FROM, it will display a warning."),
2018*5796c8dcSSimon Schubert            &unsetlist);
2019*5796c8dcSSimon Schubert 
2020*5796c8dcSSimon Schubert   add_cmd ("substitute-path", class_files, show_substitute_path_command,
2021*5796c8dcSSimon Schubert            _("\
2022*5796c8dcSSimon Schubert Usage: show substitute-path [FROM]\n\
2023*5796c8dcSSimon Schubert Print the rule for substituting FROM in source file names. If FROM\n\
2024*5796c8dcSSimon Schubert is not specified, print all substitution rules."),
2025*5796c8dcSSimon Schubert            &showlist);
2026*5796c8dcSSimon Schubert }
2027