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