blob: 7f24b1b825fc81e11578163f5c85740a9acf5cb9 [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
31 private static void testMethodTracing() throws Exception {
Jeff Hao8cf89c42014-09-09 13:07:59 -070032 File tempFile;
33 try {
34 tempFile = File.createTempFile("test", ".trace");
35 } catch (IOException e) {
36 System.setProperty("java.io.tmpdir", "/sdcard");
37 tempFile = File.createTempFile("test", ".trace");
Jeff Haod063d912014-09-08 09:38:18 -070038 }
Jeff Hao8cf89c42014-09-09 13:07:59 -070039 tempFile.deleteOnExit();
40 String tempFileName = tempFile.getPath();
Jeff Haod063d912014-09-08 09:38:18 -070041
Jeff Haocbe15be2014-09-08 15:32:39 -070042 if (VMDebug.getMethodTracingMode() != 0) {
43 VMDebug.stopMethodTracing();
44 }
45
Jeff Haod063d912014-09-08 09:38:18 -070046 System.out.println("Confirm enable/disable");
47 System.out.println("status=" + VMDebug.getMethodTracingMode());
48 VMDebug.startMethodTracing(tempFileName, 0, 0, false, 0);
49 System.out.println("status=" + VMDebug.getMethodTracingMode());
50 VMDebug.stopMethodTracing();
51 System.out.println("status=" + VMDebug.getMethodTracingMode());
52 if (tempFile.length() == 0) {
53 System.out.println("ERROR: tracing output file is empty");
54 }
55
56 System.out.println("Confirm sampling");
57 VMDebug.startMethodTracing(tempFileName, 0, 0, true, 1000);
58 System.out.println("status=" + VMDebug.getMethodTracingMode());
59 VMDebug.stopMethodTracing();
60 System.out.println("status=" + VMDebug.getMethodTracingMode());
61 if (tempFile.length() == 0) {
62 System.out.println("ERROR: sample tracing output file is empty");
63 }
64
65 System.out.println("Test starting when already started");
66 VMDebug.startMethodTracing(tempFileName, 0, 0, false, 0);
67 System.out.println("status=" + VMDebug.getMethodTracingMode());
68 VMDebug.startMethodTracing(tempFileName, 0, 0, false, 0);
69 System.out.println("status=" + VMDebug.getMethodTracingMode());
70
71 System.out.println("Test stopping when already stopped");
72 VMDebug.stopMethodTracing();
73 System.out.println("status=" + VMDebug.getMethodTracingMode());
74 VMDebug.stopMethodTracing();
75 System.out.println("status=" + VMDebug.getMethodTracingMode());
76
77 System.out.println("Test tracing with empty filename");
78 try {
79 VMDebug.startMethodTracing("", 0, 0, false, 0);
80 System.out.println("Should have thrown an exception");
81 } catch (Exception e) {
82 System.out.println("Got expected exception");
83 }
84
85 System.out.println("Test tracing with bogus (< 1024 && != 0) filesize");
86 try {
87 VMDebug.startMethodTracing(tempFileName, 1000, 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 sampling with bogus (<= 0) interval");
94 try {
95 VMDebug.startMethodTracing(tempFileName, 0, 0, true, 0);
96 System.out.println("Should have thrown an exception");
97 } catch (Exception e) {
98 System.out.println("Got expected exception");
99 }
100
101 tempFile.delete();
102 }
103
104 private static class VMDebug {
105 private static final Method startMethodTracingMethod;
106 private static final Method stopMethodTracingMethod;
107 private static final Method getMethodTracingModeMethod;
108 static {
109 try {
110 Class c = Class.forName("dalvik.system.VMDebug");
111 startMethodTracingMethod = c.getDeclaredMethod("startMethodTracing", String.class,
112 Integer.TYPE, Integer.TYPE, Boolean.TYPE, Integer.TYPE);
113 stopMethodTracingMethod = c.getDeclaredMethod("stopMethodTracing");
114 getMethodTracingModeMethod = c.getDeclaredMethod("getMethodTracingMode");
115 } catch (Exception e) {
116 throw new RuntimeException(e);
117 }
118 }
119
120 public static void startMethodTracing(String filename, int bufferSize, int flags,
121 boolean samplingEnabled, int intervalUs) throws Exception {
122 startMethodTracingMethod.invoke(null, filename, bufferSize, flags, samplingEnabled,
123 intervalUs);
124 }
125 public static void stopMethodTracing() throws Exception {
126 stopMethodTracingMethod.invoke(null);
127 }
128 public static int getMethodTracingMode() throws Exception {
129 return (int) getMethodTracingModeMethod.invoke(null);
130 }
131 }
132}