• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2025 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 package android.vmdebug.cts;
17 
18 import static org.junit.Assert.assertNotEquals;
19 import static org.junit.Assert.assertNotNull;
20 import static org.junit.Assert.assertTrue;
21 
22 import android.platform.test.annotations.RequiresFlagsEnabled;
23 
24 import androidx.test.core.app.ApplicationProvider;
25 import androidx.test.runner.AndroidJUnit4;
26 
27 import dalvik.system.VMDebug;
28 
29 import org.junit.Test;
30 import org.junit.runner.RunWith;
31 
32 import java.io.File;
33 import java.io.FileOutputStream;
34 
35 /** Tests for VMDebug API */
36 @RunWith(AndroidJUnit4.class)
37 public class VMDebugDeviceTest {
38 
39     @Test
40     @RequiresFlagsEnabled(com.android.art.flags.Flags.FLAG_ALWAYS_ENABLE_PROFILE_CODE)
testLowOverheadTraceFileName()41     public void testLowOverheadTraceFileName() throws Exception {
42         File file = getTraceFile();
43         try {
44             VMDebug.TraceDestination trace =
45                     VMDebug.TraceDestination.fromFileName(file.getAbsolutePath());
46             testLowOverheadTrace(trace);
47         } finally {
48             file.delete();
49         }
50     }
51 
52     @Test
53     @RequiresFlagsEnabled(com.android.art.flags.Flags.FLAG_ALWAYS_ENABLE_PROFILE_CODE)
testLowOverheadTraceFd()54     public void testLowOverheadTraceFd() throws Exception {
55         File file = getTraceFile();
56         try (FileOutputStream out_file = new FileOutputStream(file)) {
57             VMDebug.TraceDestination trace =
58                     VMDebug.TraceDestination.fromFileDescriptor(out_file.getFD());
59             testLowOverheadTrace(trace);
60         } finally {
61             file.delete();
62         }
63     }
64 
65     @Test
66     @RequiresFlagsEnabled(com.android.art.flags.Flags.FLAG_EXECUTABLE_METHOD_FILE_OFFSETS)
testGetExecutableMethodFileOffsets()67     public void testGetExecutableMethodFileOffsets() throws Exception {
68         java.lang.reflect.Method method = this.getClass().getDeclaredMethod("testMethod");
69 
70         VMDebug.ExecutableMethodFileOffsets offsets =
71                 VMDebug.getExecutableMethodFileOffsets(method);
72 
73         assertNotNull(offsets);
74         String containerPath = offsets.getContainerPath();
75         assertNotNull(containerPath);
76         assertNotEquals("", containerPath);
77         assertTrue(offsets.getMethodOffset() > 0);
78         assertTrue(offsets.getContainerOffset() > 0);
79     }
80 
testMethod()81     private void testMethod() {
82         final long debugTime = 1000;
83 
84         try {
85             Thread.sleep(debugTime);
86         } catch (Exception e) {
87             // This method is just used to generate code to be traced. So just ignore any
88             // exceptions.
89         }
90     }
91 
testLowOverheadTrace(VMDebug.TraceDestination trace)92     private void testLowOverheadTrace(VMDebug.TraceDestination trace) throws Exception {
93         VMDebug.startLowOverheadTraceForAllMethods();
94         testMethod();
95         VMDebug.dumpLowOverheadTrace(trace);
96         VMDebug.stopLowOverheadTrace();
97     }
98 
getTraceFile()99     private File getTraceFile() {
100         File dir = ApplicationProvider.getApplicationContext().getFilesDir();
101         File file = new File(dir, "vmdebug_lowoverhead.trace");
102         return file;
103     }
104 }
105