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