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