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