src/os/aix/vm/porting_aix.cpp

Wed, 27 Apr 2016 01:25:04 +0800

author
aoqi
date
Wed, 27 Apr 2016 01:25:04 +0800
changeset 0
f90c822e73f8
permissions
-rw-r--r--

Initial load
http://hg.openjdk.java.net/jdk8u/jdk8u/hotspot/
changeset: 6782:28b50d07f6f8
tag: jdk8u25-b17

aoqi@0 1 /*
aoqi@0 2 * Copyright 2012, 2013 SAP AG. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation.
aoqi@0 8 *
aoqi@0 9 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 12 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 13 * accompanied this code).
aoqi@0 14 *
aoqi@0 15 * You should have received a copy of the GNU General Public License version
aoqi@0 16 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 18 *
aoqi@0 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 20 * or visit www.oracle.com if you need additional information or have any
aoqi@0 21 * questions.
aoqi@0 22 *
aoqi@0 23 */
aoqi@0 24
aoqi@0 25 #include "asm/assembler.hpp"
aoqi@0 26 #include "loadlib_aix.hpp"
aoqi@0 27 #include "porting_aix.hpp"
aoqi@0 28 #include "utilities/debug.hpp"
aoqi@0 29
aoqi@0 30 #include <demangle.h>
aoqi@0 31 #include <sys/debug.h>
aoqi@0 32
aoqi@0 33 //////////////////////////////////
aoqi@0 34 // Provide implementation for dladdr based on LoadedLibraries pool and
aoqi@0 35 // traceback table scan (see getFuncName).
aoqi@0 36
aoqi@0 37 // Search traceback table in stack,
aoqi@0 38 // return procedure name from trace back table.
aoqi@0 39 #define MAX_FUNC_SEARCH_LEN 0x10000
aoqi@0 40 // Any PC below this value is considered toast.
aoqi@0 41 #define MINIMUM_VALUE_FOR_PC ((unsigned int*)0x1024)
aoqi@0 42
aoqi@0 43 #define PTRDIFF_BYTES(p1,p2) (((ptrdiff_t)p1) - ((ptrdiff_t)p2))
aoqi@0 44
aoqi@0 45 // Align a pointer without having to cast.
aoqi@0 46 inline char* align_ptr_up(char* ptr, intptr_t alignment) {
aoqi@0 47 return (char*) align_size_up((intptr_t)ptr, alignment);
aoqi@0 48 }
aoqi@0 49
aoqi@0 50 // Trace if verbose to tty.
aoqi@0 51 // I use these now instead of the Xtrace system because the latter is
aoqi@0 52 // not available at init time, hence worthless. Until we fix this, all
aoqi@0 53 // tracing here is done with -XX:+Verbose.
aoqi@0 54 #define trcVerbose(fmt, ...) { \
aoqi@0 55 if (Verbose) { \
aoqi@0 56 fprintf(stderr, fmt, ##__VA_ARGS__); \
aoqi@0 57 fputc('\n', stderr); fflush(stderr); \
aoqi@0 58 } \
aoqi@0 59 }
aoqi@0 60 #define ERRBYE(s) { trcVerbose(s); return -1; }
aoqi@0 61
aoqi@0 62 // Unfortunately, the interface of dladdr makes the implementator
aoqi@0 63 // responsible for maintaining memory for function name/library
aoqi@0 64 // name. I guess this is because most OS's keep those values as part
aoqi@0 65 // of the mapped executable image ready to use. On AIX, this doesn't
aoqi@0 66 // work, so I have to keep the returned strings. For now, I do this in
aoqi@0 67 // a primitive string map. Should this turn out to be a performance
aoqi@0 68 // problem, a better hashmap has to be used.
aoqi@0 69 class fixed_strings {
aoqi@0 70 struct node {
aoqi@0 71 char* v;
aoqi@0 72 node* next;
aoqi@0 73 };
aoqi@0 74
aoqi@0 75 node* first;
aoqi@0 76
aoqi@0 77 public:
aoqi@0 78
aoqi@0 79 fixed_strings() : first(0) {}
aoqi@0 80 ~fixed_strings() {
aoqi@0 81 node* n = first;
aoqi@0 82 while (n) {
aoqi@0 83 node* p = n;
aoqi@0 84 n = n->next;
aoqi@0 85 free(p->v);
aoqi@0 86 delete p;
aoqi@0 87 }
aoqi@0 88 }
aoqi@0 89
aoqi@0 90 char* intern(const char* s) {
aoqi@0 91 for (node* n = first; n; n = n->next) {
aoqi@0 92 if (strcmp(n->v, s) == 0) {
aoqi@0 93 return n->v;
aoqi@0 94 }
aoqi@0 95 }
aoqi@0 96 node* p = new node;
aoqi@0 97 p->v = strdup(s);
aoqi@0 98 p->next = first;
aoqi@0 99 first = p;
aoqi@0 100 return p->v;
aoqi@0 101 }
aoqi@0 102 };
aoqi@0 103
aoqi@0 104 static fixed_strings dladdr_fixed_strings;
aoqi@0 105
aoqi@0 106 // Given a code pointer, returns the function name and the displacement.
aoqi@0 107 // Function looks for the traceback table at the end of the function.
aoqi@0 108 extern "C" int getFuncName(
aoqi@0 109 codeptr_t pc, // [in] program counter
aoqi@0 110 char* p_name, size_t namelen, // [out] optional: function name ("" if not available)
aoqi@0 111 int* p_displacement, // [out] optional: displacement (-1 if not available)
aoqi@0 112 const struct tbtable** p_tb, // [out] optional: ptr to traceback table to get further
aoqi@0 113 // information (NULL if not available)
aoqi@0 114 char* p_errmsg, size_t errmsglen // [out] optional: user provided buffer for error messages
aoqi@0 115 ) {
aoqi@0 116 struct tbtable* tb = 0;
aoqi@0 117 unsigned int searchcount = 0;
aoqi@0 118
aoqi@0 119 // initialize output parameters
aoqi@0 120 if (p_name && namelen > 0) {
aoqi@0 121 *p_name = '\0';
aoqi@0 122 }
aoqi@0 123 if (p_errmsg && errmsglen > 0) {
aoqi@0 124 *p_errmsg = '\0';
aoqi@0 125 }
aoqi@0 126 if (p_displacement) {
aoqi@0 127 *p_displacement = -1;
aoqi@0 128 }
aoqi@0 129 if (p_tb) {
aoqi@0 130 *p_tb = NULL;
aoqi@0 131 }
aoqi@0 132
aoqi@0 133 // weed out obvious bogus states
aoqi@0 134 if (pc < MINIMUM_VALUE_FOR_PC) {
aoqi@0 135 ERRBYE("invalid program counter");
aoqi@0 136 }
aoqi@0 137
aoqi@0 138 codeptr_t pc2 = pc;
aoqi@0 139
aoqi@0 140 // make sure the pointer is word aligned.
aoqi@0 141 pc2 = (codeptr_t) align_ptr_up((char*)pc2, 4);
aoqi@0 142
aoqi@0 143 // Find start of traceback table.
aoqi@0 144 // (starts after code, is marked by word-aligned (32bit) zeros)
aoqi@0 145 while ((*pc2 != NULL) && (searchcount++ < MAX_FUNC_SEARCH_LEN)) {
aoqi@0 146 pc2++;
aoqi@0 147 }
aoqi@0 148 if (*pc2 != 0) {
aoqi@0 149 ERRBYE("could not find traceback table within 5000 bytes of program counter");
aoqi@0 150 }
aoqi@0 151 //
aoqi@0 152 // Set up addressability to the traceback table
aoqi@0 153 //
aoqi@0 154 tb = (struct tbtable*) (pc2 + 1);
aoqi@0 155
aoqi@0 156 // Is this really a traceback table? No way to be sure but
aoqi@0 157 // some indicators we can check.
aoqi@0 158 if (tb->tb.lang >= 0xf && tb->tb.lang <= 0xfb) {
aoqi@0 159 // Language specifiers, go from 0 (C) to 14 (Objective C).
aoqi@0 160 // According to spec, 0xf-0xfa reserved, 0xfb-0xff reserved for ibm.
aoqi@0 161 ERRBYE("not a traceback table");
aoqi@0 162 }
aoqi@0 163
aoqi@0 164 // Existence of fields in the tbtable extension are contingent upon
aoqi@0 165 // specific fields in the base table. Check for their existence so
aoqi@0 166 // that we can address the function name if it exists.
aoqi@0 167 pc2 = (codeptr_t) tb +
aoqi@0 168 sizeof(struct tbtable_short)/sizeof(int);
aoqi@0 169 if (tb->tb.fixedparms != 0 || tb->tb.floatparms != 0)
aoqi@0 170 pc2++;
aoqi@0 171
aoqi@0 172 if (tb->tb.has_tboff == TRUE) {
aoqi@0 173
aoqi@0 174 // I want to know the displacement
aoqi@0 175 const unsigned int tb_offset = *pc2;
aoqi@0 176 codeptr_t start_of_procedure =
aoqi@0 177 (codeptr_t)(((char*)tb) - 4 - tb_offset); // (-4 to omit leading 0000)
aoqi@0 178
aoqi@0 179 // Weed out the cases where we did find the wrong traceback table.
aoqi@0 180 if (pc < start_of_procedure) {
aoqi@0 181 ERRBYE("could not find (the real) traceback table within 5000 bytes of program counter");
aoqi@0 182 }
aoqi@0 183
aoqi@0 184 // return the displacement
aoqi@0 185 if (p_displacement) {
aoqi@0 186 (*p_displacement) = (int) PTRDIFF_BYTES(pc, start_of_procedure);
aoqi@0 187 }
aoqi@0 188
aoqi@0 189 pc2++;
aoqi@0 190 } else {
aoqi@0 191 // return -1 for displacement
aoqi@0 192 if (p_displacement) {
aoqi@0 193 (*p_displacement) = -1;
aoqi@0 194 }
aoqi@0 195 }
aoqi@0 196
aoqi@0 197 if (tb->tb.int_hndl == TRUE)
aoqi@0 198 pc2++;
aoqi@0 199
aoqi@0 200 if (tb->tb.has_ctl == TRUE)
aoqi@0 201 pc2 += (*pc2) + 1; // don't care
aoqi@0 202
aoqi@0 203 //
aoqi@0 204 // return function name if it exists.
aoqi@0 205 //
aoqi@0 206 if (p_name && namelen > 0) {
aoqi@0 207 if (tb->tb.name_present) {
aoqi@0 208 char buf[256];
aoqi@0 209 const short l = MIN2<short>(*((short*)pc2), sizeof(buf) - 1);
aoqi@0 210 memcpy(buf, (char*)pc2 + sizeof(short), l);
aoqi@0 211 buf[l] = '\0';
aoqi@0 212
aoqi@0 213 p_name[0] = '\0';
aoqi@0 214
aoqi@0 215 // If it is a C++ name, try and demangle it using the Demangle interface (see demangle.h).
aoqi@0 216 char* rest;
aoqi@0 217 Name* const name = Demangle(buf, rest);
aoqi@0 218 if (name) {
aoqi@0 219 const char* const demangled_name = name->Text();
aoqi@0 220 if (demangled_name) {
aoqi@0 221 strncpy(p_name, demangled_name, namelen-1);
aoqi@0 222 p_name[namelen-1] = '\0';
aoqi@0 223 }
aoqi@0 224 delete name;
aoqi@0 225 }
aoqi@0 226
aoqi@0 227 // Fallback: if demangling did not work, just provide the unmangled name.
aoqi@0 228 if (p_name[0] == '\0') {
aoqi@0 229 strncpy(p_name, buf, namelen-1);
aoqi@0 230 p_name[namelen-1] = '\0';
aoqi@0 231 }
aoqi@0 232
aoqi@0 233 } else {
aoqi@0 234 strncpy(p_name, "<nameless function>", namelen-1);
aoqi@0 235 p_name[namelen-1] = '\0';
aoqi@0 236 }
aoqi@0 237 }
aoqi@0 238 // Return traceback table, if user wants it.
aoqi@0 239 if (p_tb) {
aoqi@0 240 (*p_tb) = tb;
aoqi@0 241 }
aoqi@0 242
aoqi@0 243 return 0;
aoqi@0 244 }
aoqi@0 245
aoqi@0 246 // Special implementation of dladdr for Aix based on LoadedLibraries
aoqi@0 247 // Note: dladdr returns non-zero for ok, 0 for error!
aoqi@0 248 // Note: dladdr is not posix, but a non-standard GNU extension. So this tries to
aoqi@0 249 // fulfill the contract of dladdr on Linux (see http://linux.die.net/man/3/dladdr)
aoqi@0 250 // Note: addr may be both an AIX function descriptor or a real code pointer
aoqi@0 251 // to the entry of a function.
aoqi@0 252 extern "C"
aoqi@0 253 int dladdr(void* addr, Dl_info* info) {
aoqi@0 254
aoqi@0 255 if (!addr) {
aoqi@0 256 return 0;
aoqi@0 257 }
aoqi@0 258
aoqi@0 259 assert(info, "");
aoqi@0 260
aoqi@0 261 int rc = 0;
aoqi@0 262
aoqi@0 263 const char* const ZEROSTRING = "";
aoqi@0 264
aoqi@0 265 // Always return a string, even if a "" one. Linux dladdr manpage
aoqi@0 266 // does not say anything about returning NULL
aoqi@0 267 info->dli_fname = ZEROSTRING;
aoqi@0 268 info->dli_sname = ZEROSTRING;
aoqi@0 269 info->dli_saddr = NULL;
aoqi@0 270
aoqi@0 271 address p = (address) addr;
aoqi@0 272 const LoadedLibraryModule* lib = NULL;
aoqi@0 273
aoqi@0 274 enum { noclue, code, data } type = noclue;
aoqi@0 275
aoqi@0 276 trcVerbose("dladdr(%p)...", p);
aoqi@0 277
aoqi@0 278 // Note: input address may be a function. I accept both a pointer to
aoqi@0 279 // the entry of a function and a pointer to the function decriptor.
aoqi@0 280 // (see ppc64 ABI)
aoqi@0 281 lib = LoadedLibraries::find_for_text_address(p);
aoqi@0 282 if (lib) {
aoqi@0 283 type = code;
aoqi@0 284 }
aoqi@0 285
aoqi@0 286 if (!lib) {
aoqi@0 287 // Not a pointer into any text segment. Is it a function descriptor?
aoqi@0 288 const FunctionDescriptor* const pfd = (const FunctionDescriptor*) p;
aoqi@0 289 p = pfd->entry();
aoqi@0 290 if (p) {
aoqi@0 291 lib = LoadedLibraries::find_for_text_address(p);
aoqi@0 292 if (lib) {
aoqi@0 293 type = code;
aoqi@0 294 }
aoqi@0 295 }
aoqi@0 296 }
aoqi@0 297
aoqi@0 298 if (!lib) {
aoqi@0 299 // Neither direct code pointer nor function descriptor. A data ptr?
aoqi@0 300 p = (address)addr;
aoqi@0 301 lib = LoadedLibraries::find_for_data_address(p);
aoqi@0 302 if (lib) {
aoqi@0 303 type = data;
aoqi@0 304 }
aoqi@0 305 }
aoqi@0 306
aoqi@0 307 // If we did find the shared library this address belongs to (either
aoqi@0 308 // code or data segment) resolve library path and, if possible, the
aoqi@0 309 // symbol name.
aoqi@0 310 if (lib) {
aoqi@0 311 const char* const interned_libpath =
aoqi@0 312 dladdr_fixed_strings.intern(lib->get_fullpath());
aoqi@0 313 if (interned_libpath) {
aoqi@0 314 info->dli_fname = interned_libpath;
aoqi@0 315 }
aoqi@0 316
aoqi@0 317 if (type == code) {
aoqi@0 318
aoqi@0 319 // For code symbols resolve function name and displacement. Use
aoqi@0 320 // displacement to calc start of function.
aoqi@0 321 char funcname[256] = "";
aoqi@0 322 int displacement = 0;
aoqi@0 323
aoqi@0 324 if (getFuncName((codeptr_t) p, funcname, sizeof(funcname), &displacement,
aoqi@0 325 NULL, NULL, 0) == 0) {
aoqi@0 326 if (funcname[0] != '\0') {
aoqi@0 327 const char* const interned = dladdr_fixed_strings.intern(funcname);
aoqi@0 328 info->dli_sname = interned;
aoqi@0 329 trcVerbose("... function name: %s ...", interned);
aoqi@0 330 }
aoqi@0 331
aoqi@0 332 // From the displacement calculate the start of the function.
aoqi@0 333 if (displacement != -1) {
aoqi@0 334 info->dli_saddr = p - displacement;
aoqi@0 335 } else {
aoqi@0 336 info->dli_saddr = p;
aoqi@0 337 }
aoqi@0 338 } else {
aoqi@0 339
aoqi@0 340 // No traceback table found. Just assume the pointer is it.
aoqi@0 341 info->dli_saddr = p;
aoqi@0 342
aoqi@0 343 }
aoqi@0 344
aoqi@0 345 } else if (type == data) {
aoqi@0 346
aoqi@0 347 // For data symbols.
aoqi@0 348 info->dli_saddr = p;
aoqi@0 349
aoqi@0 350 } else {
aoqi@0 351 ShouldNotReachHere();
aoqi@0 352 }
aoqi@0 353
aoqi@0 354 rc = 1; // success: return 1 [sic]
aoqi@0 355
aoqi@0 356 }
aoqi@0 357
aoqi@0 358 // sanity checks.
aoqi@0 359 if (rc) {
aoqi@0 360 assert(info->dli_fname, "");
aoqi@0 361 assert(info->dli_sname, "");
aoqi@0 362 assert(info->dli_saddr, "");
aoqi@0 363 }
aoqi@0 364
aoqi@0 365 return rc; // error: return 0 [sic]
aoqi@0 366
aoqi@0 367 }

mercurial