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