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