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