]> Git Repo - binutils.git/blob - gdb/source.c
add go32 tools to the go32 targetted toolchains
[binutils.git] / gdb / source.c
1 /* List lines of source files for GDB, the GNU debugger.
2    Copyright (C) 1986, 1987, 1988, 1989, 1991 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., 675 Mass Ave, Cambridge, MA 02139, USA.  */
19
20 #include "defs.h"
21 #include "symtab.h"
22 #include "expression.h"
23 #include "language.h"
24 #include "command.h"
25 #include "gdbcmd.h"
26 #include "frame.h"
27
28 #ifdef USG
29 #include <sys/types.h>
30 #endif
31
32 #include <string.h>
33 #include <sys/param.h>
34 #include <sys/stat.h>
35 #include <fcntl.h>
36 #include "gdbcore.h"
37 #include "regex.h"
38 #include "symfile.h"
39 #include "objfiles.h"
40
41 /* Prototypes for local functions. */
42
43 static int
44 open_source_file PARAMS ((struct symtab *));
45
46 static int
47 get_filename_and_charpos PARAMS ((struct symtab *, char **));
48
49 static void
50 reverse_search_command PARAMS ((char *, int));
51
52 static void
53 forward_search_command PARAMS ((char *, int));
54
55 static void
56 line_info PARAMS ((char *, int));
57
58 static void
59 list_command PARAMS ((char *, int));
60
61 static void
62 ambiguous_line_spec PARAMS ((struct symtabs_and_lines *));
63
64 static void
65 source_info PARAMS ((char *, int));
66
67 static void
68 show_directories PARAMS ((char *, int));
69
70 static void
71 find_source_lines PARAMS ((struct symtab *, int));
72
73 /* If we use this declaration, it breaks because of fucking ANSI "const" stuff
74    on some systems.  We just have to not declare it at all, have it default
75    to int, and possibly botch on a few systems.  Thanks, ANSIholes... */
76 /* extern char *strstr(); */
77
78 /* Path of directories to search for source files.
79    Same format as the PATH environment variable's value.  */
80
81 char *source_path;
82
83 /* Symtab of default file for listing lines of.  */
84
85 struct symtab *current_source_symtab;
86
87 /* Default next line to list.  */
88
89 int current_source_line;
90
91 /* Default number of lines to print with commands like "list".
92    This is based on guessing how many long (i.e. more than chars_per_line
93    characters) lines there will be.  To be completely correct, "list"
94    and friends should be rewritten to count characters and see where
95    things are wrapping, but that would be a fair amount of work.  */
96
97 int lines_to_list = 10;
98
99 /* Line number of last line printed.  Default for various commands.
100    current_source_line is usually, but not always, the same as this.  */
101
102 static int last_line_listed;
103
104 /* First line number listed by last listing command.  */
105
106 static int first_line_listed;
107
108 \f
109 /* Set the source file default for the "list" command to be S.
110
111    If S is NULL, and we don't have a default, find one.  This
112    should only be called when the user actually tries to use the
113    default, since we produce an error if we can't find a reasonable
114    default.  Also, since this can cause symbols to be read, doing it
115    before we need to would make things slower than necessary.  */
116
117 void
118 select_source_symtab (s)
119      register struct symtab *s;
120 {
121   struct symtabs_and_lines sals;
122   struct symtab_and_line sal;
123   struct partial_symtab *ps;
124   struct partial_symtab *cs_pst = 0;
125   struct objfile *ofp;
126   
127   if (s)
128     {
129       current_source_symtab = s;
130       current_source_line = 1;
131       return;
132     }
133
134   if (current_source_symtab)
135     return;
136
137   /* Make the default place to list be the function `main'
138      if one exists.  */
139   if (lookup_symbol ("main", 0, VAR_NAMESPACE, 0, NULL))
140     {
141       sals = decode_line_spec ("main", 1);
142       sal = sals.sals[0];
143       free (sals.sals);
144       current_source_symtab = sal.symtab;
145       current_source_line = max (sal.line - (lines_to_list - 1), 1);
146       if (current_source_symtab)
147         return;
148     }
149   
150   /* All right; find the last file in the symtab list (ignoring .h's).  */
151
152   current_source_line = 1;
153
154   for (ofp = object_files; ofp != NULL; ofp = ofp -> next)
155     {
156       for (s = ofp -> symtabs; s; s = s->next)
157         {
158           char *name = s -> filename;
159           int len = strlen (name);
160           if (! (len > 2 && (STREQ (&name[len - 2], ".h"))))
161             {
162               current_source_symtab = s;
163             }
164         }
165     }
166   if (current_source_symtab)
167     return;
168
169   /* Howabout the partial symbol tables? */
170
171   for (ofp = object_files; ofp != NULL; ofp = ofp -> next)
172     {
173       for (ps = ofp -> psymtabs; ps != NULL; ps = ps -> next)
174         {
175           char *name = ps -> filename;
176           int len = strlen (name);
177           if (! (len > 2 && (STREQ (&name[len - 2], ".h"))))
178             {
179               cs_pst = ps;
180             }
181         }
182     }
183   if (cs_pst)
184     {
185       if (cs_pst -> readin)
186         {
187           fatal ("Internal: select_source_symtab: readin pst found and no symtabs.");
188         }
189       else
190         {
191           current_source_symtab = PSYMTAB_TO_SYMTAB (cs_pst);
192         }
193     }
194
195   error ("Can't find a default source file");
196 }
197 \f
198 static void
199 show_directories (ignore, from_tty)
200      char *ignore;
201      int from_tty;
202 {
203   puts_filtered ("Source directories searched: ");
204   puts_filtered (source_path);
205   puts_filtered ("\n");
206 }
207
208 /* Forget what we learned about line positions in source files,
209    and which directories contain them;
210    must check again now since files may be found in
211    a different directory now.  */
212
213 void
214 forget_cached_source_info ()
215 {
216   register struct symtab *s;
217   register struct objfile *objfile;
218
219   for (objfile = object_files; objfile != NULL; objfile = objfile -> next)
220     {
221       for (s = objfile -> symtabs; s != NULL; s = s -> next)
222         {
223           if (s -> line_charpos != NULL)
224             {
225               mfree (objfile -> md, s -> line_charpos);
226               s -> line_charpos = NULL;
227             }
228           if (s -> fullname != NULL)
229             {
230               mfree (objfile -> md, s -> fullname);
231               s -> fullname = NULL;
232             }
233         }
234     }
235 }
236
237 void
238 init_source_path ()
239 {
240   source_path = savestring ("$cdir:$cwd", /* strlen of it */ 10);
241   forget_cached_source_info ();
242 }
243
244 /* Add zero or more directories to the front of the source path.  */
245  
246 void
247 directory_command (dirname, from_tty)
248      char *dirname;
249      int from_tty;
250 {
251   dont_repeat ();
252   /* FIXME, this goes to "delete dir"... */
253   if (dirname == 0)
254     {
255       if (query ("Reinitialize source path to empty? ", ""))
256         {
257           free (source_path);
258           init_source_path ();
259         }
260     }
261   else
262     mod_path (dirname, &source_path);
263   if (from_tty)
264     show_directories ((char *)0, from_tty);
265   forget_cached_source_info ();
266 }
267
268 /* Add zero or more directories to the front of an arbitrary path.  */
269
270 void
271 mod_path (dirname, which_path)
272      char *dirname;
273      char **which_path;
274 {
275   char *old = *which_path;
276   int prefix = 0;
277
278   if (dirname == 0)
279     return;
280
281   dirname = strsave (dirname);
282   make_cleanup (free, dirname);
283
284   do
285     {
286       char *name = dirname;
287       register char *p;
288       struct stat st;
289
290       {
291         char *colon = strchr (name, ':');
292         char *space = strchr (name, ' ');
293         char *tab = strchr (name, '\t');
294         if (colon == 0 && space == 0 && tab ==  0)
295           p = dirname = name + strlen (name);
296         else
297           {
298             p = 0;
299             if (colon != 0 && (p == 0 || colon < p))
300               p = colon;
301             if (space != 0 && (p == 0 || space < p))
302               p = space;
303             if (tab != 0 && (p == 0 || tab < p))
304               p = tab;
305             dirname = p + 1;
306             while (*dirname == ':' || *dirname == ' ' || *dirname == '\t')
307               ++dirname;
308           }
309       }
310
311       if (p[-1] == '/')
312         /* Sigh. "foo/" => "foo" */
313         --p;
314       *p = '\0';
315
316       while (p[-1] == '.')
317         {
318           if (p - name == 1)
319             {
320               /* "." => getwd ().  */
321               name = current_directory;
322               goto append;
323             }
324           else if (p[-2] == '/')
325             {
326               if (p - name == 2)
327                 {
328                   /* "/." => "/".  */
329                   *--p = '\0';
330                   goto append;
331                 }
332               else
333                 {
334                   /* "...foo/." => "...foo".  */
335                   p -= 2;
336                   *p = '\0';
337                   continue;
338                 }
339             }
340           else
341             break;
342         }
343
344       if (name[0] == '~')
345         name = tilde_expand (name);
346       else if (name[0] != '/' && name[0] != '$')
347         name = concat (current_directory, "/", name, NULL);
348       else
349         name = savestring (name, p - name);
350       make_cleanup (free, name);
351
352       /* Unless it's a variable, check existence.  */
353       if (name[0] != '$') {
354         if (stat (name, &st) < 0)
355           perror_with_name (name);
356         if ((st.st_mode & S_IFMT) != S_IFDIR)
357           error ("%s is not a directory.", name);
358       }
359
360     append:
361       {
362         register unsigned int len = strlen (name);
363
364         p = *which_path;
365         while (1)
366           {
367             if (!strncmp (p, name, len)
368                 && (p[len] == '\0' || p[len] == ':'))
369               {
370                 /* Found it in the search path, remove old copy */
371                 if (p > *which_path)
372                   p--;                  /* Back over leading colon */
373                 if (prefix > p - *which_path)
374                   goto skip_dup;        /* Same dir twice in one cmd */
375                 strcpy (p, &p[len+1]);  /* Copy from next \0 or  : */
376               }
377             p = strchr (p, ':');
378             if (p != 0)
379               ++p;
380             else
381               break;
382           }
383         if (p == 0)
384           {
385             /* 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.  */
386             if (prefix)
387               {
388                 char *temp, c;
389
390                 c = old[prefix];
391                 old[prefix] = '\0';
392                 temp = concat (old, ":", name, NULL);
393                 old[prefix] = c;
394                 *which_path = concat (temp, "", &old[prefix], NULL);
395                 prefix = strlen (temp);
396                 free (temp);
397               }
398             else
399               {
400                 *which_path = concat (name, (old[0]? ":" : old), old, NULL);
401                 prefix = strlen (name);
402               }
403             free (old);
404             old = *which_path;
405           }
406       }
407   skip_dup: ;
408     } while (*dirname != '\0');
409 }
410
411
412 static void
413 source_info (ignore, from_tty)
414      char *ignore;
415      int from_tty;
416 {
417   register struct symtab *s = current_source_symtab;
418
419   if (!s)
420     {
421       printf_filtered("No current source file.\n");
422       return;
423     }
424   printf_filtered ("Current source file is %s\n", s->filename);
425   if (s->dirname)
426     printf_filtered ("Compilation directory is %s\n", s->dirname);
427   if (s->fullname)
428     printf_filtered ("Located in %s\n", s->fullname);
429   if (s->nlines)
430     printf_filtered ("Contains %d line%s.\n", s->nlines,
431                      s->nlines == 1 ? "" : "s");
432
433   printf_filtered("Source language is %s.\n", language_str (s->language));
434 }
435
436
437 \f
438 /* Open a file named STRING, searching path PATH (dir names sep by colons)
439    using mode MODE and protection bits PROT in the calls to open.
440
441    If TRY_CWD_FIRST, try to open ./STRING before searching PATH.
442    (ie pretend the first element of PATH is ".").  This also indicates
443    that a slash in STRING disables searching of the path (this is
444    so that "exec-file ./foo" or "symbol-file ./foo" insures that you
445    get that particular version of foo or an error message).
446
447    If FILENAMED_OPENED is non-null, set it to a newly allocated string naming
448    the actual file opened (this string will always start with a "/".  We
449    have to take special pains to avoid doubling the "/" between the directory
450    and the file, sigh!  Emacs gets confuzzed by this when we print the
451    source file name!!! 
452
453    If a file is found, return the descriptor.
454    Otherwise, return -1, with errno set for the last name we tried to open.  */
455
456 /*  >>>> This should only allow files of certain types,
457     >>>>  eg executable, non-directory */
458 int
459 openp (path, try_cwd_first, string, mode, prot, filename_opened)
460      char *path;
461      int try_cwd_first;
462      char *string;
463      int mode;
464      int prot;
465      char **filename_opened;
466 {
467   register int fd;
468   register char *filename;
469   register char *p, *p1;
470   register int len;
471   int alloclen;
472
473   if (!path)
474     path = ".";
475
476   if (try_cwd_first || string[0] == '/')
477     {
478       filename = string;
479       fd = open (filename, mode, prot);
480       if (fd >= 0 || string[0] == '/' || strchr (string, '/'))
481         goto done;
482     }
483
484   /* ./foo => foo */
485   while (string[0] == '.' && string[1] == '/')
486     string += 2;
487
488   alloclen = strlen (path) + strlen (string) + 2;
489   filename = (char *) alloca (alloclen);
490   fd = -1;
491   for (p = path; p; p = p1 ? p1 + 1 : 0)
492     {
493       p1 = (char *) strchr (p, ':');
494       if (p1)
495         len = p1 - p;
496       else
497         len = strlen (p);
498
499       if (len == 4 && p[0] == '$' && p[1] == 'c'
500                    && p[2] == 'w' && p[3] == 'd') {
501         /* Name is $cwd -- insert current directory name instead.  */
502         int newlen;
503
504         /* First, realloc the filename buffer if too short. */
505         len = strlen (current_directory);
506         newlen = len + strlen (string) + 2;
507         if (newlen > alloclen) {
508           alloclen = newlen;
509           filename = (char *) alloca (alloclen);
510         }
511         strcpy (filename, current_directory);
512       } else {
513         /* Normal file name in path -- just use it.  */
514         strncpy (filename, p, len);
515         filename[len] = 0;
516       }
517
518       /* Remove trailing slashes */
519       while (len > 0 && filename[len-1] == '/')
520        filename[--len] = 0;
521
522       strcat (filename+len, "/");
523       strcat (filename, string);
524
525       fd = open (filename, mode, prot);
526       if (fd >= 0) break;
527     }
528
529  done:
530   if (filename_opened)
531     if (fd < 0)
532       *filename_opened = (char *) 0;
533     else if (filename[0] == '/')
534       *filename_opened = savestring (filename, strlen (filename));
535     else
536       {
537         /* Beware the // my son, the Emacs barfs, the botch that catch... */
538            
539         *filename_opened = concat (current_directory, 
540            '/' == current_directory[strlen(current_directory)-1]? "": "/",
541                                    filename, NULL);
542       }
543
544   return fd;
545 }
546
547 /* Open a source file given a symtab S.  Returns a file descriptor
548    or negative number for error.  */
549
550 static int
551 open_source_file (s)
552      struct symtab *s;
553 {
554   char *path = source_path;
555   char *p;
556   int result;
557   char *fullname;
558
559   /* Quick way out if we already know its full name */
560   if (s->fullname) 
561     {
562       result = open (s->fullname, O_RDONLY);
563       if (result >= 0)
564         return result;
565       /* Didn't work -- free old one, try again. */
566       mfree (s->objfile->md, s->fullname);
567       s->fullname = NULL;
568     }
569
570   if (s->dirname != NULL)
571     {
572       /* Replace a path entry of  $cdir  with the compilation directory name */
573 #define cdir_len        5
574       /* We cast strstr's result in case an ANSIhole has made it const,
575          which produces a "required warning" when assigned to a nonconst. */
576       p = (char *)strstr (source_path, "$cdir");
577       if (p && (p == path || p[-1] == ':')
578             && (p[cdir_len] == ':' || p[cdir_len] == '\0')) {
579         int len;
580
581         path = (char *)
582                alloca (strlen (source_path) + 1 + strlen (s->dirname) + 1);
583         len = p - source_path;
584         strncpy (path, source_path, len);               /* Before $cdir */
585         strcpy (path + len, s->dirname);                /* new stuff */
586         strcat (path + len, source_path + len + cdir_len); /* After $cdir */
587       }
588     }
589
590   result = openp (path, 0, s->filename, O_RDONLY, 0, &s->fullname);
591   if (result < 0)
592     {
593       /* Didn't work.  Try using just the basename. */
594       p = basename (s->filename);
595       if (p != s->filename)
596         result = openp(path, 0, p, O_RDONLY,0, &s->fullname);
597     }
598   if (result >= 0)
599     {
600       fullname = s -> fullname;
601       s -> fullname = mstrsave (s -> objfile -> md, s -> fullname);
602       free (fullname);
603     }
604   return result;
605 }
606
607 \f
608 /* Create and initialize the table S->line_charpos that records
609    the positions of the lines in the source file, which is assumed
610    to be open on descriptor DESC.
611    All set S->nlines to the number of such lines.  */
612
613 static void
614 find_source_lines (s, desc)
615      struct symtab *s;
616      int desc;
617 {
618   struct stat st;
619   register char *data, *p, *end;
620   int nlines = 0;
621   int lines_allocated = 1000;
622   int *line_charpos;
623   long exec_mtime;
624   int size;
625 #ifdef LSEEK_NOT_LINEAR
626   char c;
627 #endif
628
629   line_charpos = (int *) xmmalloc (s -> objfile -> md,
630                                    lines_allocated * sizeof (int));
631   if (fstat (desc, &st) < 0)
632    perror_with_name (s->filename);
633
634   if (exec_bfd) {
635     exec_mtime = bfd_get_mtime(exec_bfd);
636     if (exec_mtime && exec_mtime < st.st_mtime)
637      printf_filtered ("Source file is more recent than executable.\n");
638   }
639
640 #ifdef LSEEK_NOT_LINEAR
641   /* Have to read it byte by byte to find out where the chars live */
642
643    line_charpos[0] = tell(desc);
644    nlines = 1;
645    while (myread(desc, &c, 1)>0) 
646    {
647      if (c == '\n') 
648      {
649        if (nlines == lines_allocated) 
650        {
651          lines_allocated *= 2;
652          line_charpos =
653           (int *) xmrealloc (s -> objfile -> md, (char *) line_charpos,
654                              sizeof (int) * lines_allocated);
655        }
656        line_charpos[nlines++] = tell(desc);
657      }
658    }
659
660 #else
661   /* st_size might be a large type, but we only support source files whose 
662      size fits in an int.  FIXME. */
663   size = (int) st.st_size;
664
665 #ifdef BROKEN_LARGE_ALLOCA
666   data = (char *) xmalloc (size);
667   make_cleanup (free, data);
668 #else
669   data = (char *) alloca (size);
670 #endif
671   if (myread (desc, data, size) < 0)
672    perror_with_name (s->filename);
673   end = data + size;
674   p = data;
675   line_charpos[0] = 0;
676   nlines = 1;
677   while (p != end)
678   {
679     if (*p++ == '\n'
680         /* A newline at the end does not start a new line.  */
681         && p != end)
682     {
683       if (nlines == lines_allocated)
684       {
685         lines_allocated *= 2;
686         line_charpos =
687          (int *) xmrealloc (s -> objfile -> md, (char *) line_charpos,
688                             sizeof (int) * lines_allocated);
689       }
690       line_charpos[nlines++] = p - data;
691     }
692   }
693 #endif
694   s->nlines = nlines;
695   s->line_charpos =
696    (int *) xmrealloc (s -> objfile -> md, (char *) line_charpos,
697                       nlines * sizeof (int));
698
699 }
700
701 /* Return the character position of a line LINE in symtab S.
702    Return 0 if anything is invalid.  */
703
704 #if 0   /* Currently unused */
705
706 int
707 source_line_charpos (s, line)
708      struct symtab *s;
709      int line;
710 {
711   if (!s) return 0;
712   if (!s->line_charpos || line <= 0) return 0;
713   if (line > s->nlines)
714     line = s->nlines;
715   return s->line_charpos[line - 1];
716 }
717
718 /* Return the line number of character position POS in symtab S.  */
719
720 int
721 source_charpos_line (s, chr)
722     register struct symtab *s;
723     register int chr;
724 {
725   register int line = 0;
726   register int *lnp;
727     
728   if (s == 0 || s->line_charpos == 0) return 0;
729   lnp = s->line_charpos;
730   /* Files are usually short, so sequential search is Ok */
731   while (line < s->nlines  && *lnp <= chr)
732     {
733       line++;
734       lnp++;
735     }
736   if (line >= s->nlines)
737     line = s->nlines;
738   return line;
739 }
740
741 #endif  /* 0 */
742
743 \f
744 /* Get full pathname and line number positions for a symtab.
745    Return nonzero if line numbers may have changed.
746    Set *FULLNAME to actual name of the file as found by `openp',
747    or to 0 if the file is not found.  */
748
749 static int
750 get_filename_and_charpos (s, fullname)
751      struct symtab *s;
752      char **fullname;
753 {
754   register int desc, linenums_changed = 0;
755   
756   desc = open_source_file (s);
757   if (desc < 0)
758     {
759       if (fullname)
760         *fullname = NULL;
761       return 0;
762     }  
763   if (fullname)
764     *fullname = s->fullname;
765   if (s->line_charpos == 0) linenums_changed = 1;
766   if (linenums_changed) find_source_lines (s, desc);
767   close (desc);
768   return linenums_changed;
769 }
770
771 /* Print text describing the full name of the source file S
772    and the line number LINE and its corresponding character position.
773    The text starts with two Ctrl-z so that the Emacs-GDB interface
774    can easily find it.
775
776    MID_STATEMENT is nonzero if the PC is not at the beginning of that line.
777
778    Return 1 if successful, 0 if could not find the file.  */
779
780 int
781 identify_source_line (s, line, mid_statement, pc)
782      struct symtab *s;
783      int line;
784      int mid_statement;
785      CORE_ADDR pc;
786 {
787   if (s->line_charpos == 0)
788     get_filename_and_charpos (s, (char **)NULL);
789   if (s->fullname == 0)
790     return 0;
791   if (line >= s->nlines) 
792    return 0;
793   printf ("\032\032%s:%d:%d:%s:0x%x\n", s->fullname,
794           line, s->line_charpos[line - 1],
795           mid_statement ? "middle" : "beg",
796           pc);
797   current_source_line = line;
798   first_line_listed = line;
799   last_line_listed = line;
800   current_source_symtab = s;
801   return 1;
802 }
803 \f
804 /* Print source lines from the file of symtab S,
805    starting with line number LINE and stopping before line number STOPLINE.  */
806
807 void
808 print_source_lines (s, line, stopline, noerror)
809      struct symtab *s;
810      int line, stopline;
811      int noerror;
812 {
813   register int c;
814   register int desc;
815   register FILE *stream;
816   int nlines = stopline - line;
817
818   /* Regardless of whether we can open the file, set current_source_symtab. */
819   current_source_symtab = s;
820   current_source_line = line;
821   first_line_listed = line;
822
823   desc = open_source_file (s);
824   if (desc < 0)
825     {
826       if (! noerror) {
827         char *name = alloca (strlen (s->filename) + 100);
828         sprintf (name, "%s:%d", s->filename, line);
829         print_sys_errmsg (name, errno);
830       }
831       return;
832     }
833
834   if (s->line_charpos == 0)
835     find_source_lines (s, desc);
836
837   if (line < 1 || line > s->nlines)
838     {
839       close (desc);
840       error ("Line number %d out of range; %s has %d lines.",
841              line, s->filename, s->nlines);
842     }
843
844   if (lseek (desc, s->line_charpos[line - 1], 0) < 0)
845     {
846       close (desc);
847       perror_with_name (s->filename);
848     }
849
850   stream = fdopen (desc, FOPEN_RT);
851   clearerr (stream);
852
853   while (nlines-- > 0)
854     {
855       c = fgetc (stream);
856       if (c == EOF) break;
857       last_line_listed = current_source_line;
858       printf_filtered ("%d\t", current_source_line++);
859       do
860         {
861           if (c < 040 && c != '\t' && c != '\n' && c != '\r')
862               printf_filtered ("^%c", c + 0100);
863           else if (c == 0177)
864             printf_filtered ("^?");
865           else
866             printf_filtered ("%c", c);
867         } while (c != '\n' && (c = fgetc (stream)) >= 0);
868     }
869
870   fclose (stream);
871 }
872 \f
873
874
875 /* 
876   C++
877   Print a list of files and line numbers which a user may choose from
878   in order to list a function which was specified ambiguously
879   (as with `list classname::overloadedfuncname', for example).
880   The vector in SALS provides the filenames and line numbers.
881   */
882 static void
883 ambiguous_line_spec (sals)
884      struct symtabs_and_lines *sals;
885 {
886   int i;
887
888   for (i = 0; i < sals->nelts; ++i)
889     printf_filtered("file: \"%s\", line number: %d\n",
890                     sals->sals[i].symtab->filename, sals->sals[i].line);
891 }
892
893
894 static void
895 list_command (arg, from_tty)
896      char *arg;
897      int from_tty;
898 {
899   struct symtabs_and_lines sals, sals_end;
900   struct symtab_and_line sal, sal_end;
901   struct symbol *sym;
902   char *arg1;
903   int no_end = 1;
904   int dummy_end = 0;
905   int dummy_beg = 0;
906   int linenum_beg = 0;
907   char *p;
908
909   if (!have_full_symbols () && !have_partial_symbols())
910     error ("No symbol table is loaded.  Use the \"file\" command.");
911
912   /* Pull in a current source symtab if necessary */
913   if (current_source_symtab == 0 &&
914       (arg == 0 || arg[0] == '+' || arg[0] == '-'))
915     select_source_symtab (0);
916
917   /* "l" or "l +" lists next ten lines.  */
918
919   if (arg == 0 || STREQ (arg, "+"))
920     {
921       if (current_source_symtab == 0)
922         error ("No default source file yet.  Do \"help list\".");
923       print_source_lines (current_source_symtab, current_source_line,
924                           current_source_line + lines_to_list, 0);
925       return;
926     }
927
928   /* "l -" lists previous ten lines, the ones before the ten just listed.  */
929   if (STREQ (arg, "-"))
930     {
931       if (current_source_symtab == 0)
932         error ("No default source file yet.  Do \"help list\".");
933       print_source_lines (current_source_symtab,
934                           max (first_line_listed - lines_to_list, 1),
935                           first_line_listed, 0);
936       return;
937     }
938
939   /* Now if there is only one argument, decode it in SAL
940      and set NO_END.
941      If there are two arguments, decode them in SAL and SAL_END
942      and clear NO_END; however, if one of the arguments is blank,
943      set DUMMY_BEG or DUMMY_END to record that fact.  */
944
945   arg1 = arg;
946   if (*arg1 == ',')
947     dummy_beg = 1;
948   else
949     {
950       sals = decode_line_1 (&arg1, 0, 0, 0);
951
952       if (! sals.nelts) return;  /*  C++  */
953       if (sals.nelts > 1)
954         {
955           ambiguous_line_spec (&sals);
956           free (sals.sals);
957           return;
958         }
959
960       sal = sals.sals[0];
961       free (sals.sals);
962     }
963
964   /* Record whether the BEG arg is all digits.  */
965
966   for (p = arg; p != arg1 && *p >= '0' && *p <= '9'; p++);
967   linenum_beg = (p == arg1);
968
969   while (*arg1 == ' ' || *arg1 == '\t')
970     arg1++;
971   if (*arg1 == ',')
972     {
973       no_end = 0;
974       arg1++;
975       while (*arg1 == ' ' || *arg1 == '\t')
976         arg1++;
977       if (*arg1 == 0)
978         dummy_end = 1;
979       else
980         {
981           if (dummy_beg)
982             sals_end = decode_line_1 (&arg1, 0, 0, 0);
983           else
984             sals_end = decode_line_1 (&arg1, 0, sal.symtab, sal.line);
985           if (sals_end.nelts == 0) 
986             return;
987           if (sals_end.nelts > 1)
988             {
989               ambiguous_line_spec (&sals_end);
990               free (sals_end.sals);
991               return;
992             }
993           sal_end = sals_end.sals[0];
994           free (sals_end.sals);
995         }
996     }
997
998   if (*arg1)
999     error ("Junk at end of line specification.");
1000
1001   if (!no_end && !dummy_beg && !dummy_end
1002       && sal.symtab != sal_end.symtab)
1003     error ("Specified start and end are in different files.");
1004   if (dummy_beg && dummy_end)
1005     error ("Two empty args do not say what lines to list.");
1006  
1007   /* if line was specified by address,
1008      first print exactly which line, and which file.
1009      In this case, sal.symtab == 0 means address is outside
1010      of all known source files, not that user failed to give a filename.  */
1011   if (*arg == '*')
1012     {
1013       if (sal.symtab == 0)
1014         error ("No source file for address %s.", local_hex_string(sal.pc));
1015       sym = find_pc_function (sal.pc);
1016       if (sym)
1017         {
1018           printf_filtered ("%s is in ", local_hex_string(sal.pc));
1019           fputs_filtered (SYMBOL_SOURCE_NAME (sym), stdout);
1020           printf_filtered (" (%s:%d).\n", sal.symtab->filename, sal.line);
1021         }
1022       else
1023         printf_filtered ("%s is at %s:%d.\n",
1024                          local_hex_string(sal.pc), 
1025                          sal.symtab->filename, sal.line);
1026     }
1027
1028   /* If line was not specified by just a line number,
1029      and it does not imply a symtab, it must be an undebuggable symbol
1030      which means no source code.  */
1031
1032   if (! linenum_beg && sal.symtab == 0)
1033     error ("No line number known for %s.", arg);
1034
1035   /* If this command is repeated with RET,
1036      turn it into the no-arg variant.  */
1037
1038   if (from_tty)
1039     *arg = 0;
1040
1041   if (dummy_beg && sal_end.symtab == 0)
1042     error ("No default source file yet.  Do \"help list\".");
1043   if (dummy_beg)
1044     print_source_lines (sal_end.symtab,
1045                         max (sal_end.line - (lines_to_list - 1), 1),
1046                         sal_end.line + 1, 0);
1047   else if (sal.symtab == 0)
1048     error ("No default source file yet.  Do \"help list\".");
1049   else if (no_end)
1050     print_source_lines (sal.symtab,
1051                         max (sal.line - (lines_to_list / 2), 1),
1052                         sal.line + (lines_to_list / 2), 0);
1053   else
1054     print_source_lines (sal.symtab, sal.line,
1055                         (dummy_end
1056                          ? sal.line + lines_to_list
1057                          : sal_end.line + 1),
1058                         0);
1059 }
1060 \f
1061 /* Print info on range of pc's in a specified line.  */
1062
1063 static void
1064 line_info (arg, from_tty)
1065      char *arg;
1066      int from_tty;
1067 {
1068   struct symtabs_and_lines sals;
1069   struct symtab_and_line sal;
1070   CORE_ADDR start_pc, end_pc;
1071   int i;
1072
1073   if (arg == 0)
1074     {
1075       sal.symtab = current_source_symtab;
1076       sal.line = last_line_listed;
1077       sals.nelts = 1;
1078       sals.sals = (struct symtab_and_line *)
1079         xmalloc (sizeof (struct symtab_and_line));
1080       sals.sals[0] = sal;
1081     }
1082   else
1083     {
1084       sals = decode_line_spec_1 (arg, 0);
1085       
1086       dont_repeat ();
1087     }
1088
1089   /* C++  More than one line may have been specified, as when the user
1090      specifies an overloaded function name. Print info on them all. */
1091   for (i = 0; i < sals.nelts; i++)
1092     {
1093       sal = sals.sals[i];
1094       
1095       if (sal.symtab == 0)
1096         {
1097           printf_filtered ("No line number information available");
1098           if (sal.pc != 0)
1099             {
1100               /* This is useful for "info line *0x7f34".  If we can't tell the
1101                  user about a source line, at least let them have the symbolic
1102                  address.  */
1103               printf_filtered (" for address ");
1104               wrap_here ("  ");
1105               print_address (sal.pc, stdout);
1106             }
1107           else
1108             printf_filtered (".");
1109           printf_filtered ("\n");
1110         }
1111       else if (sal.line > 0
1112           && find_line_pc_range (sal.symtab, sal.line, &start_pc, &end_pc))
1113         {
1114           if (start_pc == end_pc)
1115             printf_filtered ("Line %d of \"%s\" is at pc %s but contains no code.\n",
1116                              sal.line, sal.symtab->filename, local_hex_string(start_pc));
1117           else
1118             {
1119               printf_filtered ("Line %d of \"%s\" starts at pc %s",
1120                                sal.line, sal.symtab->filename, 
1121                                local_hex_string(start_pc));
1122               printf_filtered (" and ends at %s.\n",
1123                                local_hex_string(end_pc));
1124             }
1125           /* x/i should display this line's code.  */
1126           set_next_address (start_pc);
1127           /* Repeating "info line" should do the following line.  */
1128           last_line_listed = sal.line + 1;
1129
1130           /* If this is the only line, show the source code.  If it could
1131              not find the file, don't do anything special.  */
1132           if (frame_file_full_name && sals.nelts == 1)
1133             identify_source_line (sal.symtab, sal.line, 0, start_pc);
1134         }
1135       else
1136         /* Is there any case in which we get here, and have an address
1137            which the user would want to see?  If we have debugging symbols
1138            and no line numbers?  */
1139         printf_filtered ("Line number %d is out of range for \"%s\".\n",
1140                          sal.line, sal.symtab->filename);
1141     }
1142 }
1143 \f
1144 /* Commands to search the source file for a regexp.  */
1145
1146 /* ARGSUSED */
1147 static void
1148 forward_search_command (regex, from_tty)
1149      char *regex;
1150      int from_tty;
1151 {
1152   register int c;
1153   register int desc;
1154   register FILE *stream;
1155   int line = last_line_listed + 1;
1156   char *msg;
1157
1158   msg = (char *) re_comp (regex);
1159   if (msg)
1160     error (msg);
1161
1162   if (current_source_symtab == 0)
1163     select_source_symtab (0);
1164
1165   /* Search from last_line_listed+1 in current_source_symtab */
1166
1167   desc = open_source_file (current_source_symtab);
1168   if (desc < 0)
1169     perror_with_name (current_source_symtab->filename);
1170
1171   if (current_source_symtab->line_charpos == 0)
1172     find_source_lines (current_source_symtab, desc);
1173
1174   if (line < 1 || line > current_source_symtab->nlines)
1175     {
1176       close (desc);
1177       error ("Expression not found");
1178     }
1179
1180   if (lseek (desc, current_source_symtab->line_charpos[line - 1], 0) < 0)
1181     {
1182       close (desc);
1183       perror_with_name (current_source_symtab->filename);
1184     }
1185
1186   stream = fdopen (desc, FOPEN_RT);
1187   clearerr (stream);
1188   while (1) {
1189 /* FIXME!!!  We walk right off the end of buf if we get a long line!!! */
1190     char buf[4096];             /* Should be reasonable??? */
1191     register char *p = buf;
1192
1193     c = getc (stream);
1194     if (c == EOF)
1195       break;
1196     do {
1197       *p++ = c;
1198     } while (c != '\n' && (c = getc (stream)) >= 0);
1199
1200     /* we now have a source line in buf, null terminate and match */
1201     *p = 0;
1202     if (re_exec (buf) > 0)
1203       {
1204         /* Match! */
1205         fclose (stream);
1206         print_source_lines (current_source_symtab,
1207                            line, line+1, 0);
1208         current_source_line = max (line - lines_to_list / 2, 1);
1209         return;
1210       }
1211     line++;
1212   }
1213
1214   printf_filtered ("Expression not found\n");
1215   fclose (stream);
1216 }
1217
1218 /* ARGSUSED */
1219 static void
1220 reverse_search_command (regex, from_tty)
1221      char *regex;
1222      int from_tty;
1223 {
1224   register int c;
1225   register int desc;
1226   register FILE *stream;
1227   int line = last_line_listed - 1;
1228   char *msg;
1229
1230   msg = (char *) re_comp (regex);
1231   if (msg)
1232     error (msg);
1233
1234   if (current_source_symtab == 0)
1235     select_source_symtab (0);
1236
1237   /* Search from last_line_listed-1 in current_source_symtab */
1238
1239   desc = open_source_file (current_source_symtab);
1240   if (desc < 0)
1241     perror_with_name (current_source_symtab->filename);
1242
1243   if (current_source_symtab->line_charpos == 0)
1244     find_source_lines (current_source_symtab, desc);
1245
1246   if (line < 1 || line > current_source_symtab->nlines)
1247     {
1248       close (desc);
1249       error ("Expression not found");
1250     }
1251
1252   if (lseek (desc, current_source_symtab->line_charpos[line - 1], 0) < 0)
1253     {
1254       close (desc);
1255       perror_with_name (current_source_symtab->filename);
1256     }
1257
1258   stream = fdopen (desc, FOPEN_RT);
1259   clearerr (stream);
1260   while (line > 1)
1261     {
1262 /* FIXME!!!  We walk right off the end of buf if we get a long line!!! */
1263       char buf[4096];           /* Should be reasonable??? */
1264       register char *p = buf;
1265
1266       c = getc (stream);
1267       if (c == EOF)
1268         break;
1269       do {
1270         *p++ = c;
1271       } while (c != '\n' && (c = getc (stream)) >= 0);
1272
1273       /* We now have a source line in buf; null terminate and match.  */
1274       *p = 0;
1275       if (re_exec (buf) > 0)
1276         {
1277           /* Match! */
1278           fclose (stream);
1279           print_source_lines (current_source_symtab,
1280                               line, line+1, 0);
1281           current_source_line = max (line - lines_to_list / 2, 1);
1282           return;
1283         }
1284       line--;
1285       if (fseek (stream, current_source_symtab->line_charpos[line - 1], 0) < 0)
1286         {
1287           fclose (stream);
1288           perror_with_name (current_source_symtab->filename);
1289         }
1290     }
1291
1292   printf_filtered ("Expression not found\n");
1293   fclose (stream);
1294   return;
1295 }
1296 \f
1297 void
1298 _initialize_source ()
1299 {
1300   current_source_symtab = 0;
1301   init_source_path ();
1302
1303   add_com ("directory", class_files, directory_command,
1304            "Add directory DIR to beginning of search path for source files.\n\
1305 Forget cached info on source file locations and line positions.\n\
1306 DIR can also be $cwd for the current working directory, or $cdir for the\n\
1307 directory in which the source file was compiled into object code.\n\
1308 With no argument, reset the search path to $cdir:$cwd, the default.");
1309
1310   add_cmd ("directories", no_class, show_directories,
1311            "Current search path for finding source files.\n\
1312 $cwd in the path means the current working directory.\n\
1313 $cdir in the path means the compilation directory of the source file.",
1314            &showlist);
1315
1316   add_info ("source", source_info,
1317             "Information about the current source file.");
1318
1319   add_info ("line", line_info,
1320             "Core addresses of the code for a source line.\n\
1321 Line can be specified as\n\
1322   LINENUM, to list around that line in current file,\n\
1323   FILE:LINENUM, to list around that line in that file,\n\
1324   FUNCTION, to list around beginning of that function,\n\
1325   FILE:FUNCTION, to distinguish among like-named static functions.\n\
1326 Default is to describe the last source line that was listed.\n\n\
1327 This sets the default address for \"x\" to the line's first instruction\n\
1328 so that \"x/i\" suffices to start examining the machine code.\n\
1329 The address is also stored as the value of \"$_\".");
1330
1331   add_com ("forward-search", class_files, forward_search_command,
1332            "Search for regular expression (see regex(3)) from last line listed.");
1333   add_com_alias ("search", "forward-search", class_files, 0);
1334
1335   add_com ("reverse-search", class_files, reverse_search_command,
1336            "Search backward for regular expression (see regex(3)) from last line listed.");
1337
1338   add_com ("list", class_files, list_command,
1339            "List specified function or line.\n\
1340 With no argument, lists ten more lines after or around previous listing.\n\
1341 \"list -\" lists the ten lines before a previous ten-line listing.\n\
1342 One argument specifies a line, and ten lines are listed around that line.\n\
1343 Two arguments with comma between specify starting and ending lines to list.\n\
1344 Lines can be specified in these ways:\n\
1345   LINENUM, to list around that line in current file,\n\
1346   FILE:LINENUM, to list around that line in that file,\n\
1347   FUNCTION, to list around beginning of that function,\n\
1348   FILE:FUNCTION, to distinguish among like-named static functions.\n\
1349   *ADDRESS, to list around the line containing that address.\n\
1350 With two args if one is empty it stands for ten lines away from the other arg.");
1351   add_com_alias ("l", "list", class_files, 1);
1352
1353   add_show_from_set
1354     (add_set_cmd ("listsize", class_support, var_uinteger,
1355                   (char *)&lines_to_list,
1356         "Set number of source lines gdb will list by default.",
1357                   &setlist),
1358      &showlist);
1359 }
This page took 0.09931 seconds and 4 git commands to generate.