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