[Soot-list] Can I ask for a code snippet of creating call graph for a library (no main class)?

Yi Lin qinsoon at gmail.com
Sun Jul 1 21:43:10 EDT 2012


Hi,

I got control flow graph in soot. It seems to provide enough information 
that I need, but it is not ideal.

What I am trying to do is writing a Java-to-C compiler (not for the 
whole Java language but only a subset of Java). Thus I need structural 
representation of methods and classes. If the structural representation 
is closer to source code, the code generation would be more 
straightforward and the generated code would be more readable (though no 
one is supposed to read them). The structural representation could be 
AST or control flow graph. I could work on control flow graph, but it is 
based on some level of IR in soot. I would prefer something based on 
original bytecode or source code. I assume soot should maintain 
structural representation for bytecode or source code it scans.

I am just quite new to this field. Any suggestions, advices and 
critiques are welcome. Thanks.

Regards,
Yi

On 29/06/12 18:23 , Z wrote:
> Hello, I don't understand. Did you mean you will need a control flow 
> graph rather than AST to do your transformation?
>
> Zell.
>
> On Fri, Jun 29, 2012 at 7:26 AM, Yi Lin <qinsoon at gmail.com 
> <mailto:qinsoon at gmail.com>> wrote:
>
>     Hi,
>
>     I finally solved this. I will attach my code at the end (it may help
>     others perhaps, and any improvements are welcome).
>
>     But I now have another problem with Soot. Basically I need AST and
>     call
>     graph for my work. I got call graph now, but how can I get AST from
>     soot? I see that Dava could decompile bytecode and construct AST.
>     But I
>     assume soot should maintian ASTs when it do transformation. What is a
>     straightforward way to get AST in soot?
>
>     Thank you very much.
>
>     Regards,
>     Yi
>
>     -------------------
>     code for generating call graph for library (no main class):
>
>     build call graph:
>         // PackManager manages the packs containing the various phases and
>     their options
>         PackManager.v().getPack("wjtp").add(new Transform("wjtp.myTrans",
>     new SceneTransformer() {
>           @Override
>           protected void internalTransform(String phaseName, Map
>     options) {
>             CHATransformer.v().transform();
>           }
>         }));
>
>         Options.v().set_whole_program(true);
>         Options.v().set_process_dir(Arrays.asList(libDir)); //library dir
>     Options.v().set_soot_classpath("../rt.jar:"+libDir+":../jce.jar:.");
>     //library dir along with rt.jar/jce.jar from JRE/JDK
>
>         Scene.v().setEntryPoints(getAllMethods());
>         Scene.v().loadNecessaryClasses();
>         PackManager.v().getPack("wjtp").apply();
>
>     get all methods (as entrypoints):
>
>     public List<SootMethod> getAllMethods() {
>         ArrayList<SootMethod> entrypoints = new ArrayList<SootMethod>();
>         for (String klassName : allClasses) {
>             // klassName such as org.abc.MyClass
>             Scene.v().forceResolve(klassName, SootClass.SIGNATURES);
>             SootClass klass = Scene.v().getSootClass(klassName);
>
>             // adding all non-abstract method as entrypoint
>             for (SootMethod m : klass.getMethods()) {
>               if (!m.isAbstract()) {
>                 entrypoints.add(m);
>               }
>             }
>           }
>         return entrypoints;
>     }
>
>     use call graph:
>
>         SootClass klass =
>     Scene.v().getSootClass(someClassYouAreInterested);
>         SootMethod method =
>     klass.getMethodByName(someMethodYouAreInterested);
>         CallGraph cg = Scene.v().getCallGraph();
>         Iterator<MethodOrMethodContext> sources = new
>     Sources(cg.edgesInto(focus));
>         while (sources.hasNext()) {
>           SootMethod src = (SootMethod)sources.next();
>           System.out.println(method + " is called by " + src);
>         }
>
>     On 26/06/12 14:17 , Z wrote:
>     > It  might be more efficient that you learn them like -w, entrypoints
>     > etc... one by one. Most of them can be found in the survivor's
>     guide.
>     >
>     > If you don't know whether to use such such options, try them!
>     >
>     > Cheers,
>     > Zell.
>     >
>     >> long list of questions, like whether to use whole program mode
>     '-w',
>     >> what should be set as 'entrypoints', whether to use
>     >> CHATransformer.v().transform() or
>     >> PhaseOptions.v().setPhaseOption("cg.cha", "enabled:true") then
>     >> runPacks(), what is the timing to do that, etc. A code snippet
>     would
>     >> answer all those annoying questions for me...
>     >>
>     >> Thank you very much.
>     >>
>     >> Regards,
>     >> Yi
>     >> _______________________________________________
>     >> Soot-list mailing list
>     >> Soot-list at sable.mcgill.ca <mailto:Soot-list at sable.mcgill.ca>
>     >> http://mailman.cs.mcgill.ca/mailman/listinfo/soot-list
>     > _______________________________________________
>     > Soot-list mailing list
>     > Soot-list at sable.mcgill.ca <mailto:Soot-list at sable.mcgill.ca>
>     > http://mailman.cs.mcgill.ca/mailman/listinfo/soot-list
>
>
>     _______________________________________________
>     Soot-list mailing list
>     Soot-list at sable.mcgill.ca <mailto:Soot-list at sable.mcgill.ca>
>     http://mailman.cs.mcgill.ca/mailman/listinfo/soot-list
>
>
>
>
> _______________________________________________
> Soot-list mailing list
> Soot-list at sable.mcgill.ca
> http://mailman.cs.mcgill.ca/mailman/listinfo/soot-list


-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mailman.cs.mcgill.ca/pipermail/soot-list/attachments/20120702/45a89d18/attachment.html 


More information about the Soot-list mailing list