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