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