| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.ddm; |
| 18 |
|
| 19 |
|
| 20 |
import android.os.Debug; |
| 21 |
import android.util.Log; |
| 22 |
|
| 23 |
import org.apache.harmony.dalvik.ddmc.Chunk; |
| 24 |
import org.apache.harmony.dalvik.ddmc.ChunkHandler; |
| 25 |
import org.apache.harmony.dalvik.ddmc.DdmServer; |
| 26 |
|
| 27 |
import java.nio.ByteBuffer; |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
public class DdmHandleProfiling extends DdmHandle { |
| 33 |
|
| 34 |
public static final int CHUNK_MPRS = ChunkHandler.type("MPRS"); |
| 35 |
public static final int CHUNK_MPRE = ChunkHandler.type("MPRE"); |
| 36 |
public static final int CHUNK_MPSS = ChunkHandler.type("MPSS"); |
| 37 |
public static final int CHUNK_MPSE = ChunkHandler.type("MPSE"); |
| 38 |
public static final int CHUNK_MPRQ = ChunkHandler.type("MPRQ"); |
| 39 |
public static final int CHUNK_SPSS = ChunkHandler.type("SPSS"); |
| 40 |
public static final int CHUNK_SPSE = ChunkHandler.type("SPSE"); |
| 41 |
|
| 42 |
private static final boolean DEBUG = false; |
| 43 |
private static DdmHandleProfiling mInstance = new DdmHandleProfiling(); |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
private DdmHandleProfiling() {} |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
public static void register() { |
| 53 |
DdmServer.registerHandler(CHUNK_MPRS, mInstance); |
| 54 |
DdmServer.registerHandler(CHUNK_MPRE, mInstance); |
| 55 |
DdmServer.registerHandler(CHUNK_MPSS, mInstance); |
| 56 |
DdmServer.registerHandler(CHUNK_MPSE, mInstance); |
| 57 |
DdmServer.registerHandler(CHUNK_MPRQ, mInstance); |
| 58 |
DdmServer.registerHandler(CHUNK_SPSS, mInstance); |
| 59 |
DdmServer.registerHandler(CHUNK_SPSE, mInstance); |
| 60 |
} |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
public void onConnected() {} |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
public void onDisconnected() {} |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
public Chunk handleChunk(Chunk request) { |
| 78 |
if (DEBUG) |
| 79 |
Log.v("ddm-heap", "Handling " + name(request.type) + " chunk"); |
| 80 |
int type = request.type; |
| 81 |
|
| 82 |
if (type == CHUNK_MPRS) { |
| 83 |
return handleMPRS(request); |
| 84 |
} else if (type == CHUNK_MPRE) { |
| 85 |
return handleMPRE(request); |
| 86 |
} else if (type == CHUNK_MPSS) { |
| 87 |
return handleMPSS(request); |
| 88 |
} else if (type == CHUNK_MPSE) { |
| 89 |
return handleMPSEOrSPSE(request, "Method"); |
| 90 |
} else if (type == CHUNK_MPRQ) { |
| 91 |
return handleMPRQ(request); |
| 92 |
} else if (type == CHUNK_SPSS) { |
| 93 |
return handleSPSS(request); |
| 94 |
} else if (type == CHUNK_SPSE) { |
| 95 |
return handleMPSEOrSPSE(request, "Sample"); |
| 96 |
} else { |
| 97 |
throw new RuntimeException("Unknown packet " + name(type)); |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
private Chunk handleMPRS(Chunk request) { |
| 105 |
ByteBuffer in = wrapChunk(request); |
| 106 |
|
| 107 |
int bufferSize = in.getInt(); |
| 108 |
int flags = in.getInt(); |
| 109 |
int len = in.getInt(); |
| 110 |
String fileName = getString(in, len); |
| 111 |
if (DEBUG) |
| 112 |
Log.v("ddm-heap", "Method profiling start: filename='" + fileName |
| 113 |
+ "', size=" + bufferSize + ", flags=" + flags); |
| 114 |
|
| 115 |
try { |
| 116 |
Debug.startMethodTracing(fileName, bufferSize, flags); |
| 117 |
return null; |
| 118 |
} catch (RuntimeException re) { |
| 119 |
return createFailChunk(1, re.getMessage()); |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
private Chunk handleMPRE(Chunk request) { |
| 127 |
byte result; |
| 128 |
|
| 129 |
try { |
| 130 |
Debug.stopMethodTracing(); |
| 131 |
result = 0; |
| 132 |
} catch (RuntimeException re) { |
| 133 |
Log.w("ddm-heap", "Method profiling end failed: " |
| 134 |
+ re.getMessage()); |
| 135 |
result = 1; |
| 136 |
} |
| 137 |
|
| 138 |
|
| 139 |
byte[] reply = { result }; |
| 140 |
return new Chunk(CHUNK_MPRE, reply, 0, reply.length); |
| 141 |
} |
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
private Chunk handleMPSS(Chunk request) { |
| 147 |
ByteBuffer in = wrapChunk(request); |
| 148 |
|
| 149 |
int bufferSize = in.getInt(); |
| 150 |
int flags = in.getInt(); |
| 151 |
if (DEBUG) { |
| 152 |
Log.v("ddm-heap", "Method prof stream start: size=" + bufferSize |
| 153 |
+ ", flags=" + flags); |
| 154 |
} |
| 155 |
|
| 156 |
try { |
| 157 |
Debug.startMethodTracingDdms(bufferSize, flags, false, 0); |
| 158 |
return null; |
| 159 |
} catch (RuntimeException re) { |
| 160 |
return createFailChunk(1, re.getMessage()); |
| 161 |
} |
| 162 |
} |
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
private Chunk handleMPSEOrSPSE(Chunk request, String type) { |
| 169 |
if (DEBUG) { |
| 170 |
Log.v("ddm-heap", type + " prof stream end"); |
| 171 |
} |
| 172 |
|
| 173 |
try { |
| 174 |
Debug.stopMethodTracing(); |
| 175 |
} catch (RuntimeException re) { |
| 176 |
Log.w("ddm-heap", type + " prof stream end failed: " |
| 177 |
+ re.getMessage()); |
| 178 |
return createFailChunk(1, re.getMessage()); |
| 179 |
} |
| 180 |
|
| 181 |
|
| 182 |
return null; |
| 183 |
} |
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
private Chunk handleMPRQ(Chunk request) { |
| 189 |
int result = Debug.getMethodTracingMode(); |
| 190 |
|
| 191 |
|
| 192 |
byte[] reply = { (byte) result }; |
| 193 |
return new Chunk(CHUNK_MPRQ, reply, 0, reply.length); |
| 194 |
} |
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
private Chunk handleSPSS(Chunk request) { |
| 200 |
ByteBuffer in = wrapChunk(request); |
| 201 |
|
| 202 |
int bufferSize = in.getInt(); |
| 203 |
int flags = in.getInt(); |
| 204 |
int interval = in.getInt(); |
| 205 |
if (DEBUG) { |
| 206 |
Log.v("ddm-heap", "Sample prof stream start: size=" + bufferSize |
| 207 |
+ ", flags=" + flags + ", interval=" + interval); |
| 208 |
} |
| 209 |
|
| 210 |
try { |
| 211 |
Debug.startMethodTracingDdms(bufferSize, flags, true, interval); |
| 212 |
return null; |
| 213 |
} catch (RuntimeException re) { |
| 214 |
return createFailChunk(1, re.getMessage()); |
| 215 |
} |
| 216 |
} |
| 217 |
} |
| 218 |
|