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