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