blob: 4d781c3fc21fd68e51694039cd688278c0f7d906 [file] [log] [blame]
Jeff Haod063d912014-09-08 09:38:18 -07001/*
2 * Copyright (C) 2014 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
17import java.io.File;
18import java.io.IOException;
19import java.lang.reflect.Method;
20
21public class Main {
22 public static void main(String[] args) throws Exception {
23 String name = System.getProperty("java.vm.name");
24 if (!"Dalvik".equals(name)) {
25 System.out.println("This test is not supported on " + name);
26 return;
27 }
28 testMethodTracing();
29 }
30
Andreas Gampe74035032015-01-27 16:12:08 -080031 private static File createTempFile() throws Exception {
Jeff Hao8cf89c42014-09-09 13:07:59 -070032 try {
Andreas Gampe74035032015-01-27 16:12:08 -080033 return File.createTempFile("test", ".trace");
Jeff Hao8cf89c42014-09-09 13:07:59 -070034 } catch (IOException e) {
Andreas Gampe74035032015-01-27 16:12:08 -080035 System.setProperty("java.io.tmpdir", "/data/local/tmp");
36 try {
37 return File.createTempFile("test", ".trace");
38 } catch (IOException e2) {
39 System.setProperty("java.io.tmpdir", "/sdcard");
40 return File.createTempFile("test", ".trace");
41 }
Jeff Haod063d912014-09-08 09:38:18 -070042 }
Andreas Gampe74035032015-01-27 16:12:08 -080043 }
44
45 private static void testMethodTracing() throws Exception {
46 File tempFile = createTempFile();
Jeff Hao8cf89c42014-09-09 13:07:59 -070047 tempFile.deleteOnExit();
48 String tempFileName = tempFile.getPath();
Jeff Haod063d912014-09-08 09:38:18 -070049
Jeff Haocbe15be2014-09-08 15:32:39 -070050 if (VMDebug.getMethodTracingMode() != 0) {
51 VMDebug.stopMethodTracing();
52 }
53
Jeff Haod063d912014-09-08 09:38:18 -070054 System.out.println("Confirm enable/disable");
55 System.out.println("status=" + VMDebug.getMethodTracingMode());
56 VMDebug.startMethodTracing(tempFileName, 0, 0, false, 0);
57 System.out.println("status=" + VMDebug.getMethodTracingMode());
58 VMDebug.stopMethodTracing();
59 System.out.println("status=" + VMDebug.getMethodTracingMode());
60 if (tempFile.length() == 0) {
61 System.out.println("ERROR: tracing output file is empty");
62 }
63
64 System.out.println("Confirm sampling");
65 VMDebug.startMethodTracing(tempFileName, 0, 0, true, 1000);
66 System.out.println("status=" + VMDebug.getMethodTracingMode());
67 VMDebug.stopMethodTracing();
68 System.out.println("status=" + VMDebug.getMethodTracingMode());
69 if (tempFile.length() == 0) {
70 System.out.println("ERROR: sample tracing output file is empty");
71 }
72
73 System.out.println("Test starting when already started");
74 VMDebug.startMethodTracing(tempFileName, 0, 0, false, 0);
75 System.out.println("status=" + VMDebug.getMethodTracingMode());
76 VMDebug.startMethodTracing(tempFileName, 0, 0, false, 0);
77 System.out.println("status=" + VMDebug.getMethodTracingMode());
78
79 System.out.println("Test stopping when already stopped");
80 VMDebug.stopMethodTracing();
81 System.out.println("status=" + VMDebug.getMethodTracingMode());
82 VMDebug.stopMethodTracing();
83 System.out.println("status=" + VMDebug.getMethodTracingMode());
84
85 System.out.println("Test tracing with empty filename");
86 try {
87 VMDebug.startMethodTracing("", 0, 0, false, 0);
88 System.out.println("Should have thrown an exception");
89 } catch (Exception e) {
90 System.out.println("Got expected exception");
91 }
92
93 System.out.println("Test tracing with bogus (< 1024 && != 0) filesize");
94 try {
95 VMDebug.startMethodTracing(tempFileName, 1000, 0, false, 0);
96 System.out.println("Should have thrown an exception");
97 } catch (Exception e) {
98 System.out.println("Got expected exception");
99 }
100
101 System.out.println("Test sampling with bogus (<= 0) interval");
102 try {
103 VMDebug.startMethodTracing(tempFileName, 0, 0, true, 0);
104 System.out.println("Should have thrown an exception");
105 } catch (Exception e) {
106 System.out.println("Got expected exception");
107 }
108
109 tempFile.delete();
110 }
111
112 private static class VMDebug {
113 private static final Method startMethodTracingMethod;
114 private static final Method stopMethodTracingMethod;
115 private static final Method getMethodTracingModeMethod;
116 static {
117 try {
118 Class c = Class.forName("dalvik.system.VMDebug");
119 startMethodTracingMethod = c.getDeclaredMethod("startMethodTracing", String.class,
120 Integer.TYPE, Integer.TYPE, Boolean.TYPE, Integer.TYPE);
121 stopMethodTracingMethod = c.getDeclaredMethod("stopMethodTracing");
122 getMethodTracingModeMethod = c.getDeclaredMethod("getMethodTracingMode");
123 } catch (Exception e) {
124 throw new RuntimeException(e);
125 }
126 }
127
128 public static void startMethodTracing(String filename, int bufferSize, int flags,
129 boolean samplingEnabled, int intervalUs) throws Exception {
130 startMethodTracingMethod.invoke(null, filename, bufferSize, flags, samplingEnabled,
131 intervalUs);
132 }
133 public static void stopMethodTracing() throws Exception {
134 stopMethodTracingMethod.invoke(null);
135 }
136 public static int getMethodTracingMode() throws Exception {
137 return (int) getMethodTracingModeMethod.invoke(null);
138 }
139 }
140}