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