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