* symtab.c (symbol_search_name): Minor reformatting.
[deliverable/binutils-gdb.git] / gdb / source.c
CommitLineData
c906108c 1/* List lines of source files for GDB, the GNU debugger.
a752853e 2 Copyright 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994,
5e987968 3 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004
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
20 Foundation, Inc., 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, 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"
32
33#include <sys/types.h>
34#include "gdb_string.h"
35#include "gdb_stat.h"
36#include <fcntl.h>
c906108c 37#include "gdbcore.h"
88987551 38#include "gdb_regex.h"
c906108c
SS
39#include "symfile.h"
40#include "objfiles.h"
41#include "annotate.h"
42#include "gdbtypes.h"
c5f0f3d0 43#include "linespec.h"
fe4e3eb8 44#include "filenames.h" /* for DOSish file names */
d75b5104 45#include "completer.h"
8b93c638 46#include "ui-out.h"
dbda9972 47#include "readline/readline.h"
c906108c 48
3e3a28f1
MK
49#ifndef O_BINARY
50#define O_BINARY 0
51#endif
52
c906108c
SS
53#ifdef CRLF_SOURCE_FILES
54
55/* Define CRLF_SOURCE_FILES in an xm-*.h file if source files on the
56 host use \r\n rather than just \n. Defining CRLF_SOURCE_FILES is
57 much faster than defining LSEEK_NOT_LINEAR. */
58
c906108c
SS
59#define OPEN_MODE (O_RDONLY | O_BINARY)
60#define FDOPEN_MODE FOPEN_RB
61
62#else /* ! defined (CRLF_SOURCE_FILES) */
63
64#define OPEN_MODE O_RDONLY
65#define FDOPEN_MODE FOPEN_RT
66
67#endif /* ! defined (CRLF_SOURCE_FILES) */
68
c906108c
SS
69/* Prototypes for exported functions. */
70
a14ed312 71void _initialize_source (void);
c906108c
SS
72
73/* Prototypes for local functions. */
74
a14ed312 75static int get_filename_and_charpos (struct symtab *, char **);
c906108c 76
a14ed312 77static void reverse_search_command (char *, int);
c906108c 78
a14ed312 79static void forward_search_command (char *, int);
c906108c 80
a14ed312 81static void line_info (char *, int);
c906108c 82
a14ed312 83static void source_info (char *, int);
c906108c 84
a14ed312 85static void show_directories (char *, int);
c906108c
SS
86
87/* Path of directories to search for source files.
88 Same format as the PATH environment variable's value. */
89
90char *source_path;
91
92/* Symtab of default file for listing lines of. */
93
0378c332 94static struct symtab *current_source_symtab;
c906108c
SS
95
96/* Default next line to list. */
97
0378c332 98static int current_source_line;
c906108c
SS
99
100/* Default number of lines to print with commands like "list".
101 This is based on guessing how many long (i.e. more than chars_per_line
102 characters) lines there will be. To be completely correct, "list"
103 and friends should be rewritten to count characters and see where
104 things are wrapping, but that would be a fair amount of work. */
105
106int lines_to_list = 10;
107
108/* Line number of last line printed. Default for various commands.
109 current_source_line is usually, but not always, the same as this. */
110
111static int last_line_listed;
112
113/* First line number listed by last listing command. */
114
115static int first_line_listed;
116
117/* Saves the name of the last source file visited and a possible error code.
118 Used to prevent repeating annoying "No such file or directories" msgs */
119
120static struct symtab *last_source_visited = NULL;
121static int last_source_error = 0;
c906108c 122\f
0378c332
FN
123/* Return the first line listed by print_source_lines.
124 Used by command interpreters to request listing from
125 a previous point. */
126
127int
128get_first_line_listed (void)
129{
130 return first_line_listed;
131}
132
133/* Return the default number of lines to print with commands like the
134 cli "list". The caller of print_source_lines must use this to
135 calculate the end line and use it in the call to print_source_lines
136 as it does not automatically use this value. */
137
138int
139get_lines_to_list (void)
140{
141 return lines_to_list;
142}
143
144/* Return the current source file for listing and next line to list.
145 NOTE: The returned sal pc and end fields are not valid. */
146
147struct symtab_and_line
148get_current_source_symtab_and_line (void)
149{
150 struct symtab_and_line cursal;
151
152 cursal.symtab = current_source_symtab;
153 cursal.line = current_source_line;
53cb0458
FN
154 cursal.pc = 0;
155 cursal.end = 0;
0378c332
FN
156
157 return cursal;
158}
159
53cb0458
FN
160/* If the current source file for listing is not set, try and get a default.
161 Usually called before get_current_source_symtab_and_line() is called.
0378c332 162 It may err out if a default cannot be determined.
53cb0458
FN
163 We must be cautious about where it is called, as it can recurse as the
164 process of determining a new default may call the caller!
165 Use get_current_source_symtab_and_line only to get whatever
166 we have without erroring out or trying to get a default. */
0378c332 167
53cb0458
FN
168void
169set_default_source_symtab_and_line (void)
0378c332
FN
170{
171 struct symtab_and_line cursal;
172
173 if (!have_full_symbols () && !have_partial_symbols ())
174 error ("No symbol table is loaded. Use the \"file\" command.");
175
176 /* Pull in a current source symtab if necessary */
177 if (current_source_symtab == 0)
178 select_source_symtab (0);
0378c332
FN
179}
180
181/* Return the current default file for listing and next line to list
182 (the returned sal pc and end fields are not valid.)
53cb0458
FN
183 and set the current default to whatever is in SAL.
184 NOTE: The returned sal pc and end fields are not valid. */
0378c332
FN
185
186struct symtab_and_line
53cb0458 187set_current_source_symtab_and_line (const struct symtab_and_line *sal)
0378c332
FN
188{
189 struct symtab_and_line cursal;
190
191 cursal.symtab = current_source_symtab;
192 cursal.line = current_source_line;
193
194 current_source_symtab = sal->symtab;
195 current_source_line = sal->line;
c214a6fd
FN
196 cursal.pc = 0;
197 cursal.end = 0;
0378c332
FN
198
199 return cursal;
200}
201
202/* Reset any information stored about a default file and line to print. */
203
204void
205clear_current_source_symtab_and_line (void)
206{
207 current_source_symtab = 0;
208 current_source_line = 0;
209}
c5aa993b 210
c906108c
SS
211/* Set the source file default for the "list" command to be S.
212
213 If S is NULL, and we don't have a default, find one. This
214 should only be called when the user actually tries to use the
215 default, since we produce an error if we can't find a reasonable
216 default. Also, since this can cause symbols to be read, doing it
217 before we need to would make things slower than necessary. */
218
219void
aa1ee363 220select_source_symtab (struct symtab *s)
c906108c
SS
221{
222 struct symtabs_and_lines sals;
223 struct symtab_and_line sal;
224 struct partial_symtab *ps;
225 struct partial_symtab *cs_pst = 0;
226 struct objfile *ofp;
c5aa993b 227
c906108c
SS
228 if (s)
229 {
230 current_source_symtab = s;
231 current_source_line = 1;
232 return;
233 }
234
235 if (current_source_symtab)
236 return;
237
238 /* Make the default place to list be the function `main'
239 if one exists. */
176620f1 240 if (lookup_symbol (main_name (), 0, VAR_DOMAIN, 0, NULL))
c906108c 241 {
51cc5b07 242 sals = decode_line_spec (main_name (), 1);
c906108c 243 sal = sals.sals[0];
b8c9b27d 244 xfree (sals.sals);
c906108c
SS
245 current_source_symtab = sal.symtab;
246 current_source_line = max (sal.line - (lines_to_list - 1), 1);
247 if (current_source_symtab)
c5aa993b 248 return;
c906108c 249 }
c5aa993b 250
c906108c
SS
251 /* All right; find the last file in the symtab list (ignoring .h's). */
252
253 current_source_line = 1;
254
c5aa993b 255 for (ofp = object_files; ofp != NULL; ofp = ofp->next)
c906108c 256 {
c5aa993b 257 for (s = ofp->symtabs; s; s = s->next)
c906108c 258 {
c5aa993b 259 char *name = s->filename;
c906108c 260 int len = strlen (name);
cb137aa5 261 if (!(len > 2 && (DEPRECATED_STREQ (&name[len - 2], ".h"))))
c906108c
SS
262 {
263 current_source_symtab = s;
264 }
265 }
266 }
267 if (current_source_symtab)
268 return;
269
270 /* Howabout the partial symbol tables? */
271
c5aa993b 272 for (ofp = object_files; ofp != NULL; ofp = ofp->next)
c906108c 273 {
c5aa993b 274 for (ps = ofp->psymtabs; ps != NULL; ps = ps->next)
c906108c 275 {
c5aa993b 276 char *name = ps->filename;
c906108c 277 int len = strlen (name);
cb137aa5 278 if (!(len > 2 && (DEPRECATED_STREQ (&name[len - 2], ".h"))))
c906108c
SS
279 {
280 cs_pst = ps;
281 }
282 }
283 }
284 if (cs_pst)
285 {
c5aa993b 286 if (cs_pst->readin)
c906108c 287 {
8e65ff28
AC
288 internal_error (__FILE__, __LINE__,
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
300 error ("Can't find a default source file");
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 {
43ff13b4 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
390/* Add zero or more directories to the front of an arbitrary path. */
391
392void
fba45db2 393mod_path (char *dirname, char **which_path)
c04e0a08
JJ
394{
395 add_path (dirname, which_path, 1);
396}
397
398/* Workhorse of mod_path. Takes an extra argument to determine
399 if dirname should be parsed for separators that indicate multiple
400 directories. This allows for interfaces that pre-parse the dirname
401 and allow specification of traditional separator characters such
402 as space or tab. */
403
404void
405add_path (char *dirname, char **which_path, int parse_separators)
c906108c
SS
406{
407 char *old = *which_path;
408 int prefix = 0;
409
410 if (dirname == 0)
411 return;
412
4fcf66da 413 dirname = xstrdup (dirname);
b8c9b27d 414 make_cleanup (xfree, dirname);
c906108c
SS
415
416 do
417 {
418 char *name = dirname;
aa1ee363 419 char *p;
c906108c
SS
420 struct stat st;
421
422 {
c04e0a08
JJ
423 char *separator = NULL;
424 char *space = NULL;
425 char *tab = NULL;
426
427 if (parse_separators)
428 {
429 separator = strchr (name, DIRNAME_SEPARATOR);
430 space = strchr (name, ' ');
431 tab = strchr (name, '\t');
432 }
c906108c 433
c5aa993b 434 if (separator == 0 && space == 0 && tab == 0)
c906108c
SS
435 p = dirname = name + strlen (name);
436 else
437 {
438 p = 0;
439 if (separator != 0 && (p == 0 || separator < p))
440 p = separator;
441 if (space != 0 && (p == 0 || space < p))
442 p = space;
443 if (tab != 0 && (p == 0 || tab < p))
444 p = tab;
445 dirname = p + 1;
446 while (*dirname == DIRNAME_SEPARATOR
447 || *dirname == ' '
448 || *dirname == '\t')
449 ++dirname;
450 }
451 }
452
fe4e3eb8 453 if (!(IS_DIR_SEPARATOR (*name) && p <= name + 1) /* "/" */
c3690141 454#ifdef HAVE_DOS_BASED_FILE_SYSTEM
7be570e7 455 /* On MS-DOS and MS-Windows, h:\ is different from h: */
fe4e3eb8 456 && !(p == name + 3 && name[1] == ':') /* "d:/" */
7be570e7 457#endif
fe4e3eb8 458 && IS_DIR_SEPARATOR (p[-1]))
c906108c
SS
459 /* Sigh. "foo/" => "foo" */
460 --p;
c906108c
SS
461 *p = '\0';
462
7be570e7 463 while (p > name && p[-1] == '.')
c906108c
SS
464 {
465 if (p - name == 1)
466 {
467 /* "." => getwd (). */
468 name = current_directory;
469 goto append;
470 }
fe4e3eb8 471 else if (p > name + 1 && IS_DIR_SEPARATOR (p[-2]))
c906108c
SS
472 {
473 if (p - name == 2)
474 {
475 /* "/." => "/". */
476 *--p = '\0';
477 goto append;
478 }
479 else
480 {
481 /* "...foo/." => "...foo". */
482 p -= 2;
483 *p = '\0';
484 continue;
485 }
486 }
487 else
488 break;
489 }
490
491 if (name[0] == '~')
492 name = tilde_expand (name);
c3690141 493#ifdef HAVE_DOS_BASED_FILE_SYSTEM
fe4e3eb8 494 else if (IS_ABSOLUTE_PATH (name) && p == name + 2) /* "d:" => "d:." */
7be570e7
JM
495 name = concat (name, ".", NULL);
496#endif
fe4e3eb8 497 else if (!IS_ABSOLUTE_PATH (name) && name[0] != '$')
c5aa993b 498 name = concat (current_directory, SLASH_STRING, name, NULL);
c906108c
SS
499 else
500 name = savestring (name, p - name);
b8c9b27d 501 make_cleanup (xfree, name);
c906108c
SS
502
503 /* Unless it's a variable, check existence. */
c5aa993b
JM
504 if (name[0] != '$')
505 {
506 /* These are warnings, not errors, since we don't want a
507 non-existent directory in a .gdbinit file to stop processing
508 of the .gdbinit file.
509
510 Whether they get added to the path is more debatable. Current
511 answer is yes, in case the user wants to go make the directory
512 or whatever. If the directory continues to not exist/not be
513 a directory/etc, then having them in the path should be
514 harmless. */
515 if (stat (name, &st) < 0)
516 {
517 int save_errno = errno;
518 fprintf_unfiltered (gdb_stderr, "Warning: ");
519 print_sys_errmsg (name, save_errno);
520 }
521 else if ((st.st_mode & S_IFMT) != S_IFDIR)
522 warning ("%s is not a directory.", name);
523 }
c906108c
SS
524
525 append:
526 {
aa1ee363 527 unsigned int len = strlen (name);
c906108c
SS
528
529 p = *which_path;
530 while (1)
531 {
7be570e7
JM
532 /* FIXME: strncmp loses in interesting ways on MS-DOS and
533 MS-Windows because of case-insensitivity and two different
534 but functionally identical slash characters. We need a
535 special filesystem-dependent file-name comparison function.
536
537 Actually, even on Unix I would use realpath() or its work-
538 alike before comparing. Then all the code above which
539 removes excess slashes and dots could simply go away. */
c906108c
SS
540 if (!strncmp (p, name, len)
541 && (p[len] == '\0' || p[len] == DIRNAME_SEPARATOR))
542 {
543 /* Found it in the search path, remove old copy */
544 if (p > *which_path)
c5aa993b 545 p--; /* Back over leading separator */
c906108c
SS
546 if (prefix > p - *which_path)
547 goto skip_dup; /* Same dir twice in one cmd */
c5aa993b 548 strcpy (p, &p[len + 1]); /* Copy from next \0 or : */
c906108c
SS
549 }
550 p = strchr (p, DIRNAME_SEPARATOR);
551 if (p != 0)
552 ++p;
553 else
554 break;
555 }
556 if (p == 0)
557 {
558 char tinybuf[2];
559
560 tinybuf[0] = DIRNAME_SEPARATOR;
561 tinybuf[1] = '\0';
562
c04e0a08
JJ
563 /* If we have already tacked on a name(s) in this command, be sure they stay
564 on the front as we tack on some more. */
c906108c
SS
565 if (prefix)
566 {
567 char *temp, c;
568
569 c = old[prefix];
570 old[prefix] = '\0';
571 temp = concat (old, tinybuf, name, NULL);
572 old[prefix] = c;
573 *which_path = concat (temp, "", &old[prefix], NULL);
574 prefix = strlen (temp);
b8c9b27d 575 xfree (temp);
c906108c
SS
576 }
577 else
578 {
579 *which_path = concat (name, (old[0] ? tinybuf : old), old, NULL);
580 prefix = strlen (name);
581 }
b8c9b27d 582 xfree (old);
c906108c
SS
583 old = *which_path;
584 }
585 }
c5aa993b
JM
586 skip_dup:;
587 }
588 while (*dirname != '\0');
c906108c
SS
589}
590
591
592static void
fba45db2 593source_info (char *ignore, int from_tty)
c906108c 594{
52f0bd74 595 struct symtab *s = current_source_symtab;
c906108c
SS
596
597 if (!s)
598 {
c5aa993b 599 printf_filtered ("No current source file.\n");
c906108c
SS
600 return;
601 }
602 printf_filtered ("Current source file is %s\n", s->filename);
603 if (s->dirname)
604 printf_filtered ("Compilation directory is %s\n", s->dirname);
605 if (s->fullname)
606 printf_filtered ("Located in %s\n", s->fullname);
607 if (s->nlines)
608 printf_filtered ("Contains %d line%s.\n", s->nlines,
609 s->nlines == 1 ? "" : "s");
610
611 printf_filtered ("Source language is %s.\n", language_str (s->language));
612 printf_filtered ("Compiled with %s debugging format.\n", s->debugformat);
919d772c
JB
613 printf_filtered ("%s preprocessor macro info.\n",
614 s->macro_table ? "Includes" : "Does not include");
c906108c 615}
c5aa993b 616\f
c906108c 617
1da1a192
JB
618/* Return True if the file NAME exists and is a regular file */
619static int
620is_regular_file (const char *name)
621{
622 struct stat st;
623 const int status = stat (name, &st);
624
625 /* Stat should never fail except when the file does not exist.
626 If stat fails, analyze the source of error and return True
627 unless the file does not exist, to avoid returning false results
628 on obscure systems where stat does not work as expected.
629 */
630 if (status != 0)
631 return (errno != ENOENT);
632
633 return S_ISREG (st.st_mode);
634}
c906108c 635
c906108c
SS
636/* Open a file named STRING, searching path PATH (dir names sep by some char)
637 using mode MODE and protection bits PROT in the calls to open.
638
014d698b
EZ
639 OPTS specifies the function behaviour in specific cases.
640
641 If OPF_TRY_CWD_FIRST, try to open ./STRING before searching PATH.
c906108c
SS
642 (ie pretend the first element of PATH is "."). This also indicates
643 that a slash in STRING disables searching of the path (this is
644 so that "exec-file ./foo" or "symbol-file ./foo" insures that you
645 get that particular version of foo or an error message).
646
014d698b
EZ
647 If OPTS has OPF_SEARCH_IN_PATH set, absolute names will also be
648 searched in path (we usually want this for source files but not for
649 executables).
650
e7a8479f 651 If FILENAME_OPENED is non-null, set it to a newly allocated string naming
a89f66e4 652 the actual file opened (this string will always start with a "/"). We
c906108c
SS
653 have to take special pains to avoid doubling the "/" between the directory
654 and the file, sigh! Emacs gets confuzzed by this when we print the
655 source file name!!!
656
657 If a file is found, return the descriptor.
658 Otherwise, return -1, with errno set for the last name we tried to open. */
659
660/* >>>> This should only allow files of certain types,
f2172603 661 >>>> eg executable, non-directory */
c906108c 662int
014d698b 663openp (const char *path, int opts, const char *string,
1f8cc6db 664 int mode, int prot,
fba45db2 665 char **filename_opened)
c906108c 666{
52f0bd74
AC
667 int fd;
668 char *filename;
1f8cc6db
AC
669 const char *p;
670 const char *p1;
52f0bd74 671 int len;
c906108c
SS
672 int alloclen;
673
674 if (!path)
675 path = ".";
676
c906108c 677 mode |= O_BINARY;
c906108c 678
014d698b 679 if ((opts & OPF_TRY_CWD_FIRST) || IS_ABSOLUTE_PATH (string))
c906108c
SS
680 {
681 int i;
072b1022
DJ
682
683 if (is_regular_file (string))
684 {
685 filename = alloca (strlen (string) + 1);
686 strcpy (filename, string);
687 fd = open (filename, mode, prot);
688 if (fd >= 0)
689 goto done;
690 }
691 else
3f565f1e
DJ
692 {
693 filename = NULL;
694 fd = -1;
695 }
072b1022 696
014d698b
EZ
697 if (!(opts & OPF_SEARCH_IN_PATH))
698 for (i = 0; string[i]; i++)
699 if (IS_DIR_SEPARATOR (string[i]))
700 goto done;
c906108c
SS
701 }
702
014d698b
EZ
703 /* /foo => foo, to avoid multiple slashes that Emacs doesn't like. */
704 while (IS_DIR_SEPARATOR(string[0]))
705 string++;
706
c906108c 707 /* ./foo => foo */
fe4e3eb8 708 while (string[0] == '.' && IS_DIR_SEPARATOR (string[1]))
c906108c
SS
709 string += 2;
710
711 alloclen = strlen (path) + strlen (string) + 2;
1f8cc6db 712 filename = alloca (alloclen);
c906108c
SS
713 fd = -1;
714 for (p = path; p; p = p1 ? p1 + 1 : 0)
715 {
1f8cc6db 716 p1 = strchr (p, DIRNAME_SEPARATOR);
c906108c
SS
717 if (p1)
718 len = p1 - p;
719 else
720 len = strlen (p);
721
722 if (len == 4 && p[0] == '$' && p[1] == 'c'
c5aa993b
JM
723 && p[2] == 'w' && p[3] == 'd')
724 {
725 /* Name is $cwd -- insert current directory name instead. */
726 int newlen;
727
728 /* First, realloc the filename buffer if too short. */
729 len = strlen (current_directory);
730 newlen = len + strlen (string) + 2;
731 if (newlen > alloclen)
732 {
733 alloclen = newlen;
1f8cc6db 734 filename = alloca (alloclen);
c5aa993b
JM
735 }
736 strcpy (filename, current_directory);
737 }
738 else
739 {
740 /* Normal file name in path -- just use it. */
741 strncpy (filename, p, len);
742 filename[len] = 0;
c906108c 743 }
c906108c
SS
744
745 /* Remove trailing slashes */
fe4e3eb8 746 while (len > 0 && IS_DIR_SEPARATOR (filename[len - 1]))
c906108c
SS
747 filename[--len] = 0;
748
c5aa993b 749 strcat (filename + len, SLASH_STRING);
c906108c
SS
750 strcat (filename, string);
751
1da1a192 752 if (is_regular_file (filename))
5e987968
AS
753 {
754 fd = open (filename, mode);
755 if (fd >= 0)
756 break;
757 }
c906108c
SS
758 }
759
c5aa993b 760done:
c906108c
SS
761 if (filename_opened)
762 {
a89f66e4
JB
763 /* If a file was opened, canonicalize its filename. Use xfullpath
764 rather than gdb_realpath to avoid resolving the basename part
765 of filenames when the associated file is a symbolic link. This
766 fixes a potential inconsistency between the filenames known to
767 GDB and the filenames it prints in the annotations. */
c906108c 768 if (fd < 0)
1f8cc6db 769 *filename_opened = NULL;
fe4e3eb8 770 else if (IS_ABSOLUTE_PATH (filename))
a89f66e4 771 *filename_opened = xfullpath (filename);
c906108c
SS
772 else
773 {
774 /* Beware the // my son, the Emacs barfs, the botch that catch... */
c5aa993b 775
58d370e0 776 char *f = concat (current_directory,
5e987968
AS
777 IS_DIR_SEPARATOR (current_directory[strlen (current_directory) - 1])
778 ? "" : SLASH_STRING,
779 filename, NULL);
a89f66e4 780 *filename_opened = xfullpath (f);
58d370e0 781 xfree (f);
c5aa993b 782 }
c906108c 783 }
c906108c
SS
784
785 return fd;
786}
787
c5aa993b 788
c906108c
SS
789/* This is essentially a convenience, for clients that want the behaviour
790 of openp, using source_path, but that really don't want the file to be
791 opened but want instead just to know what the full pathname is (as
792 qualified against source_path).
793
794 The current working directory is searched first.
795
796 If the file was found, this function returns 1, and FULL_PATHNAME is
797 set to the fully-qualified pathname.
798
5e987968 799 Else, this functions returns 0, and FULL_PATHNAME is set to NULL. */
c906108c 800int
fba45db2 801source_full_path_of (char *filename, char **full_pathname)
c906108c 802{
c5aa993b 803 int fd;
c906108c 804
014d698b
EZ
805 fd = openp (source_path, OPF_TRY_CWD_FIRST | OPF_SEARCH_IN_PATH, filename,
806 O_RDONLY, 0, full_pathname);
c906108c
SS
807 if (fd < 0)
808 {
809 *full_pathname = NULL;
810 return 0;
811 }
812
813 close (fd);
814 return 1;
815}
816
57c22c6c
BR
817/* This function is capable of finding the absolute path to a
818 source file, and opening it, provided you give it an
819 OBJFILE and FILENAME. Both the DIRNAME and FULLNAME are only
820 added suggestions on where to find the file.
821
822 OBJFILE should be the objfile associated with a psymtab or symtab.
823 FILENAME should be the filename to open.
824 DIRNAME is the compilation directory of a particular source file.
825 Only some debug formats provide this info.
826 FULLNAME can be the last known absolute path to the file in question.
827
828 On Success
829 A valid file descriptor is returned. ( the return value is positive )
830 FULLNAME is set to the absolute path to the file just opened.
831
832 On Failure
833 A non valid file descriptor is returned. ( the return value is negitive )
834 FULLNAME is set to NULL. */
c906108c 835int
57c22c6c 836find_and_open_source (struct objfile *objfile,
5e987968
AS
837 const char *filename,
838 const char *dirname,
839 char **fullname)
c906108c
SS
840{
841 char *path = source_path;
31889e00 842 const char *p;
c906108c 843 int result;
c906108c
SS
844
845 /* Quick way out if we already know its full name */
57c22c6c 846 if (*fullname)
c906108c 847 {
57c22c6c 848 result = open (*fullname, OPEN_MODE);
c906108c 849 if (result >= 0)
c5aa993b 850 return result;
c906108c 851 /* Didn't work -- free old one, try again. */
2dc74dc1 852 xfree (*fullname);
57c22c6c 853 *fullname = NULL;
c906108c
SS
854 }
855
57c22c6c 856 if (dirname != NULL)
c906108c
SS
857 {
858 /* Replace a path entry of $cdir with the compilation directory name */
859#define cdir_len 5
860 /* We cast strstr's result in case an ANSIhole has made it const,
c5aa993b
JM
861 which produces a "required warning" when assigned to a nonconst. */
862 p = (char *) strstr (source_path, "$cdir");
c906108c 863 if (p && (p == path || p[-1] == DIRNAME_SEPARATOR)
c5aa993b 864 && (p[cdir_len] == DIRNAME_SEPARATOR || p[cdir_len] == '\0'))
c906108c
SS
865 {
866 int len;
867
868 path = (char *)
57c22c6c 869 alloca (strlen (source_path) + 1 + strlen (dirname) + 1);
c906108c 870 len = p - source_path;
c5aa993b 871 strncpy (path, source_path, len); /* Before $cdir */
57c22c6c 872 strcpy (path + len, dirname); /* new stuff */
c5aa993b 873 strcat (path + len, source_path + len + cdir_len); /* After $cdir */
c906108c
SS
874 }
875 }
876
014d698b 877 result = openp (path, OPF_SEARCH_IN_PATH, filename, OPEN_MODE, 0, fullname);
c906108c
SS
878 if (result < 0)
879 {
880 /* Didn't work. Try using just the basename. */
57c22c6c
BR
881 p = lbasename (filename);
882 if (p != filename)
014d698b 883 result = openp (path, OPF_SEARCH_IN_PATH, p, OPEN_MODE, 0, fullname);
c906108c 884 }
c906108c
SS
885
886 if (result >= 0)
887 {
57c22c6c
BR
888 char *tmp_fullname;
889 tmp_fullname = *fullname;
982526a1 890 *fullname = xstrdup (tmp_fullname);
57c22c6c 891 xfree (tmp_fullname);
c906108c
SS
892 }
893 return result;
894}
895
57c22c6c
BR
896/* Open a source file given a symtab S. Returns a file descriptor or
897 negative number for error.
898
899 This function is a convience function to find_and_open_source. */
900
901int
902open_source_file (struct symtab *s)
903{
5e987968
AS
904 if (!s)
905 return -1;
57c22c6c
BR
906
907 return find_and_open_source (s->objfile, s->filename, s->dirname,
5e987968 908 &s->fullname);
57c22c6c
BR
909}
910
911/* Finds the fullname that a symtab represents.
c906108c 912
57c22c6c
BR
913 If this functions finds the fullname, it will save it in ps->fullname
914 and it will also return the value.
915
916 If this function fails to find the file that this symtab represents,
917 NULL will be returned and ps->fullname will be set to NULL. */
c906108c 918char *
57c22c6c 919symtab_to_fullname (struct symtab *s)
c906108c 920{
57c22c6c 921 int r;
c906108c
SS
922
923 if (!s)
924 return NULL;
925
57c22c6c
BR
926 /* Don't check s->fullname here, the file could have been
927 deleted/moved/..., look for it again */
5e987968
AS
928 r = find_and_open_source (s->objfile, s->filename, s->dirname,
929 &s->fullname);
c906108c 930
57c22c6c 931 if (r)
5e987968
AS
932 {
933 close (r);
934 return s->fullname;
935 }
c906108c 936
57c22c6c
BR
937 return NULL;
938}
c906108c 939
57c22c6c 940/* Finds the fullname that a partial_symtab represents.
c906108c 941
57c22c6c
BR
942 If this functions finds the fullname, it will save it in ps->fullname
943 and it will also return the value.
c906108c 944
57c22c6c
BR
945 If this function fails to find the file that this partial_symtab represents,
946 NULL will be returned and ps->fullname will be set to NULL. */
947char *
948psymtab_to_fullname (struct partial_symtab *ps)
949{
950 int r;
951
952 if (!ps)
953 return NULL;
954
955 /* Don't check ps->fullname here, the file could have been
956 deleted/moved/..., look for it again */
5e987968
AS
957 r = find_and_open_source (ps->objfile, ps->filename, ps->dirname,
958 &ps->fullname);
57c22c6c
BR
959
960 if (r)
5e987968
AS
961 {
962 close (r);
963 return ps->fullname;
964 }
965
57c22c6c
BR
966 return NULL;
967}
5e987968 968\f
c906108c
SS
969/* Create and initialize the table S->line_charpos that records
970 the positions of the lines in the source file, which is assumed
971 to be open on descriptor DESC.
972 All set S->nlines to the number of such lines. */
973
974void
fba45db2 975find_source_lines (struct symtab *s, int desc)
c906108c
SS
976{
977 struct stat st;
52f0bd74 978 char *data, *p, *end;
c906108c
SS
979 int nlines = 0;
980 int lines_allocated = 1000;
981 int *line_charpos;
982 long mtime = 0;
983 int size;
984
7936743b 985 line_charpos = (int *) xmalloc (lines_allocated * sizeof (int));
c906108c
SS
986 if (fstat (desc, &st) < 0)
987 perror_with_name (s->filename);
988
989 if (s && s->objfile && s->objfile->obfd)
c5aa993b 990 mtime = bfd_get_mtime (s->objfile->obfd);
c906108c 991 else if (exec_bfd)
c5aa993b 992 mtime = bfd_get_mtime (exec_bfd);
c906108c
SS
993
994 if (mtime && mtime < st.st_mtime)
995 {
c5aa993b 996 warning ("Source file is more recent than executable.\n");
c906108c
SS
997 }
998
999#ifdef LSEEK_NOT_LINEAR
1000 {
1001 char c;
1002
1003 /* Have to read it byte by byte to find out where the chars live */
1004
1005 line_charpos[0] = lseek (desc, 0, SEEK_CUR);
1006 nlines = 1;
c5aa993b 1007 while (myread (desc, &c, 1) > 0)
c906108c 1008 {
c5aa993b 1009 if (c == '\n')
c906108c 1010 {
c5aa993b 1011 if (nlines == lines_allocated)
c906108c
SS
1012 {
1013 lines_allocated *= 2;
1014 line_charpos =
0efffb96
AC
1015 (int *) xrealloc ((char *) line_charpos,
1016 sizeof (int) * lines_allocated);
c906108c
SS
1017 }
1018 line_charpos[nlines++] = lseek (desc, 0, SEEK_CUR);
1019 }
1020 }
1021 }
1022#else /* lseek linear. */
1023 {
1024 struct cleanup *old_cleanups;
1025
1026 /* st_size might be a large type, but we only support source files whose
1027 size fits in an int. */
1028 size = (int) st.st_size;
1029
1030 /* Use malloc, not alloca, because this may be pretty large, and we may
1031 run into various kinds of limits on stack size. */
1032 data = (char *) xmalloc (size);
b8c9b27d 1033 old_cleanups = make_cleanup (xfree, data);
c906108c
SS
1034
1035 /* Reassign `size' to result of read for systems where \r\n -> \n. */
1036 size = myread (desc, data, size);
1037 if (size < 0)
1038 perror_with_name (s->filename);
1039 end = data + size;
1040 p = data;
1041 line_charpos[0] = 0;
1042 nlines = 1;
1043 while (p != end)
1044 {
1045 if (*p++ == '\n'
c5aa993b 1046 /* A newline at the end does not start a new line. */
c906108c
SS
1047 && p != end)
1048 {
1049 if (nlines == lines_allocated)
1050 {
1051 lines_allocated *= 2;
1052 line_charpos =
0efffb96
AC
1053 (int *) xrealloc ((char *) line_charpos,
1054 sizeof (int) * lines_allocated);
c906108c
SS
1055 }
1056 line_charpos[nlines++] = p - data;
1057 }
1058 }
1059 do_cleanups (old_cleanups);
1060 }
1061#endif /* lseek linear. */
1062 s->nlines = nlines;
1063 s->line_charpos =
0efffb96 1064 (int *) xrealloc ((char *) line_charpos, nlines * sizeof (int));
c906108c
SS
1065
1066}
1067
1068/* Return the character position of a line LINE in symtab S.
1069 Return 0 if anything is invalid. */
1070
c5aa993b 1071#if 0 /* Currently unused */
c906108c
SS
1072
1073int
fba45db2 1074source_line_charpos (struct symtab *s, int line)
c906108c 1075{
c5aa993b
JM
1076 if (!s)
1077 return 0;
1078 if (!s->line_charpos || line <= 0)
1079 return 0;
c906108c
SS
1080 if (line > s->nlines)
1081 line = s->nlines;
1082 return s->line_charpos[line - 1];
1083}
1084
1085/* Return the line number of character position POS in symtab S. */
1086
1087int
aa1ee363 1088source_charpos_line (struct symtab *s, int chr)
c906108c 1089{
52f0bd74
AC
1090 int line = 0;
1091 int *lnp;
c5aa993b
JM
1092
1093 if (s == 0 || s->line_charpos == 0)
1094 return 0;
c906108c
SS
1095 lnp = s->line_charpos;
1096 /* Files are usually short, so sequential search is Ok */
c5aa993b 1097 while (line < s->nlines && *lnp <= chr)
c906108c
SS
1098 {
1099 line++;
1100 lnp++;
1101 }
1102 if (line >= s->nlines)
1103 line = s->nlines;
1104 return line;
1105}
1106
c5aa993b 1107#endif /* 0 */
c906108c 1108\f
c5aa993b 1109
c906108c
SS
1110/* Get full pathname and line number positions for a symtab.
1111 Return nonzero if line numbers may have changed.
1112 Set *FULLNAME to actual name of the file as found by `openp',
1113 or to 0 if the file is not found. */
1114
1115static int
fba45db2 1116get_filename_and_charpos (struct symtab *s, char **fullname)
c906108c 1117{
52f0bd74 1118 int desc, linenums_changed = 0;
c5aa993b 1119
c906108c
SS
1120 desc = open_source_file (s);
1121 if (desc < 0)
1122 {
1123 if (fullname)
1124 *fullname = NULL;
1125 return 0;
c5aa993b 1126 }
c906108c
SS
1127 if (fullname)
1128 *fullname = s->fullname;
c5aa993b
JM
1129 if (s->line_charpos == 0)
1130 linenums_changed = 1;
1131 if (linenums_changed)
1132 find_source_lines (s, desc);
c906108c
SS
1133 close (desc);
1134 return linenums_changed;
1135}
1136
1137/* Print text describing the full name of the source file S
1138 and the line number LINE and its corresponding character position.
1139 The text starts with two Ctrl-z so that the Emacs-GDB interface
1140 can easily find it.
1141
1142 MID_STATEMENT is nonzero if the PC is not at the beginning of that line.
1143
1144 Return 1 if successful, 0 if could not find the file. */
1145
1146int
fba45db2
KB
1147identify_source_line (struct symtab *s, int line, int mid_statement,
1148 CORE_ADDR pc)
c906108c
SS
1149{
1150 if (s->line_charpos == 0)
c5aa993b 1151 get_filename_and_charpos (s, (char **) NULL);
c906108c
SS
1152 if (s->fullname == 0)
1153 return 0;
1154 if (line > s->nlines)
1155 /* Don't index off the end of the line_charpos array. */
1156 return 0;
1157 annotate_source (s->fullname, line, s->line_charpos[line - 1],
1158 mid_statement, pc);
1159
1160 current_source_line = line;
1161 first_line_listed = line;
1162 last_line_listed = line;
1163 current_source_symtab = s;
1164 return 1;
1165}
c906108c 1166\f
c5aa993b 1167
c906108c
SS
1168/* Print source lines from the file of symtab S,
1169 starting with line number LINE and stopping before line number STOPLINE. */
1170
a14ed312
KB
1171static void print_source_lines_base (struct symtab *s, int line, int stopline,
1172 int noerror);
c906108c 1173static void
fba45db2 1174print_source_lines_base (struct symtab *s, int line, int stopline, int noerror)
c906108c 1175{
52f0bd74
AC
1176 int c;
1177 int desc;
1178 FILE *stream;
c906108c
SS
1179 int nlines = stopline - line;
1180
1181 /* Regardless of whether we can open the file, set current_source_symtab. */
1182 current_source_symtab = s;
1183 current_source_line = line;
1184 first_line_listed = line;
1185
8b93c638
JM
1186 /* If printing of source lines is disabled, just print file and line number */
1187 if (ui_out_test_flags (uiout, ui_source_list))
1188 {
c5aa993b
JM
1189 /* Only prints "No such file or directory" once */
1190 if ((s != last_source_visited) || (!last_source_error))
1191 {
1192 last_source_visited = s;
1193 desc = open_source_file (s);
1194 }
1195 else
1196 {
1197 desc = last_source_error;
1198 noerror = 1;
1199 }
8b93c638
JM
1200 }
1201 else
1202 {
1203 desc = -1;
1204 noerror = 1;
1205 }
c906108c
SS
1206
1207 if (desc < 0)
1208 {
1209 last_source_error = desc;
1210
c5aa993b
JM
1211 if (!noerror)
1212 {
c906108c
SS
1213 char *name = alloca (strlen (s->filename) + 100);
1214 sprintf (name, "%d\t%s", line, s->filename);
1215 print_sys_errmsg (name, errno);
1216 }
1217 else
8b93c638
JM
1218 ui_out_field_int (uiout, "line", line);
1219 ui_out_text (uiout, "\tin ");
1220 ui_out_field_string (uiout, "file", s->filename);
1221 ui_out_text (uiout, "\n");
c906108c
SS
1222
1223 return;
1224 }
1225
1226 last_source_error = 0;
1227
1228 if (s->line_charpos == 0)
1229 find_source_lines (s, desc);
1230
1231 if (line < 1 || line > s->nlines)
1232 {
1233 close (desc);
1234 error ("Line number %d out of range; %s has %d lines.",
1235 line, s->filename, s->nlines);
1236 }
1237
1238 if (lseek (desc, s->line_charpos[line - 1], 0) < 0)
1239 {
1240 close (desc);
1241 perror_with_name (s->filename);
1242 }
1243
1244 stream = fdopen (desc, FDOPEN_MODE);
1245 clearerr (stream);
1246
1247 while (nlines-- > 0)
1248 {
8b93c638
JM
1249 char buf[20];
1250
1251 c = fgetc (stream);
1252 if (c == EOF)
1253 break;
1254 last_line_listed = current_source_line;
1255 sprintf (buf, "%d\t", current_source_line++);
1256 ui_out_text (uiout, buf);
1257 do
1258 {
1259 if (c < 040 && c != '\t' && c != '\n' && c != '\r')
1260 {
1261 sprintf (buf, "^%c", c + 0100);
1262 ui_out_text (uiout, buf);
1263 }
1264 else if (c == 0177)
1265 ui_out_text (uiout, "^?");
1266#ifdef CRLF_SOURCE_FILES
1267 else if (c == '\r')
1268 {
1269 /* Skip a \r character, but only before a \n. */
1270 int c1 = fgetc (stream);
1271
1272 if (c1 != '\n')
1273 printf_filtered ("^%c", c + 0100);
1274 if (c1 != EOF)
1275 ungetc (c1, stream);
1276 }
1277#endif
1278 else
1279 {
1280 sprintf (buf, "%c", c);
1281 ui_out_text (uiout, buf);
1282 }
1283 }
1284 while (c != '\n' && (c = fgetc (stream)) >= 0);
c906108c
SS
1285 }
1286
1287 fclose (stream);
1288}
1289\f
1290/* Show source lines from the file of symtab S, starting with line
1291 number LINE and stopping before line number STOPLINE. If this is the
1292 not the command line version, then the source is shown in the source
1293 window otherwise it is simply printed */
1294
c5aa993b 1295void
fba45db2 1296print_source_lines (struct symtab *s, int line, int stopline, int noerror)
c906108c 1297{
c5aa993b 1298 print_source_lines_base (s, line, stopline, noerror);
c906108c
SS
1299}
1300\f
c906108c
SS
1301/* Print info on range of pc's in a specified line. */
1302
1303static void
fba45db2 1304line_info (char *arg, int from_tty)
c906108c
SS
1305{
1306 struct symtabs_and_lines sals;
1307 struct symtab_and_line sal;
1308 CORE_ADDR start_pc, end_pc;
1309 int i;
1310
fe39c653 1311 init_sal (&sal); /* initialize to zeroes */
c906108c
SS
1312
1313 if (arg == 0)
1314 {
1315 sal.symtab = current_source_symtab;
1316 sal.line = last_line_listed;
1317 sals.nelts = 1;
1318 sals.sals = (struct symtab_and_line *)
1319 xmalloc (sizeof (struct symtab_and_line));
1320 sals.sals[0] = sal;
1321 }
1322 else
1323 {
1324 sals = decode_line_spec_1 (arg, 0);
c5aa993b 1325
c906108c
SS
1326 dont_repeat ();
1327 }
1328
1329 /* C++ More than one line may have been specified, as when the user
1330 specifies an overloaded function name. Print info on them all. */
1331 for (i = 0; i < sals.nelts; i++)
1332 {
1333 sal = sals.sals[i];
c5aa993b 1334
c906108c
SS
1335 if (sal.symtab == 0)
1336 {
1337 printf_filtered ("No line number information available");
1338 if (sal.pc != 0)
1339 {
1340 /* This is useful for "info line *0x7f34". If we can't tell the
c5aa993b
JM
1341 user about a source line, at least let them have the symbolic
1342 address. */
c906108c
SS
1343 printf_filtered (" for address ");
1344 wrap_here (" ");
1345 print_address (sal.pc, gdb_stdout);
1346 }
1347 else
1348 printf_filtered (".");
1349 printf_filtered ("\n");
1350 }
1351 else if (sal.line > 0
1352 && find_line_pc_range (sal, &start_pc, &end_pc))
1353 {
1354 if (start_pc == end_pc)
1355 {
1356 printf_filtered ("Line %d of \"%s\"",
1357 sal.line, sal.symtab->filename);
1358 wrap_here (" ");
1359 printf_filtered (" is at address ");
1360 print_address (start_pc, gdb_stdout);
1361 wrap_here (" ");
1362 printf_filtered (" but contains no code.\n");
1363 }
1364 else
1365 {
1366 printf_filtered ("Line %d of \"%s\"",
1367 sal.line, sal.symtab->filename);
1368 wrap_here (" ");
1369 printf_filtered (" starts at address ");
1370 print_address (start_pc, gdb_stdout);
1371 wrap_here (" ");
1372 printf_filtered (" and ends at ");
1373 print_address (end_pc, gdb_stdout);
1374 printf_filtered (".\n");
1375 }
1376
1377 /* x/i should display this line's code. */
1378 set_next_address (start_pc);
1379
1380 /* Repeating "info line" should do the following line. */
1381 last_line_listed = sal.line + 1;
1382
1383 /* If this is the only line, show the source code. If it could
1384 not find the file, don't do anything special. */
1385 if (annotation_level && sals.nelts == 1)
1386 identify_source_line (sal.symtab, sal.line, 0, start_pc);
1387 }
1388 else
1389 /* Is there any case in which we get here, and have an address
1390 which the user would want to see? If we have debugging symbols
1391 and no line numbers? */
1392 printf_filtered ("Line number %d is out of range for \"%s\".\n",
1393 sal.line, sal.symtab->filename);
1394 }
b8c9b27d 1395 xfree (sals.sals);
c906108c
SS
1396}
1397\f
1398/* Commands to search the source file for a regexp. */
1399
c906108c 1400static void
fba45db2 1401forward_search_command (char *regex, int from_tty)
c906108c 1402{
52f0bd74
AC
1403 int c;
1404 int desc;
1405 FILE *stream;
c906108c
SS
1406 int line;
1407 char *msg;
1408
c906108c 1409 line = last_line_listed + 1;
c906108c
SS
1410
1411 msg = (char *) re_comp (regex);
1412 if (msg)
306d9ac5 1413 error ("%s", msg);
c906108c
SS
1414
1415 if (current_source_symtab == 0)
1416 select_source_symtab (0);
1417
1418 desc = open_source_file (current_source_symtab);
1419 if (desc < 0)
1420 perror_with_name (current_source_symtab->filename);
1421
1422 if (current_source_symtab->line_charpos == 0)
1423 find_source_lines (current_source_symtab, desc);
1424
1425 if (line < 1 || line > current_source_symtab->nlines)
1426 {
1427 close (desc);
1428 error ("Expression not found");
1429 }
1430
1431 if (lseek (desc, current_source_symtab->line_charpos[line - 1], 0) < 0)
1432 {
1433 close (desc);
1434 perror_with_name (current_source_symtab->filename);
1435 }
1436
1437 stream = fdopen (desc, FDOPEN_MODE);
1438 clearerr (stream);
c5aa993b
JM
1439 while (1)
1440 {
1441 static char *buf = NULL;
aa1ee363 1442 char *p;
c5aa993b
JM
1443 int cursize, newsize;
1444
1445 cursize = 256;
1446 buf = xmalloc (cursize);
1447 p = buf;
1448
1449 c = getc (stream);
1450 if (c == EOF)
1451 break;
1452 do
c906108c 1453 {
c5aa993b
JM
1454 *p++ = c;
1455 if (p - buf == cursize)
1456 {
1457 newsize = cursize + cursize / 2;
1458 buf = xrealloc (buf, newsize);
1459 p = buf + cursize;
1460 cursize = newsize;
1461 }
c906108c 1462 }
c5aa993b 1463 while (c != '\n' && (c = getc (stream)) >= 0);
c906108c 1464
7be570e7
JM
1465#ifdef CRLF_SOURCE_FILES
1466 /* Remove the \r, if any, at the end of the line, otherwise
1467 regular expressions that end with $ or \n won't work. */
1468 if (p - buf > 1 && p[-2] == '\r')
1469 {
1470 p--;
1471 p[-1] = '\n';
1472 }
1473#endif
1474
c5aa993b
JM
1475 /* we now have a source line in buf, null terminate and match */
1476 *p = 0;
1477 if (re_exec (buf) > 0)
1478 {
1479 /* Match! */
1480 fclose (stream);
c5aa993b
JM
1481 print_source_lines (current_source_symtab, line, line + 1, 0);
1482 set_internalvar (lookup_internalvar ("_"),
1483 value_from_longest (builtin_type_int,
1484 (LONGEST) line));
1485 current_source_line = max (line - lines_to_list / 2, 1);
1486 return;
1487 }
1488 line++;
1489 }
c906108c
SS
1490
1491 printf_filtered ("Expression not found\n");
1492 fclose (stream);
1493}
1494
c906108c 1495static void
fba45db2 1496reverse_search_command (char *regex, int from_tty)
c906108c 1497{
52f0bd74
AC
1498 int c;
1499 int desc;
1500 FILE *stream;
c906108c
SS
1501 int line;
1502 char *msg;
063190b6 1503
c906108c 1504 line = last_line_listed - 1;
c906108c
SS
1505
1506 msg = (char *) re_comp (regex);
1507 if (msg)
306d9ac5 1508 error ("%s", msg);
c906108c
SS
1509
1510 if (current_source_symtab == 0)
1511 select_source_symtab (0);
1512
1513 desc = open_source_file (current_source_symtab);
1514 if (desc < 0)
1515 perror_with_name (current_source_symtab->filename);
1516
1517 if (current_source_symtab->line_charpos == 0)
1518 find_source_lines (current_source_symtab, desc);
1519
1520 if (line < 1 || line > current_source_symtab->nlines)
1521 {
1522 close (desc);
1523 error ("Expression not found");
1524 }
1525
1526 if (lseek (desc, current_source_symtab->line_charpos[line - 1], 0) < 0)
1527 {
1528 close (desc);
1529 perror_with_name (current_source_symtab->filename);
1530 }
1531
1532 stream = fdopen (desc, FDOPEN_MODE);
1533 clearerr (stream);
1534 while (line > 1)
1535 {
1536/* FIXME!!! We walk right off the end of buf if we get a long line!!! */
1537 char buf[4096]; /* Should be reasonable??? */
aa1ee363 1538 char *p = buf;
c906108c
SS
1539
1540 c = getc (stream);
1541 if (c == EOF)
1542 break;
c5aa993b
JM
1543 do
1544 {
1545 *p++ = c;
1546 }
1547 while (c != '\n' && (c = getc (stream)) >= 0);
c906108c 1548
7be570e7
JM
1549#ifdef CRLF_SOURCE_FILES
1550 /* Remove the \r, if any, at the end of the line, otherwise
1551 regular expressions that end with $ or \n won't work. */
1552 if (p - buf > 1 && p[-2] == '\r')
1553 {
1554 p--;
1555 p[-1] = '\n';
1556 }
1557#endif
1558
c906108c
SS
1559 /* We now have a source line in buf; null terminate and match. */
1560 *p = 0;
1561 if (re_exec (buf) > 0)
1562 {
1563 /* Match! */
1564 fclose (stream);
c5aa993b 1565 print_source_lines (current_source_symtab, line, line + 1, 0);
c906108c
SS
1566 set_internalvar (lookup_internalvar ("_"),
1567 value_from_longest (builtin_type_int,
1568 (LONGEST) line));
1569 current_source_line = max (line - lines_to_list / 2, 1);
1570 return;
1571 }
1572 line--;
1573 if (fseek (stream, current_source_symtab->line_charpos[line - 1], 0) < 0)
1574 {
1575 fclose (stream);
1576 perror_with_name (current_source_symtab->filename);
1577 }
1578 }
1579
1580 printf_filtered ("Expression not found\n");
1581 fclose (stream);
1582 return;
1583}
1584\f
1585void
fba45db2 1586_initialize_source (void)
c906108c
SS
1587{
1588 struct cmd_list_element *c;
1589 current_source_symtab = 0;
1590 init_source_path ();
1591
1592 /* The intention is to use POSIX Basic Regular Expressions.
1593 Always use the GNU regex routine for consistency across all hosts.
1594 Our current GNU regex.c does not have all the POSIX features, so this is
1595 just an approximation. */
1596 re_set_syntax (RE_SYNTAX_GREP);
1597
1598 c = add_cmd ("directory", class_files, directory_command,
c5aa993b 1599 "Add directory DIR to beginning of search path for source files.\n\
c906108c
SS
1600Forget cached info on source file locations and line positions.\n\
1601DIR can also be $cwd for the current working directory, or $cdir for the\n\
1602directory in which the source file was compiled into object code.\n\
1603With no argument, reset the search path to $cdir:$cwd, the default.",
1604 &cmdlist);
1605
1606 if (dbx_commands)
c5aa993b 1607 add_com_alias ("use", "directory", class_files, 0);
c906108c 1608
5ba2abeb 1609 set_cmd_completer (c, filename_completer);
c906108c
SS
1610
1611 add_cmd ("directories", no_class, show_directories,
1612 "Current search path for finding source files.\n\
1613$cwd in the path means the current working directory.\n\
1614$cdir in the path means the compilation directory of the source file.",
1615 &showlist);
1616
1617 if (xdb_commands)
1618 {
c5aa993b 1619 add_com_alias ("D", "directory", class_files, 0);
c906108c 1620 add_cmd ("ld", no_class, show_directories,
c5aa993b 1621 "Current search path for finding source files.\n\
c906108c
SS
1622$cwd in the path means the current working directory.\n\
1623$cdir in the path means the compilation directory of the source file.",
c5aa993b 1624 &cmdlist);
c906108c
SS
1625 }
1626
1627 add_info ("source", source_info,
1628 "Information about the current source file.");
1629
1630 add_info ("line", line_info,
1631 concat ("Core addresses of the code for a source line.\n\
1632Line can be specified as\n\
1633 LINENUM, to list around that line in current file,\n\
1634 FILE:LINENUM, to list around that line in that file,\n\
1635 FUNCTION, to list around beginning of that function,\n\
1636 FILE:FUNCTION, to distinguish among like-named static functions.\n\
1637", "\
1638Default is to describe the last source line that was listed.\n\n\
1639This sets the default address for \"x\" to the line's first instruction\n\
1640so that \"x/i\" suffices to start examining the machine code.\n\
1641The address is also stored as the value of \"$_\".", NULL));
1642
1643 add_com ("forward-search", class_files, forward_search_command,
1644 "Search for regular expression (see regex(3)) from last line listed.\n\
1645The matching line number is also stored as the value of \"$_\".");
1646 add_com_alias ("search", "forward-search", class_files, 0);
1647
1648 add_com ("reverse-search", class_files, reverse_search_command,
1649 "Search backward for regular expression (see regex(3)) from last line listed.\n\
1650The matching line number is also stored as the value of \"$_\".");
1651
1652 if (xdb_commands)
1653 {
c5aa993b
JM
1654 add_com_alias ("/", "forward-search", class_files, 0);
1655 add_com_alias ("?", "reverse-search", class_files, 0);
c906108c
SS
1656 }
1657
cb1a6d5f 1658 deprecated_add_show_from_set
c906108c 1659 (add_set_cmd ("listsize", class_support, var_uinteger,
c5aa993b
JM
1660 (char *) &lines_to_list,
1661 "Set number of source lines gdb will list by default.",
c906108c
SS
1662 &setlist),
1663 &showlist);
1664}
This page took 0.533079 seconds and 4 git commands to generate.