The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2007 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 android.ddm; |
| 18 | |
Orion Hodson | e649a56 | 2019-02-04 16:54:24 +0000 | [diff] [blame] | 19 | import android.os.Debug; |
| 20 | import android.os.UserHandle; |
| 21 | import android.util.Log; |
| 22 | |
| 23 | import dalvik.system.VMRuntime; |
| 24 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 25 | import org.apache.harmony.dalvik.ddmc.Chunk; |
| 26 | import org.apache.harmony.dalvik.ddmc.ChunkHandler; |
| 27 | import org.apache.harmony.dalvik.ddmc.DdmServer; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 28 | |
| 29 | import java.nio.ByteBuffer; |
| 30 | |
| 31 | /** |
The Android Open Source Project | 7b0b1ed | 2009-03-18 22:20:26 -0700 | [diff] [blame] | 32 | * Handle "hello" messages and feature discovery. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 33 | */ |
| 34 | public class DdmHandleHello extends ChunkHandler { |
| 35 | |
| 36 | public static final int CHUNK_HELO = type("HELO"); |
| 37 | public static final int CHUNK_WAIT = type("WAIT"); |
The Android Open Source Project | 7b0b1ed | 2009-03-18 22:20:26 -0700 | [diff] [blame] | 38 | public static final int CHUNK_FEAT = type("FEAT"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 39 | |
Orion Hodson | e649a56 | 2019-02-04 16:54:24 +0000 | [diff] [blame] | 40 | private static final int CLIENT_PROTOCOL_VERSION = 1; |
| 41 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 42 | private static DdmHandleHello mInstance = new DdmHandleHello(); |
| 43 | |
Siva Velusamy | 945bfb6 | 2013-01-06 16:03:12 -0800 | [diff] [blame] | 44 | private static final String[] FRAMEWORK_FEATURES = new String[] { |
| 45 | "opengl-tracing", |
| 46 | "view-hierarchy", |
| 47 | }; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 48 | |
| 49 | /* singleton, do not instantiate */ |
| 50 | private DdmHandleHello() {} |
| 51 | |
| 52 | /** |
| 53 | * Register for the messages we're interested in. |
| 54 | */ |
| 55 | public static void register() { |
| 56 | DdmServer.registerHandler(CHUNK_HELO, mInstance); |
The Android Open Source Project | 7b0b1ed | 2009-03-18 22:20:26 -0700 | [diff] [blame] | 57 | DdmServer.registerHandler(CHUNK_FEAT, mInstance); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Called when the DDM server connects. The handler is allowed to |
| 62 | * send messages to the server. |
| 63 | */ |
| 64 | public void connected() { |
Joe Onorato | 43a1765 | 2011-04-06 19:22:23 -0700 | [diff] [blame] | 65 | if (false) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 66 | Log.v("ddm-hello", "Connected!"); |
| 67 | |
Andy McFadden | e577232 | 2010-01-22 07:23:31 -0800 | [diff] [blame] | 68 | if (false) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 69 | /* test spontaneous transmission */ |
| 70 | byte[] data = new byte[] { 0, 1, 2, 3, 4, -4, -3, -2, -1, 127 }; |
| 71 | Chunk testChunk = |
| 72 | new Chunk(ChunkHandler.type("TEST"), data, 1, data.length-2); |
| 73 | DdmServer.sendChunk(testChunk); |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Called when the DDM server disconnects. Can be used to disable |
| 79 | * periodic transmissions or clean up saved state. |
| 80 | */ |
| 81 | public void disconnected() { |
Joe Onorato | 43a1765 | 2011-04-06 19:22:23 -0700 | [diff] [blame] | 82 | if (false) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 83 | Log.v("ddm-hello", "Disconnected!"); |
| 84 | } |
| 85 | |
| 86 | /** |
The Android Open Source Project | 7b0b1ed | 2009-03-18 22:20:26 -0700 | [diff] [blame] | 87 | * Handle a chunk of data. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 88 | */ |
| 89 | public Chunk handleChunk(Chunk request) { |
Joe Onorato | 43a1765 | 2011-04-06 19:22:23 -0700 | [diff] [blame] | 90 | if (false) |
The Android Open Source Project | 7b0b1ed | 2009-03-18 22:20:26 -0700 | [diff] [blame] | 91 | Log.v("ddm-heap", "Handling " + name(request.type) + " chunk"); |
| 92 | int type = request.type; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 93 | |
The Android Open Source Project | 7b0b1ed | 2009-03-18 22:20:26 -0700 | [diff] [blame] | 94 | if (type == CHUNK_HELO) { |
| 95 | return handleHELO(request); |
| 96 | } else if (type == CHUNK_FEAT) { |
| 97 | return handleFEAT(request); |
| 98 | } else { |
| 99 | throw new RuntimeException("Unknown packet " |
| 100 | + ChunkHandler.name(type)); |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | /* |
Jeff Hao | 406ec15 | 2013-07-30 10:13:41 -0700 | [diff] [blame] | 105 | * Handle introductory packet. This is called during JNI_CreateJavaVM |
| 106 | * before frameworks native methods are registered, so be careful not |
| 107 | * to call any APIs that depend on frameworks native code. |
The Android Open Source Project | 7b0b1ed | 2009-03-18 22:20:26 -0700 | [diff] [blame] | 108 | */ |
| 109 | private Chunk handleHELO(Chunk request) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 110 | if (false) |
| 111 | return createFailChunk(123, "This is a test"); |
| 112 | |
| 113 | /* |
| 114 | * Process the request. |
| 115 | */ |
| 116 | ByteBuffer in = wrapChunk(request); |
| 117 | |
| 118 | int serverProtoVers = in.getInt(); |
Joe Onorato | 43a1765 | 2011-04-06 19:22:23 -0700 | [diff] [blame] | 119 | if (false) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 120 | Log.v("ddm-hello", "Server version is " + serverProtoVers); |
| 121 | |
| 122 | /* |
| 123 | * Create a response. |
| 124 | */ |
| 125 | String vmName = System.getProperty("java.vm.name", "?"); |
| 126 | String vmVersion = System.getProperty("java.vm.version", "?"); |
| 127 | String vmIdent = vmName + " v" + vmVersion; |
| 128 | |
Chester Hsieh | ab720ee | 2019-11-12 12:01:54 -0800 | [diff] [blame] | 129 | DdmHandleAppName.Names names = DdmHandleAppName.getNames(); |
| 130 | String appName = names.getAppName(); |
| 131 | String pkgName = names.getPkgName(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 132 | |
Sebastien Hertz | e901dbd | 2014-05-16 16:54:16 +0200 | [diff] [blame] | 133 | VMRuntime vmRuntime = VMRuntime.getRuntime(); |
| 134 | String instructionSetDescription = |
| 135 | vmRuntime.is64Bit() ? "64-bit" : "32-bit"; |
| 136 | String vmInstructionSet = vmRuntime.vmInstructionSet(); |
| 137 | if (vmInstructionSet != null && vmInstructionSet.length() > 0) { |
| 138 | instructionSetDescription += " (" + vmInstructionSet + ")"; |
| 139 | } |
| 140 | String vmFlags = "CheckJNI=" |
| 141 | + (vmRuntime.isCheckJniEnabled() ? "true" : "false"); |
Oleksiy Vyalov | b68bcbd | 2016-03-30 20:23:25 -0700 | [diff] [blame] | 142 | boolean isNativeDebuggable = vmRuntime.isNativeDebuggable(); |
Sebastien Hertz | e901dbd | 2014-05-16 16:54:16 +0200 | [diff] [blame] | 143 | |
Chester Hsieh | ab720ee | 2019-11-12 12:01:54 -0800 | [diff] [blame] | 144 | ByteBuffer out = ByteBuffer.allocate(32 |
Sebastien Hertz | e901dbd | 2014-05-16 16:54:16 +0200 | [diff] [blame] | 145 | + vmIdent.length() * 2 |
| 146 | + appName.length() * 2 |
| 147 | + instructionSetDescription.length() * 2 |
Oleksiy Vyalov | b68bcbd | 2016-03-30 20:23:25 -0700 | [diff] [blame] | 148 | + vmFlags.length() * 2 |
Chester Hsieh | ab720ee | 2019-11-12 12:01:54 -0800 | [diff] [blame] | 149 | + 1 |
| 150 | + pkgName.length() * 2); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 151 | out.order(ChunkHandler.CHUNK_ORDER); |
Orion Hodson | e649a56 | 2019-02-04 16:54:24 +0000 | [diff] [blame] | 152 | out.putInt(CLIENT_PROTOCOL_VERSION); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 153 | out.putInt(android.os.Process.myPid()); |
| 154 | out.putInt(vmIdent.length()); |
| 155 | out.putInt(appName.length()); |
| 156 | putString(out, vmIdent); |
| 157 | putString(out, appName); |
Siva Velusamy | d693dfa | 2012-09-10 14:36:58 -0700 | [diff] [blame] | 158 | out.putInt(UserHandle.myUserId()); |
Sebastien Hertz | e901dbd | 2014-05-16 16:54:16 +0200 | [diff] [blame] | 159 | out.putInt(instructionSetDescription.length()); |
| 160 | putString(out, instructionSetDescription); |
| 161 | out.putInt(vmFlags.length()); |
| 162 | putString(out, vmFlags); |
Oleksiy Vyalov | b68bcbd | 2016-03-30 20:23:25 -0700 | [diff] [blame] | 163 | out.put((byte)(isNativeDebuggable ? 1 : 0)); |
Chester Hsieh | ab720ee | 2019-11-12 12:01:54 -0800 | [diff] [blame] | 164 | out.putInt(pkgName.length()); |
| 165 | putString(out, pkgName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 166 | |
| 167 | Chunk reply = new Chunk(CHUNK_HELO, out); |
| 168 | |
| 169 | /* |
| 170 | * Take the opportunity to inform DDMS if we are waiting for a |
| 171 | * debugger to attach. |
| 172 | */ |
| 173 | if (Debug.waitingForDebugger()) |
| 174 | sendWAIT(0); |
| 175 | |
| 176 | return reply; |
| 177 | } |
| 178 | |
The Android Open Source Project | 7b0b1ed | 2009-03-18 22:20:26 -0700 | [diff] [blame] | 179 | /* |
| 180 | * Handle request for list of supported features. |
| 181 | */ |
| 182 | private Chunk handleFEAT(Chunk request) { |
| 183 | // TODO: query the VM to ensure that support for these features |
| 184 | // is actually compiled in |
Siva Velusamy | 0c1761b | 2012-12-14 17:09:06 -0800 | [diff] [blame] | 185 | final String[] vmFeatures = Debug.getVmFeatureList(); |
The Android Open Source Project | 7b0b1ed | 2009-03-18 22:20:26 -0700 | [diff] [blame] | 186 | |
Joe Onorato | 43a1765 | 2011-04-06 19:22:23 -0700 | [diff] [blame] | 187 | if (false) |
Andy McFadden | 98d4935 | 2009-09-04 11:16:50 -0700 | [diff] [blame] | 188 | Log.v("ddm-heap", "Got feature list request"); |
The Android Open Source Project | 7b0b1ed | 2009-03-18 22:20:26 -0700 | [diff] [blame] | 189 | |
Siva Velusamy | 945bfb6 | 2013-01-06 16:03:12 -0800 | [diff] [blame] | 190 | int size = 4 + 4 * (vmFeatures.length + FRAMEWORK_FEATURES.length); |
Siva Velusamy | 0c1761b | 2012-12-14 17:09:06 -0800 | [diff] [blame] | 191 | for (int i = vmFeatures.length-1; i >= 0; i--) |
| 192 | size += vmFeatures[i].length() * 2; |
Siva Velusamy | 945bfb6 | 2013-01-06 16:03:12 -0800 | [diff] [blame] | 193 | for (int i = FRAMEWORK_FEATURES.length-1; i>= 0; i--) |
| 194 | size += FRAMEWORK_FEATURES[i].length() * 2; |
The Android Open Source Project | 7b0b1ed | 2009-03-18 22:20:26 -0700 | [diff] [blame] | 195 | |
| 196 | ByteBuffer out = ByteBuffer.allocate(size); |
| 197 | out.order(ChunkHandler.CHUNK_ORDER); |
Siva Velusamy | 945bfb6 | 2013-01-06 16:03:12 -0800 | [diff] [blame] | 198 | out.putInt(vmFeatures.length + FRAMEWORK_FEATURES.length); |
Siva Velusamy | 0c1761b | 2012-12-14 17:09:06 -0800 | [diff] [blame] | 199 | for (int i = vmFeatures.length-1; i >= 0; i--) { |
| 200 | out.putInt(vmFeatures[i].length()); |
| 201 | putString(out, vmFeatures[i]); |
| 202 | } |
Siva Velusamy | 945bfb6 | 2013-01-06 16:03:12 -0800 | [diff] [blame] | 203 | for (int i = FRAMEWORK_FEATURES.length-1; i >= 0; i--) { |
| 204 | out.putInt(FRAMEWORK_FEATURES[i].length()); |
| 205 | putString(out, FRAMEWORK_FEATURES[i]); |
The Android Open Source Project | 7b0b1ed | 2009-03-18 22:20:26 -0700 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | return new Chunk(CHUNK_FEAT, out); |
| 209 | } |
| 210 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 211 | /** |
| 212 | * Send up a WAIT chunk. The only currently defined value for "reason" |
| 213 | * is zero, which means "waiting for a debugger". |
| 214 | */ |
| 215 | public static void sendWAIT(int reason) { |
| 216 | byte[] data = new byte[] { (byte) reason }; |
| 217 | Chunk waitChunk = new Chunk(CHUNK_WAIT, data, 0, 1); |
| 218 | DdmServer.sendChunk(waitChunk); |
| 219 | } |
| 220 | } |
| 221 | |