Path: blob/master/src/hotspot/os/aix/porting_aix.cpp
40930 views
/*1* Copyright (c) 2012, 2019 SAP SE. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324#include "asm/assembler.hpp"25#include "compiler/disassembler.hpp"26#include "loadlib_aix.hpp"27#include "memory/allocation.hpp"28#include "memory/allocation.inline.hpp"29#include "misc_aix.hpp"30#include "porting_aix.hpp"31#include "runtime/os.hpp"32#include "runtime/thread.hpp"33#include "utilities/align.hpp"34#include "utilities/debug.hpp"3536// distinguish old xlc and xlclang++, where37// <ibmdemangle.h> is suggested but not found in GA release (might come with a fix)38#if defined(__clang__)39#define DISABLE_DEMANGLE40// #include <ibmdemangle.h>41#else42#include <demangle.h>43#endif4445#include <sys/debug.h>46#include <pthread.h>47#include <ucontext.h>4849//////////////////////////////////50// Provide implementation for dladdr based on LoadedLibraries pool and51// traceback table scan5253// Search traceback table in stack,54// return procedure name from trace back table.55#define MAX_FUNC_SEARCH_LEN 0x100005657#define PTRDIFF_BYTES(p1,p2) (((ptrdiff_t)p1) - ((ptrdiff_t)p2))5859// Typedefs for stackslots, stack pointers, pointers to op codes.60typedef unsigned long stackslot_t;61typedef stackslot_t* stackptr_t;62typedef unsigned int* codeptr_t;6364// Unfortunately, the interface of dladdr makes the implementator65// responsible for maintaining memory for function name/library66// name. I guess this is because most OS's keep those values as part67// of the mapped executable image ready to use. On AIX, this doesn't68// work, so I have to keep the returned strings. For now, I do this in69// a primitive string map. Should this turn out to be a performance70// problem, a better hashmap has to be used.71class fixed_strings {72struct node : public CHeapObj<mtInternal> {73char* v;74node* next;75};7677node* first;7879public:8081fixed_strings() : first(0) {}82~fixed_strings() {83node* n = first;84while (n) {85node* p = n;86n = n->next;87os::free(p->v);88delete p;89}90}9192char* intern(const char* s) {93for (node* n = first; n; n = n->next) {94if (strcmp(n->v, s) == 0) {95return n->v;96}97}98node* p = new node;99p->v = os::strdup_check_oom(s);100p->next = first;101first = p;102return p->v;103}104};105106static fixed_strings dladdr_fixed_strings;107108bool AixSymbols::get_function_name (109address pc0, // [in] program counter110char* p_name, size_t namelen, // [out] optional: function name ("" if not available)111int* p_displacement, // [out] optional: displacement (-1 if not available)112const struct tbtable** p_tb, // [out] optional: ptr to traceback table to get further113// information (NULL if not available)114bool demangle // [in] whether to demangle the name115) {116struct tbtable* tb = 0;117unsigned int searchcount = 0;118119// initialize output parameters120if (p_name && namelen > 0) {121*p_name = '\0';122}123if (p_displacement) {124*p_displacement = -1;125}126if (p_tb) {127*p_tb = NULL;128}129130codeptr_t pc = (codeptr_t)pc0;131132// weed out obvious bogus states133if (pc < (codeptr_t)0x1000) {134trcVerbose("invalid program counter");135return false;136}137138// We see random but frequent crashes in this function since some months mainly on shutdown139// (-XX:+DumpInfoAtExit). It appears the page we are reading is randomly disappearing while140// we read it (?).141// As the pc cannot be trusted to be anything sensible lets make all reads via SafeFetch. Also142// bail if this is not a text address right now.143if (!LoadedLibraries::find_for_text_address(pc, NULL)) {144trcVerbose("not a text address");145return false;146}147148// .. (Note that is_readable_pointer returns true if safefetch stubs are not there yet;149// in that case I try reading the traceback table unsafe - I rather risk secondary crashes in150// error files than not having a callstack.)151#define CHECK_POINTER_READABLE(p) \152if (!os::is_readable_pointer(p)) { \153trcVerbose("pc not readable"); \154return false; \155}156157codeptr_t pc2 = (codeptr_t) pc;158159// Make sure the pointer is word aligned.160pc2 = (codeptr_t) align_up((char*)pc2, 4);161CHECK_POINTER_READABLE(pc2)162163// Find start of traceback table.164// (starts after code, is marked by word-aligned (32bit) zeros)165while ((*pc2 != NULL) && (searchcount++ < MAX_FUNC_SEARCH_LEN)) {166CHECK_POINTER_READABLE(pc2)167pc2++;168}169if (*pc2 != 0) {170trcVerbose("no traceback table found");171return false;172}173//174// Set up addressability to the traceback table175//176tb = (struct tbtable*) (pc2 + 1);177178// Is this really a traceback table? No way to be sure but179// some indicators we can check.180if (tb->tb.lang >= 0xf && tb->tb.lang <= 0xfb) {181// Language specifiers, go from 0 (C) to 14 (Objective C).182// According to spec, 0xf-0xfa reserved, 0xfb-0xff reserved for ibm.183trcVerbose("no traceback table found");184return false;185}186187// Existence of fields in the tbtable extension are contingent upon188// specific fields in the base table. Check for their existence so189// that we can address the function name if it exists.190pc2 = (codeptr_t) tb +191sizeof(struct tbtable_short)/sizeof(int);192if (tb->tb.fixedparms != 0 || tb->tb.floatparms != 0)193pc2++;194195CHECK_POINTER_READABLE(pc2)196197if (tb->tb.has_tboff == TRUE) {198199// I want to know the displacement200const unsigned int tb_offset = *pc2;201codeptr_t start_of_procedure =202(codeptr_t)(((char*)tb) - 4 - tb_offset); // (-4 to omit leading 0000)203204// Weed out the cases where we did find the wrong traceback table.205if (pc < start_of_procedure) {206trcVerbose("no traceback table found");207return false;208}209210// return the displacement211if (p_displacement) {212(*p_displacement) = (int) PTRDIFF_BYTES(pc, start_of_procedure);213}214215pc2++;216} else {217// return -1 for displacement218if (p_displacement) {219(*p_displacement) = -1;220}221}222223if (tb->tb.int_hndl == TRUE)224pc2++;225226if (tb->tb.has_ctl == TRUE)227pc2 += (*pc2) + 1; // don't care228229CHECK_POINTER_READABLE(pc2)230231//232// return function name if it exists.233//234if (p_name && namelen > 0) {235if (tb->tb.name_present) {236// Copy name from text because it may not be zero terminated.237const short l = MIN2<short>(*((short*)pc2), namelen - 1);238// Be very careful.239int i = 0; char* const p = (char*)pc2 + sizeof(short);240while (i < l && os::is_readable_pointer(p + i)) {241p_name[i] = p[i];242i++;243}244p_name[i] = '\0';245246// If it is a C++ name, try and demangle it using the Demangle interface (see demangle.h).247#ifndef DISABLE_DEMANGLE248if (demangle) {249char* rest;250Name* const name = Demangle(p_name, rest);251if (name) {252const char* const demangled_name = name->Text();253if (demangled_name) {254strncpy(p_name, demangled_name, namelen-1);255p_name[namelen-1] = '\0';256}257delete name;258}259}260#endif261} else {262strncpy(p_name, "<nameless function>", namelen-1);263p_name[namelen-1] = '\0';264}265}266267// Return traceback table, if user wants it.268if (p_tb) {269(*p_tb) = tb;270}271272return true;273274}275276bool AixSymbols::get_module_name(address pc,277char* p_name, size_t namelen) {278279if (p_name && namelen > 0) {280p_name[0] = '\0';281loaded_module_t lm;282if (LoadedLibraries::find_for_text_address(pc, &lm) != NULL) {283strncpy(p_name, lm.shortname, namelen);284p_name[namelen - 1] = '\0';285return true;286}287}288289return false;290}291292// Special implementation of dladdr for Aix based on LoadedLibraries293// Note: dladdr returns non-zero for ok, 0 for error!294// Note: dladdr is not posix, but a non-standard GNU extension. So this tries to295// fulfill the contract of dladdr on Linux (see http://linux.die.net/man/3/dladdr)296// Note: addr may be both an AIX function descriptor or a real code pointer297// to the entry of a function.298extern "C"299int dladdr(void* addr, Dl_info* info) {300301if (!addr) {302return 0;303}304305assert(info, "");306307int rc = 0;308309const char* const ZEROSTRING = "";310311// Always return a string, even if a "" one. Linux dladdr manpage312// does not say anything about returning NULL313info->dli_fname = ZEROSTRING;314info->dli_sname = ZEROSTRING;315info->dli_saddr = NULL;316317address p = (address) addr;318loaded_module_t lm;319bool found = false;320321enum { noclue, code, data } type = noclue;322323trcVerbose("dladdr(%p)...", p);324325// Note: input address may be a function. I accept both a pointer to326// the entry of a function and a pointer to the function decriptor.327// (see ppc64 ABI)328found = LoadedLibraries::find_for_text_address(p, &lm);329if (found) {330type = code;331}332333if (!found) {334// Not a pointer into any text segment. Is it a function descriptor?335const FunctionDescriptor* const pfd = (const FunctionDescriptor*) p;336p = pfd->entry();337if (p) {338found = LoadedLibraries::find_for_text_address(p, &lm);339if (found) {340type = code;341}342}343}344345if (!found) {346// Neither direct code pointer nor function descriptor. A data ptr?347p = (address)addr;348found = LoadedLibraries::find_for_data_address(p, &lm);349if (found) {350type = data;351}352}353354// If we did find the shared library this address belongs to (either355// code or data segment) resolve library path and, if possible, the356// symbol name.357if (found) {358359// No need to intern the libpath, that one is already interned one layer below.360info->dli_fname = lm.path;361362if (type == code) {363364// For code symbols resolve function name and displacement. Use365// displacement to calc start of function.366char funcname[256] = "";367int displacement = 0;368369if (AixSymbols::get_function_name(p, funcname, sizeof(funcname),370&displacement, NULL, true)) {371if (funcname[0] != '\0') {372const char* const interned = dladdr_fixed_strings.intern(funcname);373info->dli_sname = interned;374trcVerbose("... function name: %s ...", interned);375}376377// From the displacement calculate the start of the function.378if (displacement != -1) {379info->dli_saddr = p - displacement;380} else {381info->dli_saddr = p;382}383} else {384385// No traceback table found. Just assume the pointer is it.386info->dli_saddr = p;387388}389390} else if (type == data) {391392// For data symbols.393info->dli_saddr = p;394395} else {396ShouldNotReachHere();397}398399rc = 1; // success: return 1 [sic]400401}402403// sanity checks.404if (rc) {405assert(info->dli_fname, "");406assert(info->dli_sname, "");407assert(info->dli_saddr, "");408}409410return rc; // error: return 0 [sic]411412}413414/////////////////////////////////////////////////////////////////////////////415// Native callstack dumping416417// Print the traceback table for one stack frame.418static void print_tbtable (outputStream* st, const struct tbtable* p_tb) {419420if (p_tb == NULL) {421st->print("<null>");422return;423}424425switch(p_tb->tb.lang) {426case TB_C: st->print("C"); break;427case TB_FORTRAN: st->print("FORTRAN"); break;428case TB_PASCAL: st->print("PASCAL"); break;429case TB_ADA: st->print("ADA"); break;430case TB_PL1: st->print("PL1"); break;431case TB_BASIC: st->print("BASIC"); break;432case TB_LISP: st->print("LISP"); break;433case TB_COBOL: st->print("COBOL"); break;434case TB_MODULA2: st->print("MODULA2"); break;435case TB_CPLUSPLUS: st->print("C++"); break;436case TB_RPG: st->print("RPG"); break;437case TB_PL8: st->print("PL8"); break;438case TB_ASM: st->print("ASM"); break;439case TB_HPJ: st->print("HPJ"); break;440default: st->print("unknown");441}442st->print(" ");443444if (p_tb->tb.globallink) {445st->print("globallink ");446}447if (p_tb->tb.is_eprol) {448st->print("eprol ");449}450if (p_tb->tb.int_proc) {451st->print("int_proc ");452}453if (p_tb->tb.tocless) {454st->print("tocless ");455}456if (p_tb->tb.fp_present) {457st->print("fp_present ");458}459if (p_tb->tb.int_hndl) {460st->print("interrupt_handler ");461}462if (p_tb->tb.uses_alloca) {463st->print("uses_alloca ");464}465if (p_tb->tb.saves_cr) {466st->print("saves_cr ");467}468if (p_tb->tb.saves_lr) {469st->print("saves_lr ");470}471if (p_tb->tb.stores_bc) {472st->print("stores_bc ");473}474if (p_tb->tb.fixup) {475st->print("fixup ");476}477if (p_tb->tb.fpr_saved > 0) {478st->print("fpr_saved:%d ", p_tb->tb.fpr_saved);479}480if (p_tb->tb.gpr_saved > 0) {481st->print("gpr_saved:%d ", p_tb->tb.gpr_saved);482}483if (p_tb->tb.fixedparms > 0) {484st->print("fixedparms:%d ", p_tb->tb.fixedparms);485}486if (p_tb->tb.floatparms > 0) {487st->print("floatparms:%d ", p_tb->tb.floatparms);488}489if (p_tb->tb.parmsonstk > 0) {490st->print("parmsonstk:%d", p_tb->tb.parmsonstk);491}492}493494// Print information for pc (module, function, displacement, traceback table)495// on one line.496static void print_info_for_pc (outputStream* st, codeptr_t pc, char* buf,497size_t buf_size, bool demangle) {498const struct tbtable* tb = NULL;499int displacement = -1;500501if (!os::is_readable_pointer(pc)) {502st->print("(invalid)");503return;504}505506if (AixSymbols::get_module_name((address)pc, buf, buf_size)) {507st->print("%s", buf);508} else {509st->print("(unknown module)");510}511st->print("::");512if (AixSymbols::get_function_name((address)pc, buf, buf_size,513&displacement, &tb, demangle)) {514st->print("%s", buf);515} else {516st->print("(unknown function)");517}518if (displacement == -1) {519st->print("+?");520} else {521st->print("+0x%x", displacement);522}523if (tb) {524st->fill_to(64);525st->print(" (");526print_tbtable(st, tb);527st->print(")");528}529}530531static void print_stackframe(outputStream* st, stackptr_t sp, char* buf,532size_t buf_size, bool demangle) {533534stackptr_t sp2 = sp;535536// skip backchain537538sp2++;539540// skip crsave541542sp2++;543544// retrieve lrsave. That is the only info I need to get the function/displacement545546codeptr_t lrsave = (codeptr_t) *(sp2);547st->print (PTR64_FORMAT " - " PTR64_FORMAT " ", sp2, lrsave);548549if (lrsave != NULL) {550print_info_for_pc(st, lrsave, buf, buf_size, demangle);551}552553}554555// Function to check a given stack pointer against given stack limits.556static bool is_valid_stackpointer(stackptr_t sp, stackptr_t stack_base, size_t stack_size) {557if (((uintptr_t)sp) & 0x7) {558return false;559}560if (sp > stack_base) {561return false;562}563if (sp < (stackptr_t) ((address)stack_base - stack_size)) {564return false;565}566return true;567}568569// Returns true if function is a valid codepointer.570static bool is_valid_codepointer(codeptr_t p) {571if (!p) {572return false;573}574if (((uintptr_t)p) & 0x3) {575return false;576}577if (LoadedLibraries::find_for_text_address(p, NULL) == NULL) {578return false;579}580return true;581}582583// Function tries to guess if the given combination of stack pointer, stack base584// and stack size is a valid stack frame.585static bool is_valid_frame (stackptr_t p, stackptr_t stack_base, size_t stack_size) {586587if (!is_valid_stackpointer(p, stack_base, stack_size)) {588return false;589}590591// First check - the occurrence of a valid backchain pointer up the stack, followed by a592// valid codeptr, counts as a good candidate.593stackptr_t sp2 = (stackptr_t) *p;594if (is_valid_stackpointer(sp2, stack_base, stack_size) && // found a valid stack pointer in the stack...595((sp2 - p) > 6) && // ... pointing upwards and not into my frame...596is_valid_codepointer((codeptr_t)(*(sp2 + 2)))) // ... followed by a code pointer after two slots...597{598return true;599}600601return false;602}603604// Try to relocate a stack back chain in a given stack.605// Used in callstack dumping, when the backchain is broken by an overwriter606static stackptr_t try_find_backchain (stackptr_t last_known_good_frame,607stackptr_t stack_base, size_t stack_size)608{609if (!is_valid_stackpointer(last_known_good_frame, stack_base, stack_size)) {610return NULL;611}612613stackptr_t sp = last_known_good_frame;614615sp += 6; // Omit next fixed frame slots.616while (sp < stack_base) {617if (is_valid_frame(sp, stack_base, stack_size)) {618return sp;619}620sp ++;621}622623return NULL;624}625626static void decode_instructions_at_pc(const char* header,627codeptr_t pc, int num_before,628int num_after, outputStream* st) {629// TODO: PPC port Disassembler::decode(pc, 16, 16, st);630}631632633void AixNativeCallstack::print_callstack_for_context(outputStream* st, const ucontext_t* context,634bool demangle, char* buf, size_t buf_size) {635636#define MAX_CALLSTACK_DEPTH 50637638unsigned long* sp;639unsigned long* sp_last;640int frame;641642// To print the first frame, use the current value of iar:643// current entry indicated by iar (the current pc)644codeptr_t cur_iar = 0;645stackptr_t cur_sp = 0;646codeptr_t cur_rtoc = 0;647codeptr_t cur_lr = 0;648649const ucontext_t* uc = (const ucontext_t*) context;650651// fallback: use the current context652ucontext_t local_context;653if (!uc) {654st->print_cr("No context given, using current context.");655if (getcontext(&local_context) == 0) {656uc = &local_context;657} else {658st->print_cr("No context given and getcontext failed. ");659return;660}661}662663cur_iar = (codeptr_t)uc->uc_mcontext.jmp_context.iar;664cur_sp = (stackptr_t)uc->uc_mcontext.jmp_context.gpr[1];665cur_rtoc = (codeptr_t)uc->uc_mcontext.jmp_context.gpr[2];666cur_lr = (codeptr_t)uc->uc_mcontext.jmp_context.lr;667668// syntax used here:669// n -------------- <-- stack_base, stack_to670// n-1 | |671// ... | older |672// ... | frames | |673// | | | stack grows downward674// ... | younger | |675// ... | frames | V676// | |677// |------------| <-- cur_sp, current stack ptr678// | |679// | unsused |680// | stack |681// | |682// . .683// . .684// . .685// . .686// | |687// 0 -------------- <-- stack_from688//689690// Retrieve current stack base, size from the current thread. If there is none,691// retrieve it from the OS.692stackptr_t stack_base = NULL;693size_t stack_size = NULL;694{695AixMisc::stackbounds_t stackbounds;696if (!AixMisc::query_stack_bounds_for_current_thread(&stackbounds)) {697st->print_cr("Cannot retrieve stack bounds.");698return;699}700stack_base = (stackptr_t)stackbounds.base;701stack_size = stackbounds.size;702}703704st->print_cr("------ current frame:");705st->print("iar: " PTR64_FORMAT " ", p2i(cur_iar));706print_info_for_pc(st, cur_iar, buf, buf_size, demangle);707st->cr();708709if (cur_iar && os::is_readable_pointer(cur_iar)) {710decode_instructions_at_pc(711"Decoded instructions at iar:",712cur_iar, 32, 16, st);713}714715// Print out lr too, which may be interesting if we did jump to some bogus location;716// in those cases the new frame is not built up yet and the caller location is only717// preserved via lr register.718st->print("lr: " PTR64_FORMAT " ", p2i(cur_lr));719print_info_for_pc(st, cur_lr, buf, buf_size, demangle);720st->cr();721722if (cur_lr && os::is_readable_pointer(cur_lr)) {723decode_instructions_at_pc(724"Decoded instructions at lr:",725cur_lr, 32, 16, st);726}727728// Check and print sp.729st->print("sp: " PTR64_FORMAT " ", p2i(cur_sp));730if (!is_valid_stackpointer(cur_sp, stack_base, stack_size)) {731st->print("(invalid) ");732goto cleanup;733} else {734st->print("(base - 0x%X) ", PTRDIFF_BYTES(stack_base, cur_sp));735}736st->cr();737738// Check and print rtoc.739st->print("rtoc: " PTR64_FORMAT " ", p2i(cur_rtoc));740if (cur_rtoc == NULL || cur_rtoc == (codeptr_t)-1 ||741!os::is_readable_pointer(cur_rtoc)) {742st->print("(invalid)");743} else if (((uintptr_t)cur_rtoc) & 0x7) {744st->print("(unaligned)");745}746st->cr();747748st->print_cr("|---stackaddr----| |----lrsave------|: <function name>");749750///751// Walk callstack.752//753// (if no context was given, use the current stack)754sp = (unsigned long*)(*(unsigned long*)cur_sp); // Stack pointer755sp_last = cur_sp;756757frame = 0;758759while (frame < MAX_CALLSTACK_DEPTH) {760761// Check sp.762bool retry = false;763if (sp == NULL) {764// The backchain pointer was NULL. This normally means the end of the chain. But the765// stack might be corrupted, and it may be worth looking for the stack chain.766if (is_valid_stackpointer(sp_last, stack_base, stack_size) && (stack_base - 0x10) > sp_last) {767// If we are not within <guess> 0x10 stackslots of the stack base, we assume that this768// is indeed not the end of the chain but that the stack was corrupted. So lets try to769// find the end of the chain.770st->print_cr("*** back chain pointer is NULL - end of stack or broken backchain ? ***");771retry = true;772} else {773st->print_cr("*** end of backchain ***");774goto end_walk_callstack;775}776} else if (!is_valid_stackpointer(sp, stack_base, stack_size)) {777st->print_cr("*** stack pointer invalid - backchain corrupted (" PTR_FORMAT ") ***", p2i(sp));778retry = true;779} else if (sp < sp_last) {780st->print_cr("invalid stack pointer: " PTR_FORMAT " (not monotone raising)", p2i(sp));781retry = true;782}783784// If backchain is broken, try to recover, by manually scanning the stack for a pattern785// which looks like a valid stack.786if (retry) {787st->print_cr("trying to recover and find backchain...");788sp = try_find_backchain(sp_last, stack_base, stack_size);789if (sp) {790st->print_cr("found something which looks like a backchain at " PTR64_FORMAT ", after 0x%x bytes... ",791p2i(sp), PTRDIFF_BYTES(sp, sp_last));792} else {793st->print_cr("did not find a backchain, giving up.");794goto end_walk_callstack;795}796}797798// Print stackframe.799print_stackframe(st, sp, buf, buf_size, demangle);800st->cr();801frame ++;802803// Next stack frame and link area.804sp_last = sp;805sp = (unsigned long*)(*sp);806}807808// Prevent endless loops in case of invalid callstacks.809if (frame == MAX_CALLSTACK_DEPTH) {810st->print_cr("...(stopping after %d frames.", MAX_CALLSTACK_DEPTH);811}812813end_walk_callstack:814815st->print_cr("-----------------------");816817cleanup:818819return;820821}822823824bool AixMisc::query_stack_bounds_for_current_thread(stackbounds_t* out) {825826// Information about this api can be found (a) in the pthread.h header and827// (b) in http://publib.boulder.ibm.com/infocenter/pseries/v5r3/index.jsp?topic=/com.ibm.aix.basetechref/doc/basetrf1/pthread_getthrds_np.htm828//829// The use of this API to find out the current stack is kind of undefined.830// But after a lot of tries and asking IBM about it, I concluded that it is safe831// enough for cases where I let the pthread library create its stacks. For cases832// where I create an own stack and pass this to pthread_create, it seems not to833// work (the returned stack size in that case is 0).834835pthread_t tid = pthread_self();836struct __pthrdsinfo pinfo;837char dummy[1]; // Just needed to satisfy pthread_getthrds_np.838int dummy_size = sizeof(dummy);839840memset(&pinfo, 0, sizeof(pinfo));841842const int rc = pthread_getthrds_np(&tid, PTHRDSINFO_QUERY_ALL, &pinfo,843sizeof(pinfo), dummy, &dummy_size);844845if (rc != 0) {846fprintf(stderr, "pthread_getthrds_np failed (%d)\n", rc);847fflush(stdout);848return false;849}850851// The following may happen when invoking pthread_getthrds_np on a pthread852// running on a user provided stack (when handing down a stack to pthread853// create, see pthread_attr_setstackaddr).854// Not sure what to do then.855if (pinfo.__pi_stackend == NULL || pinfo.__pi_stackaddr == NULL) {856fprintf(stderr, "pthread_getthrds_np - invalid values\n");857fflush(stdout);858return false;859}860861// Note: we get three values from pthread_getthrds_np:862// __pi_stackaddr, __pi_stacksize, __pi_stackend863//864// high addr --------------------- base, high865//866// | pthread internal data, like ~2K867// |868// | --------------------- __pi_stackend (usually not page aligned, (xxxxF890))869// |870// |871// |872// |873// |874// |875// | --------------------- (__pi_stackend - __pi_stacksize)876// |877// | padding to align the following AIX guard pages, if enabled.878// |879// V --------------------- __pi_stackaddr low, base - size880//881// low addr AIX guard pages, if enabled (AIXTHREAD_GUARDPAGES > 0)882//883884out->base = (address)pinfo.__pi_stackend;885address low = (address)pinfo.__pi_stackaddr;886out->size = out->base - low;887return true;888889}890891892893894895896