|
Added
Link Here
|
| 1 |
/********************************************************************** |
| 2 |
* Copyright (c) 2007 IBM Corporation and others. |
| 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 |
* IBM and OCSystems- Initial API and implementation |
| 10 |
**********************************************************************/ |
| 11 |
|
| 12 |
|
| 13 |
package org.eclipse.tptp.platform.probekit.launch.launchpad; |
| 14 |
|
| 15 |
import java.io.BufferedWriter; |
| 16 |
import java.io.DataInputStream; |
| 17 |
import java.io.File; |
| 18 |
import java.io.FileInputStream; |
| 19 |
import java.io.FileNotFoundException; |
| 20 |
import java.io.FileWriter; |
| 21 |
import java.io.IOException; |
| 22 |
import java.nio.ByteBuffer; |
| 23 |
import java.util.Iterator; |
| 24 |
import java.util.Map; |
| 25 |
import java.util.Set; |
| 26 |
import java.util.Vector; |
| 27 |
|
| 28 |
import org.eclipse.core.runtime.CoreException; |
| 29 |
import org.eclipse.debug.core.ILaunchConfiguration; |
| 30 |
import org.eclipse.hyades.models.hierarchy.TRCAgentProxy; |
| 31 |
import org.eclipse.tptp.platform.execution.client.agent.IAgent; |
| 32 |
import org.eclipse.tptp.platform.execution.client.agent.ICollector; |
| 33 |
import org.eclipse.tptp.platform.execution.client.core.INode; |
| 34 |
import org.eclipse.tptp.platform.execution.util.ICommandElement; |
| 35 |
import org.eclipse.tptp.platform.execution.util.ICommandHandler; |
| 36 |
import org.eclipse.tptp.platform.execution.util.internal.CommandFragment; |
| 37 |
import org.eclipse.tptp.platform.probekit.launch.internal.wizard.CustomBase64Encoder; |
| 38 |
import org.eclipse.tptp.platform.probekit.registry.ProbeRegistry; |
| 39 |
import org.eclipse.tptp.platform.probekit.registry.ProbeRegistryEntry; |
| 40 |
import org.eclipse.tptp.platform.probekit.util.InvalidProbeBundleException; |
| 41 |
import org.eclipse.tptp.platform.probekit.util.ProbeLaunchConfigString; |
| 42 |
import org.eclipse.tptp.platform.probekit.util.ProbekitConstants; |
| 43 |
import org.eclipse.tptp.platform.probekit.util.ProbekitDebugConfig; |
| 44 |
/** |
| 45 |
* This class replaces LaunchPad.java for the JVMTI implementation |
| 46 |
* of probekit profiling. After the agent attaches but before it calls |
| 47 |
* RESUME there is a small window of time in which we can send Commands to |
| 48 |
* the agent. This class is invoked during that time from the agentActive |
| 49 |
* call to the ProbeControlListener, notified at the start of this crucial |
| 50 |
* time. |
| 51 |
* |
| 52 |
* This class is used to first retrieve the ProbeID information stored |
| 53 |
* as an attribute in the LaunchConfiguration which is passed in to |
| 54 |
* agentActive. Once we have the ProbeIDs of all the selected probes, we |
| 55 |
* can look up the probe information for these probes stored in the |
| 56 |
* Probe Registry. |
| 57 |
* |
| 58 |
* Upon retrieving the registry entry, we have to wrap the class file |
| 59 |
* and probescript information as a String using a Base64 encoder so that it |
| 60 |
* can be sent as the commandData in the new execution framework |
| 61 |
* command protocol. |
| 62 |
* |
| 63 |
* The command creation and invocation of agent.sendCommand happens during |
| 64 |
* deployClasses() which uses the agent passed in to send a command |
| 65 |
* containing all of the necessary probe file information to the agent. |
| 66 |
* |
| 67 |
* @author Liz Dancy |
| 68 |
*/ |
| 69 |
public class LaunchPadJVMTI |
| 70 |
|
| 71 |
{ |
| 72 |
/* The active agent which is being used to send Commands */ |
| 73 |
private ICollector agent; |
| 74 |
/* The encoding to use */ |
| 75 |
private static final String ENCODING_NAME_UTF8 = "UTF-8"; |
| 76 |
|
| 77 |
/** |
| 78 |
* |
| 79 |
* @param currentAgent |
| 80 |
* @param config |
| 81 |
* @throws CoreException |
| 82 |
* @throws LaunchPadException |
| 83 |
*/ |
| 84 |
public void agentActive(ICollector currentAgent, ILaunchConfiguration config) throws CoreException, LaunchPadException |
| 85 |
{ |
| 86 |
{ |
| 87 |
agent= currentAgent; |
| 88 |
if(agent == null) |
| 89 |
// if the agent is null we want to stop right away |
| 90 |
{ |
| 91 |
throw new LaunchPadException("Agent not found"); |
| 92 |
} |
| 93 |
|
| 94 |
String probeID; |
| 95 |
ProbeRegistry registry = ProbeRegistry.getRegistry(); |
| 96 |
Map m = config.getAttributes(); |
| 97 |
|
| 98 |
// Extract the stuff we need to launch a probe from the configuration: |
| 99 |
// Right now we need the probe filter list and probe registry ID. |
| 100 |
String key = " "; |
| 101 |
|
| 102 |
Set keys = m.keySet(); |
| 103 |
Iterator iter = keys.iterator(); |
| 104 |
while (iter.hasNext()){ |
| 105 |
|
| 106 |
key = (String)iter.next(); |
| 107 |
|
| 108 |
// We are looking for this key: org.eclipse.tptp.platform.probekit.Probespec |
| 109 |
// Initially when we stored the attributes we stored each selected probe as a |
| 110 |
// String with this key as the prefix |
| 111 |
if(!key.startsWith(ProbeLaunchConfigString.AGENT_CONFIG_NAME_PREFIX)) |
| 112 |
{ |
| 113 |
continue; |
| 114 |
} |
| 115 |
Object value = m.get((Object)key); |
| 116 |
String probeFilters; |
| 117 |
ProbeLaunchConfigString probeConfig = ProbeLaunchConfigString.fromString(value.toString()); |
| 118 |
|
| 119 |
if(probeConfig.getType() == ProbeLaunchConfigString.TYPE_REGISTRY_ID) |
| 120 |
{ |
| 121 |
probeID = probeConfig.getRegistryId(); |
| 122 |
ProbeRegistryEntry registryEntry = registry.lookupById(probeID); |
| 123 |
if(null != registryEntry) |
| 124 |
{ |
| 125 |
deployProbe(registryEntry); |
| 126 |
} |
| 127 |
} |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
}// end method |
| 132 |
/** |
| 133 |
* Depolys a probe given a probe registry entry. |
| 134 |
* This method is usually called when somebody configures an application |
| 135 |
* to be launched with probes. It can be called more than once if several |
| 136 |
* probes were selected from the registry in the launch configuration. |
| 137 |
* |
| 138 |
* The right invokation point is the AgentActive callback (after the |
| 139 |
* agent was activated but before it processed the resume command. |
| 140 |
* |
| 141 |
* @param probeRegistryEntry - Probe Registry Entry |
| 142 |
* @throws LaunchPadException - Launchpad Exception |
| 143 |
* |
| 144 |
*/ |
| 145 |
public void deployProbe(ProbeRegistryEntry probeRegistryEntry) throws LaunchPadException |
| 146 |
{ |
| 147 |
try { |
| 148 |
//final String [] probeClassPath = new String [probeRegistryEntry.getFiles().length + 1];// used to hold the absolute path of the probe files |
| 149 |
Vector classPathVector = new Vector(); |
| 150 |
File probeScript = probeRegistryEntry.getProbescript(); |
| 151 |
File probeFiles[] = probeRegistryEntry.getFiles(); |
| 152 |
String probeScriptPath = probeScript.getAbsolutePath(); |
| 153 |
classPathVector.add(probeScriptPath); |
| 154 |
// add the probe script path first |
| 155 |
|
| 156 |
for(int i = 0; i < probeFiles.length; i++) |
| 157 |
{ |
| 158 |
if(LaunchPadUtils.isClass(probeFiles[i])) |
| 159 |
// then we want to add the classPath to probeClassPath |
| 160 |
// so that we have the probescript followed by |
| 161 |
// the probe class |
| 162 |
// followed by the second class file |
| 163 |
{ |
| 164 |
String filePath = ((File)probeFiles[i]).getAbsolutePath(); |
| 165 |
classPathVector.add(filePath); |
| 166 |
|
| 167 |
} |
| 168 |
|
| 169 |
} |
| 170 |
prepareFileArrays(classPathVector.toArray(), agent); |
| 171 |
} |
| 172 |
catch (InvalidProbeBundleException ex) |
| 173 |
{ |
| 174 |
LaunchPadUtils.trace("InvalidProbeBundleException in deployProbe()"); |
| 175 |
throw new LaunchPadException("Invalid registry entry:" + probeRegistryEntry.getId()); |
| 176 |
} |
| 177 |
} |
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
/** |
| 182 |
* This method is used to read in the class files and create two |
| 183 |
* arrays, one of the class files themselves and one of the file names |
| 184 |
* to pass to the method which sends the probekit commands to the agent |
| 185 |
*/ |
| 186 |
|
| 187 |
public void prepareFileArrays(Object [] probeClassPath, ICollector agent) |
| 188 |
{ |
| 189 |
try{ |
| 190 |
Vector classNames = new Vector(); |
| 191 |
Vector bytes = new Vector(); |
| 192 |
|
| 193 |
for (int i=0; i< probeClassPath.length; i++) |
| 194 |
{ |
| 195 |
if (probeClassPath[i] instanceof String){ |
| 196 |
String path = (String) probeClassPath[i]; |
| 197 |
// and all of the entries in probeClassPath should be Strings |
| 198 |
File f = new File(path); |
| 199 |
classNames.add(f.getName().replaceAll(".class$", " ")); |
| 200 |
bytes.add(readFileIntoBuffer(f)); |
| 201 |
} |
| 202 |
} |
| 203 |
|
| 204 |
|
| 205 |
deployProbeClasses(bytes, classNames, agent); |
| 206 |
} |
| 207 |
catch(FileNotFoundException e) |
| 208 |
{ |
| 209 |
// could be missing or invalid probes? |
| 210 |
} |
| 211 |
catch(IOException e){ |
| 212 |
// IOException reading in file |
| 213 |
} |
| 214 |
}// end method |
| 215 |
|
| 216 |
/** |
| 217 |
* A helper method to read a file into a buffer and produce an |
| 218 |
* array of bytes |
| 219 |
* @param file The give file |
| 220 |
* @return |
| 221 |
* @throws FileNotFoundException |
| 222 |
* @throws IOException |
| 223 |
*/ |
| 224 |
private static byte[] readFileIntoBuffer(File file) throws FileNotFoundException, IOException |
| 225 |
{ |
| 226 |
FileInputStream fs; |
| 227 |
byte buffer[]; |
| 228 |
|
| 229 |
fs = new FileInputStream(file); |
| 230 |
DataInputStream probeStream = new DataInputStream(fs); |
| 231 |
try { |
| 232 |
int bufferSize = (int)file.length(); |
| 233 |
buffer = new byte[bufferSize]; |
| 234 |
probeStream.read(buffer); |
| 235 |
} finally { |
| 236 |
probeStream.close(); |
| 237 |
} |
| 238 |
return buffer; |
| 239 |
} |
| 240 |
|
| 241 |
|
| 242 |
public static void deployProbeClasses(Vector bytes, Vector classNames, IAgent agent) |
| 243 |
{ |
| 244 |
try |
| 245 |
{ |
| 246 |
int count = 4; // magic |
| 247 |
for (int i = 0; i < bytes.size(); i++) |
| 248 |
{ |
| 249 |
String className = (String)classNames.get(i); |
| 250 |
byte[] classBytes =(byte[]) bytes.get(i); |
| 251 |
byte classNameBytes[] = className.getBytes(ENCODING_NAME_UTF8); |
| 252 |
count += 4 + classNameBytes.length + 4 + classBytes.length; |
| 253 |
} |
| 254 |
|
| 255 |
// Allocate buffer |
| 256 |
ByteBuffer buffer = ByteBuffer.allocate(count); // tmp |
| 257 |
buffer.putInt(0xDECAECC0); |
| 258 |
|
| 259 |
for (int i = 0; i < bytes.size(); i++) |
| 260 |
{ |
| 261 |
String className = (String)classNames.get(i); |
| 262 |
byte[] classBytes =(byte[]) bytes.get(i); |
| 263 |
byte classNameBytes[] = className.getBytes(ENCODING_NAME_UTF8); |
| 264 |
/* |
| 265 |
int commandSize =4 + // magic |
| 266 |
4 + // className length |
| 267 |
classNameBytes.length + |
| 268 |
4 + // class size |
| 269 |
classBytes.length |
| 270 |
; |
| 271 |
*/ |
| 272 |
|
| 273 |
// Populate buffer with data |
| 274 |
buffer.putInt(classNameBytes.length).put(classNameBytes); |
| 275 |
buffer.putInt(classBytes.length).put(classBytes); |
| 276 |
} |
| 277 |
|
| 278 |
// Prepare RAC command |
| 279 |
String commandData = new CustomBase64Encoder().encode(buffer.array()); |
| 280 |
/// |
| 281 |
try { |
| 282 |
BufferedWriter out = new BufferedWriter(new FileWriter("C:/fileWriter2.txt")); |
| 283 |
out.write(commandData); |
| 284 |
out.close(); |
| 285 |
} |
| 286 |
catch (IOException e) |
| 287 |
{ |
| 288 |
|
| 289 |
} |
| 290 |
/// |
| 291 |
|
| 292 |
CommandFragment command = new CommandFragment(); |
| 293 |
command.setCommand( |
| 294 |
"<applyOptions iid='org.eclipse.tptp.jvmti'>" + |
| 295 |
"<option><name>PROBEKIT_CLASS</name>" + |
| 296 |
"<value>" + commandData + "</value><option></applyOptions>"); |
| 297 |
|
| 298 |
// Invoke RAC command- this is using the new sendCommand from the new execution framework |
| 299 |
agent.sendCommand(command.buildCommand(), new ICommandHandler() { |
| 300 |
public void incomingCommand(INode node, ICommandElement element) |
| 301 |
{ |
| 302 |
//Handler goes here |
| 303 |
} |
| 304 |
} |
| 305 |
); |
| 306 |
}// end try |
| 307 |
catch (Exception e) |
| 308 |
{ |
| 309 |
System.out.println(e.getMessage() + "is the error message"); |
| 310 |
} |
| 311 |
}// end method deployProbeClasses |
| 312 |
}// end class |
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|