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