1 /* 2 * Copyright (C) 2024 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package art; 18 19 import java.lang.reflect.Executable; 20 import java.lang.reflect.Method; 21 import java.util.Arrays; 22 23 public class Test2282 { 24 static int LAST_LINE_NUMBER = -1; 25 static int CATCH_LINE_NUMBER = -1; 26 static Method TEST_METHOD; 27 static Method CATCH_HANDLER; 28 static Method SINGLE_STEP_HANDLER; 29 notifySingleStep(Thread thr, Executable e, long loc)30 public static void notifySingleStep(Thread thr, Executable e, long loc) { 31 if (!e.equals(TEST_METHOD) || LAST_LINE_NUMBER != -1) { 32 // Ignore single stepping from methods like enableSingleStepping. We are only interested 33 // in the first line from the test method. So ignore others. This would make the test 34 // more robust if the implementation changes. 35 return; 36 } 37 38 int cur_line = Breakpoint.locationToLine(e, loc); 39 System.out.println("Single step: " + e + " @ line=" + cur_line); 40 LAST_LINE_NUMBER = cur_line; 41 if (LAST_LINE_NUMBER == CATCH_LINE_NUMBER) { 42 System.out.println("Single stepping was enabled in catch handler. We should not break " 43 + "on catch line"); 44 } 45 } 46 ExceptionCatchHandler( Thread thr, Executable method, long location, Throwable exception)47 public static void ExceptionCatchHandler( 48 Thread thr, Executable method, long location, Throwable exception) { 49 CATCH_LINE_NUMBER = Breakpoint.locationToLine(method, location); 50 System.out.println(thr.getName() + ": " + method + " @ line = " + CATCH_LINE_NUMBER 51 + " caught " + exception.getClass() + ": " + exception.getMessage()); 52 Exceptions.disableExceptionTracing(Thread.currentThread()); 53 Trace.enableSingleStepTracing(Test2282.class, SINGLE_STEP_HANDLER, Thread.currentThread()); 54 } 55 56 public static class TestException extends Error { TestException(String e)57 public TestException(String e) { 58 super(e); 59 } 60 } 61 run()62 public static void run() throws Exception { 63 CATCH_HANDLER = Test2282.class.getDeclaredMethod("ExceptionCatchHandler", Thread.class, 64 Executable.class, Long.TYPE, Throwable.class); 65 SINGLE_STEP_HANDLER = Test2282.class.getDeclaredMethod( 66 "notifySingleStep", Thread.class, Executable.class, Long.TYPE); 67 TEST_METHOD = Test2282.class.getDeclaredMethod("run"); 68 // Enable exception catch event and setup a handler for the events. 69 Exceptions.setupExceptionTracing(Test2282.class, TestException.class, 70 /*ExceptionCaughtEventHandler=*/null, CATCH_HANDLER); 71 Exceptions.enableExceptionCatchEvent(Thread.currentThread()); 72 73 try { 74 throw new TestException("Test"); 75 } catch (TestException e) { 76 // Single step should stop here. 77 Trace.disableTracing(Thread.currentThread()); 78 System.out.println("Caught TestException"); 79 } 80 } 81 } 82