* Copyright 2012, 2013 SAP AG. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*
*/
#include "asm/assembler.hpp"
#include "loadlib_aix.hpp"
#include "porting_aix.hpp"
#include "utilities/debug.hpp"
#include <demangle.h>
#include <sys/debug.h>
#define MAX_FUNC_SEARCH_LEN 0x10000
#define MINIMUM_VALUE_FOR_PC ((unsigned int*)0x1024)
#define PTRDIFF_BYTES(p1,p2) (((ptrdiff_t)p1) - ((ptrdiff_t)p2))
inline char* align_ptr_up(char* ptr, intptr_t alignment) {
return (char*) align_size_up((intptr_t)ptr, alignment);
}
#define trcVerbose(fmt, ...) { \
if (Verbose) { \
fprintf(stderr, fmt, ##__VA_ARGS__); \
fputc('\n', stderr); fflush(stderr); \
} \
}
#define ERRBYE(s) { trcVerbose(s); return -1; }
class fixed_strings {
struct node {
char* v;
node* next;
};
node* first;
public:
fixed_strings() : first(0) {}
~fixed_strings() {
node* n = first;
while (n) {
node* p = n;
n = n->next;
free(p->v);
delete p;
}
}
char* intern(const char* s) {
for (node* n = first; n; n = n->next) {
if (strcmp(n->v, s) == 0) {
return n->v;
}
}
node* p = new node;
p->v = strdup(s);
p->next = first;
first = p;
return p->v;
}
};
static fixed_strings dladdr_fixed_strings;
extern "C" int getFuncName(
codeptr_t pc,
char* p_name, size_t namelen,
int* p_displacement,
const struct tbtable** p_tb,
char* p_errmsg, size_t errmsglen
) {
struct tbtable* tb = 0;
unsigned int searchcount = 0;
if (p_name && namelen > 0) {
*p_name = '\0';
}
if (p_errmsg && errmsglen > 0) {
*p_errmsg = '\0';
}
if (p_displacement) {
*p_displacement = -1;
}
if (p_tb) {
*p_tb = NULL;
}
if (pc < MINIMUM_VALUE_FOR_PC) {
ERRBYE("invalid program counter");
}
codeptr_t pc2 = pc;
pc2 = (codeptr_t) align_ptr_up((char*)pc2, 4);
while ((*pc2 != NULL) && (searchcount++ < MAX_FUNC_SEARCH_LEN)) {
pc2++;
}
if (*pc2 != 0) {
ERRBYE("could not find traceback table within 5000 bytes of program counter");
}
tb = (struct tbtable*) (pc2 + 1);
if (tb->tb.lang >= 0xf && tb->tb.lang <= 0xfb) {
ERRBYE("not a traceback table");
}
pc2 = (codeptr_t) tb +
sizeof(struct tbtable_short)/sizeof(int);
if (tb->tb.fixedparms != 0 || tb->tb.floatparms != 0)
pc2++;
if (tb->tb.has_tboff == TRUE) {
const unsigned int tb_offset = *pc2;
codeptr_t start_of_procedure =
(codeptr_t)(((char*)tb) - 4 - tb_offset);
if (pc < start_of_procedure) {
ERRBYE("could not find (the real) traceback table within 5000 bytes of program counter");
}
if (p_displacement) {
(*p_displacement) = (int) PTRDIFF_BYTES(pc, start_of_procedure);
}
pc2++;
} else {
if (p_displacement) {
(*p_displacement) = -1;
}
}
if (tb->tb.int_hndl == TRUE)
pc2++;
if (tb->tb.has_ctl == TRUE)
pc2 += (*pc2) + 1;
if (p_name && namelen > 0) {
if (tb->tb.name_present) {
char buf[256];
const short l = MIN2<short>(*((short*)pc2), sizeof(buf) - 1);
memcpy(buf, (char*)pc2 + sizeof(short), l);
buf[l] = '\0';
p_name[0] = '\0';
char* rest;
Name* const name = Demangle(buf, rest);
if (name) {
const char* const demangled_name = name->Text();
if (demangled_name) {
strncpy(p_name, demangled_name, namelen-1);
p_name[namelen-1] = '\0';
}
delete name;
}
if (p_name[0] == '\0') {
strncpy(p_name, buf, namelen-1);
p_name[namelen-1] = '\0';
}
} else {
strncpy(p_name, "<nameless function>", namelen-1);
p_name[namelen-1] = '\0';
}
}
if (p_tb) {
(*p_tb) = tb;
}
return 0;
}
extern "C"
int dladdr(void* addr, Dl_info* info) {
if (!addr) {
return 0;
}
assert(info, "");
int rc = 0;
const char* const ZEROSTRING = "";
info->dli_fname = ZEROSTRING;
info->dli_sname = ZEROSTRING;
info->dli_saddr = NULL;
address p = (address) addr;
const LoadedLibraryModule* lib = NULL;
enum { noclue, code, data } type = noclue;
trcVerbose("dladdr(%p)...", p);
lib = LoadedLibraries::find_for_text_address(p);
if (lib) {
type = code;
}
if (!lib) {
const FunctionDescriptor* const pfd = (const FunctionDescriptor*) p;
p = pfd->entry();
if (p) {
lib = LoadedLibraries::find_for_text_address(p);
if (lib) {
type = code;
}
}
}
if (!lib) {
p = (address)addr;
lib = LoadedLibraries::find_for_data_address(p);
if (lib) {
type = data;
}
}
if (lib) {
const char* const interned_libpath =
dladdr_fixed_strings.intern(lib->get_fullpath());
if (interned_libpath) {
info->dli_fname = interned_libpath;
}
if (type == code) {
char funcname[256] = "";
int displacement = 0;
if (getFuncName((codeptr_t) p, funcname, sizeof(funcname), &displacement,
NULL, NULL, 0) == 0) {
if (funcname[0] != '\0') {
const char* const interned = dladdr_fixed_strings.intern(funcname);
info->dli_sname = interned;
trcVerbose("... function name: %s ...", interned);
}
if (displacement != -1) {
info->dli_saddr = p - displacement;
} else {
info->dli_saddr = p;
}
} else {
info->dli_saddr = p;
}
} else if (type == data) {
info->dli_saddr = p;
} else {
ShouldNotReachHere();
}
rc = 1;
}
if (rc) {
assert(info->dli_fname, "");
assert(info->dli_sname, "");
assert(info->dli_saddr, "");
}
return rc;
}