| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.ddm; |
| 18 |
|
| 19 |
import android.os.Debug; |
| 20 |
import android.os.UserHandle; |
| 21 |
import android.util.Log; |
| 22 |
|
| 23 |
import dalvik.system.VMRuntime; |
| 24 |
|
| 25 |
import org.apache.harmony.dalvik.ddmc.Chunk; |
| 26 |
import org.apache.harmony.dalvik.ddmc.ChunkHandler; |
| 27 |
import org.apache.harmony.dalvik.ddmc.DdmServer; |
| 28 |
|
| 29 |
import java.nio.ByteBuffer; |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
public class DdmHandleHello extends DdmHandle { |
| 35 |
|
| 36 |
public static final int CHUNK_HELO = ChunkHandler.type("HELO"); |
| 37 |
public static final int CHUNK_WAIT = ChunkHandler.type("WAIT"); |
| 38 |
public static final int CHUNK_FEAT = ChunkHandler.type("FEAT"); |
| 39 |
|
| 40 |
private static final int CLIENT_PROTOCOL_VERSION = 1; |
| 41 |
|
| 42 |
private static DdmHandleHello mInstance = new DdmHandleHello(); |
| 43 |
|
| 44 |
private static final String[] FRAMEWORK_FEATURES = new String[] { |
| 45 |
"opengl-tracing", |
| 46 |
"view-hierarchy", |
| 47 |
}; |
| 48 |
|
| 49 |
|
| 50 |
private DdmHandleHello() {} |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
public static void register() { |
| 56 |
DdmServer.registerHandler(CHUNK_HELO, mInstance); |
| 57 |
DdmServer.registerHandler(CHUNK_FEAT, mInstance); |
| 58 |
} |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
public void onConnected() { |
| 65 |
if (false) |
| 66 |
Log.v("ddm-hello", "Connected!"); |
| 67 |
|
| 68 |
if (false) { |
| 69 |
|
| 70 |
byte[] data = new byte[] { 0, 1, 2, 3, 4, -4, -3, -2, -1, 127 }; |
| 71 |
Chunk testChunk = new Chunk(ChunkHandler.type("TEST"), data, 1, data.length - 2); |
| 72 |
DdmServer.sendChunk(testChunk); |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
public void onDisconnected() { |
| 81 |
if (false) |
| 82 |
Log.v("ddm-hello", "Disconnected!"); |
| 83 |
} |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
public Chunk handleChunk(Chunk request) { |
| 89 |
if (false) |
| 90 |
Log.v("ddm-heap", "Handling " + name(request.type) + " chunk"); |
| 91 |
int type = request.type; |
| 92 |
|
| 93 |
if (type == CHUNK_HELO) { |
| 94 |
return handleHELO(request); |
| 95 |
} else if (type == CHUNK_FEAT) { |
| 96 |
return handleFEAT(request); |
| 97 |
} else { |
| 98 |
throw new RuntimeException("Unknown packet " + name(type)); |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
private Chunk handleHELO(Chunk request) { |
| 108 |
if (false) |
| 109 |
return createFailChunk(123, "This is a test"); |
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
ByteBuffer in = wrapChunk(request); |
| 115 |
|
| 116 |
int serverProtoVers = in.getInt(); |
| 117 |
if (false) |
| 118 |
Log.v("ddm-hello", "Server version is " + serverProtoVers); |
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
String vmName = System.getProperty("java.vm.name", "?"); |
| 124 |
String vmVersion = System.getProperty("java.vm.version", "?"); |
| 125 |
String vmIdent = vmName + " v" + vmVersion; |
| 126 |
|
| 127 |
DdmHandleAppName.Names names = DdmHandleAppName.getNames(); |
| 128 |
String appName = names.getAppName(); |
| 129 |
String pkgName = names.getPkgName(); |
| 130 |
|
| 131 |
VMRuntime vmRuntime = VMRuntime.getRuntime(); |
| 132 |
String instructionSetDescription = |
| 133 |
vmRuntime.is64Bit() ? "64-bit" : "32-bit"; |
| 134 |
String vmInstructionSet = vmRuntime.vmInstructionSet(); |
| 135 |
if (vmInstructionSet != null && vmInstructionSet.length() > 0) { |
| 136 |
instructionSetDescription += " (" + vmInstructionSet + ")"; |
| 137 |
} |
| 138 |
String vmFlags = "CheckJNI=" |
| 139 |
+ (vmRuntime.isCheckJniEnabled() ? "true" : "false"); |
| 140 |
boolean isNativeDebuggable = vmRuntime.isNativeDebuggable(); |
| 141 |
|
| 142 |
ByteBuffer out = ByteBuffer.allocate(32 |
| 143 |
+ vmIdent.length() * 2 |
| 144 |
+ appName.length() * 2 |
| 145 |
+ instructionSetDescription.length() * 2 |
| 146 |
+ vmFlags.length() * 2 |
| 147 |
+ 1 |
| 148 |
+ pkgName.length() * 2); |
| 149 |
out.order(ChunkHandler.CHUNK_ORDER); |
| 150 |
out.putInt(CLIENT_PROTOCOL_VERSION); |
| 151 |
out.putInt(android.os.Process.myPid()); |
| 152 |
out.putInt(vmIdent.length()); |
| 153 |
out.putInt(appName.length()); |
| 154 |
putString(out, vmIdent); |
| 155 |
putString(out, appName); |
| 156 |
out.putInt(UserHandle.myUserId()); |
| 157 |
out.putInt(instructionSetDescription.length()); |
| 158 |
putString(out, instructionSetDescription); |
| 159 |
out.putInt(vmFlags.length()); |
| 160 |
putString(out, vmFlags); |
| 161 |
out.put((byte)(isNativeDebuggable ? 1 : 0)); |
| 162 |
out.putInt(pkgName.length()); |
| 163 |
putString(out, pkgName); |
| 164 |
|
| 165 |
Chunk reply = new Chunk(CHUNK_HELO, out); |
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
if (Debug.waitingForDebugger()) |
| 172 |
sendWAIT(0); |
| 173 |
|
| 174 |
return reply; |
| 175 |
} |
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
private Chunk handleFEAT(Chunk request) { |
| 181 |
|
| 182 |
|
| 183 |
final String[] vmFeatures = Debug.getVmFeatureList(); |
| 184 |
|
| 185 |
if (false) |
| 186 |
Log.v("ddm-heap", "Got feature list request"); |
| 187 |
|
| 188 |
int size = 4 + 4 * (vmFeatures.length + FRAMEWORK_FEATURES.length); |
| 189 |
for (int i = vmFeatures.length-1; i >= 0; i--) |
| 190 |
size += vmFeatures[i].length() * 2; |
| 191 |
for (int i = FRAMEWORK_FEATURES.length-1; i>= 0; i--) |
| 192 |
size += FRAMEWORK_FEATURES[i].length() * 2; |
| 193 |
|
| 194 |
ByteBuffer out = ByteBuffer.allocate(size); |
| 195 |
out.order(ChunkHandler.CHUNK_ORDER); |
| 196 |
out.putInt(vmFeatures.length + FRAMEWORK_FEATURES.length); |
| 197 |
for (int i = vmFeatures.length-1; i >= 0; i--) { |
| 198 |
out.putInt(vmFeatures[i].length()); |
| 199 |
putString(out, vmFeatures[i]); |
| 200 |
} |
| 201 |
for (int i = FRAMEWORK_FEATURES.length-1; i >= 0; i--) { |
| 202 |
out.putInt(FRAMEWORK_FEATURES[i].length()); |
| 203 |
putString(out, FRAMEWORK_FEATURES[i]); |
| 204 |
} |
| 205 |
|
| 206 |
return new Chunk(CHUNK_FEAT, out); |
| 207 |
} |
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
public static void sendWAIT(int reason) { |
| 214 |
byte[] data = new byte[] { (byte) reason }; |
| 215 |
Chunk waitChunk = new Chunk(CHUNK_WAIT, data, 0, 1); |
| 216 |
DdmServer.sendChunk(waitChunk); |
| 217 |
} |
| 218 |
} |
| 219 |
|