Hi!
My name is Bruno Neves, I am a Phd. Student at Federal University Of Rio
Grande.
I need build a instrumenter using BCEL to generate a instrucion
(bytecode) trace file of some applications. So, I'd like to know if
someone have already contructed this kind of tool using BCEL and can
give me the code or some (suggestion) to help me fix my code (attached
file).
I have interest in see more small examples of using BCEL too.
Tank you of any help!
regards,
Bruno.
________________
import java.io.*;
import java.util.Iterator;
import org.apache.bcel.classfile.*;
import org.apache.bcel.generic.*;
import org.apache.bcel.Repository;
import org.apache.bcel.util.InstructionFinder;
public class MakeTrace {
static ClassParser p;
static JavaClass jc;
static ClassGen cg;
static MethodGen mg;
static Method method;
static ConstantPoolGen cp;
public static void main(String[] args) {
String path = "C:\\a\\i\\";
File in_dir = new File(path);
String filelist[] = in_dir.list();
int numClassFiles = 0;
for (int i = 0; i < filelist.length; i++) { //
String filename = filelist[i];
if (filename.endsWith(".class") &&
!filename.equals("MyCounter.class")) {
numClassFiles++;
}
}
String filesToInstrument[] = new String[numClassFiles];
int counter = 0;
for (int i = 0; i < filelist.length; i++) { //
String filename = filelist[i];
if (filename.endsWith(".class") &&
!filename.equals("MyCounter.class")) {
int pos = filename.indexOf(".");
String nameClass = filename.substring(0, pos);
filesToInstrument[counter] = nameClass;
counter++;
Instrument("C:\\a\\i\\", "C:\\a\\o\\", filename);
}
}
}
public static void Instrument(String pathIn, String pathOut, String
classFile) {
try{
//........READING CLASSFILE
p = new ClassParser(classFile);
jc = p.parse();
cg = new ClassGen(jc);
cp = new ConstantPoolGen(jc.getConstantPool());
//........INSTRUMMENTING CLASSFILE
//instrMet(cg, cp, jc);
instrMet();
//........FINISHING AND SAVING NEW CLASSFILE
JavaClass jc1 = cg.getJavaClass();
jc1.setConstantPool(cp.getFinalConstantPool());
jc1.dump(pathOut + classFile); //PODE DAR ERRO (MESMO
CLASSFILE DA ABERTURA)
System.out.println(classFile + " instrumented!");
}catch (IOException o){
System.out.println("Erro I/O lendo: " + classFile);
}
}
public static void instrMet() {
Method[] methods = jc.getMethods();
for(int i=0; i < methods.length; i++) {
method = cg.containsMethod(methods[i].getName(),
methods[i].getSignature());
System.out.println("metName: " + method.getName());
mg = new MethodGen(method, jc.getClassName(), cp);
if (method.getName().equals("<init>")){
//instrInstr(true);
continue;
}else{
instrInstr(false);
}
//instrInstr(mg, cg);
}
}
public static void instrInstr(boolean isConstructor) {
InstructionList il = mg.getInstructionList();
InstructionHandle[] ihs = il.getInstructionHandles();
for(int i=0; i < ihs.length; i++) {
InstructionHandle ih = ihs[i];
Instruction instr = ih.getInstruction();
InstructionFactory f = new InstructionFactory(cg, cp);
if (isConstructor){
if (i >= 2){
il.insert(instr, f.createPrintln("instrName: " +
instr.getName() + " Opcode: " + instr.getOpcode()));
}else{
il.append(f.createPrintln("instrName: " +
instr.getName() + " Opcode: " + instr.getOpcode()));
}
}else{
il.insert(instr, f.createPrintln("instrName: " +
instr.getName() + " Opcode: " + instr.getOpcode()));
//il.append(f.createPrintln("Inseriu instrução!!!"));
}
System.out.println("instrName: " + instr.getName() + "
Opcode: " + instr.getOpcode());
}
mg.setInstructionList(il);
// mg.
cg.removeMethod(method);
cg.addMethod(mg.getMethod());
}
}
/*
* Created on 07/11/2005
*
* To change the template for this generated file go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
package traceTool;
/**
* @author Administrador
*
* To change the template for this generated type comment go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
import java.io.*;
import java.util.Iterator;
import org.apache.bcel.classfile.*;
import org.apache.bcel.generic.*;
import org.apache.bcel.Repository;
import org.apache.bcel.util.InstructionFinder;
public class MakeTrace {
static ClassParser p;
static JavaClass jc;
static ClassGen cg;
static MethodGen mg;
static Method method;
static ConstantPoolGen cp;
//THIS PART IS TESTED AND OK
//THE MAIN METHOD ONLY READ ALL .CLASS IN A INPATH DIRECTORY AND CALL THE INSTRUMENTER FOR EACH CLASS FILE
public static void main(String[] args) {
String path = "C:\\a\\i\\";
File in_dir = new File(path);
String filelist[] = in_dir.list();
int numClassFiles = 0;
for (int i = 0; i < filelist.length; i++) { //
String filename = filelist[i];
if (filename.endsWith(".class") && !filename.equals("PrintTrace.class")) {
numClassFiles++;
}
}
String filesToInstrument[] = new String[numClassFiles];
int counter = 0;
for (int i = 0; i < filelist.length; i++) { //
String filename = filelist[i];
if (filename.endsWith(".class") && !filename.equals("PrintTrace.class")) {
int pos = filename.indexOf(".");
String nameClass = filename.substring(0, pos);
filesToInstrument[counter] = nameClass;
counter++;
//CALLING THE INSTRUMENTER
Instrument("C:\\a\\i\\", "C:\\a\\o\\", filename);
}
}
}
//METHOD INSTRUMENT
public static void Instrument(String pathIn, String pathOut, String classFile) {
try{
//........READING CLASSFILE
p = new ClassParser(classFile);
jc = p.parse();
cg = new ClassGen(jc);
cp = new ConstantPoolGen(jc.getConstantPool());
//........INSTRUMMENTING CLASSFILE
//instrMet(cg, cp, jc);
instrMet();
//........FINISHING AND SAVING NEW CLASSFILE
JavaClass jc1 = cg.getJavaClass();
jc1.setConstantPool(cp.getFinalConstantPool());
jc1.dump(pathOut + classFile); //PODE DAR ERRO (MESMO CLASSFILE DA ABERTURA)
System.out.println(classFile + " instrumented!");
}catch (IOException o){
System.out.println("Error I/O reading: " + classFile);
}
}
//THIS METHOD CREATE THE METHODGEN FOR EDITING AND SENDS IT TO METHOD THAT EDITS THE INSTRUCIONS
//I'M NOT EDITING CONSTRUCTORS AT THIS MOMENT
public static void instrMet() {
Method[] methods = jc.getMethods();
for(int i=0; i < methods.length; i++) {
method = cg.containsMethod(methods[i].getName(), methods[i].getSignature());
//System.out.println("metName: " + method.getName());
mg = new MethodGen(method, jc.getClassName(), cp);
if (method.getName().equals("<init>")){
//instrInstr(true);
continue;
}else{
instrInstr(false);
}
//instrInstr(mg, cg);
}
}
//THIS METHOD INSERT "SYSTEM.OUT.PRINTLN" CALLS BEFORE EACH INSTRUCTIONS
public static void instrInstr(boolean isConstructor) {
InstructionList il = mg.getInstructionList();
InstructionHandle[] ihs = il.getInstructionHandles();
for(int i=0; i < ihs.length; i++) {
InstructionHandle ih = ihs[i];
Instruction instr = ih.getInstruction();
InstructionFactory f = new InstructionFactory(cg, cp);
il.insert(instr, f.createPrintln("instrName: " + instr.getName() + " Opcode: " + instr.getOpcode()));
//System.out.println("instrName: " + instr.getName() + " Opcode: " + instr.getOpcode());
}
mg.setInstructionList(il);
cg.removeMethod(method);
cg.addMethod(mg.getMethod());
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]