Path: blob/master/debugtools/DDR_VM/src/com/ibm/j9ddr/libraries/DTFJLibraryAdapter.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;2526import java.io.File;27import java.io.FileNotFoundException;28import java.io.IOException;29import java.util.ArrayList;30import java.util.Iterator;31import java.util.List;32import java.util.Properties;33import java.util.logging.Level;34import java.util.logging.Logger;3536import com.ibm.dtfj.corereaders.Builder;37import com.ibm.dtfj.corereaders.ClosingFileReader;38import com.ibm.dtfj.corereaders.DumpFactory;39import com.ibm.dtfj.corereaders.NewElfDump;40import com.ibm.dtfj.corereaders.NewAixDump;41import com.ibm.dtfj.corereaders.ICoreFileReader;4243@SuppressWarnings("restriction")44public class DTFJLibraryAdapter implements Builder, LibraryAdapter {45private static final Logger logger = Logger.getLogger(LibraryCollector.LOGGER_NAME);46private File _parentDir;47private ArrayList<String> moduleNames = null;48private final ArrayList<String> errorMessages = new ArrayList<String>();49private boolean isAIX = false; //flag to indicate if running on AIX5051public static void main(String[] args)52{53if (args.length > 0) {54for (int x = 0; x < args.length; x++) {55File coreFile = new File(args[x]);56System.out.println("Reading \"" + coreFile.getAbsolutePath() + "\"...");57DTFJLibraryAdapter adapter = new DTFJLibraryAdapter();58ArrayList<String> modules = adapter.getLibraryList(coreFile);59for(String module : modules) {60System.out.println("\t" + module);61}62}63} else {64System.out.println("Usage: \"LibraryAdapter <core files> ...\"");65System.exit(1);66}67}6869public boolean isLibraryCollectionRequired(File coreFile) {70ICoreFileReader reader = null;71try {72ClosingFileReader closingFile = new ClosingFileReader(coreFile);73reader = DumpFactory.createDumpForCore(closingFile);74} catch (Exception e) {75logger.log(SEVERE, "Could not determine if library collection is required for " + coreFile.getAbsolutePath(), e);76errorMessages.add(e.getMessage());77return false; //if this fails, then so would any collection attempt as well78}79if (reader instanceof NewElfDump) {80return true;81}82if (reader instanceof NewAixDump) {83return true;84}85return false;86}87888990public ArrayList<String> getLibraryList(final File coreFile)91{92if(moduleNames == null) {93moduleNames = new ArrayList<String>();94try {95_parentDir = coreFile.getParentFile();96logger.fine("Creating DTFJ core file reader");97ClosingFileReader closingFile = new ClosingFileReader(coreFile);98ICoreFileReader reader = DumpFactory.createDumpForCore(closingFile);99isAIX = (reader instanceof NewAixDump);100logger.fine("Extracting modules");101reader.extract(this);102} catch (FileNotFoundException e) {103logger.log(SEVERE, "Could not open core file " + coreFile.getAbsolutePath(), e);104errorMessages.add(e.getMessage());105} catch (IOException e) {106logger.log(SEVERE, "Error processing core file " + coreFile.getAbsolutePath(), e);107errorMessages.add(e.getMessage());108}109}110return moduleNames;111}112113public ArrayList<String> getErrorMessages() {114return errorMessages;115}116117public ClosingFileReader openFile(String filename) throws IOException118{119//given that we may be looking for a file which came from another system, look it up in our support file dir first (we need that to over-ride the absolute path since it may be looking for a file in the same location as one on the local machine - this happens often if moving a core file from one Unix system to another)120ClosingFileReader candidate = null;121File absolute = new File(filename);122String fileName = absolute.getName();123File supportFileCopy = new File(_parentDir, fileName);124if (supportFileCopy.exists()) {125candidate = new ClosingFileReader(supportFileCopy);126} else {127candidate = new ClosingFileReader(absolute);128}129return candidate;130}131132@SuppressWarnings("unchecked")133public Object buildModule(String name, Properties properties, Iterator sections, Iterator symbols, long loadAddress)134{135logger.fine("Module : " + name);136if(isAIX) {137int pos = name.indexOf(".a("); //check on AIX if module is the .a file or library138if((pos != -1) && (name.lastIndexOf(')') == name.length() - 1)) {139moduleNames.add(name.substring(0, pos + 2));140} else {141moduleNames.add(name);142}143} else {144moduleNames.add(name);145}146return null;147}148149@SuppressWarnings("unchecked")150public Object buildProcess(Object addressSpace, String pid, String commandLine, Properties environment, Object currentThread, Iterator threads, Object executable, Iterator libraries, int addressSize)151{152if(logger.isLoggable(Level.FINEST)) {153String msg = String.format("Building process %s : %s", pid, commandLine);154logger.finest(msg);155}156return null; //no-op157}158159public Object buildAddressSpace(String name, int id)160{161if(logger.isLoggable(Level.FINEST)) {162String msg = String.format("Building address space %s[%d]", name, id);163logger.finest(msg);164}165return null; //no-op166}167168public Object buildRegister(String name, Number value)169{170return null; //no-op171}172173public Object buildStackSection(Object addressSpace, long stackStart, long stackEnd)174{175return null; //no-op176}177178@SuppressWarnings("unchecked")179public Object buildThread(String name, Iterator registers, Iterator stackSections, Iterator stackFrames, Properties properties, int signalNumber)180{181if(logger.isLoggable(Level.FINEST)) {182String msg = String.format("Building thread %s [signal %d]", name, signalNumber);183logger.finest(msg);184}185return null; //no-op186}187188public Object buildModuleSection(Object addressSpace, String name, long imageStart, long imageEnd)189{190if(logger.isLoggable(Level.FINEST)) {191String msg = String.format("Building module %s [0x%s - 0x%s]", name, Long.toHexString(imageStart), Long.toHexString(imageEnd));192logger.finest(msg);193}194return null; //no-op195}196197public Object buildStackFrame(Object addressSpace, long stackBasePointer, long pc)198{199return null; //no-op200}201202public Object buildSymbol(Object addressSpace, String functionName, long relocatedFunctionAddress)203{204return null; //no-op205}206207public Object buildCorruptData(Object addressSpace, String message, long address)208{209return null; //no-op210}211212public long getEnvironmentAddress()213{214return 0; //no-op215}216217@SuppressWarnings("unchecked")218public long getValueOfNamedRegister(List registers, String string)219{220return 0; //no-op221}222223public void setExecutableUnavailable(String description)224{225errorMessages.add("Library collection not possible as the executable cannot be found [" + description + "]");226if(logger.isLoggable(Level.FINEST)) {227String msg = String.format("Executable not available : %s", description);228logger.warning(msg);229}230}231232public void setOSType(String osType)233{234if(logger.isLoggable(Level.FINEST)) {235String msg = String.format("OS Type : %s", osType);236logger.finest(msg);237}238}239240public void setCPUType(String cpuType)241{242//no-op243}244245public void setCPUSubType(String subType)246{247//no-op248}249250public void setCreationTime(long millis)251{252//no-op253}254255@SuppressWarnings("unchecked")256public Object buildModule(String name, Properties properties, Iterator sections, Iterator symbols)257{258if(logger.isLoggable(Level.FINEST)) {259String msg = String.format("Building module %s", name);260logger.finest(msg);261}262return null;263}264265}266267268