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