Audit Java: system to detect exceptions thrown / caught (aop?)

后端 未结 6 1433
广开言路
广开言路 2021-02-07 08:56

Due to checked exceptions, we can have some problems in production having all exceptions caught in the right place and logged correctly.

I wonder if there is some openso

6条回答
  •  不思量自难忘°
    2021-02-07 09:04

    I didn't though about that yet but one solution, if you do not need to detect exceptions thrown on production envirionment, is to attach to your Java application a custom debugger that can be triggered whenever an exception is raised.

    This french blog article talk about how to do it: http://blog.xebia.fr/2011/12/12/legacy-code-gestion-des-exceptions-avec-jpda/

    Here is the code:

    Run with debug: Xdebug -Xrunjdwp:transport=dt_socket,address=8000,server=y,suspend=n

    Connect to the JVM:

    public static VirtualMachine connect(String port) throws IOException, IllegalConnectorArgumentsException {
            AttachingConnector connector = null;
            VirtualMachineManager vmManager = Bootstrap.virtualMachineManager();
            for (Connector aconnector : vmManager.allConnectors()) {
                if ("com.sun.jdi.SocketAttach".equals(aconnector.name())) {
      connector = (AttachingConnector) aconnector;
      break;
         }
     }
     Map args = connector.defaultArguments();
            Connector.Argument pidArgument = args.get("port");
            pidArgument.setValue(port);
            return connector.attach(args);
     }
    

    Create your breakpoints. Exemple:

    public static void createExceptionBreakPoint(VirtualMachine vm) {
            EventRequestManager erm = vm.eventRequestManager();
            List referenceTypes = vm.classesByName("java.lang.Throwable");
            for (ReferenceType refType : referenceTypes){
      ExceptionRequest exceptionRequest = erm.createExceptionRequest(refType, true, true);
      exceptionRequest.setEnabled(true);
     }
    }
    

    And then handle the exceptions:

    public static void handleExceptionEvent(ExceptionEvent exceptionEvent) throws Exception {
            ObjectReference remoteException = exceptionEvent.exception();
            ThreadReference thread = exceptionEvent.thread();
            List paramList = new ArrayList(1);
            paramList.add(dumpFileName);
            //crer un printStream dans la JVM cible
            ObjectReference printStreamRef = printStreamClassType.newInstance(thread, printStreamConstructor, paramList,
                ObjectReference.INVOKE_SINGLE_THREADED);
        ReferenceType remoteType = remoteException.referenceType();
        Method printStackTrace = (Method) remoteType.methodsByName("printStackTrace").get(1);
        paramList.clear();
        paramList.add(printStreamRef);
        remoteException.invokeMethod(thread, printStackTrace, paramList, ObjectReference.INVOKE_SINGLE_THREADED);
        Scanner scanner = new Scanner(new File(dumpFileName.value()));
        while (scanner.hasNextLine()){
      System.out.println(scanner.nextLine());
     }
    }
    

    A bit heavy but it works, now how to catch the exceptions that are logged and the others?

提交回复
热议问题