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