1 /* 2 * Copyright (c) 2012, 2019 SAP SE. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 * 23 */ 24 25 #include "asm/assembler.hpp" 26 #include "compiler/disassembler.hpp" 27 #include "loadlib_aix.hpp" 28 #include "memory/allocation.hpp" 29 #include "memory/allocation.inline.hpp" 30 #include "misc_aix.hpp" 31 #include "porting_aix.hpp" 32 #include "runtime/os.hpp" 33 #include "runtime/thread.hpp" 34 #include "utilities/align.hpp" 35 #include "utilities/debug.hpp" 36 37 // distinguish old xlc and xlclang++, where 38 // <ibmdemangle.h> is suggested but not found 39 #if defined(__clang__) 40 #define DISABLE_DEMANGLE 41 // #include <ibmdemangle.h> 42 #else 43 #include <demangle.h> 44 #endif 45 46 #include <sys/debug.h> 47 #include <pthread.h> 48 #include <ucontext.h> 49 50 ////////////////////////////////// 51 // Provide implementation for dladdr based on LoadedLibraries pool and 52 // traceback table scan 53 54 // Search traceback table in stack, 55 // return procedure name from trace back table. 56 #define MAX_FUNC_SEARCH_LEN 0x10000 57 58 #define PTRDIFF_BYTES(p1,p2) (((ptrdiff_t)p1) - ((ptrdiff_t)p2)) 59 60 // Typedefs for stackslots, stack pointers, pointers to op codes. 61 typedef unsigned long stackslot_t; 62 typedef stackslot_t* stackptr_t; 63 typedef unsigned int* codeptr_t; 64 65 // Unfortunately, the interface of dladdr makes the implementator 66 // responsible for maintaining memory for function name/library 67 // name. I guess this is because most OS's keep those values as part 68 // of the mapped executable image ready to use. On AIX, this doesn't 69 // work, so I have to keep the returned strings. For now, I do this in 70 // a primitive string map. Should this turn out to be a performance 71 // problem, a better hashmap has to be used. 72 class fixed_strings { 73 struct node : public CHeapObj<mtInternal> { 74 char* v; 75 node* next; 76 }; 77 78 node* first; 79 80 public: 81 82 fixed_strings() : first(0) {} 83 ~fixed_strings() { 84 node* n = first; 85 while (n) { 86 node* p = n; 87 n = n->next; 88 os::free(p->v); 89 delete p; 90 } 91 } 92 93 char* intern(const char* s) { 94 for (node* n = first; n; n = n->next) { 95 if (strcmp(n->v, s) == 0) { 96 return n->v; 97 } 98 } 99 node* p = new node; 100 p->v = os::strdup_check_oom(s); 101 p->next = first; 102 first = p; 103 return p->v; 104 } 105 }; 106 107 static fixed_strings dladdr_fixed_strings; 108 109 bool AixSymbols::get_function_name ( 110 address pc0, // [in] program counter 111 char* p_name, size_t namelen, // [out] optional: function name ("" if not available) 112 int* p_displacement, // [out] optional: displacement (-1 if not available) 113 const struct tbtable** p_tb, // [out] optional: ptr to traceback table to get further 114 // information (NULL if not available) 115 bool demangle // [in] whether to demangle the name 116 ) { 117 struct tbtable* tb = 0; 118 unsigned int searchcount = 0; 119 120 // initialize output parameters 121 if (p_name && namelen > 0) { 122 *p_name = '\0'; 123 } 124 if (p_displacement) { 125 *p_displacement = -1; 126 } 127 if (p_tb) { 128 *p_tb = NULL; 129 } 130 131 codeptr_t pc = (codeptr_t)pc0; 132 133 // weed out obvious bogus states 134 if (pc < (codeptr_t)0x1000) { 135 trcVerbose("invalid program counter"); 136 return false; 137 } 138 139 // We see random but frequent crashes in this function since some months mainly on shutdown 140 // (-XX:+DumpInfoAtExit). It appears the page we are reading is randomly disappearing while 141 // we read it (?). 142 // As the pc cannot be trusted to be anything sensible lets make all reads via SafeFetch. Also 143 // bail if this is not a text address right now. 144 if (!LoadedLibraries::find_for_text_address(pc, NULL)) { 145 trcVerbose("not a text address"); 146 return false; 147 } 148 149 // .. (Note that is_readable_pointer returns true if safefetch stubs are not there yet; 150 // in that case I try reading the traceback table unsafe - I rather risk secondary crashes in 151 // error files than not having a callstack.) 152 #define CHECK_POINTER_READABLE(p) \ 153 if (!os::is_readable_pointer(p)) { \ 154 trcVerbose("pc not readable"); \ 155 return false; \ 156 } 157 158 codeptr_t pc2 = (codeptr_t) pc; 159 160 // Make sure the pointer is word aligned. 161 pc2 = (codeptr_t) align_up((char*)pc2, 4); 162 CHECK_POINTER_READABLE(pc2) 163 164 // Find start of traceback table. 165 // (starts after code, is marked by word-aligned (32bit) zeros) 166 while ((*pc2 != NULL) && (searchcount++ < MAX_FUNC_SEARCH_LEN)) { 167 CHECK_POINTER_READABLE(pc2) 168 pc2++; 169 } 170 if (*pc2 != 0) { 171 trcVerbose("no traceback table found"); 172 return false; 173 } 174 // 175 // Set up addressability to the traceback table 176 // 177 tb = (struct tbtable*) (pc2 + 1); 178 179 // Is this really a traceback table? No way to be sure but 180 // some indicators we can check. 181 if (tb->tb.lang >= 0xf && tb->tb.lang <= 0xfb) { 182 // Language specifiers, go from 0 (C) to 14 (Objective C). 183 // According to spec, 0xf-0xfa reserved, 0xfb-0xff reserved for ibm. 184 trcVerbose("no traceback table found"); 185 return false; 186 } 187 188 // Existence of fields in the tbtable extension are contingent upon 189 // specific fields in the base table. Check for their existence so 190 // that we can address the function name if it exists. 191 pc2 = (codeptr_t) tb + 192 sizeof(struct tbtable_short)/sizeof(int); 193 if (tb->tb.fixedparms != 0 || tb->tb.floatparms != 0) 194 pc2++; 195 196 CHECK_POINTER_READABLE(pc2) 197 198 if (tb->tb.has_tboff == TRUE) { 199 200 // I want to know the displacement 201 const unsigned int tb_offset = *pc2; 202 codeptr_t start_of_procedure = 203 (codeptr_t)(((char*)tb) - 4 - tb_offset); // (-4 to omit leading 0000) 204 205 // Weed out the cases where we did find the wrong traceback table. 206 if (pc < start_of_procedure) { 207 trcVerbose("no traceback table found"); 208 return false; 209 } 210 211 // return the displacement 212 if (p_displacement) { 213 (*p_displacement) = (int) PTRDIFF_BYTES(pc, start_of_procedure); 214 } 215 216 pc2++; 217 } else { 218 // return -1 for displacement 219 if (p_displacement) { 220 (*p_displacement) = -1; 221 } 222 } 223 224 if (tb->tb.int_hndl == TRUE) 225 pc2++; 226 227 if (tb->tb.has_ctl == TRUE) 228 pc2 += (*pc2) + 1; // don't care 229 230 CHECK_POINTER_READABLE(pc2) 231 232 // 233 // return function name if it exists. 234 // 235 if (p_name && namelen > 0) { 236 if (tb->tb.name_present) { 237 // Copy name from text because it may not be zero terminated. 238 const short l = MIN2<short>(*((short*)pc2), namelen - 1); 239 // Be very careful. 240 int i = 0; char* const p = (char*)pc2 + sizeof(short); 241 while (i < l && os::is_readable_pointer(p + i)) { 242 p_name[i] = p[i]; 243 i++; 244 } 245 p_name[i] = '\0'; 246 247 // If it is a C++ name, try and demangle it using the Demangle interface (see demangle.h). 248 #ifndef DISABLE_DEMANGLE 249 if (demangle) { 250 char* rest; 251 Name* const name = Demangle(p_name, rest); 252 if (name) { 253 const char* const demangled_name = name->Text(); 254 if (demangled_name) { 255 strncpy(p_name, demangled_name, namelen-1); 256 p_name[namelen-1] = '\0'; 257 } 258 delete name; 259 } 260 } 261 #endif 262 } else { 263 strncpy(p_name, "<nameless function>", namelen-1); 264 p_name[namelen-1] = '\0'; 265 } 266 } 267 268 // Return traceback table, if user wants it. 269 if (p_tb) { 270 (*p_tb) = tb; 271 } 272 273 return true; 274 275 } 276 277 bool AixSymbols::get_module_name(address pc, 278 char* p_name, size_t namelen) { 279 280 if (p_name && namelen > 0) { 281 p_name[0] = '\0'; 282 loaded_module_t lm; 283 if (LoadedLibraries::find_for_text_address(pc, &lm) != NULL) { 284 strncpy(p_name, lm.shortname, namelen); 285 p_name[namelen - 1] = '\0'; 286 return true; 287 } 288 } 289 290 return false; 291 } 292 293 // Special implementation of dladdr for Aix based on LoadedLibraries 294 // Note: dladdr returns non-zero for ok, 0 for error! 295 // Note: dladdr is not posix, but a non-standard GNU extension. So this tries to 296 // fulfill the contract of dladdr on Linux (see http://linux.die.net/man/3/dladdr) 297 // Note: addr may be both an AIX function descriptor or a real code pointer 298 // to the entry of a function. 299 extern "C" 300 int dladdr(void* addr, Dl_info* info) { 301 302 if (!addr) { 303 return 0; 304 } 305 306 assert(info, ""); 307 308 int rc = 0; 309 310 const char* const ZEROSTRING = ""; 311 312 // Always return a string, even if a "" one. Linux dladdr manpage 313 // does not say anything about returning NULL 314 info->dli_fname = ZEROSTRING; 315 info->dli_sname = ZEROSTRING; 316 info->dli_saddr = NULL; 317 318 address p = (address) addr; 319 loaded_module_t lm; 320 bool found = false; 321 322 enum { noclue, code, data } type = noclue; 323 324 trcVerbose("dladdr(%p)...", p); 325 326 // Note: input address may be a function. I accept both a pointer to 327 // the entry of a function and a pointer to the function decriptor. 328 // (see ppc64 ABI) 329 found = LoadedLibraries::find_for_text_address(p, &lm); 330 if (found) { 331 type = code; 332 } 333 334 if (!found) { 335 // Not a pointer into any text segment. Is it a function descriptor? 336 const FunctionDescriptor* const pfd = (const FunctionDescriptor*) p; 337 p = pfd->entry(); 338 if (p) { 339 found = LoadedLibraries::find_for_text_address(p, &lm); 340 if (found) { 341 type = code; 342 } 343 } 344 } 345 346 if (!found) { 347 // Neither direct code pointer nor function descriptor. A data ptr? 348 p = (address)addr; 349 found = LoadedLibraries::find_for_data_address(p, &lm); 350 if (found) { 351 type = data; 352 } 353 } 354 355 // If we did find the shared library this address belongs to (either 356 // code or data segment) resolve library path and, if possible, the 357 // symbol name. 358 if (found) { 359 360 // No need to intern the libpath, that one is already interned one layer below. 361 info->dli_fname = lm.path; 362 363 if (type == code) { 364 365 // For code symbols resolve function name and displacement. Use 366 // displacement to calc start of function. 367 char funcname[256] = ""; 368 int displacement = 0; 369 370 if (AixSymbols::get_function_name(p, funcname, sizeof(funcname), 371 &displacement, NULL, true)) { 372 if (funcname[0] != '\0') { 373 const char* const interned = dladdr_fixed_strings.intern(funcname); 374 info->dli_sname = interned; 375 trcVerbose("... function name: %s ...", interned); 376 } 377 378 // From the displacement calculate the start of the function. 379 if (displacement != -1) { 380 info->dli_saddr = p - displacement; 381 } else { 382 info->dli_saddr = p; 383 } 384 } else { 385 386 // No traceback table found. Just assume the pointer is it. 387 info->dli_saddr = p; 388 389 } 390 391 } else if (type == data) { 392 393 // For data symbols. 394 info->dli_saddr = p; 395 396 } else { 397 ShouldNotReachHere(); 398 } 399 400 rc = 1; // success: return 1 [sic] 401 402 } 403 404 // sanity checks. 405 if (rc) { 406 assert(info->dli_fname, ""); 407 assert(info->dli_sname, ""); 408 assert(info->dli_saddr, ""); 409 } 410 411 return rc; // error: return 0 [sic] 412 413 } 414 415 ///////////////////////////////////////////////////////////////////////////// 416 // Native callstack dumping 417 418 // Print the traceback table for one stack frame. 419 static void print_tbtable (outputStream* st, const struct tbtable* p_tb) { 420 421 if (p_tb == NULL) { 422 st->print("<null>"); 423 return; 424 } 425 426 switch(p_tb->tb.lang) { 427 case TB_C: st->print("C"); break; 428 case TB_FORTRAN: st->print("FORTRAN"); break; 429 case TB_PASCAL: st->print("PASCAL"); break; 430 case TB_ADA: st->print("ADA"); break; 431 case TB_PL1: st->print("PL1"); break; 432 case TB_BASIC: st->print("BASIC"); break; 433 case TB_LISP: st->print("LISP"); break; 434 case TB_COBOL: st->print("COBOL"); break; 435 case TB_MODULA2: st->print("MODULA2"); break; 436 case TB_CPLUSPLUS: st->print("C++"); break; 437 case TB_RPG: st->print("RPG"); break; 438 case TB_PL8: st->print("PL8"); break; 439 case TB_ASM: st->print("ASM"); break; 440 case TB_HPJ: st->print("HPJ"); break; 441 default: st->print("unknown"); 442 } 443 st->print(" "); 444 445 if (p_tb->tb.globallink) { 446 st->print("globallink "); 447 } 448 if (p_tb->tb.is_eprol) { 449 st->print("eprol "); 450 } 451 if (p_tb->tb.int_proc) { 452 st->print("int_proc "); 453 } 454 if (p_tb->tb.tocless) { 455 st->print("tocless "); 456 } 457 if (p_tb->tb.fp_present) { 458 st->print("fp_present "); 459 } 460 if (p_tb->tb.int_hndl) { 461 st->print("interrupt_handler "); 462 } 463 if (p_tb->tb.uses_alloca) { 464 st->print("uses_alloca "); 465 } 466 if (p_tb->tb.saves_cr) { 467 st->print("saves_cr "); 468 } 469 if (p_tb->tb.saves_lr) { 470 st->print("saves_lr "); 471 } 472 if (p_tb->tb.stores_bc) { 473 st->print("stores_bc "); 474 } 475 if (p_tb->tb.fixup) { 476 st->print("fixup "); 477 } 478 if (p_tb->tb.fpr_saved > 0) { 479 st->print("fpr_saved:%d ", p_tb->tb.fpr_saved); 480 } 481 if (p_tb->tb.gpr_saved > 0) { 482 st->print("gpr_saved:%d ", p_tb->tb.gpr_saved); 483 } 484 if (p_tb->tb.fixedparms > 0) { 485 st->print("fixedparms:%d ", p_tb->tb.fixedparms); 486 } 487 if (p_tb->tb.floatparms > 0) { 488 st->print("floatparms:%d ", p_tb->tb.floatparms); 489 } 490 if (p_tb->tb.parmsonstk > 0) { 491 st->print("parmsonstk:%d", p_tb->tb.parmsonstk); 492 } 493 } 494 495 // Print information for pc (module, function, displacement, traceback table) 496 // on one line. 497 static void print_info_for_pc (outputStream* st, codeptr_t pc, char* buf, 498 size_t buf_size, bool demangle) { 499 const struct tbtable* tb = NULL; 500 int displacement = -1; 501 502 if (!os::is_readable_pointer(pc)) { 503 st->print("(invalid)"); 504 return; 505 } 506 507 if (AixSymbols::get_module_name((address)pc, buf, buf_size)) { 508 st->print("%s", buf); 509 } else { 510 st->print("(unknown module)"); 511 } 512 st->print("::"); 513 if (AixSymbols::get_function_name((address)pc, buf, buf_size, 514 &displacement, &tb, demangle)) { 515 st->print("%s", buf); 516 } else { 517 st->print("(unknown function)"); 518 } 519 if (displacement == -1) { 520 st->print("+?"); 521 } else { 522 st->print("+0x%x", displacement); 523 } 524 if (tb) { 525 st->fill_to(64); 526 st->print(" ("); 527 print_tbtable(st, tb); 528 st->print(")"); 529 } 530 } 531 532 static void print_stackframe(outputStream* st, stackptr_t sp, char* buf, 533 size_t buf_size, bool demangle) { 534 535 stackptr_t sp2 = sp; 536 537 // skip backchain 538 539 sp2++; 540 541 // skip crsave 542 543 sp2++; 544 545 // retrieve lrsave. That is the only info I need to get the function/displacement 546 547 codeptr_t lrsave = (codeptr_t) *(sp2); 548 st->print (PTR64_FORMAT " - " PTR64_FORMAT " ", sp2, lrsave); 549 550 if (lrsave != NULL) { 551 print_info_for_pc(st, lrsave, buf, buf_size, demangle); 552 } 553 554 } 555 556 // Function to check a given stack pointer against given stack limits. 557 static bool is_valid_stackpointer(stackptr_t sp, stackptr_t stack_base, size_t stack_size) { 558 if (((uintptr_t)sp) & 0x7) { 559 return false; 560 } 561 if (sp > stack_base) { 562 return false; 563 } 564 if (sp < (stackptr_t) ((address)stack_base - stack_size)) { 565 return false; 566 } 567 return true; 568 } 569 570 // Returns true if function is a valid codepointer. 571 static bool is_valid_codepointer(codeptr_t p) { 572 if (!p) { 573 return false; 574 } 575 if (((uintptr_t)p) & 0x3) { 576 return false; 577 } 578 if (LoadedLibraries::find_for_text_address(p, NULL) == NULL) { 579 return false; 580 } 581 return true; 582 } 583 584 // Function tries to guess if the given combination of stack pointer, stack base 585 // and stack size is a valid stack frame. 586 static bool is_valid_frame (stackptr_t p, stackptr_t stack_base, size_t stack_size) { 587 588 if (!is_valid_stackpointer(p, stack_base, stack_size)) { 589 return false; 590 } 591 592 // First check - the occurrence of a valid backchain pointer up the stack, followed by a 593 // valid codeptr, counts as a good candidate. 594 stackptr_t sp2 = (stackptr_t) *p; 595 if (is_valid_stackpointer(sp2, stack_base, stack_size) && // found a valid stack pointer in the stack... 596 ((sp2 - p) > 6) && // ... pointing upwards and not into my frame... 597 is_valid_codepointer((codeptr_t)(*(sp2 + 2)))) // ... followed by a code pointer after two slots... 598 { 599 return true; 600 } 601 602 return false; 603 } 604 605 // Try to relocate a stack back chain in a given stack. 606 // Used in callstack dumping, when the backchain is broken by an overwriter 607 static stackptr_t try_find_backchain (stackptr_t last_known_good_frame, 608 stackptr_t stack_base, size_t stack_size) 609 { 610 if (!is_valid_stackpointer(last_known_good_frame, stack_base, stack_size)) { 611 return NULL; 612 } 613 614 stackptr_t sp = last_known_good_frame; 615 616 sp += 6; // Omit next fixed frame slots. 617 while (sp < stack_base) { 618 if (is_valid_frame(sp, stack_base, stack_size)) { 619 return sp; 620 } 621 sp ++; 622 } 623 624 return NULL; 625 } 626 627 static void decode_instructions_at_pc(const char* header, 628 codeptr_t pc, int num_before, 629 int num_after, outputStream* st) { 630 // TODO: PPC port Disassembler::decode(pc, 16, 16, st); 631 } 632 633 634 void AixNativeCallstack::print_callstack_for_context(outputStream* st, const ucontext_t* context, 635 bool demangle, char* buf, size_t buf_size) { 636 637 #define MAX_CALLSTACK_DEPTH 50 638 639 unsigned long* sp; 640 unsigned long* sp_last; 641 int frame; 642 643 // To print the first frame, use the current value of iar: 644 // current entry indicated by iar (the current pc) 645 codeptr_t cur_iar = 0; 646 stackptr_t cur_sp = 0; 647 codeptr_t cur_rtoc = 0; 648 codeptr_t cur_lr = 0; 649 650 const ucontext_t* uc = (const ucontext_t*) context; 651 652 // fallback: use the current context 653 ucontext_t local_context; 654 if (!uc) { 655 st->print_cr("No context given, using current context."); 656 if (getcontext(&local_context) == 0) { 657 uc = &local_context; 658 } else { 659 st->print_cr("No context given and getcontext failed. "); 660 return; 661 } 662 } 663 664 cur_iar = (codeptr_t)uc->uc_mcontext.jmp_context.iar; 665 cur_sp = (stackptr_t)uc->uc_mcontext.jmp_context.gpr[1]; 666 cur_rtoc = (codeptr_t)uc->uc_mcontext.jmp_context.gpr[2]; 667 cur_lr = (codeptr_t)uc->uc_mcontext.jmp_context.lr; 668 669 // syntax used here: 670 // n -------------- <-- stack_base, stack_to 671 // n-1 | | 672 // ... | older | 673 // ... | frames | | 674 // | | | stack grows downward 675 // ... | younger | | 676 // ... | frames | V 677 // | | 678 // |------------| <-- cur_sp, current stack ptr 679 // | | 680 // | unsused | 681 // | stack | 682 // | | 683 // . . 684 // . . 685 // . . 686 // . . 687 // | | 688 // 0 -------------- <-- stack_from 689 // 690 691 // Retrieve current stack base, size from the current thread. If there is none, 692 // retrieve it from the OS. 693 stackptr_t stack_base = NULL; 694 size_t stack_size = NULL; 695 { 696 AixMisc::stackbounds_t stackbounds; 697 if (!AixMisc::query_stack_bounds_for_current_thread(&stackbounds)) { 698 st->print_cr("Cannot retrieve stack bounds."); 699 return; 700 } 701 stack_base = (stackptr_t)stackbounds.base; 702 stack_size = stackbounds.size; 703 } 704 705 st->print_cr("------ current frame:"); 706 st->print("iar: " PTR64_FORMAT " ", p2i(cur_iar)); 707 print_info_for_pc(st, cur_iar, buf, buf_size, demangle); 708 st->cr(); 709 710 if (cur_iar && os::is_readable_pointer(cur_iar)) { 711 decode_instructions_at_pc( 712 "Decoded instructions at iar:", 713 cur_iar, 32, 16, st); 714 } 715 716 // Print out lr too, which may be interesting if we did jump to some bogus location; 717 // in those cases the new frame is not built up yet and the caller location is only 718 // preserved via lr register. 719 st->print("lr: " PTR64_FORMAT " ", p2i(cur_lr)); 720 print_info_for_pc(st, cur_lr, buf, buf_size, demangle); 721 st->cr(); 722 723 if (cur_lr && os::is_readable_pointer(cur_lr)) { 724 decode_instructions_at_pc( 725 "Decoded instructions at lr:", 726 cur_lr, 32, 16, st); 727 } 728 729 // Check and print sp. 730 st->print("sp: " PTR64_FORMAT " ", p2i(cur_sp)); 731 if (!is_valid_stackpointer(cur_sp, stack_base, stack_size)) { 732 st->print("(invalid) "); 733 goto cleanup; 734 } else { 735 st->print("(base - 0x%X) ", PTRDIFF_BYTES(stack_base, cur_sp)); 736 } 737 st->cr(); 738 739 // Check and print rtoc. 740 st->print("rtoc: " PTR64_FORMAT " ", p2i(cur_rtoc)); 741 if (cur_rtoc == NULL || cur_rtoc == (codeptr_t)-1 || 742 !os::is_readable_pointer(cur_rtoc)) { 743 st->print("(invalid)"); 744 } else if (((uintptr_t)cur_rtoc) & 0x7) { 745 st->print("(unaligned)"); 746 } 747 st->cr(); 748 749 st->print_cr("|---stackaddr----| |----lrsave------|: <function name>"); 750 751 /// 752 // Walk callstack. 753 // 754 // (if no context was given, use the current stack) 755 sp = (unsigned long*)(*(unsigned long*)cur_sp); // Stack pointer 756 sp_last = cur_sp; 757 758 frame = 0; 759 760 while (frame < MAX_CALLSTACK_DEPTH) { 761 762 // Check sp. 763 bool retry = false; 764 if (sp == NULL) { 765 // The backchain pointer was NULL. This normally means the end of the chain. But the 766 // stack might be corrupted, and it may be worth looking for the stack chain. 767 if (is_valid_stackpointer(sp_last, stack_base, stack_size) && (stack_base - 0x10) > sp_last) { 768 // If we are not within <guess> 0x10 stackslots of the stack base, we assume that this 769 // is indeed not the end of the chain but that the stack was corrupted. So lets try to 770 // find the end of the chain. 771 st->print_cr("*** back chain pointer is NULL - end of stack or broken backchain ? ***"); 772 retry = true; 773 } else { 774 st->print_cr("*** end of backchain ***"); 775 goto end_walk_callstack; 776 } 777 } else if (!is_valid_stackpointer(sp, stack_base, stack_size)) { 778 st->print_cr("*** stack pointer invalid - backchain corrupted (" PTR_FORMAT ") ***", p2i(sp)); 779 retry = true; 780 } else if (sp < sp_last) { 781 st->print_cr("invalid stack pointer: " PTR_FORMAT " (not monotone raising)", p2i(sp)); 782 retry = true; 783 } 784 785 // If backchain is broken, try to recover, by manually scanning the stack for a pattern 786 // which looks like a valid stack. 787 if (retry) { 788 st->print_cr("trying to recover and find backchain..."); 789 sp = try_find_backchain(sp_last, stack_base, stack_size); 790 if (sp) { 791 st->print_cr("found something which looks like a backchain at " PTR64_FORMAT ", after 0x%x bytes... ", 792 p2i(sp), PTRDIFF_BYTES(sp, sp_last)); 793 } else { 794 st->print_cr("did not find a backchain, giving up."); 795 goto end_walk_callstack; 796 } 797 } 798 799 // Print stackframe. 800 print_stackframe(st, sp, buf, buf_size, demangle); 801 st->cr(); 802 frame ++; 803 804 // Next stack frame and link area. 805 sp_last = sp; 806 sp = (unsigned long*)(*sp); 807 } 808 809 // Prevent endless loops in case of invalid callstacks. 810 if (frame == MAX_CALLSTACK_DEPTH) { 811 st->print_cr("...(stopping after %d frames.", MAX_CALLSTACK_DEPTH); 812 } 813 814 end_walk_callstack: 815 816 st->print_cr("-----------------------"); 817 818 cleanup: 819 820 return; 821 822 } 823 824 825 bool AixMisc::query_stack_bounds_for_current_thread(stackbounds_t* out) { 826 827 // Information about this api can be found (a) in the pthread.h header and 828 // (b) in http://publib.boulder.ibm.com/infocenter/pseries/v5r3/index.jsp?topic=/com.ibm.aix.basetechref/doc/basetrf1/pthread_getthrds_np.htm 829 // 830 // The use of this API to find out the current stack is kind of undefined. 831 // But after a lot of tries and asking IBM about it, I concluded that it is safe 832 // enough for cases where I let the pthread library create its stacks. For cases 833 // where I create an own stack and pass this to pthread_create, it seems not to 834 // work (the returned stack size in that case is 0). 835 836 pthread_t tid = pthread_self(); 837 struct __pthrdsinfo pinfo; 838 char dummy[1]; // Just needed to satisfy pthread_getthrds_np. 839 int dummy_size = sizeof(dummy); 840 841 memset(&pinfo, 0, sizeof(pinfo)); 842 843 const int rc = pthread_getthrds_np(&tid, PTHRDSINFO_QUERY_ALL, &pinfo, 844 sizeof(pinfo), dummy, &dummy_size); 845 846 if (rc != 0) { 847 fprintf(stderr, "pthread_getthrds_np failed (%d)\n", rc); 848 fflush(stdout); 849 return false; 850 } 851 852 // The following may happen when invoking pthread_getthrds_np on a pthread 853 // running on a user provided stack (when handing down a stack to pthread 854 // create, see pthread_attr_setstackaddr). 855 // Not sure what to do then. 856 if (pinfo.__pi_stackend == NULL || pinfo.__pi_stackaddr == NULL) { 857 fprintf(stderr, "pthread_getthrds_np - invalid values\n"); 858 fflush(stdout); 859 return false; 860 } 861 862 // Note: we get three values from pthread_getthrds_np: 863 // __pi_stackaddr, __pi_stacksize, __pi_stackend 864 // 865 // high addr --------------------- base, high 866 // 867 // | pthread internal data, like ~2K 868 // | 869 // | --------------------- __pi_stackend (usually not page aligned, (xxxxF890)) 870 // | 871 // | 872 // | 873 // | 874 // | 875 // | 876 // | --------------------- (__pi_stackend - __pi_stacksize) 877 // | 878 // | padding to align the following AIX guard pages, if enabled. 879 // | 880 // V --------------------- __pi_stackaddr low, base - size 881 // 882 // low addr AIX guard pages, if enabled (AIXTHREAD_GUARDPAGES > 0) 883 // 884 885 out->base = (address)pinfo.__pi_stackend; 886 address low = (address)pinfo.__pi_stackaddr; 887 out->size = out->base - low; 888 return true; 889 890 } 891 892 893 894