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