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