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