2012-12-17 57 views
8

我試圖獲取Java程序方法參數的值。 我正在使用ASM來測試字節碼並獲取這些值。 但是,我遇到了一些麻煩。ASM中的Java方法參數值

這是用於檢測代碼的visitCode()方法。 它在做什麼:

  1. 創建一個空數組來存儲收集的參數。
  2. 對於每個參數,將其值加載到數組中。
  3. 將此數組發送到我的代理的OnMethodEntry方法(其中將使用值)。

@Override 
public void visitCode() { 
    int paramLength = paramTypes.length; 

    // Create array with length equal to number of parameters 
    mv.visitIntInsn(Opcodes.BIPUSH, paramLength); 
    mv.visitTypeInsn(Opcodes.ANEWARRAY, "java/lang/Object"); 
    mv.visitVarInsn(Opcodes.ASTORE, paramLength); 

    // Fill the created array with method parameters 
    int i = 0; 
    for (Type tp : paramTypes) { 
     mv.visitVarInsn(Opcodes.ALOAD, paramLength); 
     mv.visitIntInsn(Opcodes.BIPUSH, i); 

     if (tp.equals(Type.BOOLEAN_TYPE) || tp.equals(Type.BYTE_TYPE) || tp.equals(Type.CHAR_TYPE) || tp.equals(Type.SHORT_TYPE) || tp.equals(Type.INT_TYPE)) 
      mv.visitVarInsn(Opcodes.ILOAD, i); 
     else if (tp.equals(Type.LONG_TYPE)) { 
      mv.visitVarInsn(Opcodes.LLOAD, i); 
      i++; 
     } 
     else if (tp.equals(Type.FLOAT_TYPE)) 
      mv.visitVarInsn(Opcodes.FLOAD, i); 
     else if (tp.equals(Type.DOUBLE_TYPE)) { 
      mv.visitVarInsn(Opcodes.DLOAD, i); 
      i++; 
     } 
     else 
      mv.visitVarInsn(Opcodes.ALOAD, i); 

     mv.visitInsn(Opcodes.AASTORE); 
     i++; 
    } 

    // Load id, class name and method name 
    this.visitLdcInsn(new Integer(this.methodID)); 
    this.visitLdcInsn(this.className); 
    this.visitLdcInsn(this.methodName); 

    // Load the array of parameters that we created 
    this.visitVarInsn(Opcodes.ALOAD, paramLength); 

    mv.visitMethodInsn(Opcodes.INVOKESTATIC, 
      "jalen/MethodStats", 
      "onMethodEntry", 
      "(ILjava/lang/String;Ljava/lang/String;[Ljava/lang/Object;)V"); 
    super.visitCode(); 
} 

但是,當顯然該方法有多個參數時,這不起作用。

得到的類文件顯示這樣的事情:

static void moveDisk(char arg0, char arg1, PrintStream arg2) { 
Object[] arrayOfObject = new Object[3]; arrayOfObject[0] = ???; arrayOfObject[1] = ???; 
Object localObject; 
arrayOfObject[2] = localObject; MethodStats.onMethodEntry(5, "hanoi/TowersOfHanoi", "moveDisk", arrayOfObject); 

在其中創建而不是加載參數2個本地對象。

字節碼不顯示任何奇怪:

static void moveDisk(char, char, java.io.PrintStream); 
Code: 
    0: bipush  3 
    2: anewarray  #4     // class java/lang/Object 
    5: astore_3  
    6: aload_3  
    7: bipush  0 
    9: iload_0  
    10: aastore  
    11: aload_3  
    12: bipush  1 
    14: iload_1  
    15: aastore  
    16: aload_3  
    17: bipush  2 
    19: aload_2  
    20: aastore  
    21: ldc   #118    // int 5 
    23: ldc   #12     // String hanoi/TowersOfHanoi 
    25: ldc   #119    // String moveDisk 
    27: aload_3  
    28: invokestatic #19     // Method jalen/MethodStats.onMethodEntry:(ILjava/lang/String;Ljava/lang/String;[Ljava/lang/Object;)V 

最後,表明是(用時-noverify)錯誤:

param: [Ljava.lang.String;@420e54f3 
Exception in thread "Jalen Agent" java.lang.NullPointerException 
at hanoi.TowersOfHanoi.solveHanoi(TowersOfHanoi.java) 
at hanoi.TowersOfHanoi.main(TowersOfHanoi.java:29) 

否則,它是:

Exception in thread "Jalen Agent" java.lang.VerifyError: (class: hanoi/TowersOfHanoi, method: moveDisk signature: (CCLjava/io/PrintStream;)V) Expecting to find object/array on stack 
    at java.lang.Class.getDeclaredMethods0(Native Method) 
    at java.lang.Class.privateGetDeclaredMethods(Class.java:2442) 
    at java.lang.Class.getMethod0(Class.java:2685) 
    at java.lang.Class.getMethod(Class.java:1620) 
    at sun.launcher.LauncherHelper.getMainMethod(LauncherHelper.java:492) 
    at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:484) 

通常情況下,這應該工作,因爲我只是從堆棧幀加載信息。 我也試過檢查靜態的非靜態方法&(這裏解釋的堆棧:http://www.artima.com/insidejvm/ed2/jvm8.html),但仍然沒有成功。

任何想法爲什麼會發生這種情況,或者可能是解決方案的想法?

謝謝:)

編輯:

(由下面INT3 :)給點建議謝謝)拳擊最多時的原始類型它現在的工作。下面是visitCode()方法的工作的代碼:

