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