Richard Uhler | b730b78 | 2015-07-15 16:01:58 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 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 com.android.ahat; |
| 18 | |
| 19 | import com.android.tools.perflib.heap.ArrayInstance; |
| 20 | import com.android.tools.perflib.heap.ClassInstance; |
| 21 | import com.android.tools.perflib.heap.ClassObj; |
Richard Uhler | 1a5baaa | 2015-12-21 12:47:26 -0800 | [diff] [blame] | 22 | import com.android.tools.perflib.heap.Heap; |
Gus Smith | e6a6387 | 2016-06-09 16:50:44 -0700 | [diff] [blame] | 23 | import com.android.tools.perflib.heap.Instance; |
Richard Uhler | b730b78 | 2015-07-15 16:01:58 -0700 | [diff] [blame] | 24 | import com.android.tools.perflib.heap.Type; |
Gus Smith | e6a6387 | 2016-06-09 16:50:44 -0700 | [diff] [blame] | 25 | |
Richard Uhler | b730b78 | 2015-07-15 16:01:58 -0700 | [diff] [blame] | 26 | import java.awt.image.BufferedImage; |
| 27 | |
| 28 | /** |
| 29 | * Utilities for extracting information from hprof instances. |
| 30 | */ |
| 31 | class InstanceUtils { |
| 32 | /** |
| 33 | * Returns true if the given instance is an instance of a class with the |
| 34 | * given name. |
| 35 | */ |
Richard Uhler | 1a5baaa | 2015-12-21 12:47:26 -0800 | [diff] [blame] | 36 | private static boolean isInstanceOfClass(Instance inst, String className) { |
Richard Uhler | 3524472 | 2015-09-10 16:45:54 -0700 | [diff] [blame] | 37 | ClassObj cls = (inst == null) ? null : inst.getClassObj(); |
Richard Uhler | b730b78 | 2015-07-15 16:01:58 -0700 | [diff] [blame] | 38 | return (cls != null && className.equals(cls.getClassName())); |
| 39 | } |
| 40 | |
| 41 | /** |
Richard Uhler | b730b78 | 2015-07-15 16:01:58 -0700 | [diff] [blame] | 42 | * Read the byte[] value from an hprof Instance. |
| 43 | * Returns null if the instance is not a byte array. |
| 44 | */ |
| 45 | private static byte[] asByteArray(Instance inst) { |
Gus Smith | e6a6387 | 2016-06-09 16:50:44 -0700 | [diff] [blame] | 46 | if (!(inst instanceof ArrayInstance)) { |
Richard Uhler | b730b78 | 2015-07-15 16:01:58 -0700 | [diff] [blame] | 47 | return null; |
| 48 | } |
| 49 | |
Gus Smith | e6a6387 | 2016-06-09 16:50:44 -0700 | [diff] [blame] | 50 | ArrayInstance array = (ArrayInstance) inst; |
Richard Uhler | b730b78 | 2015-07-15 16:01:58 -0700 | [diff] [blame] | 51 | if (array.getArrayType() != Type.BYTE) { |
| 52 | return null; |
| 53 | } |
| 54 | |
| 55 | Object[] objs = array.getValues(); |
| 56 | byte[] bytes = new byte[objs.length]; |
| 57 | for (int i = 0; i < objs.length; i++) { |
Gus Smith | e6a6387 | 2016-06-09 16:50:44 -0700 | [diff] [blame] | 58 | Byte b = (Byte) objs[i]; |
Richard Uhler | b730b78 | 2015-07-15 16:01:58 -0700 | [diff] [blame] | 59 | bytes[i] = b.byteValue(); |
| 60 | } |
| 61 | return bytes; |
| 62 | } |
| 63 | |
| 64 | |
Richard Uhler | 77ff54b | 2015-08-31 10:22:56 -0700 | [diff] [blame] | 65 | /** |
| 66 | * Read the string value from an hprof Instance. |
| 67 | * Returns null if the object can't be interpreted as a string. |
| 68 | */ |
Richard Uhler | b730b78 | 2015-07-15 16:01:58 -0700 | [diff] [blame] | 69 | public static String asString(Instance inst) { |
Richard Uhler | 77ff54b | 2015-08-31 10:22:56 -0700 | [diff] [blame] | 70 | return asString(inst, -1); |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Read the string value from an hprof Instance. |
| 75 | * Returns null if the object can't be interpreted as a string. |
| 76 | * The returned string is truncated to maxChars characters. |
| 77 | * If maxChars is negative, the returned string is not truncated. |
| 78 | */ |
| 79 | public static String asString(Instance inst, int maxChars) { |
Richard Uhler | c7f7712 | 2016-02-11 08:48:24 -0800 | [diff] [blame] | 80 | // The inst object could either be a java.lang.String or a char[]. If it |
| 81 | // is a char[], use that directly as the value, otherwise use the value |
| 82 | // field of the string object. The field accesses for count and offset |
| 83 | // later on will work okay regardless of what type the inst object is. |
| 84 | Object value = inst; |
| 85 | if (isInstanceOfClass(inst, "java.lang.String")) { |
| 86 | value = getField(inst, "value"); |
Richard Uhler | b730b78 | 2015-07-15 16:01:58 -0700 | [diff] [blame] | 87 | } |
Richard Uhler | 788b21e | 2015-08-31 10:33:56 -0700 | [diff] [blame] | 88 | |
Richard Uhler | 788b21e | 2015-08-31 10:33:56 -0700 | [diff] [blame] | 89 | if (!(value instanceof ArrayInstance)) { |
| 90 | return null; |
| 91 | } |
| 92 | |
| 93 | ArrayInstance chars = (ArrayInstance) value; |
| 94 | if (chars.getArrayType() != Type.CHAR) { |
| 95 | return null; |
| 96 | } |
| 97 | |
| 98 | // TODO: When perflib provides a better way to get the length of the |
| 99 | // array, we should use that here. |
| 100 | int numChars = chars.getValues().length; |
| 101 | int count = getIntField(inst, "count", numChars); |
Richard Uhler | 788b21e | 2015-08-31 10:33:56 -0700 | [diff] [blame] | 102 | if (count == 0) { |
| 103 | return ""; |
| 104 | } |
Richard Uhler | 77ff54b | 2015-08-31 10:22:56 -0700 | [diff] [blame] | 105 | if (0 <= maxChars && maxChars < count) { |
| 106 | count = maxChars; |
| 107 | } |
Richard Uhler | 788b21e | 2015-08-31 10:33:56 -0700 | [diff] [blame] | 108 | |
Richard Uhler | 77ff54b | 2015-08-31 10:22:56 -0700 | [diff] [blame] | 109 | int offset = getIntField(inst, "offset", 0); |
| 110 | int end = offset + count - 1; |
Richard Uhler | 788b21e | 2015-08-31 10:33:56 -0700 | [diff] [blame] | 111 | if (offset >= 0 && offset < numChars && end >= 0 && end < numChars) { |
| 112 | return new String(chars.asCharArray(offset, count)); |
| 113 | } |
| 114 | return null; |
Richard Uhler | b730b78 | 2015-07-15 16:01:58 -0700 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | /** |
| 118 | * Read the bitmap data for the given android.graphics.Bitmap object. |
| 119 | * Returns null if the object isn't for android.graphics.Bitmap or the |
| 120 | * bitmap data couldn't be read. |
| 121 | */ |
| 122 | public static BufferedImage asBitmap(Instance inst) { |
| 123 | if (!isInstanceOfClass(inst, "android.graphics.Bitmap")) { |
| 124 | return null; |
| 125 | } |
| 126 | |
Richard Uhler | 1a5baaa | 2015-12-21 12:47:26 -0800 | [diff] [blame] | 127 | Integer width = getIntField(inst, "mWidth", null); |
Richard Uhler | b730b78 | 2015-07-15 16:01:58 -0700 | [diff] [blame] | 128 | if (width == null) { |
| 129 | return null; |
| 130 | } |
| 131 | |
Richard Uhler | 1a5baaa | 2015-12-21 12:47:26 -0800 | [diff] [blame] | 132 | Integer height = getIntField(inst, "mHeight", null); |
Richard Uhler | b730b78 | 2015-07-15 16:01:58 -0700 | [diff] [blame] | 133 | if (height == null) { |
| 134 | return null; |
| 135 | } |
| 136 | |
| 137 | byte[] buffer = getByteArrayField(inst, "mBuffer"); |
| 138 | if (buffer == null) { |
| 139 | return null; |
| 140 | } |
| 141 | |
| 142 | // Convert the raw data to an image |
| 143 | // Convert BGRA to ABGR |
| 144 | int[] abgr = new int[height * width]; |
| 145 | for (int i = 0; i < abgr.length; i++) { |
| 146 | abgr[i] = ( |
Gus Smith | e6a6387 | 2016-06-09 16:50:44 -0700 | [diff] [blame] | 147 | (((int) buffer[i * 4 + 3] & 0xFF) << 24) |
| 148 | + (((int) buffer[i * 4 + 0] & 0xFF) << 16) |
| 149 | + (((int) buffer[i * 4 + 1] & 0xFF) << 8) |
| 150 | + ((int) buffer[i * 4 + 2] & 0xFF)); |
Richard Uhler | b730b78 | 2015-07-15 16:01:58 -0700 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | BufferedImage bitmap = new BufferedImage( |
| 154 | width, height, BufferedImage.TYPE_4BYTE_ABGR); |
| 155 | bitmap.setRGB(0, 0, width, height, abgr, 0, width); |
| 156 | return bitmap; |
| 157 | } |
| 158 | |
| 159 | /** |
| 160 | * Read a field of an instance. |
| 161 | * Returns null if the field value is null or if the field couldn't be read. |
| 162 | */ |
Richard Uhler | 3524472 | 2015-09-10 16:45:54 -0700 | [diff] [blame] | 163 | public static Object getField(Instance inst, String fieldName) { |
Richard Uhler | b730b78 | 2015-07-15 16:01:58 -0700 | [diff] [blame] | 164 | if (!(inst instanceof ClassInstance)) { |
| 165 | return null; |
| 166 | } |
| 167 | |
| 168 | ClassInstance clsinst = (ClassInstance) inst; |
| 169 | Object value = null; |
| 170 | int count = 0; |
| 171 | for (ClassInstance.FieldValue field : clsinst.getValues()) { |
| 172 | if (fieldName.equals(field.getField().getName())) { |
| 173 | value = field.getValue(); |
| 174 | count++; |
| 175 | } |
| 176 | } |
| 177 | return count == 1 ? value : null; |
| 178 | } |
| 179 | |
| 180 | /** |
| 181 | * Read a reference field of an instance. |
| 182 | * Returns null if the field value is null, or if the field couldn't be read. |
| 183 | */ |
| 184 | private static Instance getRefField(Instance inst, String fieldName) { |
| 185 | Object value = getField(inst, fieldName); |
| 186 | if (!(value instanceof Instance)) { |
| 187 | return null; |
| 188 | } |
Gus Smith | e6a6387 | 2016-06-09 16:50:44 -0700 | [diff] [blame] | 189 | return (Instance) value; |
Richard Uhler | b730b78 | 2015-07-15 16:01:58 -0700 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | /** |
| 193 | * Read an int field of an instance. |
| 194 | * The field is assumed to be an int type. |
Richard Uhler | 1a5baaa | 2015-12-21 12:47:26 -0800 | [diff] [blame] | 195 | * Returns <code>def</code> if the field value is not an int or could not be |
| 196 | * read. |
Richard Uhler | b730b78 | 2015-07-15 16:01:58 -0700 | [diff] [blame] | 197 | */ |
Richard Uhler | 1a5baaa | 2015-12-21 12:47:26 -0800 | [diff] [blame] | 198 | private static Integer getIntField(Instance inst, String fieldName, Integer def) { |
Richard Uhler | b730b78 | 2015-07-15 16:01:58 -0700 | [diff] [blame] | 199 | Object value = getField(inst, fieldName); |
| 200 | if (!(value instanceof Integer)) { |
Richard Uhler | 1a5baaa | 2015-12-21 12:47:26 -0800 | [diff] [blame] | 201 | return def; |
Richard Uhler | b730b78 | 2015-07-15 16:01:58 -0700 | [diff] [blame] | 202 | } |
Gus Smith | e6a6387 | 2016-06-09 16:50:44 -0700 | [diff] [blame] | 203 | return (Integer) value; |
Richard Uhler | b730b78 | 2015-07-15 16:01:58 -0700 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | /** |
Richard Uhler | 1a5baaa | 2015-12-21 12:47:26 -0800 | [diff] [blame] | 207 | * Read a long field of an instance. |
| 208 | * The field is assumed to be a long type. |
| 209 | * Returns <code>def</code> if the field value is not an long or could not |
| 210 | * be read. |
Richard Uhler | 788b21e | 2015-08-31 10:33:56 -0700 | [diff] [blame] | 211 | */ |
Richard Uhler | 1a5baaa | 2015-12-21 12:47:26 -0800 | [diff] [blame] | 212 | private static Long getLongField(Instance inst, String fieldName, Long def) { |
| 213 | Object value = getField(inst, fieldName); |
| 214 | if (!(value instanceof Long)) { |
| 215 | return def; |
| 216 | } |
Gus Smith | e6a6387 | 2016-06-09 16:50:44 -0700 | [diff] [blame] | 217 | return (Long) value; |
Richard Uhler | 788b21e | 2015-08-31 10:33:56 -0700 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | /** |
Richard Uhler | b730b78 | 2015-07-15 16:01:58 -0700 | [diff] [blame] | 221 | * Read the given field from the given instance. |
| 222 | * The field is assumed to be a byte[] field. |
| 223 | * Returns null if the field value is null, not a byte[] or could not be read. |
| 224 | */ |
| 225 | private static byte[] getByteArrayField(Instance inst, String fieldName) { |
| 226 | Object value = getField(inst, fieldName); |
| 227 | if (!(value instanceof Instance)) { |
| 228 | return null; |
| 229 | } |
Gus Smith | e6a6387 | 2016-06-09 16:50:44 -0700 | [diff] [blame] | 230 | return asByteArray((Instance) value); |
Richard Uhler | b730b78 | 2015-07-15 16:01:58 -0700 | [diff] [blame] | 231 | } |
| 232 | |
Richard Uhler | b730b78 | 2015-07-15 16:01:58 -0700 | [diff] [blame] | 233 | // Return the bitmap instance associated with this object, or null if there |
| 234 | // is none. This works for android.graphics.Bitmap instances and their |
| 235 | // underlying Byte[] instances. |
| 236 | public static Instance getAssociatedBitmapInstance(Instance inst) { |
| 237 | ClassObj cls = inst.getClassObj(); |
| 238 | if (cls == null) { |
| 239 | return null; |
| 240 | } |
| 241 | |
| 242 | if ("android.graphics.Bitmap".equals(cls.getClassName())) { |
| 243 | return inst; |
| 244 | } |
| 245 | |
| 246 | if (inst instanceof ArrayInstance) { |
Gus Smith | e6a6387 | 2016-06-09 16:50:44 -0700 | [diff] [blame] | 247 | ArrayInstance array = (ArrayInstance) inst; |
Richard Uhler | 6919a01 | 2015-12-15 09:15:00 -0800 | [diff] [blame] | 248 | if (array.getArrayType() == Type.BYTE && inst.getHardReverseReferences().size() == 1) { |
| 249 | Instance ref = inst.getHardReverseReferences().get(0); |
Richard Uhler | b730b78 | 2015-07-15 16:01:58 -0700 | [diff] [blame] | 250 | ClassObj clsref = ref.getClassObj(); |
| 251 | if (clsref != null && "android.graphics.Bitmap".equals(clsref.getClassName())) { |
| 252 | return ref; |
| 253 | } |
| 254 | } |
| 255 | } |
| 256 | return null; |
| 257 | } |
| 258 | |
Richard Uhler | b357730 | 2015-10-29 13:02:42 -0700 | [diff] [blame] | 259 | private static boolean isJavaLangRefReference(Instance inst) { |
| 260 | ClassObj cls = (inst == null) ? null : inst.getClassObj(); |
| 261 | while (cls != null) { |
| 262 | if ("java.lang.ref.Reference".equals(cls.getClassName())) { |
| 263 | return true; |
| 264 | } |
| 265 | cls = cls.getSuperClassObj(); |
| 266 | } |
| 267 | return false; |
| 268 | } |
| 269 | |
| 270 | public static Instance getReferent(Instance inst) { |
| 271 | if (isJavaLangRefReference(inst)) { |
| 272 | return getRefField(inst, "referent"); |
| 273 | } |
| 274 | return null; |
| 275 | } |
| 276 | |
Richard Uhler | b730b78 | 2015-07-15 16:01:58 -0700 | [diff] [blame] | 277 | /** |
| 278 | * Assuming inst represents a DexCache object, return the dex location for |
| 279 | * that dex cache. Returns null if the given instance doesn't represent a |
| 280 | * DexCache object or the location could not be found. |
Richard Uhler | 77ff54b | 2015-08-31 10:22:56 -0700 | [diff] [blame] | 281 | * If maxChars is non-negative, the returned location is truncated to |
| 282 | * maxChars in length. |
Richard Uhler | b730b78 | 2015-07-15 16:01:58 -0700 | [diff] [blame] | 283 | */ |
Richard Uhler | 77ff54b | 2015-08-31 10:22:56 -0700 | [diff] [blame] | 284 | public static String getDexCacheLocation(Instance inst, int maxChars) { |
Richard Uhler | b730b78 | 2015-07-15 16:01:58 -0700 | [diff] [blame] | 285 | if (isInstanceOfClass(inst, "java.lang.DexCache")) { |
| 286 | Instance location = getRefField(inst, "location"); |
| 287 | if (location != null) { |
Richard Uhler | 77ff54b | 2015-08-31 10:22:56 -0700 | [diff] [blame] | 288 | return asString(location, maxChars); |
Richard Uhler | b730b78 | 2015-07-15 16:01:58 -0700 | [diff] [blame] | 289 | } |
| 290 | } |
| 291 | return null; |
| 292 | } |
Richard Uhler | 1a5baaa | 2015-12-21 12:47:26 -0800 | [diff] [blame] | 293 | |
| 294 | public static class NativeAllocation { |
| 295 | public long size; |
| 296 | public Heap heap; |
| 297 | public long pointer; |
| 298 | public Instance referent; |
| 299 | |
| 300 | public NativeAllocation(long size, Heap heap, long pointer, Instance referent) { |
| 301 | this.size = size; |
| 302 | this.heap = heap; |
| 303 | this.pointer = pointer; |
| 304 | this.referent = referent; |
| 305 | } |
| 306 | } |
| 307 | |
| 308 | /** |
| 309 | * Assuming inst represents a NativeAllocation, return information about the |
| 310 | * native allocation. Returns null if the given instance doesn't represent a |
| 311 | * native allocation. |
| 312 | */ |
| 313 | public static NativeAllocation getNativeAllocation(Instance inst) { |
| 314 | if (!isInstanceOfClass(inst, "libcore.util.NativeAllocationRegistry$CleanerThunk")) { |
| 315 | return null; |
| 316 | } |
| 317 | |
| 318 | Long pointer = InstanceUtils.getLongField(inst, "nativePtr", null); |
| 319 | if (pointer == null) { |
| 320 | return null; |
| 321 | } |
| 322 | |
| 323 | // Search for the registry field of inst. |
| 324 | // Note: We know inst as an instance of ClassInstance because we already |
| 325 | // read the nativePtr field from it. |
| 326 | Instance registry = null; |
Gus Smith | e6a6387 | 2016-06-09 16:50:44 -0700 | [diff] [blame] | 327 | for (ClassInstance.FieldValue field : ((ClassInstance) inst).getValues()) { |
Richard Uhler | 1a5baaa | 2015-12-21 12:47:26 -0800 | [diff] [blame] | 328 | Object fieldValue = field.getValue(); |
| 329 | if (fieldValue instanceof Instance) { |
Gus Smith | e6a6387 | 2016-06-09 16:50:44 -0700 | [diff] [blame] | 330 | Instance fieldInst = (Instance) fieldValue; |
Richard Uhler | 1a5baaa | 2015-12-21 12:47:26 -0800 | [diff] [blame] | 331 | if (isInstanceOfClass(fieldInst, "libcore.util.NativeAllocationRegistry")) { |
| 332 | registry = fieldInst; |
| 333 | break; |
| 334 | } |
| 335 | } |
| 336 | } |
| 337 | |
| 338 | if (registry == null) { |
| 339 | return null; |
| 340 | } |
| 341 | |
| 342 | Long size = InstanceUtils.getLongField(registry, "size", null); |
| 343 | if (size == null) { |
| 344 | return null; |
| 345 | } |
| 346 | |
| 347 | Instance referent = null; |
Richard Uhler | 6919a01 | 2015-12-15 09:15:00 -0800 | [diff] [blame] | 348 | for (Instance ref : inst.getHardReverseReferences()) { |
Richard Uhler | 1a5baaa | 2015-12-21 12:47:26 -0800 | [diff] [blame] | 349 | if (isInstanceOfClass(ref, "sun.misc.Cleaner")) { |
| 350 | referent = InstanceUtils.getReferent(ref); |
| 351 | if (referent != null) { |
| 352 | break; |
| 353 | } |
| 354 | } |
| 355 | } |
| 356 | |
| 357 | if (referent == null) { |
| 358 | return null; |
| 359 | } |
| 360 | return new NativeAllocation(size, inst.getHeap(), pointer, referent); |
| 361 | } |
Richard Uhler | b730b78 | 2015-07-15 16:01:58 -0700 | [diff] [blame] | 362 | } |