|
Added
Link Here
|
| 1 |
/******************************************************************************* |
| 2 |
* Copyright (c) 2007 Intel Corporation. |
| 3 |
* All rights reserved. This program and the accompanying materials |
| 4 |
* are made available under the terms of the Eclipse Public License v1.0 |
| 5 |
* which accompanies this distribution, and is available at |
| 6 |
* http://www.eclipse.org/legal/epl-v10.html |
| 7 |
* |
| 8 |
* Contributors: |
| 9 |
* Stanislav Polevic, Intel - Initial API and Implementation |
| 10 |
* |
| 11 |
* $Id: |
| 12 |
*******************************************************************************/ |
| 13 |
package org.eclipse.hyades.loaders.internal.binary.v1; |
| 14 |
|
| 15 |
import java.io.IOException; |
| 16 |
import java.io.InputStream; |
| 17 |
import java.util.HashMap; |
| 18 |
import java.util.Map; |
| 19 |
|
| 20 |
import org.eclipse.hyades.loaders.internal.binary.BFParser; |
| 21 |
import org.eclipse.hyades.loaders.internal.binary.BFParserFactory; |
| 22 |
import org.eclipse.hyades.loaders.internal.binary.BFReader; |
| 23 |
import org.eclipse.hyades.loaders.internal.binary.BinaryFragment; |
| 24 |
import org.eclipse.hyades.loaders.internal.binary.BinaryFragmentParser; |
| 25 |
import org.eclipse.hyades.loaders.internal.binary.IgnoredBinaryFragmentParser; |
| 26 |
import org.eclipse.hyades.loaders.internal.binary.Offset; |
| 27 |
import org.eclipse.hyades.loaders.util.HierarchyContext; |
| 28 |
|
| 29 |
public class ParserImpl1 implements BFParser { |
| 30 |
|
| 31 |
public static final Double version = new Double(1.0); |
| 32 |
|
| 33 |
// ID (Short) -> Class (class) |
| 34 |
Map messageHandlers = new HashMap(); |
| 35 |
|
| 36 |
private final HierarchyContext context; |
| 37 |
|
| 38 |
private final BFReader reader; |
| 39 |
|
| 40 |
public ParserImpl1(HierarchyContext context, BFReader reader) { |
| 41 |
this.context = context; |
| 42 |
this.reader = reader; |
| 43 |
initialize(); |
| 44 |
} |
| 45 |
|
| 46 |
protected void initialize() { |
| 47 |
// System handlers 0-1000 |
| 48 |
messageHandlers.put(BFEncodingParser.ID, BFEncodingParser.class); |
| 49 |
messageHandlers.put(BFFrequencyParser.ID, BFFrequencyParser.class); |
| 50 |
|
| 51 |
// Data handlers 1000-32767 |
| 52 |
messageHandlers.put(BFNodeParser.ID, BFNodeParser.class); |
| 53 |
messageHandlers.put(BFProcessCreateParser.ID, BFProcessCreateParser.class); |
| 54 |
messageHandlers.put(BFAgentCreateParser.ID, BFAgentCreateParser.class); |
| 55 |
messageHandlers.put(BFAgentDestroyParser.ID, BFAgentDestroyParser.class); |
| 56 |
messageHandlers.put(BFTraceStartParser.ID, BFTraceStartParser.class); |
| 57 |
messageHandlers.put(BFTraceEndParser.ID, BFTraceEndParser.class); |
| 58 |
messageHandlers.put(BFFilterParser.ID, BFFilterParser.class); |
| 59 |
messageHandlers.put(BFOptionParser.ID, BFOptionParser.class); |
| 60 |
messageHandlers.put(BFThreadStartParser.ID, BFThreadStartParser.class); |
| 61 |
messageHandlers.put(BFThreadEndParser.ID, BFThreadEndParser.class); |
| 62 |
messageHandlers.put(BFClassDefParser.ID, BFClassDefParser.class); |
| 63 |
messageHandlers.put(BFMethodDefParser.ID, BFMethodDefParser.class); |
| 64 |
messageHandlers.put(BFObjAllocParser.ID, BFObjAllocParser.class); |
| 65 |
messageHandlers.put(BFMethodEntryParser.ID, BFMethodEntryParser.class); |
| 66 |
messageHandlers.put(BFMethodExitParser.ID, BFMethodExitParser.class); |
| 67 |
// Method call ignored |
| 68 |
// Method exit ignored |
| 69 |
messageHandlers.put(BFInvocationContextParser.ID, BFInvocationContextParser.class); |
| 70 |
messageHandlers.put(BFObjDefParser.ID, BFObjDefParser.class); |
| 71 |
messageHandlers.put(BFValueParser.ID, BFValueParser.class); |
| 72 |
messageHandlers.put(BFMethodCountParser.ID, BFMethodCountParser.class); |
| 73 |
messageHandlers.put(BFLineParser.ID, BFLineParser.class); |
| 74 |
messageHandlers.put(BFGCStartParser.ID, BFGCStartParser.class); |
| 75 |
messageHandlers.put(BFObjFreeParser.ID, BFObjFreeParser.class); |
| 76 |
messageHandlers.put(BFClassUnloadParser.ID, BFClassUnloadParser.class); |
| 77 |
messageHandlers.put(BFObjMoveParser.ID, BFObjMoveParser.class); |
| 78 |
messageHandlers.put(BFGCFinishParser.ID, BFGCFinishParser.class); |
| 79 |
messageHandlers.put(BFThrowParser.ID, BFThrowParser.class); |
| 80 |
messageHandlers.put(BFCatchParser.ID, BFCatchParser.class); |
| 81 |
messageHandlers.put(BFRuntimeInitDoneParser.ID, BFRuntimeInitDoneParser.class); |
| 82 |
messageHandlers.put(BFRuntimeShutdownParser.ID, BFRuntimeShutdownParser.class); |
| 83 |
messageHandlers.put(BFMonWaitParser.ID, BFMonWaitParser.class); |
| 84 |
messageHandlers.put(BFMonWaitedParser.ID, BFMonWaitedParser.class); |
| 85 |
messageHandlers.put(BFMonContendedEnterParser.ID, BFMonContendedEnterParser.class); |
| 86 |
messageHandlers.put(BFMonContendedEnteredParser.ID, BFMonContendedEnteredParser.class); |
| 87 |
messageHandlers.put(BFAGMethodEntryParser.ID, BFAGMethodEntryParser.class); |
| 88 |
messageHandlers.put(BFAGMethodExitParser.ID, BFAGMethodExitParser.class); |
| 89 |
messageHandlers.put(BFHeapDumpDefParser.ID, BFHeapDumpDefParser.class); |
| 90 |
messageHandlers.put(BFGCRootParser.ID, BFGCRootParser.class); |
| 91 |
messageHandlers.put(BFObjRefParser.ID, BFObjRefParser.class); |
| 92 |
// Insert Custom message parser here |
| 93 |
messageHandlers.put(BFMonNotifyCalledParser.ID, BFMonNotifyCalledParser.class); |
| 94 |
messageHandlers.put(BFThreadInterruptCalledParser.ID, BFThreadInterruptCalledParser.class); |
| 95 |
messageHandlers.put(BFThreadStartCalledParser.ID, BFThreadStartCalledParser.class); |
| 96 |
} |
| 97 |
|
| 98 |
public void parseEvents(byte[] buffer, Offset marker) { |
| 99 |
try { |
| 100 |
do { |
| 101 |
BinaryFragment binaryFragment = getBinaryFragment(buffer, marker); |
| 102 |
if (binaryFragment == null) { |
| 103 |
break; |
| 104 |
} else if (binaryFragment.getId() == null) { |
| 105 |
continue; |
| 106 |
} |
| 107 |
parseBinaryFragment(binaryFragment, marker); |
| 108 |
} while (true); |
| 109 |
} catch (Throwable e) { |
| 110 |
e.printStackTrace(); |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
public void parseEvents(InputStream inputStream, Offset marker) { |
| 115 |
try { |
| 116 |
do { |
| 117 |
BinaryFragment binaryFragment = getBinaryFragment(inputStream, marker); |
| 118 |
if (binaryFragment == null) { |
| 119 |
break; |
| 120 |
} else if (binaryFragment.getId() == null) { |
| 121 |
continue; |
| 122 |
} |
| 123 |
parseBinaryFragment(binaryFragment, marker); |
| 124 |
} while (true); |
| 125 |
} catch (Throwable e) { |
| 126 |
e.printStackTrace(); |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
protected BinaryFragment getBinaryFragment(byte[] buffer, Offset marker) throws IOException { |
| 131 |
if (BinaryFragment.isEOF(buffer, marker.getOffset())) { |
| 132 |
return null; |
| 133 |
} |
| 134 |
BinaryFragment binaryFragment = new BinaryFragment(buffer, marker.getOffset(), reader); |
| 135 |
if (marker.increaseOffset(BinaryFragment.HEADER_LENGTH) < 0) { |
| 136 |
return null; |
| 137 |
} |
| 138 |
|
| 139 |
binaryFragment.setBody(buffer); |
| 140 |
binaryFragment.setBodyOffset(marker.getOffset()); |
| 141 |
if (marker.increaseOffset(binaryFragment.getLength()) < 0) { |
| 142 |
return null; |
| 143 |
} |
| 144 |
|
| 145 |
if (binaryFragment.getId() == null) { |
| 146 |
System.err.println("Unparseable event " + buffer); |
| 147 |
} |
| 148 |
|
| 149 |
return binaryFragment; |
| 150 |
} |
| 151 |
|
| 152 |
protected BinaryFragment getBinaryFragment(InputStream inputStream, Offset marker) throws IOException { |
| 153 |
byte[] header = new byte[BinaryFragment.HEADER_LENGTH]; |
| 154 |
if (inputStream.read(header) < 0) { |
| 155 |
return null; |
| 156 |
} |
| 157 |
if (BinaryFragment.isEOF(header, 0)) { |
| 158 |
return null; |
| 159 |
} |
| 160 |
BinaryFragment binaryFragment = new BinaryFragment(header, 0, reader); |
| 161 |
if (marker.increaseOffset(BinaryFragment.HEADER_LENGTH) < 0) { |
| 162 |
return null; |
| 163 |
} |
| 164 |
|
| 165 |
byte[] body = new byte[binaryFragment.getLength()]; |
| 166 |
if(inputStream.read(body) < 0) { |
| 167 |
return null; |
| 168 |
} |
| 169 |
|
| 170 |
binaryFragment.setBody(body); |
| 171 |
if (marker.increaseOffset(binaryFragment.getLength()) < 0) { |
| 172 |
return null; |
| 173 |
} |
| 174 |
|
| 175 |
if (binaryFragment.getId() == null) { |
| 176 |
System.err.println("Unparseable event " + header); |
| 177 |
} |
| 178 |
|
| 179 |
return binaryFragment; |
| 180 |
} |
| 181 |
|
| 182 |
protected void parseBinaryFragment(BinaryFragment binaryFragment, Offset marker) |
| 183 |
throws InstantiationException, IllegalAccessException { |
| 184 |
BinaryFragmentParser fragmentParser = getFragmentParser(binaryFragment.getId()); |
| 185 |
|
| 186 |
if (fragmentParser.getClass().equals(IgnoredBinaryFragmentParser.class)) { |
| 187 |
System.err.println("Got unknown message at " + marker.getOffset()); |
| 188 |
} |
| 189 |
|
| 190 |
fragmentParser.initialize(context); |
| 191 |
Offset bodyOffset = new Offset(binaryFragment.getBodyOffset(), binaryFragment.getBodyOffset() + binaryFragment.getLength()); |
| 192 |
if (fragmentParser.parse(binaryFragment, bodyOffset, reader)) { |
| 193 |
fragmentParser.addYourselfInContext(); |
| 194 |
if ((context.getAgent() != null) && (context.getAgent().eResource() != null) && !context.getAgent().eResource().isModified()) { |
| 195 |
context.getAgent().eResource().setModified(true); |
| 196 |
} |
| 197 |
} |
| 198 |
} |
| 199 |
|
| 200 |
protected BinaryFragmentParser getFragmentParser(Short id) throws InstantiationException, IllegalAccessException { |
| 201 |
Class clazz = (Class) messageHandlers.get(id); |
| 202 |
|
| 203 |
if (clazz == null) { |
| 204 |
return new IgnoredBinaryFragmentParser(); |
| 205 |
} |
| 206 |
BinaryFragmentParser fragmentParser = (BinaryFragmentParser) clazz.newInstance(); |
| 207 |
if (fragmentParser == null) { |
| 208 |
return new IgnoredBinaryFragmentParser(); |
| 209 |
} |
| 210 |
|
| 211 |
return fragmentParser; |
| 212 |
} |
| 213 |
|
| 214 |
public static void register(BFParserFactory parserFactory) { |
| 215 |
parserFactory.put(version, ParserImpl1.class); |
| 216 |
} |
| 217 |
|
| 218 |
} |