1 /* Cache and manage frames for GDB, the GNU debugger. 2 3 Copyright (C) 1986-2023 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 3 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, see <http://www.gnu.org/licenses/>. */ 19 20 #include "defs.h" 21 #include "frame.h" 22 #include "frame-info.h" 23 #include "target.h" 24 #include "value.h" 25 #include "inferior.h" /* for inferior_ptid */ 26 #include "regcache.h" 27 #include "user-regs.h" 28 #include "gdbsupport/gdb_obstack.h" 29 #include "dummy-frame.h" 30 #include "sentinel-frame.h" 31 #include "gdbcore.h" 32 #include "annotate.h" 33 #include "language.h" 34 #include "frame-unwind.h" 35 #include "frame-base.h" 36 #include "command.h" 37 #include "gdbcmd.h" 38 #include "observable.h" 39 #include "objfiles.h" 40 #include "gdbthread.h" 41 #include "block.h" 42 #include "inline-frame.h" 43 #include "tracepoint.h" 44 #include "hashtab.h" 45 #include "valprint.h" 46 #include "cli/cli-option.h" 47 48 /* The sentinel frame terminates the innermost end of the frame chain. 49 If unwound, it returns the information needed to construct an 50 innermost frame. 51 52 The current frame, which is the innermost frame, can be found at 53 sentinel_frame->prev. */ 54 55 static frame_info *sentinel_frame; 56 57 /* Number of calls to reinit_frame_cache. */ 58 static unsigned int frame_cache_generation = 0; 59 60 /* See frame.h. */ 61 62 unsigned int 63 get_frame_cache_generation () 64 { 65 return frame_cache_generation; 66 } 67 68 /* The values behind the global "set backtrace ..." settings. */ 69 set_backtrace_options user_set_backtrace_options; 70 71 static frame_info_ptr get_prev_frame_raw (frame_info_ptr this_frame); 72 static const char *frame_stop_reason_symbol_string (enum unwind_stop_reason reason); 73 74 /* Status of some values cached in the frame_info object. */ 75 76 enum cached_copy_status 77 { 78 /* Value is unknown. */ 79 CC_UNKNOWN, 80 81 /* We have a value. */ 82 CC_VALUE, 83 84 /* Value was not saved. */ 85 CC_NOT_SAVED, 86 87 /* Value is unavailable. */ 88 CC_UNAVAILABLE 89 }; 90 91 enum class frame_id_status 92 { 93 /* Frame id is not computed. */ 94 NOT_COMPUTED = 0, 95 96 /* Frame id is being computed (compute_frame_id is active). */ 97 COMPUTING, 98 99 /* Frame id has been computed. */ 100 COMPUTED, 101 }; 102 103 /* We keep a cache of stack frames, each of which is a "struct 104 frame_info". The innermost one gets allocated (in 105 wait_for_inferior) each time the inferior stops; sentinel_frame 106 points to it. Additional frames get allocated (in get_prev_frame) 107 as needed, and are chained through the next and prev fields. Any 108 time that the frame cache becomes invalid (most notably when we 109 execute something, but also if we change how we interpret the 110 frames (e.g. "set heuristic-fence-post" in mips-tdep.c, or anything 111 which reads new symbols)), we should call reinit_frame_cache. */ 112 113 struct frame_info 114 { 115 /* Return a string representation of this frame. */ 116 std::string to_string () const; 117 118 /* Level of this frame. The inner-most (youngest) frame is at level 119 0. As you move towards the outer-most (oldest) frame, the level 120 increases. This is a cached value. It could just as easily be 121 computed by counting back from the selected frame to the inner 122 most frame. */ 123 /* NOTE: cagney/2002-04-05: Perhaps a level of ``-1'' should be 124 reserved to indicate a bogus frame - one that has been created 125 just to keep GDB happy (GDB always needs a frame). For the 126 moment leave this as speculation. */ 127 int level; 128 129 /* The frame's program space. */ 130 struct program_space *pspace; 131 132 /* The frame's address space. */ 133 const address_space *aspace; 134 135 /* The frame's low-level unwinder and corresponding cache. The 136 low-level unwinder is responsible for unwinding register values 137 for the previous frame. The low-level unwind methods are 138 selected based on the presence, or otherwise, of register unwind 139 information such as CFI. */ 140 void *prologue_cache; 141 const struct frame_unwind *unwind; 142 143 /* Cached copy of the previous frame's architecture. */ 144 struct 145 { 146 bool p; 147 struct gdbarch *arch; 148 } prev_arch; 149 150 /* Cached copy of the previous frame's resume address. */ 151 struct { 152 cached_copy_status status; 153 /* Did VALUE require unmasking when being read. */ 154 bool masked; 155 CORE_ADDR value; 156 } prev_pc; 157 158 /* Cached copy of the previous frame's function address. */ 159 struct 160 { 161 CORE_ADDR addr; 162 cached_copy_status status; 163 } prev_func; 164 165 /* This frame's ID. */ 166 struct 167 { 168 frame_id_status p; 169 struct frame_id value; 170 } this_id; 171 172 /* The frame's high-level base methods, and corresponding cache. 173 The high level base methods are selected based on the frame's 174 debug info. */ 175 const struct frame_base *base; 176 void *base_cache; 177 178 /* Pointers to the next (down, inner, younger) and previous (up, 179 outer, older) frame_info's in the frame cache. */ 180 struct frame_info *next; /* down, inner, younger */ 181 bool prev_p; 182 struct frame_info *prev; /* up, outer, older */ 183 184 /* The reason why we could not set PREV, or UNWIND_NO_REASON if we 185 could. Only valid when PREV_P is set. */ 186 enum unwind_stop_reason stop_reason; 187 188 /* A frame specific string describing the STOP_REASON in more detail. 189 Only valid when PREV_P is set, but even then may still be NULL. */ 190 const char *stop_string; 191 }; 192 193 /* See frame.h. */ 194 195 void 196 set_frame_previous_pc_masked (frame_info_ptr frame) 197 { 198 frame->prev_pc.masked = true; 199 } 200 201 /* See frame.h. */ 202 203 bool 204 get_frame_pc_masked (frame_info_ptr frame) 205 { 206 gdb_assert (frame->next != nullptr); 207 gdb_assert (frame->next->prev_pc.status == CC_VALUE); 208 209 return frame->next->prev_pc.masked; 210 } 211 212 /* A frame stash used to speed up frame lookups. Create a hash table 213 to stash frames previously accessed from the frame cache for 214 quicker subsequent retrieval. The hash table is emptied whenever 215 the frame cache is invalidated. */ 216 217 static htab_t frame_stash; 218 219 /* Internal function to calculate a hash from the frame_id addresses, 220 using as many valid addresses as possible. Frames below level 0 221 are not stored in the hash table. */ 222 223 static hashval_t 224 frame_addr_hash (const void *ap) 225 { 226 const frame_info *frame = (const frame_info *) ap; 227 const struct frame_id f_id = frame->this_id.value; 228 hashval_t hash = 0; 229 230 gdb_assert (f_id.stack_status != FID_STACK_INVALID 231 || f_id.code_addr_p 232 || f_id.special_addr_p); 233 234 if (f_id.stack_status == FID_STACK_VALID) 235 hash = iterative_hash (&f_id.stack_addr, 236 sizeof (f_id.stack_addr), hash); 237 if (f_id.code_addr_p) 238 hash = iterative_hash (&f_id.code_addr, 239 sizeof (f_id.code_addr), hash); 240 if (f_id.special_addr_p) 241 hash = iterative_hash (&f_id.special_addr, 242 sizeof (f_id.special_addr), hash); 243 244 return hash; 245 } 246 247 /* Internal equality function for the hash table. This function 248 defers equality operations to frame_id::operator==. */ 249 250 static int 251 frame_addr_hash_eq (const void *a, const void *b) 252 { 253 const frame_info *f_entry = (const frame_info *) a; 254 const frame_info *f_element = (const frame_info *) b; 255 256 return f_entry->this_id.value == f_element->this_id.value; 257 } 258 259 /* Internal function to create the frame_stash hash table. 100 seems 260 to be a good compromise to start the hash table at. */ 261 262 static void 263 frame_stash_create (void) 264 { 265 frame_stash = htab_create (100, 266 frame_addr_hash, 267 frame_addr_hash_eq, 268 NULL); 269 } 270 271 /* Internal function to add a frame to the frame_stash hash table. 272 Returns false if a frame with the same ID was already stashed, true 273 otherwise. */ 274 275 static bool 276 frame_stash_add (frame_info *frame) 277 { 278 /* Do not try to stash the sentinel frame. */ 279 gdb_assert (frame->level >= 0); 280 281 frame_info **slot = (frame_info **) htab_find_slot (frame_stash, 282 frame, INSERT); 283 284 /* If we already have a frame in the stack with the same id, we 285 either have a stack cycle (corrupted stack?), or some bug 286 elsewhere in GDB. In any case, ignore the duplicate and return 287 an indication to the caller. */ 288 if (*slot != nullptr) 289 return false; 290 291 *slot = frame; 292 return true; 293 } 294 295 /* Internal function to search the frame stash for an entry with the 296 given frame ID. If found, return that frame. Otherwise return 297 NULL. */ 298 299 static frame_info_ptr 300 frame_stash_find (struct frame_id id) 301 { 302 struct frame_info dummy; 303 frame_info *frame; 304 305 dummy.this_id.value = id; 306 frame = (frame_info *) htab_find (frame_stash, &dummy); 307 return frame_info_ptr (frame); 308 } 309 310 /* Internal function to invalidate the frame stash by removing all 311 entries in it. This only occurs when the frame cache is 312 invalidated. */ 313 314 static void 315 frame_stash_invalidate (void) 316 { 317 htab_empty (frame_stash); 318 } 319 320 /* See frame.h */ 321 scoped_restore_selected_frame::scoped_restore_selected_frame () 322 { 323 m_lang = current_language->la_language; 324 save_selected_frame (&m_fid, &m_level); 325 } 326 327 /* See frame.h */ 328 scoped_restore_selected_frame::~scoped_restore_selected_frame () 329 { 330 restore_selected_frame (m_fid, m_level); 331 set_language (m_lang); 332 } 333 334 /* Flag to control debugging. */ 335 336 bool frame_debug; 337 338 static void 339 show_frame_debug (struct ui_file *file, int from_tty, 340 struct cmd_list_element *c, const char *value) 341 { 342 gdb_printf (file, _("Frame debugging is %s.\n"), value); 343 } 344 345 /* Implementation of "show backtrace past-main". */ 346 347 static void 348 show_backtrace_past_main (struct ui_file *file, int from_tty, 349 struct cmd_list_element *c, const char *value) 350 { 351 gdb_printf (file, 352 _("Whether backtraces should " 353 "continue past \"main\" is %s.\n"), 354 value); 355 } 356 357 /* Implementation of "show backtrace past-entry". */ 358 359 static void 360 show_backtrace_past_entry (struct ui_file *file, int from_tty, 361 struct cmd_list_element *c, const char *value) 362 { 363 gdb_printf (file, _("Whether backtraces should continue past the " 364 "entry point of a program is %s.\n"), 365 value); 366 } 367 368 /* Implementation of "show backtrace limit". */ 369 370 static void 371 show_backtrace_limit (struct ui_file *file, int from_tty, 372 struct cmd_list_element *c, const char *value) 373 { 374 gdb_printf (file, 375 _("An upper bound on the number " 376 "of backtrace levels is %s.\n"), 377 value); 378 } 379 380 /* See frame.h. */ 381 382 std::string 383 frame_id::to_string () const 384 { 385 const struct frame_id &id = *this; 386 387 std::string res = "{"; 388 389 if (id.stack_status == FID_STACK_INVALID) 390 res += "!stack"; 391 else if (id.stack_status == FID_STACK_UNAVAILABLE) 392 res += "stack=<unavailable>"; 393 else if (id.stack_status == FID_STACK_SENTINEL) 394 res += "stack=<sentinel>"; 395 else if (id.stack_status == FID_STACK_OUTER) 396 res += "stack=<outer>"; 397 else 398 res += std::string ("stack=") + hex_string (id.stack_addr); 399 400 /* Helper function to format 'N=A' if P is true, otherwise '!N'. */ 401 auto field_to_string = [] (const char *n, bool p, CORE_ADDR a) -> std::string 402 { 403 if (p) 404 return std::string (n) + "=" + core_addr_to_string (a); 405 else 406 return std::string ("!") + std::string (n); 407 }; 408 409 res += (std::string (",") 410 + field_to_string ("code", id.code_addr_p, id.code_addr) 411 + std::string (",") 412 + field_to_string ("special", id.special_addr_p, id.special_addr)); 413 414 if (id.artificial_depth) 415 res += ",artificial=" + std::to_string (id.artificial_depth); 416 res += "}"; 417 return res; 418 } 419 420 /* Return a string representation of TYPE. */ 421 422 static const char * 423 frame_type_str (frame_type type) 424 { 425 switch (type) 426 { 427 case NORMAL_FRAME: 428 return "NORMAL_FRAME"; 429 430 case DUMMY_FRAME: 431 return "DUMMY_FRAME"; 432 433 case INLINE_FRAME: 434 return "INLINE_FRAME"; 435 436 case TAILCALL_FRAME: 437 return "TAILCALL_FRAME"; 438 439 case SIGTRAMP_FRAME: 440 return "SIGTRAMP_FRAME"; 441 442 case ARCH_FRAME: 443 return "ARCH_FRAME"; 444 445 case SENTINEL_FRAME: 446 return "SENTINEL_FRAME"; 447 448 default: 449 return "<unknown type>"; 450 }; 451 } 452 453 /* See struct frame_info. */ 454 455 std::string 456 frame_info::to_string () const 457 { 458 const frame_info *fi = this; 459 460 std::string res; 461 462 res += string_printf ("{level=%d,", fi->level); 463 464 if (fi->unwind != NULL) 465 res += string_printf ("type=%s,", frame_type_str (fi->unwind->type)); 466 else 467 res += "type=<unknown>,"; 468 469 if (fi->unwind != NULL) 470 res += string_printf ("unwinder=\"%s\",", fi->unwind->name); 471 else 472 res += "unwinder=<unknown>,"; 473 474 if (fi->next == NULL || fi->next->prev_pc.status == CC_UNKNOWN) 475 res += "pc=<unknown>,"; 476 else if (fi->next->prev_pc.status == CC_VALUE) 477 res += string_printf ("pc=%s%s,", hex_string (fi->next->prev_pc.value), 478 fi->next->prev_pc.masked ? "[PAC]" : ""); 479 else if (fi->next->prev_pc.status == CC_NOT_SAVED) 480 res += "pc=<not saved>,"; 481 else if (fi->next->prev_pc.status == CC_UNAVAILABLE) 482 res += "pc=<unavailable>,"; 483 484 if (fi->this_id.p == frame_id_status::NOT_COMPUTED) 485 res += "id=<not computed>,"; 486 else if (fi->this_id.p == frame_id_status::COMPUTING) 487 res += "id=<computing>,"; 488 else 489 res += string_printf ("id=%s,", fi->this_id.value.to_string ().c_str ()); 490 491 if (fi->next != NULL && fi->next->prev_func.status == CC_VALUE) 492 res += string_printf ("func=%s", hex_string (fi->next->prev_func.addr)); 493 else 494 res += "func=<unknown>"; 495 496 res += "}"; 497 498 return res; 499 } 500 501 /* Given FRAME, return the enclosing frame as found in real frames read-in from 502 inferior memory. Skip any previous frames which were made up by GDB. 503 Return FRAME if FRAME is a non-artificial frame. 504 Return NULL if FRAME is the start of an artificial-only chain. */ 505 506 static frame_info_ptr 507 skip_artificial_frames (frame_info_ptr frame) 508 { 509 /* Note we use get_prev_frame_always, and not get_prev_frame. The 510 latter will truncate the frame chain, leading to this function 511 unintentionally returning a null_frame_id (e.g., when the user 512 sets a backtrace limit). 513 514 Note that for record targets we may get a frame chain that consists 515 of artificial frames only. */ 516 while (get_frame_type (frame) == INLINE_FRAME 517 || get_frame_type (frame) == TAILCALL_FRAME) 518 { 519 frame = get_prev_frame_always (frame); 520 if (frame == NULL) 521 break; 522 } 523 524 return frame; 525 } 526 527 frame_info_ptr 528 skip_unwritable_frames (frame_info_ptr frame) 529 { 530 while (gdbarch_code_of_frame_writable (get_frame_arch (frame), frame) == 0) 531 { 532 frame = get_prev_frame (frame); 533 if (frame == NULL) 534 break; 535 } 536 537 return frame; 538 } 539 540 /* See frame.h. */ 541 542 frame_info_ptr 543 skip_tailcall_frames (frame_info_ptr frame) 544 { 545 while (get_frame_type (frame) == TAILCALL_FRAME) 546 { 547 /* Note that for record targets we may get a frame chain that consists of 548 tailcall frames only. */ 549 frame = get_prev_frame (frame); 550 if (frame == NULL) 551 break; 552 } 553 554 return frame; 555 } 556 557 /* Compute the frame's uniq ID that can be used to, later, re-find the 558 frame. */ 559 560 static void 561 compute_frame_id (frame_info_ptr fi) 562 { 563 FRAME_SCOPED_DEBUG_ENTER_EXIT; 564 565 gdb_assert (fi->this_id.p == frame_id_status::NOT_COMPUTED); 566 567 unsigned int entry_generation = get_frame_cache_generation (); 568 569 try 570 { 571 /* Mark this frame's id as "being computed. */ 572 fi->this_id.p = frame_id_status::COMPUTING; 573 574 frame_debug_printf ("fi=%d", fi->level); 575 576 /* Find the unwinder. */ 577 if (fi->unwind == NULL) 578 frame_unwind_find_by_frame (fi, &fi->prologue_cache); 579 580 /* Find THIS frame's ID. */ 581 /* Default to outermost if no ID is found. */ 582 fi->this_id.value = outer_frame_id; 583 fi->unwind->this_id (fi, &fi->prologue_cache, &fi->this_id.value); 584 gdb_assert (frame_id_p (fi->this_id.value)); 585 586 /* Mark this frame's id as "computed". */ 587 fi->this_id.p = frame_id_status::COMPUTED; 588 589 frame_debug_printf (" -> %s", fi->this_id.value.to_string ().c_str ()); 590 } 591 catch (const gdb_exception &ex) 592 { 593 /* On error, revert the frame id status to not computed. If the frame 594 cache generation changed, the frame object doesn't exist anymore, so 595 don't touch it. */ 596 if (get_frame_cache_generation () == entry_generation) 597 fi->this_id.p = frame_id_status::NOT_COMPUTED; 598 599 throw; 600 } 601 } 602 603 /* Return a frame uniq ID that can be used to, later, re-find the 604 frame. */ 605 606 struct frame_id 607 get_frame_id (frame_info_ptr fi) 608 { 609 if (fi == NULL) 610 return null_frame_id; 611 612 /* It's always invalid to try to get a frame's id while it is being 613 computed. */ 614 gdb_assert (fi->this_id.p != frame_id_status::COMPUTING); 615 616 if (fi->this_id.p == frame_id_status::NOT_COMPUTED) 617 { 618 /* If we haven't computed the frame id yet, then it must be that 619 this is the current frame. Compute it now, and stash the 620 result. The IDs of other frames are computed as soon as 621 they're created, in order to detect cycles. See 622 get_prev_frame_if_no_cycle. */ 623 gdb_assert (fi->level == 0); 624 625 /* Compute. */ 626 compute_frame_id (fi); 627 628 /* Since this is the first frame in the chain, this should 629 always succeed. */ 630 bool stashed = frame_stash_add (fi.get ()); 631 gdb_assert (stashed); 632 } 633 634 return fi->this_id.value; 635 } 636 637 struct frame_id 638 get_stack_frame_id (frame_info_ptr next_frame) 639 { 640 return get_frame_id (skip_artificial_frames (next_frame)); 641 } 642 643 struct frame_id 644 frame_unwind_caller_id (frame_info_ptr next_frame) 645 { 646 frame_info_ptr this_frame; 647 648 /* Use get_prev_frame_always, and not get_prev_frame. The latter 649 will truncate the frame chain, leading to this function 650 unintentionally returning a null_frame_id (e.g., when a caller 651 requests the frame ID of "main()"s caller. */ 652 653 next_frame = skip_artificial_frames (next_frame); 654 if (next_frame == NULL) 655 return null_frame_id; 656 657 this_frame = get_prev_frame_always (next_frame); 658 if (this_frame) 659 return get_frame_id (skip_artificial_frames (this_frame)); 660 else 661 return null_frame_id; 662 } 663 664 const struct frame_id null_frame_id = { 0 }; /* All zeros. */ 665 const struct frame_id sentinel_frame_id = { 0, 0, 0, FID_STACK_SENTINEL, 0, 1, 0 }; 666 const struct frame_id outer_frame_id = { 0, 0, 0, FID_STACK_OUTER, 0, 1, 0 }; 667 668 struct frame_id 669 frame_id_build_special (CORE_ADDR stack_addr, CORE_ADDR code_addr, 670 CORE_ADDR special_addr) 671 { 672 struct frame_id id = null_frame_id; 673 674 id.stack_addr = stack_addr; 675 id.stack_status = FID_STACK_VALID; 676 id.code_addr = code_addr; 677 id.code_addr_p = true; 678 id.special_addr = special_addr; 679 id.special_addr_p = true; 680 return id; 681 } 682 683 /* See frame.h. */ 684 685 struct frame_id 686 frame_id_build_unavailable_stack (CORE_ADDR code_addr) 687 { 688 struct frame_id id = null_frame_id; 689 690 id.stack_status = FID_STACK_UNAVAILABLE; 691 id.code_addr = code_addr; 692 id.code_addr_p = true; 693 return id; 694 } 695 696 /* See frame.h. */ 697 698 struct frame_id 699 frame_id_build_unavailable_stack_special (CORE_ADDR code_addr, 700 CORE_ADDR special_addr) 701 { 702 struct frame_id id = null_frame_id; 703 704 id.stack_status = FID_STACK_UNAVAILABLE; 705 id.code_addr = code_addr; 706 id.code_addr_p = true; 707 id.special_addr = special_addr; 708 id.special_addr_p = true; 709 return id; 710 } 711 712 struct frame_id 713 frame_id_build (CORE_ADDR stack_addr, CORE_ADDR code_addr) 714 { 715 struct frame_id id = null_frame_id; 716 717 id.stack_addr = stack_addr; 718 id.stack_status = FID_STACK_VALID; 719 id.code_addr = code_addr; 720 id.code_addr_p = true; 721 return id; 722 } 723 724 struct frame_id 725 frame_id_build_wild (CORE_ADDR stack_addr) 726 { 727 struct frame_id id = null_frame_id; 728 729 id.stack_addr = stack_addr; 730 id.stack_status = FID_STACK_VALID; 731 return id; 732 } 733 734 bool 735 frame_id_p (frame_id l) 736 { 737 /* The frame is valid iff it has a valid stack address. */ 738 bool p = l.stack_status != FID_STACK_INVALID; 739 740 frame_debug_printf ("l=%s -> %d", l.to_string ().c_str (), p); 741 742 return p; 743 } 744 745 bool 746 frame_id_artificial_p (frame_id l) 747 { 748 if (!frame_id_p (l)) 749 return false; 750 751 return l.artificial_depth != 0; 752 } 753 754 bool 755 frame_id::operator== (const frame_id &r) const 756 { 757 bool eq; 758 759 if (stack_status == FID_STACK_INVALID 760 || r.stack_status == FID_STACK_INVALID) 761 /* Like a NaN, if either ID is invalid, the result is false. 762 Note that a frame ID is invalid iff it is the null frame ID. */ 763 eq = false; 764 else if (stack_status != r.stack_status || stack_addr != r.stack_addr) 765 /* If .stack addresses are different, the frames are different. */ 766 eq = false; 767 else if (code_addr_p && r.code_addr_p && code_addr != r.code_addr) 768 /* An invalid code addr is a wild card. If .code addresses are 769 different, the frames are different. */ 770 eq = false; 771 else if (special_addr_p && r.special_addr_p 772 && special_addr != r.special_addr) 773 /* An invalid special addr is a wild card (or unused). Otherwise 774 if special addresses are different, the frames are different. */ 775 eq = false; 776 else if (artificial_depth != r.artificial_depth) 777 /* If artificial depths are different, the frames must be different. */ 778 eq = false; 779 else 780 /* Frames are equal. */ 781 eq = true; 782 783 frame_debug_printf ("l=%s, r=%s -> %d", 784 to_string ().c_str (), r.to_string ().c_str (), eq); 785 786 return eq; 787 } 788 789 /* Safety net to check whether frame ID L should be inner to 790 frame ID R, according to their stack addresses. 791 792 This method cannot be used to compare arbitrary frames, as the 793 ranges of valid stack addresses may be discontiguous (e.g. due 794 to sigaltstack). 795 796 However, it can be used as safety net to discover invalid frame 797 IDs in certain circumstances. Assuming that NEXT is the immediate 798 inner frame to THIS and that NEXT and THIS are both NORMAL frames: 799 800 * The stack address of NEXT must be inner-than-or-equal to the stack 801 address of THIS. 802 803 Therefore, if frame_id_inner (THIS, NEXT) holds, some unwind 804 error has occurred. 805 806 * If NEXT and THIS have different stack addresses, no other frame 807 in the frame chain may have a stack address in between. 808 809 Therefore, if frame_id_inner (TEST, THIS) holds, but 810 frame_id_inner (TEST, NEXT) does not hold, TEST cannot refer 811 to a valid frame in the frame chain. 812 813 The sanity checks above cannot be performed when a SIGTRAMP frame 814 is involved, because signal handlers might be executed on a different 815 stack than the stack used by the routine that caused the signal 816 to be raised. This can happen for instance when a thread exceeds 817 its maximum stack size. In this case, certain compilers implement 818 a stack overflow strategy that cause the handler to be run on a 819 different stack. */ 820 821 static bool 822 frame_id_inner (struct gdbarch *gdbarch, struct frame_id l, struct frame_id r) 823 { 824 bool inner; 825 826 if (l.stack_status != FID_STACK_VALID || r.stack_status != FID_STACK_VALID) 827 /* Like NaN, any operation involving an invalid ID always fails. 828 Likewise if either ID has an unavailable stack address. */ 829 inner = false; 830 else if (l.artificial_depth > r.artificial_depth 831 && l.stack_addr == r.stack_addr 832 && l.code_addr_p == r.code_addr_p 833 && l.special_addr_p == r.special_addr_p 834 && l.special_addr == r.special_addr) 835 { 836 /* Same function, different inlined functions. */ 837 const struct block *lb, *rb; 838 839 gdb_assert (l.code_addr_p && r.code_addr_p); 840 841 lb = block_for_pc (l.code_addr); 842 rb = block_for_pc (r.code_addr); 843 844 if (lb == NULL || rb == NULL) 845 /* Something's gone wrong. */ 846 inner = false; 847 else 848 /* This will return true if LB and RB are the same block, or 849 if the block with the smaller depth lexically encloses the 850 block with the greater depth. */ 851 inner = contained_in (lb, rb); 852 } 853 else 854 /* Only return non-zero when strictly inner than. Note that, per 855 comment in "frame.h", there is some fuzz here. Frameless 856 functions are not strictly inner than (same .stack but 857 different .code and/or .special address). */ 858 inner = gdbarch_inner_than (gdbarch, l.stack_addr, r.stack_addr); 859 860 frame_debug_printf ("is l=%s inner than r=%s? %d", 861 l.to_string ().c_str (), r.to_string ().c_str (), 862 inner); 863 864 return inner; 865 } 866 867 frame_info_ptr 868 frame_find_by_id (struct frame_id id) 869 { 870 frame_info_ptr frame, prev_frame; 871 872 /* ZERO denotes the null frame, let the caller decide what to do 873 about it. Should it instead return get_current_frame()? */ 874 if (!frame_id_p (id)) 875 return NULL; 876 877 /* Check for the sentinel frame. */ 878 if (id == sentinel_frame_id) 879 return frame_info_ptr (sentinel_frame); 880 881 /* Try using the frame stash first. Finding it there removes the need 882 to perform the search by looping over all frames, which can be very 883 CPU-intensive if the number of frames is very high (the loop is O(n) 884 and get_prev_frame performs a series of checks that are relatively 885 expensive). This optimization is particularly useful when this function 886 is called from another function (such as value_fetch_lazy, case 887 VALUE_LVAL (val) == lval_register) which already loops over all frames, 888 making the overall behavior O(n^2). */ 889 frame = frame_stash_find (id); 890 if (frame) 891 return frame; 892 893 for (frame = get_current_frame (); ; frame = prev_frame) 894 { 895 struct frame_id self = get_frame_id (frame); 896 897 if (id == self) 898 /* An exact match. */ 899 return frame; 900 901 prev_frame = get_prev_frame (frame); 902 if (!prev_frame) 903 return NULL; 904 905 /* As a safety net to avoid unnecessary backtracing while trying 906 to find an invalid ID, we check for a common situation where 907 we can detect from comparing stack addresses that no other 908 frame in the current frame chain can have this ID. See the 909 comment at frame_id_inner for details. */ 910 if (get_frame_type (frame) == NORMAL_FRAME 911 && !frame_id_inner (get_frame_arch (frame), id, self) 912 && frame_id_inner (get_frame_arch (prev_frame), id, 913 get_frame_id (prev_frame))) 914 return NULL; 915 } 916 return NULL; 917 } 918 919 static CORE_ADDR 920 frame_unwind_pc (frame_info_ptr this_frame) 921 { 922 if (this_frame->prev_pc.status == CC_UNKNOWN) 923 { 924 struct gdbarch *prev_gdbarch; 925 CORE_ADDR pc = 0; 926 bool pc_p = false; 927 928 /* The right way. The `pure' way. The one true way. This 929 method depends solely on the register-unwind code to 930 determine the value of registers in THIS frame, and hence 931 the value of this frame's PC (resume address). A typical 932 implementation is no more than: 933 934 frame_unwind_register (this_frame, ISA_PC_REGNUM, buf); 935 return extract_unsigned_integer (buf, size of ISA_PC_REGNUM); 936 937 Note: this method is very heavily dependent on a correct 938 register-unwind implementation, it pays to fix that 939 method first; this method is frame type agnostic, since 940 it only deals with register values, it works with any 941 frame. This is all in stark contrast to the old 942 FRAME_SAVED_PC which would try to directly handle all the 943 different ways that a PC could be unwound. */ 944 prev_gdbarch = frame_unwind_arch (this_frame); 945 946 try 947 { 948 pc = gdbarch_unwind_pc (prev_gdbarch, this_frame); 949 pc_p = true; 950 } 951 catch (const gdb_exception_error &ex) 952 { 953 if (ex.error == NOT_AVAILABLE_ERROR) 954 { 955 this_frame->prev_pc.status = CC_UNAVAILABLE; 956 957 frame_debug_printf ("this_frame=%d -> <unavailable>", 958 this_frame->level); 959 } 960 else if (ex.error == OPTIMIZED_OUT_ERROR) 961 { 962 this_frame->prev_pc.status = CC_NOT_SAVED; 963 964 frame_debug_printf ("this_frame=%d -> <not saved>", 965 this_frame->level); 966 } 967 else 968 throw; 969 } 970 971 if (pc_p) 972 { 973 this_frame->prev_pc.value = pc; 974 this_frame->prev_pc.status = CC_VALUE; 975 976 frame_debug_printf ("this_frame=%d -> %s", 977 this_frame->level, 978 hex_string (this_frame->prev_pc.value)); 979 } 980 } 981 982 if (this_frame->prev_pc.status == CC_VALUE) 983 return this_frame->prev_pc.value; 984 else if (this_frame->prev_pc.status == CC_UNAVAILABLE) 985 throw_error (NOT_AVAILABLE_ERROR, _("PC not available")); 986 else if (this_frame->prev_pc.status == CC_NOT_SAVED) 987 throw_error (OPTIMIZED_OUT_ERROR, _("PC not saved")); 988 else 989 internal_error ("unexpected prev_pc status: %d", 990 (int) this_frame->prev_pc.status); 991 } 992 993 CORE_ADDR 994 frame_unwind_caller_pc (frame_info_ptr this_frame) 995 { 996 this_frame = skip_artificial_frames (this_frame); 997 998 /* We must have a non-artificial frame. The caller is supposed to check 999 the result of frame_unwind_caller_id (), which returns NULL_FRAME_ID 1000 in this case. */ 1001 gdb_assert (this_frame != NULL); 1002 1003 return frame_unwind_pc (this_frame); 1004 } 1005 1006 bool 1007 get_frame_func_if_available (frame_info_ptr this_frame, CORE_ADDR *pc) 1008 { 1009 frame_info *next_frame = this_frame->next; 1010 1011 if (next_frame->prev_func.status == CC_UNKNOWN) 1012 { 1013 CORE_ADDR addr_in_block; 1014 1015 /* Make certain that this, and not the adjacent, function is 1016 found. */ 1017 if (!get_frame_address_in_block_if_available (this_frame, &addr_in_block)) 1018 { 1019 next_frame->prev_func.status = CC_UNAVAILABLE; 1020 1021 frame_debug_printf ("this_frame=%d -> unavailable", 1022 this_frame->level); 1023 } 1024 else 1025 { 1026 next_frame->prev_func.status = CC_VALUE; 1027 next_frame->prev_func.addr = get_pc_function_start (addr_in_block); 1028 1029 frame_debug_printf ("this_frame=%d -> %s", 1030 this_frame->level, 1031 hex_string (next_frame->prev_func.addr)); 1032 } 1033 } 1034 1035 if (next_frame->prev_func.status == CC_UNAVAILABLE) 1036 { 1037 *pc = -1; 1038 return false; 1039 } 1040 else 1041 { 1042 gdb_assert (next_frame->prev_func.status == CC_VALUE); 1043 1044 *pc = next_frame->prev_func.addr; 1045 return true; 1046 } 1047 } 1048 1049 CORE_ADDR 1050 get_frame_func (frame_info_ptr this_frame) 1051 { 1052 CORE_ADDR pc; 1053 1054 if (!get_frame_func_if_available (this_frame, &pc)) 1055 throw_error (NOT_AVAILABLE_ERROR, _("PC not available")); 1056 1057 return pc; 1058 } 1059 1060 std::unique_ptr<readonly_detached_regcache> 1061 frame_save_as_regcache (frame_info_ptr this_frame) 1062 { 1063 auto cooked_read = [this_frame] (int regnum, gdb_byte *buf) 1064 { 1065 if (!deprecated_frame_register_read (this_frame, regnum, buf)) 1066 return REG_UNAVAILABLE; 1067 else 1068 return REG_VALID; 1069 }; 1070 1071 std::unique_ptr<readonly_detached_regcache> regcache 1072 (new readonly_detached_regcache (get_frame_arch (this_frame), cooked_read)); 1073 1074 return regcache; 1075 } 1076 1077 void 1078 frame_pop (frame_info_ptr this_frame) 1079 { 1080 frame_info_ptr prev_frame; 1081 1082 if (get_frame_type (this_frame) == DUMMY_FRAME) 1083 { 1084 /* Popping a dummy frame involves restoring more than just registers. 1085 dummy_frame_pop does all the work. */ 1086 dummy_frame_pop (get_frame_id (this_frame), inferior_thread ()); 1087 return; 1088 } 1089 1090 /* Ensure that we have a frame to pop to. */ 1091 prev_frame = get_prev_frame_always (this_frame); 1092 1093 if (!prev_frame) 1094 error (_("Cannot pop the initial frame.")); 1095 1096 /* Ignore TAILCALL_FRAME type frames, they were executed already before 1097 entering THISFRAME. */ 1098 prev_frame = skip_tailcall_frames (prev_frame); 1099 1100 if (prev_frame == NULL) 1101 error (_("Cannot find the caller frame.")); 1102 1103 /* Make a copy of all the register values unwound from this frame. 1104 Save them in a scratch buffer so that there isn't a race between 1105 trying to extract the old values from the current regcache while 1106 at the same time writing new values into that same cache. */ 1107 std::unique_ptr<readonly_detached_regcache> scratch 1108 = frame_save_as_regcache (prev_frame); 1109 1110 /* FIXME: cagney/2003-03-16: It should be possible to tell the 1111 target's register cache that it is about to be hit with a burst 1112 register transfer and that the sequence of register writes should 1113 be batched. The pair target_prepare_to_store() and 1114 target_store_registers() kind of suggest this functionality. 1115 Unfortunately, they don't implement it. Their lack of a formal 1116 definition can lead to targets writing back bogus values 1117 (arguably a bug in the target code mind). */ 1118 /* Now copy those saved registers into the current regcache. */ 1119 get_current_regcache ()->restore (scratch.get ()); 1120 1121 /* We've made right mess of GDB's local state, just discard 1122 everything. */ 1123 reinit_frame_cache (); 1124 } 1125 1126 void 1127 frame_register_unwind (frame_info_ptr next_frame, int regnum, 1128 int *optimizedp, int *unavailablep, 1129 enum lval_type *lvalp, CORE_ADDR *addrp, 1130 int *realnump, gdb_byte *bufferp) 1131 { 1132 struct value *value; 1133 1134 /* Require all but BUFFERP to be valid. A NULL BUFFERP indicates 1135 that the value proper does not need to be fetched. */ 1136 gdb_assert (optimizedp != NULL); 1137 gdb_assert (lvalp != NULL); 1138 gdb_assert (addrp != NULL); 1139 gdb_assert (realnump != NULL); 1140 /* gdb_assert (bufferp != NULL); */ 1141 1142 value = frame_unwind_register_value (next_frame, regnum); 1143 1144 gdb_assert (value != NULL); 1145 1146 *optimizedp = value_optimized_out (value); 1147 *unavailablep = !value_entirely_available (value); 1148 *lvalp = VALUE_LVAL (value); 1149 *addrp = value_address (value); 1150 if (*lvalp == lval_register) 1151 *realnump = VALUE_REGNUM (value); 1152 else 1153 *realnump = -1; 1154 1155 if (bufferp) 1156 { 1157 if (!*optimizedp && !*unavailablep) 1158 memcpy (bufferp, value_contents_all (value).data (), 1159 value_type (value)->length ()); 1160 else 1161 memset (bufferp, 0, value_type (value)->length ()); 1162 } 1163 1164 /* Dispose of the new value. This prevents watchpoints from 1165 trying to watch the saved frame pointer. */ 1166 release_value (value); 1167 } 1168 1169 /* Get the value of the register that belongs to this FRAME. This 1170 function is a wrapper to the call sequence ``frame_register_unwind 1171 (get_next_frame (FRAME))''. As per frame_register_unwind(), if 1172 VALUEP is NULL, the registers value is not fetched/computed. */ 1173 1174 static void 1175 frame_register (frame_info_ptr frame, int regnum, 1176 int *optimizedp, int *unavailablep, enum lval_type *lvalp, 1177 CORE_ADDR *addrp, int *realnump, gdb_byte *bufferp) 1178 { 1179 /* Require all but BUFFERP to be valid. A NULL BUFFERP indicates 1180 that the value proper does not need to be fetched. */ 1181 gdb_assert (optimizedp != NULL); 1182 gdb_assert (lvalp != NULL); 1183 gdb_assert (addrp != NULL); 1184 gdb_assert (realnump != NULL); 1185 /* gdb_assert (bufferp != NULL); */ 1186 1187 /* Obtain the register value by unwinding the register from the next 1188 (more inner frame). */ 1189 gdb_assert (frame != NULL && frame->next != NULL); 1190 frame_register_unwind (frame_info_ptr (frame->next), regnum, optimizedp, 1191 unavailablep, lvalp, addrp, realnump, bufferp); 1192 } 1193 1194 void 1195 frame_unwind_register (frame_info_ptr next_frame, int regnum, gdb_byte *buf) 1196 { 1197 int optimized; 1198 int unavailable; 1199 CORE_ADDR addr; 1200 int realnum; 1201 enum lval_type lval; 1202 1203 frame_register_unwind (next_frame, regnum, &optimized, &unavailable, 1204 &lval, &addr, &realnum, buf); 1205 1206 if (optimized) 1207 throw_error (OPTIMIZED_OUT_ERROR, 1208 _("Register %d was not saved"), regnum); 1209 if (unavailable) 1210 throw_error (NOT_AVAILABLE_ERROR, 1211 _("Register %d is not available"), regnum); 1212 } 1213 1214 void 1215 get_frame_register (frame_info_ptr frame, 1216 int regnum, gdb_byte *buf) 1217 { 1218 frame_unwind_register (frame_info_ptr (frame->next), regnum, buf); 1219 } 1220 1221 struct value * 1222 frame_unwind_register_value (frame_info_ptr next_frame, int regnum) 1223 { 1224 FRAME_SCOPED_DEBUG_ENTER_EXIT; 1225 1226 gdb_assert (next_frame != NULL); 1227 gdbarch *gdbarch = frame_unwind_arch (next_frame); 1228 frame_debug_printf ("frame=%d, regnum=%d(%s)", 1229 next_frame->level, regnum, 1230 user_reg_map_regnum_to_name (gdbarch, regnum)); 1231 1232 /* Find the unwinder. */ 1233 if (next_frame->unwind == NULL) 1234 frame_unwind_find_by_frame (next_frame, &next_frame->prologue_cache); 1235 1236 /* Ask this frame to unwind its register. */ 1237 value *value = next_frame->unwind->prev_register (next_frame, 1238 &next_frame->prologue_cache, 1239 regnum); 1240 1241 if (frame_debug) 1242 { 1243 string_file debug_file; 1244 1245 gdb_printf (&debug_file, " ->"); 1246 if (value_optimized_out (value)) 1247 { 1248 gdb_printf (&debug_file, " "); 1249 val_print_not_saved (&debug_file); 1250 } 1251 else 1252 { 1253 if (VALUE_LVAL (value) == lval_register) 1254 gdb_printf (&debug_file, " register=%d", 1255 VALUE_REGNUM (value)); 1256 else if (VALUE_LVAL (value) == lval_memory) 1257 gdb_printf (&debug_file, " address=%s", 1258 paddress (gdbarch, 1259 value_address (value))); 1260 else 1261 gdb_printf (&debug_file, " computed"); 1262 1263 if (value_lazy (value)) 1264 gdb_printf (&debug_file, " lazy"); 1265 else 1266 { 1267 int i; 1268 gdb::array_view<const gdb_byte> buf = value_contents (value); 1269 1270 gdb_printf (&debug_file, " bytes="); 1271 gdb_printf (&debug_file, "["); 1272 for (i = 0; i < register_size (gdbarch, regnum); i++) 1273 gdb_printf (&debug_file, "%02x", buf[i]); 1274 gdb_printf (&debug_file, "]"); 1275 } 1276 } 1277 1278 frame_debug_printf ("%s", debug_file.c_str ()); 1279 } 1280 1281 return value; 1282 } 1283 1284 struct value * 1285 get_frame_register_value (frame_info_ptr frame, int regnum) 1286 { 1287 return frame_unwind_register_value (frame_info_ptr (frame->next), regnum); 1288 } 1289 1290 LONGEST 1291 frame_unwind_register_signed (frame_info_ptr next_frame, int regnum) 1292 { 1293 struct gdbarch *gdbarch = frame_unwind_arch (next_frame); 1294 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch); 1295 struct value *value = frame_unwind_register_value (next_frame, regnum); 1296 1297 gdb_assert (value != NULL); 1298 1299 if (value_optimized_out (value)) 1300 { 1301 throw_error (OPTIMIZED_OUT_ERROR, 1302 _("Register %d was not saved"), regnum); 1303 } 1304 if (!value_entirely_available (value)) 1305 { 1306 throw_error (NOT_AVAILABLE_ERROR, 1307 _("Register %d is not available"), regnum); 1308 } 1309 1310 LONGEST r = extract_signed_integer (value_contents_all (value), byte_order); 1311 1312 release_value (value); 1313 return r; 1314 } 1315 1316 LONGEST 1317 get_frame_register_signed (frame_info_ptr frame, int regnum) 1318 { 1319 return frame_unwind_register_signed (frame_info_ptr (frame->next), regnum); 1320 } 1321 1322 ULONGEST 1323 frame_unwind_register_unsigned (frame_info_ptr next_frame, int regnum) 1324 { 1325 struct gdbarch *gdbarch = frame_unwind_arch (next_frame); 1326 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch); 1327 int size = register_size (gdbarch, regnum); 1328 struct value *value = frame_unwind_register_value (next_frame, regnum); 1329 1330 gdb_assert (value != NULL); 1331 1332 if (value_optimized_out (value)) 1333 { 1334 throw_error (OPTIMIZED_OUT_ERROR, 1335 _("Register %d was not saved"), regnum); 1336 } 1337 if (!value_entirely_available (value)) 1338 { 1339 throw_error (NOT_AVAILABLE_ERROR, 1340 _("Register %d is not available"), regnum); 1341 } 1342 1343 ULONGEST r = extract_unsigned_integer (value_contents_all (value).data (), 1344 size, byte_order); 1345 1346 release_value (value); 1347 return r; 1348 } 1349 1350 ULONGEST 1351 get_frame_register_unsigned (frame_info_ptr frame, int regnum) 1352 { 1353 return frame_unwind_register_unsigned (frame_info_ptr (frame->next), regnum); 1354 } 1355 1356 bool 1357 read_frame_register_unsigned (frame_info_ptr frame, int regnum, 1358 ULONGEST *val) 1359 { 1360 struct value *regval = get_frame_register_value (frame, regnum); 1361 1362 if (!value_optimized_out (regval) 1363 && value_entirely_available (regval)) 1364 { 1365 struct gdbarch *gdbarch = get_frame_arch (frame); 1366 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch); 1367 int size = register_size (gdbarch, VALUE_REGNUM (regval)); 1368 1369 *val = extract_unsigned_integer (value_contents (regval).data (), size, 1370 byte_order); 1371 return true; 1372 } 1373 1374 return false; 1375 } 1376 1377 void 1378 put_frame_register (frame_info_ptr frame, int regnum, 1379 const gdb_byte *buf) 1380 { 1381 struct gdbarch *gdbarch = get_frame_arch (frame); 1382 int realnum; 1383 int optim; 1384 int unavail; 1385 enum lval_type lval; 1386 CORE_ADDR addr; 1387 1388 frame_register (frame, regnum, &optim, &unavail, 1389 &lval, &addr, &realnum, NULL); 1390 if (optim) 1391 error (_("Attempt to assign to a register that was not saved.")); 1392 switch (lval) 1393 { 1394 case lval_memory: 1395 { 1396 write_memory (addr, buf, register_size (gdbarch, regnum)); 1397 break; 1398 } 1399 case lval_register: 1400 get_current_regcache ()->cooked_write (realnum, buf); 1401 break; 1402 default: 1403 error (_("Attempt to assign to an unmodifiable value.")); 1404 } 1405 } 1406 1407 /* This function is deprecated. Use get_frame_register_value instead, 1408 which provides more accurate information. 1409 1410 Find and return the value of REGNUM for the specified stack frame. 1411 The number of bytes copied is REGISTER_SIZE (REGNUM). 1412 1413 Returns 0 if the register value could not be found. */ 1414 1415 bool 1416 deprecated_frame_register_read (frame_info_ptr frame, int regnum, 1417 gdb_byte *myaddr) 1418 { 1419 int optimized; 1420 int unavailable; 1421 enum lval_type lval; 1422 CORE_ADDR addr; 1423 int realnum; 1424 1425 frame_register (frame, regnum, &optimized, &unavailable, 1426 &lval, &addr, &realnum, myaddr); 1427 1428 return !optimized && !unavailable; 1429 } 1430 1431 bool 1432 get_frame_register_bytes (frame_info_ptr frame, int regnum, 1433 CORE_ADDR offset, 1434 gdb::array_view<gdb_byte> buffer, 1435 int *optimizedp, int *unavailablep) 1436 { 1437 struct gdbarch *gdbarch = get_frame_arch (frame); 1438 int i; 1439 int maxsize; 1440 int numregs; 1441 1442 /* Skip registers wholly inside of OFFSET. */ 1443 while (offset >= register_size (gdbarch, regnum)) 1444 { 1445 offset -= register_size (gdbarch, regnum); 1446 regnum++; 1447 } 1448 1449 /* Ensure that we will not read beyond the end of the register file. 1450 This can only ever happen if the debug information is bad. */ 1451 maxsize = -offset; 1452 numregs = gdbarch_num_cooked_regs (gdbarch); 1453 for (i = regnum; i < numregs; i++) 1454 { 1455 int thissize = register_size (gdbarch, i); 1456 1457 if (thissize == 0) 1458 break; /* This register is not available on this architecture. */ 1459 maxsize += thissize; 1460 } 1461 1462 int len = buffer.size (); 1463 if (len > maxsize) 1464 error (_("Bad debug information detected: " 1465 "Attempt to read %d bytes from registers."), len); 1466 1467 /* Copy the data. */ 1468 while (len > 0) 1469 { 1470 int curr_len = register_size (gdbarch, regnum) - offset; 1471 1472 if (curr_len > len) 1473 curr_len = len; 1474 1475 gdb_byte *myaddr = buffer.data (); 1476 1477 if (curr_len == register_size (gdbarch, regnum)) 1478 { 1479 enum lval_type lval; 1480 CORE_ADDR addr; 1481 int realnum; 1482 1483 frame_register (frame, regnum, optimizedp, unavailablep, 1484 &lval, &addr, &realnum, myaddr); 1485 if (*optimizedp || *unavailablep) 1486 return false; 1487 } 1488 else 1489 { 1490 struct value *value 1491 = frame_unwind_register_value (frame_info_ptr (frame->next), 1492 regnum); 1493 gdb_assert (value != NULL); 1494 *optimizedp = value_optimized_out (value); 1495 *unavailablep = !value_entirely_available (value); 1496 1497 if (*optimizedp || *unavailablep) 1498 { 1499 release_value (value); 1500 return false; 1501 } 1502 1503 memcpy (myaddr, value_contents_all (value).data () + offset, 1504 curr_len); 1505 release_value (value); 1506 } 1507 1508 myaddr += curr_len; 1509 len -= curr_len; 1510 offset = 0; 1511 regnum++; 1512 } 1513 1514 *optimizedp = 0; 1515 *unavailablep = 0; 1516 1517 return true; 1518 } 1519 1520 void 1521 put_frame_register_bytes (frame_info_ptr frame, int regnum, 1522 CORE_ADDR offset, 1523 gdb::array_view<const gdb_byte> buffer) 1524 { 1525 struct gdbarch *gdbarch = get_frame_arch (frame); 1526 1527 /* Skip registers wholly inside of OFFSET. */ 1528 while (offset >= register_size (gdbarch, regnum)) 1529 { 1530 offset -= register_size (gdbarch, regnum); 1531 regnum++; 1532 } 1533 1534 int len = buffer.size (); 1535 /* Copy the data. */ 1536 while (len > 0) 1537 { 1538 int curr_len = register_size (gdbarch, regnum) - offset; 1539 1540 if (curr_len > len) 1541 curr_len = len; 1542 1543 const gdb_byte *myaddr = buffer.data (); 1544 if (curr_len == register_size (gdbarch, regnum)) 1545 { 1546 put_frame_register (frame, regnum, myaddr); 1547 } 1548 else 1549 { 1550 struct value *value 1551 = frame_unwind_register_value (frame_info_ptr (frame->next), 1552 regnum); 1553 gdb_assert (value != NULL); 1554 1555 memcpy ((char *) value_contents_writeable (value).data () + offset, 1556 myaddr, curr_len); 1557 put_frame_register (frame, regnum, 1558 value_contents_raw (value).data ()); 1559 release_value (value); 1560 } 1561 1562 myaddr += curr_len; 1563 len -= curr_len; 1564 offset = 0; 1565 regnum++; 1566 } 1567 } 1568 1569 /* Create a sentinel frame. */ 1570 1571 static frame_info * 1572 create_sentinel_frame (struct program_space *pspace, struct regcache *regcache) 1573 { 1574 frame_info *frame = FRAME_OBSTACK_ZALLOC (struct frame_info); 1575 1576 frame->level = -1; 1577 frame->pspace = pspace; 1578 frame->aspace = regcache->aspace (); 1579 /* Explicitly initialize the sentinel frame's cache. Provide it 1580 with the underlying regcache. In the future additional 1581 information, such as the frame's thread will be added. */ 1582 frame->prologue_cache = sentinel_frame_cache (regcache); 1583 /* For the moment there is only one sentinel frame implementation. */ 1584 frame->unwind = &sentinel_frame_unwind; 1585 /* Link this frame back to itself. The frame is self referential 1586 (the unwound PC is the same as the pc), so make it so. */ 1587 frame->next = frame; 1588 /* The sentinel frame has a special ID. */ 1589 frame->this_id.p = frame_id_status::COMPUTED; 1590 frame->this_id.value = sentinel_frame_id; 1591 1592 frame_debug_printf (" -> %s", frame->to_string ().c_str ()); 1593 1594 return frame; 1595 } 1596 1597 /* Cache for frame addresses already read by gdb. Valid only while 1598 inferior is stopped. Control variables for the frame cache should 1599 be local to this module. */ 1600 1601 static struct obstack frame_cache_obstack; 1602 1603 void * 1604 frame_obstack_zalloc (unsigned long size) 1605 { 1606 void *data = obstack_alloc (&frame_cache_obstack, size); 1607 1608 memset (data, 0, size); 1609 return data; 1610 } 1611 1612 static frame_info_ptr get_prev_frame_always_1 (frame_info_ptr this_frame); 1613 1614 frame_info_ptr 1615 get_current_frame (void) 1616 { 1617 frame_info_ptr current_frame; 1618 1619 /* First check, and report, the lack of registers. Having GDB 1620 report "No stack!" or "No memory" when the target doesn't even 1621 have registers is very confusing. Besides, "printcmd.exp" 1622 explicitly checks that ``print $pc'' with no registers prints "No 1623 registers". */ 1624 if (!target_has_registers ()) 1625 error (_("No registers.")); 1626 if (!target_has_stack ()) 1627 error (_("No stack.")); 1628 if (!target_has_memory ()) 1629 error (_("No memory.")); 1630 /* Traceframes are effectively a substitute for the live inferior. */ 1631 if (get_traceframe_number () < 0) 1632 validate_registers_access (); 1633 1634 if (sentinel_frame == NULL) 1635 sentinel_frame = 1636 create_sentinel_frame (current_program_space, get_current_regcache ()); 1637 1638 /* Set the current frame before computing the frame id, to avoid 1639 recursion inside compute_frame_id, in case the frame's 1640 unwinder decides to do a symbol lookup (which depends on the 1641 selected frame's block). 1642 1643 This call must always succeed. In particular, nothing inside 1644 get_prev_frame_always_1 should try to unwind from the 1645 sentinel frame, because that could fail/throw, and we always 1646 want to leave with the current frame created and linked in -- 1647 we should never end up with the sentinel frame as outermost 1648 frame. */ 1649 current_frame = get_prev_frame_always_1 (frame_info_ptr (sentinel_frame)); 1650 gdb_assert (current_frame != NULL); 1651 1652 return current_frame; 1653 } 1654 1655 /* The "selected" stack frame is used by default for local and arg 1656 access. 1657 1658 The "single source of truth" for the selected frame is the 1659 SELECTED_FRAME_ID / SELECTED_FRAME_LEVEL pair. 1660 1661 Frame IDs can be saved/restored across reinitializing the frame 1662 cache, while frame_info pointers can't (frame_info objects are 1663 invalidated). If we know the corresponding frame_info object, it 1664 is cached in SELECTED_FRAME. 1665 1666 If SELECTED_FRAME_ID / SELECTED_FRAME_LEVEL are null_frame_id / -1, 1667 and the target has stack and is stopped, the selected frame is the 1668 current (innermost) frame. This means that SELECTED_FRAME_LEVEL is 1669 never 0 and SELECTED_FRAME_ID is never the ID of the innermost 1670 frame. 1671 1672 If SELECTED_FRAME_ID / SELECTED_FRAME_LEVEL are null_frame_id / -1, 1673 and the target has no stack or is executing, then there's no 1674 selected frame. */ 1675 static frame_id selected_frame_id = null_frame_id; 1676 static int selected_frame_level = -1; 1677 1678 /* The cached frame_info object pointing to the selected frame. 1679 Looked up on demand by get_selected_frame. */ 1680 static frame_info_ptr selected_frame; 1681 1682 /* See frame.h. */ 1683 1684 void 1685 save_selected_frame (frame_id *frame_id, int *frame_level) 1686 noexcept 1687 { 1688 *frame_id = selected_frame_id; 1689 *frame_level = selected_frame_level; 1690 } 1691 1692 /* See frame.h. */ 1693 1694 void 1695 restore_selected_frame (frame_id frame_id, int frame_level) 1696 noexcept 1697 { 1698 /* save_selected_frame never returns level == 0, so we shouldn't see 1699 it here either. */ 1700 gdb_assert (frame_level != 0); 1701 1702 /* FRAME_ID can be null_frame_id only IFF frame_level is -1. */ 1703 gdb_assert ((frame_level == -1 && !frame_id_p (frame_id)) 1704 || (frame_level != -1 && frame_id_p (frame_id))); 1705 1706 selected_frame_id = frame_id; 1707 selected_frame_level = frame_level; 1708 1709 /* Will be looked up later by get_selected_frame. */ 1710 selected_frame = nullptr; 1711 } 1712 1713 /* Lookup the frame_info object for the selected frame FRAME_ID / 1714 FRAME_LEVEL and cache the result. 1715 1716 If FRAME_LEVEL > 0 and the originally selected frame isn't found, 1717 warn and select the innermost (current) frame. */ 1718 1719 static void 1720 lookup_selected_frame (struct frame_id a_frame_id, int frame_level) 1721 { 1722 frame_info_ptr frame = NULL; 1723 int count; 1724 1725 /* This either means there was no selected frame, or the selected 1726 frame was the current frame. In either case, select the current 1727 frame. */ 1728 if (frame_level == -1) 1729 { 1730 select_frame (get_current_frame ()); 1731 return; 1732 } 1733 1734 /* select_frame never saves 0 in SELECTED_FRAME_LEVEL, so we 1735 shouldn't see it here. */ 1736 gdb_assert (frame_level > 0); 1737 1738 /* Restore by level first, check if the frame id is the same as 1739 expected. If that fails, try restoring by frame id. If that 1740 fails, nothing to do, just warn the user. */ 1741 1742 count = frame_level; 1743 frame = find_relative_frame (get_current_frame (), &count); 1744 if (count == 0 1745 && frame != NULL 1746 /* The frame ids must match - either both valid or both 1747 outer_frame_id. The latter case is not failsafe, but since 1748 it's highly unlikely the search by level finds the wrong 1749 frame, it's 99.9(9)% of the time (for all practical purposes) 1750 safe. */ 1751 && get_frame_id (frame) == a_frame_id) 1752 { 1753 /* Cool, all is fine. */ 1754 select_frame (frame); 1755 return; 1756 } 1757 1758 frame = frame_find_by_id (a_frame_id); 1759 if (frame != NULL) 1760 { 1761 /* Cool, refound it. */ 1762 select_frame (frame); 1763 return; 1764 } 1765 1766 /* Nothing else to do, the frame layout really changed. Select the 1767 innermost stack frame. */ 1768 select_frame (get_current_frame ()); 1769 1770 /* Warn the user. */ 1771 if (frame_level > 0 && !current_uiout->is_mi_like_p ()) 1772 { 1773 warning (_("Couldn't restore frame #%d in " 1774 "current thread. Bottom (innermost) frame selected:"), 1775 frame_level); 1776 /* For MI, we should probably have a notification about current 1777 frame change. But this error is not very likely, so don't 1778 bother for now. */ 1779 print_stack_frame (get_selected_frame (NULL), 1, SRC_AND_LOC, 1); 1780 } 1781 } 1782 1783 bool 1784 has_stack_frames () 1785 { 1786 if (!target_has_registers () || !target_has_stack () 1787 || !target_has_memory ()) 1788 return false; 1789 1790 /* Traceframes are effectively a substitute for the live inferior. */ 1791 if (get_traceframe_number () < 0) 1792 { 1793 /* No current inferior, no frame. */ 1794 if (inferior_ptid == null_ptid) 1795 return false; 1796 1797 thread_info *tp = inferior_thread (); 1798 /* Don't try to read from a dead thread. */ 1799 if (tp->state == THREAD_EXITED) 1800 return false; 1801 1802 /* ... or from a spinning thread. */ 1803 if (tp->executing ()) 1804 return false; 1805 } 1806 1807 return true; 1808 } 1809 1810 /* See frame.h. */ 1811 1812 frame_info_ptr 1813 get_selected_frame (const char *message) 1814 { 1815 if (selected_frame == NULL) 1816 { 1817 if (message != NULL && !has_stack_frames ()) 1818 error (("%s"), message); 1819 1820 lookup_selected_frame (selected_frame_id, selected_frame_level); 1821 } 1822 /* There is always a frame. */ 1823 gdb_assert (selected_frame != NULL); 1824 return selected_frame; 1825 } 1826 1827 /* This is a variant of get_selected_frame() which can be called when 1828 the inferior does not have a frame; in that case it will return 1829 NULL instead of calling error(). */ 1830 1831 frame_info_ptr 1832 deprecated_safe_get_selected_frame (void) 1833 { 1834 if (!has_stack_frames ()) 1835 return NULL; 1836 return get_selected_frame (NULL); 1837 } 1838 1839 /* Invalidate the selected frame. */ 1840 1841 static void 1842 invalidate_selected_frame () 1843 { 1844 selected_frame = nullptr; 1845 selected_frame_level = -1; 1846 selected_frame_id = null_frame_id; 1847 } 1848 1849 /* See frame.h. */ 1850 1851 void 1852 select_frame (frame_info_ptr fi) 1853 { 1854 gdb_assert (fi != nullptr); 1855 1856 selected_frame = fi; 1857 selected_frame_level = frame_relative_level (fi); 1858 if (selected_frame_level == 0) 1859 { 1860 /* Treat the current frame especially -- we want to always 1861 save/restore it without warning, even if the frame ID changes 1862 (see lookup_selected_frame). E.g.: 1863 1864 // The current frame is selected, the target had just stopped. 1865 { 1866 scoped_restore_selected_frame restore_frame; 1867 some_operation_that_changes_the_stack (); 1868 } 1869 // scoped_restore_selected_frame's dtor runs, but the 1870 // original frame_id can't be found. No matter whether it 1871 // is found or not, we still end up with the now-current 1872 // frame selected. Warning in lookup_selected_frame in this 1873 // case seems pointless. 1874 1875 Also get_frame_id may access the target's registers/memory, 1876 and thus skipping get_frame_id optimizes the common case. 1877 1878 Saving the selected frame this way makes get_selected_frame 1879 and restore_current_frame return/re-select whatever frame is 1880 the innermost (current) then. */ 1881 selected_frame_level = -1; 1882 selected_frame_id = null_frame_id; 1883 } 1884 else 1885 selected_frame_id = get_frame_id (fi); 1886 1887 /* NOTE: cagney/2002-05-04: FI can be NULL. This occurs when the 1888 frame is being invalidated. */ 1889 1890 /* FIXME: kseitz/2002-08-28: It would be nice to call 1891 selected_frame_level_changed_event() right here, but due to limitations 1892 in the current interfaces, we would end up flooding UIs with events 1893 because select_frame() is used extensively internally. 1894 1895 Once we have frame-parameterized frame (and frame-related) commands, 1896 the event notification can be moved here, since this function will only 1897 be called when the user's selected frame is being changed. */ 1898 1899 /* Ensure that symbols for this frame are read in. Also, determine the 1900 source language of this frame, and switch to it if desired. */ 1901 if (fi) 1902 { 1903 CORE_ADDR pc; 1904 1905 /* We retrieve the frame's symtab by using the frame PC. 1906 However we cannot use the frame PC as-is, because it usually 1907 points to the instruction following the "call", which is 1908 sometimes the first instruction of another function. So we 1909 rely on get_frame_address_in_block() which provides us with a 1910 PC which is guaranteed to be inside the frame's code 1911 block. */ 1912 if (get_frame_address_in_block_if_available (fi, &pc)) 1913 { 1914 struct compunit_symtab *cust = find_pc_compunit_symtab (pc); 1915 1916 if (cust != NULL 1917 && cust->language () != current_language->la_language 1918 && cust->language () != language_unknown 1919 && language_mode == language_mode_auto) 1920 set_language (cust->language ()); 1921 } 1922 } 1923 } 1924 1925 /* Create an arbitrary (i.e. address specified by user) or innermost frame. 1926 Always returns a non-NULL value. */ 1927 1928 frame_info_ptr 1929 create_new_frame (CORE_ADDR addr, CORE_ADDR pc) 1930 { 1931 frame_info *fi; 1932 1933 frame_debug_printf ("addr=%s, pc=%s", hex_string (addr), hex_string (pc)); 1934 1935 fi = FRAME_OBSTACK_ZALLOC (struct frame_info); 1936 1937 fi->next = create_sentinel_frame (current_program_space, 1938 get_current_regcache ()); 1939 1940 /* Set/update this frame's cached PC value, found in the next frame. 1941 Do this before looking for this frame's unwinder. A sniffer is 1942 very likely to read this, and the corresponding unwinder is 1943 entitled to rely that the PC doesn't magically change. */ 1944 fi->next->prev_pc.value = pc; 1945 fi->next->prev_pc.status = CC_VALUE; 1946 1947 /* We currently assume that frame chain's can't cross spaces. */ 1948 fi->pspace = fi->next->pspace; 1949 fi->aspace = fi->next->aspace; 1950 1951 /* Select/initialize both the unwind function and the frame's type 1952 based on the PC. */ 1953 frame_unwind_find_by_frame (frame_info_ptr (fi), &fi->prologue_cache); 1954 1955 fi->this_id.p = frame_id_status::COMPUTED; 1956 fi->this_id.value = frame_id_build (addr, pc); 1957 1958 frame_debug_printf (" -> %s", fi->to_string ().c_str ()); 1959 1960 return frame_info_ptr (fi); 1961 } 1962 1963 /* Return the frame that THIS_FRAME calls (NULL if THIS_FRAME is the 1964 innermost frame). Be careful to not fall off the bottom of the 1965 frame chain and onto the sentinel frame. */ 1966 1967 frame_info_ptr 1968 get_next_frame (frame_info_ptr this_frame) 1969 { 1970 if (this_frame->level > 0) 1971 return frame_info_ptr (this_frame->next); 1972 else 1973 return NULL; 1974 } 1975 1976 /* Return the frame that THIS_FRAME calls. If THIS_FRAME is the 1977 innermost (i.e. current) frame, return the sentinel frame. Thus, 1978 unlike get_next_frame(), NULL will never be returned. */ 1979 1980 frame_info_ptr 1981 get_next_frame_sentinel_okay (frame_info_ptr this_frame) 1982 { 1983 gdb_assert (this_frame != NULL); 1984 1985 /* Note that, due to the manner in which the sentinel frame is 1986 constructed, this_frame->next still works even when this_frame 1987 is the sentinel frame. But we disallow it here anyway because 1988 calling get_next_frame_sentinel_okay() on the sentinel frame 1989 is likely a coding error. */ 1990 gdb_assert (this_frame != sentinel_frame); 1991 1992 return frame_info_ptr (this_frame->next); 1993 } 1994 1995 /* Observer for the target_changed event. */ 1996 1997 static void 1998 frame_observer_target_changed (struct target_ops *target) 1999 { 2000 reinit_frame_cache (); 2001 } 2002 2003 /* Flush the entire frame cache. */ 2004 2005 void 2006 reinit_frame_cache (void) 2007 { 2008 ++frame_cache_generation; 2009 2010 /* Tear down all frame caches. */ 2011 for (frame_info *fi = sentinel_frame; fi != NULL; fi = fi->prev) 2012 { 2013 if (fi->prologue_cache && fi->unwind->dealloc_cache) 2014 fi->unwind->dealloc_cache (fi, fi->prologue_cache); 2015 if (fi->base_cache && fi->base->unwind->dealloc_cache) 2016 fi->base->unwind->dealloc_cache (fi, fi->base_cache); 2017 } 2018 2019 /* Since we can't really be sure what the first object allocated was. */ 2020 obstack_free (&frame_cache_obstack, 0); 2021 obstack_init (&frame_cache_obstack); 2022 2023 if (sentinel_frame != NULL) 2024 annotate_frames_invalid (); 2025 2026 sentinel_frame = NULL; /* Invalidate cache */ 2027 invalidate_selected_frame (); 2028 frame_stash_invalidate (); 2029 2030 for (frame_info_ptr &iter : frame_info_ptr::frame_list) 2031 iter.invalidate (); 2032 2033 frame_debug_printf ("generation=%d", frame_cache_generation); 2034 } 2035 2036 /* Find where a register is saved (in memory or another register). 2037 The result of frame_register_unwind is just where it is saved 2038 relative to this particular frame. */ 2039 2040 static void 2041 frame_register_unwind_location (frame_info_ptr this_frame, int regnum, 2042 int *optimizedp, enum lval_type *lvalp, 2043 CORE_ADDR *addrp, int *realnump) 2044 { 2045 gdb_assert (this_frame == NULL || this_frame->level >= 0); 2046 2047 while (this_frame != NULL) 2048 { 2049 int unavailable; 2050 2051 frame_register_unwind (this_frame, regnum, optimizedp, &unavailable, 2052 lvalp, addrp, realnump, NULL); 2053 2054 if (*optimizedp) 2055 break; 2056 2057 if (*lvalp != lval_register) 2058 break; 2059 2060 regnum = *realnump; 2061 this_frame = get_next_frame (this_frame); 2062 } 2063 } 2064 2065 /* Get the previous raw frame, and check that it is not identical to 2066 same other frame frame already in the chain. If it is, there is 2067 most likely a stack cycle, so we discard it, and mark THIS_FRAME as 2068 outermost, with UNWIND_SAME_ID stop reason. Unlike the other 2069 validity tests, that compare THIS_FRAME and the next frame, we do 2070 this right after creating the previous frame, to avoid ever ending 2071 up with two frames with the same id in the frame chain. 2072 2073 There is however, one case where this cycle detection is not desirable, 2074 when asking for the previous frame of an inline frame, in this case, if 2075 the previous frame is a duplicate and we return nullptr then we will be 2076 unable to calculate the frame_id of the inline frame, this in turn 2077 causes inline_frame_this_id() to fail. So for inline frames (and only 2078 for inline frames), the previous frame will always be returned, even when it 2079 has a duplicate frame_id. We're not worried about cycles in the frame 2080 chain as, if the previous frame returned here has a duplicate frame_id, 2081 then the frame_id of the inline frame, calculated based off the frame_id 2082 of the previous frame, should also be a duplicate. */ 2083 2084 static frame_info_ptr 2085 get_prev_frame_maybe_check_cycle (frame_info_ptr this_frame) 2086 { 2087 frame_info_ptr prev_frame = get_prev_frame_raw (this_frame); 2088 2089 /* Don't compute the frame id of the current frame yet. Unwinding 2090 the sentinel frame can fail (e.g., if the thread is gone and we 2091 can't thus read its registers). If we let the cycle detection 2092 code below try to compute a frame ID, then an error thrown from 2093 within the frame ID computation would result in the sentinel 2094 frame as outermost frame, which is bogus. Instead, we'll compute 2095 the current frame's ID lazily in get_frame_id. Note that there's 2096 no point in doing cycle detection when there's only one frame, so 2097 nothing is lost here. */ 2098 if (prev_frame->level == 0) 2099 return prev_frame; 2100 2101 unsigned int entry_generation = get_frame_cache_generation (); 2102 2103 try 2104 { 2105 compute_frame_id (prev_frame); 2106 2107 bool cycle_detection_p = get_frame_type (this_frame) != INLINE_FRAME; 2108 2109 /* This assert checks GDB's state with respect to calculating the 2110 frame-id of THIS_FRAME, in the case where THIS_FRAME is an inline 2111 frame. 2112 2113 If THIS_FRAME is frame #0, and is an inline frame, then we put off 2114 calculating the frame_id until we specifically make a call to 2115 get_frame_id(). As a result we can enter this function in two 2116 possible states. If GDB asked for the previous frame of frame #0 2117 then THIS_FRAME will be frame #0 (an inline frame), and the 2118 frame_id will be in the NOT_COMPUTED state. However, if GDB asked 2119 for the frame_id of frame #0, then, as getting the frame_id of an 2120 inline frame requires us to get the frame_id of the previous 2121 frame, we will still end up in here, and the frame_id status will 2122 be COMPUTING. 2123 2124 If, instead, THIS_FRAME is at a level greater than #0 then things 2125 are simpler. For these frames we immediately compute the frame_id 2126 when the frame is initially created, and so, for those frames, we 2127 will always enter this function with the frame_id status of 2128 COMPUTING. */ 2129 gdb_assert (cycle_detection_p 2130 || (this_frame->level > 0 2131 && (this_frame->this_id.p 2132 == frame_id_status::COMPUTING)) 2133 || (this_frame->level == 0 2134 && (this_frame->this_id.p 2135 != frame_id_status::COMPUTED))); 2136 2137 /* We must do the CYCLE_DETECTION_P check after attempting to add 2138 PREV_FRAME into the cache; if PREV_FRAME is unique then we do want 2139 it in the cache, but if it is a duplicate and CYCLE_DETECTION_P is 2140 false, then we don't want to unlink it. */ 2141 if (!frame_stash_add (prev_frame.get ()) && cycle_detection_p) 2142 { 2143 /* Another frame with the same id was already in the stash. We just 2144 detected a cycle. */ 2145 frame_debug_printf (" -> nullptr // this frame has same ID"); 2146 2147 this_frame->stop_reason = UNWIND_SAME_ID; 2148 /* Unlink. */ 2149 prev_frame->next = NULL; 2150 this_frame->prev = NULL; 2151 prev_frame = NULL; 2152 } 2153 } 2154 catch (const gdb_exception &ex) 2155 { 2156 if (get_frame_cache_generation () == entry_generation) 2157 { 2158 prev_frame->next = NULL; 2159 this_frame->prev = NULL; 2160 } 2161 2162 throw; 2163 } 2164 2165 return prev_frame; 2166 } 2167 2168 /* Helper function for get_prev_frame_always, this is called inside a 2169 TRY_CATCH block. Return the frame that called THIS_FRAME or NULL if 2170 there is no such frame. This may throw an exception. */ 2171 2172 static frame_info_ptr 2173 get_prev_frame_always_1 (frame_info_ptr this_frame) 2174 { 2175 FRAME_SCOPED_DEBUG_ENTER_EXIT; 2176 2177 gdb_assert (this_frame != NULL); 2178 2179 if (frame_debug) 2180 { 2181 if (this_frame != NULL) 2182 frame_debug_printf ("this_frame=%d", this_frame->level); 2183 else 2184 frame_debug_printf ("this_frame=nullptr"); 2185 } 2186 2187 struct gdbarch *gdbarch = get_frame_arch (this_frame); 2188 2189 /* Only try to do the unwind once. */ 2190 if (this_frame->prev_p) 2191 { 2192 if (this_frame->prev != nullptr) 2193 frame_debug_printf (" -> %s // cached", 2194 this_frame->prev->to_string ().c_str ()); 2195 else 2196 frame_debug_printf 2197 (" -> nullptr // %s // cached", 2198 frame_stop_reason_symbol_string (this_frame->stop_reason)); 2199 return frame_info_ptr (this_frame->prev); 2200 } 2201 2202 /* If the frame unwinder hasn't been selected yet, we must do so 2203 before setting prev_p; otherwise the check for misbehaved 2204 sniffers will think that this frame's sniffer tried to unwind 2205 further (see frame_cleanup_after_sniffer). */ 2206 if (this_frame->unwind == NULL) 2207 frame_unwind_find_by_frame (this_frame, &this_frame->prologue_cache); 2208 2209 this_frame->prev_p = true; 2210 this_frame->stop_reason = UNWIND_NO_REASON; 2211 2212 /* If we are unwinding from an inline frame, all of the below tests 2213 were already performed when we unwound from the next non-inline 2214 frame. We must skip them, since we can not get THIS_FRAME's ID 2215 until we have unwound all the way down to the previous non-inline 2216 frame. */ 2217 if (get_frame_type (this_frame) == INLINE_FRAME) 2218 return get_prev_frame_maybe_check_cycle (this_frame); 2219 2220 /* If this_frame is the current frame, then compute and stash its 2221 frame id prior to fetching and computing the frame id of the 2222 previous frame. Otherwise, the cycle detection code in 2223 get_prev_frame_if_no_cycle() will not work correctly. When 2224 get_frame_id() is called later on, an assertion error will be 2225 triggered in the event of a cycle between the current frame and 2226 its previous frame. 2227 2228 Note we do this after the INLINE_FRAME check above. That is 2229 because the inline frame's frame id computation needs to fetch 2230 the frame id of its previous real stack frame. I.e., we need to 2231 avoid recursion in that case. This is OK since we're sure the 2232 inline frame won't create a cycle with the real stack frame. See 2233 inline_frame_this_id. */ 2234 if (this_frame->level == 0) 2235 get_frame_id (this_frame); 2236 2237 /* Check that this frame is unwindable. If it isn't, don't try to 2238 unwind to the prev frame. */ 2239 this_frame->stop_reason 2240 = this_frame->unwind->stop_reason (this_frame, 2241 &this_frame->prologue_cache); 2242 2243 if (this_frame->stop_reason != UNWIND_NO_REASON) 2244 { 2245 frame_debug_printf 2246 (" -> nullptr // %s", 2247 frame_stop_reason_symbol_string (this_frame->stop_reason)); 2248 return NULL; 2249 } 2250 2251 /* Check that this frame's ID isn't inner to (younger, below, next) 2252 the next frame. This happens when a frame unwind goes backwards. 2253 This check is valid only if this frame and the next frame are NORMAL. 2254 See the comment at frame_id_inner for details. */ 2255 if (get_frame_type (this_frame) == NORMAL_FRAME 2256 && this_frame->next->unwind->type == NORMAL_FRAME 2257 && frame_id_inner (get_frame_arch (frame_info_ptr (this_frame->next)), 2258 get_frame_id (this_frame), 2259 get_frame_id (frame_info_ptr (this_frame->next)))) 2260 { 2261 CORE_ADDR this_pc_in_block; 2262 struct minimal_symbol *morestack_msym; 2263 const char *morestack_name = NULL; 2264 2265 /* gcc -fsplit-stack __morestack can continue the stack anywhere. */ 2266 this_pc_in_block = get_frame_address_in_block (this_frame); 2267 morestack_msym = lookup_minimal_symbol_by_pc (this_pc_in_block).minsym; 2268 if (morestack_msym) 2269 morestack_name = morestack_msym->linkage_name (); 2270 if (!morestack_name || strcmp (morestack_name, "__morestack") != 0) 2271 { 2272 frame_debug_printf (" -> nullptr // this frame ID is inner"); 2273 this_frame->stop_reason = UNWIND_INNER_ID; 2274 return NULL; 2275 } 2276 } 2277 2278 /* Check that this and the next frame do not unwind the PC register 2279 to the same memory location. If they do, then even though they 2280 have different frame IDs, the new frame will be bogus; two 2281 functions can't share a register save slot for the PC. This can 2282 happen when the prologue analyzer finds a stack adjustment, but 2283 no PC save. 2284 2285 This check does assume that the "PC register" is roughly a 2286 traditional PC, even if the gdbarch_unwind_pc method adjusts 2287 it (we do not rely on the value, only on the unwound PC being 2288 dependent on this value). A potential improvement would be 2289 to have the frame prev_pc method and the gdbarch unwind_pc 2290 method set the same lval and location information as 2291 frame_register_unwind. */ 2292 if (this_frame->level > 0 2293 && gdbarch_pc_regnum (gdbarch) >= 0 2294 && get_frame_type (this_frame) == NORMAL_FRAME 2295 && (get_frame_type (frame_info_ptr (this_frame->next)) == NORMAL_FRAME 2296 || get_frame_type (frame_info_ptr (this_frame->next)) == INLINE_FRAME)) 2297 { 2298 int optimized, realnum, nrealnum; 2299 enum lval_type lval, nlval; 2300 CORE_ADDR addr, naddr; 2301 2302 frame_register_unwind_location (this_frame, 2303 gdbarch_pc_regnum (gdbarch), 2304 &optimized, &lval, &addr, &realnum); 2305 frame_register_unwind_location (get_next_frame (this_frame), 2306 gdbarch_pc_regnum (gdbarch), 2307 &optimized, &nlval, &naddr, &nrealnum); 2308 2309 if ((lval == lval_memory && lval == nlval && addr == naddr) 2310 || (lval == lval_register && lval == nlval && realnum == nrealnum)) 2311 { 2312 frame_debug_printf (" -> nullptr // no saved PC"); 2313 this_frame->stop_reason = UNWIND_NO_SAVED_PC; 2314 this_frame->prev = NULL; 2315 return NULL; 2316 } 2317 } 2318 2319 return get_prev_frame_maybe_check_cycle (this_frame); 2320 } 2321 2322 /* Return a "struct frame_info" corresponding to the frame that called 2323 THIS_FRAME. Returns NULL if there is no such frame. 2324 2325 Unlike get_prev_frame, this function always tries to unwind the 2326 frame. */ 2327 2328 frame_info_ptr 2329 get_prev_frame_always (frame_info_ptr this_frame) 2330 { 2331 frame_info_ptr prev_frame = NULL; 2332 2333 try 2334 { 2335 prev_frame = get_prev_frame_always_1 (this_frame); 2336 } 2337 catch (const gdb_exception_error &ex) 2338 { 2339 if (ex.error == MEMORY_ERROR) 2340 { 2341 this_frame->stop_reason = UNWIND_MEMORY_ERROR; 2342 if (ex.message != NULL) 2343 { 2344 char *stop_string; 2345 size_t size; 2346 2347 /* The error needs to live as long as the frame does. 2348 Allocate using stack local STOP_STRING then assign the 2349 pointer to the frame, this allows the STOP_STRING on the 2350 frame to be of type 'const char *'. */ 2351 size = ex.message->size () + 1; 2352 stop_string = (char *) frame_obstack_zalloc (size); 2353 memcpy (stop_string, ex.what (), size); 2354 this_frame->stop_string = stop_string; 2355 } 2356 prev_frame = NULL; 2357 } 2358 else 2359 throw; 2360 } 2361 2362 return prev_frame; 2363 } 2364 2365 /* Construct a new "struct frame_info" and link it previous to 2366 this_frame. */ 2367 2368 static frame_info_ptr 2369 get_prev_frame_raw (frame_info_ptr this_frame) 2370 { 2371 frame_info *prev_frame; 2372 2373 /* Allocate the new frame but do not wire it in to the frame chain. 2374 Some (bad) code in INIT_FRAME_EXTRA_INFO tries to look along 2375 frame->next to pull some fancy tricks (of course such code is, by 2376 definition, recursive). Try to prevent it. 2377 2378 There is no reason to worry about memory leaks, should the 2379 remainder of the function fail. The allocated memory will be 2380 quickly reclaimed when the frame cache is flushed, and the `we've 2381 been here before' check above will stop repeated memory 2382 allocation calls. */ 2383 prev_frame = FRAME_OBSTACK_ZALLOC (struct frame_info); 2384 prev_frame->level = this_frame->level + 1; 2385 2386 /* For now, assume we don't have frame chains crossing address 2387 spaces. */ 2388 prev_frame->pspace = this_frame->pspace; 2389 prev_frame->aspace = this_frame->aspace; 2390 2391 /* Don't yet compute ->unwind (and hence ->type). It is computed 2392 on-demand in get_frame_type, frame_register_unwind, and 2393 get_frame_id. */ 2394 2395 /* Don't yet compute the frame's ID. It is computed on-demand by 2396 get_frame_id(). */ 2397 2398 /* The unwound frame ID is validate at the start of this function, 2399 as part of the logic to decide if that frame should be further 2400 unwound, and not here while the prev frame is being created. 2401 Doing this makes it possible for the user to examine a frame that 2402 has an invalid frame ID. 2403 2404 Some very old VAX code noted: [...] For the sake of argument, 2405 suppose that the stack is somewhat trashed (which is one reason 2406 that "info frame" exists). So, return 0 (indicating we don't 2407 know the address of the arglist) if we don't know what frame this 2408 frame calls. */ 2409 2410 /* Link it in. */ 2411 this_frame->prev = prev_frame; 2412 prev_frame->next = this_frame.get (); 2413 2414 frame_debug_printf (" -> %s", prev_frame->to_string ().c_str ()); 2415 2416 return frame_info_ptr (prev_frame); 2417 } 2418 2419 /* Debug routine to print a NULL frame being returned. */ 2420 2421 static void 2422 frame_debug_got_null_frame (frame_info_ptr this_frame, 2423 const char *reason) 2424 { 2425 if (frame_debug) 2426 { 2427 if (this_frame != NULL) 2428 frame_debug_printf ("this_frame=%d -> %s", this_frame->level, reason); 2429 else 2430 frame_debug_printf ("this_frame=nullptr -> %s", reason); 2431 } 2432 } 2433 2434 /* Is this (non-sentinel) frame in the "main"() function? */ 2435 2436 static bool 2437 inside_main_func (frame_info_ptr this_frame) 2438 { 2439 if (current_program_space->symfile_object_file == nullptr) 2440 return false; 2441 2442 CORE_ADDR sym_addr; 2443 const char *name = main_name (); 2444 bound_minimal_symbol msymbol 2445 = lookup_minimal_symbol (name, NULL, 2446 current_program_space->symfile_object_file); 2447 if (msymbol.minsym == nullptr) 2448 { 2449 /* In some language (for example Fortran) there will be no minimal 2450 symbol with the name of the main function. In this case we should 2451 search the full symbols to see if we can find a match. */ 2452 struct block_symbol bs = lookup_symbol (name, NULL, VAR_DOMAIN, 0); 2453 if (bs.symbol == nullptr) 2454 return false; 2455 2456 /* We might have found some unrelated symbol. For example, the 2457 Rust compiler can emit both a subprogram and a namespace with 2458 the same name in the same scope; and due to how gdb's symbol 2459 tables currently work, we can't request the one we'd 2460 prefer. */ 2461 if (bs.symbol->aclass () != LOC_BLOCK) 2462 return false; 2463 2464 const struct block *block = bs.symbol->value_block (); 2465 gdb_assert (block != nullptr); 2466 sym_addr = block->start (); 2467 } 2468 else 2469 sym_addr = msymbol.value_address (); 2470 2471 /* Convert any function descriptor addresses into the actual function 2472 code address. */ 2473 sym_addr = gdbarch_convert_from_func_ptr_addr 2474 (get_frame_arch (this_frame), sym_addr, current_inferior ()->top_target ()); 2475 2476 return sym_addr == get_frame_func (this_frame); 2477 } 2478 2479 /* Test whether THIS_FRAME is inside the process entry point function. */ 2480 2481 static bool 2482 inside_entry_func (frame_info_ptr this_frame) 2483 { 2484 CORE_ADDR entry_point; 2485 2486 if (!entry_point_address_query (&entry_point)) 2487 return false; 2488 2489 return get_frame_func (this_frame) == entry_point; 2490 } 2491 2492 /* Return a structure containing various interesting information about 2493 the frame that called THIS_FRAME. Returns NULL if there is entier 2494 no such frame or the frame fails any of a set of target-independent 2495 condition that should terminate the frame chain (e.g., as unwinding 2496 past main()). 2497 2498 This function should not contain target-dependent tests, such as 2499 checking whether the program-counter is zero. */ 2500 2501 frame_info_ptr 2502 get_prev_frame (frame_info_ptr this_frame) 2503 { 2504 FRAME_SCOPED_DEBUG_ENTER_EXIT; 2505 2506 CORE_ADDR frame_pc; 2507 int frame_pc_p; 2508 2509 /* There is always a frame. If this assertion fails, suspect that 2510 something should be calling get_selected_frame() or 2511 get_current_frame(). */ 2512 gdb_assert (this_frame != NULL); 2513 2514 frame_pc_p = get_frame_pc_if_available (this_frame, &frame_pc); 2515 2516 /* tausq/2004-12-07: Dummy frames are skipped because it doesn't make much 2517 sense to stop unwinding at a dummy frame. One place where a dummy 2518 frame may have an address "inside_main_func" is on HPUX. On HPUX, the 2519 pcsqh register (space register for the instruction at the head of the 2520 instruction queue) cannot be written directly; the only way to set it 2521 is to branch to code that is in the target space. In order to implement 2522 frame dummies on HPUX, the called function is made to jump back to where 2523 the inferior was when the user function was called. If gdb was inside 2524 the main function when we created the dummy frame, the dummy frame will 2525 point inside the main function. */ 2526 if (this_frame->level >= 0 2527 && get_frame_type (this_frame) == NORMAL_FRAME 2528 && !user_set_backtrace_options.backtrace_past_main 2529 && frame_pc_p 2530 && inside_main_func (this_frame)) 2531 /* Don't unwind past main(). Note, this is done _before_ the 2532 frame has been marked as previously unwound. That way if the 2533 user later decides to enable unwinds past main(), that will 2534 automatically happen. */ 2535 { 2536 frame_debug_got_null_frame (this_frame, "inside main func"); 2537 return NULL; 2538 } 2539 2540 /* If the user's backtrace limit has been exceeded, stop. We must 2541 add two to the current level; one of those accounts for backtrace_limit 2542 being 1-based and the level being 0-based, and the other accounts for 2543 the level of the new frame instead of the level of the current 2544 frame. */ 2545 if (this_frame->level + 2 > user_set_backtrace_options.backtrace_limit) 2546 { 2547 frame_debug_got_null_frame (this_frame, "backtrace limit exceeded"); 2548 return NULL; 2549 } 2550 2551 /* If we're already inside the entry function for the main objfile, 2552 then it isn't valid. Don't apply this test to a dummy frame - 2553 dummy frame PCs typically land in the entry func. Don't apply 2554 this test to the sentinel frame. Sentinel frames should always 2555 be allowed to unwind. */ 2556 /* NOTE: cagney/2003-07-07: Fixed a bug in inside_main_func() - 2557 wasn't checking for "main" in the minimal symbols. With that 2558 fixed asm-source tests now stop in "main" instead of halting the 2559 backtrace in weird and wonderful ways somewhere inside the entry 2560 file. Suspect that tests for inside the entry file/func were 2561 added to work around that (now fixed) case. */ 2562 /* NOTE: cagney/2003-07-15: danielj (if I'm reading it right) 2563 suggested having the inside_entry_func test use the 2564 inside_main_func() msymbol trick (along with entry_point_address() 2565 I guess) to determine the address range of the start function. 2566 That should provide a far better stopper than the current 2567 heuristics. */ 2568 /* NOTE: tausq/2004-10-09: this is needed if, for example, the compiler 2569 applied tail-call optimizations to main so that a function called 2570 from main returns directly to the caller of main. Since we don't 2571 stop at main, we should at least stop at the entry point of the 2572 application. */ 2573 if (this_frame->level >= 0 2574 && get_frame_type (this_frame) == NORMAL_FRAME 2575 && !user_set_backtrace_options.backtrace_past_entry 2576 && frame_pc_p 2577 && inside_entry_func (this_frame)) 2578 { 2579 frame_debug_got_null_frame (this_frame, "inside entry func"); 2580 return NULL; 2581 } 2582 2583 /* Assume that the only way to get a zero PC is through something 2584 like a SIGSEGV or a dummy frame, and hence that NORMAL frames 2585 will never unwind a zero PC. */ 2586 if (this_frame->level > 0 2587 && (get_frame_type (this_frame) == NORMAL_FRAME 2588 || get_frame_type (this_frame) == INLINE_FRAME) 2589 && get_frame_type (get_next_frame (this_frame)) == NORMAL_FRAME 2590 && frame_pc_p && frame_pc == 0) 2591 { 2592 frame_debug_got_null_frame (this_frame, "zero PC"); 2593 return NULL; 2594 } 2595 2596 return get_prev_frame_always (this_frame); 2597 } 2598 2599 CORE_ADDR 2600 get_frame_pc (frame_info_ptr frame) 2601 { 2602 gdb_assert (frame->next != NULL); 2603 return frame_unwind_pc (frame_info_ptr (frame->next)); 2604 } 2605 2606 bool 2607 get_frame_pc_if_available (frame_info_ptr frame, CORE_ADDR *pc) 2608 { 2609 2610 gdb_assert (frame->next != NULL); 2611 2612 try 2613 { 2614 *pc = frame_unwind_pc (frame_info_ptr (frame->next)); 2615 } 2616 catch (const gdb_exception_error &ex) 2617 { 2618 if (ex.error == NOT_AVAILABLE_ERROR) 2619 return false; 2620 else 2621 throw; 2622 } 2623 2624 return true; 2625 } 2626 2627 /* Return an address that falls within THIS_FRAME's code block. */ 2628 2629 CORE_ADDR 2630 get_frame_address_in_block (frame_info_ptr this_frame) 2631 { 2632 /* A draft address. */ 2633 CORE_ADDR pc = get_frame_pc (this_frame); 2634 2635 frame_info_ptr next_frame (this_frame->next); 2636 2637 /* Calling get_frame_pc returns the resume address for THIS_FRAME. 2638 Normally the resume address is inside the body of the function 2639 associated with THIS_FRAME, but there is a special case: when 2640 calling a function which the compiler knows will never return 2641 (for instance abort), the call may be the very last instruction 2642 in the calling function. The resume address will point after the 2643 call and may be at the beginning of a different function 2644 entirely. 2645 2646 If THIS_FRAME is a signal frame or dummy frame, then we should 2647 not adjust the unwound PC. For a dummy frame, GDB pushed the 2648 resume address manually onto the stack. For a signal frame, the 2649 OS may have pushed the resume address manually and invoked the 2650 handler (e.g. GNU/Linux), or invoked the trampoline which called 2651 the signal handler - but in either case the signal handler is 2652 expected to return to the trampoline. So in both of these 2653 cases we know that the resume address is executable and 2654 related. So we only need to adjust the PC if THIS_FRAME 2655 is a normal function. 2656 2657 If the program has been interrupted while THIS_FRAME is current, 2658 then clearly the resume address is inside the associated 2659 function. There are three kinds of interruption: debugger stop 2660 (next frame will be SENTINEL_FRAME), operating system 2661 signal or exception (next frame will be SIGTRAMP_FRAME), 2662 or debugger-induced function call (next frame will be 2663 DUMMY_FRAME). So we only need to adjust the PC if 2664 NEXT_FRAME is a normal function. 2665 2666 We check the type of NEXT_FRAME first, since it is already 2667 known; frame type is determined by the unwinder, and since 2668 we have THIS_FRAME we've already selected an unwinder for 2669 NEXT_FRAME. 2670 2671 If the next frame is inlined, we need to keep going until we find 2672 the real function - for instance, if a signal handler is invoked 2673 while in an inlined function, then the code address of the 2674 "calling" normal function should not be adjusted either. */ 2675 2676 while (get_frame_type (next_frame) == INLINE_FRAME) 2677 next_frame = frame_info_ptr (next_frame->next); 2678 2679 if ((get_frame_type (next_frame) == NORMAL_FRAME 2680 || get_frame_type (next_frame) == TAILCALL_FRAME) 2681 && (get_frame_type (this_frame) == NORMAL_FRAME 2682 || get_frame_type (this_frame) == TAILCALL_FRAME 2683 || get_frame_type (this_frame) == INLINE_FRAME)) 2684 return pc - 1; 2685 2686 return pc; 2687 } 2688 2689 bool 2690 get_frame_address_in_block_if_available (frame_info_ptr this_frame, 2691 CORE_ADDR *pc) 2692 { 2693 2694 try 2695 { 2696 *pc = get_frame_address_in_block (this_frame); 2697 } 2698 catch (const gdb_exception_error &ex) 2699 { 2700 if (ex.error == NOT_AVAILABLE_ERROR) 2701 return false; 2702 throw; 2703 } 2704 2705 return true; 2706 } 2707 2708 symtab_and_line 2709 find_frame_sal (frame_info_ptr frame) 2710 { 2711 frame_info_ptr next_frame; 2712 int notcurrent; 2713 CORE_ADDR pc; 2714 2715 if (frame_inlined_callees (frame) > 0) 2716 { 2717 struct symbol *sym; 2718 2719 /* If the current frame has some inlined callees, and we have a next 2720 frame, then that frame must be an inlined frame. In this case 2721 this frame's sal is the "call site" of the next frame's inlined 2722 function, which can not be inferred from get_frame_pc. */ 2723 next_frame = get_next_frame (frame); 2724 if (next_frame) 2725 sym = get_frame_function (next_frame); 2726 else 2727 sym = inline_skipped_symbol (inferior_thread ()); 2728 2729 /* If frame is inline, it certainly has symbols. */ 2730 gdb_assert (sym); 2731 2732 symtab_and_line sal; 2733 if (sym->line () != 0) 2734 { 2735 sal.symtab = sym->symtab (); 2736 sal.line = sym->line (); 2737 } 2738 else 2739 /* If the symbol does not have a location, we don't know where 2740 the call site is. Do not pretend to. This is jarring, but 2741 we can't do much better. */ 2742 sal.pc = get_frame_pc (frame); 2743 2744 sal.pspace = get_frame_program_space (frame); 2745 return sal; 2746 } 2747 2748 /* If FRAME is not the innermost frame, that normally means that 2749 FRAME->pc points at the return instruction (which is *after* the 2750 call instruction), and we want to get the line containing the 2751 call (because the call is where the user thinks the program is). 2752 However, if the next frame is either a SIGTRAMP_FRAME or a 2753 DUMMY_FRAME, then the next frame will contain a saved interrupt 2754 PC and such a PC indicates the current (rather than next) 2755 instruction/line, consequently, for such cases, want to get the 2756 line containing fi->pc. */ 2757 if (!get_frame_pc_if_available (frame, &pc)) 2758 return {}; 2759 2760 notcurrent = (pc != get_frame_address_in_block (frame)); 2761 return find_pc_line (pc, notcurrent); 2762 } 2763 2764 /* Per "frame.h", return the ``address'' of the frame. Code should 2765 really be using get_frame_id(). */ 2766 CORE_ADDR 2767 get_frame_base (frame_info_ptr fi) 2768 { 2769 return get_frame_id (fi).stack_addr; 2770 } 2771 2772 /* High-level offsets into the frame. Used by the debug info. */ 2773 2774 CORE_ADDR 2775 get_frame_base_address (frame_info_ptr fi) 2776 { 2777 if (get_frame_type (fi) != NORMAL_FRAME) 2778 return 0; 2779 if (fi->base == NULL) 2780 fi->base = frame_base_find_by_frame (fi); 2781 /* Sneaky: If the low-level unwind and high-level base code share a 2782 common unwinder, let them share the prologue cache. */ 2783 if (fi->base->unwind == fi->unwind) 2784 return fi->base->this_base (fi, &fi->prologue_cache); 2785 return fi->base->this_base (fi, &fi->base_cache); 2786 } 2787 2788 CORE_ADDR 2789 get_frame_locals_address (frame_info_ptr fi) 2790 { 2791 if (get_frame_type (fi) != NORMAL_FRAME) 2792 return 0; 2793 /* If there isn't a frame address method, find it. */ 2794 if (fi->base == NULL) 2795 fi->base = frame_base_find_by_frame (fi); 2796 /* Sneaky: If the low-level unwind and high-level base code share a 2797 common unwinder, let them share the prologue cache. */ 2798 if (fi->base->unwind == fi->unwind) 2799 return fi->base->this_locals (fi, &fi->prologue_cache); 2800 return fi->base->this_locals (fi, &fi->base_cache); 2801 } 2802 2803 CORE_ADDR 2804 get_frame_args_address (frame_info_ptr fi) 2805 { 2806 if (get_frame_type (fi) != NORMAL_FRAME) 2807 return 0; 2808 /* If there isn't a frame address method, find it. */ 2809 if (fi->base == NULL) 2810 fi->base = frame_base_find_by_frame (fi); 2811 /* Sneaky: If the low-level unwind and high-level base code share a 2812 common unwinder, let them share the prologue cache. */ 2813 if (fi->base->unwind == fi->unwind) 2814 return fi->base->this_args (fi, &fi->prologue_cache); 2815 return fi->base->this_args (fi, &fi->base_cache); 2816 } 2817 2818 /* Return true if the frame unwinder for frame FI is UNWINDER; false 2819 otherwise. */ 2820 2821 bool 2822 frame_unwinder_is (frame_info_ptr fi, const frame_unwind *unwinder) 2823 { 2824 if (fi->unwind == nullptr) 2825 frame_unwind_find_by_frame (fi, &fi->prologue_cache); 2826 2827 return fi->unwind == unwinder; 2828 } 2829 2830 /* Level of the selected frame: 0 for innermost, 1 for its caller, ... 2831 or -1 for a NULL frame. */ 2832 2833 int 2834 frame_relative_level (frame_info_ptr fi) 2835 { 2836 if (fi == NULL) 2837 return -1; 2838 else 2839 return fi->level; 2840 } 2841 2842 enum frame_type 2843 get_frame_type (frame_info_ptr frame) 2844 { 2845 if (frame->unwind == NULL) 2846 /* Initialize the frame's unwinder because that's what 2847 provides the frame's type. */ 2848 frame_unwind_find_by_frame (frame, &frame->prologue_cache); 2849 return frame->unwind->type; 2850 } 2851 2852 struct program_space * 2853 get_frame_program_space (frame_info_ptr frame) 2854 { 2855 return frame->pspace; 2856 } 2857 2858 struct program_space * 2859 frame_unwind_program_space (frame_info_ptr this_frame) 2860 { 2861 gdb_assert (this_frame); 2862 2863 /* This is really a placeholder to keep the API consistent --- we 2864 assume for now that we don't have frame chains crossing 2865 spaces. */ 2866 return this_frame->pspace; 2867 } 2868 2869 const address_space * 2870 get_frame_address_space (frame_info_ptr frame) 2871 { 2872 return frame->aspace; 2873 } 2874 2875 /* Memory access methods. */ 2876 2877 void 2878 get_frame_memory (frame_info_ptr this_frame, CORE_ADDR addr, 2879 gdb::array_view<gdb_byte> buffer) 2880 { 2881 read_memory (addr, buffer.data (), buffer.size ()); 2882 } 2883 2884 LONGEST 2885 get_frame_memory_signed (frame_info_ptr this_frame, CORE_ADDR addr, 2886 int len) 2887 { 2888 struct gdbarch *gdbarch = get_frame_arch (this_frame); 2889 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch); 2890 2891 return read_memory_integer (addr, len, byte_order); 2892 } 2893 2894 ULONGEST 2895 get_frame_memory_unsigned (frame_info_ptr this_frame, CORE_ADDR addr, 2896 int len) 2897 { 2898 struct gdbarch *gdbarch = get_frame_arch (this_frame); 2899 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch); 2900 2901 return read_memory_unsigned_integer (addr, len, byte_order); 2902 } 2903 2904 bool 2905 safe_frame_unwind_memory (frame_info_ptr this_frame, 2906 CORE_ADDR addr, gdb::array_view<gdb_byte> buffer) 2907 { 2908 /* NOTE: target_read_memory returns zero on success! */ 2909 return target_read_memory (addr, buffer.data (), buffer.size ()) == 0; 2910 } 2911 2912 /* Architecture methods. */ 2913 2914 struct gdbarch * 2915 get_frame_arch (frame_info_ptr this_frame) 2916 { 2917 return frame_unwind_arch (frame_info_ptr (this_frame->next)); 2918 } 2919 2920 struct gdbarch * 2921 frame_unwind_arch (frame_info_ptr next_frame) 2922 { 2923 if (!next_frame->prev_arch.p) 2924 { 2925 struct gdbarch *arch; 2926 2927 if (next_frame->unwind == NULL) 2928 frame_unwind_find_by_frame (next_frame, &next_frame->prologue_cache); 2929 2930 if (next_frame->unwind->prev_arch != NULL) 2931 arch = next_frame->unwind->prev_arch (next_frame, 2932 &next_frame->prologue_cache); 2933 else 2934 arch = get_frame_arch (next_frame); 2935 2936 next_frame->prev_arch.arch = arch; 2937 next_frame->prev_arch.p = true; 2938 frame_debug_printf ("next_frame=%d -> %s", 2939 next_frame->level, 2940 gdbarch_bfd_arch_info (arch)->printable_name); 2941 } 2942 2943 return next_frame->prev_arch.arch; 2944 } 2945 2946 struct gdbarch * 2947 frame_unwind_caller_arch (frame_info_ptr next_frame) 2948 { 2949 next_frame = skip_artificial_frames (next_frame); 2950 2951 /* We must have a non-artificial frame. The caller is supposed to check 2952 the result of frame_unwind_caller_id (), which returns NULL_FRAME_ID 2953 in this case. */ 2954 gdb_assert (next_frame != NULL); 2955 2956 return frame_unwind_arch (next_frame); 2957 } 2958 2959 /* Gets the language of FRAME. */ 2960 2961 enum language 2962 get_frame_language (frame_info_ptr frame) 2963 { 2964 CORE_ADDR pc = 0; 2965 bool pc_p = false; 2966 2967 gdb_assert (frame!= NULL); 2968 2969 /* We determine the current frame language by looking up its 2970 associated symtab. To retrieve this symtab, we use the frame 2971 PC. However we cannot use the frame PC as is, because it 2972 usually points to the instruction following the "call", which 2973 is sometimes the first instruction of another function. So 2974 we rely on get_frame_address_in_block(), it provides us with 2975 a PC that is guaranteed to be inside the frame's code 2976 block. */ 2977 2978 try 2979 { 2980 pc = get_frame_address_in_block (frame); 2981 pc_p = true; 2982 } 2983 catch (const gdb_exception_error &ex) 2984 { 2985 if (ex.error != NOT_AVAILABLE_ERROR) 2986 throw; 2987 } 2988 2989 if (pc_p) 2990 { 2991 struct compunit_symtab *cust = find_pc_compunit_symtab (pc); 2992 2993 if (cust != NULL) 2994 return cust->language (); 2995 } 2996 2997 return language_unknown; 2998 } 2999 3000 /* Stack pointer methods. */ 3001 3002 CORE_ADDR 3003 get_frame_sp (frame_info_ptr this_frame) 3004 { 3005 struct gdbarch *gdbarch = get_frame_arch (this_frame); 3006 3007 /* NOTE drow/2008-06-28: gdbarch_unwind_sp could be converted to 3008 operate on THIS_FRAME now. */ 3009 return gdbarch_unwind_sp (gdbarch, frame_info_ptr (this_frame->next)); 3010 } 3011 3012 /* Return the reason why we can't unwind past FRAME. */ 3013 3014 enum unwind_stop_reason 3015 get_frame_unwind_stop_reason (frame_info_ptr frame) 3016 { 3017 /* Fill-in STOP_REASON. */ 3018 get_prev_frame_always (frame); 3019 gdb_assert (frame->prev_p); 3020 3021 return frame->stop_reason; 3022 } 3023 3024 /* Return a string explaining REASON. */ 3025 3026 const char * 3027 unwind_stop_reason_to_string (enum unwind_stop_reason reason) 3028 { 3029 switch (reason) 3030 { 3031 #define SET(name, description) \ 3032 case name: return _(description); 3033 #include "unwind_stop_reasons.def" 3034 #undef SET 3035 3036 default: 3037 internal_error ("Invalid frame stop reason"); 3038 } 3039 } 3040 3041 const char * 3042 frame_stop_reason_string (frame_info_ptr fi) 3043 { 3044 gdb_assert (fi->prev_p); 3045 gdb_assert (fi->prev == NULL); 3046 3047 /* Return the specific string if we have one. */ 3048 if (fi->stop_string != NULL) 3049 return fi->stop_string; 3050 3051 /* Return the generic string if we have nothing better. */ 3052 return unwind_stop_reason_to_string (fi->stop_reason); 3053 } 3054 3055 /* Return the enum symbol name of REASON as a string, to use in debug 3056 output. */ 3057 3058 static const char * 3059 frame_stop_reason_symbol_string (enum unwind_stop_reason reason) 3060 { 3061 switch (reason) 3062 { 3063 #define SET(name, description) \ 3064 case name: return #name; 3065 #include "unwind_stop_reasons.def" 3066 #undef SET 3067 3068 default: 3069 internal_error ("Invalid frame stop reason"); 3070 } 3071 } 3072 3073 /* Clean up after a failed (wrong unwinder) attempt to unwind past 3074 FRAME. */ 3075 3076 void 3077 frame_cleanup_after_sniffer (frame_info_ptr frame) 3078 { 3079 /* The sniffer should not allocate a prologue cache if it did not 3080 match this frame. */ 3081 gdb_assert (frame->prologue_cache == NULL); 3082 3083 /* No sniffer should extend the frame chain; sniff based on what is 3084 already certain. */ 3085 gdb_assert (!frame->prev_p); 3086 3087 /* The sniffer should not check the frame's ID; that's circular. */ 3088 gdb_assert (frame->this_id.p != frame_id_status::COMPUTED); 3089 3090 /* Clear cached fields dependent on the unwinder. 3091 3092 The previous PC is independent of the unwinder, but the previous 3093 function is not (see get_frame_address_in_block). */ 3094 frame->prev_func.status = CC_UNKNOWN; 3095 frame->prev_func.addr = 0; 3096 3097 /* Discard the unwinder last, so that we can easily find it if an assertion 3098 in this function triggers. */ 3099 frame->unwind = NULL; 3100 } 3101 3102 /* Set FRAME's unwinder temporarily, so that we can call a sniffer. 3103 If sniffing fails, the caller should be sure to call 3104 frame_cleanup_after_sniffer. */ 3105 3106 void 3107 frame_prepare_for_sniffer (frame_info_ptr frame, 3108 const struct frame_unwind *unwind) 3109 { 3110 gdb_assert (frame->unwind == NULL); 3111 frame->unwind = unwind; 3112 } 3113 3114 static struct cmd_list_element *set_backtrace_cmdlist; 3115 static struct cmd_list_element *show_backtrace_cmdlist; 3116 3117 /* Definition of the "set backtrace" settings that are exposed as 3118 "backtrace" command options. */ 3119 3120 using boolean_option_def 3121 = gdb::option::boolean_option_def<set_backtrace_options>; 3122 3123 const gdb::option::option_def set_backtrace_option_defs[] = { 3124 3125 boolean_option_def { 3126 "past-main", 3127 [] (set_backtrace_options *opt) { return &opt->backtrace_past_main; }, 3128 show_backtrace_past_main, /* show_cmd_cb */ 3129 N_("Set whether backtraces should continue past \"main\"."), 3130 N_("Show whether backtraces should continue past \"main\"."), 3131 N_("Normally the caller of \"main\" is not of interest, so GDB will terminate\n\ 3132 the backtrace at \"main\". Set this if you need to see the rest\n\ 3133 of the stack trace."), 3134 }, 3135 3136 boolean_option_def { 3137 "past-entry", 3138 [] (set_backtrace_options *opt) { return &opt->backtrace_past_entry; }, 3139 show_backtrace_past_entry, /* show_cmd_cb */ 3140 N_("Set whether backtraces should continue past the entry point of a program."), 3141 N_("Show whether backtraces should continue past the entry point of a program."), 3142 N_("Normally there are no callers beyond the entry point of a program, so GDB\n\ 3143 will terminate the backtrace there. Set this if you need to see\n\ 3144 the rest of the stack trace."), 3145 }, 3146 }; 3147 3148 /* Implement the 'maintenance print frame-id' command. */ 3149 3150 static void 3151 maintenance_print_frame_id (const char *args, int from_tty) 3152 { 3153 frame_info_ptr frame; 3154 3155 /* Use the currently selected frame, or select a frame based on the level 3156 number passed by the user. */ 3157 if (args == nullptr) 3158 frame = get_selected_frame ("No frame selected"); 3159 else 3160 { 3161 int level = value_as_long (parse_and_eval (args)); 3162 frame = find_relative_frame (get_current_frame (), &level); 3163 } 3164 3165 /* Print the frame-id. */ 3166 gdb_assert (frame != nullptr); 3167 gdb_printf ("frame-id for frame #%d: %s\n", 3168 frame_relative_level (frame), 3169 get_frame_id (frame).to_string ().c_str ()); 3170 } 3171 3172 void _initialize_frame (); 3173 void 3174 _initialize_frame () 3175 { 3176 obstack_init (&frame_cache_obstack); 3177 3178 frame_stash_create (); 3179 3180 gdb::observers::target_changed.attach (frame_observer_target_changed, 3181 "frame"); 3182 3183 add_setshow_prefix_cmd ("backtrace", class_maintenance, 3184 _("\ 3185 Set backtrace specific variables.\n\ 3186 Configure backtrace variables such as the backtrace limit"), 3187 _("\ 3188 Show backtrace specific variables.\n\ 3189 Show backtrace variables such as the backtrace limit."), 3190 &set_backtrace_cmdlist, &show_backtrace_cmdlist, 3191 &setlist, &showlist); 3192 3193 add_setshow_uinteger_cmd ("limit", class_obscure, 3194 &user_set_backtrace_options.backtrace_limit, _("\ 3195 Set an upper bound on the number of backtrace levels."), _("\ 3196 Show the upper bound on the number of backtrace levels."), _("\ 3197 No more than the specified number of frames can be displayed or examined.\n\ 3198 Literal \"unlimited\" or zero means no limit."), 3199 NULL, 3200 show_backtrace_limit, 3201 &set_backtrace_cmdlist, 3202 &show_backtrace_cmdlist); 3203 3204 gdb::option::add_setshow_cmds_for_options 3205 (class_stack, &user_set_backtrace_options, 3206 set_backtrace_option_defs, &set_backtrace_cmdlist, &show_backtrace_cmdlist); 3207 3208 /* Debug this files internals. */ 3209 add_setshow_boolean_cmd ("frame", class_maintenance, &frame_debug, _("\ 3210 Set frame debugging."), _("\ 3211 Show frame debugging."), _("\ 3212 When non-zero, frame specific internal debugging is enabled."), 3213 NULL, 3214 show_frame_debug, 3215 &setdebuglist, &showdebuglist); 3216 3217 add_cmd ("frame-id", class_maintenance, maintenance_print_frame_id, 3218 _("Print the current frame-id."), 3219 &maintenanceprintlist); 3220 } 3221