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