15796c8dcSSimon Schubert /* List lines of source files for GDB, the GNU debugger.
2*ef5ccd6cSJohn Marino Copyright (C) 1986-2013 Free Software Foundation, Inc.
35796c8dcSSimon Schubert
45796c8dcSSimon Schubert This file is part of GDB.
55796c8dcSSimon Schubert
65796c8dcSSimon Schubert This program is free software; you can redistribute it and/or modify
75796c8dcSSimon Schubert it under the terms of the GNU General Public License as published by
85796c8dcSSimon Schubert the Free Software Foundation; either version 3 of the License, or
95796c8dcSSimon Schubert (at your option) any later version.
105796c8dcSSimon Schubert
115796c8dcSSimon Schubert This program is distributed in the hope that it will be useful,
125796c8dcSSimon Schubert but WITHOUT ANY WARRANTY; without even the implied warranty of
135796c8dcSSimon Schubert MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
145796c8dcSSimon Schubert GNU General Public License for more details.
155796c8dcSSimon Schubert
165796c8dcSSimon Schubert You should have received a copy of the GNU General Public License
175796c8dcSSimon Schubert along with this program. If not, see <http://www.gnu.org/licenses/>. */
185796c8dcSSimon Schubert
195796c8dcSSimon Schubert #include "defs.h"
205796c8dcSSimon Schubert #include "arch-utils.h"
215796c8dcSSimon Schubert #include "symtab.h"
225796c8dcSSimon Schubert #include "expression.h"
235796c8dcSSimon Schubert #include "language.h"
245796c8dcSSimon Schubert #include "command.h"
255796c8dcSSimon Schubert #include "source.h"
265796c8dcSSimon Schubert #include "gdbcmd.h"
275796c8dcSSimon Schubert #include "frame.h"
285796c8dcSSimon Schubert #include "value.h"
295796c8dcSSimon Schubert #include "gdb_assert.h"
305796c8dcSSimon Schubert
315796c8dcSSimon Schubert #include <sys/types.h>
325796c8dcSSimon Schubert #include "gdb_string.h"
335796c8dcSSimon Schubert #include "gdb_stat.h"
345796c8dcSSimon Schubert #include <fcntl.h>
355796c8dcSSimon Schubert #include "gdbcore.h"
365796c8dcSSimon Schubert #include "gdb_regex.h"
375796c8dcSSimon Schubert #include "symfile.h"
385796c8dcSSimon Schubert #include "objfiles.h"
395796c8dcSSimon Schubert #include "annotate.h"
405796c8dcSSimon Schubert #include "gdbtypes.h"
415796c8dcSSimon Schubert #include "linespec.h"
425796c8dcSSimon Schubert #include "filenames.h" /* for DOSish file names */
435796c8dcSSimon Schubert #include "completer.h"
445796c8dcSSimon Schubert #include "ui-out.h"
455796c8dcSSimon Schubert #include "readline/readline.h"
465796c8dcSSimon Schubert
47cf7f2e2dSJohn Marino #include "psymtab.h"
48cf7f2e2dSJohn Marino
495796c8dcSSimon Schubert
505796c8dcSSimon Schubert #define OPEN_MODE (O_RDONLY | O_BINARY)
515796c8dcSSimon Schubert #define FDOPEN_MODE FOPEN_RB
525796c8dcSSimon Schubert
535796c8dcSSimon Schubert /* Prototypes for exported functions. */
545796c8dcSSimon Schubert
555796c8dcSSimon Schubert void _initialize_source (void);
565796c8dcSSimon Schubert
575796c8dcSSimon Schubert /* Prototypes for local functions. */
585796c8dcSSimon Schubert
595796c8dcSSimon Schubert static int get_filename_and_charpos (struct symtab *, char **);
605796c8dcSSimon Schubert
615796c8dcSSimon Schubert static void reverse_search_command (char *, int);
625796c8dcSSimon Schubert
635796c8dcSSimon Schubert static void forward_search_command (char *, int);
645796c8dcSSimon Schubert
655796c8dcSSimon Schubert static void line_info (char *, int);
665796c8dcSSimon Schubert
675796c8dcSSimon Schubert static void source_info (char *, int);
685796c8dcSSimon Schubert
695796c8dcSSimon Schubert /* Path of directories to search for source files.
705796c8dcSSimon Schubert Same format as the PATH environment variable's value. */
715796c8dcSSimon Schubert
725796c8dcSSimon Schubert char *source_path;
735796c8dcSSimon Schubert
745796c8dcSSimon Schubert /* Support for source path substitution commands. */
755796c8dcSSimon Schubert
765796c8dcSSimon Schubert struct substitute_path_rule
775796c8dcSSimon Schubert {
785796c8dcSSimon Schubert char *from;
795796c8dcSSimon Schubert char *to;
805796c8dcSSimon Schubert struct substitute_path_rule *next;
815796c8dcSSimon Schubert };
825796c8dcSSimon Schubert
835796c8dcSSimon Schubert static struct substitute_path_rule *substitute_path_rules = NULL;
845796c8dcSSimon Schubert
855796c8dcSSimon Schubert /* Symtab of default file for listing lines of. */
865796c8dcSSimon Schubert
875796c8dcSSimon Schubert static struct symtab *current_source_symtab;
885796c8dcSSimon Schubert
895796c8dcSSimon Schubert /* Default next line to list. */
905796c8dcSSimon Schubert
915796c8dcSSimon Schubert static int current_source_line;
925796c8dcSSimon Schubert
93cf7f2e2dSJohn Marino static struct program_space *current_source_pspace;
94cf7f2e2dSJohn Marino
955796c8dcSSimon Schubert /* Default number of lines to print with commands like "list".
965796c8dcSSimon Schubert This is based on guessing how many long (i.e. more than chars_per_line
975796c8dcSSimon Schubert characters) lines there will be. To be completely correct, "list"
985796c8dcSSimon Schubert and friends should be rewritten to count characters and see where
995796c8dcSSimon Schubert things are wrapping, but that would be a fair amount of work. */
1005796c8dcSSimon Schubert
1015796c8dcSSimon Schubert int lines_to_list = 10;
1025796c8dcSSimon Schubert static void
show_lines_to_list(struct ui_file * file,int from_tty,struct cmd_list_element * c,const char * value)1035796c8dcSSimon Schubert show_lines_to_list (struct ui_file *file, int from_tty,
1045796c8dcSSimon Schubert struct cmd_list_element *c, const char *value)
1055796c8dcSSimon Schubert {
106c50c785cSJohn Marino fprintf_filtered (file,
107c50c785cSJohn Marino _("Number of source lines gdb "
108c50c785cSJohn Marino "will list by default is %s.\n"),
1095796c8dcSSimon Schubert value);
1105796c8dcSSimon Schubert }
1115796c8dcSSimon Schubert
112*ef5ccd6cSJohn Marino /* Possible values of 'set filename-display'. */
113*ef5ccd6cSJohn Marino static const char filename_display_basename[] = "basename";
114*ef5ccd6cSJohn Marino static const char filename_display_relative[] = "relative";
115*ef5ccd6cSJohn Marino static const char filename_display_absolute[] = "absolute";
116*ef5ccd6cSJohn Marino
117*ef5ccd6cSJohn Marino static const char *const filename_display_kind_names[] = {
118*ef5ccd6cSJohn Marino filename_display_basename,
119*ef5ccd6cSJohn Marino filename_display_relative,
120*ef5ccd6cSJohn Marino filename_display_absolute,
121*ef5ccd6cSJohn Marino NULL
122*ef5ccd6cSJohn Marino };
123*ef5ccd6cSJohn Marino
124*ef5ccd6cSJohn Marino static const char *filename_display_string = filename_display_relative;
125*ef5ccd6cSJohn Marino
126*ef5ccd6cSJohn Marino static void
show_filename_display_string(struct ui_file * file,int from_tty,struct cmd_list_element * c,const char * value)127*ef5ccd6cSJohn Marino show_filename_display_string (struct ui_file *file, int from_tty,
128*ef5ccd6cSJohn Marino struct cmd_list_element *c, const char *value)
129*ef5ccd6cSJohn Marino {
130*ef5ccd6cSJohn Marino fprintf_filtered (file, _("Filenames are displayed as \"%s\".\n"), value);
131*ef5ccd6cSJohn Marino }
132*ef5ccd6cSJohn Marino
1335796c8dcSSimon Schubert /* Line number of last line printed. Default for various commands.
1345796c8dcSSimon Schubert current_source_line is usually, but not always, the same as this. */
1355796c8dcSSimon Schubert
1365796c8dcSSimon Schubert static int last_line_listed;
1375796c8dcSSimon Schubert
1385796c8dcSSimon Schubert /* First line number listed by last listing command. */
1395796c8dcSSimon Schubert
1405796c8dcSSimon Schubert static int first_line_listed;
1415796c8dcSSimon Schubert
1425796c8dcSSimon Schubert /* Saves the name of the last source file visited and a possible error code.
143c50c785cSJohn Marino Used to prevent repeating annoying "No such file or directories" msgs. */
1445796c8dcSSimon Schubert
1455796c8dcSSimon Schubert static struct symtab *last_source_visited = NULL;
1465796c8dcSSimon Schubert static int last_source_error = 0;
1475796c8dcSSimon Schubert
1485796c8dcSSimon Schubert /* Return the first line listed by print_source_lines.
1495796c8dcSSimon Schubert Used by command interpreters to request listing from
1505796c8dcSSimon Schubert a previous point. */
1515796c8dcSSimon Schubert
1525796c8dcSSimon Schubert int
get_first_line_listed(void)1535796c8dcSSimon Schubert get_first_line_listed (void)
1545796c8dcSSimon Schubert {
1555796c8dcSSimon Schubert return first_line_listed;
1565796c8dcSSimon Schubert }
1575796c8dcSSimon Schubert
1585796c8dcSSimon Schubert /* Return the default number of lines to print with commands like the
1595796c8dcSSimon Schubert cli "list". The caller of print_source_lines must use this to
1605796c8dcSSimon Schubert calculate the end line and use it in the call to print_source_lines
1615796c8dcSSimon Schubert as it does not automatically use this value. */
1625796c8dcSSimon Schubert
1635796c8dcSSimon Schubert int
get_lines_to_list(void)1645796c8dcSSimon Schubert get_lines_to_list (void)
1655796c8dcSSimon Schubert {
1665796c8dcSSimon Schubert return lines_to_list;
1675796c8dcSSimon Schubert }
1685796c8dcSSimon Schubert
1695796c8dcSSimon Schubert /* Return the current source file for listing and next line to list.
1705796c8dcSSimon Schubert NOTE: The returned sal pc and end fields are not valid. */
1715796c8dcSSimon Schubert
1725796c8dcSSimon Schubert struct symtab_and_line
get_current_source_symtab_and_line(void)1735796c8dcSSimon Schubert get_current_source_symtab_and_line (void)
1745796c8dcSSimon Schubert {
1755796c8dcSSimon Schubert struct symtab_and_line cursal = { 0 };
1765796c8dcSSimon Schubert
177cf7f2e2dSJohn Marino cursal.pspace = current_source_pspace;
1785796c8dcSSimon Schubert cursal.symtab = current_source_symtab;
1795796c8dcSSimon Schubert cursal.line = current_source_line;
1805796c8dcSSimon Schubert cursal.pc = 0;
1815796c8dcSSimon Schubert cursal.end = 0;
1825796c8dcSSimon Schubert
1835796c8dcSSimon Schubert return cursal;
1845796c8dcSSimon Schubert }
1855796c8dcSSimon Schubert
1865796c8dcSSimon Schubert /* If the current source file for listing is not set, try and get a default.
1875796c8dcSSimon Schubert Usually called before get_current_source_symtab_and_line() is called.
1885796c8dcSSimon Schubert It may err out if a default cannot be determined.
1895796c8dcSSimon Schubert We must be cautious about where it is called, as it can recurse as the
1905796c8dcSSimon Schubert process of determining a new default may call the caller!
1915796c8dcSSimon Schubert Use get_current_source_symtab_and_line only to get whatever
1925796c8dcSSimon Schubert we have without erroring out or trying to get a default. */
1935796c8dcSSimon Schubert
1945796c8dcSSimon Schubert void
set_default_source_symtab_and_line(void)1955796c8dcSSimon Schubert set_default_source_symtab_and_line (void)
1965796c8dcSSimon Schubert {
1975796c8dcSSimon Schubert if (!have_full_symbols () && !have_partial_symbols ())
1985796c8dcSSimon Schubert error (_("No symbol table is loaded. Use the \"file\" command."));
1995796c8dcSSimon Schubert
200c50c785cSJohn Marino /* Pull in a current source symtab if necessary. */
2015796c8dcSSimon Schubert if (current_source_symtab == 0)
2025796c8dcSSimon Schubert select_source_symtab (0);
2035796c8dcSSimon Schubert }
2045796c8dcSSimon Schubert
2055796c8dcSSimon Schubert /* Return the current default file for listing and next line to list
2065796c8dcSSimon Schubert (the returned sal pc and end fields are not valid.)
2075796c8dcSSimon Schubert and set the current default to whatever is in SAL.
2085796c8dcSSimon Schubert NOTE: The returned sal pc and end fields are not valid. */
2095796c8dcSSimon Schubert
2105796c8dcSSimon Schubert struct symtab_and_line
set_current_source_symtab_and_line(const struct symtab_and_line * sal)2115796c8dcSSimon Schubert set_current_source_symtab_and_line (const struct symtab_and_line *sal)
2125796c8dcSSimon Schubert {
2135796c8dcSSimon Schubert struct symtab_and_line cursal = { 0 };
2145796c8dcSSimon Schubert
215cf7f2e2dSJohn Marino cursal.pspace = current_source_pspace;
2165796c8dcSSimon Schubert cursal.symtab = current_source_symtab;
2175796c8dcSSimon Schubert cursal.line = current_source_line;
2185796c8dcSSimon Schubert cursal.pc = 0;
2195796c8dcSSimon Schubert cursal.end = 0;
2205796c8dcSSimon Schubert
221cf7f2e2dSJohn Marino current_source_pspace = sal->pspace;
222cf7f2e2dSJohn Marino current_source_symtab = sal->symtab;
223cf7f2e2dSJohn Marino current_source_line = sal->line;
224cf7f2e2dSJohn Marino
2255796c8dcSSimon Schubert return cursal;
2265796c8dcSSimon Schubert }
2275796c8dcSSimon Schubert
2285796c8dcSSimon Schubert /* Reset any information stored about a default file and line to print. */
2295796c8dcSSimon Schubert
2305796c8dcSSimon Schubert void
clear_current_source_symtab_and_line(void)2315796c8dcSSimon Schubert clear_current_source_symtab_and_line (void)
2325796c8dcSSimon Schubert {
2335796c8dcSSimon Schubert current_source_symtab = 0;
2345796c8dcSSimon Schubert current_source_line = 0;
2355796c8dcSSimon Schubert }
2365796c8dcSSimon Schubert
2375796c8dcSSimon Schubert /* Set the source file default for the "list" command to be S.
2385796c8dcSSimon Schubert
2395796c8dcSSimon Schubert If S is NULL, and we don't have a default, find one. This
2405796c8dcSSimon Schubert should only be called when the user actually tries to use the
2415796c8dcSSimon Schubert default, since we produce an error if we can't find a reasonable
2425796c8dcSSimon Schubert default. Also, since this can cause symbols to be read, doing it
2435796c8dcSSimon Schubert before we need to would make things slower than necessary. */
2445796c8dcSSimon Schubert
2455796c8dcSSimon Schubert void
select_source_symtab(struct symtab * s)2465796c8dcSSimon Schubert select_source_symtab (struct symtab *s)
2475796c8dcSSimon Schubert {
2485796c8dcSSimon Schubert struct symtabs_and_lines sals;
2495796c8dcSSimon Schubert struct symtab_and_line sal;
2505796c8dcSSimon Schubert struct objfile *ofp;
2515796c8dcSSimon Schubert
2525796c8dcSSimon Schubert if (s)
2535796c8dcSSimon Schubert {
2545796c8dcSSimon Schubert current_source_symtab = s;
2555796c8dcSSimon Schubert current_source_line = 1;
256cf7f2e2dSJohn Marino current_source_pspace = SYMTAB_PSPACE (s);
2575796c8dcSSimon Schubert return;
2585796c8dcSSimon Schubert }
2595796c8dcSSimon Schubert
2605796c8dcSSimon Schubert if (current_source_symtab)
2615796c8dcSSimon Schubert return;
2625796c8dcSSimon Schubert
2635796c8dcSSimon Schubert /* Make the default place to list be the function `main'
2645796c8dcSSimon Schubert if one exists. */
2655796c8dcSSimon Schubert if (lookup_symbol (main_name (), 0, VAR_DOMAIN, 0))
2665796c8dcSSimon Schubert {
267*ef5ccd6cSJohn Marino sals = decode_line_with_current_source (main_name (),
268*ef5ccd6cSJohn Marino DECODE_LINE_FUNFIRSTLINE);
2695796c8dcSSimon Schubert sal = sals.sals[0];
2705796c8dcSSimon Schubert xfree (sals.sals);
271cf7f2e2dSJohn Marino current_source_pspace = sal.pspace;
2725796c8dcSSimon Schubert current_source_symtab = sal.symtab;
2735796c8dcSSimon Schubert current_source_line = max (sal.line - (lines_to_list - 1), 1);
2745796c8dcSSimon Schubert if (current_source_symtab)
2755796c8dcSSimon Schubert return;
2765796c8dcSSimon Schubert }
2775796c8dcSSimon Schubert
2785796c8dcSSimon Schubert /* Alright; find the last file in the symtab list (ignoring .h's
2795796c8dcSSimon Schubert and namespace symtabs). */
2805796c8dcSSimon Schubert
2815796c8dcSSimon Schubert current_source_line = 1;
2825796c8dcSSimon Schubert
283cf7f2e2dSJohn Marino ALL_OBJFILES (ofp)
2845796c8dcSSimon Schubert {
2855796c8dcSSimon Schubert for (s = ofp->symtabs; s; s = s->next)
2865796c8dcSSimon Schubert {
2875796c8dcSSimon Schubert const char *name = s->filename;
2885796c8dcSSimon Schubert int len = strlen (name);
289cf7f2e2dSJohn Marino
2905796c8dcSSimon Schubert if (!(len > 2 && (strcmp (&name[len - 2], ".h") == 0
2915796c8dcSSimon Schubert || strcmp (name, "<<C++-namespaces>>") == 0)))
292cf7f2e2dSJohn Marino {
293cf7f2e2dSJohn Marino current_source_pspace = current_program_space;
2945796c8dcSSimon Schubert current_source_symtab = s;
2955796c8dcSSimon Schubert }
2965796c8dcSSimon Schubert }
297cf7f2e2dSJohn Marino }
298cf7f2e2dSJohn Marino
2995796c8dcSSimon Schubert if (current_source_symtab)
3005796c8dcSSimon Schubert return;
3015796c8dcSSimon Schubert
302cf7f2e2dSJohn Marino ALL_OBJFILES (ofp)
3035796c8dcSSimon Schubert {
304cf7f2e2dSJohn Marino if (ofp->sf)
305cf7f2e2dSJohn Marino s = ofp->sf->qf->find_last_source_symtab (ofp);
306cf7f2e2dSJohn Marino if (s)
307cf7f2e2dSJohn Marino current_source_symtab = s;
3085796c8dcSSimon Schubert }
3095796c8dcSSimon Schubert if (current_source_symtab)
3105796c8dcSSimon Schubert return;
3115796c8dcSSimon Schubert
3125796c8dcSSimon Schubert error (_("Can't find a default source file"));
3135796c8dcSSimon Schubert }
3145796c8dcSSimon Schubert
315c50c785cSJohn Marino /* Handler for "set directories path-list" command.
316c50c785cSJohn Marino "set dir mumble" doesn't prepend paths, it resets the entire
317c50c785cSJohn Marino path list. The theory is that set(show(dir)) should be a no-op. */
318c50c785cSJohn Marino
3195796c8dcSSimon Schubert static void
set_directories_command(char * args,int from_tty,struct cmd_list_element * c)320c50c785cSJohn Marino set_directories_command (char *args, int from_tty, struct cmd_list_element *c)
321c50c785cSJohn Marino {
322c50c785cSJohn Marino /* This is the value that was set.
323c50c785cSJohn Marino It needs to be processed to maintain $cdir:$cwd and remove dups. */
324c50c785cSJohn Marino char *set_path = source_path;
325c50c785cSJohn Marino
326c50c785cSJohn Marino /* We preserve the invariant that $cdir:$cwd begins life at the end of
327c50c785cSJohn Marino the list by calling init_source_path. If they appear earlier in
328c50c785cSJohn Marino SET_PATH then mod_path will move them appropriately.
329c50c785cSJohn Marino mod_path will also remove duplicates. */
330c50c785cSJohn Marino init_source_path ();
331c50c785cSJohn Marino if (*set_path != '\0')
332c50c785cSJohn Marino mod_path (set_path, &source_path);
333c50c785cSJohn Marino
334c50c785cSJohn Marino xfree (set_path);
335c50c785cSJohn Marino }
336c50c785cSJohn Marino
337c50c785cSJohn Marino /* Print the list of source directories.
338c50c785cSJohn Marino This is used by the "ld" command, so it has the signature of a command
339c50c785cSJohn Marino function. */
340c50c785cSJohn Marino
341c50c785cSJohn Marino static void
show_directories_1(char * ignore,int from_tty)342c50c785cSJohn Marino show_directories_1 (char *ignore, int from_tty)
3435796c8dcSSimon Schubert {
3445796c8dcSSimon Schubert puts_filtered ("Source directories searched: ");
3455796c8dcSSimon Schubert puts_filtered (source_path);
3465796c8dcSSimon Schubert puts_filtered ("\n");
3475796c8dcSSimon Schubert }
3485796c8dcSSimon Schubert
349c50c785cSJohn Marino /* Handler for "show directories" command. */
350c50c785cSJohn Marino
351c50c785cSJohn Marino static void
show_directories_command(struct ui_file * file,int from_tty,struct cmd_list_element * c,const char * value)352c50c785cSJohn Marino show_directories_command (struct ui_file *file, int from_tty,
353c50c785cSJohn Marino struct cmd_list_element *c, const char *value)
354c50c785cSJohn Marino {
355c50c785cSJohn Marino show_directories_1 (NULL, from_tty);
356c50c785cSJohn Marino }
357c50c785cSJohn Marino
358a45ae5f8SJohn Marino /* Forget line positions and file names for the symtabs in a
359a45ae5f8SJohn Marino particular objfile. */
3605796c8dcSSimon Schubert
3615796c8dcSSimon Schubert void
forget_cached_source_info_for_objfile(struct objfile * objfile)362a45ae5f8SJohn Marino forget_cached_source_info_for_objfile (struct objfile *objfile)
3635796c8dcSSimon Schubert {
3645796c8dcSSimon Schubert struct symtab *s;
3655796c8dcSSimon Schubert
366a45ae5f8SJohn Marino ALL_OBJFILE_SYMTABS (objfile, s)
3675796c8dcSSimon Schubert {
3685796c8dcSSimon Schubert if (s->line_charpos != NULL)
3695796c8dcSSimon Schubert {
3705796c8dcSSimon Schubert xfree (s->line_charpos);
3715796c8dcSSimon Schubert s->line_charpos = NULL;
3725796c8dcSSimon Schubert }
3735796c8dcSSimon Schubert if (s->fullname != NULL)
3745796c8dcSSimon Schubert {
3755796c8dcSSimon Schubert xfree (s->fullname);
3765796c8dcSSimon Schubert s->fullname = NULL;
3775796c8dcSSimon Schubert }
3785796c8dcSSimon Schubert }
3795796c8dcSSimon Schubert
380cf7f2e2dSJohn Marino if (objfile->sf)
381cf7f2e2dSJohn Marino objfile->sf->qf->forget_cached_source_info (objfile);
3825796c8dcSSimon Schubert }
3835796c8dcSSimon Schubert
384a45ae5f8SJohn Marino /* Forget what we learned about line positions in source files, and
385a45ae5f8SJohn Marino which directories contain them; must check again now since files
386a45ae5f8SJohn Marino may be found in a different directory now. */
387a45ae5f8SJohn Marino
388a45ae5f8SJohn Marino void
forget_cached_source_info(void)389a45ae5f8SJohn Marino forget_cached_source_info (void)
390a45ae5f8SJohn Marino {
391a45ae5f8SJohn Marino struct program_space *pspace;
392a45ae5f8SJohn Marino struct objfile *objfile;
393a45ae5f8SJohn Marino
394a45ae5f8SJohn Marino ALL_PSPACES (pspace)
395a45ae5f8SJohn Marino ALL_PSPACE_OBJFILES (pspace, objfile)
396a45ae5f8SJohn Marino {
397a45ae5f8SJohn Marino forget_cached_source_info_for_objfile (objfile);
398a45ae5f8SJohn Marino }
399a45ae5f8SJohn Marino
4005796c8dcSSimon Schubert last_source_visited = NULL;
4015796c8dcSSimon Schubert }
4025796c8dcSSimon Schubert
4035796c8dcSSimon Schubert void
init_source_path(void)4045796c8dcSSimon Schubert init_source_path (void)
4055796c8dcSSimon Schubert {
4065796c8dcSSimon Schubert char buf[20];
4075796c8dcSSimon Schubert
408*ef5ccd6cSJohn Marino xsnprintf (buf, sizeof (buf), "$cdir%c$cwd", DIRNAME_SEPARATOR);
4095796c8dcSSimon Schubert source_path = xstrdup (buf);
4105796c8dcSSimon Schubert forget_cached_source_info ();
4115796c8dcSSimon Schubert }
4125796c8dcSSimon Schubert
4135796c8dcSSimon Schubert /* Add zero or more directories to the front of the source path. */
4145796c8dcSSimon Schubert
415*ef5ccd6cSJohn Marino static void
directory_command(char * dirname,int from_tty)4165796c8dcSSimon Schubert directory_command (char *dirname, int from_tty)
4175796c8dcSSimon Schubert {
4185796c8dcSSimon Schubert dont_repeat ();
4195796c8dcSSimon Schubert /* FIXME, this goes to "delete dir"... */
4205796c8dcSSimon Schubert if (dirname == 0)
4215796c8dcSSimon Schubert {
4225796c8dcSSimon Schubert if (!from_tty || query (_("Reinitialize source path to empty? ")))
4235796c8dcSSimon Schubert {
4245796c8dcSSimon Schubert xfree (source_path);
4255796c8dcSSimon Schubert init_source_path ();
4265796c8dcSSimon Schubert }
4275796c8dcSSimon Schubert }
4285796c8dcSSimon Schubert else
4295796c8dcSSimon Schubert {
4305796c8dcSSimon Schubert mod_path (dirname, &source_path);
4315796c8dcSSimon Schubert forget_cached_source_info ();
4325796c8dcSSimon Schubert }
4335796c8dcSSimon Schubert if (from_tty)
434c50c785cSJohn Marino show_directories_1 ((char *) 0, from_tty);
4355796c8dcSSimon Schubert }
4365796c8dcSSimon Schubert
4375796c8dcSSimon Schubert /* Add a path given with the -d command line switch.
4385796c8dcSSimon Schubert This will not be quoted so we must not treat spaces as separators. */
4395796c8dcSSimon Schubert
4405796c8dcSSimon Schubert void
directory_switch(char * dirname,int from_tty)4415796c8dcSSimon Schubert directory_switch (char *dirname, int from_tty)
4425796c8dcSSimon Schubert {
4435796c8dcSSimon Schubert add_path (dirname, &source_path, 0);
4445796c8dcSSimon Schubert }
4455796c8dcSSimon Schubert
4465796c8dcSSimon Schubert /* Add zero or more directories to the front of an arbitrary path. */
4475796c8dcSSimon Schubert
4485796c8dcSSimon Schubert void
mod_path(char * dirname,char ** which_path)4495796c8dcSSimon Schubert mod_path (char *dirname, char **which_path)
4505796c8dcSSimon Schubert {
4515796c8dcSSimon Schubert add_path (dirname, which_path, 1);
4525796c8dcSSimon Schubert }
4535796c8dcSSimon Schubert
4545796c8dcSSimon Schubert /* Workhorse of mod_path. Takes an extra argument to determine
4555796c8dcSSimon Schubert if dirname should be parsed for separators that indicate multiple
4565796c8dcSSimon Schubert directories. This allows for interfaces that pre-parse the dirname
4575796c8dcSSimon Schubert and allow specification of traditional separator characters such
4585796c8dcSSimon Schubert as space or tab. */
4595796c8dcSSimon Schubert
4605796c8dcSSimon Schubert void
add_path(char * dirname,char ** which_path,int parse_separators)4615796c8dcSSimon Schubert add_path (char *dirname, char **which_path, int parse_separators)
4625796c8dcSSimon Schubert {
4635796c8dcSSimon Schubert char *old = *which_path;
4645796c8dcSSimon Schubert int prefix = 0;
465*ef5ccd6cSJohn Marino VEC (char_ptr) *dir_vec = NULL;
466*ef5ccd6cSJohn Marino struct cleanup *back_to;
467*ef5ccd6cSJohn Marino int ix;
468*ef5ccd6cSJohn Marino char *name;
4695796c8dcSSimon Schubert
4705796c8dcSSimon Schubert if (dirname == 0)
4715796c8dcSSimon Schubert return;
4725796c8dcSSimon Schubert
4735796c8dcSSimon Schubert if (parse_separators)
4745796c8dcSSimon Schubert {
475*ef5ccd6cSJohn Marino char **argv, **argvp;
4765796c8dcSSimon Schubert
477*ef5ccd6cSJohn Marino /* This will properly parse the space and tab separators
478*ef5ccd6cSJohn Marino and any quotes that may exist. */
479*ef5ccd6cSJohn Marino argv = gdb_buildargv (dirname);
480*ef5ccd6cSJohn Marino
481*ef5ccd6cSJohn Marino for (argvp = argv; *argvp; argvp++)
482*ef5ccd6cSJohn Marino dirnames_to_char_ptr_vec_append (&dir_vec, *argvp);
483*ef5ccd6cSJohn Marino
484*ef5ccd6cSJohn Marino freeargv (argv);
4855796c8dcSSimon Schubert }
4865796c8dcSSimon Schubert else
487*ef5ccd6cSJohn Marino VEC_safe_push (char_ptr, dir_vec, xstrdup (dirname));
488*ef5ccd6cSJohn Marino back_to = make_cleanup_free_char_ptr_vec (dir_vec);
4895796c8dcSSimon Schubert
490*ef5ccd6cSJohn Marino for (ix = 0; VEC_iterate (char_ptr, dir_vec, ix, name); ++ix)
4915796c8dcSSimon Schubert {
4925796c8dcSSimon Schubert char *p;
4935796c8dcSSimon Schubert struct stat st;
4945796c8dcSSimon Schubert
4955796c8dcSSimon Schubert /* Spaces and tabs will have been removed by buildargv().
496*ef5ccd6cSJohn Marino NAME is the start of the directory.
497*ef5ccd6cSJohn Marino P is the '\0' following the end. */
498*ef5ccd6cSJohn Marino p = name + strlen (name);
4995796c8dcSSimon Schubert
5005796c8dcSSimon Schubert while (!(IS_DIR_SEPARATOR (*name) && p <= name + 1) /* "/" */
5015796c8dcSSimon Schubert #ifdef HAVE_DOS_BASED_FILE_SYSTEM
5025796c8dcSSimon Schubert /* On MS-DOS and MS-Windows, h:\ is different from h: */
5035796c8dcSSimon Schubert && !(p == name + 3 && name[1] == ':') /* "d:/" */
5045796c8dcSSimon Schubert #endif
5055796c8dcSSimon Schubert && IS_DIR_SEPARATOR (p[-1]))
5065796c8dcSSimon Schubert /* Sigh. "foo/" => "foo" */
5075796c8dcSSimon Schubert --p;
5085796c8dcSSimon Schubert *p = '\0';
5095796c8dcSSimon Schubert
5105796c8dcSSimon Schubert while (p > name && p[-1] == '.')
5115796c8dcSSimon Schubert {
5125796c8dcSSimon Schubert if (p - name == 1)
5135796c8dcSSimon Schubert {
5145796c8dcSSimon Schubert /* "." => getwd (). */
5155796c8dcSSimon Schubert name = current_directory;
5165796c8dcSSimon Schubert goto append;
5175796c8dcSSimon Schubert }
5185796c8dcSSimon Schubert else if (p > name + 1 && IS_DIR_SEPARATOR (p[-2]))
5195796c8dcSSimon Schubert {
5205796c8dcSSimon Schubert if (p - name == 2)
5215796c8dcSSimon Schubert {
5225796c8dcSSimon Schubert /* "/." => "/". */
5235796c8dcSSimon Schubert *--p = '\0';
5245796c8dcSSimon Schubert goto append;
5255796c8dcSSimon Schubert }
5265796c8dcSSimon Schubert else
5275796c8dcSSimon Schubert {
5285796c8dcSSimon Schubert /* "...foo/." => "...foo". */
5295796c8dcSSimon Schubert p -= 2;
5305796c8dcSSimon Schubert *p = '\0';
5315796c8dcSSimon Schubert continue;
5325796c8dcSSimon Schubert }
5335796c8dcSSimon Schubert }
5345796c8dcSSimon Schubert else
5355796c8dcSSimon Schubert break;
5365796c8dcSSimon Schubert }
5375796c8dcSSimon Schubert
5385796c8dcSSimon Schubert if (name[0] == '~')
5395796c8dcSSimon Schubert name = tilde_expand (name);
5405796c8dcSSimon Schubert #ifdef HAVE_DOS_BASED_FILE_SYSTEM
5415796c8dcSSimon Schubert else if (IS_ABSOLUTE_PATH (name) && p == name + 2) /* "d:" => "d:." */
5425796c8dcSSimon Schubert name = concat (name, ".", (char *)NULL);
5435796c8dcSSimon Schubert #endif
5445796c8dcSSimon Schubert else if (!IS_ABSOLUTE_PATH (name) && name[0] != '$')
5455796c8dcSSimon Schubert name = concat (current_directory, SLASH_STRING, name, (char *)NULL);
5465796c8dcSSimon Schubert else
5475796c8dcSSimon Schubert name = savestring (name, p - name);
5485796c8dcSSimon Schubert make_cleanup (xfree, name);
5495796c8dcSSimon Schubert
5505796c8dcSSimon Schubert /* Unless it's a variable, check existence. */
5515796c8dcSSimon Schubert if (name[0] != '$')
5525796c8dcSSimon Schubert {
5535796c8dcSSimon Schubert /* These are warnings, not errors, since we don't want a
5545796c8dcSSimon Schubert non-existent directory in a .gdbinit file to stop processing
5555796c8dcSSimon Schubert of the .gdbinit file.
5565796c8dcSSimon Schubert
5575796c8dcSSimon Schubert Whether they get added to the path is more debatable. Current
5585796c8dcSSimon Schubert answer is yes, in case the user wants to go make the directory
5595796c8dcSSimon Schubert or whatever. If the directory continues to not exist/not be
5605796c8dcSSimon Schubert a directory/etc, then having them in the path should be
5615796c8dcSSimon Schubert harmless. */
5625796c8dcSSimon Schubert if (stat (name, &st) < 0)
5635796c8dcSSimon Schubert {
5645796c8dcSSimon Schubert int save_errno = errno;
565cf7f2e2dSJohn Marino
5665796c8dcSSimon Schubert fprintf_unfiltered (gdb_stderr, "Warning: ");
5675796c8dcSSimon Schubert print_sys_errmsg (name, save_errno);
5685796c8dcSSimon Schubert }
5695796c8dcSSimon Schubert else if ((st.st_mode & S_IFMT) != S_IFDIR)
5705796c8dcSSimon Schubert warning (_("%s is not a directory."), name);
5715796c8dcSSimon Schubert }
5725796c8dcSSimon Schubert
5735796c8dcSSimon Schubert append:
5745796c8dcSSimon Schubert {
5755796c8dcSSimon Schubert unsigned int len = strlen (name);
576*ef5ccd6cSJohn Marino char tinybuf[2];
5775796c8dcSSimon Schubert
5785796c8dcSSimon Schubert p = *which_path;
579c50c785cSJohn Marino /* FIXME: we should use realpath() or its work-alike
580c50c785cSJohn Marino before comparing. Then all the code above which
5815796c8dcSSimon Schubert removes excess slashes and dots could simply go away. */
582*ef5ccd6cSJohn Marino if (!filename_cmp (p, name))
5835796c8dcSSimon Schubert {
584c50c785cSJohn Marino /* Found it in the search path, remove old copy. */
5855796c8dcSSimon Schubert if (p > *which_path)
586c50c785cSJohn Marino p--; /* Back over leading separator. */
5875796c8dcSSimon Schubert if (prefix > p - *which_path)
588c50c785cSJohn Marino goto skip_dup; /* Same dir twice in one cmd. */
589*ef5ccd6cSJohn Marino memmove (p, &p[len + 1], strlen (&p[len + 1]) + 1); /* Copy from next \0 or : */
5905796c8dcSSimon Schubert }
5915796c8dcSSimon Schubert
5925796c8dcSSimon Schubert tinybuf[0] = DIRNAME_SEPARATOR;
5935796c8dcSSimon Schubert tinybuf[1] = '\0';
5945796c8dcSSimon Schubert
595c50c785cSJohn Marino /* If we have already tacked on a name(s) in this command,
596c50c785cSJohn Marino be sure they stay on the front as we tack on some
597c50c785cSJohn Marino more. */
5985796c8dcSSimon Schubert if (prefix)
5995796c8dcSSimon Schubert {
6005796c8dcSSimon Schubert char *temp, c;
6015796c8dcSSimon Schubert
6025796c8dcSSimon Schubert c = old[prefix];
6035796c8dcSSimon Schubert old[prefix] = '\0';
6045796c8dcSSimon Schubert temp = concat (old, tinybuf, name, (char *)NULL);
6055796c8dcSSimon Schubert old[prefix] = c;
6065796c8dcSSimon Schubert *which_path = concat (temp, "", &old[prefix], (char *) NULL);
6075796c8dcSSimon Schubert prefix = strlen (temp);
6085796c8dcSSimon Schubert xfree (temp);
6095796c8dcSSimon Schubert }
6105796c8dcSSimon Schubert else
6115796c8dcSSimon Schubert {
6125796c8dcSSimon Schubert *which_path = concat (name, (old[0] ? tinybuf : old),
6135796c8dcSSimon Schubert old, (char *)NULL);
6145796c8dcSSimon Schubert prefix = strlen (name);
6155796c8dcSSimon Schubert }
6165796c8dcSSimon Schubert xfree (old);
6175796c8dcSSimon Schubert old = *which_path;
6185796c8dcSSimon Schubert }
619c50c785cSJohn Marino skip_dup:
620c50c785cSJohn Marino ;
6215796c8dcSSimon Schubert }
622*ef5ccd6cSJohn Marino
623*ef5ccd6cSJohn Marino do_cleanups (back_to);
6245796c8dcSSimon Schubert }
6255796c8dcSSimon Schubert
6265796c8dcSSimon Schubert
6275796c8dcSSimon Schubert static void
source_info(char * ignore,int from_tty)6285796c8dcSSimon Schubert source_info (char *ignore, int from_tty)
6295796c8dcSSimon Schubert {
6305796c8dcSSimon Schubert struct symtab *s = current_source_symtab;
6315796c8dcSSimon Schubert
6325796c8dcSSimon Schubert if (!s)
6335796c8dcSSimon Schubert {
6345796c8dcSSimon Schubert printf_filtered (_("No current source file.\n"));
6355796c8dcSSimon Schubert return;
6365796c8dcSSimon Schubert }
6375796c8dcSSimon Schubert printf_filtered (_("Current source file is %s\n"), s->filename);
6385796c8dcSSimon Schubert if (s->dirname)
6395796c8dcSSimon Schubert printf_filtered (_("Compilation directory is %s\n"), s->dirname);
6405796c8dcSSimon Schubert if (s->fullname)
6415796c8dcSSimon Schubert printf_filtered (_("Located in %s\n"), s->fullname);
6425796c8dcSSimon Schubert if (s->nlines)
6435796c8dcSSimon Schubert printf_filtered (_("Contains %d line%s.\n"), s->nlines,
6445796c8dcSSimon Schubert s->nlines == 1 ? "" : "s");
6455796c8dcSSimon Schubert
6465796c8dcSSimon Schubert printf_filtered (_("Source language is %s.\n"), language_str (s->language));
6475796c8dcSSimon Schubert printf_filtered (_("Compiled with %s debugging format.\n"), s->debugformat);
6485796c8dcSSimon Schubert printf_filtered (_("%s preprocessor macro info.\n"),
6495796c8dcSSimon Schubert s->macro_table ? "Includes" : "Does not include");
6505796c8dcSSimon Schubert }
6515796c8dcSSimon Schubert
6525796c8dcSSimon Schubert
653c50c785cSJohn Marino /* Return True if the file NAME exists and is a regular file. */
6545796c8dcSSimon Schubert static int
is_regular_file(const char * name)6555796c8dcSSimon Schubert is_regular_file (const char *name)
6565796c8dcSSimon Schubert {
6575796c8dcSSimon Schubert struct stat st;
6585796c8dcSSimon Schubert const int status = stat (name, &st);
6595796c8dcSSimon Schubert
6605796c8dcSSimon Schubert /* Stat should never fail except when the file does not exist.
6615796c8dcSSimon Schubert If stat fails, analyze the source of error and return True
6625796c8dcSSimon Schubert unless the file does not exist, to avoid returning false results
663c50c785cSJohn Marino on obscure systems where stat does not work as expected. */
664c50c785cSJohn Marino
6655796c8dcSSimon Schubert if (status != 0)
6665796c8dcSSimon Schubert return (errno != ENOENT);
6675796c8dcSSimon Schubert
6685796c8dcSSimon Schubert return S_ISREG (st.st_mode);
6695796c8dcSSimon Schubert }
6705796c8dcSSimon Schubert
6715796c8dcSSimon Schubert /* Open a file named STRING, searching path PATH (dir names sep by some char)
6725796c8dcSSimon Schubert using mode MODE in the calls to open. You cannot use this function to
6735796c8dcSSimon Schubert create files (O_CREAT).
6745796c8dcSSimon Schubert
6755796c8dcSSimon Schubert OPTS specifies the function behaviour in specific cases.
6765796c8dcSSimon Schubert
6775796c8dcSSimon Schubert If OPF_TRY_CWD_FIRST, try to open ./STRING before searching PATH.
6785796c8dcSSimon Schubert (ie pretend the first element of PATH is "."). This also indicates
6795796c8dcSSimon Schubert that a slash in STRING disables searching of the path (this is
6805796c8dcSSimon Schubert so that "exec-file ./foo" or "symbol-file ./foo" insures that you
6815796c8dcSSimon Schubert get that particular version of foo or an error message).
6825796c8dcSSimon Schubert
6835796c8dcSSimon Schubert If OPTS has OPF_SEARCH_IN_PATH set, absolute names will also be
6845796c8dcSSimon Schubert searched in path (we usually want this for source files but not for
6855796c8dcSSimon Schubert executables).
6865796c8dcSSimon Schubert
6875796c8dcSSimon Schubert If FILENAME_OPENED is non-null, set it to a newly allocated string naming
6885796c8dcSSimon Schubert the actual file opened (this string will always start with a "/"). We
6895796c8dcSSimon Schubert have to take special pains to avoid doubling the "/" between the directory
6905796c8dcSSimon Schubert and the file, sigh! Emacs gets confuzzed by this when we print the
6915796c8dcSSimon Schubert source file name!!!
6925796c8dcSSimon Schubert
693*ef5ccd6cSJohn Marino If OPTS does not have OPF_DISABLE_REALPATH set return FILENAME_OPENED
694*ef5ccd6cSJohn Marino resolved by gdb_realpath. Even with OPF_DISABLE_REALPATH this function
695*ef5ccd6cSJohn Marino still returns filename starting with "/". If FILENAME_OPENED is NULL
696*ef5ccd6cSJohn Marino this option has no effect.
697*ef5ccd6cSJohn Marino
6985796c8dcSSimon Schubert If a file is found, return the descriptor.
6995796c8dcSSimon Schubert Otherwise, return -1, with errno set for the last name we tried to open. */
7005796c8dcSSimon Schubert
7015796c8dcSSimon Schubert /* >>>> This should only allow files of certain types,
702c50c785cSJohn Marino >>>> eg executable, non-directory. */
7035796c8dcSSimon Schubert int
openp(const char * path,int opts,const char * string,int mode,char ** filename_opened)7045796c8dcSSimon Schubert openp (const char *path, int opts, const char *string,
7055796c8dcSSimon Schubert int mode, char **filename_opened)
7065796c8dcSSimon Schubert {
7075796c8dcSSimon Schubert int fd;
7085796c8dcSSimon Schubert char *filename;
7095796c8dcSSimon Schubert int alloclen;
710*ef5ccd6cSJohn Marino VEC (char_ptr) *dir_vec;
711*ef5ccd6cSJohn Marino struct cleanup *back_to;
712*ef5ccd6cSJohn Marino int ix;
713*ef5ccd6cSJohn Marino char *dir;
7145796c8dcSSimon Schubert
7155796c8dcSSimon Schubert /* The open syscall MODE parameter is not specified. */
7165796c8dcSSimon Schubert gdb_assert ((mode & O_CREAT) == 0);
717cf7f2e2dSJohn Marino gdb_assert (string != NULL);
718cf7f2e2dSJohn Marino
719cf7f2e2dSJohn Marino /* A file with an empty name cannot possibly exist. Report a failure
720cf7f2e2dSJohn Marino without further checking.
721cf7f2e2dSJohn Marino
722cf7f2e2dSJohn Marino This is an optimization which also defends us against buggy
723cf7f2e2dSJohn Marino implementations of the "stat" function. For instance, we have
724cf7f2e2dSJohn Marino noticed that a MinGW debugger built on Windows XP 32bits crashes
725cf7f2e2dSJohn Marino when the debugger is started with an empty argument. */
726cf7f2e2dSJohn Marino if (string[0] == '\0')
727cf7f2e2dSJohn Marino {
728cf7f2e2dSJohn Marino errno = ENOENT;
729cf7f2e2dSJohn Marino return -1;
730cf7f2e2dSJohn Marino }
7315796c8dcSSimon Schubert
7325796c8dcSSimon Schubert if (!path)
7335796c8dcSSimon Schubert path = ".";
7345796c8dcSSimon Schubert
7355796c8dcSSimon Schubert mode |= O_BINARY;
7365796c8dcSSimon Schubert
7375796c8dcSSimon Schubert if ((opts & OPF_TRY_CWD_FIRST) || IS_ABSOLUTE_PATH (string))
7385796c8dcSSimon Schubert {
7395796c8dcSSimon Schubert int i;
7405796c8dcSSimon Schubert
7415796c8dcSSimon Schubert if (is_regular_file (string))
7425796c8dcSSimon Schubert {
7435796c8dcSSimon Schubert filename = alloca (strlen (string) + 1);
7445796c8dcSSimon Schubert strcpy (filename, string);
7455796c8dcSSimon Schubert fd = open (filename, mode);
7465796c8dcSSimon Schubert if (fd >= 0)
7475796c8dcSSimon Schubert goto done;
7485796c8dcSSimon Schubert }
7495796c8dcSSimon Schubert else
7505796c8dcSSimon Schubert {
7515796c8dcSSimon Schubert filename = NULL;
7525796c8dcSSimon Schubert fd = -1;
7535796c8dcSSimon Schubert }
7545796c8dcSSimon Schubert
7555796c8dcSSimon Schubert if (!(opts & OPF_SEARCH_IN_PATH))
7565796c8dcSSimon Schubert for (i = 0; string[i]; i++)
7575796c8dcSSimon Schubert if (IS_DIR_SEPARATOR (string[i]))
7585796c8dcSSimon Schubert goto done;
7595796c8dcSSimon Schubert }
7605796c8dcSSimon Schubert
761cf7f2e2dSJohn Marino /* For dos paths, d:/foo -> /foo, and d:foo -> foo. */
762cf7f2e2dSJohn Marino if (HAS_DRIVE_SPEC (string))
763cf7f2e2dSJohn Marino string = STRIP_DRIVE_SPEC (string);
764cf7f2e2dSJohn Marino
7655796c8dcSSimon Schubert /* /foo => foo, to avoid multiple slashes that Emacs doesn't like. */
7665796c8dcSSimon Schubert while (IS_DIR_SEPARATOR(string[0]))
7675796c8dcSSimon Schubert string++;
7685796c8dcSSimon Schubert
7695796c8dcSSimon Schubert /* ./foo => foo */
7705796c8dcSSimon Schubert while (string[0] == '.' && IS_DIR_SEPARATOR (string[1]))
7715796c8dcSSimon Schubert string += 2;
7725796c8dcSSimon Schubert
7735796c8dcSSimon Schubert alloclen = strlen (path) + strlen (string) + 2;
7745796c8dcSSimon Schubert filename = alloca (alloclen);
7755796c8dcSSimon Schubert fd = -1;
7765796c8dcSSimon Schubert
777*ef5ccd6cSJohn Marino dir_vec = dirnames_to_char_ptr_vec (path);
778*ef5ccd6cSJohn Marino back_to = make_cleanup_free_char_ptr_vec (dir_vec);
779*ef5ccd6cSJohn Marino
780*ef5ccd6cSJohn Marino for (ix = 0; VEC_iterate (char_ptr, dir_vec, ix, dir); ++ix)
781*ef5ccd6cSJohn Marino {
782*ef5ccd6cSJohn Marino size_t len = strlen (dir);
783*ef5ccd6cSJohn Marino
784*ef5ccd6cSJohn Marino if (strcmp (dir, "$cwd") == 0)
7855796c8dcSSimon Schubert {
7865796c8dcSSimon Schubert /* Name is $cwd -- insert current directory name instead. */
7875796c8dcSSimon Schubert int newlen;
7885796c8dcSSimon Schubert
7895796c8dcSSimon Schubert /* First, realloc the filename buffer if too short. */
7905796c8dcSSimon Schubert len = strlen (current_directory);
7915796c8dcSSimon Schubert newlen = len + strlen (string) + 2;
7925796c8dcSSimon Schubert if (newlen > alloclen)
7935796c8dcSSimon Schubert {
7945796c8dcSSimon Schubert alloclen = newlen;
7955796c8dcSSimon Schubert filename = alloca (alloclen);
7965796c8dcSSimon Schubert }
7975796c8dcSSimon Schubert strcpy (filename, current_directory);
7985796c8dcSSimon Schubert }
799*ef5ccd6cSJohn Marino else if (strchr(dir, '~'))
800*ef5ccd6cSJohn Marino {
801*ef5ccd6cSJohn Marino /* See whether we need to expand the tilde. */
802*ef5ccd6cSJohn Marino int newlen;
803*ef5ccd6cSJohn Marino char *tilde_expanded;
804*ef5ccd6cSJohn Marino
805*ef5ccd6cSJohn Marino tilde_expanded = tilde_expand (dir);
806*ef5ccd6cSJohn Marino
807*ef5ccd6cSJohn Marino /* First, realloc the filename buffer if too short. */
808*ef5ccd6cSJohn Marino len = strlen (tilde_expanded);
809*ef5ccd6cSJohn Marino newlen = len + strlen (string) + 2;
810*ef5ccd6cSJohn Marino if (newlen > alloclen)
811*ef5ccd6cSJohn Marino {
812*ef5ccd6cSJohn Marino alloclen = newlen;
813*ef5ccd6cSJohn Marino filename = alloca (alloclen);
814*ef5ccd6cSJohn Marino }
815*ef5ccd6cSJohn Marino strcpy (filename, tilde_expanded);
816*ef5ccd6cSJohn Marino xfree (tilde_expanded);
817*ef5ccd6cSJohn Marino }
8185796c8dcSSimon Schubert else
8195796c8dcSSimon Schubert {
8205796c8dcSSimon Schubert /* Normal file name in path -- just use it. */
821*ef5ccd6cSJohn Marino strcpy (filename, dir);
822cf7f2e2dSJohn Marino
823cf7f2e2dSJohn Marino /* Don't search $cdir. It's also a magic path like $cwd, but we
824cf7f2e2dSJohn Marino don't have enough information to expand it. The user *could*
825cf7f2e2dSJohn Marino have an actual directory named '$cdir' but handling that would
826cf7f2e2dSJohn Marino be confusing, it would mean different things in different
827cf7f2e2dSJohn Marino contexts. If the user really has '$cdir' one can use './$cdir'.
828cf7f2e2dSJohn Marino We can get $cdir when loading scripts. When loading source files
829cf7f2e2dSJohn Marino $cdir must have already been expanded to the correct value. */
830*ef5ccd6cSJohn Marino if (strcmp (dir, "$cdir") == 0)
831cf7f2e2dSJohn Marino continue;
8325796c8dcSSimon Schubert }
8335796c8dcSSimon Schubert
834c50c785cSJohn Marino /* Remove trailing slashes. */
8355796c8dcSSimon Schubert while (len > 0 && IS_DIR_SEPARATOR (filename[len - 1]))
8365796c8dcSSimon Schubert filename[--len] = 0;
8375796c8dcSSimon Schubert
8385796c8dcSSimon Schubert strcat (filename + len, SLASH_STRING);
8395796c8dcSSimon Schubert strcat (filename, string);
8405796c8dcSSimon Schubert
8415796c8dcSSimon Schubert if (is_regular_file (filename))
8425796c8dcSSimon Schubert {
8435796c8dcSSimon Schubert fd = open (filename, mode);
8445796c8dcSSimon Schubert if (fd >= 0)
8455796c8dcSSimon Schubert break;
8465796c8dcSSimon Schubert }
8475796c8dcSSimon Schubert }
8485796c8dcSSimon Schubert
849*ef5ccd6cSJohn Marino do_cleanups (back_to);
850*ef5ccd6cSJohn Marino
8515796c8dcSSimon Schubert done:
8525796c8dcSSimon Schubert if (filename_opened)
8535796c8dcSSimon Schubert {
854*ef5ccd6cSJohn Marino /* If a file was opened, canonicalize its filename. */
8555796c8dcSSimon Schubert if (fd < 0)
8565796c8dcSSimon Schubert *filename_opened = NULL;
857*ef5ccd6cSJohn Marino else
858*ef5ccd6cSJohn Marino {
859*ef5ccd6cSJohn Marino char *(*realpath_fptr) (const char *);
860*ef5ccd6cSJohn Marino
861*ef5ccd6cSJohn Marino realpath_fptr = ((opts & OPF_DISABLE_REALPATH) != 0
862*ef5ccd6cSJohn Marino ? xstrdup : gdb_realpath);
863*ef5ccd6cSJohn Marino
864*ef5ccd6cSJohn Marino if (IS_ABSOLUTE_PATH (filename))
865*ef5ccd6cSJohn Marino *filename_opened = realpath_fptr (filename);
8665796c8dcSSimon Schubert else
8675796c8dcSSimon Schubert {
8685796c8dcSSimon Schubert /* Beware the // my son, the Emacs barfs, the botch that catch... */
8695796c8dcSSimon Schubert
8705796c8dcSSimon Schubert char *f = concat (current_directory,
8715796c8dcSSimon Schubert IS_DIR_SEPARATOR (current_directory[strlen (current_directory) - 1])
8725796c8dcSSimon Schubert ? "" : SLASH_STRING,
8735796c8dcSSimon Schubert filename, (char *)NULL);
874cf7f2e2dSJohn Marino
875*ef5ccd6cSJohn Marino *filename_opened = realpath_fptr (f);
8765796c8dcSSimon Schubert xfree (f);
8775796c8dcSSimon Schubert }
8785796c8dcSSimon Schubert }
879*ef5ccd6cSJohn Marino }
8805796c8dcSSimon Schubert
8815796c8dcSSimon Schubert return fd;
8825796c8dcSSimon Schubert }
8835796c8dcSSimon Schubert
8845796c8dcSSimon Schubert
8855796c8dcSSimon Schubert /* This is essentially a convenience, for clients that want the behaviour
8865796c8dcSSimon Schubert of openp, using source_path, but that really don't want the file to be
8875796c8dcSSimon Schubert opened but want instead just to know what the full pathname is (as
8885796c8dcSSimon Schubert qualified against source_path).
8895796c8dcSSimon Schubert
8905796c8dcSSimon Schubert The current working directory is searched first.
8915796c8dcSSimon Schubert
8925796c8dcSSimon Schubert If the file was found, this function returns 1, and FULL_PATHNAME is
8935796c8dcSSimon Schubert set to the fully-qualified pathname.
8945796c8dcSSimon Schubert
8955796c8dcSSimon Schubert Else, this functions returns 0, and FULL_PATHNAME is set to NULL. */
8965796c8dcSSimon Schubert int
source_full_path_of(const char * filename,char ** full_pathname)8975796c8dcSSimon Schubert source_full_path_of (const char *filename, char **full_pathname)
8985796c8dcSSimon Schubert {
8995796c8dcSSimon Schubert int fd;
9005796c8dcSSimon Schubert
9015796c8dcSSimon Schubert fd = openp (source_path, OPF_TRY_CWD_FIRST | OPF_SEARCH_IN_PATH, filename,
9025796c8dcSSimon Schubert O_RDONLY, full_pathname);
9035796c8dcSSimon Schubert if (fd < 0)
9045796c8dcSSimon Schubert {
9055796c8dcSSimon Schubert *full_pathname = NULL;
9065796c8dcSSimon Schubert return 0;
9075796c8dcSSimon Schubert }
9085796c8dcSSimon Schubert
9095796c8dcSSimon Schubert close (fd);
9105796c8dcSSimon Schubert return 1;
9115796c8dcSSimon Schubert }
9125796c8dcSSimon Schubert
9135796c8dcSSimon Schubert /* Return non-zero if RULE matches PATH, that is if the rule can be
9145796c8dcSSimon Schubert applied to PATH. */
9155796c8dcSSimon Schubert
9165796c8dcSSimon Schubert static int
substitute_path_rule_matches(const struct substitute_path_rule * rule,const char * path)9175796c8dcSSimon Schubert substitute_path_rule_matches (const struct substitute_path_rule *rule,
9185796c8dcSSimon Schubert const char *path)
9195796c8dcSSimon Schubert {
9205796c8dcSSimon Schubert const int from_len = strlen (rule->from);
9215796c8dcSSimon Schubert const int path_len = strlen (path);
9225796c8dcSSimon Schubert char *path_start;
9235796c8dcSSimon Schubert
9245796c8dcSSimon Schubert if (path_len < from_len)
9255796c8dcSSimon Schubert return 0;
9265796c8dcSSimon Schubert
9275796c8dcSSimon Schubert /* The substitution rules are anchored at the start of the path,
9285796c8dcSSimon Schubert so the path should start with rule->from. There is no filename
9295796c8dcSSimon Schubert comparison routine, so we need to extract the first FROM_LEN
9305796c8dcSSimon Schubert characters from PATH first and use that to do the comparison. */
9315796c8dcSSimon Schubert
9325796c8dcSSimon Schubert path_start = alloca (from_len + 1);
9335796c8dcSSimon Schubert strncpy (path_start, path, from_len);
9345796c8dcSSimon Schubert path_start[from_len] = '\0';
9355796c8dcSSimon Schubert
9365796c8dcSSimon Schubert if (FILENAME_CMP (path_start, rule->from) != 0)
9375796c8dcSSimon Schubert return 0;
9385796c8dcSSimon Schubert
9395796c8dcSSimon Schubert /* Make sure that the region in the path that matches the substitution
9405796c8dcSSimon Schubert rule is immediately followed by a directory separator (or the end of
9415796c8dcSSimon Schubert string character). */
9425796c8dcSSimon Schubert
9435796c8dcSSimon Schubert if (path[from_len] != '\0' && !IS_DIR_SEPARATOR (path[from_len]))
9445796c8dcSSimon Schubert return 0;
9455796c8dcSSimon Schubert
9465796c8dcSSimon Schubert return 1;
9475796c8dcSSimon Schubert }
9485796c8dcSSimon Schubert
9495796c8dcSSimon Schubert /* Find the substitute-path rule that applies to PATH and return it.
9505796c8dcSSimon Schubert Return NULL if no rule applies. */
9515796c8dcSSimon Schubert
9525796c8dcSSimon Schubert static struct substitute_path_rule *
get_substitute_path_rule(const char * path)9535796c8dcSSimon Schubert get_substitute_path_rule (const char *path)
9545796c8dcSSimon Schubert {
9555796c8dcSSimon Schubert struct substitute_path_rule *rule = substitute_path_rules;
9565796c8dcSSimon Schubert
9575796c8dcSSimon Schubert while (rule != NULL && !substitute_path_rule_matches (rule, path))
9585796c8dcSSimon Schubert rule = rule->next;
9595796c8dcSSimon Schubert
9605796c8dcSSimon Schubert return rule;
9615796c8dcSSimon Schubert }
9625796c8dcSSimon Schubert
9635796c8dcSSimon Schubert /* If the user specified a source path substitution rule that applies
9645796c8dcSSimon Schubert to PATH, then apply it and return the new path. This new path must
9655796c8dcSSimon Schubert be deallocated afterwards.
9665796c8dcSSimon Schubert
9675796c8dcSSimon Schubert Return NULL if no substitution rule was specified by the user,
9685796c8dcSSimon Schubert or if no rule applied to the given PATH. */
9695796c8dcSSimon Schubert
970*ef5ccd6cSJohn Marino char *
rewrite_source_path(const char * path)9715796c8dcSSimon Schubert rewrite_source_path (const char *path)
9725796c8dcSSimon Schubert {
9735796c8dcSSimon Schubert const struct substitute_path_rule *rule = get_substitute_path_rule (path);
9745796c8dcSSimon Schubert char *new_path;
9755796c8dcSSimon Schubert int from_len;
9765796c8dcSSimon Schubert
9775796c8dcSSimon Schubert if (rule == NULL)
9785796c8dcSSimon Schubert return NULL;
9795796c8dcSSimon Schubert
9805796c8dcSSimon Schubert from_len = strlen (rule->from);
9815796c8dcSSimon Schubert
9825796c8dcSSimon Schubert /* Compute the rewritten path and return it. */
9835796c8dcSSimon Schubert
9845796c8dcSSimon Schubert new_path =
9855796c8dcSSimon Schubert (char *) xmalloc (strlen (path) + 1 + strlen (rule->to) - from_len);
9865796c8dcSSimon Schubert strcpy (new_path, rule->to);
9875796c8dcSSimon Schubert strcat (new_path, path + from_len);
9885796c8dcSSimon Schubert
9895796c8dcSSimon Schubert return new_path;
9905796c8dcSSimon Schubert }
9915796c8dcSSimon Schubert
992cf7f2e2dSJohn Marino int
find_and_open_source(const char * filename,const char * dirname,char ** fullname)9935796c8dcSSimon Schubert find_and_open_source (const char *filename,
9945796c8dcSSimon Schubert const char *dirname,
9955796c8dcSSimon Schubert char **fullname)
9965796c8dcSSimon Schubert {
9975796c8dcSSimon Schubert char *path = source_path;
9985796c8dcSSimon Schubert const char *p;
9995796c8dcSSimon Schubert int result;
10005796c8dcSSimon Schubert
1001c50c785cSJohn Marino /* Quick way out if we already know its full name. */
10025796c8dcSSimon Schubert
10035796c8dcSSimon Schubert if (*fullname)
10045796c8dcSSimon Schubert {
10055796c8dcSSimon Schubert /* The user may have requested that source paths be rewritten
10065796c8dcSSimon Schubert according to substitution rules he provided. If a substitution
10075796c8dcSSimon Schubert rule applies to this path, then apply it. */
10085796c8dcSSimon Schubert char *rewritten_fullname = rewrite_source_path (*fullname);
10095796c8dcSSimon Schubert
10105796c8dcSSimon Schubert if (rewritten_fullname != NULL)
10115796c8dcSSimon Schubert {
10125796c8dcSSimon Schubert xfree (*fullname);
10135796c8dcSSimon Schubert *fullname = rewritten_fullname;
10145796c8dcSSimon Schubert }
10155796c8dcSSimon Schubert
10165796c8dcSSimon Schubert result = open (*fullname, OPEN_MODE);
10175796c8dcSSimon Schubert if (result >= 0)
1018*ef5ccd6cSJohn Marino {
1019*ef5ccd6cSJohn Marino char *lpath = gdb_realpath (*fullname);
1020*ef5ccd6cSJohn Marino
1021*ef5ccd6cSJohn Marino xfree (*fullname);
1022*ef5ccd6cSJohn Marino *fullname = lpath;
10235796c8dcSSimon Schubert return result;
1024*ef5ccd6cSJohn Marino }
1025*ef5ccd6cSJohn Marino
10265796c8dcSSimon Schubert /* Didn't work -- free old one, try again. */
10275796c8dcSSimon Schubert xfree (*fullname);
10285796c8dcSSimon Schubert *fullname = NULL;
10295796c8dcSSimon Schubert }
10305796c8dcSSimon Schubert
10315796c8dcSSimon Schubert if (dirname != NULL)
10325796c8dcSSimon Schubert {
10335796c8dcSSimon Schubert /* If necessary, rewrite the compilation directory name according
10345796c8dcSSimon Schubert to the source path substitution rules specified by the user. */
10355796c8dcSSimon Schubert
10365796c8dcSSimon Schubert char *rewritten_dirname = rewrite_source_path (dirname);
10375796c8dcSSimon Schubert
10385796c8dcSSimon Schubert if (rewritten_dirname != NULL)
10395796c8dcSSimon Schubert {
10405796c8dcSSimon Schubert make_cleanup (xfree, rewritten_dirname);
10415796c8dcSSimon Schubert dirname = rewritten_dirname;
10425796c8dcSSimon Schubert }
10435796c8dcSSimon Schubert
1044c50c785cSJohn Marino /* Replace a path entry of $cdir with the compilation directory
1045c50c785cSJohn Marino name. */
10465796c8dcSSimon Schubert #define cdir_len 5
10475796c8dcSSimon Schubert /* We cast strstr's result in case an ANSIhole has made it const,
10485796c8dcSSimon Schubert which produces a "required warning" when assigned to a nonconst. */
10495796c8dcSSimon Schubert p = (char *) strstr (source_path, "$cdir");
10505796c8dcSSimon Schubert if (p && (p == path || p[-1] == DIRNAME_SEPARATOR)
10515796c8dcSSimon Schubert && (p[cdir_len] == DIRNAME_SEPARATOR || p[cdir_len] == '\0'))
10525796c8dcSSimon Schubert {
10535796c8dcSSimon Schubert int len;
10545796c8dcSSimon Schubert
10555796c8dcSSimon Schubert path = (char *)
10565796c8dcSSimon Schubert alloca (strlen (source_path) + 1 + strlen (dirname) + 1);
10575796c8dcSSimon Schubert len = p - source_path;
10585796c8dcSSimon Schubert strncpy (path, source_path, len); /* Before $cdir */
10595796c8dcSSimon Schubert strcpy (path + len, dirname); /* new stuff */
1060c50c785cSJohn Marino strcat (path + len, source_path + len + cdir_len); /* After
1061c50c785cSJohn Marino $cdir */
10625796c8dcSSimon Schubert }
10635796c8dcSSimon Schubert }
10645796c8dcSSimon Schubert
10655796c8dcSSimon Schubert if (IS_ABSOLUTE_PATH (filename))
10665796c8dcSSimon Schubert {
10675796c8dcSSimon Schubert /* If filename is absolute path, try the source path
10685796c8dcSSimon Schubert substitution on it. */
10695796c8dcSSimon Schubert char *rewritten_filename = rewrite_source_path (filename);
10705796c8dcSSimon Schubert
10715796c8dcSSimon Schubert if (rewritten_filename != NULL)
10725796c8dcSSimon Schubert {
10735796c8dcSSimon Schubert make_cleanup (xfree, rewritten_filename);
10745796c8dcSSimon Schubert filename = rewritten_filename;
10755796c8dcSSimon Schubert }
10765796c8dcSSimon Schubert }
10775796c8dcSSimon Schubert
10785796c8dcSSimon Schubert result = openp (path, OPF_SEARCH_IN_PATH, filename, OPEN_MODE, fullname);
10795796c8dcSSimon Schubert if (result < 0)
10805796c8dcSSimon Schubert {
10815796c8dcSSimon Schubert /* Didn't work. Try using just the basename. */
10825796c8dcSSimon Schubert p = lbasename (filename);
10835796c8dcSSimon Schubert if (p != filename)
10845796c8dcSSimon Schubert result = openp (path, OPF_SEARCH_IN_PATH, p, OPEN_MODE, fullname);
10855796c8dcSSimon Schubert }
10865796c8dcSSimon Schubert
10875796c8dcSSimon Schubert return result;
10885796c8dcSSimon Schubert }
10895796c8dcSSimon Schubert
10905796c8dcSSimon Schubert /* Open a source file given a symtab S. Returns a file descriptor or
10915796c8dcSSimon Schubert negative number for error.
10925796c8dcSSimon Schubert
10935796c8dcSSimon Schubert This function is a convience function to find_and_open_source. */
10945796c8dcSSimon Schubert
10955796c8dcSSimon Schubert int
open_source_file(struct symtab * s)10965796c8dcSSimon Schubert open_source_file (struct symtab *s)
10975796c8dcSSimon Schubert {
10985796c8dcSSimon Schubert if (!s)
10995796c8dcSSimon Schubert return -1;
11005796c8dcSSimon Schubert
11015796c8dcSSimon Schubert return find_and_open_source (s->filename, s->dirname, &s->fullname);
11025796c8dcSSimon Schubert }
11035796c8dcSSimon Schubert
11045796c8dcSSimon Schubert /* Finds the fullname that a symtab represents.
11055796c8dcSSimon Schubert
1106*ef5ccd6cSJohn Marino This functions finds the fullname and saves it in s->fullname.
1107*ef5ccd6cSJohn Marino It will also return the value.
11085796c8dcSSimon Schubert
11095796c8dcSSimon Schubert If this function fails to find the file that this symtab represents,
1110*ef5ccd6cSJohn Marino the expected fullname is used. Therefore the files does not have to
1111*ef5ccd6cSJohn Marino exist. */
1112a45ae5f8SJohn Marino
1113*ef5ccd6cSJohn Marino const char *
symtab_to_fullname(struct symtab * s)11145796c8dcSSimon Schubert symtab_to_fullname (struct symtab *s)
11155796c8dcSSimon Schubert {
1116a45ae5f8SJohn Marino /* Use cached copy if we have it.
1117a45ae5f8SJohn Marino We rely on forget_cached_source_info being called appropriately
1118a45ae5f8SJohn Marino to handle cases like the file being moved. */
1119*ef5ccd6cSJohn Marino if (s->fullname == NULL)
11205796c8dcSSimon Schubert {
1121*ef5ccd6cSJohn Marino int fd = find_and_open_source (s->filename, s->dirname, &s->fullname);
1122*ef5ccd6cSJohn Marino
1123*ef5ccd6cSJohn Marino if (fd >= 0)
1124*ef5ccd6cSJohn Marino close (fd);
1125*ef5ccd6cSJohn Marino else
1126*ef5ccd6cSJohn Marino {
1127*ef5ccd6cSJohn Marino char *fullname;
1128*ef5ccd6cSJohn Marino struct cleanup *back_to;
1129*ef5ccd6cSJohn Marino
1130*ef5ccd6cSJohn Marino /* rewrite_source_path would be applied by find_and_open_source, we
1131*ef5ccd6cSJohn Marino should report the pathname where GDB tried to find the file. */
1132*ef5ccd6cSJohn Marino
1133*ef5ccd6cSJohn Marino if (s->dirname == NULL || IS_ABSOLUTE_PATH (s->filename))
1134*ef5ccd6cSJohn Marino fullname = xstrdup (s->filename);
1135*ef5ccd6cSJohn Marino else
1136*ef5ccd6cSJohn Marino fullname = concat (s->dirname, SLASH_STRING, s->filename, NULL);
1137*ef5ccd6cSJohn Marino
1138*ef5ccd6cSJohn Marino back_to = make_cleanup (xfree, fullname);
1139*ef5ccd6cSJohn Marino s->fullname = rewrite_source_path (fullname);
1140*ef5ccd6cSJohn Marino if (s->fullname == NULL)
1141*ef5ccd6cSJohn Marino s->fullname = xstrdup (fullname);
1142*ef5ccd6cSJohn Marino do_cleanups (back_to);
1143*ef5ccd6cSJohn Marino }
1144*ef5ccd6cSJohn Marino }
1145*ef5ccd6cSJohn Marino
11465796c8dcSSimon Schubert return s->fullname;
11475796c8dcSSimon Schubert }
11485796c8dcSSimon Schubert
1149*ef5ccd6cSJohn Marino /* See commentary in source.h. */
1150*ef5ccd6cSJohn Marino
1151*ef5ccd6cSJohn Marino const char *
symtab_to_filename_for_display(struct symtab * symtab)1152*ef5ccd6cSJohn Marino symtab_to_filename_for_display (struct symtab *symtab)
1153*ef5ccd6cSJohn Marino {
1154*ef5ccd6cSJohn Marino if (filename_display_string == filename_display_basename)
1155*ef5ccd6cSJohn Marino return lbasename (symtab->filename);
1156*ef5ccd6cSJohn Marino else if (filename_display_string == filename_display_absolute)
1157*ef5ccd6cSJohn Marino return symtab_to_fullname (symtab);
1158*ef5ccd6cSJohn Marino else if (filename_display_string == filename_display_relative)
1159*ef5ccd6cSJohn Marino return symtab->filename;
1160*ef5ccd6cSJohn Marino else
1161*ef5ccd6cSJohn Marino internal_error (__FILE__, __LINE__, _("invalid filename_display_string"));
11625796c8dcSSimon Schubert }
11635796c8dcSSimon Schubert
11645796c8dcSSimon Schubert /* Create and initialize the table S->line_charpos that records
11655796c8dcSSimon Schubert the positions of the lines in the source file, which is assumed
11665796c8dcSSimon Schubert to be open on descriptor DESC.
11675796c8dcSSimon Schubert All set S->nlines to the number of such lines. */
11685796c8dcSSimon Schubert
11695796c8dcSSimon Schubert void
find_source_lines(struct symtab * s,int desc)11705796c8dcSSimon Schubert find_source_lines (struct symtab *s, int desc)
11715796c8dcSSimon Schubert {
11725796c8dcSSimon Schubert struct stat st;
11735796c8dcSSimon Schubert char *data, *p, *end;
11745796c8dcSSimon Schubert int nlines = 0;
11755796c8dcSSimon Schubert int lines_allocated = 1000;
11765796c8dcSSimon Schubert int *line_charpos;
11775796c8dcSSimon Schubert long mtime = 0;
11785796c8dcSSimon Schubert int size;
11795796c8dcSSimon Schubert
11805796c8dcSSimon Schubert gdb_assert (s);
11815796c8dcSSimon Schubert line_charpos = (int *) xmalloc (lines_allocated * sizeof (int));
11825796c8dcSSimon Schubert if (fstat (desc, &st) < 0)
1183*ef5ccd6cSJohn Marino perror_with_name (symtab_to_filename_for_display (s));
11845796c8dcSSimon Schubert
11855796c8dcSSimon Schubert if (s->objfile && s->objfile->obfd)
11865796c8dcSSimon Schubert mtime = s->objfile->mtime;
11875796c8dcSSimon Schubert else if (exec_bfd)
11885796c8dcSSimon Schubert mtime = exec_bfd_mtime;
11895796c8dcSSimon Schubert
11905796c8dcSSimon Schubert if (mtime && mtime < st.st_mtime)
11915796c8dcSSimon Schubert warning (_("Source file is more recent than executable."));
11925796c8dcSSimon Schubert
11935796c8dcSSimon Schubert {
11945796c8dcSSimon Schubert struct cleanup *old_cleanups;
11955796c8dcSSimon Schubert
11965796c8dcSSimon Schubert /* st_size might be a large type, but we only support source files whose
11975796c8dcSSimon Schubert size fits in an int. */
11985796c8dcSSimon Schubert size = (int) st.st_size;
11995796c8dcSSimon Schubert
12005796c8dcSSimon Schubert /* Use malloc, not alloca, because this may be pretty large, and we may
12015796c8dcSSimon Schubert run into various kinds of limits on stack size. */
12025796c8dcSSimon Schubert data = (char *) xmalloc (size);
12035796c8dcSSimon Schubert old_cleanups = make_cleanup (xfree, data);
12045796c8dcSSimon Schubert
12055796c8dcSSimon Schubert /* Reassign `size' to result of read for systems where \r\n -> \n. */
12065796c8dcSSimon Schubert size = myread (desc, data, size);
12075796c8dcSSimon Schubert if (size < 0)
1208*ef5ccd6cSJohn Marino perror_with_name (symtab_to_filename_for_display (s));
12095796c8dcSSimon Schubert end = data + size;
12105796c8dcSSimon Schubert p = data;
12115796c8dcSSimon Schubert line_charpos[0] = 0;
12125796c8dcSSimon Schubert nlines = 1;
12135796c8dcSSimon Schubert while (p != end)
12145796c8dcSSimon Schubert {
12155796c8dcSSimon Schubert if (*p++ == '\n'
12165796c8dcSSimon Schubert /* A newline at the end does not start a new line. */
12175796c8dcSSimon Schubert && p != end)
12185796c8dcSSimon Schubert {
12195796c8dcSSimon Schubert if (nlines == lines_allocated)
12205796c8dcSSimon Schubert {
12215796c8dcSSimon Schubert lines_allocated *= 2;
12225796c8dcSSimon Schubert line_charpos =
12235796c8dcSSimon Schubert (int *) xrealloc ((char *) line_charpos,
12245796c8dcSSimon Schubert sizeof (int) * lines_allocated);
12255796c8dcSSimon Schubert }
12265796c8dcSSimon Schubert line_charpos[nlines++] = p - data;
12275796c8dcSSimon Schubert }
12285796c8dcSSimon Schubert }
12295796c8dcSSimon Schubert do_cleanups (old_cleanups);
12305796c8dcSSimon Schubert }
1231a45ae5f8SJohn Marino
12325796c8dcSSimon Schubert s->nlines = nlines;
12335796c8dcSSimon Schubert s->line_charpos =
12345796c8dcSSimon Schubert (int *) xrealloc ((char *) line_charpos, nlines * sizeof (int));
12355796c8dcSSimon Schubert
12365796c8dcSSimon Schubert }
12375796c8dcSSimon Schubert
12385796c8dcSSimon Schubert
12395796c8dcSSimon Schubert
12405796c8dcSSimon Schubert /* Get full pathname and line number positions for a symtab.
12415796c8dcSSimon Schubert Return nonzero if line numbers may have changed.
12425796c8dcSSimon Schubert Set *FULLNAME to actual name of the file as found by `openp',
12435796c8dcSSimon Schubert or to 0 if the file is not found. */
12445796c8dcSSimon Schubert
12455796c8dcSSimon Schubert static int
get_filename_and_charpos(struct symtab * s,char ** fullname)12465796c8dcSSimon Schubert get_filename_and_charpos (struct symtab *s, char **fullname)
12475796c8dcSSimon Schubert {
12485796c8dcSSimon Schubert int desc, linenums_changed = 0;
12495796c8dcSSimon Schubert struct cleanup *cleanups;
12505796c8dcSSimon Schubert
12515796c8dcSSimon Schubert desc = open_source_file (s);
12525796c8dcSSimon Schubert if (desc < 0)
12535796c8dcSSimon Schubert {
12545796c8dcSSimon Schubert if (fullname)
12555796c8dcSSimon Schubert *fullname = NULL;
12565796c8dcSSimon Schubert return 0;
12575796c8dcSSimon Schubert }
12585796c8dcSSimon Schubert cleanups = make_cleanup_close (desc);
12595796c8dcSSimon Schubert if (fullname)
12605796c8dcSSimon Schubert *fullname = s->fullname;
12615796c8dcSSimon Schubert if (s->line_charpos == 0)
12625796c8dcSSimon Schubert linenums_changed = 1;
12635796c8dcSSimon Schubert if (linenums_changed)
12645796c8dcSSimon Schubert find_source_lines (s, desc);
12655796c8dcSSimon Schubert do_cleanups (cleanups);
12665796c8dcSSimon Schubert return linenums_changed;
12675796c8dcSSimon Schubert }
12685796c8dcSSimon Schubert
12695796c8dcSSimon Schubert /* Print text describing the full name of the source file S
12705796c8dcSSimon Schubert and the line number LINE and its corresponding character position.
12715796c8dcSSimon Schubert The text starts with two Ctrl-z so that the Emacs-GDB interface
12725796c8dcSSimon Schubert can easily find it.
12735796c8dcSSimon Schubert
12745796c8dcSSimon Schubert MID_STATEMENT is nonzero if the PC is not at the beginning of that line.
12755796c8dcSSimon Schubert
12765796c8dcSSimon Schubert Return 1 if successful, 0 if could not find the file. */
12775796c8dcSSimon Schubert
12785796c8dcSSimon Schubert int
identify_source_line(struct symtab * s,int line,int mid_statement,CORE_ADDR pc)12795796c8dcSSimon Schubert identify_source_line (struct symtab *s, int line, int mid_statement,
12805796c8dcSSimon Schubert CORE_ADDR pc)
12815796c8dcSSimon Schubert {
12825796c8dcSSimon Schubert if (s->line_charpos == 0)
12835796c8dcSSimon Schubert get_filename_and_charpos (s, (char **) NULL);
12845796c8dcSSimon Schubert if (s->fullname == 0)
12855796c8dcSSimon Schubert return 0;
12865796c8dcSSimon Schubert if (line > s->nlines)
12875796c8dcSSimon Schubert /* Don't index off the end of the line_charpos array. */
12885796c8dcSSimon Schubert return 0;
12895796c8dcSSimon Schubert annotate_source (s->fullname, line, s->line_charpos[line - 1],
12905796c8dcSSimon Schubert mid_statement, get_objfile_arch (s->objfile), pc);
12915796c8dcSSimon Schubert
12925796c8dcSSimon Schubert current_source_line = line;
12935796c8dcSSimon Schubert first_line_listed = line;
12945796c8dcSSimon Schubert last_line_listed = line;
12955796c8dcSSimon Schubert current_source_symtab = s;
12965796c8dcSSimon Schubert return 1;
12975796c8dcSSimon Schubert }
12985796c8dcSSimon Schubert
12995796c8dcSSimon Schubert
13005796c8dcSSimon Schubert /* Print source lines from the file of symtab S,
13015796c8dcSSimon Schubert starting with line number LINE and stopping before line number STOPLINE. */
13025796c8dcSSimon Schubert
13035796c8dcSSimon Schubert static void
print_source_lines_base(struct symtab * s,int line,int stopline,enum print_source_lines_flags flags)1304*ef5ccd6cSJohn Marino print_source_lines_base (struct symtab *s, int line, int stopline,
1305*ef5ccd6cSJohn Marino enum print_source_lines_flags flags)
13065796c8dcSSimon Schubert {
13075796c8dcSSimon Schubert int c;
13085796c8dcSSimon Schubert int desc;
1309cf7f2e2dSJohn Marino int noprint = 0;
13105796c8dcSSimon Schubert FILE *stream;
13115796c8dcSSimon Schubert int nlines = stopline - line;
13125796c8dcSSimon Schubert struct cleanup *cleanup;
1313a45ae5f8SJohn Marino struct ui_out *uiout = current_uiout;
13145796c8dcSSimon Schubert
13155796c8dcSSimon Schubert /* Regardless of whether we can open the file, set current_source_symtab. */
13165796c8dcSSimon Schubert current_source_symtab = s;
13175796c8dcSSimon Schubert current_source_line = line;
13185796c8dcSSimon Schubert first_line_listed = line;
13195796c8dcSSimon Schubert
1320c50c785cSJohn Marino /* If printing of source lines is disabled, just print file and line
1321c50c785cSJohn Marino number. */
13225796c8dcSSimon Schubert if (ui_out_test_flags (uiout, ui_source_list))
13235796c8dcSSimon Schubert {
1324c50c785cSJohn Marino /* Only prints "No such file or directory" once. */
13255796c8dcSSimon Schubert if ((s != last_source_visited) || (!last_source_error))
13265796c8dcSSimon Schubert {
13275796c8dcSSimon Schubert last_source_visited = s;
13285796c8dcSSimon Schubert desc = open_source_file (s);
13295796c8dcSSimon Schubert }
13305796c8dcSSimon Schubert else
13315796c8dcSSimon Schubert {
13325796c8dcSSimon Schubert desc = last_source_error;
1333*ef5ccd6cSJohn Marino flags |= PRINT_SOURCE_LINES_NOERROR;
13345796c8dcSSimon Schubert }
13355796c8dcSSimon Schubert }
13365796c8dcSSimon Schubert else
13375796c8dcSSimon Schubert {
1338cf7f2e2dSJohn Marino desc = last_source_error;
1339*ef5ccd6cSJohn Marino flags |= PRINT_SOURCE_LINES_NOERROR;
1340cf7f2e2dSJohn Marino noprint = 1;
13415796c8dcSSimon Schubert }
13425796c8dcSSimon Schubert
1343cf7f2e2dSJohn Marino if (desc < 0 || noprint)
13445796c8dcSSimon Schubert {
13455796c8dcSSimon Schubert last_source_error = desc;
13465796c8dcSSimon Schubert
1347*ef5ccd6cSJohn Marino if (!(flags & PRINT_SOURCE_LINES_NOERROR))
13485796c8dcSSimon Schubert {
1349*ef5ccd6cSJohn Marino const char *filename = symtab_to_filename_for_display (s);
1350*ef5ccd6cSJohn Marino int len = strlen (filename) + 100;
1351*ef5ccd6cSJohn Marino char *name = alloca (len);
1352*ef5ccd6cSJohn Marino
1353*ef5ccd6cSJohn Marino xsnprintf (name, len, "%d\t%s", line, filename);
13545796c8dcSSimon Schubert print_sys_errmsg (name, errno);
13555796c8dcSSimon Schubert }
13565796c8dcSSimon Schubert else
1357a45ae5f8SJohn Marino {
13585796c8dcSSimon Schubert ui_out_field_int (uiout, "line", line);
13595796c8dcSSimon Schubert ui_out_text (uiout, "\tin ");
1360*ef5ccd6cSJohn Marino
1361*ef5ccd6cSJohn Marino /* CLI expects only the "file" field. TUI expects only the
1362*ef5ccd6cSJohn Marino "fullname" field (and TUI does break if "file" is printed).
1363*ef5ccd6cSJohn Marino MI expects both fields. ui_source_list is set only for CLI,
1364*ef5ccd6cSJohn Marino not for TUI. */
1365*ef5ccd6cSJohn Marino if (ui_out_is_mi_like_p (uiout)
1366*ef5ccd6cSJohn Marino || ui_out_test_flags (uiout, ui_source_list))
1367*ef5ccd6cSJohn Marino ui_out_field_string (uiout, "file",
1368*ef5ccd6cSJohn Marino symtab_to_filename_for_display (s));
1369*ef5ccd6cSJohn Marino if (ui_out_is_mi_like_p (uiout)
1370*ef5ccd6cSJohn Marino || !ui_out_test_flags (uiout, ui_source_list))
1371*ef5ccd6cSJohn Marino {
1372*ef5ccd6cSJohn Marino const char *s_fullname = symtab_to_fullname (s);
1373*ef5ccd6cSJohn Marino char *local_fullname;
1374*ef5ccd6cSJohn Marino
1375*ef5ccd6cSJohn Marino /* ui_out_field_string may free S_FULLNAME by calling
1376*ef5ccd6cSJohn Marino open_source_file for it again. See e.g.,
1377*ef5ccd6cSJohn Marino tui_field_string->tui_show_source. */
1378*ef5ccd6cSJohn Marino local_fullname = alloca (strlen (s_fullname) + 1);
1379*ef5ccd6cSJohn Marino strcpy (local_fullname, s_fullname);
1380*ef5ccd6cSJohn Marino
1381*ef5ccd6cSJohn Marino ui_out_field_string (uiout, "fullname", local_fullname);
1382*ef5ccd6cSJohn Marino }
1383*ef5ccd6cSJohn Marino
13845796c8dcSSimon Schubert ui_out_text (uiout, "\n");
1385a45ae5f8SJohn Marino }
13865796c8dcSSimon Schubert
13875796c8dcSSimon Schubert return;
13885796c8dcSSimon Schubert }
13895796c8dcSSimon Schubert
13905796c8dcSSimon Schubert last_source_error = 0;
13915796c8dcSSimon Schubert
13925796c8dcSSimon Schubert if (s->line_charpos == 0)
13935796c8dcSSimon Schubert find_source_lines (s, desc);
13945796c8dcSSimon Schubert
13955796c8dcSSimon Schubert if (line < 1 || line > s->nlines)
13965796c8dcSSimon Schubert {
13975796c8dcSSimon Schubert close (desc);
13985796c8dcSSimon Schubert error (_("Line number %d out of range; %s has %d lines."),
1399*ef5ccd6cSJohn Marino line, symtab_to_filename_for_display (s), s->nlines);
14005796c8dcSSimon Schubert }
14015796c8dcSSimon Schubert
14025796c8dcSSimon Schubert if (lseek (desc, s->line_charpos[line - 1], 0) < 0)
14035796c8dcSSimon Schubert {
14045796c8dcSSimon Schubert close (desc);
1405*ef5ccd6cSJohn Marino perror_with_name (symtab_to_filename_for_display (s));
14065796c8dcSSimon Schubert }
14075796c8dcSSimon Schubert
14085796c8dcSSimon Schubert stream = fdopen (desc, FDOPEN_MODE);
14095796c8dcSSimon Schubert clearerr (stream);
14105796c8dcSSimon Schubert cleanup = make_cleanup_fclose (stream);
14115796c8dcSSimon Schubert
14125796c8dcSSimon Schubert while (nlines-- > 0)
14135796c8dcSSimon Schubert {
14145796c8dcSSimon Schubert char buf[20];
14155796c8dcSSimon Schubert
14165796c8dcSSimon Schubert c = fgetc (stream);
14175796c8dcSSimon Schubert if (c == EOF)
14185796c8dcSSimon Schubert break;
14195796c8dcSSimon Schubert last_line_listed = current_source_line;
1420*ef5ccd6cSJohn Marino if (flags & PRINT_SOURCE_LINES_FILENAME)
1421*ef5ccd6cSJohn Marino {
1422*ef5ccd6cSJohn Marino ui_out_text (uiout, symtab_to_filename_for_display (s));
1423*ef5ccd6cSJohn Marino ui_out_text (uiout, ":");
1424*ef5ccd6cSJohn Marino }
1425*ef5ccd6cSJohn Marino xsnprintf (buf, sizeof (buf), "%d\t", current_source_line++);
14265796c8dcSSimon Schubert ui_out_text (uiout, buf);
14275796c8dcSSimon Schubert do
14285796c8dcSSimon Schubert {
14295796c8dcSSimon Schubert if (c < 040 && c != '\t' && c != '\n' && c != '\r')
14305796c8dcSSimon Schubert {
1431*ef5ccd6cSJohn Marino xsnprintf (buf, sizeof (buf), "^%c", c + 0100);
14325796c8dcSSimon Schubert ui_out_text (uiout, buf);
14335796c8dcSSimon Schubert }
14345796c8dcSSimon Schubert else if (c == 0177)
14355796c8dcSSimon Schubert ui_out_text (uiout, "^?");
14365796c8dcSSimon Schubert else if (c == '\r')
14375796c8dcSSimon Schubert {
14385796c8dcSSimon Schubert /* Skip a \r character, but only before a \n. */
14395796c8dcSSimon Schubert int c1 = fgetc (stream);
14405796c8dcSSimon Schubert
14415796c8dcSSimon Schubert if (c1 != '\n')
14425796c8dcSSimon Schubert printf_filtered ("^%c", c + 0100);
14435796c8dcSSimon Schubert if (c1 != EOF)
14445796c8dcSSimon Schubert ungetc (c1, stream);
14455796c8dcSSimon Schubert }
14465796c8dcSSimon Schubert else
14475796c8dcSSimon Schubert {
1448*ef5ccd6cSJohn Marino xsnprintf (buf, sizeof (buf), "%c", c);
14495796c8dcSSimon Schubert ui_out_text (uiout, buf);
14505796c8dcSSimon Schubert }
14515796c8dcSSimon Schubert }
14525796c8dcSSimon Schubert while (c != '\n' && (c = fgetc (stream)) >= 0);
14535796c8dcSSimon Schubert }
14545796c8dcSSimon Schubert
14555796c8dcSSimon Schubert do_cleanups (cleanup);
14565796c8dcSSimon Schubert }
14575796c8dcSSimon Schubert
14585796c8dcSSimon Schubert /* Show source lines from the file of symtab S, starting with line
14595796c8dcSSimon Schubert number LINE and stopping before line number STOPLINE. If this is
14605796c8dcSSimon Schubert not the command line version, then the source is shown in the source
1461c50c785cSJohn Marino window otherwise it is simply printed. */
14625796c8dcSSimon Schubert
14635796c8dcSSimon Schubert void
print_source_lines(struct symtab * s,int line,int stopline,enum print_source_lines_flags flags)1464*ef5ccd6cSJohn Marino print_source_lines (struct symtab *s, int line, int stopline,
1465*ef5ccd6cSJohn Marino enum print_source_lines_flags flags)
14665796c8dcSSimon Schubert {
1467*ef5ccd6cSJohn Marino print_source_lines_base (s, line, stopline, flags);
14685796c8dcSSimon Schubert }
14695796c8dcSSimon Schubert
14705796c8dcSSimon Schubert /* Print info on range of pc's in a specified line. */
14715796c8dcSSimon Schubert
14725796c8dcSSimon Schubert static void
line_info(char * arg,int from_tty)14735796c8dcSSimon Schubert line_info (char *arg, int from_tty)
14745796c8dcSSimon Schubert {
14755796c8dcSSimon Schubert struct symtabs_and_lines sals;
14765796c8dcSSimon Schubert struct symtab_and_line sal;
14775796c8dcSSimon Schubert CORE_ADDR start_pc, end_pc;
14785796c8dcSSimon Schubert int i;
1479a45ae5f8SJohn Marino struct cleanup *cleanups;
14805796c8dcSSimon Schubert
14815796c8dcSSimon Schubert init_sal (&sal); /* initialize to zeroes */
14825796c8dcSSimon Schubert
14835796c8dcSSimon Schubert if (arg == 0)
14845796c8dcSSimon Schubert {
14855796c8dcSSimon Schubert sal.symtab = current_source_symtab;
1486a45ae5f8SJohn Marino sal.pspace = current_program_space;
14875796c8dcSSimon Schubert sal.line = last_line_listed;
14885796c8dcSSimon Schubert sals.nelts = 1;
14895796c8dcSSimon Schubert sals.sals = (struct symtab_and_line *)
14905796c8dcSSimon Schubert xmalloc (sizeof (struct symtab_and_line));
14915796c8dcSSimon Schubert sals.sals[0] = sal;
14925796c8dcSSimon Schubert }
14935796c8dcSSimon Schubert else
14945796c8dcSSimon Schubert {
1495*ef5ccd6cSJohn Marino sals = decode_line_with_last_displayed (arg, DECODE_LINE_LIST_MODE);
14965796c8dcSSimon Schubert
14975796c8dcSSimon Schubert dont_repeat ();
14985796c8dcSSimon Schubert }
14995796c8dcSSimon Schubert
1500a45ae5f8SJohn Marino cleanups = make_cleanup (xfree, sals.sals);
1501a45ae5f8SJohn Marino
15025796c8dcSSimon Schubert /* C++ More than one line may have been specified, as when the user
15035796c8dcSSimon Schubert specifies an overloaded function name. Print info on them all. */
15045796c8dcSSimon Schubert for (i = 0; i < sals.nelts; i++)
15055796c8dcSSimon Schubert {
15065796c8dcSSimon Schubert sal = sals.sals[i];
1507a45ae5f8SJohn Marino if (sal.pspace != current_program_space)
1508a45ae5f8SJohn Marino continue;
15095796c8dcSSimon Schubert
15105796c8dcSSimon Schubert if (sal.symtab == 0)
15115796c8dcSSimon Schubert {
15125796c8dcSSimon Schubert struct gdbarch *gdbarch = get_current_arch ();
15135796c8dcSSimon Schubert
15145796c8dcSSimon Schubert printf_filtered (_("No line number information available"));
15155796c8dcSSimon Schubert if (sal.pc != 0)
15165796c8dcSSimon Schubert {
15175796c8dcSSimon Schubert /* This is useful for "info line *0x7f34". If we can't tell the
15185796c8dcSSimon Schubert user about a source line, at least let them have the symbolic
15195796c8dcSSimon Schubert address. */
15205796c8dcSSimon Schubert printf_filtered (" for address ");
15215796c8dcSSimon Schubert wrap_here (" ");
15225796c8dcSSimon Schubert print_address (gdbarch, sal.pc, gdb_stdout);
15235796c8dcSSimon Schubert }
15245796c8dcSSimon Schubert else
15255796c8dcSSimon Schubert printf_filtered (".");
15265796c8dcSSimon Schubert printf_filtered ("\n");
15275796c8dcSSimon Schubert }
15285796c8dcSSimon Schubert else if (sal.line > 0
15295796c8dcSSimon Schubert && find_line_pc_range (sal, &start_pc, &end_pc))
15305796c8dcSSimon Schubert {
15315796c8dcSSimon Schubert struct gdbarch *gdbarch = get_objfile_arch (sal.symtab->objfile);
15325796c8dcSSimon Schubert
15335796c8dcSSimon Schubert if (start_pc == end_pc)
15345796c8dcSSimon Schubert {
15355796c8dcSSimon Schubert printf_filtered ("Line %d of \"%s\"",
1536*ef5ccd6cSJohn Marino sal.line,
1537*ef5ccd6cSJohn Marino symtab_to_filename_for_display (sal.symtab));
15385796c8dcSSimon Schubert wrap_here (" ");
15395796c8dcSSimon Schubert printf_filtered (" is at address ");
15405796c8dcSSimon Schubert print_address (gdbarch, start_pc, gdb_stdout);
15415796c8dcSSimon Schubert wrap_here (" ");
15425796c8dcSSimon Schubert printf_filtered (" but contains no code.\n");
15435796c8dcSSimon Schubert }
15445796c8dcSSimon Schubert else
15455796c8dcSSimon Schubert {
15465796c8dcSSimon Schubert printf_filtered ("Line %d of \"%s\"",
1547*ef5ccd6cSJohn Marino sal.line,
1548*ef5ccd6cSJohn Marino symtab_to_filename_for_display (sal.symtab));
15495796c8dcSSimon Schubert wrap_here (" ");
15505796c8dcSSimon Schubert printf_filtered (" starts at address ");
15515796c8dcSSimon Schubert print_address (gdbarch, start_pc, gdb_stdout);
15525796c8dcSSimon Schubert wrap_here (" ");
15535796c8dcSSimon Schubert printf_filtered (" and ends at ");
15545796c8dcSSimon Schubert print_address (gdbarch, end_pc, gdb_stdout);
15555796c8dcSSimon Schubert printf_filtered (".\n");
15565796c8dcSSimon Schubert }
15575796c8dcSSimon Schubert
15585796c8dcSSimon Schubert /* x/i should display this line's code. */
15595796c8dcSSimon Schubert set_next_address (gdbarch, start_pc);
15605796c8dcSSimon Schubert
15615796c8dcSSimon Schubert /* Repeating "info line" should do the following line. */
15625796c8dcSSimon Schubert last_line_listed = sal.line + 1;
15635796c8dcSSimon Schubert
15645796c8dcSSimon Schubert /* If this is the only line, show the source code. If it could
15655796c8dcSSimon Schubert not find the file, don't do anything special. */
15665796c8dcSSimon Schubert if (annotation_level && sals.nelts == 1)
15675796c8dcSSimon Schubert identify_source_line (sal.symtab, sal.line, 0, start_pc);
15685796c8dcSSimon Schubert }
15695796c8dcSSimon Schubert else
15705796c8dcSSimon Schubert /* Is there any case in which we get here, and have an address
15715796c8dcSSimon Schubert which the user would want to see? If we have debugging symbols
15725796c8dcSSimon Schubert and no line numbers? */
15735796c8dcSSimon Schubert printf_filtered (_("Line number %d is out of range for \"%s\".\n"),
1574*ef5ccd6cSJohn Marino sal.line, symtab_to_filename_for_display (sal.symtab));
15755796c8dcSSimon Schubert }
1576a45ae5f8SJohn Marino do_cleanups (cleanups);
15775796c8dcSSimon Schubert }
15785796c8dcSSimon Schubert
15795796c8dcSSimon Schubert /* Commands to search the source file for a regexp. */
15805796c8dcSSimon Schubert
15815796c8dcSSimon Schubert static void
forward_search_command(char * regex,int from_tty)15825796c8dcSSimon Schubert forward_search_command (char *regex, int from_tty)
15835796c8dcSSimon Schubert {
15845796c8dcSSimon Schubert int c;
15855796c8dcSSimon Schubert int desc;
15865796c8dcSSimon Schubert FILE *stream;
15875796c8dcSSimon Schubert int line;
15885796c8dcSSimon Schubert char *msg;
15895796c8dcSSimon Schubert struct cleanup *cleanups;
15905796c8dcSSimon Schubert
15915796c8dcSSimon Schubert line = last_line_listed + 1;
15925796c8dcSSimon Schubert
15935796c8dcSSimon Schubert msg = (char *) re_comp (regex);
15945796c8dcSSimon Schubert if (msg)
15955796c8dcSSimon Schubert error (("%s"), msg);
15965796c8dcSSimon Schubert
15975796c8dcSSimon Schubert if (current_source_symtab == 0)
15985796c8dcSSimon Schubert select_source_symtab (0);
15995796c8dcSSimon Schubert
16005796c8dcSSimon Schubert desc = open_source_file (current_source_symtab);
16015796c8dcSSimon Schubert if (desc < 0)
1602*ef5ccd6cSJohn Marino perror_with_name (symtab_to_filename_for_display (current_source_symtab));
16035796c8dcSSimon Schubert cleanups = make_cleanup_close (desc);
16045796c8dcSSimon Schubert
16055796c8dcSSimon Schubert if (current_source_symtab->line_charpos == 0)
16065796c8dcSSimon Schubert find_source_lines (current_source_symtab, desc);
16075796c8dcSSimon Schubert
16085796c8dcSSimon Schubert if (line < 1 || line > current_source_symtab->nlines)
16095796c8dcSSimon Schubert error (_("Expression not found"));
16105796c8dcSSimon Schubert
16115796c8dcSSimon Schubert if (lseek (desc, current_source_symtab->line_charpos[line - 1], 0) < 0)
1612*ef5ccd6cSJohn Marino perror_with_name (symtab_to_filename_for_display (current_source_symtab));
16135796c8dcSSimon Schubert
16145796c8dcSSimon Schubert discard_cleanups (cleanups);
16155796c8dcSSimon Schubert stream = fdopen (desc, FDOPEN_MODE);
16165796c8dcSSimon Schubert clearerr (stream);
16175796c8dcSSimon Schubert cleanups = make_cleanup_fclose (stream);
16185796c8dcSSimon Schubert while (1)
16195796c8dcSSimon Schubert {
16205796c8dcSSimon Schubert static char *buf = NULL;
16215796c8dcSSimon Schubert char *p;
16225796c8dcSSimon Schubert int cursize, newsize;
16235796c8dcSSimon Schubert
16245796c8dcSSimon Schubert cursize = 256;
16255796c8dcSSimon Schubert buf = xmalloc (cursize);
16265796c8dcSSimon Schubert p = buf;
16275796c8dcSSimon Schubert
16285796c8dcSSimon Schubert c = getc (stream);
16295796c8dcSSimon Schubert if (c == EOF)
16305796c8dcSSimon Schubert break;
16315796c8dcSSimon Schubert do
16325796c8dcSSimon Schubert {
16335796c8dcSSimon Schubert *p++ = c;
16345796c8dcSSimon Schubert if (p - buf == cursize)
16355796c8dcSSimon Schubert {
16365796c8dcSSimon Schubert newsize = cursize + cursize / 2;
16375796c8dcSSimon Schubert buf = xrealloc (buf, newsize);
16385796c8dcSSimon Schubert p = buf + cursize;
16395796c8dcSSimon Schubert cursize = newsize;
16405796c8dcSSimon Schubert }
16415796c8dcSSimon Schubert }
16425796c8dcSSimon Schubert while (c != '\n' && (c = getc (stream)) >= 0);
16435796c8dcSSimon Schubert
16445796c8dcSSimon Schubert /* Remove the \r, if any, at the end of the line, otherwise
16455796c8dcSSimon Schubert regular expressions that end with $ or \n won't work. */
16465796c8dcSSimon Schubert if (p - buf > 1 && p[-2] == '\r')
16475796c8dcSSimon Schubert {
16485796c8dcSSimon Schubert p--;
16495796c8dcSSimon Schubert p[-1] = '\n';
16505796c8dcSSimon Schubert }
16515796c8dcSSimon Schubert
1652c50c785cSJohn Marino /* We now have a source line in buf, null terminate and match. */
16535796c8dcSSimon Schubert *p = 0;
16545796c8dcSSimon Schubert if (re_exec (buf) > 0)
16555796c8dcSSimon Schubert {
16565796c8dcSSimon Schubert /* Match! */
16575796c8dcSSimon Schubert do_cleanups (cleanups);
16585796c8dcSSimon Schubert print_source_lines (current_source_symtab, line, line + 1, 0);
16595796c8dcSSimon Schubert set_internalvar_integer (lookup_internalvar ("_"), line);
16605796c8dcSSimon Schubert current_source_line = max (line - lines_to_list / 2, 1);
16615796c8dcSSimon Schubert return;
16625796c8dcSSimon Schubert }
16635796c8dcSSimon Schubert line++;
16645796c8dcSSimon Schubert }
16655796c8dcSSimon Schubert
16665796c8dcSSimon Schubert printf_filtered (_("Expression not found\n"));
16675796c8dcSSimon Schubert do_cleanups (cleanups);
16685796c8dcSSimon Schubert }
16695796c8dcSSimon Schubert
16705796c8dcSSimon Schubert static void
reverse_search_command(char * regex,int from_tty)16715796c8dcSSimon Schubert reverse_search_command (char *regex, int from_tty)
16725796c8dcSSimon Schubert {
16735796c8dcSSimon Schubert int c;
16745796c8dcSSimon Schubert int desc;
16755796c8dcSSimon Schubert FILE *stream;
16765796c8dcSSimon Schubert int line;
16775796c8dcSSimon Schubert char *msg;
16785796c8dcSSimon Schubert struct cleanup *cleanups;
16795796c8dcSSimon Schubert
16805796c8dcSSimon Schubert line = last_line_listed - 1;
16815796c8dcSSimon Schubert
16825796c8dcSSimon Schubert msg = (char *) re_comp (regex);
16835796c8dcSSimon Schubert if (msg)
16845796c8dcSSimon Schubert error (("%s"), msg);
16855796c8dcSSimon Schubert
16865796c8dcSSimon Schubert if (current_source_symtab == 0)
16875796c8dcSSimon Schubert select_source_symtab (0);
16885796c8dcSSimon Schubert
16895796c8dcSSimon Schubert desc = open_source_file (current_source_symtab);
16905796c8dcSSimon Schubert if (desc < 0)
1691*ef5ccd6cSJohn Marino perror_with_name (symtab_to_filename_for_display (current_source_symtab));
16925796c8dcSSimon Schubert cleanups = make_cleanup_close (desc);
16935796c8dcSSimon Schubert
16945796c8dcSSimon Schubert if (current_source_symtab->line_charpos == 0)
16955796c8dcSSimon Schubert find_source_lines (current_source_symtab, desc);
16965796c8dcSSimon Schubert
16975796c8dcSSimon Schubert if (line < 1 || line > current_source_symtab->nlines)
16985796c8dcSSimon Schubert error (_("Expression not found"));
16995796c8dcSSimon Schubert
17005796c8dcSSimon Schubert if (lseek (desc, current_source_symtab->line_charpos[line - 1], 0) < 0)
1701*ef5ccd6cSJohn Marino perror_with_name (symtab_to_filename_for_display (current_source_symtab));
17025796c8dcSSimon Schubert
17035796c8dcSSimon Schubert discard_cleanups (cleanups);
17045796c8dcSSimon Schubert stream = fdopen (desc, FDOPEN_MODE);
17055796c8dcSSimon Schubert clearerr (stream);
17065796c8dcSSimon Schubert cleanups = make_cleanup_fclose (stream);
17075796c8dcSSimon Schubert while (line > 1)
17085796c8dcSSimon Schubert {
17095796c8dcSSimon Schubert /* FIXME!!! We walk right off the end of buf if we get a long line!!! */
17105796c8dcSSimon Schubert char buf[4096]; /* Should be reasonable??? */
17115796c8dcSSimon Schubert char *p = buf;
17125796c8dcSSimon Schubert
17135796c8dcSSimon Schubert c = getc (stream);
17145796c8dcSSimon Schubert if (c == EOF)
17155796c8dcSSimon Schubert break;
17165796c8dcSSimon Schubert do
17175796c8dcSSimon Schubert {
17185796c8dcSSimon Schubert *p++ = c;
17195796c8dcSSimon Schubert }
17205796c8dcSSimon Schubert while (c != '\n' && (c = getc (stream)) >= 0);
17215796c8dcSSimon Schubert
17225796c8dcSSimon Schubert /* Remove the \r, if any, at the end of the line, otherwise
17235796c8dcSSimon Schubert regular expressions that end with $ or \n won't work. */
17245796c8dcSSimon Schubert if (p - buf > 1 && p[-2] == '\r')
17255796c8dcSSimon Schubert {
17265796c8dcSSimon Schubert p--;
17275796c8dcSSimon Schubert p[-1] = '\n';
17285796c8dcSSimon Schubert }
17295796c8dcSSimon Schubert
17305796c8dcSSimon Schubert /* We now have a source line in buf; null terminate and match. */
17315796c8dcSSimon Schubert *p = 0;
17325796c8dcSSimon Schubert if (re_exec (buf) > 0)
17335796c8dcSSimon Schubert {
17345796c8dcSSimon Schubert /* Match! */
17355796c8dcSSimon Schubert do_cleanups (cleanups);
17365796c8dcSSimon Schubert print_source_lines (current_source_symtab, line, line + 1, 0);
17375796c8dcSSimon Schubert set_internalvar_integer (lookup_internalvar ("_"), line);
17385796c8dcSSimon Schubert current_source_line = max (line - lines_to_list / 2, 1);
17395796c8dcSSimon Schubert return;
17405796c8dcSSimon Schubert }
17415796c8dcSSimon Schubert line--;
17425796c8dcSSimon Schubert if (fseek (stream, current_source_symtab->line_charpos[line - 1], 0) < 0)
17435796c8dcSSimon Schubert {
1744*ef5ccd6cSJohn Marino const char *filename;
1745*ef5ccd6cSJohn Marino
17465796c8dcSSimon Schubert do_cleanups (cleanups);
1747*ef5ccd6cSJohn Marino filename = symtab_to_filename_for_display (current_source_symtab);
1748*ef5ccd6cSJohn Marino perror_with_name (filename);
17495796c8dcSSimon Schubert }
17505796c8dcSSimon Schubert }
17515796c8dcSSimon Schubert
17525796c8dcSSimon Schubert printf_filtered (_("Expression not found\n"));
17535796c8dcSSimon Schubert do_cleanups (cleanups);
17545796c8dcSSimon Schubert return;
17555796c8dcSSimon Schubert }
17565796c8dcSSimon Schubert
17575796c8dcSSimon Schubert /* If the last character of PATH is a directory separator, then strip it. */
17585796c8dcSSimon Schubert
17595796c8dcSSimon Schubert static void
strip_trailing_directory_separator(char * path)17605796c8dcSSimon Schubert strip_trailing_directory_separator (char *path)
17615796c8dcSSimon Schubert {
17625796c8dcSSimon Schubert const int last = strlen (path) - 1;
17635796c8dcSSimon Schubert
17645796c8dcSSimon Schubert if (last < 0)
17655796c8dcSSimon Schubert return; /* No stripping is needed if PATH is the empty string. */
17665796c8dcSSimon Schubert
17675796c8dcSSimon Schubert if (IS_DIR_SEPARATOR (path[last]))
17685796c8dcSSimon Schubert path[last] = '\0';
17695796c8dcSSimon Schubert }
17705796c8dcSSimon Schubert
17715796c8dcSSimon Schubert /* Return the path substitution rule that matches FROM.
17725796c8dcSSimon Schubert Return NULL if no rule matches. */
17735796c8dcSSimon Schubert
17745796c8dcSSimon Schubert static struct substitute_path_rule *
find_substitute_path_rule(const char * from)17755796c8dcSSimon Schubert find_substitute_path_rule (const char *from)
17765796c8dcSSimon Schubert {
17775796c8dcSSimon Schubert struct substitute_path_rule *rule = substitute_path_rules;
17785796c8dcSSimon Schubert
17795796c8dcSSimon Schubert while (rule != NULL)
17805796c8dcSSimon Schubert {
17815796c8dcSSimon Schubert if (FILENAME_CMP (rule->from, from) == 0)
17825796c8dcSSimon Schubert return rule;
17835796c8dcSSimon Schubert rule = rule->next;
17845796c8dcSSimon Schubert }
17855796c8dcSSimon Schubert
17865796c8dcSSimon Schubert return NULL;
17875796c8dcSSimon Schubert }
17885796c8dcSSimon Schubert
17895796c8dcSSimon Schubert /* Add a new substitute-path rule at the end of the current list of rules.
17905796c8dcSSimon Schubert The new rule will replace FROM into TO. */
17915796c8dcSSimon Schubert
17925796c8dcSSimon Schubert void
add_substitute_path_rule(char * from,char * to)17935796c8dcSSimon Schubert add_substitute_path_rule (char *from, char *to)
17945796c8dcSSimon Schubert {
17955796c8dcSSimon Schubert struct substitute_path_rule *rule;
17965796c8dcSSimon Schubert struct substitute_path_rule *new_rule;
17975796c8dcSSimon Schubert
17985796c8dcSSimon Schubert new_rule = xmalloc (sizeof (struct substitute_path_rule));
17995796c8dcSSimon Schubert new_rule->from = xstrdup (from);
18005796c8dcSSimon Schubert new_rule->to = xstrdup (to);
18015796c8dcSSimon Schubert new_rule->next = NULL;
18025796c8dcSSimon Schubert
18035796c8dcSSimon Schubert /* If the list of rules are empty, then insert the new rule
18045796c8dcSSimon Schubert at the head of the list. */
18055796c8dcSSimon Schubert
18065796c8dcSSimon Schubert if (substitute_path_rules == NULL)
18075796c8dcSSimon Schubert {
18085796c8dcSSimon Schubert substitute_path_rules = new_rule;
18095796c8dcSSimon Schubert return;
18105796c8dcSSimon Schubert }
18115796c8dcSSimon Schubert
18125796c8dcSSimon Schubert /* Otherwise, skip to the last rule in our list and then append
18135796c8dcSSimon Schubert the new rule. */
18145796c8dcSSimon Schubert
18155796c8dcSSimon Schubert rule = substitute_path_rules;
18165796c8dcSSimon Schubert while (rule->next != NULL)
18175796c8dcSSimon Schubert rule = rule->next;
18185796c8dcSSimon Schubert
18195796c8dcSSimon Schubert rule->next = new_rule;
18205796c8dcSSimon Schubert }
18215796c8dcSSimon Schubert
18225796c8dcSSimon Schubert /* Remove the given source path substitution rule from the current list
18235796c8dcSSimon Schubert of rules. The memory allocated for that rule is also deallocated. */
18245796c8dcSSimon Schubert
18255796c8dcSSimon Schubert static void
delete_substitute_path_rule(struct substitute_path_rule * rule)18265796c8dcSSimon Schubert delete_substitute_path_rule (struct substitute_path_rule *rule)
18275796c8dcSSimon Schubert {
18285796c8dcSSimon Schubert if (rule == substitute_path_rules)
18295796c8dcSSimon Schubert substitute_path_rules = rule->next;
18305796c8dcSSimon Schubert else
18315796c8dcSSimon Schubert {
18325796c8dcSSimon Schubert struct substitute_path_rule *prev = substitute_path_rules;
18335796c8dcSSimon Schubert
18345796c8dcSSimon Schubert while (prev != NULL && prev->next != rule)
18355796c8dcSSimon Schubert prev = prev->next;
18365796c8dcSSimon Schubert
18375796c8dcSSimon Schubert gdb_assert (prev != NULL);
18385796c8dcSSimon Schubert
18395796c8dcSSimon Schubert prev->next = rule->next;
18405796c8dcSSimon Schubert }
18415796c8dcSSimon Schubert
18425796c8dcSSimon Schubert xfree (rule->from);
18435796c8dcSSimon Schubert xfree (rule->to);
18445796c8dcSSimon Schubert xfree (rule);
18455796c8dcSSimon Schubert }
18465796c8dcSSimon Schubert
18475796c8dcSSimon Schubert /* Implement the "show substitute-path" command. */
18485796c8dcSSimon Schubert
18495796c8dcSSimon Schubert static void
show_substitute_path_command(char * args,int from_tty)18505796c8dcSSimon Schubert show_substitute_path_command (char *args, int from_tty)
18515796c8dcSSimon Schubert {
18525796c8dcSSimon Schubert struct substitute_path_rule *rule = substitute_path_rules;
18535796c8dcSSimon Schubert char **argv;
18545796c8dcSSimon Schubert char *from = NULL;
18555796c8dcSSimon Schubert
18565796c8dcSSimon Schubert argv = gdb_buildargv (args);
18575796c8dcSSimon Schubert make_cleanup_freeargv (argv);
18585796c8dcSSimon Schubert
18595796c8dcSSimon Schubert /* We expect zero or one argument. */
18605796c8dcSSimon Schubert
18615796c8dcSSimon Schubert if (argv != NULL && argv[0] != NULL && argv[1] != NULL)
18625796c8dcSSimon Schubert error (_("Too many arguments in command"));
18635796c8dcSSimon Schubert
18645796c8dcSSimon Schubert if (argv != NULL && argv[0] != NULL)
18655796c8dcSSimon Schubert from = argv[0];
18665796c8dcSSimon Schubert
18675796c8dcSSimon Schubert /* Print the substitution rules. */
18685796c8dcSSimon Schubert
18695796c8dcSSimon Schubert if (from != NULL)
18705796c8dcSSimon Schubert printf_filtered
18715796c8dcSSimon Schubert (_("Source path substitution rule matching `%s':\n"), from);
18725796c8dcSSimon Schubert else
18735796c8dcSSimon Schubert printf_filtered (_("List of all source path substitution rules:\n"));
18745796c8dcSSimon Schubert
18755796c8dcSSimon Schubert while (rule != NULL)
18765796c8dcSSimon Schubert {
18775796c8dcSSimon Schubert if (from == NULL || FILENAME_CMP (rule->from, from) == 0)
18785796c8dcSSimon Schubert printf_filtered (" `%s' -> `%s'.\n", rule->from, rule->to);
18795796c8dcSSimon Schubert rule = rule->next;
18805796c8dcSSimon Schubert }
18815796c8dcSSimon Schubert }
18825796c8dcSSimon Schubert
18835796c8dcSSimon Schubert /* Implement the "unset substitute-path" command. */
18845796c8dcSSimon Schubert
18855796c8dcSSimon Schubert static void
unset_substitute_path_command(char * args,int from_tty)18865796c8dcSSimon Schubert unset_substitute_path_command (char *args, int from_tty)
18875796c8dcSSimon Schubert {
18885796c8dcSSimon Schubert struct substitute_path_rule *rule = substitute_path_rules;
18895796c8dcSSimon Schubert char **argv = gdb_buildargv (args);
18905796c8dcSSimon Schubert char *from = NULL;
18915796c8dcSSimon Schubert int rule_found = 0;
18925796c8dcSSimon Schubert
18935796c8dcSSimon Schubert /* This function takes either 0 or 1 argument. */
18945796c8dcSSimon Schubert
18955796c8dcSSimon Schubert make_cleanup_freeargv (argv);
18965796c8dcSSimon Schubert if (argv != NULL && argv[0] != NULL && argv[1] != NULL)
18975796c8dcSSimon Schubert error (_("Incorrect usage, too many arguments in command"));
18985796c8dcSSimon Schubert
18995796c8dcSSimon Schubert if (argv != NULL && argv[0] != NULL)
19005796c8dcSSimon Schubert from = argv[0];
19015796c8dcSSimon Schubert
19025796c8dcSSimon Schubert /* If the user asked for all the rules to be deleted, ask him
19035796c8dcSSimon Schubert to confirm and give him a chance to abort before the action
19045796c8dcSSimon Schubert is performed. */
19055796c8dcSSimon Schubert
19065796c8dcSSimon Schubert if (from == NULL
19075796c8dcSSimon Schubert && !query (_("Delete all source path substitution rules? ")))
19085796c8dcSSimon Schubert error (_("Canceled"));
19095796c8dcSSimon Schubert
19105796c8dcSSimon Schubert /* Delete the rule matching the argument. No argument means that
19115796c8dcSSimon Schubert all rules should be deleted. */
19125796c8dcSSimon Schubert
19135796c8dcSSimon Schubert while (rule != NULL)
19145796c8dcSSimon Schubert {
19155796c8dcSSimon Schubert struct substitute_path_rule *next = rule->next;
19165796c8dcSSimon Schubert
19175796c8dcSSimon Schubert if (from == NULL || FILENAME_CMP (from, rule->from) == 0)
19185796c8dcSSimon Schubert {
19195796c8dcSSimon Schubert delete_substitute_path_rule (rule);
19205796c8dcSSimon Schubert rule_found = 1;
19215796c8dcSSimon Schubert }
19225796c8dcSSimon Schubert
19235796c8dcSSimon Schubert rule = next;
19245796c8dcSSimon Schubert }
19255796c8dcSSimon Schubert
19265796c8dcSSimon Schubert /* If the user asked for a specific rule to be deleted but
19275796c8dcSSimon Schubert we could not find it, then report an error. */
19285796c8dcSSimon Schubert
19295796c8dcSSimon Schubert if (from != NULL && !rule_found)
19305796c8dcSSimon Schubert error (_("No substitution rule defined for `%s'"), from);
19315796c8dcSSimon Schubert
19325796c8dcSSimon Schubert forget_cached_source_info ();
19335796c8dcSSimon Schubert }
19345796c8dcSSimon Schubert
19355796c8dcSSimon Schubert /* Add a new source path substitution rule. */
19365796c8dcSSimon Schubert
19375796c8dcSSimon Schubert static void
set_substitute_path_command(char * args,int from_tty)19385796c8dcSSimon Schubert set_substitute_path_command (char *args, int from_tty)
19395796c8dcSSimon Schubert {
19405796c8dcSSimon Schubert char **argv;
19415796c8dcSSimon Schubert struct substitute_path_rule *rule;
19425796c8dcSSimon Schubert
19435796c8dcSSimon Schubert argv = gdb_buildargv (args);
19445796c8dcSSimon Schubert make_cleanup_freeargv (argv);
19455796c8dcSSimon Schubert
19465796c8dcSSimon Schubert if (argv == NULL || argv[0] == NULL || argv [1] == NULL)
19475796c8dcSSimon Schubert error (_("Incorrect usage, too few arguments in command"));
19485796c8dcSSimon Schubert
19495796c8dcSSimon Schubert if (argv[2] != NULL)
19505796c8dcSSimon Schubert error (_("Incorrect usage, too many arguments in command"));
19515796c8dcSSimon Schubert
19525796c8dcSSimon Schubert if (*(argv[0]) == '\0')
19535796c8dcSSimon Schubert error (_("First argument must be at least one character long"));
19545796c8dcSSimon Schubert
19555796c8dcSSimon Schubert /* Strip any trailing directory separator character in either FROM
19565796c8dcSSimon Schubert or TO. The substitution rule already implicitly contains them. */
19575796c8dcSSimon Schubert strip_trailing_directory_separator (argv[0]);
19585796c8dcSSimon Schubert strip_trailing_directory_separator (argv[1]);
19595796c8dcSSimon Schubert
19605796c8dcSSimon Schubert /* If a rule with the same "from" was previously defined, then
19615796c8dcSSimon Schubert delete it. This new rule replaces it. */
19625796c8dcSSimon Schubert
19635796c8dcSSimon Schubert rule = find_substitute_path_rule (argv[0]);
19645796c8dcSSimon Schubert if (rule != NULL)
19655796c8dcSSimon Schubert delete_substitute_path_rule (rule);
19665796c8dcSSimon Schubert
19675796c8dcSSimon Schubert /* Insert the new substitution rule. */
19685796c8dcSSimon Schubert
19695796c8dcSSimon Schubert add_substitute_path_rule (argv[0], argv[1]);
19705796c8dcSSimon Schubert forget_cached_source_info ();
19715796c8dcSSimon Schubert }
19725796c8dcSSimon Schubert
19735796c8dcSSimon Schubert
19745796c8dcSSimon Schubert void
_initialize_source(void)19755796c8dcSSimon Schubert _initialize_source (void)
19765796c8dcSSimon Schubert {
19775796c8dcSSimon Schubert struct cmd_list_element *c;
1978cf7f2e2dSJohn Marino
19795796c8dcSSimon Schubert current_source_symtab = 0;
19805796c8dcSSimon Schubert init_source_path ();
19815796c8dcSSimon Schubert
19825796c8dcSSimon Schubert /* The intention is to use POSIX Basic Regular Expressions.
19835796c8dcSSimon Schubert Always use the GNU regex routine for consistency across all hosts.
19845796c8dcSSimon Schubert Our current GNU regex.c does not have all the POSIX features, so this is
19855796c8dcSSimon Schubert just an approximation. */
19865796c8dcSSimon Schubert re_set_syntax (RE_SYNTAX_GREP);
19875796c8dcSSimon Schubert
19885796c8dcSSimon Schubert c = add_cmd ("directory", class_files, directory_command, _("\
19895796c8dcSSimon Schubert Add directory DIR to beginning of search path for source files.\n\
19905796c8dcSSimon Schubert Forget cached info on source file locations and line positions.\n\
19915796c8dcSSimon Schubert DIR can also be $cwd for the current working directory, or $cdir for the\n\
19925796c8dcSSimon Schubert directory in which the source file was compiled into object code.\n\
19935796c8dcSSimon Schubert With no argument, reset the search path to $cdir:$cwd, the default."),
19945796c8dcSSimon Schubert &cmdlist);
19955796c8dcSSimon Schubert
19965796c8dcSSimon Schubert if (dbx_commands)
19975796c8dcSSimon Schubert add_com_alias ("use", "directory", class_files, 0);
19985796c8dcSSimon Schubert
19995796c8dcSSimon Schubert set_cmd_completer (c, filename_completer);
20005796c8dcSSimon Schubert
2001c50c785cSJohn Marino add_setshow_optional_filename_cmd ("directories",
2002c50c785cSJohn Marino class_files,
2003c50c785cSJohn Marino &source_path,
2004c50c785cSJohn Marino _("\
2005c50c785cSJohn Marino Set the search path for finding source files."),
2006c50c785cSJohn Marino _("\
2007c50c785cSJohn Marino Show the search path for finding source files."),
2008c50c785cSJohn Marino _("\
20095796c8dcSSimon Schubert $cwd in the path means the current working directory.\n\
2010c50c785cSJohn Marino $cdir in the path means the compilation directory of the source file.\n\
2011c50c785cSJohn Marino GDB ensures the search path always ends with $cdir:$cwd by\n\
2012c50c785cSJohn Marino appending these directories if necessary.\n\
2013c50c785cSJohn Marino Setting the value to an empty string sets it to $cdir:$cwd, the default."),
2014c50c785cSJohn Marino set_directories_command,
2015c50c785cSJohn Marino show_directories_command,
2016c50c785cSJohn Marino &setlist, &showlist);
20175796c8dcSSimon Schubert
20185796c8dcSSimon Schubert if (xdb_commands)
20195796c8dcSSimon Schubert {
20205796c8dcSSimon Schubert add_com_alias ("D", "directory", class_files, 0);
2021c50c785cSJohn Marino add_cmd ("ld", no_class, show_directories_1, _("\
20225796c8dcSSimon Schubert Current search path for finding source files.\n\
20235796c8dcSSimon Schubert $cwd in the path means the current working directory.\n\
20245796c8dcSSimon Schubert $cdir in the path means the compilation directory of the source file."),
20255796c8dcSSimon Schubert &cmdlist);
20265796c8dcSSimon Schubert }
20275796c8dcSSimon Schubert
20285796c8dcSSimon Schubert add_info ("source", source_info,
20295796c8dcSSimon Schubert _("Information about the current source file."));
20305796c8dcSSimon Schubert
20315796c8dcSSimon Schubert add_info ("line", line_info, _("\
20325796c8dcSSimon Schubert Core addresses of the code for a source line.\n\
20335796c8dcSSimon Schubert Line can be specified as\n\
20345796c8dcSSimon Schubert LINENUM, to list around that line in current file,\n\
20355796c8dcSSimon Schubert FILE:LINENUM, to list around that line in that file,\n\
20365796c8dcSSimon Schubert FUNCTION, to list around beginning of that function,\n\
20375796c8dcSSimon Schubert FILE:FUNCTION, to distinguish among like-named static functions.\n\
20385796c8dcSSimon Schubert Default is to describe the last source line that was listed.\n\n\
20395796c8dcSSimon Schubert This sets the default address for \"x\" to the line's first instruction\n\
20405796c8dcSSimon Schubert so that \"x/i\" suffices to start examining the machine code.\n\
20415796c8dcSSimon Schubert The address is also stored as the value of \"$_\"."));
20425796c8dcSSimon Schubert
20435796c8dcSSimon Schubert add_com ("forward-search", class_files, forward_search_command, _("\
20445796c8dcSSimon Schubert Search for regular expression (see regex(3)) from last line listed.\n\
20455796c8dcSSimon Schubert The matching line number is also stored as the value of \"$_\"."));
20465796c8dcSSimon Schubert add_com_alias ("search", "forward-search", class_files, 0);
2047*ef5ccd6cSJohn Marino add_com_alias ("fo", "forward-search", class_files, 1);
20485796c8dcSSimon Schubert
20495796c8dcSSimon Schubert add_com ("reverse-search", class_files, reverse_search_command, _("\
20505796c8dcSSimon Schubert Search backward for regular expression (see regex(3)) from last line listed.\n\
20515796c8dcSSimon Schubert The matching line number is also stored as the value of \"$_\"."));
2052cf7f2e2dSJohn Marino add_com_alias ("rev", "reverse-search", class_files, 1);
20535796c8dcSSimon Schubert
20545796c8dcSSimon Schubert if (xdb_commands)
20555796c8dcSSimon Schubert {
20565796c8dcSSimon Schubert add_com_alias ("/", "forward-search", class_files, 0);
20575796c8dcSSimon Schubert add_com_alias ("?", "reverse-search", class_files, 0);
20585796c8dcSSimon Schubert }
20595796c8dcSSimon Schubert
20605796c8dcSSimon Schubert add_setshow_integer_cmd ("listsize", class_support, &lines_to_list, _("\
20615796c8dcSSimon Schubert Set number of source lines gdb will list by default."), _("\
20625796c8dcSSimon Schubert Show number of source lines gdb will list by default."), NULL,
20635796c8dcSSimon Schubert NULL,
20645796c8dcSSimon Schubert show_lines_to_list,
20655796c8dcSSimon Schubert &setlist, &showlist);
20665796c8dcSSimon Schubert
20675796c8dcSSimon Schubert add_cmd ("substitute-path", class_files, set_substitute_path_command,
20685796c8dcSSimon Schubert _("\
20695796c8dcSSimon Schubert Usage: set substitute-path FROM TO\n\
20705796c8dcSSimon Schubert Add a substitution rule replacing FROM into TO in source file names.\n\
20715796c8dcSSimon Schubert If a substitution rule was previously set for FROM, the old rule\n\
20725796c8dcSSimon Schubert is replaced by the new one."),
20735796c8dcSSimon Schubert &setlist);
20745796c8dcSSimon Schubert
20755796c8dcSSimon Schubert add_cmd ("substitute-path", class_files, unset_substitute_path_command,
20765796c8dcSSimon Schubert _("\
20775796c8dcSSimon Schubert Usage: unset substitute-path [FROM]\n\
20785796c8dcSSimon Schubert Delete the rule for substituting FROM in source file names. If FROM\n\
20795796c8dcSSimon Schubert is not specified, all substituting rules are deleted.\n\
20805796c8dcSSimon Schubert If the debugger cannot find a rule for FROM, it will display a warning."),
20815796c8dcSSimon Schubert &unsetlist);
20825796c8dcSSimon Schubert
20835796c8dcSSimon Schubert add_cmd ("substitute-path", class_files, show_substitute_path_command,
20845796c8dcSSimon Schubert _("\
20855796c8dcSSimon Schubert Usage: show substitute-path [FROM]\n\
20865796c8dcSSimon Schubert Print the rule for substituting FROM in source file names. If FROM\n\
20875796c8dcSSimon Schubert is not specified, print all substitution rules."),
20885796c8dcSSimon Schubert &showlist);
2089*ef5ccd6cSJohn Marino
2090*ef5ccd6cSJohn Marino add_setshow_enum_cmd ("filename-display", class_files,
2091*ef5ccd6cSJohn Marino filename_display_kind_names,
2092*ef5ccd6cSJohn Marino &filename_display_string, _("\
2093*ef5ccd6cSJohn Marino Set how to display filenames."), _("\
2094*ef5ccd6cSJohn Marino Show how to display filenames."), _("\
2095*ef5ccd6cSJohn Marino filename-display can be:\n\
2096*ef5ccd6cSJohn Marino basename - display only basename of a filename\n\
2097*ef5ccd6cSJohn Marino relative - display a filename relative to the compilation directory\n\
2098*ef5ccd6cSJohn Marino absolute - display an absolute filename\n\
2099*ef5ccd6cSJohn Marino By default, relative filenames are displayed."),
2100*ef5ccd6cSJohn Marino NULL,
2101*ef5ccd6cSJohn Marino show_filename_display_string,
2102*ef5ccd6cSJohn Marino &setlist, &showlist);
2103*ef5ccd6cSJohn Marino
21045796c8dcSSimon Schubert }
2105