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