1 19 20 package soot.jimple.spark.pag; 21 import java.util.*; 22 import soot.jimple.spark.*; 23 import soot.*; 24 import soot.util.*; 25 import soot.jimple.spark.sets.*; 26 import java.io.*; 27 import java.util.jar.*; 28 import java.util.zip.*; 29 30 33 public class PAG2HTML { 34 public PAG2HTML( PAG pag, String output_dir ) { 35 this.pag = pag; 36 this.output_dir = output_dir; 37 } 38 public void dump() { 39 for( Iterator vIt = pag.getVarNodeNumberer().iterator(); vIt.hasNext(); ) { 40 final VarNode v = (VarNode) vIt.next(); 41 mergedNodes.put( v.getReplacement(), v ); 42 if( v instanceof LocalVarNode ) { 43 SootMethod m = ((LocalVarNode)v).getMethod(); 44 if( m != null ) { 45 methodToNodes.put( m, v ); 46 } 47 } 48 } 49 try { 50 JarOutputStream jarOut = new JarOutputStream( 51 new FileOutputStream( new File(output_dir, "pag.jar") ) ); 52 for( Iterator vIt = mergedNodes.keySet().iterator(); vIt.hasNext(); ) { 53 final VarNode v = (VarNode) vIt.next(); 54 dumpVarNode( v, jarOut ); 55 } 56 for( Iterator mIt = methodToNodes.keySet().iterator(); mIt.hasNext(); ) { 57 final SootMethod m = (SootMethod) mIt.next(); 58 dumpMethod( m, jarOut ); 59 } 60 addSymLinks( pag.getVarNodeNumberer().iterator(), jarOut ); 61 jarOut.close(); 62 } catch( IOException e ) { 63 throw new RuntimeException ( "Couldn't dump html"+e ); 64 } 65 } 66 67 68 69 70 71 protected PAG pag; 72 protected String output_dir; 73 protected MultiMap mergedNodes = new HashMultiMap(); 74 protected MultiMap methodToNodes = new HashMultiMap(); 75 76 protected void dumpVarNode( VarNode v, JarOutputStream jarOut ) throws IOException { 77 jarOut.putNextEntry( new ZipEntry( "nodes/n"+v.getNumber()+".html" ) ); 78 final PrintWriter out = new PrintWriter( jarOut ); 79 out.println( "<html>" ); 80 81 out.println( "Green node for:" ); 82 out.println( varNodeReps( v ) ); 83 84 out.println( "Declared type: "+v.getType() ); 85 86 out.println( "<hr>Reaching blue nodes:" ); 87 out.println( "<ul>" ); 88 v.getP2Set().forall( new P2SetVisitor() { 89 public final void visit( Node n ) { 90 out.println( "<li>"+htmlify(n.toString()) ); 91 } 92 } ); 93 out.println( "</ul>" ); 94 95 out.println( "<hr>Outgoing edges:" ); 96 Node[] succs = pag.simpleLookup( v ); 97 for( int i = 0; i < succs.length; i++ ) { 98 VarNode succ = (VarNode) succs[i]; 99 out.println( varNodeReps( succ ) ); 100 } 101 102 out.println( "<hr>Incoming edges: " ); 103 succs = pag.simpleInvLookup( v ); 104 for( int i = 0; i < succs.length; i++ ) { 105 VarNode succ = (VarNode) succs[i]; 106 out.println( varNodeReps( succ ) ); 107 } 108 109 out.println( "</html>" ); 110 out.flush(); 111 } 112 protected String varNodeReps( VarNode v ) { 113 StringBuffer ret = new StringBuffer (); 114 ret.append( "<ul>\n" ); 115 for( Iterator vvIt = mergedNodes.get( v ).iterator(); vvIt.hasNext(); ) { 116 final VarNode vv = (VarNode) vvIt.next(); 117 ret.append( varNode( "", vv ) ); 118 } 119 ret.append( "</ul>\n" ); 120 return ret.toString(); 121 } 122 protected String varNode( String dirPrefix, VarNode vv ) { 123 StringBuffer ret = new StringBuffer (); 124 ret.append( "<li><a HREF=\""+dirPrefix+"n"+vv.getNumber()+".html\">" ); 125 ret.append( ""+htmlify(vv.getVariable().toString()) ); 126 ret.append( "</a><br>" ); 127 ret.append( "<li>Context: " ); 128 ret.append( ""+(vv.context() == null ?"null":htmlify(vv.context().toString()) ) ); 129 ret.append( "</a><br>" ); 130 if( vv instanceof LocalVarNode ) { 131 LocalVarNode lvn = (LocalVarNode) vv; 132 SootMethod m = lvn.getMethod(); 133 if( m != null ) { 134 ret.append( "<a HREF=\"../" 135 +toFileName(m.toString() )+".html\">" ); 136 ret.append( htmlify(m.toString())+"</a><br>" ); 137 } 138 } 139 ret.append( htmlify(vv.getType().toString())+"\n" ); 140 return ret.toString(); 141 } 142 protected static String htmlify( String s ) { 143 StringBuffer b = new StringBuffer ( s ); 144 for( int i = 0; i < b.length(); i++ ) { 145 if( b.charAt( i ) == '<' ) { 146 b.replace( i, i+1, "<" ); 147 } 148 if( b.charAt( i ) == '>' ) { 149 b.replace( i, i+1, ">" ); 150 } 151 } 152 return b.toString(); 153 } 154 protected void dumpMethod( SootMethod m, JarOutputStream jarOut ) throws IOException { 155 jarOut.putNextEntry( new ZipEntry( 156 ""+toFileName( m.toString() )+".html" ) ); 157 final PrintWriter out = new PrintWriter( jarOut ); 158 out.println( "<html>" ); 159 160 out.println( "This is method "+htmlify( m.toString() )+"<hr>" ); 161 for( Iterator it = methodToNodes.get( m ).iterator(); it.hasNext(); ) { 162 out.println( varNode( "nodes/", (VarNode) it.next() ) ); 163 } 164 out.println( "</html>" ); 165 out.flush(); 166 } 167 protected void addSymLinks( Iterator nodes, JarOutputStream jarOut ) throws IOException { 168 jarOut.putNextEntry( new ZipEntry( "symlinks.sh" ) ); 169 final PrintWriter out = new PrintWriter( jarOut ); 170 out.println( "#!/bin/sh" ); 171 while( nodes.hasNext() ) { 172 VarNode v = (VarNode) nodes.next(); 173 VarNode rep = (VarNode) v.getReplacement(); 174 if( v != rep ) { 175 out.println( "ln -s n"+rep.getNumber()+".html n"+v.getNumber()+".html" ); 176 } 177 } 178 out.flush(); 179 } 180 protected String toFileName( String s ) { 181 StringBuffer ret = new StringBuffer (); 182 for( int i = 0; i < s.length(); i++ ) { 183 char c = s.charAt( i ); 184 if( c == '<' ) ret.append( '{' ); 185 else if( c == '>' ) ret.append( '}' ); 186 else ret.append( c ); 187 } 188 return ret.toString(); 189 } 190 } 191 192 | Popular Tags |