编程语言
首页 > 编程语言> > java-如何通过ASM在字节码中捕获运行时异常

java-如何通过ASM在字节码中捕获运行时异常

作者:互联网

我试图通过异常捕获运行时异常.我能够捕获通常的方法退出事件.
但是,控件永远不会到达opcode == Opcodes.ATHROW.

我认为在调用事件时做错了事.

这是我的示例代码:

 public void visitCode() { 
//          mv.visitFieldInsn(Opcodes.GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;"); 
//          mv.visitLdcInsn("Entering method "  + fQMethodName); 
//          mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/io/PrintStream","println","(Ljava/lang/String;)V"); 
            }


     @Override
    public void visitInsn(int opcode)
    {



         if (opcode == Opcodes.ATHROW)
         {
                          mv.visitFieldInsn(Opcodes.GETSTATIC, "java/lang/System", "err", "Ljava/io/PrintStream;");

          mv.visitLdcInsn("Exiting on exception " );         
             mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/io/PrintStream", "println",
                    "(Ljava/lang/String;)V");
         }


        super.visitInsn(opcode);
    }


         public void visitMethodInsn(int opcode, String owner, String name,
                   String desc) {


                  super.visitMethodInsn(opcode, owner, name, desc);
                    //                
                  if (opcode == Opcodes.ATHROW)
                  {
                                              mv.visitFieldInsn(Opcodes.GETSTATIC, "java/lang/System", "err", "Ljava/io/PrintStream;");

                   mv.visitLdcInsn("Exiting on exception " + name);         
                      mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/io/PrintStream", "println",
                            "(Ljava/lang/String;)V");
                  }
                  else if (!name.equals("println")
                    && !name.equals("<init>")
                    && (opcode == Opcodes.INVOKEVIRTUAL || opcode == Opcodes.INVOKESPECIAL
                      || opcode == Opcodes.INVOKESTATIC || opcode == Opcodes.INVOKEDYNAMIC)) {




                      this.currentMethod = name;

                   onFinally(opcode);
                  }
                 }

                 private void onFinally(int opcode) {
                                          mv.visitFieldInsn(Opcodes.GETSTATIC, "java/lang/System", "err",
                    "Ljava/io/PrintStream;");
                  mv.visitLdcInsn("Returning to " + fQMethodName + " from " + currentMethod);
                  mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/io/PrintStream", "println",
                    "(Ljava/lang/String;)V");
                 }

在运行时异常时,将清除现有堆栈内容,仅保留可抛出对象.我可以通过ASM获得堆栈顶部元素(基本上是堆栈框架)吗?

非常感谢.

编辑:

这是我添加try-catch块后的修改程序.

@Holger:谢谢您的帮助.真的很感激.

因此,我试图在ASM的MethodVisitor中插入动态try-catch块.

这是我的代码:

public void visitCode() 
    {

        super.visitCode();

        this.visitTryCatchBlock(lblTryBlockStart, lblTryBlockEnd, lblCatchExceptionBlockStart, "java/lang/Exception");
        this.visitLabel(lblTryBlockStart);

    }  



    public void visitMaxs(int maxStack, int maxLocals)
    {
        // visit try block end label
        this.visitLabel(lblTryBlockEnd);
        // visit normal execution exit block
        this.visitJumpInsn(Opcodes.GOTO, exitBlock);

        // visit catch exception block
        this.visitLabel(lblCatchExceptionBlockStart);
        // store the exception
        this.visitVarInsn(Opcodes.ASTORE, 1);
        // load the exception
        this.visitVarInsn(Opcodes.ALOAD, 1);
        // call printStackTrace()
        //this.visitInsn(Opcodes.ATHROW);
        this.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/Exception", "printStackTrace", "()V");


        // exit from this dynamic block
        this.visitLabel(exitBlock);

        super.visitMaxs(maxStack+2, maxLocals+2);

    }

我的意图是添加动态try-catch块,如果抛出任何异常,则将其打印出来.

解决方法:

RuntimeExceptions不需要抛出一条抛出指令. idiv或irem指令可以引发ArithmeticException,getfield,putfield或invoke…指令可以引发NullPointerException,仅举一些例子.因此,您在这些地方找不到抛出指示.

拦截所有异常的唯一方法是注入一个异常处理程序,该异常处理程序将处理您的代码,并根据需要重新引发异常.

标签:java-bytecode-asm,java
来源: https://codeday.me/bug/20191122/2062132.html