[Soot-list] Question about using Soot for instrumentation

Alvin Yan feiya200 at cs.uregina.ca
Sat Apr 5 00:50:59 EDT 2008


Definitely you can. You can insert Unit to an ExceptionalUnitGraph (it has 
heads and tails). Or you can insert Unit to a unitchain without building 
UnitGraph, then the head of the method would be the first unit in the chain, 
then the tail of the method would be any unit that instanceof ReturnVoidStmt 
or ReturnStmt.

When I first learnt to use soot I wrote this piece of code to rewrite a 
given class to add a System.out.println("Hello World") in the beginning of 
every method.

Scene.v().loadClassAndSupport("java.lang.System");
SootClass sootclass = Scene.v().loadClassAndSupport("isorem.test.Test");
Scene.v().loadNecessaryClasses();

Iterator<SootMethod> methodIt = sootclass.getMethods().iterator();
while (methodIt.hasNext()) {
	SootMethod m = methodIt.next();
	JimpleBody body = (JimpleBody) m.retrieveActiveBody();//this will raise an 
exception if there's no method body, such as interface/abstract method
	System.out.println("=======================================");
	System.out.println("Rewriting method: " + m.getName());
	Chain<Unit> units = body.getUnits();

	// Add locals, java.io.printStream tmpRef
	Local tmpRef = Jimple.v().newLocal("tmpRef",
			RefType.v("java.io.PrintStream"));
	body.getLocals().add(tmpRef);
	// add "tmpRef = java.lang.System.out"
	Unit u1 = (Jimple.v().newAssignStmt(tmpRef ,Jimple.v().newStaticFieldRef(
			Scene.v().getField("<java.lang.System: java.io.PrintStream 
out>").makeRef())));
	if(Modifier.isStatic(m.getModifiers())){
		units.addFirst(u1);
	}else{
		units.insertAfter(u1, units.getFirst());//because the first one is r0 = 
@this, need to step over it, otherwise there'll be no "this" ref
	}
	// insert "tmpRef.println("Hello world!")"
	SootMethod toCall = Scene.v().getMethod(
			"<java.io.PrintStream: void println(java.lang.String)>");
	units.insertAfter(((Jimple.v().newInvokeStmt(Jimple.v().newVirtualInvokeExpr(
			tmpRef,toCall.makeRef(),StringConstant.v("Entering method: "+ 
m.getName()))))), u1);

	// the following is for exiting method
	// add "tmpRef = java.lang.System.out"
	u1 = (Jimple.v().newAssignStmt(tmpRef ,Jimple.v().newStaticFieldRef(
			Scene.v().getField("<java.lang.System: java.io.PrintStream 
out>").makeRef())));
	units.insertBefore(u1, units.getLast());
	// insert "tmpRef.println("Hello world!")"
	toCall = Scene.v().getMethod(
			"<java.io.PrintStream: void println(java.lang.String)>");
	units.insertAfter(((Jimple.v().newInvokeStmt(Jimple.v().newVirtualInvokeExpr(
			tmpRef,toCall.makeRef(),StringConstant.v("Exiting method: "+ 
m.getName()))))), u1);
}
SootSupportUtil.createNecessaryDirectories(sootclass.getName());
String fileName = SourceLocator.v().getFileNameFor(sootclass,
		Options.output_format_class);
OutputStream streamOut = new JasminOutputStream(new FileOutputStream(
		fileName));
PrintWriter writerOut = new PrintWriter(new OutputStreamWriter(
		streamOut));
JasminClass jasminClass = new soot.jimple.JasminClass(sootclass);
jasminClass.print(writerOut);
writerOut.flush();
streamOut.close();



Hope this helps.

Alvin Yan



--------------------------------------------------
From: "Bharathi Seshadri" <seshadri_bharathi at yahoo.com>
Sent: Friday, April 04, 2008 6:14 PM
To: <soot-list at sable.mcgill.ca>
Subject: [Soot-list] Question about using Soot for instrumentation

> Hi,
>
> I'm new to soot. I'm trying to explore ways of using
> Soot to add code to a Java program to do some kind of
> dynamic tracing.
>
> I see that Soot can be used to instrument class files.
>
> Can I use soot to match a particular signature and add
> code to extract arguments and return values of that
> function (i.e., add code to prologue and epilogue of
> the matched function to extract some information out
> of it)?
>
> I would appreciate if someone could let me know if
> this level of an instrumentation is feasible with soot
> and if there are any relevant examples available that
> I can refer to in this regard.
>
> Thanks much,
> Bharathi
>
>
>
>
>
>
> 
> ____________________________________________________________________________________
> You rock. That's why Blockbuster's offering you one month of Blockbuster 
> Total Access, No Cost.
> http://tc.deals.yahoo.com/tc/blockbuster/text5.com
> _______________________________________________
> Soot-list mailing list
> Soot-list at sable.mcgill.ca
> http://mailman.cs.mcgill.ca/mailman/listinfo/soot-list
> 


More information about the Soot-list mailing list