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