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