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