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