porting_aix.cpp revision 13243:7235bc30c0d7
1204642Srdivacky/* 2204642Srdivacky * Copyright (c) 2012, 2013 SAP SE. All rights reserved. 3204642Srdivacky * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4204642Srdivacky * 5204642Srdivacky * This code is free software; you can redistribute it and/or modify it 6204642Srdivacky * under the terms of the GNU General Public License version 2 only, as 7204642Srdivacky * published by the Free Software Foundation. 8204642Srdivacky * 9204642Srdivacky * This code is distributed in the hope that it will be useful, but WITHOUT 10204642Srdivacky * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11204642Srdivacky * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12204642Srdivacky * version 2 for more details (a copy is included in the LICENSE file that 13204642Srdivacky * accompanied this code). 14204642Srdivacky * 15204642Srdivacky * You should have received a copy of the GNU General Public License version 16204642Srdivacky * 2 along with this work; if not, write to the Free Software Foundation, 17204642Srdivacky * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18204642Srdivacky * 19204642Srdivacky * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20204642Srdivacky * or visit www.oracle.com if you need additional information or have any 21204642Srdivacky * questions. 22204642Srdivacky * 23204642Srdivacky */ 24204642Srdivacky 25204642Srdivacky#include "asm/assembler.hpp" 26204642Srdivacky#include "compiler/disassembler.hpp" 27204642Srdivacky#include "loadlib_aix.hpp" 28204642Srdivacky#include "memory/allocation.hpp" 29204642Srdivacky#include "memory/allocation.inline.hpp" 30204642Srdivacky#include "misc_aix.hpp" 31204642Srdivacky#include "porting_aix.hpp" 32204642Srdivacky#include "runtime/os.hpp" 33204642Srdivacky#include "runtime/thread.hpp" 34204642Srdivacky#include "utilities/debug.hpp" 35204642Srdivacky 36204642Srdivacky#include <demangle.h> 37204642Srdivacky#include <sys/debug.h> 38204642Srdivacky#include <pthread.h> 39204642Srdivacky#include <ucontext.h> 40204642Srdivacky 41204642Srdivacky////////////////////////////////// 42204642Srdivacky// Provide implementation for dladdr based on LoadedLibraries pool and 43204642Srdivacky// traceback table scan 44204642Srdivacky 45204642Srdivacky// Search traceback table in stack, 46205218Srdivacky// return procedure name from trace back table. 47205218Srdivacky#define MAX_FUNC_SEARCH_LEN 0x10000 48205218Srdivacky 49204642Srdivacky#define PTRDIFF_BYTES(p1,p2) (((ptrdiff_t)p1) - ((ptrdiff_t)p2)) 50206083Srdivacky 51206083Srdivacky// Typedefs for stackslots, stack pointers, pointers to op codes. 52206083Srdivackytypedef unsigned long stackslot_t; 53205218Srdivackytypedef stackslot_t* stackptr_t; 54263508Sdimtypedef unsigned int* codeptr_t; 55263508Sdim 56263508Sdim// Unfortunately, the interface of dladdr makes the implementator 57263508Sdim// responsible for maintaining memory for function name/library 58263508Sdim// name. I guess this is because most OS's keep those values as part 59204642Srdivacky// of the mapped executable image ready to use. On AIX, this doesn't 60204642Srdivacky// work, so I have to keep the returned strings. For now, I do this in 61204642Srdivacky// a primitive string map. Should this turn out to be a performance 62204642Srdivacky// problem, a better hashmap has to be used. 63204642Srdivackyclass fixed_strings { 64204642Srdivacky struct node : public CHeapObj<mtInternal> { 65204642Srdivacky char* v; 66204642Srdivacky node* next; 67204642Srdivacky }; 68204642Srdivacky 69204642Srdivacky node* first; 70204642Srdivacky 71204642Srdivacky public: 72204642Srdivacky 73204642Srdivacky fixed_strings() : first(0) {} 74204642Srdivacky ~fixed_strings() { 75204642Srdivacky node* n = first; 76204642Srdivacky while (n) { 77204642Srdivacky node* p = n; 78204642Srdivacky n = n->next; 79204642Srdivacky os::free(p->v); 80204642Srdivacky delete p; 81218893Sdim } 82204642Srdivacky } 83204642Srdivacky 84204642Srdivacky char* intern(const char* s) { 85204642Srdivacky for (node* n = first; n; n = n->next) { 86204642Srdivacky if (strcmp(n->v, s) == 0) { 87204642Srdivacky return n->v; 88204642Srdivacky } 89204642Srdivacky } 90204642Srdivacky node* p = new node; 91204642Srdivacky p->v = os::strdup_check_oom(s); 92204642Srdivacky p->next = first; 93204642Srdivacky first = p; 94204642Srdivacky return p->v; 95204642Srdivacky } 96204642Srdivacky}; 97204642Srdivacky 98204642Srdivackystatic fixed_strings dladdr_fixed_strings; 99204642Srdivacky 100204642Srdivackybool AixSymbols::get_function_name ( 101204642Srdivacky address pc0, // [in] program counter 102204642Srdivacky char* p_name, size_t namelen, // [out] optional: function name ("" if not available) 103204642Srdivacky int* p_displacement, // [out] optional: displacement (-1 if not available) 104204642Srdivacky const struct tbtable** p_tb, // [out] optional: ptr to traceback table to get further 105204642Srdivacky // information (NULL if not available) 106218893Sdim bool demangle // [in] whether to demangle the name 107204642Srdivacky ) { 108204642Srdivacky struct tbtable* tb = 0; 109204642Srdivacky unsigned int searchcount = 0; 110204642Srdivacky 111204642Srdivacky // initialize output parameters 112204642Srdivacky if (p_name && namelen > 0) { 113204642Srdivacky *p_name = '\0'; 114204642Srdivacky } 115218893Sdim if (p_displacement) { 116204642Srdivacky *p_displacement = -1; 117204642Srdivacky } 118218893Sdim if (p_tb) { 119204642Srdivacky *p_tb = NULL; 120204642Srdivacky } 121204642Srdivacky 122218893Sdim codeptr_t pc = (codeptr_t)pc0; 123204642Srdivacky 124204642Srdivacky // weed out obvious bogus states 125204642Srdivacky if (pc < (codeptr_t)0x1000) { 126204642Srdivacky trcVerbose("invalid program counter"); 127218893Sdim return false; 128218893Sdim } 129204642Srdivacky 130218893Sdim // We see random but frequent crashes in this function since some months mainly on shutdown 131218893Sdim // (-XX:+DumpInfoAtExit). It appears the page we are reading is randomly disappearing while 132204642Srdivacky // we read it (?). 133204642Srdivacky // As the pc cannot be trusted to be anything sensible lets make all reads via SafeFetch. Also 134204642Srdivacky // bail if this is not a text address right now. 135204642Srdivacky if (!LoadedLibraries::find_for_text_address(pc, NULL)) { 136204642Srdivacky trcVerbose("not a text address"); 137204642Srdivacky return false; 138204642Srdivacky } 139204642Srdivacky 140204642Srdivacky // .. (Note that is_readable_pointer returns true if safefetch stubs are not there yet; 141204642Srdivacky // in that case I try reading the traceback table unsafe - I rather risk secondary crashes in 142204642Srdivacky // error files than not having a callstack.) 143204642Srdivacky#define CHECK_POINTER_READABLE(p) \ 144204642Srdivacky if (!MiscUtils::is_readable_pointer(p)) { \ 145204642Srdivacky trcVerbose("pc not readable"); \ 146204642Srdivacky return false; \ 147204642Srdivacky } 148204642Srdivacky 149204642Srdivacky codeptr_t pc2 = (codeptr_t) pc; 150204642Srdivacky 151204642Srdivacky // Make sure the pointer is word aligned. 152204642Srdivacky pc2 = (codeptr_t) align_up((char*)pc2, 4); 153204642Srdivacky CHECK_POINTER_READABLE(pc2) 154204642Srdivacky 155204642Srdivacky // Find start of traceback table. 156204642Srdivacky // (starts after code, is marked by word-aligned (32bit) zeros) 157204642Srdivacky while ((*pc2 != NULL) && (searchcount++ < MAX_FUNC_SEARCH_LEN)) { 158204642Srdivacky CHECK_POINTER_READABLE(pc2) 159204642Srdivacky pc2++; 160204642Srdivacky } 161204642Srdivacky if (*pc2 != 0) { 162204642Srdivacky trcVerbose("no traceback table found"); 163204642Srdivacky return false; 164204642Srdivacky } 165204642Srdivacky // 166204642Srdivacky // Set up addressability to the traceback table 167204642Srdivacky // 168204642Srdivacky tb = (struct tbtable*) (pc2 + 1); 169204642Srdivacky 170204642Srdivacky // Is this really a traceback table? No way to be sure but 171204642Srdivacky // some indicators we can check. 172204642Srdivacky if (tb->tb.lang >= 0xf && tb->tb.lang <= 0xfb) { 173204642Srdivacky // Language specifiers, go from 0 (C) to 14 (Objective C). 174204642Srdivacky // According to spec, 0xf-0xfa reserved, 0xfb-0xff reserved for ibm. 175204642Srdivacky trcVerbose("no traceback table found"); 176204642Srdivacky return false; 177204642Srdivacky } 178204642Srdivacky 179204642Srdivacky // Existence of fields in the tbtable extension are contingent upon 180204642Srdivacky // specific fields in the base table. Check for their existence so 181204642Srdivacky // that we can address the function name if it exists. 182204642Srdivacky pc2 = (codeptr_t) tb + 183204642Srdivacky sizeof(struct tbtable_short)/sizeof(int); 184204642Srdivacky if (tb->tb.fixedparms != 0 || tb->tb.floatparms != 0) 185204642Srdivacky pc2++; 186204642Srdivacky 187204642Srdivacky CHECK_POINTER_READABLE(pc2) 188204642Srdivacky 189204642Srdivacky if (tb->tb.has_tboff == TRUE) { 190204642Srdivacky 191204642Srdivacky // I want to know the displacement 192204642Srdivacky const unsigned int tb_offset = *pc2; 193204642Srdivacky codeptr_t start_of_procedure = 194204642Srdivacky (codeptr_t)(((char*)tb) - 4 - tb_offset); // (-4 to omit leading 0000) 195204642Srdivacky 196204642Srdivacky // Weed out the cases where we did find the wrong traceback table. 197204642Srdivacky if (pc < start_of_procedure) { 198204642Srdivacky trcVerbose("no traceback table found"); 199204642Srdivacky return false; 200204642Srdivacky } 201204642Srdivacky 202204642Srdivacky // return the displacement 203204642Srdivacky if (p_displacement) { 204204642Srdivacky (*p_displacement) = (int) PTRDIFF_BYTES(pc, start_of_procedure); 205204642Srdivacky } 206204642Srdivacky 207204642Srdivacky pc2++; 208204642Srdivacky } else { 209204642Srdivacky // return -1 for displacement 210204642Srdivacky if (p_displacement) { 211204642Srdivacky (*p_displacement) = -1; 212204642Srdivacky } 213204642Srdivacky } 214204642Srdivacky 215204642Srdivacky if (tb->tb.int_hndl == TRUE) 216204642Srdivacky pc2++; 217204642Srdivacky 218204642Srdivacky if (tb->tb.has_ctl == TRUE) 219204642Srdivacky pc2 += (*pc2) + 1; // don't care 220204642Srdivacky 221204642Srdivacky CHECK_POINTER_READABLE(pc2) 222204642Srdivacky 223204642Srdivacky // 224204642Srdivacky // return function name if it exists. 225204642Srdivacky // 226204642Srdivacky if (p_name && namelen > 0) { 227204642Srdivacky if (tb->tb.name_present) { 228204642Srdivacky // Copy name from text because it may not be zero terminated. 229204961Srdivacky const short l = MIN2<short>(*((short*)pc2), namelen - 1); 230204961Srdivacky // Be very careful. 231204961Srdivacky int i = 0; char* const p = (char*)pc2 + sizeof(short); 232204961Srdivacky while (i < l && MiscUtils::is_readable_pointer(p + i)) { 233204961Srdivacky p_name[i] = p[i]; 234204961Srdivacky i++; 235204961Srdivacky } 236204961Srdivacky p_name[i] = '\0'; 237204961Srdivacky 238204961Srdivacky // If it is a C++ name, try and demangle it using the Demangle interface (see demangle.h). 239204961Srdivacky if (demangle) { 240204642Srdivacky char* rest; 241204642Srdivacky Name* const name = Demangle(p_name, rest); 242204642Srdivacky if (name) { 243204642Srdivacky const char* const demangled_name = name->Text(); 244204642Srdivacky if (demangled_name) { 245204642Srdivacky strncpy(p_name, demangled_name, namelen-1); 246204642Srdivacky p_name[namelen-1] = '\0'; 247204642Srdivacky } 248204642Srdivacky delete name; 249204642Srdivacky } 250204642Srdivacky } 251204642Srdivacky } else { 252204642Srdivacky strncpy(p_name, "<nameless function>", namelen-1); 253204642Srdivacky p_name[namelen-1] = '\0'; 254204642Srdivacky } 255204642Srdivacky } 256204642Srdivacky 257204642Srdivacky // Return traceback table, if user wants it. 258204642Srdivacky if (p_tb) { 259204642Srdivacky (*p_tb) = tb; 260204642Srdivacky } 261204642Srdivacky 262204642Srdivacky return true; 263204642Srdivacky 264204642Srdivacky} 265204642Srdivacky 266204642Srdivackybool AixSymbols::get_module_name(address pc, 267204642Srdivacky char* p_name, size_t namelen) { 268204642Srdivacky 269204642Srdivacky if (p_name && namelen > 0) { 270204642Srdivacky p_name[0] = '\0'; 271204642Srdivacky loaded_module_t lm; 272204642Srdivacky if (LoadedLibraries::find_for_text_address(pc, &lm) != NULL) { 273204642Srdivacky strncpy(p_name, lm.shortname, namelen); 274204642Srdivacky p_name[namelen - 1] = '\0'; 275204642Srdivacky return true; 276204642Srdivacky } 277204642Srdivacky } 278204642Srdivacky 279204642Srdivacky return false; 280204642Srdivacky} 281204642Srdivacky 282204642Srdivacky// Special implementation of dladdr for Aix based on LoadedLibraries 283204642Srdivacky// Note: dladdr returns non-zero for ok, 0 for error! 284204642Srdivacky// Note: dladdr is not posix, but a non-standard GNU extension. So this tries to 285204642Srdivacky// fulfill the contract of dladdr on Linux (see http://linux.die.net/man/3/dladdr) 286204642Srdivacky// Note: addr may be both an AIX function descriptor or a real code pointer 287204642Srdivacky// to the entry of a function. 288204642Srdivackyextern "C" 289204642Srdivackyint dladdr(void* addr, Dl_info* info) { 290204642Srdivacky 291204642Srdivacky if (!addr) { 292204642Srdivacky return 0; 293204642Srdivacky } 294204642Srdivacky 295204642Srdivacky assert(info, ""); 296204642Srdivacky 297204642Srdivacky int rc = 0; 298204642Srdivacky 299204642Srdivacky const char* const ZEROSTRING = ""; 300204642Srdivacky 301204642Srdivacky // Always return a string, even if a "" one. Linux dladdr manpage 302204642Srdivacky // does not say anything about returning NULL 303204642Srdivacky info->dli_fname = ZEROSTRING; 304204642Srdivacky info->dli_sname = ZEROSTRING; 305204642Srdivacky info->dli_saddr = NULL; 306204642Srdivacky 307204961Srdivacky address p = (address) addr; 308204961Srdivacky loaded_module_t lm; 309204961Srdivacky bool found = false; 310204961Srdivacky 311204961Srdivacky enum { noclue, code, data } type = noclue; 312204961Srdivacky 313204961Srdivacky trcVerbose("dladdr(%p)...", p); 314204961Srdivacky 315204961Srdivacky // Note: input address may be a function. I accept both a pointer to 316204961Srdivacky // the entry of a function and a pointer to the function decriptor. 317204961Srdivacky // (see ppc64 ABI) 318204961Srdivacky found = LoadedLibraries::find_for_text_address(p, &lm); 319204961Srdivacky if (found) { 320204961Srdivacky type = code; 321204961Srdivacky } 322204961Srdivacky 323204961Srdivacky if (!found) { 324204961Srdivacky // Not a pointer into any text segment. Is it a function descriptor? 325204961Srdivacky const FunctionDescriptor* const pfd = (const FunctionDescriptor*) p; 326204642Srdivacky p = pfd->entry(); 327204961Srdivacky if (p) { 328204961Srdivacky found = LoadedLibraries::find_for_text_address(p, &lm); 329204961Srdivacky if (found) { 330204961Srdivacky type = code; 331204961Srdivacky } 332204961Srdivacky } 333204961Srdivacky } 334204961Srdivacky 335204961Srdivacky if (!found) { 336204961Srdivacky // Neither direct code pointer nor function descriptor. A data ptr? 337204961Srdivacky p = (address)addr; 338204961Srdivacky found = LoadedLibraries::find_for_data_address(p, &lm); 339204961Srdivacky if (found) { 340204961Srdivacky type = data; 341204961Srdivacky } 342204961Srdivacky } 343204961Srdivacky 344204642Srdivacky // If we did find the shared library this address belongs to (either 345204961Srdivacky // code or data segment) resolve library path and, if possible, the 346204961Srdivacky // symbol name. 347204642Srdivacky if (found) { 348204642Srdivacky 349204642Srdivacky // No need to intern the libpath, that one is already interned one layer below. 350204642Srdivacky info->dli_fname = lm.path; 351204642Srdivacky 352204642Srdivacky if (type == code) { 353204642Srdivacky 354204642Srdivacky // For code symbols resolve function name and displacement. Use 355204642Srdivacky // displacement to calc start of function. 356204642Srdivacky char funcname[256] = ""; 357204642Srdivacky int displacement = 0; 358204642Srdivacky 359204642Srdivacky if (AixSymbols::get_function_name(p, funcname, sizeof(funcname), 360204642Srdivacky &displacement, NULL, true)) { 361204642Srdivacky if (funcname[0] != '\0') { 362204642Srdivacky const char* const interned = dladdr_fixed_strings.intern(funcname); 363204642Srdivacky info->dli_sname = interned; 364204642Srdivacky trcVerbose("... function name: %s ...", interned); 365204642Srdivacky } 366204642Srdivacky 367204642Srdivacky // From the displacement calculate the start of the function. 368204642Srdivacky if (displacement != -1) { 369204642Srdivacky info->dli_saddr = p - displacement; 370204642Srdivacky } else { 371204642Srdivacky info->dli_saddr = p; 372204642Srdivacky } 373204642Srdivacky } else { 374204642Srdivacky 375204642Srdivacky // No traceback table found. Just assume the pointer is it. 376204642Srdivacky info->dli_saddr = p; 377204642Srdivacky 378204642Srdivacky } 379204642Srdivacky 380204642Srdivacky } else if (type == data) { 381204642Srdivacky 382204642Srdivacky // For data symbols. 383204642Srdivacky info->dli_saddr = p; 384204642Srdivacky 385204642Srdivacky } else { 386204642Srdivacky ShouldNotReachHere(); 387204642Srdivacky } 388204642Srdivacky 389204642Srdivacky rc = 1; // success: return 1 [sic] 390204642Srdivacky 391204642Srdivacky } 392204642Srdivacky 393204642Srdivacky // sanity checks. 394204642Srdivacky if (rc) { 395204642Srdivacky assert(info->dli_fname, ""); 396204642Srdivacky assert(info->dli_sname, ""); 397204642Srdivacky assert(info->dli_saddr, ""); 398204642Srdivacky } 399204642Srdivacky 400204642Srdivacky return rc; // error: return 0 [sic] 401204642Srdivacky 402204642Srdivacky} 403204642Srdivacky 404204642Srdivacky///////////////////////////////////////////////////////////////////////////// 405204642Srdivacky// Native callstack dumping 406204642Srdivacky 407204642Srdivacky// Print the traceback table for one stack frame. 408204642Srdivackystatic void print_tbtable (outputStream* st, const struct tbtable* p_tb) { 409204642Srdivacky 410204961Srdivacky if (p_tb == NULL) { 411204961Srdivacky st->print("<null>"); 412204961Srdivacky return; 413204642Srdivacky } 414204961Srdivacky 415204642Srdivacky switch(p_tb->tb.lang) { 416204642Srdivacky case TB_C: st->print("C"); break; 417204642Srdivacky case TB_FORTRAN: st->print("FORTRAN"); break; 418204642Srdivacky case TB_PASCAL: st->print("PASCAL"); break; 419204642Srdivacky case TB_ADA: st->print("ADA"); break; 420204642Srdivacky case TB_PL1: st->print("PL1"); break; 421204642Srdivacky case TB_BASIC: st->print("BASIC"); break; 422204961Srdivacky case TB_LISP: st->print("LISP"); break; 423204961Srdivacky case TB_COBOL: st->print("COBOL"); break; 424204961Srdivacky case TB_MODULA2: st->print("MODULA2"); break; 425204961Srdivacky case TB_CPLUSPLUS: st->print("C++"); break; 426204961Srdivacky case TB_RPG: st->print("RPG"); break; 427206083Srdivacky case TB_PL8: st->print("PL8"); break; 428204961Srdivacky case TB_ASM: st->print("ASM"); break; 429204961Srdivacky case TB_HPJ: st->print("HPJ"); break; 430204961Srdivacky default: st->print("unknown"); 431206083Srdivacky } 432206083Srdivacky st->print(" "); 433204961Srdivacky 434204961Srdivacky if (p_tb->tb.globallink) { 435204961Srdivacky st->print("globallink "); 436204642Srdivacky } 437204961Srdivacky if (p_tb->tb.is_eprol) { 438204961Srdivacky st->print("eprol "); 439204961Srdivacky } 440204961Srdivacky if (p_tb->tb.int_proc) { 441204961Srdivacky st->print("int_proc "); 442204961Srdivacky } 443204642Srdivacky if (p_tb->tb.tocless) { 444204642Srdivacky st->print("tocless "); 445204642Srdivacky } 446204642Srdivacky if (p_tb->tb.fp_present) { 447204642Srdivacky st->print("fp_present "); 448204642Srdivacky } 449204642Srdivacky if (p_tb->tb.int_hndl) { 450204642Srdivacky st->print("interrupt_handler "); 451204642Srdivacky } 452204642Srdivacky if (p_tb->tb.uses_alloca) { 453204642Srdivacky st->print("uses_alloca "); 454204642Srdivacky } 455204642Srdivacky if (p_tb->tb.saves_cr) { 456204642Srdivacky st->print("saves_cr "); 457204642Srdivacky } 458204642Srdivacky if (p_tb->tb.saves_lr) { 459204642Srdivacky st->print("saves_lr "); 460204642Srdivacky } 461204642Srdivacky if (p_tb->tb.stores_bc) { 462204642Srdivacky st->print("stores_bc "); 463204642Srdivacky } 464204961Srdivacky if (p_tb->tb.fixup) { 465204642Srdivacky st->print("fixup "); 466204642Srdivacky } 467204961Srdivacky if (p_tb->tb.fpr_saved > 0) { 468204961Srdivacky st->print("fpr_saved:%d ", p_tb->tb.fpr_saved); 469204961Srdivacky } 470204961Srdivacky if (p_tb->tb.gpr_saved > 0) { 471204961Srdivacky st->print("gpr_saved:%d ", p_tb->tb.gpr_saved); 472204961Srdivacky } 473204961Srdivacky if (p_tb->tb.fixedparms > 0) { 474204961Srdivacky st->print("fixedparms:%d ", p_tb->tb.fixedparms); 475204961Srdivacky } 476204961Srdivacky if (p_tb->tb.floatparms > 0) { 477204961Srdivacky st->print("floatparms:%d ", p_tb->tb.floatparms); 478204961Srdivacky } 479204961Srdivacky if (p_tb->tb.parmsonstk > 0) { 480204961Srdivacky st->print("parmsonstk:%d", p_tb->tb.parmsonstk); 481204961Srdivacky } 482204961Srdivacky} 483204961Srdivacky 484204961Srdivacky// Print information for pc (module, function, displacement, traceback table) 485204961Srdivacky// on one line. 486204961Srdivackystatic void print_info_for_pc (outputStream* st, codeptr_t pc, char* buf, 487204961Srdivacky size_t buf_size, bool demangle) { 488204961Srdivacky const struct tbtable* tb = NULL; 489204961Srdivacky int displacement = -1; 490204961Srdivacky 491204642Srdivacky if (!MiscUtils::is_readable_pointer(pc)) { 492204642Srdivacky st->print("(invalid)"); 493204961Srdivacky return; 494204961Srdivacky } 495204961Srdivacky 496204961Srdivacky if (AixSymbols::get_module_name((address)pc, buf, buf_size)) { 497206083Srdivacky st->print("%s", buf); 498204961Srdivacky } else { 499204961Srdivacky st->print("(unknown module)"); 500204642Srdivacky } 501204642Srdivacky st->print("::"); 502204642Srdivacky if (AixSymbols::get_function_name((address)pc, buf, buf_size, 503204642Srdivacky &displacement, &tb, demangle)) { 504204642Srdivacky st->print("%s", buf); 505204642Srdivacky } else { 506204642Srdivacky st->print("(unknown function)"); 507204642Srdivacky } 508204642Srdivacky if (displacement == -1) { 509204642Srdivacky st->print("+?"); 510204642Srdivacky } else { 511204642Srdivacky st->print("+0x%x", displacement); 512204642Srdivacky } 513204642Srdivacky if (tb) { 514204642Srdivacky st->fill_to(64); 515204642Srdivacky st->print(" ("); 516204642Srdivacky print_tbtable(st, tb); 517204642Srdivacky st->print(")"); 518 } 519} 520 521static void print_stackframe(outputStream* st, stackptr_t sp, char* buf, 522 size_t buf_size, bool demangle) { 523 524 stackptr_t sp2 = sp; 525 526 // skip backchain 527 528 sp2++; 529 530 // skip crsave 531 532 sp2++; 533 534 // retrieve lrsave. That is the only info I need to get the function/displacement 535 536 codeptr_t lrsave = (codeptr_t) *(sp2); 537 st->print (PTR64_FORMAT " - " PTR64_FORMAT " ", sp2, lrsave); 538 539 if (lrsave != NULL) { 540 print_info_for_pc(st, lrsave, buf, buf_size, demangle); 541 } 542 543} 544 545// Function to check a given stack pointer against given stack limits. 546static bool is_valid_stackpointer(stackptr_t sp, stackptr_t stack_base, size_t stack_size) { 547 if (((uintptr_t)sp) & 0x7) { 548 return false; 549 } 550 if (sp > stack_base) { 551 return false; 552 } 553 if (sp < (stackptr_t) ((address)stack_base - stack_size)) { 554 return false; 555 } 556 return true; 557} 558 559// Returns true if function is a valid codepointer. 560static bool is_valid_codepointer(codeptr_t p) { 561 if (!p) { 562 return false; 563 } 564 if (((uintptr_t)p) & 0x3) { 565 return false; 566 } 567 if (LoadedLibraries::find_for_text_address(p, NULL) == NULL) { 568 return false; 569 } 570 return true; 571} 572 573// Function tries to guess if the given combination of stack pointer, stack base 574// and stack size is a valid stack frame. 575static bool is_valid_frame (stackptr_t p, stackptr_t stack_base, size_t stack_size) { 576 577 if (!is_valid_stackpointer(p, stack_base, stack_size)) { 578 return false; 579 } 580 581 // First check - the occurrence of a valid backchain pointer up the stack, followed by a 582 // valid codeptr, counts as a good candidate. 583 stackptr_t sp2 = (stackptr_t) *p; 584 if (is_valid_stackpointer(sp2, stack_base, stack_size) && // found a valid stack pointer in the stack... 585 ((sp2 - p) > 6) && // ... pointing upwards and not into my frame... 586 is_valid_codepointer((codeptr_t)(*(sp2 + 2)))) // ... followed by a code pointer after two slots... 587 { 588 return true; 589 } 590 591 return false; 592} 593 594// Try to relocate a stack back chain in a given stack. 595// Used in callstack dumping, when the backchain is broken by an overwriter 596static stackptr_t try_find_backchain (stackptr_t last_known_good_frame, 597 stackptr_t stack_base, size_t stack_size) 598{ 599 if (!is_valid_stackpointer(last_known_good_frame, stack_base, stack_size)) { 600 return NULL; 601 } 602 603 stackptr_t sp = last_known_good_frame; 604 605 sp += 6; // Omit next fixed frame slots. 606 while (sp < stack_base) { 607 if (is_valid_frame(sp, stack_base, stack_size)) { 608 return sp; 609 } 610 sp ++; 611 } 612 613 return NULL; 614} 615 616static void decode_instructions_at_pc(const char* header, 617 codeptr_t pc, int num_before, 618 int num_after, outputStream* st) { 619 // TODO: PPC port Disassembler::decode(pc, 16, 16, st); 620} 621 622 623void AixNativeCallstack::print_callstack_for_context(outputStream* st, const ucontext_t* context, 624 bool demangle, char* buf, size_t buf_size) { 625 626#define MAX_CALLSTACK_DEPTH 50 627 628 unsigned long* sp; 629 unsigned long* sp_last; 630 int frame; 631 632 // To print the first frame, use the current value of iar: 633 // current entry indicated by iar (the current pc) 634 codeptr_t cur_iar = 0; 635 stackptr_t cur_sp = 0; 636 codeptr_t cur_rtoc = 0; 637 codeptr_t cur_lr = 0; 638 639 const ucontext_t* uc = (const ucontext_t*) context; 640 641 // fallback: use the current context 642 ucontext_t local_context; 643 if (!uc) { 644 st->print_cr("No context given, using current context."); 645 if (getcontext(&local_context) == 0) { 646 uc = &local_context; 647 } else { 648 st->print_cr("No context given and getcontext failed. "); 649 return; 650 } 651 } 652 653 cur_iar = (codeptr_t)uc->uc_mcontext.jmp_context.iar; 654 cur_sp = (stackptr_t)uc->uc_mcontext.jmp_context.gpr[1]; 655 cur_rtoc = (codeptr_t)uc->uc_mcontext.jmp_context.gpr[2]; 656 cur_lr = (codeptr_t)uc->uc_mcontext.jmp_context.lr; 657 658 // syntax used here: 659 // n -------------- <-- stack_base, stack_to 660 // n-1 | | 661 // ... | older | 662 // ... | frames | | 663 // | | | stack grows downward 664 // ... | younger | | 665 // ... | frames | V 666 // | | 667 // |------------| <-- cur_sp, current stack ptr 668 // | | 669 // | unsused | 670 // | stack | 671 // | | 672 // . . 673 // . . 674 // . . 675 // . . 676 // | | 677 // 0 -------------- <-- stack_from 678 // 679 680 // Retrieve current stack base, size from the current thread. If there is none, 681 // retrieve it from the OS. 682 stackptr_t stack_base = NULL; 683 size_t stack_size = NULL; 684 { 685 AixMisc::stackbounds_t stackbounds; 686 if (!AixMisc::query_stack_bounds_for_current_thread(&stackbounds)) { 687 st->print_cr("Cannot retrieve stack bounds."); 688 return; 689 } 690 stack_base = (stackptr_t)stackbounds.base; 691 stack_size = stackbounds.size; 692 } 693 694 st->print_cr("------ current frame:"); 695 st->print("iar: " PTR64_FORMAT " ", p2i(cur_iar)); 696 print_info_for_pc(st, cur_iar, buf, buf_size, demangle); 697 st->cr(); 698 699 if (cur_iar && MiscUtils::is_readable_pointer(cur_iar)) { 700 decode_instructions_at_pc( 701 "Decoded instructions at iar:", 702 cur_iar, 32, 16, st); 703 } 704 705 // Print out lr too, which may be interesting if we did jump to some bogus location; 706 // in those cases the new frame is not built up yet and the caller location is only 707 // preserved via lr register. 708 st->print("lr: " PTR64_FORMAT " ", p2i(cur_lr)); 709 print_info_for_pc(st, cur_lr, buf, buf_size, demangle); 710 st->cr(); 711 712 if (cur_lr && MiscUtils::is_readable_pointer(cur_lr)) { 713 decode_instructions_at_pc( 714 "Decoded instructions at lr:", 715 cur_lr, 32, 16, st); 716 } 717 718 // Check and print sp. 719 st->print("sp: " PTR64_FORMAT " ", p2i(cur_sp)); 720 if (!is_valid_stackpointer(cur_sp, stack_base, stack_size)) { 721 st->print("(invalid) "); 722 goto cleanup; 723 } else { 724 st->print("(base - 0x%X) ", PTRDIFF_BYTES(stack_base, cur_sp)); 725 } 726 st->cr(); 727 728 // Check and print rtoc. 729 st->print("rtoc: " PTR64_FORMAT " ", p2i(cur_rtoc)); 730 if (cur_rtoc == NULL || cur_rtoc == (codeptr_t)-1 || 731 !MiscUtils::is_readable_pointer(cur_rtoc)) { 732 st->print("(invalid)"); 733 } else if (((uintptr_t)cur_rtoc) & 0x7) { 734 st->print("(unaligned)"); 735 } 736 st->cr(); 737 738 st->print_cr("|---stackaddr----| |----lrsave------|: <function name>"); 739 740 /// 741 // Walk callstack. 742 // 743 // (if no context was given, use the current stack) 744 sp = (unsigned long*)(*(unsigned long*)cur_sp); // Stack pointer 745 sp_last = cur_sp; 746 747 frame = 0; 748 749 while (frame < MAX_CALLSTACK_DEPTH) { 750 751 // Check sp. 752 bool retry = false; 753 if (sp == NULL) { 754 // The backchain pointer was NULL. This normally means the end of the chain. But the 755 // stack might be corrupted, and it may be worth looking for the stack chain. 756 if (is_valid_stackpointer(sp_last, stack_base, stack_size) && (stack_base - 0x10) > sp_last) { 757 // If we are not within <guess> 0x10 stackslots of the stack base, we assume that this 758 // is indeed not the end of the chain but that the stack was corrupted. So lets try to 759 // find the end of the chain. 760 st->print_cr("*** back chain pointer is NULL - end of stack or broken backchain ? ***"); 761 retry = true; 762 } else { 763 st->print_cr("*** end of backchain ***"); 764 goto end_walk_callstack; 765 } 766 } else if (!is_valid_stackpointer(sp, stack_base, stack_size)) { 767 st->print_cr("*** stack pointer invalid - backchain corrupted (" PTR_FORMAT ") ***", p2i(sp)); 768 retry = true; 769 } else if (sp < sp_last) { 770 st->print_cr("invalid stack pointer: " PTR_FORMAT " (not monotone raising)", p2i(sp)); 771 retry = true; 772 } 773 774 // If backchain is broken, try to recover, by manually scanning the stack for a pattern 775 // which looks like a valid stack. 776 if (retry) { 777 st->print_cr("trying to recover and find backchain..."); 778 sp = try_find_backchain(sp_last, stack_base, stack_size); 779 if (sp) { 780 st->print_cr("found something which looks like a backchain at " PTR64_FORMAT ", after 0x%x bytes... ", 781 p2i(sp), PTRDIFF_BYTES(sp, sp_last)); 782 } else { 783 st->print_cr("did not find a backchain, giving up."); 784 goto end_walk_callstack; 785 } 786 } 787 788 // Print stackframe. 789 print_stackframe(st, sp, buf, buf_size, demangle); 790 st->cr(); 791 frame ++; 792 793 // Next stack frame and link area. 794 sp_last = sp; 795 sp = (unsigned long*)(*sp); 796 } 797 798 // Prevent endless loops in case of invalid callstacks. 799 if (frame == MAX_CALLSTACK_DEPTH) { 800 st->print_cr("...(stopping after %d frames.", MAX_CALLSTACK_DEPTH); 801 } 802 803end_walk_callstack: 804 805 st->print_cr("-----------------------"); 806 807cleanup: 808 809 return; 810 811} 812 813 814bool AixMisc::query_stack_bounds_for_current_thread(stackbounds_t* out) { 815 816 // Information about this api can be found (a) in the pthread.h header and 817 // (b) in http://publib.boulder.ibm.com/infocenter/pseries/v5r3/index.jsp?topic=/com.ibm.aix.basetechref/doc/basetrf1/pthread_getthrds_np.htm 818 // 819 // The use of this API to find out the current stack is kind of undefined. 820 // But after a lot of tries and asking IBM about it, I concluded that it is safe 821 // enough for cases where I let the pthread library create its stacks. For cases 822 // where I create an own stack and pass this to pthread_create, it seems not to 823 // work (the returned stack size in that case is 0). 824 825 pthread_t tid = pthread_self(); 826 struct __pthrdsinfo pinfo; 827 char dummy[1]; // Just needed to satisfy pthread_getthrds_np. 828 int dummy_size = sizeof(dummy); 829 830 memset(&pinfo, 0, sizeof(pinfo)); 831 832 const int rc = pthread_getthrds_np(&tid, PTHRDSINFO_QUERY_ALL, &pinfo, 833 sizeof(pinfo), dummy, &dummy_size); 834 835 if (rc != 0) { 836 fprintf(stderr, "pthread_getthrds_np failed (%d)\n", rc); 837 fflush(stdout); 838 return false; 839 } 840 841 // The following may happen when invoking pthread_getthrds_np on a pthread 842 // running on a user provided stack (when handing down a stack to pthread 843 // create, see pthread_attr_setstackaddr). 844 // Not sure what to do then. 845 if (pinfo.__pi_stackend == NULL || pinfo.__pi_stackaddr == NULL) { 846 fprintf(stderr, "pthread_getthrds_np - invalid values\n"); 847 fflush(stdout); 848 return false; 849 } 850 851 // Note: we get three values from pthread_getthrds_np: 852 // __pi_stackaddr, __pi_stacksize, __pi_stackend 853 // 854 // high addr --------------------- base, high 855 // 856 // | pthread internal data, like ~2K 857 // | 858 // | --------------------- __pi_stackend (usually not page aligned, (xxxxF890)) 859 // | 860 // | 861 // | 862 // | 863 // | 864 // | 865 // | --------------------- (__pi_stackend - __pi_stacksize) 866 // | 867 // | padding to align the following AIX guard pages, if enabled. 868 // | 869 // V --------------------- __pi_stackaddr low, base - size 870 // 871 // low addr AIX guard pages, if enabled (AIXTHREAD_GUARDPAGES > 0) 872 // 873 874 out->base = (address)pinfo.__pi_stackend; 875 address low = (address)pinfo.__pi_stackaddr; 876 out->size = out->base - low; 877 return true; 878 879} 880 881 882 883 884