blob: 5088d228e73b7407092173f0121ccab206fba420 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
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
17package android.ddm;
18
19import org.apache.harmony.dalvik.ddmc.Chunk;
20import org.apache.harmony.dalvik.ddmc.ChunkHandler;
21import org.apache.harmony.dalvik.ddmc.DdmServer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080022import android.util.Log;
23import android.os.Debug;
24
25import java.nio.ByteBuffer;
26
27/**
The Android Open Source Project7b0b1ed2009-03-18 22:20:26 -070028 * Handle "hello" messages and feature discovery.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080029 */
30public class DdmHandleHello extends ChunkHandler {
31
32 public static final int CHUNK_HELO = type("HELO");
33 public static final int CHUNK_WAIT = type("WAIT");
The Android Open Source Project7b0b1ed2009-03-18 22:20:26 -070034 public static final int CHUNK_FEAT = type("FEAT");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080035
36 private static DdmHandleHello mInstance = new DdmHandleHello();
37
38
39 /* singleton, do not instantiate */
40 private DdmHandleHello() {}
41
42 /**
43 * Register for the messages we're interested in.
44 */
45 public static void register() {
46 DdmServer.registerHandler(CHUNK_HELO, mInstance);
The Android Open Source Project7b0b1ed2009-03-18 22:20:26 -070047 DdmServer.registerHandler(CHUNK_FEAT, mInstance);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080048 }
49
50 /**
51 * Called when the DDM server connects. The handler is allowed to
52 * send messages to the server.
53 */
54 public void connected() {
Joe Onorato43a17652011-04-06 19:22:23 -070055 if (false)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080056 Log.v("ddm-hello", "Connected!");
57
Andy McFaddene5772322010-01-22 07:23:31 -080058 if (false) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080059 /* test spontaneous transmission */
60 byte[] data = new byte[] { 0, 1, 2, 3, 4, -4, -3, -2, -1, 127 };
61 Chunk testChunk =
62 new Chunk(ChunkHandler.type("TEST"), data, 1, data.length-2);
63 DdmServer.sendChunk(testChunk);
64 }
65 }
66
67 /**
68 * Called when the DDM server disconnects. Can be used to disable
69 * periodic transmissions or clean up saved state.
70 */
71 public void disconnected() {
Joe Onorato43a17652011-04-06 19:22:23 -070072 if (false)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080073 Log.v("ddm-hello", "Disconnected!");
74 }
75
76 /**
The Android Open Source Project7b0b1ed2009-03-18 22:20:26 -070077 * Handle a chunk of data.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080078 */
79 public Chunk handleChunk(Chunk request) {
Joe Onorato43a17652011-04-06 19:22:23 -070080 if (false)
The Android Open Source Project7b0b1ed2009-03-18 22:20:26 -070081 Log.v("ddm-heap", "Handling " + name(request.type) + " chunk");
82 int type = request.type;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080083
The Android Open Source Project7b0b1ed2009-03-18 22:20:26 -070084 if (type == CHUNK_HELO) {
85 return handleHELO(request);
86 } else if (type == CHUNK_FEAT) {
87 return handleFEAT(request);
88 } else {
89 throw new RuntimeException("Unknown packet "
90 + ChunkHandler.name(type));
91 }
92 }
93
94 /*
95 * Handle introductory packet.
96 */
97 private Chunk handleHELO(Chunk request) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080098 if (false)
99 return createFailChunk(123, "This is a test");
100
101 /*
102 * Process the request.
103 */
104 ByteBuffer in = wrapChunk(request);
105
106 int serverProtoVers = in.getInt();
Joe Onorato43a17652011-04-06 19:22:23 -0700107 if (false)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800108 Log.v("ddm-hello", "Server version is " + serverProtoVers);
109
110 /*
111 * Create a response.
112 */
113 String vmName = System.getProperty("java.vm.name", "?");
114 String vmVersion = System.getProperty("java.vm.version", "?");
115 String vmIdent = vmName + " v" + vmVersion;
116
117 //String appName = android.app.ActivityThread.currentPackageName();
118 //if (appName == null)
119 // appName = "unknown";
120 String appName = DdmHandleAppName.getAppName();
121
122 ByteBuffer out = ByteBuffer.allocate(16
123 + vmIdent.length()*2 + appName.length()*2);
124 out.order(ChunkHandler.CHUNK_ORDER);
125 out.putInt(DdmServer.CLIENT_PROTOCOL_VERSION);
126 out.putInt(android.os.Process.myPid());
127 out.putInt(vmIdent.length());
128 out.putInt(appName.length());
129 putString(out, vmIdent);
130 putString(out, appName);
131
132 Chunk reply = new Chunk(CHUNK_HELO, out);
133
134 /*
135 * Take the opportunity to inform DDMS if we are waiting for a
136 * debugger to attach.
137 */
138 if (Debug.waitingForDebugger())
139 sendWAIT(0);
140
141 return reply;
142 }
143
The Android Open Source Project7b0b1ed2009-03-18 22:20:26 -0700144 /*
145 * Handle request for list of supported features.
146 */
147 private Chunk handleFEAT(Chunk request) {
148 // TODO: query the VM to ensure that support for these features
149 // is actually compiled in
Andy McFaddene5772322010-01-22 07:23:31 -0800150 final String[] features = Debug.getVmFeatureList();
The Android Open Source Project7b0b1ed2009-03-18 22:20:26 -0700151
Joe Onorato43a17652011-04-06 19:22:23 -0700152 if (false)
Andy McFadden98d49352009-09-04 11:16:50 -0700153 Log.v("ddm-heap", "Got feature list request");
The Android Open Source Project7b0b1ed2009-03-18 22:20:26 -0700154
155 int size = 4 + 4 * features.length;
156 for (int i = features.length-1; i >= 0; i--)
157 size += features[i].length() * 2;
158
159 ByteBuffer out = ByteBuffer.allocate(size);
160 out.order(ChunkHandler.CHUNK_ORDER);
161 out.putInt(features.length);
162 for (int i = features.length-1; i >= 0; i--) {
163 out.putInt(features[i].length());
164 putString(out, features[i]);
165 }
166
167 return new Chunk(CHUNK_FEAT, out);
168 }
169
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800170 /**
171 * Send up a WAIT chunk. The only currently defined value for "reason"
172 * is zero, which means "waiting for a debugger".
173 */
174 public static void sendWAIT(int reason) {
175 byte[] data = new byte[] { (byte) reason };
176 Chunk waitChunk = new Chunk(CHUNK_WAIT, data, 0, 1);
177 DdmServer.sendChunk(waitChunk);
178 }
179}
180