2014-12-07 44 views
1

我是ASM和字節碼操作的新手。我的任務非常簡單:我告訴我的代理訪問什麼類和方法,並測量該方法的執行時間。這項測量是通過Guava圖書館的秒錶課程完成的。 ASM基本上圍繞方法體,方法是在開始時啓動秒錶,並在方法體的末尾停止並打印執行時間。這在一些方法上有效,但在大多數方法上失敗了。ASM字節碼操作:測量方法執行速度

這是ASM訪問目標方法開始處的部分。

package com.agent.agentclasses; 


import org.objectweb.asm.MethodVisitor; 
import org.objectweb.asm.Opcodes; 
import org.objectweb.asm.commons.LocalVariablesSorter; 
import org.objectweb.asm.Type; 

public class ModifierMethodWriter extends LocalVariablesSorter /*MethodVisitor*/ { 
    private int time; 
    protected ModifierMethodWriter(int api, int access, String desc, MethodVisitor mv) { 
     super(api, access, desc, mv); 
    } 


    @Override 
    public void visitCode() { 
     System.out.println("I am @ModifierMethodWriter!"); 

     /* 
     * Guava - beginning of the method ! 
     */ 
     time = newLocal(Type.getObjectType("stopwatch")); 


     super.visitMethodInsn(Opcodes.INVOKESTATIC, "com/google/common/base/Stopwatch", "createStarted", "()Lcom/google/common/base/Stopwatch;", false); 

     super.visitVarInsn(Opcodes.ASTORE, time); 
     super.visitCode(); 




    } 

添加代碼到最後!

package com.agent.agentclasses; 

import org.objectweb.asm.MethodVisitor; 
import org.objectweb.asm.Opcodes; 
import org.objectweb.asm.commons.AdviceAdapter; 

public class AddCodeBeforeReturn extends AdviceAdapter { 
    public AddCodeBeforeReturn(int api, MethodVisitor mv, 
      int acc, String name, String desc){ 

     super(api, mv, acc, name, desc); 

    } 
    @Override 
    protected void onMethodExit(int opcode) { 
     /* 
     * Guava - Before return ! 
     */ 
     super.visitFieldInsn(Opcodes.GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;"); 
     super.visitIntInsn(Opcodes.ALOAD, 2); 
     super.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "com/google/common/base/Stopwatch", "stop", "()Lcom/google/common/base/Stopwatch;", false); 
     super.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "com/google/common/base/Stopwatch", "toString", "()Ljava/lang/String;", false); 
     super.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/io/PrintStream", "println", "(Ljava/lang/String;)V", false); 
    } 
} 

這是測試類,我想測試我的經紀人:這是HSQL DB類,我想衡量全選()方法的執行時間。

package com.agent.database; 

import java.sql.Connection; 
import java.sql.DriverManager; 
import java.sql.ResultSet; 
import java.sql.Statement; 
import com.agent.testclasses.*; 
public class Database { 

    private Connection connection = null; 
    private ResultSet resultSet = null; 
    private Statement statement = null; 
    private String createtablestr = " CREATE TABLE IF NOT EXISTS Students(Id int,Name varchar(255));"; 
    private static int id = 1; 

    public String connect() { 
     System.out.println("I am in connect!"); 
     try{ 
     Class.forName("org.hsqldb.jdbcDriver"); 
     connection = DriverManager.getConnection("jdbc:hsqldb:file:C:/hsqldb/studentdb", "sa", ""); 
     if (connection == null) 
     { 

      return "Error: Connection failed."; 
     } 
      statement = connection.createStatement(); 
      statement.executeUpdate(createtablestr); 
     }catch(Throwable ex){ 
      ex.printStackTrace(); 
     } 

     return "Connection succesful!"; 
    } 

    public String insert(Student student){ 
     connect(); 
     try { 

      String insertStr = "INSERT INTO Students VALUES ('"+ id +"','"+ student.getName() + "')"; 
      statement.executeUpdate(insertStr); 
      id++; 
     }catch(Throwable ex){ex.printStackTrace(); return "Error: Data was not written!";} 

     return "Data written succesfully!"; 
    } 

    public String selectAll() { 


     connect(); 
     String outputHTML=""; 
     try{ 


      resultSet = statement.executeQuery("SELECT * FROM Students"); 


      while (resultSet.next()) 
      { 
       outputHTML+= "<tr>" + "<td>" + resultSet.getString("Id") + "</td>" + "<td>" + resultSet.getString("Name") + "</td>" + "</tr>"; 
      } 



      resultSet.close(); 
      connection.commit(); 
      connection.close(); 


     } 
     catch (Throwable e) 
     { 
      e.printStackTrace(); 
     } 

     return outputHTML; 
    } 
} 

最後,該JVM thorws例外:

Exception in thread "main" java.lang.VerifyError: Stack map does not match the one at exception handler 85 
    Exception Details: 
     Location: 
     com/agent/database/Database.connect()Ljava/lang/String; @85: astore_2 
     Reason: 
     Type top (current frame, locals[1]) is not assignable to 'stopwatch' (stack map, locals[1]) 
     Current Frame: 
     bci: @12 
     flags: { } 
     locals: { 'com/agent/database/Database', top, 'com/google/common/base/Stopwatch' } 
     stack: { 'java/lang/Throwable' } 
     Stackmap Frame: 
     bci: @85 
     flags: { } 
     locals: { 'com/agent/database/Database', 'stopwatch' } 
     stack: { 'java/lang/Throwable' } 
     Bytecode: 
     0x0000000: b800 2a4d b200 3212 34b6 003a 123c b800 
     0x0000010: 4257 2a12 4412 4612 48b8 004e b500 182a 
     0x0000020: b400 18c7 0014 1250 b200 3219 02b6 0053 
     0x0000030: b600 56b6 003a b02a 2ab4 0018 b900 5e01 
     0x0000040: 00b5 001c 2ab4 001c 2ab4 0020 b900 6402 
     0x0000050: 0057 a700 084d 2cb6 0067 1269 b200 3219 
     0x0000060: 02b6 0053 b600 56b6 003a b0    
     Exception Handler Table: 
     bci [12, 38] => handler: 85 
     bci [55, 82] => handler: 85 
     Stackmap Table: 
     append_frame(@55,Object[#88]) 
     same_locals_1_stack_item_frame(@85,Object[#44]) 
     same_frame(@90) 

     at com.agent.testers.tester1.main(tester1.java:15) 

在我看來,那ASM創建在秒錶局部變量表新條目後,JVM嘗試與本地覆蓋它我想要的方法的變量(在這種情況下selectAll())。因此:

類型頂部(當前幀,當地人[1])是不能分配給 '秒錶' (堆棧地圖,當地人[1])

我不知道這是否是實際問題與否,但我需要解決它!最後,我的代理應該可以用任何方法插入。所有的幫助將不勝感激!謝謝。

回答

1

您正在新分配的'time'局部變量中創建'StopWatch'實例,然後從本地變量插槽2加載其實例,並且應該在'super.visitCode()'後面添加代碼。

另見ASM FAQ Why do I get the xxx verifier error更多的細節如何使用CheckClassAdapter調試你的字節碼轉換。

+0

尤金你好!首先感謝你的迴應!其次,我有點困惑,因爲我不' – pvammus 2014-12-11 17:17:16