Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openj9
Path: blob/master/debugtools/DDR_VM/src/com/ibm/j9ddr/tools/DTFJWalker.java
6005 views
1
/*******************************************************************************
2
* Copyright (c) 1991, 2014 IBM Corp. and others
3
*
4
* This program and the accompanying materials are made available under
5
* the terms of the Eclipse Public License 2.0 which accompanies this
6
* distribution and is available at https://www.eclipse.org/legal/epl-2.0/
7
* or the Apache License, Version 2.0 which accompanies this distribution and
8
* is available at https://www.apache.org/licenses/LICENSE-2.0.
9
*
10
* This Source Code may also be made available under the following
11
* Secondary Licenses when the conditions for such availability set
12
* forth in the Eclipse Public License, v. 2.0 are satisfied: GNU
13
* General Public License, version 2 with the GNU Classpath
14
* Exception [1] and GNU General Public License, version 2 with the
15
* OpenJDK Assembly Exception [2].
16
*
17
* [1] https://www.gnu.org/software/classpath/license.html
18
* [2] http://openjdk.java.net/legal/assembly-exception.html
19
*
20
* 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-exception
21
*******************************************************************************/
22
23
package com.ibm.j9ddr.tools;
24
25
import java.io.File;
26
import java.io.IOException;
27
import java.lang.reflect.Method;
28
import java.util.ArrayList;
29
import java.util.Iterator;
30
import java.util.Stack;
31
import java.util.logging.Level;
32
import java.util.logging.Logger;
33
34
import com.ibm.dtfj.image.CorruptData;
35
import com.ibm.dtfj.image.Image;
36
import com.ibm.j9ddr.view.dtfj.image.J9DDRImageFactory;
37
38
/**
39
* @author apilkington
40
*
41
* Class used to provide a high level indication of the validity of a given core file.
42
* It walks all iterators looking to exercise as much of the DTFJ API as possible and
43
* reports back the location of any exceptions or corrupt data encountered
44
*
45
*/
46
47
@SuppressWarnings("restriction")
48
public class DTFJWalker {
49
private final Logger logger = Logger.getLogger(getClass().getName());
50
private final Stack<String> path = new Stack<String>();
51
private final ArrayList<InvocationResult> results = new ArrayList<InvocationResult>();
52
53
public static void main(String[] args) {
54
DTFJWalker walker = new DTFJWalker();
55
if (args.length != 1) {
56
printHelp();
57
System.exit(1);
58
}
59
walker.walkCoreFile(args[0]);
60
}
61
62
/**
63
* Print usage help to stdout
64
*/
65
private static void printHelp() {
66
System.out.println("Usage :\n\njava com.ibm.j9ddr.tools.DTFJWalker <core file>\n");
67
System.out.println("<core file> : the path and file name of the core file");
68
}
69
70
public void walkCoreFile(String path) {
71
File file = new File(path);
72
walkCoreFile(file);
73
}
74
75
public void walkCoreFile(File file) {
76
//the factory will have to be on the classpath as it is in the same jar as this class
77
J9DDRImageFactory factory = new J9DDRImageFactory();
78
Image image = null;
79
try {
80
image = factory.getImage(file);
81
} catch (IOException e) {
82
System.err.println("Failed to create an Image from the core file : " + e.getMessage());
83
logger.log(Level.WARNING, "Failed to create Image from core file", e);
84
return;
85
}
86
try {
87
Method iterator = image.getClass().getDeclaredMethod("getAddressSpaces", (Class[]) null);
88
iterate(image, iterator, 100);
89
} catch (Exception e) {
90
System.err.println("Failed to get address space iterator method : " + e.getMessage());
91
logger.log(Level.WARNING, "Failed to get address space iterator method", e);
92
return;
93
}
94
System.out.println("Walk complete");
95
showResults();
96
}
97
98
private void showResults() {
99
for(InvocationResult result : results) {
100
System.out.println(result.toString());
101
}
102
}
103
104
private void iterate(Object object, Method iterator, int maxItems) {
105
int count = 0;
106
path.push(getNameFromMethod(iterator));
107
iterator.setAccessible(true);
108
try {
109
Object result = iterator.invoke(object, (Object[]) null);
110
if(result instanceof Iterator<?>) {
111
Iterator<?> list = (Iterator<?>) result;
112
while(list.hasNext() && (count < maxItems)) {
113
Object data = list.next();
114
if(data instanceof CorruptData) {
115
results.add(new InvocationResult(getCurrentPath(), (CorruptData) data));
116
} else {
117
Method[] methods = data.getClass().getMethods();
118
for(Method method : methods) {
119
if(method.getReturnType().getSimpleName().equals("Iterator")) {
120
path.push("[" + count + "]/");
121
iterate(data, method, maxItems);
122
path.pop();
123
}
124
}
125
}
126
count++;
127
}
128
}
129
if(result instanceof CorruptData) {
130
results.add(new InvocationResult(getCurrentPath(), (CorruptData) result));
131
}
132
} catch (Exception e) {
133
System.err.println("Exception thrown by iterator : " + e.getMessage());
134
logger.log(Level.WARNING, "Exception thrown by iterator", e);
135
results.add(new InvocationResult(getCurrentPath(), e.getCause()));
136
//fall through and exit the invocation of this iterator
137
}
138
path.pop();
139
}
140
141
private String getCurrentPath() {
142
Iterator<String> elements = path.iterator();
143
StringBuilder builder = new StringBuilder();
144
while(elements.hasNext()) {
145
builder.append(elements.next());
146
}
147
return builder.toString();
148
}
149
150
private String getNameFromMethod(Method method) {
151
String name = method.getName();
152
for(int i = 0; i < name.length(); i++) {
153
if(name.charAt(i) < 'a') {
154
return name.substring(i);
155
}
156
}
157
return name;
158
}
159
160
private class InvocationResult {
161
private final String path;
162
private final CorruptData corruptData;
163
private final Throwable exception;
164
165
InvocationResult(String path, CorruptData corruptData, Throwable exception) {
166
this.path = path;
167
this.corruptData = corruptData;
168
this.exception = exception;
169
}
170
171
InvocationResult(String path,CorruptData corruptData) {
172
this.path = path;
173
this.corruptData = corruptData;
174
this.exception = null;
175
}
176
177
InvocationResult(String path,Throwable exception) {
178
this.path = path;
179
this.corruptData = null;
180
this.exception = exception;
181
}
182
183
@Override
184
public String toString() {
185
StringBuilder builder = new StringBuilder(path);
186
builder.append(" = ");
187
if(null != corruptData) {
188
builder.append(corruptData.toString());
189
}
190
if(null != exception) {
191
builder.append(exception.getClass().getName());
192
builder.append("[");
193
builder.append(exception.getMessage());
194
builder.append("] ");
195
}
196
return builder.toString();
197
}
198
199
200
}
201
}
202
203