@Override 
public void visitCode() { 
    int paramLength = paramTypes.length; 

    // Create array with length equal to number of parameters 
    mv.visitIntInsn(Opcodes.BIPUSH, paramLength); 
    mv.visitTypeInsn(Opcodes.ANEWARRAY, "java/lang/Object"); 
    mv.visitVarInsn(Opcodes.ASTORE, paramLength); 

    // Fill the created array with method parameters 
    int i = 0; 
    for (Type tp : paramTypes) { 
     mv.visitVarInsn(Opcodes.ALOAD, paramLength); 
     mv.visitIntInsn(Opcodes.BIPUSH, i); 

     if (tp.equals(Type.BOOLEAN_TYPE)) { 
      mv.visitVarInsn(Opcodes.ILOAD, i); 
      mv.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Boolean", "valueOf", "(Z)Ljava/lang/Boolean;"); 
     } 
     else if (tp.equals(Type.BYTE_TYPE)) { 
      mv.visitVarInsn(Opcodes.ILOAD, i); 
      mv.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Byte", "valueOf", "(B)Ljava/lang/Byte;"); 
     } 
     else if (tp.equals(Type.CHAR_TYPE)) { 
      mv.visitVarInsn(Opcodes.ILOAD, i); 
      mv.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Character", "valueOf", "(C)Ljava/lang/Character;"); 
     } 
     else if (tp.equals(Type.SHORT_TYPE)) { 
      mv.visitVarInsn(Opcodes.ILOAD, i); 
      mv.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Short", "valueOf", "(S)Ljava/lang/Short;"); 
     } 
     else if (tp.equals(Type.INT_TYPE)) { 
      mv.visitVarInsn(Opcodes.ILOAD, i); 
      mv.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Integer", "valueOf", "(I)Ljava/lang/Integer;"); 
     } 
     else if (tp.equals(Type.LONG_TYPE)) { 
      mv.visitVarInsn(Opcodes.LLOAD, i); 
      mv.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Long", "valueOf", "(J)Ljava/lang/Long;"); 
      i++; 
     } 
     else if (tp.equals(Type.FLOAT_TYPE)) { 
      mv.visitVarInsn(Opcodes.FLOAD, i); 
      mv.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Float", "valueOf", "(F)Ljava/lang/Float;"); 
     } 
     else if (tp.equals(Type.DOUBLE_TYPE)) { 
      mv.visitVarInsn(Opcodes.DLOAD, i); 
      mv.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Double", "valueOf", "(D)Ljava/lang/Double;"); 
      i++; 
     } 
     else 
      mv.visitVarInsn(Opcodes.ALOAD, i); 

     mv.visitInsn(Opcodes.AASTORE); 
     i++; 
    } 

    // Load id, class name and method name 
    this.visitLdcInsn(new Integer(this.methodID)); 
    this.visitLdcInsn(this.className); 
    this.visitLdcInsn(this.methodName); 

    // Load the array of parameters that we created 
    this.visitVarInsn(Opcodes.ALOAD, paramLength); 

    mv.visitMethodInsn(Opcodes.INVOKESTATIC, 
      "jalen/MethodStats", 
      "onMethodEntry", 
      "(ILjava/lang/String;Ljava/lang/String;[Ljava/lang/Object;)V"); 
    super.visitCode(); 
} 
+0

如何將參數返回到堆棧,以便函數可以繼續使用它們? – Shay

回答

6

您正在使用aastorechar存儲到對象陣列,這是一種類型的錯誤。 aastore應該只用於存儲對象和數組,這可能是爲什麼錯誤顯示'期望對象/數組在堆棧'。字符應該使用castore存儲在字符數組中。但是,由於您希望這適用於任意簽名,因此您可能希望將基元類型放入對象中,然後您可以使用這些對象 - 例如, char應裝在一個java.lang.Character對象中。