Path: blob/master/debugtools/DDR_VM/src/com/ibm/j9ddr/tools/DTFJWalker.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.tools;2324import java.io.File;25import java.io.IOException;26import java.lang.reflect.Method;27import java.util.ArrayList;28import java.util.Iterator;29import java.util.Stack;30import java.util.logging.Level;31import java.util.logging.Logger;3233import com.ibm.dtfj.image.CorruptData;34import com.ibm.dtfj.image.Image;35import com.ibm.j9ddr.view.dtfj.image.J9DDRImageFactory;3637/**38* @author apilkington39*40* Class used to provide a high level indication of the validity of a given core file.41* It walks all iterators looking to exercise as much of the DTFJ API as possible and42* reports back the location of any exceptions or corrupt data encountered43*44*/4546@SuppressWarnings("restriction")47public class DTFJWalker {48private final Logger logger = Logger.getLogger(getClass().getName());49private final Stack<String> path = new Stack<String>();50private final ArrayList<InvocationResult> results = new ArrayList<InvocationResult>();5152public static void main(String[] args) {53DTFJWalker walker = new DTFJWalker();54if (args.length != 1) {55printHelp();56System.exit(1);57}58walker.walkCoreFile(args[0]);59}6061/**62* Print usage help to stdout63*/64private static void printHelp() {65System.out.println("Usage :\n\njava com.ibm.j9ddr.tools.DTFJWalker <core file>\n");66System.out.println("<core file> : the path and file name of the core file");67}6869public void walkCoreFile(String path) {70File file = new File(path);71walkCoreFile(file);72}7374public void walkCoreFile(File file) {75//the factory will have to be on the classpath as it is in the same jar as this class76J9DDRImageFactory factory = new J9DDRImageFactory();77Image image = null;78try {79image = factory.getImage(file);80} catch (IOException e) {81System.err.println("Failed to create an Image from the core file : " + e.getMessage());82logger.log(Level.WARNING, "Failed to create Image from core file", e);83return;84}85try {86Method iterator = image.getClass().getDeclaredMethod("getAddressSpaces", (Class[]) null);87iterate(image, iterator, 100);88} catch (Exception e) {89System.err.println("Failed to get address space iterator method : " + e.getMessage());90logger.log(Level.WARNING, "Failed to get address space iterator method", e);91return;92}93System.out.println("Walk complete");94showResults();95}9697private void showResults() {98for(InvocationResult result : results) {99System.out.println(result.toString());100}101}102103private void iterate(Object object, Method iterator, int maxItems) {104int count = 0;105path.push(getNameFromMethod(iterator));106iterator.setAccessible(true);107try {108Object result = iterator.invoke(object, (Object[]) null);109if(result instanceof Iterator<?>) {110Iterator<?> list = (Iterator<?>) result;111while(list.hasNext() && (count < maxItems)) {112Object data = list.next();113if(data instanceof CorruptData) {114results.add(new InvocationResult(getCurrentPath(), (CorruptData) data));115} else {116Method[] methods = data.getClass().getMethods();117for(Method method : methods) {118if(method.getReturnType().getSimpleName().equals("Iterator")) {119path.push("[" + count + "]/");120iterate(data, method, maxItems);121path.pop();122}123}124}125count++;126}127}128if(result instanceof CorruptData) {129results.add(new InvocationResult(getCurrentPath(), (CorruptData) result));130}131} catch (Exception e) {132System.err.println("Exception thrown by iterator : " + e.getMessage());133logger.log(Level.WARNING, "Exception thrown by iterator", e);134results.add(new InvocationResult(getCurrentPath(), e.getCause()));135//fall through and exit the invocation of this iterator136}137path.pop();138}139140private String getCurrentPath() {141Iterator<String> elements = path.iterator();142StringBuilder builder = new StringBuilder();143while(elements.hasNext()) {144builder.append(elements.next());145}146return builder.toString();147}148149private String getNameFromMethod(Method method) {150String name = method.getName();151for(int i = 0; i < name.length(); i++) {152if(name.charAt(i) < 'a') {153return name.substring(i);154}155}156return name;157}158159private class InvocationResult {160private final String path;161private final CorruptData corruptData;162private final Throwable exception;163164InvocationResult(String path, CorruptData corruptData, Throwable exception) {165this.path = path;166this.corruptData = corruptData;167this.exception = exception;168}169170InvocationResult(String path,CorruptData corruptData) {171this.path = path;172this.corruptData = corruptData;173this.exception = null;174}175176InvocationResult(String path,Throwable exception) {177this.path = path;178this.corruptData = null;179this.exception = exception;180}181182@Override183public String toString() {184StringBuilder builder = new StringBuilder(path);185builder.append(" = ");186if(null != corruptData) {187builder.append(corruptData.toString());188}189if(null != exception) {190builder.append(exception.getClass().getName());191builder.append("[");192builder.append(exception.getMessage());193builder.append("] ");194}195return builder.toString();196}197198199}200}201202203