source.c revision 1.8 1 1.1 christos /* List lines of source files for GDB, the GNU debugger.
2 1.8 christos Copyright (C) 1986-2019 Free Software Foundation, Inc.
3 1.1 christos
4 1.1 christos This file is part of GDB.
5 1.1 christos
6 1.1 christos This program is free software; you can redistribute it and/or modify
7 1.1 christos it under the terms of the GNU General Public License as published by
8 1.1 christos the Free Software Foundation; either version 3 of the License, or
9 1.1 christos (at your option) any later version.
10 1.1 christos
11 1.1 christos This program is distributed in the hope that it will be useful,
12 1.1 christos but WITHOUT ANY WARRANTY; without even the implied warranty of
13 1.1 christos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 1.1 christos GNU General Public License for more details.
15 1.1 christos
16 1.1 christos You should have received a copy of the GNU General Public License
17 1.1 christos along with this program. If not, see <http://www.gnu.org/licenses/>. */
18 1.1 christos
19 1.1 christos #include "defs.h"
20 1.1 christos #include "arch-utils.h"
21 1.1 christos #include "symtab.h"
22 1.1 christos #include "expression.h"
23 1.1 christos #include "language.h"
24 1.1 christos #include "command.h"
25 1.1 christos #include "source.h"
26 1.1 christos #include "gdbcmd.h"
27 1.1 christos #include "frame.h"
28 1.1 christos #include "value.h"
29 1.8 christos #include "common/filestuff.h"
30 1.1 christos
31 1.1 christos #include <sys/types.h>
32 1.1 christos #include <fcntl.h>
33 1.1 christos #include "gdbcore.h"
34 1.1 christos #include "gdb_regex.h"
35 1.1 christos #include "symfile.h"
36 1.1 christos #include "objfiles.h"
37 1.1 christos #include "annotate.h"
38 1.1 christos #include "gdbtypes.h"
39 1.1 christos #include "linespec.h"
40 1.1 christos #include "filenames.h" /* for DOSish file names */
41 1.1 christos #include "completer.h"
42 1.1 christos #include "ui-out.h"
43 1.1 christos #include "readline/readline.h"
44 1.6 christos #include "common/enum-flags.h"
45 1.8 christos #include "common/scoped_fd.h"
46 1.7 christos #include <algorithm>
47 1.8 christos #include "common/pathstuff.h"
48 1.8 christos #include "source-cache.h"
49 1.1 christos
50 1.1 christos #define OPEN_MODE (O_RDONLY | O_BINARY)
51 1.1 christos #define FDOPEN_MODE FOPEN_RB
52 1.1 christos
53 1.1 christos /* Path of directories to search for source files.
54 1.1 christos Same format as the PATH environment variable's value. */
55 1.1 christos
56 1.1 christos char *source_path;
57 1.1 christos
58 1.1 christos /* Support for source path substitution commands. */
59 1.1 christos
60 1.1 christos struct substitute_path_rule
61 1.1 christos {
62 1.1 christos char *from;
63 1.1 christos char *to;
64 1.1 christos struct substitute_path_rule *next;
65 1.1 christos };
66 1.1 christos
67 1.1 christos static struct substitute_path_rule *substitute_path_rules = NULL;
68 1.1 christos
69 1.1 christos /* Symtab of default file for listing lines of. */
70 1.1 christos
71 1.1 christos static struct symtab *current_source_symtab;
72 1.1 christos
73 1.1 christos /* Default next line to list. */
74 1.1 christos
75 1.1 christos static int current_source_line;
76 1.1 christos
77 1.1 christos static struct program_space *current_source_pspace;
78 1.1 christos
79 1.1 christos /* Default number of lines to print with commands like "list".
80 1.1 christos This is based on guessing how many long (i.e. more than chars_per_line
81 1.1 christos characters) lines there will be. To be completely correct, "list"
82 1.1 christos and friends should be rewritten to count characters and see where
83 1.1 christos things are wrapping, but that would be a fair amount of work. */
84 1.1 christos
85 1.6 christos static int lines_to_list = 10;
86 1.1 christos static void
87 1.1 christos show_lines_to_list (struct ui_file *file, int from_tty,
88 1.1 christos struct cmd_list_element *c, const char *value)
89 1.1 christos {
90 1.1 christos fprintf_filtered (file,
91 1.1 christos _("Number of source lines gdb "
92 1.1 christos "will list by default is %s.\n"),
93 1.1 christos value);
94 1.1 christos }
95 1.1 christos
96 1.1 christos /* Possible values of 'set filename-display'. */
97 1.1 christos static const char filename_display_basename[] = "basename";
98 1.1 christos static const char filename_display_relative[] = "relative";
99 1.1 christos static const char filename_display_absolute[] = "absolute";
100 1.1 christos
101 1.1 christos static const char *const filename_display_kind_names[] = {
102 1.1 christos filename_display_basename,
103 1.1 christos filename_display_relative,
104 1.1 christos filename_display_absolute,
105 1.1 christos NULL
106 1.1 christos };
107 1.1 christos
108 1.1 christos static const char *filename_display_string = filename_display_relative;
109 1.1 christos
110 1.1 christos static void
111 1.1 christos show_filename_display_string (struct ui_file *file, int from_tty,
112 1.1 christos struct cmd_list_element *c, const char *value)
113 1.1 christos {
114 1.1 christos fprintf_filtered (file, _("Filenames are displayed as \"%s\".\n"), value);
115 1.1 christos }
116 1.1 christos
117 1.1 christos /* Line number of last line printed. Default for various commands.
118 1.1 christos current_source_line is usually, but not always, the same as this. */
119 1.1 christos
120 1.1 christos static int last_line_listed;
121 1.1 christos
122 1.3 christos /* First line number listed by last listing command. If 0, then no
123 1.3 christos source lines have yet been listed since the last time the current
124 1.3 christos source line was changed. */
125 1.1 christos
126 1.1 christos static int first_line_listed;
127 1.1 christos
128 1.1 christos /* Saves the name of the last source file visited and a possible error code.
129 1.1 christos Used to prevent repeating annoying "No such file or directories" msgs. */
130 1.1 christos
131 1.1 christos static struct symtab *last_source_visited = NULL;
132 1.1 christos static int last_source_error = 0;
133 1.1 christos
134 1.1 christos /* Return the first line listed by print_source_lines.
136 1.1 christos Used by command interpreters to request listing from
137 1.1 christos a previous point. */
138 1.1 christos
139 1.1 christos int
140 1.1 christos get_first_line_listed (void)
141 1.1 christos {
142 1.1 christos return first_line_listed;
143 1.1 christos }
144 1.3 christos
145 1.3 christos /* Clear line listed range. This makes the next "list" center the
146 1.3 christos printed source lines around the current source line. */
147 1.3 christos
148 1.3 christos static void
149 1.3 christos clear_lines_listed_range (void)
150 1.3 christos {
151 1.3 christos first_line_listed = 0;
152 1.3 christos last_line_listed = 0;
153 1.3 christos }
154 1.1 christos
155 1.1 christos /* Return the default number of lines to print with commands like the
156 1.1 christos cli "list". The caller of print_source_lines must use this to
157 1.1 christos calculate the end line and use it in the call to print_source_lines
158 1.1 christos as it does not automatically use this value. */
159 1.1 christos
160 1.1 christos int
161 1.1 christos get_lines_to_list (void)
162 1.1 christos {
163 1.1 christos return lines_to_list;
164 1.1 christos }
165 1.1 christos
166 1.1 christos /* Return the current source file for listing and next line to list.
167 1.1 christos NOTE: The returned sal pc and end fields are not valid. */
168 1.1 christos
169 1.1 christos struct symtab_and_line
170 1.1 christos get_current_source_symtab_and_line (void)
171 1.8 christos {
172 1.1 christos symtab_and_line cursal;
173 1.1 christos
174 1.1 christos cursal.pspace = current_source_pspace;
175 1.1 christos cursal.symtab = current_source_symtab;
176 1.1 christos cursal.line = current_source_line;
177 1.1 christos cursal.pc = 0;
178 1.1 christos cursal.end = 0;
179 1.1 christos
180 1.1 christos return cursal;
181 1.1 christos }
182 1.1 christos
183 1.1 christos /* If the current source file for listing is not set, try and get a default.
184 1.1 christos Usually called before get_current_source_symtab_and_line() is called.
185 1.1 christos It may err out if a default cannot be determined.
186 1.1 christos We must be cautious about where it is called, as it can recurse as the
187 1.1 christos process of determining a new default may call the caller!
188 1.1 christos Use get_current_source_symtab_and_line only to get whatever
189 1.1 christos we have without erroring out or trying to get a default. */
190 1.1 christos
191 1.1 christos void
192 1.1 christos set_default_source_symtab_and_line (void)
193 1.1 christos {
194 1.1 christos if (!have_full_symbols () && !have_partial_symbols ())
195 1.1 christos error (_("No symbol table is loaded. Use the \"file\" command."));
196 1.1 christos
197 1.1 christos /* Pull in a current source symtab if necessary. */
198 1.1 christos if (current_source_symtab == 0)
199 1.1 christos select_source_symtab (0);
200 1.1 christos }
201 1.1 christos
202 1.1 christos /* Return the current default file for listing and next line to list
203 1.1 christos (the returned sal pc and end fields are not valid.)
204 1.1 christos and set the current default to whatever is in SAL.
205 1.1 christos NOTE: The returned sal pc and end fields are not valid. */
206 1.1 christos
207 1.8 christos struct symtab_and_line
208 1.1 christos set_current_source_symtab_and_line (const symtab_and_line &sal)
209 1.8 christos {
210 1.1 christos symtab_and_line cursal;
211 1.1 christos
212 1.1 christos cursal.pspace = current_source_pspace;
213 1.1 christos cursal.symtab = current_source_symtab;
214 1.1 christos cursal.line = current_source_line;
215 1.1 christos cursal.pc = 0;
216 1.1 christos cursal.end = 0;
217 1.8 christos
218 1.8 christos current_source_pspace = sal.pspace;
219 1.8 christos current_source_symtab = sal.symtab;
220 1.1 christos current_source_line = sal.line;
221 1.3 christos
222 1.3 christos /* Force the next "list" to center around the current line. */
223 1.3 christos clear_lines_listed_range ();
224 1.1 christos
225 1.1 christos return cursal;
226 1.1 christos }
227 1.1 christos
228 1.1 christos /* Reset any information stored about a default file and line to print. */
229 1.1 christos
230 1.1 christos void
231 1.1 christos clear_current_source_symtab_and_line (void)
232 1.1 christos {
233 1.1 christos current_source_symtab = 0;
234 1.1 christos current_source_line = 0;
235 1.1 christos }
236 1.8 christos
237 1.1 christos /* See source.h. */
238 1.1 christos
239 1.1 christos void
240 1.1 christos select_source_symtab (struct symtab *s)
241 1.1 christos {
242 1.1 christos if (s)
243 1.1 christos {
244 1.1 christos current_source_symtab = s;
245 1.1 christos current_source_line = 1;
246 1.1 christos current_source_pspace = SYMTAB_PSPACE (s);
247 1.1 christos return;
248 1.1 christos }
249 1.1 christos
250 1.1 christos if (current_source_symtab)
251 1.1 christos return;
252 1.1 christos
253 1.1 christos /* Make the default place to list be the function `main'
254 1.6 christos if one exists. */
255 1.1 christos if (lookup_symbol (main_name (), 0, VAR_DOMAIN, 0).symbol)
256 1.8 christos {
257 1.8 christos std::vector<symtab_and_line> sals
258 1.8 christos = decode_line_with_current_source (main_name (),
259 1.8 christos DECODE_LINE_FUNFIRSTLINE);
260 1.1 christos const symtab_and_line &sal = sals[0];
261 1.1 christos current_source_pspace = sal.pspace;
262 1.7 christos current_source_symtab = sal.symtab;
263 1.1 christos current_source_line = std::max (sal.line - (lines_to_list - 1), 1);
264 1.1 christos if (current_source_symtab)
265 1.1 christos return;
266 1.1 christos }
267 1.1 christos
268 1.1 christos /* Alright; find the last file in the symtab list (ignoring .h's
269 1.1 christos and namespace symtabs). */
270 1.1 christos
271 1.1 christos current_source_line = 1;
272 1.8 christos
273 1.1 christos for (objfile *ofp : current_program_space->objfiles ())
274 1.8 christos {
275 1.8 christos for (compunit_symtab *cu : ofp->compunits ())
276 1.8 christos {
277 1.8 christos for (symtab *symtab : compunit_filetabs (cu))
278 1.8 christos {
279 1.8 christos const char *name = symtab->filename;
280 1.3 christos int len = strlen (name);
281 1.8 christos
282 1.8 christos if (!(len > 2 && (strcmp (&name[len - 2], ".h") == 0
283 1.8 christos || strcmp (name, "<<C++-namespaces>>") == 0)))
284 1.8 christos {
285 1.8 christos current_source_pspace = current_program_space;
286 1.8 christos current_source_symtab = symtab;
287 1.8 christos }
288 1.1 christos }
289 1.1 christos }
290 1.1 christos }
291 1.1 christos
292 1.1 christos if (current_source_symtab)
293 1.1 christos return;
294 1.8 christos
295 1.8 christos for (objfile *objfile : current_program_space->objfiles ())
296 1.8 christos {
297 1.8 christos if (objfile->sf)
298 1.8 christos s = objfile->sf->qf->find_last_source_symtab (objfile);
299 1.8 christos if (s)
300 1.8 christos current_source_symtab = s;
301 1.1 christos }
302 1.1 christos if (current_source_symtab)
303 1.1 christos return;
304 1.1 christos
305 1.1 christos error (_("Can't find a default source file"));
306 1.1 christos }
307 1.1 christos
308 1.1 christos /* Handler for "set directories path-list" command.
310 1.1 christos "set dir mumble" doesn't prepend paths, it resets the entire
311 1.1 christos path list. The theory is that set(show(dir)) should be a no-op. */
312 1.8 christos
313 1.8 christos static void
314 1.1 christos set_directories_command (const char *args,
315 1.1 christos int from_tty, struct cmd_list_element *c)
316 1.1 christos {
317 1.1 christos /* This is the value that was set.
318 1.1 christos It needs to be processed to maintain $cdir:$cwd and remove dups. */
319 1.1 christos char *set_path = source_path;
320 1.1 christos
321 1.1 christos /* We preserve the invariant that $cdir:$cwd begins life at the end of
322 1.1 christos the list by calling init_source_path. If they appear earlier in
323 1.1 christos SET_PATH then mod_path will move them appropriately.
324 1.1 christos mod_path will also remove duplicates. */
325 1.1 christos init_source_path ();
326 1.1 christos if (*set_path != '\0')
327 1.1 christos mod_path (set_path, &source_path);
328 1.1 christos
329 1.1 christos xfree (set_path);
330 1.1 christos }
331 1.1 christos
332 1.1 christos /* Print the list of source directories.
333 1.1 christos This is used by the "ld" command, so it has the signature of a command
334 1.1 christos function. */
335 1.1 christos
336 1.1 christos static void
337 1.1 christos show_directories_1 (char *ignore, int from_tty)
338 1.1 christos {
339 1.1 christos puts_filtered ("Source directories searched: ");
340 1.1 christos puts_filtered (source_path);
341 1.1 christos puts_filtered ("\n");
342 1.1 christos }
343 1.1 christos
344 1.1 christos /* Handler for "show directories" command. */
345 1.1 christos
346 1.1 christos static void
347 1.1 christos show_directories_command (struct ui_file *file, int from_tty,
348 1.1 christos struct cmd_list_element *c, const char *value)
349 1.1 christos {
350 1.1 christos show_directories_1 (NULL, from_tty);
351 1.8 christos }
352 1.1 christos
353 1.1 christos /* See source.h. */
354 1.1 christos
355 1.1 christos void
356 1.8 christos forget_cached_source_info_for_objfile (struct objfile *objfile)
357 1.1 christos {
358 1.8 christos for (compunit_symtab *cu : objfile->compunits ())
359 1.1 christos {
360 1.8 christos for (symtab *s : compunit_filetabs (cu))
361 1.8 christos {
362 1.8 christos if (s->line_charpos != NULL)
363 1.8 christos {
364 1.8 christos xfree (s->line_charpos);
365 1.8 christos s->line_charpos = NULL;
366 1.8 christos }
367 1.8 christos if (s->fullname != NULL)
368 1.8 christos {
369 1.8 christos xfree (s->fullname);
370 1.1 christos s->fullname = NULL;
371 1.1 christos }
372 1.1 christos }
373 1.1 christos }
374 1.1 christos
375 1.1 christos if (objfile->sf)
376 1.1 christos objfile->sf->qf->forget_cached_source_info (objfile);
377 1.8 christos }
378 1.1 christos
379 1.1 christos /* See source.h. */
380 1.1 christos
381 1.1 christos void
382 1.1 christos forget_cached_source_info (void)
383 1.1 christos {
384 1.1 christos struct program_space *pspace;
385 1.8 christos
386 1.8 christos ALL_PSPACES (pspace)
387 1.8 christos for (objfile *objfile : pspace->objfiles ())
388 1.8 christos {
389 1.1 christos forget_cached_source_info_for_objfile (objfile);
390 1.8 christos }
391 1.1 christos
392 1.1 christos g_source_cache.clear ();
393 1.1 christos last_source_visited = NULL;
394 1.1 christos }
395 1.1 christos
396 1.1 christos void
397 1.1 christos init_source_path (void)
398 1.1 christos {
399 1.1 christos char buf[20];
400 1.1 christos
401 1.1 christos xsnprintf (buf, sizeof (buf), "$cdir%c$cwd", DIRNAME_SEPARATOR);
402 1.1 christos source_path = xstrdup (buf);
403 1.1 christos forget_cached_source_info ();
404 1.1 christos }
405 1.1 christos
406 1.1 christos /* Add zero or more directories to the front of the source path. */
407 1.8 christos
408 1.1 christos static void
409 1.1 christos directory_command (const char *dirname, int from_tty)
410 1.1 christos {
411 1.1 christos dont_repeat ();
412 1.1 christos /* FIXME, this goes to "delete dir"... */
413 1.1 christos if (dirname == 0)
414 1.1 christos {
415 1.1 christos if (!from_tty || query (_("Reinitialize source path to empty? ")))
416 1.1 christos {
417 1.1 christos xfree (source_path);
418 1.1 christos init_source_path ();
419 1.1 christos }
420 1.1 christos }
421 1.1 christos else
422 1.1 christos {
423 1.1 christos mod_path (dirname, &source_path);
424 1.1 christos forget_cached_source_info ();
425 1.1 christos }
426 1.1 christos if (from_tty)
427 1.1 christos show_directories_1 ((char *) 0, from_tty);
428 1.1 christos }
429 1.1 christos
430 1.1 christos /* Add a path given with the -d command line switch.
431 1.1 christos This will not be quoted so we must not treat spaces as separators. */
432 1.8 christos
433 1.1 christos void
434 1.1 christos directory_switch (const char *dirname, int from_tty)
435 1.1 christos {
436 1.1 christos add_path (dirname, &source_path, 0);
437 1.1 christos }
438 1.1 christos
439 1.1 christos /* Add zero or more directories to the front of an arbitrary path. */
440 1.8 christos
441 1.1 christos void
442 1.1 christos mod_path (const char *dirname, char **which_path)
443 1.1 christos {
444 1.1 christos add_path (dirname, which_path, 1);
445 1.1 christos }
446 1.1 christos
447 1.1 christos /* Workhorse of mod_path. Takes an extra argument to determine
448 1.1 christos if dirname should be parsed for separators that indicate multiple
449 1.1 christos directories. This allows for interfaces that pre-parse the dirname
450 1.1 christos and allow specification of traditional separator characters such
451 1.1 christos as space or tab. */
452 1.8 christos
453 1.1 christos void
454 1.1 christos add_path (const char *dirname, char **which_path, int parse_separators)
455 1.1 christos {
456 1.8 christos char *old = *which_path;
457 1.1 christos int prefix = 0;
458 1.1 christos std::vector<gdb::unique_xmalloc_ptr<char>> dir_vec;
459 1.1 christos
460 1.1 christos if (dirname == 0)
461 1.1 christos return;
462 1.1 christos
463 1.1 christos if (parse_separators)
464 1.1 christos {
465 1.8 christos /* This will properly parse the space and tab separators
466 1.1 christos and any quotes that may exist. */
467 1.8 christos gdb_argv argv (dirname);
468 1.8 christos
469 1.1 christos for (char *arg : argv)
470 1.1 christos dirnames_to_char_ptr_vec_append (&dir_vec, arg);
471 1.8 christos }
472 1.1 christos else
473 1.8 christos dir_vec.emplace_back (xstrdup (dirname));
474 1.1 christos
475 1.8 christos for (const gdb::unique_xmalloc_ptr<char> &name_up : dir_vec)
476 1.1 christos {
477 1.1 christos char *name = name_up.get ();
478 1.8 christos char *p;
479 1.1 christos struct stat st;
480 1.1 christos gdb::unique_xmalloc_ptr<char> new_name_holder;
481 1.1 christos
482 1.1 christos /* Spaces and tabs will have been removed by buildargv().
483 1.1 christos NAME is the start of the directory.
484 1.1 christos P is the '\0' following the end. */
485 1.1 christos p = name + strlen (name);
486 1.1 christos
487 1.1 christos while (!(IS_DIR_SEPARATOR (*name) && p <= name + 1) /* "/" */
488 1.1 christos #ifdef HAVE_DOS_BASED_FILE_SYSTEM
489 1.1 christos /* On MS-DOS and MS-Windows, h:\ is different from h: */
490 1.1 christos && !(p == name + 3 && name[1] == ':') /* "d:/" */
491 1.1 christos #endif
492 1.1 christos && IS_DIR_SEPARATOR (p[-1]))
493 1.1 christos /* Sigh. "foo/" => "foo" */
494 1.1 christos --p;
495 1.1 christos *p = '\0';
496 1.1 christos
497 1.1 christos while (p > name && p[-1] == '.')
498 1.1 christos {
499 1.1 christos if (p - name == 1)
500 1.1 christos {
501 1.1 christos /* "." => getwd (). */
502 1.1 christos name = current_directory;
503 1.1 christos goto append;
504 1.1 christos }
505 1.1 christos else if (p > name + 1 && IS_DIR_SEPARATOR (p[-2]))
506 1.1 christos {
507 1.1 christos if (p - name == 2)
508 1.1 christos {
509 1.1 christos /* "/." => "/". */
510 1.1 christos *--p = '\0';
511 1.1 christos goto append;
512 1.1 christos }
513 1.1 christos else
514 1.1 christos {
515 1.1 christos /* "...foo/." => "...foo". */
516 1.1 christos p -= 2;
517 1.1 christos *p = '\0';
518 1.1 christos continue;
519 1.1 christos }
520 1.1 christos }
521 1.1 christos else
522 1.1 christos break;
523 1.1 christos }
524 1.8 christos
525 1.1 christos if (name[0] == '~')
526 1.1 christos new_name_holder.reset (tilde_expand (name));
527 1.8 christos #ifdef HAVE_DOS_BASED_FILE_SYSTEM
528 1.1 christos else if (IS_ABSOLUTE_PATH (name) && p == name + 2) /* "d:" => "d:." */
529 1.1 christos new_name_holder.reset (concat (name, ".", (char *) NULL));
530 1.8 christos #endif
531 1.8 christos else if (!IS_ABSOLUTE_PATH (name) && name[0] != '$')
532 1.1 christos new_name_holder.reset (concat (current_directory, SLASH_STRING, name,
533 1.8 christos (char *) NULL));
534 1.8 christos else
535 1.1 christos new_name_holder.reset (savestring (name, p - name));
536 1.1 christos name = new_name_holder.get ();
537 1.1 christos
538 1.1 christos /* Unless it's a variable, check existence. */
539 1.1 christos if (name[0] != '$')
540 1.1 christos {
541 1.1 christos /* These are warnings, not errors, since we don't want a
542 1.1 christos non-existent directory in a .gdbinit file to stop processing
543 1.1 christos of the .gdbinit file.
544 1.1 christos
545 1.1 christos Whether they get added to the path is more debatable. Current
546 1.1 christos answer is yes, in case the user wants to go make the directory
547 1.1 christos or whatever. If the directory continues to not exist/not be
548 1.1 christos a directory/etc, then having them in the path should be
549 1.1 christos harmless. */
550 1.1 christos if (stat (name, &st) < 0)
551 1.1 christos {
552 1.1 christos int save_errno = errno;
553 1.1 christos
554 1.1 christos fprintf_unfiltered (gdb_stderr, "Warning: ");
555 1.1 christos print_sys_errmsg (name, save_errno);
556 1.1 christos }
557 1.1 christos else if ((st.st_mode & S_IFMT) != S_IFDIR)
558 1.1 christos warning (_("%s is not a directory."), name);
559 1.1 christos }
560 1.1 christos
561 1.1 christos append:
562 1.1 christos {
563 1.1 christos unsigned int len = strlen (name);
564 1.1 christos char tinybuf[2];
565 1.1 christos
566 1.1 christos p = *which_path;
567 1.1 christos while (1)
568 1.1 christos {
569 1.1 christos /* FIXME: we should use realpath() or its work-alike
570 1.1 christos before comparing. Then all the code above which
571 1.1 christos removes excess slashes and dots could simply go away. */
572 1.1 christos if (!filename_ncmp (p, name, len)
573 1.1 christos && (p[len] == '\0' || p[len] == DIRNAME_SEPARATOR))
574 1.1 christos {
575 1.1 christos /* Found it in the search path, remove old copy. */
576 1.1 christos if (p > *which_path)
577 1.1 christos {
578 1.1 christos /* Back over leading separator. */
579 1.1 christos p--;
580 1.1 christos }
581 1.1 christos if (prefix > p - *which_path)
582 1.1 christos {
583 1.1 christos /* Same dir twice in one cmd. */
584 1.1 christos goto skip_dup;
585 1.1 christos }
586 1.1 christos /* Copy from next '\0' or ':'. */
587 1.1 christos memmove (p, &p[len + 1], strlen (&p[len + 1]) + 1);
588 1.1 christos }
589 1.1 christos p = strchr (p, DIRNAME_SEPARATOR);
590 1.1 christos if (p != 0)
591 1.1 christos ++p;
592 1.1 christos else
593 1.1 christos break;
594 1.1 christos }
595 1.1 christos
596 1.1 christos tinybuf[0] = DIRNAME_SEPARATOR;
597 1.1 christos tinybuf[1] = '\0';
598 1.1 christos
599 1.1 christos /* If we have already tacked on a name(s) in this command,
600 1.1 christos be sure they stay on the front as we tack on some
601 1.1 christos more. */
602 1.1 christos if (prefix)
603 1.1 christos {
604 1.1 christos char *temp, c;
605 1.1 christos
606 1.1 christos c = old[prefix];
607 1.1 christos old[prefix] = '\0';
608 1.1 christos temp = concat (old, tinybuf, name, (char *)NULL);
609 1.1 christos old[prefix] = c;
610 1.1 christos *which_path = concat (temp, "", &old[prefix], (char *) NULL);
611 1.1 christos prefix = strlen (temp);
612 1.1 christos xfree (temp);
613 1.1 christos }
614 1.1 christos else
615 1.1 christos {
616 1.1 christos *which_path = concat (name, (old[0] ? tinybuf : old),
617 1.1 christos old, (char *)NULL);
618 1.1 christos prefix = strlen (name);
619 1.1 christos }
620 1.1 christos xfree (old);
621 1.1 christos old = *which_path;
622 1.1 christos }
623 1.1 christos skip_dup:
624 1.1 christos ;
625 1.1 christos }
626 1.1 christos }
627 1.1 christos
628 1.8 christos
629 1.1 christos static void
630 1.1 christos info_source_command (const char *ignore, int from_tty)
631 1.5 christos {
632 1.1 christos struct symtab *s = current_source_symtab;
633 1.1 christos struct compunit_symtab *cust;
634 1.1 christos
635 1.1 christos if (!s)
636 1.1 christos {
637 1.1 christos printf_filtered (_("No current source file.\n"));
638 1.5 christos return;
639 1.5 christos }
640 1.1 christos
641 1.3 christos cust = SYMTAB_COMPUNIT (s);
642 1.3 christos printf_filtered (_("Current source file is %s\n"), s->filename);
643 1.1 christos if (SYMTAB_DIRNAME (s) != NULL)
644 1.1 christos printf_filtered (_("Compilation directory is %s\n"), SYMTAB_DIRNAME (s));
645 1.1 christos if (s->fullname)
646 1.1 christos printf_filtered (_("Located in %s\n"), s->fullname);
647 1.1 christos if (s->nlines)
648 1.1 christos printf_filtered (_("Contains %d line%s.\n"), s->nlines,
649 1.1 christos s->nlines == 1 ? "" : "s");
650 1.5 christos
651 1.5 christos printf_filtered (_("Source language is %s.\n"), language_str (s->language));
652 1.5 christos printf_filtered (_("Producer is %s.\n"),
653 1.3 christos COMPUNIT_PRODUCER (cust) != NULL
654 1.5 christos ? COMPUNIT_PRODUCER (cust) : _("unknown"));
655 1.1 christos printf_filtered (_("Compiled with %s debugging format.\n"),
656 1.5 christos COMPUNIT_DEBUGFORMAT (cust));
657 1.3 christos printf_filtered (_("%s preprocessor macro info.\n"),
658 1.1 christos COMPUNIT_MACRO_TABLE (cust) != NULL
659 1.1 christos ? "Includes" : "Does not include");
660 1.1 christos }
661 1.1 christos
662 1.1 christos
664 1.1 christos /* Open a file named STRING, searching path PATH (dir names sep by some char)
665 1.1 christos using mode MODE in the calls to open. You cannot use this function to
666 1.1 christos create files (O_CREAT).
667 1.1 christos
668 1.1 christos OPTS specifies the function behaviour in specific cases.
669 1.1 christos
670 1.1 christos If OPF_TRY_CWD_FIRST, try to open ./STRING before searching PATH.
671 1.1 christos (ie pretend the first element of PATH is "."). This also indicates
672 1.1 christos that, unless OPF_SEARCH_IN_PATH is also specified, a slash in STRING
673 1.1 christos disables searching of the path (this is so that "exec-file ./foo" or
674 1.1 christos "symbol-file ./foo" insures that you get that particular version of
675 1.1 christos foo or an error message).
676 1.1 christos
677 1.1 christos If OPTS has OPF_SEARCH_IN_PATH set, absolute names will also be
678 1.1 christos searched in path (we usually want this for source files but not for
679 1.1 christos executables).
680 1.1 christos
681 1.1 christos If FILENAME_OPENED is non-null, set it to a newly allocated string naming
682 1.1 christos the actual file opened (this string will always start with a "/"). We
683 1.1 christos have to take special pains to avoid doubling the "/" between the directory
684 1.1 christos and the file, sigh! Emacs gets confuzzed by this when we print the
685 1.1 christos source file name!!!
686 1.1 christos
687 1.1 christos If OPTS has OPF_RETURN_REALPATH set return FILENAME_OPENED resolved by
688 1.1 christos gdb_realpath. Even without OPF_RETURN_REALPATH this function still returns
689 1.1 christos filename starting with "/". If FILENAME_OPENED is NULL this option has no
690 1.1 christos effect.
691 1.1 christos
692 1.1 christos If a file is found, return the descriptor.
693 1.1 christos Otherwise, return -1, with errno set for the last name we tried to open. */
694 1.1 christos
695 1.8 christos /* >>>> This should only allow files of certain types,
696 1.8 christos >>>> eg executable, non-directory. */
697 1.1 christos int
698 1.1 christos openp (const char *path, openp_flags opts, const char *string,
699 1.1 christos int mode, gdb::unique_xmalloc_ptr<char> *filename_opened)
700 1.1 christos {
701 1.6 christos int fd;
702 1.6 christos char *filename;
703 1.6 christos int alloclen;
704 1.8 christos /* The errno set for the last name we tried to open (and
705 1.1 christos failed). */
706 1.1 christos int last_errno = 0;
707 1.1 christos std::vector<gdb::unique_xmalloc_ptr<char>> dir_vec;
708 1.1 christos
709 1.1 christos /* The open syscall MODE parameter is not specified. */
710 1.1 christos gdb_assert ((mode & O_CREAT) == 0);
711 1.1 christos gdb_assert (string != NULL);
712 1.1 christos
713 1.1 christos /* A file with an empty name cannot possibly exist. Report a failure
714 1.1 christos without further checking.
715 1.1 christos
716 1.1 christos This is an optimization which also defends us against buggy
717 1.1 christos implementations of the "stat" function. For instance, we have
718 1.1 christos noticed that a MinGW debugger built on Windows XP 32bits crashes
719 1.1 christos when the debugger is started with an empty argument. */
720 1.1 christos if (string[0] == '\0')
721 1.1 christos {
722 1.1 christos errno = ENOENT;
723 1.1 christos return -1;
724 1.1 christos }
725 1.1 christos
726 1.1 christos if (!path)
727 1.1 christos path = ".";
728 1.1 christos
729 1.1 christos mode |= O_BINARY;
730 1.6 christos
731 1.1 christos if ((opts & OPF_TRY_CWD_FIRST) || IS_ABSOLUTE_PATH (string))
732 1.6 christos {
733 1.1 christos int i, reg_file_errno;
734 1.6 christos
735 1.1 christos if (is_regular_file (string, ®_file_errno))
736 1.1 christos {
737 1.1 christos filename = (char *) alloca (strlen (string) + 1);
738 1.1 christos strcpy (filename, string);
739 1.6 christos fd = gdb_open_cloexec (filename, mode, 0);
740 1.1 christos if (fd >= 0)
741 1.1 christos goto done;
742 1.1 christos last_errno = errno;
743 1.1 christos }
744 1.1 christos else
745 1.6 christos {
746 1.1 christos filename = NULL;
747 1.1 christos fd = -1;
748 1.1 christos last_errno = reg_file_errno;
749 1.1 christos }
750 1.1 christos
751 1.1 christos if (!(opts & OPF_SEARCH_IN_PATH))
752 1.1 christos for (i = 0; string[i]; i++)
753 1.1 christos if (IS_DIR_SEPARATOR (string[i]))
754 1.1 christos goto done;
755 1.1 christos }
756 1.1 christos
757 1.1 christos /* For dos paths, d:/foo -> /foo, and d:foo -> foo. */
758 1.1 christos if (HAS_DRIVE_SPEC (string))
759 1.1 christos string = STRIP_DRIVE_SPEC (string);
760 1.1 christos
761 1.1 christos /* /foo => foo, to avoid multiple slashes that Emacs doesn't like. */
762 1.1 christos while (IS_DIR_SEPARATOR(string[0]))
763 1.1 christos string++;
764 1.1 christos
765 1.1 christos /* ./foo => foo */
766 1.1 christos while (string[0] == '.' && IS_DIR_SEPARATOR (string[1]))
767 1.6 christos string += 2;
768 1.1 christos
769 1.6 christos alloclen = strlen (path) + strlen (string) + 2;
770 1.1 christos filename = (char *) alloca (alloclen);
771 1.1 christos fd = -1;
772 1.1 christos last_errno = ENOENT;
773 1.8 christos
774 1.1 christos dir_vec = dirnames_to_char_ptr_vec (path);
775 1.8 christos
776 1.1 christos for (const gdb::unique_xmalloc_ptr<char> &dir_up : dir_vec)
777 1.6 christos {
778 1.1 christos char *dir = dir_up.get ();
779 1.1 christos size_t len = strlen (dir);
780 1.1 christos int reg_file_errno;
781 1.1 christos
782 1.1 christos if (strcmp (dir, "$cwd") == 0)
783 1.1 christos {
784 1.1 christos /* Name is $cwd -- insert current directory name instead. */
785 1.1 christos int newlen;
786 1.1 christos
787 1.1 christos /* First, realloc the filename buffer if too short. */
788 1.1 christos len = strlen (current_directory);
789 1.1 christos newlen = len + strlen (string) + 2;
790 1.6 christos if (newlen > alloclen)
791 1.1 christos {
792 1.1 christos alloclen = newlen;
793 1.1 christos filename = (char *) alloca (alloclen);
794 1.1 christos }
795 1.1 christos strcpy (filename, current_directory);
796 1.1 christos }
797 1.1 christos else if (strchr(dir, '~'))
798 1.1 christos {
799 1.8 christos /* See whether we need to expand the tilde. */
800 1.1 christos int newlen;
801 1.1 christos
802 1.8 christos gdb::unique_xmalloc_ptr<char> tilde_expanded (tilde_expand (dir));
803 1.1 christos
804 1.1 christos /* First, realloc the filename buffer if too short. */
805 1.1 christos len = strlen (tilde_expanded.get ());
806 1.1 christos newlen = len + strlen (string) + 2;
807 1.6 christos if (newlen > alloclen)
808 1.1 christos {
809 1.8 christos alloclen = newlen;
810 1.1 christos filename = (char *) alloca (alloclen);
811 1.1 christos }
812 1.1 christos strcpy (filename, tilde_expanded.get ());
813 1.1 christos }
814 1.1 christos else
815 1.1 christos {
816 1.1 christos /* Normal file name in path -- just use it. */
817 1.1 christos strcpy (filename, dir);
818 1.1 christos
819 1.1 christos /* Don't search $cdir. It's also a magic path like $cwd, but we
820 1.1 christos don't have enough information to expand it. The user *could*
821 1.1 christos have an actual directory named '$cdir' but handling that would
822 1.1 christos be confusing, it would mean different things in different
823 1.1 christos contexts. If the user really has '$cdir' one can use './$cdir'.
824 1.1 christos We can get $cdir when loading scripts. When loading source files
825 1.1 christos $cdir must have already been expanded to the correct value. */
826 1.1 christos if (strcmp (dir, "$cdir") == 0)
827 1.1 christos continue;
828 1.1 christos }
829 1.1 christos
830 1.1 christos /* Remove trailing slashes. */
831 1.1 christos while (len > 0 && IS_DIR_SEPARATOR (filename[len - 1]))
832 1.1 christos filename[--len] = 0;
833 1.1 christos
834 1.6 christos strcat (filename + len, SLASH_STRING);
835 1.1 christos strcat (filename, string);
836 1.1 christos
837 1.1 christos if (is_regular_file (filename, ®_file_errno))
838 1.1 christos {
839 1.6 christos fd = gdb_open_cloexec (filename, mode, 0);
840 1.1 christos if (fd >= 0)
841 1.6 christos break;
842 1.6 christos last_errno = errno;
843 1.1 christos }
844 1.1 christos else
845 1.1 christos last_errno = reg_file_errno;
846 1.1 christos }
847 1.1 christos
848 1.1 christos done:
849 1.1 christos if (filename_opened)
850 1.8 christos {
851 1.1 christos /* If a file was opened, canonicalize its filename. */
852 1.1 christos if (fd < 0)
853 1.1 christos filename_opened->reset (NULL);
854 1.1 christos else if ((opts & OPF_RETURN_REALPATH) != 0)
855 1.1 christos *filename_opened = gdb_realpath (filename);
856 1.1 christos else
857 1.6 christos *filename_opened = gdb_abspath (filename);
858 1.1 christos }
859 1.1 christos
860 1.1 christos errno = last_errno;
861 1.1 christos return fd;
862 1.1 christos }
863 1.1 christos
864 1.1 christos
865 1.1 christos /* This is essentially a convenience, for clients that want the behaviour
866 1.1 christos of openp, using source_path, but that really don't want the file to be
867 1.1 christos opened but want instead just to know what the full pathname is (as
868 1.1 christos qualified against source_path).
869 1.1 christos
870 1.1 christos The current working directory is searched first.
871 1.1 christos
872 1.1 christos If the file was found, this function returns 1, and FULL_PATHNAME is
873 1.1 christos set to the fully-qualified pathname.
874 1.8 christos
875 1.8 christos Else, this functions returns 0, and FULL_PATHNAME is set to NULL. */
876 1.1 christos int
877 1.1 christos source_full_path_of (const char *filename,
878 1.1 christos gdb::unique_xmalloc_ptr<char> *full_pathname)
879 1.1 christos {
880 1.1 christos int fd;
881 1.1 christos
882 1.1 christos fd = openp (source_path,
883 1.1 christos OPF_TRY_CWD_FIRST | OPF_SEARCH_IN_PATH | OPF_RETURN_REALPATH,
884 1.8 christos filename, O_RDONLY, full_pathname);
885 1.1 christos if (fd < 0)
886 1.1 christos {
887 1.1 christos full_pathname->reset (NULL);
888 1.1 christos return 0;
889 1.1 christos }
890 1.1 christos
891 1.1 christos close (fd);
892 1.1 christos return 1;
893 1.1 christos }
894 1.1 christos
895 1.1 christos /* Return non-zero if RULE matches PATH, that is if the rule can be
896 1.1 christos applied to PATH. */
897 1.1 christos
898 1.1 christos static int
899 1.1 christos substitute_path_rule_matches (const struct substitute_path_rule *rule,
900 1.1 christos const char *path)
901 1.1 christos {
902 1.1 christos const int from_len = strlen (rule->from);
903 1.1 christos const int path_len = strlen (path);
904 1.1 christos
905 1.1 christos if (path_len < from_len)
906 1.3 christos return 0;
907 1.1 christos
908 1.3 christos /* The substitution rules are anchored at the start of the path,
909 1.1 christos so the path should start with rule->from. */
910 1.1 christos
911 1.1 christos if (filename_ncmp (path, rule->from, from_len) != 0)
912 1.1 christos return 0;
913 1.1 christos
914 1.3 christos /* Make sure that the region in the path that matches the substitution
915 1.1 christos rule is immediately followed by a directory separator (or the end of
916 1.1 christos string character). */
917 1.1 christos
918 1.1 christos if (path[from_len] != '\0' && !IS_DIR_SEPARATOR (path[from_len]))
919 1.1 christos return 0;
920 1.1 christos
921 1.1 christos return 1;
922 1.1 christos }
923 1.1 christos
924 1.1 christos /* Find the substitute-path rule that applies to PATH and return it.
925 1.1 christos Return NULL if no rule applies. */
926 1.1 christos
927 1.1 christos static struct substitute_path_rule *
928 1.1 christos get_substitute_path_rule (const char *path)
929 1.1 christos {
930 1.1 christos struct substitute_path_rule *rule = substitute_path_rules;
931 1.1 christos
932 1.1 christos while (rule != NULL && !substitute_path_rule_matches (rule, path))
933 1.1 christos rule = rule->next;
934 1.1 christos
935 1.1 christos return rule;
936 1.8 christos }
937 1.8 christos
938 1.1 christos /* If the user specified a source path substitution rule that applies
939 1.1 christos to PATH, then apply it and return the new path.
940 1.8 christos
941 1.8 christos Return NULL if no substitution rule was specified by the user,
942 1.1 christos or if no rule applied to the given PATH. */
943 1.1 christos
944 1.1 christos gdb::unique_xmalloc_ptr<char>
945 1.1 christos rewrite_source_path (const char *path)
946 1.1 christos {
947 1.1 christos const struct substitute_path_rule *rule = get_substitute_path_rule (path);
948 1.1 christos char *new_path;
949 1.1 christos int from_len;
950 1.1 christos
951 1.1 christos if (rule == NULL)
952 1.1 christos return NULL;
953 1.1 christos
954 1.1 christos from_len = strlen (rule->from);
955 1.1 christos
956 1.1 christos /* Compute the rewritten path and return it. */
957 1.1 christos
958 1.1 christos new_path =
959 1.1 christos (char *) xmalloc (strlen (path) + 1 + strlen (rule->to) - from_len);
960 1.8 christos strcpy (new_path, rule->to);
961 1.1 christos strcat (new_path, path + from_len);
962 1.1 christos
963 1.8 christos return gdb::unique_xmalloc_ptr<char> (new_path);
964 1.8 christos }
965 1.8 christos
966 1.1 christos /* See source.h. */
967 1.1 christos
968 1.8 christos scoped_fd
969 1.1 christos find_and_open_source (const char *filename,
970 1.1 christos const char *dirname,
971 1.1 christos gdb::unique_xmalloc_ptr<char> *fullname)
972 1.1 christos {
973 1.1 christos char *path = source_path;
974 1.1 christos const char *p;
975 1.1 christos int result;
976 1.1 christos
977 1.1 christos /* Quick way out if we already know its full name. */
978 1.1 christos
979 1.1 christos if (*fullname)
980 1.1 christos {
981 1.8 christos /* The user may have requested that source paths be rewritten
982 1.8 christos according to substitution rules he provided. If a substitution
983 1.1 christos rule applies to this path, then apply it. */
984 1.1 christos gdb::unique_xmalloc_ptr<char> rewritten_fullname
985 1.8 christos = rewrite_source_path (fullname->get ());
986 1.1 christos
987 1.8 christos if (rewritten_fullname != NULL)
988 1.1 christos *fullname = std::move (rewritten_fullname);
989 1.1 christos
990 1.8 christos result = gdb_open_cloexec (fullname->get (), OPEN_MODE, 0);
991 1.8 christos if (result >= 0)
992 1.1 christos {
993 1.1 christos *fullname = gdb_realpath (fullname->get ());
994 1.1 christos return scoped_fd (result);
995 1.8 christos }
996 1.1 christos
997 1.1 christos /* Didn't work -- free old one, try again. */
998 1.8 christos fullname->reset (NULL);
999 1.1 christos }
1000 1.1 christos
1001 1.1 christos gdb::unique_xmalloc_ptr<char> rewritten_dirname;
1002 1.1 christos if (dirname != NULL)
1003 1.1 christos {
1004 1.8 christos /* If necessary, rewrite the compilation directory name according
1005 1.1 christos to the source path substitution rules specified by the user. */
1006 1.1 christos
1007 1.8 christos rewritten_dirname = rewrite_source_path (dirname);
1008 1.8 christos
1009 1.1 christos if (rewritten_dirname != NULL)
1010 1.1 christos dirname = rewritten_dirname.get ();
1011 1.1 christos
1012 1.1 christos /* Replace a path entry of $cdir with the compilation directory
1013 1.1 christos name. */
1014 1.1 christos #define cdir_len 5
1015 1.1 christos /* We cast strstr's result in case an ANSIhole has made it const,
1016 1.1 christos which produces a "required warning" when assigned to a nonconst. */
1017 1.1 christos p = (char *) strstr (source_path, "$cdir");
1018 1.1 christos if (p && (p == path || p[-1] == DIRNAME_SEPARATOR)
1019 1.1 christos && (p[cdir_len] == DIRNAME_SEPARATOR || p[cdir_len] == '\0'))
1020 1.1 christos {
1021 1.1 christos int len;
1022 1.1 christos
1023 1.1 christos path = (char *)
1024 1.1 christos alloca (strlen (source_path) + 1 + strlen (dirname) + 1);
1025 1.1 christos len = p - source_path;
1026 1.1 christos strncpy (path, source_path, len); /* Before $cdir */
1027 1.1 christos strcpy (path + len, dirname); /* new stuff */
1028 1.1 christos strcat (path + len, source_path + len + cdir_len); /* After
1029 1.1 christos $cdir */
1030 1.8 christos }
1031 1.1 christos }
1032 1.1 christos
1033 1.1 christos gdb::unique_xmalloc_ptr<char> rewritten_filename;
1034 1.1 christos if (IS_ABSOLUTE_PATH (filename))
1035 1.8 christos {
1036 1.1 christos /* If filename is absolute path, try the source path
1037 1.1 christos substitution on it. */
1038 1.8 christos rewritten_filename = rewrite_source_path (filename);
1039 1.1 christos
1040 1.1 christos if (rewritten_filename != NULL)
1041 1.1 christos filename = rewritten_filename.get ();
1042 1.1 christos }
1043 1.1 christos
1044 1.1 christos result = openp (path, OPF_SEARCH_IN_PATH | OPF_RETURN_REALPATH, filename,
1045 1.1 christos OPEN_MODE, fullname);
1046 1.1 christos if (result < 0)
1047 1.1 christos {
1048 1.1 christos /* Didn't work. Try using just the basename. */
1049 1.1 christos p = lbasename (filename);
1050 1.1 christos if (p != filename)
1051 1.1 christos result = openp (path, OPF_SEARCH_IN_PATH | OPF_RETURN_REALPATH, p,
1052 1.8 christos OPEN_MODE, fullname);
1053 1.1 christos }
1054 1.1 christos
1055 1.1 christos return scoped_fd (result);
1056 1.1 christos }
1057 1.1 christos
1058 1.1 christos /* Open a source file given a symtab S. Returns a file descriptor or
1059 1.1 christos negative number for error.
1060 1.8 christos
1061 1.1 christos This function is a convience function to find_and_open_source. */
1062 1.1 christos
1063 1.1 christos scoped_fd
1064 1.8 christos open_source_file (struct symtab *s)
1065 1.1 christos {
1066 1.8 christos if (!s)
1067 1.8 christos return scoped_fd (-1);
1068 1.8 christos
1069 1.8 christos gdb::unique_xmalloc_ptr<char> fullname (s->fullname);
1070 1.8 christos s->fullname = NULL;
1071 1.8 christos scoped_fd fd = find_and_open_source (s->filename, SYMTAB_DIRNAME (s),
1072 1.1 christos &fullname);
1073 1.1 christos s->fullname = fullname.release ();
1074 1.1 christos return fd;
1075 1.1 christos }
1076 1.1 christos
1077 1.1 christos /* Finds the fullname that a symtab represents.
1078 1.1 christos
1079 1.1 christos This functions finds the fullname and saves it in s->fullname.
1080 1.1 christos It will also return the value.
1081 1.1 christos
1082 1.1 christos If this function fails to find the file that this symtab represents,
1083 1.1 christos the expected fullname is used. Therefore the files does not have to
1084 1.1 christos exist. */
1085 1.1 christos
1086 1.1 christos const char *
1087 1.1 christos symtab_to_fullname (struct symtab *s)
1088 1.1 christos {
1089 1.1 christos /* Use cached copy if we have it.
1090 1.1 christos We rely on forget_cached_source_info being called appropriately
1091 1.8 christos to handle cases like the file being moved. */
1092 1.1 christos if (s->fullname == NULL)
1093 1.8 christos {
1094 1.1 christos scoped_fd fd = open_source_file (s);
1095 1.8 christos
1096 1.1 christos if (fd.get () < 0)
1097 1.1 christos {
1098 1.1 christos gdb::unique_xmalloc_ptr<char> fullname;
1099 1.1 christos
1100 1.3 christos /* rewrite_source_path would be applied by find_and_open_source, we
1101 1.8 christos should report the pathname where GDB tried to find the file. */
1102 1.1 christos
1103 1.8 christos if (SYMTAB_DIRNAME (s) == NULL || IS_ABSOLUTE_PATH (s->filename))
1104 1.8 christos fullname.reset (xstrdup (s->filename));
1105 1.1 christos else
1106 1.8 christos fullname.reset (concat (SYMTAB_DIRNAME (s), SLASH_STRING,
1107 1.1 christos s->filename, (char *) NULL));
1108 1.8 christos
1109 1.1 christos s->fullname = rewrite_source_path (fullname.get ()).release ();
1110 1.1 christos if (s->fullname == NULL)
1111 1.1 christos s->fullname = fullname.release ();
1112 1.1 christos }
1113 1.1 christos }
1114 1.1 christos
1115 1.1 christos return s->fullname;
1116 1.1 christos }
1117 1.1 christos
1118 1.1 christos /* See commentary in source.h. */
1119 1.1 christos
1120 1.1 christos const char *
1121 1.1 christos symtab_to_filename_for_display (struct symtab *symtab)
1122 1.1 christos {
1123 1.1 christos if (filename_display_string == filename_display_basename)
1124 1.1 christos return lbasename (symtab->filename);
1125 1.1 christos else if (filename_display_string == filename_display_absolute)
1126 1.1 christos return symtab_to_fullname (symtab);
1127 1.1 christos else if (filename_display_string == filename_display_relative)
1128 1.1 christos return symtab->filename;
1129 1.1 christos else
1130 1.1 christos internal_error (__FILE__, __LINE__, _("invalid filename_display_string"));
1131 1.1 christos }
1132 1.1 christos
1133 1.1 christos /* Create and initialize the table S->line_charpos that records
1135 1.1 christos the positions of the lines in the source file, which is assumed
1136 1.1 christos to be open on descriptor DESC.
1137 1.1 christos All set S->nlines to the number of such lines. */
1138 1.1 christos
1139 1.8 christos void
1140 1.1 christos find_source_lines (struct symtab *s, int desc)
1141 1.1 christos {
1142 1.1 christos struct stat st;
1143 1.1 christos char *p, *end;
1144 1.1 christos int nlines = 0;
1145 1.1 christos int lines_allocated = 1000;
1146 1.1 christos int *line_charpos;
1147 1.6 christos long mtime = 0;
1148 1.1 christos int size;
1149 1.1 christos
1150 1.1 christos gdb_assert (s);
1151 1.3 christos line_charpos = XNEWVEC (int, lines_allocated);
1152 1.3 christos if (fstat (desc, &st) < 0)
1153 1.1 christos perror_with_name (symtab_to_filename_for_display (s));
1154 1.1 christos
1155 1.1 christos if (SYMTAB_OBJFILE (s) != NULL && SYMTAB_OBJFILE (s)->obfd != NULL)
1156 1.1 christos mtime = SYMTAB_OBJFILE (s)->mtime;
1157 1.1 christos else if (exec_bfd)
1158 1.1 christos mtime = exec_bfd_mtime;
1159 1.1 christos
1160 1.1 christos if (mtime && mtime < st.st_mtime)
1161 1.1 christos warning (_("Source file is more recent than executable."));
1162 1.1 christos
1163 1.1 christos {
1164 1.8 christos /* st_size might be a large type, but we only support source files whose
1165 1.8 christos size fits in an int. */
1166 1.8 christos size = (int) st.st_size;
1167 1.1 christos
1168 1.1 christos /* Use the heap, not the stack, because this may be pretty large,
1169 1.8 christos and we may run into various kinds of limits on stack size. */
1170 1.1 christos gdb::def_vector<char> data (size);
1171 1.1 christos
1172 1.8 christos /* Reassign `size' to result of read for systems where \r\n -> \n. */
1173 1.8 christos size = myread (desc, data.data (), size);
1174 1.1 christos if (size < 0)
1175 1.1 christos perror_with_name (symtab_to_filename_for_display (s));
1176 1.1 christos end = data.data () + size;
1177 1.1 christos p = &data[0];
1178 1.1 christos line_charpos[0] = 0;
1179 1.1 christos nlines = 1;
1180 1.1 christos while (p != end)
1181 1.1 christos {
1182 1.1 christos if (*p++ == '\n'
1183 1.1 christos /* A newline at the end does not start a new line. */
1184 1.1 christos && p != end)
1185 1.1 christos {
1186 1.1 christos if (nlines == lines_allocated)
1187 1.1 christos {
1188 1.1 christos lines_allocated *= 2;
1189 1.8 christos line_charpos =
1190 1.1 christos (int *) xrealloc ((char *) line_charpos,
1191 1.1 christos sizeof (int) * lines_allocated);
1192 1.1 christos }
1193 1.1 christos line_charpos[nlines++] = p - data.data ();
1194 1.1 christos }
1195 1.1 christos }
1196 1.1 christos }
1197 1.1 christos
1198 1.1 christos s->nlines = nlines;
1199 1.1 christos s->line_charpos =
1200 1.1 christos (int *) xrealloc ((char *) line_charpos, nlines * sizeof (int));
1201 1.1 christos
1202 1.1 christos }
1203 1.1 christos
1204 1.1 christos
1205 1.1 christos
1207 1.1 christos /* Get full pathname and line number positions for a symtab.
1208 1.1 christos Set *FULLNAME to actual name of the file as found by `openp',
1209 1.8 christos or to 0 if the file is not found. */
1210 1.8 christos
1211 1.1 christos static void
1212 1.1 christos get_filename_and_charpos (struct symtab *s, char **fullname)
1213 1.1 christos {
1214 1.8 christos scoped_fd desc = open_source_file (s);
1215 1.1 christos if (desc.get () < 0)
1216 1.1 christos {
1217 1.1 christos if (fullname)
1218 1.1 christos *fullname = NULL;
1219 1.8 christos return;
1220 1.1 christos }
1221 1.1 christos if (fullname)
1222 1.8 christos *fullname = s->fullname;
1223 1.1 christos if (s->line_charpos == 0)
1224 1.1 christos find_source_lines (s, desc.get ());
1225 1.1 christos }
1226 1.1 christos
1227 1.1 christos /* See source.h. */
1228 1.1 christos
1229 1.1 christos int
1230 1.1 christos identify_source_line (struct symtab *s, int line, int mid_statement,
1231 1.1 christos CORE_ADDR pc)
1232 1.1 christos {
1233 1.1 christos if (s->line_charpos == 0)
1234 1.1 christos get_filename_and_charpos (s, (char **) NULL);
1235 1.1 christos if (s->fullname == 0)
1236 1.3 christos return 0;
1237 1.1 christos if (line > s->nlines)
1238 1.1 christos /* Don't index off the end of the line_charpos array. */
1239 1.1 christos return 0;
1240 1.3 christos annotate_source (s->fullname, line, s->line_charpos[line - 1],
1241 1.1 christos mid_statement, get_objfile_arch (SYMTAB_OBJFILE (s)), pc);
1242 1.1 christos
1243 1.1 christos current_source_line = line;
1244 1.1 christos current_source_symtab = s;
1245 1.1 christos clear_lines_listed_range ();
1246 1.1 christos return 1;
1247 1.1 christos }
1248 1.1 christos
1249 1.1 christos
1251 1.1 christos /* Print source lines from the file of symtab S,
1252 1.8 christos starting with line number LINE and stopping before line number STOPLINE. */
1253 1.1 christos
1254 1.1 christos static void
1255 1.1 christos print_source_lines_base (struct symtab *s, int line, int stopline,
1256 1.1 christos print_source_lines_flags flags)
1257 1.1 christos {
1258 1.1 christos scoped_fd desc;
1259 1.1 christos int noprint = 0;
1260 1.1 christos int nlines = stopline - line;
1261 1.1 christos struct ui_out *uiout = current_uiout;
1262 1.1 christos
1263 1.1 christos /* Regardless of whether we can open the file, set current_source_symtab. */
1264 1.7 christos current_source_symtab = s;
1265 1.1 christos current_source_line = line;
1266 1.1 christos first_line_listed = line;
1267 1.1 christos
1268 1.1 christos /* If printing of source lines is disabled, just print file and line
1269 1.1 christos number. */
1270 1.1 christos if (uiout->test_flags (ui_source_list))
1271 1.8 christos {
1272 1.8 christos /* Only prints "No such file or directory" once. */
1273 1.8 christos if ((s != last_source_visited) || (!last_source_error))
1274 1.8 christos {
1275 1.8 christos last_source_visited = s;
1276 1.1 christos desc = open_source_file (s);
1277 1.1 christos if (desc.get () < 0)
1278 1.1 christos {
1279 1.1 christos last_source_error = desc.get ();
1280 1.8 christos noprint = 1;
1281 1.1 christos }
1282 1.1 christos }
1283 1.1 christos else
1284 1.1 christos {
1285 1.6 christos flags |= PRINT_SOURCE_LINES_NOERROR;
1286 1.1 christos noprint = 1;
1287 1.1 christos }
1288 1.1 christos }
1289 1.8 christos else
1290 1.1 christos {
1291 1.1 christos flags |= PRINT_SOURCE_LINES_NOERROR;
1292 1.1 christos noprint = 1;
1293 1.1 christos }
1294 1.1 christos
1295 1.6 christos if (noprint)
1296 1.1 christos {
1297 1.1 christos if (!(flags & PRINT_SOURCE_LINES_NOERROR))
1298 1.1 christos {
1299 1.1 christos const char *filename = symtab_to_filename_for_display (s);
1300 1.1 christos int len = strlen (filename) + 100;
1301 1.1 christos char *name = (char *) alloca (len);
1302 1.7 christos
1303 1.7 christos xsnprintf (name, len, "%d\t%s", line, filename);
1304 1.1 christos print_sys_errmsg (name, errno);
1305 1.1 christos }
1306 1.1 christos else
1307 1.1 christos {
1308 1.1 christos uiout->field_int ("line", line);
1309 1.7 christos uiout->text ("\tin ");
1310 1.8 christos
1311 1.8 christos /* CLI expects only the "file" field. TUI expects only the
1312 1.7 christos "fullname" field (and TUI does break if "file" is printed).
1313 1.1 christos MI expects both fields. ui_source_list is set only for CLI,
1314 1.1 christos not for TUI. */
1315 1.1 christos if (uiout->is_mi_like_p () || uiout->test_flags (ui_source_list))
1316 1.1 christos uiout->field_string ("file", symtab_to_filename_for_display (s),
1317 1.1 christos ui_out_style_kind::FILE);
1318 1.1 christos if (uiout->is_mi_like_p () || !uiout->test_flags (ui_source_list))
1319 1.1 christos {
1320 1.6 christos const char *s_fullname = symtab_to_fullname (s);
1321 1.1 christos char *local_fullname;
1322 1.1 christos
1323 1.7 christos /* ui_out_field_string may free S_FULLNAME by calling
1324 1.1 christos open_source_file for it again. See e.g.,
1325 1.1 christos tui_field_string->tui_show_source. */
1326 1.7 christos local_fullname = (char *) alloca (strlen (s_fullname) + 1);
1327 1.1 christos strcpy (local_fullname, s_fullname);
1328 1.1 christos
1329 1.1 christos uiout->field_string ("fullname", local_fullname);
1330 1.1 christos }
1331 1.1 christos
1332 1.1 christos uiout->text ("\n");
1333 1.1 christos }
1334 1.8 christos
1335 1.8 christos return;
1336 1.8 christos }
1337 1.8 christos
1338 1.1 christos last_source_error = 0;
1339 1.8 christos
1340 1.8 christos /* If the user requested a sequence of lines that seems to go backward
1341 1.8 christos (from high to low line numbers) then we don't print anything. */
1342 1.8 christos if (stopline <= line)
1343 1.1 christos return;
1344 1.8 christos
1345 1.8 christos std::string lines;
1346 1.1 christos if (!g_source_cache.get_source_lines (s, line, stopline - 1, &lines))
1347 1.1 christos error (_("Line number %d out of range; %s has %d lines."),
1348 1.1 christos line, symtab_to_filename_for_display (s), s->nlines);
1349 1.1 christos
1350 1.1 christos const char *iter = lines.c_str ();
1351 1.1 christos while (nlines-- > 0 && *iter != '\0')
1352 1.7 christos {
1353 1.7 christos char buf[20];
1354 1.1 christos
1355 1.1 christos last_line_listed = current_source_line;
1356 1.7 christos if (flags & PRINT_SOURCE_LINES_FILENAME)
1357 1.8 christos {
1358 1.8 christos uiout->text (symtab_to_filename_for_display (s));
1359 1.1 christos uiout->text (":");
1360 1.8 christos }
1361 1.8 christos xsnprintf (buf, sizeof (buf), "%d\t", current_source_line++);
1362 1.8 christos uiout->text (buf);
1363 1.8 christos
1364 1.8 christos while (*iter != '\0')
1365 1.8 christos {
1366 1.8 christos /* Find a run of characters that can be emitted at once.
1367 1.8 christos This is done so that escape sequences are kept
1368 1.8 christos together. */
1369 1.8 christos const char *start = iter;
1370 1.8 christos while (true)
1371 1.8 christos {
1372 1.8 christos int skip_bytes;
1373 1.8 christos
1374 1.8 christos char c = *iter;
1375 1.8 christos if (c == '\033' && skip_ansi_escape (iter, &skip_bytes))
1376 1.8 christos iter += skip_bytes;
1377 1.8 christos else if (c >= 0 && c < 040 && c != '\t')
1378 1.8 christos break;
1379 1.1 christos else if (c == 0177)
1380 1.8 christos break;
1381 1.8 christos else
1382 1.8 christos ++iter;
1383 1.8 christos }
1384 1.8 christos if (iter > start)
1385 1.8 christos {
1386 1.8 christos std::string text (start, iter);
1387 1.8 christos uiout->text (text.c_str ());
1388 1.8 christos }
1389 1.8 christos if (*iter == '\r')
1390 1.1 christos {
1391 1.8 christos /* Treat either \r or \r\n as a single newline. */
1392 1.1 christos ++iter;
1393 1.8 christos if (*iter == '\n')
1394 1.8 christos ++iter;
1395 1.1 christos break;
1396 1.8 christos }
1397 1.1 christos else if (*iter == '\n')
1398 1.8 christos {
1399 1.7 christos ++iter;
1400 1.8 christos break;
1401 1.8 christos }
1402 1.8 christos else if (*iter > 0 && *iter < 040)
1403 1.8 christos {
1404 1.8 christos xsnprintf (buf, sizeof (buf), "^%c", *iter + 0100);
1405 1.8 christos uiout->text (buf);
1406 1.1 christos ++iter;
1407 1.1 christos }
1408 1.8 christos else if (*iter == 0177)
1409 1.1 christos {
1410 1.1 christos uiout->text ("^?");
1411 1.1 christos ++iter;
1412 1.8 christos }
1413 1.8 christos }
1414 1.1 christos uiout->text ("\n");
1415 1.1 christos }
1416 1.1 christos }
1417 1.6 christos
1418 1.1 christos
1420 1.1 christos /* See source.h. */
1421 1.8 christos
1422 1.8 christos void
1423 1.8 christos print_source_lines (struct symtab *s, int line, int stopline,
1424 1.8 christos print_source_lines_flags flags)
1425 1.8 christos {
1426 1.8 christos print_source_lines_base (s, line, stopline, flags);
1427 1.8 christos }
1428 1.8 christos
1429 1.8 christos /* See source.h. */
1430 1.8 christos
1431 1.8 christos void
1432 1.8 christos print_source_lines (struct symtab *s, source_lines_range line_range,
1433 1.1 christos print_source_lines_flags flags)
1434 1.1 christos {
1435 1.1 christos print_source_lines_base (s, line_range.startline (),
1436 1.1 christos line_range.stopline (), flags);
1437 1.8 christos }
1438 1.1 christos
1439 1.1 christos
1440 1.1 christos
1441 1.8 christos /* Print info on range of pc's in a specified line. */
1443 1.8 christos
1444 1.1 christos static void
1445 1.1 christos info_line_command (const char *arg, int from_tty)
1446 1.1 christos {
1447 1.8 christos CORE_ADDR start_pc, end_pc;
1448 1.8 christos
1449 1.3 christos std::vector<symtab_and_line> decoded_sals;
1450 1.8 christos symtab_and_line curr_sal;
1451 1.3 christos gdb::array_view<symtab_and_line> sals;
1452 1.8 christos
1453 1.3 christos if (arg == 0)
1454 1.8 christos {
1455 1.1 christos curr_sal.symtab = current_source_symtab;
1456 1.1 christos curr_sal.pspace = current_program_space;
1457 1.1 christos if (last_line_listed != 0)
1458 1.8 christos curr_sal.line = last_line_listed;
1459 1.8 christos else
1460 1.8 christos curr_sal.line = current_source_line;
1461 1.1 christos
1462 1.1 christos sals = curr_sal;
1463 1.1 christos }
1464 1.1 christos else
1465 1.1 christos {
1466 1.1 christos decoded_sals = decode_line_with_last_displayed (arg,
1467 1.8 christos DECODE_LINE_LIST_MODE);
1468 1.1 christos sals = decoded_sals;
1469 1.1 christos
1470 1.1 christos dont_repeat ();
1471 1.1 christos }
1472 1.1 christos
1473 1.1 christos /* C++ More than one line may have been specified, as when the user
1474 1.1 christos specifies an overloaded function name. Print info on them all. */
1475 1.1 christos for (const auto &sal : sals)
1476 1.1 christos {
1477 1.1 christos if (sal.pspace != current_program_space)
1478 1.1 christos continue;
1479 1.1 christos
1480 1.1 christos if (sal.symtab == 0)
1481 1.1 christos {
1482 1.1 christos struct gdbarch *gdbarch = get_current_arch ();
1483 1.1 christos
1484 1.1 christos printf_filtered (_("No line number information available"));
1485 1.1 christos if (sal.pc != 0)
1486 1.1 christos {
1487 1.1 christos /* This is useful for "info line *0x7f34". If we can't tell the
1488 1.1 christos user about a source line, at least let them have the symbolic
1489 1.1 christos address. */
1490 1.1 christos printf_filtered (" for address ");
1491 1.1 christos wrap_here (" ");
1492 1.1 christos print_address (gdbarch, sal.pc, gdb_stdout);
1493 1.3 christos }
1494 1.3 christos else
1495 1.1 christos printf_filtered (".");
1496 1.1 christos printf_filtered ("\n");
1497 1.1 christos }
1498 1.1 christos else if (sal.line > 0
1499 1.1 christos && find_line_pc_range (sal, &start_pc, &end_pc))
1500 1.1 christos {
1501 1.1 christos struct gdbarch *gdbarch
1502 1.1 christos = get_objfile_arch (SYMTAB_OBJFILE (sal.symtab));
1503 1.1 christos
1504 1.1 christos if (start_pc == end_pc)
1505 1.1 christos {
1506 1.1 christos printf_filtered ("Line %d of \"%s\"",
1507 1.1 christos sal.line,
1508 1.1 christos symtab_to_filename_for_display (sal.symtab));
1509 1.1 christos wrap_here (" ");
1510 1.1 christos printf_filtered (" is at address ");
1511 1.1 christos print_address (gdbarch, start_pc, gdb_stdout);
1512 1.1 christos wrap_here (" ");
1513 1.1 christos printf_filtered (" but contains no code.\n");
1514 1.1 christos }
1515 1.1 christos else
1516 1.1 christos {
1517 1.1 christos printf_filtered ("Line %d of \"%s\"",
1518 1.1 christos sal.line,
1519 1.1 christos symtab_to_filename_for_display (sal.symtab));
1520 1.1 christos wrap_here (" ");
1521 1.1 christos printf_filtered (" starts at address ");
1522 1.1 christos print_address (gdbarch, start_pc, gdb_stdout);
1523 1.1 christos wrap_here (" ");
1524 1.1 christos printf_filtered (" and ends at ");
1525 1.1 christos print_address (gdbarch, end_pc, gdb_stdout);
1526 1.1 christos printf_filtered (".\n");
1527 1.1 christos }
1528 1.1 christos
1529 1.8 christos /* x/i should display this line's code. */
1530 1.1 christos set_next_address (gdbarch, start_pc);
1531 1.1 christos
1532 1.1 christos /* Repeating "info line" should do the following line. */
1533 1.1 christos last_line_listed = sal.line + 1;
1534 1.1 christos
1535 1.1 christos /* If this is the only line, show the source code. If it could
1536 1.1 christos not find the file, don't do anything special. */
1537 1.1 christos if (annotation_level && sals.size () == 1)
1538 1.1 christos identify_source_line (sal.symtab, sal.line, 0, start_pc);
1539 1.1 christos }
1540 1.1 christos else
1541 1.1 christos /* Is there any case in which we get here, and have an address
1542 1.1 christos which the user would want to see? If we have debugging symbols
1543 1.8 christos and no line numbers? */
1544 1.8 christos printf_filtered (_("Line number %d is out of range for \"%s\".\n"),
1545 1.8 christos sal.line, symtab_to_filename_for_display (sal.symtab));
1546 1.8 christos }
1547 1.1 christos }
1548 1.8 christos
1549 1.1 christos /* Commands to search the source file for a regexp. */
1551 1.1 christos
1552 1.1 christos /* Helper for forward_search_command/reverse_search_command. FORWARD
1553 1.1 christos indicates direction: true for forward, false for
1554 1.1 christos backward/reverse. */
1555 1.1 christos
1556 1.1 christos static void
1557 1.8 christos search_command_helper (const char *regex, int from_tty, bool forward)
1558 1.8 christos {
1559 1.1 christos const char *msg = re_comp (regex);
1560 1.1 christos if (msg)
1561 1.1 christos error (("%s"), msg);
1562 1.8 christos
1563 1.8 christos if (current_source_symtab == 0)
1564 1.8 christos select_source_symtab (0);
1565 1.8 christos
1566 1.8 christos scoped_fd desc = open_source_file (current_source_symtab);
1567 1.1 christos if (desc.get () < 0)
1568 1.1 christos perror_with_name (symtab_to_filename_for_display (current_source_symtab));
1569 1.1 christos
1570 1.1 christos if (current_source_symtab->line_charpos == 0)
1571 1.8 christos find_source_lines (current_source_symtab, desc.get ());
1572 1.8 christos
1573 1.1 christos int line = (forward
1574 1.1 christos ? last_line_listed + 1
1575 1.8 christos : last_line_listed - 1);
1576 1.8 christos
1577 1.8 christos if (line < 1 || line > current_source_symtab->nlines)
1578 1.8 christos error (_("Expression not found"));
1579 1.8 christos
1580 1.8 christos if (lseek (desc.get (), current_source_symtab->line_charpos[line - 1], 0)
1581 1.1 christos < 0)
1582 1.1 christos perror_with_name (symtab_to_filename_for_display (current_source_symtab));
1583 1.8 christos
1584 1.1 christos gdb_file_up stream = desc.to_file (FDOPEN_MODE);
1585 1.8 christos clearerr (stream.get ());
1586 1.1 christos
1587 1.1 christos gdb::def_vector<char> buf;
1588 1.1 christos buf.reserve (256);
1589 1.1 christos
1590 1.8 christos while (1)
1591 1.1 christos {
1592 1.8 christos buf.resize (0);
1593 1.1 christos
1594 1.1 christos int c = fgetc (stream.get ());
1595 1.1 christos if (c == EOF)
1596 1.8 christos break;
1597 1.8 christos do
1598 1.1 christos {
1599 1.8 christos buf.push_back (c);
1600 1.8 christos }
1601 1.1 christos while (c != '\n' && (c = fgetc (stream.get ())) >= 0);
1602 1.1 christos
1603 1.1 christos /* Remove the \r, if any, at the end of the line, otherwise
1604 1.8 christos regular expressions that end with $ or \n won't work. */
1605 1.8 christos size_t sz = buf.size ();
1606 1.1 christos if (sz >= 2 && buf[sz - 2] == '\r')
1607 1.1 christos {
1608 1.1 christos buf[sz - 2] = '\n';
1609 1.1 christos buf.resize (sz - 1);
1610 1.7 christos }
1611 1.1 christos
1612 1.1 christos /* We now have a source line in buf, null terminate and match. */
1613 1.8 christos buf.push_back ('\0');
1614 1.8 christos if (re_exec (buf.data ()) > 0)
1615 1.8 christos {
1616 1.8 christos /* Match! */
1617 1.8 christos print_source_lines (current_source_symtab, line, line + 1, 0);
1618 1.8 christos set_internalvar_integer (lookup_internalvar ("_"), line);
1619 1.8 christos current_source_line = std::max (line - lines_to_list / 2, 1);
1620 1.8 christos return;
1621 1.8 christos }
1622 1.8 christos
1623 1.8 christos if (forward)
1624 1.8 christos line++;
1625 1.8 christos else
1626 1.8 christos {
1627 1.8 christos line--;
1628 1.8 christos if (line < 1)
1629 1.1 christos break;
1630 1.1 christos if (fseek (stream.get (),
1631 1.1 christos current_source_symtab->line_charpos[line - 1], 0) < 0)
1632 1.1 christos {
1633 1.1 christos const char *filename
1634 1.1 christos = symtab_to_filename_for_display (current_source_symtab);
1635 1.8 christos perror_with_name (filename);
1636 1.1 christos }
1637 1.8 christos }
1638 1.8 christos }
1639 1.1 christos
1640 1.8 christos printf_filtered (_("Expression not found\n"));
1641 1.8 christos }
1642 1.8 christos
1643 1.8 christos static void
1644 1.1 christos forward_search_command (const char *regex, int from_tty)
1645 1.1 christos {
1646 1.1 christos search_command_helper (regex, from_tty, true);
1647 1.1 christos }
1648 1.1 christos
1649 1.1 christos static void
1650 1.1 christos reverse_search_command (const char *regex, int from_tty)
1651 1.1 christos {
1652 1.1 christos search_command_helper (regex, from_tty, false);
1653 1.1 christos }
1654 1.1 christos
1655 1.1 christos /* If the last character of PATH is a directory separator, then strip it. */
1656 1.1 christos
1657 1.1 christos static void
1658 1.1 christos strip_trailing_directory_separator (char *path)
1659 1.1 christos {
1660 1.1 christos const int last = strlen (path) - 1;
1661 1.1 christos
1662 1.1 christos if (last < 0)
1663 1.1 christos return; /* No stripping is needed if PATH is the empty string. */
1664 1.1 christos
1665 1.1 christos if (IS_DIR_SEPARATOR (path[last]))
1666 1.1 christos path[last] = '\0';
1667 1.1 christos }
1668 1.1 christos
1669 1.1 christos /* Return the path substitution rule that matches FROM.
1670 1.1 christos Return NULL if no rule matches. */
1671 1.1 christos
1672 1.1 christos static struct substitute_path_rule *
1673 1.1 christos find_substitute_path_rule (const char *from)
1674 1.1 christos {
1675 1.1 christos struct substitute_path_rule *rule = substitute_path_rules;
1676 1.1 christos
1677 1.1 christos while (rule != NULL)
1678 1.1 christos {
1679 1.1 christos if (FILENAME_CMP (rule->from, from) == 0)
1680 1.1 christos return rule;
1681 1.1 christos rule = rule->next;
1682 1.1 christos }
1683 1.1 christos
1684 1.1 christos return NULL;
1685 1.6 christos }
1686 1.1 christos
1687 1.1 christos /* Add a new substitute-path rule at the end of the current list of rules.
1688 1.1 christos The new rule will replace FROM into TO. */
1689 1.1 christos
1690 1.1 christos void
1691 1.1 christos add_substitute_path_rule (char *from, char *to)
1692 1.1 christos {
1693 1.1 christos struct substitute_path_rule *rule;
1694 1.1 christos struct substitute_path_rule *new_rule = XNEW (struct substitute_path_rule);
1695 1.1 christos
1696 1.1 christos new_rule->from = xstrdup (from);
1697 1.1 christos new_rule->to = xstrdup (to);
1698 1.1 christos new_rule->next = NULL;
1699 1.1 christos
1700 1.1 christos /* If the list of rules are empty, then insert the new rule
1701 1.1 christos at the head of the list. */
1702 1.1 christos
1703 1.1 christos if (substitute_path_rules == NULL)
1704 1.1 christos {
1705 1.1 christos substitute_path_rules = new_rule;
1706 1.1 christos return;
1707 1.1 christos }
1708 1.1 christos
1709 1.1 christos /* Otherwise, skip to the last rule in our list and then append
1710 1.1 christos the new rule. */
1711 1.1 christos
1712 1.1 christos rule = substitute_path_rules;
1713 1.1 christos while (rule->next != NULL)
1714 1.1 christos rule = rule->next;
1715 1.1 christos
1716 1.1 christos rule->next = new_rule;
1717 1.1 christos }
1718 1.1 christos
1719 1.1 christos /* Remove the given source path substitution rule from the current list
1720 1.1 christos of rules. The memory allocated for that rule is also deallocated. */
1721 1.1 christos
1722 1.1 christos static void
1723 1.1 christos delete_substitute_path_rule (struct substitute_path_rule *rule)
1724 1.1 christos {
1725 1.1 christos if (rule == substitute_path_rules)
1726 1.1 christos substitute_path_rules = rule->next;
1727 1.1 christos else
1728 1.1 christos {
1729 1.1 christos struct substitute_path_rule *prev = substitute_path_rules;
1730 1.1 christos
1731 1.1 christos while (prev != NULL && prev->next != rule)
1732 1.1 christos prev = prev->next;
1733 1.1 christos
1734 1.1 christos gdb_assert (prev != NULL);
1735 1.1 christos
1736 1.1 christos prev->next = rule->next;
1737 1.1 christos }
1738 1.8 christos
1739 1.1 christos xfree (rule->from);
1740 1.1 christos xfree (rule->to);
1741 1.1 christos xfree (rule);
1742 1.1 christos }
1743 1.8 christos
1744 1.1 christos /* Implement the "show substitute-path" command. */
1745 1.1 christos
1746 1.1 christos static void
1747 1.1 christos show_substitute_path_command (const char *args, int from_tty)
1748 1.1 christos {
1749 1.1 christos struct substitute_path_rule *rule = substitute_path_rules;
1750 1.1 christos char *from = NULL;
1751 1.1 christos
1752 1.1 christos gdb_argv argv (args);
1753 1.1 christos
1754 1.1 christos /* We expect zero or one argument. */
1755 1.1 christos
1756 1.1 christos if (argv != NULL && argv[0] != NULL && argv[1] != NULL)
1757 1.1 christos error (_("Too many arguments in command"));
1758 1.1 christos
1759 1.1 christos if (argv != NULL && argv[0] != NULL)
1760 1.1 christos from = argv[0];
1761 1.1 christos
1762 1.1 christos /* Print the substitution rules. */
1763 1.3 christos
1764 1.1 christos if (from != NULL)
1765 1.1 christos printf_filtered
1766 1.1 christos (_("Source path substitution rule matching `%s':\n"), from);
1767 1.1 christos else
1768 1.1 christos printf_filtered (_("List of all source path substitution rules:\n"));
1769 1.1 christos
1770 1.1 christos while (rule != NULL)
1771 1.1 christos {
1772 1.8 christos if (from == NULL || substitute_path_rule_matches (rule, from) != 0)
1773 1.1 christos printf_filtered (" `%s' -> `%s'.\n", rule->from, rule->to);
1774 1.1 christos rule = rule->next;
1775 1.8 christos }
1776 1.1 christos }
1777 1.1 christos
1778 1.1 christos /* Implement the "unset substitute-path" command. */
1779 1.1 christos
1780 1.1 christos static void
1781 1.1 christos unset_substitute_path_command (const char *args, int from_tty)
1782 1.1 christos {
1783 1.1 christos struct substitute_path_rule *rule = substitute_path_rules;
1784 1.1 christos gdb_argv argv (args);
1785 1.1 christos char *from = NULL;
1786 1.1 christos int rule_found = 0;
1787 1.1 christos
1788 1.1 christos /* This function takes either 0 or 1 argument. */
1789 1.1 christos
1790 1.1 christos if (argv != NULL && argv[0] != NULL && argv[1] != NULL)
1791 1.1 christos error (_("Incorrect usage, too many arguments in command"));
1792 1.1 christos
1793 1.1 christos if (argv != NULL && argv[0] != NULL)
1794 1.1 christos from = argv[0];
1795 1.1 christos
1796 1.1 christos /* If the user asked for all the rules to be deleted, ask him
1797 1.1 christos to confirm and give him a chance to abort before the action
1798 1.1 christos is performed. */
1799 1.1 christos
1800 1.1 christos if (from == NULL
1801 1.1 christos && !query (_("Delete all source path substitution rules? ")))
1802 1.1 christos error (_("Canceled"));
1803 1.1 christos
1804 1.1 christos /* Delete the rule matching the argument. No argument means that
1805 1.1 christos all rules should be deleted. */
1806 1.1 christos
1807 1.1 christos while (rule != NULL)
1808 1.1 christos {
1809 1.1 christos struct substitute_path_rule *next = rule->next;
1810 1.1 christos
1811 1.1 christos if (from == NULL || FILENAME_CMP (from, rule->from) == 0)
1812 1.1 christos {
1813 1.1 christos delete_substitute_path_rule (rule);
1814 1.1 christos rule_found = 1;
1815 1.1 christos }
1816 1.1 christos
1817 1.1 christos rule = next;
1818 1.1 christos }
1819 1.1 christos
1820 1.1 christos /* If the user asked for a specific rule to be deleted but
1821 1.1 christos we could not find it, then report an error. */
1822 1.1 christos
1823 1.8 christos if (from != NULL && !rule_found)
1824 1.1 christos error (_("No substitution rule defined for `%s'"), from);
1825 1.1 christos
1826 1.1 christos forget_cached_source_info ();
1827 1.8 christos }
1828 1.1 christos
1829 1.1 christos /* Add a new source path substitution rule. */
1830 1.1 christos
1831 1.1 christos static void
1832 1.1 christos set_substitute_path_command (const char *args, int from_tty)
1833 1.1 christos {
1834 1.1 christos struct substitute_path_rule *rule;
1835 1.1 christos
1836 1.1 christos gdb_argv argv (args);
1837 1.1 christos
1838 1.1 christos if (argv == NULL || argv[0] == NULL || argv [1] == NULL)
1839 1.1 christos error (_("Incorrect usage, too few arguments in command"));
1840 1.1 christos
1841 1.1 christos if (argv[2] != NULL)
1842 1.1 christos error (_("Incorrect usage, too many arguments in command"));
1843 1.1 christos
1844 1.1 christos if (*(argv[0]) == '\0')
1845 1.1 christos error (_("First argument must be at least one character long"));
1846 1.1 christos
1847 1.1 christos /* Strip any trailing directory separator character in either FROM
1848 1.1 christos or TO. The substitution rule already implicitly contains them. */
1849 1.1 christos strip_trailing_directory_separator (argv[0]);
1850 1.1 christos strip_trailing_directory_separator (argv[1]);
1851 1.1 christos
1852 1.1 christos /* If a rule with the same "from" was previously defined, then
1853 1.1 christos delete it. This new rule replaces it. */
1854 1.8 christos
1855 1.1 christos rule = find_substitute_path_rule (argv[0]);
1856 1.8 christos if (rule != NULL)
1857 1.8 christos delete_substitute_path_rule (rule);
1858 1.8 christos
1859 1.8 christos /* Insert the new substitution rule. */
1860 1.8 christos
1861 1.8 christos add_substitute_path_rule (argv[0], argv[1]);
1862 1.8 christos forget_cached_source_info ();
1863 1.8 christos }
1864 1.8 christos
1865 1.8 christos /* See source.h. */
1866 1.8 christos
1867 1.8 christos source_lines_range::source_lines_range (int startline,
1868 1.8 christos source_lines_range::direction dir)
1869 1.8 christos {
1870 1.8 christos if (dir == source_lines_range::FORWARD)
1871 1.8 christos {
1872 1.8 christos LONGEST end = static_cast <LONGEST> (startline) + get_lines_to_list ();
1873 1.8 christos
1874 1.8 christos if (end > INT_MAX)
1875 1.8 christos end = INT_MAX;
1876 1.8 christos
1877 1.8 christos m_startline = startline;
1878 1.8 christos m_stopline = static_cast <int> (end);
1879 1.8 christos }
1880 1.8 christos else
1881 1.1 christos {
1882 1.1 christos LONGEST start = static_cast <LONGEST> (startline) - get_lines_to_list ();
1883 1.1 christos
1884 1.1 christos if (start < 1)
1885 1.1 christos start = 1;
1886 1.1 christos
1887 1.1 christos m_startline = static_cast <int> (start);
1888 1.1 christos m_stopline = startline;
1889 1.1 christos }
1890 1.1 christos }
1891 1.1 christos
1892 1.1 christos
1893 1.1 christos void
1895 1.1 christos _initialize_source (void)
1896 1.1 christos {
1897 1.1 christos struct cmd_list_element *c;
1898 1.1 christos
1899 1.1 christos current_source_symtab = 0;
1900 1.1 christos init_source_path ();
1901 1.1 christos
1902 1.1 christos /* The intention is to use POSIX Basic Regular Expressions.
1903 1.1 christos Always use the GNU regex routine for consistency across all hosts.
1904 1.1 christos Our current GNU regex.c does not have all the POSIX features, so this is
1905 1.1 christos just an approximation. */
1906 1.1 christos re_set_syntax (RE_SYNTAX_GREP);
1907 1.1 christos
1908 1.1 christos c = add_cmd ("directory", class_files, directory_command, _("\
1909 1.1 christos Add directory DIR to beginning of search path for source files.\n\
1910 1.1 christos Forget cached info on source file locations and line positions.\n\
1911 1.1 christos DIR can also be $cwd for the current working directory, or $cdir for the\n\
1912 1.1 christos directory in which the source file was compiled into object code.\n\
1913 1.1 christos With no argument, reset the search path to $cdir:$cwd, the default."),
1914 1.1 christos &cmdlist);
1915 1.1 christos
1916 1.1 christos if (dbx_commands)
1917 1.1 christos add_com_alias ("use", "directory", class_files, 0);
1918 1.1 christos
1919 1.1 christos set_cmd_completer (c, filename_completer);
1920 1.1 christos
1921 1.1 christos add_setshow_optional_filename_cmd ("directories",
1922 1.1 christos class_files,
1923 1.1 christos &source_path,
1924 1.1 christos _("\
1925 1.1 christos Set the search path for finding source files."),
1926 1.1 christos _("\
1927 1.1 christos Show the search path for finding source files."),
1928 1.8 christos _("\
1929 1.1 christos $cwd in the path means the current working directory.\n\
1930 1.1 christos $cdir in the path means the compilation directory of the source file.\n\
1931 1.8 christos GDB ensures the search path always ends with $cdir:$cwd by\n\
1932 1.1 christos appending these directories if necessary.\n\
1933 1.1 christos Setting the value to an empty string sets it to $cdir:$cwd, the default."),
1934 1.1 christos set_directories_command,
1935 1.1 christos show_directories_command,
1936 1.1 christos &setlist, &showlist);
1937 1.1 christos
1938 1.1 christos add_info ("source", info_source_command,
1939 1.1 christos _("Information about the current source file."));
1940 1.1 christos
1941 1.1 christos add_info ("line", info_line_command, _("\
1942 1.1 christos Core addresses of the code for a source line.\n\
1943 1.1 christos Line can be specified as\n\
1944 1.1 christos LINENUM, to list around that line in current file,\n\
1945 1.1 christos FILE:LINENUM, to list around that line in that file,\n\
1946 1.1 christos FUNCTION, to list around beginning of that function,\n\
1947 1.1 christos FILE:FUNCTION, to distinguish among like-named static functions.\n\
1948 1.1 christos Default is to describe the last source line that was listed.\n\n\
1949 1.1 christos This sets the default address for \"x\" to the line's first instruction\n\
1950 1.1 christos so that \"x/i\" suffices to start examining the machine code.\n\
1951 1.1 christos The address is also stored as the value of \"$_\"."));
1952 1.1 christos
1953 1.1 christos add_com ("forward-search", class_files, forward_search_command, _("\
1954 1.1 christos Search for regular expression (see regex(3)) from last line listed.\n\
1955 1.1 christos The matching line number is also stored as the value of \"$_\"."));
1956 1.1 christos add_com_alias ("search", "forward-search", class_files, 0);
1957 1.1 christos add_com_alias ("fo", "forward-search", class_files, 1);
1958 1.1 christos
1959 1.1 christos add_com ("reverse-search", class_files, reverse_search_command, _("\
1960 1.1 christos Search backward for regular expression (see regex(3)) from last line listed.\n\
1961 1.1 christos The matching line number is also stored as the value of \"$_\"."));
1962 1.1 christos add_com_alias ("rev", "reverse-search", class_files, 1);
1963 1.1 christos
1964 1.1 christos add_setshow_integer_cmd ("listsize", class_support, &lines_to_list, _("\
1965 1.1 christos Set number of source lines gdb will list by default."), _("\
1966 1.1 christos Show number of source lines gdb will list by default."), _("\
1967 1.1 christos Use this to choose how many source lines the \"list\" displays (unless\n\
1968 1.1 christos the \"list\" argument explicitly specifies some other number).\n\
1969 1.1 christos A value of \"unlimited\", or zero, means there's no limit."),
1970 1.1 christos NULL,
1971 1.1 christos show_lines_to_list,
1972 1.1 christos &setlist, &showlist);
1973 1.1 christos
1974 1.1 christos add_cmd ("substitute-path", class_files, set_substitute_path_command,
1975 1.1 christos _("\
1976 1.1 christos Usage: set substitute-path FROM TO\n\
1977 1.1 christos Add a substitution rule replacing FROM into TO in source file names.\n\
1978 1.1 christos If a substitution rule was previously set for FROM, the old rule\n\
1979 1.1 christos is replaced by the new one."),
1980 1.1 christos &setlist);
1981 1.1 christos
1982 1.1 christos add_cmd ("substitute-path", class_files, unset_substitute_path_command,
1983 1.1 christos _("\
1984 1.1 christos Usage: unset substitute-path [FROM]\n\
1985 1.1 christos Delete the rule for substituting FROM in source file names. If FROM\n\
1986 1.1 christos is not specified, all substituting rules are deleted.\n\
1987 1.1 christos If the debugger cannot find a rule for FROM, it will display a warning."),
1988 1.1 christos &unsetlist);
1989 1.1 christos
1990 1.1 christos add_cmd ("substitute-path", class_files, show_substitute_path_command,
1991 1.1 christos _("\
1992 1.1 christos Usage: show substitute-path [FROM]\n\
1993 1.1 christos Print the rule for substituting FROM in source file names. If FROM\n\
1994 1.1 christos is not specified, print all substitution rules."),
1995 1.1 christos &showlist);
1996 1.1 christos
1997 1.1 christos add_setshow_enum_cmd ("filename-display", class_files,
1998 1.1 christos filename_display_kind_names,
1999 1.1 christos &filename_display_string, _("\
2000 1.1 christos Set how to display filenames."), _("\
2001 1.1 christos Show how to display filenames."), _("\
2002 filename-display can be:\n\
2003 basename - display only basename of a filename\n\
2004 relative - display a filename relative to the compilation directory\n\
2005 absolute - display an absolute filename\n\
2006 By default, relative filenames are displayed."),
2007 NULL,
2008 show_filename_display_string,
2009 &setlist, &showlist);
2010
2011 }
2012