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