aoqi@0: /* aoqi@0: * Copyright 2012, 2013 SAP AG. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: * aoqi@0: */ aoqi@0: aoqi@0: aoqi@0: // Implementation of LoadedLibraries and friends aoqi@0: aoqi@0: // Ultimately this just uses loadquery() aoqi@0: // See: aoqi@0: // http://publib.boulder.ibm.com/infocenter/pseries/v5r3/index.jsp aoqi@0: // ?topic=/com.ibm.aix.basetechref/doc/basetrf1/loadquery.htm aoqi@0: aoqi@0: #ifndef __STDC_FORMAT_MACROS aoqi@0: #define __STDC_FORMAT_MACROS aoqi@0: #endif aoqi@0: // 'allocation.inline.hpp' triggers the inclusion of 'inttypes.h' which defines macros aoqi@0: // required by the definitions in 'globalDefinitions.hpp'. But these macros in 'inttypes.h' aoqi@0: // are only defined if '__STDC_FORMAT_MACROS' is defined! aoqi@0: #include "memory/allocation.inline.hpp" aoqi@0: #include "oops/oop.inline.hpp" aoqi@0: #include "runtime/threadCritical.hpp" aoqi@0: #include "utilities/debug.hpp" aoqi@0: #include "utilities/ostream.hpp" aoqi@0: #include "loadlib_aix.hpp" aoqi@0: #include "porting_aix.hpp" aoqi@0: aoqi@0: // For loadquery() aoqi@0: #include aoqi@0: aoqi@0: /////////////////////////////////////////////////////////////////////////////// aoqi@0: // Implementation for LoadedLibraryModule aoqi@0: aoqi@0: // output debug info aoqi@0: void LoadedLibraryModule::print(outputStream* os) const { aoqi@0: os->print("%15.15s: text: " INTPTR_FORMAT " - " INTPTR_FORMAT aoqi@0: ", data: " INTPTR_FORMAT " - " INTPTR_FORMAT " ", aoqi@0: shortname, text_from, text_to, data_from, data_to); aoqi@0: os->print(" %s", fullpath); aoqi@0: if (strlen(membername) > 0) { aoqi@0: os->print("(%s)", membername); aoqi@0: } aoqi@0: os->cr(); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: /////////////////////////////////////////////////////////////////////////////// aoqi@0: // Implementation for LoadedLibraries aoqi@0: aoqi@0: // class variables aoqi@0: LoadedLibraryModule LoadedLibraries::tab[MAX_MODULES]; aoqi@0: int LoadedLibraries::num_loaded = 0; aoqi@0: aoqi@0: // Checks whether the address p points to any of the loaded code segments. aoqi@0: // If it does, returns the LoadedLibraryModule entry. If not, returns NULL. aoqi@0: // static aoqi@0: const LoadedLibraryModule* LoadedLibraries::find_for_text_address(const unsigned char* p) { aoqi@0: aoqi@0: if (num_loaded == 0) { aoqi@0: reload(); aoqi@0: } aoqi@0: for (int i = 0; i < num_loaded; i++) { aoqi@0: if (tab[i].is_in_text(p)) { aoqi@0: return &tab[i]; aoqi@0: } aoqi@0: } aoqi@0: return NULL; aoqi@0: } aoqi@0: aoqi@0: // Checks whether the address p points to any of the loaded data segments. aoqi@0: // If it does, returns the LoadedLibraryModule entry. If not, returns NULL. aoqi@0: // static aoqi@0: const LoadedLibraryModule* LoadedLibraries::find_for_data_address(const unsigned char* p) { aoqi@0: if (num_loaded == 0) { aoqi@0: reload(); aoqi@0: } aoqi@0: for (int i = 0; i < num_loaded; i++) { aoqi@0: if (tab[i].is_in_data(p)) { aoqi@0: return &tab[i]; aoqi@0: } aoqi@0: } aoqi@0: return NULL; aoqi@0: } aoqi@0: aoqi@0: // Rebuild the internal table of LoadedLibraryModule objects aoqi@0: // static aoqi@0: void LoadedLibraries::reload() { aoqi@0: aoqi@0: ThreadCritical cs; aoqi@0: aoqi@0: // discard old content aoqi@0: num_loaded = 0; aoqi@0: aoqi@0: // Call loadquery(L_GETINFO..) to get a list of all loaded Dlls from AIX. aoqi@0: size_t buf_size = 4096; aoqi@0: char* loadquery_buf = AllocateHeap(buf_size, mtInternal); aoqi@0: aoqi@0: while(loadquery(L_GETINFO, loadquery_buf, buf_size) == -1) { aoqi@0: if (errno == ENOMEM) { aoqi@0: buf_size *= 2; aoqi@0: loadquery_buf = ReallocateHeap(loadquery_buf, buf_size, mtInternal); aoqi@0: } else { aoqi@0: FreeHeap(loadquery_buf); aoqi@0: // Ensure that the uintptr_t pointer is valid aoqi@0: assert(errno != EFAULT, "loadquery: Invalid uintptr_t in info buffer."); aoqi@0: fprintf(stderr, "loadquery failed (%d %s)", errno, strerror(errno)); aoqi@0: return; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: // Iterate over the loadquery result. For details see sys/ldr.h on AIX. aoqi@0: const struct ld_info* p = (struct ld_info*) loadquery_buf; aoqi@0: aoqi@0: // Ensure we have all loaded libs. aoqi@0: bool all_loaded = false; aoqi@0: while(num_loaded < MAX_MODULES) { aoqi@0: LoadedLibraryModule& mod = tab[num_loaded]; aoqi@0: mod.text_from = (const unsigned char*) p->ldinfo_textorg; aoqi@0: mod.text_to = (const unsigned char*) (((char*)p->ldinfo_textorg) + p->ldinfo_textsize); aoqi@0: mod.data_from = (const unsigned char*) p->ldinfo_dataorg; aoqi@0: mod.data_to = (const unsigned char*) (((char*)p->ldinfo_dataorg) + p->ldinfo_datasize); aoqi@0: sprintf(mod.fullpath, "%.*s", sizeof(mod.fullpath), p->ldinfo_filename); aoqi@0: // do we have a member name as well (see ldr.h)? aoqi@0: const char* p_mbr_name = p->ldinfo_filename + strlen(p->ldinfo_filename) + 1; aoqi@0: if (*p_mbr_name) { aoqi@0: sprintf(mod.membername, "%.*s", sizeof(mod.membername), p_mbr_name); aoqi@0: } else { aoqi@0: mod.membername[0] = '\0'; aoqi@0: } aoqi@0: aoqi@0: // fill in the short name aoqi@0: const char* p_slash = strrchr(mod.fullpath, '/'); aoqi@0: if (p_slash) { aoqi@0: sprintf(mod.shortname, "%.*s", sizeof(mod.shortname), p_slash + 1); aoqi@0: } else { aoqi@0: sprintf(mod.shortname, "%.*s", sizeof(mod.shortname), mod.fullpath); aoqi@0: } aoqi@0: num_loaded ++; aoqi@0: aoqi@0: // next entry... aoqi@0: if (p->ldinfo_next) { aoqi@0: p = (struct ld_info*)(((char*)p) + p->ldinfo_next); aoqi@0: } else { aoqi@0: all_loaded = true; aoqi@0: break; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: FreeHeap(loadquery_buf); aoqi@0: aoqi@0: // Ensure we have all loaded libs aoqi@0: assert(all_loaded, "loadquery returned more entries then expected. Please increase MAX_MODULES"); aoqi@0: aoqi@0: } // end LoadedLibraries::reload() aoqi@0: aoqi@0: aoqi@0: // output loaded libraries table aoqi@0: //static aoqi@0: void LoadedLibraries::print(outputStream* os) { aoqi@0: aoqi@0: for (int i = 0; i < num_loaded; i++) { aoqi@0: tab[i].print(os); aoqi@0: } aoqi@0: aoqi@0: } aoqi@0: