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