2002-04-22 Michael Chastain <mec@shout.net>
[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
SS
505
506
c906108c
SS
507/* Open a file named STRING, searching path PATH (dir names sep by some char)
508 using mode MODE and protection bits PROT in the calls to open.
509
510 If TRY_CWD_FIRST, try to open ./STRING before searching PATH.
511 (ie pretend the first element of PATH is "."). This also indicates
512 that a slash in STRING disables searching of the path (this is
513 so that "exec-file ./foo" or "symbol-file ./foo" insures that you
514 get that particular version of foo or an error message).
515
e7a8479f 516 If FILENAME_OPENED is non-null, set it to a newly allocated string naming
a89f66e4 517 the actual file opened (this string will always start with a "/"). We
c906108c
SS
518 have to take special pains to avoid doubling the "/" between the directory
519 and the file, sigh! Emacs gets confuzzed by this when we print the
520 source file name!!!
521
522 If a file is found, return the descriptor.
523 Otherwise, return -1, with errno set for the last name we tried to open. */
524
525/* >>>> This should only allow files of certain types,
f2172603 526 >>>> eg executable, non-directory */
c906108c 527int
1f8cc6db
AC
528openp (const char *path, int try_cwd_first, const char *string,
529 int mode, int prot,
fba45db2 530 char **filename_opened)
c906108c
SS
531{
532 register int fd;
533 register char *filename;
1f8cc6db
AC
534 const char *p;
535 const char *p1;
c906108c
SS
536 register int len;
537 int alloclen;
538
539 if (!path)
540 path = ".";
541
608506ed 542#if defined(_WIN32) || defined(__CYGWIN__)
c906108c
SS
543 mode |= O_BINARY;
544#endif
545
fe4e3eb8 546 if (try_cwd_first || IS_ABSOLUTE_PATH (string))
c906108c
SS
547 {
548 int i;
1f8cc6db
AC
549 filename = alloca (strlen (string) + 1);
550 strcpy (filename, string);
c906108c
SS
551 fd = open (filename, mode, prot);
552 if (fd >= 0)
553 goto done;
554 for (i = 0; string[i]; i++)
fe4e3eb8 555 if (IS_DIR_SEPARATOR (string[i]))
c906108c
SS
556 goto done;
557 }
558
559 /* ./foo => foo */
fe4e3eb8 560 while (string[0] == '.' && IS_DIR_SEPARATOR (string[1]))
c906108c
SS
561 string += 2;
562
563 alloclen = strlen (path) + strlen (string) + 2;
1f8cc6db 564 filename = alloca (alloclen);
c906108c
SS
565 fd = -1;
566 for (p = path; p; p = p1 ? p1 + 1 : 0)
567 {
1f8cc6db 568 p1 = strchr (p, DIRNAME_SEPARATOR);
c906108c
SS
569 if (p1)
570 len = p1 - p;
571 else
572 len = strlen (p);
573
574 if (len == 4 && p[0] == '$' && p[1] == 'c'
c5aa993b
JM
575 && p[2] == 'w' && p[3] == 'd')
576 {
577 /* Name is $cwd -- insert current directory name instead. */
578 int newlen;
579
580 /* First, realloc the filename buffer if too short. */
581 len = strlen (current_directory);
582 newlen = len + strlen (string) + 2;
583 if (newlen > alloclen)
584 {
585 alloclen = newlen;
1f8cc6db 586 filename = alloca (alloclen);
c5aa993b
JM
587 }
588 strcpy (filename, current_directory);
589 }
590 else
591 {
592 /* Normal file name in path -- just use it. */
593 strncpy (filename, p, len);
594 filename[len] = 0;
c906108c 595 }
c906108c
SS
596
597 /* Remove trailing slashes */
fe4e3eb8 598 while (len > 0 && IS_DIR_SEPARATOR (filename[len - 1]))
c906108c
SS
599 filename[--len] = 0;
600
c5aa993b 601 strcat (filename + len, SLASH_STRING);
c906108c
SS
602 strcat (filename, string);
603
604 fd = open (filename, mode);
c5aa993b
JM
605 if (fd >= 0)
606 break;
c906108c
SS
607 }
608
c5aa993b 609done:
c906108c
SS
610 if (filename_opened)
611 {
a89f66e4
JB
612 /* If a file was opened, canonicalize its filename. Use xfullpath
613 rather than gdb_realpath to avoid resolving the basename part
614 of filenames when the associated file is a symbolic link. This
615 fixes a potential inconsistency between the filenames known to
616 GDB and the filenames it prints in the annotations. */
c906108c 617 if (fd < 0)
1f8cc6db 618 *filename_opened = NULL;
fe4e3eb8 619 else if (IS_ABSOLUTE_PATH (filename))
a89f66e4 620 *filename_opened = xfullpath (filename);
c906108c
SS
621 else
622 {
623 /* Beware the // my son, the Emacs barfs, the botch that catch... */
c5aa993b 624
58d370e0 625 char *f = concat (current_directory,
fe4e3eb8 626 IS_DIR_SEPARATOR (current_directory[strlen (current_directory) - 1])
c5aa993b 627 ? "" : SLASH_STRING,
c906108c 628 filename, NULL);
a89f66e4 629 *filename_opened = xfullpath (f);
58d370e0 630 xfree (f);
c5aa993b 631 }
c906108c 632 }
c906108c
SS
633
634 return fd;
635}
636
c5aa993b 637
c906108c
SS
638/* This is essentially a convenience, for clients that want the behaviour
639 of openp, using source_path, but that really don't want the file to be
640 opened but want instead just to know what the full pathname is (as
641 qualified against source_path).
642
643 The current working directory is searched first.
644
645 If the file was found, this function returns 1, and FULL_PATHNAME is
646 set to the fully-qualified pathname.
647
648 Else, this functions returns 0, and FULL_PATHNAME is set to NULL.
c5aa993b 649 */
c906108c 650int
fba45db2 651source_full_path_of (char *filename, char **full_pathname)
c906108c 652{
c5aa993b 653 int fd;
c906108c
SS
654
655 fd = openp (source_path, 1, filename, O_RDONLY, 0, full_pathname);
656 if (fd < 0)
657 {
658 *full_pathname = NULL;
659 return 0;
660 }
661
662 close (fd);
663 return 1;
664}
665
666
667/* Open a source file given a symtab S. Returns a file descriptor or
668 negative number for error. */
669
670int
fba45db2 671open_source_file (struct symtab *s)
c906108c
SS
672{
673 char *path = source_path;
31889e00 674 const char *p;
c906108c
SS
675 int result;
676 char *fullname;
677
678 /* Quick way out if we already know its full name */
c5aa993b 679 if (s->fullname)
c906108c
SS
680 {
681 result = open (s->fullname, OPEN_MODE);
682 if (result >= 0)
c5aa993b 683 return result;
c906108c 684 /* Didn't work -- free old one, try again. */
aac7f4ea 685 xmfree (s->objfile->md, s->fullname);
c906108c
SS
686 s->fullname = NULL;
687 }
688
689 if (s->dirname != NULL)
690 {
691 /* Replace a path entry of $cdir with the compilation directory name */
692#define cdir_len 5
693 /* We cast strstr's result in case an ANSIhole has made it const,
c5aa993b
JM
694 which produces a "required warning" when assigned to a nonconst. */
695 p = (char *) strstr (source_path, "$cdir");
c906108c 696 if (p && (p == path || p[-1] == DIRNAME_SEPARATOR)
c5aa993b 697 && (p[cdir_len] == DIRNAME_SEPARATOR || p[cdir_len] == '\0'))
c906108c
SS
698 {
699 int len;
700
701 path = (char *)
702 alloca (strlen (source_path) + 1 + strlen (s->dirname) + 1);
703 len = p - source_path;
c5aa993b
JM
704 strncpy (path, source_path, len); /* Before $cdir */
705 strcpy (path + len, s->dirname); /* new stuff */
706 strcat (path + len, source_path + len + cdir_len); /* After $cdir */
c906108c
SS
707 }
708 }
709
710 result = openp (path, 0, s->filename, OPEN_MODE, 0, &s->fullname);
711 if (result < 0)
712 {
713 /* Didn't work. Try using just the basename. */
31889e00 714 p = lbasename (s->filename);
c906108c
SS
715 if (p != s->filename)
716 result = openp (path, 0, p, OPEN_MODE, 0, &s->fullname);
717 }
c906108c
SS
718
719 if (result >= 0)
720 {
721 fullname = s->fullname;
722 s->fullname = mstrsave (s->objfile->md, s->fullname);
b8c9b27d 723 xfree (fullname);
c906108c
SS
724 }
725 return result;
726}
727
728/* Return the path to the source file associated with symtab. Returns NULL
729 if no symtab. */
730
731char *
fba45db2 732symtab_to_filename (struct symtab *s)
c906108c
SS
733{
734 int fd;
735
736 if (!s)
737 return NULL;
738
739 /* If we've seen the file before, just return fullname. */
740
741 if (s->fullname)
742 return s->fullname;
743
744 /* Try opening the file to setup fullname */
745
746 fd = open_source_file (s);
747 if (fd < 0)
748 return s->filename; /* File not found. Just use short name */
749
750 /* Found the file. Cleanup and return the full name */
751
752 close (fd);
753 return s->fullname;
754}
c906108c 755\f
c5aa993b 756
c906108c
SS
757/* Create and initialize the table S->line_charpos that records
758 the positions of the lines in the source file, which is assumed
759 to be open on descriptor DESC.
760 All set S->nlines to the number of such lines. */
761
762void
fba45db2 763find_source_lines (struct symtab *s, int desc)
c906108c
SS
764{
765 struct stat st;
766 register char *data, *p, *end;
767 int nlines = 0;
768 int lines_allocated = 1000;
769 int *line_charpos;
770 long mtime = 0;
771 int size;
772
c5aa993b 773 line_charpos = (int *) xmmalloc (s->objfile->md,
c906108c
SS
774 lines_allocated * sizeof (int));
775 if (fstat (desc, &st) < 0)
776 perror_with_name (s->filename);
777
778 if (s && s->objfile && s->objfile->obfd)
c5aa993b 779 mtime = bfd_get_mtime (s->objfile->obfd);
c906108c 780 else if (exec_bfd)
c5aa993b 781 mtime = bfd_get_mtime (exec_bfd);
c906108c
SS
782
783 if (mtime && mtime < st.st_mtime)
784 {
c5aa993b 785 warning ("Source file is more recent than executable.\n");
c906108c
SS
786 }
787
788#ifdef LSEEK_NOT_LINEAR
789 {
790 char c;
791
792 /* Have to read it byte by byte to find out where the chars live */
793
794 line_charpos[0] = lseek (desc, 0, SEEK_CUR);
795 nlines = 1;
c5aa993b 796 while (myread (desc, &c, 1) > 0)
c906108c 797 {
c5aa993b 798 if (c == '\n')
c906108c 799 {
c5aa993b 800 if (nlines == lines_allocated)
c906108c
SS
801 {
802 lines_allocated *= 2;
803 line_charpos =
c5aa993b 804 (int *) xmrealloc (s->objfile->md, (char *) line_charpos,
c906108c
SS
805 sizeof (int) * lines_allocated);
806 }
807 line_charpos[nlines++] = lseek (desc, 0, SEEK_CUR);
808 }
809 }
810 }
811#else /* lseek linear. */
812 {
813 struct cleanup *old_cleanups;
814
815 /* st_size might be a large type, but we only support source files whose
816 size fits in an int. */
817 size = (int) st.st_size;
818
819 /* Use malloc, not alloca, because this may be pretty large, and we may
820 run into various kinds of limits on stack size. */
821 data = (char *) xmalloc (size);
b8c9b27d 822 old_cleanups = make_cleanup (xfree, data);
c906108c
SS
823
824 /* Reassign `size' to result of read for systems where \r\n -> \n. */
825 size = myread (desc, data, size);
826 if (size < 0)
827 perror_with_name (s->filename);
828 end = data + size;
829 p = data;
830 line_charpos[0] = 0;
831 nlines = 1;
832 while (p != end)
833 {
834 if (*p++ == '\n'
c5aa993b 835 /* A newline at the end does not start a new line. */
c906108c
SS
836 && p != end)
837 {
838 if (nlines == lines_allocated)
839 {
840 lines_allocated *= 2;
841 line_charpos =
c5aa993b 842 (int *) xmrealloc (s->objfile->md, (char *) line_charpos,
c906108c
SS
843 sizeof (int) * lines_allocated);
844 }
845 line_charpos[nlines++] = p - data;
846 }
847 }
848 do_cleanups (old_cleanups);
849 }
850#endif /* lseek linear. */
851 s->nlines = nlines;
852 s->line_charpos =
c5aa993b
JM
853 (int *) xmrealloc (s->objfile->md, (char *) line_charpos,
854 nlines * sizeof (int));
c906108c
SS
855
856}
857
858/* Return the character position of a line LINE in symtab S.
859 Return 0 if anything is invalid. */
860
c5aa993b 861#if 0 /* Currently unused */
c906108c
SS
862
863int
fba45db2 864source_line_charpos (struct symtab *s, int line)
c906108c 865{
c5aa993b
JM
866 if (!s)
867 return 0;
868 if (!s->line_charpos || line <= 0)
869 return 0;
c906108c
SS
870 if (line > s->nlines)
871 line = s->nlines;
872 return s->line_charpos[line - 1];
873}
874
875/* Return the line number of character position POS in symtab S. */
876
877int
fba45db2 878source_charpos_line (register struct symtab *s, register int chr)
c906108c
SS
879{
880 register int line = 0;
881 register int *lnp;
c5aa993b
JM
882
883 if (s == 0 || s->line_charpos == 0)
884 return 0;
c906108c
SS
885 lnp = s->line_charpos;
886 /* Files are usually short, so sequential search is Ok */
c5aa993b 887 while (line < s->nlines && *lnp <= chr)
c906108c
SS
888 {
889 line++;
890 lnp++;
891 }
892 if (line >= s->nlines)
893 line = s->nlines;
894 return line;
895}
896
c5aa993b 897#endif /* 0 */
c906108c 898\f
c5aa993b 899
c906108c
SS
900/* Get full pathname and line number positions for a symtab.
901 Return nonzero if line numbers may have changed.
902 Set *FULLNAME to actual name of the file as found by `openp',
903 or to 0 if the file is not found. */
904
905static int
fba45db2 906get_filename_and_charpos (struct symtab *s, char **fullname)
c906108c
SS
907{
908 register int desc, linenums_changed = 0;
c5aa993b 909
c906108c
SS
910 desc = open_source_file (s);
911 if (desc < 0)
912 {
913 if (fullname)
914 *fullname = NULL;
915 return 0;
c5aa993b 916 }
c906108c
SS
917 if (fullname)
918 *fullname = s->fullname;
c5aa993b
JM
919 if (s->line_charpos == 0)
920 linenums_changed = 1;
921 if (linenums_changed)
922 find_source_lines (s, desc);
c906108c
SS
923 close (desc);
924 return linenums_changed;
925}
926
927/* Print text describing the full name of the source file S
928 and the line number LINE and its corresponding character position.
929 The text starts with two Ctrl-z so that the Emacs-GDB interface
930 can easily find it.
931
932 MID_STATEMENT is nonzero if the PC is not at the beginning of that line.
933
934 Return 1 if successful, 0 if could not find the file. */
935
936int
fba45db2
KB
937identify_source_line (struct symtab *s, int line, int mid_statement,
938 CORE_ADDR pc)
c906108c
SS
939{
940 if (s->line_charpos == 0)
c5aa993b 941 get_filename_and_charpos (s, (char **) NULL);
c906108c
SS
942 if (s->fullname == 0)
943 return 0;
944 if (line > s->nlines)
945 /* Don't index off the end of the line_charpos array. */
946 return 0;
947 annotate_source (s->fullname, line, s->line_charpos[line - 1],
948 mid_statement, pc);
949
950 current_source_line = line;
951 first_line_listed = line;
952 last_line_listed = line;
953 current_source_symtab = s;
954 return 1;
955}
c906108c 956\f
c5aa993b 957
c906108c
SS
958/* Print source lines from the file of symtab S,
959 starting with line number LINE and stopping before line number STOPLINE. */
960
a14ed312
KB
961static void print_source_lines_base (struct symtab *s, int line, int stopline,
962 int noerror);
c906108c 963static void
fba45db2 964print_source_lines_base (struct symtab *s, int line, int stopline, int noerror)
c906108c
SS
965{
966 register int c;
967 register int desc;
968 register FILE *stream;
969 int nlines = stopline - line;
970
971 /* Regardless of whether we can open the file, set current_source_symtab. */
972 current_source_symtab = s;
973 current_source_line = line;
974 first_line_listed = line;
975
8b93c638
JM
976 /* If printing of source lines is disabled, just print file and line number */
977 if (ui_out_test_flags (uiout, ui_source_list))
978 {
c5aa993b
JM
979 /* Only prints "No such file or directory" once */
980 if ((s != last_source_visited) || (!last_source_error))
981 {
982 last_source_visited = s;
983 desc = open_source_file (s);
984 }
985 else
986 {
987 desc = last_source_error;
988 noerror = 1;
989 }
8b93c638
JM
990 }
991 else
992 {
993 desc = -1;
994 noerror = 1;
995 }
c906108c
SS
996
997 if (desc < 0)
998 {
999 last_source_error = desc;
1000
c5aa993b
JM
1001 if (!noerror)
1002 {
c906108c
SS
1003 char *name = alloca (strlen (s->filename) + 100);
1004 sprintf (name, "%d\t%s", line, s->filename);
1005 print_sys_errmsg (name, errno);
1006 }
1007 else
8b93c638
JM
1008 ui_out_field_int (uiout, "line", line);
1009 ui_out_text (uiout, "\tin ");
1010 ui_out_field_string (uiout, "file", s->filename);
1011 ui_out_text (uiout, "\n");
c906108c
SS
1012
1013 return;
1014 }
1015
1016 last_source_error = 0;
1017
1018 if (s->line_charpos == 0)
1019 find_source_lines (s, desc);
1020
1021 if (line < 1 || line > s->nlines)
1022 {
1023 close (desc);
1024 error ("Line number %d out of range; %s has %d lines.",
1025 line, s->filename, s->nlines);
1026 }
1027
1028 if (lseek (desc, s->line_charpos[line - 1], 0) < 0)
1029 {
1030 close (desc);
1031 perror_with_name (s->filename);
1032 }
1033
1034 stream = fdopen (desc, FDOPEN_MODE);
1035 clearerr (stream);
1036
1037 while (nlines-- > 0)
1038 {
8b93c638
JM
1039 char buf[20];
1040
1041 c = fgetc (stream);
1042 if (c == EOF)
1043 break;
1044 last_line_listed = current_source_line;
1045 sprintf (buf, "%d\t", current_source_line++);
1046 ui_out_text (uiout, buf);
1047 do
1048 {
1049 if (c < 040 && c != '\t' && c != '\n' && c != '\r')
1050 {
1051 sprintf (buf, "^%c", c + 0100);
1052 ui_out_text (uiout, buf);
1053 }
1054 else if (c == 0177)
1055 ui_out_text (uiout, "^?");
1056#ifdef CRLF_SOURCE_FILES
1057 else if (c == '\r')
1058 {
1059 /* Skip a \r character, but only before a \n. */
1060 int c1 = fgetc (stream);
1061
1062 if (c1 != '\n')
1063 printf_filtered ("^%c", c + 0100);
1064 if (c1 != EOF)
1065 ungetc (c1, stream);
1066 }
1067#endif
1068 else
1069 {
1070 sprintf (buf, "%c", c);
1071 ui_out_text (uiout, buf);
1072 }
1073 }
1074 while (c != '\n' && (c = fgetc (stream)) >= 0);
c906108c
SS
1075 }
1076
1077 fclose (stream);
1078}
1079\f
1080/* Show source lines from the file of symtab S, starting with line
1081 number LINE and stopping before line number STOPLINE. If this is the
1082 not the command line version, then the source is shown in the source
1083 window otherwise it is simply printed */
1084
c5aa993b 1085void
fba45db2 1086print_source_lines (struct symtab *s, int line, int stopline, int noerror)
c906108c 1087{
c5aa993b 1088 print_source_lines_base (s, line, stopline, noerror);
c906108c
SS
1089}
1090\f
1091
1092
1093/* Print a list of files and line numbers which a user may choose from
c5aa993b
JM
1094 in order to list a function which was specified ambiguously (as with
1095 `list classname::overloadedfuncname', for example). The vector in
1096 SALS provides the filenames and line numbers. */
c906108c
SS
1097
1098static void
fba45db2 1099ambiguous_line_spec (struct symtabs_and_lines *sals)
c906108c
SS
1100{
1101 int i;
1102
1103 for (i = 0; i < sals->nelts; ++i)
c5aa993b
JM
1104 printf_filtered ("file: \"%s\", line number: %d\n",
1105 sals->sals[i].symtab->filename, sals->sals[i].line);
c906108c
SS
1106}
1107
1108static void
fba45db2 1109list_command (char *arg, int from_tty)
c906108c
SS
1110{
1111 struct symtabs_and_lines sals, sals_end;
1112 struct symtab_and_line sal, sal_end;
1113 struct symbol *sym;
1114 char *arg1;
1115 int no_end = 1;
1116 int dummy_end = 0;
1117 int dummy_beg = 0;
1118 int linenum_beg = 0;
1119 char *p;
1120
c5aa993b 1121 if (!have_full_symbols () && !have_partial_symbols ())
c906108c
SS
1122 error ("No symbol table is loaded. Use the \"file\" command.");
1123
1124 /* Pull in a current source symtab if necessary */
1125 if (current_source_symtab == 0 &&
1126 (arg == 0 || arg[0] == '+' || arg[0] == '-'))
1127 select_source_symtab (0);
1128
1129 /* "l" or "l +" lists next ten lines. */
1130
1131 if (arg == 0 || STREQ (arg, "+"))
1132 {
1133 if (current_source_symtab == 0)
1134 error ("No default source file yet. Do \"help list\".");
1135 print_source_lines (current_source_symtab, current_source_line,
1136 current_source_line + lines_to_list, 0);
1137 return;
1138 }
1139
1140 /* "l -" lists previous ten lines, the ones before the ten just listed. */
1141 if (STREQ (arg, "-"))
1142 {
1143 if (current_source_symtab == 0)
1144 error ("No default source file yet. Do \"help list\".");
1145 print_source_lines (current_source_symtab,
1146 max (first_line_listed - lines_to_list, 1),
1147 first_line_listed, 0);
1148 return;
1149 }
1150
1151 /* Now if there is only one argument, decode it in SAL
1152 and set NO_END.
1153 If there are two arguments, decode them in SAL and SAL_END
1154 and clear NO_END; however, if one of the arguments is blank,
1155 set DUMMY_BEG or DUMMY_END to record that fact. */
1156
1157 arg1 = arg;
1158 if (*arg1 == ',')
1159 dummy_beg = 1;
1160 else
1161 {
1162 sals = decode_line_1 (&arg1, 0, 0, 0, 0);
1163
c5aa993b
JM
1164 if (!sals.nelts)
1165 return; /* C++ */
c906108c
SS
1166 if (sals.nelts > 1)
1167 {
1168 ambiguous_line_spec (&sals);
b8c9b27d 1169 xfree (sals.sals);
c906108c
SS
1170 return;
1171 }
1172
1173 sal = sals.sals[0];
b8c9b27d 1174 xfree (sals.sals);
c906108c
SS
1175 }
1176
1177 /* Record whether the BEG arg is all digits. */
1178
1179 for (p = arg; p != arg1 && *p >= '0' && *p <= '9'; p++);
1180 linenum_beg = (p == arg1);
1181
1182 while (*arg1 == ' ' || *arg1 == '\t')
1183 arg1++;
1184 if (*arg1 == ',')
1185 {
1186 no_end = 0;
1187 arg1++;
1188 while (*arg1 == ' ' || *arg1 == '\t')
1189 arg1++;
1190 if (*arg1 == 0)
1191 dummy_end = 1;
1192 else
1193 {
1194 if (dummy_beg)
1195 sals_end = decode_line_1 (&arg1, 0, 0, 0, 0);
1196 else
1197 sals_end = decode_line_1 (&arg1, 0, sal.symtab, sal.line, 0);
c5aa993b 1198 if (sals_end.nelts == 0)
c906108c
SS
1199 return;
1200 if (sals_end.nelts > 1)
1201 {
1202 ambiguous_line_spec (&sals_end);
b8c9b27d 1203 xfree (sals_end.sals);
c906108c
SS
1204 return;
1205 }
1206 sal_end = sals_end.sals[0];
b8c9b27d 1207 xfree (sals_end.sals);
c906108c
SS
1208 }
1209 }
1210
1211 if (*arg1)
1212 error ("Junk at end of line specification.");
1213
1214 if (!no_end && !dummy_beg && !dummy_end
1215 && sal.symtab != sal_end.symtab)
1216 error ("Specified start and end are in different files.");
1217 if (dummy_beg && dummy_end)
1218 error ("Two empty args do not say what lines to list.");
c5aa993b 1219
c906108c
SS
1220 /* if line was specified by address,
1221 first print exactly which line, and which file.
1222 In this case, sal.symtab == 0 means address is outside
1223 of all known source files, not that user failed to give a filename. */
1224 if (*arg == '*')
1225 {
1226 if (sal.symtab == 0)
1227 /* FIXME-32x64--assumes sal.pc fits in long. */
1228 error ("No source file for address %s.",
c5aa993b 1229 local_hex_string ((unsigned long) sal.pc));
c906108c
SS
1230 sym = find_pc_function (sal.pc);
1231 if (sym)
1232 {
1233 print_address_numeric (sal.pc, 1, gdb_stdout);
1234 printf_filtered (" is in ");
1235 fputs_filtered (SYMBOL_SOURCE_NAME (sym), gdb_stdout);
1236 printf_filtered (" (%s:%d).\n", sal.symtab->filename, sal.line);
1237 }
1238 else
1239 {
1240 print_address_numeric (sal.pc, 1, gdb_stdout);
1241 printf_filtered (" is at %s:%d.\n",
1242 sal.symtab->filename, sal.line);
1243 }
1244 }
1245
1246 /* If line was not specified by just a line number,
1247 and it does not imply a symtab, it must be an undebuggable symbol
1248 which means no source code. */
1249
c5aa993b 1250 if (!linenum_beg && sal.symtab == 0)
c906108c
SS
1251 error ("No line number known for %s.", arg);
1252
1253 /* If this command is repeated with RET,
1254 turn it into the no-arg variant. */
1255
1256 if (from_tty)
1257 *arg = 0;
1258
1259 if (dummy_beg && sal_end.symtab == 0)
1260 error ("No default source file yet. Do \"help list\".");
1261 if (dummy_beg)
1262 print_source_lines (sal_end.symtab,
1263 max (sal_end.line - (lines_to_list - 1), 1),
1264 sal_end.line + 1, 0);
1265 else if (sal.symtab == 0)
1266 error ("No default source file yet. Do \"help list\".");
1267 else if (no_end)
7a292a7a 1268 {
d4f3574e
SS
1269 int first_line = sal.line - lines_to_list / 2;
1270
1271 if (first_line < 1) first_line = 1;
1272
1273 print_source_lines (sal.symtab, first_line, first_line + lines_to_list,
1274 0);
7a292a7a 1275 }
c906108c
SS
1276 else
1277 print_source_lines (sal.symtab, sal.line,
1278 (dummy_end
1279 ? sal.line + lines_to_list
1280 : sal_end.line + 1),
1281 0);
1282}
1283\f
1284/* Print info on range of pc's in a specified line. */
1285
1286static void
fba45db2 1287line_info (char *arg, int from_tty)
c906108c
SS
1288{
1289 struct symtabs_and_lines sals;
1290 struct symtab_and_line sal;
1291 CORE_ADDR start_pc, end_pc;
1292 int i;
1293
c5aa993b 1294 INIT_SAL (&sal); /* initialize to zeroes */
c906108c
SS
1295
1296 if (arg == 0)
1297 {
1298 sal.symtab = current_source_symtab;
1299 sal.line = last_line_listed;
1300 sals.nelts = 1;
1301 sals.sals = (struct symtab_and_line *)
1302 xmalloc (sizeof (struct symtab_and_line));
1303 sals.sals[0] = sal;
1304 }
1305 else
1306 {
1307 sals = decode_line_spec_1 (arg, 0);
c5aa993b 1308
c906108c
SS
1309 dont_repeat ();
1310 }
1311
1312 /* C++ More than one line may have been specified, as when the user
1313 specifies an overloaded function name. Print info on them all. */
1314 for (i = 0; i < sals.nelts; i++)
1315 {
1316 sal = sals.sals[i];
c5aa993b 1317
c906108c
SS
1318 if (sal.symtab == 0)
1319 {
1320 printf_filtered ("No line number information available");
1321 if (sal.pc != 0)
1322 {
1323 /* This is useful for "info line *0x7f34". If we can't tell the
c5aa993b
JM
1324 user about a source line, at least let them have the symbolic
1325 address. */
c906108c
SS
1326 printf_filtered (" for address ");
1327 wrap_here (" ");
1328 print_address (sal.pc, gdb_stdout);
1329 }
1330 else
1331 printf_filtered (".");
1332 printf_filtered ("\n");
1333 }
1334 else if (sal.line > 0
1335 && find_line_pc_range (sal, &start_pc, &end_pc))
1336 {
1337 if (start_pc == end_pc)
1338 {
1339 printf_filtered ("Line %d of \"%s\"",
1340 sal.line, sal.symtab->filename);
1341 wrap_here (" ");
1342 printf_filtered (" is at address ");
1343 print_address (start_pc, gdb_stdout);
1344 wrap_here (" ");
1345 printf_filtered (" but contains no code.\n");
1346 }
1347 else
1348 {
1349 printf_filtered ("Line %d of \"%s\"",
1350 sal.line, sal.symtab->filename);
1351 wrap_here (" ");
1352 printf_filtered (" starts at address ");
1353 print_address (start_pc, gdb_stdout);
1354 wrap_here (" ");
1355 printf_filtered (" and ends at ");
1356 print_address (end_pc, gdb_stdout);
1357 printf_filtered (".\n");
1358 }
1359
1360 /* x/i should display this line's code. */
1361 set_next_address (start_pc);
1362
1363 /* Repeating "info line" should do the following line. */
1364 last_line_listed = sal.line + 1;
1365
1366 /* If this is the only line, show the source code. If it could
1367 not find the file, don't do anything special. */
1368 if (annotation_level && sals.nelts == 1)
1369 identify_source_line (sal.symtab, sal.line, 0, start_pc);
1370 }
1371 else
1372 /* Is there any case in which we get here, and have an address
1373 which the user would want to see? If we have debugging symbols
1374 and no line numbers? */
1375 printf_filtered ("Line number %d is out of range for \"%s\".\n",
1376 sal.line, sal.symtab->filename);
1377 }
b8c9b27d 1378 xfree (sals.sals);
c906108c
SS
1379}
1380\f
1381/* Commands to search the source file for a regexp. */
1382
1383/* ARGSUSED */
1384static void
fba45db2 1385forward_search_command (char *regex, int from_tty)
c906108c
SS
1386{
1387 register int c;
1388 register int desc;
1389 register FILE *stream;
1390 int line;
1391 char *msg;
1392
c906108c 1393 line = last_line_listed + 1;
c906108c
SS
1394
1395 msg = (char *) re_comp (regex);
1396 if (msg)
1397 error (msg);
1398
1399 if (current_source_symtab == 0)
1400 select_source_symtab (0);
1401
1402 desc = open_source_file (current_source_symtab);
1403 if (desc < 0)
1404 perror_with_name (current_source_symtab->filename);
1405
1406 if (current_source_symtab->line_charpos == 0)
1407 find_source_lines (current_source_symtab, desc);
1408
1409 if (line < 1 || line > current_source_symtab->nlines)
1410 {
1411 close (desc);
1412 error ("Expression not found");
1413 }
1414
1415 if (lseek (desc, current_source_symtab->line_charpos[line - 1], 0) < 0)
1416 {
1417 close (desc);
1418 perror_with_name (current_source_symtab->filename);
1419 }
1420
1421 stream = fdopen (desc, FDOPEN_MODE);
1422 clearerr (stream);
c5aa993b
JM
1423 while (1)
1424 {
1425 static char *buf = NULL;
1426 register char *p;
1427 int cursize, newsize;
1428
1429 cursize = 256;
1430 buf = xmalloc (cursize);
1431 p = buf;
1432
1433 c = getc (stream);
1434 if (c == EOF)
1435 break;
1436 do
c906108c 1437 {
c5aa993b
JM
1438 *p++ = c;
1439 if (p - buf == cursize)
1440 {
1441 newsize = cursize + cursize / 2;
1442 buf = xrealloc (buf, newsize);
1443 p = buf + cursize;
1444 cursize = newsize;
1445 }
c906108c 1446 }
c5aa993b 1447 while (c != '\n' && (c = getc (stream)) >= 0);
c906108c 1448
7be570e7
JM
1449#ifdef CRLF_SOURCE_FILES
1450 /* Remove the \r, if any, at the end of the line, otherwise
1451 regular expressions that end with $ or \n won't work. */
1452 if (p - buf > 1 && p[-2] == '\r')
1453 {
1454 p--;
1455 p[-1] = '\n';
1456 }
1457#endif
1458
c5aa993b
JM
1459 /* we now have a source line in buf, null terminate and match */
1460 *p = 0;
1461 if (re_exec (buf) > 0)
1462 {
1463 /* Match! */
1464 fclose (stream);
c5aa993b
JM
1465 print_source_lines (current_source_symtab, line, line + 1, 0);
1466 set_internalvar (lookup_internalvar ("_"),
1467 value_from_longest (builtin_type_int,
1468 (LONGEST) line));
1469 current_source_line = max (line - lines_to_list / 2, 1);
1470 return;
1471 }
1472 line++;
1473 }
c906108c
SS
1474
1475 printf_filtered ("Expression not found\n");
1476 fclose (stream);
1477}
1478
1479/* ARGSUSED */
1480static void
fba45db2 1481reverse_search_command (char *regex, int from_tty)
c906108c
SS
1482{
1483 register int c;
1484 register int desc;
1485 register FILE *stream;
1486 int line;
1487 char *msg;
063190b6 1488
c906108c 1489 line = last_line_listed - 1;
c906108c
SS
1490
1491 msg = (char *) re_comp (regex);
1492 if (msg)
1493 error (msg);
1494
1495 if (current_source_symtab == 0)
1496 select_source_symtab (0);
1497
1498 desc = open_source_file (current_source_symtab);
1499 if (desc < 0)
1500 perror_with_name (current_source_symtab->filename);
1501
1502 if (current_source_symtab->line_charpos == 0)
1503 find_source_lines (current_source_symtab, desc);
1504
1505 if (line < 1 || line > current_source_symtab->nlines)
1506 {
1507 close (desc);
1508 error ("Expression not found");
1509 }
1510
1511 if (lseek (desc, current_source_symtab->line_charpos[line - 1], 0) < 0)
1512 {
1513 close (desc);
1514 perror_with_name (current_source_symtab->filename);
1515 }
1516
1517 stream = fdopen (desc, FDOPEN_MODE);
1518 clearerr (stream);
1519 while (line > 1)
1520 {
1521/* FIXME!!! We walk right off the end of buf if we get a long line!!! */
1522 char buf[4096]; /* Should be reasonable??? */
1523 register char *p = buf;
1524
1525 c = getc (stream);
1526 if (c == EOF)
1527 break;
c5aa993b
JM
1528 do
1529 {
1530 *p++ = c;
1531 }
1532 while (c != '\n' && (c = getc (stream)) >= 0);
c906108c 1533
7be570e7
JM
1534#ifdef CRLF_SOURCE_FILES
1535 /* Remove the \r, if any, at the end of the line, otherwise
1536 regular expressions that end with $ or \n won't work. */
1537 if (p - buf > 1 && p[-2] == '\r')
1538 {
1539 p--;
1540 p[-1] = '\n';
1541 }
1542#endif
1543
c906108c
SS
1544 /* We now have a source line in buf; null terminate and match. */
1545 *p = 0;
1546 if (re_exec (buf) > 0)
1547 {
1548 /* Match! */
1549 fclose (stream);
c5aa993b 1550 print_source_lines (current_source_symtab, line, line + 1, 0);
c906108c
SS
1551 set_internalvar (lookup_internalvar ("_"),
1552 value_from_longest (builtin_type_int,
1553 (LONGEST) line));
1554 current_source_line = max (line - lines_to_list / 2, 1);
1555 return;
1556 }
1557 line--;
1558 if (fseek (stream, current_source_symtab->line_charpos[line - 1], 0) < 0)
1559 {
1560 fclose (stream);
1561 perror_with_name (current_source_symtab->filename);
1562 }
1563 }
1564
1565 printf_filtered ("Expression not found\n");
1566 fclose (stream);
1567 return;
1568}
1569\f
1570void
fba45db2 1571_initialize_source (void)
c906108c
SS
1572{
1573 struct cmd_list_element *c;
1574 current_source_symtab = 0;
1575 init_source_path ();
1576
1577 /* The intention is to use POSIX Basic Regular Expressions.
1578 Always use the GNU regex routine for consistency across all hosts.
1579 Our current GNU regex.c does not have all the POSIX features, so this is
1580 just an approximation. */
1581 re_set_syntax (RE_SYNTAX_GREP);
1582
1583 c = add_cmd ("directory", class_files, directory_command,
c5aa993b 1584 "Add directory DIR to beginning of search path for source files.\n\
c906108c
SS
1585Forget cached info on source file locations and line positions.\n\
1586DIR can also be $cwd for the current working directory, or $cdir for the\n\
1587directory in which the source file was compiled into object code.\n\
1588With no argument, reset the search path to $cdir:$cwd, the default.",
1589 &cmdlist);
1590
1591 if (dbx_commands)
c5aa993b 1592 add_com_alias ("use", "directory", class_files, 0);
c906108c 1593
5ba2abeb 1594 set_cmd_completer (c, filename_completer);
c906108c
SS
1595
1596 add_cmd ("directories", no_class, show_directories,
1597 "Current search path for finding source files.\n\
1598$cwd in the path means the current working directory.\n\
1599$cdir in the path means the compilation directory of the source file.",
1600 &showlist);
1601
1602 if (xdb_commands)
1603 {
c5aa993b 1604 add_com_alias ("D", "directory", class_files, 0);
c906108c 1605 add_cmd ("ld", no_class, show_directories,
c5aa993b 1606 "Current search path for finding source files.\n\
c906108c
SS
1607$cwd in the path means the current working directory.\n\
1608$cdir in the path means the compilation directory of the source file.",
c5aa993b 1609 &cmdlist);
c906108c
SS
1610 }
1611
1612 add_info ("source", source_info,
1613 "Information about the current source file.");
1614
1615 add_info ("line", line_info,
1616 concat ("Core addresses of the code for a source line.\n\
1617Line can be specified as\n\
1618 LINENUM, to list around that line in current file,\n\
1619 FILE:LINENUM, to list around that line in that file,\n\
1620 FUNCTION, to list around beginning of that function,\n\
1621 FILE:FUNCTION, to distinguish among like-named static functions.\n\
1622", "\
1623Default is to describe the last source line that was listed.\n\n\
1624This sets the default address for \"x\" to the line's first instruction\n\
1625so that \"x/i\" suffices to start examining the machine code.\n\
1626The address is also stored as the value of \"$_\".", NULL));
1627
1628 add_com ("forward-search", class_files, forward_search_command,
1629 "Search for regular expression (see regex(3)) from last line listed.\n\
1630The matching line number is also stored as the value of \"$_\".");
1631 add_com_alias ("search", "forward-search", class_files, 0);
1632
1633 add_com ("reverse-search", class_files, reverse_search_command,
1634 "Search backward for regular expression (see regex(3)) from last line listed.\n\
1635The matching line number is also stored as the value of \"$_\".");
1636
1637 if (xdb_commands)
1638 {
c5aa993b
JM
1639 add_com_alias ("/", "forward-search", class_files, 0);
1640 add_com_alias ("?", "reverse-search", class_files, 0);
c906108c
SS
1641 }
1642
1643 add_com ("list", class_files, list_command,
1644 concat ("List specified function or line.\n\
1645With no argument, lists ten more lines after or around previous listing.\n\
1646\"list -\" lists the ten lines before a previous ten-line listing.\n\
1647One argument specifies a line, and ten lines are listed around that line.\n\
1648Two arguments with comma between specify starting and ending lines to list.\n\
1649", "\
1650Lines can be specified in these ways:\n\
1651 LINENUM, to list around that line in current file,\n\
1652 FILE:LINENUM, to list around that line in that file,\n\
1653 FUNCTION, to list around beginning of that function,\n\
1654 FILE:FUNCTION, to distinguish among like-named static functions.\n\
1655 *ADDRESS, to list around the line containing that address.\n\
1656With two args if one is empty it stands for ten lines away from the other arg.", NULL));
1657
1658 if (!xdb_commands)
1659 add_com_alias ("l", "list", class_files, 1);
1660 else
1661 add_com_alias ("v", "list", class_files, 1);
1662
1663 if (dbx_commands)
1664 add_com_alias ("file", "list", class_files, 1);
1665
1666 add_show_from_set
1667 (add_set_cmd ("listsize", class_support, var_uinteger,
c5aa993b
JM
1668 (char *) &lines_to_list,
1669 "Set number of source lines gdb will list by default.",
c906108c
SS
1670 &setlist),
1671 &showlist);
1672}
This page took 0.297047 seconds and 4 git commands to generate.