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