Path: blob/master/debugtools/DDR_VM/src/com/ibm/j9ddr/libraries/DDRLibraryAdapter.java
6005 views
/*******************************************************************************1* Copyright (c) 1991, 2014 IBM Corp. and others2*3* This program and the accompanying materials are made available under4* the terms of the Eclipse Public License 2.0 which accompanies this5* distribution and is available at https://www.eclipse.org/legal/epl-2.0/6* or the Apache License, Version 2.0 which accompanies this distribution and7* is available at https://www.apache.org/licenses/LICENSE-2.0.8*9* This Source Code may also be made available under the following10* Secondary Licenses when the conditions for such availability set11* forth in the Eclipse Public License, v. 2.0 are satisfied: GNU12* General Public License, version 2 with the GNU Classpath13* Exception [1] and GNU General Public License, version 2 with the14* OpenJDK Assembly Exception [2].15*16* [1] https://www.gnu.org/software/classpath/license.html17* [2] http://openjdk.java.net/legal/assembly-exception.html18*19* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 OR LicenseRef-GPL-2.0 WITH Assembly-exception20*******************************************************************************/2122package com.ibm.j9ddr.libraries;2324import static java.util.logging.Level.SEVERE;25import static java.util.logging.Level.WARNING;2627import java.io.File;28import java.io.IOException;29import java.util.ArrayList;30import java.util.Iterator;31import java.util.logging.Logger;3233import com.ibm.dtfj.image.CorruptDataException;34import com.ibm.dtfj.image.DataUnavailable;35import com.ibm.dtfj.image.Image;36import com.ibm.dtfj.image.ImageAddressSpace;37import com.ibm.dtfj.image.ImageFactory;38import com.ibm.dtfj.image.ImageModule;39import com.ibm.dtfj.image.ImageProcess;40import com.ibm.j9ddr.corereaders.CoreReader;41import com.ibm.j9ddr.corereaders.ICore;42import com.ibm.j9ddr.corereaders.ILibraryDependentCore;43import com.ibm.j9ddr.view.dtfj.image.J9DDRImageFactory;4445@SuppressWarnings("restriction")46public class DDRLibraryAdapter implements LibraryAdapter {47private static final Logger logger = Logger.getLogger(LibraryCollector.LOGGER_NAME);48private ArrayList<String> moduleNames = null;49private final ArrayList<String> errorMessages = new ArrayList<String>();5051public ArrayList<String> getErrorMessages() {52return errorMessages;53}5455public ArrayList<String> getLibraryList(final File coreFile) {56if(moduleNames == null) {57constructLibraryList(coreFile);58}59return moduleNames;60}6162public boolean isLibraryCollectionRequired(File coreFile) {63ICore reader = getReader(coreFile.getPath());64return (reader instanceof ILibraryDependentCore);65}6667private ICore getReader(final String path) {68final ICore reader;69try {70reader = CoreReader.readCoreFile(path);71} catch (IOException e) {72logger.log(SEVERE, "Could not open core file", e);73errorMessages.add(e.getMessage());74return null;75}76return reader;77}7879/**80* Constructs the list of libraries required using the DDR implementation of the DTFJ Image* API.81* This ensures that the correct classloading is used for determining which libraries to collect.82* @param coreFile core file to process83*/84@SuppressWarnings({ "unchecked" })85private void constructLibraryList(final File coreFile) {86moduleNames = new ArrayList<String>();87ImageFactory factory = new J9DDRImageFactory();88final Image image;89final boolean isAIX;90try {91image = factory.getImage(coreFile);92isAIX = image.getSystemType().toLowerCase().startsWith("aix");93} catch (IOException e) {94logger.log(SEVERE, "Could not open core file", e);95errorMessages.add(e.getMessage());96return;97} catch (CorruptDataException e) {98logger.log(SEVERE, "Could not determine system type", e);99errorMessages.add(e.getMessage());100return;101} catch (DataUnavailable e) {102logger.log(SEVERE, "Could not determine system type", e);103errorMessages.add(e.getMessage());104return;105}106for(Iterator spaces = image.getAddressSpaces(); spaces.hasNext(); ) {107ImageAddressSpace space = (ImageAddressSpace) spaces.next();108for(Iterator procs = space.getProcesses(); procs.hasNext(); ) {109ImageProcess proc = (ImageProcess) procs.next();110try {111ImageModule exe = proc.getExecutable(); //add the executable to the list of libraries to be collected112moduleNames.add(exe.getName());113for(Iterator libraries = proc.getLibraries(); libraries.hasNext(); ) {114ImageModule module = (ImageModule) libraries.next();115String key = null;116try { //handle CDE thrown by getName(), as this is required further on this call needs to succeed117if(isAIX) {118key = module.getName();119int pos = key.indexOf(".a("); //check on AIX if module is the .a file or library120if((pos != -1) && (key.lastIndexOf(')') == key.length() - 1)) {121key = key.substring(0, pos + 2);122}123} else {124key = module.getName();125}126logger.fine("Module : " + key);127if(!moduleNames.contains(key)) { //don't store duplicate libraries128moduleNames.add(key);129}130} catch (Exception e) {131logger.log(WARNING, "Error getting module name", e);132}133}134} catch (DataUnavailable e) {135logger.log(WARNING, "Error getting library list", e);136errorMessages.add(e.getMessage());137} catch (com.ibm.dtfj.image.CorruptDataException e) {138logger.log(WARNING, "Error getting library list", e);139errorMessages.add(e.getMessage());140}141}142}143}144145}146147148