rev 9211 : 8140645: Recent Developments for AIX
Summary: Port recent developments from SAP for AIX to the OpenJDK

   1 /*
   2  * Copyright 2012, 2013 SAP AG. 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 "memory/allocation.hpp"
  27 #include "memory/allocation.inline.hpp"
  28 #include "runtime/os.hpp"
  29 #include "loadlib_aix.hpp"
  30 #include "porting_aix.hpp"
  31 #include "utilities/debug.hpp"
  32 



  33 #include <demangle.h>
  34 #include <sys/debug.h>
  35 
  36 //////////////////////////////////
  37 // Provide implementation for dladdr based on LoadedLibraries pool and
  38 // traceback table scan (see getFuncName).
  39 
  40 // Search traceback table in stack,
  41 // return procedure name from trace back table.
  42 #define MAX_FUNC_SEARCH_LEN 0x10000
  43 // Any PC below this value is considered toast.
  44 #define MINIMUM_VALUE_FOR_PC ((unsigned int*)0x1024)
  45 
  46 #define PTRDIFF_BYTES(p1,p2) (((ptrdiff_t)p1) - ((ptrdiff_t)p2))
  47 
  48 // Align a pointer without having to cast.
  49 inline char* align_ptr_up(char* ptr, intptr_t alignment) {
  50   return (char*) align_size_up((intptr_t)ptr, alignment);
  51 }
  52 
  53 // Trace if verbose to tty.
  54 // I use these now instead of the Xtrace system because the latter is
  55 // not available at init time, hence worthless. Until we fix this, all
  56 // tracing here is done with -XX:+Verbose.
  57 #define trcVerbose(fmt, ...) { \
  58   if (Verbose) { \
  59     fprintf(stderr, fmt, ##__VA_ARGS__); \
  60     fputc('\n', stderr); fflush(stderr); \
  61   } \
  62 }
  63 #define ERRBYE(s) { trcVerbose(s); return -1; }
  64 
  65 // Unfortunately, the interface of dladdr makes the implementator
  66 // responsible for maintaining memory for function name/library
  67 // name. I guess this is because most OS's keep those values as part
  68 // of the mapped executable image ready to use. On AIX, this doesn't
  69 // work, so I have to keep the returned strings. For now, I do this in
  70 // a primitive string map. Should this turn out to be a performance
  71 // problem, a better hashmap has to be used.
  72 class fixed_strings {
  73   struct node : public CHeapObj<mtInternal> {
  74     char* v;
  75     node* next;
  76   };
  77 
  78   node* first;
  79 
  80   public:
  81 
  82   fixed_strings() : first(0) {}
  83   ~fixed_strings() {
  84     node* n = first;
  85     while (n) {
  86       node* p = n;
  87       n = n->next;
  88       os::free(p->v);
  89       delete p;
  90     }
  91   }
  92 
  93   char* intern(const char* s) {
  94     for (node* n = first; n; n = n->next) {
  95       if (strcmp(n->v, s) == 0) {
  96         return n->v;
  97       }
  98     }
  99     node* p = new node;
 100     p->v = os::strdup_check_oom(s);
 101     p->next = first;
 102     first = p;
 103     return p->v;
 104   }
 105 };
 106 
 107 static fixed_strings dladdr_fixed_strings;
 108 
 109 // Given a code pointer, returns the function name and the displacement.
 110 // Function looks for the traceback table at the end of the function.
 111 extern "C" int getFuncName(
 112     codeptr_t pc,                    // [in] program counter
 113     char* p_name, size_t namelen,    // [out] optional: function name ("" if not available)
 114     int* p_displacement,             // [out] optional: displacement (-1 if not available)
 115     const struct tbtable** p_tb,     // [out] optional: ptr to traceback table to get further
 116                                      //                 information (NULL if not available)
 117     char* p_errmsg, size_t errmsglen,// [out] optional: user provided buffer for error messages
 118     bool demangle                    // [in] whether to demangle the name
 119   ) {
 120   struct tbtable* tb = 0;
 121   unsigned int searchcount = 0;
 122 
 123   // initialize output parameters
 124   if (p_name && namelen > 0) {
 125     *p_name = '\0';
 126   }
 127   if (p_errmsg && errmsglen > 0) {
 128     *p_errmsg = '\0';
 129   }
 130   if (p_displacement) {
 131     *p_displacement = -1;
 132   }
 133   if (p_tb) {
 134     *p_tb = NULL;
 135   }
 136 
 137   // weed out obvious bogus states
 138   if (pc < MINIMUM_VALUE_FOR_PC) {
 139     ERRBYE("invalid program counter");
 140   }
 141 

















 142   codeptr_t pc2 = pc;
 143 
 144   // make sure the pointer is word aligned.
 145   pc2 = (codeptr_t) align_ptr_up((char*)pc2, 4);

 146 
 147   // Find start of traceback table.
 148   // (starts after code, is marked by word-aligned (32bit) zeros)
 149   while ((*pc2 != NULL) && (searchcount++ < MAX_FUNC_SEARCH_LEN)) {

 150     pc2++;
 151   }
 152   if (*pc2 != 0) {
 153     ERRBYE("could not find traceback table within 5000 bytes of program counter");
 154   }
 155   //
 156   // Set up addressability to the traceback table
 157   //
 158   tb = (struct tbtable*) (pc2 + 1);
 159 
 160   // Is this really a traceback table? No way to be sure but
 161   // some indicators we can check.
 162   if (tb->tb.lang >= 0xf && tb->tb.lang <= 0xfb) {
 163     // Language specifiers, go from 0 (C) to 14 (Objective C).
 164     // According to spec, 0xf-0xfa reserved, 0xfb-0xff reserved for ibm.
 165     ERRBYE("not a traceback table");
 166   }
 167 
 168   // Existence of fields in the tbtable extension are contingent upon
 169   // specific fields in the base table.  Check for their existence so
 170   // that we can address the function name if it exists.
 171   pc2 = (codeptr_t) tb +
 172     sizeof(struct tbtable_short)/sizeof(int);
 173   if (tb->tb.fixedparms != 0 || tb->tb.floatparms != 0)
 174     pc2++;
 175 


 176   if (tb->tb.has_tboff == TRUE) {
 177 
 178     // I want to know the displacement
 179     const unsigned int tb_offset = *pc2;
 180     codeptr_t start_of_procedure =
 181     (codeptr_t)(((char*)tb) - 4 - tb_offset);  // (-4 to omit leading 0000)
 182 
 183     // Weed out the cases where we did find the wrong traceback table.
 184     if (pc < start_of_procedure) {
 185       ERRBYE("could not find (the real) traceback table within 5000 bytes of program counter");
 186     }
 187 
 188     // return the displacement
 189     if (p_displacement) {
 190       (*p_displacement) = (int) PTRDIFF_BYTES(pc, start_of_procedure);
 191     }
 192 
 193     pc2++;
 194   } else {
 195     // return -1 for displacement
 196     if (p_displacement) {
 197       (*p_displacement) = -1;
 198     }
 199   }
 200 
 201   if (tb->tb.int_hndl == TRUE)
 202     pc2++;
 203 
 204   if (tb->tb.has_ctl == TRUE)
 205     pc2 += (*pc2) + 1; // don't care
 206 


 207   //
 208   // return function name if it exists.
 209   //
 210   if (p_name && namelen > 0) {
 211     if (tb->tb.name_present) {


 212       char buf[256];
 213       const short l = MIN2<short>(*((short*)pc2), sizeof(buf) - 1);
 214       memcpy(buf, (char*)pc2 + sizeof(short), l);
 215       buf[l] = '\0';





 216 
 217       p_name[0] = '\0';
 218 
 219       // If it is a C++ name, try and demangle it using the Demangle interface (see demangle.h).
 220       if (demangle) {
 221         char* rest;
 222         Name* const name = Demangle(buf, rest);
 223         if (name) {
 224           const char* const demangled_name = name->Text();
 225           if (demangled_name) {
 226             strncpy(p_name, demangled_name, namelen-1);
 227             p_name[namelen-1] = '\0';
 228           }
 229           delete name;
 230         }
 231       }
 232 
 233       // Fallback: if demangling did not work, just provide the unmangled name.
 234       if (p_name[0] == '\0') {
 235         strncpy(p_name, buf, namelen-1);
 236         p_name[namelen-1] = '\0';
 237       }
 238 
 239     } else {
 240       strncpy(p_name, "<nameless function>", namelen-1);
 241       p_name[namelen-1] = '\0';
 242     }
 243   }
 244   // Return traceback table, if user wants it.
 245   if (p_tb) {
 246     (*p_tb) = tb;
 247   }
 248 
 249   return 0;
 250 }
 251 
 252 // Special implementation of dladdr for Aix based on LoadedLibraries
 253 // Note: dladdr returns non-zero for ok, 0 for error!
 254 // Note: dladdr is not posix, but a non-standard GNU extension. So this tries to
 255 //   fulfill the contract of dladdr on Linux (see http://linux.die.net/man/3/dladdr)
 256 // Note: addr may be both an AIX function descriptor or a real code pointer
 257 //   to the entry of a function.
 258 extern "C"
 259 int dladdr(void* addr, Dl_info* info) {
 260 
 261   if (!addr) {
 262     return 0;
 263   }
 264 
 265   assert(info, "");
 266 
 267   int rc = 0;
 268 
 269   const char* const ZEROSTRING = "";
 270 
 271   // Always return a string, even if a "" one. Linux dladdr manpage
 272   // does not say anything about returning NULL
 273   info->dli_fname = ZEROSTRING;
 274   info->dli_sname = ZEROSTRING;
 275   info->dli_saddr = NULL;
 276 
 277   address p = (address) addr;
 278   const LoadedLibraryModule* lib = NULL;

 279 
 280   enum { noclue, code, data } type = noclue;
 281 
 282   trcVerbose("dladdr(%p)...", p);
 283 
 284   // Note: input address may be a function. I accept both a pointer to
 285   // the entry of a function and a pointer to the function decriptor.
 286   // (see ppc64 ABI)
 287   lib = LoadedLibraries::find_for_text_address(p);
 288   if (lib) {
 289     type = code;
 290   }
 291 
 292   if (!lib) {
 293     // Not a pointer into any text segment. Is it a function descriptor?
 294     const FunctionDescriptor* const pfd = (const FunctionDescriptor*) p;
 295     p = pfd->entry();
 296     if (p) {
 297       lib = LoadedLibraries::find_for_text_address(p);
 298       if (lib) {
 299         type = code;
 300       }
 301     }
 302   }
 303 
 304   if (!lib) {
 305     // Neither direct code pointer nor function descriptor. A data ptr?
 306     p = (address)addr;
 307     lib = LoadedLibraries::find_for_data_address(p);
 308     if (lib) {
 309       type = data;
 310     }
 311   }
 312 
 313   // If we did find the shared library this address belongs to (either
 314   // code or data segment) resolve library path and, if possible, the
 315   // symbol name.
 316   if (lib) {
 317     const char* const interned_libpath =
 318       dladdr_fixed_strings.intern(lib->get_fullpath());
 319     if (interned_libpath) {
 320       info->dli_fname = interned_libpath;
 321     }
 322 
 323     if (type == code) {
 324 
 325       // For code symbols resolve function name and displacement. Use
 326       // displacement to calc start of function.
 327       char funcname[256] = "";
 328       int displacement = 0;
 329 
 330       if (getFuncName((codeptr_t) p, funcname, sizeof(funcname), &displacement,
 331                       NULL, NULL, 0, true /* demangle */) == 0) {
 332         if (funcname[0] != '\0') {
 333           const char* const interned = dladdr_fixed_strings.intern(funcname);
 334           info->dli_sname = interned;
 335           trcVerbose("... function name: %s ...", interned);
 336         }
 337 
 338         // From the displacement calculate the start of the function.
 339         if (displacement != -1) {
 340           info->dli_saddr = p - displacement;
 341         } else {
 342           info->dli_saddr = p;
 343         }
 344       } else {
 345 
 346         // No traceback table found. Just assume the pointer is it.
 347         info->dli_saddr = p;
 348 
 349       }
 350 
 351     } else if (type == data) {
 352 
 353       // For data symbols.
 354       info->dli_saddr = p;
 355 
 356     } else {
 357       ShouldNotReachHere();
 358     }
 359 
 360     rc = 1; // success: return 1 [sic]
 361 
 362   }
 363 
 364   // sanity checks.
 365   if (rc) {
 366     assert(info->dli_fname, "");
 367     assert(info->dli_sname, "");
 368     assert(info->dli_saddr, "");
 369   }
 370 
 371   return rc; // error: return 0 [sic]
 372 
 373 }

--- EOF ---